'page', 'post_status' => array( 'publish', 'draft', 'pending', 'future', 'private' ), 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => self::META_MANAGED, 'value' => '1', ), ), 'orderby' => 'title', 'order' => 'ASC', ); $query = new WP_Query( $args ); $mapped = array(); if ( $query->have_posts() ) { foreach ( $query->posts as $post ) { $source_url = get_post_meta( $post->ID, self::META_SOURCE_URL, true ); $source_type = get_post_meta( $post->ID, self::META_SOURCE_TYPE, true ); $last_indexed = get_post_meta( $post->ID, self::META_LAST_INDEXED, true ); $last_status = get_post_meta( $post->ID, self::META_LAST_STATUS, true ); $last_error = get_post_meta( $post->ID, self::META_LAST_ERROR, true ); $mapped[] = array( 'id' => $post->ID, 'title' => $post->post_title, 'slug' => $post->post_name, 'url' => get_permalink( $post->ID ), 'source_url' => $source_url, 'source_type' => $source_type ? strtoupper( $source_type ) : 'HTML', 'last_indexed' => $last_indexed ? date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $last_indexed ) : __( 'Never', 'crowbridge' ), 'status' => $last_status ? $last_status : 'success', 'error' => $last_error, ); } } return $mapped; } /** * Returns published pages not yet mapped to CrowBridge. * * @return array */ public function get_unmapped_pages() { $args = array( 'post_type' => 'page', 'post_status' => 'publish', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => self::META_MANAGED, 'compare' => 'NOT EXISTS', ), ), 'orderby' => 'title', 'order' => 'ASC', ); $query = new WP_Query( $args ); $unmapped = array(); if ( $query->have_posts() ) { foreach ( $query->posts as $post ) { $unmapped[] = array( 'id' => $post->ID, 'title' => ! empty( $post->post_title ) ? $post->post_title : sprintf( /* translators: %d: post ID */ __( '(no title #%d)', 'crowbridge' ), $post->ID ), ); } } return $unmapped; } /** * Maps a WordPress Page to a static source and indexes it. * * @param int $post_id * @param string $source * @return true|WP_Error */ public function map_page( $post_id, $source ) { $post_id = absint( $post_id ); $post = get_post( $post_id ); if ( ! $post || 'page' !== $post->post_type ) { return new WP_Error( 'invalid_page', __( 'The selected WordPress Page does not exist.', 'crowbridge' ) ); } // Check if already mapped. if ( '1' === get_post_meta( $post_id, self::META_MANAGED, true ) ) { return new WP_Error( 'already_mapped', __( 'This Page is already mapped to a source.', 'crowbridge' ) ); } // Perform initial fetch and index. $result = $this->sync_content( $post_id, $source ); if ( is_wp_error( $result ) ) { return $result; } return true; } /** * Re-indexes a single mapped page. * * @param int $post_id * @return true|WP_Error */ public function reindex_page( $post_id ) { $post_id = absint( $post_id ); $source = get_post_meta( $post_id, self::META_SOURCE_URL, true ); if ( empty( $source ) ) { return new WP_Error( 'no_source', __( 'This page has no registered source URL.', 'crowbridge' ) ); } return $this->sync_content( $post_id, $source ); } /** * Re-indexes all mapped pages. * * @return array Summary of success and failed counts. */ public function reindex_all() { $mapped = $this->get_mapped_pages(); $success = 0; $failed = 0; $errors = array(); foreach ( $mapped as $item ) { $res = $this->reindex_page( $item['id'] ); if ( is_wp_error( $res ) ) { $failed++; $errors[] = sprintf( '%s: %s', $item['title'], $res->get_error_message() ); } else { $success++; } } return array( 'success' => $success, 'failed' => $failed, 'errors' => $errors, ); } /** * Removes the mapping from a WordPress page. * * @param int $post_id * @return bool */ public function unlink_page( $post_id ) { $post_id = absint( $post_id ); delete_post_meta( $post_id, self::META_MANAGED ); delete_post_meta( $post_id, self::META_SOURCE_URL ); delete_post_meta( $post_id, self::META_SOURCE_TYPE ); delete_post_meta( $post_id, self::META_LAST_INDEXED ); delete_post_meta( $post_id, self::META_LAST_STATUS ); delete_post_meta( $post_id, self::META_LAST_ERROR ); return true; } /** * Fetches, parses, and synchronizes static content with the WordPress Page. * * @param int $post_id * @param string $source * @return true|WP_Error */ private function sync_content( $post_id, $source ) { $fetched = Fetcher::fetch( $source ); if ( is_wp_error( $fetched ) ) { // Per Question 15: Preserve existing content on failure and record error. update_post_meta( $post_id, self::META_LAST_STATUS, 'error' ); update_post_meta( $post_id, self::META_LAST_ERROR, $fetched->get_error_message() ); update_post_meta( $post_id, self::META_LAST_INDEXED, current_time( 'timestamp' ) ); return $fetched; } $parsed = Parser::parse( $fetched['content'], $fetched['type'] ); if ( is_wp_error( $parsed ) ) { update_post_meta( $post_id, self::META_LAST_STATUS, 'error' ); update_post_meta( $post_id, self::META_LAST_ERROR, $parsed->get_error_message() ); update_post_meta( $post_id, self::META_LAST_INDEXED, current_time( 'timestamp' ) ); return $parsed; } $current_post = get_post( $post_id ); // Prepare updated post fields. $post_data = array( 'ID' => $post_id, 'post_content' => $parsed['content'], 'post_excerpt' => $parsed['excerpt'], ); // Title: Static source title overrides WP Page title; if empty, retain existing (Q13 & Q20 Option B). if ( ! empty( $parsed['title'] ) ) { $post_data['post_title'] = $parsed['title']; } // Slug: Update slug to match sanitized static filename (Q16). if ( ! empty( $fetched['filename'] ) ) { $sanitized_slug = sanitize_title( $fetched['filename'] ); if ( ! empty( $sanitized_slug ) ) { $post_data['post_name'] = $sanitized_slug; } } // Remove any infinite loops when updating post. remove_action( 'post_updated', 'wp_save_post_revision' ); $update_result = wp_update_post( $post_data, true ); if ( is_wp_error( $update_result ) ) { update_post_meta( $post_id, self::META_LAST_STATUS, 'error' ); update_post_meta( $post_id, self::META_LAST_ERROR, $update_result->get_error_message() ); update_post_meta( $post_id, self::META_LAST_INDEXED, current_time( 'timestamp' ) ); return $update_result; } // Update post metadata. update_post_meta( $post_id, self::META_MANAGED, '1' ); update_post_meta( $post_id, self::META_SOURCE_URL, $source ); update_post_meta( $post_id, self::META_SOURCE_TYPE, $fetched['type'] ); update_post_meta( $post_id, self::META_LAST_INDEXED, current_time( 'timestamp' ) ); update_post_meta( $post_id, self::META_LAST_STATUS, 'success' ); delete_post_meta( $post_id, self::META_LAST_ERROR ); return true; } }