Completed
Push — 2.x ( 9aea5f...6c9ff5 )
by Scott Kingsley
09:30
created

PodsAPI   D

Complexity

Total Complexity 1736

Size/Duplication

Total Lines 8171
Duplicated Lines 16.44 %

Coupling/Cohesion

Components 3
Dependencies 9
Metric Value
wmc 1736
lcom 3
cbo 9
dl 1343
loc 8171
rs 4.4103

79 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 14 5
B __construct() 0 20 5
C save_wp_object() 0 24 7
B delete_wp_object() 0 18 6
D save_post() 11 41 13
F save_post_meta() 69 69 20
D save_user() 13 48 13
D save_user_meta() 62 62 15
C save_comment() 13 41 12
D save_comment_meta() 62 62 15
D save_term() 0 56 13
F save_term_meta() 73 73 21
C save_setting() 2 24 8
B rename_wp_object_type() 0 36 6
F get_wp_object_fields() 2 507 16
F add_pod() 22 160 41
F save_pod() 79 650 167
F save_field() 101 519 156
A save_slug_fix() 0 6 3
F save_object() 20 85 15
A save_template() 0 7 1
A save_page() 0 18 3
A save_helper() 0 17 3
F save_pod_item() 126 840 242
A save_pod_items() 0 18 4
B get_changed_fields() 0 20 5
F save_relationships() 29 125 22
C duplicate_pod() 16 58 13
C duplicate_field() 6 49 8
C duplicate_pod_item() 0 37 7
B export_pod_item() 0 29 6
F export_pod_item_level() 0 101 29
C reorder_pod_item() 0 40 11
C reset_pod() 14 102 12
D delete_pod() 24 88 13
F delete_field() 0 73 17
A delete_object() 4 16 3
A delete_template() 0 5 1
A delete_page() 0 11 3
A delete_helper() 0 5 1
F delete_pod_item() 43 130 36
D delete_object_from_relationships() 0 93 15
D delete_relationships() 27 102 19
C pod_exists() 9 23 8
F load_pod() 58 248 71
F load_pods() 37 263 99
B field_exists() 10 21 7
F load_field() 46 192 50
F load_fields() 36 151 52
C load_object() 4 50 15
F load_objects() 20 91 32
A load_template() 0 8 2
A load_templates() 0 8 2
A load_page() 4 12 4
A load_pages() 0 8 2
A load_helper() 8 8 2
A load_helpers() 8 8 2
C load_pod_item() 4 23 8
C load_sister_fields() 14 45 15
B detect_pod_field_from_sql_data_type() 0 25 2
A get_field_types() 0 3 1
A get_field_definition() 0 5 1
F handle_field_validation() 0 75 27
F lookup_related_items() 71 165 55
F lookup_related_items_from() 74 131 42
F get_table_info_load() 14 105 28
F get_table_info() 85 452 89
A export_package() 0 6 2
A replace_package() 0 3 1
A import_package() 0 6 2
A validate_package() 0 3 1
F import() 8 159 50
B export() 0 33 6
A csv_to_php() 0 7 1
B cache_flush_pods() 0 30 5
F process_form() 0 66 13
A do_hook() 7 7 2
A __get() 4 20 3
A __call() 4 13 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PodsAPI often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PodsAPI, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @package Pods
4
 */
5
class PodsAPI {
6
7
    /**
8
     * @var PodsAPI
9
     */
10
    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...
11
12
	/**
13
	 * @var array PodsAPI
14
	 */
15
	static $instances = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $instances.

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...
16
17
18
    /**
19
     * @var bool
20
     */
21
    public $display_errors = false;
22
23
    /**
24
     * @var array|bool|mixed|null|void
25
     */
26
    public $pod_data;
27
28
    /**
29
     * @var
30
     */
31
    public $pod;
32
33
    /**
34
     * @var
35
     */
36
    public $pod_id;
37
38
    /**
39
     * @var
40
     */
41
    public $fields;
42
43
    /**
44
     * @var
45
     * @deprecated 2.0
46
     */
47
    public $format = null;
48
49
    /**
50
     * @var
51
     */
52
    private $deprecated;
53
54
	/**
55
	 * @var array
56
	 * @since 2.5
57
	 */
58
	private $fields_cache = array();
59
60
	/**
61
	 * @var array
62
	 * @since 2.5
63
	 *
64
	 */
65
	private static $table_info_cache = array();
66
67
	/**
68
	 * @var array
69
	 * @since 2.5
70
	 *
71
	 */
72
	private static $related_item_cache = array();
73
74
	/**
75
     * Singleton-ish handling for a basic pods_api() request
76
     *
77
     * @param string $pod (optional) The pod name
78
     * @param string $format (deprecated) Format for import/export, "php" or "csv"
79
     *
80
     * @return \PodsAPI
81
     *
82
     * @since 2.3.5
83
     */
84
	public static function init ( $pod = null, $format = null ) {
85
		if ( null !== $pod || null !== $format ) {
86
			if ( ! isset( self::$instances[ $pod ] ) ) {
87
				// Cache API singleton per Pod
88
				self::$instances[ $pod ] = new PodsAPI( $pod, $format );
89
			}
90
			return self::$instances[ $pod ];
91
		}
92
		elseif ( !is_object( self::$instance ) ) {
93
			self::$instance = new PodsAPI();
94
		}
95
96
		return self::$instance;
97
	}
98
99
100
    /**
101
     * Store and retrieve data programatically
102
     *
103
     * @param string $pod (optional) The pod name
104
     * @param string $format (deprecated) Format for import/export, "php" or "csv"
105
     *
106
     * @return \PodsAPI
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...
107
     *
108
     * @license http://www.gnu.org/licenses/gpl-2.0.html
109
     * @since 1.7.1
110
     */
111
    public function __construct ( $pod = null, $format = null ) {
112
        if ( null !== $pod && 0 < strlen( (string) $pod ) ) {
113
            if ( null !== $format ) {
114
                $this->format = $format;
0 ignored issues
show
Deprecated Code introduced by
The property PodsAPI::$format has been deprecated with message: 2.0

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

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

Loading history...
115
116
                pods_deprecated( 'pods_api( $pod, $format )', '2.0', 'pods_api( $pod )' );
117
            }
118
119
            $pod = pods_clean_name( $pod );
120
121
            $pod = $this->load_pod( array( 'name' => $pod, 'table_info' => true ), false );
122
123
            if ( !empty( $pod ) ) {
124
                $this->pod_data = $pod;
125
                $this->pod = $pod[ 'name' ];
126
                $this->pod_id = $pod[ 'id' ];
127
                $this->fields = $pod[ 'fields' ];
128
            }
129
        }
130
    }
131
132
    /**
133
     * Save a WP object and its meta
134
     *
135
     * @param string $object_type Object type: post|taxonomy|user|comment|setting
136
     * @param array $data All post data to be saved
137
     * @param array $meta (optional) Associative array of meta keys and values
138
     * @param bool $strict (optional) Decides whether the previous saved meta should be deleted or not
139
     * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
140
     * @param array $fields (optional) The array of fields and their options, for further processing with
141
     *
142
     * @return bool|mixed
143
     *
144
     * @since 2.0
145
     */
146
    public function save_wp_object ( $object_type, $data, $meta = array(), $strict = false, $sanitized = false, $fields = array() ) {
147
        if ( in_array( $object_type, array( 'post_type', 'media' ) ) )
148
            $object_type = 'post';
149
150
        if ( 'taxonomy' == $object_type )
151
            $object_type = 'term';
152
153
        if ( $sanitized ) {
154
            $data = pods_unsanitize( $data );
155
            $meta = pods_unsanitize( $meta );
156
        }
157
158
        if ( in_array( $object_type, array( 'post', 'term', 'user', 'comment' ) ) )
159
            return call_user_func( array( $this, 'save_' . $object_type ), $data, $meta, $strict, false, $fields );
160
        elseif ( 'settings' == $object_type ) {
161
            // Nothing to save
162
            if ( empty( $meta ) )
163
                return true;
164
165
            return $this->save_setting( pods_var( 'option_id', $data ), $meta, false );
166
        }
167
168
        return false;
169
    }
170
171
    /**
172
     * Delete a WP object
173
     *
174
     * @param string $object_type Object type: post|user|comment
175
     * @param int $id Object ID
176
     * @param bool $force_delete (optional) Force deletion instead of trashing (post types only)
177
     *
178
     * @return bool|mixed
179
     *
180
     * @since 2.0
181
     */
182
    public function delete_wp_object ( $object_type, $id, $force_delete = true ) {
183
        if ( in_array( $object_type, array( 'post_type', 'media' ) ) )
184
            $object_type = 'post';
185
186
        if ( 'taxonomy' == $object_type )
187
            $object_type = 'term';
188
189
        if ( empty( $id ) )
190
            return false;
191
192
        if ( in_array( $object_type, array( 'post' ) ) )
193
            return wp_delete_post( $id, $force_delete );
194
195
        if ( function_exists( 'wp_delete_' . $object_type ) )
196
            return call_user_func( 'wp_delete_' . $object_type, $id );
197
198
        return false;
199
    }
200
201
    /**
202
     * Save a post and it's meta
203
     *
204
     * @param array $post_data All post data to be saved (using wp_insert_post / wp_update_post)
205
     * @param array $post_meta (optional) All meta to be saved (set value to null to delete)
206
     * @param bool $strict (optional) Whether to delete previously saved meta not in $post_meta
207
     * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
208
     * @param array $fields (optional) The array of fields and their options, for further processing with
209
     *
210
     * @return mixed|void
211
     *
212
     * @since 2.0
213
     */
214
    public function save_post ( $post_data, $post_meta = null, $strict = false, $sanitized = false, $fields = array() ) {
215
        $conflicted = pods_no_conflict_check( 'post' );
216
217
        if ( !$conflicted )
218
            pods_no_conflict_on( 'post' );
219
220
        if ( !is_array( $post_data ) || empty( $post_data ) )
221
            $post_data = array( 'post_title' => '' );
222
223
        if ( !is_array( $post_meta ) )
224
            $post_meta = array();
225
226
        if ( $sanitized ) {
227
            $post_data = pods_unsanitize( $post_data );
228
            $post_meta = pods_unsanitize( $post_meta );
229
        }
230
231
        if ( !isset( $post_data[ 'ID' ] ) || empty( $post_data[ 'ID' ] ) )
232
            $post_data[ 'ID' ] = wp_insert_post( $post_data, true );
233
        elseif ( 2 < count( $post_data ) || !isset( $post_data[ 'post_type' ] ) )
234
            $post_data[ 'ID' ] = wp_update_post( $post_data, true );
235
236 View Code Duplication
        if ( is_wp_error( $post_data[ 'ID' ] ) ) {
237
            if ( !$conflicted )
238
                pods_no_conflict_off( 'post' );
239
240
            /**
241
             * @var $post_error WP_Error
242
             */
243
            $post_error = $post_data[ 'ID' ];
244
245
            return pods_error( $post_error->get_error_message(), $this );
246
        }
247
248
        $this->save_post_meta( $post_data[ 'ID' ], $post_meta, $strict, $fields );
249
250
        if ( !$conflicted )
251
            pods_no_conflict_off( 'post' );
252
253
        return $post_data[ 'ID' ];
254
    }
255
256
    /**
257
     * Save a post's meta
258
     *
259
     * @param int $id Post ID
260
     * @param array $post_meta All meta to be saved (set value to null to delete)
261
     * @param bool $strict Whether to delete previously saved meta not in $post_meta
262
     * @param array $fields (optional) The array of fields and their options, for further processing with
263
     *
264
     * @return int Id of the post with the meta
265
     *
266
     * @since 2.0
267
     */
268 View Code Duplication
    public function save_post_meta ( $id, $post_meta = null, $strict = false, $fields = array() ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
269
        $simple_tableless_objects = PodsForm::simple_tableless_objects();
270
271
        $conflicted = pods_no_conflict_check( 'post' );
272
273
        if ( !$conflicted )
274
            pods_no_conflict_on( 'post' );
275
276
        if ( !is_array( $post_meta ) )
277
            $post_meta = array();
278
279
        $id = (int) $id;
280
281
        $meta = get_post_meta( $id );
282
283
        foreach ( $meta as $k => $value ) {
284
            if ( is_array( $value ) && 1 == count( $value ) )
285
                $meta[ $k ] = current( $value );
286
        }
287
288
        foreach ( $post_meta as $meta_key => $meta_value ) {
289
            if ( null === $meta_value || ( $strict && '' === $post_meta[ $meta_key ] ) ) {
290
                $old_meta_value = '';
291
292
                if ( isset( $meta[ $meta_key ] ) )
293
                    $old_meta_value = $meta[ $meta_key ];
294
295
                delete_post_meta( $id, $meta_key, $old_meta_value );
296
            }
297
            else {
298
				$simple = false;
299
300
				if ( isset( $fields[ $meta_key ] ) ) {
301
					$field_data = $fields[ $meta_key ];
302
303
					$simple = ( 'pick' == $field_data[ 'type' ] && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
304
				}
305
306
				if ( $simple ) {
307
					delete_post_meta( $id, $meta_key );
308
309
					update_post_meta( $id, '_pods_' . $meta_key, $meta_value );
310
311
					if ( ! is_array( $meta_value ) ) {
312
						$meta_value = array( $meta_value );
313
					}
314
315
					foreach ( $meta_value as $value ) {
316
						add_post_meta( $id, $meta_key, $value );
317
					}
318
				}
319
				else {
320
                	update_post_meta( $id, $meta_key, $meta_value );
321
				}
322
			}
323
        }
324
325
        if ( $strict ) {
326
            foreach ( $meta as $meta_key => $meta_value ) {
327
                if ( !isset( $post_meta[ $meta_key ] ) )
328
                    delete_post_meta( $id, $meta_key, $meta_value );
329
            }
330
        }
331
332
        if ( !$conflicted )
333
            pods_no_conflict_off( 'post' );
334
335
        return $id;
336
    }
337
338
    /**
339
     * Save a user and it's meta
340
     *
341
     * @param array $user_data All user data to be saved (using wp_insert_user / wp_update_user)
342
     * @param array $user_meta (optional) All meta to be saved (set value to null to delete)
343
     * @param bool $strict (optional) Whether to delete previously saved meta not in $user_meta
344
     * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
345
     * @param array $fields (optional) The array of fields and their options, for further processing with
346
     *
347
     * @return int Returns user id on success
348
     *
349
     * @since 2.0
350
     */
351
    public function save_user ( $user_data, $user_meta = null, $strict = false, $sanitized = false, $fields = array() ) {
352 View Code Duplication
        if ( !is_array( $user_data ) || empty( $user_data ) )
353
            return pods_error( __( 'User data is required but is either invalid or empty', 'pods' ), $this );
354
355
        $conflicted = pods_no_conflict_check( 'user' );
356
357
        if ( !$conflicted )
358
            pods_no_conflict_on( 'user' );
359
360
        if ( !is_array( $user_meta ) )
361
            $user_meta = array();
362
363
        if ( $sanitized ) {
364
            $user_data = pods_unsanitize( $user_data );
365
            $user_meta = pods_unsanitize( $user_meta );
366
        }
367
368
		// Set role
369
		if ( isset( $user_meta[ 'role' ] ) ) {
370
			$user_data[ 'role' ] = $user_meta[ 'role' ];
371
372
			unset( $user_meta[ 'role' ] );
373
		}
374
375
        if ( !isset( $user_data[ 'ID' ] ) || empty( $user_data[ 'ID' ] ) )
376
            $user_data[ 'ID' ] = wp_insert_user( $user_data );
377
        elseif ( 1 < count( $user_data ) )
378
            wp_update_user( $user_data );
379
380 View Code Duplication
        if ( is_wp_error( $user_data[ 'ID' ] ) ) {
381
            if ( !$conflicted )
382
                pods_no_conflict_off( 'user' );
383
384
            /**
385
             * @var $user_error WP_Error
386
             */
387
            $user_error = $user_data[ 'ID' ];
388
389
            return pods_error( $user_error->get_error_message(), $this );
390
        }
391
392
        $this->save_user_meta( $user_data[ 'ID' ], $user_meta, $strict, $fields );
393
394
        if ( !$conflicted )
395
            pods_no_conflict_off( 'user' );
396
397
        return $user_data[ 'ID' ];
398
    }
399
400
    /**
401
     * Save a user meta
402
     *
403
     * @param int $id User ID
404
     * @param array $user_meta (optional) All meta to be saved (set value to null to delete)
405
     * @param bool $strict (optional) Whether to delete previously saved meta not in $user_meta
406
     * @param array $fields (optional) The array of fields and their options, for further processing with
407
     *
408
     * @return int User ID
409
     *
410
     * @since 2.0
411
     *
412
     */
413 View Code Duplication
    public function save_user_meta ( $id, $user_meta = null, $strict = false, $fields = array() ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
414
        $simple_tableless_objects = PodsForm::simple_tableless_objects();
415
416
        $conflicted = pods_no_conflict_check( 'user' );
417
418
        if ( !$conflicted )
419
            pods_no_conflict_on( 'user' );
420
421
        if ( !is_array( $user_meta ) )
422
            $user_meta = array();
423
424
        $id = (int) $id;
425
426
        $meta = get_user_meta( $id );
427
428
        foreach ( $user_meta as $meta_key => $meta_value ) {
429
            if ( null === $meta_value ) {
430
                $old_meta_value = '';
431
432
                if ( isset( $meta[ $meta_key ] ) )
433
                    $old_meta_value = $meta[ $meta_key ];
434
435
                delete_user_meta( $id, $meta_key, $old_meta_value );
436
            }
437
            else {
438
				$simple = false;
439
440
				if ( isset( $fields[ $meta_key ] ) ) {
441
					$field_data = $fields[ $meta_key ];
442
443
					$simple = ( 'pick' == $field_data[ 'type' ] && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
444
				}
445
446
				if ( $simple ) {
447
					delete_user_meta( $id, $meta_key );
448
449
					if ( ! is_array( $meta_value ) ) {
450
						$meta_value = array( $meta_value );
451
					}
452
453
					foreach ( $meta_value as $value ) {
454
						add_user_meta( $id, $meta_key, $value );
455
					}
456
				}
457
				else {
458
                	update_user_meta( $id, $meta_key, $meta_value );
459
				}
460
			}
461
        }
462
463
        if ( $strict ) {
464
            foreach ( $meta as $meta_key => $meta_value ) {
465
                if ( !isset( $user_meta[ $meta_key ] ) )
466
                    delete_user_meta( $id, $meta_key, $user_meta[ $meta_key ] );
467
            }
468
        }
469
470
        if ( !$conflicted )
471
            pods_no_conflict_off( 'user' );
472
473
        return $id;
474
    }
475
476
    /**
477
     * Save a comment and it's meta
478
     *
479
     * @param array $comment_data All comment data to be saved (using wp_insert_comment / wp_update_comment)
480
     * @param array $comment_meta (optional) All meta to be saved (set value to null to delete)
481
     * @param bool $strict (optional) Whether to delete previously saved meta not in $comment_meta
482
     * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
483
     * @param array $fields (optional) The array of fields and their options, for further processing with
484
     *
485
     * @return int Comment ID
486
     *
487
     * @since 2.0
488
     */
489
    public function save_comment ( $comment_data, $comment_meta = null, $strict = false, $sanitized = false, $fields = array() ) {
490 View Code Duplication
        if ( !is_array( $comment_data ) || empty( $comment_data ) )
491
            return pods_error( __( 'Comment data is required but is either invalid or empty', 'pods' ), $this );
492
493
        $conflicted = pods_no_conflict_check( 'comment' );
494
495
        if ( !$conflicted )
496
            pods_no_conflict_on( 'comment' );
497
498
        if ( !is_array( $comment_meta ) )
499
            $comment_meta = array();
500
501
        if ( $sanitized ) {
502
            $comment_data = pods_unsanitize( $comment_data );
503
            $comment_meta = pods_unsanitize( $comment_meta );
504
        }
505
506
        if ( !isset( $comment_data[ 'comment_ID' ] ) || empty( $comment_data[ 'comment_ID' ] ) )
507
	        $comment_data[ 'comment_ID' ] = wp_insert_comment( pods_slash( $comment_data ) ); // Expects slashed
508
        elseif ( 1 < count( $comment_data ) )
509
            wp_update_comment( $comment_data );
510
511 View Code Duplication
        if ( is_wp_error( $comment_data[ 'comment_ID' ] ) ) {
512
            if ( !$conflicted )
513
                pods_no_conflict_off( 'comment' );
514
515
            /**
516
             * @var $comment_error WP_Error
517
             */
518
            $comment_error = $comment_data[ 'comment_ID' ];
519
520
            return pods_error( $comment_error->get_error_message(), $this );
521
        }
522
523
        $this->save_comment_meta( $comment_data[ 'comment_ID' ], $comment_meta, $strict, $fields );
524
525
        if ( !$conflicted )
526
            pods_no_conflict_off( 'comment' );
527
528
        return $comment_data[ 'comment_ID' ];
529
    }
530
531
    /**
532
     * Save a comment meta
533
     *
534
     * @param int $id Comment ID
535
     * @param array $comment_meta (optional) All meta to be saved (set value to null to delete)
536
     * @param bool $strict (optional) Whether to delete previously saved meta not in $comment_meta
537
     * @param array $fields (optional) The array of fields and their options, for further processing with
538
     *
539
     * @return int Comment ID
540
     *
541
     * @since 2.0
542
     */
543 View Code Duplication
    public function save_comment_meta ( $id, $comment_meta = null, $strict = false, $fields = array() ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
544
        $simple_tableless_objects = PodsForm::simple_tableless_objects();
545
546
        $conflicted = pods_no_conflict_check( 'comment' );
547
548
        if ( !$conflicted )
549
            pods_no_conflict_on( 'comment' );
550
551
        if ( !is_array( $comment_meta ) )
552
            $comment_meta = array();
553
554
        $id = (int) $id;
555
556
        $meta = get_comment_meta( $id );
557
558
        foreach ( $comment_meta as $meta_key => $meta_value ) {
559
            if ( null === $meta_value ) {
560
                $old_meta_value = '';
561
562
                if ( isset( $meta[ $meta_key ] ) )
563
                    $old_meta_value = $meta[ $meta_key ];
564
565
                delete_comment_meta( $id, $meta_key, $old_meta_value );
566
            }
567
            else {
568
				$simple = false;
569
570
				if ( isset( $fields[ $meta_key ] ) ) {
571
					$field_data = $fields[ $meta_key ];
572
573
					$simple = ( 'pick' == $field_data[ 'type' ] && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
574
				}
575
576
				if ( $simple ) {
577
					delete_comment_meta( $id, $meta_key );
578
579
					if ( ! is_array( $meta_value ) ) {
580
						$meta_value = array( $meta_value );
581
					}
582
583
					foreach ( $meta_value as $value ) {
584
						add_comment_meta( $id, $meta_key, $value );
585
					}
586
				}
587
				else {
588
                	update_comment_meta( $id, $meta_key, $meta_value );
589
				}
590
			}
591
        }
592
593
        if ( $strict ) {
594
            foreach ( $meta as $meta_key => $meta_value ) {
595
                if ( !isset( $comment_meta[ $meta_key ] ) )
596
                    delete_comment_meta( (int) $id, $meta_key, $comment_meta[ $meta_key ] );
597
            }
598
        }
599
600
        if ( !$conflicted )
601
            pods_no_conflict_off( 'comment' );
602
603
        return $id;
604
    }
605
606
    /**
607
     * Save a taxonomy's term
608
     *
609
     * @param array $term_data All term data to be saved (using wp_insert_term / wp_update_term)
610
     * @param array $term_meta All meta to be saved (set value to null to delete)
611
     * @param bool $strict (optional) Whether to delete previously saved meta not in $post_meta
612
     * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
613
     * @param array $fields (optional) The array of fields and their options, for further processing with
614
     *
615
     * @return int Term ID
616
     *
617
     * @since 2.0
618
     */
619
    public function save_term ( $term_data, $term_meta, $strict = false, $sanitized = false, $fields = array() ) {
620
		if ( empty( $term_data['taxonomy'] ) ) {
621
            return 0;
622
		}
623
624
        $conflicted = pods_no_conflict_check( 'taxonomy' );
625
626
        if ( !is_array( $term_data ) || empty( $term_data ) )
627
            $term_data = array( 'name' => '' );
628
629
        if ( !$conflicted )
630
            pods_no_conflict_on( 'taxonomy' );
631
632
        if ( !is_array( $term_meta ) )
633
            $term_meta = array();
634
635
        if ( $sanitized ) {
636
            $term_data = pods_unsanitize( $term_data );
637
            $term_meta = pods_unsanitize( $term_meta );
638
        }
639
640
        $taxonomy = $term_data['taxonomy'];
641
642
        unset( $term_data['taxonomy'] );
643
644
        if ( empty( $term_data['term_id'] ) ) {
645
        	$term_name = $term_data['name'];
646
647
        	unset( $term_data['name'] );
648
649
            $term_data['term_id'] = wp_insert_term( $term_name, $taxonomy, $term_data );
650
		} elseif ( 2 < count( $term_data ) ) {
651
            $term_data['term_id'] = wp_update_term( $term_data['term_id'], $taxonomy, $term_data );
652
        }
653
654
        if ( is_wp_error( $term_data['term_id'] ) ) {
655
            if ( !$conflicted )
656
                pods_no_conflict_off( 'taxonomy' );
657
658
            /**
659
             * @var $term_error WP_Error
660
             */
661
            $term_error = $term_data[ 'term_id' ];
662
663
            return pods_error( $term_error->get_error_message(), $this );
664
        }
665
        elseif ( is_array( $term_data['term_id'] ) )
666
            $term_data['term_id'] = $term_data['term_id'][ 'term_id' ];
667
668
        $this->save_term_meta( $term_data['term_id'], $term_meta, $strict, $fields );
669
670
        if ( !$conflicted )
671
            pods_no_conflict_off( 'taxonomy' );
672
673
        return $term_data['term_id'];
674
    }
675
676
    /**
677
     * Save a term's meta
678
     *
679
     * @param int $id Term ID
680
     * @param array $term_meta All meta to be saved (set value to null to delete)
681
     * @param bool $strict Whether to delete previously saved meta not in $term_meta
682
     * @param array $fields (optional) The array of fields and their options, for further processing with
683
     *
684
     * @return int Id of the term with the meta
685
     *
686
     * @since 2.0
687
     */
688 View Code Duplication
    public function save_term_meta ( $id, $term_meta = null, $strict = false, $fields = array() ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
689
        if ( ! function_exists( 'get_term_meta' ) ) {
690
        	return $id;
691
        }
692
693
        $simple_tableless_objects = PodsForm::simple_tableless_objects();
694
695
        $conflicted = pods_no_conflict_check( 'taxonomy' );
696
697
        if ( !$conflicted )
698
            pods_no_conflict_on( 'taxonomy' );
699
700
        if ( !is_array( $term_meta ) )
701
            $term_meta = array();
702
703
        $id = (int) $id;
704
705
        $meta = get_term_meta( $id );
706
707
        foreach ( $meta as $k => $value ) {
708
            if ( is_array( $value ) && 1 == count( $value ) )
709
                $meta[ $k ] = current( $value );
710
        }
711
712
        foreach ( $term_meta as $meta_key => $meta_value ) {
713
            if ( null === $meta_value || ( $strict && '' === $term_meta[ $meta_key ] ) ) {
714
                $old_meta_value = '';
715
716
                if ( isset( $meta[ $meta_key ] ) )
717
                    $old_meta_value = $meta[ $meta_key ];
718
719
                delete_term_meta( $id, $meta_key, $old_meta_value );
720
            }
721
            else {
722
				$simple = false;
723
724
				if ( isset( $fields[ $meta_key ] ) ) {
725
					$field_data = $fields[ $meta_key ];
726
727
					$simple = ( 'pick' == $field_data[ 'type' ] && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
728
				}
729
730
				if ( $simple ) {
731
					delete_term_meta( $id, $meta_key );
732
733
					update_term_meta( $id, '_pods_' . $meta_key, $meta_value );
734
735
					if ( ! is_array( $meta_value ) ) {
736
						$meta_value = array( $meta_value );
737
					}
738
739
					foreach ( $meta_value as $value ) {
740
						add_term_meta( $id, $meta_key, $value );
741
					}
742
				}
743
				else {
744
                	update_term_meta( $id, $meta_key, $meta_value );
745
				}
746
			}
747
        }
748
749
        if ( $strict ) {
750
            foreach ( $meta as $meta_key => $meta_value ) {
751
                if ( !isset( $term_meta[ $meta_key ] ) )
752
                    delete_term_meta( $id, $meta_key, $meta_value );
753
            }
754
        }
755
756
        if ( !$conflicted )
757
            pods_no_conflict_off( 'taxonomy' );
758
759
        return $id;
760
    }
761
762
    /**
763
     * Save a set of options
764
     *
765
     * @param string $setting Setting group name
766
     * @param array $option_data All option data to be saved
767
     * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
768
     *
769
     * @return bool
770
     *
771
     * @since 2.3
772
     */
773
    public function save_setting ( $setting, $option_data, $sanitized = false ) {
774 View Code Duplication
        if ( !is_array( $option_data ) || empty( $option_data ) )
775
            return pods_error( __( 'Setting data is required but is either invalid or empty', 'pods' ), $this );
776
777
        $conflicted = pods_no_conflict_check( 'settings' );
778
779
        if ( !$conflicted )
780
            pods_no_conflict_on( 'settings' );
781
782
        if ( $sanitized )
783
            $option_data = pods_unsanitize( $option_data );
784
785
        foreach ( $option_data as $option => $value ) {
786
            if ( !empty( $setting ) )
787
                $option = $setting . '_' . $option;
788
789
            update_option( $option, $value );
790
        }
791
792
        if ( !$conflicted )
793
            pods_no_conflict_off( 'settings' );
794
795
        return true;
796
    }
797
798
    /**
799
     * Rename a WP object's type
800
     *
801
     * @param string $object_type Object type: post|taxonomy|comment|setting
802
     * @param string $old_name The old name
803
     * @param string $new_name The new name
804
     *
805
     * @return bool
806
     *
807
     * @since 2.0
808
     */
809
    public function rename_wp_object_type ( $object_type, $old_name, $new_name ) {
810
        /**
811
         * @var $wpdb wpdb
812
         */
813
        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...
814
815
        if ( 'post_type' == $object_type )
816
            $object_type = 'post';
817
818
        if ( 'post' == $object_type ) {
819
            pods_query( "UPDATE `{$wpdb->posts}` SET `post_type` = %s WHERE `post_type` = %s", array(
820
                $new_name,
821
                $old_name
822
            ) );
823
        }
824
        elseif ( 'taxonomy' == $object_type ) {
825
            pods_query( "UPDATE `{$wpdb->term_taxonomy}` SET `taxonomy` = %s WHERE `taxonomy` = %s", array(
826
                $new_name,
827
                $old_name
828
            ) );
829
        }
830
        elseif ( 'comment' == $object_type ) {
831
            pods_query( "UPDATE `{$wpdb->comments}` SET `comment_type` = %s WHERE `comment_type` = %s", array(
832
                $new_name,
833
                $old_name
834
            ) );
835
        }
836
        elseif ( 'settings' == $object_type ) {
837
            pods_query( "UPDATE `{$wpdb->options}` SET `option_name` = REPLACE( `option_name`, %s, %s ) WHERE `option_name` LIKE '" . pods_sanitize_like( $old_name ) . "_%'", array(
838
                $new_name . '_',
839
                $old_name . '_'
840
            ) );
841
        }
842
843
        return true;
844
    }
845
846
    /**
847
     * Get a list of core WP object fields for a specific object
848
     *
849
     * @param string $object The pod type to look for, possible values: post_type, user, comment, taxonomy
850
     * @param array $pod Array of Pod data
851
     * @param boolean $refresh Whether to force refresh the information
852
     *
853
     * @return array Array of fields
854
     *
855
     * @since 2.0
856
     */
857
    public function get_wp_object_fields ( $object = 'post_type', $pod = null, $refresh = false ) {
858
        $pod_name = pods_var_raw( 'name', $pod, $object, null, true );
859
860
		if ( 'media' == $pod_name ) {
861
			$object = 'post_type';
862
			$pod_name = 'attachment';
863
		}
864
865
        $fields = false;
866
867
        if ( pods_api_cache() )
868
            $fields = pods_transient_get( trim( 'pods_api_object_fields_' . $object . $pod_name . '_', '_' ) );
869
870
        if ( false !== $fields && !$refresh )
871
            return $this->do_hook( 'get_wp_object_fields', $fields, $object, $pod );
872
873
        $fields = array();
874
875
        if ( 'post_type' == $object ) {
876
            $fields = array(
877
	            'ID' => array(
878
		            'name' => 'ID',
879
		            'label' => 'ID',
880
		            'type' => 'number',
881
		            'alias' => array( 'id' ),
882
	                'options' => array(
883
                        'number_format' => '9999.99'
884
	                )
885
	            ),
886
                'post_title' => array(
887
                    'name' => 'post_title',
888
                    'label' => 'Title',
889
                    'type' => 'text',
890
                    'alias' => array( 'title', 'name' ),
891
                    'options' => array(
892
                        'display_filter' => 'the_title',
893
                        'display_filter_args' => array( 'post_ID' )
894
                    )
895
                ),
896
                'post_content' => array(
897
                    'name' => 'post_content',
898
                    'label' => 'Content',
899
                    'type' => 'wysiwyg',
900
                    'alias' => array( 'content' ),
901
                    'options' => array(
902
                        'wysiwyg_allowed_html_tags' => '',
903
                        'display_filter' => 'the_content',
904
                        'pre_save' => 0
905
                    )
906
                ),
907
                'post_excerpt' => array(
908
                    'name' => 'post_excerpt',
909
                    'label' => 'Excerpt',
910
                    'type' => 'paragraph',
911
                    'alias' => array( 'excerpt' ),
912
                    'options' => array(
913
                        'paragraph_allow_html' => 1,
914
                        'paragraph_allowed_html_tags' => '',
915
                        'display_filter' => 'the_excerpt',
916
                        'pre_save' => 0
917
                    )
918
                ),
919
                'post_author' => array(
920
                    'name' => 'post_author',
921
                    'label' => 'Author',
922
                    'type' => 'pick',
923
                    'alias' => array( 'author' ),
924
                    'pick_object' => 'user',
925
                    'options' => array(
926
                        'pick_format_type' => 'single',
927
                        'pick_format_single' => 'autocomplete',
928
                        'default_value' => '{@user.ID}'
929
                    )
930
                ),
931
                'post_date' => array(
932
                    'name' => 'post_date',
933
                    'label' => 'Publish Date',
934
                    'type' => 'datetime',
935
                    'alias' => array( 'created', 'date' )
936
                ),
937
                'post_date_gmt' => array(
938
                    'name' => 'post_date_gmt',
939
                    'label' => 'Publish Date (GMT)',
940
                    'type' => 'datetime',
941
                    'alias' => array(),
942
                    'hidden' => true
943
                ),
944
                'post_status' => array(
945
                    'name' => 'post_status',
946
                    'label' => 'Status',
947
                    'type' => 'pick',
948
                    'pick_object' => 'post-status',
949
                    'default' => $this->do_hook( 'default_status_' . $pod_name, pods_var( 'default_status', pods_var_raw( 'options', $pod ), 'draft', null, true ), $pod ),
950
                    'alias' => array( 'status' )
951
                ),
952
                'comment_status' => array(
953
                    'name' => 'comment_status',
954
                    'label' => 'Comment Status',
955
                    'type' => 'text',
956
                    'default' => get_option( 'default_comment_status', 'open' ),
957
                    'alias' => array(),
958
                    'data' => array(
959
                        'open' => __( 'Open', 'pods' ),
960
                        'closed' => __( 'Closed', 'pods' )
961
                    )
962
                ),
963
                'ping_status' => array(
964
                    'name' => 'ping_status',
965
                    'label' => 'Ping Status',
966
                    'default' => get_option( 'default_ping_status', 'open' ),
967
                    'type' => 'text',
968
                    'alias' => array(),
969
                    'data' => array(
970
                        'open' => __( 'Open', 'pods' ),
971
                        'closed' => __( 'Closed', 'pods' )
972
                    )
973
                ),
974
                'post_password' => array(
975
                    'name' => 'post_password',
976
                    'label' => 'Password',
977
                    'type' => 'text',
978
                    'alias' => array()
979
                ),
980
                'post_name' => array(
981
                    'name' => 'post_name',
982
                    'label' => 'Permalink',
983
                    'type' => 'slug',
984
                    'alias' => array( 'slug', 'permalink' )
985
                ),
986
                'to_ping' => array(
987
                    'name' => 'to_ping',
988
                    'label' => 'To Ping',
989
                    'type' => 'text',
990
                    'alias' => array(),
991
                    'hidden' => true
992
                ),
993
                'pinged' => array(
994
                    'name' => 'pinged',
995
                    'label' => 'Pinged',
996
                    'type' => 'text',
997
                    'alias' => array(),
998
                    'hidden' => true
999
                ),
1000
                'post_modified' => array(
1001
                    'name' => 'post_modified',
1002
                    'label' => 'Last Modified Date',
1003
                    'type' => 'datetime',
1004
                    'alias' => array( 'modified' ),
1005
                    'hidden' => true
1006
                ),
1007
                'post_modified_gmt' => array(
1008
                    'name' => 'post_modified_gmt',
1009
                    'label' => 'Last Modified Date (GMT)',
1010
                    'type' => 'datetime',
1011
                    'alias' => array(),
1012
                    'hidden' => true
1013
                ),
1014
                'post_content_filtered' => array(
1015
                    'name' => 'post_content_filtered',
1016
                    'label' => 'Content (filtered)',
1017
                    'type' => 'paragraph',
1018
                    'alias' => array(),
1019
                    'hidden' => true,
1020
                    'options' => array(
1021
                        'paragraph_allow_html' => 1,
1022
                        'paragraph_oembed' => 1,
1023
                        'paragraph_wptexturize' => 1,
1024
                        'paragraph_convert_chars' => 1,
1025
                        'paragraph_wpautop' => 1,
1026
                        'paragraph_allow_shortcode' => 1,
1027
                        'paragraph_allowed_html_tags' => ''
1028
                    )
1029
                ),
1030
                'post_parent' => array(
1031
                    'name' => 'post_parent',
1032
                    'label' => 'Parent',
1033
                    'type' => 'pick',
1034
                    'pick_object' => 'post_type',
1035
                    'pick_val' => '__current__',
1036
                    'alias' => array( 'parent' ),
1037
                    'data' => array(),
1038
                    'hidden' => true
1039
                ),
1040
                'guid' => array(
1041
                    'name' => 'guid',
1042
                    'label' => 'GUID',
1043
                    'type' => 'text',
1044
                    'alias' => array(),
1045
                    'hidden' => true
1046
                ),
1047
                'menu_order' => array(
1048
                    'name' => 'menu_order',
1049
                    'label' => 'Menu Order',
1050
                    'type' => 'number',
1051
                    'alias' => array(),
1052
	                'options' => array(
1053
                        'number_format' => '9999.99'
1054
	                )
1055
                ),
1056
                'post_type' => array(
1057
                    'name' => 'post_type',
1058
                    'label' => 'Type',
1059
                    'type' => 'text',
1060
                    'alias' => array( 'type' ),
1061
                    'hidden' => true
1062
                ),
1063
                'post_mime_type' => array(
1064
                    'name' => 'post_mime_type',
1065
                    'label' => 'Mime Type',
1066
                    'type' => 'text',
1067
                    'alias' => array(),
1068
                    'hidden' => true
1069
                ),
1070
                'comment_count' => array(
1071
                    'name' => 'comment_count',
1072
                    'label' => 'Comment Count',
1073
                    'type' => 'number',
1074
                    'alias' => array(),
1075
                    'hidden' => true
1076
                )
1077
            );
1078
1079
            if ( !empty( $pod ) ) {
1080
                $taxonomies = get_object_taxonomies( $pod_name, 'objects' );
1081
1082
                foreach ( $taxonomies as $taxonomy ) {
1083
                    $fields[ $taxonomy->name ] = array(
1084
                        'name' => $taxonomy->name,
1085
                        'label' => $taxonomy->labels->name,
1086
                        'type' => 'taxonomy',
1087
						'pick_object' => 'taxonomy',
1088
						'pick_val' => $taxonomy->name,
1089
                        'alias' => array(),
1090
                        'hidden' => true,
1091
						'options' => array(
1092
							'taxonomy_format_type' => 'multi'
1093
						)
1094
                    );
1095
                }
1096
            }
1097
        }
1098
        elseif ( 'user' == $object ) {
1099
            $fields = array(
1100
	            'ID' => array(
1101
		            'name' => 'ID',
1102
		            'label' => 'ID',
1103
		            'type' => 'number',
1104
		            'alias' => array( 'id' ),
1105
	                'options' => array(
1106
                        'number_format' => '9999.99'
1107
	                )
1108
	            ),
1109
                'user_login' => array(
1110
                    'name' => 'user_login',
1111
                    'label' => 'Title',
1112
                    'type' => 'text',
1113
                    'alias' => array( 'login' ),
1114
                    'options' => array(
1115
                        'required' => 1
1116
                    )
1117
                ),
1118
                'user_nicename' => array(
1119
                    'name' => 'user_nicename',
1120
                    'label' => 'Permalink',
1121
                    'type' => 'slug',
1122
                    'alias' => array( 'nicename', 'slug', 'permalink' )
1123
                ),
1124
                'display_name' => array(
1125
                    'name' => 'display_name',
1126
                    'label' => 'Display Name',
1127
                    'type' => 'text',
1128
                    'alias' => array( 'title', 'name' )
1129
                ),
1130
                'user_pass' => array(
1131
                    'name' => 'user_pass',
1132
                    'label' => 'Password',
1133
                    'type' => 'text',
1134
                    'alias' => array( 'password', 'pass' ),
1135
                    'options' => array(
1136
                        'required' => 1,
1137
                        'text_format_type' => 'password'
1138
                    )
1139
                ),
1140
                'user_email' => array(
1141
                    'name' => 'user_email',
1142
                    'label' => 'E-mail',
1143
                    'type' => 'text',
1144
                    'alias' => array( 'email' ),
1145
                    'options' => array(
1146
                        'required' => 1,
1147
                        'text_format_type' => 'email'
1148
                    )
1149
                ),
1150
                'user_url' => array(
1151
                    'name' => 'user_url',
1152
                    'label' => 'URL',
1153
                    'type' => 'text',
1154
                    'alias' => array( 'url', 'website' ),
1155
                    'options' => array(
1156
                        'required' => 0,
1157
                        'text_format_type' => 'website',
1158
                        'text_format_website' => 'normal'
1159
                    )
1160
                ),
1161
                'user_registered' => array(
1162
                    'name' => 'user_registered',
1163
                    'label' => 'Registration Date',
1164
                    'type' => 'date',
1165
                    'alias' => array( 'created', 'date', 'registered' ),
1166
                    'options' => array(
1167
                        'date_format_type' => 'datetime'
1168
                    )
1169
                )
1170
            );
1171
        }
1172
        elseif ( 'comment' == $object ) {
1173
            $fields = array(
1174
	            'comment_ID' => array(
1175
		            'name' => 'comment_ID',
1176
		            'label' => 'ID',
1177
		            'type' => 'number',
1178
		            'alias' => array( 'id', 'ID', 'comment_id' ),
1179
	                'options' => array(
1180
                        'number_format' => '9999.99'
1181
	                )
1182
	            ),
1183
                'comment_content' => array(
1184
                    'name' => 'comment_content',
1185
                    'label' => 'Content',
1186
                    'type' => 'wysiwyg',
1187
                    'alias' => array( 'content' )
1188
                ),
1189
                'comment_approved' => array(
1190
                    'name' => 'comment_approved',
1191
                    'label' => 'Approved',
1192
                    'type' => 'number',
1193
                    'alias' => array( 'approved' ),
1194
	                'options' => array(
1195
                        'number_format' => '9999.99'
1196
	                )
1197
                ),
1198
                'comment_post_ID' => array(
1199
                    'name' => 'comment_post_ID',
1200
                    'label' => 'Post',
1201
                    'type' => 'pick',
1202
                    'alias' => array( 'post', 'post_id' ),
1203
                    'data' => array()
1204
                ),
1205
                'user_id' => array(
1206
                    'name' => 'user_id',
1207
                    'label' => 'Author',
1208
                    'type' => 'pick',
1209
                    'alias' => array( 'author' ),
1210
                    'pick_object' => 'user',
1211
                    'data' => array()
1212
                ),
1213
                'comment_date' => array(
1214
                    'name' => 'comment_date',
1215
                    'label' => 'Date',
1216
                    'type' => 'date',
1217
                    'alias' => array( 'created', 'date' ),
1218
                    'options' => array(
1219
                        'date_format_type' => 'datetime'
1220
                    )
1221
                ),
1222
                'comment_author' => array(
1223
                    'name' => 'comment_author',
1224
                    'label' => 'Author',
1225
                    'type' => 'text',
1226
                    'alias' => array( 'author' )
1227
                ),
1228
                'comment_author_email' => array(
1229
                    'name' => 'comment_author_email',
1230
                    'label' => 'Author E-mail',
1231
                    'type' => 'email',
1232
                    'alias' => array( 'author_email' )
1233
                ),
1234
                'comment_author_url' => array(
1235
                    'name' => 'comment_author_url',
1236
                    'label' => 'Author URL',
1237
                    'type' => 'text',
1238
                    'alias' => array( 'author_url' )
1239
                ),
1240
                'comment_author_IP' => array(
1241
                    'name' => 'comment_author_IP',
1242
                    'label' => 'Author IP',
1243
                    'type' => 'text',
1244
                    'alias' => array( 'author_IP' )
1245
                ),
1246
                'comment_type' => array(
1247
                    'name' => 'comment_type',
1248
                    'label' => 'Type',
1249
                    'type' => 'text',
1250
                    'alias' => array( 'type' ),
1251
                    'hidden' => true
1252
                ),
1253
                'comment_parent' => array(
1254
                    'name' => 'comment_parent',
1255
                    'label' => 'Parent',
1256
                    'type' => 'pick',
1257
                    'pick_object' => 'comment',
1258
                    'pick_val' => '__current__',
1259
                    'alias' => array( 'parent' ),
1260
                    'data' => array(),
1261
                    'hidden' => true
1262
                )
1263
            );
1264
        }
1265
        elseif ( 'taxonomy' == $object ) {
1266
            $fields = array(
1267
	            'term_id' => array(
1268
		            'name' => 'term_id',
1269
		            'label' => 'ID',
1270
		            'type' => 'number',
1271
		            'alias' => array( 'id', 'ID' ),
1272
	                'options' => array(
1273
                        'number_format' => '9999.99'
1274
	                )
1275
	            ),
1276
                'name' => array(
1277
                    'name' => 'name',
1278
                    'label' => 'Title',
1279
                    'type' => 'text',
1280
                    'alias' => array( 'title' )
1281
                ),
1282
                'slug' => array(
1283
                    'name' => 'slug',
1284
                    'label' => 'Permalink',
1285
                    'type' => 'slug',
1286
                    'alias' => array( 'permalink' )
1287
                ),
1288
                'description' => array(
1289
                    'name' => 'description',
1290
                    'label' => 'Description',
1291
                    'type' => 'wysiwyg',
1292
                    'alias' => array( 'content' )
1293
                ),
1294
                'taxonomy' => array(
1295
                    'name' => 'taxonomy',
1296
                    'label' => 'Taxonomy',
1297
                    'type' => 'pick',
1298
                    'alias' => array()
1299
                ),
1300
                'parent' => array(
1301
                    'name' => 'parent',
1302
                    'label' => 'Parent',
1303
                    'type' => 'pick',
1304
                    'pick_object' => 'taxonomy',
1305
                    'pick_val' => '__current__',
1306
                    'alias' => array( 'parent' ),
1307
                    'data' => array(),
1308
                    'hidden' => true
1309
                ),
1310
                'term_taxonomy_id' => array(
1311
                    'name' => 'term_taxonomy_id',
1312
                    'label' => 'Term Taxonomy ID',
1313
                    'type' => 'number',
1314
                    'alias' => array(),
1315
                    'hidden' => true,
1316
	                'options' => array(
1317
                        'number_format' => '9999.99'
1318
	                )
1319
                ),
1320
                'term_group' => array(
1321
                    'name' => 'term_group',
1322
                    'label' => 'Term Group',
1323
                    'type' => 'number',
1324
                    'alias' => array( 'group' ),
1325
                    'hidden' => true,
1326
	                'options' => array(
1327
                        'number_format' => '9999.99'
1328
	                )
1329
                ),
1330
                'count' => array(
1331
                    'name' => 'count',
1332
                    'label' => 'Count',
1333
                    'type' => 'number',
1334
                    'alias' => array(),
1335
                    'hidden' => true,
1336
	                'options' => array(
1337
                        'number_format' => '9999.99'
1338
	                )
1339
                )
1340
            );
1341
        }
1342
1343
        $fields = $this->do_hook( 'get_wp_object_fields', $fields, $object, $pod );
1344
1345
        foreach ( $fields as $field => $options ) {
1346
            if ( !isset( $options[ 'alias' ] ) )
1347
                $options[ 'alias' ] = array();
1348
            else
1349
                $options[ 'alias' ] = (array) $options[ 'alias' ];
1350
1351
            if ( !isset( $options[ 'name' ] ) )
1352
                $options[ 'name' ] = $field;
1353
1354
            $fields[ $field ] = $options;
1355
        }
1356
1357
        $fields = PodsForm::fields_setup( $fields );
1358
1359 View Code Duplication
        if ( did_action( 'init' ) && pods_api_cache() )
1360
            pods_transient_set( trim( 'pods_api_object_fields_' . $object . $pod_name . '_', '_' ), $fields );
1361
1362
        return $fields;
1363
    }
1364
1365
    /**
1366
     *
1367
     * @see PodsAPI::save_pod
1368
     *
1369
     * Add a Pod via the Wizard
1370
     *
1371
     * $params['create_extend'] string Create or Extend a Content Type
1372
     * $params['create_pod_type'] string Pod Type (for Creating)
1373
     * $params['create_name'] string Pod Name (for Creating)
1374
     * $params['create_label_plural'] string Plural Label (for Creating)
1375
     * $params['create_label_singular'] string Singular Label (for Creating)
1376
     * $params['create_storage'] string Storage Type (for Creating Post Types)
1377
     * $params['create_storage_taxonomy'] string Storage Type (for Creating Taxonomies)
1378
     * $params['extend_pod_type'] string Pod Type (for Extending)
1379
     * $params['extend_post_type'] string Post Type (for Extending Post Types)
1380
     * $params['extend_taxonomy'] string Taxonomy (for Extending Taxonomies)
1381
     * $params['extend_storage'] string Storage Type (for Extending Post Types / Users / Comments)
1382
     *
1383
     * @param array $params An associative array of parameters
1384
     *
1385
     * @return bool|int Pod ID
1386
     * @since 2.0
1387
     */
1388
    public function add_pod ( $params ) {
1389
        $defaults = array(
1390
            'create_extend' => 'create',
1391
            'create_pod_type' => 'post_type',
1392
1393
            'create_name' => '',
1394
            'create_label_singular' => '',
1395
            'create_label_plural' => '',
1396
            'create_storage' => 'meta',
1397
            'create_storage_taxonomy' => ( function_exists( 'get_term_meta' ) ? 'meta' : 'none' ),
1398
1399
            'create_setting_name' => '',
1400
            'create_label_title' => '',
1401
            'create_label_menu' => '',
1402
            'create_menu_location' => 'settings',
1403
1404
            'extend_pod_type' => 'post_type',
1405
            'extend_post_type' => 'post',
1406
            'extend_taxonomy' => 'category',
1407
            'extend_table' => '',
1408
            'extend_storage' => 'meta',
1409
            'extend_storage_taxonomy' => ( function_exists( 'get_term_meta' ) ? 'meta' : 'table' ),
1410
        );
1411
1412
        $params = (object) array_merge( $defaults, (array) $params );
1413
1414
        if ( empty( $params->create_extend ) || !in_array( $params->create_extend, array( 'create', 'extend' ) ) )
1415
            return pods_error( __( 'Please choose whether to Create or Extend a Content Type', 'pods' ), $this );
1416
1417
        $pod_params = array(
1418
            'name' => '',
1419
            'label' => '',
1420
            'type' => '',
1421
            'storage' => 'table',
1422
            'object' => '',
1423
            'options' => array()
1424
        );
1425
1426
        if ( 'create' == $params->create_extend ) {
1427
            $label = ucwords( str_replace( '_', ' ', $params->create_name ) );
1428
1429
            if ( !empty( $params->create_label_singular ) )
1430
                $label = $params->create_label_singular;
1431
1432
            $pod_params[ 'name' ] = $params->create_name;
1433
            $pod_params[ 'label' ] = ( !empty( $params->create_label_plural ) ? $params->create_label_plural : $label );
1434
            $pod_params[ 'type' ] = $params->create_pod_type;
1435
            $pod_params[ 'options' ] = array(
1436
                'label_singular' => ( !empty( $params->create_label_singular ) ? $params->create_label_singular : $pod_params[ 'label' ] ),
1437
                'public' => 1,
1438
                'show_ui' => 1
1439
            );
1440
1441
            // Auto-generate name if not provided
1442
            if ( empty( $pod_params[ 'name' ] ) && !empty( $pod_params[ 'options' ][ 'label_singular' ] ) )
1443
                $pod_params[ 'name' ] = pods_clean_name( $pod_params[ 'options' ][ 'label_singular' ] );
1444
1445
            if ( 'post_type' == $pod_params[ 'type' ] ) {
1446
                if ( empty(  $pod_params[ 'name' ] ) )
1447
                    return pods_error( 'Please enter a Name for this Pod', $this );
1448
1449
                $pod_params[ 'storage' ] = $params->create_storage;
1450
1451
                if ( pods_tableless() )
1452
                    $pod_params[ 'storage' ] = 'meta';
1453
            }
1454 View Code Duplication
            elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
1455
                if ( empty(  $pod_params[ 'name' ] ) )
1456
                    return pods_error( 'Please enter a Name for this Pod', $this );
1457
1458
                $pod_params[ 'storage' ] = $params->create_storage_taxonomy;
1459
1460
                if ( pods_tableless() )
1461
                    $pod_params[ 'storage' ] = ( function_exists( 'get_term_meta' ) ? 'meta' : 'none' );
1462
            }
1463 View Code Duplication
            elseif ( 'pod' == $pod_params[ 'type' ] ) {
1464
                if ( empty(  $pod_params[ 'name' ] ) )
1465
                    return pods_error( 'Please enter a Name for this Pod', $this );
1466
1467
                if ( pods_tableless() ) {
1468
                    $pod_params[ 'type' ] = 'post_type';
1469
                    $pod_params[ 'storage' ] = 'meta';
1470
                }
1471
            }
1472
            elseif ( 'settings' == $pod_params[ 'type' ] ) {
1473
                $pod_params[ 'name' ] = $params->create_setting_name;
1474
                $pod_params[ 'label' ] = ( !empty( $params->create_label_title ) ? $params->create_label_title : ucwords( str_replace( '_', ' ', $params->create_setting_name ) ) );
1475
                $pod_params[ 'options' ] = array(
1476
                    'menu_name' => ( !empty( $params->create_label_menu ) ? $params->create_label_menu : $pod_params[ 'label' ] ),
1477
                    'menu_location' => $params->create_menu_location
1478
                );
1479
                $pod_params[ 'storage' ] = 'none';
1480
1481
                // Auto-generate name if not provided
1482
                if ( empty( $pod_params[ 'name' ] ) && !empty( $pod_params[ 'label' ] ) )
1483
                    $pod_params[ 'name' ] = pods_clean_name( $pod_params[ 'label' ] );
1484
1485
                if ( empty( $pod_params[ 'name' ] ) )
1486
                    return pods_error( 'Please enter a Name for this Pod', $this );
1487
            }
1488
        }
1489
        elseif ( 'extend' == $params->create_extend ) {
1490
            $pod_params[ 'type' ] = $params->extend_pod_type;
1491
1492
            if ( 'post_type' == $pod_params[ 'type' ] ) {
1493
                $pod_params[ 'storage' ] = $params->extend_storage;
1494
1495
                if ( pods_tableless() )
1496
                    $pod_params[ 'storage' ] = 'meta';
1497
1498
                $pod_params[ 'name' ] = $params->extend_post_type;
1499
            }
1500
            elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
1501
                $pod_params[ 'storage' ] = $params->extend_storage_taxonomy;
1502
1503
                if ( pods_tableless() )
1504
                    $pod_params[ 'storage' ] = ( function_exists( 'get_term_meta' ) ? 'meta' : 'none' );
1505
1506
                $pod_params[ 'name' ] = $params->extend_taxonomy;
1507
            }
1508
            elseif ( 'table' == $pod_params[ 'type' ] ) {
1509
                $pod_params[ 'storage' ] = 'table';
1510
                $pod_params[ 'name' ] = $params->extend_table;
1511
            }
1512
            else {
1513
                $pod_params[ 'storage' ] = $params->extend_storage;
1514
1515
                if ( pods_tableless() )
1516
                    $pod_params[ 'storage' ] = 'meta';
1517
1518
                $pod_params[ 'name' ] = $params->extend_pod_type;
1519
            }
1520
1521
            $pod_params[ 'label' ] = ucwords( str_replace( '_', ' ', $pod_params[ 'name' ] ) );
1522
            $pod_params[ 'object' ] = $pod_params[ 'name' ];
1523
        }
1524
1525
        if ( empty( $pod_params[ 'object' ] ) ) {
1526
            if ( 'post_type' == $pod_params[ 'type' ] ) {
1527
                $check = get_post_type_object( $pod_params[ 'name' ] );
1528
1529 View Code Duplication
                if ( !empty( $check ) )
1530
                    return pods_error( sprintf( __( 'Post Type %s already exists, try extending it instead', 'pods' ), $pod_params[ 'name' ] ), $this );
1531
1532
                $pod_params[ 'options' ][ 'supports_title' ] = 1;
1533
                $pod_params[ 'options' ][ 'supports_editor' ] = 1;
1534
            }
1535
            elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
1536
                $check = get_taxonomy( $pod_params[ 'name' ] );
1537
1538 View Code Duplication
                if ( !empty( $check ) )
1539
                    return pods_error( sprintf( __( 'Taxonomy %s already exists, try extending it instead', 'pods' ), $pod_params[ 'name' ] ), $this );
1540
            }
1541
        }
1542
1543
        if ( !empty( $pod_params ) )
1544
            return $this->save_pod( $pod_params );
1545
1546
        return false;
1547
    }
1548
1549
    /**
1550
     * Add or edit a Pod
1551
     *
1552
     * $params['id'] int The Pod ID
1553
     * $params['name'] string The Pod name
1554
     * $params['label'] string The Pod label
1555
     * $params['type'] string The Pod type
1556
     * $params['object'] string The object being extended (if any)
1557
     * $params['storage'] string The Pod storage
1558
     * $params['options'] array Options
1559
     *
1560
     * @param array $params An associative array of parameters
1561
     * @param bool $sanitized (optional) Decides whether the params have been sanitized before being passed, will sanitize them if false.
1562
     * @param bool|int $db (optional) Whether to save into the DB or just return Pod array.
1563
     *
1564
     * @return int Pod ID
1565
     * @since 1.7.9
1566
     */
1567
    public function save_pod ( $params, $sanitized = false, $db = true ) {
1568
        $tableless_field_types = PodsForm::tableless_field_types();
1569
        $simple_tableless_objects = PodsForm::simple_tableless_objects();
1570
1571
        $load_params = (object) $params;
1572
1573
        if ( isset( $load_params->id ) && isset( $load_params->name ) )
1574
            unset( $load_params->name );
1575
1576
        if ( isset( $load_params->old_name ) )
1577
            $load_params->name = $load_params->old_name;
1578
1579
        $load_params->table_info = true;
1580
1581
        $pod = $this->load_pod( $load_params, false );
1582
1583
        $params = (object) $params;
1584
1585
        if ( false === $sanitized )
1586
            $params = pods_sanitize( $params );
1587
1588
        $old_id = $old_name = $old_storage = null;
1589
1590
        $old_fields = $old_options = array();
1591
1592
		if ( isset( $params->name ) && ! isset( $params->object ) ) {
1593
			$params->name = pods_clean_name( $params->name );
1594
		}
1595
1596
        if ( !empty( $pod ) ) {
1597
            if ( isset( $params->id ) && 0 < $params->id )
1598
                $old_id = $params->id;
1599
1600
            $params->id = $pod[ 'id' ];
1601
1602
            $old_name = $pod[ 'name' ];
1603
            $old_storage = $pod[ 'storage' ];
1604
            $old_fields = $pod[ 'fields' ];
1605
            $old_options = $pod[ 'options' ];
1606
1607
            if ( !isset( $params->name ) && empty( $params->name ) )
1608
                $params->name = $pod[ 'name' ];
1609
1610
            if ( $old_name != $params->name && false !== $this->pod_exists( array( 'name' => $params->name ) ) )
1611
                return pods_error( sprintf( __( 'Pod %s already exists, you cannot rename %s to that', 'pods' ), $params->name, $old_name ), $this );
1612
1613
            if ( $old_name != $params->name && in_array( $pod[ 'type' ], array( 'user', 'comment', 'media' ) ) && in_array( $pod[ 'object' ], array( 'user', 'comment', 'media' ) ) )
1614
                return pods_error( sprintf( __( 'Pod %s cannot be renamed, it extends an existing WP Object', 'pods' ), $old_name ), $this );
1615
1616
            if ( $old_name != $params->name && in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && !empty( $pod[ 'object' ] ) && $pod[ 'object' ] == $old_name )
1617
                return pods_error( sprintf( __( 'Pod %s cannot be renamed, it extends an existing WP Object', 'pods' ), $old_name ), $this );
1618
1619
            if ( $old_id != $params->id ) {
1620
                if ( $params->type == $pod[ 'type' ] && isset( $params->object ) && $params->object == $pod[ 'object' ] )
1621
                    return pods_error( sprintf( __( 'Pod using %s already exists, you can not reuse an object across multiple pods', 'pods' ), $params->object ), $this );
1622
                else
1623
                    return pods_error( sprintf( __( 'Pod %s already exists', 'pods' ), $params->name ), $this );
1624
            }
1625
        }
1626
		elseif ( in_array( $params->name, array( 'order','orderby','post_type' ) ) && 'post_type' == pods_var( 'type', $params ) ) {
1627
			return pods_error( sprintf( 'There are certain names that a Custom Post Types cannot be named and unfortunately, %s is one of them.', $params->name ), $this );
1628
		}
1629
        else {
1630
            $pod = array(
1631
                'id' => 0,
1632
                'name' => $params->name,
1633
                'label' => $params->name,
1634
                'description' => '',
1635
                'type' => 'pod',
1636
                'storage' => 'table',
1637
                'object' => '',
1638
                'alias' => '',
1639
                'options' => array(),
1640
                'fields' => array()
1641
            );
1642
        }
1643
1644
        // Blank out fields and options for AJAX calls (everything should be sent to it for a full overwrite)
1645
        if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
1646
            $pod[ 'fields' ] = array();
1647
            $pod[ 'options' ] = array();
1648
        }
1649
1650
        // Setup options
1651
        $options = get_object_vars( $params );
1652
1653
        if ( isset( $options[ 'method' ] ) )
1654
            unset( $options[ 'method' ] );
1655
1656
        $options_ignore = array(
1657
            'object_type',
1658
            'object_name',
1659
            'table',
1660
            'meta_table',
1661
            'pod_table',
1662
            'field_id',
1663
            'field_index',
1664
            'field_slug',
1665
            'field_type',
1666
            'field_parent',
1667
            'field_parent_select',
1668
            'meta_field_id',
1669
            'meta_field_index',
1670
            'meta_field_value',
1671
            'pod_field_id',
1672
            'pod_field_index',
1673
            'object_fields',
1674
            'join',
1675
            'where',
1676
            'where_default',
1677
            'orderby',
1678
            'pod',
1679
            'recurse',
1680
            'table_info',
1681
            'attributes',
1682
            'group',
1683
            'grouped',
1684
            'developer_mode',
1685
            'dependency',
1686
            'depends-on',
1687
            'excludes-on'
1688
        );
1689
1690
        foreach ( $options_ignore as $ignore ) {
1691
            if ( isset( $options[ $ignore ] ) )
1692
                unset( $options[ $ignore ] );
1693
        }
1694
1695
        $exclude = array(
1696
            'id',
1697
            'name',
1698
            'label',
1699
            'description',
1700
            'type',
1701
            'storage',
1702
            'object',
1703
            'alias',
1704
            'options',
1705
            'fields'
1706
        );
1707
1708 View Code Duplication
        foreach ( $exclude as $k => $exclude_field ) {
1709
            $aliases = array( $exclude_field );
1710
1711
            if ( is_array( $exclude_field ) ) {
1712
                $aliases = array_merge( array( $k ), $exclude_field );
1713
                $exclude_field = $k;
1714
            }
1715
1716
            foreach ( $aliases as $alias ) {
1717
                if ( isset( $options[ $alias ] ) ) {
1718
                    $pod[ $exclude_field ] = pods_trim( $options[ $alias ] );
1719
1720
                    unset( $options[ $alias ] );
1721
                }
1722
            }
1723
        }
1724
1725
        if ( pods_tableless() && !in_array( $pod[ 'type' ], array( 'settings', 'table' ) ) ) {
1726
            if ( 'pod' == $pod[ 'type' ] )
1727
                $pod[ 'type' ] = 'post_type';
1728
1729 View Code Duplication
            if ( 'table' == $pod[ 'storage' ] ) {
1730
                if ( 'taxonomy' == $pod[ 'type' ] && ! function_exists( 'get_term_meta' ) )
1731
                    $pod[ 'storage' ] = 'none';
1732
                else
1733
                    $pod[ 'storage' ] = 'meta';
1734
            }
1735
        }
1736
1737
        $pod[ 'options' ][ 'type' ] = $pod[ 'type' ];
1738
        $pod[ 'options' ][ 'storage' ] = $pod[ 'storage' ];
1739
        $pod[ 'options' ][ 'object' ] = $pod[ 'object' ];
1740
        $pod[ 'options' ][ 'alias' ] = $pod[ 'alias' ];
1741
1742
        $pod[ 'options' ] = array_merge( $pod[ 'options' ], $options );
1743
1744
		/**
1745
		 * @var WP_Query
1746
		 */
1747
		global $wp_query;
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...
1748
1749
		$reserved_query_vars = array(
1750
			'post_type',
1751
			'taxonomy',
1752
			'output'
1753
		);
1754
1755
		if ( is_object( $wp_query ) ) {
1756
			$reserved_query_vars = array_merge( $reserved_query_vars, array_keys( $wp_query->fill_query_vars( array() ) ) );
1757
		}
1758
1759 View Code Duplication
		if ( isset( $pod[ 'options' ][ 'query_var_string' ] ) ) {
1760
			if ( in_array( $pod[ 'options' ][ 'query_var_string' ], $reserved_query_vars ) ) {
1761
				$pod[ 'options' ][ 'query_var_string' ] = $pod[ 'options' ][ 'type' ] . '_' . $pod[ 'options' ][ 'query_var_string' ];
1762
			}
1763
		}
1764
1765 View Code Duplication
		if ( isset( $pod[ 'options' ][ 'query_var' ] ) ) {
1766
			if ( in_array( $pod[ 'options' ][ 'query_var' ], $reserved_query_vars ) ) {
1767
				$pod[ 'options' ][ 'query_var' ] = $pod[ 'options' ][ 'type' ] . '_' . $pod[ 'options' ][ 'query_var' ];
1768
			}
1769
		}
1770
1771
        if ( strlen( $pod[ 'label' ] ) < 1 )
1772
            $pod[ 'label' ] = $pod[ 'name' ];
1773
1774
        if ( 'post_type' == $pod[ 'type' ] ) {
1775
            // Max length for post types are 20 characters
1776
            $pod[ 'name' ] = substr( $pod[ 'name' ], 0, 20 );
1777
        }
1778
        elseif ( 'taxonomy' == $pod[ 'type' ] ) {
1779
            // Max length for taxonomies are 32 characters
1780
            $pod[ 'name' ] = substr( $pod[ 'name' ], 0, 32 );
1781
        }
1782
1783
        $params->id = $pod[ 'id' ];
1784
        $params->name = $pod[ 'name' ];
1785
1786
        if ( null !== $old_name && $old_name != $params->name && empty( $pod[ 'object' ] ) ) {
1787
            if ( 'post_type' == $pod[ 'type' ] ) {
1788
                $check = get_post_type_object( $params->name );
1789
1790 View Code Duplication
                if ( !empty( $check ) )
1791
                    return pods_error( sprintf( __( 'Post Type %s already exists, you cannot rename %s to that', 'pods' ), $params->name, $old_name ), $this );
1792
            }
1793
            elseif ( 'taxonomy' == $pod[ 'type' ] ) {
1794
                $check = get_taxonomy( $params->name );
1795
1796 View Code Duplication
                if ( !empty( $check ) )
1797
                    return pods_error( sprintf( __( 'Taxonomy %s already exists, you cannot rename %s to that', 'pods' ), $params->name, $old_name ), $this );
1798
            }
1799
        }
1800
1801
        $field_table_operation = true;
1802
1803
        // Add new pod
1804
        if ( empty( $params->id ) ) {
1805
            if ( strlen( $params->name ) < 1 )
1806
                return pods_error( __( 'Pod name cannot be empty', 'pods' ), $this );
1807
1808
            $post_data = array(
1809
                'post_name' => $pod[ 'name' ],
1810
                'post_title' => $pod[ 'label' ],
1811
                'post_content' => $pod[ 'description' ],
1812
                'post_type' => '_pods_pod',
1813
                'post_status' => 'publish'
1814
            );
1815
1816
            if ( 'pod' == $pod[ 'type' ] && ( !is_array( $pod[ 'fields' ] ) || empty( $pod[ 'fields' ] ) ) ) {
1817
                $pod[ 'fields' ] = array();
1818
1819
                $pod[ 'fields' ][ 'name' ] = array(
1820
                    'name' => 'name',
1821
                    'label' => 'Name',
1822
                    'type' => 'text',
1823
                    'options' => array(
1824
                        'required' => '1'
1825
                    )
1826
                );
1827
1828
                $pod[ 'fields' ][ 'created' ] = array(
1829
                    'name' => 'created',
1830
                    'label' => 'Date Created',
1831
                    'type' => 'datetime',
1832
                    'options' => array(
1833
                        'datetime_format' => 'ymd_slash',
1834
                        'datetime_time_type' => '12',
1835
                        'datetime_time_format' => 'h_mm_ss_A'
1836
                    )
1837
                );
1838
1839
                $pod[ 'fields' ][ 'modified' ] = array(
1840
                    'name' => 'modified',
1841
                    'label' => 'Date Modified',
1842
                    'type' => 'datetime',
1843
                    'options' => array(
1844
                        'datetime_format' => 'ymd_slash',
1845
                        'datetime_time_type' => '12',
1846
                        'datetime_time_format' => 'h_mm_ss_A'
1847
                    )
1848
                );
1849
1850
                $pod[ 'fields' ][ 'author' ] = array(
1851
                    'name' => 'author',
1852
                    'label' => 'Author',
1853
                    'type' => 'pick',
1854
                    'pick_object' => 'user',
1855
                    'options' => array(
1856
                        'pick_format_type' => 'single',
1857
                        'pick_format_single' => 'autocomplete',
1858
                        'default_value' => '{@user.ID}'
1859
                    )
1860
                );
1861
1862
                $pod[ 'fields' ][ 'permalink' ] = array(
1863
                    'name' => 'permalink',
1864
                    'label' => 'Permalink',
1865
                    'type' => 'slug',
1866
                    'description' => 'Leave blank to auto-generate from Name'
1867
                );
1868
1869
                if ( !isset( $pod[ 'options' ][ 'pod_index' ] ) )
1870
                    $pod[ 'options' ][ 'pod_index' ] = 'name';
1871
            }
1872
1873
            $pod = $this->do_hook( 'save_pod_default_pod', $pod, $params, $sanitized, $db );
1874
1875
            $field_table_operation = false;
1876
        }
1877
        else {
1878
            $post_data = array(
1879
                'ID' => $pod[ 'id' ],
1880
                'post_name' => $pod[ 'name' ],
1881
                'post_title' => $pod[ 'label' ],
1882
                'post_content' => $pod[ 'description' ],
1883
                'post_status' => 'publish'
1884
            );
1885
        }
1886
1887 View Code Duplication
        if ( true === $db ) {
1888
            if ( !has_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ) ) )
1889
                add_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ), 100, 6 );
1890
1891
            $conflicted = false;
1892
1893
            // Headway compatibility fix
1894
            if ( has_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 ) ) {
1895
                remove_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
1896
1897
                $conflicted = true;
1898
            }
1899
1900
            $params->id = $this->save_wp_object( 'post', $post_data, $pod[ 'options' ], true, true );
1901
1902
            if ( $conflicted )
1903
                add_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
1904
1905
            if ( false === $params->id )
1906
                return pods_error( __( 'Cannot save Pod', 'pods' ), $this );
1907
        }
1908
        elseif ( empty( $params->id ) )
1909
            $params->id = (int) $db;
1910
1911
        $pod[ 'id' ] = $params->id;
1912
1913
        // Setup / update tables
1914
        if ( 'table' != $pod[ 'type' ] && 'table' == $pod[ 'storage' ] && $old_storage != $pod[ 'storage' ] && $db ) {
1915
            $definitions = array( "`id` BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY" );
1916
1917
            $defined_fields = array();
1918
1919
            foreach ( $pod[ 'fields' ] as $field ) {
1920
                if ( !is_array( $field ) || !isset( $field[ 'name' ] ) || in_array( $field[ 'name' ], $defined_fields ) )
1921
                    continue;
1922
1923
                $defined_fields[] = $field[ 'name' ];
1924
1925
                if ( !in_array( $field[ 'type' ], $tableless_field_types ) || ( 'pick' == $field[ 'type' ] && in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) ) ) {
1926
                    $definition = $this->get_field_definition( $field[ 'type' ], array_merge( $field, pods_var_raw( 'options', $field, array() ) ) );
1927
1928
                    if ( 0 < strlen( $definition ) )
1929
                        $definitions[] = "`{$field['name']}` " . $definition;
1930
                }
1931
            }
1932
1933
            pods_query( "DROP TABLE IF EXISTS `@wp_pods_{$params->name}`" );
1934
1935
            $result = pods_query( "CREATE TABLE `@wp_pods_{$params->name}` (" . implode( ', ', $definitions ) . ") DEFAULT CHARSET utf8", $this );
1936
1937
            if ( empty( $result ) )
1938
                return pods_error( __( 'Cannot add Database Table for Pod', 'pods' ), $this );
1939
1940
        }
1941
        elseif ( 'table' != $pod[ 'type' ] && 'table' == $pod[ 'storage' ] && $pod[ 'storage' ] == $old_storage && null !== $old_name && $old_name != $params->name && $db ) {
1942
            $result = pods_query( "ALTER TABLE `@wp_pods_{$old_name}` RENAME `@wp_pods_{$params->name}`", $this );
1943
1944
            if ( empty( $result ) )
1945
                return pods_error( __( 'Cannot update Database Table for Pod', 'pods' ), $this );
1946
        }
1947
1948
        /**
1949
         * @var $wpdb wpdb
1950
         */
1951
        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...
1952
1953
		if ( null !== $old_name && $old_name != $params->name && $db ) {
1954
        	// Rename items in the DB pointed at the old WP Object names
1955
			if ( 'post_type' == $pod[ 'type' ] && empty( $pod[ 'object' ] ) ) {
1956
				$this->rename_wp_object_type( 'post', $old_name, $params->name );
1957
			}
1958 View Code Duplication
			elseif ( 'taxonomy' == $pod[ 'type' ] && empty( $pod[ 'object' ] ) ) {
1959
				$this->rename_wp_object_type( 'taxonomy', $old_name, $params->name );
1960
			}
1961 View Code Duplication
			elseif ( 'comment' == $pod[ 'type' ] && empty( $pod[ 'object' ] ) ) {
1962
				$this->rename_wp_object_type( 'comment', $old_name, $params->name );
1963
			}
1964
			elseif ( 'settings' == $pod[ 'type' ] ) {
1965
				$this->rename_wp_object_type( 'settings', $old_name, $params->name );
1966
			}
1967
1968
        	// Sync any related fields if the name has changed
1969
            $fields = pods_query( "
1970
                SELECT `p`.`ID`
1971
                FROM `{$wpdb->posts}` AS `p`
1972
                LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
1973
                LEFT JOIN `{$wpdb->postmeta}` AS `pm2` ON `pm2`.`post_id` = `p`.`ID`
1974
                WHERE
1975
                    `p`.`post_type` = '_pods_field'
1976
                    AND `pm`.`meta_key` = 'pick_object'
1977
                    AND (
1978
                    	`pm`.`meta_value` = 'pod'
1979
                    	OR `pm`.`meta_value` = '" . $pod[ 'type' ] . "'
1980
					)
1981
                    AND `pm2`.`meta_key` = 'pick_val'
1982
                    AND `pm2`.`meta_value` = '{$old_name}'
1983
            " );
1984
1985 View Code Duplication
            if ( !empty( $fields ) ) {
1986
                foreach ( $fields as $field ) {
1987
                    update_post_meta( $field->ID, 'pick_object', $pod[ 'type' ] );
1988
                    update_post_meta( $field->ID, 'pick_val', $params->name );
1989
                }
1990
            }
1991
1992
            $fields = pods_query( "
1993
                SELECT `p`.`ID`
1994
                FROM `{$wpdb->posts}` AS `p`
1995
                LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
1996
                WHERE
1997
                    `p`.`post_type` = '_pods_field'
1998
                    AND `pm`.`meta_key` = 'pick_object'
1999
                    AND (
2000
                    	`pm`.`meta_value` = 'pod-{$old_name}'
2001
                    	OR `pm`.`meta_value` = '" . $pod[ 'type' ] . "-{$old_name}'
2002
					)
2003
            " );
2004
2005 View Code Duplication
            if ( !empty( $fields ) ) {
2006
                foreach ( $fields as $field ) {
2007
                    update_post_meta( $field->ID, 'pick_object', $pod[ 'type' ] );
2008
                    update_post_meta( $field->ID, 'pick_val', $params->name );
2009
                }
2010
            }
2011
        }
2012
2013
        // Sync built-in options for post types and taxonomies
2014
        if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && empty( $pod[ 'object' ] ) && $db ) {
2015
            // Build list of 'built_in' for later
2016
            $built_in = array();
2017
2018
            foreach ( $pod[ 'options' ] as $key => $val ) {
2019
                if ( false === strpos( $key, 'built_in_' ) )
2020
                    continue;
2021
                elseif ( false !== strpos( $key, 'built_in_post_types_' ) )
2022
                    $built_in_type = 'post_type';
2023
                elseif ( false !== strpos( $key, 'built_in_taxonomies_' ) )
2024
                    $built_in_type = 'taxonomy';
2025
                else
2026
                    continue;
2027
2028
                if ( $built_in_type == $pod[ 'type' ] )
2029
                    continue;
2030
2031
                if ( !isset( $built_in[ $built_in_type ] ) )
2032
                    $built_in[ $built_in_type ] = array();
2033
2034
                $built_in_object = str_replace( array( 'built_in_post_types_', 'built_in_taxonomies_' ), '', $key );
2035
2036
                $built_in[ $built_in_type ][ $built_in_object ] = (int) $val;
2037
            }
2038
2039
            $lookup_option = $lookup_built_in = false;
2040
2041
            $lookup_name = $pod[ 'name' ];
2042
2043
            if ( 'post_type' == $pod[ 'type' ] ) {
2044
                $lookup_option = 'built_in_post_types_' . $lookup_name;
2045
                $lookup_built_in = 'taxonomy';
2046
            }
2047
            elseif ( 'taxonomy' == $pod[ 'type' ] ) {
2048
                $lookup_option = 'built_in_taxonomies_' . $lookup_name;
2049
                $lookup_built_in = 'post_type';
2050
            }
2051
2052
            if ( !empty( $lookup_option ) && !empty( $lookup_built_in ) && isset( $built_in[ $lookup_built_in ] ) ) {
2053
                foreach ( $built_in[ $lookup_built_in ] as $built_in_object => $val ) {
2054
                    $search_val = 1;
2055
2056
                    if ( 1 == $val )
2057
                        $search_val = 0;
2058
2059
                    $query = "SELECT p.ID FROM {$wpdb->posts} AS p
2060
                                LEFT JOIN {$wpdb->postmeta} AS pm ON pm.post_id = p.ID AND pm.meta_key = '{$lookup_option}'
2061
                                LEFT JOIN {$wpdb->postmeta} AS pm2 ON pm2.post_id = p.ID AND pm2.meta_key = 'type' AND pm2.meta_value = '{$lookup_built_in}'
2062
                                LEFT JOIN {$wpdb->postmeta} AS pm3 ON pm3.post_id = p.ID AND pm3.meta_key = 'object' AND pm3.meta_value = ''
2063
                                WHERE p.post_type = '_pods_pod' AND p.post_name = '{$built_in_object}'
2064
                                    AND pm2.meta_id IS NOT NULL
2065
                                    AND ( pm.meta_id IS NULL OR pm.meta_value = {$search_val} )";
2066
2067
                    $results = pods_query( $query );
2068
2069
                    if ( !empty( $results ) ) {
2070
                        foreach ( $results as $the_pod ) {
2071
                            delete_post_meta( $the_pod->ID, $lookup_option );
2072
2073
                            add_post_meta( $the_pod->ID, $lookup_option, $val );
2074
                        }
2075
                    }
2076
                }
2077
            }
2078
        }
2079
2080
        $saved = array();
2081
        $errors = array();
2082
2083
        $field_index_change = false;
2084
        $field_index_id = 0;
2085
2086
        $id_required = false;
2087
2088
        $field_index = pods_var( 'pod_index', $pod[ 'options' ], 'id', null, true );
2089
2090
        if ( 'pod' == $pod[ 'type' ] && !empty( $pod[ 'fields' ] ) && isset( $pod[ 'fields' ][ $field_index ] ) )
2091
            $field_index_id = $pod[ 'fields' ][ $field_index ];
2092
2093
        if ( isset( $params->fields ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
2094
            $fields = array();
2095
2096
            if ( isset( $params->fields ) ) {
2097
                $params->fields = (array) $params->fields;
2098
2099
                $weight = 0;
2100
2101
                foreach ( $params->fields as $field ) {
2102
                    if ( !is_array( $field ) || !isset( $field[ 'name' ] ) )
2103
                        continue;
2104
2105
                    if ( !isset( $field[ 'weight' ] ) ) {
2106
                        $field[ 'weight' ] = $weight;
2107
2108
                        $weight++;
2109
                    }
2110
2111
                    $fields[ $field[ 'name' ] ] = $field;
2112
                }
2113
            }
2114
2115
            $weight = 0;
2116
2117
            $saved_field_ids = array();
2118
2119
            foreach ( $pod[ 'fields' ] as $k => $field ) {
2120
                if ( !empty( $old_id ) && ( !is_array( $field ) || !isset( $field[ 'name' ] ) || !isset( $fields[ $field[ 'name' ] ] ) ) ) {
2121
                    // Iterative change handling for setup-edit.php
2122
                    if ( !is_array( $field ) && isset( $old_fields[ $k ] ) )
2123
                        $saved[ $old_fields[ $k ][ 'name' ] ] = true;
2124
2125
                    continue;
2126
                }
2127
2128
                if ( !empty( $old_id ) )
2129
                    $field = array_merge( $field, $fields[ $field[ 'name' ] ] );
2130
2131
                $field[ 'pod' ] = $pod;
2132
2133
                if ( !isset( $field[ 'weight' ] ) ) {
2134
                    $field[ 'weight' ] = $weight;
2135
2136
                    $weight++;
2137
                }
2138
2139
                if ( 0 < $field_index_id && pods_var( 'id', $field ) == $field_index_id )
2140
                    $field_index_change = $field[ 'name' ];
2141
2142
                if ( 0 < pods_var( 'id', $field ) )
2143
                    $id_required = true;
2144
2145
                if ( $id_required )
2146
                    $field[ 'id_required' ] = true;
2147
2148
                $field_data = $field;
2149
2150
                $field = $this->save_field( $field_data, $field_table_operation, true, $db );
2151
2152
                if ( true !== $db ) {
2153
                    $pod[ 'fields' ][ $k ] = $field;
2154
                    $saved_field_ids[] = $field[ 'id' ];
2155
                }
2156
                else {
2157
                    if ( !empty( $field ) && 0 < $field ) {
2158
                        $saved[ $field_data[ 'name' ] ] = true;
2159
                        $saved_field_ids[] = $field;
2160
                    }
2161
                    else
2162
                        $errors[] = sprintf( __( 'Cannot save the %s field', 'pods' ), $field_data[ 'name' ] );
2163
                }
2164
            }
2165
2166
            if ( true === $db ) {
2167
                foreach ( $old_fields as $field ) {
2168
                    if ( isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) || isset( $saved[ $field[ 'name' ] ] ) || in_array( $field[ 'id' ], $saved_field_ids ) )
2169
                        continue;
2170
2171
                    if ( $field[ 'id' ] == $field_index_id )
2172
                        $field_index_change = 'id';
2173
                    elseif ( $field[ 'name' ] == $field_index )
2174
                        $field_index_change = 'id';
2175
2176
                    $this->delete_field( array(
2177
                        'id' => (int) $field[ 'id' ],
2178
                        'name' => $field[ 'name' ],
2179
                        'pod' => $pod
2180
                    ), $field_table_operation );
2181
                }
2182
            }
2183
2184
            // Update field index if the name has changed or the field has been removed
2185
            if ( false !== $field_index_change && true === $db )
2186
                update_post_meta( $pod[ 'id' ], 'pod_index', $field_index_change );
2187
        }
2188
2189
        if ( !empty( $errors ) )
2190
            return pods_error( $errors, $this );
2191
2192
        $this->cache_flush_pods( $pod );
2193
2194
        if ( 'post_type' == $pod[ 'type' ] )
2195
            PodsMeta::$post_types[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
2196
        elseif ( 'taxonomy' == $pod[ 'type' ] )
2197
            PodsMeta::$taxonomies[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
2198
        elseif ( 'media' == $pod[ 'type' ] )
2199
            PodsMeta::$media[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
2200 View Code Duplication
        elseif ( 'user' == $pod[ 'type' ] )
2201
            PodsMeta::$user[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
2202
        elseif ( 'comment' == $pod[ 'type' ] )
2203
            PodsMeta::$comment[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
2204
2205
        // Register Post Types / Taxonomies post-registration from PodsInit
2206
        if ( !empty( PodsInit::$content_types_registered ) && in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && empty( $pod[ 'object' ] ) ) {
0 ignored issues
show
Bug introduced by
The property content_types_registered cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
2207
            global $pods_init;
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...
2208
2209
            $pods_init->setup_content_types( true );
2210
        }
2211
2212
        if ( true === $db )
2213
            return $pod[ 'id' ];
2214
        else
2215
            return $pod;
2216
    }
2217
2218
    /**
2219
     * Add or edit a field within a Pod
2220
     *
2221
     * $params['id'] int Field ID (id OR pod_id+pod+name required)
2222
     * $params['pod_id'] int Pod ID (id OR pod_id+pod+name required)
2223
     * $params['pod'] string Pod name (id OR pod_id+pod+name required)
2224
     * $params['name'] string Field name (id OR pod_id+pod+name required)
2225
     * $params['label'] string (optional) Field label
2226
     * $params['type'] string (optional) Field type (avatar, boolean, code, color, currency, date, datetime, email, file, number, paragraph, password, phone, pick, slug, text, time, website, wysiwyg)
2227
     * $params['pick_object'] string (optional) Related Object (for relationships)
2228
     * $params['pick_val'] string (optional) Related Object name (for relationships)
2229
     * $params['sister_id'] int (optional) Related Field ID (for bidirectional relationships)
2230
     * $params['weight'] int (optional) Order in which the field appears
2231
     * $params['options'] array (optional) Options
2232
     *
2233
     * @param array $params An associative array of parameters
2234
     * @param bool $table_operation (optional) Whether or not to handle table operations
2235
     * @param bool $sanitized (optional) Decides wether the params have been sanitized before being passed, will sanitize them if false.
2236
     * @param bool|int $db (optional) Whether to save into the DB or just return field array.
2237
     *
2238
     * @return int|array The field ID or field array (if !$db)
2239
     * @since 1.7.9
2240
     */
2241
    public function save_field ( $params, $table_operation = true, $sanitized = false, $db = true ) {
2242
        /**
2243
         * @var $wpdb wpdb
2244
         */
2245
        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...
2246
2247
        if ( true !== $db )
2248
            $table_operation = false;
2249
2250
        $tableless_field_types = PodsForm::tableless_field_types();
2251
        $simple_tableless_objects = PodsForm::simple_tableless_objects();
2252
2253
        $params = (object) $params;
2254
2255
        if ( false === $sanitized )
2256
            $params = pods_sanitize( $params );
2257
2258
        if ( isset( $params->pod_id ) )
2259
            $params->pod_id = pods_absint( $params->pod_id );
2260
2261
        if ( true !== $db )
2262
            $params->pod_id = (int) $db;
2263
2264
        $pod = null;
2265
        $save_pod = false;
2266
        $id_required = false;
2267
2268
        if ( isset( $params->id_required ) ) {
2269
            unset( $params->id_required );
2270
2271
            $id_required = true;
2272
        }
2273
2274 View Code Duplication
        if ( ( !isset( $params->pod ) || empty( $params->pod ) ) && ( !isset( $params->pod_id ) || empty( $params->pod_id ) ) )
2275
            return pods_error( __( 'Pod ID or name is required', 'pods' ), $this );
2276
2277
        if ( isset( $params->pod ) && is_array( $params->pod ) ) {
2278
            $pod = $params->pod;
2279
2280
            $save_pod = true;
2281
        }
2282 View Code Duplication
        elseif ( ( !isset( $params->pod_id ) || empty( $params->pod_id ) ) && ( true === $db || 0 < $db ) )
2283
            $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => true ) );
2284 View Code Duplication
        elseif ( !isset( $params->pod ) && ( true === $db || 0 < $db ) )
2285
            $pod = $this->load_pod( array( 'id' => $params->pod_id, 'table_info' => true ) );
2286 View Code Duplication
        elseif ( true === $db || 0 < $db )
2287
            $pod = $this->load_pod( array( 'id' => $params->pod_id, 'name' => $params->pod, 'table_info' => true ) );
2288
2289
        if ( empty( $pod ) && true === $db )
2290
            return pods_error( __( 'Pod not found', 'pods' ), $this );
2291
2292
        $params->pod_id = $pod[ 'id' ];
2293
        $params->pod = $pod[ 'name' ];
2294
        $params->pod_data = $pod;
2295
2296
        $params->name = pods_clean_name( $params->name, true, ( 'meta' == $pod[ 'storage' ] ? false : true ) );
2297
2298
        if ( !isset( $params->id ) )
2299
            $params->id = 0;
2300
2301
        if ( empty( $params->name ) )
2302
            return pods_error( 'Pod field name is required', $this );
2303
2304
        $field = $this->load_field( $params );
2305
2306
        unset( $params->pod_data );
2307
2308
        $old_id = $old_name = $old_type = $old_definition = $old_simple = $old_options = $old_sister_id = null;
2309
2310
        if ( !empty( $field ) ) {
2311
            $old_id = pods_var( 'id', $field );
2312
            $old_name = pods_clean_name( $field[ 'name' ], true, ( 'meta' == $pod[ 'storage' ] ? false : true ) );
2313
            $old_type = $field[ 'type' ];
2314
            $old_options = $field[ 'options' ];
2315
            $old_sister_id = (int) pods_var( 'sister_id', $old_options, 0 );
2316
2317
            $old_simple = ( 'pick' == $old_type && in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) );
2318
2319
            if ( isset( $params->name ) && !empty( $params->name ) )
2320
                $field[ 'name' ] = $params->name;
2321
2322
            if ( $old_name != $field[ 'name' ] && false !== $this->field_exists( $params ) )
2323
                return pods_error( sprintf( __( 'Field %s already exists, you cannot rename %s to that', 'pods' ), $field[ 'name' ], $old_name ), $this );
2324
2325
            if ( ( $id_required || !empty( $params->id ) ) && ( empty( $old_id ) || $old_id != $params->id ) )
2326
                return pods_error( sprintf( __( 'Field %s already exists', 'pods' ), $field[ 'name' ] ), $this );
2327
2328
            if ( empty( $params->id ) )
2329
                $params->id = $old_id;
2330
2331
            if ( !in_array( $old_type, $tableless_field_types ) || $old_simple ) {
2332
                $definition = $this->get_field_definition( $old_type, array_merge( $field, $old_options ) );
2333
2334
                if ( 0 < strlen( $definition ) )
2335
                    $old_definition = "`{$old_name}` " . $definition;
2336
            }
2337
        }
2338
        else {
2339
            $field = array(
2340
                'id' => 0,
2341
                'pod_id' => $params->pod_id,
2342
                'name' => $params->name,
2343
                'label' => $params->name,
2344
                'description' => '',
2345
                'type' => 'text',
2346
                'pick_object' => '',
2347
                'pick_val' => '',
2348
                'sister_id' => '',
2349
                'weight' => null,
2350
                'options' => array()
2351
            );
2352
        }
2353
2354
        // Setup options
2355
        $options = get_object_vars( $params );
2356
2357
        $options_ignore = array(
2358
            'method',
2359
            'table_info',
2360
            'attributes',
2361
            'group',
2362
            'grouped',
2363
            'developer_mode',
2364
            'dependency',
2365
            'depends-on',
2366
            'excludes-on'
2367
        );
2368
2369
        foreach ( $options_ignore as $ignore ) {
2370
            if ( isset( $options[ $ignore ] ) )
2371
                unset( $options[ $ignore ] );
2372
        }
2373
2374
        if ( isset( $options[ 'method' ] ) )
2375
            unset( $options[ 'method' ] );
2376
        elseif ( isset( $options[ 'table_info' ] ) )
2377
            unset( $options[ 'table_info' ] );
2378
2379
        $exclude = array(
2380
            'id',
2381
            'pod_id',
2382
            'pod',
2383
            'name',
2384
            'label',
2385
            'description',
2386
            'type',
2387
            'pick_object',
2388
            'pick_val',
2389
            'sister_id',
2390
            'weight',
2391
            'options'
2392
        );
2393
2394 View Code Duplication
        foreach ( $exclude as $k => $exclude_field ) {
2395
            $aliases = array( $exclude_field );
2396
2397
            if ( is_array( $exclude_field ) ) {
2398
                $aliases = array_merge( array( $k ), $exclude_field );
2399
                $exclude_field = $k;
2400
            }
2401
2402
            foreach ( $aliases as $alias ) {
2403
                if ( isset( $options[ $alias ] ) ) {
2404
                    $field[ $exclude_field ] = pods_trim( $options[ $alias ] );
2405
2406
                    unset( $options[ $alias ] );
2407
                }
2408
            }
2409
        }
2410
2411
        if ( strlen( $field[ 'label' ] ) < 1 )
2412
            $field[ 'label' ] = $field[ 'name' ];
2413
2414
        $field[ 'options' ][ 'type' ] = $field[ 'type' ];
2415
2416
        if ( in_array( $field[ 'options' ][ 'type' ], $tableless_field_types ) ) {
2417
            // Clean up special drop-down in field editor and save out pick_val
2418
            $field[ 'pick_object' ] = pods_var( 'pick_object', $field, '', null, true );
2419
2420
            if ( 0 === strpos( $field[ 'pick_object' ], 'pod-' ) ) {
2421
                $field[ 'pick_val' ] = pods_str_replace( 'pod-', '', $field[ 'pick_object' ], 1 );
2422
                $field[ 'pick_object' ] = 'pod';
2423
            }
2424 View Code Duplication
            elseif ( 0 === strpos( $field[ 'pick_object' ], 'post_type-' ) ) {
2425
                $field[ 'pick_val' ] = pods_str_replace( 'post_type-', '', $field[ 'pick_object' ], 1 );
2426
                $field[ 'pick_object' ] = 'post_type';
2427
            }
2428 View Code Duplication
            elseif ( 0 === strpos( $field[ 'pick_object' ], 'taxonomy-' ) ) {
2429
                $field[ 'pick_val' ] = pods_str_replace( 'taxonomy-', '', $field[ 'pick_object' ], 1 );
2430
                $field[ 'pick_object' ] = 'taxonomy';
2431
            }
2432
            elseif ( 'table' == $field[ 'pick_object' ] && 0 < strlen( pods_var_raw( 'pick_table', $field[ 'options' ] ) ) ) {
2433
                $field[ 'pick_val' ] = $field[ 'options' ][ 'pick_table' ];
2434
                $field[ 'pick_object' ] = 'table';
2435
            }
2436
            elseif ( false === strpos( $field[ 'pick_object' ], '-' ) && !in_array( $field[ 'pick_object' ], array( 'pod', 'post_type', 'taxonomy' ) ) ) {
2437
                $field[ 'pick_val' ] = '';
2438
			}
2439
			elseif ( 'custom-simple' == $field[ 'pick_object' ] ) {
2440
                $field[ 'pick_val' ] = '';
2441
			}
2442
2443
            $field[ 'options' ][ 'pick_object' ] = $field[ 'pick_object' ];
2444
            $field[ 'options' ][ 'pick_val' ] = $field[ 'pick_val' ];
2445
            $field[ 'options' ][ 'sister_id' ] = pods_var( 'sister_id', $field );
2446
2447
            unset( $field[ 'pick_object' ] );
2448
            unset( $field[ 'pick_val' ] );
2449
2450
            if ( isset( $field[ 'sister_id' ] ) )
2451
                unset( $field[ 'sister_id' ] );
2452
        }
2453
2454
        $field[ 'options' ] = array_merge( $field[ 'options' ], $options );
2455
2456
        $object_fields = (array) pods_var_raw( 'object_fields', $pod, array(), null, true );
2457
2458
        if ( 0 < $old_id && defined( 'PODS_FIELD_STRICT' ) && !PODS_FIELD_STRICT )
2459
            $params->id = $field[ 'id' ] = $old_id;
2460
2461
        // Add new field
2462
        if ( !isset( $params->id ) || empty( $params->id ) || empty( $field ) ) {
2463
            if ( $table_operation && in_array( $field[ 'name' ], array( 'created', 'modified' ) ) && !in_array( $field[ 'type' ], array( 'date', 'datetime' ) ) && ( !defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) )
2464
                return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2465
2466
            if ( $table_operation && 'author' == $field[ 'name' ] && 'pick' != $field[ 'type' ] && ( !defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) )
2467
                return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2468
2469 View Code Duplication
            if ( in_array( $field[ 'name' ], array( 'id', 'ID' ) ) )
2470
                return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2471
2472
            foreach ( $object_fields as $object_field => $object_field_opt ) {
2473
                if ( $object_field == $field[ 'name' ] || in_array( $field[ 'name' ], $object_field_opt[ 'alias' ] ) )
2474
                    return pods_error( sprintf( __( '%s is reserved for internal WordPress or Pods usage, please try a different name. Also consider what WordPress and Pods provide you built-in.', 'pods' ), $field[ 'name' ] ), $this );
2475
            }
2476
2477
            if ( in_array( $field[ 'name' ], array( 'rss' ) ) ) // Reserved post_name values that can't be used as field names
2478
                $field[ 'name' ] .= '2';
2479
2480
            if ( 'slug' == $field[ 'type' ] && true === $db ) {
2481 View Code Duplication
                if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy', 'user' ) ) )
2482
                    return pods_error( __( 'This pod already has an internal WordPress permalink field', 'pods' ), $this );
2483
2484
                $slug_field = get_posts( array(
2485
                    'post_type' => '_pods_field',
2486
                    'orderby' => 'menu_order',
2487
                    'order' => 'ASC',
2488
                    'posts_per_page' => 1,
2489
                    'post_parent' => $field[ 'pod_id' ],
2490
                    'meta_query' => array(
2491
                        array(
2492
                            'key' => 'type',
2493
                            'value' => 'slug'
2494
                        )
2495
                    )
2496
                ) );
2497
2498
                if ( !empty( $slug_field ) )
2499
                    return pods_error( __( 'This pod already has a permalink field', 'pods' ), $this );
2500
            }
2501
2502
            // Sink the new field to the bottom of the list
2503
            if ( null === $field[ 'weight' ] ) {
2504
                $field[ 'weight' ] = 0;
2505
2506
                $bottom_most_field = get_posts( array(
2507
                    'post_type' => '_pods_field',
2508
                    'orderby' => 'menu_order',
2509
                    'order' => 'DESC',
2510
                    'posts_per_page' => 1,
2511
                    'post_parent' => $field[ 'pod_id' ]
2512
                ) );
2513
2514
                if ( !empty( $bottom_most_field ) )
2515
                    $field[ 'weight' ] = pods_absint( $bottom_most_field[ 0 ]->menu_order ) + 1;
2516
            }
2517
2518
            $field[ 'weight' ] = pods_absint( $field[ 'weight' ] );
2519
2520
            $post_data = array(
2521
                'post_name' => $field[ 'name' ],
2522
                'post_title' => $field[ 'label' ],
2523
                'post_content' => $field[ 'description' ],
2524
                'post_type' => '_pods_field',
2525
                'post_parent' => $field[ 'pod_id' ],
2526
                'post_status' => 'publish',
2527
                'menu_order' => $field[ 'weight' ]
2528
            );
2529
        }
2530
        else {
2531
            if ( in_array( $field[ 'name' ], array( 'id', 'ID' ) ) ) {
2532 View Code Duplication
                if ( null !== $old_name )
2533
                    return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2534
                else
2535
                    return pods_error( sprintf( __( '%s is not editable', 'pods' ), $field[ 'name' ] ), $this );
2536
            }
2537
2538
            if ( null !== $old_name && $field[ 'name' ] != $old_name && in_array( $field[ 'name' ], array( 'created', 'modified' ) ) && !in_array( $field[ 'type' ], array( 'date', 'datetime' ) ) && ( !defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) )
2539
                return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2540
2541
            if ( null !== $old_name && $field[ 'name' ] != $old_name && 'author' == $field[ 'name' ] && 'pick' != $field[ 'type' ] && ( !defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) )
2542
                return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2543
2544
            foreach ( $object_fields as $object_field => $object_field_opt ) {
2545
                if ( $object_field != $field[ 'name' ] && !in_array( $field[ 'name' ], $object_field_opt[ 'alias' ] ) )
2546
                    continue;
2547
2548 View Code Duplication
                if ( null !== $old_name )
2549
                    return pods_error( sprintf( __( '%s is reserved for internal WordPress or Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2550
                else
2551
                    return pods_error( sprintf( __( '%s is not editable', 'pods' ), $field[ 'name' ] ), $this );
2552
            }
2553
2554
            $post_data = array(
2555
                'ID' => $field[ 'id' ],
2556
                'post_name' => $field[ 'name' ],
2557
                'post_title' => $field[ 'label' ],
2558
                'post_content' => $field[ 'description' ]
2559
            );
2560
2561
            if ( null !== $field[ 'weight' ] ) {
2562
                $field[ 'weight' ] = pods_absint( $field[ 'weight' ] );
2563
2564
                $post_data[ 'menu_order' ] = $field[ 'weight' ];
2565
            }
2566
        }
2567
2568 View Code Duplication
        if ( true === $db ) {
2569
            if ( !has_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ) ) )
2570
                add_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ), 100, 6 );
2571
2572
            $conflicted = false;
2573
2574
            // Headway compatibility fix
2575
            if ( has_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 ) ) {
2576
                remove_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
2577
2578
                $conflicted = true;
2579
            }
2580
2581
            $params->id = $this->save_wp_object( 'post', $post_data, $field[ 'options' ], true, true );
2582
2583
            if ( $conflicted )
2584
                add_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
2585
2586
            if ( false === $params->id )
2587
                return pods_error( __( 'Cannot save Field', 'pods' ), $this );
2588
        }
2589
        else
2590
            $params->id = $field[ 'name' ];
2591
2592
        $field[ 'id' ] = $params->id;
2593
2594
        $simple = ( 'pick' == $field[ 'type' ] && in_array( pods_var( 'pick_object', $field[ 'options' ] ), $simple_tableless_objects ) );
2595
2596
        $definition = false;
2597
2598
        if ( !in_array( $field[ 'type' ], $tableless_field_types ) || $simple ) {
2599
            $field_definition = $this->get_field_definition( $field[ 'type' ], array_merge( $field, $field[ 'options' ] ) );
2600
2601
            if ( 0 < strlen( $field_definition ) )
2602
                $definition = '`' . $field[ 'name' ] . '` ' . $field_definition;
2603
        }
2604
2605
        $sister_id = (int) pods_var( 'sister_id', $field[ 'options' ], 0 );
2606
2607
        if ( $table_operation && 'table' == $pod[ 'storage' ] && !pods_tableless() ) {
2608
            if ( !empty( $old_id ) ) {
2609
                if ( ( $field[ 'type' ] != $old_type || $old_simple != $simple ) && empty( $definition ) )
2610
                    pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` DROP COLUMN `{$old_name}`", false );
2611
                elseif ( 0 < strlen( $definition ) ) {
2612
                    if ( $old_name != $field[ 'name' ] || $old_simple != $simple ) {
2613
                        $test = false;
2614
2615 View Code Duplication
                        if ( 0 < strlen( $old_definition ) )
2616
                            $test = pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` CHANGE `{$old_name}` {$definition}", false );
2617
2618
                        // If the old field doesn't exist, continue to add a new field
2619
                        if ( false === $test )
2620
                            pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` ADD COLUMN {$definition}", __( 'Cannot create new field', 'pods' ) );
2621
                    }
2622
                    elseif ( null !== $old_definition && $definition != $old_definition ) {
2623
                        $test = pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` CHANGE `{$old_name}` {$definition}", false );
2624
2625
                        // If the old field doesn't exist, continue to add a new field
2626
                        if ( false === $test )
2627
                            pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` ADD COLUMN {$definition}", __( 'Cannot create new field', 'pods' ) );
2628
                    }
2629
                }
2630
            }
2631
            elseif ( 0 < strlen( $definition ) ) {
2632
                $test = false;
2633
2634 View Code Duplication
                if ( 0 < strlen( $old_definition ) )
2635
                    $test = pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` CHANGE `" . $field[ 'name' ] . "` {$definition}", false );
2636
2637
                // If the old field doesn't exist, continue to add a new field
2638
                if ( false === $test )
2639
                    pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` ADD COLUMN {$definition}", __( 'Cannot create new field', 'pods' ) );
2640
            }
2641
        }
2642
2643
        if ( !empty( $old_id ) && 'meta' == $pod[ 'storage' ] && $old_name != $field[ 'name' ] && $pod[ 'meta_table' ] != $pod[ 'table' ] ) {
2644
            $prepare = array(
2645
                $field[ 'name' ],
2646
                $old_name
2647
            );
2648
2649
            // Users don't have a type
2650
            if ( !empty( $pod[ 'field_type' ] ) )
2651
                $prepare[] = $pod[ 'name' ];
2652
2653
            pods_query( "
2654
                UPDATE `{$pod[ 'meta_table' ]}` AS `m`
2655
                LEFT JOIN `{$pod[ 'table' ]}` AS `t`
2656
                    ON `t`.`{$pod[ 'field_id' ]}` = `m`.`{$pod[ 'meta_field_id' ]}`
2657
                SET
2658
                    `m`.`{$pod[ 'meta_field_index' ]}` = %s
2659
                WHERE
2660
                    `m`.`{$pod[ 'meta_field_index' ]}` = %s
2661
            " . ( !empty( $pod[ 'field_type' ] ) ? " AND `t`.`{$pod[ 'field_type' ]}` = %s" : "" ),
2662
                $prepare
2663
            );
2664
        }
2665
2666
        if ( $field[ 'type' ] != $old_type && in_array( $old_type, $tableless_field_types ) ) {
2667
            delete_post_meta( $old_sister_id, 'sister_id' );
2668
2669
            if ( true === $db ) {
2670
                pods_query( "
2671
                        DELETE pm
2672
                        FROM {$wpdb->postmeta} AS pm
2673
                        LEFT JOIN {$wpdb->posts} AS p
2674
                            ON p.post_type = '_pods_field'
2675
                            AND p.ID = pm.post_id
2676
                        WHERE
2677
                            p.ID IS NOT NULL
2678
                            AND pm.meta_key = 'sister_id'
2679
                            AND pm.meta_value = %d
2680
                    ", array(
2681
                        $params->id
2682
                    )
2683
                );
2684
2685
                if ( !pods_tableless() ) {
2686
                    pods_query( "DELETE FROM @wp_podsrel WHERE `field_id` = {$params->id}", false );
2687
2688
                    pods_query( "
2689
                            UPDATE `@wp_podsrel`
2690
                            SET `related_field_id` = 0
2691
                            WHERE `field_id` = %d
2692
                        ", array(
2693
                            $old_sister_id
2694
                        )
2695
                    );
2696
                }
2697
            }
2698
        }
2699 View Code Duplication
        elseif ( 0 < $sister_id ) {
2700
            update_post_meta( $sister_id, 'sister_id', $params->id );
2701
2702
            if ( true === $db && ( !pods_tableless() ) ) {
2703
                pods_query( "
2704
                        UPDATE `@wp_podsrel`
2705
                        SET `related_field_id` = %d
2706
                        WHERE `field_id` = %d
2707
                    ",
2708
                    array(
2709
                        $params->id,
2710
                        $sister_id
2711
                    )
2712
                );
2713
            }
2714
        }
2715 View Code Duplication
        elseif ( 0 < $old_sister_id ) {
2716
            delete_post_meta( $old_sister_id, 'sister_id' );
2717
2718
            if ( true === $db && ( !pods_tableless() ) ) {
2719
                pods_query( "
2720
                        UPDATE `@wp_podsrel`
2721
                        SET `related_field_id` = 0
2722
                        WHERE `field_id` = %d
2723
                    ", array(
2724
                        $old_sister_id
2725
                    )
2726
                );
2727
            }
2728
        }
2729
2730
        if ( !empty( $old_id ) && $old_name != $field[ 'name' ] && true === $db ) {
2731
            pods_query( "
2732
                    UPDATE `@wp_postmeta`
2733
                    SET `meta_value` = %s
2734
                    WHERE
2735
                        `post_id` = %d
2736
                        AND `meta_key` = 'pod_index'
2737
                        AND `meta_value` = %s
2738
                ", array(
2739
                    $field[ 'name' ],
2740
                    $pod[ 'id' ],
2741
                    $old_name
2742
                )
2743
            );
2744
        }
2745
2746
        if ( !$save_pod )
2747
            $this->cache_flush_pods( $pod );
2748
        else {
2749
            pods_transient_clear( 'pods_field_' . $pod[ 'name' ] . '_' . $field[ 'name' ] );
2750
2751
            if ( !empty( $old_id ) && $old_name != $field[ 'name' ] )
2752
                pods_transient_clear( 'pods_field_' . $pod[ 'name' ] . '_' . $old_name );
2753
        }
2754
2755
        if ( true === $db )
2756
            return $params->id;
2757
        else
2758
            return $field;
2759
    }
2760
2761
    /**
2762
     * Fix Pod / Field post_name to ensure they are exactly as saved (allow multiple posts w/ same post_name)
2763
     *
2764
     * @param string $slug Unique slug value
2765
     * @param int $post_ID Post ID
2766
     * @param string $post_status Post Status
2767
     * @param string $post_type Post Type
2768
     * @param int $post_parent Post Parent ID
2769
     * @param string $original_slug Original slug value
2770
     *
2771
     * @return string Final slug value
2772
     *
2773
     * @since 2.3.3
2774
     */
2775
    public function save_slug_fix ( $slug, $post_ID, $post_status, $post_type, $post_parent = 0, $original_slug = null ) {
2776
        if ( in_array( $post_type, array( '_pods_field', '_pods_pod' ) ) && false !== strpos( $slug, '-' ) )
2777
            $slug = $original_slug;
2778
2779
        return $slug;
2780
    }
2781
2782
    /**
2783
     * Add or Edit a Pods Object
2784
     *
2785
     * $params['id'] int The Object ID
2786
     * $params['name'] string The Object name
2787
     * $params['type'] string The Object type
2788
     * $params['options'] Associative array of Object options
2789
     *
2790
     * @param array|object $params An associative array of parameters
2791
     * @param bool $sanitized (optional) Decides whether the params have been sanitized before being passed, will sanitize them if false.
2792
     *
2793
     * @return int The Object ID
2794
     * @since 2.0
2795
     */
2796
    public function save_object ( $params, $sanitized = false ) {
2797
        $params = (object) $params;
2798
2799
        if ( false === $sanitized )
2800
            $params = pods_sanitize( $params );
2801
2802 View Code Duplication
        if ( !isset( $params->name ) || empty( $params->name ) )
2803
            return pods_error( __( 'Name must be given to save an Object', 'pods' ), $this );
2804
2805 View Code Duplication
        if ( !isset( $params->type ) || empty( $params->type ) )
2806
            return pods_error( __( 'Type must be given to save an Object', 'pods' ), $this );
2807
2808
        $object = array(
2809
            'id' => 0,
2810
            'name' => $params->name,
2811
            'type' => $params->type,
2812
            'code' => '',
2813
            'options' => array()
2814
        );
2815
2816
        // Setup options
2817
        $options = get_object_vars( $params );
2818
2819
        if ( isset( $options[ 'method' ] ) )
2820
            unset( $options[ 'method' ] );
2821
2822
        $exclude = array(
2823
            'id',
2824
            'name',
2825
            'helper_type',
2826
            'code',
2827
            'options',
2828
            'status'
2829
        );
2830
2831 View Code Duplication
        foreach ( $exclude as $k => $exclude_field ) {
2832
            $aliases = array( $exclude_field );
2833
2834
            if ( is_array( $exclude_field ) ) {
2835
                $aliases = array_merge( array( $k ), $exclude_field );
2836
                $exclude_field = $k;
2837
            }
2838
2839
            foreach ( $aliases as $alias ) {
2840
                if ( isset( $options[ $alias ] ) ) {
2841
                    $object[ $exclude_field ] = pods_trim( $options[ $alias ] );
2842
2843
                    unset( $options[ $alias ] );
2844
                }
2845
            }
2846
        }
2847
2848
        if ( 'helper' == $object[ 'type' ] )
2849
            $object[ 'options' ][ 'helper_type' ] = $object[ 'helper_type' ];
2850
2851
        if ( isset( $object[ 'options' ][ 'code' ] ) )
2852
            unset( $object[ 'options' ][ 'code' ] );
2853
2854
        $object[ 'options' ] = array_merge( $object[ 'options' ], $options );
2855
2856
        $post_data = array(
2857
            'post_name' => pods_clean_name( $object[ 'name' ], true),
2858
            'post_title' => $object[ 'name' ],
2859
            'post_content' => $object[ 'code' ],
2860
            'post_type' => '_pods_' . $object[ 'type' ],
2861
            'post_status' => 'publish'
2862
        );
2863
2864
        if ( !empty( $object[ 'id' ] ) )
2865
            $post_data[ 'ID' ] = $object[ 'id' ];
2866
2867
        if ( null !== pods_var( 'status', $object, null, null, true ) )
2868
            $post_data[ 'post_status' ] = pods_var( 'status', $object, null, null, true );
2869
2870
        remove_filter( 'content_save_pre', 'balanceTags', 50 );
2871
2872
        $post_data = pods_sanitize( $post_data );
2873
2874
        $params->id = $this->save_post( $post_data, $object[ 'options' ], true, true );
2875
2876
        pods_transient_clear( 'pods_objects_' . $params->type );
2877
        pods_transient_clear( 'pods_objects_' . $params->type . '_get' );
2878
2879
        return $params->id;
2880
    }
2881
2882
    /**
2883
     * @see PodsAPI::save_object
2884
     *
2885
     * Add or edit a Pod Template
2886
     *
2887
     * $params['id'] int The template ID
2888
     * $params['name'] string The template name
2889
     * $params['code'] string The template code
2890
     *
2891
     * @param array|object $params An associative array of parameters
2892
     * @param bool $sanitized  (optional) Decides wether the params have been sanitized before being passed, will sanitize them if false.
2893
     *
2894
     * @return int The Template ID
2895
     *
2896
     * @since 1.7.9
2897
     */
2898
    public function save_template ( $params, $sanitized = false ) {
2899
        $params = (object) $params;
2900
2901
        $params->type = 'template';
2902
2903
        return $this->save_object( $params, $sanitized );
2904
    }
2905
2906
    /**
2907
     * @see PodsAPI::save_object
2908
     *
2909
     * Add or edit a Pod Page
2910
     *
2911
     * $params['id'] int The page ID
2912
     * $params['name'] string The page URI
2913
     * $params['code'] string The page code
2914
     *
2915
     * @param array|object $params An associative array of parameters
2916
     * @param bool $sanitized  (optional) Decides wether the params have been sanitized before being passed, will sanitize them if false.
2917
     *
2918
     * @return int The page ID
2919
     * @since 1.7.9
2920
     */
2921
    public function save_page ( $params, $sanitized = false ) {
2922
        $params = (object) $params;
2923
2924
        if ( !isset( $params->name ) ) {
2925
            $params->name = $params->uri;
2926
            unset( $params->uri );
2927
        }
2928
2929
        if ( isset( $params->phpcode ) ) {
2930
            $params->code = $params->phpcode;
2931
            unset( $params->phpcode );
2932
        }
2933
2934
        $params->name = trim( $params->name, '/' );
2935
        $params->type = 'page';
2936
2937
        return $this->save_object( $params, $sanitized );
2938
    }
2939
2940
    /**
2941
     * @see PodsAPI::save_object
2942
     *
2943
     * Add or edit a Pod Helper
2944
     *
2945
     * $params['id'] int The helper ID
2946
     * $params['name'] string The helper name
2947
     * $params['helper_type'] string The helper type ("pre_save", "display", etc)
2948
     * $params['code'] string The helper code
2949
     *
2950
     * @param array $params An associative array of parameters
2951
     * @param bool $sanitized  (optional) Decides wether the params have been sanitized before being passed, will sanitize them if false.
2952
     *
2953
     * @return int The helper ID
2954
     * @since 1.7.9
2955
     */
2956
    public function save_helper ( $params, $sanitized = false ) {
2957
        $params = (object) $params;
2958
2959
        if ( isset( $params->phpcode ) ) {
2960
            $params->code = $params->phpcode;
2961
            unset( $params->phpcode );
2962
        }
2963
2964
        if ( isset( $params->type ) ) {
2965
            $params->helper_type = $params->type;
2966
            unset( $params->type );
2967
        }
2968
2969
        $params->type = 'helper';
2970
2971
        return $this->save_object( $params, $sanitized );
2972
    }
2973
2974
    /**
2975
     * Add or edit a single pod item
2976
     *
2977
     * $params['pod'] string The Pod name (pod or pod_id is required)
2978
     * $params['pod_id'] string The Pod ID (pod or pod_id is required)
2979
     * $params['id'] int The item ID
2980
     * $params['data'] array (optional) Associative array of field names + values
2981
     * $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
2982
	 * $params['track_changed_fields'] bool Set to true to enable tracking of saved fields via PodsAPI::get_changed_fields()
2983
     *
2984
     * @param array|object $params An associative array of parameters
2985
     *
2986
     * @return int The item ID
2987
     *
2988
     * @since 1.7.9
2989
     */
2990
    public function save_pod_item ( $params ) {
2991
2992
		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...
2993
2994
        $params = (object) pods_str_replace( '@wp_', '{prefix}', $params );
2995
2996
        $tableless_field_types = PodsForm::tableless_field_types();
2997
        $repeatable_field_types = PodsForm::repeatable_field_types();
2998
        $simple_tableless_objects = PodsForm::simple_tableless_objects();
2999
3000
        // @deprecated 2.0
3001
        if ( isset( $params->datatype ) ) {
3002
            pods_deprecated( '$params->pod instead of $params->datatype', '2.0' );
3003
3004
            $params->pod = $params->datatype;
3005
3006
            unset( $params->datatype );
3007
3008
            if ( isset( $params->pod_id ) ) {
3009
                pods_deprecated( '$params->id instead of $params->pod_id', '2.0' );
3010
3011
                $params->id = $params->pod_id;
3012
3013
                unset( $params->pod_id );
3014
            }
3015
3016
            if ( isset( $params->data ) && !empty( $params->data ) && is_array( $params->data ) ) {
3017
                $check = current( $params->data );
3018
3019
                if ( is_array( $check ) ) {
3020
                    pods_deprecated( 'PodsAPI::save_pod_items', '2.0' );
3021
3022
                    return $this->save_pod_items( $params, $params->data );
3023
                }
3024
            }
3025
        }
3026
3027
        // @deprecated 2.0
3028
        if ( isset( $params->tbl_row_id ) ) {
3029
            pods_deprecated( '$params->id instead of $params->tbl_row_id', '2.0' );
3030
3031
            $params->id = $params->tbl_row_id;
3032
3033
            unset( $params->tbl_row_id );
3034
        }
3035
3036
        // @deprecated 2.0
3037
        if ( isset( $params->columns ) ) {
3038
            pods_deprecated( '$params->data instead of $params->columns', '2.0' );
3039
3040
            $params->data = $params->columns;
3041
3042
            unset( $params->columns );
3043
        }
3044
3045
        if ( !isset( $params->pod ) )
3046
            $params->pod = false;
3047 View Code Duplication
        if ( isset( $params->pod_id ) )
3048
            $params->pod_id = pods_absint( $params->pod_id );
3049
        else
3050
            $params->pod_id = 0;
3051
3052 View Code Duplication
        if ( isset( $params->id ) )
3053
            $params->id = pods_absint( $params->id );
3054
        else
3055
            $params->id = 0;
3056
3057
        if ( !isset( $params->from ) )
3058
            $params->from = 'save';
3059
3060
        if ( !isset( $params->location ) )
3061
            $params->location = null;
3062
3063
        if ( !isset( $params->track_changed_fields ) )
3064
            $params->track_changed_fields = false;
3065
3066
		/**
3067
		 * Override $params['track_changed_fields']
3068
		 *
3069
		 * Use for globally setting field change tracking.
3070
		 *
3071
		 * @param bool
3072
		 *
3073
		 * @since 2.3.19
3074
		 */
3075
		$track_changed_fields = apply_filters( 'pods_api_save_pod_item_track_changed_fields_' . $params->pod, (boolean) $params->track_changed_fields, $params );
3076
		$changed_fields = array();
3077
3078
		if ( !isset( $params->clear_slug_cache ) ) {
3079
			$params->clear_slug_cache = true;
3080
		}
3081
3082
        // Support for bulk edit
3083
        if ( isset( $params->id ) && !empty( $params->id ) && is_array( $params->id ) ) {
3084
            $ids = array();
3085
            $new_params = $params;
3086
3087
            foreach ( $params->id as $id ) {
3088
                $new_params->id = $id;
3089
3090
                $ids[] = $this->save_pod_item( $new_params );
3091
            }
3092
3093
            return $ids;
3094
        }
3095
3096
        // Allow Helpers to know what's going on, are we adding or saving?
3097
        $is_new_item = false;
3098
3099
        if ( empty( $params->id ) )
3100
            $is_new_item = true;
3101
3102
        if ( isset( $params->is_new_item ) )
3103
            $is_new_item = (boolean) $params->is_new_item;
3104
3105
        // Allow Helpers to bypass subsequent helpers in recursive save_pod_item calls
3106
        $bypass_helpers = false;
3107
3108
        if ( isset( $params->bypass_helpers ) && false !== $params->bypass_helpers )
3109
            $bypass_helpers = true;
3110
3111
        // Allow Custom Fields not defined by Pods to be saved
3112
        $allow_custom_fields = false;
3113
3114
        if ( isset( $params->allow_custom_fields ) && false !== $params->allow_custom_fields )
3115
            $allow_custom_fields = true;
3116
3117
        // Get array of Pods
3118
        $pod = $this->load_pod( array( 'id' => $params->pod_id, 'name' => $params->pod, 'table_info' => true ) );
3119
3120
        if ( false === $pod )
3121
            return pods_error( __( 'Pod not found', 'pods' ), $this );
3122
3123
        $params->pod = $pod[ 'name' ];
3124
        $params->pod_id = $pod[ 'id' ];
3125
3126
        if ( 'settings' == $pod[ 'type' ] )
3127
            $params->id = $pod[ 'id' ];
3128
3129
        $fields = $pod[ 'fields' ];
3130
3131
        $object_fields = (array) pods_var_raw( 'object_fields', $pod, array(), null, true );
3132
3133
        $fields_active = array();
3134
        $custom_data = array();
3135
3136
        // Find the active fields (loop through $params->data to retain order)
3137
        if ( !empty( $params->data ) && is_array( $params->data ) ) {
3138
            $custom_fields = array();
3139
3140
            foreach ( $params->data as $field => $value ) {
3141
                if ( isset( $object_fields[ $field ] ) ) {
3142
                    $object_fields[ $field ][ 'value' ] = $value;
3143
                    $fields_active[] = $field;
3144
                }
3145
                elseif ( isset( $fields[ $field ] ) ) {
3146
                    if ( 'save' == $params->from || true === PodsForm::permission( $fields[ $field ][ 'type' ], $field, $fields[ $field ], $fields, $pod, $params->id, $params ) ) {
3147
                        $fields[ $field ][ 'value' ] = $value;
3148
                        $fields_active[] = $field;
3149
                    }
3150
                    elseif ( !pods_has_permissions( $fields[ $field ][ 'options' ] ) && pods_var( 'hidden', $fields[ $field ][ 'options' ], false ) ) {
3151
                        $fields[ $field ][ 'value' ] = $value;
3152
                        $fields_active[] = $field;
3153
                    }
3154
                }
3155
                else {
3156
                    $found = false;
3157
3158
                    foreach ( $object_fields as $object_field => $object_field_opt ) {
3159
                        if ( in_array( $field, $object_field_opt[ 'alias' ] ) ) {
3160
                            $object_fields[ $object_field ][ 'value' ] = $value;
3161
                            $fields_active[] = $object_field;
3162
3163
                            $found = true;
3164
3165
                            break;
3166
                        }
3167
                    }
3168
3169
                    if ( $allow_custom_fields && !$found )
3170
                        $custom_fields[] = $field;
3171
                }
3172
            }
3173
3174
            if ( $allow_custom_fields && !empty( $custom_fields ) ) {
3175
                foreach ( $custom_fields as $field ) {
3176
                    $custom_data[ $field ] = $params->data[ $field ];
3177
                }
3178
            }
3179
3180
           unset( $params->data );
3181
        }
3182
3183 View Code Duplication
		if ( empty( $params->id ) && !in_array( 'created', $fields_active ) && isset( $fields[ 'created' ] ) && in_array( $fields[ 'created' ][ 'type' ], array( 'date', 'datetime' ) ) ) {
3184
			$fields[ 'created' ][ 'value' ] = current_time( 'mysql' );
3185
			$fields_active[] = 'created';
3186
		}
3187
3188 View Code Duplication
		if ( !in_array( 'modified', $fields_active ) && isset( $fields[ 'modified' ] ) && in_array( $fields[ 'modified' ][ 'type' ], array( 'date', 'datetime' ) ) ) {
3189
			$fields[ 'modified' ][ 'value' ] = current_time( 'mysql' );
3190
			$fields_active[] = 'modified';
3191
		}
3192
3193
        if ( in_array( $pod[ 'type' ], array( 'pod', 'table' ) ) && empty( $params->id ) && !empty( $pod[ 'pod_field_index' ] ) && in_array( $pod[ 'pod_field_index' ], $fields_active ) && !in_array( $pod[ 'pod_field_slug' ], $fields_active ) && isset( $fields[ $pod[ 'pod_field_slug' ] ] ) ) {
3194
			$fields[ $pod[ 'pod_field_slug' ] ][ 'value' ] = ''; // this will get picked up by slug pre_save method
3195
			$fields_active[] = $pod[ 'pod_field_slug' ];
3196
        }
3197
3198
        // Handle hidden fields
3199
        if ( empty( $params->id ) ) {
3200
            foreach ( $fields as $field => $field_data ) {
3201
                if ( in_array( $field, $fields_active ) )
3202
                    continue;
3203
3204
                if ( in_array( $params->from, array( 'save', 'process_form' ) ) || true === PodsForm::permission( $fields[ $field ][ 'type' ], $field, $fields[ $field ], $fields, $pod, $params->id, $params ) ) {
3205
                    $value = PodsForm::default_value( pods_var_raw( $field, 'post' ), $field_data[ 'type' ], $field, pods_var_raw( 'options', $field_data, $field_data, null, true ), $pod, $params->id );
3206
3207
                    if ( null !== $value && '' !== $value && false !== $value ) {
3208
                        $fields[ $field ][ 'value' ] = $value;
3209
                        $fields_active[] = $field;
3210
                    }
3211
                }
3212
            }
3213
3214
			// Set default field values for object fields
3215
			if ( !empty( $object_fields ) ) {
3216 View Code Duplication
				foreach ( $object_fields as $field => $field_data ) {
3217
					if ( in_array( $field, $fields_active ) ) {
3218
						continue;
3219
					}
3220
					elseif ( !isset( $field_data[ 'default' ] ) || strlen( $field_data[ 'default' ] ) < 1 ) {
3221
						continue;
3222
					}
3223
3224
                    $value = PodsForm::default_value( pods_var_raw( $field, 'post' ), $field_data[ 'type' ], $field, pods_var_raw( 'options', $field_data, $field_data, null, true ), $pod, $params->id );
3225
3226
                    if ( null !== $value && '' !== $value && false !== $value ) {
3227
                        $object_fields[ $field ][ 'value' ] = $value;
3228
                        $fields_active[] = $field;
3229
                    }
3230
				}
3231
			}
3232
3233
			// Set default field values for Pod fields
3234 View Code Duplication
			foreach ( $fields as $field => $field_data ) {
3235
				if ( in_array( $field, $fields_active ) ) {
3236
					continue;
3237
				}
3238
				elseif ( !isset( $field_data[ 'default' ] ) || strlen( $field_data[ 'default' ] ) < 1 ) {
3239
					continue;
3240
				}
3241
3242
				$value = PodsForm::default_value( pods_var_raw( $field, 'post' ), $field_data[ 'type' ], $field, pods_var_raw( 'options', $field_data, $field_data, null, true ), $pod, $params->id );
3243
3244
				if ( null !== $value && '' !== $value && false !== $value ) {
3245
					$fields[ $field ][ 'value' ] = $value;
3246
					$fields_active[] = $field;
3247
				}
3248
			}
3249
        }
3250
3251
        $columns =& $fields; // @deprecated 2.0
3252
        $active_columns =& $fields_active; // @deprecated 2.0
3253
        $params->tbl_row_id =& $params->id; // @deprecated 2.0
3254
3255
        $pre_save_helpers = $post_save_helpers = array();
3256
3257
		$pieces = array(
3258
			'fields',
3259
			'params',
3260
			'pod',
3261
			'fields_active',
3262
			'object_fields',
3263
			'custom_fields',
3264
			'custom_data',
3265
			'track_changed_fields',
3266
			'changed_fields'
3267
		);
3268
3269
        if ( false === $bypass_helpers ) {
3270
            // Plugin hooks
3271
            $hooked = $this->do_hook( 'pre_save_pod_item', compact( $pieces ), $is_new_item, $params->id );
3272
3273
            if ( is_array( $hooked ) && !empty( $hooked ) )
3274
                extract( $hooked );
3275
3276
            $hooked = $this->do_hook( "pre_save_pod_item_{$params->pod}", compact( $pieces ), $is_new_item, $params->id );
3277
3278
            if ( is_array( $hooked ) && !empty( $hooked ) )
3279
                extract( $hooked );
3280
3281
            if ( $is_new_item ) {
3282
                $hooked = $this->do_hook( 'pre_create_pod_item', compact( $pieces ) );
3283
3284
                if ( is_array( $hooked ) && !empty( $hooked ) )
3285
                    extract( $hooked );
3286
3287
                $hooked = $this->do_hook( "pre_create_pod_item_{$params->pod}", compact( $pieces ) );
3288
3289
                if ( is_array( $hooked ) && !empty( $hooked ) )
3290
                    extract( $hooked );
3291
            }
3292
            else {
3293
                $hooked = $this->do_hook( 'pre_edit_pod_item', compact( $pieces ), $params->id );
3294
3295
                if ( is_array( $hooked ) && !empty( $hooked ) )
3296
                    extract( $hooked );
3297
3298
                $hooked = $this->do_hook( "pre_edit_pod_item_{$params->pod}", compact( $pieces ), $params->id );
3299
3300
                if ( is_array( $hooked ) && !empty( $hooked ) )
3301
                    extract( $hooked );
3302
            }
3303
3304
            // Call any pre-save helpers (if not bypassed)
3305 View Code Duplication
            if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) {
3306
                if ( !empty( $pod[ 'options' ] ) && is_array( $pod[ 'options' ] ) ) {
3307
                    $helpers = array( 'pre_save_helpers', 'post_save_helpers' );
3308
3309
                    foreach ( $helpers as $helper ) {
3310
                        if ( isset( $pod[ 'options' ][ $helper ] ) && !empty( $pod[ 'options' ][ $helper ] ) )
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
3311
                            ${$helper} = explode( ',', $pod[ 'options' ][ $helper ] );
3312
                    }
3313
                }
3314
3315
                if ( !empty( $pre_save_helpers ) ) {
3316
                    pods_deprecated( sprintf( __( 'Pre-save helpers are deprecated, use the action pods_pre_save_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
3317
3318
                    foreach ( $pre_save_helpers as $helper ) {
3319
                        $helper = $this->load_helper( array( 'name' => $helper ) );
3320
3321
                        if ( false !== $helper )
3322
                            eval( '?>' . $helper[ 'code' ] );
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
3323
                    }
3324
                }
3325
            }
3326
        }
3327
3328
		if ( $track_changed_fields ) {
3329
			$changed_fields = $this->get_changed_fields( compact( $pieces ) );
3330
		}
3331
3332
        $table_data = $table_formats = $update_values = $rel_fields = $rel_field_ids = array();
3333
3334
        $object_type = $pod[ 'type' ];
3335
3336
        $object_ID = 'ID';
3337
3338
        if ( 'comment' == $object_type )
3339
            $object_ID = 'comment_ID';
3340
        elseif ( 'taxonomy' == $object_type )
3341
            $object_ID = 'term_id';
3342
3343
        $object_data = $object_meta = $post_term_data = array();
3344
3345
        if ( 'settings' == $object_type )
3346
            $object_data[ 'option_id' ] = $pod[ 'name' ];
3347
        elseif ( !empty( $params->id ) )
3348
            $object_data[ $object_ID ] = $params->id;
3349
3350
        $fields_active = array_unique( $fields_active );
3351
3352
        // Loop through each active field, validating and preparing the table data
3353
        foreach ( $fields_active as $field ) {
3354 View Code Duplication
            if ( isset( $object_fields[ $field ] ) )
3355
                $field_data = $object_fields[ $field ];
3356
            elseif ( isset( $fields[ $field ] ) )
3357
                $field_data = $fields[ $field ];
3358
            else
3359
                continue;
3360
3361
            $value = $field_data[ 'value' ];
3362
            $type = $field_data[ 'type' ];
3363
            $options = pods_var( 'options', $field_data, array() );
3364
3365
            // WPML AJAX compatibility
3366
            if ( is_admin() && isset( $_GET[ 'page' ] ) && false !== strpos( $_GET[ 'page' ], '/menu/languages.php' ) && isset( $_POST[ 'icl_ajx_action' ] ) && isset( $_POST[ '_icl_nonce' ] ) && wp_verify_nonce( $_POST[ '_icl_nonce' ], $_POST[ 'icl_ajx_action' ] . '_nonce' ) )
3367
                $options[ 'unique' ] = $fields[ $field ][ 'options' ][ 'unique' ] = $options[ 'required' ] = $fields[ $field ][ 'options' ][ 'required' ] = 0;
3368
            else {
3369
                // Validate value
3370
                $validate = $this->handle_field_validation( $value, $field, $object_fields, $fields, $pod, $params );
3371
3372
                if ( false === $validate )
3373
                    $validate = sprintf( __( 'There was an issue validating the field %s', 'pods' ), $field_data[ 'label' ] );
3374
                elseif ( true !== $validate )
3375
                    $validate = (array) $validate;
3376
3377
                if ( !is_bool( $validate ) && !empty( $validate ) )
3378
                    return pods_error( $validate, $this );
3379
            }
3380
3381
            $value = PodsForm::pre_save( $field_data[ 'type' ], $value, $params->id, $field, array_merge( $field_data, $options ), array_merge( $fields, $object_fields ), $pod, $params );
3382
3383
            $field_data[ 'value' ] = $value;
3384
3385
            if ( isset( $object_fields[ $field ] ) ) {
3386
				if ( 'taxonomy' == $object_fields[ $field ][ 'type' ] ) {
3387
					$post_term_data[ $field ] = $value;
3388
				}
3389
				else {
3390
                	$object_data[ $field ] = $value;
3391
				}
3392
			}
3393
            else {
3394
                $simple = ( 'pick' == $type && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
3395
                $simple = (boolean) $this->do_hook( 'tableless_custom', $simple, $field_data, $field, $fields, $pod, $params );
3396
3397
                // Handle Simple Relationships
3398
                if ( $simple ) {
3399
                    if ( !is_array( $value ) )
3400
                        $value = explode( ',', $value );
3401
3402
                    $pick_limit = (int) pods_var_raw( 'pick_limit', $options, 0 );
3403
3404
                    if ( 'single' == pods_var_raw( 'pick_format_type', $options ) )
3405
                        $pick_limit = 1;
3406
3407
                    if ( 'custom-simple' == pods_var( 'pick_object', $field_data ) ) {
3408
                        $custom = pods_var_raw( 'pick_custom', $options, '' );
3409
3410
                        $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $field_data[ 'name' ], $value, array_merge( $field_data, $options ), $pod, $params->id );
3411
3412
                        if ( empty( $value ) || empty( $custom ) )
3413
                            $value = '';
3414
                        elseif ( !empty( $custom ) ) {
3415
                            if ( !is_array( $custom ) ) {
3416
                                $custom = explode( "\n", $custom );
3417
3418
                                $custom_values = array();
3419
3420
                                foreach ( $custom as $c => $cv ) {
3421
                                    if ( 0 < strlen( $cv ) ) {
3422
                                        $custom_label = explode( '|', $cv );
3423
3424
                                        if ( !isset( $custom_label[ 1 ] ) )
3425
                                            $custom_label[ 1 ] = $custom_label[ 0 ];
3426
3427
                                        $custom_label[ 0 ] = trim( (string) $custom_label[ 0 ] );
3428
                                        $custom_label[ 1 ] = trim( (string) $custom_label[ 1 ] );
3429
                                        $custom_values[ $custom_label[ 0 ] ] = $custom_label[ 1 ];
3430
                                    }
3431
                                }
3432
                            }
3433
                            else
3434
                                $custom_values = $custom;
3435
3436
                            $values = array();
3437
3438
                            foreach ( $value as $k => $v ) {
3439
                                $v = pods_unsanitize( $v );
3440
3441
                                if ( isset( $custom_values[ $v ] ) )
3442
                                    $values[ $k ] = $v;
3443
                            }
3444
3445
                            $value = $values;
3446
                        }
3447
                    }
3448
3449
                    if ( 0 < $pick_limit && !empty( $value ) )
3450
                        $value = array_slice( $value, 0, $pick_limit );
3451
3452
                    // Don't save an empty array, just make it an empty string
3453 View Code Duplication
                    if ( empty( $value ) )
3454
                        $value = '';
3455
                    elseif ( is_array( $value ) ) {
3456
                        // If there's just one item, don't save as an array, save the string
3457
                        if ( 1 == $pick_limit || 1 == count( $value ) )
3458
                            $value = implode( '', $value );
3459
                        // If storage is set to table, json encode, otherwise WP will serialize automatically
3460
                        elseif ( 'table' == pods_var( 'storage', $pod ) )
3461
                            $value = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $value, JSON_UNESCAPED_UNICODE ) : json_encode( $value );
0 ignored issues
show
Unused Code introduced by
The call to json_encode() has too many arguments starting with JSON_UNESCAPED_UNICODE.

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

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

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

Loading history...
3462
                    }
3463
                }
3464
3465
                // Prepare all table / meta data
3466
                if ( !in_array( $type, $tableless_field_types ) || $simple ) {
3467
                    if ( in_array( $type, $repeatable_field_types ) && 1 == pods_var( $type . '_repeatable', $field_data, 0 ) ) {
3468
                        // Don't save an empty array, just make it an empty string
3469 View Code Duplication
                        if ( empty( $value ) )
3470
                            $value = '';
3471
                        elseif ( is_array( $value ) ) {
3472
                            // If there's just one item, don't save as an array, save the string
3473
                            if ( 1 == count( $value ) )
3474
                                $value = implode( '', $value );
3475
                            // If storage is set to table, json encode, otherwise WP will serialize automatically
3476
                            elseif ( 'table' == pods_var( 'storage', $pod ) )
3477
                                $value = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $value, JSON_UNESCAPED_UNICODE ) : json_encode( $value );
0 ignored issues
show
Unused Code introduced by
The call to json_encode() has too many arguments starting with JSON_UNESCAPED_UNICODE.

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

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

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

Loading history...
3478
                        }
3479
                    }
3480
3481
                    $table_data[ $field ] = str_replace( array( '{prefix}', '@wp_' ), array( '{/prefix/}', '{prefix}' ), $value ); // Fix for pods_query
3482
                    $table_formats[] = PodsForm::prepare( $type, $options );
3483
3484
                    $object_meta[ $field ] = $value;
3485
                }
3486
                // Store relational field data to be looped through later
3487
                else {
3488
                    // Convert values from a comma-separated string into an array
3489
                    if ( !is_array( $value ) )
3490
                        $value = explode( ',', $value );
3491
3492
                    $rel_fields[ $type ][ $field ] = $value;
3493
                    $rel_field_ids[] = $field_data[ 'id' ];
3494
                }
3495
            }
3496
        }
3497
3498
        if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) ) {
3499
            $object_name = $pod[ 'name' ];
3500
3501
            if ( !empty( $pod[ 'object' ] ) )
3502
                $object_name = $pod[ 'object' ];
3503
3504
            $object_name_field = 'post_type';
3505
3506
            if ( 'taxonomy' == $pod['type'] ) {
3507
                $object_name_field = 'taxonomy';
3508
            }
3509
3510
            $object_data[ $object_name_field ] = $object_name;
3511
        }
3512
3513
        if ( ( 'meta' == $pod[ 'storage' ] || 'settings' == $pod[ 'type' ] || ( 'taxonomy' == $pod[ 'type' ] && 'none' == $pod[ 'storage' ] ) ) && !in_array( $pod[ 'type' ], array( 'pod', 'table', '' ) ) ) {
3514
            if ( $allow_custom_fields && !empty( $custom_data ) )
3515
                $object_meta = array_merge( $custom_data, $object_meta );
3516
3517
			$fields_to_send = array_flip( array_keys( $object_meta ) );
3518
3519
			foreach ( $fields_to_send as $field => $field_data ) {
3520 View Code Duplication
				if ( isset( $object_fields[ $field ] ) ) {
3521
					$field_data = $object_fields[ $field ];
3522
				}
3523
				elseif ( isset( $fields[ $field ] ) ) {
3524
					$field_data = $fields[ $field ];
3525
				}
3526
				else {
3527
					unset( $fields_to_send[ $field ] );
3528
				}
3529
3530
				$fields_to_send[ $field ] = $field_data;
3531
			}
3532
3533
            $params->id = $this->save_wp_object( $object_type, $object_data, $object_meta, false, true, $fields_to_send );
3534
3535
            if ( !empty( $params->id ) && 'settings' == $object_type )
3536
                $params->id = $pod[ 'id' ];
3537
        }
3538
        else {
3539
            if ( ! in_array( $pod[ 'type' ], array( 'pod', 'table', '' ) ) ) {
3540
                $params->id = $this->save_wp_object( $object_type, $object_data, array(), false, true );
3541
            }
3542
3543
            if ( 'table' == $pod[ 'storage' ] ) {
3544
                // Every row should have an id set here, otherwise Pods with nothing
3545
                // but relationship fields won't get properly ID'd
3546
                if ( empty( $params->id ) )
3547
                    $params->id = 0;
3548
3549
                $table_data = array( 'id' => $params->id ) + $table_data;
3550
                array_unshift( $table_formats, '%d' );
3551
3552
                if ( !empty( $table_data ) ) {
3553
                    $sql = pods_data()->insert_on_duplicate( "@wp_pods_{$params->pod}", $table_data, $table_formats );
3554
3555
                    $id = pods_query( $sql, 'Cannot add/save table row' );
3556
3557
                    if ( empty( $params->id ) )
3558
                        $params->id = $id;
3559
                }
3560
            }
3561
        }
3562
3563
        $params->id = (int) $params->id;
3564
3565
		// Save terms for taxonomies associated to a post type
3566
        if ( 0 < $params->id && 'post_type' == $pod[ 'type' ] && !empty( $post_term_data ) ) {
3567
			foreach ( $post_term_data as $post_taxonomy => $post_terms ) {
3568
                $post_terms = (array) $post_terms;
3569
3570
                foreach ( $post_terms as $k => $v ) {
3571
                    if ( ! preg_match( '/[^0-9]/', $v ) ) {
3572
                        $v = (int) $v;
3573
                    }
3574
3575
                    $post_terms[ $k ] = $v;
3576
                }
3577
3578
				wp_set_object_terms( $params->id, $post_terms, $post_taxonomy );
3579
			}
3580
		}
3581
3582
        $no_conflict = pods_no_conflict_check( $pod[ 'type' ] );
3583
3584
        if ( !$no_conflict )
3585
            pods_no_conflict_on( $pod[ 'type' ] );
3586
3587
        // Save relationship / file data
3588
        if ( !empty( $rel_fields ) ) {
3589
            foreach ( $rel_fields as $type => $data ) {
3590
                // Only handle tableless fields
3591
                if ( !in_array( $type, $tableless_field_types ) ) {
3592
                    continue;
3593
				}
3594
3595
                foreach ( $data as $field => $values ) {
3596
                    $pick_val = pods_var( 'pick_val', $fields[ $field ] );
3597
3598
                    if ( 'table' == pods_var( 'pick_object', $fields[ $field ] ) ) {
3599
                        $pick_val = pods_var( 'pick_table', $fields[ $field ][ 'options' ], $pick_val, null, true );
3600
					}
3601
3602 View Code Duplication
                    if ( '__current__' == $pick_val ) {
3603
                        if ( is_object( $pod ) ) {
3604
                            $pick_val = $pod->pod;
3605
						}
3606
                        elseif ( is_array( $pod ) ) {
3607
                            $pick_val = $pod[ 'name' ];
3608
						}
3609
                        elseif ( 0 < strlen( $pod ) ) {
3610
                            $pick_val = $pod;
3611
						}
3612
                    }
3613
3614
                    $fields[ $field ][ 'options' ][ 'table_info' ] = pods_api()->get_table_info( pods_var( 'pick_object', $fields[ $field ] ), $pick_val, null, null, $fields[ $field ][ 'options' ] );
3615
3616
                    if ( isset( $fields[ $field ][ 'options' ][ 'table_info' ][ 'pod' ] ) && !empty( $fields[ $field ][ 'options' ][ 'table_info' ][ 'pod' ] ) && isset( $fields[ $field ][ 'options' ][ 'table_info' ][ 'pod' ][ 'name' ] ) ) {
3617
						$search_data = pods( $fields[ $field ][ 'options' ][ 'table_info' ][ 'pod' ][ 'name' ] );
3618
3619
						$data_mode = 'pods';
3620
                    }
3621
					else {
3622
						$search_data = pods_data();
3623
						$search_data->table( $fields[ $field ][ 'options' ][ 'table_info' ] );
3624
3625
						$data_mode = 'data';
3626
					}
3627
3628
					$find_rel_params = array(
3629
						'select' => "`t`.`{$search_data->field_id}`",
3630
						'where' => "`t`.`{$search_data->field_slug}` = %s OR `t`.`{$search_data->field_index}` = %s",
3631
						'limit' => 1,
3632
						'pagination' => false,
3633
						'search' => false
3634
					);
3635
3636
					if ( empty( $search_data->field_slug ) && !empty( $search_data->field_index ) ) {
3637
						$find_rel_params[ 'where' ] = "`t`.`{$search_data->field_index}` = %s";
3638
					}
3639
					elseif ( empty( $search_data->field_slug ) && empty( $search_data->field_index ) ) {
3640
						$find_rel_params = false;
3641
					}
3642
3643
                    $related_limit = (int) pods_var_raw( $type . '_limit', $fields[ $field ][ 'options' ], 0 );
3644
3645 View Code Duplication
                    if ( 'single' == pods_var_raw( $type . '_format_type', $fields[ $field ][ 'options' ] ) ) {
3646
                        $related_limit = 1;
3647
					}
3648
3649
                    // Enforce integers / unique values for IDs
3650
                    $value_ids = array();
3651
3652
					$is_file_field = in_array( $type, PodsForm::file_field_types() );
3653
					$is_taggable = ( in_array( $type, PodsForm::tableless_field_types() ) && 1 == pods_v( $type . '_taggable', $fields[ $field ][ 'options' ] ) );
3654
3655
					// @todo Handle simple relationships eventually
3656
                    foreach ( $values as $v ) {
3657
                        if ( !empty( $v ) ) {
3658
							if ( !is_array( $v ) ) {
3659
								if ( !preg_match( '/[^0-9]/', $v ) ) {
3660
									$v = (int) $v;
3661
								}
3662
								// File handling
3663
								elseif ( $is_file_field ) {
3664
									// Get ID from GUID
3665
									$v = pods_image_id_from_field( $v );
3666
3667
									// If file not found, add it
3668
									if ( empty( $v ) ) {
3669
										$v = pods_attachment_import( $v );
3670
									}
3671
								}
3672
								// Reference by slug
3673
								else {
3674
									$v_data = false;
3675
3676
									if ( false !== $find_rel_params ) {
3677
										$rel_params = $find_rel_params;
3678
										$rel_params[ 'where' ] = $wpdb->prepare( $rel_params[ 'where' ], array( $v, $v ) );
3679
3680
										$search_data->select( $rel_params );
0 ignored issues
show
Bug introduced by
The method select does only exist in PodsData, but not in Pods.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
3681
3682
										$v_data = $search_data->fetch( $v );
3683
									}
3684
3685
									if ( !empty( $v_data ) && isset( $v_data[ $search_data->field_id ] ) ) {
3686
										$v = (int) $v_data[ $search_data->field_id ];
3687
									}
3688
									// Allow tagging for Pods objects
3689
									elseif ( $is_taggable && 'pods' == $data_mode ) {
3690
										$tag_data = array(
3691
											$search_data->field_index => $v
3692
										);
3693
3694
										if ( 'post_type' == $search_data->pod_data[ 'type' ] ) {
3695
											$tag_data[ 'post_status' ] = 'publish';
3696
										}
3697
3698
										/**
3699
										 * Filter for changing tag before adding new item.
3700
										 *
3701
										 * @param array $tag_data Fields for creating new item.
3702
										 * @param int $v Field ID of tag.
3703
										 * @param obj $search_data Search object for tag.
3704
										 * @param string $field Table info for field.
3705
										 * @param array	$pieces Field array.
3706
										 *
3707
										 * @since 2.3.19
3708
										 */
3709
										$tag_data = apply_filters( 'pods_api_save_pod_item_taggable_data', $tag_data, $v, $search_data, $field, compact( $pieces ) );
3710
3711
										// Save $v to a new item on related object
3712
										$v = $search_data->add( $tag_data );
0 ignored issues
show
Bug introduced by
The method add does only exist in Pods, but not in PodsData.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
3713
3714
										// @todo Support non-Pods for tagging
3715
									}
3716
								}
3717
							}
3718
							elseif ( $is_file_field && isset( $v[ 'id' ] ) ) {
3719
								$v = (int) $v[ 'id' ];
3720
							}
3721
							else {
3722
								continue;
3723
							}
3724
3725
							if ( !empty( $v ) && !in_array( $v, $value_ids ) ) {
3726
								$value_ids[] = $v;
3727
							}
3728
                        }
3729
                    }
3730
3731
                    $value_ids = array_unique( array_filter( $value_ids ) );
3732
3733
                    // Limit values
3734
                    if ( 0 < $related_limit && !empty( $value_ids ) )
3735
                        $value_ids = array_slice( $value_ids, 0, $related_limit );
3736
3737
                    // Get current values
3738
                    if ( 'pick' == $type && isset( PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ] ) && isset( PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ][ 'current_ids' ] ) )
3739
                        $related_ids = PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ][ 'current_ids' ];
3740
                    else
3741
                        $related_ids = $this->lookup_related_items( $fields[ $field ][ 'id' ], $pod[ 'id' ], $params->id, $fields[ $field ], $pod );
3742
3743
                    // Get ids to remove
3744
                    $remove_ids = array_diff( $related_ids, $value_ids );
3745
3746
                    // Delete relationships
3747
                    if ( !empty( $remove_ids ) )
3748
                        $this->delete_relationships( $params->id, $remove_ids, $pod, $fields[ $field ] );
3749
3750
                    // Save relationships
3751
                    if ( !empty( $value_ids ) )
3752
                        $this->save_relationships( $params->id, $value_ids, $pod, $fields[ $field ] );
3753
3754
                    // Run save function for field type (where needed)
3755
                    PodsForm::save( $type, $values, $params->id, $field, array_merge( $fields[ $field ], $fields[ $field ][ 'options' ] ), array_merge( $fields, $object_fields ), $pod, $params );
3756
                }
3757
3758
                // Unset data no longer needed
3759
                if ( 'pick' == $type ) {
3760
                    foreach ( $data as $field => $values ) {
3761
                        if ( isset( PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ] ) ) {
3762
                            unset( PodsField_Pick::$related_data[ PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ][ 'related_field' ][ 'id' ] ] );
3763
                            unset( PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ] );
3764
                        }
3765
                    }
3766
                }
3767
            }
3768
        }
3769
3770
        if ( !$no_conflict )
3771
            pods_no_conflict_off( $pod[ 'type' ] );
3772
3773
        if ( false === $bypass_helpers ) {
3774
            $pieces = compact( $pieces );
3775
3776
            // Plugin hooks
3777
            $this->do_hook( 'post_save_pod_item', $pieces, $is_new_item, $params->id );
3778
            $this->do_hook( "post_save_pod_item_{$params->pod}", $pieces, $is_new_item, $params->id );
3779
3780
            if ( $is_new_item ) {
3781
                $this->do_hook( 'post_create_pod_item', $pieces, $params->id );
3782
                $this->do_hook( "post_create_pod_item_{$params->pod}", $pieces, $params->id );
3783
            }
3784
            else {
3785
                $this->do_hook( 'post_edit_pod_item', $pieces, $params->id );
3786
                $this->do_hook( "post_edit_pod_item_{$params->pod}", $pieces, $params->id );
3787
            }
3788
3789
            // Call any post-save helpers (if not bypassed)
3790
            if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) {
3791 View Code Duplication
                if ( !empty( $post_save_helpers ) ) {
3792
                    pods_deprecated( sprintf( __( 'Post-save helpers are deprecated, use the action pods_post_save_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
3793
3794
                    foreach ( $post_save_helpers as $helper ) {
3795
                        $helper = $this->load_helper( array( 'name' => $helper ) );
3796
3797
                        if ( false !== $helper && ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) )
3798
                            eval( '?>' . $helper[ 'code' ] );
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
3799
                    }
3800
                }
3801
            }
3802
        }
3803
3804
        // Clear cache
3805
        pods_cache_clear( $params->id, 'pods_items_' . $pod[ 'name' ] );
3806
3807
		if ( $params->clear_slug_cache && !empty( $pod[ 'field_slug' ] ) ) {
3808
			$slug = pods( $pod[ 'name' ], $params->id )->field( $pod[ 'field_slug' ] );
3809
3810
			if ( 0 < strlen( $slug ) ) {
3811
        		pods_cache_clear( $slug, 'pods_items_' . $pod[ 'name' ] );
3812
			}
3813
		}
3814
3815
        // Clear WP meta cache
3816
        if ( in_array( $pod[ 'type' ], array( 'post_type', 'media', 'taxonomy', 'user', 'comment' ) ) ) {
3817
            $meta_type = $pod[ 'type' ];
3818
3819
            if ( 'post_type' == $meta_type )
3820
                $meta_type = 'post';
3821
3822
            wp_cache_delete( $params->id, $meta_type . '_meta' );
3823
            wp_cache_delete( $params->id, 'pods_' . $meta_type . '_meta' );
3824
        }
3825
3826
        // Success! Return the id
3827
        return $params->id;
3828
3829
    }
3830
3831
    /**
3832
     * @see PodsAPI::save_pod_item
3833
     * Add multiple pod items
3834
     *
3835
     * $params['pod'] string The Pod name (pod or pod_id is required)
3836
     * $params['pod_id'] string The Pod ID (pod or pod_id is required)
3837
     * $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
3838
     *
3839
     * $data['id'] int The item ID (optional)
3840
     * $data['data'] array An associative array of field names + values
3841
     *
3842
     * @param array|object $params An associative array of parameters, data excluded
3843
     * @param array $data An associative array of pod ids and field names + values (arrays of field data)
3844
     *
3845
     * @return int The item ID
3846
     * @since 2.0
3847
     */
3848
    public function save_pod_items ( $params, $data ) {
3849
        $params = (object) $params;
3850
3851
        $ids = array();
3852
3853
        foreach ( $data as $fields ) {
3854
            $params->data = $fields;
3855
3856
            if ( isset( $fields[ 'id' ] ) && isset( $fields[ 'data' ] ) ) {
3857
                $params->id = $fields[ 'id' ];
3858
                $params->data = $fields[ 'data' ];
3859
            }
3860
3861
            $ids[] = $this->save_pod_item( $params );
3862
        }
3863
3864
        return $ids;
3865
    }
3866
3867
	/**
3868
	 * Get the fields that have changed during a save
3869
	 *
3870
	 * @param array $pieces Pieces array from save_pod_item
3871
	 *
3872
	 * @return array Array of fields and values that have changed
3873
	 */
3874
	public function get_changed_fields( $pieces ) {
3875
3876
		$fields = $pieces[ 'fields' ];
3877
		$fields_active = $pieces[ 'fields_active' ];
3878
3879
		$fields_changed = array();
3880
3881
		if ( 0 < $pieces[ 'params' ]->id ) {
3882
			$pod = pods( $pieces[ 'params' ]->pod, $pieces[ 'params' ]->id );
3883
3884
			foreach ( $fields_active as $field ) {
3885
				if ( isset( $fields[ $field ] ) && $pod->raw( $field ) != $fields[ $field ][ 'value' ] ) {
3886
					$fields_changed[ $field ] = $fields[ $field ][ 'value' ];
3887
				}
3888
			}
3889
		}
3890
3891
		return $fields_changed;
3892
3893
	}
3894
3895
    /**
3896
     * Save relationships
3897
     *
3898
     * @param int $id ID of item
3899
     * @param int|array $related_id ID or IDs to save
0 ignored issues
show
Documentation introduced by
There is no parameter named $related_id. Did you maybe mean $related_ids?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
3900
     * @param array $pod Pod data
3901
     * @param array $field Field data
3902
     */
3903
    public function save_relationships ( $id, $related_ids, $pod, $field ) {
3904
        // Get current values
3905
        if ( 'pick' == $field[ 'type' ] && isset( PodsField_Pick::$related_data[ $field[ 'id' ] ] ) && isset( PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'current_ids' ] ) )
3906
            $current_ids = PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'current_ids' ];
3907
        else
3908
            $current_ids = $this->lookup_related_items( $field[ 'id' ], $pod[ 'id' ], $id, $field, $pod );
3909
3910
        if ( !is_array( $related_ids ) )
3911
            $related_ids = implode( ',', $related_ids );
3912
3913
        foreach ( $related_ids as $k => $related_id ) {
0 ignored issues
show
Bug introduced by
The expression $related_ids of type string|array 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...
3914
            $related_ids[ $k ] = (int) $related_id;
3915
        }
3916
3917
        $related_ids = array_unique( array_filter( $related_ids ) );
3918
3919
        $related_limit = (int) pods_var_raw( $field[ 'type' ] . '_limit', $field[ 'options' ], 0 );
3920
3921 View Code Duplication
        if ( 'single' == pods_var_raw( $field[ 'type' ] . '_format_type', $field[ 'options' ] ) )
3922
            $related_limit = 1;
3923
3924
        // Limit values
3925
        if ( 0 < $related_limit && !empty( $related_ids ) )
3926
            $related_ids = array_slice( $related_ids, 0, $related_limit );
3927
3928
        // Post Types, Media, Users, and Comments (meta-based)
3929 View Code Duplication
        if ( in_array( $pod[ 'type' ], array( 'post_type', 'media', 'taxonomy', 'user', 'comment' ) ) ) {
3930
            $object_type = $pod[ 'type' ];
3931
3932
            if ( in_array( $object_type, array( 'post_type', 'media' ) ) )
3933
                $object_type = 'post';
3934
            elseif ( 'taxonomy' == $object_type )
3935
                $object_type = 'term';
3936
3937
            delete_metadata( $object_type, $id, $field[ 'name' ] );
3938
3939
            if ( !empty( $related_ids ) ) {
3940
                update_metadata( $object_type, $id, '_pods_' . $field[ 'name' ], $related_ids );
3941
3942
                foreach ( $related_ids as $related_id ) {
3943
                    add_metadata( $object_type, $id, $field[ 'name' ], $related_id );
3944
                }
3945
            }
3946
            else
3947
                delete_metadata( $object_type, $id, '_pods_' . $field[ 'name' ] );
3948
        }
3949
        // Custom Settings Pages (options-based)
3950
        elseif ( 'settings' == $pod[ 'type' ] ) {
3951
            if ( !empty( $related_ids ) )
3952
                update_option( $pod[ 'name' ] . '_' . $field[ 'name' ], $related_ids );
3953
            else
3954
                delete_option( $pod[ 'name' ] . '_' . $field[ 'name' ] );
3955
        }
3956
3957
        $related_pod_id = $related_field_id = 0;
3958
3959
        if ( 'pick' == $field[ 'type' ] && isset( PodsField_Pick::$related_data[ $field[ 'id' ] ] ) && !empty( PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'related_field' ] ) ) {
3960
            $related_pod_id = PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'related_pod' ][ 'id' ];
3961
            $related_field_id = PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'related_field' ][ 'id' ];
3962
        }
3963
3964
        // Relationships table
3965
        if ( !pods_tableless() ) {
3966
            $related_weight = 0;
3967
3968
            foreach ( $related_ids as $related_id ) {
3969
                if ( in_array( $related_id, $current_ids ) ) {
3970
                    pods_query( "
3971
                        UPDATE `@wp_podsrel`
3972
                        SET
3973
                            `pod_id` = %d,
3974
                            `field_id` = %d,
3975
                            `item_id` = %d,
3976
                            `related_pod_id` = %d,
3977
                            `related_field_id` = %d,
3978
                            `related_item_id` = %d,
3979
                            `weight` = %d
3980
                        WHERE
3981
                            `pod_id` = %d
3982
                            AND `field_id` = %d
3983
                            AND `item_id` = %d
3984
                            AND `related_item_id` = %d
3985
                    ", array(
3986
                        $pod[ 'id' ],
3987
                        $field[ 'id' ],
3988
                        $id,
3989
                        $related_pod_id,
3990
                        $related_field_id,
3991
                        $related_id,
3992
                        $related_weight,
3993
3994
                        $pod[ 'id' ],
3995
                        $field[ 'id' ],
3996
                        $id,
3997
                        $related_id,
3998
                    ) );
3999
                }
4000
                else {
4001
                    pods_query( "
4002
                        INSERT INTO `@wp_podsrel`
4003
                            (
4004
                                `pod_id`,
4005
                                `field_id`,
4006
                                `item_id`,
4007
                                `related_pod_id`,
4008
                                `related_field_id`,
4009
                                `related_item_id`,
4010
                                `weight`
4011
                            )
4012
                        VALUES ( %d, %d, %d, %d, %d, %d, %d )
4013
                    ", array(
4014
                        $pod[ 'id' ],
4015
                        $field[ 'id' ],
4016
                        $id,
4017
                        $related_pod_id,
4018
                        $related_field_id,
4019
                        $related_id,
4020
                        $related_weight
4021
                    ) );
4022
                }
4023
4024
                $related_weight++;
4025
            }
4026
        }
4027
    }
4028
4029
    /**
4030
     * Duplicate a Pod
4031
     *
4032
     * $params['id'] int The Pod ID
4033
     * $params['name'] string The Pod name
4034
     * $params['new_name'] string The new Pod name
4035
     *
4036
     * @param array $params An associative array of parameters
4037
     * @param bool $strict (optional) Makes sure a pod exists, if it doesn't throws an error
4038
     *
4039
     * @return int New Pod ID
4040
     * @since 2.3
4041
     */
4042
    public function duplicate_pod ( $params, $strict = false ) {
4043 View Code Duplication
        if ( !is_object( $params ) && !is_array( $params ) ) {
4044
            if ( is_numeric( $params ) )
4045
                $params = array( 'id' => $params );
4046
            else
4047
                $params = array( 'name' => $params );
4048
4049
            $params = (object) pods_sanitize( $params );
4050
        }
4051
        else
4052
            $params = (object) pods_sanitize( $params );
4053
4054
        $params->table_info = false;
4055
4056
        $pod = $this->load_pod( $params, $strict );
4057
4058
        if ( empty( $pod ) ) {
4059
            if ( false !== $strict )
4060
                return pods_error( __( 'Pod not found', 'pods' ), $this );
4061
4062
            return false;
4063
        }
4064
        elseif ( in_array( $pod[ 'type' ], array( 'media', 'user', 'comment' ) ) ) {
4065
            if ( false !== $strict )
4066
                return pods_error( __( 'Pod not allowed to be duplicated', 'pods' ), $this );
4067
4068
            return false;
4069
        }
4070
        elseif ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && 0 < strlen( $pod[ 'object' ] ) ) {
4071
			$pod[ 'object' ] = '';
4072
        }
4073
4074
        unset( $pod[ 'id' ] );
4075
4076
        if ( isset( $params->new_name ) )
4077
            $pod[ 'name' ] = $params->new_name;
4078
4079
        $try = 1;
4080
4081
        $check_name = $pod[ 'name' ];
4082
        $new_label = $pod[ 'label' ];
4083
4084 View Code Duplication
        while ( $this->load_pod( array( 'name' => $check_name, 'table_info' => false ), false ) ) {
4085
            $try++;
4086
4087
            $check_name = $pod[ 'name' ] . $try;
4088
            $new_label = $pod[ 'label' ] . $try;
4089
        }
4090
4091
        $pod[ 'name' ] = $check_name;
4092
        $pod[ 'label' ] = $new_label;
4093
4094
        foreach ( $pod[ 'fields' ] as $field => $field_data ) {
4095
            unset( $pod[ 'fields' ][ $field ][ 'id' ] );
4096
        }
4097
4098
        return $this->save_pod( $pod );
4099
    }
4100
4101
    /**
4102
     * Duplicate a Field
4103
     *
4104
     * $params['pod_id'] int The Pod ID
4105
     * $params['pod'] string The Pod name
4106
     * $params['id'] int The Field ID
4107
     * $params['name'] string The Field name
4108
     * $params['new_name'] string The new Field name
4109
     *
4110
     * @param array $params An associative array of parameters
4111
     * @param bool $strict (optional) Makes sure a field exists, if it doesn't throws an error
4112
     *
4113
     * @return int New Field ID
4114
     * @since 2.3.10
4115
     */
4116
	public function duplicate_field( $params, $strict = false ) {
4117
4118
		if ( !is_object( $params ) && !is_array( $params ) ) {
4119
			if ( is_numeric( $params ) ) {
4120
				$params = array( 'id' => $params );
4121
			}
4122
			else {
4123
				$params = array( 'name' => $params );
4124
			}
4125
		}
4126
4127
		$params = (object) pods_sanitize( $params );
4128
4129
		$params->table_info = false;
4130
4131
		$field = $this->load_field( $params, $strict );
4132
4133
		if ( empty( $field ) ) {
4134
			if ( false !== $strict ) {
4135
				return pods_error( __( 'Field not found', 'pods' ), $this );
4136
			}
4137
4138
			return false;
4139
		}
4140
4141
		unset( $field[ 'id' ] );
4142
4143
		if ( isset( $params->new_name ) ) {
4144
			$field[ 'name' ] = $params->new_name;
4145
		}
4146
4147
		$try = 1;
4148
4149
		$check_name = $field[ 'name' ];
4150
		$new_label = $field[ 'label' ];
4151
4152 View Code Duplication
		while ( $this->load_field( array( 'pod_id' => $field[ 'pod_id' ], 'name' => $check_name, 'table_info' => false ), false ) ) {
4153
			$try++;
4154
4155
			$check_name = $field[ 'name' ] . $try;
4156
			$new_label = $field[ 'label' ] . $try;
4157
		}
4158
4159
		$field[ 'name' ] = $check_name;
4160
		$field[ 'label' ] = $new_label;
4161
4162
		return $this->save_field( $field );
4163
4164
	}
4165
4166
    /**
4167
     * @see PodsAPI::save_pod_item
4168
     *
4169
     * Duplicate a pod item
4170
     *
4171
     * $params['pod'] string The Pod name
4172
     * $params['id'] int The item's ID from the wp_pods_* table
4173
     *
4174
     * @param array $params An associative array of parameters
4175
     *
4176
     * @return int The table row ID
4177
     * @since 1.12
4178
     */
4179
    public function duplicate_pod_item ( $params ) {
4180
        $params = (object) pods_sanitize( $params );
4181
4182
        $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => false ) );
4183
4184
        if ( false === $pod )
4185
            return pods_error( __( 'Pod not found', 'pods' ), $this );
4186
4187
        $pod = pods( $params->pod, $params->id );
4188
4189
        $params->pod = $pod->pod;
4190
        $params->pod_id = $pod->pod_id;
4191
4192
        $fields = (array) pods_var_raw( 'fields', $pod->pod_data, array(), null, true );
4193
        $object_fields = (array) pods_var_raw( 'object_fields', $pod->pod_data, array(), null, true );
4194
4195
        if ( !empty( $object_fields ) )
4196
            $fields = array_merge( $object_fields, $fields );
4197
4198
        $save_params = array(
4199
            'pod' => $params->pod,
4200
            'data' => array()
4201
        );
4202
4203
        foreach ( $fields as $field ) {
4204
            $value = $pod->field( array( 'name' => $field[ 'name' ], 'output' => 'ids' ) );
4205
4206
            if ( !empty( $value ) || ( !is_array( $value ) && 0 < strlen( $value ) ) )
4207
                $save_params[ 'data' ][ $field[ 'name' ] ] = $value;
4208
        }
4209
4210
        $save_params = $this->do_hook( 'duplicate_pod_item', $save_params, $pod->pod, $pod->id(), $params );
4211
4212
        $id = $this->save_pod_item( $save_params );
4213
4214
        return $id;
4215
    }
4216
4217
    /**
4218
     * @see pods()
4219
     *
4220
     * Export a pod item
4221
     *
4222
     * $params['pod'] string The Pod name
4223
     * $params['id'] int The item's ID from the wp_pods_* table
4224
     * $params['fields'] array The fields to export
4225
     * $params['depth'] int How many levels deep to export data
4226
     *
4227
     * @param array $params An associative array of parameters
4228
     * @param object $pod (optional) Pods object
4229
     *
4230
     * @return int The table row ID
4231
     * @since 1.12
4232
     */
4233
    public function export_pod_item ( $params, $pod = null ) {
4234
        if ( !is_object( $pod ) || 'Pods' != get_class( $pod ) ) {
4235
            if ( empty( $params ) )
4236
                return false;
4237
4238
            $params = (object) pods_sanitize( $params );
4239
4240
            $pod = pods( $params->pod, $params->id, false );
4241
4242
            if ( empty( $pod ) )
4243
                return false;
4244
        }
4245
4246
        $fields = (array) pods_var_raw( 'fields', $params, array(), null, true );
4247
        $depth = (int) pods_var_raw( 'depth', $params, 2, null, true );
4248
        $object_fields = (array) pods_var_raw( 'object_fields', $pod->pod_data, array(), null, true );
4249
        $flatten = (boolean) pods_var( 'flatten', $params, false, null, true );
4250
4251
        if ( empty( $fields ) ) {
4252
            $fields = $pod->fields;
4253
            $fields = array_merge( $fields, $object_fields );
4254
        }
4255
4256
        $data = $this->export_pod_item_level( $pod, $fields, $depth, $flatten );
4257
4258
        $data = $this->do_hook( 'export_pod_item', $data, $pod->pod, $pod->id(), $pod, $fields, $depth, $flatten );
4259
4260
        return $data;
4261
    }
4262
4263
    /**
4264
     * Export a pod item by depth level
4265
     *
4266
     * @param Pods $pod Pods object
4267
     * @param array $fields Fields to export
4268
     * @param int $depth Depth limit
4269
     * @param boolean $flatten Whether to flatten arrays for display
4270
     * @param int $current_depth Current depth level
4271
     *
4272
     * @return array Data array
4273
     *
4274
     * @since 2.3
4275
     */
4276
    private function export_pod_item_level ( $pod, $fields, $depth, $flatten = false, $current_depth = 1 ) {
4277
        $tableless_field_types = PodsForm::tableless_field_types();
4278
        $simple_tableless_objects = PodsForm::simple_tableless_objects();
4279
4280
        $object_fields = (array) pods_var_raw( 'object_fields', $pod->pod_data, array(), null, true );
4281
4282
        $export_fields = array();
4283
4284
        foreach ( $fields as $k => $field ) {
4285
            if ( !is_array( $field ) ) {
4286
                $field = array(
4287
                    'id' => 0,
4288
                    'name' => $field
4289
                );
4290
            }
4291
4292
            if ( isset( $pod->fields[ $field[ 'name' ] ] ) ) {
4293
                $field = $pod->fields[ $field[ 'name' ] ];
4294
                $field[ 'lookup_name' ] = $field[ 'name' ];
4295
4296
                if ( in_array( $field[ 'type' ], $tableless_field_types ) && !in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) ) {
4297
                    if ( 'pick' == $field[ 'type' ] ) {
4298
                        if ( empty( $field[ 'table_info' ] ) )
4299
                            $field[ 'table_info' ] = $this->get_table_info( pods_var_raw( 'pick_object', $field ), pods_var_raw( 'pick_val', $field ), null, null, $field );
4300
4301
                        if ( !empty( $field[ 'table_info' ] ) )
4302
                            $field[ 'lookup_name' ] .= '.' . $field[ 'table_info' ][ 'field_id' ];
4303
                    }
4304
                    elseif ( in_array( $field[ 'type' ], PodsForm::file_field_types() ) )
4305
                        $field[ 'lookup_name' ] .= '.guid';
4306
                }
4307
4308
                $export_fields[ $field[ 'name' ] ] = $field;
4309
            }
4310
            elseif ( isset( $object_fields[ $field[ 'name' ] ] ) ) {
4311
                $field = $object_fields[ $field[ 'name' ] ];
4312
                $field[ 'lookup_name' ] = $field[ 'name' ];
4313
4314
                $export_fields[ $field[ 'name' ] ] = $field;
4315
            }
4316
            elseif ( $field[ 'name' ] == $pod->pod_data[ 'field_id' ] ) {
4317
                $field[ 'type' ] = 'number';
4318
                $field[ 'lookup_name' ] = $field[ 'name' ];
4319
4320
                $export_fields[ $field[ 'name' ] ] = $field;
4321
            }
4322
        }
4323
4324
        $data = array();
4325
4326
        foreach ( $export_fields as $field ) {
4327
            // Return IDs (or guid for files) if only one level deep
4328
            if ( 1 == $depth )
4329
                $data[ $field[ 'name' ] ] = $pod->field( array( 'name' => $field[ 'lookup_name' ], 'output' => 'arrays' ) );
4330
            // Recurse depth levels for pick fields if $depth allows
4331
            elseif ( ( -1 == $depth || $current_depth < $depth ) && 'pick' == $field[ 'type' ] && !in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) ) {
4332
                $related_data = array();
4333
4334
                $related_ids = $pod->field( array( 'name' => $field[ 'name' ], 'output' => 'ids' ) );
4335
4336
                if ( !empty( $related_ids ) ) {
4337
                    $related_ids = (array) $related_ids;
4338
4339
                    $pick_object = pods_var_raw( 'pick_object', $field );
4340
4341
                    $related_pod = pods( pods_var_raw( 'pick_val', $field ), null, false );
4342
4343
                    // If this isn't a Pod, return data exactly as Pods does normally
4344
                    if ( empty( $related_pod ) || ( 'pod' != $pick_object && $pick_object != $related_pod->pod_data[ 'type' ] ) || $related_pod->pod == $pod->pod )
4345
                        $related_data = $pod->field( array( 'name' => $field[ 'name' ], 'output' => 'arrays' ) );
4346
                    else {
4347
                        $related_object_fields = (array) pods_var_raw( 'object_fields', $related_pod->pod_data, array(), null, true );
4348
4349
                        $related_fields = array_merge( $related_pod->fields, $related_object_fields );
4350
4351
                        foreach ( $related_ids as $related_id ) {
4352
                            if ( $related_pod->fetch( $related_id ) ) {
4353
                                $related_item = $this->export_pod_item_level( $related_pod, $related_fields, $depth, $flatten, ( $current_depth + 1 ) );
4354
4355
                                $related_data[ $related_id ] = $this->do_hook( 'export_pod_item_level', $related_item, $related_pod->pod, $related_pod->id(), $related_pod, $related_fields, $depth, $flatten, ( $current_depth + 1 ) );
4356
                            }
4357
                        }
4358
4359
                        if ( $flatten && !empty( $related_data ) )
4360
                            $related_data = pods_serial_comma( array_values( $related_data ), array( 'and' => '', 'field_index' => $related_pod->pod_data[ 'field_index' ] ) );
4361
                    }
4362
                }
4363
4364
                $data[ $field[ 'name' ] ] = $related_data;
4365
            }
4366
            // Return data exactly as Pods does normally
4367
            else
4368
                $data[ $field[ 'name' ] ] = $pod->field( array( 'name' => $field[ 'name' ], 'output' => 'arrays' ) );
4369
4370
            if ( $flatten && is_array( $data[ $field[ 'name' ] ] ) )
4371
                $data[ $field[ 'name' ] ] = pods_serial_comma( $data[ $field[ 'name' ] ], array( 'field' => $field[ 'name' ], 'fields' => $export_fields, 'and' => '' ) );
4372
        }
4373
4374
	$data[ 'id' ] = (int) $pod->id();
4375
        return $data;
4376
    }
4377
4378
    /**
4379
     * Reorder a Pod
4380
     *
4381
     * $params['pod'] string The Pod name
4382
     * $params['field'] string The field name of the field to reorder
4383
     * $params['order'] array The key => value array of items to reorder (key should be an integer)
4384
     *
4385
     * @param array $params An associative array of parameters
4386
     *
4387
     * @return bool
4388
     *
4389
     * @since 1.9.0
4390
     */
4391
    public function reorder_pod_item ( $params ) {
4392
        $params = (object) pods_sanitize( $params );
4393
4394
        // @deprecated 2.0
4395
        if ( isset( $params->datatype ) ) {
4396
            pods_deprecated( __( '$params->pod instead of $params->datatype', 'pods' ), '2.0' );
4397
4398
            $params->pod = $params->datatype;
4399
4400
            unset( $params->datatype );
4401
        }
4402
4403
        if ( null === pods_var_raw( 'pod', $params, null, null, true ) )
4404
            return pods_error( __( '$params->pod is required', 'pods' ), $this );
4405
4406
        if ( !is_array( $params->order ) )
4407
            $params->order = explode( ',', $params->order );
4408
4409
        $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => true ) );
4410
4411
        $params->name = $pod[ 'name' ];
4412
4413
        if ( false === $pod )
4414
            return pods_error( __( 'Pod is required', 'pods' ), $this );
4415
4416
        foreach ( $params->order as $order => $id ) {
4417
            if ( isset( $pod[ 'fields' ][ $params->field ] ) || isset( $pod[ 'object_fields' ][ $params->field ] ) ) {
4418
                if ( 'table' == $pod[ 'storage' ] && ( !pods_tableless() ) ) {
4419
                    if ( isset( $pod[ 'fields' ][ $params->field ] ) )
4420
                        pods_query( "UPDATE `@wp_pods_{$params->name}` SET `{$params->field}` = " . pods_absint( $order ) . " WHERE `id` = " . pods_absint( $id ) . " LIMIT 1" );
4421
                    else
4422
                        pods_query( "UPDATE `{$pod['table']}` SET `{$params->field}` = " . pods_absint( $order ) . " WHERE `{$pod['field_id']}` = " . pods_absint( $id ) . " LIMIT 1" );
4423
                }
4424
                else
4425
                    $this->save_pod_item( array( 'pod' => $params->pod, 'pod_id' => $params->pod_id, 'id' => $id, 'data' => array( $params->field => pods_absint( $order ) ) ) );
4426
            }
4427
        }
4428
4429
        return true;
4430
    }
4431
4432
    /**
4433
     *
4434
     * Delete all content for a Pod
4435
     *
4436
     * $params['id'] int The Pod ID
4437
     * $params['name'] string The Pod name
4438
     *
4439
     * @param array $params An associative array of parameters
4440
     * @param array $pod Pod data
4441
     *
4442
     * @return bool
4443
     *
4444
     * @uses pods_query
4445
     * @uses pods_cache_clear
4446
     *
4447
     * @since 1.9.0
4448
     */
4449
    public function reset_pod ( $params, $pod = false ) {
4450
        $params = (object) pods_sanitize( $params );
4451
4452
        $params->table_info = true;
4453
4454
        if ( empty( $pod ) )
4455
            $pod = $this->load_pod( $params );
4456
4457
        if ( false === $pod )
4458
            return pods_error( __( 'Pod not found', 'pods' ), $this );
4459
4460
        $params->id = $pod[ 'id' ];
4461
        $params->name = $pod[ 'name' ];
4462
4463 View Code Duplication
        if ( !pods_tableless() ) {
4464
            if ( 'table' == $pod[ 'storage' ] ) {
4465
                try {
4466
                    pods_query( "TRUNCATE `@wp_pods_{$params->name}`", false );
4467
                }
4468
                catch ( Exception $e ) {
4469
                    // Allow pod to be reset if the table doesn't exist
4470
                    if ( false === strpos( $e->getMessage(), 'Unknown table' ) )
4471
                        return pods_error( $e->getMessage(), $this );
4472
                }
4473
            }
4474
4475
            pods_query( "DELETE FROM `@wp_podsrel` WHERE `pod_id` = {$params->id} OR `related_pod_id` = {$params->id}", false );
4476
        }
4477
4478
        // @todo Delete relationships from tableless relationships
4479
4480
        // Delete all posts/revisions from this post type
4481
        if ( in_array( $pod[ 'type' ], array( 'post_type', 'media' ) ) ) {
4482
            $type = pods_var( 'object', $pod, $pod[ 'name' ], null, true );
4483
4484
            $sql = "
4485
                DELETE `t`, `r`, `m`
4486
                FROM `{$pod['table']}` AS `t`
4487
                LEFT JOIN `{$pod['meta_table']}` AS `m`
4488
                    ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
4489
                LEFT JOIN `{$pod['table']}` AS `r`
4490
                    ON `r`.`post_parent` = `t`.`{$pod['field_id']}` AND `r`.`post_status` = 'inherit'
4491
                WHERE `t`.`{$pod['field_type']}` = '{$type}'
4492
            ";
4493
4494
            pods_query( $sql, false );
4495
        }
4496
        // Delete all terms from this taxonomy
4497
        elseif ( 'taxonomy' == $pod[ 'type' ] ) {
4498
            if ( function_exists( 'get_term_meta' ) ) {
4499
                $sql = "
4500
                    DELETE `t`, `m`, `tt`, `tr`
4501
                    FROM `{$pod['table']}` AS `t`
4502
                    LEFT JOIN `{$pod['meta_table']}` AS `m`
4503
                        ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
4504
                    " . $pod['join']['tt'] . "
4505
                    " . $pod['join']['tr'] . "
4506
                    WHERE " . implode( ' AND ', $pod['where'] ) . "
4507
                ";
4508
            } else {
4509
                $sql = "
4510
                    DELETE `t`, `tt`, `tr`
4511
                    FROM `{$pod['table']}` AS `t`
4512
                    " . $pod['join']['tt'] . "
4513
                    " . $pod['join']['tr'] . "
4514
                    WHERE " . implode( ' AND ', $pod['where'] ) . "
4515
                ";
4516
            }
4517
4518
            pods_query( $sql, false );
4519
        }
4520
        // Delete all users except the current one
4521
        elseif ( 'user' == $pod[ 'type' ] ) {
4522
            $sql = "
4523
                DELETE `t`, `m`
4524
                FROM `{$pod['table']}` AS `t`
4525
                LEFT JOIN `{$pod['meta_table']}` AS `m`
4526
                    ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
4527
                WHERE `t`.`{$pod['field_id']}` != " . (int) get_current_user_id() . "
4528
            ";
4529
4530
            pods_query( $sql, false );
4531
        }
4532
        // Delete all comments
4533
        elseif ( 'comment' == $pod[ 'type' ] ) {
4534
            $type = pods_var( 'object', $pod, $pod[ 'name' ], null, true );
4535
4536
            $sql = "
4537
                DELETE `t`, `m`
4538
                FROM `{$pod['table']}` AS `t`
4539
                LEFT JOIN `{$pod['meta_table']}` AS `m`
4540
                    ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
4541
                WHERE `t`.`{$pod['field_type']}` = '{$type}'
4542
            ";
4543
4544
            pods_query( $sql, false );
4545
        }
4546
4547
        pods_cache_clear( true ); // only way to reliably clear out cached data across an entire group
4548
4549
        return true;
4550
    }
4551
4552
    /**
4553
     * Delete a Pod and all its content
4554
     *
4555
     * $params['id'] int The Pod ID
4556
     * $params['name'] string The Pod name
4557
     *
4558
     * @param array $params An associative array of parameters
4559
     * @param bool $strict (optional) Makes sure a pod exists, if it doesn't throws an error
4560
     * @param bool $delete_all (optional) Whether to delete all content from a WP object
4561
     *
4562
     * @uses PodsAPI::load_pod
4563
     * @uses wp_delete_post
4564
     * @uses pods_query
4565
     *
4566
     * @return bool
4567
     * @since 1.7.9
4568
     */
4569
    public function delete_pod ( $params, $strict = false, $delete_all = false ) {
4570
        /**
4571
         * @var $wpdb wpdb
4572
         */
4573
        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...
4574
4575 View Code Duplication
        if ( !is_object( $params ) && !is_array( $params ) ) {
4576
            if ( is_numeric( $params ) )
4577
                $params = array( 'id' => $params );
4578
            else
4579
                $params = array( 'name' => $params );
4580
4581
            $params = (object) pods_sanitize( $params );
4582
        }
4583
        else
4584
            $params = (object) pods_sanitize( $params );
4585
4586
        $params->table_info = false;
4587
4588
        $pod = $this->load_pod( $params, $strict );
4589
4590
        if ( empty( $pod ) ) {
4591
            if ( false !== $strict )
4592
                return pods_error( __( 'Pod not found', 'pods' ), $this );
4593
4594
            return false;
4595
        }
4596
4597
        $params->id = (int) $pod[ 'id' ];
4598
        $params->name = $pod[ 'name' ];
4599
4600
        foreach ( $pod[ 'fields' ] as $field ) {
4601
            $field[ 'pod' ] = $pod;
4602
4603
            $this->delete_field( $field, false );
4604
        }
4605
4606
        // Only delete the post once the fields are taken care of, it's not required anymore
4607
        $success = wp_delete_post( $params->id );
4608
4609
        if ( !$success )
4610
            return pods_error( __( 'Pod unable to be deleted', 'pods' ), $this );
4611
4612
        // Reset content
4613
        if ( $delete_all )
4614
            $this->reset_pod( $params, $pod );
4615
4616 View Code Duplication
        if ( !pods_tableless() ) {
4617
            if ( 'table' == $pod[ 'storage' ] ) {
4618
                try {
4619
                    pods_query( "DROP TABLE IF EXISTS `@wp_pods_{$params->name}`", false );
4620
                }
4621
                catch ( Exception $e ) {
4622
                    // Allow pod to be deleted if the table doesn't exist
4623
                    if ( false === strpos( $e->getMessage(), 'Unknown table' ) )
4624
                        return pods_error( $e->getMessage(), $this );
4625
                }
4626
            }
4627
4628
            pods_query( "DELETE FROM `@wp_podsrel` WHERE `pod_id` = {$params->id} OR `related_pod_id` = {$params->id}", false );
4629
        }
4630
4631
        // @todo Delete relationships from tableless relationships
4632
4633
        // Delete any relationship references
4634
        $sql = "
4635
            DELETE `pm`
4636
            FROM `{$wpdb->postmeta}` AS `pm`
4637
            LEFT JOIN `{$wpdb->posts}` AS `p`
4638
                ON `p`.`post_type` = '_pods_field'
4639
                    AND `p`.`ID` = `pm`.`post_id`
4640
            LEFT JOIN `{$wpdb->postmeta}` AS `pm2`
4641
                ON `pm2`.`meta_key` = 'pick_object'
4642
                    AND `pm2`.`meta_value` = 'pod'
4643
                    AND `pm2`.`post_id` = `pm`.`post_id`
4644
            WHERE
4645
                `p`.`ID` IS NOT NULL
4646
                AND `pm2`.`meta_id` IS NOT NULL
4647
                AND `pm`.`meta_key` = 'pick_val'
4648
                AND `pm`.`meta_value` = '{$params->name}'
4649
        ";
4650
4651
        pods_query( $sql );
4652
4653
        $this->cache_flush_pods( $pod );
4654
4655
        return true;
4656
    }
4657
4658
    /**
4659
     * Drop a field within a Pod
4660
     *
4661
     * $params['id'] int The field ID
4662
     * $params['name'] int The field name
4663
     * $params['pod'] string The Pod name
4664
     * $params['pod_id'] string The Pod name
4665
     *
4666
     * @param array $params An associative array of parameters
4667
     * @param bool $table_operation Whether or not to handle table operations
4668
     *
4669
     * @uses PodsAPI::load_field
4670
     * @uses wp_delete_post
4671
     * @uses pods_query
4672
     *
4673
     * @return bool
4674
     * @since 1.7.9
4675
     */
4676
    public function delete_field ( $params, $table_operation = true ) {
4677
        /**
4678
         * @var $wpdb wpdb
4679
         */
4680
        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...
4681
4682
        $tableless_field_types = PodsForm::tableless_field_types();
4683
        $simple_tableless_objects = PodsForm::simple_tableless_objects();
4684
4685
        $params = (object) pods_sanitize( $params );
4686
4687
        if ( !isset( $params->pod ) )
4688
            $params->pod = '';
4689
4690
        if ( !isset( $params->pod_id ) )
4691
            $params->pod_id = 0;
4692
4693
        $pod = $params->pod;
4694
4695
        $save_pod = false;
4696
4697
        if ( !is_array( $pod ) )
4698
            $pod = $this->load_pod( array( 'name' => $pod, 'id' => $params->pod_id, 'table_info' => false ) );
4699
        else
4700
            $save_pod = true;
4701
4702
        if ( empty( $pod ) )
4703
            return pods_error( __( 'Pod not found', 'pods' ), $this );
4704
4705
        $params->pod_id = $pod[ 'id' ];
4706
        $params->pod = $pod[ 'name' ];
4707
4708
        if ( !isset( $params->name ) )
4709
            $params->name = '';
4710
4711
        if ( !isset( $params->id ) )
4712
            $params->id = 0;
4713
4714
        $field = $this->load_field( array( 'name' => $params->name, 'id' => $params->id, 'pod' => $params->pod, 'pod_id' => $params->pod_id ) );
4715
4716
        if ( false === $field )
4717
            return pods_error( __( 'Field not found', 'pods' ), $this );
4718
4719
        $params->id = $field[ 'id' ];
4720
        $params->name = $field[ 'name' ];
4721
4722
        $simple = ( 'pick' == $field[ 'type' ] && in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) );
4723
        $simple = (boolean) $this->do_hook( 'tableless_custom', $simple, $field, $pod, $params );
4724
4725
        if ( $table_operation && 'table' == $pod[ 'storage' ] && ( !in_array( $field[ 'type' ], $tableless_field_types ) || $simple ) )
4726
            pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` DROP COLUMN `{$params->name}`", false );
4727
4728
        $success = wp_delete_post( $params->id );
4729
4730
        if ( !$success )
4731
            return pods_error( __( 'Field unable to be deleted', 'pods' ), $this );
4732
4733
        $wpdb->query( $wpdb->prepare( "DELETE pm FROM {$wpdb->postmeta} AS pm
4734
            LEFT JOIN {$wpdb->posts} AS p
4735
                ON p.post_type = '_pods_field' AND p.ID = pm.post_id
4736
            WHERE p.ID IS NOT NULL AND pm.meta_key = 'sister_id' AND pm.meta_value = %d", $params->id ) );
4737
4738
        if ( ( !pods_tableless() ) && $table_operation ) {
4739
            pods_query( "DELETE FROM `@wp_podsrel` WHERE (`pod_id` = {$params->pod_id} AND `field_id` = {$params->id}) OR (`related_pod_id` = {$params->pod_id} AND `related_field_id` = {$params->id})", false );
4740
        }
4741
4742
        // @todo Delete tableless relationship meta
4743
4744
        if ( true === $save_pod )
4745
            $this->cache_flush_pods( $pod );
4746
4747
        return true;
4748
    }
4749
4750
    /**
4751
     * Drop a Pod Object
4752
     *
4753
     * $params['id'] int The object ID
4754
     * $params['name'] string The object name
4755
     * $params['type'] string The object type
4756
     *
4757
     * @param array|object $params An associative array of parameters
4758
     *
4759
     * @uses wp_delete_post
4760
     *
4761
     * @return bool
4762
     * @since 2.0
4763
     */
4764
    public function delete_object ( $params ) {
4765
        $params = (object) $params;
4766
        $object = $this->load_object( $params );
4767
4768 View Code Duplication
        if ( empty( $object ) )
4769
            return pods_error( sprintf( __( "%s Object not found", 'pods' ), ucwords( $params->type ) ), $this );
4770
4771
        $success = wp_delete_post( $params->id );
4772
4773 View Code Duplication
        if ( !$success )
4774
            return pods_error( sprintf( __( "%s Object not deleted", 'pods' ), ucwords( $params->type ) ), $this );
4775
4776
        pods_transient_clear( 'pods_objects_' . $params->type );
4777
4778
        return true;
4779
    }
4780
4781
    /**
4782
     * @see PodsAPI::delete_object
4783
     *
4784
     * Drop a Pod Template
4785
     *
4786
     * $params['id'] int The template ID
4787
     * $params['name'] string The template name
4788
     *
4789
     * @param array $params An associative array of parameters
4790
     *
4791
     * @return bool
4792
     * @since 1.7.9
4793
     */
4794
    public function delete_template ( $params ) {
4795
        $params = (object) $params;
4796
        $params->type = 'template';
4797
        return $this->delete_object( $params );
4798
    }
4799
4800
    /**
4801
     * @see PodsAPI::delete_object
4802
     *
4803
     * Drop a Pod Page
4804
     *
4805
     * $params['id'] int The page ID
4806
     * $params['uri'] string The page URI
4807
     *
4808
     * @param array $params An associative array of parameters
4809
     *
4810
     * @return bool
4811
     * @since 1.7.9
4812
     */
4813
    public function delete_page ( $params ) {
4814
        $params = (object) $params;
4815
        if ( isset( $params->uri ) ) {
4816
            $params->name = $params->uri;
4817
            unset( $params->uri );
4818
        }
4819
        if ( isset( $params->name ) )
4820
            $params->name = trim( $params->name, '/' );
4821
        $params->type = 'page';
4822
        return $this->delete_object( $params );
4823
    }
4824
4825
    /**
4826
     * @see PodsAPI::delete_object
4827
     *
4828
     * Drop a Pod Helper
4829
     *
4830
     * $params['id'] int The helper ID
4831
     * $params['name'] string The helper name
4832
     *
4833
     * @param array $params An associative array of parameters
4834
     *
4835
     * @return bool
4836
     * @since 1.7.9
4837
     */
4838
    public function delete_helper ( $params ) {
4839
        $params = (object) $params;
4840
        $params->type = 'helper';
4841
        return $this->delete_object( $params );
4842
    }
4843
4844
    /**
4845
     * Drop a single pod item
4846
     *
4847
     * $params['id'] int (optional) The item's ID from the wp_pod_* table (used with datatype parameter)
4848
     * $params['pod'] string (optional) The Pod name (used with id parameter)
4849
     * $params['pod_id'] int (optional) The Pod ID (used with id parameter)
4850
     * $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
4851
     *
4852
     * @param array $params An associative array of parameters
4853
     * @param bool $wp Whether to run WP object delete action
4854
     *
4855
     * @return bool
4856
     * @since 1.7.9
4857
     */
4858
    public function delete_pod_item ( $params, $wp = true ) {
4859
        $params = (object) pods_sanitize( $params );
4860
4861
        // @deprecated 2.0
4862
        if ( isset( $params->datatype_id ) || isset( $params->datatype ) || isset( $params->tbl_row_id ) ) {
4863
            if ( isset( $params->tbl_row_id ) ) {
4864
                pods_deprecated( __( '$params->id instead of $params->tbl_row_id', 'pods' ), '2.0' );
4865
                $params->id = $params->tbl_row_id;
4866
                unset( $params->tbl_row_id );
4867
            }
4868
4869 View Code Duplication
            if ( isset( $params->pod_id ) ) {
4870
                pods_deprecated( __( '$params->id instead of $params->pod_id', 'pods' ), '2.0' );
4871
                $params->id = $params->pod_id;
4872
                unset( $params->pod_id );
4873
            }
4874
4875 View Code Duplication
            if ( isset( $params->dataype_id ) ) {
4876
                pods_deprecated( __( '$params->pod_id instead of $params->datatype_id', 'pods' ), '2.0' );
4877
                $params->pod_id = $params->dataype_id;
4878
                unset( $params->dataype_id );
4879
            }
4880
4881
            if ( isset( $params->datatype ) ) {
4882
                pods_deprecated( __( '$params->pod instead of $params->datatype', 'pods' ), '2.0' );
4883
                $params->pod = $params->datatype;
4884
                unset( $params->datatype );
4885
            }
4886
        }
4887
4888
        if ( !isset( $params->id ) )
4889
            return pods_error( __( 'Pod Item not found', 'pods' ), $this );
4890
4891
        $params->id = pods_absint( $params->id );
4892
4893
        if ( !isset( $params->pod ) )
4894
            $params->pod = '';
4895
4896
        if ( !isset( $params->pod_id ) )
4897
            $params->pod_id = 0;
4898
4899
        $pod = $this->load_pod( array( 'name' => $params->pod, 'id' => $params->pod_id, 'table_info' => false ) );
4900
4901
        if ( false === $pod )
4902
            return pods_error( __( 'Pod not found', 'pods' ), $this );
4903
4904
        $params->pod_id = $pod[ 'id' ];
4905
        $params->pod = $pod[ 'name' ];
4906
4907
        // Allow Helpers to bypass subsequent helpers in recursive delete_pod_item calls
4908
        $bypass_helpers = false;
4909
4910
        if ( isset( $params->bypass_helpers ) && false !== $params->bypass_helpers )
4911
            $bypass_helpers = true;
4912
4913
        $pre_delete_helpers = $post_delete_helpers = array();
4914
4915
        if ( false === $bypass_helpers ) {
4916
            // Plugin hook
4917
            $this->do_hook( 'pre_delete_pod_item', $params, $pod );
4918
            $this->do_hook( "pre_delete_pod_item_{$params->pod}", $params, $pod );
4919
4920
            // Call any pre-save helpers (if not bypassed)
4921 View Code Duplication
            if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) {
4922
                if ( !empty( $pod[ 'options' ] ) && is_array( $pod[ 'options' ] ) ) {
4923
                    $helpers = array( 'pre_delete_helpers', 'post_delete_helpers' );
4924
4925
                    foreach ( $helpers as $helper ) {
4926
                        if ( isset( $pod[ 'options' ][ $helper ] ) && !empty( $pod[ 'options' ][ $helper ] ) )
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
4927
                            ${$helper} = explode( ',', $pod[ 'options' ][ $helper ] );
4928
                    }
4929
                }
4930
4931
                if ( !empty( $pre_delete_helpers ) ) {
4932
                    pods_deprecated( sprintf( __( 'Pre-delete helpers are deprecated, use the action pods_pre_delete_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
4933
4934
                    foreach ( $pre_delete_helpers as $helper ) {
4935
                        $helper = $this->load_helper( array( 'name' => $helper ) );
4936
4937
                        if ( false !== $helper )
4938
                            eval( '?>' . $helper[ 'code' ] );
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
4939
                    }
4940
                }
4941
            }
4942
        }
4943
4944
        // Delete object from relationship fields
4945
        $this->delete_object_from_relationships( $params->id, $pod );
4946
4947
        if ( 'table' == $pod[ 'storage' ] )
4948
            pods_query( "DELETE FROM `@wp_pods_{$params->pod}` WHERE `id` = {$params->id} LIMIT 1" );
4949
4950
        if ( $wp ) {
4951
            if ( 'taxonomy' == $pod['type'] ) {
4952
                $taxonomy = $pod['name'];
4953
4954
                if ( ! empty( $pod['object'] ) ) {
4955
                    $taxonomy = $pod['object'];
4956
                }
4957
4958
                wp_delete_term( $params->id, $taxonomy );
4959
            } elseif ( ! in_array( $pod['type'], array( 'pod', 'table', '', 'taxonomy' ) ) ) {
4960
                $this->delete_wp_object( $pod['type'], $params->id );
4961
            }
4962
        }
4963
4964
        if ( false === $bypass_helpers ) {
4965
            // Plugin hook
4966
            $this->do_hook( 'post_delete_pod_item', $params, $pod );
4967
            $this->do_hook( "post_delete_pod_item_{$params->pod}", $params, $pod );
4968
4969
            // Call any post-save helpers (if not bypassed)
4970 View Code Duplication
            if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) {
4971
                if ( !empty( $post_delete_helpers ) ) {
4972
                    pods_deprecated( sprintf( __( 'Post-delete helpers are deprecated, use the action pods_post_delete_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
4973
4974
                    foreach ( $post_delete_helpers as $helper ) {
4975
                        $helper = $this->load_helper( array( 'name' => $helper ) );
4976
4977
                        if ( false !== $helper )
4978
                            eval( '?>' . $helper[ 'code' ] );
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
4979
                    }
4980
                }
4981
            }
4982
        }
4983
4984
        pods_cache_clear( $params->id, 'pods_items_' . $params->pod );
4985
4986
        return true;
4987
    }
4988
4989
    /**
4990
     * Delete an object from tableless fields
4991
     *
4992
     * @param int $id
4993
     * @param string $type
0 ignored issues
show
Bug introduced by
There is no parameter named $type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
4994
     * @param string $name
4995
     *
4996
     * @return bool
4997
     *
4998
     * @since 2.3
4999
     */
5000
    public function delete_object_from_relationships ( $id, $object, $name = null ) {
5001
        /**
5002
         * @var $pods_init \PodsInit
5003
         */
5004
        global $pods_init;
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...
5005
5006
        $pod = false;
5007
5008
        // Run any bidirectional delete operations
5009
        if ( is_array( $object ) )
5010
            $pod = $object;
5011
        elseif ( is_object( $pods_init ) )
5012
            $pod = PodsInit::$meta->get_object( $object, $name );
0 ignored issues
show
Bug introduced by
The property meta cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
5013
5014
        if ( !empty( $pod ) ) {
5015
            $object = $pod[ 'type' ];
5016
            $name = $pod[ 'name' ];
5017
5018
            foreach ( $pod[ 'fields' ] as $field ) {
5019
                PodsForm::delete( $field[ 'type' ], $id, $field[ 'name' ], array_merge( $field, $field[ 'options' ] ), $pod );
5020
            }
5021
        }
5022
5023
        // Lookup related fields (non-bidirectional)
5024
        $params = array(
5025
            'where' => array(
5026
                array(
5027
                    'key' => 'type',
5028
                    'value' => 'pick'
5029
                ),
5030
                array(
5031
                    'key' => 'pick_object',
5032
                    'value' => $object
5033
                )
5034
            )
5035
        );
5036
5037
        if ( !empty( $name ) && $name != $object ) {
5038
            $params[ 'where' ][] = array(
5039
                'key' => 'pick_val',
5040
                'value' => $name
5041
            );
5042
        }
5043
5044
        $fields = $this->load_fields( $params, false );
5045
5046
        if ( !empty( $pod ) && 'media' == $pod[ 'type' ] ) {
5047
            $params[ 'where' ] = array(
5048
                array(
5049
                    'key' => 'type',
5050
                    'value' => 'file'
5051
                )
5052
            );
5053
5054
            $fields = array_merge( $fields, $this->load_fields( $params, false ) );
5055
        }
5056
5057
        if ( is_array( $fields ) && !empty( $fields ) ) {
5058
            foreach ( $fields as $related_field ) {
5059
                $related_pod = $this->load_pod( array( 'id' => $related_field[ 'pod_id' ], 'fields' => false ), false );
5060
5061
                if ( empty( $related_pod ) )
5062
                    continue;
5063
5064
                $related_from = $this->lookup_related_items_from( $related_field[ 'id' ], $related_pod[ 'id' ], $id, $related_field, $related_pod );
5065
5066
                $this->delete_relationships( $related_from, $id, $related_pod, $related_field );
5067
            }
5068
        }
5069
5070
        if ( !empty( $pod ) && !pods_tableless() ) {
5071
            pods_query( "
5072
                DELETE FROM `@wp_podsrel`
5073
                WHERE
5074
                (
5075
                    `pod_id` = %d
5076
                    AND `item_id` = %d
5077
                )
5078
                OR (
5079
                    `related_pod_id` = %d
5080
                    AND `related_item_id` = %d
5081
                )
5082
            ", array(
5083
                $pod[ 'id' ],
5084
                $id,
5085
5086
                $pod[ 'id' ],
5087
                $id
5088
            ) );
5089
        }
5090
5091
        return true;
5092
    }
5093
5094
    /**
5095
     * Delete relationships
5096
     *
5097
     * @param int|array $related_id IDs for items to save
5098
     * @param int|array $id ID or IDs to remove
5099
     * @param array $related_pod Pod data
5100
     * @param array $related_field Field data
5101
     *
5102
     * @return void
5103
     *
5104
     * @since 2.3
5105
     */
5106
    public function delete_relationships ( $related_id, $id, $related_pod, $related_field ) {
5107
        if ( is_array( $related_id ) ) {
5108
            foreach ( $related_id as $rid ) {
5109
                $this->delete_relationships( $rid, $id, $related_pod, $related_field );
5110
            }
5111
5112
            return;
5113
        }
5114
5115
        if ( is_array( $id ) ) {
5116
            foreach ( $id as $rid ) {
5117
                $this->delete_relationships( $related_id, $rid, $related_pod, $related_field );
5118
            }
5119
5120
            return;
5121
        }
5122
5123
        $id = (int) $id;
5124
5125
        if ( empty( $id ) )
5126
            return;
5127
5128
        $related_ids = $this->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod );
5129
5130
        if ( empty( $related_ids ) )
5131
            return;
5132
        elseif ( !in_array( $id, $related_ids ) )
5133
            return;
5134
5135
	    if ( isset( self::$related_item_cache[ $related_pod[ 'id' ] ][ $related_field[ 'id' ] ] ) ) {
5136
		    // Delete relationship from cache
5137
		    unset( self::$related_item_cache[ $related_pod[ 'id' ] ][ $related_field[ 'id' ] ] );
5138
	    }
5139
        unset( $related_ids[ array_search( $id, $related_ids ) ] );
5140
5141
        $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] );
5142
5143
        if ( !$no_conflict )
5144
            pods_no_conflict_on( $related_pod[ 'type' ] );
5145
5146
        // Post Types, Media, Users, and Comments (meta-based)
5147 View Code Duplication
        if ( in_array( $related_pod[ 'type' ], array( 'post_type', 'media', 'taxonomy', 'user', 'comment' ) ) ) {
5148
            $object_type = $related_pod[ 'type' ];
5149
5150
            if ( in_array( $object_type, array( 'post_type', 'media' ) ) )
5151
                $object_type = 'post';
5152
            elseif ( 'taxonomy' == $object_type )
5153
                $object_type = 'term';
5154
5155
            delete_metadata( $object_type, $related_id, $related_field[ 'name' ] );
5156
5157
            if ( !empty( $related_ids ) ) {
5158
                update_metadata( $object_type, $related_id, '_pods_' . $related_field[ 'name' ], $related_ids );
5159
5160
                foreach ( $related_ids as $rel_id ) {
5161
                    add_metadata( $object_type, $related_id, $related_field[ 'name' ], $rel_id );
5162
                }
5163
            }
5164
            else
5165
                delete_metadata( $object_type, $related_id, '_pods_' . $related_field[ 'name' ] );
5166
        }
5167
        // Custom Settings Pages (options-based)
5168
        elseif ( 'settings' == $related_pod[ 'type' ] ) {
5169
            if ( !empty( $related_ids ) )
5170
                update_option( $related_pod[ 'name' ] . '_' . $related_field[ 'name' ], $related_ids );
5171
            else
5172
                delete_option( $related_pod[ 'name' ] . '_' . $related_field[ 'name' ] );
5173
        }
5174
5175
        // Relationships table
5176
        if ( !pods_tableless() ) {
5177
            pods_query( "
5178
                DELETE FROM `@wp_podsrel`
5179
                WHERE
5180
                (
5181
                    `pod_id` = %d
5182
                    AND `field_id` = %d
5183
                    AND `item_id` = %d
5184
                    AND `related_item_id` = %d
5185
                )
5186
                OR (
5187
                    `related_pod_id` = %d
5188
                    AND `related_field_id` = %d
5189
                    AND `related_item_id` = %d
5190
                    AND `item_id` = %d
5191
                )
5192
            ", array(
5193
                $related_pod[ 'id' ],
5194
                $related_field[ 'id' ],
5195
                $related_id,
5196
                $id,
5197
5198
                $related_pod[ 'id' ],
5199
                $related_field[ 'id' ],
5200
                $related_id,
5201
                $id
5202
            ) );
5203
        }
5204
5205
        if ( !$no_conflict )
5206
            pods_no_conflict_off( $related_pod[ 'type' ] );
5207
    }
5208
5209
    /**
5210
     * Check if a Pod exists
5211
     *
5212
     * $params['id'] int Pod ID
5213
     * $params['name'] string Pod name
5214
     *
5215
     * @param array $params An associative array of parameters
5216
     *
5217
     * @return bool True if exists
5218
     *
5219
     * @since 1.12
5220
     */
5221
    public function pod_exists ( $params, $type = null ) {
5222
        if ( is_string( $params ) )
5223
            $params = array( 'name' => $params );
5224
5225
        $params = (object) pods_sanitize( $params );
5226
5227
        if ( !empty( $params->id ) || !empty( $params->name ) ) {
5228 View Code Duplication
            if ( !isset( $params->name ) )
5229
                $pod = get_post( $dummy = (int) $params->id );
5230
            else {
5231
                $pod = get_posts( array(
5232
                    'name' => $params->name,
5233
                    'post_type' => '_pods_pod',
5234
                    'posts_per_page' => 1
5235
                ) );
5236
            }
5237
5238
            if ( !empty( $pod ) && ( empty( $type ) || $type == get_post_meta( $pod->ID, 'type', true ) ) )
5239
                return true;
5240
        }
5241
5242
        return false;
5243
    }
5244
5245
    /**
5246
     * Load a Pod and all of its fields
5247
     *
5248
     * $params['id'] int The Pod ID
5249
     * $params['name'] string The Pod name
5250
     * $params['fields'] bool Whether to load fields (default is true)
5251
     *
5252
     * @param array|object $params An associative array of parameters or pod name as a string
5253
     * @param bool $strict Makes sure the pod exists, throws an error if it doesn't work
5254
     *
5255
     * @return array|bool|mixed|void
5256
     * @since 1.7.9
5257
     */
5258
    public function load_pod ( $params, $strict = true ) {
5259
5260
        /**
5261
         * @var $sitepress SitePress
5262
		 * @var $wpdb wpdb
5263
         */
5264
        global $sitepress, $icl_adjust_id_url_filter_off, $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...
5265
5266
        $current_language = false;
5267
        $load_fields = true;
5268
5269
        // WPML support
5270 View Code Duplication
        if ( is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5271
            $current_language = pods_sanitize( ICL_LANGUAGE_CODE );
5272
        // Polylang support
5273
        elseif ( function_exists( 'pll_current_language' ) )
5274
            $current_language = pll_current_language( 'slug' );
5275
5276
        if ( !is_array( $params ) && !is_object( $params ) )
5277
            $params = array( 'name' => $params, 'table_info' => false, 'fields' => true );
5278
5279
        if ( is_object( $params ) && isset( $params->fields ) && !$params->fields )
5280
            $load_fields = false;
5281
        elseif ( is_array( $params ) && isset( $params[ 'fields' ] ) && !$params[ 'fields' ] )
5282
            $load_fields = false;
5283
5284
	    $table_info = false;
5285
5286
        if ( is_object( $params ) && ! empty( $params->table_info ) )
5287
            $table_info = true;
5288
        elseif ( is_array( $params ) && ! empty( $params[ 'table_info' ] ) )
5289
            $table_info = true;
5290
5291
        $transient = 'pods_' . $wpdb->prefix . '_pod';
5292
5293
        if ( !empty( $current_language ) )
5294
            $transient .= '_' . $current_language;
5295
5296
        if ( !$load_fields )
5297
            $transient .= '_nofields';
5298
5299
        if ( $table_info )
5300
            $transient .= '_tableinfo';
5301
5302
        if ( is_object( $params ) && isset( $params->post_name ) ) {
5303
            $pod = false;
5304
5305
            if ( pods_api_cache() )
5306
                $pod = pods_transient_get( $transient . '_' . $params->post_name );
5307
5308 View Code Duplication
            if ( false !== $pod && ( ! $table_info || isset( $pod[ 'table' ] ) ) ) {
5309
                if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5310
                    $pod = array_merge( $pod, $this->get_table_info( $pod[ 'type' ], $pod[ 'object' ], $pod[ 'name' ], $pod ) );
5311
5312
                return $pod;
5313
            }
5314
5315
            $_pod = get_object_vars( $params );
5316
        }
5317
        else {
5318
            $params = (object) pods_sanitize( $params );
5319
5320 View Code Duplication
            if ( ( !isset( $params->id ) || empty( $params->id ) ) && ( !isset( $params->name ) || empty( $params->name ) ) ) {
5321
                if ( $strict )
5322
                    return pods_error( 'Either Pod ID or Name are required', $this );
5323
5324
                return false;
5325
            }
5326
5327
            if ( isset( $params->name ) ) {
5328
                $pod = false;
5329
5330
				if ( '_pods_pod' == $params->name ) {
5331
					$pod = array(
5332
						'id' => 0,
5333
						'name' => $params->name,
5334
						'label' => __( 'Pods', 'pods' ),
5335
						'type' => 'post_type',
5336
						'storage' => 'meta',
5337
						'options' => array(
5338
							'label_singular' => __( 'Pod', 'pods' )
5339
						),
5340
						'fields' => array()
5341
					);
5342
				}
5343
				elseif ( '_pods_field' == $params->name ) {
5344
					$pod = array(
5345
						'id' => 0,
5346
						'name' => $params->name,
5347
						'label' => __( 'Pod Fields', 'pods' ),
5348
						'type' => 'post_type',
5349
						'storage' => 'meta',
5350
						'options' => array(
5351
							'label_singular' => __( 'Pod Field', 'pods' )
5352
						),
5353
						'fields' => array()
5354
					);
5355
				}
5356
                elseif ( pods_api_cache() )
5357
                    $pod = pods_transient_get( $transient . '_' . $params->name );
5358
5359 View Code Duplication
                if ( false !== $pod && ( ! $table_info || isset( $pod[ 'table' ] ) ) ) {
5360
                    if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5361
                        $pod = array_merge( $pod, $this->get_table_info( $pod[ 'type' ], $pod[ 'object' ], $pod[ 'name' ], $pod ) );
5362
5363
                    return $pod;
5364
                }
5365
            }
5366
5367 View Code Duplication
            if ( !isset( $params->name ) )
5368
                $pod = get_post( $dummy = (int) $params->id );
5369
            else {
5370
                $pod = get_posts( array(
5371
                    'name' => $params->name,
5372
                    'post_type' => '_pods_pod',
5373
                    'posts_per_page' => 1
5374
                ) );
5375
            }
5376
5377
            if ( empty( $pod ) ) {
5378
                if ( $strict )
5379
                    return pods_error( __( 'Pod not found', 'pods' ), $this );
5380
5381
                return false;
5382
            }
5383
5384
            if ( is_array( $pod ) )
5385
                $pod = $pod[ 0 ];
5386
5387
            $_pod = get_object_vars( $pod );
5388
        }
5389
5390
        $pod = false;
5391
5392
        if ( pods_api_cache() )
5393
            $pod = pods_transient_get( $transient . '_' . $_pod[ 'post_name' ] );
5394
5395 View Code Duplication
        if ( false !== $pod && ( ! $table_info || isset( $pod[ 'table' ] ) ) ) {
5396
            if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5397
                $pod = array_merge( $pod, $this->get_table_info( $pod[ 'type' ], $pod[ 'object' ], $pod[ 'name' ], $pod ) );
5398
5399
            return $pod;
5400
        }
5401
5402
        $pod = array(
5403
            'id' => $_pod[ 'ID' ],
5404
            'name' => $_pod[ 'post_name' ],
5405
            'label' => $_pod[ 'post_title' ],
5406
            'description' => $_pod[ 'post_content' ]
5407
        );
5408
5409
        if ( strlen( $pod[ 'label' ] ) < 1 )
5410
            $pod[ 'label' ] = $pod[ 'name' ];
5411
5412
        // @todo update with a method to put all options in
5413
        $defaults = array(
5414
            'show_in_menu' => 1,
5415
            'type' => 'post_type',
5416
            'storage' => 'meta',
5417
            'object' => '',
5418
            'alias' => ''
5419
        );
5420
5421
        $pod[ 'options' ] = get_post_meta( $pod[ 'id' ] );
5422
5423 View Code Duplication
        foreach ( $pod[ 'options' ] as $option => $value ) {
5424
            if ( is_array( $value ) ) {
5425
                foreach ( $value as $k => $v ) {
5426
                    if ( !is_array( $v ) )
5427
                        $value[ $k ] = maybe_unserialize( $v );
5428
                }
5429
5430
                if ( 1 == count( $value ) )
5431
                    $value = current( $value );
5432
            }
5433
            else
5434
                $value = maybe_unserialize( $value );
5435
5436
            $pod[ 'options' ][ $option ] = $value;
5437
        }
5438
5439
        $pod[ 'options' ] = array_merge( $defaults, $pod[ 'options' ] );
5440
5441
        $pod[ 'type' ] = $pod[ 'options' ][ 'type' ];
5442
        $pod[ 'storage' ] = $pod[ 'options' ][ 'storage' ];
5443
        $pod[ 'object' ] = $pod[ 'options' ][ 'object' ];
5444
        $pod[ 'alias' ] = $pod[ 'options' ][ 'alias' ];
5445
5446
        unset( $pod[ 'options' ][ 'type' ] );
5447
        unset( $pod[ 'options' ][ 'storage' ] );
5448
        unset( $pod[ 'options' ][ 'object' ] );
5449
        unset( $pod[ 'options' ][ 'alias' ] );
5450
5451
        if ( $table_info )
5452
            $pod = array_merge( $this->get_table_info( $pod[ 'type' ], $pod[ 'object' ], $pod[ 'name' ], $pod ), $pod );
5453
5454
        // Override old 'none' storage type
5455 View Code Duplication
        if ( 'taxonomy' == $pod['type'] && 'none' == $pod['storage'] && function_exists( 'get_term_meta' ) ) {
5456
            $pod[ 'storage' ] = 'meta';
5457
        }
5458
5459
        if ( isset( $pod[ 'pod' ] ) )
5460
            unset( $pod[ 'pod' ] );
5461
5462
        $pod[ 'fields' ] = array();
5463
5464
        $pod[ 'object_fields' ] = array();
5465
5466
        if ( 'pod' != $pod[ 'type' ] )
5467
            $pod[ 'object_fields' ] = $this->get_wp_object_fields( $pod[ 'type' ], $pod );
5468
5469
        $fields = get_posts( array(
5470
            'post_type' => '_pods_field',
5471
            'posts_per_page' => -1,
5472
            'nopaging' => true,
5473
            'post_parent' => $pod[ 'id' ],
5474
            'orderby' => 'menu_order',
5475
            'order' => 'ASC'
5476
        ) );
5477
5478
        if ( !empty( $fields ) ) {
5479
            foreach ( $fields as $field ) {
5480
                $field->pod = $pod[ 'name' ];
5481
                $field->table_info = $table_info;
5482
5483
                if ( $load_fields ) {
5484
                    $field = $this->load_field( $field );
5485
5486
                    $field = PodsForm::field_setup( $field, null, $field[ 'type' ] );
5487
                }
5488
                else {
5489
                    $field = array(
5490
                        'id' => $field->ID,
5491
                        'name' => $field->post_name,
5492
                        'label' => $field->post_title,
5493
                        'type' => get_post_meta( $field->ID, 'type', true )
5494
                    );
5495
                }
5496
5497
                $pod[ 'fields' ][ $field[ 'name' ] ] = $field;
5498
            }
5499
        }
5500
5501 View Code Duplication
        if ( did_action( 'init' ) && pods_api_cache() )
5502
            pods_transient_set( $transient . '_' . $pod[ 'name' ], $pod );
5503
5504
        return $pod;
5505
    }
5506
5507
    /**
5508
     * Load a list of Pods based on filters specified.
5509
     *
5510
     * $params['type'] string/array Pod Type(s) to filter by
5511
     * $params['object'] string/array Pod Object(s) to filter by
5512
     * $params['options'] array Pod Option(s) key=>value array to filter by
5513
     * $params['orderby'] string ORDER BY clause of query
5514
     * $params['limit'] string Number of Pods to return
5515
     * $params['where'] string WHERE clause of query
5516
     * $params['ids'] string|array IDs of Objects
5517
     * $params['count'] boolean Return only a count of Pods
5518
     * $params['names'] boolean Return only an array of name => label
5519
     * $params['ids'] boolean Return only an array of ID => label
5520
     * $params['fields'] boolean Return pod fields with Pods (default is true)
5521
     * $params['key_names'] boolean Return pods keyed by name
5522
     *
5523
     * @param array $params An associative array of parameters
5524
     *
5525
     * @return array|mixed
5526
     *
5527
     * @uses PodsAPI::load_pod
5528
     *
5529
     * @since 2.0
5530
     */
5531
    public function load_pods ( $params = null ) {
5532
5533
        /**
5534
         * @var $sitepress SitePress
5535
         */
5536
        global $sitepress, $icl_adjust_id_url_filter_off;
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...
5537
5538
        $current_language = false;
5539
5540
        // WPML support
5541 View Code Duplication
        if ( is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5542
            $current_language = pods_sanitize( ICL_LANGUAGE_CODE );
5543
        // Polylang support
5544
        elseif ( function_exists( 'pll_current_language' ) )
5545
            $current_language = pll_current_language( 'slug' );
5546
5547
        $params = (object) pods_sanitize( $params );
5548
5549
        $order = 'ASC';
5550
        $orderby = 'menu_order title';
5551
        $limit = -1;
5552
        $ids = false;
5553
5554
        $meta_query = array();
5555
        $cache_key = '';
5556
5557
        if ( isset( $params->type ) && !empty( $params->type ) ) {
5558
            if ( !is_array( $params->type ) )
5559
                $params->type = array( trim( $params->type ) );
5560
5561
            sort( $params->type );
5562
5563
            $meta_query[] = array(
5564
                'key' => 'type',
5565
                'value' => $params->type,
5566
                'compare' => 'IN'
5567
            );
5568
5569
            if ( 0 < count( $params->type ) )
5570
                $cache_key .= '_type_' . trim( implode( '_', $params->type ) );
5571
        }
5572
5573
        if ( isset( $params->object ) && !empty( $params->object ) ) {
5574
            if ( !is_array( $params->object ) )
5575
                $params->object = array( $params->object );
5576
5577
            $params->object = pods_trim( $params->object );
5578
5579
            sort( $params->object );
5580
5581
            $meta_query[] = array(
5582
                'key' => 'object',
5583
                'value' => $params->object,
5584
                'compare' => 'IN'
5585
            );
5586
5587
            if ( 1 == count( $params->object ) )
5588
                $cache_key .= '_object_' . trim( implode( '', $params->object ) );
5589
        }
5590
5591 View Code Duplication
        if ( isset( $params->options ) && !empty( $params->options ) && is_array( $params->options ) ) {
5592
            foreach ( $params->options as $option => $value ) {
5593
                if ( !is_array( $value ) )
5594
                    $value = array( $value );
5595
5596
                $value = pods_trim( $value );
5597
5598
                sort( $value );
5599
5600
                $meta_query[] = array(
5601
                    'key' => $option,
5602
                    'value' => pods_sanitize( $value ),
5603
                    'compare' => 'IN'
5604
                );
5605
            }
5606
5607
            $cache_key = '';
5608
        }
5609
5610
        if ( isset( $params->where ) && is_array( $params->where ) )
5611
            $meta_query = array_merge( $meta_query, (array) $params->where );
5612
5613 View Code Duplication
        if ( isset( $params->order ) && !empty( $params->order ) && in_array( strtoupper( $params->order ), array( 'ASC', 'DESC' ) ) )
5614
            $order = strtoupper( $params->order );
5615
5616
        if ( isset( $params->orderby ) && !empty( $params->orderby ) )
5617
            $orderby = strtoupper( $params->orderby );
5618
5619
        if ( isset( $params->limit ) && !empty( $params->limit ) )
5620
            $limit = pods_absint( $params->limit );
5621
5622
        if ( isset( $params->ids ) && !empty( $params->ids ) ) {
5623
            $ids = $params->ids;
5624
5625
            if ( !is_array( $ids ) )
5626
                $ids = explode( ',', $ids );
5627
        }
5628
5629
        if ( empty( $ids ) )
5630
            $ids = false;
5631
5632
        $pre_key = '';
5633
5634
        if ( !empty( $current_language ) )
5635
            $pre_key .= '_' . $current_language;
5636
5637
        if ( isset( $params->count ) && $params->count )
5638
            $pre_key .= '_count';
5639
5640
        if ( isset( $params->ids ) && $params->ids && !empty( $ids ) )
5641
            $pre_key .= '_ids_' . implode( '_', $ids );
5642
5643
        if ( isset( $params->names ) && $params->names )
5644
            $pre_key .= '_names';
5645
        elseif ( isset( $params->names_ids ) && $params->names_ids )
5646
            $pre_key .= '_names_ids';
5647
5648
        if ( isset( $params->key_names ) && $params->key_names )
5649
            $pre_key .= '_namekeys';
5650
5651
        if ( isset( $params->fields ) && !$params->fields )
5652
            $pre_key .= '_nofields';
5653
5654
        if ( isset( $params->table_info ) && $params->table_info )
5655
            $pre_key .= '_tableinfo';
5656
5657
5658
        $pre_key .= '_get';
5659
5660
        if ( empty( $cache_key ) )
5661
            $cache_key = 'pods' . $pre_key . '_all';
5662
        else
5663
            $cache_key = 'pods' . $pre_key . $cache_key;
5664
5665
        if ( pods_api_cache() && !empty( $cache_key ) && ( 'pods' . ( !empty( $current_language ) ? '_' . $current_language : '' ) . '_get_all' != $cache_key || empty( $meta_query ) ) && $limit < 1 && ( empty( $orderby ) || 'menu_order title' == $orderby ) && empty( $ids ) ) {
5666
            $the_pods = pods_transient_get( $cache_key );
5667
5668
            if ( false === $the_pods )
5669
                $the_pods = pods_cache_get( $cache_key, 'pods' );
5670
5671
            if ( !is_array( $the_pods ) && 'none' == $the_pods )
5672
                return array();
5673
            elseif ( false !== $the_pods )
5674
                return $the_pods;
5675
        }
5676
5677
        $the_pods = array();
5678
5679
		$args = array(
5680
			'post_type' => '_pods_pod',
5681
			'nopaging' => true,
5682
			'posts_per_page' => $limit,
5683
			'order' => $order,
5684
			'orderby' => $orderby,
5685
			'meta_query' => $meta_query,
5686
		);
5687
5688
		// Only set post__in if there are ids to filter (see https://core.trac.wordpress.org/ticket/28099)
5689
		if ( false != $ids ) {
5690
			$args[ 'post__in' ] = $ids;
5691
		}
5692
5693
		$_pods = get_posts( $args );
5694
5695
        $export_ignore = array(
5696
            'object_type',
5697
            'object_name',
5698
            'table',
5699
            'meta_table',
5700
            'pod_table',
5701
            'field_id',
5702
            'field_index',
5703
            'field_slug',
5704
            'field_type',
5705
            'field_parent',
5706
            'field_parent_select',
5707
            'meta_field_id',
5708
            'meta_field_index',
5709
            'meta_field_value',
5710
            'pod_field_id',
5711
            'pod_field_index',
5712
            'object_fields',
5713
            'join',
5714
            'where',
5715
            'where_default',
5716
            'orderby',
5717
            'pod',
5718
            'recurse',
5719
            'table_info',
5720
            'attributes',
5721
            'group',
5722
            'grouped',
5723
            'developer_mode',
5724
            'dependency',
5725
            'depends-on',
5726
            'excludes-on'
5727
        );
5728
5729
        $total_fields = 0;
5730
5731
        if ( isset( $params->count ) && $params->count )
5732
            $the_pods = count( $_pods );
5733
        else {
5734
            foreach ( $_pods as $pod ) {
5735
                if ( isset( $params->names ) && $params->names )
5736
                    $the_pods[ $pod->post_name ] = $pod->post_title;
5737
                elseif ( isset( $params->names_ids ) && $params->names_ids )
5738
                    $the_pods[ $pod->ID ] = $pod->post_name;
5739
                else {
5740
                    if ( isset( $params->fields ) && !$params->fields )
5741
                        $pod->fields = false;
5742
5743
                    $pod = $this->load_pod( $pod );
5744
5745
                    // Remove extra data not needed
5746
                    if ( pods_var( 'export', $params, false ) && ( !isset( $params->fields ) || $params->fields ) ) {
5747
                        foreach ( $export_ignore as $ignore ) {
5748
                            if ( isset( $pod[ $ignore ] ) )
5749
                                unset( $pod[ $ignore ] );
5750
                        }
5751
5752
                        foreach ( $pod[ 'fields' ] as $field => $field_data ) {
5753
                            if ( isset( $pod[ 'fields' ][ $field ][ 'table_info' ] ) )
5754
                                unset( $pod[ 'fields' ][ $field ][ 'table_info' ] );
5755
                        }
5756
                    }
5757
5758
                    $total_fields += count( $pod[ 'fields' ] );
5759
5760
                    if ( isset( $params->key_names ) && $params->key_names )
5761
                        $the_pods[ $pod[ 'name' ] ] = $pod;
5762
                    else
5763
                        $the_pods[ $pod[ 'id' ] ] = $pod;
5764
                }
5765
            }
5766
        }
5767
5768
        if ( ( !function_exists( 'pll_current_language' ) || ( isset( $params->refresh ) && $params->refresh ) ) && !empty( $cache_key ) && ( 'pods' != $cache_key || empty( $meta_query ) ) && $limit < 1 && ( empty( $orderby ) || 'menu_order title' == $orderby ) && empty( $ids ) ) {
5769
            // Too many Pods can cause issues with the DB when caching is not enabled
5770
            if ( 15 < count( $the_pods ) || 75 < count( $total_fields ) ) {
5771
                pods_transient_clear( $cache_key );
5772
5773 View Code Duplication
                if ( pods_api_cache() ) {
5774
                    if ( empty( $the_pods ) && ( !isset( $params->count ) || !$params->count ) )
5775
                        pods_cache_set( $cache_key, 'none', 'pods' );
5776
                    else
5777
                        pods_cache_set( $cache_key, $the_pods, 'pods' );
5778
                }
5779
            }
5780
            else {
5781
                pods_cache_clear( $cache_key, 'pods' );
5782
5783 View Code Duplication
                if ( pods_api_cache() ) {
5784
                    if ( empty( $the_pods ) && ( !isset( $params->count ) || !$params->count ) )
5785
                        pods_transient_set( $cache_key, 'none' );
5786
                    else
5787
                        pods_transient_set( $cache_key, $the_pods );
5788
                }
5789
            }
5790
        }
5791
5792
        return $the_pods;
5793
    }
5794
5795
    /**
5796
     * Check if a Pod's field exists
5797
     *
5798
     * $params['pod_id'] int The Pod ID
5799
     * $params['id'] int The field ID
5800
     * $params['name'] string The field name
5801
     *
5802
     * @param array $params An associative array of parameters
5803
     *
5804
     * @return bool
5805
     *
5806
     * @since 1.12
5807
     */
5808
    public function field_exists ( $params ) {
5809
        $params = (object) pods_sanitize( $params );
5810
5811
        if ( ( !empty( $params->id ) || !empty( $params->name ) ) && isset( $params->pod_id ) && !empty( $params->pod_id ) ) {
5812 View Code Duplication
            if ( !isset( $params->name ) )
5813
                $field = get_post( $dummy = (int) $params->id );
5814
            else {
5815
                $field = get_posts( array(
5816
                    'name' => $params->name,
5817
                    'post_type' => '_pods_field',
5818
                    'posts_per_page' => 1,
5819
                    'post_parent' => $params->pod_id
5820
                ) );
5821
            }
5822
5823
            if ( !empty( $field ) )
5824
                return true;
5825
        }
5826
5827
        return false;
5828
    }
5829
5830
    /**
5831
     * Load a field
5832
     *
5833
     * $params['pod_id'] int The Pod ID
5834
     * $params['pod'] string The Pod name
5835
     * $params['id'] int The field ID
5836
     * $params['name'] string The field name
5837
     * $params['table_info'] boolean Whether to lookup a pick field's table info
5838
     *
5839
     * @param array $params An associative array of parameters
5840
     * @param boolean $strict Whether to require a field exist or not when loading the info
5841
     *
5842
     * @return array|bool Array with field data, false if field not found
5843
     * @since 1.7.9
5844
     */
5845
    public function load_field ( $params, $strict = false ) {
5846
        $params = (object) $params;
5847
5848
        if ( !isset( $params->table_info ) )
5849
            $params->table_info = false;
5850
5851
        $pod = array();
5852
        $field = array();
5853
5854
        if ( isset( $params->post_title ) )
5855
            $_field = $params;
5856
        elseif ( isset( $params->id ) && !empty( $params->id ) )
5857
            $_field = get_post( $dumb = (int) $params->id );
5858
        else {
5859
            if ( !isset( $params->pod ) )
5860
                $params->pod = '';
5861
5862
            if ( !isset( $params->pod_id ) )
5863
                $params->pod_id = 0;
5864
5865
            if ( isset( $params->pod_data ) )
5866
                $pod = $params->pod_data;
5867 View Code Duplication
            else {
5868
                $pod = $this->load_pod( array( 'name' => $params->pod, 'id' => $params->pod_id, 'table_info' => false ), false );
5869
5870
                if ( false === $pod ) {
5871
                    if ( $strict )
5872
                        return pods_error( __( 'Pod not found', 'pods' ), $this );
5873
5874
                    return false;
5875
                }
5876
            }
5877
5878
            $params->pod_id = $pod[ 'id' ];
5879
            $params->pod = $pod[ 'name' ];
5880
5881 View Code Duplication
            if ( empty( $params->name ) && empty( $params->pod ) && empty( $params->pod_id ) )
5882
                return pods_error( __( 'Either Field Name or Field ID / Pod ID are required', 'pods' ), $this );
5883
5884
            $params->name = pods_clean_name( $params->name, true, ( 'meta' == $pod[ 'storage' ] ? false : true ) );
5885
5886
            if ( isset( $pod[ 'fields' ][ $params->name ] ) && isset( $pod[ 'fields' ][ $params->name ][ 'id' ] ) )
5887
                return $pod[ 'fields' ][ $params->name ];
5888
5889
            $field = false;
5890
5891
            if ( pods_api_cache() )
5892
                $field = pods_transient_get( 'pods_field_' . $params->pod . '_' . $params->name );
5893
5894
            if ( empty( $field ) ) {
5895
                $field = get_posts( array(
5896
                    'name' => $params->name,
5897
                    'post_type' => '_pods_field',
5898
                    'posts_per_page' => 1,
5899
                    'post_parent' => $params->pod_id
5900
                ) );
5901
5902
                if ( empty( $field ) ) {
5903
                    if ( $strict )
5904
                        return pods_error( __( 'Field not found', 'pods' ), $this );
5905
5906
                    return false;
5907
                }
5908
5909
                $_field = $field[ 0 ];
5910
5911
                $field = array();
5912
            }
5913
        }
5914
5915
        if ( empty( $_field ) ) {
5916
            if ( $strict )
5917
                return pods_error( __( 'Field not found', 'pods' ), $this );
5918
5919
            return false;
5920
        }
5921
5922
        $_field = get_object_vars( $_field );
5923
5924
        if ( !isset( $pod[ 'name' ] ) && !isset( $_field[ 'pod' ] ) ) {
5925
            if ( 0 < $_field[ 'post_parent' ] )
5926
                $pod = $this->load_pod( array( 'id' => $_field[ 'post_parent' ], 'table_info' => false ), false );
5927
5928
            if ( empty( $pod ) ) {
5929
                if ( $strict )
5930
                    return pods_error( __( 'Pod for field not found', 'pods' ), $this );
5931
5932
                return false;
5933
            }
5934
        }
5935
5936
        if ( empty( $field ) ) {
5937 View Code Duplication
            if ( pods_api_cache() && ( isset( $pod[ 'name' ] ) || isset( $_field[ 'pod' ] ) ) )
5938
                $field = pods_transient_get( 'pods_field_' . pods_var( 'name', $pod, pods_var( 'pod', $_field ), null, true ) . '_' . $_field[ 'post_name' ] );
5939
5940
            if ( empty( $field ) ) {
5941
                $defaults = array(
5942
                    'type' => 'text'
5943
                );
5944
5945
                $field = array(
5946
                    'id' => $_field[ 'ID' ],
5947
                    'name' => $_field[ 'post_name' ],
5948
                    'label' => $_field[ 'post_title' ],
5949
                    'description' => $_field[ 'post_content' ],
5950
                    'weight' => $_field[ 'menu_order' ],
5951
                    'pod_id' => $_field[ 'post_parent' ],
5952
                    'pick_object' => '',
5953
                    'pick_val' => '',
5954
                    'sister_id' => '',
5955
                    'table_info' => array()
5956
                );
5957
5958
                if ( isset( $pod[ 'name' ] ) )
5959
                    $field[ 'pod' ] = $pod[ 'name' ];
5960
                elseif ( isset( $_field[ 'pod' ] ) )
5961
                    $field[ 'pod' ] = $_field[ 'pod' ];
5962
5963
                $field[ 'options' ] = get_post_meta( $field[ 'id' ] );
5964
5965
                $options_ignore = array(
5966
                    'method',
5967
                    'table_info',
5968
                    'attributes',
5969
                    'group',
5970
                    'grouped',
5971
                    'developer_mode',
5972
                    'dependency',
5973
                    'depends-on',
5974
                    'excludes-on'
5975
                );
5976
5977
                foreach ( $options_ignore as $ignore ) {
5978
                    if ( isset( $field[ 'options' ][ $ignore ] ) )
5979
                        unset( $field[ 'options' ][ $ignore ] );
5980
                }
5981
5982 View Code Duplication
                foreach ( $field[ 'options' ] as $option => $value ) {
5983
                    if ( is_array( $value ) ) {
5984
                        foreach ( $value as $k => $v ) {
5985
                            if ( !is_array( $v ) )
5986
                                $value[ $k ] = maybe_unserialize( $v );
5987
                        }
5988
5989
                        if ( 1 == count( $value ) )
5990
                            $value = current( $value );
5991
                    }
5992
                    else
5993
                        $value = maybe_unserialize( $value );
5994
5995
                    $field[ 'options' ][ $option ] = $value;
5996
                }
5997
5998
                $field[ 'options' ] = array_merge( $defaults, $field[ 'options' ] );
5999
6000
                $field[ 'type' ] = $field[ 'options' ][ 'type' ];
6001
6002
                unset( $field[ 'options' ][ 'type' ] );
6003
6004 View Code Duplication
                if ( isset( $field[ 'options' ][ 'pick_object' ] ) ) {
6005
                    $field[ 'pick_object' ] = $field[ 'options' ][ 'pick_object' ];
6006
6007
                    unset( $field[ 'options' ][ 'pick_object' ] );
6008
                }
6009
6010 View Code Duplication
                if ( isset( $field[ 'options' ][ 'pick_val' ] ) ) {
6011
                    $field[ 'pick_val' ] = $field[ 'options' ][ 'pick_val' ];
6012
6013
                    unset( $field[ 'options' ][ 'pick_val' ] );
6014
                }
6015
6016 View Code Duplication
                if ( isset( $field[ 'options' ][ 'sister_id' ] ) ) {
6017
                    $field[ 'sister_id' ] = $field[ 'options' ][ 'sister_id' ];
6018
6019
                    unset( $field[ 'options' ][ 'sister_id' ] );
6020
                }
6021
6022
                if ( isset( $field[ 'options' ][ 'sister_field_id' ] ) )
6023
                    unset( $field[ 'options' ][ 'sister_field_id' ] );
6024
6025 View Code Duplication
                if ( pods_api_cache() && ( isset( $pod[ 'name' ] ) || isset( $_field[ 'pod' ] ) ) )
6026
                    pods_transient_set( 'pods_field_' . pods_var( 'name', $pod, pods_var( 'pod', $_field ), null, true ) . '_' . $field[ 'name' ], $field );
6027
            }
6028
        }
6029
6030
        $field[ 'table_info' ] = array();
6031
6032
        if ( 'pick' == $field[ 'type' ] && $params->table_info )
6033
            $field[ 'table_info' ] = $this->get_table_info( $field[ 'pick_object' ], $field[ 'pick_val' ], null, null, $field );
6034
6035
        return $field;
6036
    }
6037
6038
    /**
6039
     * Load fields by Pod, ID, Name, and/or Type
6040
     *
6041
     * $params['pod_id'] int The Pod ID
6042
     * $params['pod'] string The Pod name
6043
     * $params['id'] array The field IDs
6044
     * $params['name'] array The field names
6045
     * $params['type'] array The field types
6046
     * $params['options'] array Field Option(s) key=>value array to filter by
6047
     * $params['where'] string WHERE clause of query
6048
     *
6049
     * @param array $params An associative array of parameters
6050
     * @param bool $strict  Whether to require a field exist or not when loading the info
6051
     *
6052
     * @return array Array of field data.
6053
     *
6054
     * @since 1.7.9
6055
     */
6056
    public function load_fields ( $params, $strict = false ) {
6057
		// @todo Get away from using md5/serialize, I'm sure we can cache specific parts
6058
	    $cache_key = md5( serialize( $params  ) );
6059
	    if ( isset( $this->fields_cache[ $cache_key ] ) ) {
6060
		    return $this->fields_cache[ $cache_key ];
6061
	    }
6062
6063
        $params = (object) pods_sanitize( $params );
6064
6065
        if ( !isset( $params->pod ) || empty( $params->pod ) )
6066
            $params->pod = '';
6067
6068
        if ( !isset( $params->pod_id ) || empty( $params->pod_id ) )
6069
            $params->pod_id = 0;
6070
6071 View Code Duplication
        if ( !isset( $params->name ) || empty( $params->name ) )
6072
            $params->name = array();
6073
        else
6074
            $params->name = (array) $params->name;
6075
6076
        if ( !isset( $params->id ) || empty( $params->id ) )
6077
            $params->id = array();
6078
        else {
6079
            $params->id = (array) $params->id;
6080
6081
            foreach ( $params->id as &$id ) {
6082
                $id = pods_absint( $id );
6083
            }
6084
        }
6085
6086 View Code Duplication
        if ( !isset( $params->type ) || empty( $params->type ) )
6087
            $params->type = array();
6088
        else
6089
            $params->type = (array) $params->type;
6090
6091
        $fields = array();
6092
6093
        if ( !empty( $params->pod ) || !empty( $params->pod_id ) ) {
6094
            $pod = $this->load_pod( array( 'name' => $params->pod, 'id' => $params->pod_id, 'table_info' => true ), false );
6095
6096
            if ( false === $pod ) {
6097
                if ( $strict )
6098
                    return pods_error( __( 'Pod not found', 'pods' ), $this );
6099
6100
                return $fields;
6101
            }
6102
6103
			$pod[ 'fields' ] = array_merge( pods_var_raw( 'object_fields', $pod, array() ), $pod[ 'fields' ] );
6104
6105
            foreach ( $pod[ 'fields' ] as $field ) {
6106
                if ( empty( $params->name ) && empty( $params->id ) && empty( $params->type ) )
6107
                    $fields[ $field[ 'name' ] ] = $field;
6108
				elseif ( in_array( $fields[ 'name' ], $params->name ) || in_array( $fields[ 'id' ], $params->id ) || in_array( $fields[ 'type' ], $params->type ) )
6109
                    $fields[ $field[ 'name' ] ] = $field;
6110
            }
6111
        }
6112
        elseif ( ( isset( $params->options ) && !empty( $params->options ) && is_array( $params->options ) ) || ( isset( $params->where ) && !empty( $params->where ) && is_array( $params->where ) ) ) {
6113
            $order = 'ASC';
6114
            $orderby = 'menu_order title';
6115
            $limit = -1;
6116
            $ids = false;
6117
6118
            $meta_query = array();
6119
6120 View Code Duplication
            if ( isset( $params->options ) && !empty( $params->options ) && is_array( $params->options ) ) {
6121
                foreach ( $params->options as $option => $value ) {
6122
                    if ( !is_array( $value ) )
6123
                        $value = array( $value );
6124
6125
                    $value = pods_trim( $value );
6126
6127
                    sort( $value );
6128
6129
                    $meta_query[] = array(
6130
                        'key' => $option,
6131
                        'value' => pods_sanitize( $value ),
6132
                        'compare' => 'IN'
6133
                    );
6134
                }
6135
            }
6136
6137
            if ( isset( $params->where ) && !empty( $params->where ) && is_array( $params->where ) )
6138
                $meta_query = array_merge( $meta_query, (array) $params->where );
6139
6140
			$args = array(
6141
				'post_type' => '_pods_field',
6142
				'nopaging' => true,
6143
				'posts_per_page' => $limit,
6144
				'order' => $order,
6145
				'orderby' => $orderby,
6146
				'meta_query' => $meta_query,
6147
			);
6148
6149
			// Only set post__in if there are ids to filter (see https://core.trac.wordpress.org/ticket/28099)
6150
			if ( false != $ids ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
6151
				$args[ 'post__in' ] = $ids;
6152
			}
6153
6154
            $fields = array();
6155
6156
            $_fields = get_posts( $args );
6157
6158
            foreach ( $_fields as $field ) {
6159
                $field = $this->load_field( $field, false );
6160
6161
                if ( !empty( $field ) )
6162
                    $fields[ $field[ 'id' ] ] = $field;
6163
            }
6164
        }
6165
        else {
6166 View Code Duplication
            if ( empty( $params->name ) && empty( $params->id ) && empty( $params->type ) )
6167
                return pods_error( __( 'Either Field Name / Field ID / Field Type, or Pod Name / Pod ID are required', 'pods' ), $this );
6168
6169
            $lookup = array();
6170
6171 View Code Duplication
            if ( !empty( $params->name ) ) {
6172
                $fields = implode( "', '", $params->name );
6173
6174
                $lookup[] = "`post_name` IN ( '{$fields}' )";
6175
            }
6176
6177 View Code Duplication
            if ( !empty( $params->id ) ) {
6178
                $fields = implode( ", ", $params->id );
6179
6180
                $lookup[] = "`ID` IN ( {$fields} )";
6181
            }
6182
6183
            $lookup = implode( ' AND ', $lookup );
6184
6185
            $result = pods_query( "SELECT `ID`, `post_name`, `post_parent` FROM `@wp_posts` WHERE `post_type` = '_pods_field' AND ( {$lookup} )" );
6186
6187
            $fields = array();
6188
6189
            if ( !empty( $result ) ) {
6190
                foreach ( $result as $field ) {
6191
					$field = $this->load_field( array(
6192
                        'id' => $field->ID,
6193
                        'name' => $field->post_name,
6194
                        'pod_id' => $field->post_parent
6195
                    ), false );
6196
6197
                    if ( !empty( $field ) && ( empty( $params->type ) || in_array( $field[ 'type' ], $params->type ) ) )
6198
                        $fields[ $field[ 'id' ] ] = $field;
6199
                }
6200
            }
6201
        }
6202
	    if ( isset( $cache_key ) ) {
6203
		    $this->fields_cache[ $cache_key ] = $fields;
6204
	    }
6205
        return $fields;
6206
    }
6207
6208
    /**
6209
     * Load a Pods Object
6210
     *
6211
     * $params['id'] int The Object ID
6212
     * $params['name'] string The Object name
6213
     * $params['type'] string The Object type
6214
     *
6215
     * @param array|object $params An associative array of parameters
6216
     * @param bool $strict
6217
     *
6218
     * @return array|bool
6219
     * @since 2.0
6220
     */
6221
    public function load_object ( $params, $strict = false ) {
6222
        if ( is_object( $params ) && isset( $params->post_title ) )
6223
            $_object = get_object_vars( $params );
6224
        else {
6225
            $params = (object) pods_sanitize( $params );
6226
6227 View Code Duplication
            if ( !isset( $params->type ) || empty( $params->type ) )
6228
                return pods_error( __( 'Object type is required', 'pods' ), $this );
6229
6230 View Code Duplication
            if ( ( !isset( $params->id ) || empty( $params->id ) ) && ( !isset( $params->name ) || empty( $params->name ) ) )
6231
                return pods_error( __( 'Either Object ID or Name are required', 'pods' ), $this );
6232
6233
            /**
6234
             * @var $wpdb wpdb
6235
             */
6236
            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...
6237
6238
            if ( isset( $params->name ) )
6239
                $_object = pods_by_title( $params->name, ARRAY_A, '_pods_' . $params->type, 'publish' );
6240
            else {
6241
                $object = $params->id;
6242
6243
                $_object = get_post( $object, ARRAY_A );
6244
            }
6245
6246
            if ( empty( $_object ) ) {
6247
                if ( $strict )
6248
                    return pods_error( __( 'Object not found', 'pods' ), $this );
6249
6250
                return false;
6251
            }
6252
        }
6253
6254
        $object = array(
6255
            'id' => $_object[ 'ID' ],
6256
            'name' => $_object[ 'post_title' ],
6257
            'code' => $_object[ 'post_content' ],
6258
            'type' => str_replace( '_pods_', '', $_object[ 'post_type' ] ),
6259
            'slug' => $_object[ 'post_name' ]
6260
        );
6261
6262
        $object[ 'options' ] = get_post_meta( $object[ 'id' ] );
6263
6264
        foreach ( $object[ 'options' ] as $option => &$value ) {
6265
            if ( is_array( $value ) && 1 == count( $value ) )
6266
                $value = current( $value );
6267
        }
6268
6269
        return $object;
6270
    }
6271
6272
    /**
6273
     * Load Multiple Pods Objects
6274
     *
6275
     * $params['type'] string The Object type
6276
     * $params['options'] array Pod Option(s) key=>value array to filter by
6277
     * $params['orderby'] string ORDER BY clause of query
6278
     * $params['limit'] string Number of objects to return
6279
     * $params['where'] string WHERE clause of query
6280
     * $params['ids'] string|array IDs of Objects
6281
     *
6282
     * @param array|object $params An associative array of parameters
6283
     *
6284
     * @return array
6285
     * @since 2.0
6286
     */
6287
    public function load_objects ( $params ) {
6288
        $params = (object) pods_sanitize( $params );
6289
6290 View Code Duplication
        if ( !isset( $params->type ) || empty( $params->type ) )
6291
            return pods_error( __( 'Pods Object type is required', 'pods' ), $this );
6292
6293
        $order = 'ASC';
6294
        $orderby = 'menu_order';
6295
        $limit = -1;
6296
        $ids = false;
6297
6298
        $meta_query = array();
6299
        $cache_key = '';
6300
6301 View Code Duplication
        if ( isset( $params->options ) && !empty( $params->options ) && is_array( $params->options ) ) {
6302
            foreach ( $params->options as $option => $value ) {
6303
                if ( !is_array( $value ) )
6304
                    $value = array( $value );
6305
6306
                $value = pods_trim( $value );
6307
6308
                sort( $value );
6309
6310
                $meta_query[] = array(
6311
                    'key' => $option,
6312
                    'value' => pods_sanitize( $value ),
6313
                    'compare' => 'IN'
6314
                );
6315
            }
6316
        }
6317
6318
        if ( isset( $params->where ) && is_array( $params->where ) )
6319
            $meta_query = array_merge( $meta_query, (array) $params->where );
6320
6321 View Code Duplication
        if ( isset( $params->order ) && !empty( $params->order ) && in_array( strtoupper( $params->order ), array( 'ASC', 'DESC' ) ) )
6322
            $order = strtoupper( $params->order );
6323
6324
        if ( isset( $params->orderby ) && !empty( $params->orderby ) )
6325
            $orderby = strtoupper( $params->orderby );
6326
6327
        if ( isset( $params->limit ) && !empty( $params->limit ) )
6328
            $limit = pods_absint( $params->limit );
6329
6330
        if ( isset( $params->ids ) && !empty( $params->ids ) ) {
6331
            $ids = $params->ids;
6332
6333
            if ( !is_array( $ids ) )
6334
                $ids = explode( ',', $ids );
6335
        }
6336
6337
        if ( empty( $ids ) )
6338
            $ids = false;
6339
6340
        if ( pods_api_cache() && empty( $meta_query ) && empty( $limit ) && ( empty( $orderby ) || 'menu_order' == $orderby ) && empty( $ids ) ) {
6341
            $cache_key = 'pods_objects_' . $params->type;
6342
6343
            $the_objects = pods_transient_get( $cache_key );
6344
6345
            if ( false !== $the_objects )
6346
                return $the_objects;
6347
        }
6348
6349
        $the_objects = array();
6350
6351
		$args = array(
6352
			'post_type' => '_pods_' . $params->type,
6353
			'nopaging' => true,
6354
			'posts_per_page' => $limit,
6355
			'order' => $order,
6356
			'orderby' => $orderby,
6357
			'meta_query' => $meta_query,
6358
		);
6359
6360
		// Only set post__in if there are ids to filter (see https://core.trac.wordpress.org/ticket/28099)
6361
		if ( false != $ids ) {
6362
			$args[ 'post__in' ] = $ids;
6363
		}
6364
6365
		$objects = get_posts( $args );
6366
6367
        foreach ( $objects as $object ) {
6368
            $object = $this->load_object( $object );
6369
6370
            $the_objects[ $object[ 'name' ] ] = $object;
6371
        }
6372
6373
        if ( pods_api_cache() && !empty( $cache_key ) )
6374
            pods_transient_set( $cache_key, $the_objects );
6375
6376
        return $the_objects;
6377
    }
6378
6379
    /**
6380
     * @see PodsAPI::load_object
6381
     *
6382
     * Load a Pod Template
6383
     *
6384
     * $params['id'] int The template ID
6385
     * $params['name'] string The template name
6386
     *
6387
     * @param array $params An associative array of parameters
6388
     *
6389
     * @return array|bool
6390
     * @since 1.7.9
6391
     */
6392
    public function load_template ( $params ) {
6393
        if ( !class_exists( 'Pods_Templates' ) )
6394
            return false;
6395
6396
        $params = (object) $params;
6397
        $params->type = 'template';
6398
        return $this->load_object( $params );
6399
    }
6400
6401
    /**
6402
     * @see PodsAPI::load_objects
6403
     *
6404
     * Load Multiple Pod Templates
6405
     *
6406
     * $params['where'] string The WHERE clause of query
6407
     * $params['options'] array Pod Option(s) key=>value array to filter by
6408
     * $params['orderby'] string ORDER BY clause of query
6409
     * $params['limit'] string Number of templates to return
6410
     *
6411
     * @param array $params (optional) An associative array of parameters
6412
     *
6413
     * @return array
6414
     *
6415
     * @since 2.0
6416
     */
6417
    public function load_templates ( $params = null ) {
6418
        if ( !class_exists( 'Pods_Templates' ) )
6419
            return array();
6420
6421
        $params = (object) $params;
6422
        $params->type = 'template';
6423
        return $this->load_objects( $params );
6424
    }
6425
6426
    /**
6427
     * @see PodsAPI::load_object
6428
     *
6429
     * Load a Pod Page
6430
     *
6431
     * $params['id'] int The page ID
6432
     * $params['name'] string The page URI
6433
     *
6434
     * @param array $params An associative array of parameters
6435
     *
6436
     * @return array|bool
6437
     *
6438
     * @since 1.7.9
6439
     */
6440
    public function load_page ( $params ) {
6441
        if ( !class_exists( 'Pods_Pages' ) )
6442
            return false;
6443
6444
        $params = (object) $params;
6445 View Code Duplication
        if ( !isset( $params->name ) && isset( $params->uri ) ) {
6446
            $params->name = $params->uri;
6447
            unset( $params->uri );
6448
        }
6449
        $params->type = 'page';
6450
        return $this->load_object( $params );
6451
    }
6452
6453
    /**
6454
     * @see PodsAPI::load_objects
6455
     *
6456
     * Load Multiple Pod Pages
6457
     *
6458
     * $params['where'] string The WHERE clause of query
6459
     * $params['options'] array Pod Option(s) key=>value array to filter by
6460
     * $params['orderby'] string ORDER BY clause of query
6461
     * $params['limit'] string Number of pages to return
6462
     *
6463
     * @param array $params (optional) An associative array of parameters
6464
     *
6465
     * @return array
6466
     *
6467
     * @since 2.0
6468
     */
6469
    public function load_pages ( $params = null ) {
6470
        if ( !class_exists( 'Pods_Pages' ) )
6471
            return array();
6472
6473
        $params = (object) $params;
6474
        $params->type = 'page';
6475
        return $this->load_objects( $params );
6476
    }
6477
6478
    /**
6479
     * @see PodsAPI::load_object
6480
     *
6481
     * Load a Pod Helper
6482
     *
6483
     * $params['id'] int The helper ID
6484
     * $params['name'] string The helper name
6485
     *
6486
     * @param array $params An associative array of parameters
6487
     *
6488
     * @return array|bool
6489
     *
6490
     * @since 1.7.9
6491
     */
6492 View Code Duplication
    public function load_helper ( $params ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
6493
        if ( !class_exists( 'Pods_Helpers' ) )
6494
            return false;
6495
6496
        $params = (object) $params;
6497
        $params->type = 'helper';
6498
        return $this->load_object( $params );
6499
    }
6500
6501
    /**
6502
     * @see PodsAPI::load_objects
6503
     *
6504
     * Load Multiple Pod Helpers
6505
     *
6506
     * $params['where'] string The WHERE clause of query
6507
     * $params['options'] array Pod Option(s) key=>value array to filter by
6508
     * $params['orderby'] string ORDER BY clause of query
6509
     * $params['limit'] string Number of pages to return
6510
     *
6511
     * @param array $params (optional) An associative array of parameters
6512
     *
6513
     * @return array
6514
     *
6515
     * @since 2.0
6516
     */
6517 View Code Duplication
    public function load_helpers ( $params = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
6518
        if ( !class_exists( 'Pods_Helpers' ) )
6519
            return array();
6520
6521
        $params = (object) $params;
6522
        $params->type = 'helper';
6523
        return $this->load_objects( $params );
6524
    }
6525
6526
    /**
6527
     * Load the pod item object
6528
     *
6529
     * $params['pod'] string The datatype name
6530
     * $params['id'] int (optional) The item's ID
6531
     *
6532
     * @param array $params An associative array of parameters
6533
     *
6534
     * @return bool|\Pods
6535
     *
6536
     * @uses pods()
6537
     *
6538
     * @since 2.0
6539
     */
6540
    public function load_pod_item ( $params ) {
6541
        $params = (object) pods_sanitize( $params );
6542
6543 View Code Duplication
        if ( !isset( $params->pod ) || empty( $params->pod ) )
6544
            return pods_error( __( 'Pod name required', 'pods' ), $this );
6545 View Code Duplication
        if ( !isset( $params->id ) || empty( $params->id ) )
6546
            return pods_error( __( 'Item ID required', 'pods' ), $this );
6547
6548
        $pod = false;
6549
6550
        if ( pods_api_cache() )
6551
            $pod = pods_cache_get( $params->id, 'pods_item_object_' . $params->pod );
6552
6553
        if ( false !== $pod )
6554
            return $pod;
6555
6556
        $pod = pods( $params->pod, $params->id );
6557
6558
        if ( pods_api_cache() )
6559
            pods_cache_set( $params->id, $pod, 'pods_item_object_' . $params->pod );
6560
6561
        return $pod;
6562
    }
6563
6564
    /**
6565
     * Load potential sister fields for a specific field
6566
     *
6567
     * $params['pod'] int The Pod name
6568
     * $params['related_pod'] string The related Pod name
6569
     *
6570
     * @param array $params An associative array of parameters
6571
     * @param array $pod (optional) Array of Pod data to use (to avoid lookup)
6572
     *
6573
     * @return array|bool
6574
     *
6575
     * @since 1.7.9
6576
     *
6577
     * @uses PodsAPI::load_pod
6578
     */
6579
    public function load_sister_fields ( $params, $pod = null ) {
6580
        $params = (object) pods_sanitize( $params );
6581
6582 View Code Duplication
        if ( empty( $pod ) ) {
6583
            $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => false ), false );
6584
6585
            if ( false === $pod )
6586
                return pods_error( __( 'Pod not found', 'pods' ), $this );
6587
        }
6588
6589
        $params->pod_id = $pod[ 'id' ];
6590
        $params->pod = $pod[ 'name' ];
6591
6592
        $type = false;
6593
6594
        if ( 0 === strpos( $params->related_pod, 'pod-' ) ) {
6595
            $params->related_pod = pods_str_replace( 'pod-', '', $params->related_pod, 1 );
6596
            $type = 'pod';
6597
        }
6598 View Code Duplication
        elseif ( 0 === strpos( $params->related_pod, 'post_type-' ) ) {
6599
            $params->related_pod = pods_str_replace( 'post_type-', '', $params->related_pod, 1 );
6600
            $type = 'post_type';
6601
        }
6602 View Code Duplication
        elseif ( 0 === strpos( $params->related_pod, 'taxonomy-' ) ) {
6603
            $params->related_pod = pods_str_replace( 'taxonomy-', '', $params->related_pod, 1 );
6604
            $type = 'taxonomy';
6605
        }
6606
6607
        $related_pod = $this->load_pod( array( 'name' => $params->related_pod, 'table_info' => false ), false );
6608
6609
        if ( false === $related_pod || ( false !== $type && 'pod' != $type && $type != $related_pod[ 'type' ] ) )
6610
            return pods_error( __( 'Related Pod not found', 'pods' ), $this );
6611
6612
        $params->related_pod_id = $related_pod[ 'id' ];
6613
        $params->related_pod = $related_pod[ 'name' ];
6614
6615
        $sister_fields = array();
6616
6617
        foreach ( $related_pod[ 'fields' ] as $field ) {
6618
            if ( 'pick' == $field[ 'type' ] && in_array( $field[ 'pick_object' ], array( $pod[ 'type' ], 'pod' ) ) && ( $params->pod == $field[ 'pick_object' ] || $params->pod == $field[ 'pick_val' ] ) )
6619
                $sister_fields[ $field[ 'id' ] ] = esc_html( $field[ 'label' ] . ' (' . $field[ 'name' ] . ')' );
6620
        }
6621
6622
        return $sister_fields;
6623
    }
6624
6625
    /**
6626
     * Takes a sql field such as tinyint and returns the pods field type, such as num.
6627
     *
6628
     * @param string $sql_field The SQL field to look for
6629
     *
6630
     * @return string The field type
6631
     *
6632
     * @since 2.0
6633
     */
6634
    public static function detect_pod_field_from_sql_data_type ( $sql_field ) {
6635
        $sql_field = strtolower( $sql_field );
6636
6637
        $field_to_field_map = array(
6638
            'tinyint' => 'number',
6639
            'smallint' => 'number',
6640
            'mediumint' => 'number',
6641
            'int' => 'number',
6642
            'bigint' => 'number',
6643
            'float' => 'number',
6644
            'double' => 'number',
6645
            'decimal' => 'number',
6646
            'date' => 'date',
6647
            'datetime' => 'datetime',
6648
            'timestamp' => 'datetime',
6649
            'time' => 'time',
6650
            'year' => 'date',
6651
            'varchar' => 'text',
6652
            'text' => 'paragraph',
6653
            'mediumtext' => 'paragraph',
6654
            'longtext' => 'paragraph'
6655
        );
6656
6657
        return ( array_key_exists( $sql_field, $field_to_field_map ) ) ? $field_to_field_map[ $sql_field ] : 'paragraph';
6658
    }
6659
6660
    /**
6661
     * Gets all field types
6662
     *
6663
     * @return array Array of field types
6664
     *
6665
     * @uses PodsForm::field_loader
6666
     *
6667
     * @since 2.0
6668
     * @deprecated
6669
     */
6670
    public function get_field_types () {
6671
        return PodsForm::field_types();
6672
    }
6673
6674
    /**
6675
     * Gets the schema definition of a field.
6676
     *
6677
     * @param string $type Field type to look for
6678
     * @param array $options (optional) Options of the field to pass to the schema function.
6679
     *
6680
     * @return array|bool|mixed|null
6681
     *
6682
     * @since 2.0
6683
     */
6684
    private function get_field_definition ( $type, $options = null ) {
6685
        $definition = PodsForm::field_method( $type, 'schema', $options );
6686
6687
        return $this->do_hook( 'field_definition', $definition, $type, $options );
6688
    }
6689
6690
    /**
6691
     * @see PodsForm:validate
6692
     *
6693
     * Validates the value of a field.
6694
     *
6695
     * @param mixed $value The value to validate
6696
     * @param string $field Field to use for validation
6697
     * @param array $object_fields Fields of the object we're validating
6698
     * @param array $fields Array of all fields data
6699
     * @param array|Pods $pod Array of pod data (or Pods object)
6700
     * @param array|object $params Extra parameters to pass to the validation function of the field.
6701
     *
6702
     * @return array|bool
6703
     *
6704
     * @uses PodsForm::validate
6705
     *
6706
     * @since 2.0
6707
     */
6708
    public function handle_field_validation ( &$value, $field, $object_fields, $fields, $pod, $params ) {
6709
        $tableless_field_types = PodsForm::tableless_field_types();
6710
6711
        $fields = array_merge( $fields, $object_fields );
6712
6713
        $options = $fields[ $field ];
6714
6715
        $id = ( is_object( $params ) ? $params->id : ( is_object( $pod ) ? $pod->id() : 0 ) );
6716
6717
        if ( is_object( $pod ) )
6718
            $pod = $pod->pod_data;
6719
6720
        $type = $options[ 'type' ];
6721
        $label = $options[ 'label' ];
6722
        $label = empty( $label ) ? $field : $label;
6723
6724
        // Verify required fields
6725
        if ( 1 == pods_var( 'required', $options[ 'options' ], 0 ) && 'slug' != $type ) {
6726
            if ( '' == $value || null === $value || array() === $value )
6727
                return pods_error( sprintf( __( '%s is empty', 'pods' ), $label ), $this );
6728
6729
            if ( 'multi' == pods_var( 'pick_format_type', $options[ 'options' ] ) && 'autocomplete' != pods_var( 'pick_format_multi', $options[ 'options' ] ) ) {
6730
                $has_value = false;
6731
6732
                $check_value = (array) $value;
6733
6734
                foreach ( $check_value as $val ) {
6735
                    if ( '' != $val && null !== $val && 0 !== $val && '0' !== $val ) {
6736
                        $has_value = true;
6737
6738
                        continue;
6739
                    }
6740
                }
6741
6742
                if ( !$has_value )
6743
                    return pods_error( sprintf( __( '%s is required', 'pods' ), $label ), $this );
6744
            }
6745
6746
        }
6747
6748
        // @todo move this to after pre-save preparations
6749
        // Verify unique fields
6750
        if ( 1 == pods_var( 'unique', $options[ 'options' ], 0 ) && '' !== $value && null !== $value && array() !== $value ) {
6751
            if ( empty( $pod ) )
6752
                return false;
6753
6754
            if ( !in_array( $type, $tableless_field_types ) ) {
6755
                $exclude = '';
6756
6757
                if ( !empty( $id ) )
6758
                    $exclude = "AND `id` != {$id}";
6759
6760
                $check = false;
6761
6762
                $check_value = pods_sanitize( $value );
6763
6764
                // @todo handle meta-based fields
6765
                // Trigger an error if not unique
6766
                if ( 'table' == $pod[ 'storage' ] )
6767
                    $check = pods_query( "SELECT `id` FROM `@wp_pods_" . $pod[ 'name' ] . "` WHERE `{$field}` = '{$check_value}' {$exclude} LIMIT 1", $this );
6768
6769
                if ( !empty( $check ) )
6770
                    return pods_error( sprintf( __( '%s needs to be unique', 'pods' ), $label ), $this );
6771
            }
6772
            else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
6773
                // @todo handle tableless check
6774
            }
6775
        }
6776
6777
        $validate = PodsForm::validate( $options[ 'type' ], $value, $field, array_merge( $options, pods_var( 'options', $options, array() ) ), $fields, $pod, $id, $params );
6778
6779
        $validate = $this->do_hook( 'field_validation', $validate, $value, $field, $object_fields, $fields, $pod, $params );
6780
6781
        return $validate;
6782
    }
6783
6784
    /**
6785
     * Find items related to a parent field
6786
     *
6787
     * @param int $field_id The Field ID
6788
     * @param int $pod_id The Pod ID
6789
     * @param mixed $ids A comma-separated string (or array) of item IDs
6790
     * @param array $field Field data array
6791
     * @param array $pod Pod data array
6792
     *
6793
     * @return array
6794
     *
6795
     * @since 2.0
6796
     *
6797
     * @uses pods_query()
6798
     */
6799
    public function lookup_related_items ( $field_id, $pod_id, $ids, $field = null, $pod = null ) {
6800
        $related_ids = array();
6801
6802
        if ( !is_array( $ids ) )
6803
            $ids = explode( ',', $ids );
6804
6805
        foreach ( $ids as $k => $id ) {
6806
            $ids[ $k ] = (int) $id;
6807
        }
6808
6809
        $ids = array_unique( array_filter( $ids ) );
6810
6811
	    $idstring = implode( ',', $ids );
6812
	    if ( 0 != $pod_id && 0 != $field_id && isset( self::$related_item_cache[ $pod_id ][ $field_id ][ $idstring ] ) ) {
6813
		    // Check cache first, no point in running the same query multiple times
6814
		    return self::$related_item_cache[ $pod_id ][ $field_id ][ $idstring ];
6815
	    }
6816
6817
        $tableless_field_types = PodsForm::tableless_field_types();
6818
6819
		$field_type = pods_v( 'type', $field );
6820
6821
        if ( empty( $ids ) || !in_array( $field_type, $tableless_field_types ) )
6822
            return array();
6823
6824
        $related_pick_limit = 0;
6825
6826
		if ( empty( $field ) ) {
6827
			$field = $this->load_field( array( 'id' => $field_id ) );
6828
		}
6829
6830
        if ( !empty( $field ) ) {
6831
            $options = (array) pods_var_raw( 'options', $field, $field, null, true );
6832
6833
            $related_pick_limit = (int) pods_v( $field_type . '_limit', $options, 0 );
6834
6835
            if ( 'single' == pods_var_raw( $field_type . '_format_type', $options ) )
6836
                $related_pick_limit = 1;
6837
6838
            // Temporary hack until there's some better handling here
6839
            $related_pick_limit = $related_pick_limit * count( $ids );
6840
        }
6841
6842
		if ( 'taxonomy' == $field_type ) {
6843
			$related = wp_get_object_terms( $ids, pods_v( 'name', $field ), array( 'fields' => 'ids' ) );
6844
6845
			if ( !is_wp_error( $related ) ) {
6846
				$related_ids = $related;
6847
			}
6848
		}
6849
		elseif ( !pods_tableless() ) {
6850
            $ids = implode( ', ', $ids );
6851
6852
            $field_id = (int) $field_id;
6853
            $sister_id = (int) pods_var_raw( 'sister_id', $field, 0 );
6854
6855
            $related_where = "
6856
                `field_id` = {$field_id}
6857
                AND `item_id` IN ( {$ids} )
6858
            ";
6859
6860
            $sql = "
6861
                SELECT item_id, related_item_id, related_field_id
6862
                FROM `@wp_podsrel`
6863
                WHERE
6864
                    {$related_where}
6865
                ORDER BY `weight`
6866
            ";
6867
6868
            $relationships = pods_query( $sql );
6869
6870
            if ( !empty( $relationships ) ) {
6871
                foreach ( $relationships as $relation ) {
6872
                    if ( !in_array( $relation->related_item_id, $related_ids ) )
6873
                        $related_ids[] = (int) $relation->related_item_id;
6874
                    elseif ( 0 < $sister_id && $field_id == $relation->related_field_id && !in_array( $relation->item_id, $related_ids ) )
6875
                        $related_ids[] = (int) $relation->item_id;
6876
                }
6877
            }
6878
        }
6879 View Code Duplication
        else {
6880
            if ( !is_array( $pod ) )
6881
                $pod = $this->load_pod( array( 'id' => $pod_id, 'table_info' => false ), false );
6882
6883
            if ( !empty( $pod ) && in_array( $pod[ 'type' ], array( 'post_type', 'media', 'taxonomy', 'user', 'comment', 'settings' ) ) ) {
6884
                $meta_type = $pod[ 'type' ];
6885
6886
                if ( in_array( $meta_type, array( 'post_type', 'media' ) ) )
6887
                    $meta_type = 'post';
6888
                elseif ( 'taxonomy' == $meta_type )
6889
                    $meta_type = 'term';
6890
6891
                $no_conflict = pods_no_conflict_check( ( 'term' == $meta_type ? 'taxonomy' : $meta_type ) );
6892
6893
                if ( !$no_conflict )
6894
                    pods_no_conflict_on( ( 'term' == $meta_type ? 'taxonomy' : $meta_type ) );
6895
6896
                foreach ( $ids as $id ) {
6897
                    if ( 'settings' == $meta_type ) {
6898
                        $related_id = get_option( '_pods_' . $pod[ 'name' ] . '_' . $field[ 'name' ] );
6899
6900
                        if ( empty( $related_id ) )
6901
                            $related_id = get_option( $pod[ 'name' ] . '_' . $field[ 'name' ] );
6902
6903
                        if ( is_array( $related_id ) && !empty( $related_id ) ) {
6904
                            foreach ( $related_id as $related ) {
6905
                                if ( is_array( $related ) && !empty( $related ) ) {
6906
                                    if ( isset( $related[ 'id' ] ) )
6907
                                        $related_ids[] = (int) $related[ 'id' ];
6908
                                    else {
6909
                                        foreach ( $related as $r ) {
6910
                                            $related_ids[] = (int) $r;
6911
                                        }
6912
                                    }
6913
                                }
6914
                                else
6915
                                    $related_ids[] = (int) $related;
6916
                            }
6917
                        }
6918
                    }
6919
                    else {
6920
                        $related_id = get_metadata( $meta_type, $id, '_pods_' . $field[ 'name' ], true );
6921
6922
                        if ( empty( $related_id ) )
6923
                            $related_id = get_metadata( $meta_type, $id, $field[ 'name' ] );
6924
6925
                        if ( is_array( $related_id ) && !empty( $related_id ) ) {
6926
                            foreach ( $related_id as $related ) {
6927
                                if ( is_array( $related ) && !empty( $related ) ) {
6928
                                    if ( isset( $related[ 'id' ] ) )
6929
                                        $related_ids[] = (int) $related[ 'id' ];
6930
                                    else {
6931
                                        foreach ( $related as $r ) {
6932
                                            if ( isset( $related[ 'id' ] ) )
6933
                                                $related_ids[] = (int) $r[ 'id' ];
6934
                                            else
6935
                                                $related_ids[] = (int) $r;
6936
                                        }
6937
                                    }
6938
                                }
6939
                                else
6940
                                    $related_ids[] = (int) $related;
6941
                            }
6942
                        }
6943
                    }
6944
                }
6945
6946
                if ( !$no_conflict )
6947
                    pods_no_conflict_off( ( 'term' == $meta_type ? 'taxonomy' : $meta_type ) );
6948
            }
6949
        }
6950
6951
        if ( is_array( $related_ids ) ) {
6952
            $related_ids = array_unique( array_filter( $related_ids ) );
6953
6954
            if ( 0 < $related_pick_limit && !empty( $related_ids ) )
6955
                $related_ids = array_slice( $related_ids, 0, $related_pick_limit );
6956
        }
6957
	    if ( 0 != $pod_id && 0 != $field_id && ! empty( $related_ids ) ) {
6958
                    // Only cache if $pod_id and $field_id were passed
6959
		    self::$related_item_cache[ $pod_id ][ $field_id ][ $idstring ] = $related_ids;
6960
	    }
6961
6962
        return $related_ids;
6963
    }
6964
6965
    /**
6966
     * Find related items related to an item
6967
     *
6968
     * @param int $field_id The Field ID
6969
     * @param int $pod_id The Pod ID
6970
     * @param int $id Item ID to get related IDs from
6971
     * @param array $field Field data array
6972
     * @param array $pod Pod data array
6973
     *
6974
     * @return array|bool
6975
     *
6976
     * @since 2.3
6977
     *
6978
     * @uses pods_query()
6979
     */
6980
    public function lookup_related_items_from ( $field_id, $pod_id, $id, $field = null, $pod = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $pod_id is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $pod is not used and could be removed.

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

Loading history...
6981
        $related_ids = false;
6982
6983
        $id = (int) $id;
6984
6985
        $tableless_field_types = PodsForm::tableless_field_types();
6986
6987
        if ( empty( $id ) || !in_array( pods_v( 'type', $field ), $tableless_field_types ) )
6988
            return false;
6989
6990
        $related_pick_limit = 0;
6991
6992
        if ( !empty( $field ) ) {
6993
            $options = (array) pods_var_raw( 'options', $field, $field, null, true );
6994
6995
            $related_pick_limit = (int) pods_v( 'pick_limit', $options, 0 );
6996
6997
            if ( 'single' == pods_var_raw( 'pick_format_type', $options ) )
6998
                $related_pick_limit = 1;
6999
        }
7000
7001
        if ( !pods_tableless() ) {
7002
            $field_id = (int) $field_id;
7003
            $sister_id = (int) pods_var_raw( 'sister_id', $field, 0 );
7004
7005
            $related_where = "
7006
                `field_id` = {$field_id}
7007
                AND `related_item_id` = {$id}
7008
            ";
7009
7010
            $sql = "
7011
                SELECT *
7012
                FROM `@wp_podsrel`
7013
                WHERE
7014
                    {$related_where}
7015
                ORDER BY `weight`
7016
            ";
7017
7018
            $relationships = pods_query( $sql );
7019
7020
            if ( !empty( $relationships ) ) {
7021
                $related_ids = array();
7022
7023
                foreach ( $relationships as $relation ) {
7024
                    if ( $field_id == $relation->field_id && !in_array( $relation->item_id, $related_ids ) )
7025
                        $related_ids[] = (int) $relation->item_id;
7026
                    elseif ( 0 < $sister_id && $field_id == $relation->related_field_id && !in_array( $relation->related_item_id, $related_ids ) )
7027
                        $related_ids[] = (int) $relation->related_item_id;
7028
                }
7029
            }
7030
        }
7031 View Code Duplication
        else {
7032
            // @todo handle meta-based lookups
7033
            return false;
7034
7035
            if ( !is_array( $pod ) )
0 ignored issues
show
Unused Code introduced by
if (!is_array($pod)) { ...o' => false), false); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
Bug introduced by
The variable $pod seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
7036
                $pod = $this->load_pod( array( 'id' => $pod_id, 'table_info' => false ), false );
7037
7038
            if ( !empty( $pod ) && in_array( $pod[ 'type' ], array( 'post_type', 'media', 'taxonomy', 'user', 'comment', 'settings' ) ) ) {
0 ignored issues
show
Bug introduced by
The variable $pod seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
7039
                $related_ids = array();
7040
7041
                $meta_type = $pod[ 'type' ];
7042
7043
                if ( in_array( $meta_type, array( 'post_type', 'media' ) ) )
0 ignored issues
show
Bug introduced by
The variable $meta_type seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
7044
                    $meta_type = 'post';
7045
                elseif ( 'taxonomy' == $meta_type )
0 ignored issues
show
Bug introduced by
The variable $meta_type seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
7046
                    $meta_type = 'term';
7047
7048
                $no_conflict = pods_no_conflict_check( ( 'term' == $meta_type ? 'taxonomy' : $meta_type ) );
7049
7050
                if ( !$no_conflict )
0 ignored issues
show
Bug introduced by
The variable $no_conflict seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
7051
                    pods_no_conflict_on( ( 'term' == $meta_type ? 'taxonomy' : $meta_type ) );
7052
7053
                if ( 'settings' == $meta_type ) {
0 ignored issues
show
Bug introduced by
The variable $meta_type seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
7054
                    $related_id = get_option( '_pods_' . $pod[ 'name' ] . '_' . $field[ 'name' ] );
7055
7056
                    if ( empty( $related_id ) )
0 ignored issues
show
Bug introduced by
The variable $related_id seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
7057
                        $related_id = get_option( $pod[ 'name' ] . '_' . $field[ 'name' ] );
7058
7059
                    if ( is_array( $related_id ) && !empty( $related_id ) ) {
7060
                        foreach ( $related_id as $related ) {
7061
                            if ( is_array( $related ) && !empty( $related ) ) {
7062
                                if ( isset( $related[ 'id' ] ) )
7063
                                    $related_ids[] = (int) $related[ 'id' ];
7064
                                else {
7065
                                    foreach ( $related as $r ) {
7066
                                        $related_ids[] = (int) $r;
7067
                                    }
7068
                                }
7069
                            }
7070
                            else
7071
                                $related_ids[] = (int) $related;
7072
                        }
7073
                    }
7074
                }
7075
                else {
7076
                    $related_id = get_metadata( $meta_type, $id, '_pods_' . $field[ 'name' ], true );
7077
7078
                    if ( empty( $related_id ) )
0 ignored issues
show
Bug introduced by
The variable $related_id seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
7079
                        $related_id = get_metadata( $meta_type, $id, $field[ 'name' ] );
7080
7081
                    if ( is_array( $related_id ) && !empty( $related_id ) ) {
7082
                        foreach ( $related_id as $related ) {
7083
                            if ( is_array( $related ) && !empty( $related ) ) {
7084
                                if ( isset( $related[ 'id' ] ) )
7085
                                    $related_ids[] = (int) $related[ 'id' ];
7086
                                else {
7087
                                    foreach ( $related as $r ) {
7088
                                        if ( isset( $related[ 'id' ] ) )
7089
                                            $related_ids[] = (int) $r[ 'id' ];
7090
                                        else
7091
                                            $related_ids[] = (int) $r;
7092
                                    }
7093
                                }
7094
                            }
7095
                            else
7096
                                $related_ids[] = (int) $related;
7097
                        }
7098
                    }
7099
                }
7100
7101
                if ( !$no_conflict )
0 ignored issues
show
Bug introduced by
The variable $no_conflict seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
7102
                    pods_no_conflict_off( ( 'term' == $meta_type ? 'taxonomy' : $meta_type ) );
7103
            }
7104
        }
7105
7106
        if ( is_array( $related_ids ) )
7107
            $related_ids = array_unique( array_filter( $related_ids ) );
7108
7109
        return $related_ids;
7110
    }
7111
7112
	/**
7113
	 *
7114
	 * Load the information about an objects MySQL table
7115
	 *
7116
	 * @param $object_type
7117
	 * @param string $object The object to look for
7118
	 * @param null $name (optional) Name of the pod to load
7119
	 * @param array $pod (optional) Array with pod information
7120
	 *
7121
	 * @return array
7122
	 *
7123
	 * @since 2.5
7124
	 */
7125
	public function get_table_info_load ( $object_type, $object, $name = null, $pod = null ) {
7126
7127
		$info = array();
7128
7129
		if ( 'pod' == $object_type && null === $pod ) {
7130 View Code Duplication
			if ( empty( $name ) ) {
7131
				$prefix = 'pod-';
7132
7133
				// Make sure we actually have the prefix before trying anything with the name
7134
				if ( 0 === strpos( $object_type, $prefix ) )
7135
					$name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
7136
			}
7137
7138
			if ( empty( $name ) && !empty( $object ) )
7139
				$name = $object;
7140
7141
			$pod = $this->load_pod( array( 'name' => $name, 'table_info' => false ), false );
7142
7143
			if ( !empty( $pod ) ) {
7144
				$object_type = $pod[ 'type' ];
7145
				$name = $pod[ 'name' ];
7146
				$object = $pod[ 'object' ];
7147
7148
				$info[ 'pod' ] = $pod;
7149
			}
7150
		}
7151
		elseif ( null === $pod ) {
7152
			if ( empty( $name ) ) {
7153
				$prefix = $object_type . '-';
7154
7155
				// Make sure we actually have the prefix before trying anything with the name
7156
				if ( 0 === strpos( $object_type, $prefix ) )
7157
					$name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
7158
			}
7159
7160
			if ( empty( $name ) && !empty( $object ) )
7161
				$name = $object;
7162
7163
			if ( !empty( $name ) ) {
7164
				$pod = $this->load_pod( array( 'name' => $name, 'table_info' => false ), false );
7165
7166
				if ( !empty( $pod ) && ( null === $object_type || $object_type == $pod[ 'type' ] ) ) {
7167
					$object_type = $pod[ 'type' ];
7168
					$name = $pod[ 'name' ];
7169
					$object = $pod[ 'object' ];
7170
7171
					$info[ 'pod' ] = $pod;
7172
				}
7173
			}
7174
		}
7175
		elseif ( !empty( $pod ) )
7176
			$info[ 'pod' ] = $pod;
7177
7178
		if ( 0 === strpos( $object_type, 'pod' ) ) {
7179 View Code Duplication
			if ( empty( $name ) ) {
7180
				$prefix = 'pod-';
7181
7182
				// Make sure we actually have the prefix before trying anything with the name
7183
				if ( 0 === strpos( $object_type, $prefix ) )
7184
					$name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
7185
			}
7186
7187
			$info[ 'type' ] = 'pod';
7188
			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...
7189
7190
			$info[ 'table' ] = $info[ 'meta_table' ] = $wpdb->prefix . 'pods_' . ( empty( $object ) ? $name : $object );
7191
7192
			if ( is_array( $info[ 'pod' ] ) && 'pod' == pods_v( 'type', $info[ 'pod' ] ) ) {
7193
				$info[ 'pod_field_index' ] = $info[ 'field_index' ] = $info[ 'meta_field_index' ] = $info[ 'meta_field_value' ] = pods_v( 'pod_index', $info[ 'pod' ][ 'options' ], 'id', true );
7194
7195
				$slug_field = get_posts( array(
7196
					'post_type' => '_pods_field',
7197
					'posts_per_page' => 1,
7198
					'nopaging' => true,
7199
					'post_parent' => $info[ 'pod' ][ 'id' ],
7200
					'orderby' => 'menu_order',
7201
					'order' => 'ASC',
7202
					'meta_query' => array(
7203
						array(
7204
							'key' => 'type',
7205
							'value' => 'slug',
7206
						)
7207
					)
7208
				) );
7209
7210
				if ( !empty( $slug_field ) ) {
7211
					$slug_field = $slug_field[ 0 ];
7212
7213
					$info[ 'field_slug' ] = $info[ 'pod_field_slug' ] = $slug_field->post_name;
7214
				}
7215
7216
				if ( 1 == pods_v( 'hierarchical', $info[ 'pod' ][ 'options' ], 0 ) ) {
7217
					$parent_field = pods_v( 'pod_parent', $info[ 'pod' ][ 'options' ], 'id', true );
7218
7219
					if ( !empty( $parent_field ) && isset( $info[ 'pod' ][ 'fields' ][ $parent_field ] ) ) {
7220
						$info[ 'object_hierarchical' ] = true;
7221
7222
						$info[ 'pod_field_parent' ] = $info[ 'field_parent' ] = $parent_field . '_select';
7223
						$info[ 'field_parent_select' ] = '`' . $parent_field . '`.`id` AS `' . $info[ 'field_parent' ] . '`';
7224
					}
7225
				}
7226
			}
7227
		}
7228
		return $info;
7229
	}
7230
7231
    /**
7232
     * Get information about an objects MySQL table
7233
     *
7234
     * @param string $object_type
7235
     * @param string $object The object to look for
7236
     * @param null $name (optional) Name of the pod to load
7237
     * @param array $pod (optional) Array with pod information
7238
     * @param array $field (optional) Array with field information
7239
     *
7240
     * @return array|bool
7241
     *
7242
     * @since 2.0
7243
     */
7244
    public function get_table_info ( $object_type, $object, $name = null, $pod = null, $field = null ) {
7245
        /**
7246
         * @var $wpdb wpdb
7247
         * @var $sitepress SitePress
7248
         * @var $icl_adjust_id_url_filter_off boolean
7249
         */
7250
        global $wpdb, $sitepress, $icl_adjust_id_url_filter_off, $polylang;
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...
7251
7252
		// @todo Handle $object arrays for Post Types, Taxonomies, Comments (table pulled from first object in array)
7253
7254
        $info = array(
7255
            //'select' => '`t`.*',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
7256
            'object_type' => $object_type,
7257
            'type' => null,
7258
            'object_name' => $object,
7259
            'object_hierarchical' => false,
7260
7261
            'table' => $object,
7262
            'meta_table' => $object,
7263
            'pod_table' => $wpdb->prefix . 'pods_' . ( empty( $object ) ? $name : $object ),
7264
7265
            'field_id' => 'id',
7266
            'field_index' => 'name',
7267
            'field_slug' => null,
7268
            'field_type' => null,
7269
            'field_parent' => null,
7270
            'field_parent_select' => null,
7271
7272
            'meta_field_id' => 'id',
7273
            'meta_field_index' => 'name',
7274
            'meta_field_value' => 'name',
7275
7276
            'pod_field_id' => 'id',
7277
            'pod_field_index' => 'name',
7278
            'pod_field_slug' => null,
7279
            'pod_field_parent' => null,
7280
7281
            'join' => array(),
7282
7283
            'where' => null,
7284
            'where_default' => null,
7285
7286
            'orderby' => null,
7287
7288
            'pod' => null,
7289
            'recurse' => false
7290
        );
7291
7292
        if ( empty( $object_type ) ) {
7293
            $object_type = 'post_type';
7294
            $object = 'post';
7295
        }
7296
	    elseif ( empty( $object ) && in_array( $object_type, array( 'user', 'media', 'comment' ) ) ) {
7297
		    $object = $object_type;
7298
	    }
7299
7300
        $pod_name = $pod;
7301
7302 View Code Duplication
        if ( is_array( $pod_name ) )
7303
            $pod_name = pods_var_raw( 'name', $pod_name, ( version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $pod_name, JSON_UNESCAPED_UNICODE ) : json_encode( $pod_name ) ), null, true );
0 ignored issues
show
Unused Code introduced by
The call to json_encode() has too many arguments starting with JSON_UNESCAPED_UNICODE.

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

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

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

Loading history...
7304
	    else {
7305
		    $pod_name = $object;
7306
	    }
7307
7308
        $field_name = $field;
7309
7310 View Code Duplication
        if ( is_array( $field_name ) )
7311
            $field_name = pods_var_raw( 'name', $field_name, ( version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $pod_name, JSON_UNESCAPED_UNICODE ) : json_encode( $field_name ) ), null, true );
0 ignored issues
show
Unused Code introduced by
The call to json_encode() has too many arguments starting with JSON_UNESCAPED_UNICODE.

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

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

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

Loading history...
7312
7313
        $transient = 'pods_' . $wpdb->prefix . '_get_table_info_' . md5( $object_type . '_object_' . $object . '_name_' . $name . '_pod_' . $pod_name . '_field_' . $field_name );
7314
7315
        $current_language = false;
7316
        $current_language_t_id = $current_language_tt_id = 0;
7317
7318
        // WPML support
7319
        if ( is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
7320
            $current_language = pods_sanitize( ICL_LANGUAGE_CODE );
7321
        // Polylang support
7322
        elseif ( is_object( $polylang ) && function_exists( 'pll_current_language' ) ) {
7323
            $current_language = pods_sanitize( pll_current_language( 'slug' ) );
7324
7325
            if ( !empty( $current_language ) ) {
7326
		if ( isset( $polylang->model ) && method_exists( $polylang->model, 'get_language' )) {
7327
			$current_language_t_id = (int) $polylang->model->get_language( $current_language )->term_id;
7328
			$current_language_tt_id = (int) $polylang->model->get_language( $current_language )->term_taxonomy_id;
7329
		} else {
7330
			$current_language_t_id = (int) $polylang->get_language( $current_language )->term_id;
7331
			$current_language_tt_id = (int) $polylang->get_language( $current_language )->term_taxonomy_id;
7332
		}
7333
            }
7334
        }
7335
7336
        if ( !empty( $current_language ) )
7337
            $transient = 'pods_' . $wpdb->prefix . '_get_table_info_' . $current_language . '_' . md5( $object_type . '_object_' . $object . '_name_' . $name . '_pod_' . $pod_name . '_field_' . $field_name );
7338
7339
        $_info = false;
7340
7341
	    if ( isset( self::$table_info_cache[ $transient ] ) ) {
7342
		    // Prefer info from the object internal cache
7343
		    $_info = self::$table_info_cache[ $transient ];
7344
	    } elseif ( pods_api_cache() ) {
7345
		    $_info = pods_transient_get( $transient );
7346
		    if ( false === $_info && ! did_action( 'init' ) ) {
7347
			    $_info = pods_transient_get( $transient . '_pre_init' );
7348
		    }
7349
	    }
7350
7351
	    if ( false !== $_info ) {
7352
		    // Data was cached, use that
7353
		    $info = $_info;
7354
	    } else {
7355
	        // Data not cached, load it up
7356
		    $_info = $this->get_table_info_load( $object_type, $object, $name, $pod );
7357
		    if ( isset( $_info[ 'type' ] ) ) {
7358
			    // Allow function to override $object_type
7359
			    $object_type = $_info[ 'type' ];
7360
		    }
7361
		    $info = array_merge( $info, $_info );
7362
	    }
7363
7364
	    if ( 0 === strpos( $object_type, 'post_type' ) || 'media' == $object_type || in_array( pods_var_raw( 'type', $info[ 'pod' ] ), array( 'post_type', 'media' ) ) ) {
7365
		    $info[ 'table' ] = $wpdb->posts;
7366
		    $info[ 'meta_table' ] = $wpdb->postmeta;
7367
7368
		    $info[ 'field_id' ] = 'ID';
7369
		    $info[ 'field_index' ] = 'post_title';
7370
		    $info[ 'field_slug' ] = 'post_name';
7371
		    $info[ 'field_type' ] = 'post_type';
7372
		    $info[ 'field_parent' ] = 'post_parent';
7373
		    $info[ 'field_parent_select' ] = '`t`.`' . $info[ 'field_parent' ] . '`';
7374
7375
		    $info[ 'meta_field_id' ] = 'post_id';
7376
		    $info[ 'meta_field_index' ] = 'meta_key';
7377
		    $info[ 'meta_field_value' ] = 'meta_value';
7378
7379
		    if ( 'media' == $object_type )
7380
			    $object = 'attachment';
7381
7382
		    if ( empty( $name ) ) {
7383
			    $prefix = 'post_type-';
7384
7385
			    // Make sure we actually have the prefix before trying anything with the name
7386
			    if ( 0 === strpos( $object_type, $prefix ) )
7387
				    $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
7388
		    }
7389
7390
		    if ( 'media' != $object_type )
7391
			    $object_type = 'post_type';
7392
7393
		    $post_type = pods_sanitize( ( empty( $object ) ? $name : $object ) );
7394
7395
		    if ( 'attachment' == $post_type || 'media' == $object_type )
7396
			    $info[ 'pod_table' ] = $wpdb->prefix . 'pods_media';
7397
		    else
7398
			    $info[ 'pod_table' ] = $wpdb->prefix . 'pods_' . pods_clean_name( $post_type, true, false );
7399
7400
		    $post_type_object = get_post_type_object( $post_type );
7401
7402
		    if ( is_object( $post_type_object ) && $post_type_object->hierarchical )
7403
			    $info[ 'object_hierarchical' ] = true;
7404
7405
		    /**
7406
		     * Default Post Status to query for.
7407
		     *
7408
		     * Use to change "default" post status from publish to any other status or statuses.
7409
		     *
7410
		     * @param  array $post_status List of post statuses. Default is 'publish'
7411
		     * @param  string $post_type Post type of current object
7412
		     * @param  array $info Array of information about the object.
7413
		     * @param  string $object	Type of object
7414
		     * @param  string $name Name of pod to load
7415
		     * @param  array $pod Array with Pod information. Result of PodsAPI::load_pod()
7416
		     * @param  array $field		Array with field information
7417
		     *
7418
		     * @since unknown
7419
		     */
7420
		    $post_status = apply_filters( 'pods_api_get_table_info_default_post_status', array( 'publish' ), $post_type, $info, $object_type, $object, $name, $pod, $field );
7421
7422
		    $info[ 'where' ] = array(
7423
			    //'post_status' => '`t`.`post_status` IN ( "inherit", "publish" )', // @todo Figure out what statuses Attachments can be
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
7424
			    'post_type' => '`t`.`' . $info[ 'field_type' ] . '` = "' . $post_type . '"'
7425
		    );
7426
7427
		    if ( 'post_type' == $object_type )
7428
			    $info[ 'where_default' ] = '`t`.`post_status` IN ( "' . implode( '", "', $post_status ) . '" )';
7429
7430
		    $info[ 'orderby' ] = '`t`.`menu_order`, `t`.`' . $info[ 'field_index' ] . '`, `t`.`post_date`';
7431
7432
		    // WPML support
7433 View Code Duplication
		    if ( is_object( $sitepress ) && $sitepress->is_translated_post_type( $post_type ) && !$icl_adjust_id_url_filter_off ) {
7434
			    $info[ 'join' ][ 'wpml_translations' ] = "
7435
                        LEFT JOIN `{$wpdb->prefix}icl_translations` AS `wpml_translations`
7436
                            ON `wpml_translations`.`element_id` = `t`.`ID`
7437
                                AND `wpml_translations`.`element_type` = 'post_{$post_type}'
7438
                                AND `wpml_translations`.`language_code` = '{$current_language}'
7439
                    ";
7440
7441
			    $info[ 'join' ][ 'wpml_languages' ] = "
7442
                        LEFT JOIN `{$wpdb->prefix}icl_languages` AS `wpml_languages`
7443
                            ON `wpml_languages`.`code` = `wpml_translations`.`language_code` AND `wpml_languages`.`active` = 1
7444
                    ";
7445
7446
			    $info[ 'where' ][ 'wpml_languages' ] = "`wpml_languages`.`code` IS NOT NULL";
7447
		    }
7448
		    // Polylang support
7449
		    elseif( is_object( $polylang ) && !empty( $current_language ) && function_exists( 'pll_is_translated_post_type' ) && pll_is_translated_post_type( $post_type ) ) {
7450
			    $info[ 'join' ][ 'polylang_languages' ] = "
7451
                        LEFT JOIN `{$wpdb->term_relationships}` AS `polylang_languages`
7452
                            ON `polylang_languages`.`object_id` = `t`.`ID`
7453
                                AND `polylang_languages`.`term_taxonomy_id` = {$current_language_tt_id}
7454
                    ";
7455
7456
			    $info[ 'where' ][ 'polylang_languages' ] = "`polylang_languages`.`object_id` IS NOT NULL";
7457
		    }
7458
7459
		    $info[ 'object_fields' ] = $this->get_wp_object_fields( $object_type, $info[ 'pod' ] );
7460
	    }
7461
	    elseif ( 0 === strpos( $object_type, 'taxonomy' ) || in_array( $object_type, array( 'nav_menu', 'post_format' ) ) || 'taxonomy' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7462
		    $info[ 'table' ] = $info[ 'meta_table' ] = $wpdb->terms;
7463
7464
		    $info[ 'join' ][ 'tt' ] = "LEFT JOIN `{$wpdb->term_taxonomy}` AS `tt` ON `tt`.`term_id` = `t`.`term_id`";
7465
		    $info[ 'join' ][ 'tr' ] = "LEFT JOIN `{$wpdb->term_relationships}` AS `tr` ON `tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`";
7466
		    $info[ 'field_id' ] = $info[ 'meta_field_id' ] = 'term_id';
7467
		    $info[ 'field_index' ] = $info[ 'meta_field_index' ] = $info[ 'meta_field_value' ] = 'name';
7468
		    $info[ 'field_slug' ] = 'slug';
7469
		    $info[ 'field_type' ] = 'taxonomy';
7470
		    $info[ 'field_parent' ] = 'parent';
7471
		    $info[ 'field_parent_select' ] = '`tt`.`' . $info[ 'field_parent' ] . '`';
7472
7473
            if ( ! empty( $wpdb->termmeta ) ) {
7474
                $info[ 'meta_table' ] = $wpdb->termmeta;
7475
7476
                $info[ 'meta_field_id' ] = 'term_id';
7477
                $info[ 'meta_field_index' ] = 'meta_key';
7478
                $info[ 'meta_field_value' ] = 'meta_value';
7479
            }
7480
7481
		    if ( 'nav_menu' == $object_type )
7482
			    $object = 'nav_menu';
7483
		    elseif ( 'post_format' == $object_type )
7484
			    $object = 'post_format';
7485
7486
		    if ( empty( $name ) ) {
7487
			    $prefix = 'taxonomy-';
7488
7489
			    // Make sure we actually have the prefix before trying anything with the name
7490
			    if ( 0 === strpos( $object_type, $prefix ) )
7491
				    $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
7492
		    }
7493
7494
		    if ( !in_array( $object_type, array( 'nav_menu', 'post_format' ) ) )
7495
			    $object_type = 'taxonomy';
7496
7497
		    $taxonomy = pods_sanitize( ( empty( $object ) ? $name : $object ) );
7498
7499
		    $info[ 'pod_table' ] = $wpdb->prefix . 'pods_' . pods_clean_name( $taxonomy, true, false );
7500
7501
		    $taxonomy_object = get_taxonomy( $taxonomy );
7502
7503
		    if ( is_object( $taxonomy_object ) && $taxonomy_object->hierarchical )
7504
			    $info[ 'object_hierarchical' ] = true;
7505
7506
		    $info[ 'where' ] = array(
7507
			    'tt.taxonomy' => '`tt`.`' . $info[ 'field_type' ] . '` = "' . $taxonomy . '"'
7508
		    );
7509
7510
		    // WPML Support
7511 View Code Duplication
		    if ( is_object( $sitepress ) && $sitepress->is_translated_taxonomy( $taxonomy ) && !$icl_adjust_id_url_filter_off ) {
7512
			    $info[ 'join' ][ 'wpml_translations' ] = "
7513
                        LEFT JOIN `{$wpdb->prefix}icl_translations` AS `wpml_translations`
7514
                            ON `wpml_translations`.`element_id` = `tt`.`term_taxonomy_id`
7515
                                AND `wpml_translations`.`element_type` = 'tax_{$taxonomy}'
7516
                                AND `wpml_translations`.`language_code` = '{$current_language}'
7517
                    ";
7518
7519
			    $info[ 'join' ][ 'wpml_languages' ] = "
7520
                        LEFT JOIN `{$wpdb->prefix}icl_languages` AS `wpml_languages`
7521
                            ON `wpml_languages`.`code` = `wpml_translations`.`language_code` AND `wpml_languages`.`active` = 1
7522
                    ";
7523
7524
			    $info[ 'where' ][ 'wpml_languages' ] = "`wpml_languages`.`code` IS NOT NULL";
7525
		    }
7526
		    // Polylang support
7527
		    elseif ( is_object( $polylang ) && !empty( $current_language ) && function_exists( 'pll_is_translated_taxonomy' ) && pll_is_translated_taxonomy( $taxonomy ) ) {
7528
			    $info[ 'join' ][ 'polylang_languages' ] = "
7529
                        LEFT JOIN `{$wpdb->termmeta}` AS `polylang_languages`
7530
                            ON `polylang_languages`.`term_id` = `t`.`term_id`
7531
                                AND `polylang_languages`.`meta_value` = {$current_language_t_id}
7532
                    ";
7533
7534
			    $info[ 'where' ][ 'polylang_languages' ] = "`polylang_languages`.`term_id` IS NOT NULL";
7535
		    }
7536
7537
		    $info[ 'object_fields' ] = $this->get_wp_object_fields( $object_type, $info[ 'pod' ] );
7538
	    }
7539
	    elseif ( 'user' == $object_type || 'user' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7540
		    $info[ 'table' ] = $wpdb->users;
7541
		    $info[ 'meta_table' ] = $wpdb->usermeta;
7542
		    $info[ 'pod_table' ] = $wpdb->prefix . 'pods_user';
7543
7544
		    $info[ 'field_id' ] = 'ID';
7545
		    $info[ 'field_index' ] = 'display_name';
7546
		    $info[ 'field_slug' ] = 'user_nicename';
7547
7548
		    $info[ 'meta_field_id' ] = 'user_id';
7549
		    $info[ 'meta_field_index' ] = 'meta_key';
7550
		    $info[ 'meta_field_value' ] = 'meta_value';
7551
7552
		    $info[ 'where' ] = array(
7553
			    'user_status' => '`t`.`user_status` = 0'
7554
		    );
7555
7556
		    $info[ 'object_fields' ] = $this->get_wp_object_fields( $object_type, $info[ 'pod' ] );
7557
	    }
7558
	    elseif ( 'comment' == $object_type || 'comment' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7559
		    //$info[ 'object_hierarchical' ] = true;
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
7560
7561
		    $info[ 'table' ] = $wpdb->comments;
7562
		    $info[ 'meta_table' ] = $wpdb->commentmeta;
7563
		    $info[ 'pod_table' ] = $wpdb->prefix . 'pods_comment';
7564
7565
		    $info[ 'field_id' ] = 'comment_ID';
7566
		    $info[ 'field_index' ] = 'comment_date';
7567
		    $info[ 'field_type' ] = 'comment_type';
7568
		    $info[ 'field_parent' ] = 'comment_parent';
7569
		    $info[ 'field_parent_select' ] = '`t`.`' . $info[ 'field_parent' ] . '`';
7570
7571
		    $info[ 'meta_field_id' ] = 'comment_id';
7572
		    $info[ 'meta_field_index' ] = 'meta_key';
7573
		    $info[ 'meta_field_value' ] = 'meta_value';
7574
7575
		    $object = 'comment';
7576
7577
		    $comment_type = ( empty( $object ) ? $name : $object );
7578
7579
		    $comment_type_clause = '`t`.`' . $info[ 'field_type' ] . '` = "' . $comment_type . '"';
7580
7581
		    if ( 'comment' == $comment_type ) {
7582
			    $comment_type_clause = '( ' . $comment_type_clause . ' OR `t`.`' . $info[ 'field_type' ] . '` = "" )';
7583
		    }
7584
7585
		    $info[ 'where' ] = array(
7586
			    'comment_approved' => '`t`.`comment_approved` = 1',
7587
			    'comment_type' => $comment_type_clause
7588
		    );
7589
7590
		    $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` DESC, `t`.`' . $info[ 'field_id' ] . '`';
7591
	    }
7592 View Code Duplication
	    elseif ( in_array( $object_type, array( 'option', 'settings' ) ) || 'settings' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7593
		    $info[ 'table' ] = $wpdb->options;
7594
		    $info[ 'meta_table' ] = $wpdb->options;
7595
7596
		    $info[ 'field_id' ] = 'option_id';
7597
		    $info[ 'field_index' ] = 'option_name';
7598
7599
		    $info[ 'meta_field_id' ] = 'option_id';
7600
		    $info[ 'meta_field_index' ] = 'option_name';
7601
		    $info[ 'meta_field_value' ] = 'option_value';
7602
7603
		    $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` ASC';
7604
	    }
7605 View Code Duplication
	    elseif ( is_multisite() && ( in_array( $object_type, array( 'site_option', 'site_settings' ) ) || 'site_settings' == pods_var_raw( 'type', $info[ 'pod' ] ) ) ) {
7606
		    $info[ 'table' ] = $wpdb->sitemeta;
7607
		    $info[ 'meta_table' ] = $wpdb->sitemeta;
7608
7609
		    $info[ 'field_id' ] = 'site_id';
7610
		    $info[ 'field_index' ] = 'meta_key';
7611
7612
		    $info[ 'meta_field_id' ] = 'site_id';
7613
		    $info[ 'meta_field_index' ] = 'meta_key';
7614
		    $info[ 'meta_field_value' ] = 'meta_value';
7615
7616
		    $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` ASC';
7617
	    }
7618
	    elseif ( is_multisite() && 'network' == $object_type ) { // Network = Site
7619
		    $info[ 'table' ] = $wpdb->site;
7620
		    $info[ 'meta_table' ] = $wpdb->sitemeta;
7621
7622
		    $info[ 'field_id' ] = 'id';
7623
		    $info[ 'field_index' ] = 'domain';
7624
7625
		    $info[ 'meta_field_id' ] = 'site_id';
7626
		    $info[ 'meta_field_index' ] = 'meta_key';
7627
		    $info[ 'meta_field_value' ] = 'meta_value';
7628
7629
		    $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` ASC, `t`.`path` ASC, `t`.`' . $info[ 'field_id' ] . '`';
7630
	    }
7631
	    elseif ( is_multisite() && 'site' == $object_type ) { // Site = Blog
7632
		    $info[ 'table' ] = $wpdb->blogs;
7633
7634
		    $info[ 'field_id' ] = 'blog_id';
7635
		    $info[ 'field_index' ] = 'domain';
7636
		    $info[ 'field_type' ] = 'site_id';
7637
7638
		    $info[ 'where' ] = array(
7639
			    'archived' => '`t`.`archived` = 0',
7640
			    'spam' => '`t`.`spam` = 0',
7641
			    'deleted' => '`t`.`deleted` = 0',
7642
			    'site_id' => '`t`.`' . $info[ 'field_type' ] . '` = ' . (int) get_current_site()->id
7643
		    );
7644
7645
		    $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` ASC, `t`.`path` ASC, `t`.`' . $info[ 'field_id' ] . '`';
7646
	    }
7647
	    elseif ( 'table' == $object_type || 'table' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7648
		    $info[ 'table' ] = ( empty( $object ) ? $name : $object );
7649
		    $info[ 'pod_table' ] = $wpdb->prefix . 'pods_' . $info[ 'table' ];
7650
7651
		    if ( !empty( $field ) && is_array( $field ) ) {
7652
			    $info[ 'table' ] = pods_var_raw( 'pick_table', pods_var_raw( 'options', $field, $field ) );
7653
			    $info[ 'field_id' ] = pods_var_raw( 'pick_table_id', pods_var_raw( 'options', $field, $field ) );
7654
			    $info[ 'field_index' ] = $info[ 'meta_field_index' ] = $info[ 'meta_field_value' ] = pods_var_raw( 'pick_table_index', pods_var_raw( 'options', $field, $field ) );
7655
		    }
7656
	    }
7657
7658
	    $info[ 'table' ] = pods_clean_name( $info[ 'table' ], false, false );
7659
	    $info[ 'meta_table' ] = pods_clean_name( $info[ 'meta_table' ], false, false );
7660
	    $info[ 'pod_table' ] = pods_clean_name( $info[ 'pod_table' ], false, false );
7661
7662
	    $info[ 'field_id' ] = pods_clean_name( $info[ 'field_id' ], false, false );
7663
	    $info[ 'field_index' ] = pods_clean_name( $info[ 'field_index' ], false, false );
7664
	    $info[ 'field_slug' ] = pods_clean_name( $info[ 'field_slug' ], false, false );
7665
7666
	    $info[ 'meta_field_id' ] = pods_clean_name( $info[ 'meta_field_id' ], false, false );
7667
	    $info[ 'meta_field_index' ] = pods_clean_name( $info[ 'meta_field_index' ], false, false );
7668
	    $info[ 'meta_field_value' ] = pods_clean_name( $info[ 'meta_field_value' ], false, false );
7669
7670 View Code Duplication
	    if ( empty( $info[ 'orderby' ] ) )
7671
		    $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '`, `t`.`' . $info[ 'field_id' ] . '`';
7672
7673
	    if ( 'table' == pods_var_raw( 'storage', $info[ 'pod' ] ) && !in_array( $object_type, array( 'pod', 'table' ) ) ) {
7674
		    $info[ 'join' ][ 'd' ] = 'LEFT JOIN `' . $info[ 'pod_table' ] . '` AS `d` ON `d`.`id` = `t`.`' . $info[ 'field_id' ] . '`';
7675
		    //$info[ 'select' ] .= ', `d`.*';
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
7676
	    }
7677
7678
	    if ( !empty( $info[ 'pod' ] ) && is_array( $info[ 'pod' ] ) )
7679
		    $info[ 'recurse' ] = true;
7680
7681
	    $info[ 'type' ] = $object_type;
7682
	    $info[ 'object_name' ] = $object;
7683
7684
	    if ( pods_api_cache() ) {
7685
		    if ( ! did_action( 'init' ) ) {
7686
			    $transient .= '_pre_init';
7687
		    }
7688
		    pods_transient_set( $transient, $info );
7689
	    }
7690
7691
7692
	    self::$table_info_cache[ $transient ] = apply_filters( 'pods_api_get_table_info', $info, $object_type, $object, $name, $pod, $field, $this );
7693
7694
        return self::$table_info_cache[ $transient ];
7695
    }
7696
7697
    /**
7698
     * Export a package
7699
     *
7700
     * $params['pod'] string Pod Type IDs to export
7701
     * $params['template'] string Template IDs to export
7702
     * $params['podpage'] string Pod Page IDs to export
7703
     * $params['helper'] string Helper IDs to export
7704
     *
7705
     * @param array $params An associative array of parameters
7706
     *
7707
     * @return array|bool
7708
     *
7709
     * @since 1.9.0
7710
     * @deprecated 2.0
7711
     */
7712
    public function export_package ( $params ) {
7713
        if ( class_exists( 'Pods_Migrate_Packages' ) )
7714
            return Pods_Migrate_Packages::export( $params );
7715
7716
        return false;
7717
    }
7718
7719
    /**
7720
     * Replace an existing package
7721
     *
7722
     * @param mixed $data (optional) An associative array containing a package, or the json encoded package
7723
     *
7724
     * @return bool
7725
     *
7726
     * @since 1.9.8
7727
     * @deprecated 2.0
7728
     */
7729
    public function replace_package ( $data = false ) {
7730
        return $this->import_package( $data, true );
0 ignored issues
show
Deprecated Code introduced by
The method PodsAPI::import_package() has been deprecated with message: 2.0

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

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

Loading history...
7731
    }
7732
7733
    /**
7734
     * Import a package
7735
     *
7736
     * @param mixed $data (optional) An associative array containing a package, or the json encoded package
7737
     * @param bool $replace (optional) Replace existing items when found
7738
     *
7739
     * @return bool
7740
     *
7741
     * @since 1.9.0
7742
     * @deprecated 2.0
7743
     */
7744
    public function import_package ( $data = false, $replace = false ) {
7745
        if ( class_exists( 'Pods_Migrate_Packages' ) )
7746
            return Pods_Migrate_Packages::import( $data, $replace );
7747
7748
        return false;
7749
    }
7750
7751
    /**
7752
     * Validate a package
7753
     *
7754
     * @param array|string $data (optional) An associative array containing a package, or the json encoded package
7755
     * @param bool $output (optional)
7756
     *
7757
     * @return array|bool
7758
     *
7759
     * @since 1.9.0
7760
     * @deprecated 2.0
7761
     */
7762
    public function validate_package ( $data = false, $output = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $output is not used and could be removed.

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

Loading history...
7763
        return true;
7764
    }
7765
7766
    /**
7767
     * Import data from an array or a CSV file.
7768
     *
7769
     * @param mixed $import_data PHP associative array or CSV input
7770
     * @param bool $numeric_mode Use IDs instead of the name field when matching
7771
     * @param string $format Format of import data, options are php or csv
7772
     *
7773
     * @return array IDs of imported items
7774
     *
7775
     * @since 1.7.1
7776
     * @todo This needs some love and use of table_info etc for relationships
7777
     */
7778
    public function import ( $import_data, $numeric_mode = false, $format = null ) {
7779
        /**
7780
         * @var $wpdb wpdb
7781
         */
7782
        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...
7783
7784
        if ( null === $format && null !== $this->format )
0 ignored issues
show
Deprecated Code introduced by
The property PodsAPI::$format has been deprecated with message: 2.0

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

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

Loading history...
7785
            $format = $this->format;
0 ignored issues
show
Deprecated Code introduced by
The property PodsAPI::$format has been deprecated with message: 2.0

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

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

Loading history...
7786
7787
        if ( 'csv' == $format && !is_array( $import_data ) ) {
7788
            $data = pods_migrate( 'sv', ',' )->parse( $import_data );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as pods_migrate('sv', ',')->parse($import_data) (which targets PodsMigrate::parse()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
7789
7790
            $import_data = $data[ 'items' ];
7791
        }
7792
7793
        pods_query( "SET NAMES utf8" );
7794
        pods_query( "SET CHARACTER SET utf8" );
7795
7796
        // Loop through the array of items
7797
        $ids = array();
7798
7799
        // Test to see if it's an array of arrays
7800
        if ( !is_array( @current( $import_data ) ) )
7801
            $import_data = array( $import_data );
7802
7803
        $pod = $this->load_pod( array( 'name' => $this->pod ) );
7804
7805
        if ( false === $pod )
7806
            return pods_error( __( 'Pod not found', 'pods' ), $this );
7807
7808
        $fields = array_merge( $pod[ 'fields' ], $pod[ 'object_fields' ] );
7809
7810
        $simple_tableless_objects = PodsForm::simple_tableless_objects();
7811
7812
        foreach ( $import_data as $key => $data_row ) {
7813
            $data = array();
7814
7815
            // Loop through each field (use $fields so only valid fields get parsed)
7816
            foreach ( $fields as $field_name => $field_data ) {
7817
                if ( !isset( $data_row[ $field_name ] ) && !isset( $data_row[ $field_data[ 'label' ] ] ) )
7818
                    continue;
7819
7820
                $field_id = $field_data[ 'id' ];
7821
                $type = $field_data[ 'type' ];
7822
                $pick_object = isset( $field_data[ 'pick_object' ] ) ? $field_data[ 'pick_object' ] : '';
7823
                $pick_val = isset( $field_data[ 'pick_val' ] ) ?  $field_data[ 'pick_val' ] : '';
7824
7825
                if ( isset( $data_row[ $field_name] ) )
7826
                    $field_value = $data_row[ $field_name ];
7827
                else
7828
                    $field_value = $data_row[ $field_data[ 'label' ] ];
7829
7830
                if ( null !== $field_value && false !== $field_value && '' !== $field_value ) {
7831
                    if ( 'pick' == $type || in_array( $type, PodsForm::file_field_types() ) ) {
7832
                        $field_values = is_array( $field_value ) ? $field_value : array( $field_value );
7833
                        $pick_values = array();
7834
7835
                        foreach ( $field_values as $pick_value ) {
7836
                            if ( in_array( $type, PodsForm::file_field_types() ) || 'media' == $pick_object ) {
7837
                                $where = "`guid` = '" . pods_sanitize( $pick_value ) . "'";
7838
7839 View Code Duplication
                                if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7840
                                    $where = "`ID` = " . pods_absint( $pick_value );
7841
7842
                                $result = pods_query( "SELECT `ID` AS `id` FROM `{$wpdb->posts}` WHERE `post_type` = 'attachment' AND {$where} ORDER BY `ID`", $this );
7843
7844
                                if ( !empty( $result ) )
7845
                                    $pick_values[] = $result[ 0 ]->id;
7846
                            }
7847
                            // @todo This could and should be abstracted better and simplified
7848
                            elseif ( 'pick' == $type ) {
7849
                                $related_pod = false;
7850
7851
                                if ( 'pod' == $pick_object )
7852
                                    $related_pod = $this->load_pod( array( 'name' => $pick_val, 'table_info' => true ), false );
7853
7854
                                if ( empty( $related_pod ) ) {
7855
                                    $related_pod = array(
7856
                                        'id' => 0,
7857
                                        'type' => $pick_object
7858
                                    );
7859
                                }
7860
7861
                                if ( in_array( 'taxonomy', array( $pick_object, $related_pod[ 'type' ] ) ) ) {
7862
                                    $where = "`t`.`name` = '" . pods_sanitize( $pick_value ) . "'";
7863
7864 View Code Duplication
                                    if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7865
                                        $where = "`tt`.`term_id` = " . pods_absint( $pick_value );
7866
7867
                                    $result = pods_query( "SELECT `t`.`term_id` AS `id` FROM `{$wpdb->term_taxonomy}` AS `tt` LEFT JOIN `{$wpdb->terms}` AS `t` ON `t`.`term_id` = `tt`.`term_id` WHERE `taxonomy` = '{$pick_val}' AND {$where} ORDER BY `t`.`term_id`", $this );
7868
7869
                                    if ( !empty( $result ) )
7870
                                        $pick_values[] = $result[ 0 ]->id;
7871
                                }
7872
                                elseif ( in_array( 'post_type', array( $pick_object, $related_pod[ 'type' ] ) ) || in_array( 'media', array( $pick_object, $related_pod[ 'type' ] ) ) ) {
7873
                                    $where = "`post_title` = '" . pods_sanitize( $pick_value ) . "'";
7874
7875 View Code Duplication
                                    if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7876
                                        $where = "`ID` = " . pods_absint( $pick_value );
7877
7878
                                    $result = pods_query( "SELECT `ID` AS `id` FROM `{$wpdb->posts}` WHERE `post_type` = '{$pick_val}' AND {$where} ORDER BY `ID`", $this );
7879
7880
                                    if ( !empty( $result ) )
7881
                                        $pick_values[] = $result[ 0 ]->id;
7882
                                }
7883
                                elseif ( in_array( 'user', array( $pick_object, $related_pod[ 'type' ] ) ) ) {
7884
                                    $where = "`user_login` = '" . pods_sanitize( $pick_value ) . "'";
7885
7886 View Code Duplication
                                    if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7887
                                        $where = "`ID` = " . pods_absint( $pick_value );
7888
7889
                                    $result = pods_query( "SELECT `ID` AS `id` FROM `{$wpdb->users}` WHERE {$where} ORDER BY `ID`", $this );
7890
7891
                                    if ( !empty( $result ) )
7892
                                        $pick_values[] = $result[ 0 ]->id;
7893
                                }
7894
                                elseif ( in_array( 'comment', array( $pick_object, $related_pod[ 'type' ] ) ) ) {
7895
                                    $where = "`comment_ID` = " . pods_absint( $pick_value );
7896
7897
                                    $result = pods_query( "SELECT `comment_ID` AS `id` FROM `{$wpdb->comments}` WHERE {$where} ORDER BY `ID`", $this );
7898
7899
                                    if ( !empty( $result ) )
7900
                                        $pick_values[] = $result[ 0 ]->id;
7901
                                }
7902
                                elseif ( in_array( $pick_object, $simple_tableless_objects ) )
7903
                                    $pick_values[] = $pick_value;
7904
                                elseif ( !empty( $related_pod[ 'id' ] ) ) {
7905
                                    $where = "`" . $related_pod[ 'field_index' ] . "` = '" . pods_sanitize( $pick_value ) . "'";
7906
7907
                                    if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7908
                                        $where = "`" . $related_pod[ 'field_id' ] . "` = " . pods_absint( $pick_value );
7909
7910
                                    $result = pods_query( "SELECT `" . $related_pod[ 'field_id' ] . "` AS `id` FROM `" . $related_pod[ 'table' ] . "` WHERE {$where} ORDER BY `" . $related_pod[ 'field_id' ] . "`", $this );
7911
7912
                                    if ( !empty( $result ) )
7913
                                        $pick_values[] = $result[ 0 ]->id;
7914
                                }
7915
                            }
7916
                        }
7917
7918
                        $field_value = implode( ',', $pick_values );
7919
                    }
7920
7921
                    $data[ $field_name ] = $field_value;
7922
                }
7923
            }
7924
7925
            if ( !empty( $data ) ) {
7926
                $params = array(
7927
                    'pod' => $this->pod,
7928
                    'data' => $data
7929
                );
7930
7931
                $ids[] = $this->save_pod_item( $params );
7932
            }
7933
        }
7934
7935
        return $ids;
7936
    }
7937
7938
    /**
7939
     * Export data from a Pod
7940
     *
7941
     * @param string|object $pod The pod name or Pods object
7942
     * @param array $params An associative array of parameters
7943
     *
7944
     * @return array Data arrays of all exported pod items
7945
     * @since 1.7.1
7946
     */
7947
    public function export ( $pod = null, $params = null ) {
7948
7949
        if ( empty( $pod ) ) {
7950
            $pod = $this->pod;
7951
        }
7952
7953
        $find = array(
7954
            'limit' => -1,
7955
            'search' => false,
7956
            'pagination' => false
7957
        );
7958
7959
        if ( !empty( $params ) && isset( $params[ 'params' ] ) ) {
7960
            $find = array_merge( $find, (array) $params[ 'params' ] );
7961
7962
            unset( $params[ 'params' ] );
7963
7964
            $pod = pods( $pod, $find );
7965
        }
7966
        elseif ( !is_object( $pod ) ) {
7967
            $pod = pods( $pod, $find );
7968
        }
7969
7970
        $data = array();
7971
7972
        while ( $pod->fetch() ) {
7973
            $data[ $pod->id() ] = $this->export_pod_item( $params, $pod );
7974
        }
7975
7976
        $data = $this->do_hook( 'export', $data, $pod->pod, $pod );
7977
7978
        return $data;
7979
    }
7980
7981
    /**
7982
     * Convert CSV to a PHP array
7983
     *
7984
     * @param string $data The CSV input
7985
     *
7986
     * @return array
7987
     * @since 1.7.1
7988
     *
7989
     * @deprecated 2.3.5
7990
     */
7991
    public function csv_to_php ( $data, $delimiter = ',' ) {
7992
        pods_deprecated( "PodsAPI->csv_to_php", '2.3.5' );
7993
7994
        $data = pods_migrate( 'sv', $delimiter, $data )->parse();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as pods_migrate('sv', $delimiter, $data)->parse() (which targets PodsMigrate::parse()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
7995
7996
        return $data[ 'items' ];
7997
    }
7998
7999
    /**
8000
     * Clear Pod-related cache
8001
     *
8002
     * @param array $pod
8003
     *
8004
     * @return void
8005
     *
8006
     * @since 2.0
8007
     */
8008
    public function cache_flush_pods ( $pod = null ) {
8009
        /**
8010
         * @var $wpdb wpdb
8011
         */
8012
        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...
8013
8014
        pods_transient_clear( 'pods' );
8015
        pods_transient_clear( 'pods_components' );
8016
8017
        if ( null !== $pod && is_array( $pod ) ) {
8018
            pods_transient_clear( 'pods_pod_' . $pod[ 'name' ] );
8019
            pods_cache_clear( $pod[ 'name' ], 'pods-class' );
8020
8021
            foreach ( $pod[ 'fields' ] as $field ) {
8022
                pods_transient_clear( 'pods_field_' . $pod[ 'name' ] . '_' . $field[ 'name' ] );
8023
            }
8024
8025
            if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) )
8026
                pods_transient_clear( 'pods_wp_cpt_ct' );
8027
        }
8028
        else
8029
            pods_transient_clear( 'pods_wp_cpt_ct' );
8030
8031
        $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '_transient_pods%'" );
8032
        $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '_transient_timeout_pods%'" );
8033
8034
        pods_cache_clear( true );
8035
8036
        pods_transient_set( 'pods_flush_rewrites', 1 );
8037
    }
8038
8039
    /**
8040
     * Process a Pod-based form
8041
     *
8042
     * @param mixed $params
8043
     * @param object $obj Pod object
8044
     * @param array $fields Fields being submitted in form ( key => settings )
8045
     * @param string $thank_you URL to send to upon success
8046
     *
8047
     * @return mixed
8048
     *
8049
     * @since 2.0
8050
     */
8051
    public function process_form ( $params, $obj = null, $fields = null, $thank_you = null ) {
8052
        $this->display_errors = false;
8053
8054
        $form = null;
8055
8056
        $nonce = pods_var( '_pods_nonce', $params );
8057
        $pod = pods_var( '_pods_pod', $params );
8058
        $id = pods_var( '_pods_id', $params );
8059
        $uri = pods_var( '_pods_uri', $params );
8060
        $form = pods_var( '_pods_form', $params );
8061
        $location = pods_var( '_pods_location', $params );
8062
8063
        if ( is_object( $obj ) ) {
8064
            $pod = $obj->pod;
8065
            $id = $obj->id();
8066
        }
8067
8068
        if ( !empty( $fields ) ) {
8069
            $fields = array_keys( $fields );
8070
            $form = implode( ',', $fields );
8071
        }
8072
        else
8073
            $fields = explode( ',', $form );
8074
8075
        if ( empty( $nonce ) || empty( $pod ) || empty( $uri ) || empty( $fields ) )
8076
            return pods_error( __( 'Invalid submission', 'pods' ), $this );
8077
8078
        $uid = @session_id();
8079
8080
        if ( is_user_logged_in() )
8081
            $uid = 'user_' . get_current_user_id();
8082
8083
		$field_hash = wp_create_nonce( 'pods_fields_' . $form );
8084
8085
        $action = 'pods_form_' . $pod . '_' . $uid . '_' . $id . '_' . $uri . '_' . $field_hash;
8086
8087
        if ( empty( $uid ) )
8088
            return pods_error( __( 'Access denied for your session, please refresh and try again.', 'pods' ), $this );
8089
8090
        if ( false === wp_verify_nonce( $nonce, $action ) )
8091
            return pods_error( __( 'Access denied, please refresh and try again.', 'pods' ), $this );
8092
8093
        $data = array();
8094
8095
        foreach ( $fields as $field ) {
8096
            $data[ $field ] = pods_var_raw( 'pods_field_' . $field, $params, '' );
8097
        }
8098
8099
        $params = array(
8100
            'pod' => $pod,
8101
            'id' => $id,
8102
            'data' => $data,
8103
            'from' => 'process_form',
8104
			'location' => $location
8105
        );
8106
8107
        $id = $this->save_pod_item( $params );
8108
8109
        if ( 0 < $id && !empty( $thank_you ) ) {
8110
            $thank_you = str_replace( 'X_ID_X', $id, $thank_you );
8111
8112
            pods_redirect( $thank_you );
8113
        }
8114
8115
        return $id;
8116
    }
8117
8118
    /**
8119
     * Handle filters / actions for the class
8120
     *
8121
     * @since 2.0
8122
     */
8123 View Code Duplication
    private function do_hook () {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
8124
        $args = func_get_args();
8125
        if ( empty( $args ) )
8126
            return false;
8127
        $name = array_shift( $args );
8128
        return pods_do_hook( "api", $name, $args, $this );
8129
    }
8130
8131
    /**
8132
     * Handle variables that have been deprecated
8133
     *
8134
     * @since 2.0
8135
     */
8136
    public function __get ( $name ) {
8137
        $name = (string) $name;
8138
8139 View Code Duplication
        if ( !isset( $this->deprecated ) ) {
8140
            require_once( PODS_DIR . 'deprecated/classes/PodsAPI.php' );
8141
            $this->deprecated = new PodsAPI_Deprecated( $this );
8142
        }
8143
8144
        $var = null;
8145
8146
        if ( isset( $this->deprecated->{$name} ) ) {
8147
            pods_deprecated( "PodsAPI->{$name}", '2.0' );
8148
8149
            $var = $this->deprecated->{$name};
8150
        }
8151
        else
8152
            pods_deprecated( "PodsAPI->{$name}", '2.0' );
8153
8154
        return $var;
8155
    }
8156
8157
    /**
8158
     * Handle methods that have been deprecated
8159
     *
8160
     * @since 2.0
8161
     */
8162
    public function __call ( $name, $args ) {
8163
        $name = (string) $name;
8164
8165 View Code Duplication
        if ( !isset( $this->deprecated ) ) {
8166
            require_once( PODS_DIR . 'deprecated/classes/PodsAPI.php' );
8167
            $this->deprecated = new PodsAPI_Deprecated( $this );
8168
        }
8169
8170
        if ( method_exists( $this->deprecated, $name ) )
8171
            return call_user_func_array( array( $this->deprecated, $name ), $args );
8172
        else
8173
            pods_deprecated( "PodsAPI::{$name}", '2.0' );
8174
    }
8175
}
8176