1 | <?php |
||
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 |
||
|
|||
25 | */ |
||
26 | public function __construct() { |
||
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 || |
||
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 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 `read_post` for the given post ID, false otherwise. |
||
92 | */ |
||
93 | protected function user_can_access_post( $post_id ) { |
||
94 | return current_user_can( 'read_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 | 'tags_input' => $source_post->tags_input, |
||
114 | ); |
||
115 | |||
116 | /** |
||
117 | * Fires just before the target post is updated with its new data. |
||
118 | * Allows for final data adjustments before updating the target post. |
||
119 | * |
||
120 | * @module copy-post |
||
121 | * |
||
122 | * @since 7.0.0 |
||
123 | * |
||
124 | * @param array $data Post data with which to update the target (new) post. |
||
125 | * @param WP_Post $source_post Post object being copied. |
||
126 | * @param int $target_post_id Target post ID. |
||
127 | */ |
||
128 | $data = apply_filters( 'jetpack_copy_post_data', $data, $source_post, $target_post_id ); |
||
129 | return wp_update_post( $data ); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Update terms for post types. |
||
134 | * |
||
135 | * @param WP_Post $source_post Post object to be copied. |
||
136 | * @param int $target_post_id Target post ID. |
||
137 | * @return array Results of attempts to set each term to the target (new) post. |
||
138 | */ |
||
139 | protected function update_post_type_terms( $source_post, $target_post_id ) { |
||
140 | $results = array(); |
||
141 | |||
142 | $bypassed_post_types = apply_filters( 'jetpack_copy_post_bypassed_post_types', array( 'post', 'page' ), $source_post, $target_post_id ); |
||
143 | if ( in_array( $source_post->post_type, $bypassed_post_types, true ) ) { |
||
144 | return $results; |
||
145 | } |
||
146 | |||
147 | $taxonomies = get_object_taxonomies( $source_post, 'objects' ); |
||
148 | foreach ( $taxonomies as $taxonomy ) { |
||
149 | $terms = wp_get_post_terms( $source_post->ID, $taxonomy->name, array( 'fields' => 'ids' ) ); |
||
150 | $results[] = wp_set_post_terms( $target_post_id, $terms, $taxonomy->name ); |
||
151 | } |
||
152 | |||
153 | return $results; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Update the target post's featured image. |
||
158 | * |
||
159 | * @param WP_Post $source_post Post object to be copied. |
||
160 | * @param int $target_post_id Target post ID. |
||
161 | * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
||
162 | */ |
||
163 | protected function update_featured_image( $source_post, $target_post_id ) { |
||
167 | |||
168 | /** |
||
169 | * Update the target post's post format. |
||
170 | * |
||
171 | * @param WP_Post $source_post Post object to be copied. |
||
172 | * @param int $target_post_id Target post ID. |
||
173 | * @return array|WP_Error|false WP_Error on error, array of affected term IDs on success. |
||
174 | */ |
||
175 | protected function update_post_format( $source_post, $target_post_id ) { |
||
179 | |||
180 | /** |
||
181 | * Update the target post's Likes and Sharing statuses. |
||
182 | * |
||
183 | * @param WP_Post $source_post Post object to be copied. |
||
184 | * @param int $target_post_id Target post ID. |
||
185 | * @return array Array with the results of each update action. |
||
186 | */ |
||
187 | protected function update_likes_sharing( $source_post, $target_post_id ) { |
||
197 | |||
198 | /** |
||
199 | * Update the target post's title. |
||
200 | * |
||
201 | * @param string $post_title Post title determined by `get_default_post_to_edit()`. |
||
202 | * @param WP_Post $post Post object of newly-inserted post. |
||
203 | * @return string Updated post title from source post. |
||
204 | */ |
||
205 | public function filter_title( $post_title, $post ) { |
||
208 | |||
209 | /** |
||
210 | * Update the target post's content (`post_content`). |
||
211 | * |
||
212 | * @param string $post_content Post content determined by `get_default_post_to_edit()`. |
||
213 | * @param WP_Post $post Post object of newly-inserted post. |
||
214 | * @return string Updated post content from source post. |
||
215 | */ |
||
216 | public function filter_content( $post_content, $post ) { |
||
219 | |||
220 | /** |
||
221 | * Update the target post's excerpt. |
||
222 | * |
||
223 | * @param string $post_excerpt Post excerpt determined by `get_default_post_to_edit()`. |
||
224 | * @param WP_Post $post Post object of newly-inserted post. |
||
225 | * @return string Updated post excerpt from source post. |
||
226 | */ |
||
227 | public function filter_excerpt( $post_excerpt, $post ) { |
||
230 | |||
231 | /** |
||
232 | * Validate the post type to be used for the target post. |
||
233 | * |
||
234 | * @param WP_Post $post Post object of current post in listing. |
||
235 | * @return bool True if the post type is in a list of supported psot types; false otherwise. |
||
236 | */ |
||
237 | protected function validate_post_type( $post ) { |
||
261 | |||
262 | /** |
||
263 | * Add a "Copy" row action to supported posts/pages/CPTs on list views. |
||
264 | * |
||
265 | * @param array $actions Existing actions. |
||
266 | * @param WP_Post $post Post object of current post in list. |
||
267 | * @return array Array of updated row actions. |
||
268 | */ |
||
269 | public function add_row_action( $actions, $post ) { |
||
314 | } |
||
315 | |||
323 |
Adding a
@return
annotation 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.