:`. The * connector stores the op key, and the write's result, with what the write * created: the revision for a content change, or the post itself when the * write created no revision (an SEO-only edit, for example). A request with * an op key the connector already applied answers with the first result * instead of applying again, so Blaze can always retry a request whose * answer it lost. WordPress saves synchronously, so an op key the connector * doesn't know means the write never happened. * * Op keys live in post meta, never in options. When WordPress deletes an old * revision that carries an op key, the op key moves to the revision's post, so * it is never forgotten. */ final class OpKeys { const META_KEY = '_searchsmith_op_key'; const RESULT_PREFIX = '_searchsmith_result_'; /** * An op key: a change id, then up to four `:`-separated parts. */ const PATTERN = '^[A-Za-z0-9_-]{1,64}(:[A-Za-z0-9_-]{1,32}){0,4}$'; /** * Adds the hook. */ public static function register() { add_action( 'before_delete_post', array( self::class, 'keep_on_parent' ), 10, 2 ); } /** * The REST schema of a `op_key` argument. * * @param bool $required Whether the route requires it. * @return array */ public static function schema( $required ) { return array( 'type' => 'string', 'required' => $required, 'pattern' => self::PATTERN, ); } /** * The change id an op key begins with. * * @param string $op_key Op key. * @return string */ public static function change_id_of( $op_key ) { $parts = explode( ':', (string) $op_key, 2 ); return $parts[0]; } /** * What the connector stored for an op key, or null when no write with it * was applied. * * @param string $op_key Op key. * @return array{object_id: int, kind: string, post_id: int, result: array}|null */ public static function find( $op_key ) { $ids = get_posts( array( 'post_type' => array_values( get_post_types() ), 'post_status' => array_keys( get_post_stati() ), 'meta_key' => self::META_KEY, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Op keys are looked up only for writes and recovery. 'meta_value' => (string) $op_key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value 'fields' => 'ids', 'posts_per_page' => 1, 'orderby' => 'ID', 'order' => 'ASC', 'no_found_rows' => true, ) ); if ( ! $ids ) { return null; } $object_id = (int) $ids[0]; $record = get_metadata( 'post', $object_id, self::RESULT_PREFIX . $op_key, true ); if ( ! is_array( $record ) ) { $record = array(); } return array( 'object_id' => $object_id, 'kind' => isset( $record['kind'] ) ? (string) $record['kind'] : '', 'post_id' => isset( $record['post_id'] ) ? (int) $record['post_id'] : 0, 'result' => isset( $record['result'] ) && is_array( $record['result'] ) ? $record['result'] : array(), ); } /** * The answer to a request whose op key was already applied, or null when * the op key is new and the request should go ahead. * * Called before any other check, including the `modified_gmt` guard, so a * repeated request is never refused as stale. * * @param string|null $op_key The request's op key. * @param string $kind What the request does, such as `operations`. * @param int $post_id The post the request targets, or 0. * @return array|\WP_Error|null The first result, a refusal, or null. */ public static function replay( $op_key, $kind, $post_id ) { if ( null === $op_key || '' === $op_key ) { return null; } $found = self::find( $op_key ); if ( null === $found ) { return null; } if ( $found['kind'] !== $kind || ( $post_id && $found['post_id'] !== (int) $post_id ) ) { return new \WP_Error( 'searchsmith_op_key_reused', __( 'This op key was already used for a different write.', 'searchsmith' ), array( 'status' => 409, 'kind' => $found['kind'], 'post_id' => $found['post_id'], ) ); } return array_merge( $found['result'], array( 'replayed' => true ) ); } /** * Stores an op key with what a write created. * * @param string|null $op_key The request's op key; nothing is stored without one. * @param string $kind What the request did. * @param int $post_id The post it targeted. * @param int $object_id What it created: a revision, a post or a redirect. * @param array $result The answer it gave. */ public static function record( $op_key, $kind, $post_id, $object_id, array $result ) { if ( null === $op_key || '' === $op_key ) { return; } $record = array( 'kind' => (string) $kind, 'post_id' => (int) $post_id, 'result' => $result, ); // add_metadata() rather than add_post_meta(), which would move meta // meant for a revision onto its post. Meta functions unslash values. add_metadata( 'post', $object_id, self::META_KEY, $op_key ); add_metadata( 'post', $object_id, self::RESULT_PREFIX . $op_key, wp_slash( $record ), true ); } /** * Moves a deleted revision's op keys to its post, so an op key outlives the * revision when WordPress prunes old revisions. * * @param int $post_id The post being deleted. * @param \WP_Post $post The post being deleted. */ public static function keep_on_parent( $post_id, $post ) { if ( ! $post instanceof \WP_Post || 'revision' !== $post->post_type || ! $post->post_parent ) { return; } foreach ( (array) get_metadata( 'post', $post_id, self::META_KEY, false ) as $op_key ) { $record = get_metadata( 'post', $post_id, self::RESULT_PREFIX . $op_key, true ); add_metadata( 'post', $post->post_parent, self::META_KEY, $op_key ); add_metadata( 'post', $post->post_parent, self::RESULT_PREFIX . $op_key, wp_slash( $record ), true ); } } }