Completed
Push — master ( 18187e...a1257b )
by Zack
19:22 queued 17:03
created

GravityView_Admin_Metaboxes::update_priority()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 0
dl 0
loc 16
ccs 0
cts 15
cp 0
crap 42
rs 9.1111
c 0
b 0
f 0
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 with message: 1.19.4

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
98
99
		/**
100
		 * @action `gravityview/metaboxes/before_render` Before rendering GravityView metaboxes
101
		 * @since 1.8
102
		 * @param WP_Post $post
103
		 */
104
		do_action( 'gravityview/metaboxes/before_render', $post );
105
106
		$metaboxes = GravityView_Metabox_Tabs::get_all();
107
108
		include self::$metaboxes_dir . 'views/gravityview-navigation.php';
109
		include self::$metaboxes_dir . 'views/gravityview-content.php';
110
111
		/**
112
		 * @action `gravityview/metaboxes/after_render` After rendering GravityView metaboxes
113
		 * @since 1.8
114
		 * @param WP_Post $post
115
		 */
116
		do_action( 'gravityview/metaboxes/after_render', $post );
117
	}
118
119
	/**
120
	 * Add default tabs to the Settings metabox
121
	 * @since 1.8
122
	 */
123
	private function add_settings_metabox_tabs() {
124
125
		$metaboxes = array(
126
			array(
127
				'id' => 'template_settings',
128
				'title' => __( 'View Settings', 'gravityview' ),
129
				'file' => 'view-settings.php',
130
				'icon-class' => 'dashicons-admin-generic',
131
				'callback' => '',
132
				'callback_args' => '',
133
			),
134
			array(
135
				'id' => 'multiple_entries',
136
				'title' => __( 'Multiple Entries', 'gravityview' ),
137
				'file' => 'multiple-entries.php',
138
				'icon-class' => 'dashicons-admin-page',
139
				'callback' => '',
140
				'callback_args' => '',
141
			),
142
			array(
143
				'id' => 'single_entry', // Use the same ID as View Settings for backward compatibility
144
				'title' => __( 'Single Entry', 'gravityview' ),
145
				'file' => 'single-entry.php',
146
				'icon-class' => 'dashicons-media-default',
147
				'callback' => '',
148
				'callback_args' => '',
149
			),
150
			array(
151
				'id' => 'edit_entry', // Use the same ID as View Settings for backward compatibility
152
				'title' => __( 'Edit Entry', 'gravityview' ),
153
				'file' => 'edit-entry.php',
154
				'icon-class' => 'dashicons-welcome-write-blog',
155
				'callback' => '',
156
				'callback_args' => '',
157
			),
158
			array(
159
				'id' => 'sort_filter',
160
				'title' => __( 'Filter &amp; Sort', 'gravityview' ),
161
				'file' => 'sort-filter.php',
162
				'icon-class' => 'dashicons-sort',
163
				'callback' => '',
164
				'callback_args' => '',
165
			),
166
			array(
167
				'id' => 'permissions', // Use the same ID as View Settings for backward compatibility
168
				'title' => __( 'Permissions', 'gravityview' ),
169
				'file' => 'permissions.php',
170
				'icon-class' => 'dashicons-lock',
171
				'callback' => '',
172
				'callback_args' => '',
173
			),
174
		);
175
176
		/**
177
		 * @filter `gravityview/metaboxes/default` Modify the default settings metabox tabs
178
		 * @param array $metaboxes
179
		 * @since 1.8
180
		 */
181
		$metaboxes = apply_filters( 'gravityview/metaboxes/default', $metaboxes );
182
183
		foreach( $metaboxes as $m ) {
184
185
			$tab = new GravityView_Metabox_Tab( $m['id'], $m['title'], $m['file'], $m['icon-class'], $m['callback'], $m['callback_args'] );
186
187
			GravityView_Metabox_Tabs::add( $tab );
188
189
		}
190
191
		unset( $tab );
192
193
	}
194
195
	/**
196
	 * Generate the title for Data Source, which includes the Action Links once configured.
197
	 *
198
	 * @since 1.8
199
	 *
200
	 * @param int $post_id ID of the current post
201
	 *
202
	 * @return string "Data Source", plus links if any
203
	 */
204
	private function get_data_source_header( $post_id ) {
205
206
		//current value
207
		$current_form = gravityview_get_form_id( $post_id );
208
209
		$links = GravityView_Admin_Views::get_connected_form_links( $current_form, false );
210
211
		if( !empty( $links ) ) {
212
			$links = '<span class="alignright gv-form-links">'. $links .'</span>';
213
		}
214
215
		return __( 'Data Source', 'gravityview' ) . $links;
216
	}
217
218
	/**
219
	 * Render html for 'select form' metabox
220
	 *
221
	 * @param object $post
222
	 * @return void
223
	 */
224
	public function render_data_source_metabox( $post ) {
225
226
		include self::$metaboxes_dir . 'views/data-source.php';
227
228
	}
229
230
	/**
231
	 * Render html for 'select template' metabox
232
	 *
233
	 * @param object $post
234
	 * @return void
235
	 */
236
	public function render_select_template_metabox( $post ) {
237
238
		include self::$metaboxes_dir . 'views/select-template.php';
239
	}
240
241
	/**
242
	 * Generate the script tags necessary for the Gravity Forms Merge Tag picker to work.
243
	 *
244
	 * @param  int      $curr_form Form ID
245
	 * @return null|string     Merge tags html; NULL if $curr_form isn't defined.
246
	 */
247
	public static function render_merge_tags_scripts( $curr_form ) {
248
249
		if( empty( $curr_form )) {
250
			return NULL;
251
		}
252
253
		$form = gravityview_get_form( $curr_form );
254
255
		$get_id_backup = isset($_GET['id']) ? $_GET['id'] : NULL;
256
257
		if( isset( $form['id'] ) ) {
258
		    $form_script = 'var form = ' . GFCommon::json_encode($form) . ';';
259
260
		    // The `gf_vars()` method needs a $_GET[id] variable set with the form ID.
261
		    $_GET['id'] = $form['id'];
262
263
		} else {
264
		    $form_script = 'var form = new Form();';
265
		}
266
267
		$output = '<script type="text/javascript" data-gv-merge-tags="1">' . $form_script . "\n" . GFCommon::gf_vars(false) . '</script>';
268
269
		// Restore previous $_GET setting
270
		$_GET['id'] = $get_id_backup;
271
272
		return $output;
273
	}
274
275
	/**
276
	 * Render html for 'View Configuration' metabox
277
	 *
278
	 * @access public
279
	 * @param mixed $post
280
	 * @return void
281
	 */
282
	function render_view_configuration_metabox( $post ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
283
284
		// Use nonce for verification
285
		wp_nonce_field( 'gravityview_view_configuration', 'gravityview_view_configuration_nonce' );
286
287
		// Selected Form
288
		$curr_form = gravityview_get_form_id( $post->ID );
289
290
		$view = \GV\View::from_post( $post );
291
292
		// Selected template
293
		$curr_template = gravityview_get_template_id( $post->ID );
294
295
		echo self::render_merge_tags_scripts( $curr_form );
296
297
		include self::$metaboxes_dir . 'views/view-configuration.php';
298
	}
299
300
	/**
301
	 * Render html View General Settings
302
	 *
303
	 * @access public
304
	 * @param object $post
305
	 * @return void
306
	 */
307
	function render_view_settings_metabox( $post ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
308
309
		// View template settings
310
		$current_settings = gravityview_get_template_settings( $post->ID );
311
312
		include self::$metaboxes_dir . 'views/view-settings.php';
313
314
	}
315
316
317
318
	/**
319
	 * Render shortcode hint in the Publish metabox
320
	 *
321
	 * @access public
322
	 * @return void
323
	 */
324
	function render_shortcode_hint() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
325
		global $post;
326
327
		// Only show this on GravityView post types.
328
		if( false === gravityview()->request->is_admin( '', null ) ) {
0 ignored issues
show
Unused Code introduced by
The call to Frontend_Request::is_admin() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to Request::is_admin() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
329
			return;
330
		}
331
332
		// If the View hasn't been configured yet, don't show embed shortcode
333
		if( ! gravityview_get_directory_fields( $post->ID ) && ! gravityview_get_directory_widgets( $post->ID ) ) {
334
			return;
335
		}
336
337
		include self::$metaboxes_dir . 'views/shortcode-hint.php';
338
	}
339
340
}
341
342
new GravityView_Admin_Metaboxes;
343