GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-extension.php
Go to the documentation of this file.
1 <?php
2 namespace GV;
3 
4 /** If this file is called directly, abort. */
5 if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6  die();
7 }
8 
9 /**
10  * The \GV\Extension class.
11  *
12  * An interface that most extensions would want to adhere to and inherit from.
13  *
14  * @deprecated 2.16.1
15  *
16  * @TODO Remove once all extensions have been updated to use Foundation.
17  */
18 abstract class Extension {
19  /**
20  * @var string Name of the plugin in gravitykit.com
21  */
22  protected $_title = NULL;
23 
24  /**
25  * @var string Version number of the plugin
26  */
27  protected $_version = NULL;
28 
29  /**
30  * @var int The ID of the download on gravitykit.com
31  * @since 1.1
32  */
33  protected $_item_id = NULL;
34 
35  /**
36  * @var string Translation textdomain
37  */
38  protected $_text_domain = 'gravityview';
39 
40  /**
41  * @var string Minimum version of GravityView the Extension requires
42  */
43  protected $_min_gravityview_version = '2.0-dev';
44 
45  /**
46  * @var string Maximum version of GravityView the Extension requires, if any
47  */
48  protected $_max_gravityview_version = null;
49 
50  /**
51  * @var string Minimum version of GravityView the Extension requires
52  */
53  protected $_min_php_version = '5.6.4';
54 
55  /**
56  * @var string Author of plugin, sent when fetching license info.
57  */
58  protected $_author = 'Katz Web Services, Inc.';
59 
60  /**
61  * @var string The path to the extension.
62  */
63  protected $_path = '';
64 
65  /**
66  * @var array Admin notices to display
67  */
68  static protected $admin_notices = array();
69 
70  /**
71  * @var boolean[] An array of extension compatibility.
72  * @since 2.0 This is an array of classes instead.
73  */
74  static public $is_compatible = array();
75 
76  /**
77  * Generic initialization.
78  */
79  public function __construct() {
80  if ( false === $this->is_extension_supported() ) {
81  return;
82  }
83 
84  if ( ! $this->_path ) {
85  $this->_path = __FILE__;
86  }
87 
88  add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
89 
90  add_action( 'admin_notices', array( $this, 'admin_notice' ), 100 );
91 
92  // Save the view configuration. Run at 14 so that View metadata is already saved (at 10)
93  add_action( 'save_post', array( $this, 'save_post' ), 14 );
94 
95  add_action( 'gravityview/metaboxes/before_render', array( $this, 'add_metabox_tab' ) );
96 
97  add_filter( 'gravityview/metaboxes/tooltips', array( $this, 'tooltips' ) );
98 
99  $this->add_hooks();
100  }
101 
102  /**
103  * Load translations for the extension
104  *
105  * 1. Check `wp-content/languages/gravityview/` folder and load using `load_textdomain()`
106  * 2. Check `wp-content/plugins/gravityview/languages/` folder for `gravityview-[locale].mo` file and load using `load_textdomain()`
107  * 3. Load default file using `load_plugin_textdomain()` from `wp-content/plugins/gravityview/languages/`
108  *
109  * @return void
110  */
111  public function load_plugin_textdomain() {
112  if ( empty( $this->_text_domain ) ) {
113  gravityview()->log->debug( 'Extension translation cannot be loaded; the `_text_domain` variable is not defined', array( 'data' => $this ) );
114  return;
115  }
116 
117  // Backward compat for Ratings & Reviews / Maps
118  $path = isset( $this->_path ) ? $this->_path : ( isset( $this->plugin_file ) ? $this->plugin_file : '' );
119 
120  // Set filter for plugin's languages directory
121  $lang_dir = dirname( plugin_basename( $path ) ) . '/languages/';
122 
123  $locale = get_locale();
124 
125  if ( function_exists('get_user_locale') && is_admin() ) {
126  $locale = get_user_locale();
127  }
128 
129  // Traditional WordPress plugin locale filter
130  $locale = apply_filters( 'plugin_locale', $locale, $this->_text_domain );
131 
132  $mofile = sprintf( '%1$s-%2$s.mo', $this->_text_domain, $locale );
133 
134  // Setup paths to current locale file
135  $mofile_local = $lang_dir . $mofile;
136  $mofile_global = WP_LANG_DIR . '/' . $this->_text_domain . '/' . $mofile;
137 
138  if ( file_exists( $mofile_global ) ) {
139  // Look in global /wp-content/languages/[plugin-dir]/ folder
140  load_textdomain( $this->_text_domain, $mofile_global );
141  } elseif ( file_exists( $mofile_local ) ) {
142  // Look in local /wp-content/plugins/[plugin-dir]/languages/ folder
143  load_textdomain( $this->_text_domain, $mofile_local );
144  } else {
145  // Load the default language files
146  load_plugin_textdomain( $this->_text_domain, false, $lang_dir );
147  }
148  }
149 
150  /**
151  * Extensions should override this hook to add their hooks.
152  *
153  * @return void
154  */
155  public function add_hooks() { }
156 
157  /**
158  * Save extra view configuration.
159  *
160  * @param int $post_id Post ID
161  * @return void
162  */
163  public function save_post( $post_id ) { }
164 
165  /**
166  * Add tooltips for the extension.
167  *
168  * Add a tooltip with an array using the `title` and `value` keys. The `title` key is the H6 tag value of the tooltip; it's the headline. The `value` is the tooltip content, and can contain any HTML.
169  *
170  * The tooltip key must be `gv_{name_of_setting}`. If the name of the setting is "example_extension_setting", the code would be:
171  *
172  * <code>
173  * $tooltips['gv_example_extension_setting'] = array(
174  * 'title' => 'About Example Extension Setting',
175  * 'value' => 'When you do [x] with [y], [z] happens.'
176  * );
177  * </code>
178  *
179  * @param array $tooltips Existing GV tooltips, with `title` and `value` keys
180  * @return array Modified tooltips
181  */
182  public function tooltips( $tooltips = array() ) {
183  return $tooltips;
184  }
185 
186  /**
187  * Add a tab to GravityView Edit View tabbed metabox. By overriding this method, you will add a tab to View settings
188  *
189  * @since 1.8 (Extension version 1.0.7)
190  * @see https://gist.github.com/zackkatz/6cc381bcf54849f2ed41 For example of adding a metabox
191  *
192  * @return array Array of metabox
193  */
194  protected function tab_settings() {
195  // When overriding, return array with expected keys
196  return array();
197  }
198 
199  /**
200  * If Extension overrides tab_settings() and passes its own tab, add it to the tabbed settings metabox
201  *
202  * @since 1.8 (Extension version 1.0.7)
203  *
204  * @return void
205  */
206  public function add_metabox_tab() {
207  $tab_settings = $this->tab_settings();
208 
209  // Don't add a tab if it's empty.
210  if ( empty( $tab_settings ) ) {
211  return;
212  }
213 
214  $tab_defaults = array(
215  'id' => '',
216  'title' => '',
217  'callback' => '',
218  'icon-class' => '',
219  'file' => '',
220  'callback_args' => '',
221  'context' => 'side',
222  'priority' => 'default',
223  );
224 
225  $tab = wp_parse_args( $tab_settings, $tab_defaults );
226 
227  // Force the screen to be GravityView
228  $tab['screen'] = 'gravityview';
229 
230  if ( class_exists( 'GravityView_Metabox_Tab' ) ) {
231  $metabox = new \GravityView_Metabox_Tab( $tab['id'], $tab['title'], $tab['file'], $tab['icon-class'], $tab['callback'], $tab['callback_args'] );
232  \GravityView_Metabox_Tabs::add( $metabox );
233  } else {
234  add_meta_box( 'gravityview_' . $tab['id'], $tab['title'], $tab['callback'], $tab['screen'], $tab['context'], $tab['priority'] );
235  }
236  }
237 
238  /**
239  * Is this extension even compatible?
240  *
241  * @return boolean|null Is or is not. Null if unknown yet.
242  */
243  public static function is_compatible() {
244  return Utils::get( self::$is_compatible, get_called_class(), null );
245  }
246 
247  /**
248  * Check whether the extension is supported:
249  *
250  * - Checks if GravityView and Gravity Forms exist
251  * - Checks GravityView and Gravity Forms version numbers
252  * - Checks PHP version numbers
253  * - Sets self::$is_compatible[__CLASS__] to boolean value
254  *
255  * @return boolean Is the extension supported?
256  */
257  protected function is_extension_supported() {
258 
259  self::$is_compatible = is_array( self::$is_compatible ) ? self::$is_compatible : array( get_called_class() => (bool) self::$is_compatible );
260 
261  if ( ! function_exists( 'gravityview' ) ) {
262  $message = sprintf( __( 'Could not activate the %s Extension; GravityView is not active.', 'gk-gravityview' ), esc_html( $this->_title ) );
263  } else if ( false === version_compare( Plugin::$version, $this->_min_gravityview_version , ">=" ) ) {
264  $message = sprintf( __( 'The %s Extension requires GravityView Version %s or newer.', 'gk-gravityview' ), esc_html( $this->_title ), '<tt>'.$this->_min_gravityview_version.'</tt>' );
265  } else if ( isset( $this->_min_php_version ) && false === version_compare( phpversion(), $this->_min_php_version , ">=" ) ) {
266  $message = sprintf( __( 'The %s Extension requires PHP Version %s or newer. Please ask your host to upgrade your server\'s PHP.', 'gk-gravityview' ), esc_html( $this->_title ), '<tt>'.$this->_min_php_version.'</tt>' );
267  } else if ( ! empty( $this->_max_gravityview_version ) && false === version_compare( $this->_max_gravityview_version, Plugin::$version, ">" ) ) {
268  $message = sprintf( __( 'The %s Extension is not compatible with this version of GravityView. Please update the Extension to the latest version.', 'gk-gravityview' ), esc_html( $this->_title ) );
269  } else {
270  $message = '';
271  self::$is_compatible[ get_called_class() ] = gravityview()->plugin->is_compatible();
272  }
273 
274  if ( ! empty( $message ) ) {
275  self::add_notice( $message );
276  self::$is_compatible[ get_called_class() ] = false;
277  gravityview()->log->error( '{message}', array( 'message' => $message ) );
278  }
279 
280  return self::is_compatible();
281  }
282 
283  /**
284  * Add a notice to be displayed in the admin.
285  *
286  * @param array $notice Array with `class` and `message` keys. The message is not escaped.
287  *
288  * @return void
289  */
290  public static function add_notice( $notice = array() ) {
291 
292  if ( is_array( $notice ) && empty( $notice['message'] ) ) {
293  gravityview()->log->error( 'Notice not set', array( 'data' => $notice ) );
294  return;
295  } else if ( is_string( $notice ) ) {
296  $notice = array( 'message' => $notice );
297  }
298 
299  $notice['class'] = empty( $notice['class'] ) ? 'error' : $notice['class'];
300 
301  self::$admin_notices []= $notice;
302  }
303 
304  /**
305  * Outputs the admin notices generated by the all plugins
306  *
307  * @return void
308  */
309  public function admin_notice() {
310  if ( empty( self::$admin_notices ) ) {
311  return;
312  }
313 
314  foreach ( self::$admin_notices as $key => $notice ) {
315  echo '<div id="message" class="'. esc_attr( $notice['class'] ).'">';
316  echo wpautop( $notice['message'] );
317  echo '<div class="clear"></div>';
318  echo '</div>';
319  }
320 
321  //reset the notices handler
322  self::$admin_notices = array();
323  }
324 }
If this file is called directly, abort.
save_post( $post_id)
Save extra view configuration.
static add_notice( $notice=array())
Add a notice to be displayed in the admin.
static is_compatible()
Is this extension even compatible?
is_extension_supported()
Check whether the extension is supported:
load_plugin_textdomain()
Load translations for the extension.
tab_settings()
Add a tab to GravityView Edit View tabbed metabox.
add_hooks()
Extensions should override this hook to add their hooks.
gravityview()
The main GravityView wrapper function.
__construct()
Generic initialization.
add_metabox_tab()
If Extension overrides tab_settings() and passes its own tab, add it to the tabbed settings metabox...
static add(GravityView_Metabox_Tab $meta_box)
Add a tab.
admin_notice()
Outputs the admin notices generated by the all plugins.
tooltips( $tooltips=array())
Add tooltips for the extension.