Completed
Push — 2.x ( e88784...f57756 )
by Scott Kingsley
25:22 queued 17:24
created

PodsInit::load_components()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 3
eloc 3
nc 2
nop 0
1
<?php
2
/**
3
 * @package Pods
4
 */
5
class PodsInit {
6
7
    /**
8
     * @var PodsInit
9
     */
10
    static $instance = null;
1 ignored issue
show
Coding Style introduced by
We recommend specifying an explicit visibility for the field $instance.
Loading history...
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();
1 ignored issue
show
Coding Style introduced by
We recommend specifying an explicit visibility for the field $no_conflict.
Loading history...
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();
1 ignored issue
show
Coding Style introduced by
We recommend specifying an explicit visibility for the field $content_types_registered.
Loading history...
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;
1 ignored issue
show
Coding Style introduced by
We recommend specifying an explicit visibility for the field $components.
Loading history...
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;
1 ignored issue
show
Coding Style introduced by
We recommend specifying an explicit visibility for the field $meta.
Loading history...
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;
1 ignored issue
show
Coding Style introduced by
We recommend specifying an explicit visibility for the field $admin.
Loading history...
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;
1 ignored issue
show
Coding Style introduced by
We recommend specifying an explicit visibility for the field $version.
Loading history...
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;
1 ignored issue
show
Coding Style introduced by
We recommend specifying an explicit visibility for the field $version_last.
Loading history...
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;
1 ignored issue
show
Coding Style introduced by
We recommend specifying an explicit visibility for the field $db_version.
Loading history...
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(
1 ignored issue
show
Coding Style introduced by
We recommend specifying an explicit visibility for the field $upgrades.
Loading history...
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;
1 ignored issue
show
Coding Style introduced by
We recommend specifying an explicit visibility for the field $upgraded.
Loading history...
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;
1 ignored issue
show
Coding Style introduced by
We recommend specifying an explicit visibility for the field $upgrade_needed.
Loading history...
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
        }
853
        elseif ( 'taxonomy' == $type ) {
854
            $labels[ 'menu_name' ] = pods_var_raw( 'menu_name', $labels, $label, null, true );
855
            $labels[ 'search_items' ] = pods_var_raw( 'search_items', $labels, sprintf( __( 'Search %s', 'pods' ), $label ), null, true );
856
            $labels[ 'popular_items' ] = pods_var_raw( 'popular_items', $labels, sprintf( __( 'Popular %s', 'pods' ), $label ), null, true );
857
            $labels[ 'all_items' ] = pods_var_raw( 'all_items', $labels, sprintf( __( 'All %s', 'pods' ), $label ), null, true );
858
            $labels[ 'parent_item' ] = pods_var_raw( 'parent_item', $labels, sprintf( __( 'Parent %s', 'pods' ), $singular_label ), null, true );
859
            $labels[ 'parent_item_colon' ] = pods_var_raw( 'parent_item_colon', $labels, sprintf( __( 'Parent %s :', 'pods' ), $singular_label ), null, true );
860
            $labels[ 'edit_item' ] = pods_var_raw( 'edit_item', $labels, sprintf( __( 'Edit %s', 'pods' ), $singular_label ), null, true );
861
            $labels[ 'update_item' ] = pods_var_raw( 'update_item', $labels, sprintf( __( 'Update %s', 'pods' ), $singular_label ), null, true );
862
            $labels[ 'add_new_item' ] = pods_var_raw( 'add_new_item', $labels, sprintf( __( 'Add New %s', 'pods' ), $singular_label ), null, true );
863
            $labels[ 'new_item_name' ] = pods_var_raw( 'new_item_name', $labels, sprintf( __( 'New %s Name', 'pods' ), $singular_label ), null, true );
864
            $labels[ 'separate_items_with_commas' ] = pods_var_raw( 'separate_items_with_commas', $labels, sprintf( __( 'Separate %s with commas', 'pods' ), $label ), null, true );
865
            $labels[ 'add_or_remove_items' ] = pods_var_raw( 'add_or_remove_items', $labels, sprintf( __( 'Add or remove %s', 'pods' ), $label ), null, true );
866
            $labels[ 'choose_from_most_used' ] = pods_var_raw( 'choose_from_most_used', $labels, sprintf( __( 'Choose from the most used %s', 'pods' ), $label ), null, true );
867
        }
868
869
        $args[ 'labels' ] = $labels;
870
871
        return $args;
872
    }
873
874
    /**
875
     * Activate and Install
876
     */
877
    public function activate_install () {
878
        register_activation_hook( PODS_DIR . 'init.php', array( $this, 'activate' ) );
879
        register_deactivation_hook( PODS_DIR . 'init.php', array( $this, 'deactivate' ) );
880
881
        add_action( 'wpmu_new_blog', array( $this, 'new_blog' ), 10, 6 );
882
883
        if ( empty( self::$version ) || version_compare( self::$version, PODS_VERSION, '<' ) || version_compare( self::$version, PODS_DB_VERSION, '<=' ) || self::$upgrade_needed )
884
            $this->setup();
885
        elseif ( self::$version != PODS_VERSION ) {
886
            delete_option( 'pods_framework_version' );
887
            add_option( 'pods_framework_version', PODS_VERSION, '', 'yes' );
888
889
            pods_api()->cache_flush_pods();
890
        }
891
    }
892
893
    /**
894
     *
895
     */
896
    public function activate () {
897
        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...
898
899
        if ( function_exists( 'is_multisite' ) && is_multisite() && isset( $_GET[ 'networkwide' ] ) && 1 == $_GET[ 'networkwide' ] ) {
900
            $_blog_ids = $wpdb->get_col( "SELECT `blog_id` FROM `{$wpdb->blogs}`" );
901
902
            foreach ( $_blog_ids as $_blog_id ) {
903
                $this->setup( $_blog_id );
904
            }
905
        }
906
        else
907
            $this->setup();
908
    }
909
910
    /**
911
     *
912
     */
913
    public function deactivate () {
914
        pods_api()->cache_flush_pods();
915
    }
916
917
    /**
918
     *
919
     */
920
    public function needs_upgrade ( $current = null, $last = null ) {
921
        if ( null === $current )
922
            $current = self::$version;
923
924
        if ( null === $last )
925
            $last = self::$version_last;
926
927
        $upgrade_needed = false;
928
929
        if ( !empty( $current ) ) {
930
            foreach ( self::$upgrades as $old_version => $new_version ) {
931
                /*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...
932
                    continue;*/
933
934
                if ( version_compare( $last, $old_version, '>=' )
935
                     && version_compare( $last, $new_version, '<' )
936
                     && version_compare( $current, $new_version, '>=' )
937
                     && 1 != self::$upgraded
938
                ) {
939
                    $upgrade_needed = true;
940
941
                    break;
942
                }
943
            }
944
        }
945
946
        return $upgrade_needed;
947
    }
948
949
    /**
950
     * @param $_blog_id
951
     * @param $user_id
952
     * @param $domain
953
     * @param $path
954
     * @param $site_id
955
     * @param $meta
956
     */
957
    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...
958
        if ( function_exists( 'is_multisite' ) && is_multisite() && is_plugin_active_for_network( basename( PODS_DIR ) . '/init.php' ) )
959
            $this->setup( $_blog_id );
960
    }
961
962
    /**
963
     * @param null $_blog_id
964
     */
965
    public function setup ( $_blog_id = null ) {
966
        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...
967
968
        // Switch DB table prefixes
969 View Code Duplication
        if ( null !== $_blog_id && $_blog_id != $wpdb->blogid )
970
            switch_to_blog( pods_absint( $_blog_id ) );
971
        else
972
            $_blog_id = null;
973
974
        // Setup DB tables
975
        $pods_version = get_option( 'pods_framework_version' );
976
        $pods_version_last = get_option( 'pods_framework_version_last' );
977
978
        // Install Pods
979
        if ( empty( $pods_version ) ) {
980
            pods_upgrade()->install( $_blog_id );
981
982
            $old_version = get_option( 'pods_version' );
983
984
            if ( !empty( $old_version ) ) {
985
                if ( false === strpos( $old_version, '.' ) )
986
                    $old_version = pods_version_to_point( $old_version );
987
988
                delete_option( 'pods_framework_version_last' );
989
                add_option( 'pods_framework_version_last', $pods_version, '', 'yes' );
990
991
                self::$version_last = $old_version;
992
            }
993
        }
994
        // Upgrade Wizard needed
995
        elseif ( $this->needs_upgrade( $pods_version, $pods_version_last ) ) {
996
            // Do not do anything
997
            return;
998
        }
999
        // Update Pods and run any required DB updates
1000
        elseif ( version_compare( $pods_version, PODS_VERSION, '<=' ) ) {
1001
            if ( false !== apply_filters( 'pods_update_run', null, PODS_VERSION, $pods_version, $_blog_id ) && !isset( $_GET[ 'pods_bypass_update' ] ) ) {
1002
                do_action( 'pods_update', PODS_VERSION, $pods_version, $_blog_id );
1003
1004
                // Update 2.0 alpha / beta sites
1005
                if ( version_compare( '2.0.0-a-1', $pods_version, '<=' ) && version_compare( $pods_version, '2.0.0-b-15', '<=' ) )
1006
                    include( PODS_DIR . 'sql/update-2.0-beta.php' );
1007
1008
                if ( version_compare( $pods_version, PODS_DB_VERSION, '<=' ) )
1009
                    include( PODS_DIR . 'sql/update.php' );
1010
1011
                do_action( 'pods_update_post', PODS_VERSION, $pods_version, $_blog_id );
1012
            }
1013
1014
            delete_option( 'pods_framework_version_last' );
1015
            add_option( 'pods_framework_version_last', $pods_version, '', 'yes' );
1016
1017
            self::$version_last = $pods_version;
1018
        }
1019
1020
        delete_option( 'pods_framework_version' );
1021
        add_option( 'pods_framework_version', PODS_VERSION, '', 'yes' );
1022
1023
        delete_option( 'pods_framework_db_version' );
1024
        add_option( 'pods_framework_db_version', PODS_DB_VERSION, '', 'yes' );
1025
1026
        pods_api()->cache_flush_pods();
1027
1028
        // Restore DB table prefix (if switched)
1029
        if ( null !== $_blog_id )
1030
            restore_current_blog();
1031
	    else {
1032
		    $this->run();
1033
	    }
1034
    }
1035
1036
    /**
1037
     * @param null $_blog_id
1038
     */
1039
    public function reset ( $_blog_id = null ) {
1040
        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...
1041
1042
        // Switch DB table prefixes
1043 View Code Duplication
        if ( null !== $_blog_id && $_blog_id != $wpdb->blogid )
1044
            switch_to_blog( pods_absint( $_blog_id ) );
1045
        else
1046
            $_blog_id = null;
1047
1048
        $api = pods_api();
1049
1050
        $pods = $api->load_pods( array( 'names_ids' => true ) );
1051
1052
        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...
1053
            $api->delete_pod( array( 'id' => $pod_id ) );
1054
        }
1055
1056
        $templates = $api->load_templates();
1057
1058
        foreach ( $templates as $template ) {
1059
            $api->delete_template( array( 'id' => $template[ 'id' ] ) );
1060
        }
1061
1062
        $pages = $api->load_pages();
1063
1064
        foreach ( $pages as $page ) {
1065
            $api->delete_page( array( 'id' => $page[ 'id' ] ) );
1066
        }
1067
1068
        $helpers = $api->load_helpers();
1069
1070
        foreach ( $helpers as $helper ) {
1071
            $api->delete_helper( array( 'id' => $helper[ 'id' ] ) );
1072
        }
1073
1074
        $tables = $wpdb->get_results( "SHOW TABLES LIKE '{$wpdb->prefix}pods%'", ARRAY_N );
1075
1076
        if ( !empty( $tables ) ) {
1077
            foreach ( $tables as $table ) {
1078
                $table = $table[ 0 ];
1079
1080
                pods_query( "DROP TABLE `{$table}`", false );
1081
            }
1082
        }
1083
1084
        // Remove any orphans
1085
        $wpdb->query( "
1086
                DELETE `p`, `pm`
1087
                FROM `{$wpdb->posts}` AS `p`
1088
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1089
                    ON `pm`.`post_id` = `p`.`ID`
1090
                WHERE
1091
                    `p`.`post_type` LIKE '_pods_%'
1092
            " );
1093
1094
        delete_option( 'pods_framework_version' );
1095
        delete_option( 'pods_framework_db_version' );
1096
        delete_option( 'pods_framework_upgrade_2_0' );
1097
        delete_option( 'pods_framework_upgraded_1_x' );
1098
1099
        // @todo Make sure all entries are being cleaned and do something about the pods_framework_upgrade_{version} dynamic entries created by PodsUpgrade
1100
        delete_option( 'pods_framework_upgrade_2_0_0' );
1101
        delete_option( 'pods_framework_upgrade_2_0_sister_ids' );
1102
        delete_option( 'pods_framework_version_last' );
1103
1104
        delete_option( 'pods_component_settings' );
1105
1106
        $api->cache_flush_pods();
1107
1108
        pods_transient_clear( 'pods_flush_rewrites' );
1109
1110
        self::$version = '';
1111
1112
        // Restore DB table prefix (if switched)
1113
        if ( null !== $_blog_id )
1114
            restore_current_blog();
1115
    }
1116
1117
	public function run () {
1118
1119
		if ( ! did_action( 'plugins_loaded' ) ) {
1120
			add_action( 'plugins_loaded', array( $this, 'load_components' ), 11 );
1121
		}
1122
		else {
1123
			$this->load_components();
1124
		}
1125
1126
		if ( ! did_action( 'setup_theme' ) ) {
1127
			add_action( 'setup_theme', array( $this, 'load_meta' ), 14 );
1128
		}
1129
		else {
1130
			$this->load_meta();
1131
		}
1132
1133
		if ( ! did_action( 'init' ) ) {
1134
			add_action( 'init', array( $this, 'core' ), 11 );
1135
            add_action( 'init', array( $this, 'add_rest_support' ), 12 );
1136
	        add_action( 'init', array( $this, 'setup_content_types' ), 11 );
1137
1138
	        if ( is_admin() ) {
1139
		        add_action( 'init', array( $this, 'admin_init' ), 12 );
1140
	        }
1141
		}
1142
		else {
1143
			$this->core();
1144
			$this->add_rest_support();
1145
			$this->setup_content_types();
1146
1147
			if ( is_admin() ) {
1148
				$this->admin_init();
1149
			}
1150
		}
1151
1152
        add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1153
        add_action( 'admin_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1154
        add_action( 'login_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1155
1156
        add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 );
1157
        add_action( 'delete_attachment', array( $this, 'delete_attachment' ) );
1158
1159
        // Register widgets
1160
        add_action( 'widgets_init', array( $this, 'register_widgets' ) );
1161
1162
        // Show admin bar links
1163
        add_action( 'admin_bar_menu', array( $this, 'admin_bar_links' ), 81 );
1164
1165
	}
1166
1167
    /**
1168
     * Delete Attachments from relationships
1169
     *
1170
     * @param int $_ID
1171
     */
1172
    public function delete_attachment ( $_ID ) {
1173
        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...
1174
1175
        $_ID = (int) $_ID;
1176
1177
        do_action( 'pods_delete_attachment', $_ID );
1178
1179
        $file_types = "'" . implode( "', '", PodsForm::file_field_types() ) . "'";
1180
1181
        if ( !pods_tableless() ) {
1182
            $sql = "
1183
                DELETE `rel`
1184
                FROM `@wp_podsrel` AS `rel`
1185
                LEFT JOIN `{$wpdb->posts}` AS `p`
1186
                    ON
1187
                        `p`.`post_type` = '_pods_field'
1188
                        AND ( `p`.`ID` = `rel`.`field_id` OR `p`.`ID` = `rel`.`related_field_id` )
1189
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1190
                    ON
1191
                        `pm`.`post_id` = `p`.`ID`
1192
                        AND `pm`.`meta_key` = 'type'
1193
                        AND `pm`.`meta_value` IN ( {$file_types} )
1194
                WHERE
1195
                    `p`.`ID` IS NOT NULL
1196
                    AND `pm`.`meta_id` IS NOT NULL
1197
                    AND `rel`.`item_id` = {$_ID}";
1198
1199
            pods_query( $sql, false );
1200
        }
1201
1202
        // Post Meta
1203 View Code Duplication
        if ( !empty( PodsMeta::$post_types ) ) {
1204
            $sql =  "
1205
                DELETE `rel`
1206
                FROM `@wp_postmeta` AS `rel`
1207
                LEFT JOIN `{$wpdb->posts}` AS `p`
1208
                    ON
1209
                        `p`.`post_type` = '_pods_field'
1210
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1211
                    ON
1212
                        `pm`.`post_id` = `p`.`ID`
1213
                        AND `pm`.`meta_key` = 'type'
1214
                        AND `pm`.`meta_value` IN ( {$file_types} )
1215
                WHERE
1216
                    `p`.`ID` IS NOT NULL
1217
                    AND `pm`.`meta_id` IS NOT NULL
1218
                    AND `rel`.`meta_key` = `p`.`post_name`
1219
                    AND `rel`.`meta_value` = '{$_ID}'";
1220
1221
            pods_query( $sql, false );
1222
        }
1223
1224
        // User Meta
1225 View Code Duplication
        if ( !empty( PodsMeta::$user ) ) {
1226
            $sql = "
1227
                DELETE `rel`
1228
                FROM `@wp_usermeta` AS `rel`
1229
                LEFT JOIN `{$wpdb->posts}` AS `p`
1230
                    ON
1231
                        `p`.`post_type` = '_pods_field'
1232
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1233
                    ON
1234
                        `pm`.`post_id` = `p`.`ID`
1235
                        AND `pm`.`meta_key` = 'type'
1236
                        AND `pm`.`meta_value` IN ( {$file_types} )
1237
                WHERE
1238
                    `p`.`ID` IS NOT NULL
1239
                    AND `pm`.`meta_id` IS NOT NULL
1240
                    AND `rel`.`meta_key` = `p`.`post_name`
1241
                    AND `rel`.`meta_value` = '{$_ID}'";
1242
1243
            pods_query( $sql, false );
1244
        }
1245
1246
        // Comment Meta
1247 View Code Duplication
        if ( !empty( PodsMeta::$comment ) ) {
1248
            $sql = "
1249
                DELETE `rel`
1250
                FROM `@wp_commentmeta` AS `rel`
1251
                LEFT JOIN `{$wpdb->posts}` AS `p`
1252
                    ON
1253
                        `p`.`post_type` = '_pods_field'
1254
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1255
                    ON
1256
                        `pm`.`post_id` = `p`.`ID`
1257
                        AND `pm`.`meta_key` = 'type'
1258
                        AND `pm`.`meta_value` IN ( {$file_types} )
1259
                WHERE
1260
                    `p`.`ID` IS NOT NULL
1261
                    AND `pm`.`meta_id` IS NOT NULL
1262
                    AND `rel`.`meta_key` = `p`.`post_name`
1263
                    AND `rel`.`meta_value` = '{$_ID}'";
1264
1265
            pods_query( $sql, false );
1266
        }
1267
    }
1268
1269
    /**
1270
     * Register widgets for Pods
1271
     */
1272
    public function register_widgets () {
1273
        $widgets = array(
1274
            'PodsWidgetSingle',
1275
            'PodsWidgetList',
1276
            'PodsWidgetField',
1277
            'PodsWidgetForm',
1278
            'PodsWidgetView'
1279
        );
1280
1281
        foreach ( $widgets as $widget ) {
1282
            if ( !file_exists( PODS_DIR . 'classes/widgets/' . $widget . '.php' ) )
1283
                continue;
1284
1285
            require_once PODS_DIR . 'classes/widgets/' . $widget . '.php';
1286
1287
            register_widget( $widget );
1288
        }
1289
    }
1290
1291
    /**
1292
     * Add Admin Bar links
1293
     */
1294
    public function admin_bar_links () {
1295
        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...
1296
1297
        if ( !is_user_logged_in() || !is_admin_bar_showing() )
1298
            return;
1299
1300
        $all_pods = pods_api()->load_pods( array( 'type' => 'pod', 'fields' => false ) );
1301
1302
        // Add New item links for all pods
1303
        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...
1304
            if ( 0 == $pod[ 'options' ][ 'show_in_menu' ] )
1305
                continue;
1306
1307
            if ( !pods_is_admin( array( 'pods', 'pods_content', 'pods_add_' . $pod[ 'name' ] ) ) )
1308
                continue;
1309
1310
            $singular_label = pods_var_raw( 'label_singular', $pod[ 'options' ], pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true ), null, true );
1311
1312
            $wp_admin_bar->add_node( array(
1313
                'id' => 'new-pod-' . $pod[ 'name' ],
1314
                'title' => $singular_label,
1315
                'parent' => 'new-content',
1316
                'href' => admin_url( 'admin.php?page=pods-manage-' . $pod[ 'name' ] . '&action=add' )
1317
            ) );
1318
        }
1319
1320
        // Add edit link if we're on a pods page
1321
        if ( is_object( $pods ) && !is_wp_error( $pods ) && !empty( $pods->id ) && isset( $pods->pod_data ) && !empty( $pods->pod_data ) && 'pod' == $pods->pod_data[ 'type' ] ) {
1322
            $pod = $pods->pod_data;
1323
1324
            if ( pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $pod[ 'name' ] ) ) ) {
1325
                $singular_label = pods_var_raw( 'label_singular', $pod[ 'options' ], pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true ), null, true );
1326
1327
                $wp_admin_bar->add_node( array(
1328
                    'title' => sprintf( __( 'Edit %s', 'pods' ), $singular_label ),
1329
                    'id' => 'edit-pod',
1330
                    'href' => admin_url( 'admin.php?page=pods-manage-' . $pod[ 'name' ] . '&action=edit&id=' . $pods->id() )
1331
                ) );
1332
            }
1333
        }
1334
1335
    }
1336
1337
    /**
1338
     * Add REST API support to post type and taxonomy objects.
1339
     *
1340
     * @uses "init"
1341
     *
1342
     * @since 2.5.6
1343
     */
1344
    public function add_rest_support() {
1345
1346
        static $rest_support_added;
1347
1348
        if ( ! function_exists( 'register_api_field' ) ) {
1349
            return;
1350
        }
1351
1352
        include_once( PODS_DIR . 'classes/PodsRESTFields.php' );
1353
        include_once( PODS_DIR . 'classes/PodsRESTHandlers.php' );
1354
1355
        $rest_bases = pods_transient_get( 'pods_rest_bases' );
1356
1357
        if ( empty( $rest_bases ) ) {
1358
            $pods = pods_api()->load_pods();
1359
1360
            $rest_bases = array();
1361
1362
            if ( ! empty( $pods ) && is_array( $pods ) ) {
1363
                foreach ( $pods as $pod ) {
1364
                    $type = $pod['type'];
1365
1366
                    if ( in_array( $type, array( 'post_type', 'taxonomy' ) ) ) {
1367
                        if ( $pod && PodsRESTHandlers::pod_extends_core_route( $pod ) ) {
1368
                            $rest_bases[ $pod['name'] ] = array(
1369
                                'type' => $type,
1370
                                'base' => sanitize_title( pods_v( 'rest_base', $pod['options'], $pod['name'] ) ),
1371
                            );
1372
                        }
1373
                    }
1374
                }
1375
            }
1376
1377
            if ( empty( $rest_bases ) ) {
1378
                $rest_bases = 'none';
1379
            }
1380
1381
            pods_transient_set( 'pods_rest_bases', $rest_bases );
1382
        }
1383
1384
        if ( empty( $rest_support_added ) && ! empty( $rest_bases ) && 'none' !== $rest_bases ) {
1385
            foreach ( $rest_bases as $pod_name => $pod_info ) {
1386
                $pod_type  = $pod_info['type'];
1387
                $rest_base = $pod_info['base'];
1388
1389
                if ( 'post_type' == $pod_type ) {
1390
                    PodsRESTHandlers::post_type_rest_support( $pod_name, $rest_base );
1391
                } elseif ( 'taxonomy' == $pod_type ) {
1392
                    PodsRESTHandlers::taxonomy_rest_support( $pod_name, $rest_base );
1393
                }
1394
1395
                new PodsRESTFields( $pod_name );
1396
            }
1397
1398
            $rest_support_added = true;
1399
        }
1400
1401
    }
1402
}
1403