Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Module Name: Copy Post |
||
| 4 | * Module Description: Copy an existing post's content into a new draft post |
||
| 5 | * Jumpstart Description: Copy an existing post's content into a new draft post |
||
| 6 | * Sort Order: 15 |
||
| 7 | * First Introduced: 7.0 |
||
| 8 | * Requires Connection: No |
||
| 9 | * Auto Activate: No |
||
| 10 | * Module Tags: Writing |
||
| 11 | * Feature: Writing |
||
| 12 | * Additional Search Queries: copy, duplicate |
||
| 13 | */ |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Copy Post class. |
||
| 17 | */ |
||
| 18 | class Jetpack_Copy_Post { |
||
| 19 | /** |
||
| 20 | * Jetpack_Copy_Post_By_Param constructor. |
||
| 21 | * Add row actions to post/page/CPT listing screens. |
||
| 22 | * Process any `?copy` param if on a create new post/page/CPT screen. |
||
| 23 | * |
||
| 24 | * @return void |
||
|
0 ignored issues
–
show
|
|||
| 25 | */ |
||
| 26 | public function __construct() { |
||
| 27 | if ( 'edit.php' === $GLOBALS['pagenow'] ) { |
||
| 28 | add_filter( 'post_row_actions', array( $this, 'add_row_action' ), 10, 2 ); |
||
| 29 | add_filter( 'page_row_actions', array( $this, 'add_row_action' ), 10, 2 ); |
||
| 30 | return; |
||
| 31 | } |
||
| 32 | |||
| 33 | if ( ! empty( $_GET['jetpack-copy'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) { |
||
| 34 | add_action( 'wp_insert_post', array( $this, 'update_post_data' ), 10, 3 ); |
||
| 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 | // This `$update` check avoids infinite loops of trying to update our updated post. |
||
| 48 | if ( $update ) { |
||
| 49 | return; |
||
| 50 | } |
||
| 51 | |||
| 52 | $source_post = get_post( $_GET['jetpack-copy'] ); |
||
| 53 | if ( ! $source_post instanceof WP_Post || |
||
|
0 ignored issues
–
show
The class
WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. Loading history...
|
|||
| 54 | ! $this->user_can_access_post( $source_post->ID ) || |
||
| 55 | ! $this->validate_post_type( $source_post ) ) { |
||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | $update_results = array( |
||
| 60 | 'update_content' => $this->update_content( $source_post, $target_post_id ), |
||
| 61 | 'update_featured_image' => $this->update_featured_image( $source_post, $target_post_id ), |
||
| 62 | 'update_post_format' => $this->update_post_format( $source_post, $target_post_id ), |
||
| 63 | 'update_likes_sharing' => $this->update_likes_sharing( $source_post, $target_post_id ), |
||
| 64 | 'update_post_type_terms' => $this->update_post_type_terms( $source_post, $target_post_id ), |
||
| 65 | ); |
||
| 66 | |||
| 67 | // Required to satify get_default_post_to_edit(), which has these filters after post creation. |
||
| 68 | add_filter( 'default_title', array( $this, 'filter_title' ), 10, 2 ); |
||
| 69 | add_filter( 'default_content', array( $this, 'filter_content' ), 10, 2 ); |
||
| 70 | add_filter( 'default_excerpt', array( $this, 'filter_excerpt' ), 10, 2 ); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Fires after all updates have been performed, and default content filters have been added. |
||
| 74 | * Allows for any cleanup or post operations, and default content filters can be removed or modified. |
||
| 75 | * |
||
| 76 | * @module copy-post |
||
| 77 | * |
||
| 78 | * @since 7.0.0 |
||
| 79 | * |
||
| 80 | * @param WP_Post $source_post Post object that was copied. |
||
| 81 | * @param int $target_post_id Target post ID. |
||
| 82 | * @param array $update_results Results of all update operations, allowing action to be taken. |
||
| 83 | */ |
||
| 84 | do_action( 'jetpack_copy_post', $source_post, $target_post_id, $update_results ); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Determine if the current user has edit access to the source post. |
||
| 89 | * |
||
| 90 | * @param int $post_id Source post ID (the post being copied). |
||
| 91 | * @return bool True if user has the meta cap of `edit_post` for the given post ID, false otherwise. |
||
| 92 | */ |
||
| 93 | protected function user_can_access_post( $post_id ) { |
||
| 94 | return current_user_can( 'edit_post', $post_id ); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Update the target post's title, content, excerpt, categories, and tags. |
||
| 99 | * |
||
| 100 | * @param WP_Post $source_post Post object to be copied. |
||
| 101 | * @param int $target_post_id Target post ID. |
||
| 102 | * @return int 0 on failure, or the updated post ID on success. |
||
| 103 | */ |
||
| 104 | protected function update_content( $source_post, $target_post_id ) { |
||
| 105 | $data = array( |
||
| 106 | 'ID' => $target_post_id, |
||
| 107 | 'post_title' => $source_post->post_title, |
||
| 108 | 'post_content' => $source_post->post_content, |
||
| 109 | 'post_excerpt' => $source_post->post_excerpt, |
||
| 110 | 'comment_status' => $source_post->comment_status, |
||
| 111 | 'ping_status' => $source_post->ping_status, |
||
| 112 | 'post_category' => $source_post->post_category, |
||
| 113 | 'post_password' => $source_post->post_password, |
||
| 114 | 'tags_input' => $source_post->tags_input, |
||
| 115 | ); |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Fires just before the target post is updated with its new data. |
||
| 119 | * Allows for final data adjustments before updating the target post. |
||
| 120 | * |
||
| 121 | * @module copy-post |
||
| 122 | * |
||
| 123 | * @since 7.0.0 |
||
| 124 | * |
||
| 125 | * @param array $data Post data with which to update the target (new) post. |
||
| 126 | * @param WP_Post $source_post Post object being copied. |
||
| 127 | * @param int $target_post_id Target post ID. |
||
| 128 | */ |
||
| 129 | $data = apply_filters( 'jetpack_copy_post_data', $data, $source_post, $target_post_id ); |
||
| 130 | return wp_update_post( $data ); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Update terms for post types. |
||
| 135 | * |
||
| 136 | * @param WP_Post $source_post Post object to be copied. |
||
| 137 | * @param int $target_post_id Target post ID. |
||
| 138 | * @return array Results of attempts to set each term to the target (new) post. |
||
| 139 | */ |
||
| 140 | protected function update_post_type_terms( $source_post, $target_post_id ) { |
||
| 141 | $results = array(); |
||
| 142 | |||
| 143 | $bypassed_post_types = apply_filters( 'jetpack_copy_post_bypassed_post_types', array( 'post', 'page' ), $source_post, $target_post_id ); |
||
| 144 | if ( in_array( $source_post->post_type, $bypassed_post_types, true ) ) { |
||
| 145 | return $results; |
||
| 146 | } |
||
| 147 | |||
| 148 | $taxonomies = get_object_taxonomies( $source_post, 'objects' ); |
||
| 149 | foreach ( $taxonomies as $taxonomy ) { |
||
| 150 | $terms = wp_get_post_terms( $source_post->ID, $taxonomy->name, array( 'fields' => 'ids' ) ); |
||
| 151 | $results[] = wp_set_post_terms( $target_post_id, $terms, $taxonomy->name ); |
||
| 152 | } |
||
| 153 | |||
| 154 | return $results; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Update the target post's featured image. |
||
| 159 | * |
||
| 160 | * @param WP_Post $source_post Post object to be copied. |
||
| 161 | * @param int $target_post_id Target post ID. |
||
| 162 | * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
||
| 163 | */ |
||
| 164 | protected function update_featured_image( $source_post, $target_post_id ) { |
||
| 165 | $featured_image_id = get_post_thumbnail_id( $source_post ); |
||
| 166 | return update_post_meta( $target_post_id, '_thumbnail_id', $featured_image_id ); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Update the target post's post format. |
||
| 171 | * |
||
| 172 | * @param WP_Post $source_post Post object to be copied. |
||
| 173 | * @param int $target_post_id Target post ID. |
||
| 174 | * @return array|WP_Error|false WP_Error on error, array of affected term IDs on success. |
||
| 175 | */ |
||
| 176 | protected function update_post_format( $source_post, $target_post_id ) { |
||
| 177 | $post_format = get_post_format( $source_post ); |
||
| 178 | return set_post_format( $target_post_id, $post_format ); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Update the target post's Likes and Sharing statuses. |
||
| 183 | * |
||
| 184 | * @param WP_Post $source_post Post object to be copied. |
||
| 185 | * @param int $target_post_id Target post ID. |
||
| 186 | * @return array Array with the results of each update action. |
||
| 187 | */ |
||
| 188 | protected function update_likes_sharing( $source_post, $target_post_id ) { |
||
| 189 | $likes = get_post_meta( $source_post->ID, 'switch_like_status', true ); |
||
| 190 | $sharing = get_post_meta( $source_post->ID, 'sharing_disabled', false ); |
||
| 191 | $likes_result = update_post_meta( $target_post_id, 'switch_like_status', $likes ); |
||
| 192 | $sharing_result = update_post_meta( $target_post_id, 'sharing_disabled', $sharing ); |
||
| 193 | return array( |
||
| 194 | 'likes' => $likes_result, |
||
| 195 | 'sharing' => $sharing_result, |
||
| 196 | ); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Update the target post's title. |
||
| 201 | * |
||
| 202 | * @param string $post_title Post title determined by `get_default_post_to_edit()`. |
||
| 203 | * @param WP_Post $post Post object of newly-inserted post. |
||
| 204 | * @return string Updated post title from source post. |
||
| 205 | */ |
||
| 206 | public function filter_title( $post_title, $post ) { |
||
| 207 | return $post->post_title; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Update the target post's content (`post_content`). |
||
| 212 | * |
||
| 213 | * @param string $post_content Post content determined by `get_default_post_to_edit()`. |
||
| 214 | * @param WP_Post $post Post object of newly-inserted post. |
||
| 215 | * @return string Updated post content from source post. |
||
| 216 | */ |
||
| 217 | public function filter_content( $post_content, $post ) { |
||
| 218 | return $post->post_content; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Update the target post's excerpt. |
||
| 223 | * |
||
| 224 | * @param string $post_excerpt Post excerpt determined by `get_default_post_to_edit()`. |
||
| 225 | * @param WP_Post $post Post object of newly-inserted post. |
||
| 226 | * @return string Updated post excerpt from source post. |
||
| 227 | */ |
||
| 228 | public function filter_excerpt( $post_excerpt, $post ) { |
||
| 229 | return $post->post_excerpt; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Validate the post type to be used for the target post. |
||
| 234 | * |
||
| 235 | * @param WP_Post $post Post object of current post in listing. |
||
| 236 | * @return bool True if the post type is in a list of supported psot types; false otherwise. |
||
| 237 | */ |
||
| 238 | protected function validate_post_type( $post ) { |
||
| 239 | /** |
||
| 240 | * Fires when determining if the "Copy" row action should be made available. |
||
| 241 | * Allows overriding supported post types. |
||
| 242 | * |
||
| 243 | * @module copy-post |
||
| 244 | * |
||
| 245 | * @since 7.0.0 |
||
| 246 | * |
||
| 247 | * @param array Post types supported by default. |
||
| 248 | * @param WP_Post $post Post object of current post in listing. |
||
| 249 | */ |
||
| 250 | $valid_post_types = apply_filters( |
||
| 251 | 'jetpack_copy_post_post_types', |
||
| 252 | array( |
||
| 253 | 'post', |
||
| 254 | 'page', |
||
| 255 | 'jetpack-testimonial', |
||
| 256 | 'jetpack-portfolio', |
||
| 257 | ), |
||
| 258 | $post |
||
| 259 | ); |
||
| 260 | return in_array( $post->post_type, $valid_post_types, true ); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Add a "Copy" row action to supported posts/pages/CPTs on list views. |
||
| 265 | * |
||
| 266 | * @param array $actions Existing actions. |
||
| 267 | * @param WP_Post $post Post object of current post in list. |
||
| 268 | * @return array Array of updated row actions. |
||
| 269 | */ |
||
| 270 | public function add_row_action( $actions, $post ) { |
||
| 271 | if ( ! $this->user_can_access_post( $post->ID ) || |
||
| 272 | ! $post instanceof WP_Post || |
||
|
0 ignored issues
–
show
The class
WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. Loading history...
|
|||
| 273 | ! $this->validate_post_type( $post ) ) { |
||
| 274 | return $actions; |
||
| 275 | } |
||
| 276 | |||
| 277 | $edit_url = add_query_arg( |
||
| 278 | array( |
||
| 279 | 'post_type' => $post->post_type, |
||
| 280 | 'jetpack-copy' => $post->ID, |
||
| 281 | ), |
||
| 282 | admin_url( 'post-new.php' ) |
||
| 283 | ); |
||
| 284 | $edit_action = array( |
||
| 285 | 'jetpack-copy' => sprintf( |
||
| 286 | '<a href="%s" aria-label="%s">%s</a>', |
||
| 287 | esc_url( $edit_url ), |
||
| 288 | esc_attr__( 'Copy this post.', 'jetpack' ), |
||
| 289 | esc_html__( 'Copy', 'jetpack' ) |
||
| 290 | ), |
||
| 291 | ); |
||
| 292 | |||
| 293 | // Insert the Copy action before the Trash action. |
||
| 294 | $edit_offset = array_search( 'trash', array_keys( $actions ), true ); |
||
| 295 | $updated_actions = array_merge( |
||
| 296 | array_slice( $actions, 0, $edit_offset ), |
||
| 297 | $edit_action, |
||
| 298 | array_slice( $actions, $edit_offset ) |
||
| 299 | ); |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Fires after the new Copy action has been added to the row actions. |
||
| 303 | * Allows changes to the action presentation, or other final checks. |
||
| 304 | * |
||
| 305 | * @module copy-post |
||
| 306 | * |
||
| 307 | * @since 7.0.0 |
||
| 308 | * |
||
| 309 | * @param array $updated_actions Updated row actions with the Copy Post action. |
||
| 310 | * @param array $actions Original row actions passed to this filter. |
||
| 311 | * @param WP_Post $post Post object of current post in listing. |
||
| 312 | */ |
||
| 313 | return apply_filters( 'jetpack_copy_post_row_actions', $updated_actions, $actions, $post ); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Instantiate an instance of Jetpack_Copy_Post on the `admin_init` hook. |
||
| 319 | */ |
||
| 320 | function jetpack_copy_post_init() { |
||
| 321 | new Jetpack_Copy_Post(); |
||
| 322 | } |
||
| 323 | add_action( 'admin_init', 'jetpack_copy_post_init' ); |
||
| 324 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.