-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert-post-types.php
More file actions
400 lines (305 loc) · 15.1 KB
/
convert-post-types.php
File metadata and controls
400 lines (305 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/*
Plugin Name: Bulk Convert Post Types
Version: 1.5
Author: KCPT
Author URI: http://KCPT.org
Description: A bulk conversion utility for post types.
License: GPL2
*/
if ( ! defined ( 'ABSPATH' ) ) {
die ();
}
class KCPT_Bulk_Convert_Post_Type
{
public function __construct ()
{
add_action (
'admin_menu',
array ( $this, 'adminPage' )
);
// i18n
load_plugin_textdomain ( 'bulk-convert-post-types', '', plugin_dir_path ( __FILE__ ) . '/languages' );
}
function adminPage ()
{
$css = add_management_page (
__ ( 'Bulk Convert Post Types', 'bulk-convert-post-types' ),
__ ( 'Bulk Convert Post Types', 'bulk-convert-post-types' ),
'manage_options',
'bulk-convert-post-types',
array ( $this, 'convertOptions' )
);
add_action (
'admin_head-' . $css,
array ( $this, 'css' )
);
}
function css ()
{
?>
<style type="text/css">
div.categorychecklistbox {
float: left;
margin: 1em 1em 1em 0;
}
ul.categorychecklist {
height: 15em;
width: 20em;
overflow-y: scroll;
border: 1px solid #dfdfdf;
padding: 0 1em;
background: #fff;
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
}
ul.categorychecklist ul.children {
margin-left: 1em;
}
p.taginput {
float: left;
margin: 1em 1em 1em 0;
width: 22em;
}
p.taginput input {
width: 100%;
}
p.filters select {
width: 24em;
margin: 1em 1em 1em 0;
}
p.submit {
clear: both;
}
p.msg {
margin-left: 1em;
}
</style>
<?php
}
function convertOptions ()
{
if ( current_user_can ( 'edit_posts' ) && current_user_can ( 'edit_pages' ) ) {
$hidden_field_name = 'bulk_convert_post_submit_hidden';
if ( isset( $_POST[ $hidden_field_name ] ) && $_POST[ $hidden_field_name ] == 'Y' ) {
$this->convert ();
}
// $hidden_field_name
?>
<div class="wrap">
<?php if ( ! isset( $_POST[ $hidden_field_name ] ) || $_POST[ $hidden_field_name ] != 'Y' ) { ?>
<form method="post">
<h2><?php _e ( 'Convert Post Types', 'bulk-convert-post-types' ); ?></h2>
<p><?php _e ( 'With great power comes great responsibility. This process could <strong>really</strong> screw up your database. Please <a href="http://www.ilfilosofo.com/blog/wp-db-backup">make a backup</a> before proceeding.',
'bulk-convert-post-types' ); ?></p>
<input type="hidden" name="<?php echo esc_attr ( $hidden_field_name ); ?>" value="Y">
<p class="filters">
<?php
$typeselect = '';
if ( isset( $_POST[ 'convert_cat' ] ) ) {
$convert_cat = $_POST[ 'convert_cat' ];
} else {
$convert_cat = '';
}
$post_types = get_post_types ( array ( 'public' => true ) );
foreach ( $post_types as $type ) {
$typeselect .= "<option value=\"" . esc_attr ( $type ) . "\">";
$typeselect .= esc_html ( $type );
$typeselect .= "</option>";
}
?>
<select name="old_post_type">
<option value="-1"><?php _e ( "Convert from...",
'bulk-convert-post-types' ); ?></option>
<?php echo $typeselect; ?>
</select>
<select name="new_post_type">
<option value="-1"><?php _e ( "Convert to...", 'bulk-convert-post-types' ); ?></option>
<?php echo $typeselect; ?>
</select>
<?php wp_dropdown_categories ( 'name=convert_cat&show_option_none=Limit posts to category...&hide_empty=0&hierarchical=1&selected=' . $convert_cat ); ?>
<?php wp_dropdown_pages ( 'name=page_parent&show_option_none=Limit pages to children of...' ); ?>
</p>
<?php global $wp_taxonomies;
$nonhierarchical = ''; ?>
<?php if ( is_array ( $wp_taxonomies ) ) : ?>
<h4><?php _e ( 'Assign custom taxonomy terms', 'bulk-convert-post-types' ); ?></h4>
<?php foreach ( $wp_taxonomies as $tax ) :
if ( ! in_array ( $tax->name,
array ( 'nav_menu', 'link_category', 'podcast_format', 'format' ) )
) : ?>
<?php
if ( ! is_taxonomy_hierarchical ( $tax->name ) ) :
// non-hierarchical
$nonhierarchical .= '<p class="taginput"><label>' . esc_html ( $tax->label ) . '<br />';
$nonhierarchical .= '<input type="text" name="' . esc_attr ( $tax->name ) . '" class="widefloat" /></label></p>';
else:
// hierarchical
?>
<div class="categorychecklistbox">
<label>
<?php echo esc_html ( $tax->label ); ?><br/>
<?php if ( $tax->name === "category" ): ?>
<strong>This will remove all other categories and only set what is
selected below.</strong>
<?php endif; ?>
</label>
<ul class="categorychecklist">
<?php
wp_terms_checklist ( 0,
array (
'descendants_and_self' => 0,
'selected_cats' => false,
'popular_cats' => false,
'walker' => null,
'taxonomy' => $tax->name,
'checked_ontop' => true,
)
);
?>
</ul>
</div>
<?php
endif;
?>
<?php
endif;
endforeach;
echo '<br class="clear" />' . $nonhierarchical;
?>
<?php endif; ?>
<p class="submit">
<input type="submit" name="submit" class="primary button"
value="<?php _e ( 'Convert »', 'bulk-convert-post-types' ); ?>"/>
</p>
</form>
<?php } // if $hidden_field_name ?>
</div>
<?php } // if user can
}
function convert ()
{
global $wp_taxonomies;
$postCount = 0;
$newPostType = $_POST[ 'new_post_type' ];
$oldPostType = $_POST[ 'old_post_type' ];
$convertCat = ( ( isset( $_POST[ 'convert_cat' ] ) and ! empty( $_POST[ 'convert_cat' ] ) ) ? $_POST[ 'convert_cat' ] : false );
$postParent = ( ( isset( $_POST[ 'page_parent' ] ) and ! empty ( $_POST[ 'page_parent' ] ) ) ? $_POST[ 'page_parent' ] : false );
$postCategory = ( ( isset( $_POST[ 'post_category' ] ) and ! empty( $_POST[ 'post_category' ] ) ) ? $_POST[ 'post_category' ] : false );
$taxonomy = ( ( isset( $_POST[ 'tax_input' ] ) and ! empty( $_POST[ 'tax_input' ] ) ) ? $_POST[ 'tax_input' ] : false );
// check for invalid post type choices
if ( $newPostType == -1 || $oldPostType == -1 ) {
echo '<p class="error">' . __ ( 'Could not convert posts. One of the post types was not set.',
'bulk-convert-post-types' ) . '</p>';
return;
}
if ( ! post_type_exists ( $newPostType ) || ! post_type_exists ( $oldPostType ) ) {
echo '<p class="error">' . __ ( 'Could not convert posts. One of the selected post types does not exist.',
'bulk-convert-post-types' ) . '</p>';
return;
}
$query = array (
'posts_per_page' => -1,
'post_status' => 'any',
'post_type' => $oldPostType
);
if ( $convertCat && $convertCat > 1 ) {
$query[ 'cat' ] = $convertCat;
}
if ( $postParent && $postParent > 0 ) {
$query[ 'post_parent' ] = $postParent;
}
$items = get_posts ( $query );
if ( ! is_array ( $items ) ) {
echo '<p class="error">' .
__ ( 'Could not find any posts matching your criteria.', 'bulk-convert-post-types' ) .
'</p>';
return;
}
$postCount = count ( $items );
foreach ( $items as $post ) {
// Update the post into the database
$update[ 'ID' ] = $post->ID;
if ( $newPostType and ! $new_post_type_object = get_post_type_object ( $newPostType ) ) {
echo '<p class="error">' .
sprintf (
__ ( 'Could not convert post #%d. %s', 'bulk-convert-post-types' ),
$post->ID,
_ ( 'The new post type was not valid.' )
) .
'</p>';
} else {
set_post_type ( $post->ID, $new_post_type_object->name );
// handle post categories now; otherwise all posts will receive the default
if ( 'post' == $new_post_type_object->name && $postCategory ) {
wp_set_post_terms ( $post->ID, $postCategory, 'category', false );
} elseif ( $postCategory and is_array ( $postCategory ) ) {
$taxonomiesPossible = get_object_taxonomies ( $new_post_type_object->name );
if ( in_array ( 'category', $taxonomiesPossible ) ) {
wp_set_object_terms ( $post->ID, $postCategory, 'category', false );
}
}
// WPML support. Thanks to Jenny Beaumont! http://www.jennybeaumont.com/post-type-switcher-wpml-fix/
if ( function_exists ( 'icl_object_id' ) ) {
// adjust field 'element_type' in table 'wp_icl_translations'
// from 'post_OLDNAME' to 'post_NEWNAME'
// the post_id you look for is in column: 'element_id'
if ( $post->post_type == 'revision' ) {
if ( is_array ( $post->ancestors ) ) {
$ID = $post->ancestors[ 0 ];
}
} else {
$ID = $post->ID;
}
global $wpdb;
$wpdb->update (
$wpdb->prefix . 'icl_translations',
array ( 'element_type' => 'post_' . $new_post_type_object->name ),
array ( 'element_id' => $ID, 'element_type' => 'post_' . $post->post_type )
);
$wpdb->print_error ();
}
}
// set new taxonomy terms
foreach ( $wp_taxonomies as $tax ) {
// hierarchical custom taxonomies
if ( $taxonomy and isset( $taxonomy[ $tax->name ] ) and ! empty( $taxonomy[ $tax->name ] ) and is_array ( $taxonomy[ $tax->name ] ) ) {
wp_set_post_terms ( $post->ID, $taxonomy[ $tax->name ], $tax->name, false );
echo '<p class="msg">' .
sprintf (
__ ( 'Set %s to %s', 'bulk-convert-post-types' ),
$tax->label, $term->$name
) .
'</p>';
}
// all flat taxonomies
if ( isset( $_POST[ $tax->name ] ) && ! empty( $_POST[ $tax->name ] ) && 'post_category' != $tax->name ) {
wp_set_post_terms ( $post->ID, $_POST[ $tax->name ], $tax->name, false );
if ( 'post_category' == $tax->name ) {
echo '<p class="msg">' .
sprintf (
__ ( 'Set %s to %s', 'bulk-convert-post-types' ),
$tax->label,
join ( ', ', $_POST[ $tax->name ] )
) .
'</p>';
} else {
echo '<p class="msg">' .
sprintf (
__ ( 'Set %s to %s', 'bulk-convert-post-types' ),
$tax->label,
$_POST[ $tax->name ]
) .
'</p>';
}
}
}
}
echo '<div class="updated"><p><strong>' .
$postCount .
__ ( ' posts converted.', 'bulk-convert-post-types' ) .
'</strong></p></div>';
}
}
$kcptBulkConvertPostTypes = new KCPT_Bulk_Convert_Post_Type();