| | 1 | <?php |
| | 2 | /** |
| | 3 | * Class file for WP_Customize_Changeset |
| | 4 | * |
| | 5 | * @package WordPress |
| | 6 | * @subpackage Customize |
| | 7 | * @since 4.9.0 |
| | 8 | */ |
| | 9 | |
| | 10 | /** |
| | 11 | * Representation of a Customize Changeset. |
| | 12 | */ |
| | 13 | class WP_Customize_Changeset { |
| | 14 | /** |
| | 15 | * Changeset UUID, the post_name for the customize_changeset post containing the customized state. |
| | 16 | * |
| | 17 | * @since 4.9.0 |
| | 18 | * @var string |
| | 19 | */ |
| | 20 | protected $uuid; |
| | 21 | |
| | 22 | /** |
| | 23 | * Changeset post ID. |
| | 24 | * |
| | 25 | * @since 4.9.0 |
| | 26 | * @var int |
| | 27 | */ |
| | 28 | protected $post_id; |
| | 29 | |
| | 30 | /** |
| | 31 | * Retrieve a WP_Customize_Changeset instance from a changeset UUID. |
| | 32 | * |
| | 33 | * Defers to {@see WP_Customize_Changeset::from_post()} if a post for the UUID exists. |
| | 34 | * |
| | 35 | * @since 4.9.0 |
| | 36 | * |
| | 37 | * @param string $uuid UUID. |
| | 38 | * @return WP_Customize_Changeset Changeset instance. |
| | 39 | */ |
| | 40 | public static function from_uuid( $uuid ) { |
| | 41 | $cache_group = 'customize_changeset_post'; |
| | 42 | |
| | 43 | $post_id = wp_cache_get( $uuid, $cache_group ); |
| | 44 | |
| | 45 | if ( $post_id && 'customize_changeset' === get_post_type( $post_id ) ) { |
| | 46 | return WP_Customize_Changeset::from_post( $post_id ); |
| | 47 | } |
| | 48 | |
| | 49 | // The full post object is being retrieved so it's cached. |
| | 50 | $query = new WP_Query( array( |
| | 51 | 'post_type' => 'customize_changeset', |
| | 52 | 'post_status' => get_post_stati(), |
| | 53 | 'name' => $uuid, |
| | 54 | 'posts_per_page' => 1, |
| | 55 | 'no_found_rows' => true, |
| | 56 | 'cache_results' => true, |
| | 57 | 'update_post_meta_cache' => false, |
| | 58 | 'update_post_term_cache' => false, |
| | 59 | 'lazy_load_term_meta' => false, |
| | 60 | ) ); |
| | 61 | |
| | 62 | if ( empty( $query->posts ) ) { |
| | 63 | $instance = new WP_Customize_Changeset(); |
| | 64 | $instance->set_uuid( $uuid ); |
| | 65 | return $instance; |
| | 66 | } |
| | 67 | |
| | 68 | $post_id = $query->posts[0]->ID; |
| | 69 | wp_cache_set( $uuid, $post_id, $cache_group ); |
| | 70 | |
| | 71 | return WP_Customize_Changeset::from_post( $post_id ); |
| | 72 | } |
| | 73 | |
| | 74 | /** |
| | 75 | * Retrieve a WP_Customize_Changeset instance from a post. |
| | 76 | * |
| | 77 | * @since 4.9.0 |
| | 78 | * |
| | 79 | * @param int|WP_Post $post Post ID or object. |
| | 80 | * @return WP_Customize_Changeset Changeset instance. |
| | 81 | */ |
| | 82 | public static function from_post( $post ) { |
| | 83 | $instance = new WP_Customize_Changeset(); |
| | 84 | |
| | 85 | $post = get_post( $post ); |
| | 86 | |
| | 87 | // @todo Return null instead if post doesn't exist? |
| | 88 | if ( $post instanceof WP_Post ) { |
| | 89 | $instance->parse_post( $post ); |
| | 90 | } |
| | 91 | |
| | 92 | return $instance; |
| | 93 | } |
| | 94 | |
| | 95 | /** |
| | 96 | * Populate instance properties from a post object. |
| | 97 | * |
| | 98 | * @since 4.9.0 |
| | 99 | * |
| | 100 | * @param WP_Post $post Post object. |
| | 101 | */ |
| | 102 | public function parse_post( $post ) { |
| | 103 | $this->post_id = $post->ID; |
| | 104 | $this->uuid = $post->post_name; |
| | 105 | } |
| | 106 | |
| | 107 | /** |
| | 108 | * Set the instance UUID. |
| | 109 | * |
| | 110 | * @param string $uuid UUID. |
| | 111 | */ |
| | 112 | public function set_uuid( $uuid ) { |
| | 113 | $this->uuid = $uuid; |
| | 114 | } |
| | 115 | |
| | 116 | /** |
| | 117 | * Get the changeset UUID. |
| | 118 | * |
| | 119 | * @since 4.9.0 |
| | 120 | * |
| | 121 | * @return string UUID. |
| | 122 | */ |
| | 123 | public function get_uuid() { |
| | 124 | return $this->uuid; |
| | 125 | } |
| | 126 | |
| | 127 | /** |
| | 128 | * Get the changeset post ID. |
| | 129 | * |
| | 130 | * @return int |
| | 131 | */ |
| | 132 | public function get_post_id() { |
| | 133 | return $this->post_id; |
| | 134 | } |
| | 135 | |
| | 136 | /** |
| | 137 | * Get the data stored in the changeset post, if one exists. |
| | 138 | * |
| | 139 | * @since 4.9.0 |
| | 140 | * |
| | 141 | * @return array|WP_Error Changeset data or WP_Error on error. |
| | 142 | */ |
| | 143 | public function get_post_data() { |
| | 144 | if ( ! $this->post_id ) { |
| | 145 | return new WP_Error( 'empty_post_id' ); |
| | 146 | } |
| | 147 | |
| | 148 | $post = get_post( $this->post_id ); |
| | 149 | |
| | 150 | if ( ! $post ) { |
| | 151 | return new WP_Error( 'missing_post' ); |
| | 152 | } |
| | 153 | |
| | 154 | if ( 'customize_changeset' !== get_post_type( $post ) ) { |
| | 155 | return new WP_Error( 'wrong_post_type' ); |
| | 156 | } |
| | 157 | |
| | 158 | $data = json_decode( $post->post_content, true ); |
| | 159 | |
| | 160 | if ( function_exists( 'json_last_error' ) && json_last_error() ) { |
| | 161 | return new WP_Error( 'json_parse_error', '', json_last_error() ); |
| | 162 | } |
| | 163 | |
| | 164 | if ( ! is_array( $data ) ) { |
| | 165 | return new WP_Error( 'expected_array' ); |
| | 166 | } |
| | 167 | |
| | 168 | return $data; |
| | 169 | } |
| | 170 | |
| | 171 | /** |
| | 172 | * Get the changeset data. |
| | 173 | * |
| | 174 | * @since 4.9.0 |
| | 175 | * |
| | 176 | * @return array Changeset data. |
| | 177 | */ |
| | 178 | public function get_data() { |
| | 179 | $data = $this->get_post_data(); |
| | 180 | return ( is_wp_error( $data ) ) ? array() : $data; |
| | 181 | } |
| | 182 | |
| | 183 | /** |
| | 184 | * Save the changeset post. |
| | 185 | * |
| | 186 | * @since 4.9.0 |
| | 187 | * |
| | 188 | * @param array $args { |
| | 189 | * Array of arguments that make up the changeset. |
| | 190 | * |
| | 191 | * @type array $data Changeset data. |
| | 192 | * @type string $date_gmt Changeset date in GMT. Optional. |
| | 193 | * @type string $status Changeset status. Optional. |
| | 194 | * @type string $title Changeset title. Optional. |
| | 195 | * } |
| | 196 | * @return array|WP_Error Array on success or WP_Error. |
| | 197 | */ |
| | 198 | public function save( $args ) { |
| | 199 | $args = wp_parse_args( $args, array( |
| | 200 | 'data' => array(), |
| | 201 | 'date_gmt' => null, |
| | 202 | 'status' => null, |
| | 203 | 'title' => null, |
| | 204 | ) ); |
| | 205 | |
| | 206 | $json_options = 0; |
| | 207 | |
| | 208 | if ( defined( 'JSON_UNESCAPED_SLASHES' ) ) { |
| | 209 | // Introduced in PHP 5.4. This is only to improve readability as slashes needn't be escaped in storage. |
| | 210 | $json_options |= JSON_UNESCAPED_SLASHES; |
| | 211 | } |
| | 212 | |
| | 213 | // Also introduced in PHP 5.4, but WP defines constant for back compat. See WP Trac #30139. |
| | 214 | $json_options |= JSON_PRETTY_PRINT; |
| | 215 | |
| | 216 | $post_array = array( |
| | 217 | 'post_content' => wp_json_encode( $args['data'], $json_options ), |
| | 218 | ); |
| | 219 | |
| | 220 | if ( $args['title'] ) { |
| | 221 | $post_array['post_title'] = $args['title']; |
| | 222 | } |
| | 223 | |
| | 224 | // @todo What if there is no UUID or post ID? |
| | 225 | if ( $this->post_id ) { |
| | 226 | $post_array['ID'] = $this->post_id; |
| | 227 | } else { |
| | 228 | $post_array['post_type'] = 'customize_changeset'; |
| | 229 | $post_array['post_name'] = $this->uuid; |
| | 230 | $post_array['post_status'] = 'auto-draft'; |
| | 231 | } |
| | 232 | |
| | 233 | if ( $args['status'] ) { |
| | 234 | $post_array['post_status'] = $args['status']; |
| | 235 | } |
| | 236 | |
| | 237 | // Reset post date to now if we are publishing, otherwise pass post_date_gmt and translate for post_date. |
| | 238 | if ( 'publish' === $args['status'] ) { |
| | 239 | $post_array['post_date_gmt'] = '0000-00-00 00:00:00'; |
| | 240 | $post_array['post_date'] = '0000-00-00 00:00:00'; |
| | 241 | } elseif ( $args['date_gmt'] ) { |
| | 242 | $post_array['post_date_gmt'] = $args['date_gmt']; |
| | 243 | $post_array['post_date'] = get_date_from_gmt( $args['date_gmt'] ); |
| | 244 | } elseif ( $this->post_id && 'auto-draft' === get_post_status( $this->post_id ) ) { |
| | 245 | /* |
| | 246 | * Keep bumping the date for the auto-draft whenever it is modified; |
| | 247 | * this extends its life, preserving it from garbage-collection via |
| | 248 | * wp_delete_auto_drafts(). |
| | 249 | */ |
| | 250 | $post_array['post_date'] = current_time( 'mysql' ); |
| | 251 | $post_array['post_date_gmt'] = ''; |
| | 252 | } |
| | 253 | |
| | 254 | /* |
| | 255 | * Update the changeset post. The 'publish_customize_changeset' action |
| | 256 | * will cause the settings in the changeset to be saved via |
| | 257 | * WP_Customize_Setting::save(). |
| | 258 | */ |
| | 259 | $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) ); |
| | 260 | if ( $has_kses ) { |
| | 261 | // Prevent KSES from corrupting JSON in post_content. |
| | 262 | kses_remove_filters(); |
| | 263 | } |
| | 264 | |
| | 265 | // Note that updating a post with publish status will trigger WP_Customize_Manager::publish_changeset_values(). |
| | 266 | if ( $this->post_id ) { |
| | 267 | // Prevent date clearing. |
| | 268 | $post_array['edit_date'] = true; |
| | 269 | |
| | 270 | $result = wp_update_post( wp_slash( $post_array ), true ); |
| | 271 | } else { |
| | 272 | $result = wp_insert_post( wp_slash( $post_array ), true ); |
| | 273 | |
| | 274 | if ( is_numeric( $result ) ) { |
| | 275 | $this->post_id = (int) $result; |
| | 276 | } |
| | 277 | } |
| | 278 | |
| | 279 | if ( $has_kses ) { |
| | 280 | kses_init_filters(); |
| | 281 | } |
| | 282 | |
| | 283 | return $result; |
| | 284 | } |
| | 285 | |
| | 286 | /** |
| | 287 | * Publish the changeset values. |
| | 288 | * |
| | 289 | * @since 4.9.0 |
| | 290 | * |
| | 291 | * @param WP_Customize_Manager $wp_customize Customize manager instance that |
| | 292 | * should publish the changeset. |
| | 293 | * @return bool|WP_Error True or a WP_Error. |
| | 294 | */ |
| | 295 | public function publish( $wp_customize ) { |
| | 296 | $result = $wp_customize->_publish_changeset_values( $this->post_id ); |
| | 297 | |
| | 298 | if ( true === $result ) { |
| | 299 | /* |
| | 300 | * Trash the changeset post if revisions are not enabled. |
| | 301 | * |
| | 302 | * Unpublished changesets by default get garbage collected due to |
| | 303 | * their auto-draft status. When a changeset post is published, |
| | 304 | * however, it would no longer get cleaned out. Ths is a problem |
| | 305 | * when the changeset posts are never displayed anywhere, since they |
| | 306 | * would just be endlessly piling up. So here we use the revisions |
| | 307 | * feature to indicate whether or not a published changeset should |
| | 308 | * get trashed and thus garbage collected. |
| | 309 | */ |
| | 310 | if ( ! wp_revisions_enabled( get_post( $this->post_id ) ) ) { |
| | 311 | $this->trash(); |
| | 312 | } |
| | 313 | } |
| | 314 | |
| | 315 | return $result; |
| | 316 | } |
| | 317 | |
| | 318 | /** |
| | 319 | * Trash or delete the changeset post. |
| | 320 | * |
| | 321 | * The following re-formulates the logic from wp_trash_post() as done in |
| | 322 | * wp_publish_post(). The reason for bypassing wp_trash_post() is that it |
| | 323 | * will mutate the the post_content and the post_name when they should be |
| | 324 | * untouched. |
| | 325 | * |
| | 326 | * @global wpdb $wpdb WordPress database abstraction object. |
| | 327 | * |
| | 328 | * @return mixed The trashed post as an array or an empty value on failure. |
| | 329 | */ |
| | 330 | public function trash() { |
| | 331 | global $wpdb; |
| | 332 | |
| | 333 | if ( ! EMPTY_TRASH_DAYS ) { |
| | 334 | return wp_delete_post( (int) $this->post_id, true ); |
| | 335 | } |
| | 336 | |
| | 337 | if ( $this->post_id ) { |
| | 338 | $post = get_post( $this->post_id ); |
| | 339 | } |
| | 340 | |
| | 341 | |
| | 342 | if ( empty( $post ) ) { |
| | 343 | return $post; |
| | 344 | } |
| | 345 | |
| | 346 | if ( get_post_status( $post ) === 'trash' ) { |
| | 347 | return false; |
| | 348 | } |
| | 349 | |
| | 350 | $post_id = $post->ID; |
| | 351 | |
| | 352 | /** This action is documented in wp-includes/post.php */ |
| | 353 | do_action( 'wp_trash_post', $post_id ); |
| | 354 | |
| | 355 | add_post_meta( $post_id, '_wp_trash_meta_status', $post->post_status ); |
| | 356 | add_post_meta( $post_id, '_wp_trash_meta_time', time() ); |
| | 357 | |
| | 358 | $old_status = $post->post_status; |
| | 359 | $new_status = 'trash'; |
| | 360 | $wpdb->update( $wpdb->posts, array( 'post_status' => $new_status ), array( 'ID' => $post->ID ) ); |
| | 361 | clean_post_cache( $post->ID ); |
| | 362 | |
| | 363 | $post->post_status = $new_status; |
| | 364 | wp_transition_post_status( $new_status, $old_status, $post ); |
| | 365 | |
| | 366 | /** This action is documented in wp-includes/post.php */ |
| | 367 | do_action( 'edit_post', $post->ID, $post ); |
| | 368 | |
| | 369 | /** This action is documented in wp-includes/post.php */ |
| | 370 | do_action( "save_post_{$post->post_type}", $post->ID, $post, true ); |
| | 371 | |
| | 372 | /** This action is documented in wp-includes/post.php */ |
| | 373 | do_action( 'save_post', $post->ID, $post, true ); |
| | 374 | |
| | 375 | /** This action is documented in wp-includes/post.php */ |
| | 376 | do_action( 'wp_insert_post', $post->ID, $post, true ); |
| | 377 | |
| | 378 | wp_trash_post_comments( $post_id ); |
| | 379 | |
| | 380 | /** This action is documented in wp-includes/post.php */ |
| | 381 | do_action( 'trashed_post', $post_id ); |
| | 382 | |
| | 383 | return $post->to_array(); |
| | 384 | } |
| | 385 | } |