get_allowed_post_types(); if ( ! in_array( $post_type, $allowed_post_types, true ) ) { wp_send_json_error( 'Invalid post type' ); return; } // Get sort parameters $sort_params = $this->get_sort_parameters(); // Build orderby array with ID as secondary sort $orderby_array = $this->build_orderby_array( $sort_params['orderby'], $sort_params['order'] ); // Setup query args $args = array( 'post_type' => $post_type, 'post_status' => $this->get_post_statuses( $sort_params['post_status'] ), 'posts_per_page' => 100, // Limit for performance 'orderby' => $orderby_array, ); // Get posts $query = new WP_Query( $args ); $posts = array(); foreach ( $query->posts as $post ) { $edit_url = get_edit_post_link( $post->ID, 'raw' ); // Build URL parameters $url_params = array(); if ( ! empty( $sort_params['orderby'] ) && 'menu_order' !== $sort_params['orderby'] ) { $url_params['orderby'] = $sort_params['orderby']; } if ( ! empty( $sort_params['order'] ) && 'ASC' !== $sort_params['order'] ) { $url_params['order'] = $sort_params['order']; } if ( ! empty( $sort_params['post_status'] ) && 'all' !== $sort_params['post_status'] ) { $url_params['post_status'] = $sort_params['post_status']; } // Add URL parameters and nonce if ( ! empty( $url_params ) ) { $edit_url = add_query_arg( $url_params, $edit_url ); } // Always add nonce $edit_url = add_query_arg( AI_Editor_Nonce_Manager::NAVIGATION_NONCE, AI_Editor_Nonce_Manager::create_nonce( AI_Editor_Nonce_Manager::NAVIGATION_ACTION ), $edit_url ); $posts[] = array( 'id' => $post->ID, 'title' => get_the_title( $post ), 'edit_url' => $edit_url, 'status' => $post->post_status, 'date' => get_the_date( 'Y-m-d', $post ), ); } wp_send_json_success( $posts ); } /** * Get sort parameters from AJAX request * * @return array Sort parameters */ private function get_sort_parameters() { // Default values based on post type // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is checked in ajax_get_posts $post_type = isset( $_GET['post_type'] ) ? sanitize_key( $_GET['post_type'] ) : 'post'; // Set defaults based on post type if ( 'page' === $post_type ) { $default_orderby = 'menu_order'; $default_order = 'ASC'; } else { $default_orderby = 'date'; $default_order = 'DESC'; } $orderby = $default_orderby; $order = $default_order; $post_status = 'all'; $allowed_fields = array( 'menu_order', 'title', 'date', 'modified', 'author', 'ID' ); // Get parameters from request - nonce already verified in ajax_get_posts // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce already verified in ajax_get_posts if ( ! empty( $_GET['orderby'] ) && in_array( $_GET['orderby'], $allowed_fields, true ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce already verified in ajax_get_posts $orderby = sanitize_text_field( wp_unslash( $_GET['orderby'] ) ); } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce already verified in ajax_get_posts if ( ! empty( $_GET['order'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce already verified in ajax_get_posts $order = strtoupper( sanitize_text_field( wp_unslash( $_GET['order'] ) ) ); // Validate order parameter if ( ! in_array( $order, array( 'ASC', 'DESC' ), true ) ) { $order = $default_order; } } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce already verified in ajax_get_posts if ( isset( $_GET['post_status'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce already verified in ajax_get_posts $post_status = sanitize_text_field( wp_unslash( $_GET['post_status'] ) ); } return array( 'orderby' => $orderby, 'order' => $order, 'post_status' => $post_status, ); } /** * Build orderby array with ID as secondary sort * * @param string $orderby Primary orderby field * @param string $order Sort order (ASC/DESC) * @return array Orderby array for WP_Query */ private function build_orderby_array( $orderby, $order ) { // Special handling for menu_order (Pages) if ( 'menu_order' === $orderby ) { return array( 'menu_order' => $order, 'title' => $order, 'ID' => $order ); } // For other fields, always add ID as secondary sort return array( $orderby => $order, 'ID' => $order ); } /** * Get post statuses array * * @param string $status Status parameter * @return array Post statuses */ private function get_post_statuses( $status ) { if ( 'all' === $status || empty( $status ) ) { return array( 'publish', 'draft', 'pending', 'private', 'future' ); } return array( $status ); } /** * Get allowed post types from options * * @return array Array of allowed post types. */ private function get_allowed_post_types() { $cache_key = 'ai_editor_allowed_post_types'; $allowed_post_types = get_transient( $cache_key ); if ( false !== $allowed_post_types ) { return $allowed_post_types; } $allowed_post_types = get_option( 'ai_editor_post_types', array( 'post', 'page' ) ); if ( ! is_array( $allowed_post_types ) ) { $allowed_post_types = array(); } set_transient( $cache_key, $allowed_post_types, HOUR_IN_SECONDS ); return $allowed_post_types; } } // Initialize the class new AI_Editor_Posts_Search();