get_scannable_meta( $object_id ); $urls = array(); foreach ( $meta as $key => $values ) { foreach ( $values as $value ) { $this->extract_urls_from_value( maybe_unserialize( $value ), $urls ); } } return array_unique( $urls ); } /** * Recursively extract URL-like strings from a value. * * @param mixed $value Value (string, array, or other). * @param array $urls Collected URLs (by reference). * @return void */ private function extract_urls_from_value( $value, &$urls ) { if ( is_string( $value ) ) { // Extract anything that looks like a URL (http/https or relative path). if ( preg_match_all( '#https?://[^\s"\'<>]+#i', $value, $matches ) ) { foreach ( $matches[0] as $url ) { $urls[] = $url; } } return; } if ( is_array( $value ) ) { foreach ( $value as $v ) { $this->extract_urls_from_value( $v, $urls ); } } } /** * Replace URLs in the post's postmeta according to the mapping. * * Returns a JSON-encoded summary of changes (meta_key => [old, new]) so the * rewriter can back it up. The actual DB writes happen in persist(). * * @param string $object_type 'post'. * @param int $object_id Post ID. * @param array $mapping [ old_url => new_url ]. * @return string|null JSON summary of changes, or null if unchanged. */ public function replace_urls( $object_type, $object_id, array $mapping ) { $meta = $this->get_scannable_meta( $object_id ); $changes = array(); foreach ( $meta as $key => $values ) { $new_values = array(); $key_changed = false; foreach ( $values as $value ) { $unserialized = maybe_unserialize( $value ); $new_value = $this->replace_in_value( $unserialized, $mapping ); if ( $new_value !== $unserialized ) { $key_changed = true; } $new_values[] = $new_value; } if ( $key_changed ) { // Store unserialized values โ€” add_post_meta() in persist() // will serialize once via maybe_serialize(). Pre-serializing // here would cause double-serialization (Bug #6). $old_unserialized = array(); foreach ( $values as $idx => $orig ) { $old_unserialized[] = maybe_unserialize( $orig ); } $changes[] = array( 'key' => $key, 'old' => $old_unserialized, 'new' => $new_values, ); } } if ( empty( $changes ) ) { return null; } return wp_json_encode( $changes ); } /** * Recursively replace URLs in a value (string or array). * * @param mixed $value Value. * @param array $mapping [ old_url => new_url ]. * @return mixed Transformed value. */ private function replace_in_value( $value, array $mapping ) { if ( is_string( $value ) ) { return $this->apply_mapping_to_string( $value, $mapping ); } if ( is_array( $value ) ) { $result = array(); foreach ( $value as $k => $v ) { $result[ $k ] = $this->replace_in_value( $v, $mapping ); } return $result; } return $value; } /** * Apply the URL mapping to a string value. * * Replaces both exact URL matches and path-only matches within the string. * * @param string $str String. * @param array $mapping [ old_url => new_url ]. * @return string */ private function apply_mapping_to_string( $str, array $mapping ) { if ( '' === $str ) { return $str; } foreach ( $mapping as $old => $new ) { // Exact match. if ( $str === $old ) { return $new; } // Substring replacement (URL embedded in a longer string). if ( false !== strpos( $str, $old ) ) { $str = str_replace( $old, $new, $str ); } // Path-only match: if the string is a URL whose path matches an // old mapping key's path, replace the path portion. $str_path = wp_parse_url( $str, PHP_URL_PATH ); $old_path = wp_parse_url( $old, PHP_URL_PATH ); if ( $str_path && $old_path && $str_path === $old_path && $str !== $old ) { $scheme = wp_parse_url( $str, PHP_URL_SCHEME ); $host = wp_parse_url( $str, PHP_URL_HOST ); $new_path = wp_parse_url( $new, PHP_URL_PATH ); if ( $scheme && $host && $new_path ) { $str = $scheme . '://' . $host . $new_path; } } } return $str; } /** * Persist the postmeta changes. * * The $new_content is the JSON summary produced by replace_urls(). We * decode it and apply each change via update_post_meta. * * @param string $object_type 'post'. * @param int $object_id Post ID. * @param string $new_content JSON summary of changes. * @return bool True on success. */ public function persist( $object_type, $object_id, $new_content ) { $changes = json_decode( $new_content, true ); if ( ! is_array( $changes ) ) { return false; } foreach ( $changes as $change ) { $key = isset( $change['key'] ) ? $change['key'] : ''; $new = isset( $change['new'] ) ? $change['new'] : array(); if ( '' === $key || ! is_array( $new ) ) { continue; } // Delete existing values for this key, then re-insert. delete_post_meta( $object_id, $key ); foreach ( $new as $value ) { // Values are unserialized; add_post_meta() serializes once. add_post_meta( $object_id, $key, $value ); } } clean_post_cache( $object_id ); return true; } /** * Get the postmeta entries that should be scanned for this post. * * Filters out excluded keys and, if the `ailm_url_migrator_meta_keys` * option is set, restricts to only those keys. * * @param int $post_id Post ID. * @return array Associative array: [ meta_key => [ values ] ]. */ private function get_scannable_meta( $post_id ) { $all_meta = get_post_meta( $post_id ); if ( empty( $all_meta ) ) { return array(); } // Optional whitelist of meta keys. $whitelist = get_option( 'ailm_url_migrator_meta_keys', array() ); $whitelist = is_array( $whitelist ) ? $whitelist : array(); $result = array(); foreach ( $all_meta as $key => $values ) { // Skip excluded keys. if ( in_array( $key, $this->excluded_keys, true ) ) { continue; } // If a whitelist is set, only scan listed keys. if ( ! empty( $whitelist ) && ! in_array( $key, $whitelist, true ) ) { continue; } $result[ $key ] = $values; } return $result; } }