Completed
Pull Request — 2.x (#3273)
by Scott Kingsley
09:28 queued 04:03
created

PodsMeta::get_object()   F

Complexity

Conditions 32
Paths 686

Size

Total Lines 92
Code Lines 62

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 92
rs 2.1739
cc 32
eloc 62
nc 686
nop 3

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package Pods
4
 */
5
class PodsMeta {
6
7
    /**
8
     * @var PodsMeta
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 PodsAPI
14
     */
15
    private $api;
0 ignored issues
show
Unused Code introduced by
The property $api is not used and could be removed.

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

Loading history...
16
17
    /**
18
     * @var Pods
19
     */
20
    private static $current_pod;
21
22
    /**
23
     * @var array
24
     */
25
    private static $current_pod_data;
26
27
    /**
28
     * @var Pods
29
     */
30
    private static $current_field_pod;
31
32
    /**
33
     * @var int
34
     */
35
    public static $object_identifier = -1;
36
37
    /**
38
     * @var array
39
     */
40
    public static $advanced_content_types = array();
41
42
    /**
43
     * @var array
44
     */
45
    public static $post_types = array();
46
47
    /**
48
     * @var array
49
     */
50
    public static $taxonomies = array();
51
52
    /**
53
     * @var array
54
     */
55
    public static $media = array();
56
57
    /**
58
     * @var array
59
     */
60
    public static $user = array();
61
62
    /**
63
     * @var array
64
     */
65
    public static $comment = array();
66
67
    /**
68
     * @var array
69
     */
70
    public static $settings = array();
71
72
    /**
73
     * @var array
74
     */
75
    public static $queue = array();
76
77
    /**
78
     * @var array
79
     */
80
    public static $groups = array();
81
82
    /**
83
     * @var string
84
     */
85
    public static $old_post_status = '';
86
87
    /**
88
     * Singleton handling for a basic pods_meta() request
89
     *
90
     * @return \PodsMeta
91
     *
92
     * @since 2.3.5
93
     */
94
    public static function init () {
95
        if ( !is_object( self::$instance ) )
96
            self::$instance = new PodsMeta();
97
98
        return self::$instance;
99
    }
100
101
    /**
102
     * @return \PodsMeta
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

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

Please refer to the PHP core documentation on constructors.

Loading history...
103
     *
104
     * @since 2.0
105
     */
106
    function __construct () {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

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

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

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

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

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

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

There are different options of fixing this problem.

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

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

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

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

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

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

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

Loading history...
290
            foreach ( self::$settings as $setting_pod ) {
291
                foreach ( $setting_pod[ 'fields' ] as $option ) {
292
                    add_filter( 'pre_option_' . $setting_pod[ 'name' ] . '_' . $option[ 'name' ], array( $this, 'get_option' ), 10, 1 );
293
                    add_action( 'add_option_' . $setting_pod[ 'name' ] . '_' . $option[ 'name' ], array( $this, 'add_option' ), 10, 2 );
294
                    add_filter( 'pre_update_option_' . $setting_pod[ 'name' ] . '_' . $option[ 'name' ], array( $this, 'update_option' ), 10, 2 );
295
                }
296
            }
297
        }*/
298
299
        if ( is_admin() )
300
            $this->integrations();
301
302
        add_action( 'init', array( $this, 'enqueue' ), 9 );
303
304
        if ( function_exists( 'pll_current_language' ) )
305
            add_action( 'init', array( $this, 'cache_pods' ), 101 );
306
307
        do_action( 'pods_meta_init' );
308
309
        return $this;
310
    }
311
312
    public static function enqueue () {
313
        foreach ( self::$queue as $type => $objects ) {
314
            foreach ( $objects as $pod_name => $pod ) {
315
                pods_transient_set( 'pods_pod_' . $pod_name, $pod );
316
            }
317
318
            self::$$type = array_merge( self::$$type, $objects );
319
        }
320
    }
321
322
    /**
323
     * Go back through and cache the Pods now that Polylang has loaded
324
     */
325
    public function cache_pods () {
326
        self::$advanced_content_types = pods_api()->load_pods( array( 'type' => 'pod', 'refresh' => true ) );
327
        self::$post_types = pods_api()->load_pods( array( 'type' => 'post_type', 'refresh' => true ) );
328
        self::$taxonomies = pods_api()->load_pods( array( 'type' => 'taxonomy', 'refresh' => true ) );
329
        self::$media = pods_api()->load_pods( array( 'type' => 'media', 'refresh' => true ) );
330
        self::$user = pods_api()->load_pods( array( 'type' => 'user', 'refresh' => true ) );
331
        self::$comment = pods_api()->load_pods( array( 'type' => 'comment', 'refresh' => true ) );
332
        self::$settings = pods_api()->load_pods( array( 'type' => 'settings', 'refresh' => true ) );
333
    }
334
335
    public function register ( $type, $pod ) {
336
        $pod_type = $type;
337
338
        if ( 'post_type' == $type )
339
            $type = 'post_types';
340
        elseif ( 'taxonomy' == $type )
341
            $type = 'taxonomies';
342
        elseif ( 'pod' == $type )
343
            $type = 'advanced_content_types';
344
345
        if ( !isset( self::$queue[ $type ] ) )
346
            self::$queue[ $type ] = array();
347
348 View Code Duplication
        if ( is_array( $pod ) && !empty( $pod ) && !isset( $pod[ 'name' ] ) ) {
349
            $data = array();
350
351
            foreach ( $pod as $p ) {
352
                $data[] = $this->register( $type, $p );
353
            }
354
355
            return $data;
356
        }
357
358
        $pod[ 'type' ] = $pod_type;
359
        $pod = pods_api()->save_pod( $pod, false, false );
360
361
        if ( !empty( $pod ) ) {
362
            self::$object_identifier--;
363
364
            self::$queue[ $type ][ $pod[ 'name' ] ] = $pod;
365
366
            return $pod;
367
        }
368
369
        return false;
370
    }
371
372
    public function register_field ( $pod, $field ) {
373 View Code Duplication
        if ( is_array( $pod ) && !empty( $pod ) && !isset( $pod[ 'name' ] ) ) {
374
            $data = array();
375
376
            foreach ( $pod as $p ) {
377
                $data[] = $this->register_field( $p, $field );
378
            }
379
380
            return $data;
381
        }
382
383 View Code Duplication
        if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $pod )
384
            self::$current_pod_data = pods_api()->load_pod( array( 'name' => $pod ), false );
385
386
        $pod = self::$current_pod_data;
387
388
        if ( !empty( $pod ) ) {
389
            $type = $pod[ 'type' ];
390
391
            if ( 'post_type' == $pod[ 'type' ] )
392
                $type = 'post_types';
393
            elseif ( 'taxonomy' == $pod[ 'type' ] )
394
                $type = 'taxonomies';
395
            elseif ( 'pod' == $pod[ 'type' ] )
396
                $type = 'advanced_content_types';
397
398
            if ( !isset( self::$queue[ $pod[ 'type' ] ] ) )
399
                self::$queue[ $type ] = array();
400
401
            $field = pods_api()->save_field( $field, false, false, $pod[ 'id' ] );
402
403
            if ( !empty( $field ) ) {
404
                $pod[ 'fields' ][ $field[ 'name' ] ] = $field;
405
406
                self::$queue[ $type ][ $pod[ 'name' ] ] = $pod;
407
408
                return $field;
409
            }
410
        }
411
412
        return false;
413
    }
414
415
    public function integrations () {
416
        // Codepress Admin Columns 2.x
417
		add_filter( 'cac/storage_model/meta_keys', array( $this, 'cpac_meta_keys' ), 10, 2 );
418
        add_filter( 'cac/post_types', array( $this, 'cpac_post_types' ), 10, 1 );
419
        add_filter( 'cac/column/meta/value', array( $this, 'cpac_meta_value' ), 10, 3 );
420
    }
421
422
423
    public function cpac_meta_keys ( $meta_fields, $storage_model ) {
424
        $object_type = 'post_type';
425
        $object = $storage_model->key;
426
427
        if ( in_array( $storage_model->key, array( 'wp-links', 'link' ) ) ) {
428
            $object_type = $object = 'link';
429
        }
430
        elseif ( in_array( $storage_model->key, array( 'wp-media', 'media' ) ) ) {
431
            $object_type = $object = 'media';
432
        }
433
        elseif ( in_array( $storage_model->key, array( 'wp-users', 'user' ) ) ) {
434
            $object_type = $object = 'user';
435
        }
436
        elseif ( in_array( $storage_model->key, array( 'wp-comments', 'comment' ) ) ) {
437
            $object_type = $object = 'comment';
438
        }
439
        elseif ( 'taxonomy' === $storage_model->type ) {
440
            $object_type = 'taxonomy';
441
            $object = $storage_model->taxonomy;
442
        }
443
444 View Code Duplication
        if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $object )
445
            self::$current_pod_data = pods_api()->load_pod( array( 'name' => $object ), false );
446
447
        $pod = self::$current_pod_data;
448
449
        // Add Pods fields
450
        if ( !empty( $pod ) && $object_type == $pod[ 'type' ] ) {
451
            foreach ( $pod[ 'fields' ] as $field => $field_data ) {
452
                if ( !is_array( $meta_fields ) )
453
                    $meta_fields = array();
454
455
                if ( !in_array( $field, $meta_fields ) )
456
                    $meta_fields[] = $field;
457
            }
458
        }
459
460
        // Remove internal Pods fields
461
        if ( is_array( $meta_fields ) ) {
462
            foreach ( $meta_fields as $k => $meta_field ) {
463
                if ( 0 === strpos( $meta_field, '_pods_' ) )
464
                    unset( $meta_fields[ $k ] );
465
            }
466
        }
467
468
        return $meta_fields;
469
    }
470
471
    public function cpac_post_types ( $post_types ) {
472
        // Remove internal Pods post types
473
        foreach ( $post_types as $post_type => $post_type_name ) {
474
            if ( 0 === strpos( $post_type, '_pods_' ) || 0 === strpos( $post_type_name, '_pods_' ) )
475
                unset( $post_types[ $post_type ] );
476
        }
477
478
        return $post_types;
479
    }
480
481
    public function cpac_meta_value ( $meta, $id, $obj ) {
482
        $tableless_field_types = PodsForm::tableless_field_types();
483
484
        $object_type = 'post_type';
485
        $object = $obj->storage_model->key;
486
487
        if ( in_array( $obj->storage_model->type, array( 'wp-links', 'link' ) ) ) {
488
            $object_type = $object = 'link';
489
        }
490
        elseif ( in_array( $obj->storage_model->type, array( 'wp-media', 'media' ) ) ) {
491
            $object_type = $object = 'media';
492
        }
493
        elseif ( in_array( $obj->storage_model->type, array( 'wp-users', 'user' ) ) ) {
494
            $object_type = $object = 'user';
495
        }
496
        elseif ( in_array( $obj->storage_model->type, array( 'wp-comments', 'comment' ) ) ) {
497
            $object_type = $object = 'comment';
498
        }
499
        elseif ( 'taxonomy' === $obj->storage_model->type ) {
500
            $object_type = 'taxonomy';
501
            $object = $obj->storage_model->taxonomy;
502
        }
503
504
        $field = substr( $obj->options->field, 0, 10 ) == "cpachidden" ? str_replace( 'cpachidden', '', $obj->options->field ) : $obj->options->field;
505
        $field_type = $obj->options->field_type;
506
507 View Code Duplication
        if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $object )
508
            self::$current_pod_data = pods_api()->load_pod( array( 'name' => $object ), false );
509
510
        $pod = self::$current_pod_data;
511
512
        // Add Pods fields
513
        if ( !empty( $pod ) && isset( $pod[ 'fields' ][ $field ] ) ) {
514
            if ( in_array( $pod[ 'type' ], array( 'post_type', 'media', 'taxonomy', 'user', 'comment', 'media' ) ) && ( !empty( $field_type ) || in_array( $pod[ 'fields' ][ $field ][ 'type' ], $tableless_field_types ) ) ) {
515
                $metadata_type = $pod['type'];
516
517
                if ( in_array( $metadata_type, array( 'post_type', 'media' ) ) ) {
518
                    $metadata_type = 'post';
519
                } elseif ( 'taxonomy' == $metadata_type ) {
520
                    $metadata_type = 'term';
521
                }
522
523
                if ( 'term' == $metadata_type && ! function_exists( 'get_term_meta' ) ) {
524
                    $podterms = pods( $pod['name'], $id );
525
526
                    $meta = $podterms->field( $field );
527
                } else {
528
                    $meta = get_metadata( $metadata_type, $id, $field, true );
529
                }
530
            }
531
            elseif ( 'taxonomy' == $pod['type'] ) {
532
                $podterms = pods( $pod['name'], $id );
533
534
                $meta = $podterms->field( $field );
535
            }
536
537
            $meta = PodsForm::field_method( $pod[ 'fields' ][ $field ][ 'type' ], 'ui', $id, $meta, $field, array_merge( $pod[ 'fields' ][ $field ], $pod[ 'fields' ][ $field ][ 'options' ] ), $pod[ 'fields' ], $pod );
538
        }
539
540
        return $meta;
541
    }
542
543
    public function cpac_meta_values ( $meta, $field_type, $field, $type, $id ) {
544
        $tableless_field_types = PodsForm::tableless_field_types();
545
546
        $object = $type;
547
548
        if ( 'wp-media' == $type )
549
            $object = 'media';
550
        elseif ( 'wp-users' == $type )
551
            $object = 'user';
552
        elseif ( 'wp-comments' == $type )
553
            $object = 'comment';
554
555 View Code Duplication
        if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $object )
556
            self::$current_pod_data = pods_api()->load_pod( array( 'name' => $object ), false );
557
558
        $pod = self::$current_pod_data;
559
560
        // Add Pods fields
561
        if ( !empty( $pod ) && isset( $pod[ 'fields' ][ $field ] ) ) {
562
            if ( in_array( $pod[ 'type' ], array( 'post_type', 'user', 'taxonomy', 'comment', 'media' ) ) && ( !empty( $field_type ) || in_array( $pod[ 'fields' ][ $field ][ 'type' ], $tableless_field_types ) ) ) {
563
                $metadata_type = $pod['type'];
564
565
                if ( in_array( $metadata_type, array( 'post_type', 'media' ) ) ) {
566
                    $metadata_type = 'post';
567
                } elseif ( 'taxonomy' == $metadata_type ) {
568
                    $metadata_type = 'term';
569
                }
570
571
                $meta = get_metadata( $metadata_type, $id, $field, true );
572
            }
573
574
            $meta = PodsForm::field_method( $pod[ 'fields' ][ $field ][ 'type' ], 'ui', $id, $meta, $field, array_merge( $pod[ 'fields' ][ $field ], $pod[ 'fields' ][ $field ][ 'options' ] ), $pod[ 'fields' ], $pod );
575
        }
576
577
        return $meta;
578
    }
579
580
    /**
581
     * Add a meta group of fields to add/edit forms
582
     *
583
     * @param string|array $pod The pod or type of element to attach the group to.
584
     * @param string $label Title of the edit screen section, visible to user.
585
     * @param string|array $fields Either a comma separated list of text fields or an associative array containing field infomration.
586
     * @param string $context (optional) The part of the page where the edit screen section should be shown ('normal', 'advanced', or 'side').
587
     * @param string $priority (optional) The priority within the context where the boxes should show ('high', 'core', 'default' or 'low').
588
     *
589
     * @since 2.0
590
     *
591
     * @return mixed|void
592
     */
593
    public function group_add ( $pod, $label, $fields, $context = 'normal', $priority = 'default' ) {
594 View Code Duplication
        if ( is_array( $pod ) && !empty( $pod ) && !isset( $pod[ 'name' ] ) ) {
595
            foreach ( $pod as $p ) {
596
                $this->group_add( $pod, $label, $fields, $context, $priority );
597
            }
598
599
            return true;
600
        }
601
602
        if ( !is_array( $pod ) ) {
603 View Code Duplication
            if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $pod )
604
                self::$current_pod_data = pods_api()->load_pod( array( 'name' => $pod ), false );
605
606
            if ( !empty( self::$current_pod_data ) )
607
                $pod = self::$current_pod_data;
608
            else {
609
                $type = 'post_type';
610
611
                if ( in_array( $pod, array( 'media', 'user', 'comment' ) ) )
612
                    $type = $pod;
613
614
                $pod = array(
615
                    'name' => $pod,
616
                    'type' => $type
617
                );
618
            }
619
        }
620
621
        if ( is_array( $pod ) && !isset( $pod[ 'id' ] ) ) {
622
            $defaults = array(
623
                'name' => '',
624
                'type' => 'post_type'
625
            );
626
627
            $pod = array_merge( $defaults, $pod );
628
        }
629
630
        if ( 'post' == $pod[ 'type' ] )
631
            $pod[ 'type' ] = 'post_type';
632
633
        if ( empty( $pod[ 'name' ] ) && isset( $pod[ 'object' ] ) && !empty( $pod[ 'object' ] ) )
634
            $pod[ 'name' ] = $pod[ 'object' ];
635 View Code Duplication
        elseif ( !isset( $pod[ 'object' ] ) || empty( $pod[ 'object' ] ) )
636
            $pod[ 'object' ] = $pod[ 'name' ];
637
638
        if ( empty( $pod[ 'object' ] ) )
639
            return pods_error( __( 'Object required to add a Pods meta group', 'pods' ) );
640
641
        $object_name = $pod[ 'object' ];
642
643
        if ( 'pod' == $pod[ 'type' ] )
644
            $object_name = $pod[ 'name' ];
645
646 View Code Duplication
        if ( !isset( self::$groups[ $pod[ 'type' ] ] ) )
647
            self::$groups[ $pod[ 'type' ] ] = array();
648
649 View Code Duplication
        if ( !isset( self::$groups[ $pod[ 'type' ] ][ $object_name ] ) )
650
            self::$groups[ $pod[ 'type' ] ][ $object_name ] = array();
651
652
        $_fields = array();
653
654
        if ( !is_array( $fields ) )
655
            $fields = explode( ',', $fields );
656
657
        foreach ( $fields as $k => $field ) {
658
            $name = $k;
659
660
            $defaults = array(
661
                'name' => $name,
662
                'label' => $name,
663
                'type' => 'text'
664
            );
665
666
            if ( !is_array( $field ) ) {
667
                $name = trim( $field );
668
669
                $field = array(
670
                    'name' => $name,
671
                    'label' => $name
672
                );
673
            }
674
675
            $field = array_merge( $defaults, $field );
676
677
            $field[ 'name' ] = trim( $field[ 'name' ] );
678
679 View Code Duplication
            if ( isset( $pod[ 'fields' ] ) && isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) )
680
                $field = array_merge( $field, $pod[ 'fields' ][ $field[ 'name' ] ] );
681
682
            $_fields[ $k ] = $field;
683
        }
684
685
        // Setup field options
686
        $fields = PodsForm::fields_setup( $_fields );
687
688
        $group = array(
689
            'pod' => $pod,
690
            'label' => $label,
691
            'fields' => $fields,
692
            'context' => $context,
693
            'priority' => $priority
694
        );
695
696
        // Filter group data, pass vars separately for reference down the line (in case array changed by other filter)
697
        $group = apply_filters( 'pods_meta_group_add_' . $pod[ 'type' ] . '_' . $object_name, $group, $pod, $label, $fields );
698
        $group = apply_filters( 'pods_meta_group_add_' . $pod[ 'type' ], $group, $pod, $label, $fields );
699
        $group = apply_filters( 'pods_meta_group_add', $group, $pod, $label, $fields );
700
701
        self::$groups[ $pod[ 'type' ] ][ $object_name ][] = $group;
702
703
        // Hook it up!
704
        if ( 'post_type' == $pod[ 'type' ] ) {
705
            if ( !has_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) ) )
706
                add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
707
708
            /*if ( !has_action( 'save_post', array( $this, 'save_post' ), 10, 3 ) )
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
709
                add_action( 'save_post', array( $this, 'save_post' ), 10, 3 );*/
710
        }
711
        elseif ( 'taxonomy' == $pod[ 'type' ] ) {
712
            if ( !has_action( $pod[ 'object' ] . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 ) ) {
713
                add_action( $pod[ 'object' ] . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 );
714
                add_action( $pod[ 'object' ] . '_add_form_fields', array( $this, 'meta_taxonomy' ), 10, 1 );
715
            }
716
717
            if ( !has_action( 'edited_term', array( $this, 'save_taxonomy' ), 10, 3 ) ) {
718
                add_action( 'edited_term', array( $this, 'save_taxonomy' ), 10, 3 );
719
                add_action( 'create_term', array( $this, 'save_taxonomy' ), 10, 3 );
720
            }
721
        }
722
        elseif ( 'media' == $pod[ 'type' ] ) {
723
            if ( !has_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 ) ) {
724 View Code Duplication
                if ( pods_version_check( 'wp', '3.5' ) ) {
725
                    add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
726
                    add_action( 'wp_ajax_save-attachment-compat', array( $this, 'save_media_ajax' ), 0 );
727
                }
728
729
                add_filter( 'attachment_fields_to_edit', array( $this, 'meta_media' ), 10, 2 );
730
731
                add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 );
732
                add_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 );
733
            }
734
        }
735
        elseif ( 'user' == $pod[ 'type' ] ) {
736
            if ( !has_action( 'show_user_profile', array( $this, 'meta_user' ) ) ) {
737
                add_action( 'show_user_profile', array( $this, 'meta_user' ) );
738
                add_action( 'edit_user_profile', array( $this, 'meta_user' ) );
739
                //add_action( 'user_register', array( $this, 'save_user' ) );
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
740
                add_action( 'profile_update', array( $this, 'save_user' ) );
741
            }
742
        }
743
        elseif ( 'comment' == $pod[ 'type' ] ) {
744
            if ( !has_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 ) ) {
745
                add_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 );
746
                add_filter( 'comment_form_default_fields', array( $this, 'meta_comment_new' ) );
747
                add_action( 'add_meta_boxes_comment', array( $this, 'meta_comment_add' ) );
748
                add_action( 'wp_insert_comment', array( $this, 'save_comment' ) );
749
                add_action( 'edit_comment', array( $this, 'save_comment' ) );
750
            }
751
        }
752
    }
753
754
    public function object_get ( $type, $name ) {
755
        $object = self::$post_types;
756
757
        if ( 'taxonomy' == $type )
758
            $object = self::$taxonomies;
759
        elseif ( 'media' == $type )
760
            $object = self::$media;
761
        elseif ( 'user' == $type )
762
            $object = self::$user;
763
        elseif ( 'comment' == $type )
764
            $object = self::$comment;
765
766
        if ( 'pod' != $type && !empty( $object ) && is_array( $object ) && isset( $object[ $name ] ) )
767
            $pod = $object[ $name ];
768 View Code Duplication
        else {
769
            if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $name )
770
                self::$current_pod_data = pods_api()->load_pod( array( 'name' => $name ), false );
771
772
            $pod = self::$current_pod_data;
773
        }
774
775
        if ( empty( $pod ) )
776
            return array();
777
778
        $defaults = array(
779
            'name' => 'post',
780
            'object' => 'post',
781
            'type' => 'post_type'
782
        );
783
784
        $pod = array_merge( $defaults, (array) $pod );
785
786 View Code Duplication
        if ( empty( $pod[ 'name' ] ) )
787
            $pod[ 'name' ] = $pod[ 'object' ];
788
        elseif ( empty( $pod[ 'object' ] ) )
789
            $pod[ 'object' ] = $pod[ 'name' ];
790
791
        if ( $pod[ 'type' ] != $type )
792
            return array();
793
794
        return $pod;
795
    }
796
797
    /**
798
     * @param $type
799
     * @param $name
800
     * @param $default_fields
801
     *
802
     * @return array
803
     */
804
    public function groups_get ( $type, $name, $default_fields = null ) {
805
        if ( 'post_type' == $type && 'attachment' == $name ) {
806
            $type = 'media';
807
            $name = 'media';
808
        }
809
810
        do_action( 'pods_meta_groups', $type, $name );
811
812
        $pod = array();
813
        $fields = array();
814
815
        $object = self::$post_types;
816
817
        if ( 'taxonomy' == $type )
818
            $object = self::$taxonomies;
819
        elseif ( 'media' == $type )
820
            $object = self::$media;
821
        elseif ( 'user' == $type )
822
            $object = self::$user;
823
        elseif ( 'comment' == $type )
824
            $object = self::$comment;
825
        elseif ( 'pod' == $type )
826
            $object = self::$advanced_content_types;
827
828
        if ( !empty( $object ) && is_array( $object ) && isset( $object[ $name ] ) )
829
            $fields = $object[ $name ][ 'fields' ];
830
        else {
831 View Code Duplication
            if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $name )
832
                self::$current_pod_data = pods_api()->load_pod( array( 'name' => $name ), false );
833
834
            $pod = self::$current_pod_data;
835
836
            if ( !empty( $pod ) )
837
                $fields = $pod[ 'fields' ];
838
        }
839
840
        if ( null !== $default_fields ) {
841
            $fields = $default_fields;
842
        }
843
844
        $defaults = array(
845
            'name' => 'post',
846
            'object' => 'post',
847
            'type' => 'post_type'
848
        );
849
850
        $pod = array_merge( $defaults, (array) $pod );
851
852 View Code Duplication
        if ( empty( $pod[ 'name' ] ) )
853
            $pod[ 'name' ] = $pod[ 'object' ];
854
        elseif ( empty( $pod[ 'object' ] ) )
855
            $pod[ 'object' ] = $pod[ 'name' ];
856
857
        if ( $pod[ 'type' ] != $type )
858
            return array();
859
860
        $groups = array(
861
            array(
862
                'pod' => $pod,
863
				/**
864
				 * Filter the title of the Pods Metabox In The Post Editor
865
				 *
866
				 * @param string $title The title to use, default is 'More Fields'
867
				 * @param obj|Pod $pod Current Pods Object
868
				 * @param array $fields Array of fields that will go in the metabox
869
				 * @param string $type The type of Pod
870
				 * @params string $name Name of the Pod
871
				 *
872
				 * @returns string The title for the metabox.
873
				 *
874
				 * @since unknown
875
				 */
876
				'label' => apply_filters( 'pods_meta_default_box_title', __( 'More Fields', 'pods' ), $pod, $fields, $type, $name ),
877
                'fields' => $fields,
878
                'context' => 'normal',
879
                'priority' => 'default'
880
            )
881
        );
882
883
        if ( isset( self::$groups[ $type ] ) && isset( self::$groups[ $type ][ $name ] ) )
884
            $groups = self::$groups[ $type ][ $name ];
885
886
        return $groups;
887
    }
888
889
    /**
890
     * @param $post_type
891
     * @param null $post
892
     */
893
    public function meta_post_add ( $post_type, $post = null ) {
894
        if ( 'comment' == $post_type )
895
            return;
896
897
        if ( is_object( $post ) )
898
            $post_type = $post->post_type;
899
900
        $groups = $this->groups_get( 'post_type', $post_type );
901
        $pods_field_found = false;
902
903
        foreach ( $groups as $group ) {
904
            if ( empty( $group[ 'fields' ] ) )
905
                continue;
906
907
            $field_found = false;
908
909
            foreach ( $group[ 'fields' ] as $field ) {
910
                if ( false !== PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ] ) ) {
911
                    $field_found = true;
912
                    break;
913
                }
914
            }
915
916
            if ( empty( $group[ 'label' ] ) )
917
                $group[ 'label' ] = get_post_type_object( $post_type )->labels->label;
918
919 View Code Duplication
            if ( $field_found ) {
920
                $pods_field_found = true;
921
                add_meta_box(
922
                    'pods-meta-' . sanitize_title( $group[ 'label' ] ),
923
                    $group[ 'label' ],
924
                    array( $this, 'meta_post' ),
925
                    $post_type,
926
                    $group[ 'context' ],
927
                    $group[ 'priority' ],
928
                    array( 'group' => $group )
929
                );
930
931
            }
932
        }
933
934
		if ( $pods_field_found ) {
935
			// Only add the classes to forms that actually have pods fields
936
			add_action( 'post_edit_form_tag', array( $this, 'add_class_submittable' ) );
937
		}
938
    }
939
940
    /**
941
     *
942
     * Called by 'post_edit_form_tag' action to include the classes in the <form> tag
943
     *
944
     */
945
    public function add_class_submittable () {
946
        echo ' class="pods-submittable pods-form"';
947
    }
948
949
    /**
950
     * @param $post
951
     * @param $metabox
952
     */
953
    public function meta_post ( $post, $metabox ) {
954
        wp_enqueue_style( 'pods-form' );
955
        wp_enqueue_script( 'pods' );
956
957
		$pod_type = 'post';
958
959
		if ( 'attachment' == $post->post_type ) {
960
			$pod_type = 'media';
961
		}
962
963
        do_action( 'pods_meta_' . __FUNCTION__, $post );
964
965
        $hidden_fields = array();
966
?>
967
    <table class="form-table pods-metabox pods-admin pods-dependency">
968
		<?php echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_' . $pod_type ), 'hidden' ); ?>
969
970
        <?php
971
        $id = null;
972
973
        if ( is_object( $post ) && false === strpos( $_SERVER[ 'REQUEST_URI' ], '/post-new.php' ) )
974
            $id = $post->ID;
975
976 View Code Duplication
        if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod ) || self::$current_pod->pod != $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ] )
977
            self::$current_pod = pods( $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ], $id, true );
978
		elseif ( self::$current_pod->id() != $id )
979
			self::$current_pod->fetch( $id );
980
981
        $pod = self::$current_pod;
982
983
        foreach ( $metabox[ 'args' ][ 'group' ][ 'fields' ] as $field ) {
984
            if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field[ 'options' ], $metabox[ 'args' ][ 'group' ][ 'fields' ], $pod, $id ) ) {
985
                if ( pods_var( 'hidden', $field[ 'options' ], false ) )
986
                    $field[ 'type' ] = 'hidden';
987
                else
988
                    continue;
989
            }
990
            elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
991
                $field[ 'type' ] = 'hidden';
992
993
            $value = '';
994
995 View Code Duplication
            if ( !empty( $pod ) ) {
996
                pods_no_conflict_on( 'post' );
997
998
                $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
999
1000
                pods_no_conflict_off( 'post' );
1001
            }
1002
            elseif ( !empty( $id ) )
1003
                $value = get_post_meta( $id, $field[ 'name' ], true );
1004
1005
            if ( 'hidden' == $field[ 'type' ] ) {
1006
                $hidden_fields[] = array(
1007
                    'field' => $field,
1008
                    'value' => $value
1009
                );
1010
            }
1011
            else {
1012
                $depends = PodsForm::dependencies( $field, 'pods-meta-' );
1013
1014
            do_action( 'pods_meta_' . __FUNCTION__ . '_' . $field[ 'name' ], $post, $field, $pod );
1015
        ?>
1016
            <tr class="form-field pods-field pods-field-input <?php echo esc_attr( 'pods-form-ui-row-type-' . $field[ 'type' ] . ' pods-form-ui-row-name-' . PodsForm::clean( $field[ 'name' ], true ) ); ?> <?php echo esc_attr( $depends ); ?>">
1017
                <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th>
1018
                <td>
1019
                    <?php
1020
                        // Remove any extra ? help icons
1021
                        if ( isset( $field[ 'help' ] ) )
1022
                            unset( $field[ 'help' ] );
1023
                    ?>
1024
			<div class="pods-submittable-fields">
1025
                    <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
1026
                    <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
1027
			</div>
1028
                </td>
1029
            </tr>
1030
        <?php
1031
                do_action( 'pods_meta_' . __FUNCTION__ . '_' . $field[ 'name' ] . '_post', $post, $field, $pod );
1032
            }
1033
        }
1034
        ?>
1035
    </table>
1036
1037
    <?php
1038
        do_action( 'pods_meta_' . __FUNCTION__ . '_post', $post );
1039
1040 View Code Duplication
        foreach ( $hidden_fields as $hidden_field ) {
1041
            $field = $hidden_field[ 'field' ];
1042
1043
            echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $hidden_field[ 'value' ], 'hidden' );
1044
        }
1045
    ?>
1046
1047
    <script type="text/javascript">
1048
        jQuery( function ( $ ) {
1049
            $( document ).Pods( 'validate' );
1050
            $( document ).Pods( 'submit_meta' );
1051
            $( document ).Pods( 'dependency', true );
1052
        } );
1053
    </script>
1054
<?php
1055
    }
1056
1057
    /**
1058
     * @param $new_status
1059
     * @param $old_status
1060
     * @param $post
1061
     */
1062
    public function save_post_detect_new ( $new_status, $old_status, $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post 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...
1063
        self::$old_post_status = $old_status;
1064
    }
1065
1066
    /**
1067
     * @param $post_id
1068
     * @param $post
1069
     * @param $update
1070
     *
1071
     * @return int Post ID
1072
     */
1073
    public function save_post ( $post_id, $post, $update = null ) {
1074
        $is_new_item = false;
1075
1076
		if ( is_bool( $update ) )
1077
			$is_new_item = !$update; // false is new item
1078
		elseif ( 'new' == self::$old_post_status ) {
1079
			$is_new_item = true;
1080
		}
1081
1082 View Code Duplication
		if ( empty( $_POST ) ) {
1083
			return $post_id;
1084
		}
1085
		elseif ( !$is_new_item && !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_post' ) ) {
1086
			return $post_id;
1087
		}
1088
1089
		// Reset to avoid manual new post issues
1090
		self::$old_post_status = '';
1091
1092
		$blacklisted_types = array(
1093
			'revision',
1094
			'_pods_pod',
1095
			'_pods_field'
1096
		);
1097
1098
		$blacklisted_types = apply_filters( 'pods_meta_save_post_blacklist_types', $blacklisted_types, $post_id, $post );
1099
1100
		// @todo Figure out how to hook into autosave for saving meta
1101
1102
		// Block Autosave and Revisions
1103
		if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || in_array( $post->post_type, $blacklisted_types ) )
1104
			return $post_id;
1105
1106
		// Block Quick Edits / Bulk Edits
1107
		if ( 'edit.php' == pods_var( 'pagenow', 'global' ) && ( 'inline-save' == pods_var( 'action', 'post' ) || null != pods_var( 'bulk_edit', 'get' ) || is_array( pods_var( 'post', 'get' ) ) ) )
1108
			return $post_id;
1109
1110
		// Block Trash
1111
		if ( in_array( pods_var( 'action', 'get' ), array( 'untrash', 'trash' ) ) )
1112
			return $post_id;
1113
1114
		// Block Auto-drafting and Trash (not via Admin action)
1115
		$blacklisted_status = array(
1116
			'auto-draft',
1117
			'trash'
1118
		);
1119
1120
		$blacklisted_status = apply_filters( 'pods_meta_save_post_blacklist_status', $blacklisted_status, $post_id, $post );
1121
1122
		if ( in_array( $post->post_status, $blacklisted_status ) )
1123
			return $post_id;
1124
1125
		$groups = $this->groups_get( 'post_type', $post->post_type );
1126
1127
		if ( empty( $groups ) )
1128
			return $post_id;
1129
1130
		$data = array();
1131
1132
		$id = $post_id;
1133
		$pod = null;
1134
1135 View Code Duplication
		foreach ( $groups as $group ) {
1136
			if ( empty( $group[ 'fields' ] ) )
1137
				continue;
1138
1139
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1140
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1141
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1142
				elseif ( self::$current_pod->id() != $id )
1143
					self::$current_pod->fetch( $id );
1144
1145
				$pod = self::$current_pod;
1146
			}
1147
1148
			foreach ( $group[ 'fields' ] as $field ) {
1149
1150
				if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1151
					if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
1152
						continue;
1153
				}
1154
1155
				$data[ $field[ 'name' ] ] = '';
1156
1157
				if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
1158
					$data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
1159
			}
1160
		}
1161
1162 View Code Duplication
		if ( $is_new_item ) {
1163
			do_action( 'pods_meta_create_pre_post', $data, $pod, $id, $groups, $post, $post->post_type );
1164
			do_action( "pods_meta_create_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post );
1165
		}
1166
1167
		do_action( 'pods_meta_save_pre_post', $data, $pod, $id, $groups, $post, $post->post_type, $is_new_item );
1168
		do_action( "pods_meta_save_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post, $is_new_item );
1169
1170
		pods_no_conflict_on( 'post' );
1171
1172 View Code Duplication
		if ( !empty( $pod ) ) {
1173
			// Fix for Pods doing it's own sanitization
1174
			$data = pods_unslash( (array) $data );
1175
1176
			$pod->save( $data, null, null, array( 'is_new_item' => $is_new_item ) );
1177
		}
1178
		elseif ( !empty( $id ) ) {
1179
			foreach ( $data as $field => $value ) {
1180
				update_post_meta( $id, $field, $value );
1181
			}
1182
		}
1183
1184
		pods_no_conflict_off( 'post' );
1185
1186 View Code Duplication
		if ( $is_new_item ) {
1187
			do_action( 'pods_meta_create_post', $data, $pod, $id, $groups, $post, $post->post_type );
1188
			do_action( "pods_meta_create_post_{$post->post_type}", $data, $pod, $id, $groups, $post );
1189
		}
1190
1191
		do_action( 'pods_meta_save_post', $data, $pod, $id, $groups, $post, $post->post_type, $is_new_item );
1192
		do_action( "pods_meta_save_post_{$post->post_type}", $data, $pod, $id, $groups, $post, $is_new_item );
1193
1194
		return $post_id;
1195
1196
    }
1197
1198
    /**
1199
     * @param $form_fields
1200
     * @param $post
1201
     *
1202
     * @return array
1203
     */
1204
    public function meta_media ( $form_fields, $post ) {
1205
        $groups = $this->groups_get( 'media', 'media' );
1206
1207
        if ( empty( $groups ) || 'attachment' == pods_var( 'typenow', 'global' ) )
1208
            return $form_fields;
1209
1210
        wp_enqueue_style( 'pods-form' );
1211
1212
        $id = null;
1213
1214
        if ( is_object( $post ) )
1215
            $id = $post->ID;
1216
1217
        $pod = null;
1218
1219
		$meta_nonce = PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_media' ), 'hidden' );
1220
1221
        foreach ( $groups as $group ) {
1222
            if ( empty( $group[ 'fields' ] ) )
1223
                continue;
1224
1225
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1226
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1227
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1228
				elseif ( self::$current_pod->id() != $id )
1229
					self::$current_pod->fetch( $id );
1230
1231
				$pod = self::$current_pod;
1232
			}
1233
1234
            foreach ( $group[ 'fields' ] as $field ) {
1235
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1236
                    if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
1237
                        continue;
1238
                }
1239
1240
                $value = '';
1241
1242 View Code Duplication
                if ( !empty( $pod ) )
1243
                    $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1244
                elseif ( !empty( $id ) ) {
1245
                    pods_no_conflict_on( 'post' );
1246
1247
                    $value = get_post_meta( $id, $field[ 'name' ], true );
1248
1249
                    pods_no_conflict_off( 'post' );
1250
                }
1251
1252
                $form_fields[ 'pods_meta_' . $field[ 'name' ] ] = array(
1253
                    'label' => $field[ 'label' ],
1254
                    'input' => 'html',
1255
                    'html' => PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ) . $meta_nonce,
1256
                    'helps' => PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field )
1257
                );
1258
            }
1259
        }
1260
1261
        $form_fields = apply_filters( 'pods_meta_' . __FUNCTION__, $form_fields );
1262
1263
        return $form_fields;
1264
    }
1265
1266
    /**
1267
     * @param $post
1268
     * @param $attachment
1269
     *
1270
     * @return mixed
1271
     */
1272
    public function save_media ( $post, $attachment ) {
1273
        $groups = $this->groups_get( 'media', 'media' );
1274
1275
        if ( empty( $groups ) )
1276
            return $post;
1277
1278
        $post_id = $attachment;
1279
1280
        if ( empty( $_POST ) || !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_media' ) ) {
1281
            return $post;
1282
		}
1283
1284 View Code Duplication
        if ( is_array( $post ) && !empty( $post ) && isset( $post[ 'ID' ] ) && 'attachment' == $post[ 'post_type' ] )
1285
            $post_id = $post[ 'ID' ];
1286
1287
        if ( is_array( $post_id ) || empty( $post_id ) )
1288
            return $post;
1289
1290
        $data = array();
1291
1292
        $id = $post_id;
1293
        $pod = null;
1294
1295 View Code Duplication
        foreach ( $groups as $group ) {
1296
            if ( empty( $group[ 'fields' ] ) )
1297
                continue;
1298
1299
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1300
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1301
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1302
				elseif ( self::$current_pod->id() != $id )
1303
					self::$current_pod->fetch( $id );
1304
1305
				$pod = self::$current_pod;
1306
			}
1307
1308
            foreach ( $group[ 'fields' ] as $field ) {
1309
1310
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1311
                    if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
1312
                        continue;
1313
                }
1314
1315
                $data[ $field[ 'name' ] ] = '';
1316
1317
                if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
1318
                    $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
1319
            }
1320
        }
1321
1322
        do_action( 'pods_meta_save_pre_media', $data, $pod, $id, $groups, $post, $attachment );
1323
1324 View Code Duplication
        if ( !empty( $pod ) ) {
1325
            // Fix for Pods doing it's own sanitization
1326
            $data = pods_unslash( (array) $data );
1327
1328
            $pod->save( $data );
1329
        }
1330
        elseif ( !empty( $id ) ) {
1331
            pods_no_conflict_on( 'post' );
1332
1333
            foreach ( $data as $field => $value ) {
1334
                update_post_meta( $id, $field, $value );
1335
            }
1336
1337
            pods_no_conflict_off( 'post' );
1338
        }
1339
1340
        do_action( 'pods_meta_save_media', $data, $pod, $id, $groups, $post, $attachment );
1341
1342
        return $post;
1343
    }
1344
1345
    public function save_media_ajax () {
1346
        if ( !isset( $_POST[ 'id' ] ) || empty( $_POST[ 'id' ] ) || absint( $_POST[ 'id' ] ) < 1 )
1347
            return;
1348
1349
        $id = absint( $_POST[ 'id' ] );
1350
1351
        if ( !isset( $_POST[ 'nonce' ] ) || empty( $_POST[ 'nonce' ] ) )
1352
            return;
1353
1354
        check_ajax_referer( 'update-post_' . $id, 'nonce' );
1355
1356
        if ( !current_user_can( 'edit_post', $id ) )
1357
            return;
1358
1359
        $post = get_post( $id, ARRAY_A );
1360
1361
    	if ( 'attachment' != $post[ 'post_type' ] )
1362
            return;
1363
1364
        // fix ALL THE THINGS
1365
1366
        if ( !isset( $_REQUEST[ 'attachments' ] ) )
1367
            $_REQUEST[ 'attachments' ] = array();
1368
1369
        if ( !isset( $_REQUEST[ 'attachments' ][ $id ] ) )
1370
            $_REQUEST[ 'attachments' ][ $id ] = array();
1371
1372
        if ( empty( $_REQUEST[ 'attachments' ][ $id ] ) )
1373
            $_REQUEST[ 'attachments' ][ $id ][ '_fix_wp' ] = 1;
1374
    }
1375
1376
    /**
1377
     * @param $tag
1378
     * @param null $taxonomy
1379
     */
1380
    public function meta_taxonomy ( $tag, $taxonomy = null ) {
1381
        wp_enqueue_style( 'pods-form' );
1382
1383
        do_action( 'pods_meta_' . __FUNCTION__, $tag, $taxonomy );
1384
1385
        $taxonomy_name = $taxonomy;
1386
1387
        if ( !is_object( $tag ) )
1388
            $taxonomy_name = $tag;
1389
1390
        $groups = $this->groups_get( 'taxonomy', $taxonomy_name );
1391
1392
        $id = null;
1393
1394
        if ( is_object( $tag ) )
1395
            $id = $tag->term_id;
1396
1397
        $pod = null;
1398
1399
		echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_taxonomy' ), 'hidden' );
1400
1401
        foreach ( $groups as $group ) {
1402
            if ( empty( $group[ 'fields' ] ) )
1403
                continue;
1404
1405
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1406
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1407
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1408
				elseif ( self::$current_pod->id() != $id )
1409
					self::$current_pod->fetch( $id );
1410
1411
				$pod = self::$current_pod;
1412
			}
1413
1414
            foreach ( $group[ 'fields' ] as $field ) {
1415 View Code Duplication
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1416
                    if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1417
                        $field[ 'type' ] = 'hidden';
1418
                    else
1419
                        continue;
1420
                }
1421
                elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1422
                    $field[ 'type' ] = 'hidden';
1423
1424
                $value = '';
1425
1426 View Code Duplication
                if ( !empty( $pod ) )
1427
                    $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1428
1429
                if ( !is_object( $tag ) ) {
1430
            ?>
1431
                <div class="form-field pods-field" style="<?php echo esc_attr( 'hidden' == $field[ 'type' ] ? 'display:none;' : '' ); ?>">
1432
                    <?php
1433
                        echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field );
1434
                        echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
1435
                        echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
1436
                    ?>
1437
                </div>
1438
            <?php
1439
                }
1440
                else {
1441
            ?>
1442
                <tr class="form-field pods-field <?php echo esc_attr( 'pods-form-ui-row-type-' . $field[ 'type' ] . ' pods-form-ui-row-name-' . PodsForm::clean( $field[ 'name' ], true ) ); ?>" style="<?php echo esc_attr( 'hidden' == $field[ 'type' ] ? 'display:none;' : '' ); ?>">
1443
                    <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th>
1444
                    <td>
1445
                        <?php
1446
                            echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
1447
                            echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
1448
                        ?>
1449
                    </td>
1450
                </tr>
1451
            <?php
1452
                }
1453
            }
1454
        }
1455
1456
        do_action( 'pods_meta_' . __FUNCTION__ . '_post', $tag, $taxonomy );
1457
    }
1458
1459
    /**
1460
     * @param $term_id
1461
     * @param $term_taxonomy_id
1462
     * @param $taxonomy
1463
     */
1464
    public function save_taxonomy ( $term_id, $term_taxonomy_id, $taxonomy ) {
1465
        $is_new_item = false;
1466
1467
        if ( 'create_term' == current_filter() )
1468
            $is_new_item = true;
1469
1470
        if ( empty( $_POST ) || !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_taxonomy' ) ) {
1471
            return $term_id;
1472
		}
1473
1474
		// Block Quick Edits / Bulk Edits
1475
		if ( 'inline-save-tax' == pods_var( 'action', 'post' ) || null != pods_var( 'delete_tags', 'post' ) ) {
1476
            return $term_id;
1477
		}
1478
1479
        $groups = $this->groups_get( 'taxonomy', $taxonomy );
1480
1481
        if ( empty( $groups ) )
1482
            return $term_id;
1483
1484
		$term = null;
1485
1486
        $id = $term_id;
1487
        $pod = null;
1488
1489
		$has_fields = false;
1490
1491
        foreach ( $groups as $group ) {
1492
            if ( empty( $group[ 'fields' ] ) )
1493
                continue;
1494
1495
			if ( null === $term ) {
1496
				$term = get_term( $term_id, $taxonomy );
1497
1498
				$data = array(
1499
					'name' => $term->name
1500
				);
1501
			}
1502
1503
			$has_fields = true;
1504
1505
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1506
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1507
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1508
				elseif ( self::$current_pod->id() != $id )
1509
					self::$current_pod->fetch( $id );
1510
1511
				$pod = self::$current_pod;
1512
			}
1513
1514
            foreach ( $group[ 'fields' ] as $field ) {
1515
1516
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1517
                    if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
1518
                        continue;
1519
                }
1520
1521
                $data[ $field[ 'name' ] ] = '';
0 ignored issues
show
Bug introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1522
1523
                if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
1524
                    $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
1525
            }
1526
        }
1527
1528
		if ( !$has_fields ) {
1529
			return $term_id;
1530
		}
1531
1532 View Code Duplication
        if ( $is_new_item ) {
1533
            do_action( 'pods_meta_create_pre_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1534
            do_action( "pods_meta_create_pre_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1535
        }
1536
1537
        do_action( 'pods_meta_save_pre_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1538
        do_action( "pods_meta_save_pre_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1539
1540
        pods_no_conflict_on( 'taxonomy' );
1541
1542
        if ( !empty( $pod ) ) {
1543
            // Fix for Pods doing it's own sanitization
1544
            $data = pods_unslash( (array) $data );
1545
1546
            $pod->save( $data, null, null, array( 'is_new_item' => $is_new_item ) );
1547
        }
1548
1549
        pods_no_conflict_off( 'taxonomy' );
1550
1551 View Code Duplication
        if ( $is_new_item ) {
1552
            do_action( 'pods_meta_create_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1553
            do_action( "pods_meta_create_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1554
        }
1555
1556
        do_action( 'pods_meta_save_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1557
        do_action( "pods_meta_save_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1558
1559
		return $term_id;
1560
    }
1561
1562
    /**
1563
     * @param $user_id
1564
     */
1565
    public function meta_user ( $user_id ) {
1566
        wp_enqueue_style( 'pods-form' );
1567
1568
        do_action( 'pods_meta_' . __FUNCTION__, $user_id );
1569
1570
        $groups = $this->groups_get( 'user', 'user' );
1571
1572
        if ( is_object( $user_id ) )
1573
            $user_id = $user_id->ID;
1574
1575
        $id = $user_id;
1576
        $pod = null;
1577
1578
        foreach ( $groups as $group ) {
1579
            if ( empty( $group[ 'fields' ] ) )
1580
                continue;
1581
1582
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1583
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1584
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1585
				elseif ( self::$current_pod->id() != $id )
1586
					self::$current_pod->fetch( $id );
1587
1588
				$pod = self::$current_pod;
1589
			}
1590
1591
            $hidden_fields = array();
1592
?>
1593
    <h3><?php echo $group[ 'label' ]; ?></h3>
1594
1595
	<?php echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_user' ), 'hidden' ); ?>
1596
1597
    <table class="form-table pods-meta">
1598
        <tbody>
1599
            <?php
1600
                foreach ( $group[ 'fields' ] as $field ) {
1601
1602 View Code Duplication
                    if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1603
                        if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1604
                            $field[ 'type' ] = 'hidden';
1605
                        else
1606
                            continue;
1607
                    }
1608
                    elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1609
                        $field[ 'type' ] = 'hidden';
1610
1611
                    $value = '';
1612
1613 View Code Duplication
                    if ( !empty( $pod ) )
1614
                        $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1615
                    elseif ( !empty( $id ) ) {
1616
                        pods_no_conflict_on( 'user' );
1617
1618
                        $value = get_user_meta( $id, $field[ 'name' ], true );
1619
1620
                        pods_no_conflict_off( 'user' );
1621
                    }
1622
1623 View Code Duplication
                    if ( 'hidden' == $field[ 'type' ] ) {
1624
                        $hidden_fields[] = array(
1625
                            'field' => $field,
1626
                            'value' => $value
1627
                        );
1628
                    }
1629
                    else {
1630
            ?>
1631
                <tr class="form-field pods-field <?php echo esc_attr( 'pods-form-ui-row-type-' . $field[ 'type' ] . ' pods-form-ui-row-name-' . PodsForm::clean( $field[ 'name' ], true ) ); ?>">
1632
                    <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th>
1633
                    <td>
1634
                        <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
1635
                        <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
1636
                    </td>
1637
                </tr>
1638
            <?php
1639
                    }
1640
                }
1641
            ?>
1642
        </tbody>
1643
    </table>
1644
<?php
1645 View Code Duplication
            foreach ( $hidden_fields as $hidden_field ) {
1646
                $field = $hidden_field[ 'field' ];
1647
1648
                echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $hidden_field[ 'value' ], 'hidden' );
1649
            }
1650
        }
1651
1652
        do_action( 'pods_meta_' . __FUNCTION__ . '_post', $user_id );
1653
    }
1654
1655
    /**
1656
     * @param $user_id
1657
     */
1658
    public function save_user ( $user_id ) {
1659
1660
		$is_new_item = false;
1661
1662
		if ( 'user_register' == current_filter() ) {
1663
			$is_new_item = true;
1664
		}
1665
1666 View Code Duplication
		if ( empty( $_POST ) ) {
1667
			return $user_id;
1668
		}
1669
		elseif ( $is_new_item || !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_user' ) ) {
1670
			return $user_id;
1671
		}
1672
1673
		$groups = $this->groups_get( 'user', 'user' );
1674
1675
		if ( empty( $groups ) ) {
1676
			return $user_id;
1677
		}
1678
1679
		if ( is_object( $user_id ) ) {
1680
			$user_id = $user_id->ID;
1681
		}
1682
1683
		$data = array();
1684
1685
		$id = $user_id;
1686
		$pod = null;
1687
1688 View Code Duplication
		foreach ( $groups as $group ) {
1689
			if ( empty( $group[ 'fields' ] ) ) {
1690
				continue;
1691
			}
1692
1693
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1694
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] ) {
1695
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1696
				}
1697
				elseif ( self::$current_pod->id() != $id ) {
1698
					self::$current_pod->fetch( $id );
1699
				}
1700
1701
				$pod = self::$current_pod;
1702
			}
1703
1704
			foreach ( $group[ 'fields' ] as $field ) {
1705
1706
				if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1707
					if ( !pods_var( 'hidden', $field[ 'options' ], false ) ) {
1708
						continue;
1709
					}
1710
				}
1711
1712
				$data[ $field[ 'name' ] ] = '';
1713
1714
				if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) ) {
1715
					$data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
1716
				}
1717
			}
1718
		}
1719
1720
		if ( $is_new_item ) {
1721
			do_action( 'pods_meta_create_pre_user', $data, $pod, $id, $groups );
1722
		}
1723
1724
		do_action( 'pods_meta_save_pre_user', $data, $pod, $id, $groups, $is_new_item );
1725
1726
		pods_no_conflict_on( 'user' );
1727
1728 View Code Duplication
		if ( !empty( $pod ) ) {
1729
			// Fix for Pods doing it's own sanitization
1730
			$data = pods_unslash( (array) $data );
1731
1732
			$pod->save( $data, null, null, array( 'is_new_item' => $is_new_item ) );
1733
		}
1734
		elseif ( !empty( $id ) ) {
1735
			foreach ( $data as $field => $value ) {
1736
				update_user_meta( $id, $field, $value );
1737
			}
1738
		}
1739
1740
		pods_no_conflict_off( 'user' );
1741
1742
		if ( $is_new_item ) {
1743
			do_action( 'pods_meta_create_user', $data, $pod, $id, $groups );
1744
		}
1745
1746
		do_action( 'pods_meta_save_user', $data, $pod, $id, $groups, $is_new_item );
1747
1748
		return $user_id;
1749
1750
    }
1751
1752
    /**
1753
     * @param $commenter
1754
     * @param $user_identity
1755
     */
1756
    public function meta_comment_new_logged_in ( $commenter, $user_identity ) {
1757
        wp_enqueue_style( 'pods-form' );
1758
1759
        do_action( 'pods_meta_' . __FUNCTION__, $commenter, $user_identity );
1760
1761
        $groups = $this->groups_get( 'comment', 'comment' );
1762
1763
        $id = null;
1764
        $pod = null;
1765
1766
		echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' );
1767
1768
        foreach ( $groups as $group ) {
1769
            if ( empty( $group[ 'fields' ] ) )
1770
                continue;
1771
1772
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1773
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1774
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1775
				elseif ( self::$current_pod->id() != $id )
1776
					self::$current_pod->fetch( $id );
1777
1778
				$pod = self::$current_pod;
1779
			}
1780
1781
            foreach ( $group[ 'fields' ] as $field ) {
1782 View Code Duplication
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1783
                    if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1784
                        $field[ 'type' ] = 'hidden';
1785
                    else
1786
                        continue;
1787
                }
1788
                elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1789
                    $field[ 'type' ] = 'hidden';
1790
1791
                $value = '';
1792
1793 View Code Duplication
                if ( !empty( $pod ) )
1794
                    $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1795
                elseif ( !empty( $id ) ) {
1796
                    pods_no_conflict_on( 'comment' );
1797
1798
                    $value = get_comment_meta( $id, $field[ 'name' ], true );
1799
1800
                    pods_no_conflict_off( 'comment' );
1801
                }
1802
                ?>
1803
            <p class="comment-form-author comment-form-pods-meta-<?php echo esc_attr( $field[ 'name' ] ); ?>  pods-field" style="<?php echo esc_attr( 'hidden' == $field[ 'type' ] ? 'display:none;' : '' ); ?>">
1804
                <?php
1805
                    echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field );
1806
                    echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
1807
                    echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
1808
                ?>
1809
            </p>
1810
            <?php
1811
            }
1812
        }
1813
1814
        do_action( 'pods_meta_' . __FUNCTION__ . '_post', $commenter, $user_identity );
1815
    }
1816
1817
    /**
1818
     * @param $form_fields
1819
     *
1820
     * @return array
1821
     */
1822
    public function meta_comment_new ( $form_fields ) {
1823
        wp_enqueue_style( 'pods-form' );
1824
1825
        $groups = $this->groups_get( 'comment', 'comment' );
1826
1827
        $id = null;
1828
        $pod = null;
1829
1830
		$form_fields[ 'pods_meta' ] = PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' );
1831
1832
        foreach ( $groups as $group ) {
1833
            if ( empty( $group[ 'fields' ] ) )
1834
                continue;
1835
1836
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1837
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1838
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1839
				elseif ( self::$current_pod->id() != $id )
1840
					self::$current_pod->fetch( $id );
1841
1842
				$pod = self::$current_pod;
1843
			}
1844
1845
            foreach ( $group[ 'fields' ] as $field ) {
1846
1847 View Code Duplication
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1848
                    if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1849
                        $field[ 'type' ] = 'hidden';
1850
                    else
1851
                        continue;
1852
                }
1853
                elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1854
                    $field[ 'type' ] = 'hidden';
1855
1856
                $value = '';
1857
1858 View Code Duplication
                if ( !empty( $pod ) )
1859
                    $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1860
                elseif ( !empty( $id ) ) {
1861
                    pods_no_conflict_on( 'comment' );
1862
1863
                    $value = get_comment_meta( $id, $field[ 'name' ], true );
1864
1865
                    pods_no_conflict_off( 'comment' );
1866
                }
1867
1868
                ob_start();
1869
                ?>
1870
            <p class="comment-form-author comment-form-pods-meta-<?php echo esc_attr( $field[ 'name' ] ); ?> pods-field" style="<?php echo esc_attr( 'hidden' == $field[ 'type' ] ? 'display:none;' : '' ); ?>">
1871
                <?php
1872
                    echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field );
1873
                    echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
1874
                    echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
1875
                ?>
1876
            </p>
1877
            <?php
1878
                $form_fields[ 'pods_meta_' . $field[ 'name' ] ] = ob_get_clean();
1879
            }
1880
        }
1881
1882
        $form_fields = apply_filters( 'pods_meta_' . __FUNCTION__, $form_fields );
1883
1884
        return $form_fields;
1885
    }
1886
1887
    /**
1888
     * @param $comment_type
1889
     * @param null $comment
1890
     */
1891
    public function meta_comment_add ( $comment_type, $comment = null ) {
1892
        if ( is_object( $comment ) && isset( $comment_type->comment_type ) )
1893
            $comment_type = $comment->comment_type;
1894
1895
        if ( is_object( $comment_type ) && isset( $comment_type->comment_type ) ) {
1896
            $comment = $comment_type;
1897
            $comment_type = $comment_type->comment_type;
1898
        }
1899
1900
        if ( is_object( $comment_type ) )
1901
            return;
1902
        elseif ( empty( $comment_type ) )
1903
            $comment_type = 'comment';
1904
1905
        $groups = $this->groups_get( 'comment', $comment_type );
1906
1907
        foreach ( $groups as $group ) {
1908
            if ( empty( $group[ 'fields' ] ) )
1909
                continue;
1910
1911
            $field_found = false;
1912
1913
            foreach ( $group[ 'fields' ] as $field ) {
1914
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], null, null ) ) {
1915
                    if ( pods_var( 'hidden', $field[ 'options' ], false ) ) {
1916
                        $field_found = true;
1917
                        break;
1918
                    }
1919
                    else {
1920
                        continue;
1921
                    }
1922
                }
1923
                else {
1924
                    $field_found = true;
1925
                    break;
1926
                }
1927
            }
1928
1929 View Code Duplication
            if ( $field_found ) {
1930
                add_meta_box(
1931
                    'pods-meta-' . sanitize_title( $group[ 'label' ] ),
1932
                    $group[ 'label' ],
1933
                    array( $this, 'meta_comment' ),
1934
                    $comment_type,
1935
                    $group[ 'context' ],
1936
                    $group[ 'priority' ],
1937
                    array( 'group' => $group )
1938
                );
1939
            }
1940
        }
1941
    }
1942
1943
    /**
1944
     * @param $comment
1945
     * @param $metabox
1946
     */
1947
    public function meta_comment ( $comment, $metabox ) {
1948
        wp_enqueue_style( 'pods-form' );
1949
1950
        do_action( 'pods_meta_' . __FUNCTION__, $comment, $metabox );
1951
1952
        $hidden_fields = array();
1953
1954
		echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' );
1955
?>
1956
    <table class="form-table editcomment pods-metabox">
1957
        <?php
1958
            $id = null;
1959
1960
            if ( is_object( $comment ) )
1961
                $id = $comment->comment_ID;
1962
1963 View Code Duplication
            if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ] )
1964
                self::$current_pod = pods( $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ], $id, true );
1965
			elseif ( self::$current_pod->id() != $id )
1966
				self::$current_pod->fetch( $id );
1967
1968
            $pod = self::$current_pod;
1969
1970
            foreach ( $metabox[ 'args' ][ 'group' ][ 'fields' ] as $field ) {
1971 View Code Duplication
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $metabox[ 'args' ][ 'group' ][ 'fields' ], $pod, $id ) ) {
1972
                    if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1973
                        $field[ 'type' ] = 'hidden';
1974
                    else
1975
                        continue;
1976
                }
1977
                elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1978
                    $field[ 'type' ] = 'hidden';
1979
1980
                $value = '';
1981
1982 View Code Duplication
                if ( !empty( $pod ) )
1983
                    $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1984
1985 View Code Duplication
                if ( 'hidden' == $field[ 'type' ] ) {
1986
                    $hidden_fields[] = array(
1987
                        'field' => $field,
1988
                        'value' => $value
1989
                    );
1990
                }
1991
                else {
1992
        ?>
1993
            <tr class="form-field pods-field <?php echo esc_attr( 'pods-form-ui-row-type-' . $field[ 'type' ] . ' pods-form-ui-row-name-' . PodsForm::clean( $field[ 'name' ], true ) ); ?>">
1994
                <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th>
1995
                <td>
1996
                    <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
1997
                    <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
1998
                </td>
1999
            </tr>
2000
        <?php
2001
                }
2002
            }
2003
        ?>
2004
    </table>
2005
<?php
2006 View Code Duplication
        foreach ( $hidden_fields as $hidden_field ) {
2007
            $field = $hidden_field[ 'field' ];
2008
2009
            echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $hidden_field[ 'value' ], 'hidden' );
2010
        }
2011
2012
        do_action( 'pods_meta_' . __FUNCTION__ . '_post', $comment, $metabox );
2013
    }
2014
2015
    /**
2016
     * @param $approved
2017
     * @param $commentdata
2018
     */
2019
    public function validate_comment ( $approved, $commentdata ) {
0 ignored issues
show
Unused Code introduced by
The parameter $commentdata 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...
2020
        $groups = $this->groups_get( 'comment', 'comment' );
2021
2022
        if ( empty( $groups ) )
2023
            return $approved;
2024
2025
        $data = array();
2026
2027
        $pod = null;
2028
        $id = null;
2029
2030
        foreach ( $groups as $group ) {
2031
            if ( empty( $group[ 'fields' ] ) )
2032
                continue;
2033
2034
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
2035
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
2036
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
2037
				elseif ( self::$current_pod->id() != $id )
2038
					self::$current_pod->fetch( $id );
2039
2040
				$pod = self::$current_pod;
2041
			}
2042
2043
            foreach ( $group[ 'fields' ] as $field ) {
2044
2045
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
2046
                    if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
2047
                        continue;
2048
                }
2049
2050
                $data[ $field[ 'name' ] ] = '';
2051
2052
                if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
2053
                    $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
2054
2055
                $validate = pods_api()->handle_field_validation( $data[ $field[ 'name' ] ], $field[ 'name' ], pods_api()->get_wp_object_fields( 'comment' ), $pod->fields(), $pod, array() );
2056
2057
                if ( false === $validate )
2058
                    $validate = sprintf( __( 'There was an issue validating the field %s', 'pods' ), $field[ 'label' ] );
2059
2060
                if ( !is_bool( $validate ) && !empty( $validate ) )
2061
                    return pods_error( $validate, $this );
2062
            }
2063
        }
2064
2065
        return $approved;
2066
    }
2067
2068
    /**
2069
     * @param $comment_id
2070
     */
2071
    public function save_comment ( $comment_id ) {
2072
        $groups = $this->groups_get( 'comment', 'comment' );
2073
2074 View Code Duplication
        if ( empty( $groups ) ) {
2075
            return $comment_id;
2076
		}
2077
		elseif ( empty( $_POST ) ) {
2078
			return $comment_id;
2079
		}
2080
		elseif ( !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_comment' ) ) {
2081
			return $comment_id;
2082
		}
2083
2084
        $data = array();
2085
2086
        $id = $comment_id;
2087
        $pod = null;
2088
2089 View Code Duplication
        foreach ( $groups as $group ) {
2090
            if ( empty( $group[ 'fields' ] ) )
2091
                continue;
2092
2093
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
2094
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
2095
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
2096
				elseif ( self::$current_pod->id() != $id )
2097
					self::$current_pod->fetch( $id );
2098
2099
				$pod = self::$current_pod;
2100
			}
2101
2102
            foreach ( $group[ 'fields' ] as $field ) {
2103
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
2104
                    if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
2105
                        continue;
2106
                }
2107
2108
                $data[ $field[ 'name' ] ] = '';
2109
2110
                if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
2111
                    $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
2112
            }
2113
        }
2114
2115
        do_action( 'pods_meta_save_pre_comment', $data, $pod, $id, $groups );
2116
2117 View Code Duplication
        if ( !empty( $pod ) ) {
2118
            // Fix for Pods doing it's own sanitization
2119
            $data = pods_unslash( (array) $data );
2120
2121
            $pod->save( $data );
2122
        }
2123
        elseif ( !empty( $id ) ) {
2124
            pods_no_conflict_on( 'comment' );
2125
2126
            foreach ( $data as $field => $value ) {
2127
                update_comment_meta( $id, $field, $value );
2128
            }
2129
2130
            pods_no_conflict_off( 'comment' );
2131
        }
2132
2133
        do_action( 'pods_meta_save_comment', $data, $pod, $id, $groups );
2134
2135
        return $comment_id;
2136
    }
2137
2138
    /**
2139
     * All *_*_meta filter handler aliases
2140
     *
2141
     * @return mixed
2142
     */
2143 View Code Duplication
    public function get_post_meta () {
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...
2144
        $args = func_get_args();
2145
2146
        array_unshift( $args, 'post_type' );
2147
2148
        $_null = apply_filters( 'pods_meta_get_post_meta', null, $args );
2149
2150
        if ( null !== $_null )
2151
            return $_null;
2152
2153
        return call_user_func_array( array( $this, 'get_meta' ), $args );
2154
    }
2155
2156
    /**
2157
     * @return mixed
2158
     */
2159 View Code Duplication
    public function get_user_meta () {
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...
2160
        $args = func_get_args();
2161
2162
        array_unshift( $args, 'user' );
2163
2164
        $_null = apply_filters( 'pods_meta_get_user_meta', null, $args );
2165
2166
        if ( null !== $_null )
2167
            return $_null;
2168
2169
        return call_user_func_array( array( $this, 'get_meta' ), $args );
2170
    }
2171
2172
    /**
2173
     * @return mixed
2174
     */
2175 View Code Duplication
    public function get_comment_meta () {
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...
2176
        $args = func_get_args();
2177
2178
        array_unshift( $args, 'comment' );
2179
2180
        $_null = apply_filters( 'pods_meta_get_comment_meta', null, $args );
2181
2182
        if ( null !== $_null )
2183
            return $_null;
2184
2185
        return call_user_func_array( array( $this, 'get_meta' ), $args );
2186
    }
2187
2188
    /**
2189
     * @return mixed
2190
     */
2191 View Code Duplication
    public function get_term_meta () {
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...
2192
        $args = func_get_args();
2193
2194
        array_unshift( $args, 'term' );
2195
2196
        $_null = apply_filters( 'pods_meta_get_term_meta', null, $args );
2197
2198
        if ( null !== $_null )
2199
            return $_null;
2200
2201
        return call_user_func_array( array( $this, 'get_meta' ), $args );
2202
    }
2203
2204
    /**
2205
     * All *_*_meta filter handler aliases
2206
     *
2207
     * @return mixed
2208
     */
2209 View Code Duplication
    public function get_option () {
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...
2210
        $args = func_get_args();
2211
2212
        array_unshift( $args, 'settings' );
2213
2214
        $_null = apply_filters( 'pods_meta_get_option', null, $args );
2215
2216
        if ( null !== $_null )
2217
            return $_null;
2218
2219
        return call_user_func_array( array( $this, 'get_meta' ), $args );
2220
    }
2221
2222
    /**
2223
     * @return mixed
2224
     */
2225 View Code Duplication
    public function add_post_meta () {
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...
2226
        $args = func_get_args();
2227
2228
        array_unshift( $args, 'post_type' );
2229
2230
        $_null = apply_filters( 'pods_meta_add_post_meta', null, $args );
2231
2232
        if ( null !== $_null )
2233
            return $_null;
2234
2235
        return call_user_func_array( array( $this, 'add_meta' ), $args );
2236
    }
2237
2238
    /**
2239
     * @return mixed
2240
     */
2241 View Code Duplication
    public function add_user_meta () {
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...
2242
        $args = func_get_args();
2243
2244
        array_unshift( $args, 'user' );
2245
2246
        $_null = apply_filters( 'pods_meta_add_user_meta', null, $args );
2247
2248
        if ( null !== $_null )
2249
            return $_null;
2250
2251
        return call_user_func_array( array( $this, 'add_meta' ), $args );
2252
    }
2253
2254
    /**
2255
     * @return mixed
2256
     */
2257 View Code Duplication
    public function add_comment_meta () {
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...
2258
        $args = func_get_args();
2259
2260
        array_unshift( $args, 'comment' );
2261
2262
        $_null = apply_filters( 'pods_meta_add_comment_meta', null, $args );
2263
2264
        if ( null !== $_null )
2265
            return $_null;
2266
2267
        return call_user_func_array( array( $this, 'add_meta' ), $args );
2268
    }
2269
2270
    /**
2271
     * @return mixed
2272
     */
2273 View Code Duplication
    public function add_term_meta () {
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...
2274
        $args = func_get_args();
2275
2276
        array_unshift( $args, 'term' );
2277
2278
        $_null = apply_filters( 'pods_meta_add_term_meta', null, $args );
2279
2280
        if ( null !== $_null )
2281
            return $_null;
2282
2283
        return call_user_func_array( array( $this, 'add_meta' ), $args );
2284
    }
2285
2286
    /**
2287
     * @return mixed
2288
     */
2289 View Code Duplication
    public function add_option () {
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...
2290
        $args = func_get_args();
2291
2292
        array_unshift( $args, 'settings' );
2293
2294
        $_null = apply_filters( 'pods_meta_add_option', null, $args );
2295
2296
        if ( null !== $_null )
2297
            return $_null;
2298
2299
        return call_user_func_array( array( $this, 'add_meta' ), $args );
2300
    }
2301
2302
    /**
2303
     * @return mixed
2304
     */
2305 View Code Duplication
    public function update_post_meta () {
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...
2306
        $args = func_get_args();
2307
2308
        array_unshift( $args, 'post_type' );
2309
2310
        $_null = apply_filters( 'pods_meta_update_post_meta', null, $args );
2311
2312
        if ( null !== $_null )
2313
            return $_null;
2314
2315
        return call_user_func_array( array( $this, 'update_meta' ), $args );
2316
    }
2317
2318
    /**
2319
     * @return mixed
2320
     */
2321 View Code Duplication
    public function update_user_meta () {
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...
2322
        $args = func_get_args();
2323
2324
        array_unshift( $args, 'user' );
2325
2326
        $_null = apply_filters( 'pods_meta_update_user_meta', null, $args );
2327
2328
        if ( null !== $_null )
2329
            return $_null;
2330
2331
        return call_user_func_array( array( $this, 'update_meta' ), $args );
2332
    }
2333
2334
    /**
2335
     * @return mixed
2336
     */
2337 View Code Duplication
    public function update_comment_meta () {
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...
2338
        $args = func_get_args();
2339
2340
        array_unshift( $args, 'comment' );
2341
2342
        $_null = apply_filters( 'pods_meta_update_comment_meta', null, $args );
2343
2344
        if ( null !== $_null )
2345
            return $_null;
2346
2347
        return call_user_func_array( array( $this, 'update_meta' ), $args );
2348
    }
2349
2350
    /**
2351
     * @return mixed
2352
     */
2353 View Code Duplication
    public function update_term_meta () {
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...
2354
        $args = func_get_args();
2355
2356
        array_unshift( $args, 'term' );
2357
2358
        $_null = apply_filters( 'pods_meta_update_term_meta', null, $args );
2359
2360
        if ( null !== $_null )
2361
            return $_null;
2362
2363
        return call_user_func_array( array( $this, 'update_meta' ), $args );
2364
    }
2365
2366
    /**
2367
     * @return mixed
2368
     */
2369 View Code Duplication
    public function update_option () {
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...
2370
        $args = func_get_args();
2371
2372
        array_unshift( $args, 'settings' );
2373
2374
        $_null = apply_filters( 'pods_meta_update_option', null, $args );
2375
2376
        if ( null !== $_null )
2377
            return $_null;
2378
2379
        return call_user_func_array( array( $this, 'update_meta' ), $args );
2380
    }
2381
2382
    /**
2383
     * @return mixed
2384
     */
2385 View Code Duplication
    public function delete_post_meta () {
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...
2386
        $args = func_get_args();
2387
2388
        array_unshift( $args, 'post_type' );
2389
2390
        $_null = apply_filters( 'pods_meta_delete_post_meta', null, $args );
2391
2392
        if ( null !== $_null )
2393
            return $_null;
2394
2395
        return call_user_func_array( array( $this, 'delete_meta' ), $args );
2396
    }
2397
2398
    /**
2399
     * @return mixed
2400
     */
2401 View Code Duplication
    public function delete_user_meta () {
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...
2402
        $args = func_get_args();
2403
2404
        array_unshift( $args, 'user' );
2405
2406
        $_null = apply_filters( 'pods_meta_delete_user_meta', null, $args );
2407
2408
        if ( null !== $_null )
2409
            return $_null;
2410
2411
        return call_user_func_array( array( $this, 'delete_meta' ), $args );
2412
    }
2413
2414
    /**
2415
     * @return mixed
2416
     */
2417 View Code Duplication
    public function delete_comment_meta () {
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...
2418
        $args = func_get_args();
2419
2420
        array_unshift( $args, 'comment' );
2421
2422
        $_null = apply_filters( 'pods_meta_delete_comment_meta', null, $args );
2423
2424
        if ( null !== $_null )
2425
            return $_null;
2426
2427
        return call_user_func_array( array( $this, 'delete_meta' ), $args );
2428
    }
2429
2430
    /**
2431
     * @return mixed
2432
     */
2433 View Code Duplication
    public function delete_term_meta () {
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...
2434
        $args = func_get_args();
2435
2436
        array_unshift( $args, 'term' );
2437
2438
        $_null = apply_filters( 'pods_meta_delete_term_meta', null, $args );
2439
2440
        if ( null !== $_null )
2441
            return $_null;
2442
2443
        return call_user_func_array( array( $this, 'delete_meta' ), $args );
2444
    }
2445
2446
    /**
2447
     * @return mixed
2448
     */
2449 View Code Duplication
    public function delete_option () {
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...
2450
        $args = func_get_args();
2451
2452
        array_unshift( $args, 'settings' );
2453
2454
        $_null = apply_filters( 'pods_meta_delete_option', null, $args );
2455
2456
        if ( null !== $_null )
2457
            return $_null;
2458
2459
        return call_user_func_array( array( $this, 'delete_meta' ), $args );
2460
    }
2461
2462
    /*
2463
     * The real meta functions
2464
     */
2465
    /**
2466
     * @param $object_type
2467
     * @param $object_id
2468
     * @param string $aux
2469
     *
2470
     * @return bool|mixed
2471
     */
2472
    public function get_object ( $object_type, $object_id, $aux = '' ) {
2473
    	
2474
    	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...
2475
    	
2476
    	if ( 'term' == $object_type ) {
2477
    		$object_type = 'taxonomy';
2478
    	}
2479
    	
2480
        if ( 'post_type' == $object_type )
2481
            $objects = self::$post_types;
2482
        elseif ( 'taxonomy' == $object_type )
2483
            $objects = self::$taxonomies;
2484
        elseif ( 'media' == $object_type )
2485
            $objects = self::$media;
2486
        elseif ( 'user' == $object_type )
2487
            $objects = self::$user;
2488
        elseif ( 'comment' == $object_type )
2489
            $objects = self::$comment;
2490
        elseif ( 'settings' == $object_type )
2491
            $objects = self::$settings;
2492
        else
2493
            return false;
2494
2495
        if ( empty( $objects ) || !is_array( $objects ) )
2496
            return false;
2497
2498
        $object_name = null;
2499
2500
        if ( 'media' == $object_type )
2501
            return @current( $objects );
2502
        elseif ( 'user' == $object_type )
2503
            return @current( $objects );
2504
        elseif ( 'comment' == $object_type )
2505
            return @current( $objects );
2506
        elseif ( 'post_type' == $object_type ) {
2507
            $object = get_post( $object_id );
2508
2509
            if ( !is_object( $object ) || !isset( $object->post_type ) )
2510
                return false;
2511
2512
            $object_name = $object->post_type;
2513
        }
2514
        elseif ( 'taxonomy' == $object_type ) {
2515
            if ( pods_version_check( 'wp', '4.4' ) ) {
2516
            	$object = get_term( $object_id );
2517
2518
            	if ( !is_object( $object ) || !isset( $object->taxonomy ) )
2519
                	return false;
2520
            	
2521
            	$object_name = $object->taxonomy;
2522
            } elseif ( empty( $aux ) ) {
2523
            	$object_name = $wpdb->get_var( $wpdb->prepare( "SELECT `taxonomy` FROM `{$wpdb->term_taxonomy}` WHERE `term_id` = %d", $object_id ) );
2524
            } else { 
2525
            	$object_name = $aux;
2526
            }
2527
        }
2528
        elseif ( 'settings' == $object_type )
2529
            $object = $object_id;
2530
        else
2531
            return false;
2532
2533
        $reserved_post_types = array(
2534
			'revision'
2535
        );
2536
2537
        $reserved_post_types = apply_filters( 'pods_meta_reserved_post_types', $reserved_post_types, $object_type, $object_id, $object_name, $objects );
2538
2539
        if ( empty( $object_name ) || ( 'post_type' == $object_type && ( 0 === strpos( $object_name, '_pods_' ) ) || in_array( $object_name, $reserved_post_types ) ) ) {
2540
            return false;
2541
		}
2542
		elseif ( 'attachment' == $object_name ) {
2543
			return @current( self::$media );
2544
		}
2545
2546
        $recheck = array();
2547
2548
        // Return first created by Pods, save extended for later
2549
        foreach ( $objects as $pod ) {
2550
            if ( $object_name == $pod[ 'object' ] )
2551
                $recheck[] = $pod;
2552
2553
            if ( '' == $pod[ 'object' ] && $object_name == $pod[ 'name' ] )
2554
                return $pod;
2555
        }
2556
2557
        // If no objects created by Pods, return first extended
2558
        foreach ( $recheck as $pod ) {
2559
            return $pod;
2560
        }
2561
2562
        return false;
2563
    }
2564
2565
    /**
2566
     * @param $object_type
2567
     * @param null $_null
2568
     * @param int $object_id
2569
     * @param string $meta_key
2570
     * @param bool $single
2571
     *
2572
     * @return array|bool|int|mixed|null|string|void
2573
     */
2574
    public function get_meta ( $object_type, $_null = null, $object_id = 0, $meta_key = '', $single = false ) {
2575
        $meta_type = $object_type;
2576
2577
        if ( in_array( $meta_type, array( 'post_type', 'media' ) ) )
2578
            $meta_type = 'post';
2579
        elseif ( 'taxonomy' == $meta_type )
2580
            $meta_type = 'term';
2581
2582
        if ( empty( $meta_key ) ) {
2583
			if ( !defined( 'PODS_ALLOW_FULL_META' ) || !PODS_ALLOW_FULL_META ) {
2584
				return $_null; // don't cover get_post_meta( $id )
2585
			}
2586
2587
			$single = false;
2588
		}
2589
2590
        $object = $this->get_object( $object_type, $object_id );
2591
2592
        if ( empty( $object_id ) || empty( $object ) )
2593
            return $_null;
2594
2595
        $no_conflict = pods_no_conflict_check( $meta_type );
2596
2597
        if ( !$no_conflict )
2598
            pods_no_conflict_on( $meta_type );
2599
2600
        $meta_cache = array();
2601
2602
        if ( !$single && isset( $GLOBALS[ 'wp_object_cache' ] ) && is_object( $GLOBALS[ 'wp_object_cache' ] ) ) {
2603
            $meta_cache = wp_cache_get( $object_id, 'pods_' . $meta_type . '_meta' );
2604
2605
            if ( empty( $meta_cache ) ) {
2606
                $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
2607
2608
                if ( empty( $meta_cache ) ) {
2609
                    $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
2610
                    $meta_cache = $meta_cache[ $object_id ];
2611
                }
2612
            }
2613
        }
2614
2615
        if ( empty( $meta_cache ) || !is_array( $meta_cache ) )
2616
            $meta_cache = array();
2617
2618 View Code Duplication
        if ( !is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object[ 'name' ] )
2619
            self::$current_field_pod = pods( $object[ 'name' ], $object_id );
2620
		elseif ( self::$current_field_pod->id() != $object_id )
2621
			self::$current_field_pod->fetch( $object_id );
2622
2623
        $pod = self::$current_field_pod;
2624
2625
        $meta_keys = array( $meta_key );
2626
2627
        if ( empty( $meta_key ) )
2628
            $meta_keys = array_keys( $meta_cache );
2629
2630
        $key_found = false;
2631
2632
        foreach ( $meta_keys as $meta_k ) {
2633
            if ( !empty( $pod ) ) {
2634
                if ( isset( $pod->fields[ $meta_k ] ) ) {
2635
                    $key_found = true;
2636
2637
                    $meta_cache[ $meta_k ] = $pod->field( array( 'name' => $meta_k, 'single' => $single, 'get_meta' => true ) );
2638
2639 View Code Duplication
                    if ( ( !is_array( $meta_cache[ $meta_k ] ) || !isset( $meta_cache[ $meta_k ][ 0 ] ) ) ) {
2640
                        if ( empty( $meta_cache[ $meta_k ] ) && !is_array( $meta_cache[ $meta_k ] ) && $single )
2641
                            $meta_cache[ $meta_k ] = array();
2642
                        else
2643
                            $meta_cache[ $meta_k ] = array( $meta_cache[ $meta_k ] );
2644
                    }
2645
2646 View Code Duplication
                    if ( in_array( $pod->fields[ $meta_k ][ 'type' ], PodsForm::tableless_field_types() ) && isset( $meta_cache[ '_pods_' . $meta_k ] ) )
2647
                        unset( $meta_cache[ '_pods_' . $meta_k ] );
2648
                }
2649
                elseif ( false !== strpos( $meta_k, '.' ) ) {
2650
                    $key_found = true;
2651
2652
                    $first = current( explode( '.', $meta_k ) );
2653
2654
                    if ( isset( $pod->fields[ $first ] ) ) {
2655
                        $meta_cache[ $meta_k ] = $pod->field( array( 'name' => $meta_k, 'single' => $single, 'get_meta' => true ) );
2656
2657 View Code Duplication
                        if ( ( !is_array( $meta_cache[ $meta_k ] ) || !isset( $meta_cache[ $meta_k ][ 0 ] ) ) && $single ) {
2658
                            if ( empty( $meta_cache[ $meta_k ] ) && !is_array( $meta_cache[ $meta_k ] ) && $single )
2659
                                $meta_cache[ $meta_k ] = array();
2660
                            else
2661
                                $meta_cache[ $meta_k ] = array( $meta_cache[ $meta_k ] );
2662
                        }
2663
2664 View Code Duplication
                        if ( in_array( $pod->fields[ $first ][ 'type' ], PodsForm::tableless_field_types() ) && isset( $meta_cache[ '_pods_' . $first ] ) )
2665
                            unset( $meta_cache[ '_pods_' . $first ] );
2666
                    }
2667
                }
2668
            }
2669
        }
2670
2671
        if ( !$no_conflict )
2672
            pods_no_conflict_off( $meta_type );
2673
2674
        unset( $pod ); // memory clear
2675
2676
        if ( !$key_found )
2677
            return $_null;
2678
2679
        if ( !$single && isset( $GLOBALS[ 'wp_object_cache' ] ) && is_object( $GLOBALS[ 'wp_object_cache' ] ) )
2680
            wp_cache_set( $object_id, $meta_cache, 'pods_' . $meta_type . '_meta' );
2681
2682
        if ( empty( $meta_key ) )
2683
            return $meta_cache;
2684
        elseif ( isset( $meta_cache[ $meta_key ] ) )
2685
            $value = $meta_cache[ $meta_key ];
2686
        else
2687
            $value = '';
2688
2689
        if ( !is_numeric( $value ) && empty( $value ) ) {
2690
            if ( $single )
2691
                $value = '';
2692
            else
2693
                $value = array();
2694
        }
2695
        // get_metadata requires $meta[ 0 ] to be set for first value to be retrieved
2696
        elseif ( !is_array( $value ) )
2697
            $value = array( $value );
2698
2699
        return $value;
2700
    }
2701
2702
    /**
2703
     * @param $object_type
2704
     * @param null $_null
2705
     * @param int $object_id
2706
     * @param string $meta_key
2707
     * @param string $meta_value
2708
     * @param bool $unique
2709
     *
2710
     * @return bool|int|null
2711
     */
2712
    public function add_meta ( $object_type, $_null = null, $object_id = 0, $meta_key = '', $meta_value = '', $unique = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $unique 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...
2713
        if ( pods_tableless() )
2714
            return $_null;
2715
2716
        $object = $this->get_object( $object_type, $object_id );
2717
2718 View Code Duplication
        if ( empty( $object_id ) || empty( $object ) || !isset( $object[ 'fields' ][ $meta_key ] ) )
2719
            return $_null;
2720
2721
        if ( in_array( $object[ 'fields' ][ $meta_key ][ 'type' ], PodsForm::tableless_field_types() ) ) {
2722 View Code Duplication
            if ( !is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object[ 'name' ] )
2723
                self::$current_field_pod = pods( $object[ 'name' ], $object_id );
2724
			elseif ( self::$current_field_pod->id() != $object_id )
2725
				self::$current_field_pod->fetch( $object_id );
2726
2727
            $pod = self::$current_field_pod;
2728
2729
            $pod->add_to( $meta_key, $meta_value );
2730
        }
2731
        else {
2732 View Code Duplication
            if ( !is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object[ 'name' ] )
2733
                self::$current_field_pod = pods( $object[ 'name' ] );
2734
2735
            $pod = self::$current_field_pod;
2736
2737
            $pod->save( $meta_key, $meta_value, $object_id );
2738
        }
2739
2740
        return $object_id;
2741
    }
2742
2743
    /**
2744
     * @param $object_type
2745
     * @param null $_null
2746
     * @param int $object_id
2747
     * @param string $meta_key
2748
     * @param string $meta_value
2749
     * @param string $prev_value
2750
     *
2751
     * @return bool|int|null
2752
     */
2753
    public function update_meta ( $object_type, $_null = null, $object_id = 0, $meta_key = '', $meta_value = '', $prev_value = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $prev_value 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...
2754
        if ( pods_tableless() )
2755
            return $_null;
2756
2757
        $object = $this->get_object( $object_type, $object_id );
2758
2759 View Code Duplication
        if ( empty( $object_id ) || empty( $object ) || !isset( $object[ 'fields' ][ $meta_key ] ) )
2760
            return $_null;
2761
2762 View Code Duplication
        if ( !is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object[ 'name' ] )
2763
            self::$current_field_pod = pods( $object[ 'name' ] );
2764
2765
        $pod = self::$current_field_pod;
2766
2767
        if ( ( isset( $pod->fields[ $meta_key ] ) || false !== strpos( $meta_key, '.' ) ) && $pod->row !== null) {
2768
2769
            $key = $meta_key;
2770
            if(false !== strpos( $meta_key, '.' )){
2771
                $key = current( explode( '.', $meta_key ) );
2772
            }
2773
2774
            $pod->row[ $meta_key ] = $meta_value;
2775
2776 View Code Duplication
            if ( isset( $pod->fields[ $key ] ) ) {
2777
                if ( in_array( $pod->fields[ $key ][ 'type' ], PodsForm::tableless_field_types() ) && isset( $meta_cache[ '_pods_' . $key ] ) )
0 ignored issues
show
Bug introduced by
The variable $meta_cache seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

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

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

Loading history...
2778
                    unset( $meta_cache[ '_pods_' . $key ] );
2779
            }
2780
2781
        }
2782
2783
        $pod->save( $meta_key, $meta_value, $object_id );
2784
2785
        return $object_id;
2786
    }
2787
2788
    /**
2789
     * @param $object_type
2790
     * @param null $_null
2791
     * @param int $object_id
2792
     * @param string $meta_key
2793
     * @param string $meta_value
2794
     * @param bool $delete_all
2795
     *
2796
     * @return null
2797
     */
2798
    public function delete_meta ( $object_type, $_null = null, $object_id = 0, $meta_key = '', $meta_value = '', $delete_all = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $delete_all 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...
2799
        if ( pods_tableless() )
2800
            return $_null;
2801
2802
        $object = $this->get_object( $object_type, $object_id );
2803
2804 View Code Duplication
        if ( empty( $object_id ) || empty( $object ) || !isset( $object[ 'fields' ][ $meta_key ] ) )
2805
            return $_null;
2806
2807
        // @todo handle $delete_all (delete the field values from all pod items)
2808
        if ( !empty( $meta_value ) && in_array( $object[ 'fields' ][ $meta_key ][ 'type' ], PodsForm::tableless_field_types() ) ) {
2809 View Code Duplication
            if ( !is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object[ 'name' ] )
2810
                self::$current_field_pod = pods( $object[ 'name' ], $object_id );
2811
			elseif ( self::$current_field_pod->id() != $object_id )
2812
				self::$current_field_pod->fetch( $object_id );
2813
2814
            $pod = self::$current_field_pod;
2815
2816
            $pod->remove_from( $meta_key, $meta_value );
2817
        }
2818
        else {
2819 View Code Duplication
            if ( !is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object[ 'name' ] )
2820
                self::$current_field_pod = pods( $object[ 'name' ] );
2821
2822
            $pod = self::$current_field_pod;
2823
2824
            $pod->save( array( $meta_key => null ), null, $object_id );
2825
        }
2826
2827
        return $_null;
2828
    }
2829
2830
    public function delete_post ( $id ) {
2831
        $post = get_post( $id );
2832
2833
        if ( empty( $post ) )
2834
            return;
2835
2836
        $id = $post->ID;
2837
        $post_type = $post->post_type;
2838
2839
        return $this->delete_object( 'post_type', $id, $post_type );
2840
    }
2841
2842
    public function delete_taxonomy ( $id ) {
2843
        /**
2844
         * @var $wpdb WPDB
2845
         */
2846
        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...
2847
2848
        $terms = $wpdb->get_results( "SELECT `term_id`, `taxonomy` FROM `{$wpdb->term_taxonomy}` WHERE `term_taxonomy_id` = {$id}" );
2849
2850
        if ( empty( $terms ) )
2851
            return;
2852
2853
        foreach ( $terms as $term ) {
2854
            $id = $term->term_id;
2855
            $taxonomy = $term->taxonomy;
2856
2857
            $this->delete_object( 'taxonomy', $id, $taxonomy );
2858
        }
2859
    }
2860
2861
    /**
2862
     * Hook the split_shared_term action and point it to this method
2863
     *
2864
     * Fires after a previously shared taxonomy term is split into two separate terms.
2865
     *
2866
     * @param int    $term_id          ID of the formerly shared term.
2867
     * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
2868
     * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
2869
     * @param string $taxonomy         Taxonomy for the split term.
2870
     */
2871
    public static function split_shared_term( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
2872
2873
        require_once( PODS_DIR . 'classes/PodsTermSplitting.php' );
2874
2875
        $term_splitting = new Pods_Term_Splitting( $term_id, $new_term_id, $taxonomy );
2876
        $term_splitting->split_shared_term();
2877
2878
    }
2879
2880
    public function delete_user ( $id ) {
2881
        return $this->delete_object( 'user', $id );
2882
    }
2883
2884
    public function delete_comment ( $id ) {
2885
        return $this->delete_object( 'comment', $id );
2886
    }
2887
2888
    public function delete_media ( $id ) {
2889
        return $this->delete_object( 'media', $id );
2890
    }
2891
2892
    public function delete_object ( $type, $id, $name = null ) {
2893
        if ( empty( $name ) )
2894
            $name = $type;
2895
2896
        $object = $this->object_get( $type, $name );
2897
2898
        if ( !empty( $object ) ) {
2899
            $params = array(
2900
                'pod' => pods_var( 'name', $object ),
2901
                'pod_id' => pods_var( 'id', $object ),
2902
                'id' => $id
2903
            );
2904
2905
            return pods_api()->delete_pod_item( $params, false );
2906
        }
2907
        else
2908
            return pods_api()->delete_object_from_relationships( $id, $type, $name );
2909
    }
2910
}
2911