Completed
Pull Request — 2.x (#3586)
by
unknown
05:43
created

PodsInit::admin_bar_links()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 46
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 24
c 1
b 0
f 0
nc 13
nop 0
dl 0
loc 46
rs 5.1118

How to fix   Complexity   

Long Method

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

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

Commonly applied refactorings include:

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