Completed
Push — 2.x ( fe5e20...0d4bcf )
by Scott Kingsley
08:35
created

PodsInit::object_label_fix()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 64
Code Lines 54

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 64
rs 7.2058
cc 7
eloc 54
nc 12
nop 2

How to fix   Long Method   

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 PodsInit {
6
7
    /**
8
     * @var PodsInit
9
     */
10
    static $instance = null;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $instance.

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

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
11
12
    /**
13
     * @var array
14
     */
15
    static $no_conflict = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $no_conflict.

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

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
16
17
    /**
18
     * @var array
19
     */
20
    static $content_types_registered = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $content_types_registered.

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...
21
22
    /**
23
     * @var PodsComponents
24
     */
25
    static $components;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $components.

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...
26
27
    /**
28
     * @var PodsMeta
29
     */
30
    static $meta;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $meta.

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...
31
32
    /**
33
     * @var PodsAdmin
34
     */
35
    static $admin;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $admin.

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...
36
37
    /**
38
     * @var mixed|void
39
     */
40
    static $version;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $version.

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...
41
42
    /**
43
     * @var mixed|void
44
     */
45
    static $version_last;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $version_last.

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...
46
47
    /**
48
     * @var mixed|void
49
     */
50
    static $db_version;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $db_version.

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...
51
52
    /**
53
     * Upgrades to trigger (last installed version => upgrade version)
54
     *
55
     * @var array
56
     */
57
    static $upgrades = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $upgrades.

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...
58
        '1.0.0' => '2.0.0'
59
        //'2.0.0' => '2.1.0'
60
    );
61
62
    /**
63
     * Whether an Upgrade for 1.x has happened
64
     *
65
     * @var bool
66
     */
67
    static $upgraded;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $upgraded.

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...
68
69
    /**
70
     * Whether an Upgrade is needed
71
     *
72
     * @var bool
73
     */
74
    static $upgrade_needed = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $upgrade_needed.

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...
75
76
    /**
77
     * Singleton handling for a basic pods_init() request
78
     *
79
     * @return \PodsInit
80
     *
81
     * @since 2.3.5
82
     */
83
    public static function init () {
84
        if ( !is_object( self::$instance ) )
85
            self::$instance = new PodsInit();
86
87
        return self::$instance;
88
    }
89
90
    /**
91
     * Setup and Initiate Pods
92
     *
93
     * @return \PodsInit
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...
94
     *
95
     * @license http://www.gnu.org/licenses/gpl-2.0.html
96
     * @since 1.8.9
97
     */
98
    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...
99
        self::$version = get_option( 'pods_framework_version' );
100
        self::$version_last = get_option( 'pods_framework_version_last' );
101
        self::$db_version = get_option( 'pods_framework_db_version' );
102
        self::$upgraded = (int) get_option( 'pods_framework_upgraded_1_x' );
103
104
        if ( empty( self::$version_last ) && 0 < strlen( get_option( 'pods_version' ) ) ) {
105
            $old_version = get_option( 'pods_version' );
106
107
            if ( !empty( $old_version ) ) {
108
                if ( false === strpos( $old_version, '.' ) )
109
                    $old_version = pods_version_to_point( $old_version );
110
111
                update_option( 'pods_framework_version_last', $old_version );
112
113
                self::$version_last = $old_version;
114
            }
115
        }
116
117
        self::$upgrade_needed = $this->needs_upgrade();
118
119
        add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
120
121
        add_action( 'init', array( $this, 'activate_install' ), 9 );
122
        add_action( 'wp_loaded', array( $this, 'flush_rewrite_rules' ) );
123
124
        if ( !empty( self::$version ) ) {
125
	        $this->run();
126
        }
127
    }
128
129
    /**
130
     * Load the plugin textdomain and set default constants
131
     */
132
    public function plugins_loaded () {
133
        if ( !defined( 'PODS_LIGHT' ) )
134
            define( 'PODS_LIGHT', false );
135
136
        if ( !defined( 'PODS_TABLELESS' ) )
137
            define( 'PODS_TABLELESS', false );
138
139
        load_plugin_textdomain( 'pods', false, dirname( plugin_basename( PODS_DIR . 'init.php' ) ) . '/languages/' );
140
    }
141
142
    /**
143
     * Load Pods Components
144
     */
145
    public function load_components () {
146
        if ( !defined( 'PODS_LIGHT' ) || !PODS_LIGHT )
147
            self::$components = pods_components();
148
    }
149
150
    /**
151
     * Load Pods Meta
152
     */
153
    public function load_meta () {
154
        self::$meta = pods_meta()->core();
155
    }
156
157
    /**
158
     * Set up the Pods core
159
     */
160
    public function core () {
161
        // Session start
162
		pods_session_start();
163
164
        add_shortcode( 'pods', 'pods_shortcode' );
165
        add_shortcode( 'pods-form', 'pods_shortcode_form' );
166
167
        $security_settings = array(
168
            'pods_disable_file_browser' => 0,
169
            'pods_files_require_login' => 1,
170
            'pods_files_require_login_cap' => '',
171
            'pods_disable_file_upload' => 0,
172
            'pods_upload_require_login' => 1,
173
            'pods_upload_require_login_cap' => ''
174
        );
175
176
        foreach ( $security_settings as $security_setting => $setting ) {
177
            $setting = get_option( $security_setting );
178
            if ( !empty( $setting ) )
179
                $security_settings[ $security_setting ] = $setting;
180
        }
181
182
        foreach ( $security_settings as $security_setting => $setting ) {
183
            if ( 0 == $setting )
184
                $setting = false;
185
            elseif ( 1 == $setting )
186
                $setting = true;
187
188
            if ( in_array( $security_setting, array( 'pods_files_require_login', 'pods_upload_require_login' ) ) ) {
189
                if ( 0 < strlen( $security_settings[ $security_setting . '_cap' ] ) )
190
                    $setting = $security_settings[ $security_setting . '_cap' ];
191
            }
192
            elseif ( in_array( $security_setting, array( 'pods_files_require_login_cap', 'pods_upload_require_login_cap' ) ) )
193
                continue;
194
195
            if ( !defined( strtoupper( $security_setting ) ) )
196
                define( strtoupper( $security_setting ), $setting );
197
        }
198
199
        $this->register_pods();
200
201
        $avatar = PodsForm::field_loader( 'avatar' );
202
203
        if ( method_exists( $avatar, 'get_avatar' ) )
204
            add_filter( 'get_avatar', array( $avatar, 'get_avatar' ), 10, 4 );
205
    }
206
207
    /**
208
     * Register Scripts and Styles
209
     */
210
    public function register_assets () {
211
        if ( !wp_style_is( 'jquery-ui', 'registered' ) )
212
            wp_register_style( 'jquery-ui', PODS_URL . 'ui/css/smoothness/jquery-ui.custom.css', array(), '1.8.16' );
213
214
        wp_register_script( 'pods-json', PODS_URL . 'ui/js/jquery.json.js', array( 'jquery' ), '2.3' );
215
216
	    if ( ! wp_style_is( 'jquery-qtip2', 'registered' ) ) {
217
		    wp_register_style( 'jquery-qtip2', PODS_URL . 'ui/css/jquery.qtip.min.css', array(), '2.2' );
218
	    }
219
220
	    if ( ! wp_script_is( 'jquery-qtip2', 'registered' ) ) {
221
		    wp_register_script( 'jquery-qtip2', PODS_URL . 'ui/js/jquery.qtip.min.js', array( 'jquery' ), '2.2' );
222
	    }
223
224
        wp_register_script( 'pods', PODS_URL . 'ui/js/jquery.pods.js', array( 'jquery', 'pods-json', 'jquery-qtip2' ), PODS_VERSION );
225
226
        wp_register_style( 'pods-form', PODS_URL . 'ui/css/pods-form.css', array(), PODS_VERSION );
227
228
        wp_register_style( 'pods-cleditor', PODS_URL . 'ui/css/jquery.cleditor.css', array(), '1.3.0' );
229
        wp_register_script( 'pods-cleditor', PODS_URL . 'ui/js/jquery.cleditor.min.js', array( 'jquery' ), '1.3.0' );
230
231
        wp_register_style( 'pods-codemirror', PODS_URL . 'ui/css/codemirror.css', array(), '4.8' );
232
        wp_register_script( 'pods-codemirror', PODS_URL . 'ui/js/codemirror.js', array(), '4.8', true );
233
        wp_register_script( 'pods-codemirror-loadmode', PODS_URL . 'ui/js/codemirror/addon/mode/loadmode.js', array( 'pods-codemirror' ), '4.8', true );
234
        wp_register_script( 'pods-codemirror-overlay', PODS_URL . 'ui/js/codemirror/addon/mode/overlay.js', array( 'pods-codemirror' ), '4.8', true );
235
        wp_register_script( 'pods-codemirror-hints', PODS_URL . 'ui/js/codemirror/addon/mode/show-hint.js', array( 'pods-codemirror' ), '4.8', true );
236
        wp_register_script( 'pods-codemirror-mode-xml', PODS_URL . 'ui/js/codemirror/mode/xml/xml.js', array( 'pods-codemirror' ), '4.8', true );
237
        wp_register_script( 'pods-codemirror-mode-html', PODS_URL . 'ui/js/codemirror/mode/htmlmixed/htmlmixed.js', array( 'pods-codemirror' ), '4.8', true );
238
        wp_register_script( 'pods-codemirror-mode-css', PODS_URL . 'ui/js/codemirror/mode/css/css.js', array( 'pods-codemirror' ), '4.8', true );
239
240
        if ( !wp_style_is( 'jquery-ui-timepicker', 'registered' ) )
241
            wp_register_style( 'jquery-ui-timepicker', PODS_URL . 'ui/css/jquery.ui.timepicker.css', array(), '1.1.1' );
242
243
        if ( !wp_script_is( 'jquery-ui-timepicker', 'registered' ) ) {
244
            wp_register_script( 'jquery-ui-timepicker', PODS_URL . 'ui/js/jquery.ui.timepicker.min.js', array(
245
                'jquery',
246
                'jquery-ui-core',
247
                'jquery-ui-datepicker',
248
                'jquery-ui-slider'
249
            ), '1.1.1' );
250
        }
251
252
        wp_register_style( 'pods-attach', PODS_URL . 'ui/css/jquery.pods.attach.css', array(), PODS_VERSION );
253
        wp_register_script( 'pods-attach', PODS_URL . 'ui/js/jquery.pods.attach.js', array(), PODS_VERSION );
254
255
        wp_register_style( 'pods-select2', PODS_URL . 'ui/js/select2/select2.css', array(), '3.3.1' );
256
        wp_register_script( 'pods-select2', PODS_URL . 'ui/js/select2/select2.min.js', array( 'jquery' ), '3.3.1' );
257
258
        wp_register_script( 'pods-handlebars', PODS_URL . 'ui/js/handlebars.js', array(), '1.0.0.beta.6' );
259
    }
260
261
    /**
262
     * Register internal Post Types
263
     */
264
    public function register_pods () {
265
        $args = array(
266
            'label' => 'Pods',
267
            'labels' => array( 'singular_name' => 'Pod' ),
268
            'public' => false,
269
            'can_export' => false,
270
            'query_var' => false,
271
            'rewrite' => false,
272
            'capability_type' => 'pods_pod',
273
            'has_archive' => false,
274
            'hierarchical' => false,
275
            'supports' => array( 'title', 'author' ),
276
            'menu_icon' => 'dashicons-pods'
277
        );
278
279
        $args = self::object_label_fix( $args, 'post_type' );
280
281
        register_post_type( '_pods_pod', apply_filters( 'pods_internal_register_post_type_pod', $args ) );
282
283
        $args = array(
284
            'label' => 'Pod Fields',
285
            'labels' => array( 'singular_name' => 'Pod Field' ),
286
            'public' => false,
287
            'can_export' => false,
288
            'query_var' => false,
289
            'rewrite' => false,
290
            'capability_type' => 'pods_pod',
291
            'has_archive' => false,
292
            'hierarchical' => true,
293
            'supports' => array( 'title', 'editor', 'author' ),
294
            'menu_icon' => 'dashicons-pods'
295
        );
296
297
        $args = self::object_label_fix( $args, 'post_type' );
298
299
        register_post_type( '_pods_field', apply_filters( 'pods_internal_register_post_type_field', $args ) );
300
    }
301
302
    /**
303
     * Include Admin
304
     */
305
    public function admin_init () {
306
        self::$admin = pods_admin();
307
    }
308
309
    /**
310
     * Register Post Types and Taxonomies
311
     */
312
    public function setup_content_types ( $force = false ) {
313
        $post_types = PodsMeta::$post_types;
314
        $taxonomies = PodsMeta::$taxonomies;
315
316
        $existing_post_types = get_post_types();
317
        $existing_taxonomies = get_taxonomies();
318
319
        $pods_cpt_ct = pods_transient_get( 'pods_wp_cpt_ct' );
320
321
        $cpt_positions = array();
322
323
        if ( empty( $pods_cpt_ct ) && ( !empty( $post_types ) || !empty( $taxonomies ) ) )
324
            $force = true;
325
        elseif ( !empty( $pods_cpt_ct ) && empty( $pods_cpt_ct[ 'post_types' ] ) && !empty( $post_types ) )
326
            $force = true;
327
        elseif ( !empty( $pods_cpt_ct ) && empty( $pods_cpt_ct[ 'taxonomies' ] ) && !empty( $taxonomies ) )
328
            $force = true;
329
330
        if ( false === $pods_cpt_ct || $force ) {
331
			/**
332
			 * @var WP_Query
333
			 */
334
			global $wp_query;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

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

1. Pass all data via parameters

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

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

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

    public function myFunction() {
        // Do something
    }
}
Loading history...
335
336
			$reserved_query_vars = array(
337
				'post_type',
338
				'taxonomy',
339
				'output'
340
			);
341
342
			if ( is_object( $wp_query ) ) {
343
				$reserved_query_vars = array_merge( $reserved_query_vars, array_keys( $wp_query->fill_query_vars( array() ) ) );
344
			}
345
346
            $pods_cpt_ct = array(
347
                'post_types' => array(),
348
                'taxonomies' => array()
349
            );
350
351
            $pods_post_types = $pods_taxonomies = array();
352
            $supported_post_types = $supported_taxonomies = array();
353
354
            foreach ( $post_types as $post_type ) {
355
                // Post Type exists already
356 View Code Duplication
                if ( isset( $pods_cpt_ct[ 'post_types' ][ $post_type[ 'name' ] ] ) )
357
                    continue;
358
                elseif ( !empty( $post_type[ 'object' ] ) && isset( $existing_post_types[ $post_type[ 'object' ] ] ) )
359
                    continue;
360
                elseif ( !$force && isset( $existing_post_types[ $post_type[ 'name' ] ] ) )
361
                    continue;
362
363
                $post_type[ 'options' ][ 'name' ] = $post_type[ 'name' ];
364
                $post_type = array_merge( $post_type, (array) $post_type[ 'options' ] );
365
366
				$post_type_name = pods_var( 'name', $post_type );
367
368
                // Labels
369
                $cpt_label = esc_html( pods_var_raw( 'label', $post_type, ucwords( str_replace( '_', ' ', pods_var_raw( 'name', $post_type ) ) ), null, true ) );
370
                $cpt_singular = esc_html( pods_var_raw( 'label_singular', $post_type, ucwords( str_replace( '_', ' ', pods_var_raw( 'label', $post_type, $post_type_name, null, true ) ) ), null, true ) );
371
372
                $cpt_labels = array();
373
                $cpt_labels[ 'name' ] = $cpt_label;
374
                $cpt_labels[ 'singular_name' ] = $cpt_singular;
375
                $cpt_labels[ 'menu_name' ] = pods_var_raw( 'menu_name', $post_type, '', null, true );
376
                $cpt_labels[ 'add_new' ] = pods_var_raw( 'label_add_new', $post_type, '', null, true );
377
                $cpt_labels[ 'add_new_item' ] = pods_var_raw( 'label_add_new_item', $post_type, '', null, true );
378
                $cpt_labels[ 'new_item' ] = pods_var_raw( 'label_new_item', $post_type, '', null, true );
379
                $cpt_labels[ 'edit' ] = pods_var_raw( 'label_edit', $post_type, '', null, true );
380
                $cpt_labels[ 'edit_item' ] = pods_var_raw( 'label_edit_item', $post_type, '', null, true );
381
                $cpt_labels[ 'view' ] = pods_var_raw( 'label_view', $post_type, '', null, true );
382
                $cpt_labels[ 'view_item' ] = pods_var_raw( 'label_view_item', $post_type, '', null, true );
383
                $cpt_labels[ 'all_items' ] = pods_var_raw( 'label_all_items', $post_type, '', null, true );
384
                $cpt_labels[ 'search_items' ] = pods_var_raw( 'label_search_items', $post_type, '', null, true );
385
                $cpt_labels[ 'not_found' ] = pods_var_raw( 'label_not_found', $post_type, '', null, true );
386
                $cpt_labels[ 'not_found_in_trash' ] = pods_var_raw( 'label_not_found_in_trash', $post_type, '', null, true );
387
                $cpt_labels[ 'parent' ] = pods_var_raw( 'label_parent', $post_type, '', null, true );
388
                $cpt_labels[ 'parent_item_colon' ] = pods_var_raw( 'label_parent_item_colon', $post_type, '', null, true );
389
390
                // Supported
391
                $cpt_supported = array(
392
                    'title' => (boolean) pods_var( 'supports_title', $post_type, false ),
393
                    'editor' => (boolean) pods_var( 'supports_editor', $post_type, false ),
394
                    'author' => (boolean) pods_var( 'supports_author', $post_type, false ),
395
                    'thumbnail' => (boolean) pods_var( 'supports_thumbnail', $post_type, false ),
396
                    'excerpt' => (boolean) pods_var( 'supports_excerpt', $post_type, false ),
397
                    'trackbacks' => (boolean) pods_var( 'supports_trackbacks', $post_type, false ),
398
                    'custom-fields' => (boolean) pods_var( 'supports_custom_fields', $post_type, false ),
399
                    'comments' => (boolean) pods_var( 'supports_comments', $post_type, false ),
400
                    'revisions' => (boolean) pods_var( 'supports_revisions', $post_type, false ),
401
                    'page-attributes' => (boolean) pods_var( 'supports_page_attributes', $post_type, false ),
402
                    'post-formats' => (boolean) pods_var( 'supports_post_formats', $post_type, false )
403
                );
404
405
                // Custom Supported
406
                $cpt_supported_custom = pods_var( 'supports_custom', $post_type, '' );
407
408
                if ( !empty( $cpt_supported_custom ) ) {
409
                    $cpt_supported_custom = explode( ',', $cpt_supported_custom );
410
                    $cpt_supported_custom = array_filter( array_unique( $cpt_supported_custom ) );
411
412
                    foreach ( $cpt_supported_custom as $cpt_support ) {
413
                        $cpt_supported[ $cpt_support ] = true;
414
                    }
415
                }
416
417
                // Genesis Support
418
                if ( function_exists( 'genesis' ) ) {
419
                    $cpt_supported[ 'genesis-seo' ] = (boolean) pods_var( 'supports_genesis_seo', $post_type, false );
420
                    $cpt_supported[ 'genesis-layouts' ] = (boolean) pods_var( 'supports_genesis_layouts', $post_type, false );
421
                    $cpt_supported[ 'genesis-simple-sidebars' ] = (boolean) pods_var( 'supports_genesis_simple_sidebars', $post_type, false );
422
                }
423
424
				// YARPP Support
425
				if ( defined( 'YARPP_VERSION' ) ) {
426
                    $cpt_supported[ 'yarpp_support' ] = (boolean) pods_var( 'supports_yarpp_support', $post_type, false );
427
				}
428
429
				// Jetpack Support
430
				if ( class_exists( 'Jetpack' ) ) {
431
                    $cpt_supported[ 'supports_jetpack_publicize' ] = (boolean) pods_var( 'supports_jetpack_publicize', $post_type, false );
432
                    $cpt_supported[ 'supports_jetpack_markdown' ] = (boolean) pods_var( 'supports_jetpack_markdown', $post_type, false );
433
				}
434
435
                // WP needs something, if this was empty and none were enabled, it would show title+editor pre 3.5 :(
436
                $cpt_supports = array();
437
438
                if ( !pods_version_check( 'wp', '3.5' ) )
439
                    $cpt_supports = array( '_bug_fix_pre_35' );
440
441
                foreach ( $cpt_supported as $cpt_support => $supported ) {
442
                    if ( true === $supported )
443
                        $cpt_supports[] = $cpt_support;
444
                }
445
446
                if ( empty( $cpt_supports ) && pods_version_check( 'wp', '3.5' ) )
447
                    $cpt_supports = false;
448
449
                // Rewrite
450
                $cpt_rewrite = (boolean) pods_var( 'rewrite', $post_type, true );
451
                $cpt_rewrite_array = array(
452
                    'slug' => pods_var( 'rewrite_custom_slug', $post_type, str_replace( '_', '-', $post_type_name ), null, true ),
453
                    'with_front' => (boolean) pods_var( 'rewrite_with_front', $post_type, true ),
454
                    'feeds' => (boolean) pods_var( 'rewrite_feeds', $post_type, (boolean) pods_var( 'has_archive', $post_type, false ) ),
455
                    'pages' => (boolean) pods_var( 'rewrite_pages', $post_type, true )
456
                );
457
458
                if ( false !== $cpt_rewrite )
459
                    $cpt_rewrite = $cpt_rewrite_array;
460
461
                $capability_type = pods_var( 'capability_type', $post_type, 'post' );
462
463
                if ( 'custom' == $capability_type )
464
                    $capability_type = pods_var( 'capability_type_custom', $post_type, 'post' );
465
466
                $show_in_menu = (boolean) pods_var( 'show_in_menu', $post_type, true );
467
468
                if ( $show_in_menu && 0 < strlen( pods_var_raw( 'menu_location_custom', $post_type ) ) )
469
                    $show_in_menu = pods_var_raw( 'menu_location_custom', $post_type );
470
471
				$menu_icon = pods_var( 'menu_icon', $post_type, null, null, true );
472
473
				if ( !empty( $menu_icon ) ) {
474
					$menu_icon = pods_evaluate_tags( $menu_icon );
475
				}
476
477
                // Register Post Type
478
                $pods_post_types[ $post_type_name ] = array(
479
                    'label' => $cpt_label,
480
                    'labels' => $cpt_labels,
481
                    'description' => esc_html( pods_var_raw( 'description', $post_type ) ),
482
                    'public' => (boolean) pods_var( 'public', $post_type, true ),
483
                    'publicly_queryable' => (boolean) pods_var( 'publicly_queryable', $post_type, (boolean) pods_var( 'public', $post_type, true ) ),
484
                    'exclude_from_search' => (boolean) pods_var( 'exclude_from_search', $post_type, ( (boolean) pods_var( 'public', $post_type, true ) ? false : true ) ),
485
                    'show_ui' => (boolean) pods_var( 'show_ui', $post_type, (boolean) pods_var( 'public', $post_type, true ) ),
486
                    'show_in_menu' => $show_in_menu,
487
                    'show_in_nav_menus' => (boolean) pods_var( 'show_in_nav_menus', $post_type, (boolean) pods_var( 'public', $post_type, true ) ),
488
                    'show_in_admin_bar' => (boolean) pods_var( 'show_in_admin_bar', $post_type, (boolean) pods_var( 'show_in_menu', $post_type, true ) ),
489
                    'menu_position' => (int) pods_var( 'menu_position', $post_type, 0, null, true ),
490
                    'menu_icon' => $menu_icon,
491
                    'capability_type' => $capability_type,
492
                    //'capabilities' => $cpt_capabilities,
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
493
                    'map_meta_cap' => (boolean) pods_var( 'capability_type_extra', $post_type, true ),
494
                    'hierarchical' => (boolean) pods_var( 'hierarchical', $post_type, false ),
495
                    'supports' => $cpt_supports,
496
                    //'register_meta_box_cb' => array($this, 'manage_meta_box'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
497
                    //'permalink_epmask' => EP_PERMALINK,
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
498
                    'has_archive' => pods_v( 'has_archive_slug', $post_type, (boolean) pods_v( 'has_archive', $post_type, false ), true ),
499
                    'rewrite' => $cpt_rewrite,
500
                    'query_var' => ( false !== (boolean) pods_var( 'query_var', $post_type, true ) ? pods_var( 'query_var_string', $post_type, $post_type_name, null, true ) : false ),
501
                    'can_export' => (boolean) pods_var( 'can_export', $post_type, true )
502
                );
503
504
                // YARPP doesn't use 'supports' array option (yet)
505
                if ( ! empty( $cpt_supports[ 'yarpp_support' ] ) ) {
506
                    $pods_post_types[ $post_type_name ][ 'yarpp_support' ] = true;
507
                }
508
509
				// Prevent reserved query_var issues
510
				if ( in_array( $pods_post_types[ $post_type_name ][ 'query_var' ], $reserved_query_vars ) ) {
511
					$pods_post_types[ $post_type_name ][ 'query_var' ] = 'post_type_' . $pods_post_types[ $post_type_name ][ 'query_var' ];
512
				}
513
514
                if ( 25 == $pods_post_types[ $post_type_name ][ 'menu_position' ] )
515
                    $pods_post_types[ $post_type_name ][ 'menu_position' ]++;
516
517
                if ( $pods_post_types[ $post_type_name ][ 'menu_position' ] < 1 || in_array( $pods_post_types[ $post_type_name ][ 'menu_position' ], $cpt_positions ) )
518
                    unset( $pods_post_types[ $post_type_name ][ 'menu_position' ] );
519
                else {
520
                    $cpt_positions[] = $pods_post_types[ $post_type_name ][ 'menu_position' ];
521
522
                    // This would be nice if WP supported floats in menu_position
523
                    // $pods_post_types[ $post_type_name ][ 'menu_position' ] = $pods_post_types[ $post_type_name ][ 'menu_position' ] . '.1';
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
524
                }
525
526
                // Taxonomies
527
                $cpt_taxonomies = array();
528
                $_taxonomies = get_taxonomies();
529
                $_taxonomies = array_merge_recursive( $_taxonomies, $pods_taxonomies );
530
                $ignore = array( 'nav_menu', 'link_category', 'post_format' );
531
532 View Code Duplication
                foreach ( $_taxonomies as $taxonomy => $label ) {
533
                    if ( in_array( $taxonomy, $ignore ) )
534
                        continue;
535
536
                    if ( false !== (boolean) pods_var( 'built_in_taxonomies_' . $taxonomy, $post_type, false ) ) {
537
                        $cpt_taxonomies[] = $taxonomy;
538
539
                        if ( isset( $supported_post_types[ $taxonomy ] ) && !in_array( $post_type_name, $supported_post_types[ $taxonomy ] ) )
540
                            $supported_post_types[ $taxonomy ][] = $post_type_name;
541
                    }
542
                }
543
544
                if ( isset( $supported_taxonomies[ $post_type_name ] ) )
545
                    $supported_taxonomies[ $post_type_name ] = array_merge( (array) $supported_taxonomies[ $post_type_name ], $cpt_taxonomies );
546
                else
547
                    $supported_taxonomies[ $post_type_name ] = $cpt_taxonomies;
548
            }
549
550
            foreach ( $taxonomies as $taxonomy ) {
551
                // Taxonomy Type exists already
552 View Code Duplication
                if ( isset( $pods_cpt_ct[ 'taxonomies' ][ $taxonomy[ 'name' ] ] ) )
553
                    continue;
554
                elseif ( !empty( $taxonomy[ 'object' ] ) && isset( $existing_taxonomies[ $taxonomy[ 'object' ] ] ) )
555
                    continue;
556
                elseif ( !$force && isset( $existing_taxonomies[ $taxonomy[ 'name' ] ] ) )
557
                    continue;
558
559
                $taxonomy[ 'options' ][ 'name' ] = $taxonomy[ 'name' ];
560
                $taxonomy = array_merge( $taxonomy, (array) $taxonomy[ 'options' ] );
561
562
				$taxonomy_name = pods_var( 'name', $taxonomy );
563
564
                // Labels
565
                $ct_label = esc_html( pods_var_raw( 'label', $taxonomy, ucwords( str_replace( '_', ' ', pods_var_raw( 'name', $taxonomy ) ) ), null, true ) );
566
                $ct_singular = esc_html( pods_var_raw( 'label_singular', $taxonomy, ucwords( str_replace( '_', ' ', pods_var_raw( 'label', $taxonomy, pods_var_raw( 'name', $taxonomy ), null, true ) ) ), null, true ) );
567
568
                $ct_labels = array();
569
                $ct_labels[ 'name' ] = $ct_label;
570
                $ct_labels[ 'singular_name' ] = $ct_singular;
571
                $ct_labels[ 'menu_name' ] = pods_var_raw( 'menu_name', $taxonomy, '', null, true );
572
                $ct_labels[ 'search_items' ] = pods_var_raw( 'label_search_items', $taxonomy, '', null, true );
573
                $ct_labels[ 'popular_items' ] = pods_var_raw( 'label_popular_items', $taxonomy, '', null, true );
574
                $ct_labels[ 'all_items' ] = pods_var_raw( 'label_all_items', $taxonomy, '', null, true );
575
                $ct_labels[ 'parent_item' ] = pods_var_raw( 'label_parent_item', $taxonomy, '', null, true );
576
                $ct_labels[ 'parent_item_colon' ] = pods_var_raw( 'label_parent_item_colon', $taxonomy, '', null, true );
577
                $ct_labels[ 'edit_item' ] = pods_var_raw( 'label_edit_item', $taxonomy, '', null, true );
578
                $ct_labels[ 'update_item' ] = pods_var_raw( 'label_update_item', $taxonomy, '', null, true );
579
                $ct_labels[ 'add_new_item' ] = pods_var_raw( 'label_add_new_item', $taxonomy, '', null, true );
580
                $ct_labels[ 'new_item_name' ] = pods_var_raw( 'label_new_item_name', $taxonomy, '', null, true );
581
                $ct_labels[ 'separate_items_with_commas' ] = pods_var_raw( 'label_separate_items_with_commas', $taxonomy, '', null, true );
582
                $ct_labels[ 'add_or_remove_items' ] = pods_var_raw( 'label_add_or_remove_items', $taxonomy, '', null, true );
583
                $ct_labels[ 'choose_from_most_used' ] = pods_var_raw( 'label_choose_from_the_most_used', $taxonomy, '', null, true );
584
585
                // Rewrite
586
                $ct_rewrite = (boolean) pods_var( 'rewrite', $taxonomy, true );
587
                $ct_rewrite_array = array(
588
                    'slug' => pods_var( 'rewrite_custom_slug', $taxonomy, str_replace( '_', '-', $taxonomy_name ), null, true ),
589
                    'with_front' => (boolean) pods_var( 'rewrite_with_front', $taxonomy, true ),
590
                    'hierarchical' => (boolean) pods_var( 'rewrite_hierarchical', $taxonomy, (boolean) pods_var( 'hierarchical', $taxonomy, false ) )
591
                );
592
593
                if ( false !== $ct_rewrite )
594
                    $ct_rewrite = $ct_rewrite_array;
595
596
                // Register Taxonomy
597
                $pods_taxonomies[ $taxonomy_name ] = array(
598
                    'label' => $ct_label,
599
                    'labels' => $ct_labels,
600
                    'public' => (boolean) pods_var( 'public', $taxonomy, true ),
601
                    'show_in_nav_menus' => (boolean) pods_var( 'show_in_nav_menus', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ),
602
                    'show_ui' => (boolean) pods_var( 'show_ui', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ),
603
                    'show_tagcloud' => (boolean) pods_var( 'show_tagcloud', $taxonomy, (boolean) pods_var( 'show_ui', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ) ),
604
                    'hierarchical' => (boolean) pods_var( 'hierarchical', $taxonomy, false ),
605
                    'update_count_callback' => pods_var( 'update_count_callback', $taxonomy, null, null, true ),
606
                    'query_var' => ( false !== (boolean) pods_var( 'query_var', $taxonomy, true ) ? pods_var( 'query_var_string', $taxonomy, $taxonomy_name, null, true ) : false ),
607
                    'rewrite' => $ct_rewrite,
608
                    'show_admin_column' => (boolean) pods_var( 'show_admin_column', $taxonomy, false ),
609
                    'sort' => (boolean) pods_var( 'sort', $taxonomy, false )
610
                );
611
612
                if ( is_array( $ct_rewrite ) && !$pods_taxonomies[ $taxonomy_name ][ 'query_var' ] )
613
                    $pods_taxonomies[ $taxonomy_name ]['query_var'] = pods_var( 'query_var_string', $taxonomy, $taxonomy_name, null, true );;
614
615
				// Prevent reserved query_var issues
616
				if ( in_array( $pods_taxonomies[ $taxonomy_name ][ 'query_var' ], $reserved_query_vars ) ) {
617
					$pods_taxonomies[ $taxonomy_name ][ 'query_var' ] = 'taxonomy_' . $pods_taxonomies[ $taxonomy_name ][ 'query_var' ];
618
				}
619
620
				// Integration for Single Value Taxonomy UI
621
				if ( function_exists( 'tax_single_value_meta_box' ) ) {
622
					$pods_taxonomies[ $taxonomy_name ][ 'single_value' ] = (boolean) pods_var( 'single_value', $taxonomy, false );
623
					$pods_taxonomies[ $taxonomy_name ][ 'required' ] = (boolean) pods_var( 'single_value_required', $taxonomy, false );
624
				}
625
626
                // Post Types
627
                $ct_post_types = array();
628
                $_post_types = get_post_types();
629
                $_post_types = array_merge_recursive( $_post_types, $pods_post_types );
630
                $ignore = array( 'revision' );
631
632 View Code Duplication
                foreach ( $_post_types as $post_type => $options ) {
633
                    if ( in_array( $post_type, $ignore ) )
634
                        continue;
635
636
                    if ( false !== (boolean) pods_var( 'built_in_post_types_' . $post_type, $taxonomy, false ) ) {
637
                        $ct_post_types[] = $post_type;
638
639
                        if ( isset( $supported_taxonomies[ $post_type ] ) && !in_array( $taxonomy_name, $supported_taxonomies[ $post_type ] ) )
640
                            $supported_taxonomies[ $post_type ][] = $taxonomy_name;
641
                    }
642
                }
643
644
                if ( isset( $supported_post_types[ $taxonomy_name ] ) )
645
                    $supported_post_types[ $taxonomy_name ] = array_merge( $supported_post_types[ $taxonomy_name ], $ct_post_types );
646
                else
647
                    $supported_post_types[ $taxonomy_name ] = $ct_post_types;
648
            }
649
650
            $pods_post_types = apply_filters( 'pods_wp_post_types', $pods_post_types );
651
            $pods_taxonomies = apply_filters( 'pods_wp_taxonomies', $pods_taxonomies );
652
653
            $supported_post_types = apply_filters( 'pods_wp_supported_post_types', $supported_post_types );
654
            $supported_taxonomies = apply_filters( 'pods_wp_supported_taxonomies', $supported_taxonomies );
655
656
            foreach ( $pods_taxonomies as $taxonomy => $options ) {
657
                $ct_post_types = null;
658
659
                if ( isset( $supported_post_types[ $taxonomy ] ) && !empty( $supported_post_types[ $taxonomy ] ) )
660
                    $ct_post_types = $supported_post_types[ $taxonomy ];
661
662
                $pods_cpt_ct[ 'taxonomies' ][ $taxonomy ] = array(
663
                    'post_types' => $ct_post_types,
664
                    'options' => $options
665
                );
666
            }
667
668
            foreach ( $pods_post_types as $post_type => $options ) {
669
                if ( isset( $supported_taxonomies[ $post_type ] ) && !empty( $supported_taxonomies[ $post_type ] ) )
670
                    $options[ 'taxonomies' ] = $supported_taxonomies[ $post_type ];
671
672
                $pods_cpt_ct[ 'post_types' ][ $post_type ] = $options;
673
            }
674
675
            pods_transient_set( 'pods_wp_cpt_ct', $pods_cpt_ct );
676
        }
677
678
        foreach ( $pods_cpt_ct[ 'taxonomies' ] as $taxonomy => $options ) {
679
            if ( isset( self::$content_types_registered[ 'taxonomies' ] ) && in_array( $taxonomy, self::$content_types_registered[ 'taxonomies' ] ) )
680
                continue;
681
682
            $ct_post_types = $options[ 'post_types' ];
683
            $options = $options[ 'options' ];
684
685
            $options = apply_filters( 'pods_register_taxonomy_' . $taxonomy, $options, $taxonomy );
686
            $options = apply_filters( 'pods_register_taxonomy', $options, $taxonomy );
687
688
            $options = self::object_label_fix( $options, 'taxonomy' );
689
690
            // Max length for taxonomies are 32 characters
691
            $taxonomy = substr( $taxonomy, 0, 32 );
692
693
            // i18n compatibility for plugins that override it
694 View Code Duplication
            if ( is_array( $options[ 'rewrite' ] ) && isset( $options[ 'rewrite' ][ 'slug' ] ) && !empty( $options[ 'rewrite' ][ 'slug' ] ) )
695
                $options[ 'rewrite' ][ 'slug' ] = _x( $options[ 'rewrite' ][ 'slug' ], 'URL taxonomy slug', 'pods' );
696
697 View Code Duplication
            if ( 1 == pods_var( 'pods_debug_register', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) )
698
                pods_debug( array( $taxonomy, $ct_post_types, $options ) );
699
700
            register_taxonomy( $taxonomy, $ct_post_types, $options );
701
702
            if ( !isset( self::$content_types_registered[ 'taxonomies' ] ) )
703
                self::$content_types_registered[ 'taxonomies' ] = array();
704
705
            self::$content_types_registered[ 'taxonomies' ][] = $taxonomy;
706
        }
707
708
        foreach ( $pods_cpt_ct[ 'post_types' ] as $post_type => $options ) {
709
            if ( isset( self::$content_types_registered[ 'post_types' ] ) && in_array( $post_type, self::$content_types_registered[ 'post_types' ] ) )
710
                continue;
711
712
            $options = apply_filters( 'pods_register_post_type_' . $post_type, $options, $post_type );
713
            $options = apply_filters( 'pods_register_post_type', $options, $post_type );
714
715
            $options = self::object_label_fix( $options, 'post_type' );
716
717
            // Max length for post types are 20 characters
718
            $post_type = substr( $post_type, 0, 20 );
719
720
            // i18n compatibility for plugins that override it
721 View Code Duplication
            if ( is_array( $options[ 'rewrite' ] ) && isset( $options[ 'rewrite' ][ 'slug' ] ) && !empty( $options[ 'rewrite' ][ 'slug' ] ) )
722
                $options[ 'rewrite' ][ 'slug' ] = _x( $options[ 'rewrite' ][ 'slug' ], 'URL slug', 'pods' );
723
724 View Code Duplication
            if ( 1 == pods_var( 'pods_debug_register', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) )
725
                pods_debug( array( $post_type, $options ) );
726
727
            register_post_type( $post_type, $options );
728
729
            if ( !isset( self::$content_types_registered[ 'post_types' ] ) )
730
                self::$content_types_registered[ 'post_types' ] = array();
731
732
            self::$content_types_registered[ 'post_types' ][] = $post_type;
733
        }
734
735
    }
736
737
	/**
738
     * Check if we need to flush WordPress rewrite rules
739
     * This gets run during 'init' action late in the game to give other plugins time to register their rewrite rules
740
     *
741
     */
742
    public function flush_rewrite_rules() {
743
        $flush = pods_transient_get( 'pods_flush_rewrites' );
744
745
        if ( 1 == $flush ) {
746
            global $wp_rewrite;
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...
747
            $wp_rewrite->flush_rules();
748
            $wp_rewrite->init();
749
750
            pods_transient_set( 'pods_flush_rewrites', 0 );
751
        }
752
    }
753
754
    /**
755
     * Update Post Type messages
756
     *
757
     * @param array $messages
758
     *
759
     * @return array
760
     * @since 2.0.2
761
     */
762
    public function setup_updated_messages ( $messages ) {
763
        global $post, $post_ID;
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...
764
765
        $post_types = PodsMeta::$post_types;
766
        $existing_post_types = get_post_types();
767
768
        $pods_cpt_ct = pods_transient_get( 'pods_wp_cpt_ct' );
769
770
        if ( empty( $pods_cpt_ct ) || empty( $post_types ) )
771
            return $messages;
772
773
        foreach ( $post_types as $post_type ) {
774
            if ( !isset( $pods_cpt_ct[ 'post_types' ][ $post_type[ 'name' ] ] ) )
775
                continue;
776
777
            $labels = self::object_label_fix( $pods_cpt_ct[ 'post_types' ][ $post_type[ 'name' ] ], 'post_type' );
778
            $labels = $labels[ 'labels' ];
779
780
            $messages[ $post_type[ 'name' ] ] = array(
781
                1 => sprintf( __( '%s updated. <a href="%s">%s</a>', 'pods' ), $labels[ 'singular_name' ], esc_url( get_permalink( $post_ID ) ), $labels[ 'view_item' ] ),
782
                2 => __( 'Custom field updated.', 'pods' ),
783
                3 => __( 'Custom field deleted.', 'pods' ),
784
                4 => sprintf( __( '%s updated.', 'pods' ), $labels[ 'singular_name' ] ),
785
                /* translators: %s: date and time of the revision */
786
                5 => isset( $_GET[ 'revision' ] ) ? sprintf( __( '%s restored to revision from %s', 'pods' ), $labels[ 'singular_name' ], wp_post_revision_title( (int) $_GET[ 'revision' ], false ) ) : false,
787
                6 => sprintf( __( '%s published. <a href="%s">%s</a>', 'pods' ), $labels[ 'singular_name' ], esc_url( get_permalink( $post_ID ) ), $labels[ 'view_item' ] ),
788
                7 => sprintf( __( '%s saved.', 'pods' ), $labels[ 'singular_name' ] ),
789
                8 => sprintf( __( '%s submitted. <a target="_blank" href="%s">Preview %s</a>', 'pods' ), $labels[ 'singular_name' ], esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), $labels[ 'singular_name' ] ),
790
                9 => sprintf( __( '%s scheduled for: <strong>%s</strong>. <a target="_blank" href="%s">Preview %s</a>', 'pods' ),
791
                    $labels[ 'singular_name' ],
792
                    // translators: Publish box date format, see http://php.net/date
793
                    date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ),
794
                    esc_url( get_permalink( $post_ID ) ),
795
                    $labels[ 'singular_name' ]
796
                ),
797
                10 => sprintf( __( '%s draft updated. <a target="_blank" href="%s">Preview %s</a>', 'pods' ), $labels[ 'singular_name' ], esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), $labels[ 'singular_name' ] )
798
            );
799
800
            if ( false === (boolean) $pods_cpt_ct[ 'post_types' ][ $post_type[ 'name' ] ][ 'public' ] ) {
801
                $messages[ $post_type[ 'name' ] ][ 1 ] = sprintf( __( '%s updated.', 'pods' ), $labels[ 'singular_name' ] );
802
                $messages[ $post_type[ 'name' ] ][ 6 ] = sprintf( __( '%s published.', 'pods' ), $labels[ 'singular_name' ] );
803
                $messages[ $post_type[ 'name' ] ][ 8 ] = sprintf( __( '%s submitted.', 'pods' ), $labels[ 'singular_name' ] );
804
                $messages[ $post_type[ 'name' ] ][ 9 ] = sprintf( __( '%s scheduled for: <strong>%1$s</strong>.', 'pods' ),
805
                    $labels[ 'singular_name' ],
806
                    // translators: Publish box date format, see http://php.net/date
807
                    date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) )
808
                );
809
                $messages[ $post_type[ 'name' ] ][ 10 ] = sprintf( __( '%s draft updated.', 'pods' ), $labels[ 'singular_name' ] );
810
            }
811
        }
812
813
        return $messages;
814
    }
815
816
    /**
817
     * @param $args
818
     * @param string $type
819
     *
820
     * @return array
821
     */
822
    public static function object_label_fix ( $args, $type = 'post_type' ) {
823
        if ( empty( $args ) || !is_array( $args ) )
824
            $args = array();
825
826
        if ( !isset( $args[ 'labels' ] ) || !is_array( $args[ 'labels' ] ) )
827
            $args[ 'labels' ] = array();
828
829
        $label = pods_var_raw( 'name', $args[ 'labels' ], pods_var_raw( 'label', $args, __( 'Items', 'pods' ), null, true ), null, true );
830
        $singular_label = pods_var_raw( 'singular_name', $args[ 'labels' ], pods_var_raw( 'label_singular', $args, __( 'Item', 'pods' ), null, true ), null, true );
831
832
        $labels = $args[ 'labels' ];
833
834
        $labels[ 'name' ] = $label;
835
        $labels[ 'singular_name' ] = $singular_label;
836
837
        if ( 'post_type' == $type ) {
838
            $labels[ 'menu_name' ] = pods_var_raw( 'menu_name', $labels, $label, null, true );
839
            $labels[ 'add_new' ] = pods_var_raw( 'add_new', $labels, __( 'Add New', 'pods' ), null, true );
840
            $labels[ 'add_new_item' ] = pods_var_raw( 'add_new_item', $labels, sprintf( __( 'Add New %s', 'pods' ), $singular_label ), null, true );
841
            $labels[ 'new_item' ] = pods_var_raw( 'new_item', $labels, sprintf( __( 'New %s', 'pods' ), $singular_label ), null, true );
842
            $labels[ 'edit' ] = pods_var_raw( 'edit', $labels, __( 'Edit', 'pods' ), null, true );
843
            $labels[ 'edit_item' ] = pods_var_raw( 'edit_item', $labels, sprintf( __( 'Edit %s', 'pods' ), $singular_label ), null, true );
844
            $labels[ 'view' ] = pods_var_raw( 'view', $labels, sprintf( __( 'View %s', 'pods' ), $singular_label ), null, true );
845
            $labels[ 'view_item' ] = pods_var_raw( 'view_item', $labels, sprintf( __( 'View %s', 'pods' ), $singular_label ), null, true );
846
            $labels[ 'all_items' ] = pods_var_raw( 'all_items', $labels, sprintf( __( 'All %s', 'pods' ), $label ), null, true );
847
            $labels[ 'search_items' ] = pods_var_raw( 'search_items', $labels, sprintf( __( 'Search %s', 'pods' ), $label ), null, true );
848
            $labels[ 'not_found' ] = pods_var_raw( 'not_found', $labels, sprintf( __( 'No %s Found', 'pods' ), $label ), null, true );
849
            $labels[ 'not_found_in_trash' ] = pods_var_raw( 'not_found_in_trash', $labels, sprintf( __( 'No %s Found in Trash', 'pods' ), $label ), null, true );
850
            $labels[ 'parent' ] = pods_var_raw( 'parent', $labels, sprintf( __( 'Parent %s', 'pods' ), $singular_label ), null, true );
851
            $labels[ 'parent_item_colon' ] = pods_var_raw( 'parent_item_colon', $labels, sprintf( __( 'Parent %s:', 'pods' ), $singular_label ), null, true );
852
            $labels[ 'feature_image' ] = pods_var_raw( 'feature_image', $labels, __( 'Featured Image', 'pods' ), null, true );
853
            $labels[ 'set_featured_image' ] = pods_var_raw( 'set_featured_image', $labels, __( 'Set featured image', 'pods' ), null, true );
854
            $labels[ 'remove_featured_image' ] = pods_var_raw( 'remove_featured_image', $labels, __( 'Remove featured image', 'pods' ), null, true );
855
            $labels[ 'use_featured_image' ] = pods_var_raw( 'use_featured_image', $labels, __( 'Use as featured image', 'pods' ), null, true );
856
            $labels[ 'archives' ] = pods_var_raw( 'archives', $labels, sprintf( __( '%s Archives', 'pods' ), $singular_label ), null, true );
857
            $labels[ 'insert_into_item' ] = pods_var_raw( 'insert_into_item', $labels, sprintf( __( 'Insert into %s', 'pods' ), $singular_label ), null, true );
858
            $labels[ 'uploaded_to_this_item' ] = pods_var_raw( 'uploaded_to_this_item', $labels, sprintf( __( 'Uploaded to this %s', 'pods' ), $singular_label ), null, true );
859
            $labels[ 'filter_items_list' ] = pods_var_raw( 'filter_items_list', $labels, sprintf( __( 'Filter %s lists', 'pods' ), $label ), null, true );
860
            $labels[ 'items_list_navigation' ] = pods_var_raw( 'items_list_navigation', $labels, sprintf( __( '%s navigation', 'pods' ), $label ), null, true );
861
            $labels[ 'items_list' ] = pods_var_raw( 'items_list', $labels, sprintf( __( '%s list', 'pods' ), $label ), null, true );
862
        }
863
        elseif ( 'taxonomy' == $type ) {
864
            $labels[ 'menu_name' ] = pods_var_raw( 'menu_name', $labels, $label, null, true );
865
            $labels[ 'search_items' ] = pods_var_raw( 'search_items', $labels, sprintf( __( 'Search %s', 'pods' ), $label ), null, true );
866
            $labels[ 'popular_items' ] = pods_var_raw( 'popular_items', $labels, sprintf( __( 'Popular %s', 'pods' ), $label ), null, true );
867
            $labels[ 'all_items' ] = pods_var_raw( 'all_items', $labels, sprintf( __( 'All %s', 'pods' ), $label ), null, true );
868
            $labels[ 'parent_item' ] = pods_var_raw( 'parent_item', $labels, sprintf( __( 'Parent %s', 'pods' ), $singular_label ), null, true );
869
            $labels[ 'parent_item_colon' ] = pods_var_raw( 'parent_item_colon', $labels, sprintf( __( 'Parent %s :', 'pods' ), $singular_label ), null, true );
870
            $labels[ 'edit_item' ] = pods_var_raw( 'edit_item', $labels, sprintf( __( 'Edit %s', 'pods' ), $singular_label ), null, true );
871
            $labels[ 'update_item' ] = pods_var_raw( 'update_item', $labels, sprintf( __( 'Update %s', 'pods' ), $singular_label ), null, true );
872
            $labels[ 'add_new_item' ] = pods_var_raw( 'add_new_item', $labels, sprintf( __( 'Add New %s', 'pods' ), $singular_label ), null, true );
873
            $labels[ 'new_item_name' ] = pods_var_raw( 'new_item_name', $labels, sprintf( __( 'New %s Name', 'pods' ), $singular_label ), null, true );
874
            $labels[ 'separate_items_with_commas' ] = pods_var_raw( 'separate_items_with_commas', $labels, sprintf( __( 'Separate %s with commas', 'pods' ), $label ), null, true );
875
            $labels[ 'add_or_remove_items' ] = pods_var_raw( 'add_or_remove_items', $labels, sprintf( __( 'Add or remove %s', 'pods' ), $label ), null, true );
876
            $labels[ 'choose_from_most_used' ] = pods_var_raw( 'choose_from_most_used', $labels, sprintf( __( 'Choose from the most used %s', 'pods' ), $label ), null, true );
877
            $labels[ 'no_terms' ] = pods_var_raw( 'no_terms', $labels, sprintf( __( 'No %s', 'pods' ), $label ), null, true );
878
            $labels[ 'items_list_navigation' ] = pods_var_raw( 'items_list_navigation', $labels, sprintf( __( '%s navigation', 'pods' ), $label ), null, true );
879
            $labels[ 'items_list' ] = pods_var_raw( 'items_list', $labels, sprintf( __( '%s list', 'pods' ), $label ), null, true );
880
        }
881
882
        $args[ 'labels' ] = $labels;
883
884
        return $args;
885
    }
886
887
    /**
888
     * Activate and Install
889
     */
890
    public function activate_install () {
891
        register_activation_hook( PODS_DIR . 'init.php', array( $this, 'activate' ) );
892
        register_deactivation_hook( PODS_DIR . 'init.php', array( $this, 'deactivate' ) );
893
894
        add_action( 'wpmu_new_blog', array( $this, 'new_blog' ), 10, 6 );
895
896
        if ( empty( self::$version ) || version_compare( self::$version, PODS_VERSION, '<' ) || version_compare( self::$version, PODS_DB_VERSION, '<=' ) || self::$upgrade_needed )
897
            $this->setup();
898
        elseif ( self::$version != PODS_VERSION ) {
899
            delete_option( 'pods_framework_version' );
900
            add_option( 'pods_framework_version', PODS_VERSION, '', 'yes' );
901
902
            pods_api()->cache_flush_pods();
903
        }
904
    }
905
906
    /**
907
     *
908
     */
909
    public function activate () {
910
        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...
911
912
        if ( function_exists( 'is_multisite' ) && is_multisite() && isset( $_GET[ 'networkwide' ] ) && 1 == $_GET[ 'networkwide' ] ) {
913
            $_blog_ids = $wpdb->get_col( "SELECT `blog_id` FROM `{$wpdb->blogs}`" );
914
915
            foreach ( $_blog_ids as $_blog_id ) {
916
                $this->setup( $_blog_id );
917
            }
918
        }
919
        else
920
            $this->setup();
921
    }
922
923
    /**
924
     *
925
     */
926
    public function deactivate () {
927
        pods_api()->cache_flush_pods();
928
    }
929
930
    /**
931
     *
932
     */
933
    public function needs_upgrade ( $current = null, $last = null ) {
934
        if ( null === $current )
935
            $current = self::$version;
936
937
        if ( null === $last )
938
            $last = self::$version_last;
939
940
        $upgrade_needed = false;
941
942
        if ( !empty( $current ) ) {
943
            foreach ( self::$upgrades as $old_version => $new_version ) {
944
                /*if ( '2.1.0' == $new_version && ( is_developer() ) )
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
945
                    continue;*/
946
947
                if ( version_compare( $last, $old_version, '>=' )
948
                     && version_compare( $last, $new_version, '<' )
949
                     && version_compare( $current, $new_version, '>=' )
950
                     && 1 != self::$upgraded
951
                ) {
952
                    $upgrade_needed = true;
953
954
                    break;
955
                }
956
            }
957
        }
958
959
        return $upgrade_needed;
960
    }
961
962
    /**
963
     * @param $_blog_id
964
     * @param $user_id
965
     * @param $domain
966
     * @param $path
967
     * @param $site_id
968
     * @param $meta
969
     */
970
    public function new_blog ( $_blog_id, $user_id, $domain, $path, $site_id, $meta ) {
0 ignored issues
show
Unused Code introduced by
The parameter $user_id is not used and could be removed.

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

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

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

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

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

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

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

Loading history...
Unused Code introduced by
The parameter $meta 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...
971
        if ( function_exists( 'is_multisite' ) && is_multisite() && is_plugin_active_for_network( basename( PODS_DIR ) . '/init.php' ) )
972
            $this->setup( $_blog_id );
973
    }
974
975
    /**
976
     * @param null $_blog_id
977
     */
978
    public function setup ( $_blog_id = null ) {
979
        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...
980
981
        // Switch DB table prefixes
982 View Code Duplication
        if ( null !== $_blog_id && $_blog_id != $wpdb->blogid )
983
            switch_to_blog( pods_absint( $_blog_id ) );
984
        else
985
            $_blog_id = null;
986
987
        // Setup DB tables
988
        $pods_version = get_option( 'pods_framework_version' );
989
        $pods_version_last = get_option( 'pods_framework_version_last' );
990
991
        // Install Pods
992
        if ( empty( $pods_version ) ) {
993
            pods_upgrade()->install( $_blog_id );
994
995
            $old_version = get_option( 'pods_version' );
996
997
            if ( !empty( $old_version ) ) {
998
                if ( false === strpos( $old_version, '.' ) )
999
                    $old_version = pods_version_to_point( $old_version );
1000
1001
                delete_option( 'pods_framework_version_last' );
1002
                add_option( 'pods_framework_version_last', $pods_version, '', 'yes' );
1003
1004
                self::$version_last = $old_version;
1005
            }
1006
        }
1007
        // Upgrade Wizard needed
1008
        elseif ( $this->needs_upgrade( $pods_version, $pods_version_last ) ) {
1009
            // Do not do anything
1010
            return;
1011
        }
1012
        // Update Pods and run any required DB updates
1013
        elseif ( version_compare( $pods_version, PODS_VERSION, '<=' ) ) {
1014
            if ( false !== apply_filters( 'pods_update_run', null, PODS_VERSION, $pods_version, $_blog_id ) && !isset( $_GET[ 'pods_bypass_update' ] ) ) {
1015
                do_action( 'pods_update', PODS_VERSION, $pods_version, $_blog_id );
1016
1017
                // Update 2.0 alpha / beta sites
1018
                if ( version_compare( '2.0.0-a-1', $pods_version, '<=' ) && version_compare( $pods_version, '2.0.0-b-15', '<=' ) )
1019
                    include( PODS_DIR . 'sql/update-2.0-beta.php' );
1020
1021
                if ( version_compare( $pods_version, PODS_DB_VERSION, '<=' ) )
1022
                    include( PODS_DIR . 'sql/update.php' );
1023
1024
                do_action( 'pods_update_post', PODS_VERSION, $pods_version, $_blog_id );
1025
            }
1026
1027
            delete_option( 'pods_framework_version_last' );
1028
            add_option( 'pods_framework_version_last', $pods_version, '', 'yes' );
1029
1030
            self::$version_last = $pods_version;
1031
        }
1032
1033
        delete_option( 'pods_framework_version' );
1034
        add_option( 'pods_framework_version', PODS_VERSION, '', 'yes' );
1035
1036
        delete_option( 'pods_framework_db_version' );
1037
        add_option( 'pods_framework_db_version', PODS_DB_VERSION, '', 'yes' );
1038
1039
        pods_api()->cache_flush_pods();
1040
1041
        // Restore DB table prefix (if switched)
1042
        if ( null !== $_blog_id )
1043
            restore_current_blog();
1044
	    else {
1045
		    $this->run();
1046
	    }
1047
    }
1048
1049
    /**
1050
     * @param null $_blog_id
1051
     */
1052
    public function reset ( $_blog_id = null ) {
1053
        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...
1054
1055
        // Switch DB table prefixes
1056 View Code Duplication
        if ( null !== $_blog_id && $_blog_id != $wpdb->blogid )
1057
            switch_to_blog( pods_absint( $_blog_id ) );
1058
        else
1059
            $_blog_id = null;
1060
1061
        $api = pods_api();
1062
1063
        $pods = $api->load_pods( array( 'names_ids' => true ) );
1064
1065
        foreach ( $pods as $pod_id => $pod_label ) {
0 ignored issues
show
Bug introduced by
The expression $pods of type array|object|integer|double|string|null|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...
1066
            $api->delete_pod( array( 'id' => $pod_id ) );
1067
        }
1068
1069
        $templates = $api->load_templates();
1070
1071
        foreach ( $templates as $template ) {
1072
            $api->delete_template( array( 'id' => $template[ 'id' ] ) );
1073
        }
1074
1075
        $pages = $api->load_pages();
1076
1077
        foreach ( $pages as $page ) {
1078
            $api->delete_page( array( 'id' => $page[ 'id' ] ) );
1079
        }
1080
1081
        $helpers = $api->load_helpers();
1082
1083
        foreach ( $helpers as $helper ) {
1084
            $api->delete_helper( array( 'id' => $helper[ 'id' ] ) );
1085
        }
1086
1087
        $tables = $wpdb->get_results( "SHOW TABLES LIKE '{$wpdb->prefix}pods%'", ARRAY_N );
1088
1089
        if ( !empty( $tables ) ) {
1090
            foreach ( $tables as $table ) {
1091
                $table = $table[ 0 ];
1092
1093
                pods_query( "DROP TABLE `{$table}`", false );
1094
            }
1095
        }
1096
1097
        // Remove any orphans
1098
        $wpdb->query( "
1099
                DELETE `p`, `pm`
1100
                FROM `{$wpdb->posts}` AS `p`
1101
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1102
                    ON `pm`.`post_id` = `p`.`ID`
1103
                WHERE
1104
                    `p`.`post_type` LIKE '_pods_%'
1105
            " );
1106
1107
        delete_option( 'pods_framework_version' );
1108
        delete_option( 'pods_framework_db_version' );
1109
        delete_option( 'pods_framework_upgrade_2_0' );
1110
        delete_option( 'pods_framework_upgraded_1_x' );
1111
1112
        // @todo Make sure all entries are being cleaned and do something about the pods_framework_upgrade_{version} dynamic entries created by PodsUpgrade
1113
        delete_option( 'pods_framework_upgrade_2_0_0' );
1114
        delete_option( 'pods_framework_upgrade_2_0_sister_ids' );
1115
        delete_option( 'pods_framework_version_last' );
1116
1117
        delete_option( 'pods_component_settings' );
1118
1119
        $api->cache_flush_pods();
1120
1121
        pods_transient_clear( 'pods_flush_rewrites' );
1122
1123
        self::$version = '';
1124
1125
        // Restore DB table prefix (if switched)
1126
        if ( null !== $_blog_id )
1127
            restore_current_blog();
1128
    }
1129
1130
	public function run () {
1131
1132
		if ( ! did_action( 'plugins_loaded' ) ) {
1133
			add_action( 'plugins_loaded', array( $this, 'load_components' ), 11 );
1134
		}
1135
		else {
1136
			$this->load_components();
1137
		}
1138
1139
		if ( ! did_action( 'setup_theme' ) ) {
1140
			add_action( 'setup_theme', array( $this, 'load_meta' ), 14 );
1141
		}
1142
		else {
1143
			$this->load_meta();
1144
		}
1145
1146
		if ( ! did_action( 'init' ) ) {
1147
			add_action( 'init', array( $this, 'core' ), 11 );
1148
            add_action( 'init', array( $this, 'add_rest_support' ), 12 );
1149
	        add_action( 'init', array( $this, 'setup_content_types' ), 11 );
1150
1151
	        if ( is_admin() ) {
1152
		        add_action( 'init', array( $this, 'admin_init' ), 12 );
1153
	        }
1154
		}
1155
		else {
1156
			$this->core();
1157
			$this->add_rest_support();
1158
			$this->setup_content_types();
1159
1160
			if ( is_admin() ) {
1161
				$this->admin_init();
1162
			}
1163
		}
1164
1165
        add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1166
        add_action( 'admin_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1167
        add_action( 'login_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1168
1169
        add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 );
1170
        add_action( 'delete_attachment', array( $this, 'delete_attachment' ) );
1171
1172
        // Register widgets
1173
        add_action( 'widgets_init', array( $this, 'register_widgets' ) );
1174
1175
        // Show admin bar links
1176
        add_action( 'admin_bar_menu', array( $this, 'admin_bar_links' ), 81 );
1177
1178
	}
1179
1180
    /**
1181
     * Delete Attachments from relationships
1182
     *
1183
     * @param int $_ID
1184
     */
1185
    public function delete_attachment ( $_ID ) {
1186
        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...
1187
1188
        $_ID = (int) $_ID;
1189
1190
        do_action( 'pods_delete_attachment', $_ID );
1191
1192
        $file_types = "'" . implode( "', '", PodsForm::file_field_types() ) . "'";
1193
1194
        if ( !pods_tableless() ) {
1195
            $sql = "
1196
                DELETE `rel`
1197
                FROM `@wp_podsrel` AS `rel`
1198
                LEFT JOIN `{$wpdb->posts}` AS `p`
1199
                    ON
1200
                        `p`.`post_type` = '_pods_field'
1201
                        AND ( `p`.`ID` = `rel`.`field_id` OR `p`.`ID` = `rel`.`related_field_id` )
1202
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1203
                    ON
1204
                        `pm`.`post_id` = `p`.`ID`
1205
                        AND `pm`.`meta_key` = 'type'
1206
                        AND `pm`.`meta_value` IN ( {$file_types} )
1207
                WHERE
1208
                    `p`.`ID` IS NOT NULL
1209
                    AND `pm`.`meta_id` IS NOT NULL
1210
                    AND `rel`.`item_id` = {$_ID}";
1211
1212
            pods_query( $sql, false );
1213
        }
1214
1215
        // Post Meta
1216 View Code Duplication
        if ( !empty( PodsMeta::$post_types ) ) {
1217
            $sql =  "
1218
                DELETE `rel`
1219
                FROM `@wp_postmeta` AS `rel`
1220
                LEFT JOIN `{$wpdb->posts}` AS `p`
1221
                    ON
1222
                        `p`.`post_type` = '_pods_field'
1223
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1224
                    ON
1225
                        `pm`.`post_id` = `p`.`ID`
1226
                        AND `pm`.`meta_key` = 'type'
1227
                        AND `pm`.`meta_value` IN ( {$file_types} )
1228
                WHERE
1229
                    `p`.`ID` IS NOT NULL
1230
                    AND `pm`.`meta_id` IS NOT NULL
1231
                    AND `rel`.`meta_key` = `p`.`post_name`
1232
                    AND `rel`.`meta_value` = '{$_ID}'";
1233
1234
            pods_query( $sql, false );
1235
        }
1236
1237
        // User Meta
1238 View Code Duplication
        if ( !empty( PodsMeta::$user ) ) {
1239
            $sql = "
1240
                DELETE `rel`
1241
                FROM `@wp_usermeta` AS `rel`
1242
                LEFT JOIN `{$wpdb->posts}` AS `p`
1243
                    ON
1244
                        `p`.`post_type` = '_pods_field'
1245
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1246
                    ON
1247
                        `pm`.`post_id` = `p`.`ID`
1248
                        AND `pm`.`meta_key` = 'type'
1249
                        AND `pm`.`meta_value` IN ( {$file_types} )
1250
                WHERE
1251
                    `p`.`ID` IS NOT NULL
1252
                    AND `pm`.`meta_id` IS NOT NULL
1253
                    AND `rel`.`meta_key` = `p`.`post_name`
1254
                    AND `rel`.`meta_value` = '{$_ID}'";
1255
1256
            pods_query( $sql, false );
1257
        }
1258
1259
        // Comment Meta
1260 View Code Duplication
        if ( !empty( PodsMeta::$comment ) ) {
1261
            $sql = "
1262
                DELETE `rel`
1263
                FROM `@wp_commentmeta` AS `rel`
1264
                LEFT JOIN `{$wpdb->posts}` AS `p`
1265
                    ON
1266
                        `p`.`post_type` = '_pods_field'
1267
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1268
                    ON
1269
                        `pm`.`post_id` = `p`.`ID`
1270
                        AND `pm`.`meta_key` = 'type'
1271
                        AND `pm`.`meta_value` IN ( {$file_types} )
1272
                WHERE
1273
                    `p`.`ID` IS NOT NULL
1274
                    AND `pm`.`meta_id` IS NOT NULL
1275
                    AND `rel`.`meta_key` = `p`.`post_name`
1276
                    AND `rel`.`meta_value` = '{$_ID}'";
1277
1278
            pods_query( $sql, false );
1279
        }
1280
    }
1281
1282
    /**
1283
     * Register widgets for Pods
1284
     */
1285
    public function register_widgets () {
1286
        $widgets = array(
1287
            'PodsWidgetSingle',
1288
            'PodsWidgetList',
1289
            'PodsWidgetField',
1290
            'PodsWidgetForm',
1291
            'PodsWidgetView'
1292
        );
1293
1294
        foreach ( $widgets as $widget ) {
1295
            if ( !file_exists( PODS_DIR . 'classes/widgets/' . $widget . '.php' ) )
1296
                continue;
1297
1298
            require_once PODS_DIR . 'classes/widgets/' . $widget . '.php';
1299
1300
            register_widget( $widget );
1301
        }
1302
    }
1303
1304
    /**
1305
     * Add Admin Bar links
1306
     */
1307
    public function admin_bar_links () {
1308
        global $wp_admin_bar, $pods;
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...
1309
1310
        if ( !is_user_logged_in() || !is_admin_bar_showing() )
1311
            return;
1312
1313
        $all_pods = pods_api()->load_pods( array( 'type' => 'pod', 'fields' => false ) );
1314
1315
        // Add New item links for all pods
1316
        foreach ( $all_pods as $pod ) {
0 ignored issues
show
Bug introduced by
The expression $all_pods of type array|object|integer|double|string|null|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...
1317
            if ( 0 == $pod[ 'options' ][ 'show_in_menu' ] )
1318
                continue;
1319
1320
            if ( !pods_is_admin( array( 'pods', 'pods_content', 'pods_add_' . $pod[ 'name' ] ) ) )
1321
                continue;
1322
1323
            $singular_label = pods_var_raw( 'label_singular', $pod[ 'options' ], pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true ), null, true );
1324
1325
            $wp_admin_bar->add_node( array(
1326
                'id' => 'new-pod-' . $pod[ 'name' ],
1327
                'title' => $singular_label,
1328
                'parent' => 'new-content',
1329
                'href' => admin_url( 'admin.php?page=pods-manage-' . $pod[ 'name' ] . '&action=add' )
1330
            ) );
1331
        }
1332
1333
        // Add edit link if we're on a pods page
1334
        if ( is_object( $pods ) && !is_wp_error( $pods ) && !empty( $pods->id ) && isset( $pods->pod_data ) && !empty( $pods->pod_data ) && 'pod' == $pods->pod_data[ 'type' ] ) {
1335
            $pod = $pods->pod_data;
1336
1337
            if ( pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $pod[ 'name' ] ) ) ) {
1338
                $singular_label = pods_var_raw( 'label_singular', $pod[ 'options' ], pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true ), null, true );
1339
1340
                $wp_admin_bar->add_node( array(
1341
                    'title' => sprintf( __( 'Edit %s', 'pods' ), $singular_label ),
1342
                    'id' => 'edit-pod',
1343
                    'href' => admin_url( 'admin.php?page=pods-manage-' . $pod[ 'name' ] . '&action=edit&id=' . $pods->id() )
1344
                ) );
1345
            }
1346
        }
1347
1348
    }
1349
1350
    /**
1351
     * Add REST API support to post type and taxonomy objects.
1352
     *
1353
     * @uses "init"
1354
     *
1355
     * @since 2.5.6
1356
     */
1357
    public function add_rest_support() {
1358
1359
        static $rest_support_added;
1360
1361
        if ( ! function_exists( 'register_rest_field' ) ) {
1362
            return;
1363
        }
1364
1365
        include_once( PODS_DIR . 'classes/PodsRESTFields.php' );
1366
        include_once( PODS_DIR . 'classes/PodsRESTHandlers.php' );
1367
1368
        $rest_bases = pods_transient_get( 'pods_rest_bases' );
1369
1370
        if ( empty( $rest_bases ) ) {
1371
            $pods = pods_api()->load_pods();
1372
1373
            $rest_bases = array();
1374
1375
            if ( ! empty( $pods ) && is_array( $pods ) ) {
1376
                foreach ( $pods as $pod ) {
1377
                    $type = $pod['type'];
1378
1379
                    if ( in_array( $type, array( 'post_type', 'taxonomy' ) ) ) {
1380
                        if ( $pod && PodsRESTHandlers::pod_extends_core_route( $pod ) ) {
1381
                            $rest_bases[ $pod['name'] ] = array(
1382
                                'type' => $type,
1383
                                'base' => sanitize_title( pods_v( 'rest_base', $pod['options'], $pod['name'] ) ),
1384
                            );
1385
                        }
1386
                    }
1387
                }
1388
            }
1389
1390
            if ( empty( $rest_bases ) ) {
1391
                $rest_bases = 'none';
1392
            }
1393
1394
            pods_transient_set( 'pods_rest_bases', $rest_bases );
1395
        }
1396
1397
        if ( empty( $rest_support_added ) && ! empty( $rest_bases ) && 'none' !== $rest_bases ) {
1398
            foreach ( $rest_bases as $pod_name => $pod_info ) {
1399
                $pod_type  = $pod_info['type'];
1400
                $rest_base = $pod_info['base'];
1401
1402
                if ( 'post_type' == $pod_type ) {
1403
                    PodsRESTHandlers::post_type_rest_support( $pod_name, $rest_base );
1404
                } elseif ( 'taxonomy' == $pod_type ) {
1405
                    PodsRESTHandlers::taxonomy_rest_support( $pod_name, $rest_base );
1406
                }
1407
1408
                new PodsRESTFields( $pod_name );
1409
            }
1410
1411
            $rest_support_added = true;
1412
        }
1413
1414
    }
1415
}
1416