Completed
Pull Request — 2.x (#3586)
by
unknown
10:10 queued 03:54
created

PodsInit::setup_updated_messages()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

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