copy-post.php
| 1 | <?php |
| 2 | /** |
| 3 | * Module Name: Copy Post |
| 4 | * Module Description: Enable the option to copy entire posts and pages, including tags and settings |
| 5 | * Sort Order: 15 |
| 6 | * First Introduced: 7.0 |
| 7 | * Requires Connection: No |
| 8 | * Auto Activate: No |
| 9 | * Module Tags: Writing |
| 10 | * Feature: Writing |
| 11 | * Additional Search Queries: copy, duplicate |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * Copy Post class. |
| 16 | */ |
| 17 | class Jetpack_Copy_Post { |
| 18 | /** |
| 19 | * Jetpack_Copy_Post_By_Param constructor. |
| 20 | * Add row actions to post/page/CPT listing screens. |
| 21 | * Process any `?copy` param if on a create new post/page/CPT screen. |
| 22 | * |
| 23 | * @return void |
| 24 | */ |
| 25 | public function __construct() { |
| 26 | if ( 'edit.php' === $GLOBALS['pagenow'] ) { |
| 27 | add_filter( 'post_row_actions', array( $this, 'add_row_action' ), 10, 2 ); |
| 28 | add_filter( 'page_row_actions', array( $this, 'add_row_action' ), 10, 2 ); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | if ( ! empty( $_GET['jetpack-copy'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) { |
| 33 | add_action( 'wp_insert_post', array( $this, 'update_post_data' ), 10, 3 ); |
| 34 | add_filter( 'pre_option_default_post_format', '__return_empty_string' ); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Update the new (target) post data with the source post data. |
| 40 | * |
| 41 | * @param int $target_post_id Target post ID. |
| 42 | * @param WP_Post $post Target post object (not used). |
| 43 | * @param bool $update Whether this is an existing post being updated or not. |
| 44 | * @return void |
| 45 | */ |
| 46 | public function update_post_data( $target_post_id, $post, $update ) { |
| 47 | global $wp_version; |
| 48 | // This `$update` check avoids infinite loops of trying to update our updated post. |
| 49 | if ( $update ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | $source_post = get_post( $_GET['jetpack-copy'] ); |
| 54 | if ( ! $source_post instanceof WP_Post || |
| 55 | ! $this->user_can_access_post( $source_post->ID ) || |
| 56 | ! $this->validate_post_type( $source_post ) ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | $update_results = array( |
| 61 | 'update_content' => $this->update_content( $source_post, $target_post_id ), |
| 62 | 'update_featured_image' => $this->update_featured_image( $source_post, $target_post_id ), |
| 63 | 'update_post_format' => $this->update_post_format( $source_post, $target_post_id ), |
| 64 | 'update_likes_sharing' => $this->update_likes_sharing( $source_post, $target_post_id ), |
| 65 | 'update_post_type_terms' => $this->update_post_type_terms( $source_post, $target_post_id ), |
| 66 | ); |
| 67 | |
| 68 | // Required to satisfy get_default_post_to_edit(), which has these filters after post creation. |
| 69 | add_filter( 'default_title', array( $this, 'filter_title' ), 10, 2 ); |
| 70 | add_filter( 'default_content', array( $this, 'filter_content' ), 10, 2 ); |
| 71 | add_filter( 'default_excerpt', array( $this, 'filter_excerpt' ), 10, 2 ); |
| 72 | |
| 73 | /* |
| 74 | * Required to avoid the block editor from adding default blocks according to post format. |
| 75 | * @todo: simplify once WordPress 5.8 is the minimum required version. |
| 76 | */ |
| 77 | if ( version_compare( $wp_version, '5.8', '>=' ) ) { |
| 78 | add_filter( 'block_editor_settings_all', array( $this, 'remove_post_format_template' ) ); |
| 79 | } else { |
| 80 | add_filter( 'block_editor_settings', array( $this, 'remove_post_format_template' ) ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Fires after all updates have been performed, and default content filters have been added. |
| 85 | * Allows for any cleanup or post operations, and default content filters can be removed or modified. |
| 86 | * |
| 87 | * @module copy-post |
| 88 | * |
| 89 | * @since 7.0.0 |
| 90 | * |
| 91 | * @param WP_Post $source_post Post object that was copied. |
| 92 | * @param int $target_post_id Target post ID. |
| 93 | * @param array $update_results Results of all update operations, allowing action to be taken. |
| 94 | */ |
| 95 | do_action( 'jetpack_copy_post', $source_post, $target_post_id, $update_results ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Determine if the current user has edit access to the source post. |
| 100 | * |
| 101 | * @param int $post_id Source post ID (the post being copied). |
| 102 | * @return bool True if user has the meta cap of `edit_post` for the given post ID, false otherwise. |
| 103 | */ |
| 104 | protected function user_can_access_post( $post_id ) { |
| 105 | return current_user_can( 'edit_post', $post_id ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Update the target post's title, content, excerpt, categories, and tags. |
| 110 | * |
| 111 | * @param WP_Post $source_post Post object to be copied. |
| 112 | * @param int $target_post_id Target post ID. |
| 113 | * @return int 0 on failure, or the updated post ID on success. |
| 114 | */ |
| 115 | protected function update_content( $source_post, $target_post_id ) { |
| 116 | $data = array( |
| 117 | 'ID' => $target_post_id, |
| 118 | 'post_title' => $source_post->post_title, |
| 119 | 'post_content' => $source_post->post_content, |
| 120 | 'post_excerpt' => $source_post->post_excerpt, |
| 121 | 'comment_status' => $source_post->comment_status, |
| 122 | 'ping_status' => $source_post->ping_status, |
| 123 | 'post_category' => wp_get_post_categories( $source_post->ID ), |
| 124 | 'post_password' => $source_post->post_password, |
| 125 | 'tags_input' => $source_post->tags_input, |
| 126 | ); |
| 127 | |
| 128 | /** |
| 129 | * Fires just before the target post is updated with its new data. |
| 130 | * Allows for final data adjustments before updating the target post. |
| 131 | * |
| 132 | * @module copy-post |
| 133 | * |
| 134 | * @since 7.0.0 |
| 135 | * |
| 136 | * @param array $data Post data with which to update the target (new) post. |
| 137 | * @param WP_Post $source_post Post object being copied. |
| 138 | * @param int $target_post_id Target post ID. |
| 139 | */ |
| 140 | $data = apply_filters( 'jetpack_copy_post_data', $data, $source_post, $target_post_id ); |
| 141 | return wp_update_post( $data ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Update terms for post types. |
| 146 | * |
| 147 | * @param WP_Post $source_post Post object to be copied. |
| 148 | * @param int $target_post_id Target post ID. |
| 149 | * @return array Results of attempts to set each term to the target (new) post. |
| 150 | */ |
| 151 | protected function update_post_type_terms( $source_post, $target_post_id ) { |
| 152 | $results = array(); |
| 153 | |
| 154 | $bypassed_post_types = apply_filters( 'jetpack_copy_post_bypassed_post_types', array( 'post', 'page' ), $source_post, $target_post_id ); |
| 155 | if ( in_array( $source_post->post_type, $bypassed_post_types, true ) ) { |
| 156 | return $results; |
| 157 | } |
| 158 | |
| 159 | $taxonomies = get_object_taxonomies( $source_post, 'objects' ); |
| 160 | foreach ( $taxonomies as $taxonomy ) { |
| 161 | $terms = wp_get_post_terms( $source_post->ID, $taxonomy->name, array( 'fields' => 'ids' ) ); |
| 162 | $results[] = wp_set_post_terms( $target_post_id, $terms, $taxonomy->name ); |
| 163 | } |
| 164 | |
| 165 | return $results; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Update the target post's featured image. |
| 170 | * |
| 171 | * @param WP_Post $source_post Post object to be copied. |
| 172 | * @param int $target_post_id Target post ID. |
| 173 | * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
| 174 | */ |
| 175 | protected function update_featured_image( $source_post, $target_post_id ) { |
| 176 | $featured_image_id = get_post_thumbnail_id( $source_post ); |
| 177 | return update_post_meta( $target_post_id, '_thumbnail_id', $featured_image_id ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Update the target post's post format. |
| 182 | * |
| 183 | * @param WP_Post $source_post Post object to be copied. |
| 184 | * @param int $target_post_id Target post ID. |
| 185 | * @return array|WP_Error|false WP_Error on error, array of affected term IDs on success. |
| 186 | */ |
| 187 | protected function update_post_format( $source_post, $target_post_id ) { |
| 188 | $post_format = get_post_format( $source_post ); |
| 189 | return set_post_format( $target_post_id, $post_format ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Ensure the block editor doesn't modify the source post content for non-standard post formats. |
| 194 | * |
| 195 | * @param array $settings Settings to be passed into the block editor. |
| 196 | * @return array Settings with any `template` key removed. |
| 197 | */ |
| 198 | public function remove_post_format_template( $settings ) { |
| 199 | unset( $settings['template'] ); |
| 200 | return $settings; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Update the target post's Likes and Sharing statuses. |
| 205 | * |
| 206 | * @param WP_Post $source_post Post object to be copied. |
| 207 | * @param int $target_post_id Target post ID. |
| 208 | * @return array Array with the results of each update action. |
| 209 | */ |
| 210 | protected function update_likes_sharing( $source_post, $target_post_id ) { |
| 211 | $likes = get_post_meta( $source_post->ID, 'switch_like_status', true ); |
| 212 | $sharing = get_post_meta( $source_post->ID, 'sharing_disabled', true ); |
| 213 | |
| 214 | if ( '' !== $likes ) { |
| 215 | $likes_result = update_post_meta( $target_post_id, 'switch_like_status', $likes ); |
| 216 | } else { |
| 217 | $likes_result = null; |
| 218 | } |
| 219 | |
| 220 | if ( '' !== $sharing ) { |
| 221 | $sharing_result = update_post_meta( $target_post_id, 'sharing_disabled', $sharing ); |
| 222 | } else { |
| 223 | $sharing_result = null; |
| 224 | } |
| 225 | |
| 226 | return array( |
| 227 | 'likes' => $likes_result, |
| 228 | 'sharing' => $sharing_result, |
| 229 | ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Update the target post's title. |
| 234 | * |
| 235 | * @param string $post_title Post title determined by `get_default_post_to_edit()`. |
| 236 | * @param WP_Post $post Post object of newly-inserted post. |
| 237 | * @return string Updated post title from source post. |
| 238 | */ |
| 239 | public function filter_title( $post_title, $post ) { |
| 240 | return $post->post_title; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Update the target post's content (`post_content`). |
| 245 | * |
| 246 | * @param string $post_content Post content determined by `get_default_post_to_edit()`. |
| 247 | * @param WP_Post $post Post object of newly-inserted post. |
| 248 | * @return string Updated post content from source post. |
| 249 | */ |
| 250 | public function filter_content( $post_content, $post ) { |
| 251 | return $post->post_content; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Update the target post's excerpt. |
| 256 | * |
| 257 | * @param string $post_excerpt Post excerpt determined by `get_default_post_to_edit()`. |
| 258 | * @param WP_Post $post Post object of newly-inserted post. |
| 259 | * @return string Updated post excerpt from source post. |
| 260 | */ |
| 261 | public function filter_excerpt( $post_excerpt, $post ) { |
| 262 | return $post->post_excerpt; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Validate the post type to be used for the target post. |
| 267 | * |
| 268 | * @param WP_Post $post Post object of current post in listing. |
| 269 | * @return bool True if the post type is in a list of supported psot types; false otherwise. |
| 270 | */ |
| 271 | protected function validate_post_type( $post ) { |
| 272 | /** |
| 273 | * Fires when determining if the "Copy" row action should be made available. |
| 274 | * Allows overriding supported post types. |
| 275 | * |
| 276 | * @module copy-post |
| 277 | * |
| 278 | * @since 7.0.0 |
| 279 | * |
| 280 | * @param array Post types supported by default. |
| 281 | * @param WP_Post $post Post object of current post in listing. |
| 282 | */ |
| 283 | $valid_post_types = apply_filters( |
| 284 | 'jetpack_copy_post_post_types', |
| 285 | array( |
| 286 | 'post', |
| 287 | 'page', |
| 288 | 'jetpack-testimonial', |
| 289 | 'jetpack-portfolio', |
| 290 | ), |
| 291 | $post |
| 292 | ); |
| 293 | return in_array( $post->post_type, $valid_post_types, true ); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Add a "Copy" row action to supported posts/pages/CPTs on list views. |
| 298 | * |
| 299 | * @param array $actions Existing actions. |
| 300 | * @param WP_Post $post Post object of current post in list. |
| 301 | * @return array Array of updated row actions. |
| 302 | */ |
| 303 | public function add_row_action( $actions, $post ) { |
| 304 | if ( ! $this->user_can_access_post( $post->ID ) || |
| 305 | ! $post instanceof WP_Post || |
| 306 | ! $this->validate_post_type( $post ) ) { |
| 307 | return $actions; |
| 308 | } |
| 309 | |
| 310 | $edit_url = add_query_arg( |
| 311 | array( |
| 312 | 'post_type' => $post->post_type, |
| 313 | 'jetpack-copy' => $post->ID, |
| 314 | ), |
| 315 | admin_url( 'post-new.php' ) |
| 316 | ); |
| 317 | $edit_action = array( |
| 318 | 'jetpack-copy' => sprintf( |
| 319 | '<a href="%s" aria-label="%s">%s</a>', |
| 320 | esc_url( $edit_url ), |
| 321 | esc_attr__( 'Copy this post.', 'jetpack' ), |
| 322 | esc_html__( 'Copy', 'jetpack' ) |
| 323 | ), |
| 324 | ); |
| 325 | |
| 326 | // Insert the Copy action before the Trash action. |
| 327 | $edit_offset = array_search( 'trash', array_keys( $actions ), true ); |
| 328 | $updated_actions = array_merge( |
| 329 | array_slice( $actions, 0, $edit_offset ), |
| 330 | $edit_action, |
| 331 | array_slice( $actions, $edit_offset ) |
| 332 | ); |
| 333 | |
| 334 | /** |
| 335 | * Fires after the new Copy action has been added to the row actions. |
| 336 | * Allows changes to the action presentation, or other final checks. |
| 337 | * |
| 338 | * @module copy-post |
| 339 | * |
| 340 | * @since 7.0.0 |
| 341 | * |
| 342 | * @param array $updated_actions Updated row actions with the Copy Post action. |
| 343 | * @param array $actions Original row actions passed to this filter. |
| 344 | * @param WP_Post $post Post object of current post in listing. |
| 345 | */ |
| 346 | return apply_filters( 'jetpack_copy_post_row_actions', $updated_actions, $actions, $post ); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Instantiate an instance of Jetpack_Copy_Post on the `admin_init` hook. |
| 352 | */ |
| 353 | function jetpack_copy_post_init() { |
| 354 | new Jetpack_Copy_Post(); |
| 355 | } |
| 356 | add_action( 'admin_init', 'jetpack_copy_post_init' ); |
| 357 |