Completed
Pull Request — 2.x (#4569)
by Scott Kingsley
08:49
created

PodsMeta::validate_comment()   C

Complexity

Conditions 17
Paths 39

Size

Total Lines 56
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 29
nc 39
nop 2
dl 0
loc 56
rs 6.5889
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @package Pods
5
 */
6
class PodsMeta {
7
8
	/**
9
	 * @var PodsMeta
10
	 */
11
	static $instance = null;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $instance.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
12
13
	/**
14
	 * @var PodsAPI
15
	 */
16
	private $api;
0 ignored issues
show
Unused Code introduced by
The property $api is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
18
	/**
19
	 * @var Pods
20
	 */
21
	private static $current_pod;
22
23
	/**
24
	 * @var array
25
	 */
26
	private static $current_pod_data;
27
28
	/**
29
	 * @var Pods
30
	 */
31
	private static $current_field_pod;
32
33
	/**
34
	 * @var int
35
	 */
36
	public static $object_identifier = - 1;
37
38
	/**
39
	 * @var array
40
	 */
41
	public static $advanced_content_types = array();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $advanced_content_types exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
42
43
	/**
44
	 * @var array
45
	 */
46
	public static $post_types = array();
47
48
	/**
49
	 * @var array
50
	 */
51
	public static $taxonomies = array();
52
53
	/**
54
	 * @var array
55
	 */
56
	public static $media = array();
57
58
	/**
59
	 * @var array
60
	 */
61
	public static $user = array();
62
63
	/**
64
	 * @var array
65
	 */
66
	public static $comment = array();
67
68
	/**
69
	 * @var array
70
	 */
71
	public static $settings = array();
72
73
	/**
74
	 * @var array
75
	 */
76
	public static $queue = array();
77
78
	/**
79
	 * @var array
80
	 */
81
	public static $groups = array();
82
83
	/**
84
	 * @var array
85
	 */
86
	public static $old_post_status = array();
87
88
	/**
89
	 * Singleton handling for a basic pods_meta() request
90
	 *
91
	 * @return \PodsMeta
92
	 *
93
	 * @since 2.3.5
94
	 */
95
	public static function init() {
96
97
		if ( ! is_object( self::$instance ) ) {
98
			self::$instance = new PodsMeta();
99
		}
100
101
		return self::$instance;
102
	}
103
104
	/**
105
	 * @return \PodsMeta
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
106
	 *
107
	 * @since 2.0
108
	 */
109
	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...
110
111
	}
112
113
	/**
114
	 * @return \PodsMeta
115
	 */
116
	public function core() {
117
118
		self::$advanced_content_types = pods_api()->load_pods( array( 'type' => 'pod' ) );
119
		self::$post_types             = pods_api()->load_pods( array( 'type' => 'post_type' ) );
120
		self::$taxonomies             = pods_api()->load_pods( array( 'type' => 'taxonomy' ) );
121
		self::$media                  = pods_api()->load_pods( array( 'type' => 'media' ) );
122
		self::$user                   = pods_api()->load_pods( array( 'type' => 'user' ) );
123
		self::$comment                = pods_api()->load_pods( array( 'type' => 'comment' ) );
124
		self::$settings               = pods_api()->load_pods( array( 'type' => 'settings' ) );
125
126
		// Handle Post Type Editor (needed for Pods core)
127
128
		// Loop through and add meta boxes for individual types (can't use this, Tabify doesn't pick it up)
129
		/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
130
        foreach ( self::$post_types as $post_type ) {
131
            $post_type_name = $post_type[ 'name' ];
132
133
            if ( !empty( $post_type[ 'object' ] ) )
134
                $post_type_name = $post_type[ 'object' ];
135
136
            add_action( 'add_meta_boxes_' . $post_type_name, array( $this, 'meta_post_add' ) );
137
        }
138
        */
139
140
		add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
141
		add_action( 'transition_post_status', array( $this, 'save_post_detect_new' ), 10, 3 );
142
		add_action( 'save_post', array( $this, 'save_post' ), 10, 3 );
143
		add_filter( 'wp_insert_post_data', array( $this, 'save_post_track_changed_fields' ), 10, 2 );
144
145
		if ( apply_filters( 'pods_meta_handler', true, 'post' ) ) {
146
			// Handle *_post_meta
147
			if ( apply_filters( 'pods_meta_handler_get', true, 'post' ) ) {
148
				add_filter( 'get_post_metadata', array( $this, 'get_post_meta' ), 10, 4 );
149
			}
150
151
			if ( ! pods_tableless() ) {
152
				add_filter( 'add_post_metadata', array( $this, 'add_post_meta' ), 10, 5 );
153
				add_filter( 'update_post_metadata', array( $this, 'update_post_meta' ), 10, 5 );
154
				add_filter( 'delete_post_metadata', array( $this, 'delete_post_meta' ), 10, 5 );
155
			}
156
		}
157
158
		add_action( 'delete_post', array( $this, 'delete_post' ), 10, 1 );
159
160
		if ( ! empty( self::$taxonomies ) ) {
161
			$has_fields = false;
162
163
			// Handle Taxonomy Editor
164
			foreach ( self::$taxonomies as $taxonomy ) {
0 ignored issues
show
Bug introduced by
The expression self::$taxonomies of type array|object|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
165
				if ( empty( $taxonomy['fields'] ) ) {
166
					continue;
167
				}
168
169
				$has_fields = true;
170
171
				$taxonomy_name = $taxonomy['name'];
172
173
				if ( ! empty( $taxonomy['object'] ) ) {
174
					$taxonomy_name = $taxonomy['object'];
175
				}
176
177
				add_action( $taxonomy_name . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 );
178
				add_action( $taxonomy_name . '_add_form_fields', array( $this, 'meta_taxonomy' ), 10, 1 );
179
			}
180
181
			if ( $has_fields ) {
182
				// Handle Term Editor
183
				add_action( 'edited_term', array( $this, 'save_taxonomy' ), 10, 3 );
184
				add_action( 'create_term', array( $this, 'save_taxonomy' ), 10, 3 );
185
				add_action( 'edit_terms', array( $this, 'save_taxonomy_track_changed_fields' ), 10, 2 );
186
187
				if ( apply_filters( 'pods_meta_handler', true, 'term' ) ) {
188
					// Handle *_term_meta
189
					if ( apply_filters( 'pods_meta_handler_get', true, 'term' ) ) {
190
						add_filter( 'get_term_metadata', array( $this, 'get_term_meta' ), 10, 4 );
191
					}
192
193
					if ( ! pods_tableless() ) {
194
						add_filter( 'add_term_metadata', array( $this, 'add_term_meta' ), 10, 5 );
195
						add_filter( 'update_term_metadata', array( $this, 'update_term_meta' ), 10, 5 );
196
						add_filter( 'delete_term_metadata', array( $this, 'delete_term_meta' ), 10, 5 );
197
					}
198
				}
199
			}
200
		}
201
202
		/**
203
		 * Fires after a previously shared taxonomy term is split into two separate terms.
204
		 *
205
		 * @since 4.2.0
206
		 *
207
		 * @param int    $term_id          ID of the formerly shared term.
208
		 * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
209
		 * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
210
		 * @param string $taxonomy         Taxonomy for the split term.
211
		 */
212
		add_action( 'split_shared_term', array( $this, 'split_shared_term' ), 10, 4 );
213
214
		// Handle Delete
215
		add_action( 'delete_term_taxonomy', array( $this, 'delete_taxonomy' ), 10, 1 );
216
217
		if ( ! empty( self::$media ) ) {
218
			add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
219
			add_action( 'wp_ajax_save-attachment-compat', array( $this, 'save_media_ajax' ), 0 );
220
221
			add_filter( 'attachment_fields_to_edit', array( $this, 'meta_media' ), 10, 2 );
222
223
			add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 );
224
			add_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 );
225
			add_filter( 'wp_insert_attachment_data', array( $this, 'save_post_track_changed_fields' ), 10, 2 );
226
227
			if ( apply_filters( 'pods_meta_handler', true, 'post' ) ) {
228
				// Handle *_post_meta
229
				if ( ! has_filter( 'get_post_metadata', array( $this, 'get_post_meta' ) ) ) {
230
					if ( apply_filters( 'pods_meta_handler_get', true, 'post' ) ) {
231
						add_filter( 'get_post_metadata', array( $this, 'get_post_meta' ), 10, 4 );
232
					}
233
234
					if ( ! pods_tableless() ) {
235
						add_filter( 'add_post_metadata', array( $this, 'add_post_meta' ), 10, 5 );
236
						add_filter( 'update_post_metadata', array( $this, 'update_post_meta' ), 10, 5 );
237
						add_filter( 'delete_post_metadata', array( $this, 'delete_post_meta' ), 10, 5 );
238
					}
239
				}
240
			}
241
		}
242
243
		// Handle Delete
244
		add_action( 'delete_attachment', array( $this, 'delete_media' ), 10, 1 );
245
246
		if ( ! empty( self::$user ) ) {
247
			// Handle User Editor
248
			add_action( 'show_user_profile', array( $this, 'meta_user' ) );
249
			add_action( 'edit_user_profile', array( $this, 'meta_user' ) );
250
			add_action( 'user_register', array( $this, 'save_user' ) );
251
			add_action( 'profile_update', array( $this, 'save_user' ), 10, 2 );
252
			add_filter( 'pre_user_login', array( $this, 'save_user_track_changed_fields' ) );
253
254
			if ( apply_filters( 'pods_meta_handler', true, 'user' ) ) {
255
				// Handle *_user_meta
256
				if ( apply_filters( 'pods_meta_handler_get', true, 'user' ) ) {
257
					add_filter( 'get_user_metadata', array( $this, 'get_user_meta' ), 10, 4 );
258
				}
259
260
				if ( ! pods_tableless() ) {
261
					add_filter( 'add_user_metadata', array( $this, 'add_user_meta' ), 10, 5 );
262
					add_filter( 'update_user_metadata', array( $this, 'update_user_meta' ), 10, 5 );
263
					add_filter( 'delete_user_metadata', array( $this, 'delete_user_meta' ), 10, 5 );
264
				}
265
			}
266
		}
267
268
		// Handle Delete
269
		add_action( 'delete_user', array( $this, 'delete_user' ), 10, 1 );
270
271
		if ( ! empty( self::$comment ) ) {
272
			// Handle Comment Form / Editor
273
			add_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 );
274
			add_filter( 'comment_form_default_fields', array( $this, 'meta_comment_new' ) );
275
			add_action( 'add_meta_boxes_comment', array( $this, 'meta_comment_add' ) );
276
			add_filter( 'pre_comment_approved', array( $this, 'validate_comment' ), 10, 2 );
277
			add_action( 'comment_post', array( $this, 'save_comment' ) );
278
			add_action( 'edit_comment', array( $this, 'save_comment' ) );
279
			add_action( 'wp_update_comment_data', array( $this, 'save_comment_track_changed_fields' ), 10, 3 );
280
281
			if ( apply_filters( 'pods_meta_handler', true, 'comment' ) ) {
282
				// Handle *_comment_meta
283
				add_filter( 'get_comment_metadata', array( $this, 'get_comment_meta' ), 10, 4 );
284
285
				if ( ! pods_tableless() ) {
286
					add_filter( 'add_comment_metadata', array( $this, 'add_comment_meta' ), 10, 5 );
287
					add_filter( 'update_comment_metadata', array( $this, 'update_comment_meta' ), 10, 5 );
288
					add_filter( 'delete_comment_metadata', array( $this, 'delete_comment_meta' ), 10, 5 );
289
				}
290
			}
291
		}
292
293
		// Handle Delete
294
		add_action( 'delete_comment', array( $this, 'delete_comment' ), 10, 1 );
295
296
		// @todo Patch core to provide $option back in filters, patch core to add filter pre_add_option to add_option
297
		/*if ( !empty( self::$settings ) ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
298
            foreach ( self::$settings as $setting_pod ) {
299
                foreach ( $setting_pod[ 'fields' ] as $option ) {
300
                    add_filter( 'pre_option_' . $setting_pod[ 'name' ] . '_' . $option[ 'name' ], array( $this, 'get_option' ), 10, 1 );
301
                    add_action( 'add_option_' . $setting_pod[ 'name' ] . '_' . $option[ 'name' ], array( $this, 'add_option' ), 10, 2 );
302
                    add_filter( 'pre_update_option_' . $setting_pod[ 'name' ] . '_' . $option[ 'name' ], array( $this, 'update_option' ), 10, 2 );
303
                }
304
            }
305
        }*/
306
307
		if ( is_admin() ) {
308
			$this->integrations();
309
		}
310
311
		add_action( 'init', array( $this, 'enqueue' ), 9 );
312
313
		if ( function_exists( 'pll_current_language' ) ) {
314
			add_action( 'init', array( $this, 'cache_pods' ), 101 );
315
		}
316
317
		do_action( 'pods_meta_init' );
318
319
		return $this;
320
	}
321
322
	/**
323
	 *
324
	 */
325
	public static function enqueue() {
326
327
		foreach ( self::$queue as $type => $objects ) {
328
			foreach ( $objects as $pod_name => $pod ) {
329
				pods_transient_set( 'pods_pod_' . $pod_name, $pod );
330
			}
331
332
			self::$$type = array_merge( self::$$type, $objects );
333
		}
334
	}
335
336
	/**
337
	 * Go back through and cache the Pods now that Polylang has loaded
338
	 */
339
	public function cache_pods() {
340
341
		self::$advanced_content_types = pods_api()->load_pods( array( 'type' => 'pod', 'refresh' => true ) );
342
		self::$post_types             = pods_api()->load_pods( array( 'type' => 'post_type', 'refresh' => true ) );
343
		self::$taxonomies             = pods_api()->load_pods( array( 'type' => 'taxonomy', 'refresh' => true ) );
344
		self::$media                  = pods_api()->load_pods( array( 'type' => 'media', 'refresh' => true ) );
345
		self::$user                   = pods_api()->load_pods( array( 'type' => 'user', 'refresh' => true ) );
346
		self::$comment                = pods_api()->load_pods( array( 'type' => 'comment', 'refresh' => true ) );
347
		self::$settings               = pods_api()->load_pods( array( 'type' => 'settings', 'refresh' => true ) );
348
	}
349
350
	/**
351
	 * @param $type
352
	 * @param $pod
353
	 *
354
	 * @return array|bool|int
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|integer|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
355
	 */
356
	public function register( $type, $pod ) {
357
358
		$pod_type = $type;
359
360
		if ( 'post_type' == $type ) {
361
			$type = 'post_types';
362
		} elseif ( 'taxonomy' == $type ) {
363
			$type = 'taxonomies';
364
		} elseif ( 'pod' == $type ) {
365
			$type = 'advanced_content_types';
366
		}
367
368
		if ( ! isset( self::$queue[ $type ] ) ) {
369
			self::$queue[ $type ] = array();
370
		}
371
372
		if ( is_array( $pod ) && ! empty( $pod ) && ! isset( $pod['name'] ) ) {
373
			$data = array();
374
375
			foreach ( $pod as $p ) {
376
				$data[] = $this->register( $type, $p );
377
			}
378
379
			return $data;
380
		}
381
382
		$pod['type'] = $pod_type;
383
		$pod         = pods_api()->save_pod( $pod, false, false );
384
385
		if ( ! empty( $pod ) ) {
386
			self::$object_identifier --;
387
388
			self::$queue[ $type ][ $pod['name'] ] = $pod;
389
390
			return $pod;
391
		}
392
393
		return false;
394
	}
395
396
	/**
397
	 * @param $pod
398
	 * @param $field
399
	 *
400
	 * @return array|bool|int
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|integer|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
401
	 */
402
	public function register_field( $pod, $field ) {
403
404
		if ( is_array( $pod ) && ! empty( $pod ) && ! isset( $pod['name'] ) ) {
405
			$data = array();
406
407
			foreach ( $pod as $p ) {
408
				$data[] = $this->register_field( $p, $field );
409
			}
410
411
			return $data;
412
		}
413
414
		if ( empty( self::$current_pod_data ) || ! is_object( self::$current_pod_data ) || self::$current_pod_data['name'] != $pod ) {
415
			self::$current_pod_data = pods_api()->load_pod( array( 'name' => $pod ), false );
416
		}
417
418
		$pod = self::$current_pod_data;
419
420
		if ( ! empty( $pod ) ) {
421
			$type = $pod['type'];
422
423
			if ( 'post_type' == $pod['type'] ) {
424
				$type = 'post_types';
425
			} elseif ( 'taxonomy' == $pod['type'] ) {
426
				$type = 'taxonomies';
427
			} elseif ( 'pod' == $pod['type'] ) {
428
				$type = 'advanced_content_types';
429
			}
430
431
			if ( ! isset( self::$queue[ $pod['type'] ] ) ) {
432
				self::$queue[ $type ] = array();
433
			}
434
435
			$field = pods_api()->save_field( $field, false, false, $pod['id'] );
436
437
			if ( ! empty( $field ) ) {
438
				$pod['fields'][ $field['name'] ] = $field;
439
440
				self::$queue[ $type ][ $pod['name'] ] = $pod;
441
442
				return $field;
443
			}
444
		}
445
446
		return false;
447
	}
448
449
	/**
450
	 *
451
	 */
452
	public function integrations() {
453
454
		// `ac_is_version_gte` is since AC 3.0+
455
		if ( ! function_exists( 'ac_is_version_gte' ) ) {
456
			// Codepress Admin Columns < 2.x
457
			add_filter( 'cac/storage_model/meta_keys', array( $this, 'cpac_meta_keys' ), 10, 2 );
458
			add_filter( 'cac/post_types', array( $this, 'cpac_post_types' ), 10, 1 );
459
			add_filter( 'cac/column/meta/value', array( $this, 'cpac_meta_value' ), 10, 3 );
460
		} else {
461
			// Codepress Admin Columns 3.x +
462
			add_filter( 'ac/column/custom_field/meta_keys', array( $this, 'cpac_meta_keys' ), 10, 2 );
463
			add_filter( 'ac/post_types', array( $this, 'cpac_post_types' ), 10, 1 );
464
			add_filter( 'ac/column/value', array( $this, 'cpac_meta_value' ), 10, 3 );
465
		}
466
	}
467
468
	/**
469
	 * Admin Columns: Remove internal meta keys + add existing (public) Pod field keys.
470
	 *
471
	 * @param array                           $meta_fields
472
	 * @param \AC_Settings_Column_CustomField $storage_model
473
	 *
474
	 * @return array
475
	 */
476
	public function cpac_meta_keys( $meta_fields, $storage_model ) {
477
478
		$object_type = 'post_type';
479
		$object      = null;
480
		$obj         = null;
481
482
		if ( ! method_exists( $storage_model, 'get_column' ) ) {
483
			// Codepress Admin Columns < 2.x
484
			$object = $storage_model->key;
485
			$type   = $storage_model->type;
486
		} else {
487
			// Codepress Admin Columns 3.x +
488
			$obj    = $storage_model->get_column();
489
			$object = $obj->get_list_screen()->get_key();
490
			$type   = $obj->get_list_screen()->get_meta_type();
491
		}
492
493
		if ( in_array( $type, array( 'wp-links', 'link' ), true ) ) {
494
			$object_type = $object = 'link';
495
		} elseif ( in_array( $type, array( 'wp-media', 'media' ), true ) ) {
496
			$object_type = $object = 'media';
497
		} elseif ( in_array( $type, array( 'wp-users', 'user' ), true ) ) {
498
			$object_type = $object = 'user';
499
		} elseif ( in_array( $type, array( 'wp-comments', 'comment' ), true ) ) {
500
			$object_type = $object = 'comment';
501
		} elseif ( 'taxonomy' === $type ) {
502
			$object_type = 'taxonomy';
503
			if ( ! $obj ) {
504
				// Codepress Admin Columns < 2.x
505
				$object = $storage_model->taxonomy;
506
			} else {
507
				// Codepress Admin Columns 3.x +
508
				$object = $obj->get_taxonomy();
509
			}
510
		}
511
512
		if ( empty( self::$current_pod_data ) || ! is_object( self::$current_pod_data ) || self::$current_pod_data['name'] != $object ) {
513
			self::$current_pod_data = pods_api()->load_pod( array( 'name' => $object ), false );
514
		}
515
516
		$pod = self::$current_pod_data;
517
518
		// Add Pods fields
519
		if ( ! empty( $pod ) && $object_type == $pod['type'] ) {
520
			foreach ( $pod['fields'] as $field => $field_data ) {
521
				if ( ! is_array( $meta_fields ) ) {
522
					$meta_fields = array();
523
				}
524
525
				if ( ! in_array( $field, $meta_fields ) ) {
526
					$meta_fields[] = $field;
527
				}
528
			}
529
		}
530
531
		// Remove internal Pods fields
532
		if ( is_array( $meta_fields ) ) {
533
			foreach ( $meta_fields as $k => $meta_field ) {
534
				if ( 0 === strpos( $meta_field, '_pods_' ) ) {
535
					unset( $meta_fields[ $k ] );
536
				}
537
			}
538
		}
539
540
		return $meta_fields;
541
	}
542
543
	/**
544
	 * Admin Columns: Remove internal Pods post types.
545
	 *
546
	 * @param  array $post_types
547
	 *
548
	 * @return array
549
	 */
550
	public function cpac_post_types( $post_types ) {
551
552
		foreach ( $post_types as $post_type => $post_type_name ) {
553
			if ( 0 === strpos( $post_type, '_pods_' ) || 0 === strpos( $post_type_name, '_pods_' ) ) {
554
				unset( $post_types[ $post_type ] );
555
			}
556
		}
557
558
		return $post_types;
559
	}
560
561
	/**
562
	 * Admin Columns: For custom field column types.
563
	 *
564
	 * @param mixed      $meta
565
	 * @param int        $id
566
	 * @param \AC_Column $obj
567
	 *
568
	 * @return mixed
569
	 */
570
	public function cpac_meta_value( $meta, $id, $obj ) {
571
572
		$tableless_field_types = PodsForm::tableless_field_types();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $tableless_field_types exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
573
574
		$object_type = 'post_type';
575
		$object      = null;
576
		$type        = null;
577
578
		if ( ! method_exists( $obj, 'get_list_screen' ) ) {
579
			// Codepress Admin Columns < 2.x
580
			$object = $obj->storage_model->key;
581
			$type   = $obj->storage_model->type;
582
		} else {
583
			// Codepress Admin Columns 3.x +
584
			$object = $obj->get_list_screen()->get_key();
585
			$type   = $obj->get_list_screen()->get_meta_type();
586
		}
587
588
		if ( in_array( $type, array( 'wp-links', 'link' ), true ) ) {
589
			$object_type = $object = 'link';
590
		} elseif ( in_array( $type, array( 'wp-media', 'media' ), true ) ) {
591
			$object_type = $object = 'media';
592
		} elseif ( in_array( $type, array( 'wp-users', 'user' ), true ) ) {
593
			$object_type = $object = 'user';
594
		} elseif ( in_array( $type, array( 'wp-comments', 'comment' ), true ) ) {
595
			$object_type = $object = 'comment';
596
		} elseif ( 'taxonomy' === $type ) {
597
			$object_type = 'taxonomy';
598
			if ( ! method_exists( $obj, 'get_taxonomy' ) ) {
599
				// Codepress Admin Columns < 2.x
600
				$object = $obj->storage_model->taxonomy;
601
			} else {
602
				// Codepress Admin Columns 3.x +
603
				$object = $obj->get_taxonomy();
604
			}
605
		}
606
607
		$field      = ( "cpachidden" === substr( $obj->get_option( 'field' ), 0, 10 ) ) ? str_replace( 'cpachidden', '', $obj->get_option( 'field' ) ) : $obj->get_option( 'field' );
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal cpachidden does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
608
		$field_type = $obj->get_option( 'field_type' );
609
610
		if ( empty( self::$current_pod_data ) || ! is_object( self::$current_pod_data ) || self::$current_pod_data['name'] !== $object ) {
611
			self::$current_pod_data = pods_api()->load_pod( array( 'name' => $object ), false );
612
		}
613
614
		$pod = self::$current_pod_data;
615
616
		// Add Pods fields
617
		if ( ! empty( $pod ) && isset( $pod['fields'][ $field ] ) ) {
618
			if ( in_array( $pod['type'], array(
619
					'post_type',
620
					'media',
621
					'taxonomy',
622
					'user',
623
					'comment',
624
					'media'
625
				), true ) && ( ! empty( $field_type ) || in_array( $pod['fields'][ $field ]['type'], $tableless_field_types, true ) ) ) {
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
626
				$metadata_type = $pod['type'];
627
628
				if ( in_array( $metadata_type, array( 'post_type', 'media' ), true ) ) {
629
					$metadata_type = 'post';
630
				} elseif ( 'taxonomy' === $metadata_type ) {
631
					$metadata_type = 'term';
632
				}
633
634
				if ( 'term' === $metadata_type && ! function_exists( 'get_term_meta' ) ) {
635
					$podterms = pods( $pod['name'], $id );
636
637
					$meta = $podterms->field( $field );
638
				} else {
639
					$meta = get_metadata( $metadata_type, $id, $field, ( 'array' !== $field_type ) );
640
				}
641
			} elseif ( 'taxonomy' === $pod['type'] ) {
642
				$podterms = pods( $pod['name'], $id );
643
644
				$meta = $podterms->field( $field );
645
			}
646
647
			$meta = PodsForm::field_method( $pod['fields'][ $field ]['type'], 'ui', $id, $meta, $field, array_merge( $pod['fields'][ $field ], $pod['fields'][ $field ]['options'] ), $pod['fields'], $pod );
648
		}
649
650
		return $meta;
651
	}
652
653
	/**
654
	 * Add a meta group of fields to add/edit forms
655
	 *
656
	 * @param string|array $pod      The pod or type of element to attach the group to.
657
	 * @param string       $label    Title of the edit screen section, visible to user.
658
	 * @param string|array $fields   Either a comma separated list of text fields or an associative array containing
659
	 *                               field infomration.
660
	 * @param string       $context  (optional) The part of the page where the edit screen section should be shown
661
	 *                               ('normal', 'advanced', or 'side').
662
	 * @param string       $priority (optional) The priority within the context where the boxes should show ('high',
663
	 *                               'core', 'default' or 'low').
664
	 *
665
	 * @since 2.0
666
	 *
667
	 * @return mixed|void
668
	 */
669
	public function group_add( $pod, $label, $fields, $context = 'normal', $priority = 'default' ) {
670
671
		if ( is_array( $pod ) && ! empty( $pod ) && ! isset( $pod['name'] ) ) {
672
			foreach ( $pod as $p ) {
673
				$this->group_add( $p, $label, $fields, $context, $priority );
674
			}
675
676
			return true;
677
		}
678
679
		if ( ! is_array( $pod ) ) {
680
			if ( empty( self::$current_pod_data ) || ! is_object( self::$current_pod_data ) || self::$current_pod_data['name'] != $pod ) {
681
				self::$current_pod_data = pods_api()->load_pod( array( 'name' => $pod ), false );
682
			}
683
684
			if ( ! empty( self::$current_pod_data ) ) {
685
				$pod = self::$current_pod_data;
686
			} else {
687
				$type = 'post_type';
688
689
				if ( in_array( $pod, array( 'media', 'user', 'comment' ) ) ) {
690
					$type = $pod;
691
				}
692
693
				$pod = array(
694
					'name' => $pod,
695
					'type' => $type
696
				);
697
			}
698
		}
699
700
		if ( is_array( $pod ) && ! isset( $pod['id'] ) ) {
701
			$defaults = array(
702
				'name' => '',
703
				'type' => 'post_type'
704
			);
705
706
			$pod = array_merge( $defaults, $pod );
707
		}
708
709
		if ( 'post' == $pod['type'] ) {
710
			$pod['type'] = 'post_type';
711
		}
712
713
		if ( empty( $pod['name'] ) && isset( $pod['object'] ) && ! empty( $pod['object'] ) ) {
714
			$pod['name'] = $pod['object'];
715
		} elseif ( ! isset( $pod['object'] ) || empty( $pod['object'] ) ) {
716
			$pod['object'] = $pod['name'];
717
		}
718
719
		if ( empty( $pod['object'] ) ) {
720
			return pods_error( __( 'Object required to add a Pods meta group', 'pods' ) );
721
		}
722
723
		$object_name = $pod['object'];
724
725
		if ( 'pod' == $pod['type'] ) {
726
			$object_name = $pod['name'];
727
		}
728
729
		if ( ! isset( self::$groups[ $pod['type'] ] ) ) {
730
			self::$groups[ $pod['type'] ] = array();
731
		}
732
733
		if ( ! isset( self::$groups[ $pod['type'] ][ $object_name ] ) ) {
734
			self::$groups[ $pod['type'] ][ $object_name ] = array();
735
		}
736
737
		$_fields = array();
738
739
		if ( ! is_array( $fields ) ) {
740
			$fields = explode( ',', $fields );
741
		}
742
743
		foreach ( $fields as $k => $field ) {
744
			$name = $k;
745
746
			$defaults = array(
747
				'name'  => $name,
748
				'label' => $name,
749
				'type'  => 'text'
750
			);
751
752
			if ( ! is_array( $field ) ) {
753
				$name = trim( $field );
754
755
				$field = array(
756
					'name'  => $name,
757
					'label' => $name
758
				);
759
			}
760
761
			$field = array_merge( $defaults, $field );
762
763
			$field['name'] = trim( $field['name'] );
764
765
			if ( isset( $pod['fields'] ) && isset( $pod['fields'][ $field['name'] ] ) ) {
766
				$field = array_merge( $field, $pod['fields'][ $field['name'] ] );
767
			}
768
769
			$_fields[ $k ] = $field;
770
		}
771
772
		// Setup field options
773
		$fields = PodsForm::fields_setup( $_fields );
774
775
		$group = array(
776
			'pod'      => $pod,
777
			'label'    => $label,
778
			'fields'   => $fields,
779
			'context'  => $context,
780
			'priority' => $priority
781
		);
782
783
		// Filter group data, pass vars separately for reference down the line (in case array changed by other filter)
784
		$group = apply_filters( 'pods_meta_group_add_' . $pod['type'] . '_' . $object_name, $group, $pod, $label, $fields );
785
		$group = apply_filters( 'pods_meta_group_add_' . $pod['type'], $group, $pod, $label, $fields );
786
		$group = apply_filters( 'pods_meta_group_add', $group, $pod, $label, $fields );
787
788
		self::$groups[ $pod['type'] ][ $object_name ][] = $group;
789
790
		// Hook it up!
791
		if ( 'post_type' == $pod['type'] ) {
792
			if ( ! has_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) ) ) {
793
				add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
794
			}
795
796
			/*if ( !has_action( 'save_post', array( $this, 'save_post' ), 10, 3 ) )
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
797
                add_action( 'save_post', array( $this, 'save_post' ), 10, 3 );*/
798
		} elseif ( 'taxonomy' == $pod['type'] ) {
799
			if ( ! has_action( $pod['object'] . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 ) ) {
800
				add_action( $pod['object'] . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 );
801
				add_action( $pod['object'] . '_add_form_fields', array( $this, 'meta_taxonomy' ), 10, 1 );
802
			}
803
804
			if ( ! has_action( 'edited_term', array( $this, 'save_taxonomy' ), 10, 3 ) ) {
805
				add_action( 'edited_term', array( $this, 'save_taxonomy' ), 10, 3 );
806
				add_action( 'create_term', array( $this, 'save_taxonomy' ), 10, 3 );
807
			}
808
		} elseif ( 'media' == $pod['type'] ) {
809
			if ( ! has_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 ) ) {
810
				add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
811
				add_action( 'wp_ajax_save-attachment-compat', array( $this, 'save_media_ajax' ), 0 );
812
813
				add_filter( 'attachment_fields_to_edit', array( $this, 'meta_media' ), 10, 2 );
814
815
				add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 );
816
				add_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 );
817
			}
818
		} elseif ( 'user' == $pod['type'] ) {
819
			if ( ! has_action( 'show_user_profile', array( $this, 'meta_user' ) ) ) {
820
				add_action( 'show_user_profile', array( $this, 'meta_user' ) );
821
				add_action( 'edit_user_profile', array( $this, 'meta_user' ) );
822
				add_action( 'user_register', array( $this, 'save_user' ) );
823
				add_action( 'profile_update', array( $this, 'save_user' ), 10, 2 );
824
			}
825
		} elseif ( 'comment' == $pod['type'] ) {
826
			if ( ! has_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 ) ) {
827
				add_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 );
828
				add_filter( 'comment_form_default_fields', array( $this, 'meta_comment_new' ) );
829
				add_action( 'add_meta_boxes_comment', array( $this, 'meta_comment_add' ) );
830
				add_action( 'wp_insert_comment', array( $this, 'save_comment' ) );
831
				add_action( 'edit_comment', array( $this, 'save_comment' ) );
832
			}
833
		}
834
	}
835
836
	/**
837
	 * @param $type
838
	 * @param $name
839
	 *
840
	 * @return array|bool|mixed|void
841
	 */
842
	public function object_get( $type, $name ) {
843
844
		$object = self::$post_types;
845
846
		if ( 'term' == $type ) {
847
			$type = 'taxonomy';
848
		}
849
850
		if ( 'taxonomy' == $type ) {
851
			$object = self::$taxonomies;
852
		} elseif ( 'media' == $type ) {
853
			$object = self::$media;
854
		} elseif ( 'user' == $type ) {
855
			$object = self::$user;
856
		} elseif ( 'comment' == $type ) {
857
			$object = self::$comment;
858
		}
859
860
		if ( 'pod' != $type && ! empty( $object ) && is_array( $object ) && isset( $object[ $name ] ) ) {
861
			$pod = $object[ $name ];
862
		} else {
863
			if ( empty( self::$current_pod_data ) || ! is_object( self::$current_pod_data ) || self::$current_pod_data['name'] != $name ) {
864
				self::$current_pod_data = pods_api()->load_pod( array( 'name' => $name ), false );
865
			}
866
867
			$pod = self::$current_pod_data;
868
		}
869
870
		if ( empty( $pod ) ) {
871
			return array();
872
		}
873
874
		$defaults = array(
875
			'name'   => 'post',
876
			'object' => 'post',
877
			'type'   => 'post_type'
878
		);
879
880
		$pod = array_merge( $defaults, (array) $pod );
881
882
		if ( empty( $pod['name'] ) ) {
883
			$pod['name'] = $pod['object'];
884
		} elseif ( empty( $pod['object'] ) ) {
885
			$pod['object'] = $pod['name'];
886
		}
887
888
		if ( $pod['type'] != $type ) {
889
			return array();
890
		}
891
892
		return $pod;
893
	}
894
895
	/**
896
	 * @param $type
897
	 * @param $name
898
	 * @param $default_fields
899
	 *
900
	 * @return array
901
	 */
902
	public function groups_get( $type, $name, $default_fields = null ) {
903
904
		if ( 'post_type' == $type && 'attachment' == $name ) {
905
			$type = 'media';
906
			$name = 'media';
907
		} elseif ( 'term' == $type ) {
908
			$type = 'taxonomy';
909
		}
910
911
		do_action( 'pods_meta_groups', $type, $name );
912
913
		$pod    = array();
914
		$fields = array();
915
916
		$object = self::$post_types;
917
918
		if ( 'taxonomy' == $type ) {
919
			$object = self::$taxonomies;
920
		} elseif ( 'media' == $type ) {
921
			$object = self::$media;
922
		} elseif ( 'user' == $type ) {
923
			$object = self::$user;
924
		} elseif ( 'comment' == $type ) {
925
			$object = self::$comment;
926
		} elseif ( 'pod' == $type ) {
927
			$object = self::$advanced_content_types;
928
		}
929
930
		if ( ! empty( $object ) && is_array( $object ) && isset( $object[ $name ] ) ) {
931
			$fields = $object[ $name ]['fields'];
932
		} else {
933
			if ( empty( self::$current_pod_data ) || ! is_object( self::$current_pod_data ) || self::$current_pod_data['name'] != $name ) {
934
				self::$current_pod_data = pods_api()->load_pod( array( 'name' => $name ), false );
935
			}
936
937
			$pod = self::$current_pod_data;
938
939
			if ( ! empty( $pod ) ) {
940
				$fields = $pod['fields'];
941
			}
942
		}
943
944
		if ( null !== $default_fields ) {
945
			$fields = $default_fields;
946
		}
947
948
		$defaults = array(
949
			'name'   => 'post',
950
			'object' => 'post',
951
			'type'   => 'post_type'
952
		);
953
954
		$pod = array_merge( $defaults, (array) $pod );
955
956
		if ( empty( $pod['name'] ) ) {
957
			$pod['name'] = $pod['object'];
958
		} elseif ( empty( $pod['object'] ) ) {
959
			$pod['object'] = $pod['name'];
960
		}
961
962
		if ( $pod['type'] != $type ) {
963
			return array();
964
		}
965
966
		$groups = array(
967
			array(
968
				'pod'      => $pod,
969
				/**
970
				 * Filter the title of the Pods Metabox In The Post Editor
971
				 *
972
				 * @param string  $title  The title to use, default is 'More Fields'
973
				 * @param obj|Pod $pod    Current Pods Object
974
				 * @param array   $fields Array of fields that will go in the metabox
975
				 * @param string  $type   The type of Pod
976
				 * @param string  $name   Name of the Pod
977
				 *
978
				 * @return string The title for the metabox.
979
				 *
980
				 * @since unknown
981
				 */
982
				'label'    => apply_filters( 'pods_meta_default_box_title', __( 'More Fields', 'pods' ), $pod, $fields, $type, $name ),
983
				'fields'   => $fields,
984
				'context'  => 'normal',
985
				'priority' => 'default'
986
			)
987
		);
988
989
		if ( isset( self::$groups[ $type ] ) && isset( self::$groups[ $type ][ $name ] ) ) {
990
			$groups = self::$groups[ $type ][ $name ];
991
		}
992
993
		/**
994
		 * Filter the array of field groups
995
		 *
996
		 * @param array  $groups Array of groups
997
		 * @param string $type   The type of Pod
998
		 * @param string $name   Name of the Pod
999
		 *
1000
		 * @since 2.6.6
1001
		 */
1002
		return apply_filters( 'pods_meta_groups_get', $groups, $type, $name );
1003
	}
1004
1005
	/**
1006
	 * @param      $post_type
1007
	 * @param null $post
1008
	 */
1009
	public function meta_post_add( $post_type, $post = null ) {
1010
1011
		if ( 'comment' == $post_type ) {
1012
			return;
1013
		}
1014
1015
		if ( is_object( $post ) ) {
1016
			$post_type = $post->post_type;
1017
		}
1018
1019
		$groups           = $this->groups_get( 'post_type', $post_type );
1020
		$pods_field_found = false;
1021
1022
		foreach ( $groups as $group ) {
1023
			if ( empty( $group['fields'] ) ) {
1024
				continue;
1025
			}
1026
1027
			$field_found  = false;
1028
			$group_hidden = true;
1029
1030
			foreach ( $group['fields'] as $field ) {
1031
				if ( false !== PodsForm::permission( $field['type'], $field['name'], $field, $group['fields'] ) ) {
1032
					$field_found = true;
1033
				}
1034
				if ( ! isset( $field['options']['hidden'] ) || 1 != (int) $field['options']['hidden'] ) {
1035
					$group_hidden = false;
1036
				}
1037
			}
1038
1039
			if ( $group_hidden ) {
1040
				continue;
1041
			}
1042
1043
			if ( empty( $group['label'] ) ) {
1044
				$group['label'] = get_post_type_object( $post_type )->labels->label;
1045
			}
1046
1047
			if ( $field_found ) {
1048
				$pods_field_found = true;
1049
				add_meta_box( 'pods-meta-' . sanitize_title( $group['label'] ), $group['label'], array(
1050
						$this,
1051
						'meta_post'
1052
					), $post_type, $group['context'], $group['priority'], array( 'group' => $group ) );
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 20.
Loading history...
1053
1054
			}
1055
		}
1056
1057
		if ( $pods_field_found ) {
1058
			// Only add the classes to forms that actually have pods fields
1059
			add_action( 'post_edit_form_tag', array( $this, 'add_class_submittable' ) );
1060
		}
1061
	}
1062
1063
	/**
1064
	 *
1065
	 * Called by 'post_edit_form_tag' action to include the classes in the <form> tag
1066
	 *
1067
	 */
1068
	public function add_class_submittable() {
1069
1070
		echo ' class="pods-submittable pods-form"';
1071
	}
1072
1073
	/**
1074
	 * @param $post
1075
	 * @param $metabox
1076
	 */
1077
	public function meta_post( $post, $metabox ) {
1078
1079
		wp_enqueue_style( 'pods-form' );
1080
		wp_enqueue_script( 'pods' );
1081
1082
		$pod_type = 'post';
1083
1084
		if ( 'attachment' == $post->post_type ) {
1085
			$pod_type = 'media';
1086
		}
1087
1088
		do_action( 'pods_meta_' . __FUNCTION__, $post );
1089
1090
		$hidden_fields = array();
1091
1092
		$id = null;
1093
1094
		if ( is_object( $post ) && false === strpos( $_SERVER['REQUEST_URI'], '/post-new.php' ) ) {
1095
			$id = $post->ID;
1096
		}
1097
1098
		if ( empty( self::$current_pod_data ) || ! is_object( self::$current_pod ) || self::$current_pod->pod != $metabox['args']['group']['pod']['name'] ) {
1099
			self::$current_pod = pods( $metabox['args']['group']['pod']['name'], $id, true );
1100
		} elseif ( self::$current_pod->id() != $id ) {
1101
			self::$current_pod->fetch( $id );
1102
		}
1103
1104
		$pod = self::$current_pod;
1105
1106
		$fields = $metabox['args']['group']['fields'];
1107
1108
		/**
1109
		 * Filter the fields used for the Pods metabox group
1110
		 *
1111
		 * @since 2.6.6
1112
		 *
1113
		 * @param array   $fields  Fields from the current Pod metabox group
1114
		 * @param int     $id      Post ID
1115
		 * @param WP_Post $post    Post object
1116
		 * @param array   $metabox Metabox args from the current Pod metabox group
1117
		 * @param Pods    $pod     Pod object
1118
		 */
1119
		$fields = apply_filters( 'pods_meta_post_fields', $fields, $id, $post, $metabox, $pod );
1120
1121
		if ( empty( $fields ) ) {
1122
			_e( 'There are no fields to display', 'pods' );
1123
1124
			return;
1125
		}
1126
		?>
1127
		<table class="form-table pods-metabox pods-admin pods-dependency">
1128
			<?php
1129
			echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_' . $pod_type ), 'hidden' );
1130
1131
			foreach ( $fields as $field ) {
1132
				if ( false === PodsForm::permission( $field['type'], $field['name'], $field['options'], $fields, $pod, $id ) ) {
1133
					if ( pods_var( 'hidden', $field['options'], false ) ) {
1134
						$field['type'] = 'hidden';
1135
					} else {
1136
						continue;
1137
					}
1138
				} elseif ( ! pods_has_permissions( $field['options'] ) && pods_var( 'hidden', $field['options'], false ) ) {
1139
					$field['type'] = 'hidden';
1140
				}
1141
1142
				$value = '';
1143
1144
				if ( ! empty( $pod ) ) {
1145
					pods_no_conflict_on( 'post' );
1146
1147
					$value = $pod->field( array( 'name' => $field['name'], 'in_form' => true ) );
1148
1149
					pods_no_conflict_off( 'post' );
1150
				} elseif ( ! empty( $id ) ) {
1151
					$value = get_post_meta( $id, $field['name'], true );
1152
				}
1153
1154
				if ( 'hidden' == $field['type'] ) {
1155
					$hidden_fields[] = array(
1156
						'field' => $field,
1157
						'value' => $value
1158
					);
1159
				} else {
1160
					$dep_options = PodsForm::dependencies( $field, 'pods-meta-' );
1161
					$dep_classes = $dep_options['classes'];
1162
					$dep_data    = $dep_options['data'];
1163
1164
					do_action( 'pods_meta_' . __FUNCTION__ . '_' . $field['name'], $post, $field, $pod );
1165
					?>
1166
					<tr class="form-field pods-field pods-field-input <?php echo esc_attr( 'pods-form-ui-row-type-' . $field['type'] . ' pods-form-ui-row-name-' . PodsForm::clean( $field['name'], true ) ); ?> <?php echo esc_attr( $dep_classes ); ?>" <?php PodsForm::data( $dep_data ); ?>">
1167
					<th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field['name'], $field['label'], $field['help'], $field ); ?></th>
1168
					<td>
1169
						<?php
1170
						// Remove any extra ? help icons
1171
						if ( isset( $field['help'] ) ) {
1172
							unset( $field['help'] );
1173
						}
1174
						?>
1175
						<div class="pods-submittable-fields">
1176
							<?php echo PodsForm::field( 'pods_meta_' . $field['name'], $value, $field['type'], $field, $pod, $id ); ?>
1177
							<?php echo PodsForm::comment( 'pods_meta_' . $field['name'], $field['description'], $field ); ?>
1178
						</div>
1179
					</td>
1180
					</tr>
1181
					<?php
1182
					do_action( 'pods_meta_' . __FUNCTION__ . '_' . $field['name'] . '_post', $post, $field, $pod );
1183
				}
1184
			}
1185
			?>
1186
		</table>
1187
1188
		<?php
1189
		do_action( 'pods_meta_' . __FUNCTION__ . '_post', $post );
1190
1191
		foreach ( $hidden_fields as $hidden_field ) {
1192
			$field_data = $hidden_field['field'];
1193
1194
			echo PodsForm::field( 'pods_meta_' . $field_data['name'], $hidden_field['value'], 'hidden', $field_data );
1195
		}
1196
		?>
1197
1198
		<script type="text/javascript">
1199
			jQuery( function ( $ ) {
1200
				$( document ).Pods( 'validate' );
1201
				$( document ).Pods( 'submit_meta' );
1202
				$( document ).Pods( 'dependency', true );
1203
			} );
1204
		</script>
1205
		<?php
1206
	}
1207
1208
	/**
1209
	 * Handle integration with the transition_post_status hook
1210
	 *
1211
	 * @see wp_transition_post_status
1212
	 *
1213
	 * @param string  $new_status
1214
	 * @param string  $old_status
1215
	 * @param WP_Post $post
1216
	 */
1217
	public function save_post_detect_new( $new_status, $old_status, $post ) {
1218
1219
		if ( $post ) {
1220
			self::$old_post_status[ $post->post_type ] = $old_status;
1221
		}
1222
1223
	}
1224
1225
	/**
1226
	 * Handle integration with the save_post hook
1227
	 *
1228
	 * @see wp_insert_post
1229
	 *
1230
	 * @param int       $post_id
1231
	 * @param WP_Post   $post
1232
	 * @param bool|null $update
1233
	 */
1234
	public function save_post( $post_id, $post, $update = null ) {
1235
1236
		if ( empty( $post ) ) {
1237
			return;
1238
		}
1239
1240
		$is_new_item = false;
1241
1242
		if ( is_bool( $update ) ) {
1243
			$is_new_item = ! $update;
1244
		} // false is new item
1245
		elseif ( isset( self::$old_post_status[ $post->post_type ] ) && in_array( self::$old_post_status[ $post->post_type ], array(
1246
				'new',
1247
				'auto-draft'
1248
			), true ) ) {
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
1249
			$is_new_item = true;
1250
		}
1251
1252
		$nonced = wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_post' );
1253
1254
		if ( ! $is_new_item && false === $nonced ) {
1255
			return;
1256
		}
1257
1258
		// Unset to avoid manual new post issues
1259
		if ( isset( self::$old_post_status[ $post->post_type ] ) ) {
1260
			unset( self::$old_post_status[ $post->post_type ] );
1261
		}
1262
1263
		$blacklisted_types = array(
1264
			'revision',
1265
			'_pods_pod',
1266
			'_pods_field'
1267
		);
1268
1269
		$blacklisted_types = apply_filters( 'pods_meta_save_post_blacklist_types', $blacklisted_types, $post_id, $post );
1270
1271
		// @todo Figure out how to hook into autosave for saving meta
1272
1273
		// Block Autosave and Revisions
1274
		if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || in_array( $post->post_type, $blacklisted_types ) ) {
1275
			return;
1276
		}
1277
1278
		// Block Quick Edits / Bulk Edits
1279
		if ( 'edit.php' === pods_v( 'pagenow', 'global' ) && ( 'inline-save' === pods_v( 'action', 'post' ) || null !== pods_v( 'bulk_edit', 'get' ) || is_array( pods_v( 'post', 'get' ) ) ) ) {
1280
			return;
1281
		}
1282
1283
		// Block Trash
1284
		if ( in_array( pods_v( 'action', 'get' ), array( 'untrash', 'trash' ), true ) ) {
1285
			return;
1286
		}
1287
1288
		// Block Auto-drafting and Trash (not via Admin action)
1289
		$blacklisted_status = array(
1290
			'auto-draft',
1291
			'trash',
1292
		);
1293
1294
		$blacklisted_status = apply_filters( 'pods_meta_save_post_blacklist_status', $blacklisted_status, $post_id, $post );
1295
1296
		if ( in_array( $post->post_status, $blacklisted_status ) ) {
1297
			return;
1298
		}
1299
1300
		$groups = $this->groups_get( 'post_type', $post->post_type );
1301
1302
		$id = $post_id;
1303
1304
		if ( ! is_object( self::$current_pod ) || self::$current_pod->pod !== $post->post_type ) {
1305
			self::$current_pod = pods( $post->post_type, $id, true );
1306
		} elseif ( is_object( self::$current_pod ) && (int) self::$current_pod->id() !== (int) $id ) {
1307
			self::$current_pod->fetch( $id );
1308
		}
1309
1310
		$pod  = self::$current_pod;
1311
		$data = array();
1312
1313
		if ( $pod ) {
1314
			$rest_enable = (boolean) pods_v( 'rest_enable', $pod->pod_data['options'], false );
1315
1316
			// Block REST API saves, we handle those separately in PodsRESTHandlers
1317
			if ( defined( 'REST_REQUEST' ) && REST_REQUEST && $rest_enable ) {
1318
				return;
1319
			}
1320
		}
1321
		// The following code will run for all post_types (not just Pods)
1322
1323
		if ( false !== $nonced && ! empty( $groups ) ) {
1324
			foreach ( $groups as $group ) {
1325
				if ( empty( $group['fields'] ) ) {
1326
					continue;
1327
				}
1328
1329
				foreach ( $group['fields'] as $field ) {
1330
					if ( false === PodsForm::permission( $field['type'], $field['name'], $field, $group['fields'], $pod, $id ) ) {
1331
						if ( ! pods_v( 'hidden', $field['options'], false ) ) {
1332
							continue;
1333
						}
1334
					}
1335
1336
					$data[ $field['name'] ] = '';
1337
1338
					if ( isset( $_POST[ 'pods_meta_' . $field['name'] ] ) ) {
1339
						$data[ $field['name'] ] = $_POST[ 'pods_meta_' . $field['name'] ];
1340
					}
1341
				}
1342
			}
1343
1344
			if ( $is_new_item ) {
1345
				do_action( 'pods_meta_create_pre_post', $data, $pod, $id, $groups, $post, $post->post_type );
1346
				do_action( "pods_meta_create_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post );
1347
			}
1348
1349
			do_action( 'pods_meta_save_pre_post', $data, $pod, $id, $groups, $post, $post->post_type, $is_new_item );
1350
			do_action( "pods_meta_save_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post, $is_new_item );
1351
		}
1352
1353
		if ( $is_new_item || false !== $nonced ) {
1354
			pods_no_conflict_on( 'post' );
1355
1356
			if ( ! empty( $pod ) ) {
1357
				// Fix for Pods doing it's own sanitizing
1358
				$data = pods_unslash( (array) $data );
1359
1360
				$pod->save( $data, null, null, array( 'is_new_item' => $is_new_item, 'podsmeta' => true ) );
1361
			} elseif ( ! empty( $id ) ) {
1362
				foreach ( $data as $field => $value ) {
1363
					update_post_meta( $id, $field, $value );
1364
				}
1365
			}
1366
1367
			pods_no_conflict_off( 'post' );
1368
		}
1369
1370
		if ( false !== $nonced && ! empty( $groups ) ) {
1371
			if ( $is_new_item ) {
1372
				do_action( 'pods_meta_create_post', $data, $pod, $id, $groups, $post, $post->post_type );
1373
				do_action( "pods_meta_create_post_{$post->post_type}", $data, $pod, $id, $groups, $post );
1374
			}
1375
1376
			do_action( 'pods_meta_save_post', $data, $pod, $id, $groups, $post, $post->post_type, $is_new_item );
1377
			do_action( "pods_meta_save_post_{$post->post_type}", $data, $pod, $id, $groups, $post, $is_new_item );
1378
		}
1379
1380
	}
1381
1382
	/**
1383
	 * Track changed fields before save for posts.
1384
	 *
1385
	 * @param array $data
1386
	 * @param array $postarr
1387
	 *
1388
	 * @return array
1389
	 */
1390
	public function save_post_track_changed_fields( $data, $postarr ) {
1391
1392
		$no_conflict = pods_no_conflict_check( 'post' );
1393
1394
		if ( ! $no_conflict && ! empty( $data['post_type'] ) && ! empty( $postarr['ID'] ) ) {
1395
			$pod = $data['post_type'];
1396
			$id  = $postarr['ID'];
1397
1398
			PodsAPI::handle_changed_fields( $pod, $id, 'reset' );
1399
		}
1400
1401
		return $data;
1402
1403
	}
1404
1405
	/**
1406
	 * @param $form_fields
1407
	 * @param $post
1408
	 *
1409
	 * @return array
1410
	 */
1411
	public function meta_media( $form_fields, $post ) {
1412
1413
		$groups = $this->groups_get( 'media', 'media' );
1414
1415
		if ( empty( $groups ) || 'attachment' == pods_var( 'typenow', 'global' ) ) {
1416
			return $form_fields;
1417
		}
1418
1419
		wp_enqueue_style( 'pods-form' );
1420
		wp_enqueue_script( 'pods' );
1421
1422
		$id = null;
1423
1424
		if ( is_object( $post ) ) {
1425
			$id = $post->ID;
1426
		}
1427
1428
		$pod = null;
1429
1430
		$meta_nonce = PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_media' ), 'hidden' );
1431
1432
		foreach ( $groups as $group ) {
1433
			if ( empty( $group['fields'] ) ) {
1434
				continue;
1435
			}
1436
1437
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1438
				if ( ! is_object( self::$current_pod ) || self::$current_pod->pod != $group['pod']['name'] ) {
1439
					self::$current_pod = pods( $group['pod']['name'], $id, true );
1440
				} elseif ( self::$current_pod->id() != $id ) {
1441
					self::$current_pod->fetch( $id );
1442
				}
1443
1444
				$pod = self::$current_pod;
1445
			}
1446
1447
			foreach ( $group['fields'] as $field ) {
1448
				if ( false === PodsForm::permission( $field['type'], $field['name'], $field, $group['fields'], $pod, $id ) ) {
1449
					if ( ! pods_var( 'hidden', $field['options'], false ) ) {
1450
						continue;
1451
					}
1452
				}
1453
1454
				$value = '';
1455
1456
				if ( ! empty( $pod ) ) {
1457
					$value = $pod->field( array( 'name' => $field['name'], 'in_form' => true ) );
1458
				} elseif ( ! empty( $id ) ) {
1459
					pods_no_conflict_on( 'post' );
1460
1461
					$value = get_post_meta( $id, $field['name'], true );
1462
1463
					pods_no_conflict_off( 'post' );
1464
				}
1465
1466
				$form_fields[ 'pods_meta_' . $field['name'] ] = array(
1467
					'label' => $field['label'],
1468
					'input' => 'html',
1469
					'html'  => PodsForm::field( 'pods_meta_' . $field['name'], $value, $field['type'], $field, $pod, $id ) . $meta_nonce,
1470
					'helps' => PodsForm::comment( 'pods_meta_' . $field['name'], $field['description'], $field )
1471
				);
1472
			}
1473
		}
1474
1475
		$form_fields = apply_filters( 'pods_meta_' . __FUNCTION__, $form_fields );
1476
1477
		return $form_fields;
1478
	}
1479
1480
	/**
1481
	 * @param $post
1482
	 * @param $attachment
1483
	 *
1484
	 * @return mixed
1485
	 */
1486
	public function save_media( $post, $attachment ) {
1487
1488
		$groups = $this->groups_get( 'media', 'media' );
1489
1490
		if ( empty( $groups ) ) {
1491
			return $post;
1492
		}
1493
1494
		$post_id = $attachment;
1495
1496
		if ( empty( $_POST ) || ! wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_media' ) ) {
1497
			return $post;
1498
		}
1499
1500
		if ( is_array( $post ) && ! empty( $post ) && isset( $post['ID'] ) && 'attachment' == $post['post_type'] ) {
1501
			$post_id = $post['ID'];
1502
		}
1503
1504
		if ( is_array( $post_id ) || empty( $post_id ) ) {
1505
			return $post;
1506
		}
1507
1508
		$data = array();
1509
1510
		$id  = $post_id;
1511
		$pod = null;
1512
1513
		foreach ( $groups as $group ) {
1514
			if ( empty( $group['fields'] ) ) {
1515
				continue;
1516
			}
1517
1518
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1519
				if ( ! is_object( self::$current_pod ) || self::$current_pod->pod != $group['pod']['name'] ) {
1520
					self::$current_pod = pods( $group['pod']['name'], $id, true );
1521
				} elseif ( self::$current_pod->id() != $id ) {
1522
					self::$current_pod->fetch( $id );
1523
				}
1524
1525
				$pod = self::$current_pod;
1526
			}
1527
1528
			foreach ( $group['fields'] as $field ) {
1529
1530
				if ( false === PodsForm::permission( $field['type'], $field['name'], $field, $group['fields'], $pod, $id ) ) {
1531
					if ( ! pods_var( 'hidden', $field['options'], false ) ) {
1532
						continue;
1533
					}
1534
				}
1535
1536
				$data[ $field['name'] ] = '';
1537
1538
				if ( isset( $_POST[ 'pods_meta_' . $field['name'] ] ) ) {
1539
					$data[ $field['name'] ] = $_POST[ 'pods_meta_' . $field['name'] ];
1540
				}
1541
			}
1542
		}
1543
1544
		if ( $pod ) {
1545
			$rest_enable = (boolean) pods_v( 'rest_enable', $pod->pod_data['options'], false );
1546
1547
			// Block REST API saves, we handle those separately in PodsRESTHandlers
1548
			if ( defined( 'REST_REQUEST' ) && REST_REQUEST && $rest_enable ) {
1549
				return $post;
1550
			}
1551
		}
1552
1553
		do_action( 'pods_meta_save_pre_media', $data, $pod, $id, $groups, $post, $attachment );
1554
1555
		if ( ! empty( $pod ) ) {
1556
			// Fix for Pods doing it's own sanitization
1557
			$data = pods_unslash( (array) $data );
1558
1559
			$pod->save( $data, null, null, array( 'podsmeta' => true ) );
1560
		} elseif ( ! empty( $id ) ) {
1561
			pods_no_conflict_on( 'post' );
1562
1563
			foreach ( $data as $field => $value ) {
1564
				update_post_meta( $id, $field, $value );
1565
			}
1566
1567
			pods_no_conflict_off( 'post' );
1568
		}
1569
1570
		do_action( 'pods_meta_save_media', $data, $pod, $id, $groups, $post, $attachment );
1571
1572
		return $post;
1573
	}
1574
1575
	/**
1576
	 *
1577
	 */
1578
	public function save_media_ajax() {
1579
1580
		if ( ! isset( $_POST['id'] ) || empty( $_POST['id'] ) || absint( $_POST['id'] ) < 1 ) {
1581
			return;
1582
		}
1583
1584
		$id = absint( $_POST['id'] );
1585
1586
		if ( ! isset( $_POST['nonce'] ) || empty( $_POST['nonce'] ) ) {
1587
			return;
1588
		}
1589
1590
		check_ajax_referer( 'update-post_' . $id, 'nonce' );
1591
1592
		if ( ! current_user_can( 'edit_post', $id ) ) {
1593
			return;
1594
		}
1595
1596
		$post = get_post( $id, ARRAY_A );
1597
1598
		if ( 'attachment' != $post['post_type'] ) {
1599
			return;
1600
		}
1601
1602
		// fix ALL THE THINGS
1603
1604
		if ( ! isset( $_REQUEST['attachments'] ) ) {
1605
			$_REQUEST['attachments'] = array();
1606
		}
1607
1608
		if ( ! isset( $_REQUEST['attachments'][ $id ] ) ) {
1609
			$_REQUEST['attachments'][ $id ] = array();
1610
		}
1611
1612
		if ( empty( $_REQUEST['attachments'][ $id ] ) ) {
1613
			$_REQUEST['attachments'][ $id ]['_fix_wp'] = 1;
1614
		}
1615
	}
1616
1617
	/**
1618
	 * @param      $tag
1619
	 * @param null $taxonomy
1620
	 */
1621
	public function meta_taxonomy( $tag, $taxonomy = null ) {
1622
1623
		wp_enqueue_style( 'pods-form' );
1624
		wp_enqueue_script( 'pods' );
1625
1626
		do_action( 'pods_meta_' . __FUNCTION__, $tag, $taxonomy );
1627
1628
		$taxonomy_name = $taxonomy;
1629
1630
		if ( ! is_object( $tag ) ) {
1631
			$taxonomy_name = $tag;
1632
		}
1633
1634
		$groups = $this->groups_get( 'taxonomy', $taxonomy_name );
1635
1636
		$id = null;
1637
1638
		if ( is_object( $tag ) ) {
1639
			$id = $tag->term_id;
1640
		}
1641
1642
		$pod = null;
1643
1644
		echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_taxonomy' ), 'hidden' );
1645
1646
		foreach ( $groups as $group ) {
1647
			if ( empty( $group['fields'] ) ) {
1648
				continue;
1649
			}
1650
1651
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1652
				if ( ! is_object( self::$current_pod ) || self::$current_pod->pod != $group['pod']['name'] ) {
1653
					self::$current_pod = pods( $group['pod']['name'], $id, true );
1654
				} elseif ( self::$current_pod->id() != $id ) {
1655
					self::$current_pod->fetch( $id );
1656
				}
1657
1658
				$pod = self::$current_pod;
1659
			}
1660
1661
			foreach ( $group['fields'] as $field ) {
1662
				if ( false === PodsForm::permission( $field['type'], $field['name'], $field, $group['fields'], $pod, $id ) ) {
1663
					if ( pods_var( 'hidden', $field['options'], false ) ) {
1664
						$field['type'] = 'hidden';
1665
					} else {
1666
						continue;
1667
					}
1668
				} elseif ( ! pods_has_permissions( $field['options'] ) && pods_var( 'hidden', $field['options'], false ) ) {
1669
					$field['type'] = 'hidden';
1670
				}
1671
1672
				$value = '';
1673
1674
				if ( ! empty( $pod ) ) {
1675
					$value = $pod->field( array( 'name' => $field['name'], 'in_form' => true ) );
1676
				}
1677
1678
				if ( ! is_object( $tag ) ) {
1679
					?>
1680
					<div class="form-field pods-field" style="<?php echo esc_attr( 'hidden' == $field['type'] ? 'display:none;' : '' ); ?>">
1681
						<?php
1682
						echo PodsForm::label( 'pods_meta_' . $field['name'], $field['label'], $field['help'], $field );
1683
						echo PodsForm::field( 'pods_meta_' . $field['name'], $value, $field['type'], $field, $pod, $id );
1684
						echo PodsForm::comment( 'pods_meta_' . $field['name'], $field['description'], $field );
1685
						?>
1686
					</div>
1687
					<?php
1688
				} else {
1689
					?>
1690
					<tr class="form-field pods-field <?php echo esc_attr( 'pods-form-ui-row-type-' . $field['type'] . ' pods-form-ui-row-name-' . PodsForm::clean( $field['name'], true ) ); ?>" style="<?php echo esc_attr( 'hidden' == $field['type'] ? 'display:none;' : '' ); ?>">
1691
						<th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field['name'], $field['label'], $field['help'], $field ); ?></th>
1692
						<td>
1693
							<?php
1694
							echo PodsForm::field( 'pods_meta_' . $field['name'], $value, $field['type'], $field, $pod, $id );
1695
							echo PodsForm::comment( 'pods_meta_' . $field['name'], $field['description'], $field );
1696
							?>
1697
						</td>
1698
					</tr>
1699
					<?php
1700
				}
1701
			}
1702
		}
1703
1704
		do_action( 'pods_meta_' . __FUNCTION__ . '_post', $tag, $taxonomy );
1705
	}
1706
1707
	/**
1708
	 * @param $term_id
1709
	 * @param $term_taxonomy_id
1710
	 * @param $taxonomy
1711
	 */
1712
	public function save_taxonomy( $term_id, $term_taxonomy_id, $taxonomy ) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
1713
1714
		$is_new_item = false;
1715
1716
		if ( 'create_term' == current_filter() ) {
1717
			$is_new_item = true;
1718
		}
1719
1720
		if ( empty( $_POST ) || ! wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_taxonomy' ) ) {
1721
			return $term_id;
1722
		}
1723
1724
		// Block Quick Edits / Bulk Edits
1725
		if ( 'inline-save-tax' == pods_var( 'action', 'post' ) || null != pods_var( 'delete_tags', 'post' ) ) {
1726
			return $term_id;
1727
		}
1728
1729
		$groups = $this->groups_get( 'taxonomy', $taxonomy );
1730
1731
		if ( empty( $groups ) ) {
1732
			return $term_id;
1733
		}
1734
1735
		$term = null;
1736
1737
		$id  = $term_id;
1738
		$pod = null;
1739
1740
		$has_fields = false;
1741
1742
		foreach ( $groups as $group ) {
1743
			if ( empty( $group['fields'] ) ) {
1744
				continue;
1745
			}
1746
1747
			if ( null === $term ) {
1748
				$term = get_term( $term_id, $taxonomy );
1749
1750
				$data = array(
1751
					'name' => $term->name
1752
				);
1753
			}
1754
1755
			$has_fields = true;
1756
1757
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1758
				if ( ! is_object( self::$current_pod ) || self::$current_pod->pod != $group['pod']['name'] ) {
1759
					self::$current_pod = pods( $group['pod']['name'], $id, true );
1760
				} elseif ( self::$current_pod->id() != $id ) {
1761
					self::$current_pod->fetch( $id );
1762
				}
1763
1764
				$pod = self::$current_pod;
1765
			}
1766
1767
			foreach ( $group['fields'] as $field ) {
1768
				if ( false === PodsForm::permission( $field['type'], $field['name'], $field, $group['fields'], $pod, $id ) ) {
1769
					if ( ! pods_var( 'hidden', $field['options'], false ) ) {
1770
						continue;
1771
					}
1772
				}
1773
1774
				$data[ $field['name'] ] = '';
0 ignored issues
show
Bug introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1775
1776
				if ( isset( $_POST[ 'pods_meta_' . $field['name'] ] ) ) {
1777
					$data[ $field['name'] ] = $_POST[ 'pods_meta_' . $field['name'] ];
1778
				}
1779
			}
1780
		}
1781
1782
		if ( $pod ) {
1783
			$rest_enable = (boolean) pods_v( 'rest_enable', $pod->pod_data['options'], false );
1784
1785
			// Block REST API saves, we handle those separately in PodsRESTHandlers
1786
			if ( defined( 'REST_REQUEST' ) && REST_REQUEST && $rest_enable ) {
1787
				return $term_id;
1788
			}
1789
		}
1790
1791
		if ( ! $has_fields ) {
1792
			return $term_id;
1793
		}
1794
1795
		if ( $is_new_item ) {
1796
			do_action( 'pods_meta_create_pre_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1797
			do_action( "pods_meta_create_pre_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1798
		}
1799
1800
		do_action( 'pods_meta_save_pre_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1801
		do_action( "pods_meta_save_pre_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1802
1803
		pods_no_conflict_on( 'taxonomy' );
1804
1805
		if ( ! empty( $pod ) ) {
1806
			// Fix for Pods doing it's own sanitization
1807
			$data = pods_unslash( (array) $data );
1808
1809
			$pod->save( $data, null, null, array( 'is_new_item' => $is_new_item, 'podsmeta' => true ) );
1810
		}
1811
1812
		pods_no_conflict_off( 'taxonomy' );
1813
1814
		if ( $is_new_item ) {
1815
			do_action( 'pods_meta_create_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1816
			do_action( "pods_meta_create_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1817
		}
1818
1819
		do_action( 'pods_meta_save_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1820
		do_action( "pods_meta_save_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1821
1822
		return $term_id;
1823
	}
1824
1825
	/**
1826
	 * Track changed fields before save for terms.
1827
	 *
1828
	 * @param int    $term_id
1829
	 * @param string $taxonomy
1830
	 */
1831
	public function save_taxonomy_track_changed_fields( $term_id, $taxonomy ) {
1832
1833
		$no_conflict = pods_no_conflict_check( 'term' );
1834
1835
		if ( ! $no_conflict ) {
1836
			$pod = $taxonomy;
1837
			$id  = $term_id;
1838
1839
			PodsAPI::handle_changed_fields( $pod, $id, 'reset' );
1840
		}
1841
1842
	}
1843
1844
	/**
1845
	 * @param $user_id
1846
	 */
1847
	public function meta_user( $user_id ) {
1848
1849
		wp_enqueue_style( 'pods-form' );
1850
		wp_enqueue_script( 'pods' );
1851
1852
		do_action( 'pods_meta_' . __FUNCTION__, $user_id );
1853
1854
		$groups = $this->groups_get( 'user', 'user' );
1855
1856
		if ( is_object( $user_id ) ) {
1857
			$user_id = $user_id->ID;
1858
		}
1859
1860
		$id  = $user_id;
1861
		$pod = null;
1862
1863
		foreach ( $groups as $group ) {
1864
			if ( empty( $group['fields'] ) ) {
1865
				continue;
1866
			}
1867
1868
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1869
				if ( ! is_object( self::$current_pod ) || self::$current_pod->pod != $group['pod']['name'] ) {
1870
					self::$current_pod = pods( $group['pod']['name'], $id, true );
1871
				} elseif ( self::$current_pod->id() != $id ) {
1872
					self::$current_pod->fetch( $id );
1873
				}
1874
1875
				$pod = self::$current_pod;
1876
			}
1877
1878
			$hidden_fields = array();
1879
			?>
1880
			<h3><?php echo $group['label']; ?></h3>
1881
1882
			<?php echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_user' ), 'hidden' ); ?>
1883
1884
			<table class="form-table pods-meta">
1885
				<tbody>
1886
				<?php
1887
				foreach ( $group['fields'] as $field ) {
1888
1889
					if ( false === PodsForm::permission( $field['type'], $field['name'], $field, $group['fields'], $pod, $id ) ) {
1890
						if ( pods_var( 'hidden', $field['options'], false ) ) {
1891
							$field['type'] = 'hidden';
1892
						} else {
1893
							continue;
1894
						}
1895
					} elseif ( ! pods_has_permissions( $field['options'] ) && pods_var( 'hidden', $field['options'], false ) ) {
1896
						$field['type'] = 'hidden';
1897
					}
1898
1899
					$value = '';
1900
1901
					if ( ! empty( $pod ) ) {
1902
						$value = $pod->field( array( 'name' => $field['name'], 'in_form' => true ) );
1903
					} elseif ( ! empty( $id ) ) {
1904
						pods_no_conflict_on( 'user' );
1905
1906
						$value = get_user_meta( $id, $field['name'], true );
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
1907
1908
						pods_no_conflict_off( 'user' );
1909
					}
1910
1911
					if ( 'hidden' == $field['type'] ) {
1912
						$hidden_fields[] = array(
1913
							'field' => $field,
1914
							'value' => $value
1915
						);
1916
					} else {
1917
						?>
1918
						<tr class="form-field pods-field <?php echo esc_attr( 'pods-form-ui-row-type-' . $field['type'] . ' pods-form-ui-row-name-' . PodsForm::clean( $field['name'], true ) ); ?>">
1919
							<th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field['name'], $field['label'], $field['help'], $field ); ?></th>
1920
							<td>
1921
								<?php echo PodsForm::field( 'pods_meta_' . $field['name'], $value, $field['type'], $field, $pod, $id ); ?>
1922
								<?php echo PodsForm::comment( 'pods_meta_' . $field['name'], $field['description'], $field ); ?>
1923
							</td>
1924
						</tr>
1925
						<?php
1926
					}
1927
				}
1928
				?>
1929
				</tbody>
1930
			</table>
1931
			<?php
1932
			foreach ( $hidden_fields as $hidden_field ) {
1933
				$field_data = $hidden_field['field'];
1934
1935
				echo PodsForm::field( 'pods_meta_' . $field_data['name'], $hidden_field['value'], 'hidden', $field_data );
1936
			}
1937
		}
1938
1939
		do_action( 'pods_meta_' . __FUNCTION__ . '_post', $user_id );
1940
	}
1941
1942
	/**
1943
	 * Handle integration with the user_register and profile_update hooks.
1944
	 *
1945
	 * @see wp_insert_user
1946
	 *
1947
	 * @param int         $user_id       User ID.
1948
	 * @param object|null $old_user_data Object containing user's data prior to update.
1949
	 */
1950
	public function save_user( $user_id, $old_user_data = null ) {
1951
1952
		$is_new_item = false;
1953
1954
		if ( 'user_register' == current_filter() ) {
1955
			$is_new_item = true;
1956
		}
1957
1958
		$nonced = wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_user' );
1959
1960
		if ( ! $is_new_item && false === $nonced ) {
1961
			return;
1962
		}
1963
1964
		if ( is_object( $user_id ) ) {
1965
			$user_id = $user_id->ID;
1966
		}
1967
1968
		$groups = $this->groups_get( 'user', 'user' );
1969
1970
		$id = $user_id;
1971
1972
		if ( ! is_object( self::$current_pod ) || self::$current_pod->pod !== 'user' ) {
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
1973
			self::$current_pod = pods( 'user', $id, true );
1974
		} elseif ( is_object( self::$current_pod ) && (int) self::$current_pod->id() !== (int) $id ) {
1975
			self::$current_pod->fetch( $id );
1976
		}
1977
1978
		$pod  = self::$current_pod;
1979
		$data = array();
1980
1981
		if ( $pod ) {
1982
			$rest_enable = (boolean) pods_v( 'rest_enable', $pod->pod_data['options'], false );
1983
1984
			// Block REST API saves, we handle those separately in PodsRESTHandlers
1985
			if ( defined( 'REST_REQUEST' ) && REST_REQUEST && $rest_enable ) {
1986
				return;
1987
			}
1988
		}
1989
1990
		if ( false !== $nonced && ! empty( $groups ) ) {
1991
			foreach ( $groups as $group ) {
1992
				if ( empty( $group['fields'] ) ) {
1993
					continue;
1994
				}
1995
1996
				foreach ( $group['fields'] as $field ) {
1997
					if ( false === PodsForm::permission( $field['type'], $field['name'], $field, $group['fields'], $pod, $id ) ) {
1998
						if ( ! pods_v( 'hidden', $field['options'], false ) ) {
1999
							continue;
2000
						}
2001
					}
2002
2003
					$data[ $field['name'] ] = '';
2004
2005
					if ( isset( $_POST[ 'pods_meta_' . $field['name'] ] ) ) {
2006
						$data[ $field['name'] ] = $_POST[ 'pods_meta_' . $field['name'] ];
2007
					}
2008
				}
2009
			}
2010
2011
			if ( $is_new_item ) {
2012
				do_action( 'pods_meta_create_pre_user', $data, $pod, $id, $groups );
2013
			}
2014
2015
			do_action( 'pods_meta_save_pre_user', $data, $pod, $id, $groups, $is_new_item );
2016
		}
2017
2018
		if ( $is_new_item || false !== $nonced ) {
2019
			pods_no_conflict_on( 'user' );
2020
2021
			if ( ! empty( $pod ) ) {
2022
				// Fix for Pods doing it's own sanitizing
2023
				$data = pods_unslash( (array) $data );
2024
2025
				$pod->save( $data, null, null, array( 'is_new_item' => $is_new_item, 'podsmeta' => true ) );
2026
			} elseif ( ! empty( $id ) ) {
2027
				foreach ( $data as $field => $value ) {
2028
					update_user_meta( $id, $field, $value );
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
2029
				}
2030
			}
2031
2032
			pods_no_conflict_off( 'user' );
2033
		}
2034
2035
		if ( false !== $nonced && ! empty( $groups ) ) {
2036
			if ( $is_new_item ) {
2037
				do_action( 'pods_meta_create_user', $data, $pod, $id, $groups );
2038
			}
2039
2040
			do_action( 'pods_meta_save_user', $data, $pod, $id, $groups, $is_new_item );
2041
		}
2042
2043
	}
2044
2045
	/**
2046
	 * Track changed fields before save for users.
2047
	 *
2048
	 * @param string $user_login
2049
	 *
2050
	 * @return string
2051
	 */
2052
	public function save_user_track_changed_fields( $user_login ) {
2053
2054
		$no_conflict = pods_no_conflict_check( 'user' );
2055
2056
		if ( ! $no_conflict ) {
2057
			$user = get_user_by( 'login', $user_login );
2058
2059
			if ( $user && ! is_wp_error( $user ) ) {
2060
				$pod = 'user';
2061
				$id  = $user->ID;
2062
2063
				PodsAPI::handle_changed_fields( $pod, $id, 'reset' );
2064
			}
2065
		}
2066
2067
		return $user_login;
2068
2069
	}
2070
2071
	/**
2072
	 * @param $commenter
2073
	 * @param $user_identity
2074
	 */
2075
	public function meta_comment_new_logged_in( $commenter, $user_identity ) {
2076
2077
		wp_enqueue_style( 'pods-form' );
2078
		wp_enqueue_script( 'pods' );
2079
2080
		do_action( 'pods_meta_' . __FUNCTION__, $commenter, $user_identity );
2081
2082
		$groups = $this->groups_get( 'comment', 'comment' );
2083
2084
		$id  = null;
2085
		$pod = null;
2086
2087
		echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' );
2088
2089
		foreach ( $groups as $group ) {
2090
			if ( empty( $group['fields'] ) ) {
2091
				continue;
2092
			}
2093
2094
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
2095
				if ( ! is_object( self::$current_pod ) || self::$current_pod->pod != $group['pod']['name'] ) {
2096
					self::$current_pod = pods( $group['pod']['name'], $id, true );
2097
				} elseif ( self::$current_pod->id() != $id ) {
2098
					self::$current_pod->fetch( $id );
2099
				}
2100
2101
				$pod = self::$current_pod;
2102
			}
2103
2104
			foreach ( $group['fields'] as $field ) {
2105
				if ( false === PodsForm::permission( $field['type'], $field['name'], $field, $group['fields'], $pod, $id ) ) {
2106
					if ( pods_var( 'hidden', $field['options'], false ) ) {
2107
						$field['type'] = 'hidden';
2108
					} else {
2109
						continue;
2110
					}
2111
				} elseif ( ! pods_has_permissions( $field['options'] ) && pods_var( 'hidden', $field['options'], false ) ) {
2112
					$field['type'] = 'hidden';
2113
				}
2114
2115
				$value = '';
2116
2117
				if ( ! empty( $pod ) ) {
2118
					$value = $pod->field( array( 'name' => $field['name'], 'in_form' => true ) );
2119
				} elseif ( ! empty( $id ) ) {
2120
					pods_no_conflict_on( 'comment' );
2121
2122
					$value = get_comment_meta( $id, $field['name'], true );
2123
2124
					pods_no_conflict_off( 'comment' );
2125
				}
2126
				?>
2127
				<p class="comment-form-author comment-form-pods-meta-<?php echo esc_attr( $field['name'] ); ?>  pods-field" style="<?php echo esc_attr( 'hidden' == $field['type'] ? 'display:none;' : '' ); ?>">
2128
					<?php
2129
					echo PodsForm::label( 'pods_meta_' . $field['name'], $field['label'], $field['help'], $field );
2130
					echo PodsForm::field( 'pods_meta_' . $field['name'], $value, $field['type'], $field, $pod, $id );
2131
					echo PodsForm::comment( 'pods_meta_' . $field['name'], $field['description'], $field );
2132
					?>
2133
				</p>
2134
				<?php
2135
			}
2136
		}
2137
2138
		do_action( 'pods_meta_' . __FUNCTION__ . '_post', $commenter, $user_identity );
2139
	}
2140
2141
	/**
2142
	 * @param $form_fields
2143
	 *
2144
	 * @return array
2145
	 */
2146
	public function meta_comment_new( $form_fields ) {
2147
2148
		wp_enqueue_style( 'pods-form' );
2149
		wp_enqueue_script( 'pods' );
2150
2151
		$groups = $this->groups_get( 'comment', 'comment' );
2152
2153
		$id  = null;
2154
		$pod = null;
2155
2156
		$form_fields['pods_meta'] = PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' );
2157
2158
		foreach ( $groups as $group ) {
2159
			if ( empty( $group['fields'] ) ) {
2160
				continue;
2161
			}
2162
2163
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
2164
				if ( ! is_object( self::$current_pod ) || self::$current_pod->pod != $group['pod']['name'] ) {
2165
					self::$current_pod = pods( $group['pod']['name'], $id, true );
2166
				} elseif ( self::$current_pod->id() != $id ) {
2167
					self::$current_pod->fetch( $id );
2168
				}
2169
2170
				$pod = self::$current_pod;
2171
			}
2172
2173
			foreach ( $group['fields'] as $field ) {
2174
2175
				if ( false === PodsForm::permission( $field['type'], $field['name'], $field, $group['fields'], $pod, $id ) ) {
2176
					if ( pods_var( 'hidden', $field['options'], false ) ) {
2177
						$field['type'] = 'hidden';
2178
					} else {
2179
						continue;
2180
					}
2181
				} elseif ( ! pods_has_permissions( $field['options'] ) && pods_var( 'hidden', $field['options'], false ) ) {
2182
					$field['type'] = 'hidden';
2183
				}
2184
2185
				$value = '';
2186
2187
				if ( ! empty( $pod ) ) {
2188
					$value = $pod->field( array( 'name' => $field['name'], 'in_form' => true ) );
2189
				} elseif ( ! empty( $id ) ) {
2190
					pods_no_conflict_on( 'comment' );
2191
2192
					$value = get_comment_meta( $id, $field['name'], true );
2193
2194
					pods_no_conflict_off( 'comment' );
2195
				}
2196
2197
				ob_start();
2198
				?>
2199
				<p class="comment-form-author comment-form-pods-meta-<?php echo esc_attr( $field['name'] ); ?> pods-field" style="<?php echo esc_attr( 'hidden' == $field['type'] ? 'display:none;' : '' ); ?>">
2200
					<?php
2201
					echo PodsForm::label( 'pods_meta_' . $field['name'], $field['label'], $field['help'], $field );
2202
					echo PodsForm::field( 'pods_meta_' . $field['name'], $value, $field['type'], $field, $pod, $id );
2203
					echo PodsForm::comment( 'pods_meta_' . $field['name'], $field['description'], $field );
2204
					?>
2205
				</p>
2206
				<?php
2207
				$form_fields[ 'pods_meta_' . $field['name'] ] = ob_get_clean();
2208
			}
2209
		}
2210
2211
		$form_fields = apply_filters( 'pods_meta_' . __FUNCTION__, $form_fields );
2212
2213
		return $form_fields;
2214
	}
2215
2216
	/**
2217
	 * @param      $comment_type
2218
	 * @param null $comment
2219
	 */
2220
	public function meta_comment_add( $comment_type, $comment = null ) {
2221
2222
		if ( is_object( $comment ) && isset( $comment_type->comment_type ) ) {
2223
			$comment_type = $comment->comment_type;
2224
		}
2225
2226
		if ( is_object( $comment_type ) && isset( $comment_type->comment_type ) ) {
2227
			$comment      = $comment_type;
2228
			$comment_type = $comment_type->comment_type;
2229
		}
2230
2231
		if ( is_object( $comment_type ) ) {
2232
			return;
2233
		} elseif ( empty( $comment_type ) ) {
2234
			$comment_type = 'comment';
2235
		}
2236
2237
		$groups = $this->groups_get( 'comment', $comment_type );
2238
2239
		foreach ( $groups as $group ) {
2240
			if ( empty( $group['fields'] ) ) {
2241
				continue;
2242
			}
2243
2244
			$field_found = false;
2245
2246
			foreach ( $group['fields'] as $field ) {
2247
				if ( false === PodsForm::permission( $field['type'], $field['name'], $field, $group['fields'], null, null ) ) {
2248
					if ( pods_var( 'hidden', $field['options'], false ) ) {
2249
						$field_found = true;
2250
						break;
2251
					} else {
2252
						continue;
2253
					}
2254
				} else {
2255
					$field_found = true;
2256
					break;
2257
				}
2258
			}
2259
2260
			if ( $field_found ) {
2261
				add_meta_box( 'pods-meta-' . sanitize_title( $group['label'] ), $group['label'], array(
2262
						$this,
2263
						'meta_comment'
2264
					), $comment_type, $group['context'], $group['priority'], array( 'group' => $group ) );
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 20.
Loading history...
2265
			}
2266
		}
2267
	}
2268
2269
	/**
2270
	 * @param $comment
2271
	 * @param $metabox
2272
	 */
2273
	public function meta_comment( $comment, $metabox ) {
2274
2275
		wp_enqueue_style( 'pods-form' );
2276
		wp_enqueue_script( 'pods' );
2277
2278
		do_action( 'pods_meta_' . __FUNCTION__, $comment, $metabox );
2279
2280
		$hidden_fields = array();
2281
2282
		echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' );
2283
		?>
2284
		<table class="form-table editcomment pods-metabox">
2285
			<?php
2286
			$id = null;
2287
2288
			if ( is_object( $comment ) ) {
2289
				$id = $comment->comment_ID;
2290
			}
2291
2292
			if ( ! is_object( self::$current_pod ) || self::$current_pod->pod != $metabox['args']['group']['pod']['name'] ) {
2293
				self::$current_pod = pods( $metabox['args']['group']['pod']['name'], $id, true );
2294
			} elseif ( self::$current_pod->id() != $id ) {
2295
				self::$current_pod->fetch( $id );
2296
			}
2297
2298
			$pod = self::$current_pod;
2299
2300
			foreach ( $metabox['args']['group']['fields'] as $field ) {
2301
				if ( false === PodsForm::permission( $field['type'], $field['name'], $field, $metabox['args']['group']['fields'], $pod, $id ) ) {
2302
					if ( pods_var( 'hidden', $field['options'], false ) ) {
2303
						$field['type'] = 'hidden';
2304
					} else {
2305
						continue;
2306
					}
2307
				} elseif ( ! pods_has_permissions( $field['options'] ) && pods_var( 'hidden', $field['options'], false ) ) {
2308
					$field['type'] = 'hidden';
2309
				}
2310
2311
				$value = '';
2312
2313
				if ( ! empty( $pod ) ) {
2314
					$value = $pod->field( array( 'name' => $field['name'], 'in_form' => true ) );
2315
				}
2316
2317
				if ( 'hidden' == $field['type'] ) {
2318
					$hidden_fields[] = array(
2319
						'field' => $field,
2320
						'value' => $value
2321
					);
2322
				} else {
2323
					?>
2324
					<tr class="form-field pods-field <?php echo esc_attr( 'pods-form-ui-row-type-' . $field['type'] . ' pods-form-ui-row-name-' . PodsForm::clean( $field['name'], true ) ); ?>">
2325
						<th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field['name'], $field['label'], $field['help'], $field ); ?></th>
2326
						<td>
2327
							<?php echo PodsForm::field( 'pods_meta_' . $field['name'], $value, $field['type'], $field, $pod, $id ); ?>
2328
							<?php echo PodsForm::comment( 'pods_meta_' . $field['name'], $field['description'], $field ); ?>
2329
						</td>
2330
					</tr>
2331
					<?php
2332
				}
2333
			}
2334
			?>
2335
		</table>
2336
		<?php
2337
		foreach ( $hidden_fields as $hidden_field ) {
2338
			$field_data = $hidden_field['field'];
2339
2340
			echo PodsForm::field( 'pods_meta_' . $field_data['name'], $hidden_field['value'], 'hidden', $field_data );
2341
		}
2342
2343
		do_action( 'pods_meta_' . __FUNCTION__ . '_post', $comment, $metabox );
2344
	}
2345
2346
	/**
2347
	 * @param $approved
2348
	 * @param $commentdata
2349
	 */
2350
	public function validate_comment( $approved, $commentdata ) {
2351
2352
		$groups = $this->groups_get( 'comment', 'comment' );
2353
2354
		if ( empty( $groups ) ) {
2355
			return $approved;
2356
		}
2357
2358
		$data = array();
2359
2360
		$pod = null;
2361
		$id  = null;
2362
2363
		foreach ( $groups as $group ) {
2364
			if ( empty( $group['fields'] ) ) {
2365
				continue;
2366
			}
2367
2368
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
2369
				if ( ! is_object( self::$current_pod ) || self::$current_pod->pod != $group['pod']['name'] ) {
2370
					self::$current_pod = pods( $group['pod']['name'], $id, true );
2371
				} elseif ( self::$current_pod->id() != $id ) {
2372
					self::$current_pod->fetch( $id );
2373
				}
2374
2375
				$pod = self::$current_pod;
2376
			}
2377
2378
			foreach ( $group['fields'] as $field ) {
2379
2380
				if ( false === PodsForm::permission( $field['type'], $field['name'], $field, $group['fields'], $pod, $id ) ) {
2381
					if ( ! pods_var( 'hidden', $field['options'], false ) ) {
2382
						continue;
2383
					}
2384
				}
2385
2386
				$data[ $field['name'] ] = '';
2387
2388
				if ( isset( $_POST[ 'pods_meta_' . $field['name'] ] ) ) {
2389
					$data[ $field['name'] ] = $_POST[ 'pods_meta_' . $field['name'] ];
2390
				}
2391
2392
				$validate = pods_api()->handle_field_validation( $data[ $field['name'] ], $field['name'], pods_api()->get_wp_object_fields( 'comment' ), $pod->fields(), $pod, array() );
2393
2394
				if ( false === $validate ) {
2395
					$validate = sprintf( __( 'There was an issue validating the field %s', 'pods' ), $field['label'] );
2396
				}
2397
2398
				if ( ! is_bool( $validate ) && ! empty( $validate ) ) {
2399
					return pods_error( $validate, $this );
2400
				}
2401
			}
2402
		}
2403
2404
		return $approved;
2405
	}
2406
2407
	/**
2408
	 * @param $comment_id
2409
	 */
2410
	public function save_comment( $comment_id ) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
2411
2412
		$groups = $this->groups_get( 'comment', 'comment' );
2413
2414
		if ( empty( $groups ) ) {
2415
			return $comment_id;
2416
		} elseif ( empty( $_POST ) ) {
2417
			return $comment_id;
2418
		} elseif ( ! wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_comment' ) ) {
2419
			return $comment_id;
2420
		}
2421
2422
		$data = array();
2423
2424
		$id  = $comment_id;
2425
		$pod = null;
2426
2427
		foreach ( $groups as $group ) {
2428
			if ( empty( $group['fields'] ) ) {
2429
				continue;
2430
			}
2431
2432
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
2433
				if ( ! is_object( self::$current_pod ) || self::$current_pod->pod != $group['pod']['name'] ) {
2434
					self::$current_pod = pods( $group['pod']['name'], $id, true );
2435
				} elseif ( self::$current_pod->id() != $id ) {
2436
					self::$current_pod->fetch( $id );
2437
				}
2438
2439
				$pod = self::$current_pod;
2440
			}
2441
2442
			foreach ( $group['fields'] as $field ) {
2443
				if ( false === PodsForm::permission( $field['type'], $field['name'], $field, $group['fields'], $pod, $id ) ) {
2444
					if ( ! pods_var( 'hidden', $field['options'], false ) ) {
2445
						continue;
2446
					}
2447
				}
2448
2449
				$data[ $field['name'] ] = '';
2450
2451
				if ( isset( $_POST[ 'pods_meta_' . $field['name'] ] ) ) {
2452
					$data[ $field['name'] ] = $_POST[ 'pods_meta_' . $field['name'] ];
2453
				}
2454
			}
2455
		}
2456
2457
		if ( $pod ) {
2458
			$rest_enable = (boolean) pods_v( 'rest_enable', $pod->pod_data['options'], false );
2459
2460
			// Block REST API saves, we handle those separately in PodsRESTHandlers
2461
			if ( defined( 'REST_REQUEST' ) && REST_REQUEST && $rest_enable ) {
2462
				return $comment_id;
2463
			}
2464
		}
2465
2466
		do_action( 'pods_meta_save_pre_comment', $data, $pod, $id, $groups );
2467
2468
		if ( ! empty( $pod ) ) {
2469
			// Fix for Pods doing it's own sanitization
2470
			$data = pods_unslash( (array) $data );
2471
2472
			$pod->save( $data, null, null, array( 'podsmeta' => true ) );
2473
		} elseif ( ! empty( $id ) ) {
2474
			pods_no_conflict_on( 'comment' );
2475
2476
			foreach ( $data as $field => $value ) {
2477
				update_comment_meta( $id, $field, $value );
2478
			}
2479
2480
			pods_no_conflict_off( 'comment' );
2481
		}
2482
2483
		do_action( 'pods_meta_save_comment', $data, $pod, $id, $groups );
2484
2485
		return $comment_id;
2486
	}
2487
2488
	/**
2489
	 * Track changed fields before save for comments.
2490
	 *
2491
	 * @param array $data       The new, processed comment data.
2492
	 * @param array $comment    The old, unslashed comment data.
2493
	 * @param array $commentarr The new, raw comment data.
2494
	 *
2495
	 * @return array
2496
	 */
2497
	public function save_comment_track_changed_fields( $data, $comment, $commentarr ) {
2498
2499
		$no_conflict = pods_no_conflict_check( 'user' );
2500
2501
		if ( ! $no_conflict && ! empty( $comment['comment_type'] ) && ! empty( $comment['comment_ID'] ) ) {
2502
			$pod = $comment['comment_type'];
2503
			$id  = $comment['comment_ID'];
2504
2505
			PodsAPI::handle_changed_fields( $pod, $id, 'reset' );
2506
		}
2507
2508
		return $data;
2509
2510
	}
2511
2512
	/**
2513
	 * All *_*_meta filter handler aliases
2514
	 *
2515
	 * @return mixed
2516
	 */
2517
	public function get_post_meta() {
2518
2519
		$args = func_get_args();
2520
2521
		array_unshift( $args, 'post_type' );
2522
2523
		$_null = apply_filters( 'pods_meta_get_post_meta', null, $args );
2524
2525
		if ( null !== $_null ) {
2526
			return $_null;
2527
		}
2528
2529
		return call_user_func_array( array( $this, 'get_meta' ), $args );
2530
	}
2531
2532
	/**
2533
	 * @return mixed
2534
	 */
2535
	public function get_user_meta() {
2536
2537
		$args = func_get_args();
2538
2539
		array_unshift( $args, 'user' );
2540
2541
		$_null = apply_filters( 'pods_meta_get_user_meta', null, $args );
2542
2543
		if ( null !== $_null ) {
2544
			return $_null;
2545
		}
2546
2547
		return call_user_func_array( array( $this, 'get_meta' ), $args );
2548
	}
2549
2550
	/**
2551
	 * @return mixed
2552
	 */
2553
	public function get_comment_meta() {
2554
2555
		$args = func_get_args();
2556
2557
		array_unshift( $args, 'comment' );
2558
2559
		$_null = apply_filters( 'pods_meta_get_comment_meta', null, $args );
2560
2561
		if ( null !== $_null ) {
2562
			return $_null;
2563
		}
2564
2565
		return call_user_func_array( array( $this, 'get_meta' ), $args );
2566
	}
2567
2568
	/**
2569
	 * @return mixed
2570
	 */
2571
	public function get_term_meta() {
2572
2573
		$args = func_get_args();
2574
2575
		array_unshift( $args, 'term' );
2576
2577
		$_null = apply_filters( 'pods_meta_get_term_meta', null, $args );
2578
2579
		if ( null !== $_null ) {
2580
			return $_null;
2581
		}
2582
2583
		return call_user_func_array( array( $this, 'get_meta' ), $args );
2584
	}
2585
2586
	/**
2587
	 * All *_*_meta filter handler aliases
2588
	 *
2589
	 * @return mixed
2590
	 */
2591
	public function get_option() {
2592
2593
		$args = func_get_args();
2594
2595
		array_unshift( $args, 'settings' );
2596
2597
		$_null = apply_filters( 'pods_meta_get_option', null, $args );
2598
2599
		if ( null !== $_null ) {
2600
			return $_null;
2601
		}
2602
2603
		return call_user_func_array( array( $this, 'get_meta' ), $args );
2604
	}
2605
2606
	/**
2607
	 * @return mixed
2608
	 */
2609
	public function add_post_meta() {
2610
2611
		$args = func_get_args();
2612
2613
		array_unshift( $args, 'post_type' );
2614
2615
		$_null = apply_filters( 'pods_meta_add_post_meta', null, $args );
2616
2617
		if ( null !== $_null ) {
2618
			return $_null;
2619
		}
2620
2621
		return call_user_func_array( array( $this, 'add_meta' ), $args );
2622
	}
2623
2624
	/**
2625
	 * @return mixed
2626
	 */
2627
	public function add_user_meta() {
2628
2629
		$args = func_get_args();
2630
2631
		array_unshift( $args, 'user' );
2632
2633
		$_null = apply_filters( 'pods_meta_add_user_meta', null, $args );
2634
2635
		if ( null !== $_null ) {
2636
			return $_null;
2637
		}
2638
2639
		return call_user_func_array( array( $this, 'add_meta' ), $args );
2640
	}
2641
2642
	/**
2643
	 * @return mixed
2644
	 */
2645
	public function add_comment_meta() {
2646
2647
		$args = func_get_args();
2648
2649
		array_unshift( $args, 'comment' );
2650
2651
		$_null = apply_filters( 'pods_meta_add_comment_meta', null, $args );
2652
2653
		if ( null !== $_null ) {
2654
			return $_null;
2655
		}
2656
2657
		return call_user_func_array( array( $this, 'add_meta' ), $args );
2658
	}
2659
2660
	/**
2661
	 * @return mixed
2662
	 */
2663
	public function add_term_meta() {
2664
2665
		$args = func_get_args();
2666
2667
		array_unshift( $args, 'term' );
2668
2669
		$_null = apply_filters( 'pods_meta_add_term_meta', null, $args );
2670
2671
		if ( null !== $_null ) {
2672
			return $_null;
2673
		}
2674
2675
		return call_user_func_array( array( $this, 'add_meta' ), $args );
2676
	}
2677
2678
	/**
2679
	 * @return mixed
2680
	 */
2681
	public function add_option() {
2682
2683
		$args = func_get_args();
2684
2685
		array_unshift( $args, 'settings' );
2686
2687
		$_null = apply_filters( 'pods_meta_add_option', null, $args );
2688
2689
		if ( null !== $_null ) {
2690
			return $_null;
2691
		}
2692
2693
		return call_user_func_array( array( $this, 'add_meta' ), $args );
2694
	}
2695
2696
	/**
2697
	 * @return mixed
2698
	 */
2699
	public function update_post_meta() {
2700
2701
		$args = func_get_args();
2702
2703
		array_unshift( $args, 'post_type' );
2704
2705
		$_null = apply_filters( 'pods_meta_update_post_meta', null, $args );
2706
2707
		if ( null !== $_null ) {
2708
			return $_null;
2709
		}
2710
2711
		return call_user_func_array( array( $this, 'update_meta' ), $args );
2712
	}
2713
2714
	/**
2715
	 * @return mixed
2716
	 */
2717
	public function update_user_meta() {
2718
2719
		$args = func_get_args();
2720
2721
		array_unshift( $args, 'user' );
2722
2723
		$_null = apply_filters( 'pods_meta_update_user_meta', null, $args );
2724
2725
		if ( null !== $_null ) {
2726
			return $_null;
2727
		}
2728
2729
		return call_user_func_array( array( $this, 'update_meta' ), $args );
2730
	}
2731
2732
	/**
2733
	 * @return mixed
2734
	 */
2735
	public function update_comment_meta() {
2736
2737
		$args = func_get_args();
2738
2739
		array_unshift( $args, 'comment' );
2740
2741
		$_null = apply_filters( 'pods_meta_update_comment_meta', null, $args );
2742
2743
		if ( null !== $_null ) {
2744
			return $_null;
2745
		}
2746
2747
		return call_user_func_array( array( $this, 'update_meta' ), $args );
2748
	}
2749
2750
	/**
2751
	 * @return mixed
2752
	 */
2753
	public function update_term_meta() {
2754
2755
		$args = func_get_args();
2756
2757
		array_unshift( $args, 'term' );
2758
2759
		$_null = apply_filters( 'pods_meta_update_term_meta', null, $args );
2760
2761
		if ( null !== $_null ) {
2762
			return $_null;
2763
		}
2764
2765
		return call_user_func_array( array( $this, 'update_meta' ), $args );
2766
	}
2767
2768
	/**
2769
	 * @return mixed
2770
	 */
2771
	public function update_option() {
2772
2773
		$args = func_get_args();
2774
2775
		array_unshift( $args, 'settings' );
2776
2777
		$_null = apply_filters( 'pods_meta_update_option', null, $args );
2778
2779
		if ( null !== $_null ) {
2780
			return $_null;
2781
		}
2782
2783
		return call_user_func_array( array( $this, 'update_meta' ), $args );
2784
	}
2785
2786
	/**
2787
	 * @return mixed
2788
	 */
2789
	public function delete_post_meta() {
2790
2791
		$args = func_get_args();
2792
2793
		array_unshift( $args, 'post_type' );
2794
2795
		$_null = apply_filters( 'pods_meta_delete_post_meta', null, $args );
2796
2797
		if ( null !== $_null ) {
2798
			return $_null;
2799
		}
2800
2801
		return call_user_func_array( array( $this, 'delete_meta' ), $args );
2802
	}
2803
2804
	/**
2805
	 * @return mixed
2806
	 */
2807
	public function delete_user_meta() {
2808
2809
		$args = func_get_args();
2810
2811
		array_unshift( $args, 'user' );
2812
2813
		$_null = apply_filters( 'pods_meta_delete_user_meta', null, $args );
2814
2815
		if ( null !== $_null ) {
2816
			return $_null;
2817
		}
2818
2819
		return call_user_func_array( array( $this, 'delete_meta' ), $args );
2820
	}
2821
2822
	/**
2823
	 * @return mixed
2824
	 */
2825
	public function delete_comment_meta() {
2826
2827
		$args = func_get_args();
2828
2829
		array_unshift( $args, 'comment' );
2830
2831
		$_null = apply_filters( 'pods_meta_delete_comment_meta', null, $args );
2832
2833
		if ( null !== $_null ) {
2834
			return $_null;
2835
		}
2836
2837
		return call_user_func_array( array( $this, 'delete_meta' ), $args );
2838
	}
2839
2840
	/**
2841
	 * @return mixed
2842
	 */
2843
	public function delete_term_meta() {
2844
2845
		$args = func_get_args();
2846
2847
		array_unshift( $args, 'term' );
2848
2849
		$_null = apply_filters( 'pods_meta_delete_term_meta', null, $args );
2850
2851
		if ( null !== $_null ) {
2852
			return $_null;
2853
		}
2854
2855
		return call_user_func_array( array( $this, 'delete_meta' ), $args );
2856
	}
2857
2858
	/**
2859
	 * @return mixed
2860
	 */
2861
	public function delete_option() {
2862
2863
		$args = func_get_args();
2864
2865
		array_unshift( $args, 'settings' );
2866
2867
		$_null = apply_filters( 'pods_meta_delete_option', null, $args );
2868
2869
		if ( null !== $_null ) {
2870
			return $_null;
2871
		}
2872
2873
		return call_user_func_array( array( $this, 'delete_meta' ), $args );
2874
	}
2875
2876
	/*
2877
     * The real meta functions
2878
     */
2879
	/**
2880
	 * @param        $object_type
2881
	 * @param        $object_id
2882
	 * @param string $aux
2883
	 *
2884
	 * @return bool|mixed
2885
	 */
2886
	public function get_object( $object_type, $object_id, $aux = '' ) {
2887
2888
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
2889
2890
		if ( 'term' == $object_type ) {
2891
			$object_type = 'taxonomy';
2892
		}
2893
2894
		if ( 'post_type' == $object_type ) {
2895
			$objects = self::$post_types;
2896
		} elseif ( 'taxonomy' == $object_type ) {
2897
			$objects = self::$taxonomies;
2898
		} elseif ( 'media' == $object_type ) {
2899
			$objects = self::$media;
2900
		} elseif ( 'user' == $object_type ) {
2901
			$objects = self::$user;
2902
		} elseif ( 'comment' == $object_type ) {
2903
			$objects = self::$comment;
2904
		} elseif ( 'settings' == $object_type ) {
2905
			$objects = self::$settings;
2906
		} else {
2907
			return false;
2908
		}
2909
2910
		if ( empty( $objects ) || ! is_array( $objects ) ) {
2911
			return false;
2912
		}
2913
2914
		$object_name = null;
2915
2916
		if ( 'media' == $object_type ) {
2917
			return @current( $objects );
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
2918
		} elseif ( 'user' == $object_type ) {
2919
			return @current( $objects );
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
2920
		} elseif ( 'comment' == $object_type ) {
2921
			return @current( $objects );
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
2922
		} elseif ( 'post_type' == $object_type ) {
2923
			$object = get_post( $object_id );
2924
2925
			if ( ! is_object( $object ) || ! isset( $object->post_type ) ) {
2926
				return false;
2927
			}
2928
2929
			$object_name = $object->post_type;
2930
		} elseif ( 'taxonomy' == $object_type ) {
2931
			$object = get_term( $object_id );
2932
2933
			if ( ! is_object( $object ) || ! isset( $object->taxonomy ) ) {
2934
				return false;
2935
			}
2936
2937
			$object_name = $object->taxonomy;
2938
			if ( empty( $aux ) ) {
2939
				$object_name = $wpdb->get_var( $wpdb->prepare( "SELECT `taxonomy` FROM `{$wpdb->term_taxonomy}` WHERE `term_id` = %d", $object_id ) );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2940
			} else {
2941
				$object_name = $aux;
2942
			}
2943
		} elseif ( 'settings' == $object_type ) {
2944
			$object = $object_id;
2945
		} else {
2946
			return false;
2947
		}
2948
2949
		$reserved_post_types = array(
2950
			'revision'
2951
		);
2952
2953
		$reserved_post_types = apply_filters( 'pods_meta_reserved_post_types', $reserved_post_types, $object_type, $object_id, $object_name, $objects );
2954
2955
		if ( empty( $object_name ) || ( 'post_type' == $object_type && ( 0 === strpos( $object_name, '_pods_' ) ) || in_array( $object_name, $reserved_post_types ) ) ) {
2956
			return false;
2957
		} elseif ( 'attachment' == $object_name ) {
2958
			return @current( self::$media );
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
2959
		}
2960
2961
		$recheck = array();
2962
2963
		// Return first created by Pods, save extended for later
2964
		foreach ( $objects as $pod ) {
2965
			if ( $object_name == $pod['object'] ) {
2966
				$recheck[] = $pod;
2967
			}
2968
2969
			if ( '' == $pod['object'] && $object_name == $pod['name'] ) {
2970
				return $pod;
2971
			}
2972
		}
2973
2974
		// If no objects created by Pods, return first extended
2975
		foreach ( $recheck as $pod ) {
2976
			return $pod;
2977
		}
2978
2979
		return false;
2980
	}
2981
2982
	/**
2983
	 * @param        $object_type
2984
	 * @param null   $_null
2985
	 * @param int    $object_id
2986
	 * @param string $meta_key
2987
	 * @param bool   $single
2988
	 *
2989
	 * @return array|bool|int|mixed|null|string|void
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use null|array|string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
2990
	 */
2991
	public function get_meta( $object_type, $_null = null, $object_id = 0, $meta_key = '', $single = false ) {
2992
2993
		// Enforce boolean as it can be a string sometimes
2994
		$single = filter_var( $single, FILTER_VALIDATE_BOOLEAN );
2995
2996
		$meta_type = $object_type;
2997
2998
		if ( in_array( $meta_type, array( 'post_type', 'media' ) ) ) {
2999
			$meta_type = 'post';
3000
		} elseif ( 'taxonomy' == $meta_type ) {
3001
			$meta_type = 'term';
3002
		}
3003
3004
		if ( empty( $meta_key ) ) {
3005
			if ( ! defined( 'PODS_ALLOW_FULL_META' ) || ! PODS_ALLOW_FULL_META ) {
3006
				return $_null; // don't cover get_post_meta( $id )
3007
			}
3008
3009
			$single = false;
3010
		}
3011
3012
		if ( 'user' === $object_type && 'locale' === $meta_key ) {
3013
			return $_null; // don't interfere with locale
3014
		}
3015
3016
		$object = $this->get_object( $object_type, $object_id );
3017
3018
		if ( empty( $object_id ) || empty( $object ) ) {
3019
			return $_null;
3020
		}
3021
3022
		$no_conflict = pods_no_conflict_check( $meta_type );
3023
3024
		if ( ! $no_conflict ) {
3025
			pods_no_conflict_on( $meta_type );
3026
		}
3027
3028
		$meta_cache = array();
3029
3030
		if ( ! $single && isset( $GLOBALS['wp_object_cache'] ) && is_object( $GLOBALS['wp_object_cache'] ) ) {
3031
			$meta_cache = wp_cache_get( $object_id, 'pods_' . $meta_type . '_meta' );
3032
3033
			if ( empty( $meta_cache ) ) {
3034
				$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
3035
3036
				if ( empty( $meta_cache ) ) {
3037
					$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
3038
					$meta_cache = $meta_cache[ $object_id ];
3039
				}
3040
			}
3041
		}
3042
3043
		if ( empty( $meta_cache ) || ! is_array( $meta_cache ) ) {
3044
			$meta_cache = array();
3045
		}
3046
3047
		if ( ! is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object['name'] ) {
3048
			self::$current_field_pod = pods( $object['name'], $object_id );
3049
		} elseif ( self::$current_field_pod->id() != $object_id ) {
3050
			self::$current_field_pod->fetch( $object_id );
3051
		}
3052
3053
		$pod = self::$current_field_pod;
3054
3055
		$meta_keys = array( $meta_key );
3056
3057
		if ( empty( $meta_key ) ) {
3058
			$meta_keys = array_keys( $meta_cache );
3059
		}
3060
3061
		$key_found = false;
3062
3063
		foreach ( $meta_keys as $meta_k ) {
3064
			if ( ! empty( $pod ) ) {
3065
				if ( isset( $pod->fields[ $meta_k ] ) ) {
3066
					$key_found = true;
3067
3068
					$meta_cache[ $meta_k ] = $pod->field( array(
3069
						'name'     => $meta_k,
3070
						'single'   => $single,
3071
						'get_meta' => true
3072
					) );
3073
3074
					if ( ( ! is_array( $meta_cache[ $meta_k ] ) || ! isset( $meta_cache[ $meta_k ][0] ) ) ) {
3075
						if ( empty( $meta_cache[ $meta_k ] ) && ! is_array( $meta_cache[ $meta_k ] ) && $single ) {
3076
							$meta_cache[ $meta_k ] = array();
3077
						} else {
3078
							$meta_cache[ $meta_k ] = array( $meta_cache[ $meta_k ] );
3079
						}
3080
					}
3081
3082
					if ( in_array( $pod->fields[ $meta_k ]['type'], PodsForm::tableless_field_types() ) && isset( $meta_cache[ '_pods_' . $meta_k ] ) ) {
3083
						unset( $meta_cache[ '_pods_' . $meta_k ] );
3084
					}
3085
				} elseif ( false !== strpos( $meta_k, '.' ) ) {
3086
					$key_found = true;
3087
3088
					$first = current( explode( '.', $meta_k ) );
3089
3090
					if ( isset( $pod->fields[ $first ] ) ) {
3091
						$meta_cache[ $meta_k ] = $pod->field( array(
3092
							'name'     => $meta_k,
3093
							'single'   => $single,
3094
							'get_meta' => true
3095
						) );
3096
3097
						if ( ( ! is_array( $meta_cache[ $meta_k ] ) || ! isset( $meta_cache[ $meta_k ][0] ) ) && $single ) {
3098
							if ( empty( $meta_cache[ $meta_k ] ) && ! is_array( $meta_cache[ $meta_k ] ) && $single ) {
3099
								$meta_cache[ $meta_k ] = array();
3100
							} else {
3101
								$meta_cache[ $meta_k ] = array( $meta_cache[ $meta_k ] );
3102
							}
3103
						}
3104
3105
						if ( in_array( $pod->fields[ $first ]['type'], PodsForm::tableless_field_types() ) && isset( $meta_cache[ '_pods_' . $first ] ) ) {
3106
							unset( $meta_cache[ '_pods_' . $first ] );
3107
						}
3108
					}
3109
				}
3110
			}
3111
		}
3112
3113
		if ( ! $no_conflict ) {
3114
			pods_no_conflict_off( $meta_type );
3115
		}
3116
3117
		unset( $pod ); // memory clear
3118
3119
		if ( ! $key_found ) {
3120
			return $_null;
3121
		}
3122
3123
		if ( ! $single && isset( $GLOBALS['wp_object_cache'] ) && is_object( $GLOBALS['wp_object_cache'] ) ) {
3124
			wp_cache_set( $object_id, $meta_cache, 'pods_' . $meta_type . '_meta' );
3125
		}
3126
3127
		if ( empty( $meta_key ) ) {
3128
			return $meta_cache;
3129
		} elseif ( isset( $meta_cache[ $meta_key ] ) ) {
3130
			$value = $meta_cache[ $meta_key ];
3131
		} else {
3132
			$value = '';
3133
		}
3134
3135
		if ( ! is_numeric( $value ) && empty( $value ) ) {
3136
			if ( $single ) {
3137
				$value = '';
3138
			} else {
3139
				$value = array();
3140
			}
3141
		} // get_metadata requires $meta[ 0 ] to be set for first value to be retrieved
3142
		elseif ( ! is_array( $value ) ) {
3143
			$value = array( $value );
3144
		}
3145
3146
		return $value;
3147
	}
3148
3149
	/**
3150
	 * @param        $object_type
3151
	 * @param null   $_null
3152
	 * @param int    $object_id
3153
	 * @param string $meta_key
3154
	 * @param string $meta_value
3155
	 * @param bool   $unique
3156
	 *
3157
	 * @return bool|int|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use null|integer.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
3158
	 */
3159
	public function add_meta( $object_type, $_null = null, $object_id = 0, $meta_key = '', $meta_value = '', $unique = false ) {
3160
3161
		if ( pods_tableless() ) {
3162
			return $_null;
3163
		}
3164
3165
		$object = $this->get_object( $object_type, $object_id );
3166
3167
		if ( empty( $object_id ) || empty( $object ) || ! isset( $object['fields'][ $meta_key ] ) ) {
3168
			return $_null;
3169
		}
3170
3171
		if ( in_array( $object['fields'][ $meta_key ]['type'], PodsForm::tableless_field_types() ) ) {
3172
			if ( ! is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object['name'] ) {
3173
				self::$current_field_pod = pods( $object['name'], $object_id );
3174
			} elseif ( self::$current_field_pod->id() != $object_id ) {
3175
				self::$current_field_pod->fetch( $object_id );
3176
			}
3177
3178
			$pod = self::$current_field_pod;
3179
3180
			$pod->add_to( $meta_key, $meta_value );
3181
		} else {
3182
			if ( ! is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object['name'] ) {
3183
				self::$current_field_pod = pods( $object['name'] );
3184
			}
3185
3186
			$pod = self::$current_field_pod;
3187
3188
			$pod->save( $meta_key, $meta_value, $object_id, array(
3189
				'podsmeta_direct' => true,
3190
				'error_mode'      => 'false'
3191
			) );
3192
		}
3193
3194
		return $object_id;
3195
	}
3196
3197
	/**
3198
	 * @param        $object_type
3199
	 * @param null   $_null
3200
	 * @param int    $object_id
3201
	 * @param string $meta_key
3202
	 * @param string $meta_value
3203
	 * @param string $prev_value
3204
	 *
3205
	 * @return bool|int|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use null|integer.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
3206
	 */
3207
	public function update_meta( $object_type, $_null = null, $object_id = 0, $meta_key = '', $meta_value = '', $prev_value = '' ) {
3208
3209
		if ( pods_tableless() ) {
3210
			return $_null;
3211
		}
3212
3213
		$object = $this->get_object( $object_type, $object_id );
3214
3215
		if ( empty( $object_id ) || empty( $object ) || ! isset( $object['fields'][ $meta_key ] ) ) {
3216
			return $_null;
3217
		}
3218
3219
		if ( ! is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object['name'] ) {
3220
			self::$current_field_pod = pods( $object['name'] );
3221
		}
3222
3223
		$pod = self::$current_field_pod;
3224
3225
		if ( ( isset( $pod->fields[ $meta_key ] ) || false !== strpos( $meta_key, '.' ) ) && $pod->row !== null ) {
3226
3227
			$key = $meta_key;
3228
			if ( false !== strpos( $meta_key, '.' ) ) {
3229
				$key = current( explode( '.', $meta_key ) );
3230
			}
3231
3232
			$pod->row[ $meta_key ] = $meta_value;
3233
3234
			if ( isset( $pod->fields[ $key ] ) ) {
3235
				if ( in_array( $pod->fields[ $key ]['type'], PodsForm::tableless_field_types() ) && isset( $meta_cache[ '_pods_' . $key ] ) ) {
0 ignored issues
show
Bug introduced by
The variable $meta_cache seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
3236
					unset( $meta_cache[ '_pods_' . $key ] );
3237
				}
3238
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
3239
3240
		}
3241
3242
		$pod->save( $meta_key, $meta_value, $object_id, array( 'podsmeta_direct' => true, 'error_mode' => 'false' ) );
3243
3244
		return $object_id;
3245
	}
3246
3247
	/**
3248
	 * @param        $object_type
3249
	 * @param null   $_null
3250
	 * @param int    $object_id
3251
	 * @param string $meta_key
3252
	 * @param string $meta_value
3253
	 * @param bool   $delete_all
3254
	 *
3255
	 * @return null
3256
	 */
3257
	public function delete_meta( $object_type, $_null = null, $object_id = 0, $meta_key = '', $meta_value = '', $delete_all = false ) {
3258
3259
		if ( pods_tableless() ) {
3260
			return $_null;
3261
		}
3262
3263
		$object = $this->get_object( $object_type, $object_id );
3264
3265
		if ( empty( $object_id ) || empty( $object ) || ! isset( $object['fields'][ $meta_key ] ) ) {
3266
			return $_null;
3267
		}
3268
3269
		// @todo handle $delete_all (delete the field values from all pod items)
3270
		if ( ! empty( $meta_value ) && in_array( $object['fields'][ $meta_key ]['type'], PodsForm::tableless_field_types() ) ) {
3271
			if ( ! is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object['name'] ) {
3272
				self::$current_field_pod = pods( $object['name'], $object_id );
3273
			} elseif ( self::$current_field_pod->id() != $object_id ) {
3274
				self::$current_field_pod->fetch( $object_id );
3275
			}
3276
3277
			$pod = self::$current_field_pod;
3278
3279
			$pod->remove_from( $meta_key, $meta_value );
3280
		} else {
3281
			if ( ! is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object['name'] ) {
3282
				self::$current_field_pod = pods( $object['name'] );
3283
			}
3284
3285
			$pod = self::$current_field_pod;
3286
3287
			$pod->save( array( $meta_key => null ), null, $object_id, array(
3288
				'podsmeta_direct' => true,
3289
				'error_mode'      => 'false'
3290
			) );
3291
		}
3292
3293
		return $_null;
3294
	}
3295
3296
	/**
3297
	 * @param $id
3298
	 *
3299
	 * @return bool|void
3300
	 */
3301
	public function delete_post( $id ) {
3302
3303
		$post = get_post( $id );
3304
3305
		if ( empty( $post ) ) {
3306
			return;
3307
		}
3308
3309
		$id        = $post->ID;
3310
		$post_type = $post->post_type;
3311
3312
		return $this->delete_object( 'post_type', $id, $post_type );
3313
	}
3314
3315
	/**
3316
	 * @param $id
3317
	 */
3318
	public function delete_taxonomy( $id ) {
3319
3320
		/**
3321
		 * @var $wpdb WPDB
3322
		 */
3323
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
3324
3325
		$terms = $wpdb->get_results( "SELECT `term_id`, `taxonomy` FROM `{$wpdb->term_taxonomy}` WHERE `term_taxonomy_id` = {$id}" );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
3326
3327
		if ( empty( $terms ) ) {
3328
			return;
3329
		}
3330
3331
		foreach ( $terms as $term ) {
3332
			$id       = $term->term_id;
3333
			$taxonomy = $term->taxonomy;
3334
3335
			$this->delete_object( 'taxonomy', $id, $taxonomy );
3336
		}
3337
	}
3338
3339
	/**
3340
	 * Hook the split_shared_term action and point it to this method
3341
	 *
3342
	 * Fires after a previously shared taxonomy term is split into two separate terms.
3343
	 *
3344
	 * @param int    $term_id          ID of the formerly shared term.
3345
	 * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
3346
	 * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
3347
	 * @param string $taxonomy         Taxonomy for the split term.
3348
	 */
3349
	public static function split_shared_term( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
3350
3351
		require_once( PODS_DIR . 'classes/PodsTermSplitting.php' );
3352
3353
		$term_splitting = new Pods_Term_Splitting( $term_id, $new_term_id, $taxonomy );
3354
		$term_splitting->split_shared_term();
3355
3356
	}
3357
3358
	/**
3359
	 * @param $id
3360
	 *
3361
	 * @return bool
3362
	 */
3363
	public function delete_user( $id ) {
3364
3365
		return $this->delete_object( 'user', $id );
3366
	}
3367
3368
	/**
3369
	 * @param $id
3370
	 *
3371
	 * @return bool
3372
	 */
3373
	public function delete_comment( $id ) {
3374
3375
		return $this->delete_object( 'comment', $id );
3376
	}
3377
3378
	/**
3379
	 * @param $id
3380
	 *
3381
	 * @return bool
3382
	 */
3383
	public function delete_media( $id ) {
3384
3385
		return $this->delete_object( 'media', $id );
3386
	}
3387
3388
	/**
3389
	 * @param      $type
3390
	 * @param      $id
3391
	 * @param null $name
3392
	 *
3393
	 * @return bool
3394
	 */
3395
	public function delete_object( $type, $id, $name = null ) {
3396
3397
		if ( empty( $name ) ) {
3398
			$name = $type;
3399
		}
3400
3401
		$object = $this->object_get( $type, $name );
3402
3403
		if ( ! empty( $object ) ) {
3404
			$params = array(
3405
				'pod'    => pods_var( 'name', $object ),
3406
				'pod_id' => pods_var( 'id', $object ),
3407
				'id'     => $id
3408
			);
3409
3410
			return pods_api()->delete_pod_item( $params, false );
3411
		} else {
3412
			return pods_api()->delete_object_from_relationships( $id, $type, $name );
3413
		}
3414
	}
3415
}
3416