get_elementor_data( $object_id ); if ( empty( $data ) ) { return array(); } $urls = array(); $this->extract_urls_from_elements( $data, $urls ); return array_unique( $urls ); } /** * Recursively extract URLs from an array of Elementor elements. * * @param array $elements Element array. * @param array $urls Collected URLs (by reference). * @return void */ private function extract_urls_from_elements( $elements, &$urls ) { if ( ! is_array( $elements ) ) { return; } foreach ( $elements as $element ) { if ( isset( $element['settings'] ) && is_array( $element['settings'] ) ) { $this->extract_urls_from_settings( $element['settings'], $urls ); } if ( ! empty( $element['elements'] ) ) { $this->extract_urls_from_elements( $element['elements'], $urls ); } } } /** * Extract URLs from a single element's settings array. * * @param array $settings Settings array. * @param array $urls Collected URLs (by reference). * @return void */ private function extract_urls_from_settings( $settings, &$urls ) { foreach ( $this->url_keys as $key ) { if ( isset( $settings[ $key ] ) && is_string( $settings[ $key ] ) && '' !== $settings[ $key ] ) { $urls[] = $settings[ $key ]; } } foreach ( $this->nested_url_keys as $key ) { if ( isset( $settings[ $key ] ) && is_array( $settings[ $key ] ) && isset( $settings[ $key ]['url'] ) ) { $urls[] = $settings[ $key ]['url']; } } } /** * Replace URLs in the post's Elementor data according to the mapping. * * @param string $object_type 'post'. * @param int $object_id Post ID. * @param array $mapping [ old_url => new_url ]. * @return string|null New JSON string, or null if unchanged / not applicable. */ public function replace_urls( $object_type, $object_id, array $mapping ) { $data = $this->get_elementor_data( $object_id ); if ( empty( $data ) ) { return null; } $changed = $this->replace_in_elements( $data, $mapping ); if ( ! $changed ) { return null; } // Re-encode with the same flags Elementor uses. $new_json = wp_json_encode( $data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); return $new_json; } /** * Recursively replace URLs in an array of Elementor elements. * * @param array $elements Element array (by reference). * @param array $mapping [ old_url => new_url ]. * @return bool True if anything changed. */ private function replace_in_elements( &$elements, array $mapping ) { if ( ! is_array( $elements ) ) { return false; } $changed = false; foreach ( $elements as &$element ) { if ( isset( $element['settings'] ) && is_array( $element['settings'] ) ) { if ( $this->replace_in_settings( $element['settings'], $mapping ) ) { $changed = true; } } if ( ! empty( $element['elements'] ) ) { if ( $this->replace_in_elements( $element['elements'], $mapping ) ) { $changed = true; } } } unset( $element ); return $changed; } /** * Replace URLs in a single element's settings array. * * @param array $settings Settings array (by reference). * @param array $mapping [ old_url => new_url ]. * @return bool True if anything changed. */ private function replace_in_settings( &$settings, array $mapping ) { $changed = false; foreach ( $this->url_keys as $key ) { if ( isset( $settings[ $key ] ) && is_string( $settings[ $key ] ) && '' !== $settings[ $key ] ) { $new_val = $this->apply_mapping( $settings[ $key ], $mapping ); if ( $new_val !== $settings[ $key ] ) { $settings[ $key ] = $new_val; $changed = true; } } } foreach ( $this->nested_url_keys as $key ) { if ( isset( $settings[ $key ] ) && is_array( $settings[ $key ] ) && isset( $settings[ $key ]['url'] ) ) { $old_url = $settings[ $key ]['url']; $new_url = $this->apply_mapping( $old_url, $mapping ); if ( $new_url !== $old_url ) { $settings[ $key ]['url'] = $new_url; $changed = true; } } } return $changed; } /** * Apply the URL mapping to a single URL value. * * Tries exact match first, then path-only match. * * @param string $url URL. * @param array $mapping [ old_url => new_url ]. * @return string */ private function apply_mapping( $url, array $mapping ) { if ( isset( $mapping[ $url ] ) ) { return $mapping[ $url ]; } $url_path = wp_parse_url( $url, PHP_URL_PATH ); if ( ! $url_path ) { return $url; } foreach ( $mapping as $old => $new ) { $old_path = wp_parse_url( $old, PHP_URL_PATH ); if ( $old_path && $old_path === $url_path ) { $scheme = wp_parse_url( $url, PHP_URL_SCHEME ); $host = wp_parse_url( $url, PHP_URL_HOST ); $new_path = wp_parse_url( $new, PHP_URL_PATH ); if ( $scheme && $host && $new_path ) { return $scheme . '://' . $host . $new_path; } return $new; } } return $url; } /** * Get the decoded _elementor_data for a post. * * @param int $post_id Post ID. * @return array|null Decoded data or null. */ private function get_elementor_data( $post_id ) { $raw = get_post_meta( $post_id, '_elementor_data', true ); if ( empty( $raw ) ) { return null; } $data = json_decode( $raw, true ); if ( ! is_array( $data ) ) { return null; } return $data; } /** * Persist the new Elementor JSON to _elementor_data. * * Uses update_post_meta (Elementor reads _elementor_data on render, so * this is the correct storage path). Clears the post cache afterwards. * * @param string $object_type 'post'. * @param int $object_id Post ID. * @param string $new_content New JSON string. * @return bool True on success. */ public function persist( $object_type, $object_id, $new_content ) { // update_post_meta returns meta_id on insert / true on update; false on no change. $result = update_post_meta( (int) $object_id, '_elementor_data', $new_content ); // Clear Elementor's internal CSS cache for this post. delete_post_meta( (int) $object_id, '_elementor_css' ); clean_post_cache( $object_id ); return false !== $result; } }