Completed
Push — feature/codesniffer-fixes ( c7a4f1...41059d )
by Scott Kingsley
13:22
created

PodsInit::plugins_loaded()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package Pods
5
 */
6
class PodsInit {
7
8
	/**
9
	 * @var PodsInit
10
	 */
11
	public static $instance = null;
12
13
	/**
14
	 * @var array
15
	 */
16
	public static $no_conflict = array();
17
18
	/**
19
	 * @var array
20
	 */
21
	public static $content_types_registered = array();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $content_types_registered exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
22
23
	/**
24
	 * @var PodsComponents
25
	 */
26
	public static $components;
27
28
	/**
29
	 * @var PodsMeta
30
	 */
31
	public static $meta;
32
33
	/**
34
	 * @var PodsI18n
35
	 */
36
	public static $i18n;
37
38
	/**
39
	 * @var PodsAdmin
40
	 */
41
	public static $admin;
42
43
	/**
44
	 * @var mixed|void
45
	 */
46
	public static $version;
47
48
	/**
49
	 * @var mixed|void
50
	 */
51
	public static $version_last;
52
53
	/**
54
	 * @var mixed|void
55
	 */
56
	public static $db_version;
57
58
	/**
59
	 * Upgrades to trigger (last installed version => upgrade version)
60
	 *
61
	 * @var array
62
	 */
63
	public static $upgrades = array(
64
		'1.0.0' => '2.0.0',
65
		// '2.0.0' => '2.1.0'
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...
66
	);
67
68
	/**
69
	 * Whether an Upgrade for 1.x has happened
70
	 *
71
	 * @var bool
72
	 */
73
	public static $upgraded;
74
75
	/**
76
	 * Whether an Upgrade is needed
77
	 *
78
	 * @var bool
79
	 */
80
	public static $upgrade_needed = false;
81
82
	/**
83
	 * Singleton handling for a basic pods_init() request
84
	 *
85
	 * @return \PodsInit
86
	 *
87
	 * @since 2.3.5
88
	 */
89
	public static function init() {
90
91
		if ( ! is_object( self::$instance ) ) {
92
			self::$instance = new self();
93
		}
94
95
		return self::$instance;
96
	}
97
98
	/**
99
	 * Setup and Initiate Pods
100
	 *
101
	 * @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...
102
	 *
103
	 * @license http://www.gnu.org/licenses/gpl-2.0.html
104
	 * @since   1.8.9
105
	 */
106
	public function __construct() {
107
108
		self::$version      = get_option( 'pods_framework_version' );
109
		self::$version_last = get_option( 'pods_framework_version_last' );
110
		self::$db_version   = get_option( 'pods_framework_db_version' );
111
		self::$upgraded     = (int) get_option( 'pods_framework_upgraded_1_x' );
112
113
		if ( empty( self::$version_last ) && 0 < strlen( get_option( 'pods_version' ) ) ) {
114
			$old_version = get_option( 'pods_version' );
115
116
			if ( ! empty( $old_version ) ) {
117
				if ( false === strpos( $old_version, '.' ) ) {
118
					$old_version = pods_version_to_point( $old_version );
119
				}
120
121
				update_option( 'pods_framework_version_last', $old_version );
122
123
				self::$version_last = $old_version;
124
			}
125
		}
126
127
		self::$upgrade_needed = $this->needs_upgrade();
128
129
		add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
130
		add_action( 'plugins_loaded', array( $this, 'activate_install' ), 9 );
131
132
		add_action( 'wp_loaded', array( $this, 'flush_rewrite_rules' ) );
133
134
		$this->run();
135
136
	}
137
138
	/**
139
	 * Load the plugin textdomain and set default constants
140
	 */
141
	public function plugins_loaded() {
142
143
		if ( ! defined( 'PODS_LIGHT' ) ) {
144
			define( 'PODS_LIGHT', false );
145
		}
146
147
		if ( ! defined( 'PODS_TABLELESS' ) ) {
148
			define( 'PODS_TABLELESS', false );
149
		}
150
151
		load_plugin_textdomain( 'pods' );
152
153
	}
154
155
	/**
156
	 * Load Pods Components
157
	 */
158
	public function load_components() {
159
160
		if ( empty( self::$version ) ) {
161
			return;
162
		}
163
164
		if ( ! defined( 'PODS_LIGHT' ) || ! PODS_LIGHT ) {
165
			self::$components = pods_components();
166
		}
167
168
	}
169
170
	/**
171
	 * Load Pods Meta
172
	 */
173
	public function load_meta() {
174
175
		self::$meta = pods_meta()->core();
176
	}
177
178
	/**
179
	 *
180
	 */
181
	public function load_i18n() {
182
183
		self::$i18n = pods_i18n();
184
	}
185
186
	/**
187
	 * Set up the Pods core
188
	 */
189
	public function core() {
190
191
		if ( empty( self::$version ) ) {
192
			return;
193
		}
194
195
		// Session start
196
		pods_session_start();
197
198
		add_shortcode( 'pods', 'pods_shortcode' );
199
		add_shortcode( 'pods-form', 'pods_shortcode_form' );
200
201
		$security_settings = array(
202
			'pods_disable_file_browser'     => 0,
203
			'pods_files_require_login'      => 1,
204
			'pods_files_require_login_cap'  => '',
205
			'pods_disable_file_upload'      => 0,
206
			'pods_upload_require_login'     => 1,
207
			'pods_upload_require_login_cap' => '',
208
		);
209
210
		foreach ( $security_settings as $security_setting => $setting ) {
211
			$setting = get_option( $security_setting );
212
			if ( ! empty( $setting ) ) {
213
				$security_settings[ $security_setting ] = $setting;
214
			}
215
		}
216
217
		foreach ( $security_settings as $security_setting => $setting ) {
218
			if ( 0 === (int) $setting ) {
219
				$setting = false;
220
			} elseif ( 1 === (int) $setting ) {
221
				$setting = true;
222
			}
223
224
			if ( in_array(
225
				$security_setting, array(
226
					'pods_files_require_login',
227
					'pods_upload_require_login',
228
				), true
229
			) ) {
230
				if ( 0 < strlen( $security_settings[ $security_setting . '_cap' ] ) ) {
231
					$setting = $security_settings[ $security_setting . '_cap' ];
232
				}
233
			} elseif ( in_array(
234
				$security_setting, array(
235
					'pods_files_require_login_cap',
236
					'pods_upload_require_login_cap',
237
				), true
238
			) ) {
239
				continue;
240
			}
241
242
			if ( ! defined( strtoupper( $security_setting ) ) ) {
243
				define( strtoupper( $security_setting ), $setting );
244
			}
245
		}//end foreach
246
247
		$this->register_pods();
248
249
		$avatar = PodsForm::field_loader( 'avatar' );
250
251
		if ( method_exists( $avatar, 'get_avatar' ) ) {
252
			add_filter( 'get_avatar', array( $avatar, 'get_avatar' ), 10, 4 );
253
		}
254
	}
255
256
	/**
257
	 * Register Scripts and Styles
258
	 */
259
	public function register_assets() {
260
261
		$maybe_min = SCRIPT_DEBUG ? '' : '.min';
262
263
		wp_register_script( 'pods-json', PODS_URL . 'ui/js/jquery.json.js', array( 'jquery' ), '2.3' );
264
265
		if ( ! wp_script_is( 'jquery-qtip2', 'registered' ) ) {
266
			wp_register_script( 'jquery-qtip2', PODS_URL . 'ui/js/jquery.qtip.min.js', array( 'jquery' ), '2.2' );
267
		}
268
269
		wp_register_script(
270
			'pods', PODS_URL . 'ui/js/jquery.pods.js', array(
271
				'jquery',
272
				'pods-dfv',
273
				'pods-i18n',
274
				'pods-json',
275
				'jquery-qtip2',
276
			), PODS_VERSION, true
277
		);
278
279
		wp_register_script( 'pods-cleditor', PODS_URL . 'ui/js/jquery.cleditor.min.js', array( 'jquery' ), '1.3.0' );
280
281
		wp_register_script( 'pods-codemirror', PODS_URL . 'ui/js/codemirror.js', array(), '4.8', true );
282
		wp_register_script( 'pods-codemirror-loadmode', PODS_URL . 'ui/js/codemirror/addon/mode/loadmode.js', array( 'pods-codemirror' ), '4.8', true );
283
		wp_register_script( 'pods-codemirror-overlay', PODS_URL . 'ui/js/codemirror/addon/mode/overlay.js', array( 'pods-codemirror' ), '4.8', true );
284
		wp_register_script( 'pods-codemirror-hints', PODS_URL . 'ui/js/codemirror/addon/mode/show-hint.js', array( 'pods-codemirror' ), '4.8', true );
285
		wp_register_script( 'pods-codemirror-mode-xml', PODS_URL . 'ui/js/codemirror/mode/xml/xml.js', array( 'pods-codemirror' ), '4.8', true );
286
		wp_register_script( 'pods-codemirror-mode-html', PODS_URL . 'ui/js/codemirror/mode/htmlmixed/htmlmixed.js', array( 'pods-codemirror' ), '4.8', true );
287
		wp_register_script( 'pods-codemirror-mode-css', PODS_URL . 'ui/js/codemirror/mode/css/css.js', array( 'pods-codemirror' ), '4.8', true );
288
289
		if ( ! wp_script_is( 'jquery-ui-slideraccess', 'registered' ) ) {
290
			// No need to add dependencies. All managed by jquery-ui-timepicker.
291
			wp_register_script( 'jquery-ui-slideraccess', PODS_URL . 'ui/js/timepicker/jquery-ui-sliderAccess.js', array(), '0.3' );
292
		}
293
294
		if ( ! wp_script_is( 'jquery-ui-timepicker', 'registered' ) ) {
295
			wp_register_script(
296
				'jquery-ui-timepicker', PODS_URL . 'ui/js/timepicker/jquery-ui-timepicker-addon.min.js', array(
297
					'jquery',
298
					'jquery-ui-core',
299
					'jquery-ui-datepicker',
300
					'jquery-ui-slider',
301
					'jquery-ui-slideraccess',
302
				), '1.6.3'
303
			);
304
		}
305
		if ( ! wp_style_is( 'jquery-ui-timepicker', 'registered' ) ) {
306
			wp_register_style( 'jquery-ui-timepicker', PODS_URL . 'ui/js/timepicker/jquery-ui-timepicker-addon.min.css', array(), '1.6.3' );
307
		}
308
309
		wp_register_script(
310
			'pods-select2', PODS_URL . "ui/js/selectWoo/selectWoo{$maybe_min}.js", array(
311
				'jquery',
312
				'pods-i18n',
313
			), '1.0.1'
314
		);
315
		wp_register_style( 'pods-select2', PODS_URL . "ui/js/selectWoo/selectWoo{$maybe_min}.css", array(), '1.0.1' );
316
317
		$register_handlebars = apply_filters( 'pods_script_register_handlebars', true );
318
319
		if ( is_admin() ) {
320
			$screen = get_current_screen();
321
322
			// Deregister the outdated Pods handlebars script on TEC event screen
323
			if ( $screen && 'tribe_events' === $screen->post_type ) {
324
				$register_handlebars = false;
325
			}
326
		}
327
328
		if ( $register_handlebars ) {
329
			wp_register_script( 'pods-handlebars', PODS_URL . 'ui/js/handlebars.js', array(), '1.0.0.beta.6' );
330
		}
331
332
		// Marionette dependencies for MV fields
333
		wp_register_script( 'backbone.radio', PODS_URL . 'ui/js/marionette/backbone.radio.js', array( 'backbone' ), '2.0.0', true );
334
		wp_register_script(
335
			'marionette', PODS_URL . 'ui/js/marionette/backbone.marionette.js', array(
336
				'backbone',
337
				'backbone.radio',
338
			), '3.1.0', true
339
		);
340
341
		// MV stuff
342
		wp_register_script(
343
			'pods-dfv', PODS_URL . 'ui/js/pods-dfv/pods-dfv.min.js', array(
344
				'jquery',
345
				'jquery-ui-core',
346
				'jquery-ui-sortable',
347
				'pods-i18n',
348
				'marionette',
349
				'media-views',
350
				'media-models',
351
			), PODS_VERSION, true
352
		);
353
354
		// Check if Pod is a Modal Window
355
		if ( pods_is_modal_window() ) {
356
			add_filter( 'body_class', array( $this, 'add_classes_to_body_class' ) );
357
			add_filter( 'admin_body_class', array( $this, 'add_classes_to_body_class' ) );
358
		}
359
360
		// As of 2.7 we combine styles to just three .css files
361
		wp_register_style( 'pods-styles', PODS_URL . 'ui/styles/dist/pods.css', array(), PODS_VERSION );
362
		wp_register_style( 'pods-wizard', PODS_URL . 'ui/styles/dist/pods-wizard.css', array(), PODS_VERSION );
363
		wp_register_style( 'pods-form', PODS_URL . 'ui/styles/dist/pods-form.css', array(), PODS_VERSION );
364
365
	}
366
367
	/**
368
	 * @param string $classes Body classes.
369
	 *
370
	 * @return string
371
	 */
372
	public function add_classes_to_body_class( $classes ) {
373
374
		$classes .= 'pods-modal-window';
375
376
		return $classes;
377
	}
378
379
	/**
380
	 * Register internal Post Types
381
	 */
382
	public function register_pods() {
383
384
		$args = array(
385
			'label'           => 'Pods',
386
			'labels'          => array( 'singular_name' => 'Pod' ),
387
			'public'          => false,
388
			'can_export'      => false,
389
			'query_var'       => false,
390
			'rewrite'         => false,
391
			'capability_type' => 'pods_pod',
392
			'has_archive'     => false,
393
			'hierarchical'    => false,
394
			'supports'        => array( 'title', 'author' ),
395
			'menu_icon'       => 'dashicons-pods',
396
		);
397
398
		$args = self::object_label_fix( $args, 'post_type' );
399
400
		register_post_type( '_pods_pod', apply_filters( 'pods_internal_register_post_type_pod', $args ) );
401
402
		$args = array(
403
			'label'           => 'Pod Fields',
404
			'labels'          => array( 'singular_name' => 'Pod Field' ),
405
			'public'          => false,
406
			'can_export'      => false,
407
			'query_var'       => false,
408
			'rewrite'         => false,
409
			'capability_type' => 'pods_pod',
410
			'has_archive'     => false,
411
			'hierarchical'    => true,
412
			'supports'        => array( 'title', 'editor', 'author' ),
413
			'menu_icon'       => 'dashicons-pods',
414
		);
415
416
		$args = self::object_label_fix( $args, 'post_type' );
417
418
		register_post_type( '_pods_field', apply_filters( 'pods_internal_register_post_type_field', $args ) );
419
	}
420
421
	/**
422
	 * Include Admin
423
	 */
424
	public function admin_init() {
425
426
		self::$admin = pods_admin();
427
	}
428
429
	/**
430
	 * Register Post Types and Taxonomies
431
	 *
432
	 * @param bool $force
433
	 */
434
	public function setup_content_types( $force = false ) {
435
436
		if ( empty( self::$version ) ) {
437
			return;
438
		}
439
440
		require_once PODS_DIR . 'classes/PodsRESTHandlers.php';
441
		require_once PODS_DIR . 'classes/PodsRESTFields.php';
442
443
		$post_types = PodsMeta::$post_types;
444
		$taxonomies = PodsMeta::$taxonomies;
445
446
		$existing_post_types = get_post_types();
447
		$existing_taxonomies = get_taxonomies();
448
449
		$pods_cpt_ct = pods_transient_get( 'pods_wp_cpt_ct' );
450
451
		$cpt_positions = array();
452
453
		if ( empty( $pods_cpt_ct ) && ( ! empty( $post_types ) || ! empty( $taxonomies ) ) ) {
454
			$force = true;
455
		} elseif ( ! empty( $pods_cpt_ct ) && empty( $pods_cpt_ct['post_types'] ) && ! empty( $post_types ) ) {
456
			$force = true;
457
		} elseif ( ! empty( $pods_cpt_ct ) && empty( $pods_cpt_ct['taxonomies'] ) && ! empty( $taxonomies ) ) {
458
			$force = true;
459
		}
460
461
		if ( false === $pods_cpt_ct || $force ) {
462
			/**
463
			 * @var WP_Query
464
			 */
465
			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...
466
467
			$reserved_query_vars = array(
468
				'post_type',
469
				'taxonomy',
470
				'output',
471
			);
472
473
			if ( is_object( $wp_query ) ) {
474
				$reserved_query_vars = array_merge( $reserved_query_vars, array_keys( $wp_query->fill_query_vars( array() ) ) );
475
			}
476
477
			$pods_cpt_ct = array(
478
				'post_types' => array(),
479
				'taxonomies' => array(),
480
			);
481
482
			$pods_post_types      = $pods_taxonomies = array();
483
			$supported_post_types = $supported_taxonomies = array();
484
485
			$post_format_post_types = array();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $post_format_post_types exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
486
487
			foreach ( $post_types as $post_type ) {
488
				if ( isset( $pods_cpt_ct['post_types'][ $post_type['name'] ] ) ) {
489
					// Post type was setup already
490
					continue;
491
				} elseif ( ! empty( $post_type['object'] ) && isset( $existing_post_types[ $post_type['object'] ] ) ) {
492
					// Post type exists already
493
					continue;
494
				} elseif ( ! $force && isset( $existing_post_types[ $post_type['name'] ] ) ) {
495
					// Post type was setup and exists already, but we aren't forcing it to be setup again
496
					continue;
497
				}
498
499
				$post_type['options']['name'] = $post_type['name'];
500
				$post_type                    = array_merge( $post_type, (array) $post_type['options'] );
501
502
				$post_type_name = pods_v_sanitized( 'name', $post_type );
503
504
				// Labels
505
				$cpt_label    = esc_html( pods_v( 'label', $post_type, ucwords( str_replace( '_', ' ', pods_v( 'name', $post_type ) ) ), true ) );
506
				$cpt_singular = esc_html( pods_v( 'label_singular', $post_type, ucwords( str_replace( '_', ' ', pods_v( 'label', $post_type, $post_type_name, true ) ) ), true ) );
507
508
				$cpt_labels                          = array();
509
				$cpt_labels['name']                  = $cpt_label;
510
				$cpt_labels['singular_name']         = $cpt_singular;
511
				$cpt_labels['menu_name']             = pods_v( 'menu_name', $post_type, '', true );
512
				$cpt_labels['name_admin_bar']        = pods_v( 'name_admin_bar', $post_type, '', true );
513
				$cpt_labels['add_new']               = pods_v( 'label_add_new', $post_type, '', true );
514
				$cpt_labels['add_new_item']          = pods_v( 'label_add_new_item', $post_type, '', true );
515
				$cpt_labels['new_item']              = pods_v( 'label_new_item', $post_type, '', true );
516
				$cpt_labels['edit']                  = pods_v( 'label_edit', $post_type, '', true );
517
				$cpt_labels['edit_item']             = pods_v( 'label_edit_item', $post_type, '', true );
518
				$cpt_labels['view']                  = pods_v( 'label_view', $post_type, '', true );
519
				$cpt_labels['view_item']             = pods_v( 'label_view_item', $post_type, '', true );
520
				$cpt_labels['view_items']            = pods_v( 'label_view_items', $post_type, '', true );
521
				$cpt_labels['all_items']             = pods_v( 'label_all_items', $post_type, '', true );
522
				$cpt_labels['search_items']          = pods_v( 'label_search_items', $post_type, '', true );
523
				$cpt_labels['not_found']             = pods_v( 'label_not_found', $post_type, '', true );
524
				$cpt_labels['not_found_in_trash']    = pods_v( 'label_not_found_in_trash', $post_type, '', true );
525
				$cpt_labels['parent']                = pods_v( 'label_parent', $post_type, '', true );
526
				$cpt_labels['parent_item_colon']     = pods_v( 'label_parent_item_colon', $post_type, '', true );
527
				$cpt_labels['archives']              = pods_v( 'label_archives', $post_type, '', true );
528
				$cpt_labels['attributes']            = pods_v( 'label_attributes', $post_type, '', true );
529
				$cpt_labels['insert_into_item']      = pods_v( 'label_insert_into_item', $post_type, '', true );
530
				$cpt_labels['uploaded_to_this_item'] = pods_v( 'label_uploaded_to_this_item', $post_type, '', true );
531
				$cpt_labels['featured_image']        = pods_v( 'label_featured_image', $post_type, '', true );
532
				$cpt_labels['set_featured_image']    = pods_v( 'label_set_featured_image', $post_type, '', true );
533
				$cpt_labels['remove_featured_image'] = pods_v( 'label_remove_featured_image', $post_type, '', true );
534
				$cpt_labels['use_featured_image']    = pods_v( 'label_use_featured_image', $post_type, '', true );
535
				$cpt_labels['filter_items_list']     = pods_v( 'label_filter_items_list', $post_type, '', true );
536
				$cpt_labels['items_list_navigation'] = pods_v( 'label_items_list_navigation', $post_type, '', true );
537
				$cpt_labels['items_list']            = pods_v( 'label_items_list', $post_type, '', true );
538
539
				// Supported
540
				$cpt_supported = array(
541
					'title'           => (boolean) pods_v( 'supports_title', $post_type, false ),
542
					'editor'          => (boolean) pods_v( 'supports_editor', $post_type, false ),
543
					'author'          => (boolean) pods_v( 'supports_author', $post_type, false ),
544
					'thumbnail'       => (boolean) pods_v( 'supports_thumbnail', $post_type, false ),
545
					'excerpt'         => (boolean) pods_v( 'supports_excerpt', $post_type, false ),
546
					'trackbacks'      => (boolean) pods_v( 'supports_trackbacks', $post_type, false ),
547
					'custom-fields'   => (boolean) pods_v( 'supports_custom_fields', $post_type, false ),
548
					'comments'        => (boolean) pods_v( 'supports_comments', $post_type, false ),
549
					'revisions'       => (boolean) pods_v( 'supports_revisions', $post_type, false ),
550
					'page-attributes' => (boolean) pods_v( 'supports_page_attributes', $post_type, false ),
551
					'post-formats'    => (boolean) pods_v( 'supports_post_formats', $post_type, false ),
552
				);
553
554
				// Custom Supported
555
				$cpt_supported_custom = pods_v_sanitized( 'supports_custom', $post_type, '' );
556
557
				if ( ! empty( $cpt_supported_custom ) ) {
558
					$cpt_supported_custom = explode( ',', $cpt_supported_custom );
559
					$cpt_supported_custom = array_filter( array_unique( $cpt_supported_custom ) );
560
561
					foreach ( $cpt_supported_custom as $cpt_support ) {
562
						$cpt_supported[ $cpt_support ] = true;
563
					}
564
				}
565
566
				// Genesis Support
567
				if ( function_exists( 'genesis' ) ) {
568
					$cpt_supported['genesis-seo']             = (boolean) pods_v( 'supports_genesis_seo', $post_type, false );
569
					$cpt_supported['genesis-layouts']         = (boolean) pods_v( 'supports_genesis_layouts', $post_type, false );
570
					$cpt_supported['genesis-simple-sidebars'] = (boolean) pods_v( 'supports_genesis_simple_sidebars', $post_type, false );
571
				}
572
573
				// YARPP Support
574
				if ( defined( 'YARPP_VERSION' ) ) {
575
					$cpt_supported['yarpp_support'] = (boolean) pods_v( 'supports_yarpp_support', $post_type, false );
576
				}
577
578
				// Jetpack Support
579
				if ( class_exists( 'Jetpack' ) ) {
580
					$cpt_supported['supports_jetpack_publicize'] = (boolean) pods_v( 'supports_jetpack_publicize', $post_type, false );
581
					$cpt_supported['supports_jetpack_markdown']  = (boolean) pods_v( 'supports_jetpack_markdown', $post_type, false );
582
				}
583
584
				$cpt_supports = array();
585
586
				foreach ( $cpt_supported as $cpt_support => $supported ) {
587
					if ( true === $supported ) {
588
						$cpt_supports[] = $cpt_support;
589
590
						if ( 'post-formats' === $cpt_support ) {
591
							$post_format_post_types[] = $post_type_name;
592
						}
593
					}
594
				}
595
596
				if ( empty( $cpt_supports ) ) {
597
					$cpt_supports = false;
598
				}
599
600
				// Rewrite
601
				$cpt_rewrite       = (boolean) pods_v( 'rewrite', $post_type, true );
602
				$cpt_rewrite_array = array(
603
					'slug'       => pods_v( 'rewrite_custom_slug', $post_type, str_replace( '_', '-', $post_type_name ), true ),
604
					'with_front' => (boolean) pods_v( 'rewrite_with_front', $post_type, true ),
605
					'feeds'      => (boolean) pods_v( 'rewrite_feeds', $post_type, (boolean) pods_v( 'has_archive', $post_type, false ) ),
606
					'pages'      => (boolean) pods_v( 'rewrite_pages', $post_type, true ),
607
				);
608
609
				if ( false !== $cpt_rewrite ) {
610
					$cpt_rewrite = $cpt_rewrite_array;
611
				}
612
613
				$capability_type = pods_v( 'capability_type', $post_type, 'post' );
614
615
				if ( 'custom' === $capability_type ) {
616
					$capability_type = pods_v( 'capability_type_custom', $post_type, 'post' );
617
				}
618
619
				$show_in_menu = (boolean) pods_v( 'show_in_menu', $post_type, true );
620
621
				if ( $show_in_menu && 0 < strlen( pods_v( 'menu_location_custom', $post_type ) ) ) {
622
					$show_in_menu = pods_v( 'menu_location_custom', $post_type );
623
				}
624
625
				$menu_icon = pods_v( 'menu_icon', $post_type );
626
627
				if ( ! empty( $menu_icon ) ) {
628
					$menu_icon = pods_evaluate_tags( $menu_icon );
629
				}
630
631
				// Register Post Type
632
				$pods_post_types[ $post_type_name ] = array(
633
					'label'               => $cpt_label,
634
					'labels'              => $cpt_labels,
635
					'description'         => esc_html( pods_v( 'description', $post_type ) ),
636
					'public'              => (boolean) pods_v( 'public', $post_type, true ),
637
					'publicly_queryable'  => (boolean) pods_v( 'publicly_queryable', $post_type, (boolean) pods_v( 'public', $post_type, true ) ),
638
					'exclude_from_search' => (boolean) pods_v( 'exclude_from_search', $post_type, ( (boolean) pods_v( 'public', $post_type, true ) ? false : true ) ),
639
					'show_ui'             => (boolean) pods_v( 'show_ui', $post_type, (boolean) pods_v( 'public', $post_type, true ) ),
640
					'show_in_menu'        => $show_in_menu,
641
					'show_in_nav_menus'   => (boolean) pods_v( 'show_in_nav_menus', $post_type, (boolean) pods_v( 'public', $post_type, true ) ),
642
					'show_in_admin_bar'   => (boolean) pods_v( 'show_in_admin_bar', $post_type, (boolean) pods_v( 'show_in_menu', $post_type, true ) ),
643
					'menu_position'       => (int) pods_v( 'menu_position', $post_type, 0, true ),
644
					'menu_icon'           => $menu_icon,
645
					'capability_type'     => $capability_type,
646
					// 'capabilities' => $cpt_capabilities,
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...
647
					'map_meta_cap'        => (boolean) pods_v( 'capability_type_extra', $post_type, true ),
648
					'hierarchical'        => (boolean) pods_v( 'hierarchical', $post_type, false ),
649
					'supports'            => $cpt_supports,
650
					// 'register_meta_box_cb' => array($this, 'manage_meta_box'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
651
					// 'permalink_epmask' => EP_PERMALINK,
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
652
					'has_archive'         => pods_v( 'has_archive_slug', $post_type, (boolean) pods_v( 'has_archive', $post_type, false ), true ),
653
					'rewrite'             => $cpt_rewrite,
654
					'query_var'           => ( false !== (boolean) pods_v( 'query_var', $post_type, true ) ? pods_v( 'query_var_string', $post_type, $post_type_name, true ) : false ),
655
					'can_export'          => (boolean) pods_v( 'can_export', $post_type, true ),
656
				);
657
658
				// REST API
659
				$rest_enabled = (boolean) pods_v( 'rest_enable', $post_type, false );
660
661
				if ( $rest_enabled ) {
662
					$rest_base = sanitize_title( pods_v( 'rest_base', $post_type, $post_type_name ) );
663
664
					$pods_post_types[ $post_type_name ]['show_in_rest']          = true;
665
					$pods_post_types[ $post_type_name ]['rest_base']             = $rest_base;
666
					$pods_post_types[ $post_type_name ]['rest_controller_class'] = 'WP_REST_Posts_Controller';
667
				}
668
669
				// YARPP doesn't use 'supports' array option (yet)
670
				if ( ! empty( $cpt_supports['yarpp_support'] ) ) {
671
					$pods_post_types[ $post_type_name ]['yarpp_support'] = true;
672
				}
673
674
				// Prevent reserved query_var issues
675
				if ( in_array( $pods_post_types[ $post_type_name ]['query_var'], $reserved_query_vars, true ) ) {
676
					$pods_post_types[ $post_type_name ]['query_var'] = 'post_type_' . $pods_post_types[ $post_type_name ]['query_var'];
677
				}
678
679
				if ( 25 === (int) $pods_post_types[ $post_type_name ]['menu_position'] ) {
680
					$pods_post_types[ $post_type_name ]['menu_position'] ++;
681
				}
682
683
				if ( $pods_post_types[ $post_type_name ]['menu_position'] < 1 || in_array( $pods_post_types[ $post_type_name ]['menu_position'], $cpt_positions, true ) ) {
684
					unset( $pods_post_types[ $post_type_name ]['menu_position'] );
685
				} else {
686
					$cpt_positions[] = $pods_post_types[ $post_type_name ]['menu_position'];
687
688
					// This would be nice if WP supported floats in menu_position
689
					// $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...
690
				}
691
692
				// Taxonomies
693
				$cpt_taxonomies = array();
694
				$_taxonomies    = get_taxonomies();
695
				$_taxonomies    = array_merge_recursive( $_taxonomies, $pods_taxonomies );
696
				$ignore         = array( 'nav_menu', 'link_category', 'post_format' );
697
698
				foreach ( $_taxonomies as $taxonomy => $label ) {
699
					if ( in_array( $taxonomy, $ignore, true ) ) {
700
						continue;
701
					}
702
703
					if ( false !== (boolean) pods_v( 'built_in_taxonomies_' . $taxonomy, $post_type, false ) ) {
704
						$cpt_taxonomies[] = $taxonomy;
705
706
						if ( isset( $supported_post_types[ $taxonomy ] ) && ! in_array( $post_type_name, $supported_post_types[ $taxonomy ], true ) ) {
707
							$supported_post_types[ $taxonomy ][] = $post_type_name;
708
						}
709
					}
710
				}
711
712
				if ( isset( $supported_taxonomies[ $post_type_name ] ) ) {
713
					$supported_taxonomies[ $post_type_name ] = array_merge( (array) $supported_taxonomies[ $post_type_name ], $cpt_taxonomies );
714
				} else {
715
					$supported_taxonomies[ $post_type_name ] = $cpt_taxonomies;
716
				}
717
			}//end foreach
718
719
			foreach ( $taxonomies as $taxonomy ) {
720
				if ( isset( $pods_cpt_ct['taxonomies'][ $taxonomy['name'] ] ) ) {
721
					// Taxonomy was setup already
722
					continue;
723
				} elseif ( ! empty( $taxonomy['object'] ) && isset( $existing_taxonomies[ $taxonomy['object'] ] ) ) {
724
					// Taxonomy exists already
725
					continue;
726
				} elseif ( ! $force && isset( $existing_taxonomies[ $taxonomy['name'] ] ) ) {
727
					// Taxonomy was setup and exists already, but we aren't forcing it to be setup again
728
					continue;
729
				}
730
731
				$taxonomy['options']['name'] = $taxonomy['name'];
732
				$taxonomy                    = array_merge( $taxonomy, (array) $taxonomy['options'] );
733
734
				$taxonomy_name = pods_v( 'name', $taxonomy );
735
736
				// Labels
737
				$ct_label    = esc_html( pods_v( 'label', $taxonomy, ucwords( str_replace( '_', ' ', pods_v( 'name', $taxonomy ) ) ), true ) );
738
				$ct_singular = esc_html( pods_v( 'label_singular', $taxonomy, ucwords( str_replace( '_', ' ', pods_v( 'label', $taxonomy, pods_v( 'name', $taxonomy ), true ) ) ), true ) );
739
740
				$ct_labels                               = array();
741
				$ct_labels['name']                       = $ct_label;
742
				$ct_labels['singular_name']              = $ct_singular;
743
				$ct_labels['menu_name']                  = pods_v( 'menu_name', $taxonomy, '', true );
744
				$ct_labels['search_items']               = pods_v( 'label_search_items', $taxonomy, '', true );
745
				$ct_labels['popular_items']              = pods_v( 'label_popular_items', $taxonomy, '', true );
746
				$ct_labels['all_items']                  = pods_v( 'label_all_items', $taxonomy, '', true );
747
				$ct_labels['parent_item']                = pods_v( 'label_parent_item', $taxonomy, '', true );
748
				$ct_labels['parent_item_colon']          = pods_v( 'label_parent_item_colon', $taxonomy, '', true );
749
				$ct_labels['edit_item']                  = pods_v( 'label_edit_item', $taxonomy, '', true );
750
				$ct_labels['update_item']                = pods_v( 'label_update_item', $taxonomy, '', true );
751
				$ct_labels['view_item']                  = pods_v( 'label_view_item', $taxonomy, '', true );
752
				$ct_labels['add_new_item']               = pods_v( 'label_add_new_item', $taxonomy, '', true );
753
				$ct_labels['new_item_name']              = pods_v( 'label_new_item_name', $taxonomy, '', true );
754
				$ct_labels['separate_items_with_commas'] = pods_v( 'label_separate_items_with_commas', $taxonomy, '', true );
755
				$ct_labels['add_or_remove_items']        = pods_v( 'label_add_or_remove_items', $taxonomy, '', true );
756
				$ct_labels['choose_from_most_used']      = pods_v( 'label_choose_from_the_most_used', $taxonomy, '', true );
757
				$ct_labels['not_found']                  = pods_v( 'label_not_found', $taxonomy, '', true );
758
				$ct_labels['no_terms']                   = pods_v( 'label_no_terms', $taxonomy, '', true );
759
				$ct_labels['items_list']                 = pods_v( 'label_items_list', $taxonomy, '', true );
760
				$ct_labels['items_list_navigation']      = pods_v( 'label_items_list_navigation', $taxonomy, '', true );
761
762
				// Rewrite
763
				$ct_rewrite       = (boolean) pods_v( 'rewrite', $taxonomy, true );
764
				$ct_rewrite_array = array(
765
					'slug'         => pods_v( 'rewrite_custom_slug', $taxonomy, str_replace( '_', '-', $taxonomy_name ), true ),
766
					'with_front'   => (boolean) pods_v( 'rewrite_with_front', $taxonomy, true ),
767
					'hierarchical' => (boolean) pods_v( 'rewrite_hierarchical', $taxonomy, (boolean) pods_v( 'hierarchical', $taxonomy, false ) ),
768
				);
769
770
				if ( false !== $ct_rewrite ) {
771
					$ct_rewrite = $ct_rewrite_array;
772
				}
773
774
				/**
775
				 * Default tax capabilities
776
				 *
777
				 * @see https://codex.wordpress.org/Function_Reference/register_taxonomy
778
				 */
779
				$capability_type  = pods_v( 'capability_type', $taxonomy, 'default' );
780
				$tax_capabilities = array();
781
782
				if ( 'custom' === $capability_type ) {
783
					$capability_type = pods_v( 'capability_type_custom', $taxonomy, 'default' );
784
					if ( ! empty( $capability_type ) && 'default' !== $capability_type ) {
785
						$capability_type       .= '_term';
786
						$capability_type_plural = $capability_type . 's';
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $capability_type_plural exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
787
						$tax_capabilities       = array(
788
							// Singular
789
							'edit_term'    => 'edit_' . $capability_type,
790
							'delete_term'  => 'delete_' . $capability_type,
791
							'assign_term'  => 'assign_' . $capability_type,
792
							// Plural
793
							'manage_terms' => 'manage_' . $capability_type_plural,
794
							'edit_terms'   => 'edit_' . $capability_type_plural,
795
							'delete_terms' => 'delete_' . $capability_type_plural,
796
							'assign_terms' => 'assign_' . $capability_type_plural,
797
						);
798
					}
799
				}
800
801
				// Register Taxonomy
802
				$pods_taxonomies[ $taxonomy_name ] = array(
803
					'label'                 => $ct_label,
804
					'labels'                => $ct_labels,
805
					'public'                => (boolean) pods_v( 'public', $taxonomy, true ),
806
					'show_ui'               => (boolean) pods_v( 'show_ui', $taxonomy, (boolean) pods_v( 'public', $taxonomy, true ) ),
807
					'show_in_menu'          => (boolean) pods_v( 'show_in_menu', $taxonomy, (boolean) pods_v( 'public', $taxonomy, true ) ),
808
					'show_in_nav_menus'     => (boolean) pods_v( 'show_in_nav_menus', $taxonomy, (boolean) pods_v( 'public', $taxonomy, true ) ),
809
					'show_tagcloud'         => (boolean) pods_v( 'show_tagcloud', $taxonomy, (boolean) pods_v( 'show_ui', $taxonomy, (boolean) pods_v( 'public', $taxonomy, true ) ) ),
810
					'show_tagcloud_in_edit' => (boolean) pods_v( 'show_tagcloud_in_edit', $taxonomy, (boolean) pods_v( 'show_tagcloud', $taxonomy, (boolean) pods_v( 'show_ui', $taxonomy, (boolean) pods_v( 'public', $taxonomy, true ) ) ) ),
811
					'show_in_quick_edit'    => (boolean) pods_v( 'show_in_quick_edit', $taxonomy, (boolean) pods_v( 'show_ui', $taxonomy, (boolean) pods_v( 'public', $taxonomy, true ) ) ),
812
					'hierarchical'          => (boolean) pods_v( 'hierarchical', $taxonomy, false ),
813
					// 'capability_type'       => $capability_type,
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...
814
					'capabilities'          => $tax_capabilities,
815
					// 'map_meta_cap'          => (boolean) pods_v( 'capability_type_extra', $taxonomy, true ),
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
816
					'update_count_callback' => pods_v( 'update_count_callback', $taxonomy, null, true ),
817
					'query_var'             => ( false !== (boolean) pods_v( 'query_var', $taxonomy, true ) ? pods_v( 'query_var_string', $taxonomy, $taxonomy_name, true ) : false ),
818
					'rewrite'               => $ct_rewrite,
819
					'show_admin_column'     => (boolean) pods_v( 'show_admin_column', $taxonomy, false ),
820
					'sort'                  => (boolean) pods_v( 'sort', $taxonomy, false ),
821
				);
822
823
				if ( is_array( $ct_rewrite ) && ! $pods_taxonomies[ $taxonomy_name ]['query_var'] ) {
824
					$pods_taxonomies[ $taxonomy_name ]['query_var'] = pods_v( 'query_var_string', $taxonomy, $taxonomy_name, true );
825
				}
826
827
				// Prevent reserved query_var issues
828
				if ( in_array( $pods_taxonomies[ $taxonomy_name ]['query_var'], $reserved_query_vars, true ) ) {
829
					$pods_taxonomies[ $taxonomy_name ]['query_var'] = 'taxonomy_' . $pods_taxonomies[ $taxonomy_name ]['query_var'];
830
				}
831
832
				// REST API
833
				$rest_enabled = (boolean) pods_v( 'rest_enable', $taxonomy, false );
834
835
				if ( $rest_enabled ) {
836
					$rest_base = sanitize_title( pods_v( 'rest_base', $taxonomy, $taxonomy_name ) );
837
838
					$pods_taxonomies[ $taxonomy_name ]['show_in_rest']          = true;
839
					$pods_taxonomies[ $taxonomy_name ]['rest_base']             = $rest_base;
840
					$pods_taxonomies[ $taxonomy_name ]['rest_controller_class'] = 'WP_REST_Terms_Controller';
841
				}
842
843
				// Integration for Single Value Taxonomy UI
844
				if ( function_exists( 'tax_single_value_meta_box' ) ) {
845
					$pods_taxonomies[ $taxonomy_name ]['single_value'] = (boolean) pods_v( 'single_value', $taxonomy, false );
846
					$pods_taxonomies[ $taxonomy_name ]['required']     = (boolean) pods_v( 'single_value_required', $taxonomy, false );
847
				}
848
849
				// Post Types
850
				$ct_post_types = array();
851
				$_post_types   = get_post_types();
852
				$_post_types   = array_merge_recursive( $_post_types, $pods_post_types );
853
				$ignore        = array( 'revision' );
854
855
				foreach ( $_post_types as $post_type => $options ) {
856
					if ( in_array( $post_type, $ignore, true ) ) {
857
						continue;
858
					}
859
860
					if ( false !== (boolean) pods_v( 'built_in_post_types_' . $post_type, $taxonomy, false ) ) {
861
						$ct_post_types[] = $post_type;
862
863
						if ( isset( $supported_taxonomies[ $post_type ] ) && ! in_array( $taxonomy_name, $supported_taxonomies[ $post_type ], true ) ) {
864
							$supported_taxonomies[ $post_type ][] = $taxonomy_name;
865
						}
866
					}
867
				}
868
869
				if ( isset( $supported_post_types[ $taxonomy_name ] ) ) {
870
					$supported_post_types[ $taxonomy_name ] = array_merge( $supported_post_types[ $taxonomy_name ], $ct_post_types );
871
				} else {
872
					$supported_post_types[ $taxonomy_name ] = $ct_post_types;
873
				}
874
			}//end foreach
875
876
			$pods_post_types = apply_filters( 'pods_wp_post_types', $pods_post_types );
877
			$pods_taxonomies = apply_filters( 'pods_wp_taxonomies', $pods_taxonomies );
878
879
			$supported_post_types = apply_filters( 'pods_wp_supported_post_types', $supported_post_types );
880
			$supported_taxonomies = apply_filters( 'pods_wp_supported_taxonomies', $supported_taxonomies );
881
882
			foreach ( $pods_taxonomies as $taxonomy => $options ) {
883
				$ct_post_types = null;
884
885
				if ( isset( $supported_post_types[ $taxonomy ] ) && ! empty( $supported_post_types[ $taxonomy ] ) ) {
886
					$ct_post_types = $supported_post_types[ $taxonomy ];
887
				}
888
889
				$pods_cpt_ct['taxonomies'][ $taxonomy ] = array(
890
					'post_types' => $ct_post_types,
891
					'options'    => $options,
892
				);
893
			}
894
895
			foreach ( $pods_post_types as $post_type => $options ) {
896
				if ( isset( $supported_taxonomies[ $post_type ] ) && ! empty( $supported_taxonomies[ $post_type ] ) ) {
897
					$options['taxonomies'] = $supported_taxonomies[ $post_type ];
898
				}
899
900
				$pods_cpt_ct['post_types'][ $post_type ] = $options;
901
			}
902
903
			$pods_cpt_ct['post_format_post_types'] = $post_format_post_types;
904
905
			pods_transient_set( 'pods_wp_cpt_ct', $pods_cpt_ct );
906
		}//end if
907
908
		foreach ( $pods_cpt_ct['taxonomies'] as $taxonomy => $options ) {
909
			if ( isset( self::$content_types_registered['taxonomies'] ) && in_array( $taxonomy, self::$content_types_registered['taxonomies'], true ) ) {
910
				continue;
911
			}
912
913
			$ct_post_types = $options['post_types'];
914
			$options       = $options['options'];
915
916
			$options = self::object_label_fix( $options, 'taxonomy' );
917
918
			/**
919
			 * Hide tagcloud compatibility
920
			 *
921
			 * @todo check https://core.trac.wordpress.org/ticket/36964
922
			 * @see  wp-admin/edit-tags.php L389
923
			 */
924
			if ( true !== (boolean) pods_v( 'show_tagcloud_in_edit', $options, (boolean) pods_v( 'show_tagcloud', $options, true ) ) ) {
925
				$options['labels']['popular_items'] = null;
926
			}
927
928
			// Max length for taxonomies are 32 characters
929
			$taxonomy = substr( $taxonomy, 0, 32 );
930
931
			// i18n compatibility for plugins that override it
932
			if ( is_array( $options['rewrite'] ) && isset( $options['rewrite']['slug'] ) && ! empty( $options['rewrite']['slug'] ) ) {
933
				$options['rewrite']['slug'] = _x( $options['rewrite']['slug'], 'URL taxonomy slug', 'pods' );
934
			}
935
936
			/**
937
			 * Allow filtering of taxonomy options per taxonomy.
938
			 *
939
			 * @param array  $options       Taxonomy options
940
			 * @param string $taxonomy      Taxonomy name
941
			 * @param array  $ct_post_types Associated Post Types
942
			 */
943
			$options = apply_filters( 'pods_register_taxonomy_' . $taxonomy, $options, $taxonomy, $ct_post_types );
944
945
			/**
946
			 * Allow filtering of taxonomy options.
947
			 *
948
			 * @param array  $options       Taxonomy options
949
			 * @param string $taxonomy      Taxonomy name
950
			 * @param array  $ct_post_types Associated post types
951
			 */
952
			$options = apply_filters( 'pods_register_taxonomy', $options, $taxonomy, $ct_post_types );
953
954
			if ( 1 === (int) pods_v( 'pods_debug_register', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) {
955
				pods_debug( array( 'register_taxonomy', compact( 'taxonomy', 'ct_post_types', 'options' ) ) );
956
			}
957
958
			register_taxonomy( $taxonomy, $ct_post_types, $options );
959
960
			if ( ! empty( $options['show_in_rest'] ) ) {
961
				new PodsRESTFields( $taxonomy );
962
			}
963
964
			if ( ! isset( self::$content_types_registered['taxonomies'] ) ) {
965
				self::$content_types_registered['taxonomies'] = array();
966
			}
967
968
			self::$content_types_registered['taxonomies'][] = $taxonomy;
969
		}//end foreach
970
971
		foreach ( $pods_cpt_ct['post_types'] as $post_type => $options ) {
972
			if ( isset( self::$content_types_registered['post_types'] ) && in_array( $post_type, self::$content_types_registered['post_types'], true ) ) {
973
				continue;
974
			}
975
976
			$options = self::object_label_fix( $options, 'post_type' );
977
978
			// Max length for post types are 20 characters
979
			$post_type = substr( $post_type, 0, 20 );
980
981
			// i18n compatibility for plugins that override it
982
			if ( is_array( $options['rewrite'] ) && isset( $options['rewrite']['slug'] ) && ! empty( $options['rewrite']['slug'] ) ) {
983
				$options['rewrite']['slug'] = _x( $options['rewrite']['slug'], 'URL slug', 'pods' );
984
			}
985
986
			/**
987
			 * Allow filtering of post type options per post type.
988
			 *
989
			 * @param array  $options   Post type options
990
			 * @param string $post_type Post type name
991
			 */
992
			$options = apply_filters( 'pods_register_post_type_' . $post_type, $options, $post_type );
993
994
			/**
995
			 * Allow filtering of post type options.
996
			 *
997
			 * @param array  $options   Post type options
998
			 * @param string $post_type Post type name
999
			 */
1000
			$options = apply_filters( 'pods_register_post_type', $options, $post_type );
1001
1002
			if ( 1 === (int) pods_v( 'pods_debug_register', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) {
1003
				pods_debug( array( 'register_post_type', compact( 'post_type', 'options' ) ) );
1004
			}
1005
1006
			register_post_type( $post_type, $options );
1007
1008
			// Register post format taxonomy for this post type
1009
			if ( isset( $pods_cpt_ct['post_format_post_types'] ) && in_array( $post_type, $pods_cpt_ct['post_format_post_types'], true ) ) {
1010
				register_taxonomy_for_object_type( 'post_format', $post_type );
1011
			}
1012
1013
			if ( ! empty( $options['show_in_rest'] ) ) {
1014
				new PodsRESTFields( $post_type );
1015
			}
1016
1017
			if ( ! isset( self::$content_types_registered['post_types'] ) ) {
1018
				self::$content_types_registered['post_types'] = array();
1019
			}
1020
1021
			self::$content_types_registered['post_types'][] = $post_type;
1022
		}//end foreach
1023
1024
		// Handle existing post types / taxonomies settings (just REST for now)
1025
		global $wp_post_types, $wp_taxonomies;
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...
1026
1027
		$post_type_names = wp_list_pluck( $post_types, 'name', 'id' );
1028
		$taxonomy_names  = wp_list_pluck( $taxonomies, 'name', 'id' );
1029
1030
		foreach ( $existing_post_types as $post_type_name => $post_type_name_again ) {
1031
			if ( isset( self::$content_types_registered['post_types'] ) && in_array( $post_type_name, self::$content_types_registered['post_types'], true ) ) {
1032
				// Post type already registered / setup by Pods
1033
				continue;
1034
			}
1035
1036
			$pod_id = array_search( $post_type_name, $post_type_names, true );
1037
1038
			if ( ! $pod_id ) {
1039
				// Post type not a pod
1040
				continue;
1041
			}
1042
1043
			$pod = $post_types[ $pod_id ];
1044
1045
			// REST API
1046
			$rest_enabled = (boolean) pods_v( 'rest_enable', $pod['options'], false );
1047
1048
			if ( $rest_enabled ) {
1049
				if ( empty( $wp_post_types[ $post_type_name ]->show_in_rest ) ) {
1050
					$rest_base = sanitize_title( pods_v( 'rest_base', $pod['options'], pods_v( 'rest_base', $wp_post_types[ $post_type_name ] ), true ) );
1051
1052
					$wp_post_types[ $post_type_name ]->show_in_rest          = true;
1053
					$wp_post_types[ $post_type_name ]->rest_base             = $rest_base;
1054
					$wp_post_types[ $post_type_name ]->rest_controller_class = 'WP_REST_Posts_Controller';
1055
				}
1056
1057
				new PodsRESTFields( $post_type_name );
1058
			}
1059
		}//end foreach
1060
1061
		foreach ( $existing_taxonomies as $taxonomy_name => $taxonomy_name_again ) {
1062
			if ( isset( self::$content_types_registered['taxonomies'] ) && in_array( $taxonomy_name, self::$content_types_registered['taxonomies'], true ) ) {
1063
				// Taxonomy already registered / setup by Pods
1064
				continue;
1065
			}
1066
1067
			$pod_id = array_search( $taxonomy_name, $taxonomy_names, true );
1068
1069
			if ( ! $pod_id ) {
1070
				// Taxonomy not a pod
1071
				continue;
1072
			}
1073
1074
			$pod = $taxonomies[ $pod_id ];
1075
1076
			// REST API
1077
			$rest_enabled = (boolean) pods_v( 'rest_enable', $pod['options'], false );
1078
1079
			if ( $rest_enabled ) {
1080
				if ( empty( $wp_taxonomies[ $taxonomy_name ]->show_in_rest ) ) {
1081
					$rest_base = sanitize_title( pods_v( 'rest_base', $pod['options'], pods_v( 'rest_base', $wp_taxonomies[ $taxonomy_name ] ), true ) );
1082
1083
					$wp_taxonomies[ $taxonomy_name ]->show_in_rest          = true;
1084
					$wp_taxonomies[ $taxonomy_name ]->rest_base             = $rest_base;
1085
					$wp_taxonomies[ $taxonomy_name ]->rest_controller_class = 'WP_REST_Terms_Controller';
1086
				}
1087
1088
				new PodsRESTFields( $taxonomy_name );
1089
			}
1090
		}//end foreach
1091
1092
		if ( ! empty( PodsMeta::$user ) ) {
1093
			$pod = current( PodsMeta::$user );
1094
1095
			$rest_enabled = (boolean) pods_v( 'rest_enable', $pod['options'], false );
1096
1097
			if ( $rest_enabled ) {
1098
				new PodsRESTFields( $pod['name'] );
1099
			}
1100
		}
1101
1102
		if ( ! empty( PodsMeta::$media ) ) {
1103
			$pod = current( PodsMeta::$media );
1104
1105
			$rest_enabled = (boolean) pods_v( 'rest_enable', $pod['options'], false );
1106
1107
			if ( $rest_enabled ) {
1108
				new PodsRESTFields( $pod['name'] );
1109
			}
1110
		}
1111
1112
	}
1113
1114
	/**
1115
	 * Check if we need to flush WordPress rewrite rules
1116
	 * This gets run during 'init' action late in the game to give other plugins time to register their rewrite rules
1117
	 */
1118
	public function flush_rewrite_rules() {
1119
1120
		$flush = (int) pods_transient_get( 'pods_flush_rewrites' );
1121
1122
		if ( 1 === $flush ) {
1123
			/**
1124
			 * @var $wp_rewrite WP_Rewrite
1125
			 */
1126
			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...
1127
			$wp_rewrite->flush_rules();
1128
			$wp_rewrite->init();
1129
1130
			pods_transient_set( 'pods_flush_rewrites', 0 );
1131
		}
1132
	}
1133
1134
	/**
1135
	 * Update Post Type messages
1136
	 *
1137
	 * @param array $messages
1138
	 *
1139
	 * @return array
1140
	 * @since 2.0.2
1141
	 */
1142
	public function setup_updated_messages( $messages ) {
1143
1144
		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...
1145
1146
		$post_types          = PodsMeta::$post_types;
1147
		$existing_post_types = get_post_types();
1148
1149
		$pods_cpt_ct = pods_transient_get( 'pods_wp_cpt_ct' );
1150
1151
		if ( empty( $pods_cpt_ct ) || empty( $post_types ) ) {
1152
			return $messages;
1153
		}
1154
1155
		/**
1156
		 * Use get_preview_post_link function added in 4.4, which eventually applies preview_post_link filter
1157
		 * Before 4.4, this filter is defined in wp-admin/includes/meta-boxes.php, $post parameter added in 4.0
1158
		 * there wasn't post parameter back in 3.8
1159
		 * Let's add $post in the filter as it won't hurt anyway.
1160
		 *
1161
		 * @since 2.6.8.1
1162
		 */
1163
		$preview_post_link = function_exists( 'get_preview_post_link' ) ? get_preview_post_link( $post ) : apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ), $post );
1164
1165
		foreach ( $post_types as $post_type ) {
1166
			if ( ! isset( $pods_cpt_ct['post_types'][ $post_type['name'] ] ) ) {
1167
				continue;
1168
			}
1169
1170
			$labels = self::object_label_fix( $pods_cpt_ct['post_types'][ $post_type['name'] ], 'post_type' );
1171
			$labels = $labels['labels'];
1172
1173
			$messages[ $post_type['name'] ] = array(
1174
				1  => sprintf( __( '%1$s updated. <a href="%2$s">%3$s</a>', 'pods' ), $labels['singular_name'], esc_url( get_permalink( $post_ID ) ), $labels['view_item'] ),
1175
				2  => __( 'Custom field updated.', 'pods' ),
1176
				3  => __( 'Custom field deleted.', 'pods' ),
1177
				4  => sprintf( __( '%s updated.', 'pods' ), $labels['singular_name'] ),
1178
				/* translators: %s: date and time of the revision */
1179
				5  => isset( $_GET['revision'] ) ? sprintf( __( '%1$s restored to revision from %2$s', 'pods' ), $labels['singular_name'], wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
1180
				6  => sprintf( __( '%1$s published. <a href="%2$s">%3$s</a>', 'pods' ), $labels['singular_name'], esc_url( get_permalink( $post_ID ) ), $labels['view_item'] ),
1181
				7  => sprintf( __( '%s saved.', 'pods' ), $labels['singular_name'] ),
1182
				8  => sprintf( __( '%1$s submitted. <a target="_blank" href="%2$s">Preview %3$s</a>', 'pods' ), $labels['singular_name'], esc_url( $preview_post_link ), $labels['singular_name'] ),
1183
				9  => sprintf(
1184
					__( '%1$s scheduled for: <strong>%2$s</strong>. <a target="_blank" href="%3$s">Preview %4$s</a>', 'pods' ), $labels['singular_name'],
1185
					// translators: Publish box date format, see http://php.net/date
1186
					date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink( $post_ID ) ), $labels['singular_name']
1187
				),
1188
				10 => sprintf( __( '%1$s draft updated. <a target="_blank" href="%2$s">Preview %3$s</a>', 'pods' ), $labels['singular_name'], esc_url( $preview_post_link ), $labels['singular_name'] ),
1189
			);
1190
1191
			if ( false === (boolean) $pods_cpt_ct['post_types'][ $post_type['name'] ]['public'] ) {
1192
				$messages[ $post_type['name'] ][1] = sprintf( __( '%s updated.', 'pods' ), $labels['singular_name'] );
1193
				$messages[ $post_type['name'] ][6] = sprintf( __( '%s published.', 'pods' ), $labels['singular_name'] );
1194
				$messages[ $post_type['name'] ][8] = sprintf( __( '%s submitted.', 'pods' ), $labels['singular_name'] );
1195
				$messages[ $post_type['name'] ][9] = sprintf(
1196
					__( '%s scheduled for: <strong>%1$s</strong>.', 'pods' ), $labels['singular_name'],
1197
					// translators: Publish box date format, see http://php.net/date
1198
					date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) )
1199
				);
1200
				$messages[ $post_type['name'] ][10] = sprintf( __( '%s draft updated.', 'pods' ), $labels['singular_name'] );
1201
			}
1202
		}//end foreach
1203
1204
		return $messages;
1205
	}
1206
1207
	/**
1208
	 * @param        $args
1209
	 * @param string $type
1210
	 *
1211
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
1212
	 */
1213
	public static function object_label_fix( $args, $type = 'post_type' ) {
1214
1215
		if ( empty( $args ) || ! is_array( $args ) ) {
1216
			$args = array();
1217
		}
1218
1219
		if ( ! isset( $args['labels'] ) || ! is_array( $args['labels'] ) ) {
1220
			$args['labels'] = array();
1221
		}
1222
1223
		$label          = pods_v( 'name', $args['labels'], pods_v( 'label', $args, __( 'Items', 'pods' ), true ), true );
1224
		$singular_label = pods_v( 'singular_name', $args['labels'], pods_v( 'label_singular', $args, __( 'Item', 'pods' ), true ), true );
1225
1226
		$labels = $args['labels'];
1227
1228
		$labels['name']          = $label;
1229
		$labels['singular_name'] = $singular_label;
1230
1231
		if ( 'post_type' === $type ) {
1232
			$labels['menu_name']             = pods_v( 'menu_name', $labels, $label, true );
1233
			$labels['name_admin_bar']        = pods_v( 'name_admin_bar', $labels, $singular_label, true );
1234
			$labels['add_new']               = pods_v( 'add_new', $labels, __( 'Add New', 'pods' ), true );
1235
			$labels['add_new_item']          = pods_v( 'add_new_item', $labels, sprintf( __( 'Add New %s', 'pods' ), $singular_label ), true );
1236
			$labels['new_item']              = pods_v( 'new_item', $labels, sprintf( __( 'New %s', 'pods' ), $singular_label ), true );
1237
			$labels['edit']                  = pods_v( 'edit', $labels, __( 'Edit', 'pods' ), true );
1238
			$labels['edit_item']             = pods_v( 'edit_item', $labels, sprintf( __( 'Edit %s', 'pods' ), $singular_label ), true );
1239
			$labels['view']                  = pods_v( 'view', $labels, sprintf( __( 'View %s', 'pods' ), $singular_label ), true );
1240
			$labels['view_item']             = pods_v( 'view_item', $labels, sprintf( __( 'View %s', 'pods' ), $singular_label ), true );
1241
			$labels['view_items']            = pods_v( 'view_items', $labels, sprintf( __( 'View %s', 'pods' ), $label ), true );
1242
			$labels['all_items']             = pods_v( 'all_items', $labels, sprintf( __( 'All %s', 'pods' ), $label ), true );
1243
			$labels['search_items']          = pods_v( 'search_items', $labels, sprintf( __( 'Search %s', 'pods' ), $label ), true );
1244
			$labels['not_found']             = pods_v( 'not_found', $labels, sprintf( __( 'No %s Found', 'pods' ), $label ), true );
1245
			$labels['not_found_in_trash']    = pods_v( 'not_found_in_trash', $labels, sprintf( __( 'No %s Found in Trash', 'pods' ), $label ), true );
1246
			$labels['parent']                = pods_v( 'parent', $labels, sprintf( __( 'Parent %s', 'pods' ), $singular_label ), true );
1247
			$labels['parent_item_colon']     = pods_v( 'parent_item_colon', $labels, sprintf( __( 'Parent %s:', 'pods' ), $singular_label ), true );
1248
			$labels['featured_image']        = pods_v( 'featured_image', $labels, __( 'Featured Image', 'pods' ), true );
1249
			$labels['set_featured_image']    = pods_v( 'set_featured_image', $labels, __( 'Set featured image', 'pods' ), true );
1250
			$labels['remove_featured_image'] = pods_v( 'remove_featured_image', $labels, __( 'Remove featured image', 'pods' ), true );
1251
			$labels['use_featured_image']    = pods_v( 'use_featured_image', $labels, __( 'Use as featured image', 'pods' ), true );
1252
			$labels['archives']              = pods_v( 'archives', $labels, sprintf( __( '%s Archives', 'pods' ), $singular_label ), true );
1253
			$labels['attributes']            = pods_v( 'attributes', $labels, sprintf( __( '%s Attributes', 'pods' ), $singular_label ), true );
1254
			$labels['insert_into_item']      = pods_v( 'insert_into_item', $labels, sprintf( __( 'Insert into %s', 'pods' ), $singular_label ), true );
1255
			$labels['uploaded_to_this_item'] = pods_v( 'uploaded_to_this_item', $labels, sprintf( __( 'Uploaded to this %s', 'pods' ), $singular_label ), true );
1256
			$labels['filter_items_list']     = pods_v( 'filter_items_list', $labels, sprintf( __( 'Filter %s lists', 'pods' ), $label ), true );
1257
			$labels['items_list_navigation'] = pods_v( 'items_list_navigation', $labels, sprintf( __( '%s navigation', 'pods' ), $label ), true );
1258
			$labels['items_list']            = pods_v( 'items_list', $labels, sprintf( __( '%s list', 'pods' ), $label ), true );
1259
		} elseif ( 'taxonomy' === $type ) {
1260
			$labels['menu_name']                  = pods_v( 'menu_name', $labels, $label, true );
1261
			$labels['search_items']               = pods_v( 'search_items', $labels, sprintf( __( 'Search %s', 'pods' ), $label ), true );
1262
			$labels['popular_items']              = pods_v( 'popular_items', $labels, sprintf( __( 'Popular %s', 'pods' ), $label ), true );
1263
			$labels['all_items']                  = pods_v( 'all_items', $labels, sprintf( __( 'All %s', 'pods' ), $label ), true );
1264
			$labels['parent_item']                = pods_v( 'parent_item', $labels, sprintf( __( 'Parent %s', 'pods' ), $singular_label ), true );
1265
			$labels['parent_item_colon']          = pods_v( 'parent_item_colon', $labels, sprintf( __( 'Parent %s :', 'pods' ), $singular_label ), true );
1266
			$labels['edit_item']                  = pods_v( 'edit_item', $labels, sprintf( __( 'Edit %s', 'pods' ), $singular_label ), true );
1267
			$labels['view_item']                  = pods_v( 'view_item', $labels, sprintf( __( 'View %s', 'pods' ), $singular_label ), true );
1268
			$labels['update_item']                = pods_v( 'update_item', $labels, sprintf( __( 'Update %s', 'pods' ), $singular_label ), true );
1269
			$labels['add_new_item']               = pods_v( 'add_new_item', $labels, sprintf( __( 'Add New %s', 'pods' ), $singular_label ), true );
1270
			$labels['new_item_name']              = pods_v( 'new_item_name', $labels, sprintf( __( 'New %s Name', 'pods' ), $singular_label ), true );
1271
			$labels['separate_items_with_commas'] = pods_v( 'separate_items_with_commas', $labels, sprintf( __( 'Separate %s with commas', 'pods' ), $label ), true );
1272
			$labels['add_or_remove_items']        = pods_v( 'add_or_remove_items', $labels, sprintf( __( 'Add or remove %s', 'pods' ), $label ), true );
1273
			$labels['choose_from_most_used']      = pods_v( 'choose_from_most_used', $labels, sprintf( __( 'Choose from the most used %s', 'pods' ), $label ), true );
1274
			$labels['not_found']                  = pods_v( 'not_found', $labels, sprintf( __( 'No %s found.', 'pods' ), $label ), true );
1275
			$labels['no_terms']                   = pods_v( 'no_terms', $labels, sprintf( __( 'No %s', 'pods' ), $label ), true );
1276
			$labels['items_list_navigation']      = pods_v( 'items_list_navigation', $labels, sprintf( __( '%s navigation', 'pods' ), $label ), true );
1277
			$labels['items_list']                 = pods_v( 'items_list', $labels, sprintf( __( '%s list', 'pods' ), $label ), true );
1278
		}//end if
1279
1280
		$args['labels'] = $labels;
1281
1282
		return $args;
1283
	}
1284
1285
	/**
1286
	 * Activate and Install
1287
	 */
1288
	public function activate_install() {
1289
1290
		register_activation_hook( PODS_DIR . 'init.php', array( $this, 'activate' ) );
1291
		register_deactivation_hook( PODS_DIR . 'init.php', array( $this, 'deactivate' ) );
1292
1293
		add_action( 'wpmu_new_blog', array( $this, 'new_blog' ), 10, 6 );
1294
1295
		if ( empty( self::$version ) || version_compare( self::$version, PODS_VERSION, '<' ) || version_compare( self::$version, PODS_DB_VERSION, '<=' ) || self::$upgrade_needed ) {
1296
			$this->setup();
1297
		} elseif ( self::$version !== PODS_VERSION ) {
1298
			delete_option( 'pods_framework_version' );
1299
			add_option( 'pods_framework_version', PODS_VERSION, '', 'yes' );
1300
1301
			self::$version = PODS_VERSION;
1302
1303
			pods_api()->cache_flush_pods();
1304
		}
1305
1306
	}
1307
1308
	/**
1309
	 *
1310
	 */
1311
	public function activate() {
1312
1313
		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...
1314
1315
		if ( is_multisite() && 1 === (int) pods_v( 'networkwide' ) ) {
1316
			$_blog_ids = $wpdb->get_col( "SELECT `blog_id` FROM `{$wpdb->blogs}`" );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
1317
1318
			foreach ( $_blog_ids as $_blog_id ) {
1319
				$this->setup( $_blog_id );
1320
			}
1321
		} else {
1322
			$this->setup();
1323
		}
1324
	}
1325
1326
	/**
1327
	 *
1328
	 */
1329
	public function deactivate() {
1330
1331
		pods_api()->cache_flush_pods();
1332
1333
	}
1334
1335
	/**
1336
	 * @param null $current
1337
	 * @param null $last
1338
	 *
1339
	 * @return bool
1340
	 */
1341
	public function needs_upgrade( $current = null, $last = null ) {
1342
1343
		if ( null === $current ) {
1344
			$current = self::$version;
1345
		}
1346
1347
		if ( null === $last ) {
1348
			$last = self::$version_last;
1349
		}
1350
1351
		$upgrade_needed = false;
1352
1353
		if ( ! empty( $current ) ) {
1354
			foreach ( self::$upgrades as $old_version => $new_version ) {
1355
				/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
1356
				if ( '2.1.0' === $new_version && ( is_developer() ) )
1357
					continue;*/
1358
1359
				if ( version_compare( $last, $old_version, '>=' ) && version_compare( $last, $new_version, '<' ) && version_compare( $current, $new_version, '>=' ) && 1 !== self::$upgraded ) {
1360
					$upgrade_needed = true;
1361
1362
					break;
1363
				}
1364
			}
1365
		}
1366
1367
		return $upgrade_needed;
1368
	}
1369
1370
	/**
1371
	 * @param $_blog_id
1372
	 * @param $user_id
1373
	 * @param $domain
1374
	 * @param $path
1375
	 * @param $site_id
1376
	 * @param $meta
1377
	 */
1378
	public function new_blog( $_blog_id, $user_id, $domain, $path, $site_id, $meta ) {
1379
1380
		if ( is_multisite() && is_plugin_active_for_network( basename( PODS_DIR ) . '/init.php' ) ) {
1381
			$this->setup( $_blog_id );
1382
		}
1383
	}
1384
1385
	/**
1386
	 * @param null $_blog_id
1387
	 */
1388
	public function setup( $_blog_id = null ) {
1389
1390
		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...
1391
1392
		// Switch DB table prefixes
1393
		if ( null !== $_blog_id && $_blog_id !== $wpdb->blogid ) {
1394
			switch_to_blog( pods_absint( $_blog_id ) );
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
1395
		} else {
1396
			$_blog_id = null;
1397
		}
1398
1399
		// Setup DB tables
1400
		$pods_version      = get_option( 'pods_framework_version' );
1401
		$pods_version_last = get_option( 'pods_framework_version_last' );
1402
1403
		if ( empty( $pods_version ) ) {
1404
			// Install Pods
1405
			pods_upgrade()->install( $_blog_id );
1406
1407
			$old_version = get_option( 'pods_version' );
1408
1409
			if ( ! empty( $old_version ) ) {
1410
				if ( false === strpos( $old_version, '.' ) ) {
1411
					$old_version = pods_version_to_point( $old_version );
1412
				}
1413
1414
				delete_option( 'pods_framework_version_last' );
1415
				add_option( 'pods_framework_version_last', $pods_version, '', 'yes' );
1416
1417
				self::$version_last = $old_version;
1418
			}
1419
		} elseif ( $this->needs_upgrade( $pods_version, $pods_version_last ) ) {
1420
			// Upgrade Wizard needed
1421
			// Do not do anything
1422
			return;
1423
		} elseif ( version_compare( $pods_version, PODS_VERSION, '<=' ) ) {
1424
			// Update Pods and run any required DB updates
1425
			if ( false !== apply_filters( 'pods_update_run', null, PODS_VERSION, $pods_version, $_blog_id ) && ! isset( $_GET['pods_bypass_update'] ) ) {
1426
				do_action( 'pods_update', PODS_VERSION, $pods_version, $_blog_id );
1427
1428
				// Update 2.0 alpha / beta sites
1429
				if ( version_compare( '2.0.0-a-1', $pods_version, '<=' ) && version_compare( $pods_version, '2.0.0-b-15', '<=' ) ) {
1430
					include PODS_DIR . 'sql/update-2.0-beta.php';
1431
				}
1432
1433
				if ( version_compare( $pods_version, PODS_DB_VERSION, '<=' ) ) {
1434
					include PODS_DIR . 'sql/update.php';
1435
				}
1436
1437
				do_action( 'pods_update_post', PODS_VERSION, $pods_version, $_blog_id );
1438
			}
1439
1440
			delete_option( 'pods_framework_version_last' );
1441
			add_option( 'pods_framework_version_last', $pods_version, '', 'yes' );
1442
1443
			self::$version_last = $pods_version;
1444
		}//end if
1445
1446
		delete_option( 'pods_framework_version' );
1447
		add_option( 'pods_framework_version', PODS_VERSION, '', 'yes' );
1448
1449
		delete_option( 'pods_framework_db_version' );
1450
		add_option( 'pods_framework_db_version', PODS_DB_VERSION, '', 'yes' );
1451
1452
		self::$version    = PODS_VERSION;
1453
		self::$db_version = PODS_DB_VERSION;
1454
1455
		pods_api()->cache_flush_pods();
1456
1457
		// Restore DB table prefix (if switched)
1458
		if ( null !== $_blog_id ) {
1459
			restore_current_blog();
1460
		}
1461
1462
	}
1463
1464
	/**
1465
	 * @param null $_blog_id
1466
	 */
1467
	public function reset( $_blog_id = null ) {
1468
1469
		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...
1470
1471
		// Switch DB table prefixes
1472
		if ( null !== $_blog_id && $_blog_id !== $wpdb->blogid ) {
1473
			switch_to_blog( pods_absint( $_blog_id ) );
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
1474
		} else {
1475
			$_blog_id = null;
1476
		}
1477
1478
		$api = pods_api();
1479
1480
		$pods = $api->load_pods(
1481
			array(
1482
				'names_ids'  => true,
1483
				'table_info' => false,
1484
			)
1485
		);
1486
1487
		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...
1488
			$api->delete_pod( array( 'id' => $pod_id ) );
1489
		}
1490
1491
		$templates = $api->load_templates();
1492
1493
		foreach ( $templates as $template ) {
1494
			$api->delete_template( array( 'id' => $template['id'] ) );
1495
		}
1496
1497
		$pages = $api->load_pages();
1498
1499
		foreach ( $pages as $page ) {
1500
			$api->delete_page( array( 'id' => $page['id'] ) );
1501
		}
1502
1503
		$helpers = $api->load_helpers();
1504
1505
		foreach ( $helpers as $helper ) {
1506
			$api->delete_helper( array( 'id' => $helper['id'] ) );
1507
		}
1508
1509
		$tables = $wpdb->get_results( "SHOW TABLES LIKE '{$wpdb->prefix}pods%'", ARRAY_N );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
1510
1511
		if ( ! empty( $tables ) ) {
1512
			foreach ( $tables as $table ) {
1513
				$table = $table[0];
1514
1515
				pods_query( "DROP TABLE `{$table}`", false );
1516
			}
1517
		}
1518
1519
		// Remove any orphans
1520
		$wpdb->query(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
1521
			"
1522
                DELETE `p`, `pm`
1523
                FROM `{$wpdb->posts}` AS `p`
1524
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1525
                    ON `pm`.`post_id` = `p`.`ID`
1526
                WHERE
1527
                    `p`.`post_type` LIKE '_pods_%'
1528
            "
1529
		);
1530
1531
		delete_option( 'pods_framework_version' );
1532
		delete_option( 'pods_framework_db_version' );
1533
		delete_option( 'pods_framework_upgrade_2_0' );
1534
		delete_option( 'pods_framework_upgraded_1_x' );
1535
1536
		// @todo Make sure all entries are being cleaned and do something about the pods_framework_upgrade_{version} dynamic entries created by PodsUpgrade
1537
		delete_option( 'pods_framework_upgrade_2_0_0' );
1538
		delete_option( 'pods_framework_upgrade_2_0_sister_ids' );
1539
		delete_option( 'pods_framework_version_last' );
1540
1541
		delete_option( 'pods_component_settings' );
1542
1543
		$api->cache_flush_pods();
1544
1545
		pods_transient_clear( 'pods_flush_rewrites' );
1546
1547
		self::$version = '';
1548
1549
		// Restore DB table prefix (if switched)
1550
		if ( null !== $_blog_id ) {
1551
			restore_current_blog();
1552
		}
1553
	}
1554
1555
	public function run() {
1556
1557
		static $ran;
1558
1559
		if ( ! empty( $ran ) ) {
1560
			return;
1561
		}
1562
1563
		$ran = true;
1564
1565
		$this->load_i18n();
1566
1567
		if ( ! did_action( 'plugins_loaded' ) ) {
1568
			add_action( 'plugins_loaded', array( $this, 'load_components' ), 11 );
1569
		} else {
1570
			$this->load_components();
1571
		}
1572
1573
		if ( ! did_action( 'setup_theme' ) ) {
1574
			add_action( 'setup_theme', array( $this, 'load_meta' ), 14 );
1575
		} else {
1576
			$this->load_meta();
1577
		}
1578
1579
		if ( ! did_action( 'init' ) ) {
1580
			add_action( 'init', array( $this, 'core' ), 11 );
1581
			add_action( 'init', array( $this, 'setup_content_types' ), 11 );
1582
1583
			if ( is_admin() ) {
1584
				add_action( 'init', array( $this, 'admin_init' ), 12 );
1585
			}
1586
		} else {
1587
			$this->core();
1588
			$this->setup_content_types();
1589
1590
			if ( is_admin() ) {
1591
				$this->admin_init();
1592
			}
1593
		}
1594
1595
		add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1596
		add_action( 'admin_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1597
		add_action( 'login_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1598
1599
		add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 );
1600
		add_action( 'delete_attachment', array( $this, 'delete_attachment' ) );
1601
1602
		// Register widgets
1603
		add_action( 'widgets_init', array( $this, 'register_widgets' ) );
1604
1605
		// Show admin bar links
1606
		add_action( 'admin_bar_menu', array( $this, 'admin_bar_links' ), 81 );
1607
1608
		// Add WP-CLI commands
1609
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
1610
			require_once PODS_DIR . 'classes/cli/Pods_CLI_Command.php';
1611
			require_once PODS_DIR . 'classes/cli/PodsAPI_CLI_Command.php';
1612
		}
1613
1614
	}
1615
1616
	/**
1617
	 * Delete Attachments from relationships
1618
	 *
1619
	 * @param int $_ID
1620
	 */
1621
	public function delete_attachment( $_ID ) {
1622
1623
		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...
1624
1625
		$_ID = (int) $_ID;
1626
1627
		do_action( 'pods_delete_attachment', $_ID );
1628
1629
		$file_types = "'" . implode( "', '", PodsForm::file_field_types() ) . "'";
1630
1631
		if ( ! pods_tableless() ) {
1632
			$sql = "
1633
                DELETE `rel`
1634
                FROM `@wp_podsrel` AS `rel`
1635
                LEFT JOIN `{$wpdb->posts}` AS `p`
1636
                    ON
1637
                        `p`.`post_type` = '_pods_field'
1638
                        AND ( `p`.`ID` = `rel`.`field_id` OR `p`.`ID` = `rel`.`related_field_id` )
1639
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1640
                    ON
1641
                        `pm`.`post_id` = `p`.`ID`
1642
                        AND `pm`.`meta_key` = 'type'
1643
                        AND `pm`.`meta_value` IN ( {$file_types} )
1644
                WHERE
1645
                    `p`.`ID` IS NOT NULL
1646
                    AND `pm`.`meta_id` IS NOT NULL
1647
                    AND `rel`.`item_id` = {$_ID}";
1648
1649
			pods_query( $sql, false );
1650
		}
1651
1652
		// Post Meta
1653
		if ( ! empty( PodsMeta::$post_types ) ) {
1654
			$sql = "
1655
                DELETE `rel`
1656
                FROM `@wp_postmeta` AS `rel`
1657
                LEFT JOIN `{$wpdb->posts}` AS `p`
1658
                    ON
1659
                        `p`.`post_type` = '_pods_field'
1660
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1661
                    ON
1662
                        `pm`.`post_id` = `p`.`ID`
1663
                        AND `pm`.`meta_key` = 'type'
1664
                        AND `pm`.`meta_value` IN ( {$file_types} )
1665
                WHERE
1666
                    `p`.`ID` IS NOT NULL
1667
                    AND `pm`.`meta_id` IS NOT NULL
1668
                    AND `rel`.`meta_key` = `p`.`post_name`
1669
                    AND `rel`.`meta_value` = '{$_ID}'";
1670
1671
			pods_query( $sql, false );
1672
		}
1673
1674
		// User Meta
1675
		if ( ! empty( PodsMeta::$user ) ) {
1676
			$sql = "
1677
                DELETE `rel`
1678
                FROM `@wp_usermeta` AS `rel`
1679
                LEFT JOIN `{$wpdb->posts}` AS `p`
1680
                    ON
1681
                        `p`.`post_type` = '_pods_field'
1682
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1683
                    ON
1684
                        `pm`.`post_id` = `p`.`ID`
1685
                        AND `pm`.`meta_key` = 'type'
1686
                        AND `pm`.`meta_value` IN ( {$file_types} )
1687
                WHERE
1688
                    `p`.`ID` IS NOT NULL
1689
                    AND `pm`.`meta_id` IS NOT NULL
1690
                    AND `rel`.`meta_key` = `p`.`post_name`
1691
                    AND `rel`.`meta_value` = '{$_ID}'";
1692
1693
			pods_query( $sql, false );
1694
		}
1695
1696
		// Comment Meta
1697
		if ( ! empty( PodsMeta::$comment ) ) {
1698
			$sql = "
1699
                DELETE `rel`
1700
                FROM `@wp_commentmeta` AS `rel`
1701
                LEFT JOIN `{$wpdb->posts}` AS `p`
1702
                    ON
1703
                        `p`.`post_type` = '_pods_field'
1704
                LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1705
                    ON
1706
                        `pm`.`post_id` = `p`.`ID`
1707
                        AND `pm`.`meta_key` = 'type'
1708
                        AND `pm`.`meta_value` IN ( {$file_types} )
1709
                WHERE
1710
                    `p`.`ID` IS NOT NULL
1711
                    AND `pm`.`meta_id` IS NOT NULL
1712
                    AND `rel`.`meta_key` = `p`.`post_name`
1713
                    AND `rel`.`meta_value` = '{$_ID}'";
1714
1715
			pods_query( $sql, false );
1716
		}
1717
	}
1718
1719
	/**
1720
	 * Register widgets for Pods
1721
	 */
1722
	public function register_widgets() {
1723
1724
		$widgets = array(
1725
			'PodsWidgetSingle',
1726
			'PodsWidgetList',
1727
			'PodsWidgetField',
1728
			'PodsWidgetForm',
1729
			'PodsWidgetView',
1730
		);
1731
1732
		foreach ( $widgets as $widget ) {
1733
			if ( ! file_exists( PODS_DIR . 'classes/widgets/' . $widget . '.php' ) ) {
1734
				continue;
1735
			}
1736
1737
			require_once PODS_DIR . 'classes/widgets/' . $widget . '.php';
1738
1739
			register_widget( $widget );
1740
		}
1741
	}
1742
1743
	/**
1744
	 * Add Admin Bar links
1745
	 */
1746
	public function admin_bar_links() {
1747
1748
		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...
1749
1750
		if ( ! is_user_logged_in() || ! is_admin_bar_showing() ) {
1751
			return;
1752
		}
1753
1754
		$all_pods = pods_api()->load_pods(
1755
			array(
1756
				'type'       => 'pod',
1757
				'fields'     => false,
1758
				'table_info' => false,
1759
			)
1760
		);
1761
1762
		// Add New item links for all pods
1763
		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...
1764
			if ( 0 === (int) $pod['options']['show_in_menu'] ) {
1765
				continue;
1766
			}
1767
1768
			if ( ! pods_is_admin( array( 'pods', 'pods_content', 'pods_add_' . $pod['name'] ) ) ) {
1769
				continue;
1770
			}
1771
1772
			$singular_label = pods_v( 'label_singular', $pod['options'], pods_v( 'label', $pod, ucwords( str_replace( '_', ' ', $pod['name'] ) ), true ), true );
1773
1774
			$wp_admin_bar->add_node(
1775
				array(
1776
					'id'     => 'new-pod-' . $pod['name'],
1777
					'title'  => $singular_label,
1778
					'parent' => 'new-content',
1779
					'href'   => admin_url( 'admin.php?page=pods-manage-' . $pod['name'] . '&action=add' ),
1780
				)
1781
			);
1782
		}
1783
1784
		// Add edit link if we're on a pods page
1785
		if ( is_object( $pods ) && ! is_wp_error( $pods ) && ! empty( $pods->id ) && isset( $pods->pod_data ) && ! empty( $pods->pod_data ) && 'pod' === $pods->pod_data['type'] ) {
1786
			$pod = $pods->pod_data;
1787
1788
			if ( pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $pod['name'] ) ) ) {
1789
				$singular_label = pods_v( 'label_singular', $pod['options'], pods_v( 'label', $pod, ucwords( str_replace( '_', ' ', $pod['name'] ) ), true ), true );
1790
1791
				$wp_admin_bar->add_node(
1792
					array(
1793
						'title' => sprintf( __( 'Edit %s', 'pods' ), $singular_label ),
1794
						'id'    => 'edit-pod',
1795
						'href'  => admin_url( 'admin.php?page=pods-manage-' . $pod['name'] . '&action=edit&id=' . $pods->id() ),
1796
					)
1797
				);
1798
			}
1799
		}
1800
1801
	}
1802
}
1803