Completed
Pull Request — 2.x (#3487)
by Scott Kingsley
11:35 queued 06:19
created

PodsInit::run()   C

Complexity

Conditions 9
Paths 33

Size

Total Lines 60
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 60
rs 6.8358
cc 9
eloc 34
nc 33
nop 1

How to fix   Long Method   

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