Completed
Pull Request — 2.x (#4370)
by
unknown
05:02
created

PodsInit   D

Complexity

Total Complexity 248

Size/Duplication

Total Lines 1659
Duplicated Lines 6.63 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
dl 110
loc 1659
rs 4.4102
c 0
b 0
f 0
wmc 248
lcom 2
cbo 6

26 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
B __construct() 0 31 5
A load_components() 0 11 4
A load_meta() 0 4 1
C core() 0 59 12
A plugins_loaded() 0 13 3
A load_i18n() 0 4 1
F register_assets() 0 104 11
B register_pods() 0 38 1
A admin_init() 0 4 1
F setup_content_types() 42 524 95
A flush_rewrite_rules() 0 15 2
B setup_updated_messages() 0 59 8
C object_label_fix() 0 71 7
B activate_install() 0 19 6
B activate() 0 14 6
A deactivate() 0 5 1
D needs_upgrade() 0 27 9
A new_blog() 0 6 4
C setup() 4 75 14
C reset() 4 80 10
B run() 0 56 7
B delete_attachment() 60 97 5
A register_widgets() 0 20 3
C admin_bar_links() 0 46 13
C add_rest_support() 0 62 17

How to fix   Duplicated Code    Complexity   

Duplicated Code

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

Common duplication problems, and corresponding solutions are:

Complex Class

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

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

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

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

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 PodsI18n
34
	 */
35
	static $i18n;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $i18n.

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 PodsAdmin
39
	 */
40
	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...
41
42
	/**
43
	 * @var mixed|void
44
	 */
45
	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...
46
47
	/**
48
	 * @var mixed|void
49
	 */
50
	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...
51
52
	/**
53
	 * @var mixed|void
54
	 */
55
	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...
56
57
	/**
58
	 * Upgrades to trigger (last installed version => upgrade version)
59
	 *
60
	 * @var array
61
	 */
62
	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...
63
		'1.0.0' => '2.0.0'
64
		//'2.0.0' => '2.1.0'
65
	);
66
67
	/**
68
	 * Whether an Upgrade for 1.x has happened
69
	 *
70
	 * @var bool
71
	 */
72
	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...
73
74
	/**
75
	 * Whether an Upgrade is needed
76
	 *
77
	 * @var bool
78
	 */
79
	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...
80
81
	/**
82
	 * Singleton handling for a basic pods_init() request
83
	 *
84
	 * @return \PodsInit
85
	 *
86
	 * @since 2.3.5
87
	 */
88
	public static function init() {
89
90
		if ( ! is_object( self::$instance ) ) {
91
			self::$instance = new PodsInit();
92
		}
93
94
		return self::$instance;
95
	}
96
97
	/**
98
	 * Setup and Initiate Pods
99
	 *
100
	 * @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...
101
	 *
102
	 * @license http://www.gnu.org/licenses/gpl-2.0.html
103
	 * @since   1.8.9
104
	 */
105
	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...
106
107
		self::$version      = get_option( 'pods_framework_version' );
108
		self::$version_last = get_option( 'pods_framework_version_last' );
109
		self::$db_version   = get_option( 'pods_framework_db_version' );
110
		self::$upgraded     = (int) get_option( 'pods_framework_upgraded_1_x' );
111
112
		if ( empty( self::$version_last ) && 0 < strlen( get_option( 'pods_version' ) ) ) {
113
			$old_version = get_option( 'pods_version' );
114
115
			if ( ! empty( $old_version ) ) {
116
				if ( false === strpos( $old_version, '.' ) ) {
117
					$old_version = pods_version_to_point( $old_version );
118
				}
119
120
				update_option( 'pods_framework_version_last', $old_version );
121
122
				self::$version_last = $old_version;
123
			}
124
		}
125
126
		self::$upgrade_needed = $this->needs_upgrade();
127
128
		add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
129
		add_action( 'plugins_loaded', array( $this, 'activate_install' ), 9 );
130
131
		add_action( 'wp_loaded', array( $this, 'flush_rewrite_rules' ) );
132
133
		$this->run();
134
135
	}
136
137
	/**
138
	 * Load the plugin textdomain and set default constants
139
	 */
140
	public function plugins_loaded() {
141
142
		if ( ! defined( 'PODS_LIGHT' ) ) {
143
			define( 'PODS_LIGHT', false );
144
		}
145
146
		if ( ! defined( 'PODS_TABLELESS' ) ) {
147
			define( 'PODS_TABLELESS', false );
148
		}
149
150
		load_plugin_textdomain( 'pods' );
151
152
	}
153
154
	/**
155
	 * Load Pods Components
156
	 */
157
	public function load_components() {
158
159
		if ( empty( self::$version ) ) {
160
			return;
161
		}
162
163
		if ( ! defined( 'PODS_LIGHT' ) || ! PODS_LIGHT ) {
164
			self::$components = pods_components();
165
		}
166
167
	}
168
169
	/**
170
	 * Load Pods Meta
171
	 */
172
	public function load_meta() {
173
174
		self::$meta = pods_meta()->core();
175
	}
176
177
	/**
178
	 *
179
	 */
180
	public function load_i18n() {
181
182
		self::$i18n = pods_i18n();
183
	}
184
185
186
	/**
187
	 * Set up the Pods core
188
	 */
189
	public function core() {
190
191
		if ( empty( self::$version ) ) {
192
			return;
193
		}
194
195
		// Session start
196
		pods_session_start();
197
198
		add_shortcode( 'pods', 'pods_shortcode' );
199
		add_shortcode( 'pods-form', 'pods_shortcode_form' );
200
201
		$security_settings = array(
202
			'pods_disable_file_browser'     => 0,
203
			'pods_files_require_login'      => 1,
204
			'pods_files_require_login_cap'  => '',
205
			'pods_disable_file_upload'      => 0,
206
			'pods_upload_require_login'     => 1,
207
			'pods_upload_require_login_cap' => ''
208
		);
209
210
		foreach ( $security_settings as $security_setting => $setting ) {
211
			$setting = get_option( $security_setting );
212
			if ( ! empty( $setting ) ) {
213
				$security_settings[ $security_setting ] = $setting;
214
			}
215
		}
216
217
		foreach ( $security_settings as $security_setting => $setting ) {
218
			if ( 0 == $setting ) {
219
				$setting = false;
220
			} elseif ( 1 == $setting ) {
221
				$setting = true;
222
			}
223
224
			if ( in_array( $security_setting, array( 'pods_files_require_login', 'pods_upload_require_login' ) ) ) {
225
				if ( 0 < strlen( $security_settings[ $security_setting . '_cap' ] ) ) {
226
					$setting = $security_settings[ $security_setting . '_cap' ];
227
				}
228
			} elseif ( in_array( $security_setting, array(
229
				'pods_files_require_login_cap',
230
				'pods_upload_require_login_cap'
231
			) ) ) {
232
				continue;
233
			}
234
235
			if ( ! defined( strtoupper( $security_setting ) ) ) {
236
				define( strtoupper( $security_setting ), $setting );
237
			}
238
		}
239
240
		$this->register_pods();
241
242
		$avatar = PodsForm::field_loader( 'avatar' );
243
244
		if ( method_exists( $avatar, 'get_avatar' ) ) {
245
			add_filter( 'get_avatar', array( $avatar, 'get_avatar' ), 10, 4 );
246
		}
247
	}
248
249
	/**
250
	 * Register Scripts and Styles
251
	 */
252
	public function register_assets() {
253
254
		if ( ! wp_style_is( 'jquery-ui', 'registered' ) ) {
255
			wp_register_style( 'jquery-ui', PODS_URL . 'ui/css/smoothness/jquery-ui.custom.css', array(), '1.8.16' );
256
		}
257
258
		wp_register_script( 'pods-json', PODS_URL . 'ui/js/jquery.json.js', array( 'jquery' ), '2.3' );
259
260
		if ( ! wp_style_is( 'jquery-qtip2', 'registered' ) ) {
261
			wp_register_style( 'jquery-qtip2', PODS_URL . 'ui/css/jquery.qtip.min.css', array(), '2.2' );
262
		}
263
264
		if ( ! wp_script_is( 'jquery-qtip2', 'registered' ) ) {
265
			wp_register_script( 'jquery-qtip2', PODS_URL . 'ui/js/jquery.qtip.min.js', array( 'jquery' ), '2.2' );
266
		}
267
268
		wp_register_script( 'pods', PODS_URL . 'ui/js/jquery.pods.js', array(
269
			'jquery',
270
			'pods-dfv',
271
			'pods-i18n',
272
			'pods-json',
273
			'jquery-qtip2'
274
		), PODS_VERSION, true );
275
276
		wp_register_style( 'pods-form', PODS_URL . 'ui/css/pods-form.css', array(), PODS_VERSION );
277
278
		wp_register_style( 'pods-cleditor', PODS_URL . 'ui/css/jquery.cleditor.css', array(), '1.3.0' );
279
		wp_register_script( 'pods-cleditor', PODS_URL . 'ui/js/jquery.cleditor.min.js', array( 'jquery' ), '1.3.0' );
280
281
		wp_register_style( 'pods-codemirror', PODS_URL . 'ui/css/codemirror.css', array(), '4.8' );
282
		wp_register_script( 'pods-codemirror', PODS_URL . 'ui/js/codemirror.js', array(), '4.8', true );
283
		wp_register_script( 'pods-codemirror-loadmode', PODS_URL . 'ui/js/codemirror/addon/mode/loadmode.js', array( 'pods-codemirror' ), '4.8', true );
284
		wp_register_script( 'pods-codemirror-overlay', PODS_URL . 'ui/js/codemirror/addon/mode/overlay.js', array( 'pods-codemirror' ), '4.8', true );
285
		wp_register_script( 'pods-codemirror-hints', PODS_URL . 'ui/js/codemirror/addon/mode/show-hint.js', array( 'pods-codemirror' ), '4.8', true );
286
		wp_register_script( 'pods-codemirror-mode-xml', PODS_URL . 'ui/js/codemirror/mode/xml/xml.js', array( 'pods-codemirror' ), '4.8', true );
287
		wp_register_script( 'pods-codemirror-mode-html', PODS_URL . 'ui/js/codemirror/mode/htmlmixed/htmlmixed.js', array( 'pods-codemirror' ), '4.8', true );
288
		wp_register_script( 'pods-codemirror-mode-css', PODS_URL . 'ui/js/codemirror/mode/css/css.js', array( 'pods-codemirror' ), '4.8', true );
289
290
		if ( ! wp_script_is( 'jquery-ui-slideraccess', 'registered' ) ) {
291
			// No need to add dependencies. All managed by jquery-ui-timepicker.
292
			wp_register_script( 'jquery-ui-slideraccess', PODS_URL . 'ui/js/timepicker/jquery-ui-sliderAccess.js', array(), '0.3' );
293
		}
294
		// Localization enqueued within the fields display files.
295
		/*if ( ! wp_script_is( 'jquery-ui-timepicker-i18n', 'registered' ) ) {
296
			// No need to add dependencies. All managed by jquery-ui-timepicker.
297
			wp_register_script( 'jquery-ui-timepicker-i18n', PODS_URL . 'ui/js/timepicker/i18n/jquery-ui-timepicker-addon-i18n.min.js', array(), '0.3' );
298
		}*/
299
300
		if ( ! wp_style_is( 'jquery-ui-timepicker', 'registered' ) ) {
301
			wp_register_style( 'jquery-ui-timepicker', PODS_URL . 'ui/js/timepicker/jquery-ui-timepicker-addon.min.css', array(), '1.6.3' );
302
		}
303
304
		if ( ! wp_script_is( 'jquery-ui-timepicker', 'registered' ) ) {
305
			wp_register_script( 'jquery-ui-timepicker', PODS_URL . 'ui/js/timepicker/jquery-ui-timepicker-addon.min.js', array(
306
				'jquery',
307
				'jquery-ui-core',
308
				'jquery-ui-datepicker',
309
				'jquery-ui-slider',
310
				'jquery-ui-slideraccess',
311
			), '1.6.3' );
312
		}
313
314
		wp_register_style( 'pods-select2', PODS_URL . 'ui/js/select2/select2.min.css', array(), '4.0.3' );
315
		wp_register_script( 'pods-select2', PODS_URL . 'ui/js/select2/select2.min.js', array( 'jquery', 'pods-i18n' ), '4.0.3' );
316
317
		$register_handlebars = apply_filters( 'pods_script_register_handlebars', true );
318
319
		if ( is_admin() ) {
320
			$screen = get_current_screen();
321
322
			// Deregister the outdated Pods handlebars script on TEC event screen
323
			if ( $screen && 'tribe_events' === $screen->post_type ) {
324
				$register_handlebars = false;
325
			}
326
		}
327
328
		if ( $register_handlebars ) {
329
			wp_register_script( 'pods-handlebars', PODS_URL . 'ui/js/handlebars.js', array(), '1.0.0.beta.6' );
330
		}
331
332
		// Marionette dependencies for MV fields
333
		wp_register_script( 'backbone.radio', PODS_URL . 'ui/js/marionette/backbone.radio.js', array( 'backbone' ), '2.0.0', true );
334
		wp_register_script( 'marionette', PODS_URL . 'ui/js/marionette/backbone.marionette.js', array( 'backbone', 'backbone.radio' ), '3.1.0', true );
335
336
		// MV stuff
337
		wp_register_script(
338
			'pods-dfv',
339
			PODS_URL . 'ui/js/pods-dfv/pods-dfv.min.js',
340
			array(
341
				'jquery',
342
				'jquery-ui-core',
343
				'jquery-ui-sortable',
344
				'pods-i18n',
345
				'marionette',
346
				'media-views',
347
				'media-models'
348
			),
349
			PODS_VERSION,
350
			true
351
		);
352
353
		wp_register_style( 'pods-dfv-list', PODS_URL . 'ui/css/pods-dfv-list.css', array(), PODS_VERSION );
354
355
	}
356
357
	/**
358
	 * Register internal Post Types
359
	 */
360
	public function register_pods() {
361
362
		$args = array(
363
			'label'           => 'Pods',
364
			'labels'          => array( 'singular_name' => 'Pod' ),
365
			'public'          => false,
366
			'can_export'      => false,
367
			'query_var'       => false,
368
			'rewrite'         => false,
369
			'capability_type' => 'pods_pod',
370
			'has_archive'     => false,
371
			'hierarchical'    => false,
372
			'supports'        => array( 'title', 'author' ),
373
			'menu_icon'       => 'dashicons-pods'
374
		);
375
376
		$args = self::object_label_fix( $args, 'post_type' );
377
378
		register_post_type( '_pods_pod', apply_filters( 'pods_internal_register_post_type_pod', $args ) );
379
380
		$args = array(
381
			'label'           => 'Pod Fields',
382
			'labels'          => array( 'singular_name' => 'Pod Field' ),
383
			'public'          => false,
384
			'can_export'      => false,
385
			'query_var'       => false,
386
			'rewrite'         => false,
387
			'capability_type' => 'pods_pod',
388
			'has_archive'     => false,
389
			'hierarchical'    => true,
390
			'supports'        => array( 'title', 'editor', 'author' ),
391
			'menu_icon'       => 'dashicons-pods'
392
		);
393
394
		$args = self::object_label_fix( $args, 'post_type' );
395
396
		register_post_type( '_pods_field', apply_filters( 'pods_internal_register_post_type_field', $args ) );
397
	}
398
399
	/**
400
	 * Include Admin
401
	 */
402
	public function admin_init() {
403
404
		self::$admin = pods_admin();
405
	}
406
407
	/**
408
	 * Register Post Types and Taxonomies
409
	 */
410
	public function setup_content_types( $force = false ) {
411
412
		if ( empty( self::$version ) ) {
413
			return;
414
		}
415
416
		$post_types = PodsMeta::$post_types;
417
		$taxonomies = PodsMeta::$taxonomies;
418
419
		$existing_post_types = get_post_types();
420
		$existing_taxonomies = get_taxonomies();
421
422
		$pods_cpt_ct = pods_transient_get( 'pods_wp_cpt_ct' );
423
424
		$cpt_positions = array();
425
426
		if ( empty( $pods_cpt_ct ) && ( ! empty( $post_types ) || ! empty( $taxonomies ) ) ) {
427
			$force = true;
428
		} elseif ( ! empty( $pods_cpt_ct ) && empty( $pods_cpt_ct['post_types'] ) && ! empty( $post_types ) ) {
429
			$force = true;
430
		} elseif ( ! empty( $pods_cpt_ct ) && empty( $pods_cpt_ct['taxonomies'] ) && ! empty( $taxonomies ) ) {
431
			$force = true;
432
		}
433
434
		if ( false === $pods_cpt_ct || $force ) {
435
			/**
436
			 * @var WP_Query
437
			 */
438
			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...
439
440
			$reserved_query_vars = array(
441
				'post_type',
442
				'taxonomy',
443
				'output'
444
			);
445
446
			if ( is_object( $wp_query ) ) {
447
				$reserved_query_vars = array_merge( $reserved_query_vars, array_keys( $wp_query->fill_query_vars( array() ) ) );
448
			}
449
450
			$pods_cpt_ct = array(
451
				'post_types' => array(),
452
				'taxonomies' => array()
453
			);
454
455
			$pods_post_types      = $pods_taxonomies = array();
456
			$supported_post_types = $supported_taxonomies = array();
457
458
			$post_format_post_types = array();
459
460
			foreach ( $post_types as $post_type ) {
461
				// Post Type exists already
462 View Code Duplication
				if ( isset( $pods_cpt_ct['post_types'][ $post_type['name'] ] ) ) {
463
					continue;
464
				} elseif ( ! empty( $post_type['object'] ) && isset( $existing_post_types[ $post_type['object'] ] ) ) {
465
					continue;
466
				} elseif ( ! $force && isset( $existing_post_types[ $post_type['name'] ] ) ) {
467
					continue;
468
				}
469
470
				$post_type['options']['name'] = $post_type['name'];
471
				$post_type                    = array_merge( $post_type, (array) $post_type['options'] );
472
473
				$post_type_name = pods_v_sanitized( 'name', $post_type );
474
475
				// Labels
476
				$cpt_label    = esc_html( pods_v( 'label', $post_type, ucwords( str_replace( '_', ' ', pods_v( 'name', $post_type ) ) ), true ) );
477
				$cpt_singular = esc_html( pods_v( 'label_singular', $post_type, ucwords( str_replace( '_', ' ', pods_v( 'label', $post_type, $post_type_name, null, true ) ) ), true ) );
478
479
				$cpt_labels                          = array();
480
				$cpt_labels['name']                  = $cpt_label;
481
				$cpt_labels['singular_name']         = $cpt_singular;
482
				$cpt_labels['menu_name']             = pods_v( 'menu_name', $post_type, '', true );
483
				$cpt_labels['name_admin_bar']        = pods_v( 'name_admin_bar', $post_type, '', true );
484
				$cpt_labels['add_new']               = pods_v( 'label_add_new', $post_type, '', true );
485
				$cpt_labels['add_new_item']          = pods_v( 'label_add_new_item', $post_type, '', true );
486
				$cpt_labels['new_item']              = pods_v( 'label_new_item', $post_type, '', true );
487
				$cpt_labels['edit']                  = pods_v( 'label_edit', $post_type, '', true );
488
				$cpt_labels['edit_item']             = pods_v( 'label_edit_item', $post_type, '', true );
489
				$cpt_labels['view']                  = pods_v( 'label_view', $post_type, '', true );
490
				$cpt_labels['view_item']             = pods_v( 'label_view_item', $post_type, '', true );
491
				$cpt_labels['view_items']            = pods_v( 'label_view_items', $post_type, '', true );
492
				$cpt_labels['all_items']             = pods_v( 'label_all_items', $post_type, '', true );
493
				$cpt_labels['search_items']          = pods_v( 'label_search_items', $post_type, '', true );
494
				$cpt_labels['not_found']             = pods_v( 'label_not_found', $post_type, '', true );
495
				$cpt_labels['not_found_in_trash']    = pods_v( 'label_not_found_in_trash', $post_type, '', true );
496
				$cpt_labels['parent']                = pods_v( 'label_parent', $post_type, '', true );
497
				$cpt_labels['parent_item_colon']     = pods_v( 'label_parent_item_colon', $post_type, '', true );
498
				$cpt_labels['archives']              = pods_v( 'label_archives', $post_type, '', true );
499
				$cpt_labels['attributes']            = pods_v( 'label_attributes', $post_type, '', true );
500
				$cpt_labels['insert_into_item']      = pods_v( 'label_insert_into_item', $post_type, '', true );
501
				$cpt_labels['uploaded_to_this_item'] = pods_v( 'label_uploaded_to_this_item', $post_type, '', true );
502
				$cpt_labels['featured_image']        = pods_v( 'label_featured_image', $post_type, '', true );
503
				$cpt_labels['set_featured_image']    = pods_v( 'label_set_featured_image', $post_type, '', true );
504
				$cpt_labels['remove_featured_image'] = pods_v( 'label_remove_featured_image', $post_type, '', true );
505
				$cpt_labels['use_featured_image']    = pods_v( 'label_use_featured_image', $post_type, '', true );
506
				$cpt_labels['filter_items_list']     = pods_v( 'label_filter_items_list', $post_type, '', true );
507
				$cpt_labels['items_list_navigation'] = pods_v( 'label_items_list_navigation', $post_type, '', true );
508
				$cpt_labels['items_list']            = pods_v( 'label_items_list', $post_type, '', true );
509
510
				// Supported
511
				$cpt_supported = array(
512
					'title'           => (boolean) pods_v( 'supports_title', $post_type, false ),
513
					'editor'          => (boolean) pods_v( 'supports_editor', $post_type, false ),
514
					'author'          => (boolean) pods_v( 'supports_author', $post_type, false ),
515
					'thumbnail'       => (boolean) pods_v( 'supports_thumbnail', $post_type, false ),
516
					'excerpt'         => (boolean) pods_v( 'supports_excerpt', $post_type, false ),
517
					'trackbacks'      => (boolean) pods_v( 'supports_trackbacks', $post_type, false ),
518
					'custom-fields'   => (boolean) pods_v( 'supports_custom_fields', $post_type, false ),
519
					'comments'        => (boolean) pods_v( 'supports_comments', $post_type, false ),
520
					'revisions'       => (boolean) pods_v( 'supports_revisions', $post_type, false ),
521
					'page-attributes' => (boolean) pods_v( 'supports_page_attributes', $post_type, false ),
522
					'post-formats'    => (boolean) pods_v( 'supports_post_formats', $post_type, false )
523
				);
524
525
				// Custom Supported
526
				$cpt_supported_custom = pods_v_sanitized( 'supports_custom', $post_type, '' );
527
528
				if ( ! empty( $cpt_supported_custom ) ) {
529
					$cpt_supported_custom = explode( ',', $cpt_supported_custom );
530
					$cpt_supported_custom = array_filter( array_unique( $cpt_supported_custom ) );
531
532
					foreach ( $cpt_supported_custom as $cpt_support ) {
533
						$cpt_supported[ $cpt_support ] = true;
534
					}
535
				}
536
537
				// Genesis Support
538
				if ( function_exists( 'genesis' ) ) {
539
					$cpt_supported['genesis-seo']             = (boolean) pods_var( 'supports_genesis_seo', $post_type, false );
540
					$cpt_supported['genesis-layouts']         = (boolean) pods_var( 'supports_genesis_layouts', $post_type, false );
541
					$cpt_supported['genesis-simple-sidebars'] = (boolean) pods_var( 'supports_genesis_simple_sidebars', $post_type, false );
542
				}
543
544
				// YARPP Support
545
				if ( defined( 'YARPP_VERSION' ) ) {
546
					$cpt_supported['yarpp_support'] = (boolean) pods_var( 'supports_yarpp_support', $post_type, false );
547
				}
548
549
				// Jetpack Support
550
				if ( class_exists( 'Jetpack' ) ) {
551
					$cpt_supported['supports_jetpack_publicize'] = (boolean) pods_var( 'supports_jetpack_publicize', $post_type, false );
552
					$cpt_supported['supports_jetpack_markdown']  = (boolean) pods_var( 'supports_jetpack_markdown', $post_type, false );
553
				}
554
555
				$cpt_supports = array();
556
557
				foreach ( $cpt_supported as $cpt_support => $supported ) {
558
					if ( true === $supported ) {
559
						$cpt_supports[] = $cpt_support;
560
561
						if ( 'post-formats' === $cpt_support ) {
562
							$post_format_post_types[] = $post_type_name;
563
						}
564
					}
565
				}
566
567
				if ( empty( $cpt_supports ) ) {
568
					$cpt_supports = false;
569
				}
570
571
				// Rewrite
572
				$cpt_rewrite       = (boolean) pods_var( 'rewrite', $post_type, true );
573
				$cpt_rewrite_array = array(
574
					'slug'       => pods_var( 'rewrite_custom_slug', $post_type, str_replace( '_', '-', $post_type_name ), null, true ),
575
					'with_front' => (boolean) pods_var( 'rewrite_with_front', $post_type, true ),
576
					'feeds'      => (boolean) pods_var( 'rewrite_feeds', $post_type, (boolean) pods_var( 'has_archive', $post_type, false ) ),
577
					'pages'      => (boolean) pods_var( 'rewrite_pages', $post_type, true )
578
				);
579
580
				if ( false !== $cpt_rewrite ) {
581
					$cpt_rewrite = $cpt_rewrite_array;
582
				}
583
584
				$capability_type = pods_var( 'capability_type', $post_type, 'post' );
585
586
				if ( 'custom' == $capability_type ) {
587
					$capability_type = pods_var( 'capability_type_custom', $post_type, 'post' );
588
				}
589
590
				$show_in_menu = (boolean) pods_var( 'show_in_menu', $post_type, true );
591
592
				if ( $show_in_menu && 0 < strlen( pods_var_raw( 'menu_location_custom', $post_type ) ) ) {
593
					$show_in_menu = pods_var_raw( 'menu_location_custom', $post_type );
594
				}
595
596
				$menu_icon = pods_var( 'menu_icon', $post_type, null, null, true );
597
598
				if ( ! empty( $menu_icon ) ) {
599
					$menu_icon = pods_evaluate_tags( $menu_icon );
600
				}
601
602
				// Register Post Type
603
				$pods_post_types[ $post_type_name ] = array(
604
					'label'               => $cpt_label,
605
					'labels'              => $cpt_labels,
606
					'description'         => esc_html( pods_var_raw( 'description', $post_type ) ),
607
					'public'              => (boolean) pods_var( 'public', $post_type, true ),
608
					'publicly_queryable'  => (boolean) pods_var( 'publicly_queryable', $post_type, (boolean) pods_var( 'public', $post_type, true ) ),
609
					'exclude_from_search' => (boolean) pods_var( 'exclude_from_search', $post_type, ( (boolean) pods_var( 'public', $post_type, true ) ? false : true ) ),
610
					'show_ui'             => (boolean) pods_var( 'show_ui', $post_type, (boolean) pods_var( 'public', $post_type, true ) ),
611
					'show_in_menu'        => $show_in_menu,
612
					'show_in_nav_menus'   => (boolean) pods_var( 'show_in_nav_menus', $post_type, (boolean) pods_var( 'public', $post_type, true ) ),
613
					'show_in_admin_bar'   => (boolean) pods_var( 'show_in_admin_bar', $post_type, (boolean) pods_var( 'show_in_menu', $post_type, true ) ),
614
					'menu_position'       => (int) pods_var( 'menu_position', $post_type, 0, null, true ),
615
					'menu_icon'           => $menu_icon,
616
					'capability_type'     => $capability_type,
617
					//'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...
618
					'map_meta_cap'        => (boolean) pods_var( 'capability_type_extra', $post_type, true ),
619
					'hierarchical'        => (boolean) pods_var( 'hierarchical', $post_type, false ),
620
					'supports'            => $cpt_supports,
621
					//'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...
622
					//'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...
623
					'has_archive'         => pods_v( 'has_archive_slug', $post_type, (boolean) pods_v( 'has_archive', $post_type, false ), true ),
624
					'rewrite'             => $cpt_rewrite,
625
					'query_var'           => ( false !== (boolean) pods_var( 'query_var', $post_type, true ) ? pods_var( 'query_var_string', $post_type, $post_type_name, null, true ) : false ),
626
					'can_export'          => (boolean) pods_var( 'can_export', $post_type, true )
627
				);
628
629
				// YARPP doesn't use 'supports' array option (yet)
630
				if ( ! empty( $cpt_supports['yarpp_support'] ) ) {
631
					$pods_post_types[ $post_type_name ]['yarpp_support'] = true;
632
				}
633
634
				// Prevent reserved query_var issues
635
				if ( in_array( $pods_post_types[ $post_type_name ]['query_var'], $reserved_query_vars ) ) {
636
					$pods_post_types[ $post_type_name ]['query_var'] = 'post_type_' . $pods_post_types[ $post_type_name ]['query_var'];
637
				}
638
639
				if ( 25 == $pods_post_types[ $post_type_name ]['menu_position'] ) {
640
					$pods_post_types[ $post_type_name ]['menu_position'] ++;
641
				}
642
643
				if ( $pods_post_types[ $post_type_name ]['menu_position'] < 1 || in_array( $pods_post_types[ $post_type_name ]['menu_position'], $cpt_positions ) ) {
644
					unset( $pods_post_types[ $post_type_name ]['menu_position'] );
645
				} else {
646
					$cpt_positions[] = $pods_post_types[ $post_type_name ]['menu_position'];
647
648
					// This would be nice if WP supported floats in menu_position
649
					// $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...
650
				}
651
652
				// Taxonomies
653
				$cpt_taxonomies = array();
654
				$_taxonomies    = get_taxonomies();
655
				$_taxonomies    = array_merge_recursive( $_taxonomies, $pods_taxonomies );
656
				$ignore         = array( 'nav_menu', 'link_category', 'post_format' );
657
658 View Code Duplication
				foreach ( $_taxonomies as $taxonomy => $label ) {
659
					if ( in_array( $taxonomy, $ignore ) ) {
660
						continue;
661
					}
662
663
					if ( false !== (boolean) pods_var( 'built_in_taxonomies_' . $taxonomy, $post_type, false ) ) {
664
						$cpt_taxonomies[] = $taxonomy;
665
666
						if ( isset( $supported_post_types[ $taxonomy ] ) && ! in_array( $post_type_name, $supported_post_types[ $taxonomy ] ) ) {
667
							$supported_post_types[ $taxonomy ][] = $post_type_name;
668
						}
669
					}
670
				}
671
672
				if ( isset( $supported_taxonomies[ $post_type_name ] ) ) {
673
					$supported_taxonomies[ $post_type_name ] = array_merge( (array) $supported_taxonomies[ $post_type_name ], $cpt_taxonomies );
674
				} else {
675
					$supported_taxonomies[ $post_type_name ] = $cpt_taxonomies;
676
				}
677
			}
678
679
			foreach ( $taxonomies as $taxonomy ) {
680
				// Taxonomy Type exists already
681 View Code Duplication
				if ( isset( $pods_cpt_ct['taxonomies'][ $taxonomy['name'] ] ) ) {
682
					continue;
683
				} elseif ( ! empty( $taxonomy['object'] ) && isset( $existing_taxonomies[ $taxonomy['object'] ] ) ) {
684
					continue;
685
				} elseif ( ! $force && isset( $existing_taxonomies[ $taxonomy['name'] ] ) ) {
686
					continue;
687
				}
688
689
				$taxonomy['options']['name'] = $taxonomy['name'];
690
				$taxonomy                    = array_merge( $taxonomy, (array) $taxonomy['options'] );
691
692
				$taxonomy_name = pods_var( 'name', $taxonomy );
693
694
				// Labels
695
				$ct_label    = esc_html( pods_v( 'label', $taxonomy, ucwords( str_replace( '_', ' ', pods_v( 'name', $taxonomy ) ) ), true ) );
696
				$ct_singular = esc_html( pods_v( 'label_singular', $taxonomy, ucwords( str_replace( '_', ' ', pods_v( 'label', $taxonomy, pods_v( 'name', $taxonomy ), null, true ) ) ), true ) );
697
698
				$ct_labels                               = array();
699
				$ct_labels['name']                       = $ct_label;
700
				$ct_labels['singular_name']              = $ct_singular;
701
				$ct_labels['menu_name']                  = pods_v( 'menu_name', $taxonomy, '', true );
702
				$ct_labels['search_items']               = pods_v( 'label_search_items', $taxonomy, '', true );
703
				$ct_labels['popular_items']              = pods_v( 'label_popular_items', $taxonomy, '', true );
704
				$ct_labels['all_items']                  = pods_v( 'label_all_items', $taxonomy, '', true );
705
				$ct_labels['parent_item']                = pods_v( 'label_parent_item', $taxonomy, '', true );
706
				$ct_labels['parent_item_colon']          = pods_v( 'label_parent_item_colon', $taxonomy, '', true );
707
				$ct_labels['edit_item']                  = pods_v( 'label_edit_item', $taxonomy, '', true );
708
				$ct_labels['update_item']                = pods_v( 'label_update_item', $taxonomy, '', true );
709
				$ct_labels['view_item']                  = pods_v( 'label_view_item', $taxonomy, '', true );
710
				$ct_labels['add_new_item']               = pods_v( 'label_add_new_item', $taxonomy, '', true );
711
				$ct_labels['new_item_name']              = pods_v( 'label_new_item_name', $taxonomy, '', true );
712
				$ct_labels['separate_items_with_commas'] = pods_v( 'label_separate_items_with_commas', $taxonomy, '', true );
713
				$ct_labels['add_or_remove_items']        = pods_v( 'label_add_or_remove_items', $taxonomy, '', true );
714
				$ct_labels['choose_from_most_used']      = pods_v( 'label_choose_from_the_most_used', $taxonomy, '', true );
715
				$ct_labels['not_found']                  = pods_v( 'label_not_found', $taxonomy, '', true );
716
				$ct_labels['no_terms']                   = pods_v( 'label_no_terms', $taxonomy, '', true );
717
				$ct_labels['items_list']                 = pods_v( 'label_items_list', $taxonomy, '', true );
718
				$ct_labels['items_list_navigation']      = pods_v( 'label_items_list_navigation', $taxonomy, '', true );
719
720
				// Rewrite
721
				$ct_rewrite       = (boolean) pods_var( 'rewrite', $taxonomy, true );
722
				$ct_rewrite_array = array(
723
					'slug'         => pods_var( 'rewrite_custom_slug', $taxonomy, str_replace( '_', '-', $taxonomy_name ), null, true ),
724
					'with_front'   => (boolean) pods_var( 'rewrite_with_front', $taxonomy, true ),
725
					'hierarchical' => (boolean) pods_var( 'rewrite_hierarchical', $taxonomy, (boolean) pods_var( 'hierarchical', $taxonomy, false ) )
726
				);
727
728
				if ( false !== $ct_rewrite ) {
729
					$ct_rewrite = $ct_rewrite_array;
730
				}
731
732
				/**
733
				 * Default tax capabilities
734
				 * @see https://codex.wordpress.org/Function_Reference/register_taxonomy
735
				 */
736
				$capability_type = pods_var( 'capability_type', $taxonomy, 'default' );
737
				$tax_capabilities = array();
738
739
				if ( 'custom' == $capability_type ) {
740
					$capability_type = pods_var( 'capability_type_custom', $taxonomy, 'default' );
741
					if ( ! empty( $capability_type ) && 'default' != $capability_type ) {
742
						$capability_type .=  '_term';
743
						$capability_type_plural =  $capability_type . 's';
744
						$tax_capabilities = array(
745
							// Singular
746
							'edit_term'   => 'edit_' . $capability_type,
747
							'delete_term' => 'delete_' . $capability_type,
748
							'assign_term' => 'assign_' . $capability_type,
749
							// Plural
750
							'manage_terms' => 'manage_' . $capability_type_plural,
751
							'edit_terms'   => 'edit_' . $capability_type_plural,
752
							'delete_terms' => 'delete_' . $capability_type_plural,
753
							'assign_terms' => 'assign_' . $capability_type_plural,
754
						);
755
					}
756
				}
757
758
				// Register Taxonomy
759
				$pods_taxonomies[ $taxonomy_name ] = array(
760
					'label'                 => $ct_label,
761
					'labels'                => $ct_labels,
762
					'public'                => (boolean) pods_var( 'public', $taxonomy, true ),
763
					'show_ui'               => (boolean) pods_var( 'show_ui', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ),
764
					'show_in_menu'          => (boolean) pods_var( 'show_in_menu', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ),
765
					'show_in_nav_menus'     => (boolean) pods_var( 'show_in_nav_menus', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ),
766
					'show_tagcloud'         => (boolean) pods_var( 'show_tagcloud', $taxonomy, (boolean) pods_var( 'show_ui', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ) ),
767
					'show_tagcloud_in_edit' => (boolean) pods_var( 'show_tagcloud_in_edit', $taxonomy, (boolean) pods_var( 'show_tagcloud', $taxonomy, (boolean) pods_var( 'show_ui', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ) ) ),
768
					'show_in_quick_edit'    => (boolean) pods_var( 'show_in_quick_edit', $taxonomy, (boolean) pods_var( 'show_ui', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ) ),
769
					'hierarchical'          => (boolean) pods_var( 'hierarchical', $taxonomy, false ),
770
					//'capability_type'       => $capability_type,
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...
771
					'capabilities'          => $tax_capabilities,
772
					//'map_meta_cap'          => (boolean) pods_var( 'capability_type_extra', $taxonomy, true ),
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
773
					'update_count_callback' => pods_var( 'update_count_callback', $taxonomy, null, null, true ),
774
					'query_var'             => ( false !== (boolean) pods_var( 'query_var', $taxonomy, true ) ? pods_var( 'query_var_string', $taxonomy, $taxonomy_name, null, true ) : false ),
775
					'rewrite'               => $ct_rewrite,
776
					'show_admin_column'     => (boolean) pods_var( 'show_admin_column', $taxonomy, false ),
777
					'sort'                  => (boolean) pods_var( 'sort', $taxonomy, false ),
778
				);
779
780
				if ( is_array( $ct_rewrite ) && ! $pods_taxonomies[ $taxonomy_name ]['query_var'] ) {
781
					$pods_taxonomies[ $taxonomy_name ]['query_var'] = pods_var( 'query_var_string', $taxonomy, $taxonomy_name, null, true );
782
				};
783
784
				// Prevent reserved query_var issues
785
				if ( in_array( $pods_taxonomies[ $taxonomy_name ]['query_var'], $reserved_query_vars ) ) {
786
					$pods_taxonomies[ $taxonomy_name ]['query_var'] = 'taxonomy_' . $pods_taxonomies[ $taxonomy_name ]['query_var'];
787
				}
788
789
				// Integration for Single Value Taxonomy UI
790
				if ( function_exists( 'tax_single_value_meta_box' ) ) {
791
					$pods_taxonomies[ $taxonomy_name ]['single_value'] = (boolean) pods_var( 'single_value', $taxonomy, false );
792
					$pods_taxonomies[ $taxonomy_name ]['required']     = (boolean) pods_var( 'single_value_required', $taxonomy, false );
793
				}
794
795
				// Post Types
796
				$ct_post_types = array();
797
				$_post_types   = get_post_types();
798
				$_post_types   = array_merge_recursive( $_post_types, $pods_post_types );
799
				$ignore        = array( 'revision' );
800
801 View Code Duplication
				foreach ( $_post_types as $post_type => $options ) {
802
					if ( in_array( $post_type, $ignore ) ) {
803
						continue;
804
					}
805
806
					if ( false !== (boolean) pods_var( 'built_in_post_types_' . $post_type, $taxonomy, false ) ) {
807
						$ct_post_types[] = $post_type;
808
809
						if ( isset( $supported_taxonomies[ $post_type ] ) && ! in_array( $taxonomy_name, $supported_taxonomies[ $post_type ] ) ) {
810
							$supported_taxonomies[ $post_type ][] = $taxonomy_name;
811
						}
812
					}
813
				}
814
815
				if ( isset( $supported_post_types[ $taxonomy_name ] ) ) {
816
					$supported_post_types[ $taxonomy_name ] = array_merge( $supported_post_types[ $taxonomy_name ], $ct_post_types );
817
				} else {
818
					$supported_post_types[ $taxonomy_name ] = $ct_post_types;
819
				}
820
			}
821
822
			$pods_post_types = apply_filters( 'pods_wp_post_types', $pods_post_types );
823
			$pods_taxonomies = apply_filters( 'pods_wp_taxonomies', $pods_taxonomies );
824
825
			$supported_post_types = apply_filters( 'pods_wp_supported_post_types', $supported_post_types );
826
			$supported_taxonomies = apply_filters( 'pods_wp_supported_taxonomies', $supported_taxonomies );
827
828
			foreach ( $pods_taxonomies as $taxonomy => $options ) {
829
				$ct_post_types = null;
830
831
				if ( isset( $supported_post_types[ $taxonomy ] ) && ! empty( $supported_post_types[ $taxonomy ] ) ) {
832
					$ct_post_types = $supported_post_types[ $taxonomy ];
833
				}
834
835
				$pods_cpt_ct['taxonomies'][ $taxonomy ] = array(
836
					'post_types' => $ct_post_types,
837
					'options'    => $options
838
				);
839
			}
840
841
			foreach ( $pods_post_types as $post_type => $options ) {
842
				if ( isset( $supported_taxonomies[ $post_type ] ) && ! empty( $supported_taxonomies[ $post_type ] ) ) {
843
					$options['taxonomies'] = $supported_taxonomies[ $post_type ];
844
				}
845
846
				$pods_cpt_ct['post_types'][ $post_type ] = $options;
847
			}
848
849
			$pods_cpt_ct['post_format_post_types'] = $post_format_post_types;
850
851
			pods_transient_set( 'pods_wp_cpt_ct', $pods_cpt_ct );
852
		}
853
854
		foreach ( $pods_cpt_ct['taxonomies'] as $taxonomy => $options ) {
855
			if ( isset( self::$content_types_registered['taxonomies'] ) && in_array( $taxonomy, self::$content_types_registered['taxonomies'] ) ) {
856
				continue;
857
			}
858
859
			$ct_post_types = $options['post_types'];
860
			$options       = $options['options'];
861
862
			$options = apply_filters( 'pods_register_taxonomy_' . $taxonomy, $options, $taxonomy );
863
			$options = apply_filters( 'pods_register_taxonomy', $options, $taxonomy );
864
865
			$options = self::object_label_fix( $options, 'taxonomy' );
866
867
			/**
868
			 * Hide tagcloud compatibility
869
			 * @todo check https://core.trac.wordpress.org/ticket/36964
870
			 * @see wp-admin/edit-tags.php L389
871
			 */
872
			if ( true != (boolean) pods_var( 'show_tagcloud_in_edit', $options, (boolean) pods_var( 'show_tagcloud', $options, true ) ) ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

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

Loading history...
873
				$options['labels']['popular_items'] = null;
874
			}
875
876
			// Max length for taxonomies are 32 characters
877
			$taxonomy = substr( $taxonomy, 0, 32 );
878
879
			// i18n compatibility for plugins that override it
880 View Code Duplication
			if ( is_array( $options['rewrite'] ) && isset( $options['rewrite']['slug'] ) && ! empty( $options['rewrite']['slug'] ) ) {
881
				$options['rewrite']['slug'] = _x( $options['rewrite']['slug'], 'URL taxonomy slug', 'pods' );
882
			}
883
884 View Code Duplication
			if ( 1 == pods_var( 'pods_debug_register', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) {
885
				pods_debug( array( $taxonomy, $ct_post_types, $options ) );
886
			}
887
888
			register_taxonomy( $taxonomy, $ct_post_types, $options );
889
890
			if ( ! isset( self::$content_types_registered['taxonomies'] ) ) {
891
				self::$content_types_registered['taxonomies'] = array();
892
			}
893
894
			self::$content_types_registered['taxonomies'][] = $taxonomy;
895
		}
896
897
		foreach ( $pods_cpt_ct['post_types'] as $post_type => $options ) {
898
			if ( isset( self::$content_types_registered['post_types'] ) && in_array( $post_type, self::$content_types_registered['post_types'] ) ) {
899
				continue;
900
			}
901
902
			$options = apply_filters( 'pods_register_post_type_' . $post_type, $options, $post_type );
903
			$options = apply_filters( 'pods_register_post_type', $options, $post_type );
904
905
			$options = self::object_label_fix( $options, 'post_type' );
906
907
			// Max length for post types are 20 characters
908
			$post_type = substr( $post_type, 0, 20 );
909
910
			// i18n compatibility for plugins that override it
911 View Code Duplication
			if ( is_array( $options['rewrite'] ) && isset( $options['rewrite']['slug'] ) && ! empty( $options['rewrite']['slug'] ) ) {
912
				$options['rewrite']['slug'] = _x( $options['rewrite']['slug'], 'URL slug', 'pods' );
913
			}
914
915 View Code Duplication
			if ( 1 == pods_var( 'pods_debug_register', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) {
916
				pods_debug( array( $post_type, $options ) );
917
			}
918
919
			register_post_type( $post_type, $options );
920
921
			// Register post format taxonomy for this post type
922
			if ( isset( $pods_cpt_ct['post_format_post_types'] ) && in_array( $post_type, $pods_cpt_ct['post_format_post_types'], true ) ) {
923
				register_taxonomy_for_object_type( 'post_format', $post_type );
924
			}
925
926
			if ( ! isset( self::$content_types_registered['post_types'] ) ) {
927
				self::$content_types_registered['post_types'] = array();
928
			}
929
930
			self::$content_types_registered['post_types'][] = $post_type;
931
		}
932
933
	}
934
935
	/**
936
	 * Check if we need to flush WordPress rewrite rules
937
	 * This gets run during 'init' action late in the game to give other plugins time to register their rewrite rules
938
	 */
939
	public function flush_rewrite_rules() {
940
941
		$flush = (int) pods_transient_get( 'pods_flush_rewrites' );
942
943
		if ( 1 === $flush ) {
944
			/**
945
			 * @var $wp_rewrite WP_Rewrite
946
			 */
947
			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...
948
			$wp_rewrite->flush_rules();
949
			$wp_rewrite->init();
950
951
			pods_transient_set( 'pods_flush_rewrites', 0 );
952
		}
953
	}
954
955
	/**
956
	 * Update Post Type messages
957
	 *
958
	 * @param array $messages
959
	 *
960
	 * @return array
961
	 * @since 2.0.2
962
	 */
963
	public function setup_updated_messages( $messages ) {
964
965
		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...
966
967
		$post_types          = PodsMeta::$post_types;
968
		$existing_post_types = get_post_types();
969
970
		$pods_cpt_ct = pods_transient_get( 'pods_wp_cpt_ct' );
971
972
		if ( empty( $pods_cpt_ct ) || empty( $post_types ) ) {
973
			return $messages;
974
		}
975
976
		/**
977
		 * Use get_preview_post_link function added in 4.4, which eventually applies preview_post_link filter
978
		 * Before 4.4, this filter is defined in wp-admin/includes/meta-boxes.php, $post parameter added in 4.0
979
		 * there wasn't post parameter back in 3.8
980
		 * Let's add $post in the filter as it won't hurt anyway.
981
		 * @since 2.6.8.1
982
		*/
983
		$preview_post_link = function_exists( 'get_preview_post_link' )
984
									? get_preview_post_link( $post )
985
									: apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ), $post );
986
987
		foreach ( $post_types as $post_type ) {
988
			if ( ! isset( $pods_cpt_ct['post_types'][ $post_type['name'] ] ) ) {
989
				continue;
990
			}
991
992
			$labels = self::object_label_fix( $pods_cpt_ct['post_types'][ $post_type['name'] ], 'post_type' );
993
			$labels = $labels['labels'];
994
995
			$messages[ $post_type['name'] ] = array(
996
				1  => sprintf( __( '%s updated. <a href="%s">%s</a>', 'pods' ), $labels['singular_name'], esc_url( get_permalink( $post_ID ) ), $labels['view_item'] ),
997
				2  => __( 'Custom field updated.', 'pods' ),
998
				3  => __( 'Custom field deleted.', 'pods' ),
999
				4  => sprintf( __( '%s updated.', 'pods' ), $labels['singular_name'] ),
1000
				/* translators: %s: date and time of the revision */
1001
				5  => isset( $_GET['revision'] ) ? sprintf( __( '%s restored to revision from %s', 'pods' ), $labels['singular_name'], wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
1002
				6  => sprintf( __( '%s published. <a href="%s">%s</a>', 'pods' ), $labels['singular_name'], esc_url( get_permalink( $post_ID ) ), $labels['view_item'] ),
1003
				7  => sprintf( __( '%s saved.', 'pods' ), $labels['singular_name'] ),
1004
				8  => sprintf( __( '%s submitted. <a target="_blank" href="%s">Preview %s</a>', 'pods' ), $labels['singular_name'], esc_url( $preview_post_link ), $labels['singular_name'] ),
1005
				9  => sprintf( __( '%s scheduled for: <strong>%s</strong>. <a target="_blank" href="%s">Preview %s</a>', 'pods' ), $labels['singular_name'], // translators: Publish box date format, see http://php.net/date
1006
					date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink( $post_ID ) ), $labels['singular_name'] ),
1007
				10 => sprintf( __( '%s draft updated. <a target="_blank" href="%s">Preview %s</a>', 'pods' ), $labels['singular_name'], esc_url( $preview_post_link ), $labels['singular_name'] )
1008
			);
1009
1010
			if ( false === (boolean) $pods_cpt_ct['post_types'][ $post_type['name'] ]['public'] ) {
1011
				$messages[ $post_type['name'] ][1]  = sprintf( __( '%s updated.', 'pods' ), $labels['singular_name'] );
1012
				$messages[ $post_type['name'] ][6]  = sprintf( __( '%s published.', 'pods' ), $labels['singular_name'] );
1013
				$messages[ $post_type['name'] ][8]  = sprintf( __( '%s submitted.', 'pods' ), $labels['singular_name'] );
1014
				$messages[ $post_type['name'] ][9]  = sprintf( __( '%s scheduled for: <strong>%1$s</strong>.', 'pods' ), $labels['singular_name'], // translators: Publish box date format, see http://php.net/date
1015
					date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ) );
1016
				$messages[ $post_type['name'] ][10] = sprintf( __( '%s draft updated.', 'pods' ), $labels['singular_name'] );
1017
			}
1018
		}
1019
1020
		return $messages;
1021
	}
1022
1023
	/**
1024
	 * @param        $args
1025
	 * @param string $type
1026
	 *
1027
	 * @return array
1028
	 */
1029
	public static function object_label_fix( $args, $type = 'post_type' ) {
1030
1031
		if ( empty( $args ) || ! is_array( $args ) ) {
1032
			$args = array();
1033
		}
1034
1035
		if ( ! isset( $args['labels'] ) || ! is_array( $args['labels'] ) ) {
1036
			$args['labels'] = array();
1037
		}
1038
1039
		$label          = pods_v( 'name', $args['labels'], pods_v( 'label', $args, __( 'Items', 'pods' ), true ), true );
1040
		$singular_label = pods_v( 'singular_name', $args['labels'], pods_v( 'label_singular', $args, __( 'Item', 'pods' ), true ), true );
1041
1042
		$labels = $args['labels'];
1043
1044
		$labels['name']          = $label;
1045
		$labels['singular_name'] = $singular_label;
1046
1047
		if ( 'post_type' == $type ) {
1048
			$labels['menu_name']             = pods_v( 'menu_name', $labels, $label, true );
1049
			$labels['name_admin_bar']        = pods_v( 'name_admin_bar', $labels, $singular_label, true );
1050
			$labels['add_new']               = pods_v( 'add_new', $labels, __( 'Add New', 'pods' ), true );
1051
			$labels['add_new_item']          = pods_v( 'add_new_item', $labels, sprintf( __( 'Add New %s', 'pods' ), $singular_label ), true );
1052
			$labels['new_item']              = pods_v( 'new_item', $labels, sprintf( __( 'New %s', 'pods' ), $singular_label ), true );
1053
			$labels['edit']                  = pods_v( 'edit', $labels, __( 'Edit', 'pods' ), true );
1054
			$labels['edit_item']             = pods_v( 'edit_item', $labels, sprintf( __( 'Edit %s', 'pods' ), $singular_label ), true );
1055
			$labels['view']                  = pods_v( 'view', $labels, sprintf( __( 'View %s', 'pods' ), $singular_label ), true );
1056
			$labels['view_item']             = pods_v( 'view_item', $labels, sprintf( __( 'View %s', 'pods' ), $singular_label ), true );
1057
			$labels['view_items']            = pods_v( 'view_items', $labels, sprintf( __( 'View %s', 'pods' ), $label ), true );
1058
			$labels['all_items']             = pods_v( 'all_items', $labels, sprintf( __( 'All %s', 'pods' ), $label ), true );
1059
			$labels['search_items']          = pods_v( 'search_items', $labels, sprintf( __( 'Search %s', 'pods' ), $label ), true );
1060
			$labels['not_found']             = pods_v( 'not_found', $labels, sprintf( __( 'No %s Found', 'pods' ), $label ), true );
1061
			$labels['not_found_in_trash']    = pods_v( 'not_found_in_trash', $labels, sprintf( __( 'No %s Found in Trash', 'pods' ), $label ), true );
1062
			$labels['parent']                = pods_v( 'parent', $labels, sprintf( __( 'Parent %s', 'pods' ), $singular_label ), true );
1063
			$labels['parent_item_colon']     = pods_v( 'parent_item_colon', $labels, sprintf( __( 'Parent %s:', 'pods' ), $singular_label ), true );
1064
			$labels['featured_image']        = pods_v( 'featured_image', $labels, __( 'Featured Image', 'pods' ), true );
1065
			$labels['set_featured_image']    = pods_v( 'set_featured_image', $labels, __( 'Set featured image', 'pods' ), true );
1066
			$labels['remove_featured_image'] = pods_v( 'remove_featured_image', $labels, __( 'Remove featured image', 'pods' ), true );
1067
			$labels['use_featured_image']    = pods_v( 'use_featured_image', $labels, __( 'Use as featured image', 'pods' ), true );
1068
			$labels['archives']              = pods_v( 'archives', $labels, sprintf( __( '%s Archives', 'pods' ), $singular_label ), true );
1069
			$labels['attributes']            = pods_v( 'attributes', $labels, sprintf( __( '%s Attributes', 'pods' ), $singular_label ), true );
1070
			$labels['insert_into_item']      = pods_v( 'insert_into_item', $labels, sprintf( __( 'Insert into %s', 'pods' ), $singular_label ), true );
1071
			$labels['uploaded_to_this_item'] = pods_v( 'uploaded_to_this_item', $labels, sprintf( __( 'Uploaded to this %s', 'pods' ), $singular_label ), true );
1072
			$labels['filter_items_list']     = pods_v( 'filter_items_list', $labels, sprintf( __( 'Filter %s lists', 'pods' ), $label ), true );
1073
			$labels['items_list_navigation'] = pods_v( 'items_list_navigation', $labels, sprintf( __( '%s navigation', 'pods' ), $label ), true );
1074
			$labels['items_list']            = pods_v( 'items_list', $labels, sprintf( __( '%s list', 'pods' ), $label ), true );
1075
		} elseif ( 'taxonomy' == $type ) {
1076
			$labels['menu_name']                  = pods_v( 'menu_name', $labels, $label, true );
1077
			$labels['search_items']               = pods_v( 'search_items', $labels, sprintf( __( 'Search %s', 'pods' ), $label ), true );
1078
			$labels['popular_items']              = pods_v( 'popular_items', $labels, sprintf( __( 'Popular %s', 'pods' ), $label ), true );
1079
			$labels['all_items']                  = pods_v( 'all_items', $labels, sprintf( __( 'All %s', 'pods' ), $label ), true );
1080
			$labels['parent_item']                = pods_v( 'parent_item', $labels, sprintf( __( 'Parent %s', 'pods' ), $singular_label ), true );
1081
			$labels['parent_item_colon']          = pods_v( 'parent_item_colon', $labels, sprintf( __( 'Parent %s :', 'pods' ), $singular_label ), true );
1082
			$labels['edit_item']                  = pods_v( 'edit_item', $labels, sprintf( __( 'Edit %s', 'pods' ), $singular_label ), true );
1083
			$labels['view_item']                  = pods_v( 'view_item', $labels, sprintf( __( 'View %s', 'pods' ), $singular_label ), true );
1084
			$labels['update_item']                = pods_v( 'update_item', $labels, sprintf( __( 'Update %s', 'pods' ), $singular_label ), true );
1085
			$labels['add_new_item']               = pods_v( 'add_new_item', $labels, sprintf( __( 'Add New %s', 'pods' ), $singular_label ), true );
1086
			$labels['new_item_name']              = pods_v( 'new_item_name', $labels, sprintf( __( 'New %s Name', 'pods' ), $singular_label ), true );
1087
			$labels['separate_items_with_commas'] = pods_v( 'separate_items_with_commas', $labels, sprintf( __( 'Separate %s with commas', 'pods' ), $label ), true );
1088
			$labels['add_or_remove_items']        = pods_v( 'add_or_remove_items', $labels, sprintf( __( 'Add or remove %s', 'pods' ), $label ), true );
1089
			$labels['choose_from_most_used']      = pods_v( 'choose_from_most_used', $labels, sprintf( __( 'Choose from the most used %s', 'pods' ), $label ), true );
1090
			$labels['not_found']                  = pods_v( 'not_found', $labels, sprintf( __( 'No %s found.', 'pods' ), $label ), true );
1091
			$labels['no_terms']                   = pods_v( 'no_terms', $labels, sprintf( __( 'No %s', 'pods' ), $label ), true );
1092
			$labels['items_list_navigation']      = pods_v( 'items_list_navigation', $labels, sprintf( __( '%s navigation', 'pods' ), $label ), true );
1093
			$labels['items_list']                 = pods_v( 'items_list', $labels, sprintf( __( '%s list', 'pods' ), $label ), true );
1094
		}
1095
1096
		$args['labels'] = $labels;
1097
1098
		return $args;
1099
	}
1100
1101
	/**
1102
	 * Activate and Install
1103
	 */
1104
	public function activate_install() {
1105
1106
		register_activation_hook( PODS_DIR . 'init.php', array( $this, 'activate' ) );
1107
		register_deactivation_hook( PODS_DIR . 'init.php', array( $this, 'deactivate' ) );
1108
1109
		add_action( 'wpmu_new_blog', array( $this, 'new_blog' ), 10, 6 );
1110
1111
		if ( empty( self::$version ) || version_compare( self::$version, PODS_VERSION, '<' ) || version_compare( self::$version, PODS_DB_VERSION, '<=' ) || self::$upgrade_needed ) {
1112
			$this->setup();
1113
		} elseif ( self::$version !== PODS_VERSION ) {
1114
			delete_option( 'pods_framework_version' );
1115
			add_option( 'pods_framework_version', PODS_VERSION, '', 'yes' );
1116
1117
			self::$version = PODS_VERSION;
1118
1119
			pods_api()->cache_flush_pods();
1120
		}
1121
1122
	}
1123
1124
	/**
1125
	 *
1126
	 */
1127
	public function activate() {
1128
1129
		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...
1130
1131
		if ( function_exists( 'is_multisite' ) && is_multisite() && isset( $_GET['networkwide'] ) && 1 == $_GET['networkwide'] ) {
1132
			$_blog_ids = $wpdb->get_col( "SELECT `blog_id` FROM `{$wpdb->blogs}`" );
1133
1134
			foreach ( $_blog_ids as $_blog_id ) {
1135
				$this->setup( $_blog_id );
1136
			}
1137
		} else {
1138
			$this->setup();
1139
		}
1140
	}
1141
1142
	/**
1143
	 *
1144
	 */
1145
	public function deactivate() {
1146
1147
		pods_api()->cache_flush_pods();
1148
1149
	}
1150
1151
	/**
1152
	 *
1153
	 */
1154
	public function needs_upgrade( $current = null, $last = null ) {
1155
1156
		if ( null === $current ) {
1157
			$current = self::$version;
1158
		}
1159
1160
		if ( null === $last ) {
1161
			$last = self::$version_last;
1162
		}
1163
1164
		$upgrade_needed = false;
1165
1166
		if ( ! empty( $current ) ) {
1167
			foreach ( self::$upgrades as $old_version => $new_version ) {
1168
				/*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...
1169
					continue;*/
1170
1171
				if ( version_compare( $last, $old_version, '>=' ) && version_compare( $last, $new_version, '<' ) && version_compare( $current, $new_version, '>=' ) && 1 != self::$upgraded ) {
1172
					$upgrade_needed = true;
1173
1174
					break;
1175
				}
1176
			}
1177
		}
1178
1179
		return $upgrade_needed;
1180
	}
1181
1182
	/**
1183
	 * @param $_blog_id
1184
	 * @param $user_id
1185
	 * @param $domain
1186
	 * @param $path
1187
	 * @param $site_id
1188
	 * @param $meta
1189
	 */
1190
	public function new_blog( $_blog_id, $user_id, $domain, $path, $site_id, $meta ) {
1191
1192
		if ( function_exists( 'is_multisite' ) && is_multisite() && is_plugin_active_for_network( basename( PODS_DIR ) . '/init.php' ) ) {
1193
			$this->setup( $_blog_id );
1194
		}
1195
	}
1196
1197
	/**
1198
	 * @param null $_blog_id
1199
	 */
1200
	public function setup( $_blog_id = null ) {
1201
1202
		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...
1203
1204
		// Switch DB table prefixes
1205 View Code Duplication
		if ( null !== $_blog_id && $_blog_id != $wpdb->blogid ) {
1206
			switch_to_blog( pods_absint( $_blog_id ) );
1207
		} else {
1208
			$_blog_id = null;
1209
		}
1210
1211
		// Setup DB tables
1212
		$pods_version      = get_option( 'pods_framework_version' );
1213
		$pods_version_last = get_option( 'pods_framework_version_last' );
1214
1215
		// Install Pods
1216
		if ( empty( $pods_version ) ) {
1217
			pods_upgrade()->install( $_blog_id );
1218
1219
			$old_version = get_option( 'pods_version' );
1220
1221
			if ( ! empty( $old_version ) ) {
1222
				if ( false === strpos( $old_version, '.' ) ) {
1223
					$old_version = pods_version_to_point( $old_version );
1224
				}
1225
1226
				delete_option( 'pods_framework_version_last' );
1227
				add_option( 'pods_framework_version_last', $pods_version, '', 'yes' );
1228
1229
				self::$version_last = $old_version;
1230
			}
1231
		} // Upgrade Wizard needed
1232
		elseif ( $this->needs_upgrade( $pods_version, $pods_version_last ) ) {
1233
			// Do not do anything
1234
			return;
1235
		} // Update Pods and run any required DB updates
1236
		elseif ( version_compare( $pods_version, PODS_VERSION, '<=' ) ) {
1237
			if ( false !== apply_filters( 'pods_update_run', null, PODS_VERSION, $pods_version, $_blog_id ) && ! isset( $_GET['pods_bypass_update'] ) ) {
1238
				do_action( 'pods_update', PODS_VERSION, $pods_version, $_blog_id );
1239
1240
				// Update 2.0 alpha / beta sites
1241
				if ( version_compare( '2.0.0-a-1', $pods_version, '<=' ) && version_compare( $pods_version, '2.0.0-b-15', '<=' ) ) {
1242
					include( PODS_DIR . 'sql/update-2.0-beta.php' );
1243
				}
1244
1245
				if ( version_compare( $pods_version, PODS_DB_VERSION, '<=' ) ) {
1246
					include( PODS_DIR . 'sql/update.php' );
1247
				}
1248
1249
				do_action( 'pods_update_post', PODS_VERSION, $pods_version, $_blog_id );
1250
			}
1251
1252
			delete_option( 'pods_framework_version_last' );
1253
			add_option( 'pods_framework_version_last', $pods_version, '', 'yes' );
1254
1255
			self::$version_last = $pods_version;
1256
		}
1257
1258
		delete_option( 'pods_framework_version' );
1259
		add_option( 'pods_framework_version', PODS_VERSION, '', 'yes' );
1260
1261
		delete_option( 'pods_framework_db_version' );
1262
		add_option( 'pods_framework_db_version', PODS_DB_VERSION, '', 'yes' );
1263
1264
		self::$version = PODS_VERSION;
1265
		self::$db_version = PODS_DB_VERSION;
1266
1267
		pods_api()->cache_flush_pods();
1268
1269
		// Restore DB table prefix (if switched)
1270
		if ( null !== $_blog_id ) {
1271
			restore_current_blog();
1272
		}
1273
1274
	}
1275
1276
	/**
1277
	 * @param null $_blog_id
1278
	 */
1279
	public function reset( $_blog_id = null ) {
1280
1281
		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...
1282
1283
		// Switch DB table prefixes
1284 View Code Duplication
		if ( null !== $_blog_id && $_blog_id != $wpdb->blogid ) {
1285
			switch_to_blog( pods_absint( $_blog_id ) );
1286
		} else {
1287
			$_blog_id = null;
1288
		}
1289
1290
		$api = pods_api();
1291
1292
		$pods = $api->load_pods( array( 'names_ids' => true, 'table_info' => false ) );
1293
1294
		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...
1295
			$api->delete_pod( array( 'id' => $pod_id ) );
1296
		}
1297
1298
		$templates = $api->load_templates();
1299
1300
		foreach ( $templates as $template ) {
1301
			$api->delete_template( array( 'id' => $template['id'] ) );
1302
		}
1303
1304
		$pages = $api->load_pages();
1305
1306
		foreach ( $pages as $page ) {
1307
			$api->delete_page( array( 'id' => $page['id'] ) );
1308
		}
1309
1310
		$helpers = $api->load_helpers();
1311
1312
		foreach ( $helpers as $helper ) {
1313
			$api->delete_helper( array( 'id' => $helper['id'] ) );
1314
		}
1315
1316
		$tables = $wpdb->get_results( "SHOW TABLES LIKE '{$wpdb->prefix}pods%'", ARRAY_N );
1317
1318
		if ( ! empty( $tables ) ) {
1319
			foreach ( $tables as $table ) {
1320
				$table = $table[0];
1321
1322
				pods_query( "DROP TABLE `{$table}`", false );
1323
			}
1324
		}
1325
1326
		// Remove any orphans
1327
		$wpdb->query( "
1328
                DELETE `p`, `pm`
1329
                FROM `{$wpdb->posts}` AS `p`
1330
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1331
                    ON `pm`.`post_id` = `p`.`ID`
1332
                WHERE
1333
                    `p`.`post_type` LIKE '_pods_%'
1334
            " );
1335
1336
		delete_option( 'pods_framework_version' );
1337
		delete_option( 'pods_framework_db_version' );
1338
		delete_option( 'pods_framework_upgrade_2_0' );
1339
		delete_option( 'pods_framework_upgraded_1_x' );
1340
1341
		// @todo Make sure all entries are being cleaned and do something about the pods_framework_upgrade_{version} dynamic entries created by PodsUpgrade
1342
		delete_option( 'pods_framework_upgrade_2_0_0' );
1343
		delete_option( 'pods_framework_upgrade_2_0_sister_ids' );
1344
		delete_option( 'pods_framework_version_last' );
1345
1346
		delete_option( 'pods_component_settings' );
1347
1348
		$api->cache_flush_pods();
1349
1350
		pods_transient_clear( 'pods_flush_rewrites' );
1351
1352
		self::$version = '';
1353
1354
		// Restore DB table prefix (if switched)
1355
		if ( null !== $_blog_id ) {
1356
			restore_current_blog();
1357
		}
1358
	}
1359
1360
	public function run() {
1361
1362
		static $ran;
1363
1364
		if ( ! empty( $ran ) ) {
1365
			return;
1366
		}
1367
1368
		$ran = true;
1369
1370
		$this->load_i18n();
1371
1372
		if ( ! did_action( 'plugins_loaded' ) ) {
1373
			add_action( 'plugins_loaded', array( $this, 'load_components' ), 11 );
1374
		} else {
1375
			$this->load_components();
1376
		}
1377
1378
		if ( ! did_action( 'setup_theme' ) ) {
1379
			add_action( 'setup_theme', array( $this, 'load_meta' ), 14 );
1380
		} else {
1381
			$this->load_meta();
1382
		}
1383
1384
		if ( ! did_action( 'init' ) ) {
1385
			add_action( 'init', array( $this, 'core' ), 11 );
1386
			add_action( 'init', array( $this, 'add_rest_support' ), 12 );
1387
			add_action( 'init', array( $this, 'setup_content_types' ), 11 );
1388
1389
			if ( is_admin() ) {
1390
				add_action( 'init', array( $this, 'admin_init' ), 12 );
1391
			}
1392
		} else {
1393
			$this->core();
1394
			$this->add_rest_support();
1395
			$this->setup_content_types();
1396
1397
			if ( is_admin() ) {
1398
				$this->admin_init();
1399
			}
1400
		}
1401
1402
		add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1403
		add_action( 'admin_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1404
		add_action( 'login_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1405
1406
		add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 );
1407
		add_action( 'delete_attachment', array( $this, 'delete_attachment' ) );
1408
1409
		// Register widgets
1410
		add_action( 'widgets_init', array( $this, 'register_widgets' ) );
1411
1412
		// Show admin bar links
1413
		add_action( 'admin_bar_menu', array( $this, 'admin_bar_links' ), 81 );
1414
1415
	}
1416
1417
	/**
1418
	 * Delete Attachments from relationships
1419
	 *
1420
	 * @param int $_ID
1421
	 */
1422
	public function delete_attachment( $_ID ) {
1423
1424
		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...
1425
1426
		$_ID = (int) $_ID;
1427
1428
		do_action( 'pods_delete_attachment', $_ID );
1429
1430
		$file_types = "'" . implode( "', '", PodsForm::file_field_types() ) . "'";
1431
1432
		if ( ! pods_tableless() ) {
1433
			$sql = "
1434
                DELETE `rel`
1435
                FROM `@wp_podsrel` AS `rel`
1436
                LEFT JOIN `{$wpdb->posts}` AS `p`
1437
                    ON
1438
                        `p`.`post_type` = '_pods_field'
1439
                        AND ( `p`.`ID` = `rel`.`field_id` OR `p`.`ID` = `rel`.`related_field_id` )
1440
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1441
                    ON
1442
                        `pm`.`post_id` = `p`.`ID`
1443
                        AND `pm`.`meta_key` = 'type'
1444
                        AND `pm`.`meta_value` IN ( {$file_types} )
1445
                WHERE
1446
                    `p`.`ID` IS NOT NULL
1447
                    AND `pm`.`meta_id` IS NOT NULL
1448
                    AND `rel`.`item_id` = {$_ID}";
1449
1450
			pods_query( $sql, false );
1451
		}
1452
1453
		// Post Meta
1454 View Code Duplication
		if ( ! empty( PodsMeta::$post_types ) ) {
1455
			$sql = "
1456
                DELETE `rel`
1457
                FROM `@wp_postmeta` AS `rel`
1458
                LEFT JOIN `{$wpdb->posts}` AS `p`
1459
                    ON
1460
                        `p`.`post_type` = '_pods_field'
1461
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1462
                    ON
1463
                        `pm`.`post_id` = `p`.`ID`
1464
                        AND `pm`.`meta_key` = 'type'
1465
                        AND `pm`.`meta_value` IN ( {$file_types} )
1466
                WHERE
1467
                    `p`.`ID` IS NOT NULL
1468
                    AND `pm`.`meta_id` IS NOT NULL
1469
                    AND `rel`.`meta_key` = `p`.`post_name`
1470
                    AND `rel`.`meta_value` = '{$_ID}'";
1471
1472
			pods_query( $sql, false );
1473
		}
1474
1475
		// User Meta
1476 View Code Duplication
		if ( ! empty( PodsMeta::$user ) ) {
1477
			$sql = "
1478
                DELETE `rel`
1479
                FROM `@wp_usermeta` AS `rel`
1480
                LEFT JOIN `{$wpdb->posts}` AS `p`
1481
                    ON
1482
                        `p`.`post_type` = '_pods_field'
1483
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1484
                    ON
1485
                        `pm`.`post_id` = `p`.`ID`
1486
                        AND `pm`.`meta_key` = 'type'
1487
                        AND `pm`.`meta_value` IN ( {$file_types} )
1488
                WHERE
1489
                    `p`.`ID` IS NOT NULL
1490
                    AND `pm`.`meta_id` IS NOT NULL
1491
                    AND `rel`.`meta_key` = `p`.`post_name`
1492
                    AND `rel`.`meta_value` = '{$_ID}'";
1493
1494
			pods_query( $sql, false );
1495
		}
1496
1497
		// Comment Meta
1498 View Code Duplication
		if ( ! empty( PodsMeta::$comment ) ) {
1499
			$sql = "
1500
                DELETE `rel`
1501
                FROM `@wp_commentmeta` AS `rel`
1502
                LEFT JOIN `{$wpdb->posts}` AS `p`
1503
                    ON
1504
                        `p`.`post_type` = '_pods_field'
1505
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1506
                    ON
1507
                        `pm`.`post_id` = `p`.`ID`
1508
                        AND `pm`.`meta_key` = 'type'
1509
                        AND `pm`.`meta_value` IN ( {$file_types} )
1510
                WHERE
1511
                    `p`.`ID` IS NOT NULL
1512
                    AND `pm`.`meta_id` IS NOT NULL
1513
                    AND `rel`.`meta_key` = `p`.`post_name`
1514
                    AND `rel`.`meta_value` = '{$_ID}'";
1515
1516
			pods_query( $sql, false );
1517
		}
1518
	}
1519
1520
	/**
1521
	 * Register widgets for Pods
1522
	 */
1523
	public function register_widgets() {
1524
1525
		$widgets = array(
1526
			'PodsWidgetSingle',
1527
			'PodsWidgetList',
1528
			'PodsWidgetField',
1529
			'PodsWidgetForm',
1530
			'PodsWidgetView'
1531
		);
1532
1533
		foreach ( $widgets as $widget ) {
1534
			if ( ! file_exists( PODS_DIR . 'classes/widgets/' . $widget . '.php' ) ) {
1535
				continue;
1536
			}
1537
1538
			require_once PODS_DIR . 'classes/widgets/' . $widget . '.php';
1539
1540
			register_widget( $widget );
1541
		}
1542
	}
1543
1544
	/**
1545
	 * Add Admin Bar links
1546
	 */
1547
	public function admin_bar_links() {
1548
1549
		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...
1550
1551
		if ( ! is_user_logged_in() || ! is_admin_bar_showing() ) {
1552
			return;
1553
		}
1554
1555
		$all_pods = pods_api()->load_pods( array( 'type' => 'pod', 'fields' => false, 'table_info' => false ) );
1556
1557
		// Add New item links for all pods
1558
		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...
1559
			if ( 0 == $pod['options']['show_in_menu'] ) {
1560
				continue;
1561
			}
1562
1563
			if ( ! pods_is_admin( array( 'pods', 'pods_content', 'pods_add_' . $pod['name'] ) ) ) {
1564
				continue;
1565
			}
1566
1567
			$singular_label = pods_var_raw( 'label_singular', $pod['options'], pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod['name'] ) ), null, true ), null, true );
1568
1569
			$wp_admin_bar->add_node( array(
1570
				'id'     => 'new-pod-' . $pod['name'],
1571
				'title'  => $singular_label,
1572
				'parent' => 'new-content',
1573
				'href'   => admin_url( 'admin.php?page=pods-manage-' . $pod['name'] . '&action=add' )
1574
			) );
1575
		}
1576
1577
		// Add edit link if we're on a pods page
1578
		if ( is_object( $pods ) && ! is_wp_error( $pods ) && ! empty( $pods->id ) && isset( $pods->pod_data ) && ! empty( $pods->pod_data ) && 'pod' == $pods->pod_data['type'] ) {
1579
			$pod = $pods->pod_data;
1580
1581
			if ( pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $pod['name'] ) ) ) {
1582
				$singular_label = pods_var_raw( 'label_singular', $pod['options'], pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod['name'] ) ), null, true ), null, true );
1583
1584
				$wp_admin_bar->add_node( array(
1585
					'title' => sprintf( __( 'Edit %s', 'pods' ), $singular_label ),
1586
					'id'    => 'edit-pod',
1587
					'href'  => admin_url( 'admin.php?page=pods-manage-' . $pod['name'] . '&action=edit&id=' . $pods->id() )
1588
				) );
1589
			}
1590
		}
1591
1592
	}
1593
1594
	/**
1595
	 * Add REST API support to post type and taxonomy objects.
1596
	 *
1597
	 * @uses  "init"
1598
	 *
1599
	 * @since 2.5.6
1600
	 */
1601
	public function add_rest_support() {
1602
1603
		if ( empty( self::$version ) ) {
1604
			return;
1605
		}
1606
1607
		static $rest_support_added;
1608
1609
		if ( ! function_exists( 'register_rest_field' ) ) {
1610
			return;
1611
		}
1612
1613
		include_once( PODS_DIR . 'classes/PodsRESTFields.php' );
1614
		include_once( PODS_DIR . 'classes/PodsRESTHandlers.php' );
1615
1616
		$rest_bases = pods_transient_get( 'pods_rest_bases' );
1617
1618
		if ( empty( $rest_bases ) ) {
1619
	        $pods = pods_api()->load_pods( array( 'type' => array( 'post_type', 'taxonomy', 'user', 'media', 'comment' ), 'fields' => false, 'table_info' => false ) );
1620
1621
			$rest_bases = array();
1622
1623
			if ( ! empty( $pods ) && is_array( $pods ) ) {
1624
				foreach ( $pods as $pod ) {
1625
					$type = $pod['type'];
1626
1627
					if ( in_array( $type, array( 'post_type', 'taxonomy', 'user', 'media', 'comment' ) ) ) {
1628
						if ( $pod && PodsRESTHandlers::pod_extends_core_route( $pod ) ) {
1629
							$rest_bases[ $pod['name'] ] = array(
1630
								'type' => $type,
1631
								'base' => sanitize_title( pods_v( 'rest_base', $pod['options'], $pod['name'] ) ),
1632
							);
1633
						}
1634
					}
1635
				}
1636
			}
1637
1638
			if ( empty( $rest_bases ) ) {
1639
				$rest_bases = 'none';
1640
			}
1641
1642
			pods_transient_set( 'pods_rest_bases', $rest_bases );
1643
		}
1644
1645
		if ( empty( $rest_support_added ) && ! empty( $rest_bases ) && 'none' !== $rest_bases ) {
1646
			foreach ( $rest_bases as $pod_name => $pod_info ) {
1647
				$pod_type  = $pod_info['type'];
1648
				$rest_base = $pod_info['base'];
1649
1650
				if ( 'post_type' == $pod_type ) {
1651
					PodsRESTHandlers::post_type_rest_support( $pod_name, $rest_base );
1652
				} elseif ( 'taxonomy' == $pod_type ) {
1653
					PodsRESTHandlers::taxonomy_rest_support( $pod_name, $rest_base );
1654
				}
1655
1656
				new PodsRESTFields( $pod_name );
1657
			}
1658
1659
			$rest_support_added = true;
1660
		}
1661
1662
	}
1663
}
1664