Completed
Pull Request — 2.x (#4093)
by Tuan
07:07 queued 02:40
created

PodsInit   D

Complexity

Total Complexity 243

Size/Duplication

Total Lines 1566
Duplicated Lines 7.02 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

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

25 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
B __construct() 0 31 5
A plugins_loaded() 0 13 3
A load_components() 0 11 4
A load_meta() 0 4 1
C core() 0 59 12
B register_assets() 0 57 6
B register_pods() 0 38 1
A admin_init() 0 4 1
F setup_content_types() 42 500 96
A flush_rewrite_rules() 0 15 2
B setup_updated_messages() 0 58 8
B object_label_fix() 0 66 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 54 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 PodsAdmin
34
	 */
35
	static $admin;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $admin.

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

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
36
37
	/**
38
	 * @var mixed|void
39
	 */
40
	static $version;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $version.

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

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
41
42
	/**
43
	 * @var mixed|void
44
	 */
45
	static $version_last;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $version_last.

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

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
46
47
	/**
48
	 * @var mixed|void
49
	 */
50
	static $db_version;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $db_version.

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

class A {
    var $property;
}

the property is implicitly global.

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

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

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

class A {
    var $property;
}

the property is implicitly global.

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

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

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

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
68
69
	/**
70
	 * Whether an Upgrade is needed
71
	 *
72
	 * @var bool
73
	 */
74
	static $upgrade_needed = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $upgrade_needed.

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

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
75
76
	/**
77
	 * Singleton handling for a basic pods_init() request
78
	 *
79
	 * @return \PodsInit
80
	 *
81
	 * @since 2.3.5
82
	 */
83
	public static function init() {
84
85
		if ( ! is_object( self::$instance ) ) {
86
			self::$instance = new PodsInit();
87
		}
88
89
		return self::$instance;
90
	}
91
92
	/**
93
	 * Setup and Initiate Pods
94
	 *
95
	 * @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...
96
	 *
97
	 * @license http://www.gnu.org/licenses/gpl-2.0.html
98
	 * @since   1.8.9
99
	 */
100
	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...
101
102
		self::$version      = get_option( 'pods_framework_version' );
103
		self::$version_last = get_option( 'pods_framework_version_last' );
104
		self::$db_version   = get_option( 'pods_framework_db_version' );
105
		self::$upgraded     = (int) get_option( 'pods_framework_upgraded_1_x' );
106
107
		if ( empty( self::$version_last ) && 0 < strlen( get_option( 'pods_version' ) ) ) {
108
			$old_version = get_option( 'pods_version' );
109
110
			if ( ! empty( $old_version ) ) {
111
				if ( false === strpos( $old_version, '.' ) ) {
112
					$old_version = pods_version_to_point( $old_version );
113
				}
114
115
				update_option( 'pods_framework_version_last', $old_version );
116
117
				self::$version_last = $old_version;
118
			}
119
		}
120
121
		self::$upgrade_needed = $this->needs_upgrade();
122
123
		add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
124
		add_action( 'plugins_loaded', array( $this, 'activate_install' ), 9 );
125
126
		add_action( 'wp_loaded', array( $this, 'flush_rewrite_rules' ) );
127
128
		$this->run();
129
130
	}
131
132
	/**
133
	 * Load the plugin textdomain and set default constants
134
	 */
135
	public function plugins_loaded() {
136
137
		if ( ! defined( 'PODS_LIGHT' ) ) {
138
			define( 'PODS_LIGHT', false );
139
		}
140
141
		if ( ! defined( 'PODS_TABLELESS' ) ) {
142
			define( 'PODS_TABLELESS', false );
143
		}
144
145
		load_plugin_textdomain( 'pods' );
146
147
	}
148
149
	/**
150
	 * Load Pods Components
151
	 */
152
	public function load_components() {
153
154
		if ( empty( self::$version ) ) {
155
			return;
156
		}
157
158
		if ( ! defined( 'PODS_LIGHT' ) || ! PODS_LIGHT ) {
159
			self::$components = pods_components();
160
		}
161
162
	}
163
164
	/**
165
	 * Load Pods Meta
166
	 */
167
	public function load_meta() {
168
169
		self::$meta = pods_meta()->core();
170
	}
171
172
	/**
173
	 * Set up the Pods core
174
	 */
175
	public function core() {
176
177
		if ( empty( self::$version ) ) {
178
			return;
179
		}
180
181
		// Session start
182
		pods_session_start();
183
184
		add_shortcode( 'pods', 'pods_shortcode' );
185
		add_shortcode( 'pods-form', 'pods_shortcode_form' );
186
187
		$security_settings = array(
188
			'pods_disable_file_browser'     => 0,
189
			'pods_files_require_login'      => 1,
190
			'pods_files_require_login_cap'  => '',
191
			'pods_disable_file_upload'      => 0,
192
			'pods_upload_require_login'     => 1,
193
			'pods_upload_require_login_cap' => ''
194
		);
195
196
		foreach ( $security_settings as $security_setting => $setting ) {
197
			$setting = get_option( $security_setting );
198
			if ( ! empty( $setting ) ) {
199
				$security_settings[ $security_setting ] = $setting;
200
			}
201
		}
202
203
		foreach ( $security_settings as $security_setting => $setting ) {
204
			if ( 0 == $setting ) {
205
				$setting = false;
206
			} elseif ( 1 == $setting ) {
207
				$setting = true;
208
			}
209
210
			if ( in_array( $security_setting, array( 'pods_files_require_login', 'pods_upload_require_login' ) ) ) {
211
				if ( 0 < strlen( $security_settings[ $security_setting . '_cap' ] ) ) {
212
					$setting = $security_settings[ $security_setting . '_cap' ];
213
				}
214
			} elseif ( in_array( $security_setting, array(
215
				'pods_files_require_login_cap',
216
				'pods_upload_require_login_cap'
217
			) ) ) {
218
				continue;
219
			}
220
221
			if ( ! defined( strtoupper( $security_setting ) ) ) {
222
				define( strtoupper( $security_setting ), $setting );
223
			}
224
		}
225
226
		$this->register_pods();
227
228
		$avatar = PodsForm::field_loader( 'avatar' );
229
230
		if ( method_exists( $avatar, 'get_avatar' ) ) {
231
			add_filter( 'get_avatar', array( $avatar, 'get_avatar' ), 10, 4 );
232
		}
233
	}
234
235
	/**
236
	 * Register Scripts and Styles
237
	 */
238
	public function register_assets() {
239
240
		if ( ! wp_style_is( 'jquery-ui', 'registered' ) ) {
241
			wp_register_style( 'jquery-ui', PODS_URL . 'ui/css/smoothness/jquery-ui.custom.css', array(), '1.8.16' );
242
		}
243
244
		wp_register_script( 'pods-json', PODS_URL . 'ui/js/jquery.json.js', array( 'jquery' ), '2.3' );
245
246
		if ( ! wp_style_is( 'jquery-qtip2', 'registered' ) ) {
247
			wp_register_style( 'jquery-qtip2', PODS_URL . 'ui/css/jquery.qtip.min.css', array(), '2.2' );
248
		}
249
250
		if ( ! wp_script_is( 'jquery-qtip2', 'registered' ) ) {
251
			wp_register_script( 'jquery-qtip2', PODS_URL . 'ui/js/jquery.qtip.min.js', array( 'jquery' ), '2.2' );
252
		}
253
254
		wp_register_script( 'pods', PODS_URL . 'ui/js/jquery.pods.js', array(
255
			'jquery',
256
			'pods-json',
257
			'jquery-qtip2'
258
		), PODS_VERSION );
259
260
		wp_register_style( 'pods-form', PODS_URL . 'ui/css/pods-form.css', array(), PODS_VERSION );
261
262
		wp_register_style( 'pods-cleditor', PODS_URL . 'ui/css/jquery.cleditor.css', array(), '1.3.0' );
263
		wp_register_script( 'pods-cleditor', PODS_URL . 'ui/js/jquery.cleditor.min.js', array( 'jquery' ), '1.3.0' );
264
265
		wp_register_style( 'pods-codemirror', PODS_URL . 'ui/css/codemirror.css', array(), '4.8' );
266
		wp_register_script( 'pods-codemirror', PODS_URL . 'ui/js/codemirror.js', array(), '4.8', true );
267
		wp_register_script( 'pods-codemirror-loadmode', PODS_URL . 'ui/js/codemirror/addon/mode/loadmode.js', array( 'pods-codemirror' ), '4.8', true );
268
		wp_register_script( 'pods-codemirror-overlay', PODS_URL . 'ui/js/codemirror/addon/mode/overlay.js', array( 'pods-codemirror' ), '4.8', true );
269
		wp_register_script( 'pods-codemirror-hints', PODS_URL . 'ui/js/codemirror/addon/mode/show-hint.js', array( 'pods-codemirror' ), '4.8', true );
270
		wp_register_script( 'pods-codemirror-mode-xml', PODS_URL . 'ui/js/codemirror/mode/xml/xml.js', array( 'pods-codemirror' ), '4.8', true );
271
		wp_register_script( 'pods-codemirror-mode-html', PODS_URL . 'ui/js/codemirror/mode/htmlmixed/htmlmixed.js', array( 'pods-codemirror' ), '4.8', true );
272
		wp_register_script( 'pods-codemirror-mode-css', PODS_URL . 'ui/js/codemirror/mode/css/css.js', array( 'pods-codemirror' ), '4.8', true );
273
274
		if ( ! wp_style_is( 'jquery-ui-timepicker', 'registered' ) ) {
275
			wp_register_style( 'jquery-ui-timepicker', PODS_URL . 'ui/css/jquery.ui.timepicker.css', array(), '1.1.1' );
276
		}
277
278
		if ( ! wp_script_is( 'jquery-ui-timepicker', 'registered' ) ) {
279
			wp_register_script( 'jquery-ui-timepicker', PODS_URL . 'ui/js/jquery.ui.timepicker.min.js', array(
280
				'jquery',
281
				'jquery-ui-core',
282
				'jquery-ui-datepicker',
283
				'jquery-ui-slider'
284
			), '1.1.1' );
285
		}
286
287
		wp_register_style( 'pods-attach', PODS_URL . 'ui/css/jquery.pods.attach.css', array(), PODS_VERSION );
288
		wp_register_script( 'pods-attach', PODS_URL . 'ui/js/jquery.pods.attach.js', array(), PODS_VERSION );
289
290
		wp_register_style( 'pods-select2', PODS_URL . 'ui/js/select2/select2.css', array(), '3.3.1' );
291
		wp_register_script( 'pods-select2', PODS_URL . 'ui/js/select2/select2.min.js', array( 'jquery' ), '3.3.1' );
292
293
		wp_register_script( 'pods-handlebars', PODS_URL . 'ui/js/handlebars.js', array(), '1.0.0.beta.6' );
294
	}
295
296
	/**
297
	 * Register internal Post Types
298
	 */
299
	public function register_pods() {
300
301
		$args = array(
302
			'label'           => 'Pods',
303
			'labels'          => array( 'singular_name' => 'Pod' ),
304
			'public'          => false,
305
			'can_export'      => false,
306
			'query_var'       => false,
307
			'rewrite'         => false,
308
			'capability_type' => 'pods_pod',
309
			'has_archive'     => false,
310
			'hierarchical'    => false,
311
			'supports'        => array( 'title', 'author' ),
312
			'menu_icon'       => 'dashicons-pods'
313
		);
314
315
		$args = self::object_label_fix( $args, 'post_type' );
316
317
		register_post_type( '_pods_pod', apply_filters( 'pods_internal_register_post_type_pod', $args ) );
318
319
		$args = array(
320
			'label'           => 'Pod Fields',
321
			'labels'          => array( 'singular_name' => 'Pod Field' ),
322
			'public'          => false,
323
			'can_export'      => false,
324
			'query_var'       => false,
325
			'rewrite'         => false,
326
			'capability_type' => 'pods_pod',
327
			'has_archive'     => false,
328
			'hierarchical'    => true,
329
			'supports'        => array( 'title', 'editor', 'author' ),
330
			'menu_icon'       => 'dashicons-pods'
331
		);
332
333
		$args = self::object_label_fix( $args, 'post_type' );
334
335
		register_post_type( '_pods_field', apply_filters( 'pods_internal_register_post_type_field', $args ) );
336
	}
337
338
	/**
339
	 * Include Admin
340
	 */
341
	public function admin_init() {
342
343
		self::$admin = pods_admin();
344
	}
345
346
	/**
347
	 * Register Post Types and Taxonomies
348
	 */
349
	public function setup_content_types( $force = false ) {
350
351
		if ( empty( self::$version ) ) {
352
			return;
353
		}
354
355
		$post_types = PodsMeta::$post_types;
356
		$taxonomies = PodsMeta::$taxonomies;
357
358
		$existing_post_types = get_post_types();
359
		$existing_taxonomies = get_taxonomies();
360
361
		$pods_cpt_ct = pods_transient_get( 'pods_wp_cpt_ct' );
362
363
		$cpt_positions = array();
364
365
		if ( empty( $pods_cpt_ct ) && ( ! empty( $post_types ) || ! empty( $taxonomies ) ) ) {
366
			$force = true;
367
		} elseif ( ! empty( $pods_cpt_ct ) && empty( $pods_cpt_ct['post_types'] ) && ! empty( $post_types ) ) {
368
			$force = true;
369
		} elseif ( ! empty( $pods_cpt_ct ) && empty( $pods_cpt_ct['taxonomies'] ) && ! empty( $taxonomies ) ) {
370
			$force = true;
371
		}
372
373
		if ( false === $pods_cpt_ct || $force ) {
374
			/**
375
			 * @var WP_Query
376
			 */
377
			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...
378
379
			$reserved_query_vars = array(
380
				'post_type',
381
				'taxonomy',
382
				'output'
383
			);
384
385
			if ( is_object( $wp_query ) ) {
386
				$reserved_query_vars = array_merge( $reserved_query_vars, array_keys( $wp_query->fill_query_vars( array() ) ) );
387
			}
388
389
			$pods_cpt_ct = array(
390
				'post_types' => array(),
391
				'taxonomies' => array()
392
			);
393
394
			$pods_post_types      = $pods_taxonomies = array();
395
			$supported_post_types = $supported_taxonomies = array();
396
			
397
			$post_format_post_types = array();
398
399
			foreach ( $post_types as $post_type ) {
400
				// Post Type exists already
401 View Code Duplication
				if ( isset( $pods_cpt_ct['post_types'][ $post_type['name'] ] ) ) {
402
					continue;
403
				} elseif ( ! empty( $post_type['object'] ) && isset( $existing_post_types[ $post_type['object'] ] ) ) {
404
					continue;
405
				} elseif ( ! $force && isset( $existing_post_types[ $post_type['name'] ] ) ) {
406
					continue;
407
				}
408
409
				$post_type['options']['name'] = $post_type['name'];
410
				$post_type                    = array_merge( $post_type, (array) $post_type['options'] );
411
412
				$post_type_name = pods_var( 'name', $post_type );
413
414
				// Labels
415
				$cpt_label    = esc_html( pods_var_raw( 'label', $post_type, ucwords( str_replace( '_', ' ', pods_var_raw( 'name', $post_type ) ) ), null, true ) );
416
				$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 ) );
417
418
				$cpt_labels                       = array();
419
				$cpt_labels['name']               = $cpt_label;
420
				$cpt_labels['singular_name']      = $cpt_singular;
421
				$cpt_labels['menu_name']          = pods_var_raw( 'menu_name', $post_type, '', null, true );
422
				$cpt_labels['add_new']            = pods_var_raw( 'label_add_new', $post_type, '', null, true );
423
				$cpt_labels['add_new_item']       = pods_var_raw( 'label_add_new_item', $post_type, '', null, true );
424
				$cpt_labels['new_item']           = pods_var_raw( 'label_new_item', $post_type, '', null, true );
425
				$cpt_labels['edit']               = pods_var_raw( 'label_edit', $post_type, '', null, true );
426
				$cpt_labels['edit_item']          = pods_var_raw( 'label_edit_item', $post_type, '', null, true );
427
				$cpt_labels['view']               = pods_var_raw( 'label_view', $post_type, '', null, true );
428
				$cpt_labels['view_item']          = pods_var_raw( 'label_view_item', $post_type, '', null, true );
429
				$cpt_labels['all_items']          = pods_var_raw( 'label_all_items', $post_type, '', null, true );
430
				$cpt_labels['search_items']       = pods_var_raw( 'label_search_items', $post_type, '', null, true );
431
				$cpt_labels['not_found']          = pods_var_raw( 'label_not_found', $post_type, '', null, true );
432
				$cpt_labels['not_found_in_trash'] = pods_var_raw( 'label_not_found_in_trash', $post_type, '', null, true );
433
				$cpt_labels['parent']             = pods_var_raw( 'label_parent', $post_type, '', null, true );
434
				$cpt_labels['parent_item_colon']  = pods_var_raw( 'label_parent_item_colon', $post_type, '', null, true );
435
436
				// Supported
437
				$cpt_supported = array(
438
					'title'           => (boolean) pods_var( 'supports_title', $post_type, false ),
439
					'editor'          => (boolean) pods_var( 'supports_editor', $post_type, false ),
440
					'author'          => (boolean) pods_var( 'supports_author', $post_type, false ),
441
					'thumbnail'       => (boolean) pods_var( 'supports_thumbnail', $post_type, false ),
442
					'excerpt'         => (boolean) pods_var( 'supports_excerpt', $post_type, false ),
443
					'trackbacks'      => (boolean) pods_var( 'supports_trackbacks', $post_type, false ),
444
					'custom-fields'   => (boolean) pods_var( 'supports_custom_fields', $post_type, false ),
445
					'comments'        => (boolean) pods_var( 'supports_comments', $post_type, false ),
446
					'revisions'       => (boolean) pods_var( 'supports_revisions', $post_type, false ),
447
					'page-attributes' => (boolean) pods_var( 'supports_page_attributes', $post_type, false ),
448
					'post-formats'    => (boolean) pods_var( 'supports_post_formats', $post_type, false )
449
				);
450
451
				// Custom Supported
452
				$cpt_supported_custom = pods_var( 'supports_custom', $post_type, '' );
453
454
				if ( ! empty( $cpt_supported_custom ) ) {
455
					$cpt_supported_custom = explode( ',', $cpt_supported_custom );
456
					$cpt_supported_custom = array_filter( array_unique( $cpt_supported_custom ) );
457
458
					foreach ( $cpt_supported_custom as $cpt_support ) {
459
						$cpt_supported[ $cpt_support ] = true;
460
					}
461
				}
462
463
				// Genesis Support
464
				if ( function_exists( 'genesis' ) ) {
465
					$cpt_supported['genesis-seo']             = (boolean) pods_var( 'supports_genesis_seo', $post_type, false );
466
					$cpt_supported['genesis-layouts']         = (boolean) pods_var( 'supports_genesis_layouts', $post_type, false );
467
					$cpt_supported['genesis-simple-sidebars'] = (boolean) pods_var( 'supports_genesis_simple_sidebars', $post_type, false );
468
				}
469
470
				// YARPP Support
471
				if ( defined( 'YARPP_VERSION' ) ) {
472
					$cpt_supported['yarpp_support'] = (boolean) pods_var( 'supports_yarpp_support', $post_type, false );
473
				}
474
475
				// Jetpack Support
476
				if ( class_exists( 'Jetpack' ) ) {
477
					$cpt_supported['supports_jetpack_publicize'] = (boolean) pods_var( 'supports_jetpack_publicize', $post_type, false );
478
					$cpt_supported['supports_jetpack_markdown']  = (boolean) pods_var( 'supports_jetpack_markdown', $post_type, false );
479
				}
480
481
				// WP needs something, if this was empty and none were enabled, it would show title+editor pre 3.5 :(
482
				$cpt_supports = array();
483
484
				if ( ! pods_version_check( 'wp', '3.5' ) ) {
485
					$cpt_supports = array( '_bug_fix_pre_35' );
486
				}
487
488
				foreach ( $cpt_supported as $cpt_support => $supported ) {
489
					if ( true === $supported ) {
490
						$cpt_supports[] = $cpt_support;
491
						
492
						if ( 'post-formats' === $cpt_support ) {
493
							$post_format_post_types[] = $post_type_name;
494
						}
495
					}
496
				}
497
498
				if ( empty( $cpt_supports ) && pods_version_check( 'wp', '3.5' ) ) {
499
					$cpt_supports = false;
500
				}
501
502
				// Rewrite
503
				$cpt_rewrite       = (boolean) pods_var( 'rewrite', $post_type, true );
504
				$cpt_rewrite_array = array(
505
					'slug'       => pods_var( 'rewrite_custom_slug', $post_type, str_replace( '_', '-', $post_type_name ), null, true ),
506
					'with_front' => (boolean) pods_var( 'rewrite_with_front', $post_type, true ),
507
					'feeds'      => (boolean) pods_var( 'rewrite_feeds', $post_type, (boolean) pods_var( 'has_archive', $post_type, false ) ),
508
					'pages'      => (boolean) pods_var( 'rewrite_pages', $post_type, true )
509
				);
510
511
				if ( false !== $cpt_rewrite ) {
512
					$cpt_rewrite = $cpt_rewrite_array;
513
				}
514
515
				$capability_type = pods_var( 'capability_type', $post_type, 'post' );
516
517
				if ( 'custom' == $capability_type ) {
518
					$capability_type = pods_var( 'capability_type_custom', $post_type, 'post' );
519
				}
520
521
				$show_in_menu = (boolean) pods_var( 'show_in_menu', $post_type, true );
522
523
				if ( $show_in_menu && 0 < strlen( pods_var_raw( 'menu_location_custom', $post_type ) ) ) {
524
					$show_in_menu = pods_var_raw( 'menu_location_custom', $post_type );
525
				}
526
527
				$menu_icon = pods_var( 'menu_icon', $post_type, null, null, true );
528
529
				if ( ! empty( $menu_icon ) ) {
530
					$menu_icon = pods_evaluate_tags( $menu_icon );
531
				}
532
533
				// Register Post Type
534
				$pods_post_types[ $post_type_name ] = array(
535
					'label'               => $cpt_label,
536
					'labels'              => $cpt_labels,
537
					'description'         => esc_html( pods_var_raw( 'description', $post_type ) ),
538
					'public'              => (boolean) pods_var( 'public', $post_type, true ),
539
					'publicly_queryable'  => (boolean) pods_var( 'publicly_queryable', $post_type, (boolean) pods_var( 'public', $post_type, true ) ),
540
					'exclude_from_search' => (boolean) pods_var( 'exclude_from_search', $post_type, ( (boolean) pods_var( 'public', $post_type, true ) ? false : true ) ),
541
					'show_ui'             => (boolean) pods_var( 'show_ui', $post_type, (boolean) pods_var( 'public', $post_type, true ) ),
542
					'show_in_menu'        => $show_in_menu,
543
					'show_in_nav_menus'   => (boolean) pods_var( 'show_in_nav_menus', $post_type, (boolean) pods_var( 'public', $post_type, true ) ),
544
					'show_in_admin_bar'   => (boolean) pods_var( 'show_in_admin_bar', $post_type, (boolean) pods_var( 'show_in_menu', $post_type, true ) ),
545
					'menu_position'       => (int) pods_var( 'menu_position', $post_type, 0, null, true ),
546
					'menu_icon'           => $menu_icon,
547
					'capability_type'     => $capability_type,
548
					//'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...
549
					'map_meta_cap'        => (boolean) pods_var( 'capability_type_extra', $post_type, true ),
550
					'hierarchical'        => (boolean) pods_var( 'hierarchical', $post_type, false ),
551
					'supports'            => $cpt_supports,
552
					//'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...
553
					//'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...
554
					'has_archive'         => pods_v( 'has_archive_slug', $post_type, (boolean) pods_v( 'has_archive', $post_type, false ), true ),
555
					'rewrite'             => $cpt_rewrite,
556
					'query_var'           => ( false !== (boolean) pods_var( 'query_var', $post_type, true ) ? pods_var( 'query_var_string', $post_type, $post_type_name, null, true ) : false ),
557
					'can_export'          => (boolean) pods_var( 'can_export', $post_type, true )
558
				);
559
560
				// YARPP doesn't use 'supports' array option (yet)
561
				if ( ! empty( $cpt_supports['yarpp_support'] ) ) {
562
					$pods_post_types[ $post_type_name ]['yarpp_support'] = true;
563
				}
564
565
				// Prevent reserved query_var issues
566
				if ( in_array( $pods_post_types[ $post_type_name ]['query_var'], $reserved_query_vars ) ) {
567
					$pods_post_types[ $post_type_name ]['query_var'] = 'post_type_' . $pods_post_types[ $post_type_name ]['query_var'];
568
				}
569
570
				if ( 25 == $pods_post_types[ $post_type_name ]['menu_position'] ) {
571
					$pods_post_types[ $post_type_name ]['menu_position'] ++;
572
				}
573
574
				if ( $pods_post_types[ $post_type_name ]['menu_position'] < 1 || in_array( $pods_post_types[ $post_type_name ]['menu_position'], $cpt_positions ) ) {
575
					unset( $pods_post_types[ $post_type_name ]['menu_position'] );
576
				} else {
577
					$cpt_positions[] = $pods_post_types[ $post_type_name ]['menu_position'];
578
579
					// This would be nice if WP supported floats in menu_position
580
					// $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...
581
				}
582
583
				// Taxonomies
584
				$cpt_taxonomies = array();
585
				$_taxonomies    = get_taxonomies();
586
				$_taxonomies    = array_merge_recursive( $_taxonomies, $pods_taxonomies );
587
				$ignore         = array( 'nav_menu', 'link_category', 'post_format' );
588
589 View Code Duplication
				foreach ( $_taxonomies as $taxonomy => $label ) {
590
					if ( in_array( $taxonomy, $ignore ) ) {
591
						continue;
592
					}
593
594
					if ( false !== (boolean) pods_var( 'built_in_taxonomies_' . $taxonomy, $post_type, false ) ) {
595
						$cpt_taxonomies[] = $taxonomy;
596
597
						if ( isset( $supported_post_types[ $taxonomy ] ) && ! in_array( $post_type_name, $supported_post_types[ $taxonomy ] ) ) {
598
							$supported_post_types[ $taxonomy ][] = $post_type_name;
599
						}
600
					}
601
				}
602
603
				if ( isset( $supported_taxonomies[ $post_type_name ] ) ) {
604
					$supported_taxonomies[ $post_type_name ] = array_merge( (array) $supported_taxonomies[ $post_type_name ], $cpt_taxonomies );
605
				} else {
606
					$supported_taxonomies[ $post_type_name ] = $cpt_taxonomies;
607
				}
608
			}
609
610
			foreach ( $taxonomies as $taxonomy ) {
611
				// Taxonomy Type exists already
612 View Code Duplication
				if ( isset( $pods_cpt_ct['taxonomies'][ $taxonomy['name'] ] ) ) {
613
					continue;
614
				} elseif ( ! empty( $taxonomy['object'] ) && isset( $existing_taxonomies[ $taxonomy['object'] ] ) ) {
615
					continue;
616
				} elseif ( ! $force && isset( $existing_taxonomies[ $taxonomy['name'] ] ) ) {
617
					continue;
618
				}
619
620
				$taxonomy['options']['name'] = $taxonomy['name'];
621
				$taxonomy                    = array_merge( $taxonomy, (array) $taxonomy['options'] );
622
623
				$taxonomy_name = pods_var( 'name', $taxonomy );
624
625
				// Labels
626
				$ct_label    = esc_html( pods_var_raw( 'label', $taxonomy, ucwords( str_replace( '_', ' ', pods_var_raw( 'name', $taxonomy ) ) ), null, true ) );
627
				$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 ) );
628
629
				$ct_labels                               = array();
630
				$ct_labels['name']                       = $ct_label;
631
				$ct_labels['singular_name']              = $ct_singular;
632
				$ct_labels['menu_name']                  = pods_var_raw( 'menu_name', $taxonomy, '', null, true );
633
				$ct_labels['search_items']               = pods_var_raw( 'label_search_items', $taxonomy, '', null, true );
634
				$ct_labels['popular_items']              = pods_var_raw( 'label_popular_items', $taxonomy, '', null, true );
635
				$ct_labels['all_items']                  = pods_var_raw( 'label_all_items', $taxonomy, '', null, true );
636
				$ct_labels['parent_item']                = pods_var_raw( 'label_parent_item', $taxonomy, '', null, true );
637
				$ct_labels['parent_item_colon']          = pods_var_raw( 'label_parent_item_colon', $taxonomy, '', null, true );
638
				$ct_labels['edit_item']                  = pods_var_raw( 'label_edit_item', $taxonomy, '', null, true );
639
				$ct_labels['update_item']                = pods_var_raw( 'label_update_item', $taxonomy, '', null, true );
640
				$ct_labels['add_new_item']               = pods_var_raw( 'label_add_new_item', $taxonomy, '', null, true );
641
				$ct_labels['new_item_name']              = pods_var_raw( 'label_new_item_name', $taxonomy, '', null, true );
642
				$ct_labels['separate_items_with_commas'] = pods_var_raw( 'label_separate_items_with_commas', $taxonomy, '', null, true );
643
				$ct_labels['add_or_remove_items']        = pods_var_raw( 'label_add_or_remove_items', $taxonomy, '', null, true );
644
				$ct_labels['choose_from_most_used']      = pods_var_raw( 'label_choose_from_the_most_used', $taxonomy, '', null, true );
645
646
				// Rewrite
647
				$ct_rewrite       = (boolean) pods_var( 'rewrite', $taxonomy, true );
648
				$ct_rewrite_array = array(
649
					'slug'         => pods_var( 'rewrite_custom_slug', $taxonomy, str_replace( '_', '-', $taxonomy_name ), null, true ),
650
					'with_front'   => (boolean) pods_var( 'rewrite_with_front', $taxonomy, true ),
651
					'hierarchical' => (boolean) pods_var( 'rewrite_hierarchical', $taxonomy, (boolean) pods_var( 'hierarchical', $taxonomy, false ) )
652
				);
653
654
				if ( false !== $ct_rewrite ) {
655
					$ct_rewrite = $ct_rewrite_array;
656
				}
657
658
				/**
659
				 * Default tax capabilities
660
				 * @see https://codex.wordpress.org/Function_Reference/register_taxonomy
661
				 */
662
				$capability_type = pods_var( 'capability_type', $taxonomy, 'default' );
663
				$tax_capabilities = array();
664
665
				if ( 'custom' == $capability_type ) {
666
					$capability_type = pods_var( 'capability_type_custom', $taxonomy, 'default' );
667
					if ( ! empty( $capability_type ) && 'default' != $capability_type ) {
668
						$capability_type .=  '_term';
669
						$capability_type_plural =  $capability_type . 's';
670
						$tax_capabilities = array(
671
							// Singular
672
							'edit_term'   => 'edit_' . $capability_type,
673
							'delete_term' => 'delete_' . $capability_type,
674
							'assign_term' => 'assign_' . $capability_type,
675
							// Plural
676
							'manage_terms' => 'manage_' . $capability_type_plural,
677
							'edit_terms'   => 'edit_' . $capability_type_plural,
678
							'delete_terms' => 'delete_' . $capability_type_plural,
679
							'assign_terms' => 'assign_' . $capability_type_plural,
680
						);
681
					}
682
				}
683
684
				// Register Taxonomy
685
				$pods_taxonomies[ $taxonomy_name ] = array(
686
					'label'                 => $ct_label,
687
					'labels'                => $ct_labels,
688
					'public'                => (boolean) pods_var( 'public', $taxonomy, true ),
689
					'show_in_nav_menus'     => (boolean) pods_var( 'show_in_nav_menus', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ),
690
					'show_ui'               => (boolean) pods_var( 'show_ui', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ),
691
					'show_in_menu'          => (boolean) pods_var( 'show_in_menu', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ),
692
					'show_tagcloud'         => (boolean) pods_var( 'show_tagcloud', $taxonomy, (boolean) pods_var( 'show_ui', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ) ),
693
					'hierarchical'          => (boolean) pods_var( 'hierarchical', $taxonomy, false ),
694
					//'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...
695
					'capabilities'          => $tax_capabilities,
696
					//'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...
697
					'update_count_callback' => pods_var( 'update_count_callback', $taxonomy, null, null, true ),
698
					'query_var'             => ( false !== (boolean) pods_var( 'query_var', $taxonomy, true ) ? pods_var( 'query_var_string', $taxonomy, $taxonomy_name, null, true ) : false ),
699
					'rewrite'               => $ct_rewrite,
700
					'show_admin_column'     => (boolean) pods_var( 'show_admin_column', $taxonomy, false ),
701
					'sort'                  => (boolean) pods_var( 'sort', $taxonomy, false )
702
				);
703
704
				if ( is_array( $ct_rewrite ) && ! $pods_taxonomies[ $taxonomy_name ]['query_var'] ) {
705
					$pods_taxonomies[ $taxonomy_name ]['query_var'] = pods_var( 'query_var_string', $taxonomy, $taxonomy_name, null, true );
706
				};
707
708
				// Prevent reserved query_var issues
709
				if ( in_array( $pods_taxonomies[ $taxonomy_name ]['query_var'], $reserved_query_vars ) ) {
710
					$pods_taxonomies[ $taxonomy_name ]['query_var'] = 'taxonomy_' . $pods_taxonomies[ $taxonomy_name ]['query_var'];
711
				}
712
713
				// Integration for Single Value Taxonomy UI
714
				if ( function_exists( 'tax_single_value_meta_box' ) ) {
715
					$pods_taxonomies[ $taxonomy_name ]['single_value'] = (boolean) pods_var( 'single_value', $taxonomy, false );
716
					$pods_taxonomies[ $taxonomy_name ]['required']     = (boolean) pods_var( 'single_value_required', $taxonomy, false );
717
				}
718
719
				// Post Types
720
				$ct_post_types = array();
721
				$_post_types   = get_post_types();
722
				$_post_types   = array_merge_recursive( $_post_types, $pods_post_types );
723
				$ignore        = array( 'revision' );
724
725 View Code Duplication
				foreach ( $_post_types as $post_type => $options ) {
726
					if ( in_array( $post_type, $ignore ) ) {
727
						continue;
728
					}
729
730
					if ( false !== (boolean) pods_var( 'built_in_post_types_' . $post_type, $taxonomy, false ) ) {
731
						$ct_post_types[] = $post_type;
732
733
						if ( isset( $supported_taxonomies[ $post_type ] ) && ! in_array( $taxonomy_name, $supported_taxonomies[ $post_type ] ) ) {
734
							$supported_taxonomies[ $post_type ][] = $taxonomy_name;
735
						}
736
					}
737
				}
738
739
				if ( isset( $supported_post_types[ $taxonomy_name ] ) ) {
740
					$supported_post_types[ $taxonomy_name ] = array_merge( $supported_post_types[ $taxonomy_name ], $ct_post_types );
741
				} else {
742
					$supported_post_types[ $taxonomy_name ] = $ct_post_types;
743
				}
744
			}
745
746
			$pods_post_types = apply_filters( 'pods_wp_post_types', $pods_post_types );
747
			$pods_taxonomies = apply_filters( 'pods_wp_taxonomies', $pods_taxonomies );
748
749
			$supported_post_types = apply_filters( 'pods_wp_supported_post_types', $supported_post_types );
750
			$supported_taxonomies = apply_filters( 'pods_wp_supported_taxonomies', $supported_taxonomies );
751
752
			foreach ( $pods_taxonomies as $taxonomy => $options ) {
753
				$ct_post_types = null;
754
755
				if ( isset( $supported_post_types[ $taxonomy ] ) && ! empty( $supported_post_types[ $taxonomy ] ) ) {
756
					$ct_post_types = $supported_post_types[ $taxonomy ];
757
				}
758
759
				$pods_cpt_ct['taxonomies'][ $taxonomy ] = array(
760
					'post_types' => $ct_post_types,
761
					'options'    => $options
762
				);
763
			}
764
765
			foreach ( $pods_post_types as $post_type => $options ) {
766
				if ( isset( $supported_taxonomies[ $post_type ] ) && ! empty( $supported_taxonomies[ $post_type ] ) ) {
767
					$options['taxonomies'] = $supported_taxonomies[ $post_type ];
768
				}
769
770
				$pods_cpt_ct['post_types'][ $post_type ] = $options;
771
			}
772
			
773
			$pods_cpt_ct['post_format_post_types'] = $post_format_post_types;
774
775
			pods_transient_set( 'pods_wp_cpt_ct', $pods_cpt_ct );
776
		}
777
778
		foreach ( $pods_cpt_ct['taxonomies'] as $taxonomy => $options ) {
779
			if ( isset( self::$content_types_registered['taxonomies'] ) && in_array( $taxonomy, self::$content_types_registered['taxonomies'] ) ) {
780
				continue;
781
			}
782
783
			$ct_post_types = $options['post_types'];
784
			$options       = $options['options'];
785
786
			$options = apply_filters( 'pods_register_taxonomy_' . $taxonomy, $options, $taxonomy );
787
			$options = apply_filters( 'pods_register_taxonomy', $options, $taxonomy );
788
789
			$options = self::object_label_fix( $options, 'taxonomy' );
790
791
			// Max length for taxonomies are 32 characters
792
			$taxonomy = substr( $taxonomy, 0, 32 );
793
794
			// i18n compatibility for plugins that override it
795 View Code Duplication
			if ( is_array( $options['rewrite'] ) && isset( $options['rewrite']['slug'] ) && ! empty( $options['rewrite']['slug'] ) ) {
796
				$options['rewrite']['slug'] = _x( $options['rewrite']['slug'], 'URL taxonomy slug', 'pods' );
797
			}
798
799 View Code Duplication
			if ( 1 == pods_var( 'pods_debug_register', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) {
800
				pods_debug( array( $taxonomy, $ct_post_types, $options ) );
801
			}
802
803
			register_taxonomy( $taxonomy, $ct_post_types, $options );
804
805
			if ( ! isset( self::$content_types_registered['taxonomies'] ) ) {
806
				self::$content_types_registered['taxonomies'] = array();
807
			}
808
809
			self::$content_types_registered['taxonomies'][] = $taxonomy;
810
		}
811
812
		foreach ( $pods_cpt_ct['post_types'] as $post_type => $options ) {
813
			if ( isset( self::$content_types_registered['post_types'] ) && in_array( $post_type, self::$content_types_registered['post_types'] ) ) {
814
				continue;
815
			}
816
817
			$options = apply_filters( 'pods_register_post_type_' . $post_type, $options, $post_type );
818
			$options = apply_filters( 'pods_register_post_type', $options, $post_type );
819
820
			$options = self::object_label_fix( $options, 'post_type' );
821
822
			// Max length for post types are 20 characters
823
			$post_type = substr( $post_type, 0, 20 );
824
825
			// i18n compatibility for plugins that override it
826 View Code Duplication
			if ( is_array( $options['rewrite'] ) && isset( $options['rewrite']['slug'] ) && ! empty( $options['rewrite']['slug'] ) ) {
827
				$options['rewrite']['slug'] = _x( $options['rewrite']['slug'], 'URL slug', 'pods' );
828
			}
829
830 View Code Duplication
			if ( 1 == pods_var( 'pods_debug_register', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) {
831
				pods_debug( array( $post_type, $options ) );
832
			}
833
834
			register_post_type( $post_type, $options );
835
836
			// Register post format taxonomy for this post type
837
			if ( isset( $pods_cpt_ct['post_format_post_types'] ) && in_array( $post_type, $pods_cpt_ct['post_format_post_types'], true ) ) {
838
				register_taxonomy_for_object_type( 'post_format', $post_type );
839
			}
840
841
			if ( ! isset( self::$content_types_registered['post_types'] ) ) {
842
				self::$content_types_registered['post_types'] = array();
843
			}
844
845
			self::$content_types_registered['post_types'][] = $post_type;
846
		}
847
848
	}
849
850
	/**
851
	 * Check if we need to flush WordPress rewrite rules
852
	 * This gets run during 'init' action late in the game to give other plugins time to register their rewrite rules
853
	 */
854
	public function flush_rewrite_rules() {
855
856
		$flush = (int) pods_transient_get( 'pods_flush_rewrites' );
857
858
		if ( 1 === $flush ) {
859
			/**
860
			 * @var $wp_rewrite WP_Rewrite
861
			 */
862
			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...
863
			$wp_rewrite->flush_rules();
864
			$wp_rewrite->init();
865
866
			pods_transient_set( 'pods_flush_rewrites', 0 );
867
		}
868
	}
869
870
	/**
871
	 * Update Post Type messages
872
	 *
873
	 * @param array $messages
874
	 *
875
	 * @return array
876
	 * @since 2.0.2
877
	 */
878
	public function setup_updated_messages( $messages ) {
879
880
		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...
881
882
		$post_types          = PodsMeta::$post_types;
883
		$existing_post_types = get_post_types();
884
885
		$pods_cpt_ct = pods_transient_get( 'pods_wp_cpt_ct' );
886
887
		if ( empty( $pods_cpt_ct ) || empty( $post_types ) ) {
888
			return $messages;
889
		}
890
891
		/* Use get_preview_post_link function added in 4.4, which eventually applies preview_post_link filter
892
		 * Before 4.4, this filter is defined in wp-admin/includes/meta-boxes.php, $post parameter added in 4.0
893
		 * there wasn't post parameter back in 3.8
894
		 * Let's add $post in the filter as it won't hurt anyway.
895
         * @since 2.6.8.1
896
		*/
897
		$preview_post_link = function_exists('get_preview_post_link')
898
									? get_preview_post_link( $post )
899
									: apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ), $post );
900
901
		foreach ( $post_types as $post_type ) {
902
			if ( ! isset( $pods_cpt_ct['post_types'][ $post_type['name'] ] ) ) {
903
				continue;
904
			}
905
906
			$labels = self::object_label_fix( $pods_cpt_ct['post_types'][ $post_type['name'] ], 'post_type' );
907
			$labels = $labels['labels'];
908
909
			$messages[ $post_type['name'] ] = array(
910
				1  => sprintf( __( '%s updated. <a href="%s">%s</a>', 'pods' ), $labels['singular_name'], esc_url( get_permalink( $post_ID ) ), $labels['view_item'] ),
911
				2  => __( 'Custom field updated.', 'pods' ),
912
				3  => __( 'Custom field deleted.', 'pods' ),
913
				4  => sprintf( __( '%s updated.', 'pods' ), $labels['singular_name'] ),
914
				/* translators: %s: date and time of the revision */
915
				5  => isset( $_GET['revision'] ) ? sprintf( __( '%s restored to revision from %s', 'pods' ), $labels['singular_name'], wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
916
				6  => sprintf( __( '%s published. <a href="%s">%s</a>', 'pods' ), $labels['singular_name'], esc_url( get_permalink( $post_ID ) ), $labels['view_item'] ),
917
				7  => sprintf( __( '%s saved.', 'pods' ), $labels['singular_name'] ),
918
				8  => sprintf( __( '%s submitted. <a target="_blank" href="%s">Preview %s</a>', 'pods' ), $labels['singular_name'], esc_url( $preview_post_link ), $labels['singular_name'] ),
919
				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
920
					date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink( $post_ID ) ), $labels['singular_name'] ),
921
				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'] )
922
			);
923
924
			if ( false === (boolean) $pods_cpt_ct['post_types'][ $post_type['name'] ]['public'] ) {
925
				$messages[ $post_type['name'] ][1]  = sprintf( __( '%s updated.', 'pods' ), $labels['singular_name'] );
926
				$messages[ $post_type['name'] ][6]  = sprintf( __( '%s published.', 'pods' ), $labels['singular_name'] );
927
				$messages[ $post_type['name'] ][8]  = sprintf( __( '%s submitted.', 'pods' ), $labels['singular_name'] );
928
				$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
929
					date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ) );
930
				$messages[ $post_type['name'] ][10] = sprintf( __( '%s draft updated.', 'pods' ), $labels['singular_name'] );
931
			}
932
		}
933
934
		return $messages;
935
	}
936
937
	/**
938
	 * @param        $args
939
	 * @param string $type
940
	 *
941
	 * @return array
942
	 */
943
	public static function object_label_fix( $args, $type = 'post_type' ) {
944
945
		if ( empty( $args ) || ! is_array( $args ) ) {
946
			$args = array();
947
		}
948
949
		if ( ! isset( $args['labels'] ) || ! is_array( $args['labels'] ) ) {
950
			$args['labels'] = array();
951
		}
952
953
		$label          = pods_var_raw( 'name', $args['labels'], pods_var_raw( 'label', $args, __( 'Items', 'pods' ), null, true ), null, true );
954
		$singular_label = pods_var_raw( 'singular_name', $args['labels'], pods_var_raw( 'label_singular', $args, __( 'Item', 'pods' ), null, true ), null, true );
955
956
		$labels = $args['labels'];
957
958
		$labels['name']          = $label;
959
		$labels['singular_name'] = $singular_label;
960
961
		if ( 'post_type' == $type ) {
962
			$labels['menu_name']             = pods_var_raw( 'menu_name', $labels, $label, null, true );
963
			$labels['add_new']               = pods_var_raw( 'add_new', $labels, __( 'Add New', 'pods' ), null, true );
964
			$labels['add_new_item']          = pods_var_raw( 'add_new_item', $labels, sprintf( __( 'Add New %s', 'pods' ), $singular_label ), null, true );
965
			$labels['new_item']              = pods_var_raw( 'new_item', $labels, sprintf( __( 'New %s', 'pods' ), $singular_label ), null, true );
966
			$labels['edit']                  = pods_var_raw( 'edit', $labels, __( 'Edit', 'pods' ), null, true );
967
			$labels['edit_item']             = pods_var_raw( 'edit_item', $labels, sprintf( __( 'Edit %s', 'pods' ), $singular_label ), null, true );
968
			$labels['view']                  = pods_var_raw( 'view', $labels, sprintf( __( 'View %s', 'pods' ), $singular_label ), null, true );
969
			$labels['view_item']             = pods_var_raw( 'view_item', $labels, sprintf( __( 'View %s', 'pods' ), $singular_label ), null, true );
970
			$labels['all_items']             = pods_var_raw( 'all_items', $labels, sprintf( __( 'All %s', 'pods' ), $label ), null, true );
971
			$labels['search_items']          = pods_var_raw( 'search_items', $labels, sprintf( __( 'Search %s', 'pods' ), $label ), null, true );
972
			$labels['not_found']             = pods_var_raw( 'not_found', $labels, sprintf( __( 'No %s Found', 'pods' ), $label ), null, true );
973
			$labels['not_found_in_trash']    = pods_var_raw( 'not_found_in_trash', $labels, sprintf( __( 'No %s Found in Trash', 'pods' ), $label ), null, true );
974
			$labels['parent']                = pods_var_raw( 'parent', $labels, sprintf( __( 'Parent %s', 'pods' ), $singular_label ), null, true );
975
			$labels['parent_item_colon']     = pods_var_raw( 'parent_item_colon', $labels, sprintf( __( 'Parent %s:', 'pods' ), $singular_label ), null, true );
976
			$labels['feature_image']         = pods_var_raw( 'feature_image', $labels, __( 'Featured Image', 'pods' ), null, true );
977
			$labels['set_featured_image']    = pods_var_raw( 'set_featured_image', $labels, __( 'Set featured image', 'pods' ), null, true );
978
			$labels['remove_featured_image'] = pods_var_raw( 'remove_featured_image', $labels, __( 'Remove featured image', 'pods' ), null, true );
979
			$labels['use_featured_image']    = pods_var_raw( 'use_featured_image', $labels, __( 'Use as featured image', 'pods' ), null, true );
980
			$labels['archives']              = pods_var_raw( 'archives', $labels, sprintf( __( '%s Archives', 'pods' ), $singular_label ), null, true );
981
			$labels['insert_into_item']      = pods_var_raw( 'insert_into_item', $labels, sprintf( __( 'Insert into %s', 'pods' ), $singular_label ), null, true );
982
			$labels['uploaded_to_this_item'] = pods_var_raw( 'uploaded_to_this_item', $labels, sprintf( __( 'Uploaded to this %s', 'pods' ), $singular_label ), null, true );
983
			$labels['filter_items_list']     = pods_var_raw( 'filter_items_list', $labels, sprintf( __( 'Filter %s lists', 'pods' ), $label ), null, true );
984
			$labels['items_list_navigation'] = pods_var_raw( 'items_list_navigation', $labels, sprintf( __( '%s navigation', 'pods' ), $label ), null, true );
985
			$labels['items_list']            = pods_var_raw( 'items_list', $labels, sprintf( __( '%s list', 'pods' ), $label ), null, true );
986
		} elseif ( 'taxonomy' == $type ) {
987
			$labels['menu_name']                  = pods_var_raw( 'menu_name', $labels, $label, null, true );
988
			$labels['search_items']               = pods_var_raw( 'search_items', $labels, sprintf( __( 'Search %s', 'pods' ), $label ), null, true );
989
			$labels['popular_items']              = pods_var_raw( 'popular_items', $labels, sprintf( __( 'Popular %s', 'pods' ), $label ), null, true );
990
			$labels['all_items']                  = pods_var_raw( 'all_items', $labels, sprintf( __( 'All %s', 'pods' ), $label ), null, true );
991
			$labels['parent_item']                = pods_var_raw( 'parent_item', $labels, sprintf( __( 'Parent %s', 'pods' ), $singular_label ), null, true );
992
			$labels['parent_item_colon']          = pods_var_raw( 'parent_item_colon', $labels, sprintf( __( 'Parent %s :', 'pods' ), $singular_label ), null, true );
993
			$labels['edit_item']                  = pods_var_raw( 'edit_item', $labels, sprintf( __( 'Edit %s', 'pods' ), $singular_label ), null, true );
994
			$labels['update_item']                = pods_var_raw( 'update_item', $labels, sprintf( __( 'Update %s', 'pods' ), $singular_label ), null, true );
995
			$labels['add_new_item']               = pods_var_raw( 'add_new_item', $labels, sprintf( __( 'Add New %s', 'pods' ), $singular_label ), null, true );
996
			$labels['new_item_name']              = pods_var_raw( 'new_item_name', $labels, sprintf( __( 'New %s Name', 'pods' ), $singular_label ), null, true );
997
			$labels['separate_items_with_commas'] = pods_var_raw( 'separate_items_with_commas', $labels, sprintf( __( 'Separate %s with commas', 'pods' ), $label ), null, true );
998
			$labels['add_or_remove_items']        = pods_var_raw( 'add_or_remove_items', $labels, sprintf( __( 'Add or remove %s', 'pods' ), $label ), null, true );
999
			$labels['choose_from_most_used']      = pods_var_raw( 'choose_from_most_used', $labels, sprintf( __( 'Choose from the most used %s', 'pods' ), $label ), null, true );
1000
			$labels['no_terms']                   = pods_var_raw( 'no_terms', $labels, sprintf( __( 'No %s', 'pods' ), $label ), null, true );
1001
			$labels['items_list_navigation']      = pods_var_raw( 'items_list_navigation', $labels, sprintf( __( '%s navigation', 'pods' ), $label ), null, true );
1002
			$labels['items_list']                 = pods_var_raw( 'items_list', $labels, sprintf( __( '%s list', 'pods' ), $label ), null, true );
1003
		}
1004
1005
		$args['labels'] = $labels;
1006
1007
		return $args;
1008
	}
1009
1010
	/**
1011
	 * Activate and Install
1012
	 */
1013
	public function activate_install() {
1014
1015
		register_activation_hook( PODS_DIR . 'init.php', array( $this, 'activate' ) );
1016
		register_deactivation_hook( PODS_DIR . 'init.php', array( $this, 'deactivate' ) );
1017
1018
		add_action( 'wpmu_new_blog', array( $this, 'new_blog' ), 10, 6 );
1019
1020
		if ( empty( self::$version ) || version_compare( self::$version, PODS_VERSION, '<' ) || version_compare( self::$version, PODS_DB_VERSION, '<=' ) || self::$upgrade_needed ) {
1021
			$this->setup();
1022
		} elseif ( self::$version !== PODS_VERSION ) {
1023
			delete_option( 'pods_framework_version' );
1024
			add_option( 'pods_framework_version', PODS_VERSION, '', 'yes' );
1025
1026
			self::$version = PODS_VERSION;
1027
1028
			pods_api()->cache_flush_pods();
1029
		}
1030
1031
	}
1032
1033
	/**
1034
	 *
1035
	 */
1036
	public function activate() {
1037
1038
		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...
1039
1040
		if ( function_exists( 'is_multisite' ) && is_multisite() && isset( $_GET['networkwide'] ) && 1 == $_GET['networkwide'] ) {
1041
			$_blog_ids = $wpdb->get_col( "SELECT `blog_id` FROM `{$wpdb->blogs}`" );
1042
1043
			foreach ( $_blog_ids as $_blog_id ) {
1044
				$this->setup( $_blog_id );
1045
			}
1046
		} else {
1047
			$this->setup();
1048
		}
1049
	}
1050
1051
	/**
1052
	 *
1053
	 */
1054
	public function deactivate() {
1055
1056
		pods_api()->cache_flush_pods();
1057
1058
	}
1059
1060
	/**
1061
	 *
1062
	 */
1063
	public function needs_upgrade( $current = null, $last = null ) {
1064
1065
		if ( null === $current ) {
1066
			$current = self::$version;
1067
		}
1068
1069
		if ( null === $last ) {
1070
			$last = self::$version_last;
1071
		}
1072
1073
		$upgrade_needed = false;
1074
1075
		if ( ! empty( $current ) ) {
1076
			foreach ( self::$upgrades as $old_version => $new_version ) {
1077
				/*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...
1078
					continue;*/
1079
1080
				if ( version_compare( $last, $old_version, '>=' ) && version_compare( $last, $new_version, '<' ) && version_compare( $current, $new_version, '>=' ) && 1 != self::$upgraded ) {
1081
					$upgrade_needed = true;
1082
1083
					break;
1084
				}
1085
			}
1086
		}
1087
1088
		return $upgrade_needed;
1089
	}
1090
1091
	/**
1092
	 * @param $_blog_id
1093
	 * @param $user_id
1094
	 * @param $domain
1095
	 * @param $path
1096
	 * @param $site_id
1097
	 * @param $meta
1098
	 */
1099
	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...
1100
1101
		if ( function_exists( 'is_multisite' ) && is_multisite() && is_plugin_active_for_network( basename( PODS_DIR ) . '/init.php' ) ) {
1102
			$this->setup( $_blog_id );
1103
		}
1104
	}
1105
1106
	/**
1107
	 * @param null $_blog_id
1108
	 */
1109
	public function setup( $_blog_id = null ) {
1110
1111
		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...
1112
1113
		// Switch DB table prefixes
1114 View Code Duplication
		if ( null !== $_blog_id && $_blog_id != $wpdb->blogid ) {
1115
			switch_to_blog( pods_absint( $_blog_id ) );
1116
		} else {
1117
			$_blog_id = null;
1118
		}
1119
1120
		// Setup DB tables
1121
		$pods_version      = get_option( 'pods_framework_version' );
1122
		$pods_version_last = get_option( 'pods_framework_version_last' );
1123
1124
		// Install Pods
1125
		if ( empty( $pods_version ) ) {
1126
			pods_upgrade()->install( $_blog_id );
1127
1128
			$old_version = get_option( 'pods_version' );
1129
1130
			if ( ! empty( $old_version ) ) {
1131
				if ( false === strpos( $old_version, '.' ) ) {
1132
					$old_version = pods_version_to_point( $old_version );
1133
				}
1134
1135
				delete_option( 'pods_framework_version_last' );
1136
				add_option( 'pods_framework_version_last', $pods_version, '', 'yes' );
1137
1138
				self::$version_last = $old_version;
1139
			}
1140
		} // Upgrade Wizard needed
1141
		elseif ( $this->needs_upgrade( $pods_version, $pods_version_last ) ) {
1142
			// Do not do anything
1143
			return;
1144
		} // Update Pods and run any required DB updates
1145
		elseif ( version_compare( $pods_version, PODS_VERSION, '<=' ) ) {
1146
			if ( false !== apply_filters( 'pods_update_run', null, PODS_VERSION, $pods_version, $_blog_id ) && ! isset( $_GET['pods_bypass_update'] ) ) {
1147
				do_action( 'pods_update', PODS_VERSION, $pods_version, $_blog_id );
1148
1149
				// Update 2.0 alpha / beta sites
1150
				if ( version_compare( '2.0.0-a-1', $pods_version, '<=' ) && version_compare( $pods_version, '2.0.0-b-15', '<=' ) ) {
1151
					include( PODS_DIR . 'sql/update-2.0-beta.php' );
1152
				}
1153
1154
				if ( version_compare( $pods_version, PODS_DB_VERSION, '<=' ) ) {
1155
					include( PODS_DIR . 'sql/update.php' );
1156
				}
1157
1158
				do_action( 'pods_update_post', PODS_VERSION, $pods_version, $_blog_id );
1159
			}
1160
1161
			delete_option( 'pods_framework_version_last' );
1162
			add_option( 'pods_framework_version_last', $pods_version, '', 'yes' );
1163
1164
			self::$version_last = $pods_version;
1165
		}
1166
1167
		delete_option( 'pods_framework_version' );
1168
		add_option( 'pods_framework_version', PODS_VERSION, '', 'yes' );
1169
1170
		delete_option( 'pods_framework_db_version' );
1171
		add_option( 'pods_framework_db_version', PODS_DB_VERSION, '', 'yes' );
1172
1173
		self::$version = PODS_VERSION;
1174
		self::$db_version = PODS_DB_VERSION;
1175
1176
		pods_api()->cache_flush_pods();
1177
1178
		// Restore DB table prefix (if switched)
1179
		if ( null !== $_blog_id ) {
1180
			restore_current_blog();
1181
		}
1182
1183
	}
1184
1185
	/**
1186
	 * @param null $_blog_id
1187
	 */
1188
	public function reset( $_blog_id = null ) {
1189
1190
		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...
1191
1192
		// Switch DB table prefixes
1193 View Code Duplication
		if ( null !== $_blog_id && $_blog_id != $wpdb->blogid ) {
1194
			switch_to_blog( pods_absint( $_blog_id ) );
1195
		} else {
1196
			$_blog_id = null;
1197
		}
1198
1199
		$api = pods_api();
1200
1201
		$pods = $api->load_pods( array( 'names_ids' => true, 'table_info' => false ) );
1202
1203
		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...
1204
			$api->delete_pod( array( 'id' => $pod_id ) );
1205
		}
1206
1207
		$templates = $api->load_templates();
1208
1209
		foreach ( $templates as $template ) {
1210
			$api->delete_template( array( 'id' => $template['id'] ) );
1211
		}
1212
1213
		$pages = $api->load_pages();
1214
1215
		foreach ( $pages as $page ) {
1216
			$api->delete_page( array( 'id' => $page['id'] ) );
1217
		}
1218
1219
		$helpers = $api->load_helpers();
1220
1221
		foreach ( $helpers as $helper ) {
1222
			$api->delete_helper( array( 'id' => $helper['id'] ) );
1223
		}
1224
1225
		$tables = $wpdb->get_results( "SHOW TABLES LIKE '{$wpdb->prefix}pods%'", ARRAY_N );
1226
1227
		if ( ! empty( $tables ) ) {
1228
			foreach ( $tables as $table ) {
1229
				$table = $table[0];
1230
1231
				pods_query( "DROP TABLE `{$table}`", false );
1232
			}
1233
		}
1234
1235
		// Remove any orphans
1236
		$wpdb->query( "
1237
                DELETE `p`, `pm`
1238
                FROM `{$wpdb->posts}` AS `p`
1239
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1240
                    ON `pm`.`post_id` = `p`.`ID`
1241
                WHERE
1242
                    `p`.`post_type` LIKE '_pods_%'
1243
            " );
1244
1245
		delete_option( 'pods_framework_version' );
1246
		delete_option( 'pods_framework_db_version' );
1247
		delete_option( 'pods_framework_upgrade_2_0' );
1248
		delete_option( 'pods_framework_upgraded_1_x' );
1249
1250
		// @todo Make sure all entries are being cleaned and do something about the pods_framework_upgrade_{version} dynamic entries created by PodsUpgrade
1251
		delete_option( 'pods_framework_upgrade_2_0_0' );
1252
		delete_option( 'pods_framework_upgrade_2_0_sister_ids' );
1253
		delete_option( 'pods_framework_version_last' );
1254
1255
		delete_option( 'pods_component_settings' );
1256
1257
		$api->cache_flush_pods();
1258
1259
		pods_transient_clear( 'pods_flush_rewrites' );
1260
1261
		self::$version = '';
1262
1263
		// Restore DB table prefix (if switched)
1264
		if ( null !== $_blog_id ) {
1265
			restore_current_blog();
1266
		}
1267
	}
1268
1269
	public function run() {
1270
1271
		static $ran;
1272
1273
		if ( ! empty( $ran ) ) {
1274
			return;
1275
		}
1276
1277
		$ran = true;
1278
1279
		if ( ! did_action( 'plugins_loaded' ) ) {
1280
			add_action( 'plugins_loaded', array( $this, 'load_components' ), 11 );
1281
		} else {
1282
			$this->load_components();
1283
		}
1284
1285
		if ( ! did_action( 'setup_theme' ) ) {
1286
			add_action( 'setup_theme', array( $this, 'load_meta' ), 14 );
1287
		} else {
1288
			$this->load_meta();
1289
		}
1290
1291
		if ( ! did_action( 'init' ) ) {
1292
			add_action( 'init', array( $this, 'core' ), 11 );
1293
			add_action( 'init', array( $this, 'add_rest_support' ), 12 );
1294
			add_action( 'init', array( $this, 'setup_content_types' ), 11 );
1295
1296
			if ( is_admin() ) {
1297
				add_action( 'init', array( $this, 'admin_init' ), 12 );
1298
			}
1299
		} else {
1300
			$this->core();
1301
			$this->add_rest_support();
1302
			$this->setup_content_types();
1303
1304
			if ( is_admin() ) {
1305
				$this->admin_init();
1306
			}
1307
		}
1308
1309
		add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1310
		add_action( 'admin_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1311
		add_action( 'login_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1312
1313
		add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 );
1314
		add_action( 'delete_attachment', array( $this, 'delete_attachment' ) );
1315
1316
		// Register widgets
1317
		add_action( 'widgets_init', array( $this, 'register_widgets' ) );
1318
1319
		// Show admin bar links
1320
		add_action( 'admin_bar_menu', array( $this, 'admin_bar_links' ), 81 );
1321
1322
	}
1323
1324
	/**
1325
	 * Delete Attachments from relationships
1326
	 *
1327
	 * @param int $_ID
1328
	 */
1329
	public function delete_attachment( $_ID ) {
1330
1331
		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...
1332
1333
		$_ID = (int) $_ID;
1334
1335
		do_action( 'pods_delete_attachment', $_ID );
1336
1337
		$file_types = "'" . implode( "', '", PodsForm::file_field_types() ) . "'";
1338
1339
		if ( ! pods_tableless() ) {
1340
			$sql = "
1341
                DELETE `rel`
1342
                FROM `@wp_podsrel` AS `rel`
1343
                LEFT JOIN `{$wpdb->posts}` AS `p`
1344
                    ON
1345
                        `p`.`post_type` = '_pods_field'
1346
                        AND ( `p`.`ID` = `rel`.`field_id` OR `p`.`ID` = `rel`.`related_field_id` )
1347
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1348
                    ON
1349
                        `pm`.`post_id` = `p`.`ID`
1350
                        AND `pm`.`meta_key` = 'type'
1351
                        AND `pm`.`meta_value` IN ( {$file_types} )
1352
                WHERE
1353
                    `p`.`ID` IS NOT NULL
1354
                    AND `pm`.`meta_id` IS NOT NULL
1355
                    AND `rel`.`item_id` = {$_ID}";
1356
1357
			pods_query( $sql, false );
1358
		}
1359
1360
		// Post Meta
1361 View Code Duplication
		if ( ! empty( PodsMeta::$post_types ) ) {
1362
			$sql = "
1363
                DELETE `rel`
1364
                FROM `@wp_postmeta` AS `rel`
1365
                LEFT JOIN `{$wpdb->posts}` AS `p`
1366
                    ON
1367
                        `p`.`post_type` = '_pods_field'
1368
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1369
                    ON
1370
                        `pm`.`post_id` = `p`.`ID`
1371
                        AND `pm`.`meta_key` = 'type'
1372
                        AND `pm`.`meta_value` IN ( {$file_types} )
1373
                WHERE
1374
                    `p`.`ID` IS NOT NULL
1375
                    AND `pm`.`meta_id` IS NOT NULL
1376
                    AND `rel`.`meta_key` = `p`.`post_name`
1377
                    AND `rel`.`meta_value` = '{$_ID}'";
1378
1379
			pods_query( $sql, false );
1380
		}
1381
1382
		// User Meta
1383 View Code Duplication
		if ( ! empty( PodsMeta::$user ) ) {
1384
			$sql = "
1385
                DELETE `rel`
1386
                FROM `@wp_usermeta` AS `rel`
1387
                LEFT JOIN `{$wpdb->posts}` AS `p`
1388
                    ON
1389
                        `p`.`post_type` = '_pods_field'
1390
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1391
                    ON
1392
                        `pm`.`post_id` = `p`.`ID`
1393
                        AND `pm`.`meta_key` = 'type'
1394
                        AND `pm`.`meta_value` IN ( {$file_types} )
1395
                WHERE
1396
                    `p`.`ID` IS NOT NULL
1397
                    AND `pm`.`meta_id` IS NOT NULL
1398
                    AND `rel`.`meta_key` = `p`.`post_name`
1399
                    AND `rel`.`meta_value` = '{$_ID}'";
1400
1401
			pods_query( $sql, false );
1402
		}
1403
1404
		// Comment Meta
1405 View Code Duplication
		if ( ! empty( PodsMeta::$comment ) ) {
1406
			$sql = "
1407
                DELETE `rel`
1408
                FROM `@wp_commentmeta` AS `rel`
1409
                LEFT JOIN `{$wpdb->posts}` AS `p`
1410
                    ON
1411
                        `p`.`post_type` = '_pods_field'
1412
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1413
                    ON
1414
                        `pm`.`post_id` = `p`.`ID`
1415
                        AND `pm`.`meta_key` = 'type'
1416
                        AND `pm`.`meta_value` IN ( {$file_types} )
1417
                WHERE
1418
                    `p`.`ID` IS NOT NULL
1419
                    AND `pm`.`meta_id` IS NOT NULL
1420
                    AND `rel`.`meta_key` = `p`.`post_name`
1421
                    AND `rel`.`meta_value` = '{$_ID}'";
1422
1423
			pods_query( $sql, false );
1424
		}
1425
	}
1426
1427
	/**
1428
	 * Register widgets for Pods
1429
	 */
1430
	public function register_widgets() {
1431
1432
		$widgets = array(
1433
			'PodsWidgetSingle',
1434
			'PodsWidgetList',
1435
			'PodsWidgetField',
1436
			'PodsWidgetForm',
1437
			'PodsWidgetView'
1438
		);
1439
1440
		foreach ( $widgets as $widget ) {
1441
			if ( ! file_exists( PODS_DIR . 'classes/widgets/' . $widget . '.php' ) ) {
1442
				continue;
1443
			}
1444
1445
			require_once PODS_DIR . 'classes/widgets/' . $widget . '.php';
1446
1447
			register_widget( $widget );
1448
		}
1449
	}
1450
1451
	/**
1452
	 * Add Admin Bar links
1453
	 */
1454
	public function admin_bar_links() {
1455
1456
		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...
1457
1458
		if ( ! is_user_logged_in() || ! is_admin_bar_showing() ) {
1459
			return;
1460
		}
1461
1462
		$all_pods = pods_api()->load_pods( array( 'type' => 'pod', 'fields' => false, 'table_info' => false ) );
1463
1464
		// Add New item links for all pods
1465
		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...
1466
			if ( 0 == $pod['options']['show_in_menu'] ) {
1467
				continue;
1468
			}
1469
1470
			if ( ! pods_is_admin( array( 'pods', 'pods_content', 'pods_add_' . $pod['name'] ) ) ) {
1471
				continue;
1472
			}
1473
1474
			$singular_label = pods_var_raw( 'label_singular', $pod['options'], pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod['name'] ) ), null, true ), null, true );
1475
1476
			$wp_admin_bar->add_node( array(
1477
				'id'     => 'new-pod-' . $pod['name'],
1478
				'title'  => $singular_label,
1479
				'parent' => 'new-content',
1480
				'href'   => admin_url( 'admin.php?page=pods-manage-' . $pod['name'] . '&action=add' )
1481
			) );
1482
		}
1483
1484
		// Add edit link if we're on a pods page
1485
		if ( is_object( $pods ) && ! is_wp_error( $pods ) && ! empty( $pods->id ) && isset( $pods->pod_data ) && ! empty( $pods->pod_data ) && 'pod' == $pods->pod_data['type'] ) {
1486
			$pod = $pods->pod_data;
1487
1488
			if ( pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $pod['name'] ) ) ) {
1489
				$singular_label = pods_var_raw( 'label_singular', $pod['options'], pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod['name'] ) ), null, true ), null, true );
1490
1491
				$wp_admin_bar->add_node( array(
1492
					'title' => sprintf( __( 'Edit %s', 'pods' ), $singular_label ),
1493
					'id'    => 'edit-pod',
1494
					'href'  => admin_url( 'admin.php?page=pods-manage-' . $pod['name'] . '&action=edit&id=' . $pods->id() )
1495
				) );
1496
			}
1497
		}
1498
1499
	}
1500
1501
	/**
1502
	 * Add REST API support to post type and taxonomy objects.
1503
	 *
1504
	 * @uses  "init"
1505
	 *
1506
	 * @since 2.5.6
1507
	 */
1508
	public function add_rest_support() {
1509
1510
		if ( empty( self::$version ) ) {
1511
			return;
1512
		}
1513
1514
		static $rest_support_added;
1515
1516
		if ( ! function_exists( 'register_rest_field' ) ) {
1517
			return;
1518
		}
1519
1520
		include_once( PODS_DIR . 'classes/PodsRESTFields.php' );
1521
		include_once( PODS_DIR . 'classes/PodsRESTHandlers.php' );
1522
1523
		$rest_bases = pods_transient_get( 'pods_rest_bases' );
1524
1525
		if ( empty( $rest_bases ) ) {
1526
	        $pods = pods_api()->load_pods( array( 'type' => array( 'post_type', 'taxonomy', 'user', 'media', 'comment' ), 'fields' => false, 'table_info' => false ) );
1527
1528
			$rest_bases = array();
1529
1530
			if ( ! empty( $pods ) && is_array( $pods ) ) {
1531
				foreach ( $pods as $pod ) {
1532
					$type = $pod['type'];
1533
1534
					if ( in_array( $type, array( 'post_type', 'taxonomy', 'user', 'media', 'comment' ) ) ) {
1535
						if ( $pod && PodsRESTHandlers::pod_extends_core_route( $pod ) ) {
1536
							$rest_bases[ $pod['name'] ] = array(
1537
								'type' => $type,
1538
								'base' => sanitize_title( pods_v( 'rest_base', $pod['options'], $pod['name'] ) ),
1539
							);
1540
						}
1541
					}
1542
				}
1543
			}
1544
1545
			if ( empty( $rest_bases ) ) {
1546
				$rest_bases = 'none';
1547
			}
1548
1549
			pods_transient_set( 'pods_rest_bases', $rest_bases );
1550
		}
1551
1552
		if ( empty( $rest_support_added ) && ! empty( $rest_bases ) && 'none' !== $rest_bases ) {
1553
			foreach ( $rest_bases as $pod_name => $pod_info ) {
1554
				$pod_type  = $pod_info['type'];
1555
				$rest_base = $pod_info['base'];
1556
1557
				if ( 'post_type' == $pod_type ) {
1558
					PodsRESTHandlers::post_type_rest_support( $pod_name, $rest_base );
1559
				} elseif ( 'taxonomy' == $pod_type ) {
1560
					PodsRESTHandlers::taxonomy_rest_support( $pod_name, $rest_base );
1561
				}
1562
1563
				new PodsRESTFields( $pod_name );
1564
			}
1565
1566
			$rest_support_added = true;
1567
		}
1568
1569
	}
1570
}
1571