Completed
Push — 2.x ( ad7fbb...779dde )
by Scott Kingsley
06:37 queued 02:19
created

PodsInit::flush_rewrite_rules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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