Completed
Pull Request — 2.x (#3857)
by
unknown
05:11
created

PodsInit::setup()   C

Complexity

Conditions 14
Paths 38

Size

Total Lines 75
Code Lines 39

Duplication

Lines 4
Ratio 5.33 %

Importance

Changes 0
Metric Value
cc 14
eloc 39
c 0
b 0
f 0
nc 38
nop 1
dl 4
loc 75
rs 5.3217

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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