GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-admin-metaboxes.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Register and render the admin metaboxes for GravityView
5  */
7 
8  static $metaboxes_dir;
9 
10  /**
11  * @var int The post ID of the current View
12  */
13  var $post_id = 0;
14 
15  /**
16  *
17  */
18  function __construct() {
19 
21  return;
22  }
23 
24  self::$metaboxes_dir = GRAVITYVIEW_DIR . 'includes/admin/metaboxes/';
25 
26  include_once self::$metaboxes_dir . 'class-gravityview-metabox-tab.php';
27 
28  include_once self::$metaboxes_dir . 'class-gravityview-metabox-tabs.php';
29 
30  $this->initialize();
31 
32  }
33 
34  /**
35  * Add WordPress hooks
36  * @since 1.7.2
37  */
38  function initialize() {
39 
40  add_action( 'add_meta_boxes', array( $this, 'register_metaboxes' ) );
41 
42  add_action( 'add_meta_boxes_gravityview', array( $this, 'update_priority' ) );
43 
44  // information box
45  add_action( 'post_submitbox_misc_actions', array( $this, 'render_shortcode_hint' ) );
46  }
47 
48  /**
49  * GravityView wants to have the top (`normal`) metaboxes all to itself, so we move all plugin/theme metaboxes down to `advanced`
50  * @since 1.15.2
51  */
52  function update_priority() {
53  global $wp_meta_boxes;
54 
55  if ( ! empty( $wp_meta_boxes['gravityview'] ) ) {
56  foreach ( array( 'high', 'core', 'low' ) as $position ) {
57  if ( isset( $wp_meta_boxes['gravityview']['normal'][ $position ] ) ) {
58  foreach ( $wp_meta_boxes['gravityview']['normal'][ $position ] as $key => $meta_box ) {
59  if ( ! preg_match( '/^gravityview_/ism', $key ) ) {
60  $wp_meta_boxes['gravityview']['advanced'][ $position ][ $key ] = $meta_box;
61  unset( $wp_meta_boxes['gravityview']['normal'][ $position ][ $key ] );
62  }
63  }
64  }
65  }
66  }
67  }
68 
69  function register_metaboxes() {
70  global $post;
71 
72  // On Comment Edit, for example, $post isn't set.
73  if ( empty( $post ) || ! is_object( $post ) || ! isset( $post->ID ) ) {
74  return;
75  }
76 
77  // select data source for this view
78  add_meta_box( 'gravityview_select_form', $this->get_data_source_header( $post->ID ), array( $this, 'render_data_source_metabox' ), 'gravityview', 'normal', 'high' );
79 
80  // select view type/template
81  add_meta_box( 'gravityview_select_template', __( 'Choose a View Type', 'gk-gravityview' ), array( $this, 'render_select_template_metabox' ), 'gravityview', 'normal', 'high' );
82 
83  // View Configuration box
84  add_meta_box( 'gravityview_view_config', __( 'Layout', 'gk-gravityview' ), array( $this, 'render_view_configuration_metabox' ), 'gravityview', 'normal', 'high' );
85 
87 
88  // Other Settings box
89  add_meta_box( 'gravityview_settings', __( 'Settings', 'gk-gravityview' ), array( $this, 'settings_metabox_render' ), 'gravityview', 'normal', 'core' );
90 
91  }
92 
93  /**
94  * Render the View Settings metabox
95  * @since 1.8
96  * @param WP_Post $post
97  */
99 
100  /**
101  * @action `gravityview/metaboxes/before_render` Before rendering GravityView metaboxes
102  * @since 1.8
103  * @param WP_Post $post
104  */
105  do_action( 'gravityview/metaboxes/before_render', $post );
106 
107  $metaboxes = GravityView_Metabox_Tabs::get_all();
108 
109  include self::$metaboxes_dir . 'views/gravityview-navigation.php';
110  include self::$metaboxes_dir . 'views/gravityview-content.php';
111 
112  /**
113  * @action `gravityview/metaboxes/after_render` After rendering GravityView metaboxes
114  * @since 1.8
115  * @param WP_Post $post
116  */
117  do_action( 'gravityview/metaboxes/after_render', $post );
118  }
119 
120  /**
121  * Add default tabs to the Settings metabox
122  * @since 1.8
123  */
124  private function add_settings_metabox_tabs() {
125 
126  $metaboxes = array(
127  array(
128  'id' => 'template_settings',
129  'title' => __( 'View Settings', 'gk-gravityview' ),
130  'file' => 'view-settings.php',
131  'icon-class' => 'dashicons-admin-generic',
132  'callback' => '',
133  'callback_args' => '',
134  ),
135  array(
136  'id' => 'multiple_entries',
137  'title' => __( 'Multiple Entries', 'gk-gravityview' ),
138  'file' => 'multiple-entries.php',
139  'icon-class' => 'dashicons-admin-page',
140  'callback' => '',
141  'callback_args' => '',
142  ),
143  array(
144  'id' => 'single_entry', // Use the same ID as View Settings for backward compatibility
145  'title' => __( 'Single Entry', 'gk-gravityview' ),
146  'file' => 'single-entry.php',
147  'icon-class' => 'dashicons-media-default',
148  'callback' => '',
149  'callback_args' => '',
150  ),
151  array(
152  'id' => 'edit_entry', // Use the same ID as View Settings for backward compatibility
153  'title' => __( 'Edit Entry', 'gk-gravityview' ),
154  'file' => 'edit-entry.php',
155  'icon-class' => 'dashicons-welcome-write-blog',
156  'callback' => '',
157  'callback_args' => '',
158  ),
159  array(
160  'id' => 'delete_entry',
161  'title' => __( 'Delete Entry', 'gk-gravityview' ),
162  'file' => 'delete-entry.php',
163  'icon-class' => 'dashicons-trash',
164  'callback' => '',
165  'callback_args' => '',
166  ),
167  array(
168  'id' => 'sort_filter',
169  'title' => __( 'Filter &amp; Sort', 'gk-gravityview' ),
170  'file' => 'sort-filter.php',
171  'icon-class' => 'dashicons-sort',
172  'callback' => '',
173  'callback_args' => '',
174  ),
175  array(
176  'id' => 'permissions', // Use the same ID as View Settings for backward compatibility
177  'title' => __( 'Permissions', 'gk-gravityview' ),
178  'file' => 'permissions.php',
179  'icon-class' => 'dashicons-lock',
180  'callback' => '',
181  'callback_args' => '',
182  ),
183  );
184 
185  /**
186  * @filter `gravityview/metaboxes/default` Modify the default settings metabox tabs
187  * @param array $metaboxes
188  * @since 1.8
189  */
190  $metaboxes = apply_filters( 'gravityview/metaboxes/default', $metaboxes );
191 
192  foreach ( $metaboxes as $m ) {
193 
194  $tab = new GravityView_Metabox_Tab( $m['id'], $m['title'], $m['file'], $m['icon-class'], $m['callback'], $m['callback_args'] );
195 
197 
198  }
199 
200  unset( $tab );
201 
202  }
203 
204  /**
205  * Generate the title for Data Source, which includes the Action Links once configured.
206  *
207  * @since 1.8
208  *
209  * @param int $post_id ID of the current post
210  *
211  * @return string "Data Source", plus links if any
212  */
213  private function get_data_source_header( $post_id ) {
214  /**
215  * This method is running before GravityView's been fully set up; likely being called by another plugin.
216  * @see https://github.com/gravityview/GravityView/issues/1684
217  */
218  if ( ! class_exists( 'GravityView_Admin_Views' ) ) {
219  return __( 'Data Source', 'gk-gravityview' );
220  }
221 
223 
225 
226  if ( ! empty( $links ) ) {
227  $links = '<span class="alignright gv-form-links">' . $links . '</span>';
228  }
229 
230  $output = $links;;
231 
232  if ( ! $current_form ) {
233  // Starting from GF 2.6, GF's form_admin.js script requires window.form and window.gf_vars objects to be set when any element has a .merge-tag-support class.
234  // Since we don't yet have a form when creating a new View, we need to mock those objects.
235  $_id = isset( $_GET['id'] ) ? $_GET['id'] : null;
236  $_GET['id'] = -1; // This is needed for GFCommon::gf_vars() to return the mergeTags property.
237 
238  $output .= sprintf(
239  '<script type="text/javascript">var form = %s; %s</script>',
240  '{fields: []}',
241  GFCommon::gf_vars( false )
242  );
243 
244  $_GET['id'] = $_id;
245  }
246 
247  return __( 'Data Source', 'gk-gravityview' ) . $output;
248  }
249 
250  /**
251  * Render html for 'select form' metabox
252  *
253  * @param object $post
254  * @return void
255  */
256  public function render_data_source_metabox( $post ) {
257 
258  include self::$metaboxes_dir . 'views/data-source.php';
259 
260  }
261 
262  /**
263  * Render html for 'select template' metabox
264  *
265  * @param object $post
266  * @return void
267  */
269 
270  include self::$metaboxes_dir . 'views/select-template.php';
271  }
272 
273  /**
274  * Generate the script tags necessary for the Gravity Forms Merge Tag picker to work.
275  *
276  * @param int $curr_form Form ID
277  * @return null|string Merge tags html; NULL if $curr_form isn't defined.
278  */
279  public static function render_merge_tags_scripts( $curr_form ) {
280 
281  if ( empty( $curr_form ) ) {
282  return null;
283  }
284 
286 
287  $get_id_backup = isset( $_GET['id'] ) ? $_GET['id'] : null;
288 
289  if ( isset( $form['id'] ) ) {
290  $form_script = 'var form = ' . GFCommon::json_encode( $form ) . ';';
291 
292  // The `gf_vars()` method needs a $_GET[id] variable set with the form ID.
293  $_GET['id'] = $form['id'];
294 
295  } else {
296  $form_script = 'var form = new Form();';
297  }
298 
299  $output = '<script type="text/javascript" data-gv-merge-tags="1">' . $form_script . "\n" . GFCommon::gf_vars( false ) . '</script>';
300 
301  // Restore previous $_GET setting
302  $_GET['id'] = $get_id_backup;
303 
304  return $output;
305  }
306 
307  /**
308  * Render html for 'View Configuration' metabox
309  *
310  * @param mixed $post
311  * @return void
312  */
314 
315  // Use nonce for verification
316  wp_nonce_field( 'gravityview_view_configuration', 'gravityview_view_configuration_nonce' );
317 
318  // Selected Form
320 
321  $view = \GV\View::from_post( $post );
322 
323  // Selected template
324  $curr_template = gravityview_get_template_id( $post->ID );
325 
326  echo self::render_merge_tags_scripts( $curr_form );
327 
328  include self::$metaboxes_dir . 'views/view-configuration.php';
329  }
330 
331  /**
332  * Render html View General Settings
333  *
334  * @param object $post
335  * @return void
336  */
338 
339  // View template settings
341 
342  include self::$metaboxes_dir . 'views/view-settings.php';
343 
344  }
345 
346 
347 
348  /**
349  * Render shortcode hint in the Publish metabox
350  *
351  * @return void
352  */
354  global $post;
355 
356  // Only show this on GravityView post types.
357  if ( false === gravityview()->request->is_admin( '', null ) ) {
358  return;
359  }
360 
361  // If the View hasn't been configured yet, don't show embed shortcode
362  if ( ! gravityview_get_directory_fields( $post->ID ) && ! gravityview_get_directory_widgets( $post->ID ) ) {
363  return;
364  }
365 
366  include self::$metaboxes_dir . 'views/shortcode-hint.php';
367  }
368 
369 }
370 
const GRAVITYVIEW_DIR
"GRAVITYVIEW_DIR" "./" The absolute path to the plugin directory, with trailing slash ...
Definition: gravityview.php:49
static get_all()
Get array of all registered metaboxes.
static render_merge_tags_scripts( $curr_form)
Generate the script tags necessary for the Gravity Forms Merge Tag picker to work.
render_view_settings_metabox( $post)
Render html View General Settings.
render_view_configuration_metabox( $post)
Render html for &#39;View Configuration&#39; metabox.
static get_connected_form_links( $form, $include_form_link=true)
Get HTML links relating to a connected form, like Edit, Entries, Settings, Preview.
gravityview_get_form( $form_id)
Returns the form object for a given Form ID.
gravityview_get_directory_widgets( $post_id)
Get the widgets, as configured for a View.
gravityview_get_template_settings( $post_id)
Get all the settings for a View.
$curr_form
gravityview_get_directory_fields( $post_id, $apply_filter=true, $form_id=0)
Get the field configuration for the View.
get_data_source_header( $post_id)
Generate the title for Data Source, which includes the Action Links once configured.
static is_valid()
Is everything compatible with this version of GravityView?
render_select_template_metabox( $post)
Render html for &#39;select template&#39; metabox.
global $post
Definition: delete-entry.php:7
update_priority()
GravityView wants to have the top (normal) metaboxes all to itself, so we move all plugin/theme metab...
if(gravityview() ->plugin->is_GF_25()) $form
$current_settings
gravityview_get_form_id( $view_id)
Get the connected form ID from a View ID.
The class for a metabox tab in the GravityView View Settings metabox.
settings_metabox_render( $post)
Render the View Settings metabox.
Register and render the admin metaboxes for GravityView.
gravityview()
The main GravityView wrapper function.
render_data_source_metabox( $post)
Render html for &#39;select form&#39; metabox.
gravityview_get_template_id( $post_id)
Get the template ID (list, table, datatables, map) for a View.
add_settings_metabox_tabs()
Add default tabs to the Settings metabox.
render_shortcode_hint()
Render shortcode hint in the Publish metabox.
$current_form
Definition: data-source.php:13
static add(GravityView_Metabox_Tab $meta_box)
Add a tab.
static from_post( $post)
Construct a instance from a .