Completed
Push — 2.x ( 525abd...03f01a )
by Scott Kingsley
12:16 queued 06:02
created

PodsMeta::meta_post_add()   C

Complexity

Conditions 13
Paths 109

Size

Total Lines 52
Code Lines 33

Duplication

Lines 13
Ratio 25 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
eloc 33
c 2
b 0
f 0
nc 109
nop 2
dl 13
loc 52
rs 5.7313

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 ( 'term' == $type ) {
758
        	$type = 'taxonomy';
759
        }
760
761
        if ( 'taxonomy' == $type )
762
            $object = self::$taxonomies;
763
        elseif ( 'media' == $type )
764
            $object = self::$media;
765
        elseif ( 'user' == $type )
766
            $object = self::$user;
767
        elseif ( 'comment' == $type )
768
            $object = self::$comment;
769
770
        if ( 'pod' != $type && !empty( $object ) && is_array( $object ) && isset( $object[ $name ] ) )
771
            $pod = $object[ $name ];
772 View Code Duplication
        else {
773
            if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $name )
774
                self::$current_pod_data = pods_api()->load_pod( array( 'name' => $name ), false );
775
776
            $pod = self::$current_pod_data;
777
        }
778
779
        if ( empty( $pod ) )
780
            return array();
781
782
        $defaults = array(
783
            'name' => 'post',
784
            'object' => 'post',
785
            'type' => 'post_type'
786
        );
787
788
        $pod = array_merge( $defaults, (array) $pod );
789
790 View Code Duplication
        if ( empty( $pod[ 'name' ] ) )
791
            $pod[ 'name' ] = $pod[ 'object' ];
792
        elseif ( empty( $pod[ 'object' ] ) )
793
            $pod[ 'object' ] = $pod[ 'name' ];
794
795
        if ( $pod[ 'type' ] != $type )
796
            return array();
797
798
        return $pod;
799
    }
800
801
    /**
802
     * @param $type
803
     * @param $name
804
     * @param $default_fields
805
     *
806
     * @return array
807
     */
808
    public function groups_get ( $type, $name, $default_fields = null ) {
809
        if ( 'post_type' == $type && 'attachment' == $name ) {
810
            $type = 'media';
811
            $name = 'media';
812
        } elseif ( 'term' == $type ) {
813
            $type = 'taxonomy';
814
        }
815
816
        do_action( 'pods_meta_groups', $type, $name );
817
818
        $pod = array();
819
        $fields = array();
820
821
        $object = self::$post_types;
822
823
        if ( 'taxonomy' == $type )
824
            $object = self::$taxonomies;
825
        elseif ( 'media' == $type )
826
            $object = self::$media;
827
        elseif ( 'user' == $type )
828
            $object = self::$user;
829
        elseif ( 'comment' == $type )
830
            $object = self::$comment;
831
        elseif ( 'pod' == $type )
832
            $object = self::$advanced_content_types;
833
834
        if ( !empty( $object ) && is_array( $object ) && isset( $object[ $name ] ) )
835
            $fields = $object[ $name ][ 'fields' ];
836
        else {
837 View Code Duplication
            if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $name )
838
                self::$current_pod_data = pods_api()->load_pod( array( 'name' => $name ), false );
839
840
            $pod = self::$current_pod_data;
841
842
            if ( !empty( $pod ) )
843
                $fields = $pod[ 'fields' ];
844
        }
845
846
        if ( null !== $default_fields ) {
847
            $fields = $default_fields;
848
        }
849
850
        $defaults = array(
851
            'name' => 'post',
852
            'object' => 'post',
853
            'type' => 'post_type'
854
        );
855
856
        $pod = array_merge( $defaults, (array) $pod );
857
858 View Code Duplication
        if ( empty( $pod[ 'name' ] ) )
859
            $pod[ 'name' ] = $pod[ 'object' ];
860
        elseif ( empty( $pod[ 'object' ] ) )
861
            $pod[ 'object' ] = $pod[ 'name' ];
862
863
        if ( $pod[ 'type' ] != $type )
864
            return array();
865
866
        $groups = array(
867
            array(
868
                'pod' => $pod,
869
				/**
870
				 * Filter the title of the Pods Metabox In The Post Editor
871
				 *
872
				 * @param string $title The title to use, default is 'More Fields'
873
				 * @param obj|Pod $pod Current Pods Object
874
				 * @param array $fields Array of fields that will go in the metabox
875
				 * @param string $type The type of Pod
876
				 * @params string $name Name of the Pod
877
				 *
878
				 * @returns string The title for the metabox.
879
				 *
880
				 * @since unknown
881
				 */
882
				'label' => apply_filters( 'pods_meta_default_box_title', __( 'More Fields', 'pods' ), $pod, $fields, $type, $name ),
883
                'fields' => $fields,
884
                'context' => 'normal',
885
                'priority' => 'default'
886
            )
887
        );
888
889
        if ( isset( self::$groups[ $type ] ) && isset( self::$groups[ $type ][ $name ] ) )
890
            $groups = self::$groups[ $type ][ $name ];
891
892
        /**
893
         * Filter the array of field groups
894
         *
895
         * @param array  $groups Array of groups
896
         * @param string  $type The type of Pod
897
         * @param string  $name Name of the Pod
898
         *
899
         * @since 2.6.6
900
         */
901
        return apply_filters( 'pods_meta_groups_get', $groups, $type, $name );
902
    }
903
904
    /**
905
     * @param $post_type
906
     * @param null $post
907
     */
908
    public function meta_post_add ( $post_type, $post = null ) {
909
        if ( 'comment' == $post_type )
910
            return;
911
912
        if ( is_object( $post ) )
913
            $post_type = $post->post_type;
914
915
        $groups = $this->groups_get( 'post_type', $post_type );
916
        $pods_field_found = false;
917
918
        foreach ( $groups as $group ) {
919
            if ( empty( $group[ 'fields' ] ) )
920
                continue;
921
922
            $field_found = false;
923
            $group_hidden = true;
924
925
            foreach ( $group[ 'fields' ] as $field ) {
926
                if ( false !== PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ] ) ) {
927
                    $field_found = true;
928
                }
929
                if ( ! isset( $field['options']['hidden'] ) || 1 != (int) $field['options']['hidden'] ) {
930
                    $group_hidden = false;
931
                }
932
            }
933
934
            if ( $group_hidden ) 
935
                continue;
936
937
            if ( empty( $group[ 'label' ] ) )
938
                $group[ 'label' ] = get_post_type_object( $post_type )->labels->label;
939
940 View Code Duplication
            if ( $field_found ) {
941
                $pods_field_found = true;
942
                add_meta_box(
943
                    'pods-meta-' . sanitize_title( $group[ 'label' ] ),
944
                    $group[ 'label' ],
945
                    array( $this, 'meta_post' ),
946
                    $post_type,
947
                    $group[ 'context' ],
948
                    $group[ 'priority' ],
949
                    array( 'group' => $group )
950
                );
951
952
            }
953
        }
954
955
		if ( $pods_field_found ) {
956
			// Only add the classes to forms that actually have pods fields
957
			add_action( 'post_edit_form_tag', array( $this, 'add_class_submittable' ) );
958
		}
959
    }
960
961
    /**
962
     *
963
     * Called by 'post_edit_form_tag' action to include the classes in the <form> tag
964
     *
965
     */
966
    public function add_class_submittable () {
967
        echo ' class="pods-submittable pods-form"';
968
    }
969
970
    /**
971
     * @param $post
972
     * @param $metabox
973
     */
974
    public function meta_post ( $post, $metabox ) {
975
        wp_enqueue_style( 'pods-form' );
976
        wp_enqueue_script( 'pods' );
977
978
		$pod_type = 'post';
979
980
		if ( 'attachment' == $post->post_type ) {
981
			$pod_type = 'media';
982
		}
983
984
        do_action( 'pods_meta_' . __FUNCTION__, $post );
985
986
        $hidden_fields = array();
987
        
988
        $id = null;
989
990
        if ( is_object( $post ) && false === strpos( $_SERVER[ 'REQUEST_URI' ], '/post-new.php' ) )
991
            $id = $post->ID;
992
993 View Code Duplication
	if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod ) || self::$current_pod->pod != $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ] ) {
994
		self::$current_pod = pods( $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ], $id, true );
995
	} elseif ( self::$current_pod->id() != $id ) {
996
		self::$current_pod->fetch( $id );
997
	}
998
999
        $pod = self::$current_pod;
1000
1001
	$fields = $metabox['args']['group']['fields'];
1002
1003
	/**
1004
	 * Filter the fields used for the Pods metabox group
1005
	 *
1006
	 * @since 2.6.6
1007
	 *
1008
	 * @param array   $fields  Fields from the current Pod metabox group
1009
	 * @param int     $id      Post ID
1010
	 * @param WP_Post $post    Post object
1011
	 * @param array	  $metabox Metabox args from the current Pod metabox group
1012
	 * @param Pods    $pod     Pod object
1013
	 */
1014
 	$fields = apply_filters( 'pods_meta_post_fields', $fields, $id,  $post, $metabox, $pod  );
1015
 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
1016
 	if ( empty( $fields ) ) {
1017
 		_e( 'There are no fields to display', 'pods' );
1018
 		
1019
 		return;
1020
 	}
1021
?>
1022
    <table class="form-table pods-metabox pods-admin pods-dependency">
1023
	<?php
1024
	echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_' . $pod_type ), 'hidden' );
1025
	
1026
        foreach ( $fields as $field ) {
1027 View Code Duplication
            if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field[ 'options' ], $fields, $pod, $id ) ) {
1028
                if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1029
                    $field[ 'type' ] = 'hidden';
1030
                else
1031
                    continue;
1032
            }
1033
            elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1034
                $field[ 'type' ] = 'hidden';
1035
1036
            $value = '';
1037
1038 View Code Duplication
            if ( !empty( $pod ) ) {
1039
                pods_no_conflict_on( 'post' );
1040
1041
                $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1042
1043
                pods_no_conflict_off( 'post' );
1044
            }
1045
            elseif ( !empty( $id ) )
1046
                $value = get_post_meta( $id, $field[ 'name' ], true );
1047
1048
            if ( 'hidden' == $field[ 'type' ] ) {
1049
                $hidden_fields[] = array(
1050
                    'field' => $field,
1051
                    'value' => $value
1052
                );
1053
            }
1054
            else {
1055
                $depends = PodsForm::dependencies( $field, 'pods-meta-' );
1056
1057
            do_action( 'pods_meta_' . __FUNCTION__ . '_' . $field[ 'name' ], $post, $field, $pod );
1058
        ?>
1059
            <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 ); ?>">
1060
                <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th>
1061
                <td>
1062
                    <?php
1063
                        // Remove any extra ? help icons
1064
                        if ( isset( $field[ 'help' ] ) )
1065
                            unset( $field[ 'help' ] );
1066
                    ?>
1067
			<div class="pods-submittable-fields">
1068
                    <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
1069
                    <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
1070
			</div>
1071
                </td>
1072
            </tr>
1073
        <?php
1074
                do_action( 'pods_meta_' . __FUNCTION__ . '_' . $field[ 'name' ] . '_post', $post, $field, $pod );
1075
            }
1076
        }
1077
        ?>
1078
    </table>
1079
1080
    <?php
1081
        do_action( 'pods_meta_' . __FUNCTION__ . '_post', $post );
1082
1083 View Code Duplication
        foreach ( $hidden_fields as $hidden_field ) {
1084
            $field = $hidden_field[ 'field' ];
1085
1086
            echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $hidden_field[ 'value' ], 'hidden' );
1087
        }
1088
    ?>
1089
1090
    <script type="text/javascript">
1091
        jQuery( function ( $ ) {
1092
            $( document ).Pods( 'validate' );
1093
            $( document ).Pods( 'submit_meta' );
1094
            $( document ).Pods( 'dependency', true );
1095
        } );
1096
    </script>
1097
<?php
1098
    }
1099
1100
    /**
1101
     * @param $new_status
1102
     * @param $old_status
1103
     * @param $post
1104
     */
1105
    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...
1106
        self::$old_post_status = $old_status;
1107
    }
1108
1109
    /**
1110
     * @param $post_id
1111
     * @param $post
1112
     * @param $update
1113
     *
1114
     * @return int Post ID
1115
     */
1116
    public function save_post ( $post_id, $post, $update = null ) {
1117
        $is_new_item = false;
1118
1119
		if ( is_bool( $update ) )
1120
			$is_new_item = !$update; // false is new item
1121
		elseif ( 'new' == self::$old_post_status ) {
1122
			$is_new_item = true;
1123
		}
1124
1125 View Code Duplication
		if ( empty( $_POST ) ) {
1126
			return $post_id;
1127
		}
1128
		elseif ( !$is_new_item && !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_post' ) ) {
1129
			return $post_id;
1130
		}
1131
1132
		// Reset to avoid manual new post issues
1133
		self::$old_post_status = '';
1134
1135
		$blacklisted_types = array(
1136
			'revision',
1137
			'_pods_pod',
1138
			'_pods_field'
1139
		);
1140
1141
		$blacklisted_types = apply_filters( 'pods_meta_save_post_blacklist_types', $blacklisted_types, $post_id, $post );
1142
1143
		// @todo Figure out how to hook into autosave for saving meta
1144
1145
		// Block Autosave and Revisions
1146
		if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || in_array( $post->post_type, $blacklisted_types ) )
1147
			return $post_id;
1148
1149
		// Block Quick Edits / Bulk Edits
1150
		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' ) ) ) )
1151
			return $post_id;
1152
1153
		// Block Trash
1154
		if ( in_array( pods_var( 'action', 'get' ), array( 'untrash', 'trash' ) ) )
1155
			return $post_id;
1156
1157
		// Block Auto-drafting and Trash (not via Admin action)
1158
		$blacklisted_status = array(
1159
			'auto-draft',
1160
			'trash'
1161
		);
1162
1163
		$blacklisted_status = apply_filters( 'pods_meta_save_post_blacklist_status', $blacklisted_status, $post_id, $post );
1164
1165
		if ( in_array( $post->post_status, $blacklisted_status ) )
1166
			return $post_id;
1167
1168
		$groups = $this->groups_get( 'post_type', $post->post_type );
1169
1170
		if ( empty( $groups ) )
1171
			return $post_id;
1172
1173
		$data = array();
1174
1175
		$id = $post_id;
1176
		$pod = null;
1177
1178 View Code Duplication
		foreach ( $groups as $group ) {
1179
			if ( empty( $group[ 'fields' ] ) )
1180
				continue;
1181
1182
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1183
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1184
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1185
				elseif ( self::$current_pod->id() != $id )
1186
					self::$current_pod->fetch( $id );
1187
1188
				$pod = self::$current_pod;
1189
			}
1190
1191
			foreach ( $group[ 'fields' ] as $field ) {
1192
1193
				if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1194
					if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
1195
						continue;
1196
				}
1197
1198
				$data[ $field[ 'name' ] ] = '';
1199
1200
				if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
1201
					$data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
1202
			}
1203
		}
1204
1205 View Code Duplication
		if ( $is_new_item ) {
1206
			do_action( 'pods_meta_create_pre_post', $data, $pod, $id, $groups, $post, $post->post_type );
1207
			do_action( "pods_meta_create_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post );
1208
		}
1209
1210
		do_action( 'pods_meta_save_pre_post', $data, $pod, $id, $groups, $post, $post->post_type, $is_new_item );
1211
		do_action( "pods_meta_save_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post, $is_new_item );
1212
1213
		pods_no_conflict_on( 'post' );
1214
1215 View Code Duplication
		if ( !empty( $pod ) ) {
1216
			// Fix for Pods doing it's own sanitization
1217
			$data = pods_unslash( (array) $data );
1218
1219
			$pod->save( $data, null, null, array( 'is_new_item' => $is_new_item ) );
1220
		}
1221
		elseif ( !empty( $id ) ) {
1222
			foreach ( $data as $field => $value ) {
1223
				update_post_meta( $id, $field, $value );
1224
			}
1225
		}
1226
1227
		pods_no_conflict_off( 'post' );
1228
1229 View Code Duplication
		if ( $is_new_item ) {
1230
			do_action( 'pods_meta_create_post', $data, $pod, $id, $groups, $post, $post->post_type );
1231
			do_action( "pods_meta_create_post_{$post->post_type}", $data, $pod, $id, $groups, $post );
1232
		}
1233
1234
		do_action( 'pods_meta_save_post', $data, $pod, $id, $groups, $post, $post->post_type, $is_new_item );
1235
		do_action( "pods_meta_save_post_{$post->post_type}", $data, $pod, $id, $groups, $post, $is_new_item );
1236
1237
		return $post_id;
1238
1239
    }
1240
1241
    /**
1242
     * @param $form_fields
1243
     * @param $post
1244
     *
1245
     * @return array
1246
     */
1247
    public function meta_media ( $form_fields, $post ) {
1248
        $groups = $this->groups_get( 'media', 'media' );
1249
1250
        if ( empty( $groups ) || 'attachment' == pods_var( 'typenow', 'global' ) )
1251
            return $form_fields;
1252
1253
        wp_enqueue_style( 'pods-form' );
1254
1255
        $id = null;
1256
1257
        if ( is_object( $post ) )
1258
            $id = $post->ID;
1259
1260
        $pod = null;
1261
1262
		$meta_nonce = PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_media' ), 'hidden' );
1263
1264
        foreach ( $groups as $group ) {
1265
            if ( empty( $group[ 'fields' ] ) )
1266
                continue;
1267
1268
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1269
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1270
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1271
				elseif ( self::$current_pod->id() != $id )
1272
					self::$current_pod->fetch( $id );
1273
1274
				$pod = self::$current_pod;
1275
			}
1276
1277
            foreach ( $group[ 'fields' ] as $field ) {
1278
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1279
                    if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
1280
                        continue;
1281
                }
1282
1283
                $value = '';
1284
1285 View Code Duplication
                if ( !empty( $pod ) )
1286
                    $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1287
                elseif ( !empty( $id ) ) {
1288
                    pods_no_conflict_on( 'post' );
1289
1290
                    $value = get_post_meta( $id, $field[ 'name' ], true );
1291
1292
                    pods_no_conflict_off( 'post' );
1293
                }
1294
1295
                $form_fields[ 'pods_meta_' . $field[ 'name' ] ] = array(
1296
                    'label' => $field[ 'label' ],
1297
                    'input' => 'html',
1298
                    'html' => PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ) . $meta_nonce,
1299
                    'helps' => PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field )
1300
                );
1301
            }
1302
        }
1303
1304
        $form_fields = apply_filters( 'pods_meta_' . __FUNCTION__, $form_fields );
1305
1306
        return $form_fields;
1307
    }
1308
1309
    /**
1310
     * @param $post
1311
     * @param $attachment
1312
     *
1313
     * @return mixed
1314
     */
1315
    public function save_media ( $post, $attachment ) {
1316
        $groups = $this->groups_get( 'media', 'media' );
1317
1318
        if ( empty( $groups ) )
1319
            return $post;
1320
1321
        $post_id = $attachment;
1322
1323
        if ( empty( $_POST ) || !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_media' ) ) {
1324
            return $post;
1325
		}
1326
1327 View Code Duplication
        if ( is_array( $post ) && !empty( $post ) && isset( $post[ 'ID' ] ) && 'attachment' == $post[ 'post_type' ] )
1328
            $post_id = $post[ 'ID' ];
1329
1330
        if ( is_array( $post_id ) || empty( $post_id ) )
1331
            return $post;
1332
1333
        $data = array();
1334
1335
        $id = $post_id;
1336
        $pod = null;
1337
1338 View Code Duplication
        foreach ( $groups as $group ) {
1339
            if ( empty( $group[ 'fields' ] ) )
1340
                continue;
1341
1342
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1343
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1344
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1345
				elseif ( self::$current_pod->id() != $id )
1346
					self::$current_pod->fetch( $id );
1347
1348
				$pod = self::$current_pod;
1349
			}
1350
1351
            foreach ( $group[ 'fields' ] as $field ) {
1352
1353
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1354
                    if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
1355
                        continue;
1356
                }
1357
1358
                $data[ $field[ 'name' ] ] = '';
1359
1360
                if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
1361
                    $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
1362
            }
1363
        }
1364
1365
        do_action( 'pods_meta_save_pre_media', $data, $pod, $id, $groups, $post, $attachment );
1366
1367 View Code Duplication
        if ( !empty( $pod ) ) {
1368
            // Fix for Pods doing it's own sanitization
1369
            $data = pods_unslash( (array) $data );
1370
1371
            $pod->save( $data );
1372
        }
1373
        elseif ( !empty( $id ) ) {
1374
            pods_no_conflict_on( 'post' );
1375
1376
            foreach ( $data as $field => $value ) {
1377
                update_post_meta( $id, $field, $value );
1378
            }
1379
1380
            pods_no_conflict_off( 'post' );
1381
        }
1382
1383
        do_action( 'pods_meta_save_media', $data, $pod, $id, $groups, $post, $attachment );
1384
1385
        return $post;
1386
    }
1387
1388
    public function save_media_ajax () {
1389
        if ( !isset( $_POST[ 'id' ] ) || empty( $_POST[ 'id' ] ) || absint( $_POST[ 'id' ] ) < 1 )
1390
            return;
1391
1392
        $id = absint( $_POST[ 'id' ] );
1393
1394
        if ( !isset( $_POST[ 'nonce' ] ) || empty( $_POST[ 'nonce' ] ) )
1395
            return;
1396
1397
        check_ajax_referer( 'update-post_' . $id, 'nonce' );
1398
1399
        if ( !current_user_can( 'edit_post', $id ) )
1400
            return;
1401
1402
        $post = get_post( $id, ARRAY_A );
1403
1404
    	if ( 'attachment' != $post[ 'post_type' ] )
1405
            return;
1406
1407
        // fix ALL THE THINGS
1408
1409
        if ( !isset( $_REQUEST[ 'attachments' ] ) )
1410
            $_REQUEST[ 'attachments' ] = array();
1411
1412
        if ( !isset( $_REQUEST[ 'attachments' ][ $id ] ) )
1413
            $_REQUEST[ 'attachments' ][ $id ] = array();
1414
1415
        if ( empty( $_REQUEST[ 'attachments' ][ $id ] ) )
1416
            $_REQUEST[ 'attachments' ][ $id ][ '_fix_wp' ] = 1;
1417
    }
1418
1419
    /**
1420
     * @param $tag
1421
     * @param null $taxonomy
1422
     */
1423
    public function meta_taxonomy ( $tag, $taxonomy = null ) {
1424
        wp_enqueue_style( 'pods-form' );
1425
1426
        do_action( 'pods_meta_' . __FUNCTION__, $tag, $taxonomy );
1427
1428
        $taxonomy_name = $taxonomy;
1429
1430
        if ( !is_object( $tag ) )
1431
            $taxonomy_name = $tag;
1432
1433
        $groups = $this->groups_get( 'taxonomy', $taxonomy_name );
1434
1435
        $id = null;
1436
1437
        if ( is_object( $tag ) )
1438
            $id = $tag->term_id;
1439
1440
        $pod = null;
1441
1442
		echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_taxonomy' ), 'hidden' );
1443
1444
        foreach ( $groups as $group ) {
1445
            if ( empty( $group[ 'fields' ] ) )
1446
                continue;
1447
1448
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1449
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1450
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1451
				elseif ( self::$current_pod->id() != $id )
1452
					self::$current_pod->fetch( $id );
1453
1454
				$pod = self::$current_pod;
1455
			}
1456
1457
            foreach ( $group[ 'fields' ] as $field ) {
1458 View Code Duplication
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1459
                    if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1460
                        $field[ 'type' ] = 'hidden';
1461
                    else
1462
                        continue;
1463
                }
1464
                elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1465
                    $field[ 'type' ] = 'hidden';
1466
1467
                $value = '';
1468
1469 View Code Duplication
                if ( !empty( $pod ) )
1470
                    $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1471
1472
                if ( !is_object( $tag ) ) {
1473
            ?>
1474
                <div class="form-field pods-field" style="<?php echo esc_attr( 'hidden' == $field[ 'type' ] ? 'display:none;' : '' ); ?>">
1475
                    <?php
1476
                        echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field );
1477
                        echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
1478
                        echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
1479
                    ?>
1480
                </div>
1481
            <?php
1482
                }
1483
                else {
1484
            ?>
1485
                <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;' : '' ); ?>">
1486
                    <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th>
1487
                    <td>
1488
                        <?php
1489
                            echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
1490
                            echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
1491
                        ?>
1492
                    </td>
1493
                </tr>
1494
            <?php
1495
                }
1496
            }
1497
        }
1498
1499
        do_action( 'pods_meta_' . __FUNCTION__ . '_post', $tag, $taxonomy );
1500
    }
1501
1502
    /**
1503
     * @param $term_id
1504
     * @param $term_taxonomy_id
1505
     * @param $taxonomy
1506
     */
1507
    public function save_taxonomy ( $term_id, $term_taxonomy_id, $taxonomy ) {
1508
        $is_new_item = false;
1509
1510
        if ( 'create_term' == current_filter() )
1511
            $is_new_item = true;
1512
1513
        if ( empty( $_POST ) || !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_taxonomy' ) ) {
1514
            return $term_id;
1515
		}
1516
1517
		// Block Quick Edits / Bulk Edits
1518
		if ( 'inline-save-tax' == pods_var( 'action', 'post' ) || null != pods_var( 'delete_tags', 'post' ) ) {
1519
            return $term_id;
1520
		}
1521
1522
        $groups = $this->groups_get( 'taxonomy', $taxonomy );
1523
1524
        if ( empty( $groups ) )
1525
            return $term_id;
1526
1527
		$term = null;
1528
1529
        $id = $term_id;
1530
        $pod = null;
1531
1532
		$has_fields = false;
1533
1534
        foreach ( $groups as $group ) {
1535
            if ( empty( $group[ 'fields' ] ) )
1536
                continue;
1537
1538
			if ( null === $term ) {
1539
				$term = get_term( $term_id, $taxonomy );
1540
1541
				$data = array(
1542
					'name' => $term->name
1543
				);
1544
			}
1545
1546
			$has_fields = true;
1547
1548
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1549
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1550
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1551
				elseif ( self::$current_pod->id() != $id )
1552
					self::$current_pod->fetch( $id );
1553
1554
				$pod = self::$current_pod;
1555
			}
1556
1557
            foreach ( $group[ 'fields' ] as $field ) {
1558
1559
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1560
                    if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
1561
                        continue;
1562
                }
1563
1564
                $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...
1565
1566
                if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
1567
                    $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
1568
            }
1569
        }
1570
1571
		if ( !$has_fields ) {
1572
			return $term_id;
1573
		}
1574
1575 View Code Duplication
        if ( $is_new_item ) {
1576
            do_action( 'pods_meta_create_pre_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1577
            do_action( "pods_meta_create_pre_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1578
        }
1579
1580
        do_action( 'pods_meta_save_pre_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1581
        do_action( "pods_meta_save_pre_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1582
1583
        pods_no_conflict_on( 'taxonomy' );
1584
1585
        if ( !empty( $pod ) ) {
1586
            // Fix for Pods doing it's own sanitization
1587
            $data = pods_unslash( (array) $data );
1588
1589
            $pod->save( $data, null, null, array( 'is_new_item' => $is_new_item ) );
1590
        }
1591
1592
        pods_no_conflict_off( 'taxonomy' );
1593
1594 View Code Duplication
        if ( $is_new_item ) {
1595
            do_action( 'pods_meta_create_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1596
            do_action( "pods_meta_create_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1597
        }
1598
1599
        do_action( 'pods_meta_save_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1600
        do_action( "pods_meta_save_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1601
1602
		return $term_id;
1603
    }
1604
1605
    /**
1606
     * @param $user_id
1607
     */
1608
    public function meta_user ( $user_id ) {
1609
        wp_enqueue_style( 'pods-form' );
1610
1611
        do_action( 'pods_meta_' . __FUNCTION__, $user_id );
1612
1613
        $groups = $this->groups_get( 'user', 'user' );
1614
1615
        if ( is_object( $user_id ) )
1616
            $user_id = $user_id->ID;
1617
1618
        $id = $user_id;
1619
        $pod = null;
1620
1621
        foreach ( $groups as $group ) {
1622
            if ( empty( $group[ 'fields' ] ) )
1623
                continue;
1624
1625
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1626
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1627
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1628
				elseif ( self::$current_pod->id() != $id )
1629
					self::$current_pod->fetch( $id );
1630
1631
				$pod = self::$current_pod;
1632
			}
1633
1634
            $hidden_fields = array();
1635
?>
1636
    <h3><?php echo $group[ 'label' ]; ?></h3>
1637
1638
	<?php echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_user' ), 'hidden' ); ?>
1639
1640
    <table class="form-table pods-meta">
1641
        <tbody>
1642
            <?php
1643
                foreach ( $group[ 'fields' ] as $field ) {
1644
1645 View Code Duplication
                    if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1646
                        if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1647
                            $field[ 'type' ] = 'hidden';
1648
                        else
1649
                            continue;
1650
                    }
1651
                    elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1652
                        $field[ 'type' ] = 'hidden';
1653
1654
                    $value = '';
1655
1656 View Code Duplication
                    if ( !empty( $pod ) )
1657
                        $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1658
                    elseif ( !empty( $id ) ) {
1659
                        pods_no_conflict_on( 'user' );
1660
1661
                        $value = get_user_meta( $id, $field[ 'name' ], true );
1662
1663
                        pods_no_conflict_off( 'user' );
1664
                    }
1665
1666 View Code Duplication
                    if ( 'hidden' == $field[ 'type' ] ) {
1667
                        $hidden_fields[] = array(
1668
                            'field' => $field,
1669
                            'value' => $value
1670
                        );
1671
                    }
1672
                    else {
1673
            ?>
1674
                <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 ) ); ?>">
1675
                    <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th>
1676
                    <td>
1677
                        <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
1678
                        <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
1679
                    </td>
1680
                </tr>
1681
            <?php
1682
                    }
1683
                }
1684
            ?>
1685
        </tbody>
1686
    </table>
1687
<?php
1688 View Code Duplication
            foreach ( $hidden_fields as $hidden_field ) {
1689
                $field = $hidden_field[ 'field' ];
1690
1691
                echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $hidden_field[ 'value' ], 'hidden' );
1692
            }
1693
        }
1694
1695
        do_action( 'pods_meta_' . __FUNCTION__ . '_post', $user_id );
1696
    }
1697
1698
    /**
1699
     * @param $user_id
1700
     */
1701
    public function save_user ( $user_id ) {
1702
1703
		$is_new_item = false;
1704
1705
		if ( 'user_register' == current_filter() ) {
1706
			$is_new_item = true;
1707
		}
1708
1709 View Code Duplication
		if ( empty( $_POST ) ) {
1710
			return $user_id;
1711
		}
1712
		elseif ( $is_new_item || !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_user' ) ) {
1713
			return $user_id;
1714
		}
1715
1716
		$groups = $this->groups_get( 'user', 'user' );
1717
1718
		if ( empty( $groups ) ) {
1719
			return $user_id;
1720
		}
1721
1722
		if ( is_object( $user_id ) ) {
1723
			$user_id = $user_id->ID;
1724
		}
1725
1726
		$data = array();
1727
1728
		$id = $user_id;
1729
		$pod = null;
1730
1731 View Code Duplication
		foreach ( $groups as $group ) {
1732
			if ( empty( $group[ 'fields' ] ) ) {
1733
				continue;
1734
			}
1735
1736
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1737
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] ) {
1738
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1739
				}
1740
				elseif ( self::$current_pod->id() != $id ) {
1741
					self::$current_pod->fetch( $id );
1742
				}
1743
1744
				$pod = self::$current_pod;
1745
			}
1746
1747
			foreach ( $group[ 'fields' ] as $field ) {
1748
1749
				if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1750
					if ( !pods_var( 'hidden', $field[ 'options' ], false ) ) {
1751
						continue;
1752
					}
1753
				}
1754
1755
				$data[ $field[ 'name' ] ] = '';
1756
1757
				if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) ) {
1758
					$data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
1759
				}
1760
			}
1761
		}
1762
1763
		if ( $is_new_item ) {
1764
			do_action( 'pods_meta_create_pre_user', $data, $pod, $id, $groups );
1765
		}
1766
1767
		do_action( 'pods_meta_save_pre_user', $data, $pod, $id, $groups, $is_new_item );
1768
1769
		pods_no_conflict_on( 'user' );
1770
1771 View Code Duplication
		if ( !empty( $pod ) ) {
1772
			// Fix for Pods doing it's own sanitization
1773
			$data = pods_unslash( (array) $data );
1774
1775
			$pod->save( $data, null, null, array( 'is_new_item' => $is_new_item ) );
1776
		}
1777
		elseif ( !empty( $id ) ) {
1778
			foreach ( $data as $field => $value ) {
1779
				update_user_meta( $id, $field, $value );
1780
			}
1781
		}
1782
1783
		pods_no_conflict_off( 'user' );
1784
1785
		if ( $is_new_item ) {
1786
			do_action( 'pods_meta_create_user', $data, $pod, $id, $groups );
1787
		}
1788
1789
		do_action( 'pods_meta_save_user', $data, $pod, $id, $groups, $is_new_item );
1790
1791
		return $user_id;
1792
1793
    }
1794
1795
    /**
1796
     * @param $commenter
1797
     * @param $user_identity
1798
     */
1799
    public function meta_comment_new_logged_in ( $commenter, $user_identity ) {
1800
        wp_enqueue_style( 'pods-form' );
1801
1802
        do_action( 'pods_meta_' . __FUNCTION__, $commenter, $user_identity );
1803
1804
        $groups = $this->groups_get( 'comment', 'comment' );
1805
1806
        $id = null;
1807
        $pod = null;
1808
1809
		echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' );
1810
1811
        foreach ( $groups as $group ) {
1812
            if ( empty( $group[ 'fields' ] ) )
1813
                continue;
1814
1815
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1816
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1817
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1818
				elseif ( self::$current_pod->id() != $id )
1819
					self::$current_pod->fetch( $id );
1820
1821
				$pod = self::$current_pod;
1822
			}
1823
1824
            foreach ( $group[ 'fields' ] as $field ) {
1825 View Code Duplication
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1826
                    if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1827
                        $field[ 'type' ] = 'hidden';
1828
                    else
1829
                        continue;
1830
                }
1831
                elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1832
                    $field[ 'type' ] = 'hidden';
1833
1834
                $value = '';
1835
1836 View Code Duplication
                if ( !empty( $pod ) )
1837
                    $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1838
                elseif ( !empty( $id ) ) {
1839
                    pods_no_conflict_on( 'comment' );
1840
1841
                    $value = get_comment_meta( $id, $field[ 'name' ], true );
1842
1843
                    pods_no_conflict_off( 'comment' );
1844
                }
1845
                ?>
1846
            <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;' : '' ); ?>">
1847
                <?php
1848
                    echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field );
1849
                    echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
1850
                    echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
1851
                ?>
1852
            </p>
1853
            <?php
1854
            }
1855
        }
1856
1857
        do_action( 'pods_meta_' . __FUNCTION__ . '_post', $commenter, $user_identity );
1858
    }
1859
1860
    /**
1861
     * @param $form_fields
1862
     *
1863
     * @return array
1864
     */
1865
    public function meta_comment_new ( $form_fields ) {
1866
        wp_enqueue_style( 'pods-form' );
1867
1868
        $groups = $this->groups_get( 'comment', 'comment' );
1869
1870
        $id = null;
1871
        $pod = null;
1872
1873
		$form_fields[ 'pods_meta' ] = PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' );
1874
1875
        foreach ( $groups as $group ) {
1876
            if ( empty( $group[ 'fields' ] ) )
1877
                continue;
1878
1879
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1880
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1881
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1882
				elseif ( self::$current_pod->id() != $id )
1883
					self::$current_pod->fetch( $id );
1884
1885
				$pod = self::$current_pod;
1886
			}
1887
1888
            foreach ( $group[ 'fields' ] as $field ) {
1889
1890 View Code Duplication
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1891
                    if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1892
                        $field[ 'type' ] = 'hidden';
1893
                    else
1894
                        continue;
1895
                }
1896
                elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1897
                    $field[ 'type' ] = 'hidden';
1898
1899
                $value = '';
1900
1901 View Code Duplication
                if ( !empty( $pod ) )
1902
                    $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1903
                elseif ( !empty( $id ) ) {
1904
                    pods_no_conflict_on( 'comment' );
1905
1906
                    $value = get_comment_meta( $id, $field[ 'name' ], true );
1907
1908
                    pods_no_conflict_off( 'comment' );
1909
                }
1910
1911
                ob_start();
1912
                ?>
1913
            <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;' : '' ); ?>">
1914
                <?php
1915
                    echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field );
1916
                    echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
1917
                    echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
1918
                ?>
1919
            </p>
1920
            <?php
1921
                $form_fields[ 'pods_meta_' . $field[ 'name' ] ] = ob_get_clean();
1922
            }
1923
        }
1924
1925
        $form_fields = apply_filters( 'pods_meta_' . __FUNCTION__, $form_fields );
1926
1927
        return $form_fields;
1928
    }
1929
1930
    /**
1931
     * @param $comment_type
1932
     * @param null $comment
1933
     */
1934
    public function meta_comment_add ( $comment_type, $comment = null ) {
1935
        if ( is_object( $comment ) && isset( $comment_type->comment_type ) )
1936
            $comment_type = $comment->comment_type;
1937
1938
        if ( is_object( $comment_type ) && isset( $comment_type->comment_type ) ) {
1939
            $comment = $comment_type;
1940
            $comment_type = $comment_type->comment_type;
1941
        }
1942
1943
        if ( is_object( $comment_type ) )
1944
            return;
1945
        elseif ( empty( $comment_type ) )
1946
            $comment_type = 'comment';
1947
1948
        $groups = $this->groups_get( 'comment', $comment_type );
1949
1950
        foreach ( $groups as $group ) {
1951
            if ( empty( $group[ 'fields' ] ) )
1952
                continue;
1953
1954
            $field_found = false;
1955
1956
            foreach ( $group[ 'fields' ] as $field ) {
1957
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], null, null ) ) {
1958
                    if ( pods_var( 'hidden', $field[ 'options' ], false ) ) {
1959
                        $field_found = true;
1960
                        break;
1961
                    }
1962
                    else {
1963
                        continue;
1964
                    }
1965
                }
1966
                else {
1967
                    $field_found = true;
1968
                    break;
1969
                }
1970
            }
1971
1972 View Code Duplication
            if ( $field_found ) {
1973
                add_meta_box(
1974
                    'pods-meta-' . sanitize_title( $group[ 'label' ] ),
1975
                    $group[ 'label' ],
1976
                    array( $this, 'meta_comment' ),
1977
                    $comment_type,
1978
                    $group[ 'context' ],
1979
                    $group[ 'priority' ],
1980
                    array( 'group' => $group )
1981
                );
1982
            }
1983
        }
1984
    }
1985
1986
    /**
1987
     * @param $comment
1988
     * @param $metabox
1989
     */
1990
    public function meta_comment ( $comment, $metabox ) {
1991
        wp_enqueue_style( 'pods-form' );
1992
1993
        do_action( 'pods_meta_' . __FUNCTION__, $comment, $metabox );
1994
1995
        $hidden_fields = array();
1996
1997
		echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' );
1998
?>
1999
    <table class="form-table editcomment pods-metabox">
2000
        <?php
2001
            $id = null;
2002
2003
            if ( is_object( $comment ) )
2004
                $id = $comment->comment_ID;
2005
2006 View Code Duplication
            if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ] )
2007
                self::$current_pod = pods( $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ], $id, true );
2008
			elseif ( self::$current_pod->id() != $id )
2009
				self::$current_pod->fetch( $id );
2010
2011
            $pod = self::$current_pod;
2012
2013
            foreach ( $metabox[ 'args' ][ 'group' ][ 'fields' ] as $field ) {
2014 View Code Duplication
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $metabox[ 'args' ][ 'group' ][ 'fields' ], $pod, $id ) ) {
2015
                    if ( pods_var( 'hidden', $field[ 'options' ], false ) )
2016
                        $field[ 'type' ] = 'hidden';
2017
                    else
2018
                        continue;
2019
                }
2020
                elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
2021
                    $field[ 'type' ] = 'hidden';
2022
2023
                $value = '';
2024
2025 View Code Duplication
                if ( !empty( $pod ) )
2026
                    $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
2027
2028 View Code Duplication
                if ( 'hidden' == $field[ 'type' ] ) {
2029
                    $hidden_fields[] = array(
2030
                        'field' => $field,
2031
                        'value' => $value
2032
                    );
2033
                }
2034
                else {
2035
        ?>
2036
            <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 ) ); ?>">
2037
                <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th>
2038
                <td>
2039
                    <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
2040
                    <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
2041
                </td>
2042
            </tr>
2043
        <?php
2044
                }
2045
            }
2046
        ?>
2047
    </table>
2048
<?php
2049 View Code Duplication
        foreach ( $hidden_fields as $hidden_field ) {
2050
            $field = $hidden_field[ 'field' ];
2051
2052
            echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $hidden_field[ 'value' ], 'hidden' );
2053
        }
2054
2055
        do_action( 'pods_meta_' . __FUNCTION__ . '_post', $comment, $metabox );
2056
    }
2057
2058
    /**
2059
     * @param $approved
2060
     * @param $commentdata
2061
     */
2062
    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...
2063
        $groups = $this->groups_get( 'comment', 'comment' );
2064
2065
        if ( empty( $groups ) )
2066
            return $approved;
2067
2068
        $data = array();
2069
2070
        $pod = null;
2071
        $id = null;
2072
2073
        foreach ( $groups as $group ) {
2074
            if ( empty( $group[ 'fields' ] ) )
2075
                continue;
2076
2077
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
2078
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
2079
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
2080
				elseif ( self::$current_pod->id() != $id )
2081
					self::$current_pod->fetch( $id );
2082
2083
				$pod = self::$current_pod;
2084
			}
2085
2086
            foreach ( $group[ 'fields' ] as $field ) {
2087
2088
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
2089
                    if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
2090
                        continue;
2091
                }
2092
2093
                $data[ $field[ 'name' ] ] = '';
2094
2095
                if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
2096
                    $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
2097
2098
                $validate = pods_api()->handle_field_validation( $data[ $field[ 'name' ] ], $field[ 'name' ], pods_api()->get_wp_object_fields( 'comment' ), $pod->fields(), $pod, array() );
2099
2100
                if ( false === $validate )
2101
                    $validate = sprintf( __( 'There was an issue validating the field %s', 'pods' ), $field[ 'label' ] );
2102
2103
                if ( !is_bool( $validate ) && !empty( $validate ) )
2104
                    return pods_error( $validate, $this );
2105
            }
2106
        }
2107
2108
        return $approved;
2109
    }
2110
2111
    /**
2112
     * @param $comment_id
2113
     */
2114
    public function save_comment ( $comment_id ) {
2115
        $groups = $this->groups_get( 'comment', 'comment' );
2116
2117 View Code Duplication
        if ( empty( $groups ) ) {
2118
            return $comment_id;
2119
		}
2120
		elseif ( empty( $_POST ) ) {
2121
			return $comment_id;
2122
		}
2123
		elseif ( !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_comment' ) ) {
2124
			return $comment_id;
2125
		}
2126
2127
        $data = array();
2128
2129
        $id = $comment_id;
2130
        $pod = null;
2131
2132 View Code Duplication
        foreach ( $groups as $group ) {
2133
            if ( empty( $group[ 'fields' ] ) )
2134
                continue;
2135
2136
			if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
2137
				if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
2138
					self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
2139
				elseif ( self::$current_pod->id() != $id )
2140
					self::$current_pod->fetch( $id );
2141
2142
				$pod = self::$current_pod;
2143
			}
2144
2145
            foreach ( $group[ 'fields' ] as $field ) {
2146
                if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
2147
                    if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
2148
                        continue;
2149
                }
2150
2151
                $data[ $field[ 'name' ] ] = '';
2152
2153
                if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
2154
                    $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
2155
            }
2156
        }
2157
2158
        do_action( 'pods_meta_save_pre_comment', $data, $pod, $id, $groups );
2159
2160 View Code Duplication
        if ( !empty( $pod ) ) {
2161
            // Fix for Pods doing it's own sanitization
2162
            $data = pods_unslash( (array) $data );
2163
2164
            $pod->save( $data );
2165
        }
2166
        elseif ( !empty( $id ) ) {
2167
            pods_no_conflict_on( 'comment' );
2168
2169
            foreach ( $data as $field => $value ) {
2170
                update_comment_meta( $id, $field, $value );
2171
            }
2172
2173
            pods_no_conflict_off( 'comment' );
2174
        }
2175
2176
        do_action( 'pods_meta_save_comment', $data, $pod, $id, $groups );
2177
2178
        return $comment_id;
2179
    }
2180
2181
    /**
2182
     * All *_*_meta filter handler aliases
2183
     *
2184
     * @return mixed
2185
     */
2186 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...
2187
        $args = func_get_args();
2188
2189
        array_unshift( $args, 'post_type' );
2190
2191
        $_null = apply_filters( 'pods_meta_get_post_meta', null, $args );
2192
2193
        if ( null !== $_null )
2194
            return $_null;
2195
2196
        return call_user_func_array( array( $this, 'get_meta' ), $args );
2197
    }
2198
2199
    /**
2200
     * @return mixed
2201
     */
2202 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...
2203
        $args = func_get_args();
2204
2205
        array_unshift( $args, 'user' );
2206
2207
        $_null = apply_filters( 'pods_meta_get_user_meta', null, $args );
2208
2209
        if ( null !== $_null )
2210
            return $_null;
2211
2212
        return call_user_func_array( array( $this, 'get_meta' ), $args );
2213
    }
2214
2215
    /**
2216
     * @return mixed
2217
     */
2218 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...
2219
        $args = func_get_args();
2220
2221
        array_unshift( $args, 'comment' );
2222
2223
        $_null = apply_filters( 'pods_meta_get_comment_meta', null, $args );
2224
2225
        if ( null !== $_null )
2226
            return $_null;
2227
2228
        return call_user_func_array( array( $this, 'get_meta' ), $args );
2229
    }
2230
2231
    /**
2232
     * @return mixed
2233
     */
2234 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...
2235
        $args = func_get_args();
2236
2237
        array_unshift( $args, 'term' );
2238
2239
        $_null = apply_filters( 'pods_meta_get_term_meta', null, $args );
2240
2241
        if ( null !== $_null )
2242
            return $_null;
2243
2244
        return call_user_func_array( array( $this, 'get_meta' ), $args );
2245
    }
2246
2247
    /**
2248
     * All *_*_meta filter handler aliases
2249
     *
2250
     * @return mixed
2251
     */
2252 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...
2253
        $args = func_get_args();
2254
2255
        array_unshift( $args, 'settings' );
2256
2257
        $_null = apply_filters( 'pods_meta_get_option', null, $args );
2258
2259
        if ( null !== $_null )
2260
            return $_null;
2261
2262
        return call_user_func_array( array( $this, 'get_meta' ), $args );
2263
    }
2264
2265
    /**
2266
     * @return mixed
2267
     */
2268 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...
2269
        $args = func_get_args();
2270
2271
        array_unshift( $args, 'post_type' );
2272
2273
        $_null = apply_filters( 'pods_meta_add_post_meta', null, $args );
2274
2275
        if ( null !== $_null )
2276
            return $_null;
2277
2278
        return call_user_func_array( array( $this, 'add_meta' ), $args );
2279
    }
2280
2281
    /**
2282
     * @return mixed
2283
     */
2284 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...
2285
        $args = func_get_args();
2286
2287
        array_unshift( $args, 'user' );
2288
2289
        $_null = apply_filters( 'pods_meta_add_user_meta', null, $args );
2290
2291
        if ( null !== $_null )
2292
            return $_null;
2293
2294
        return call_user_func_array( array( $this, 'add_meta' ), $args );
2295
    }
2296
2297
    /**
2298
     * @return mixed
2299
     */
2300 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...
2301
        $args = func_get_args();
2302
2303
        array_unshift( $args, 'comment' );
2304
2305
        $_null = apply_filters( 'pods_meta_add_comment_meta', null, $args );
2306
2307
        if ( null !== $_null )
2308
            return $_null;
2309
2310
        return call_user_func_array( array( $this, 'add_meta' ), $args );
2311
    }
2312
2313
    /**
2314
     * @return mixed
2315
     */
2316 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...
2317
        $args = func_get_args();
2318
2319
        array_unshift( $args, 'term' );
2320
2321
        $_null = apply_filters( 'pods_meta_add_term_meta', null, $args );
2322
2323
        if ( null !== $_null )
2324
            return $_null;
2325
2326
        return call_user_func_array( array( $this, 'add_meta' ), $args );
2327
    }
2328
2329
    /**
2330
     * @return mixed
2331
     */
2332 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...
2333
        $args = func_get_args();
2334
2335
        array_unshift( $args, 'settings' );
2336
2337
        $_null = apply_filters( 'pods_meta_add_option', null, $args );
2338
2339
        if ( null !== $_null )
2340
            return $_null;
2341
2342
        return call_user_func_array( array( $this, 'add_meta' ), $args );
2343
    }
2344
2345
    /**
2346
     * @return mixed
2347
     */
2348 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...
2349
        $args = func_get_args();
2350
2351
        array_unshift( $args, 'post_type' );
2352
2353
        $_null = apply_filters( 'pods_meta_update_post_meta', null, $args );
2354
2355
        if ( null !== $_null )
2356
            return $_null;
2357
2358
        return call_user_func_array( array( $this, 'update_meta' ), $args );
2359
    }
2360
2361
    /**
2362
     * @return mixed
2363
     */
2364 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...
2365
        $args = func_get_args();
2366
2367
        array_unshift( $args, 'user' );
2368
2369
        $_null = apply_filters( 'pods_meta_update_user_meta', null, $args );
2370
2371
        if ( null !== $_null )
2372
            return $_null;
2373
2374
        return call_user_func_array( array( $this, 'update_meta' ), $args );
2375
    }
2376
2377
    /**
2378
     * @return mixed
2379
     */
2380 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...
2381
        $args = func_get_args();
2382
2383
        array_unshift( $args, 'comment' );
2384
2385
        $_null = apply_filters( 'pods_meta_update_comment_meta', null, $args );
2386
2387
        if ( null !== $_null )
2388
            return $_null;
2389
2390
        return call_user_func_array( array( $this, 'update_meta' ), $args );
2391
    }
2392
2393
    /**
2394
     * @return mixed
2395
     */
2396 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...
2397
        $args = func_get_args();
2398
2399
        array_unshift( $args, 'term' );
2400
2401
        $_null = apply_filters( 'pods_meta_update_term_meta', null, $args );
2402
2403
        if ( null !== $_null )
2404
            return $_null;
2405
2406
        return call_user_func_array( array( $this, 'update_meta' ), $args );
2407
    }
2408
2409
    /**
2410
     * @return mixed
2411
     */
2412 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...
2413
        $args = func_get_args();
2414
2415
        array_unshift( $args, 'settings' );
2416
2417
        $_null = apply_filters( 'pods_meta_update_option', null, $args );
2418
2419
        if ( null !== $_null )
2420
            return $_null;
2421
2422
        return call_user_func_array( array( $this, 'update_meta' ), $args );
2423
    }
2424
2425
    /**
2426
     * @return mixed
2427
     */
2428 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...
2429
        $args = func_get_args();
2430
2431
        array_unshift( $args, 'post_type' );
2432
2433
        $_null = apply_filters( 'pods_meta_delete_post_meta', null, $args );
2434
2435
        if ( null !== $_null )
2436
            return $_null;
2437
2438
        return call_user_func_array( array( $this, 'delete_meta' ), $args );
2439
    }
2440
2441
    /**
2442
     * @return mixed
2443
     */
2444 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...
2445
        $args = func_get_args();
2446
2447
        array_unshift( $args, 'user' );
2448
2449
        $_null = apply_filters( 'pods_meta_delete_user_meta', null, $args );
2450
2451
        if ( null !== $_null )
2452
            return $_null;
2453
2454
        return call_user_func_array( array( $this, 'delete_meta' ), $args );
2455
    }
2456
2457
    /**
2458
     * @return mixed
2459
     */
2460 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...
2461
        $args = func_get_args();
2462
2463
        array_unshift( $args, 'comment' );
2464
2465
        $_null = apply_filters( 'pods_meta_delete_comment_meta', null, $args );
2466
2467
        if ( null !== $_null )
2468
            return $_null;
2469
2470
        return call_user_func_array( array( $this, 'delete_meta' ), $args );
2471
    }
2472
2473
    /**
2474
     * @return mixed
2475
     */
2476 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...
2477
        $args = func_get_args();
2478
2479
        array_unshift( $args, 'term' );
2480
2481
        $_null = apply_filters( 'pods_meta_delete_term_meta', null, $args );
2482
2483
        if ( null !== $_null )
2484
            return $_null;
2485
2486
        return call_user_func_array( array( $this, 'delete_meta' ), $args );
2487
    }
2488
2489
    /**
2490
     * @return mixed
2491
     */
2492 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...
2493
        $args = func_get_args();
2494
2495
        array_unshift( $args, 'settings' );
2496
2497
        $_null = apply_filters( 'pods_meta_delete_option', null, $args );
2498
2499
        if ( null !== $_null )
2500
            return $_null;
2501
2502
        return call_user_func_array( array( $this, 'delete_meta' ), $args );
2503
    }
2504
2505
    /*
2506
     * The real meta functions
2507
     */
2508
    /**
2509
     * @param $object_type
2510
     * @param $object_id
2511
     * @param string $aux
2512
     *
2513
     * @return bool|mixed
2514
     */
2515
    public function get_object ( $object_type, $object_id, $aux = '' ) {
2516
    	
2517
    	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...
2518
    	
2519
    	if ( 'term' == $object_type ) {
2520
    		$object_type = 'taxonomy';
2521
    	}
2522
    	
2523
        if ( 'post_type' == $object_type )
2524
            $objects = self::$post_types;
2525
        elseif ( 'taxonomy' == $object_type )
2526
            $objects = self::$taxonomies;
2527
        elseif ( 'media' == $object_type )
2528
            $objects = self::$media;
2529
        elseif ( 'user' == $object_type )
2530
            $objects = self::$user;
2531
        elseif ( 'comment' == $object_type )
2532
            $objects = self::$comment;
2533
        elseif ( 'settings' == $object_type )
2534
            $objects = self::$settings;
2535
        else
2536
            return false;
2537
2538
        if ( empty( $objects ) || !is_array( $objects ) )
2539
            return false;
2540
2541
        $object_name = null;
2542
2543
        if ( 'media' == $object_type )
2544
            return @current( $objects );
2545
        elseif ( 'user' == $object_type )
2546
            return @current( $objects );
2547
        elseif ( 'comment' == $object_type )
2548
            return @current( $objects );
2549
        elseif ( 'post_type' == $object_type ) {
2550
            $object = get_post( $object_id );
2551
2552
            if ( !is_object( $object ) || !isset( $object->post_type ) )
2553
                return false;
2554
2555
            $object_name = $object->post_type;
2556
        }
2557
        elseif ( 'taxonomy' == $object_type ) {
2558
            if ( pods_version_check( 'wp', '4.4' ) ) {
2559
            	$object = get_term( $object_id );
2560
2561
            	if ( !is_object( $object ) || !isset( $object->taxonomy ) )
2562
                	return false;
2563
            	
2564
            	$object_name = $object->taxonomy;
2565
            } elseif ( empty( $aux ) ) {
2566
            	$object_name = $wpdb->get_var( $wpdb->prepare( "SELECT `taxonomy` FROM `{$wpdb->term_taxonomy}` WHERE `term_id` = %d", $object_id ) );
2567
            } else { 
2568
            	$object_name = $aux;
2569
            }
2570
        }
2571
        elseif ( 'settings' == $object_type )
2572
            $object = $object_id;
2573
        else
2574
            return false;
2575
2576
        $reserved_post_types = array(
2577
			'revision'
2578
        );
2579
2580
        $reserved_post_types = apply_filters( 'pods_meta_reserved_post_types', $reserved_post_types, $object_type, $object_id, $object_name, $objects );
2581
2582
        if ( empty( $object_name ) || ( 'post_type' == $object_type && ( 0 === strpos( $object_name, '_pods_' ) ) || in_array( $object_name, $reserved_post_types ) ) ) {
2583
            return false;
2584
		}
2585
		elseif ( 'attachment' == $object_name ) {
2586
			return @current( self::$media );
2587
		}
2588
2589
        $recheck = array();
2590
2591
        // Return first created by Pods, save extended for later
2592
        foreach ( $objects as $pod ) {
2593
            if ( $object_name == $pod[ 'object' ] )
2594
                $recheck[] = $pod;
2595
2596
            if ( '' == $pod[ 'object' ] && $object_name == $pod[ 'name' ] )
2597
                return $pod;
2598
        }
2599
2600
        // If no objects created by Pods, return first extended
2601
        foreach ( $recheck as $pod ) {
2602
            return $pod;
2603
        }
2604
2605
        return false;
2606
    }
2607
2608
    /**
2609
     * @param $object_type
2610
     * @param null $_null
2611
     * @param int $object_id
2612
     * @param string $meta_key
2613
     * @param bool $single
2614
     *
2615
     * @return array|bool|int|mixed|null|string|void
2616
     */
2617
    public function get_meta ( $object_type, $_null = null, $object_id = 0, $meta_key = '', $single = false ) {
2618
        $meta_type = $object_type;
2619
2620
        if ( in_array( $meta_type, array( 'post_type', 'media' ) ) )
2621
            $meta_type = 'post';
2622
        elseif ( 'taxonomy' == $meta_type )
2623
            $meta_type = 'term';
2624
2625
        if ( empty( $meta_key ) ) {
2626
			if ( !defined( 'PODS_ALLOW_FULL_META' ) || !PODS_ALLOW_FULL_META ) {
2627
				return $_null; // don't cover get_post_meta( $id )
2628
			}
2629
2630
			$single = false;
2631
		}
2632
2633
        $object = $this->get_object( $object_type, $object_id );
2634
2635
        if ( empty( $object_id ) || empty( $object ) )
2636
            return $_null;
2637
2638
        $no_conflict = pods_no_conflict_check( $meta_type );
2639
2640
        if ( !$no_conflict )
2641
            pods_no_conflict_on( $meta_type );
2642
2643
        $meta_cache = array();
2644
2645
        if ( !$single && isset( $GLOBALS[ 'wp_object_cache' ] ) && is_object( $GLOBALS[ 'wp_object_cache' ] ) ) {
2646
            $meta_cache = wp_cache_get( $object_id, 'pods_' . $meta_type . '_meta' );
2647
2648
            if ( empty( $meta_cache ) ) {
2649
                $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
2650
2651
                if ( empty( $meta_cache ) ) {
2652
                    $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
2653
                    $meta_cache = $meta_cache[ $object_id ];
2654
                }
2655
            }
2656
        }
2657
2658
        if ( empty( $meta_cache ) || !is_array( $meta_cache ) )
2659
            $meta_cache = array();
2660
2661 View Code Duplication
        if ( !is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object[ 'name' ] )
2662
            self::$current_field_pod = pods( $object[ 'name' ], $object_id );
2663
		elseif ( self::$current_field_pod->id() != $object_id )
2664
			self::$current_field_pod->fetch( $object_id );
2665
2666
        $pod = self::$current_field_pod;
2667
2668
        $meta_keys = array( $meta_key );
2669
2670
        if ( empty( $meta_key ) )
2671
            $meta_keys = array_keys( $meta_cache );
2672
2673
        $key_found = false;
2674
2675
        foreach ( $meta_keys as $meta_k ) {
2676
            if ( !empty( $pod ) ) {
2677
                if ( isset( $pod->fields[ $meta_k ] ) ) {
2678
                    $key_found = true;
2679
2680
                    $meta_cache[ $meta_k ] = $pod->field( array( 'name' => $meta_k, 'single' => $single, 'get_meta' => true ) );
2681
2682 View Code Duplication
                    if ( ( !is_array( $meta_cache[ $meta_k ] ) || !isset( $meta_cache[ $meta_k ][ 0 ] ) ) ) {
2683
                        if ( empty( $meta_cache[ $meta_k ] ) && !is_array( $meta_cache[ $meta_k ] ) && $single )
2684
                            $meta_cache[ $meta_k ] = array();
2685
                        else
2686
                            $meta_cache[ $meta_k ] = array( $meta_cache[ $meta_k ] );
2687
                    }
2688
2689 View Code Duplication
                    if ( in_array( $pod->fields[ $meta_k ][ 'type' ], PodsForm::tableless_field_types() ) && isset( $meta_cache[ '_pods_' . $meta_k ] ) )
2690
                        unset( $meta_cache[ '_pods_' . $meta_k ] );
2691
                }
2692
                elseif ( false !== strpos( $meta_k, '.' ) ) {
2693
                    $key_found = true;
2694
2695
                    $first = current( explode( '.', $meta_k ) );
2696
2697
                    if ( isset( $pod->fields[ $first ] ) ) {
2698
                        $meta_cache[ $meta_k ] = $pod->field( array( 'name' => $meta_k, 'single' => $single, 'get_meta' => true ) );
2699
2700 View Code Duplication
                        if ( ( !is_array( $meta_cache[ $meta_k ] ) || !isset( $meta_cache[ $meta_k ][ 0 ] ) ) && $single ) {
2701
                            if ( empty( $meta_cache[ $meta_k ] ) && !is_array( $meta_cache[ $meta_k ] ) && $single )
2702
                                $meta_cache[ $meta_k ] = array();
2703
                            else
2704
                                $meta_cache[ $meta_k ] = array( $meta_cache[ $meta_k ] );
2705
                        }
2706
2707 View Code Duplication
                        if ( in_array( $pod->fields[ $first ][ 'type' ], PodsForm::tableless_field_types() ) && isset( $meta_cache[ '_pods_' . $first ] ) )
2708
                            unset( $meta_cache[ '_pods_' . $first ] );
2709
                    }
2710
                }
2711
            }
2712
        }
2713
2714
        if ( !$no_conflict )
2715
            pods_no_conflict_off( $meta_type );
2716
2717
        unset( $pod ); // memory clear
2718
2719
        if ( !$key_found )
2720
            return $_null;
2721
2722
        if ( !$single && isset( $GLOBALS[ 'wp_object_cache' ] ) && is_object( $GLOBALS[ 'wp_object_cache' ] ) )
2723
            wp_cache_set( $object_id, $meta_cache, 'pods_' . $meta_type . '_meta' );
2724
2725
        if ( empty( $meta_key ) )
2726
            return $meta_cache;
2727
        elseif ( isset( $meta_cache[ $meta_key ] ) )
2728
            $value = $meta_cache[ $meta_key ];
2729
        else
2730
            $value = '';
2731
2732
        if ( !is_numeric( $value ) && empty( $value ) ) {
2733
            if ( $single )
2734
                $value = '';
2735
            else
2736
                $value = array();
2737
        }
2738
        // get_metadata requires $meta[ 0 ] to be set for first value to be retrieved
2739
        elseif ( !is_array( $value ) )
2740
            $value = array( $value );
2741
2742
        return $value;
2743
    }
2744
2745
    /**
2746
     * @param $object_type
2747
     * @param null $_null
2748
     * @param int $object_id
2749
     * @param string $meta_key
2750
     * @param string $meta_value
2751
     * @param bool $unique
2752
     *
2753
     * @return bool|int|null
2754
     */
2755
    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...
2756
        if ( pods_tableless() )
2757
            return $_null;
2758
2759
        $object = $this->get_object( $object_type, $object_id );
2760
2761 View Code Duplication
        if ( empty( $object_id ) || empty( $object ) || !isset( $object[ 'fields' ][ $meta_key ] ) )
2762
            return $_null;
2763
2764
        if ( in_array( $object[ 'fields' ][ $meta_key ][ 'type' ], PodsForm::tableless_field_types() ) ) {
2765 View Code Duplication
            if ( !is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object[ 'name' ] )
2766
                self::$current_field_pod = pods( $object[ 'name' ], $object_id );
2767
			elseif ( self::$current_field_pod->id() != $object_id )
2768
				self::$current_field_pod->fetch( $object_id );
2769
2770
            $pod = self::$current_field_pod;
2771
2772
            $pod->add_to( $meta_key, $meta_value );
2773
        }
2774
        else {
2775 View Code Duplication
            if ( !is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object[ 'name' ] )
2776
                self::$current_field_pod = pods( $object[ 'name' ] );
2777
2778
            $pod = self::$current_field_pod;
2779
2780
            $pod->save( $meta_key, $meta_value, $object_id );
2781
        }
2782
2783
        return $object_id;
2784
    }
2785
2786
    /**
2787
     * @param $object_type
2788
     * @param null $_null
2789
     * @param int $object_id
2790
     * @param string $meta_key
2791
     * @param string $meta_value
2792
     * @param string $prev_value
2793
     *
2794
     * @return bool|int|null
2795
     */
2796
    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...
2797
        if ( pods_tableless() )
2798
            return $_null;
2799
2800
        $object = $this->get_object( $object_type, $object_id );
2801
2802 View Code Duplication
        if ( empty( $object_id ) || empty( $object ) || !isset( $object[ 'fields' ][ $meta_key ] ) )
2803
            return $_null;
2804
2805 View Code Duplication
        if ( !is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object[ 'name' ] )
2806
            self::$current_field_pod = pods( $object[ 'name' ] );
2807
2808
        $pod = self::$current_field_pod;
2809
2810
        if ( ( isset( $pod->fields[ $meta_key ] ) || false !== strpos( $meta_key, '.' ) ) && $pod->row !== null) {
2811
2812
            $key = $meta_key;
2813
            if(false !== strpos( $meta_key, '.' )){
2814
                $key = current( explode( '.', $meta_key ) );
2815
            }
2816
2817
            $pod->row[ $meta_key ] = $meta_value;
2818
2819 View Code Duplication
            if ( isset( $pod->fields[ $key ] ) ) {
2820
                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...
2821
                    unset( $meta_cache[ '_pods_' . $key ] );
2822
            }
2823
2824
        }
2825
2826
        $pod->save( $meta_key, $meta_value, $object_id );
2827
2828
        return $object_id;
2829
    }
2830
2831
    /**
2832
     * @param $object_type
2833
     * @param null $_null
2834
     * @param int $object_id
2835
     * @param string $meta_key
2836
     * @param string $meta_value
2837
     * @param bool $delete_all
2838
     *
2839
     * @return null
2840
     */
2841
    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...
2842
        if ( pods_tableless() )
2843
            return $_null;
2844
2845
        $object = $this->get_object( $object_type, $object_id );
2846
2847 View Code Duplication
        if ( empty( $object_id ) || empty( $object ) || !isset( $object[ 'fields' ][ $meta_key ] ) )
2848
            return $_null;
2849
2850
        // @todo handle $delete_all (delete the field values from all pod items)
2851
        if ( !empty( $meta_value ) && in_array( $object[ 'fields' ][ $meta_key ][ 'type' ], PodsForm::tableless_field_types() ) ) {
2852 View Code Duplication
            if ( !is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object[ 'name' ] )
2853
                self::$current_field_pod = pods( $object[ 'name' ], $object_id );
2854
			elseif ( self::$current_field_pod->id() != $object_id )
2855
				self::$current_field_pod->fetch( $object_id );
2856
2857
            $pod = self::$current_field_pod;
2858
2859
            $pod->remove_from( $meta_key, $meta_value );
2860
        }
2861
        else {
2862 View Code Duplication
            if ( !is_object( self::$current_field_pod ) || self::$current_field_pod->pod != $object[ 'name' ] )
2863
                self::$current_field_pod = pods( $object[ 'name' ] );
2864
2865
            $pod = self::$current_field_pod;
2866
2867
            $pod->save( array( $meta_key => null ), null, $object_id );
2868
        }
2869
2870
        return $_null;
2871
    }
2872
2873
    public function delete_post ( $id ) {
2874
        $post = get_post( $id );
2875
2876
        if ( empty( $post ) )
2877
            return;
2878
2879
        $id = $post->ID;
2880
        $post_type = $post->post_type;
2881
2882
        return $this->delete_object( 'post_type', $id, $post_type );
2883
    }
2884
2885
    public function delete_taxonomy ( $id ) {
2886
        /**
2887
         * @var $wpdb WPDB
2888
         */
2889
        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...
2890
2891
        $terms = $wpdb->get_results( "SELECT `term_id`, `taxonomy` FROM `{$wpdb->term_taxonomy}` WHERE `term_taxonomy_id` = {$id}" );
2892
2893
        if ( empty( $terms ) )
2894
            return;
2895
2896
        foreach ( $terms as $term ) {
2897
            $id = $term->term_id;
2898
            $taxonomy = $term->taxonomy;
2899
2900
            $this->delete_object( 'taxonomy', $id, $taxonomy );
2901
        }
2902
    }
2903
2904
    /**
2905
     * Hook the split_shared_term action and point it to this method
2906
     *
2907
     * Fires after a previously shared taxonomy term is split into two separate terms.
2908
     *
2909
     * @param int    $term_id          ID of the formerly shared term.
2910
     * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
2911
     * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
2912
     * @param string $taxonomy         Taxonomy for the split term.
2913
     */
2914
    public static function split_shared_term( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
2915
2916
        require_once( PODS_DIR . 'classes/PodsTermSplitting.php' );
2917
2918
        $term_splitting = new Pods_Term_Splitting( $term_id, $new_term_id, $taxonomy );
2919
        $term_splitting->split_shared_term();
2920
2921
    }
2922
2923
    public function delete_user ( $id ) {
2924
        return $this->delete_object( 'user', $id );
2925
    }
2926
2927
    public function delete_comment ( $id ) {
2928
        return $this->delete_object( 'comment', $id );
2929
    }
2930
2931
    public function delete_media ( $id ) {
2932
        return $this->delete_object( 'media', $id );
2933
    }
2934
2935
    public function delete_object ( $type, $id, $name = null ) {
2936
        if ( empty( $name ) )
2937
            $name = $type;
2938
2939
        $object = $this->object_get( $type, $name );
2940
2941
        if ( !empty( $object ) ) {
2942
            $params = array(
2943
                'pod' => pods_var( 'name', $object ),
2944
                'pod_id' => pods_var( 'id', $object ),
2945
                'id' => $id
2946
            );
2947
2948
            return pods_api()->delete_pod_item( $params, false );
2949
        }
2950
        else
2951
            return pods_api()->delete_object_from_relationships( $id, $type, $name );
2952
    }
2953
}
2954