'post_title', 'content' => 'post_content', 'excerpt' => 'post_excerpt', ); /** * Applies an edit. * * @param \WP_Post $post The post to change. * @param array $request { * The edit. * * @type string $expected_modified_gmt The post's `modified_gmt` when Blaze read it. * @type string $change_id Blaze's id for this change. * @type string $title Optional new title. * @type string $content Optional new content. * @type string $excerpt Optional new excerpt. * @type array $seo Optional SEO fields; see Fields. * } * @return array|\WP_Error The result, or why the edit was refused. */ public static function edit( \WP_Post $post, array $request ) { $refusal = self::refusal( $post, $request ); if ( $refusal ) { return $refusal; } $seo = isset( $request['seo'] ) ? Fields::sanitize( (array) $request['seo'] ) : array(); $store = Stores::current(); $update = array( 'ID' => $post->ID ); foreach ( self::POST_FIELDS as $field => $column ) { if ( array_key_exists( $field, $request ) ) { $update[ $column ] = (string) $request[ $field ]; } } list( $result, $revision_ids ) = RevisionLabeler::during( (string) $request['change_id'], static function () use ( $post, $seo, $store, $update ) { // SEO fields first: saving the post afterwards lets SEO plugins // that cache derived data (Yoast's indexables) rebuild it. if ( $seo && $store ) { $store->write( $post->ID, $seo ); } // wp_update_post() unslashes its input, so slash it first. // Saving even when only SEO fields changed bumps modified_gmt, // which keeps Blaze's next version check meaningful. return wp_update_post( wp_slash( $update ), true ); } ); if ( is_wp_error( $result ) ) { return $result; } CachePurger::purge_post( $post->ID ); $saved = get_post( $post->ID ); return array( 'id' => $saved->ID, 'modified_gmt' => self::modified( $saved ), 'change_id' => (string) $request['change_id'], 'revision_ids' => $revision_ids, 'seo' => $store ? $store->read( $saved->ID ) : null, ); } /** * The post's last modification time in GMT, in the database's format. * * WordPress leaves `post_modified_gmt` empty (all zeros) on a draft that * was never saved again after it was created, while `post_modified` * holds the local time. The local time converted to GMT stands in then, * so a new draft has a real version to guard writes with. * * @param \WP_Post $post The post. * @return string */ public static function modified_mysql( \WP_Post $post ) { if ( '0000-00-00 00:00:00' !== $post->post_modified_gmt ) { return $post->post_modified_gmt; } return get_gmt_from_date( $post->post_modified ); } /** * The post's last modification time in GMT, as the REST API shows it. * * @param \WP_Post $post The post. * @return string */ public static function modified( \WP_Post $post ) { return self::rfc3339( self::modified_mysql( $post ) ); } /** * Formats a GMT database date the way the core REST API does. * * @param string $mysql_date Date in `Y-m-d H:i:s`. * @return string */ public static function rfc3339( $mysql_date ) { return mysql_to_rfc3339( $mysql_date ); } /** * Why the edit must not happen, or null when it may. * * @param \WP_Post $post The post. * @param array $request The edit. * @return \WP_Error|null */ private static function refusal( \WP_Post $post, array $request ) { $refusal = Guard::refusal( $post, isset( $request['expected_modified_gmt'] ) ? (string) $request['expected_modified_gmt'] : '' ); if ( $refusal ) { return $refusal; } if ( array_key_exists( 'content', $request ) ) { $refusal = Guard::builder_refusal( $post ); if ( $refusal ) { return $refusal; } } if ( ! empty( $request['seo'] ) && null === Stores::current() ) { return new \WP_Error( 'searchsmith_seo_unsupported', __( 'The active SEO plugin is not supported yet.', 'searchsmith' ), array( 'status' => 422, 'seo_plugin' => Environment::seo_plugin(), ) ); } return null; } }