the tag ids it held before this save, to put back once * WordPress has applied the form's stale tag list. * * @var array> */ private static $tagsToKeep = array(); /** * Register the guard's hooks. * * @return void */ public static function init(): void { add_action( 'edit_form_top', array( __CLASS__, 'renderFingerprints' ) ); add_filter( 'wp_insert_post_data', array( __CLASS__, 'keepNewerText' ), 99, 2 ); add_action( 'pre_post_update', array( __CLASS__, 'rememberNewerTags' ), 10, 1 ); add_action( 'wp_after_insert_post', array( __CLASS__, 'keepNewerTags' ), 10, 1 ); } /** * `edit_form_top`: print the load-time fingerprints into the product form. * * @param mixed $post The post being edited. * @return void */ public static function renderFingerprints( $post ): void { if ( ! $post instanceof WP_Post || 'product' !== $post->post_type ) { return; } $post_id = (int) $post->ID; $prints = array( 'post_id' => (string) $post_id ); foreach ( self::TEXT_FIELDS as $field ) { $prints[ $field ] = self::fingerprint( (string) get_post_field( $field, $post_id, 'raw' ) ); } $prints[ self::TAGS_KEY ] = self::tagFingerprint( self::storedTagNames( $post_id ) ); foreach ( $prints as $key => $value ) { printf( '', esc_attr( self::FIELD ), esc_attr( $key ), esc_attr( $value ) ); } wp_nonce_field( self::NONCE_ACTION, self::NONCE_NAME, false ); } /** * `wp_insert_post_data`: keep the stored title, description and short * description when the form left them untouched and they changed after the * form was opened. * * @param mixed $data Slashed post data about to be written. * @param mixed $postarr Slashed request data. * @return mixed */ public static function keepNewerText( $data, $postarr ) { if ( ! is_array( $data ) || ! is_array( $postarr ) || 'product' !== ( $data['post_type'] ?? '' ) ) { return $data; } $post_id = isset( $postarr['ID'] ) ? (int) $postarr['ID'] : 0; $prints = self::postedFingerprints( $post_id ); if ( null === $prints ) { return $data; } $stored = self::storedText( $post_id ); if ( null === $stored ) { return $data; } foreach ( self::TEXT_FIELDS as $field ) { if ( ! isset( $data[ $field ], $prints[ $field ], $stored[ $field ] ) ) { continue; } $untouched = self::fingerprint( (string) wp_unslash( $data[ $field ] ) ) === $prints[ $field ]; $changed_since = self::fingerprint( $stored[ $field ] ) !== $prints[ $field ]; if ( $untouched && $changed_since ) { $data[ $field ] = wp_slash( $stored[ $field ] ); } } return $data; } /** * `pre_post_update`: before WordPress applies the form's tag list, note * the stored tags when the form left its list untouched and the tags * changed after the form was opened. * * @param mixed $postId Post id. * @return void */ public static function rememberNewerTags( $postId ): void { $post_id = (int) $postId; unset( self::$tagsToKeep[ $post_id ] ); if ( $post_id <= 0 || 'product' !== get_post_type( $post_id ) ) { return; } $prints = self::postedFingerprints( $post_id ); if ( null === $prints || ! isset( $prints[ self::TAGS_KEY ] ) ) { return; } $posted = self::postedTagNames(); if ( null === $posted || self::tagFingerprint( $posted ) !== $prints[ self::TAGS_KEY ] ) { return; } if ( self::tagFingerprint( self::storedTagNames( $post_id ) ) === $prints[ self::TAGS_KEY ] ) { return; } $ids = wp_get_object_terms( $post_id, self::TAG_TAXONOMY, array( 'fields' => 'ids' ) ); if ( is_array( $ids ) ) { self::$tagsToKeep[ $post_id ] = array_map( 'intval', $ids ); } } /** * `wp_after_insert_post`: put back the tags {@see rememberNewerTags()} kept. * * @param mixed $postId Post id. * @return void */ public static function keepNewerTags( $postId ): void { $post_id = (int) $postId; if ( ! isset( self::$tagsToKeep[ $post_id ] ) ) { return; } $ids = self::$tagsToKeep[ $post_id ]; unset( self::$tagsToKeep[ $post_id ] ); wp_set_object_terms( $post_id, $ids, self::TAG_TAXONOMY ); } /** * The fingerprints this request's form carried for $postId, or null when * it carried none (any other save: REST, Quick Edit, autosave, a plugin's * own write) or they do not belong to this product. * * @param int $postId Post being saved. * @return array|null */ private static function postedFingerprints( int $postId ): ?array { if ( $postId <= 0 || ! isset( $_POST[ self::FIELD ], $_POST[ self::NONCE_NAME ] ) || ! is_array( $_POST[ self::FIELD ] ) ) { return null; } if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ self::NONCE_NAME ] ) ), self::NONCE_ACTION ) ) { return null; } $raw = wp_unslash( $_POST[ self::FIELD ] ); $prints = array(); foreach ( array_merge( array( 'post_id', self::TAGS_KEY ), self::TEXT_FIELDS ) as $key ) { if ( isset( $raw[ $key ] ) && is_string( $raw[ $key ] ) ) { $prints[ $key ] = sanitize_key( $raw[ $key ] ); } } if ( (int) ( $prints['post_id'] ?? 0 ) !== $postId ) { return null; } return $prints; } /** * The tag names the form posted, or null when the form carried no tag box. * * @return array|null */ private static function postedTagNames(): ?array { if ( ! isset( $_POST['tax_input'] ) || ! is_array( $_POST['tax_input'] ) || ! isset( $_POST['tax_input'][ self::TAG_TAXONOMY ] ) ) { return null; } $raw = wp_unslash( $_POST['tax_input'][ self::TAG_TAXONOMY ] ); if ( is_array( $raw ) ) { return array_map( 'strval', $raw ); } $names = preg_split( '/[,\x{060C}\x{FF0C}\x{3001}]/u', (string) $raw ); return is_array( $names ) ? $names : array(); } /** * @param int $postId Product id. * @return array */ private static function storedTagNames( int $postId ): array { $names = wp_get_object_terms( $postId, self::TAG_TAXONOMY, array( 'fields' => 'names' ) ); return is_array( $names ) ? array_map( 'strval', $names ) : array(); } /** * The product's stored text, read past every cache. * * @param int $postId Product id. * @return array|null */ private static function storedText( int $postId ): ?array { global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( "SELECT post_title, post_content, post_excerpt FROM {$wpdb->posts} WHERE ID = %d", $postId ), ARRAY_A ); return is_array( $row ) ? array_map( 'strval', $row ) : null; } /** * A text field's fingerprint: its visible text, with markup, entities and * runs of whitespace (including no-break spaces) reduced away. * * @param string $value Field value. * @return string */ private static function fingerprint( string $value ): string { $text = html_entity_decode( wp_strip_all_tags( $value ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ); $text = str_replace( "\xC2\xA0", ' ', $text ); $text = (string) preg_replace( '/\s+/u', ' ', $text ); return md5( trim( $text ) ); } /** * A tag list's fingerprint: order, case and blanks do not matter. * * @param array $names Tag names. * @return string */ private static function tagFingerprint( array $names ): string { $normalized = array(); foreach ( $names as $name ) { $name = trim( html_entity_decode( (string) $name, ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ); if ( '' !== $name ) { $normalized[] = function_exists( 'mb_strtolower' ) ? mb_strtolower( $name, 'UTF-8' ) : strtolower( $name ); } } $normalized = array_values( array_unique( $normalized ) ); sort( $normalized ); return md5( implode( "\n", $normalized ) ); } }