acf_form_post::validate_page()   B
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 12
nop 0
dl 0
loc 49
rs 7.8682
c 0
b 0
f 0
1
<?php
2
3
/*
4
*  ACF Post Form Class
5
*
6
*  All the logic for adding fields to posts
7
*
8
*  @class 		acf_form_post
9
*  @package		ACF
10
*  @subpackage	Forms
11
*/
12
13
if( ! class_exists('acf_form_post') ) :
14
15
class acf_form_post {
16
	
17
	var $post_id	= 0,
18
		$typenow	= '',
19
		$style		= '';
20
	
21
	
22
	/*
23
	*  __construct
24
	*
25
	*  This function will setup the class functionality
26
	*
27
	*  @type	function
28
	*  @date	5/03/2014
29
	*  @since	5.0.0
30
	*
31
	*  @param	n/a
32
	*  @return	n/a
33
	*/
0 ignored issues
show
Documentation introduced by
The doc-type n/a could not be parsed: Unknown type name "n/a" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
34
	
35 View Code Duplication
	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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
	
37
		// actions
38
		add_action('admin_enqueue_scripts',				array($this, 'admin_enqueue_scripts'));
39
		
40
		
41
		// save
42
		add_filter('wp_insert_post_empty_content',		array($this, 'wp_insert_post_empty_content'), 10, 2);
43
		add_action('save_post', 						array($this, 'save_post'), 10, 2);
44
		
45
		
46
		// ajax
47
		add_action('wp_ajax_acf/post/get_field_groups',	array($this, 'get_field_groups'));
48
		
49
	}
50
	
51
	
52
	/*
53
	*  validate_page
54
	*
55
	*  This function will check if the current page is for a post/page edit form
56
	*
57
	*  @type	function
58
	*  @date	23/06/12
59
	*  @since	3.1.8
60
	*
61
	*  @param	n/a
62
	*  @return	(boolean)
63
	*/
64
	
65
	function validate_page() {
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...
66
		
67
		// global
68
		global $post, $pagenow, $typenow;
69
		
70
		
71
		// vars
72
		$return = false;
73
		
74
		
75
		// validate page
76
		if( in_array($pagenow, array('post.php', 'post-new.php')) ) {
77
			
78
			$return = true;
79
			
80
		}
81
		
82
		
83
		// update vars
84
		if( !empty($post) ) {
85
		
86
			$this->post_id = $post->ID;
87
			$this->typenow = $typenow;
88
			
89
		}
90
		
91
		
92
		// validate post type
93
		if( in_array($typenow, array('acf-field-group', 'attachment')) ) {
94
			
95
			return false;
96
			
97
		}
98
		
99
		
100
		// validate page (Shopp)
101
		if( $pagenow == "admin.php" && isset( $_GET['page'] ) && $_GET['page'] == "shopp-products" && isset( $_GET['id'] ) ) {
102
			
103
			$return = true;
104
			
105
			$this->post_id = absint( $_GET['id'] );
106
			$this->typenow = 'shopp_product';
107
			
108
		}
109
				
110
		
111
		// return
112
		return $return;
113
	}
114
	
115
	
116
	/*
117
	*  admin_enqueue_scripts
118
	*
119
	*  This action is run after post query but before any admin script / head actions. 
120
	*  It is a good place to register all actions.
121
	*
122
	*  @type	action (admin_enqueue_scripts)
123
	*  @date	26/01/13
124
	*  @since	3.6.0
125
	*
126
	*  @param	n/a
127
	*  @return	n/a
128
	*/
0 ignored issues
show
Documentation introduced by
The doc-type n/a could not be parsed: Unknown type name "n/a" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
129
	
130 View Code Duplication
	function admin_enqueue_scripts() {
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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
		
132
		// validate page
133
		if( !$this->validate_page() ) return;
134
135
		
136
		// load acf scripts
137
		acf_enqueue_scripts();
138
		
139
		
140
		// actions
141
		add_action('acf/input/admin_head',		array($this,'admin_head'));
142
		add_action('acf/input/admin_footer',	array($this,'admin_footer'));
143
	}
144
	
145
	
146
	/*
147
	*  admin_head
148
	*
149
	*  This action will find and add field groups to the current edit page
150
	*
151
	*  @type	action (admin_head)
152
	*  @date	23/06/12
153
	*  @since	3.1.8
154
	*
155
	*  @param	n/a
156
	*  @return	n/a
157
	*/
0 ignored issues
show
Documentation introduced by
The doc-type n/a could not be parsed: Unknown type name "n/a" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
158
	
159
	function admin_head() {
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...
160
		
161
		// vars
162
		$style_found = false;
163
		
164
		
165
		// get field groups
166
		$field_groups = acf_get_field_groups();
167
		
168
		
169
		// add meta boxes
170
		if( !empty($field_groups) ) {
171
			
172
			foreach( $field_groups as $i => $field_group ) {
173
				
174
				// vars
175
				$id = "acf-{$field_group['key']}";
176
				$title = $field_group['title'];
177
				$context = $field_group['position'];
178
				$priority = 'high';
179
				$args = array( 
180
					'field_group'	=> $field_group,
181
					'visibility'	=> false
182
				);
183
				
184
				
185
				// tweaks to vars
186
				if( $context == 'side' ) {
187
					
188
					$priority = 'core';
189
				
190
				}
191
				
192
				
193
				// filter for 3rd party customization
194
				$priority = apply_filters('acf/input/meta_box_priority', $priority, $field_group);
195
				
196
				
197
				// visibility
198
				$args['visibility'] = acf_get_field_group_visibility( $field_group, array(
199
					'post_id'	=> $this->post_id, 
200
					'post_type'	=> $this->typenow
201
				));
202
				
203
				
204
				// add meta box
205
				add_meta_box( $id, $title, array($this, 'render_meta_box'), $this->typenow, $context, $priority, $args );
206
				
207
				
208
				// update style
209
				if( !$style_found && $args['visibility'] ) {
210
					
211
					$style_found = true;
212
					
213
					$this->style = acf_get_field_group_style( $field_group );
214
					
215
				}
216
				
217
			}
218
			
219
		}
220
				
221
		
222
		// Allow 'acf_after_title' metabox position
223
		add_action('edit_form_after_title', array($this, 'edit_form_after_title'));
224
		
225
		
226
		// remove ACF from meta postbox
227
		add_filter('is_protected_meta', array($this, 'is_protected_meta'), 10, 3);
228
	}
229
	
230
	
231
	/*
232
	*  edit_form_after_title
233
	*
234
	*  This action will allow ACF to render metaboxes after the title
235
	*
236
	*  @type	action
237
	*  @date	17/08/13
238
	*
239
	*  @param	n/a
240
	*  @return	n/a
241
	*/
0 ignored issues
show
Documentation introduced by
The doc-type n/a could not be parsed: Unknown type name "n/a" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
242
	
243
	function edit_form_after_title() {
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...
244
		
245
		// globals
246
		global $post, $wp_meta_boxes;
247
		
248
		
249
		// render post data
250
		acf_form_data(array( 
251
			'post_id'	=> $this->post_id, 
252
			'nonce'		=> 'post',
253
			'ajax'		=> 1
254
		));
255
		
256
		
257
		// render
258
		do_meta_boxes( get_current_screen(), 'acf_after_title', $post);
259
		
260
		
261
		// clean up
262
		unset( $wp_meta_boxes['post']['acf_after_title'] );
263
264
	}
265
	
266
	
267
	/*
268
	*  render_meta_box
269
	*
270
	*  description
271
	*
272
	*  @type	function
273
	*  @date	20/10/13
274
	*  @since	5.0.0
275
	*
276
	*  @param	$post_id (int)
277
	*  @return	$post_id (int)
278
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
279
	
280
	function render_meta_box( $post, $args ) {
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...
281
		
282
		// extract args
283
		extract( $args ); // all variables from the add_meta_box function
284
		extract( $args ); // all variables from the args argument
285
		
286
		
287
		// vars
288
		$o = array(
289
			'id'			=> $id,
290
			'key'			=> $field_group['key'],
291
			'style'			=> $field_group['style'],
292
			'label'			=> $field_group['label_placement'],
293
			'edit_url'		=> '',
294
			'edit_title'	=> __('Edit field group', 'acf'),
295
			'visibility'	=> $visibility
296
		);
297
		
298
		
299
		// edit_url
300 View Code Duplication
		if( $field_group['ID'] && acf_current_user_can_admin() ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
301
			
302
			$o['edit_url'] = admin_url('post.php?post=' . $field_group['ID'] . '&action=edit');
303
				
304
		}
305
		
306
			
307
		// load and render fields	
308
		if( $visibility ) {
309
			
310
			// load fields
311
			$fields = acf_get_fields( $field_group );
312
			
313
			
314
			// render
315
			acf_render_fields( $this->post_id, $fields, 'div', $field_group['instruction_placement'] );
316
		
317
		// render replace-me div
318
		} else {
319
			
320
			echo '<div class="acf-replace-with-fields"><div class="acf-loading"></div></div>';
321
		
322
		}
323
	
324
	?>
325
<script type="text/javascript">
326
if( typeof acf !== 'undefined' ) {
327
		
328
	acf.postbox.render(<?php echo json_encode($o); ?>);	
329
330
}
331
</script>
332
<?php
333
		
334
	}
335
	
336
	
337
	/*
338
	*  admin_footer
339
	*
340
	*  description
341
	*
342
	*  @type	function
343
	*  @date	21/10/13
344
	*  @since	5.0.0
345
	*
346
	*  @param	$post_id (int)
347
	*  @return	$post_id (int)
348
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
349
	
350
	function admin_footer(){
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...
351
		
352
		// get style of first field group
353
		echo '<style type="text/css" id="acf-style">' . $this->style . '</style>';
354
		
355
	}
356
	
357
	
358
	/*
359
	*  get_field_groups
360
	*
361
	*  This function will return all the JSON data needed to render new metaboxes
362
	*
363
	*  @type	function
364
	*  @date	21/10/13
365
	*  @since	5.0.0
366
	*
367
	*  @param	n/a
368
	*  @return	n/a
369
	*/
0 ignored issues
show
Documentation introduced by
The doc-type n/a could not be parsed: Unknown type name "n/a" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
370
371
	function get_field_groups() {
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...
372
		
373
		// options
374
		$options = acf_parse_args($_POST, array(
375
			'nonce'		=> '',
376
			'post_id'	=> 0,
377
			'ajax'		=> 1,
378
			'exists'	=> array()
379
		));
380
		
381
		
382
		// vars
383
		$json = array();
384
		$nonce = acf_extract_var( $options, 'nonce' );
385
		$exists = acf_extract_var( $options, 'exists' );
386
		
387
		
388
		// verify nonce
389
		if( !wp_verify_nonce($nonce, 'acf_nonce') ) {
390
		
391
			die;
392
			
393
		}
394
		
395
		
396
		// get field groups
397
		$field_groups = acf_get_field_groups( $options );
398
		
399
		
400
		// bail early if no field groups
401
		if( empty($field_groups) ) {
402
			
403
			wp_send_json_success( $json );
404
			
405
		}
406
		
407
		
408
		// loop through field groups
409
		foreach( $field_groups as $i => $field_group ) {
410
			
411
			// vars
412
			$item = array(
413
				//'ID'	=> $field_group['ID'], - JSON does not have ID (not used by JS anyway)
414
				'key'	=> $field_group['key'],
415
				'title'	=> $field_group['title'],
416
				'html'	=> '',
417
				'style' => ''
418
			);
419
			
420
			
421
			// style
422
			if( $i == 0 ) {
423
				
424
				$item['style'] = acf_get_field_group_style( $field_group );
425
				
426
			}
427
			
428
			
429
			// html
430
			if( !in_array($field_group['key'], $exists) ) {
431
				
432
				// load fields
433
				$fields = acf_get_fields( $field_group );
434
435
	
436
				// get field HTML
437
				ob_start();
438
				
439
				
440
				// render
441
				acf_render_fields( $options['post_id'], $fields, 'div', $field_group['instruction_placement'] );
442
				
443
				
444
				$item['html'] = ob_get_clean();
445
				
446
				
447
			}
448
			
449
			
450
			// append
451
			$json[] = $item;
452
			
453
		}
454
		
455
		
456
		// return
457
		wp_send_json_success( $json );
458
		
459
	}
460
	
461
	
462
	/*
463
	*  wp_insert_post_empty_content
464
	*
465
	*  This function will allow WP to insert a new post without title / content if ACF data exists
466
	*
467
	*  @type	function
468
	*  @date	16/07/2014
469
	*  @since	5.0.1
470
	*
471
	*  @param	$maybe_empty (bool) whether the post should be considered "empty"
472
	*  @param	$postarr (array) Array of post data
473
	*  @return	$maybe_empty
474
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $maybe_empty could not be parsed: Unknown type name "$maybe_empty" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
475
	
476
	function wp_insert_post_empty_content( $maybe_empty, $postarr ) {
0 ignored issues
show
Unused Code introduced by
The parameter $postarr is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
477
		
478
		if( $maybe_empty && !empty($_POST['_acfchanged']) ) {
479
			
480
			$maybe_empty = false;
481
			
482
		}
483
484
		
485
		// return
486
		return $maybe_empty;
487
	}
488
	
489
	
490
	/*
491
	*  save_post
492
	*
493
	*  This function will validate and save the $_POST data
494
	*
495
	*  @type	function
496
	*  @date	23/06/12
497
	*  @since	1.0.0
498
	*
499
	*  @param	$post_id (int)
500
	*  @return	$post_id (int)
501
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
502
	
503
	function save_post( $post_id, $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...
504
		
505
		// do not save if this is an auto save routine
506
		if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
507
			
508
			return $post_id;
509
			
510
		}
511
		
512
		
513
		// bail early if is acf-field-group or acf-field
514
		if( in_array($post->post_type, array('acf-field', 'acf-field-group'))) {
515
			
516
			return $post_id;
517
			
518
		}
519
		
520
		
521
		// verify and remove nonce
522
		if( !acf_verify_nonce('post', $post_id) ) {
523
			
524
			return $post_id;
525
			
526
		}
527
		
528
		
529
		// validate and save
530
		if( get_post_status($post_id) == 'publish' ) {
531
			
532
			if( acf_validate_save_post(true) ) {
533
				
534
				acf_save_post( $post_id );
535
				
536
			}
537
			
538
		} else {
539
			
540
			acf_save_post( $post_id );
541
			
542
		}
543
		
544
		
545
		// return
546
		return $post_id;
547
		
548
        
549
	}
550
	
551
	
552
	/*
553
	*  is_protected_meta
554
	*
555
	*  This function will remove any ACF meta from showing in the meta postbox
556
	*
557
	*  @type	function
558
	*  @date	12/04/2014
559
	*  @since	5.0.0
560
	*
561
	*  @param	$post_id (int)
562
	*  @return	$post_id (int)
563
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
564
	
565
	function is_protected_meta( $protected, $meta_key, $meta_type ) {
0 ignored issues
show
Unused Code introduced by
The parameter $meta_type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
566
		
567
		// if acf_get_field_reference returns a valid key, this is an acf value, so protect it!
568
		if( !$protected ) {
569
			
570
			$reference = acf_get_field_reference( $meta_key, $this->post_id );
571
			
572
			if( acf_is_field_key($reference) ) {
573
				
574
				$protected = true;
575
				
576
			} 
577
			
578
		}
579
		
580
		
581
		// return
582
		return $protected;
583
				
584
	}
585
			
586
}
587
588
new acf_form_post();
589
590
endif;
591
592
?>
593