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

Pods_Migrate_CPTUI::clean()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 21.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Name: Migrate: Import from the Custom Post Type UI plugin
4
 *
5
 * Menu Name: Migrate CPT UI
6
 *
7
 * Description: Import Custom Post Types and Taxonomies from Custom Post Type UI (<a
8
 * href="http://webdevstudios.com/plugin/custom-post-type-ui/">http://webdevstudios.com/plugin/custom-post-type-ui/</a>)
9
 *
10
 * Category: Migration
11
 *
12
 * Version: 1.0
13
 *
14
 * Plugin: pods-migrate-custom-post-type-ui/pods-migrate-custom-post-type-ui.php
15
 *
16
 * @package    Pods\Components
17
 * @subpackage Migrate-Cptui
18
 */
19
20
if ( class_exists( 'Pods_Migrate_CPTUI' ) ) {
21
	return;
22
}
23
24
/**
25
 * Class Pods_Migrate_CPTUI
26
 */
27
class Pods_Migrate_CPTUI extends PodsComponent {
28
29
	/** @var array
30
	 *
31
	 *  Support option names for multiple versions, list from newest to oldest
32
	 */
33
	private $post_option_name_list = array(
34
		'cptui_post_types',
35
		'cpt_custom_post_types',
36
	);
37
38
	/** @var array
39
	 *
40
	 *  Support option names for multiple versions, list from newest to oldest
41
	 */
42
	private $taxonomy_option_name_list = array(
43
		'cptui_taxonomies',
44
		'cpt_custom_tax_types',
45
	);
46
47
	private $api = null;
48
49
	private $post_option_name = null;
50
51
	private $taxonomy_option_name = null;
52
53
	private $post_types = array();
54
55
	private $taxonomies = array();
56
57
	/**
58
	 * {@inheritdoc}
59
	 */
60
	public function init() {
61
62
		$this->post_option_name = $this->get_option_name( $this->post_option_name_list );
63
		if ( ! is_null( $this->post_option_name ) ) {
64
			$this->post_types = (array) get_option( $this->post_option_name, array() );
65
		}
66
		$this->taxonomy_option_name = $this->get_option_name( $this->taxonomy_option_name_list );
67
		if ( ! is_null( $this->taxonomy_option_name ) ) {
68
			$this->taxonomies = (array) get_option( $this->taxonomy_option_name, array() );
69
		}
70
	}
71
72
	/**
73
	 * Enqueue styles
74
	 *
75
	 * @since 2.0
76
	 */
77
	public function admin_assets() {
78
79
		wp_enqueue_style( 'pods-wizard' );
80
	}
81
82
	/**
83
	 * Show the Admin
84
	 *
85
	 * @param $options
86
	 * @param $component
87
	 */
88
	public function admin( $options, $component ) {
89
90
		$post_types = (array) $this->post_types;
91
		$taxonomies = (array) $this->taxonomies;
92
93
		$method = 'migrate';
94
		// ajax_migrate
95
		pods_view( PODS_DIR . 'components/Migrate-CPTUI/ui/wizard.php', compact( array_keys( get_defined_vars() ) ) );
96
	}
97
98
	/**
99
	 * Handle the Migration AJAX
100
	 *
101
	 * @param $params
102
	 */
103
	public function ajax_migrate( $params ) {
104
105
		$post_types = (array) $this->post_types;
106
		$taxonomies = (array) $this->taxonomies;
107
108
		$migrate_post_types = array();
109
110
		if ( isset( $params->post_type ) && ! empty( $params->post_type ) ) {
111
			foreach ( $params->post_type as $post_type => $checked ) {
112
				if ( true === (boolean) $checked ) {
113
					$migrate_post_types[] = $post_type;
114
				}
115
			}
116
		}
117
118
		$migrate_taxonomies = array();
119
120
		if ( isset( $params->taxonomy ) && ! empty( $params->taxonomy ) ) {
121
			foreach ( $params->taxonomy as $taxonomy => $checked ) {
122
				if ( true === (boolean) $checked ) {
123
					$migrate_taxonomies[] = $taxonomy;
124
				}
125
			}
126
		}
127
128
		foreach ( $post_types as $k => $post_type ) {
129
			if ( ! in_array( pods_var( 'name', $post_type ), $migrate_post_types, true ) ) {
130
				continue;
131
			}
132
133
			$id = $this->migrate_post_type( $post_type );
134
135
			if ( 0 < $id ) {
136
				unset( $post_types[ $k ] );
137
			}
138
		}
139
140
		foreach ( $taxonomies as $k => $taxonomy ) {
141
			if ( ! in_array( pods_var( 'name', $taxonomy ), $migrate_taxonomies, true ) ) {
142
				continue;
143
			}
144
145
			$id = $this->migrate_taxonomy( $taxonomy );
146
147
			if ( 0 < $id ) {
148
				unset( $taxonomies[ $k ] );
149
			}
150
		}
151
152
		if ( 1 == pods_var( 'cleanup', $params, 0 ) ) {
153
			if ( ! empty( $post_types ) ) {
154
				if ( ! is_null( $this->post_option_name ) ) {
155
					update_option( $this->post_option_name, $post_types );
156
				}
157
			} else {
158
				if ( ! is_null( $this->post_option_name ) ) {
159
					delete_option( $this->post_option_name );
160
				}
161
			}
162
163
			if ( ! empty( $taxonomies ) ) {
164
				if ( ! is_null( $this->taxonomy_option_name ) ) {
165
					update_option( $this->taxonomy_option_name, $taxonomies );
166
				}
167
			} else {
168
				if ( ! is_null( $this->taxonomy_option_name ) ) {
169
					delete_option( $this->taxonomy_option_name );
170
				}
171
			}
172
		}//end if
173
	}
174
175
	/**
176
	 *
177
	 *
178
	 * @since 2.0
179
	 *
180
	 * @param $post_type
181
	 *
182
	 * @return bool|int|mixed
183
	 */
184
	private function migrate_post_type( $post_type ) {
185
186
		$params = array(
187
			'type'                     => 'post_type',
188
			'storage'                  => 'meta',
189
			'object'                   => '',
190
			'name'                     => pods_var_raw( 'name', $post_type ),
191
			'label'                    => pods_var_raw( 'label', $post_type ),
192
			'label_singular'           => pods_var_raw( 'singular_label', $post_type ),
193
			'description'              => pods_var_raw( 'description', $post_type ),
194
			'public'                   => pods_var_raw( 'public', $post_type ),
195
			'show_ui'                  => (int) pods_var_raw( 'show_ui', $post_type ),
196
			'has_archive'              => (int) pods_var_raw( 'has_archive', $post_type ),
197
			'exclude_from_search'      => (int) pods_var_raw( 'exclude_from_search', $post_type ),
198
			'capability_type'          => pods_var_raw( 'capability_type', $post_type ),
199
			// --!! Needs sanity checking?
200
			'hierarchical'             => (int) pods_var_raw( 'hierarchical', $post_type ),
201
			'rewrite'                  => (int) pods_var_raw( 'rewrite', $post_type ),
202
			'rewrite_custom_slug'      => pods_var_raw( 'rewrite_slug', $post_type ),
203
			'query_var'                => (int) pods_var_raw( 'query_var', $post_type ),
204
			'menu_position'            => (int) pods_var_raw( 'menu_position', $post_type ),
205
			'show_in_menu'             => (int) pods_var_raw( 'show_in_menu', $post_type ),
206
			'menu_string'              => pods_var_raw( 'show_in_menu_string', $post_type ),
207
208
			// 'supports' argument to register_post_type()
209
			'supports_title'           => ( is_array( $post_type[0] ) && in_array( 'title', $post_type[0], true ) ),
210
			'supports_editor'          => ( is_array( $post_type[0] ) && in_array( 'editor', $post_type[0], true ) ),
211
			'supports_excerpt'         => ( is_array( $post_type[0] ) && in_array( 'excerpt', $post_type[0], true ) ),
212
			'supports_trackbacks'      => ( is_array( $post_type[0] ) && in_array( 'trackbacks', $post_type[0], true ) ),
213
			'supports_custom_fields'   => ( is_array( $post_type[0] ) && in_array( 'custom-fields', $post_type[0], true ) ),
214
			'supports_comments'        => ( is_array( $post_type[0] ) && in_array( 'comments', $post_type[0], true ) ),
215
			'supports_revisions'       => ( is_array( $post_type[0] ) && in_array( 'revisions', $post_type[0], true ) ),
216
			'supports_thumbnail'       => ( is_array( $post_type[0] ) && in_array( 'thumbnail', $post_type[0], true ) ),
217
			'supports_author'          => ( is_array( $post_type[0] ) && in_array( 'author', $post_type[0], true ) ),
218
			'supports_page_attributes' => ( is_array( $post_type[0] ) && in_array( 'page-attributes', $post_type[0], true ) ),
219
220
			// 'labels' argument to register_post_type()
221
			'menu_name'                => pods_var_raw( 'menu_name', $post_type[2] ),
222
			'label_add_new'            => pods_var_raw( 'add_new', $post_type[2] ),
223
			'label_add_new_item'       => pods_var_raw( 'add_new_item', $post_type[2] ),
224
			'label_edit'               => pods_var_raw( 'edit', $post_type[2] ),
225
			'label_edit_item'          => pods_var_raw( 'edit_item', $post_type[2] ),
226
			'label_new_item'           => pods_var_raw( 'new_item', $post_type[2] ),
227
			'label_view'               => pods_var_raw( 'view', $post_type[2] ),
228
			'label_view_item'          => pods_var_raw( 'view_item', $post_type[2] ),
229
			'label_search_items'       => pods_var_raw( 'search_items', $post_type[2] ),
230
			'label_not_found'          => pods_var_raw( 'not_found', $post_type[2] ),
231
			'label_not_found_in_trash' => pods_var_raw( 'not_found_in_trash', $post_type[2] ),
232
			'label_parent'             => pods_var_raw( 'parent', $post_type[2] ),
233
		);
234
235
		// Migrate built-in taxonomies
236
		$builtin = $post_type[1];
237
		if ( is_array( $builtin ) ) {
238
			foreach ( $builtin as $taxonomy_name ) {
239
				$params[ 'built_in_taxonomies_' . $taxonomy_name ] = 1;
240
			}
241
		}
242
243
		if ( ! is_object( $this->api ) ) {
244
			$this->api = pods_api();
245
		}
246
247
		$pod = $this->api->load_pod( array( 'name' => pods_clean_name( $params['name'] ) ), false );
248
249
		if ( ! empty( $pod ) ) {
250
			return pods_error( sprintf( __( 'Pod with the name %s already exists', 'pods' ), pods_clean_name( $params['name'] ) ) );
251
		}
252
253
		$id = (int) $this->api->save_pod( $params );
254
255
		if ( empty( $id ) ) {
256
			return false;
257
		}
258
259
		$pod = $this->api->load_pod( array( 'id' => $id ), false );
260
261
		if ( empty( $pod ) ) {
262
			return false;
263
		}
264
265
		if ( $pod['name'] != $params['name'] ) {
266
			$this->api->rename_wp_object_type( $params['type '], $params['name'], $pod['name'] );
267
		}
268
269
		return $id;
270
	}
271
272
	/**
273
	 *
274
	 *
275
	 * @since 2.0
276
	 *
277
	 * @param $taxonomy
278
	 *
279
	 * @return bool|int|mixed
280
	 */
281
	private function migrate_taxonomy( $taxonomy ) {
282
283
		$params = array(
284
			'type'                             => 'taxonomy',
285
			'storage'                          => 'table',
286
			'object'                           => '',
287
			'name'                             => pods_var_raw( 'name', $taxonomy ),
288
			'label'                            => pods_var_raw( 'label', $taxonomy ),
289
			'label_singular'                   => pods_var_raw( 'singular_label', $taxonomy ),
290
			'public'                           => 1,
291
			'show_ui'                          => (int) pods_var_raw( 'show_ui', $taxonomy ),
292
			'hierarchical'                     => (int) pods_var_raw( 'hierarchical', $taxonomy ),
293
			'query_var'                        => (int) pods_var_raw( 'query_var', $taxonomy ),
294
			'rewrite'                          => (int) pods_var_raw( 'rewrite', $taxonomy ),
295
			'rewrite_custom_slug'              => pods_var_raw( 'rewrite_slug', $taxonomy ),
296
			'label_search_items'               => pods_var_raw( 'search_items', $taxonomy[0] ),
297
			'label_popular_items'              => pods_var_raw( 'popular_items', $taxonomy[0] ),
298
			'label_all_items'                  => pods_var_raw( 'all_items', $taxonomy[0] ),
299
			'label_parent'                     => pods_var_raw( 'parent_item', $taxonomy[0] ),
300
			'label_parent_item_colon'          => pods_var_raw( 'parent_item_colon', $taxonomy[0] ),
301
			'label_edit'                       => pods_var_raw( 'edit_item', $taxonomy[0] ),
302
			'label_update_item'                => pods_var_raw( 'update_item', $taxonomy[0] ),
303
			'label_add_new'                    => pods_var_raw( 'add_new_item', $taxonomy[0] ),
304
			'label_new_item'                   => pods_var_raw( 'new_item_name', $taxonomy[0] ),
305
			'label_separate_items_with_commas' => pods_var_raw( 'separate_items_with_commas', $taxonomy[0] ),
306
			'label_add_or_remove_items'        => pods_var_raw( 'add_or_remove_items', $taxonomy[0] ),
307
			'label_choose_from_the_most_used'  => pods_var_raw( 'choose_from_most_used', $taxonomy[0] ),
308
		);
309
310
		// Migrate attach-to
311
		$attach = $taxonomy[1];
312
		if ( is_array( $attach ) ) {
313
			foreach ( $attach as $type_name ) {
314
				$params[ 'built_in_post_types_' . $type_name ] = 1;
315
			}
316
		}
317
318
		if ( ! is_object( $this->api ) ) {
319
			$this->api = pods_api();
320
		}
321
322
		$pod = $this->api->load_pod( array( 'name' => pods_clean_name( $params['name'] ) ), false );
323
324
		if ( ! empty( $pod ) ) {
325
			return pods_error( sprintf( __( 'Pod with the name %s already exists', 'pods' ), pods_clean_name( $params['name'] ) ) );
326
		}
327
328
		$id = (int) $this->api->save_pod( $params );
329
330
		if ( empty( $id ) ) {
331
			return false;
332
		}
333
334
		$pod = $this->api->load_pod( array( 'id' => $id ), false );
335
336
		if ( empty( $pod ) ) {
337
			return false;
338
		}
339
340
		if ( $pod['name'] != $params['name'] ) {
341
			$this->api->rename_wp_object_type( $params['type '], $params['name'], $pod['name'] );
342
		}
343
344
		return $id;
345
	}
346
347
	/**
348
	 *
349
	 * @since 2.0
350
	 */
351
	public function clean() {
352
353
		if ( ! is_null( $this->post_option_name ) ) {
354
			delete_option( $this->post_option_name );
355
		}
356
357
		if ( ! is_null( $this->taxonomy_option_name ) ) {
358
			delete_option( $this->taxonomy_option_name );
359
		}
360
361
	}
362
363
	/**
364
	 * @param array $option_name_list List of possible option names.
365
	 *
366
	 * @return null|string The first found option name, or NULL if none were found
367
	 */
368
	private function get_option_name( $option_name_list ) {
369
370
		$option_name_list = (array) $option_name_list;
371
372
		foreach ( $option_name_list as $this_option_name ) {
373
			if ( null !== get_option( $this_option_name, null ) ) {
374
				return $this_option_name;
375
			}
376
		}
377
378
		return null;
379
380
	}
381
382
}
383