| 1 |
<?php |
| 2 |
namespace Mtphr\PostDuplicator; |
| 3 |
|
| 4 |
add_action( 'rest_api_init', __NAMESPACE__ . '\register_routes' ); |
| 5 |
|
| 6 |
/** |
| 7 |
* Register rest routes |
| 8 |
*/ |
| 9 |
function register_routes() { |
| 10 |
register_rest_route( 'post-duplicator/v1', 'duplicate-post', array( |
| 11 |
'methods' => 'POST', |
| 12 |
'permission_callback' => __NAMESPACE__ . '\duplicate_post_permissions', |
| 13 |
'callback' => __NAMESPACE__ . '\duplicate_post', |
| 14 |
) ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Duplicate a post |
| 19 |
*/ |
| 20 |
function duplicate_post_permissions( $request ) { |
| 21 |
$args = $request->get_params(); |
| 22 |
$data = $request->get_json_params(); |
| 23 |
$original_id = isset( $data['original_id'] ) ? $data['original_id'] : false; |
| 24 |
|
| 25 |
if ( ! $original_id ) { |
| 26 |
return new \WP_Error( 'no_original_id', esc_html__( 'No original id passed.', 'post-duplicator' ), array( 'status' => 403 ) ); |
| 27 |
} |
| 28 |
|
| 29 |
if ( ! current_user_can( 'edit_post', $original_id ) ) { |
| 30 |
return new \WP_Error( 'no_permission', esc_html__( 'User does not have permission to edit original post.', 'post-duplicator' ), array( 'status' => 403 ) ); |
| 31 |
} |
| 32 |
|
| 33 |
$post = get_post( $original_id ); |
| 34 |
if ( ! user_can_duplicate( $post ) ) { |
| 35 |
return new \WP_Error( 'no_permission', esc_html__( 'User does not have permission to duplicate post.', 'post-duplicator' ), array( 'status' => 403 ) ); |
| 36 |
} |
| 37 |
|
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Duplicate a post |
| 43 |
*/ |
| 44 |
function duplicate_post( $request ) { |
| 45 |
$args = $request->get_params(); |
| 46 |
$data = $request->get_json_params(); |
| 47 |
|
| 48 |
// Get access to the database |
| 49 |
global $wpdb; |
| 50 |
|
| 51 |
// Get the original id |
| 52 |
$original_id = $data['original_id']; |
| 53 |
|
| 54 |
// Get the post as an array |
| 55 |
$duplicate = $orig = get_post( $original_id, 'ARRAY_A' ); |
| 56 |
|
| 57 |
$settings = get_option_value(); |
| 58 |
|
| 59 |
// Modify some of the elements |
| 60 |
$appended = isset( $settings['title'] ) ? sanitize_text_field( $settings['title'] ) : esc_html__( 'Copy', 'post-duplicator' ); |
| 61 |
$duplicate['post_title'] = $duplicate['post_title'] . ' ' . $appended; |
| 62 |
$duplicate['post_name'] = sanitize_title( $duplicate['post_name'] . '-' . $settings['slug'] ); |
| 63 |
|
| 64 |
// Set the status |
| 65 |
if( $settings['status'] != 'same' ) { |
| 66 |
$duplicate['post_status'] = sanitize_text_field( $settings['status'] ); |
| 67 |
} |
| 68 |
|
| 69 |
// Check if a user has publish get_post_type_capabilities. If not, make sure they can't _publish |
| 70 |
if ( ! current_user_can( 'publish_posts' ) ) { |
| 71 |
// Force the post status to pending |
| 72 |
if ( 'publish' == $duplicate['post_status'] ) { |
| 73 |
$duplicate['post_status'] = 'pending'; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Set the type |
| 78 |
if( $settings['type'] != 'same' ) { |
| 79 |
$duplicate['post_type'] = sanitize_text_field( $settings['type'] ); |
| 80 |
} |
| 81 |
|
| 82 |
// Set the post date |
| 83 |
$timestamp = ( $settings['timestamp'] == 'duplicate' ) ? strtotime($duplicate['post_date']) : current_time('timestamp',0); |
| 84 |
$timestamp_gmt = ( $settings['timestamp'] == 'duplicate' ) ? strtotime($duplicate['post_date_gmt']) : current_time('timestamp',1); |
| 85 |
|
| 86 |
if( $settings['time_offset'] ) { |
| 87 |
$offset = intval($settings['time_offset_seconds']+$settings['time_offset_minutes']*60+$settings['time_offset_hours']*3600+$settings['time_offset_days']*86400); |
| 88 |
if( $settings['time_offset_direction'] == 'newer' ) { |
| 89 |
$timestamp = intval($timestamp+$offset); |
| 90 |
$timestamp_gmt = intval($timestamp_gmt+$offset); |
| 91 |
} else { |
| 92 |
$timestamp = intval($timestamp-$offset); |
| 93 |
$timestamp_gmt = intval($timestamp_gmt-$offset); |
| 94 |
} |
| 95 |
} |
| 96 |
$duplicate['post_date'] = date('Y-m-d H:i:s', $timestamp); |
| 97 |
$duplicate['post_date_gmt'] = date('Y-m-d H:i:s', $timestamp_gmt); |
| 98 |
$duplicate['post_modified'] = date('Y-m-d H:i:s', current_time('timestamp',0)); |
| 99 |
$duplicate['post_modified_gmt'] = date('Y-m-d H:i:s', current_time('timestamp',1)); |
| 100 |
if ( 'current_user' == $settings['post_author'] ) { |
| 101 |
$duplicate['post_author'] = get_current_user_id(); |
| 102 |
} |
| 103 |
|
| 104 |
// Remove some of the keys |
| 105 |
unset( $duplicate['ID'] ); |
| 106 |
unset( $duplicate['guid'] ); |
| 107 |
unset( $duplicate['comment_count'] ); |
| 108 |
|
| 109 |
//$duplicate['post_content'] = wp_slash( str_replace( array( '\r\n', '\r', '\n' ), '<br />', wp_kses_post( $duplicate['post_content'] ) ) ); |
| 110 |
add_filter( 'wp_kses_allowed_html', __NAMESPACE__ . '\additional_kses', 10, 2 ); |
| 111 |
$duplicate['post_content'] = wp_slash( wp_kses_post( $duplicate['post_content'] ) ); |
| 112 |
remove_filter( 'wp_kses_allowed_html', __NAMESPACE__ . '\additional_kses', 10, 2 ); |
| 113 |
|
| 114 |
// Insert the post into the database |
| 115 |
$duplicate_id = wp_insert_post( $duplicate ); |
| 116 |
|
| 117 |
// Duplicate all the taxonomies/terms |
| 118 |
$taxonomies = get_object_taxonomies( $duplicate['post_type'] ); |
| 119 |
$disabled_taxonomies = ['post_translations']; |
| 120 |
foreach( $taxonomies as $taxonomy ) { |
| 121 |
if ( in_array( $taxonomy, $disabled_taxonomies ) ) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
$terms = wp_get_post_terms( $original_id, $taxonomy, array('fields' => 'names') ); |
| 125 |
wp_set_object_terms( $duplicate_id, $terms, $taxonomy ); |
| 126 |
} |
| 127 |
|
| 128 |
// Duplicate all the custom fields |
| 129 |
$custom_fields = get_post_custom( $original_id ); |
| 130 |
foreach ( $custom_fields as $key => $value ) { |
| 131 |
if( is_array($value) && count($value) > 0 ) { |
| 132 |
foreach( $value as $i=>$v ) { |
| 133 |
if ( ! apply_filters( "mtphr_post_duplicator_meta_{$key}_enabled", true ) ) { |
| 134 |
continue; |
| 135 |
} |
| 136 |
$meta_value = apply_filters( "mtphr_post_duplicator_meta_value", $v, $key, $duplicate_id, $duplicate['post_type'] ); |
| 137 |
$data = array( |
| 138 |
'post_id' => intval( $duplicate_id ), |
| 139 |
'meta_key' => sanitize_text_field( $key ), |
| 140 |
'meta_value' => $meta_value, |
| 141 |
); |
| 142 |
$formats = array( |
| 143 |
'%d', |
| 144 |
'%s', |
| 145 |
'%s', |
| 146 |
); |
| 147 |
$result = $wpdb->insert( $wpdb->prefix.'postmeta', $data, $formats ); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// Add an action for others to do custom stuff |
| 153 |
do_action( 'mtphr_post_duplicator_created', $original_id, $duplicate_id, $settings ); |
| 154 |
|
| 155 |
return rest_ensure_response( [ |
| 156 |
'duplicate_id' => $duplicate_id, |
| 157 |
] , 200 ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Add custom allowed kses |
| 162 |
*/ |
| 163 |
function additional_kses( $allowed_tags ) { |
| 164 |
// Allow the center tag with its attributes |
| 165 |
$allowed_tags['center'] = array( |
| 166 |
'align' => true, |
| 167 |
'class' => true, |
| 168 |
'id' => true, |
| 169 |
'style' => true, |
| 170 |
); |
| 171 |
|
| 172 |
return $allowed_tags; |
| 173 |
} |