Completed
Push — develop ( faed85...5d0bd3 )
by Zack
04:39
created

metaboxes/class-gravityview-admin-metaboxes.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Register and render the admin metaboxes for GravityView
5
 */
6
class GravityView_Admin_Metaboxes {
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
20
		if( !GravityView_Compatibility::is_valid() ) { return; }
0 ignored issues
show
Deprecated Code introduced by
The method GravityView_Compatibility::is_valid() has been deprecated.

This method has been deprecated.

Loading history...
21
22
        self::$metaboxes_dir = GRAVITYVIEW_DIR . 'includes/admin/metaboxes/';
23
24
		include_once self::$metaboxes_dir . 'class-gravityview-metabox-tab.php';
25
26
		include_once self::$metaboxes_dir . 'class-gravityview-metabox-tabs.php';
27
28
		$this->initialize();
29
30
	}
31
32
	/**
33
	 * Add WordPress hooks
34
	 * @since 1.7.2
35
	 */
36
	function initialize() {
37
38
		add_action( 'add_meta_boxes', array( $this, 'register_metaboxes' ));
39
40
		add_action( 'add_meta_boxes_gravityview' , array( $this, 'update_priority' ) );
41
42
		// information box
43
		add_action( 'post_submitbox_misc_actions', array( $this, 'render_shortcode_hint' ) );
44
45
	}
46
47
	/**
48
	 * GravityView wants to have the top (`normal`) metaboxes all to itself, so we move all plugin/theme metaboxes down to `advanced`
49
	 * @since 1.15.2
50
	 */
51
	function update_priority() {
52
		global $wp_meta_boxes;
53
54
		if( ! empty( $wp_meta_boxes['gravityview'] ) ) {
55
			foreach( array( 'high', 'core', 'low' ) as $position ) {
56
				if( isset( $wp_meta_boxes['gravityview']['normal'][ $position ] ) ) {
57
					foreach( $wp_meta_boxes['gravityview']['normal'][ $position ] as $key => $meta_box ) {
58
						if( ! preg_match( '/^gravityview_/ism', $key ) ) {
59
							$wp_meta_boxes['gravityview']['advanced'][ $position ][ $key ] = $meta_box;
60
							unset( $wp_meta_boxes['gravityview']['normal'][ $position ][ $key ] );
61
						}
62
					}
63
				}
64
			}
65
		}
66
	}
67
68
	function register_metaboxes() {
69
		global $post;
70
71
		// On Comment Edit, for example, $post isn't set.
72
		if( empty( $post ) || !is_object( $post ) || !isset( $post->ID ) ) {
73
			return;
74
		}
75
76
		// select data source for this view
77
		add_meta_box( 'gravityview_select_form', $this->get_data_source_header( $post->ID ), array( $this, 'render_data_source_metabox' ), 'gravityview', 'normal', 'high' );
78
79
		// select view type/template
80
		add_meta_box( 'gravityview_select_template', __( 'Choose a View Type', 'gravityview' ), array( $this, 'render_select_template_metabox' ), 'gravityview', 'normal', 'high' );
81
82
		// View Configuration box
83
		add_meta_box( 'gravityview_view_config', __( 'View Configuration', 'gravityview' ), array( $this, 'render_view_configuration_metabox' ), 'gravityview', 'normal', 'high' );
84
85
		$this->add_settings_metabox_tabs();
86
87
		// Other Settings box
88
		add_meta_box( 'gravityview_settings', __( 'View Settings', 'gravityview' ), array( $this, 'settings_metabox_render' ), 'gravityview', 'normal', 'core' );
89
90
	}
91
92
	/**
93
	 * Render the View Settings metabox
94
	 * @since 1.8
95
	 * @param WP_Post $post
96
	 */
97
	function settings_metabox_render( $post ) {
98
99
		/**
100
		 * @param WP_Post $post
101
		 */
102
		do_action( 'gravityview/metaboxes/before_render', $post );
103
104
		$metaboxes = GravityView_Metabox_Tabs::get_all();
105
106
		include self::$metaboxes_dir . 'views/gravityview-navigation.php';
107
		include self::$metaboxes_dir . 'views/gravityview-content.php';
108
109
		/**
110
		 * @param WP_Post $post
111
		 */
112
		do_action( 'gravityview/metaboxes/after_render', $post );
113
	}
114
115
	/**
116
	 * Add default tabs to the Settings metabox
117
	 * @since 1.8
118
	 */
119
	private function add_settings_metabox_tabs() {
120
121
		$metaboxes = array(
122
			array(
123
				'id' => 'template_settings',
124
				'title' => __( 'View Settings', 'gravityview' ),
125
				'file' => 'view-settings.php',
126
				'icon-class' => 'dashicons-admin-generic',
127
				'callback' => '',
128
				'callback_args' => '',
129
			),
130
			array(
131
				'id' => 'single_entry', // Use the same ID as View Settings for backward compatibility
132
				'title' => __( 'Single Entry', 'gravityview' ),
133
				'file' => 'single-entry.php',
134
				'icon-class' => 'dashicons-media-default',
135
				'callback' => '',
136
				'callback_args' => '',
137
			),
138
			array(
139
				'id' => 'sort_filter',
140
				'title' => __( 'Filter &amp; Sort', 'gravityview' ),
141
				'file' => 'sort-filter.php',
142
				'icon-class' => 'dashicons-sort',
143
				'callback' => '',
144
				'callback_args' => '',
145
			),
146
		);
147
148
		/**
149
		 * @filter `gravityview/metaboxes/default` Modify the default settings metabox tabs
150
		 * @param array $metaboxes
151
		 * @since 1.8
152
		 */
153
		$metaboxes = apply_filters( 'gravityview/metaboxes/default', $metaboxes );
154
155
		foreach( $metaboxes as $m ) {
156
157
			$tab = new GravityView_Metabox_Tab( $m['id'], $m['title'], $m['file'], $m['icon-class'], $m['callback'], $m['callback_args'] );
158
159
			GravityView_Metabox_Tabs::add( $tab );
160
161
		}
162
163
		unset( $tab );
164
165
	}
166
167
	/**
168
	 * Generate the title for Data Source, which includes the Action Links once configured.
169
	 *
170
	 * @since 1.8
171
	 *
172
	 * @param int $post_id ID of the current post
173
	 *
174
	 * @return string "Data Source", plus links if any
175
	 */
176
	private function get_data_source_header( $post_id ) {
177
178
		//current value
179
		$current_form = gravityview_get_form_id( $post_id );
180
181
		$links = GravityView_Admin_Views::get_connected_form_links( $current_form, false );
182
183
		if( !empty( $links ) ) {
184
			$links = '<span class="alignright gv-form-links">'. $links .'</span>';
185
		}
186
187
		return __( 'Data Source', 'gravityview' ) . $links;
188
	}
189
190
	/**
191
	 * Render html for 'select form' metabox
192
	 *
193
	 * @access public
194
	 * @param object $post
195
	 * @return void
196
	 */
197
	function render_data_source_metabox( $post ) {
198
199
		include self::$metaboxes_dir . 'views/data-source.php';
200
201
	}
202
203
	/**
204
	 * Render html for 'select template' metabox
205
	 *
206
	 * @access public
207
	 * @param object $post
208
	 * @return void
209
	 */
210
	function render_select_template_metabox( $post ) {
211
212
		include self::$metaboxes_dir . 'views/select-template.php';
213
	}
214
215
	/**
216
	 * Generate the script tags necessary for the Gravity Forms Merge Tag picker to work.
217
	 *
218
	 * @param  int      $curr_form Form ID
219
	 * @return null|string     Merge tags html; NULL if $curr_form isn't defined.
220
	 */
221
	public static function render_merge_tags_scripts( $curr_form ) {
222
223
		if( empty( $curr_form )) {
224
			return NULL;
225
		}
226
227
		$form = gravityview_get_form( $curr_form );
228
229
		$get_id_backup = isset($_GET['id']) ? $_GET['id'] : NULL;
230
231
		if( isset( $form['id'] ) ) {
232
		    $form_script = 'var form = ' . GFCommon::json_encode($form) . ';';
233
234
		    // The `gf_vars()` method needs a $_GET[id] variable set with the form ID.
235
		    $_GET['id'] = $form['id'];
236
237
		} else {
238
		    $form_script = 'var form = new Form();';
239
		}
240
241
		$output = '<script type="text/javascript" data-gv-merge-tags="1">' . $form_script . "\n" . GFCommon::gf_vars(false) . '</script>';
242
243
		// Restore previous $_GET setting
244
		$_GET['id'] = $get_id_backup;
245
246
		return $output;
247
	}
248
249
	/**
250
	 * Render html for 'View Configuration' metabox
251
	 *
252
	 * @access public
253
	 * @param mixed $post
254
	 * @return void
255
	 */
256
	function render_view_configuration_metabox( $post ) {
257
258
		// Use nonce for verification
259
		wp_nonce_field( 'gravityview_view_configuration', 'gravityview_view_configuration_nonce' );
260
261
		// Selected Form
262
		$curr_form = gravityview_get_form_id( $post->ID );
263
264
		// Selected template
265
		$curr_template = gravityview_get_template_id( $post->ID );
266
267
		echo self::render_merge_tags_scripts( $curr_form );
268
269
		include self::$metaboxes_dir . 'views/view-configuration.php';
270
	}
271
272
	/**
273
	 * Render html View General Settings
274
	 *
275
	 * @access public
276
	 * @param object $post
277
	 * @return void
278
	 */
279
	function render_view_settings_metabox( $post ) {
280
281
		// View template settings
282
		$current_settings = gravityview_get_template_settings( $post->ID );
283
284
		include self::$metaboxes_dir . 'views/view-settings.php';
285
286
	}
287
288
289
290
	/**
291
	 * Render shortcode hint in the Publish metabox
292
	 *
293
	 * @access public
294
	 * @return void
295
	 */
296
	function render_shortcode_hint() {
297
		global $post;
298
299
		// Only show this on GravityView post types.
300
		if( false === gravityview_is_admin_page() ) { return; }
301
302
		// If the View hasn't been configured yet, don't show embed shortcode
303
		if( !gravityview_get_directory_fields( $post->ID ) ) { return; }
304
305
		include self::$metaboxes_dir . 'views/shortcode-hint.php';
306
	}
307
308
}
309
310
new GravityView_Admin_Metaboxes;
311