get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->postmeta} WHERE meta_key = %s", self::META_KEY ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- read-only query against another plugin's own custom table (import dry-run / foreign-plugin conflict detection) — not our schema, WP_Query/get_posts can't reach it. / one-shot read during import/conflict-detection, not a repeated query in a request loop — caching would serve stale data across saves. if ( $post_rows > 0 ) { return true; } return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->termmeta} WHERE meta_key = %s", self::META_KEY ) ) > 0; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- read-only query against another plugin's own custom table (import dry-run / foreign-plugin conflict detection) — not our schema, WP_Query/get_posts can't reach it. / one-shot read during import/conflict-detection, not a repeated query in a request loop — caching would serve stale data across saves. } protected function run( bool $dry, bool $overwrite ): ImportReport { global $wpdb; $report = new ImportReport(); $post_ids = array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s", self::META_KEY ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- read-only query against another plugin's own custom table (import dry-run / foreign-plugin conflict detection) — not our schema, WP_Query/get_posts can't reach it. / one-shot read during import/conflict-detection, not a repeated query in a request loop — caching would serve stale data across saves. foreach ( $post_ids as $post_id ) { $data = get_post_meta( $post_id, self::META_KEY, true ); if ( ! is_array( $data ) ) { continue; } ++$report->posts; $this->import_post_field( $report, $dry, $overwrite, $post_id, MetaFields::TITLE, $this->convert_variables( (string) ( $data['title'] ?? '' ) ) ); $this->import_post_field( $report, $dry, $overwrite, $post_id, MetaFields::DESCRIPTION, $this->convert_variables( (string) ( $data['description'] ?? '' ) ) ); $this->import_post_field( $report, $dry, $overwrite, $post_id, MetaFields::CANONICAL, (string) ( $data['canonical'] ?? '' ) ); if ( ! empty( $data['noindex'] ) ) { $this->import_post_flag( $report, $dry, $overwrite, $post_id, MetaFields::ROBOTS_INDEX, false ); } $this->import_post_og_image( $report, $dry, $overwrite, $post_id, $this->attachment_id_from_url( (string) ( $data['facebook_image'] ?? '' ) ) ); } $term_ids = array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT term_id FROM {$wpdb->termmeta} WHERE meta_key = %s", self::META_KEY ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- read-only query against another plugin's own custom table (import dry-run / foreign-plugin conflict detection) — not our schema, WP_Query/get_posts can't reach it. / one-shot read during import/conflict-detection, not a repeated query in a request loop — caching would serve stale data across saves. foreach ( $term_ids as $term_id ) { $data = get_term_meta( $term_id, self::META_KEY, true ); if ( ! is_array( $data ) ) { continue; } ++$report->terms; $this->import_term_field( $report, $dry, $overwrite, $term_id, TermMetaFields::TITLE, $this->convert_variables( (string) ( $data['title'] ?? '' ) ) ); $this->import_term_field( $report, $dry, $overwrite, $term_id, TermMetaFields::DESCRIPTION, $this->convert_variables( (string) ( $data['description'] ?? '' ) ) ); } return $report; } /** * Slim SEO manual fields may contain its `{{ tag }}` variables — convert * the common ones to our `%variable%` syntax; unknown tags are left * as-is (visible, so the user can fix them consciously). */ private function convert_variables( string $value ): string { return str_replace( array( '{{ post.title }}', '{{ site.title }}', '{{ post.excerpt }}', '{{ term.name }}' ), array( '%post_title%', '%site_name%', '%post_excerpt%', '%term_title%' ), $value ); } }