| 1 |
<?php |
| 2 |
/* |
| 3 |
Name: 文章类型转换器 |
| 4 |
URI: https://mp.weixin.qq.com/s/UPelWNVrolGCzi5POKJUaw |
| 5 |
Description: 可以将文章在多种文章类型中进行转换。 |
| 6 |
Version: 1.0 |
| 7 |
Admin: true |
| 8 |
*/ |
| 9 |
class WPJAM_Post_Type_Switcher{ |
| 10 |
public static function get_options(){ |
| 11 |
foreach(get_post_types(['show_ui'=>true], 'objects') as $ptype => $pt_object){ |
| 12 |
if($ptype != 'attachment' && !str_starts_with($ptype, 'wp_') && current_user_can($pt_object->cap->publish_posts)){ |
| 13 |
$options[$ptype] = wpjam_get_post_type_setting($ptype, 'title'); |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
return $options ?? []; |
| 18 |
} |
| 19 |
|
| 20 |
public static function set($post_id, $ptype){ |
| 21 |
if($ptype && get_post_type($post_id) != $ptype){ |
| 22 |
if(!post_type_exists($ptype) || !current_user_can(get_post_type_object($ptype)->cap->publish_posts)){ |
| 23 |
return new WP_Error('invalid_post_type'); |
| 24 |
} |
| 25 |
|
| 26 |
return wpjam_then( |
| 27 |
set_post_type($post_id, $ptype), |
| 28 |
['type'=>'redirect', 'url'=>admin_url('edit.php?post_type='.$ptype.'&id='.$post_id)] |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
return ['notice'=>'未修改文章类型']; |
| 33 |
} |
| 34 |
|
| 35 |
public static function on_after_insert_post($post_id, $post){ |
| 36 |
if((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || !current_user_can('edit_post', $post_id)){ |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
isset($_REQUEST['ptype']) && self::set($post_id, $_REQUEST['ptype']); |
| 41 |
} |
| 42 |
|
| 43 |
public static function on_post_submitbox_misc_actions(){ |
| 44 |
$current = get_post_type(); |
| 45 |
$pt_object = get_post_type_object($current); |
| 46 |
|
| 47 |
?> |
| 48 |
|
| 49 |
<div class="misc-pub-section post-type-switcher"> |
| 50 |
<label for="ptype">文章类型:</label> |
| 51 |
<strong id="post_type_display"><?php echo esc_html(wpjam_get_post_type_setting($current, 'title')); ?></strong> |
| 52 |
|
| 53 |
<?php if(current_user_can($pt_object->cap->publish_posts)){ ?> |
| 54 |
|
| 55 |
<a href="javascript:;" id="edit_post_type_switcher" class="hide-if-no-js"><?php _e( 'Edit' ); ?></a> |
| 56 |
|
| 57 |
<div id="post_type_select"> |
| 58 |
<?php echo wpjam_field(['key'=>'ptype', 'value'=>$current, 'options'=>self::get_options()]); ?> |
| 59 |
|
| 60 |
<a href="javascript:;" id="save_post_type_switcher" class="hide-if-no-js button"><?php _e( 'OK' ); ?></a> |
| 61 |
<a href="javascript:;" id="cancel_post_type_switcher" class="hide-if-no-js button-cancel"><?php _e( 'Cancel' ); ?></a> |
| 62 |
</div> |
| 63 |
|
| 64 |
<?php } ?> |
| 65 |
|
| 66 |
</div> |
| 67 |
|
| 68 |
<?php |
| 69 |
} |
| 70 |
|
| 71 |
public static function on_admin_head(){ |
| 72 |
?> |
| 73 |
<script type="text/javascript"> |
| 74 |
jQuery(function($){ |
| 75 |
$('#edit_post_type_switcher').on('click', function(e) { |
| 76 |
$(this).hide(); |
| 77 |
$('#post_type_select').slideDown(); |
| 78 |
}); |
| 79 |
|
| 80 |
$('#save_post_type_switcher').on('click', function(e) { |
| 81 |
$('#post_type_select').slideUp(); |
| 82 |
$('#edit_post_type_switcher').show(); |
| 83 |
$('#post_type_display').text($('#ptype :selected').text()); |
| 84 |
}); |
| 85 |
|
| 86 |
$('#cancel_post_type_switcher').on('click', function(e) { |
| 87 |
$('#post_type_select').slideUp(); |
| 88 |
$('#edit_post_type_switcher').show(); |
| 89 |
}); |
| 90 |
}); |
| 91 |
</script> |
| 92 |
<style type="text/css"> |
| 93 |
#post_type_select{ margin-top: 3px; display: none; } |
| 94 |
#post-body .post-type-switcher::before{ content: '\f109'; font: 400 20px/1 dashicons; speak: none; display: inline-block; padding: 0 2px 0 0; top: 0; left: -1px; position: relative; vertical-align: top; text-decoration: none !important; color: #888; } |
| 95 |
</style> |
| 96 |
<?php |
| 97 |
} |
| 98 |
|
| 99 |
public static function builtin_page_load($screen){ |
| 100 |
if($screen->base == 'edit'){ |
| 101 |
wpjam_register_list_table_action('set_post_type', [ |
| 102 |
'title' => '修改类型', |
| 103 |
'page_title' => '修改类型', |
| 104 |
'submit_text' => '修改', |
| 105 |
'callback' => fn($post_id, $data)=> self::set($post_id, $data['post_type']), |
| 106 |
'fields' => ['post_type'=>['title'=>'文章类型', 'options'=>self::get_options()]] |
| 107 |
]); |
| 108 |
}elseif($screen->base == 'post' && $screen->post_type != 'attachment' && !$screen->is_block_editor){ |
| 109 |
add_action('wp_after_insert_post', [self::class, 'on_after_insert_post'], 999, 2); |
| 110 |
add_action('post_submitbox_misc_actions', [self::class, 'on_post_submitbox_misc_actions']); |
| 111 |
add_action('admin_head', [self::class, 'on_admin_head']); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
wpjam_add_admin_load([ |
| 117 |
'base' => ['post','edit'], |
| 118 |
'model' => 'WPJAM_Post_Type_Switcher' |
| 119 |
]); |