get_allowed_post_types(); if (!in_array($post_type, $allowed_post_types, true)) { wp_send_json_error('Invalid post type'); return; } // Setup query args $args = array( 'post_type' => $post_type, 'post_status' => array('publish', 'draft', 'pending', 'private', 'future'), 'posts_per_page' => 100, // Limit for performance 'orderby' => 'title', 'order' => 'ASC', ); // Add filters from URL (like in the editor integration) // For example, if a user had filtered posts by status we should respect that $url_params = array(); foreach (array('orderby', 'order', 'post_status') as $key) { if (isset($_GET[$key])) { $url_params[$key] = sanitize_text_field(wp_unslash($_GET[$key])); // Apply to query if ($key === 'post_status' && $_GET[$key] !== 'all') { $args['post_status'] = $url_params[$key]; } elseif ($key === 'orderby' || $key === 'order') { $args[$key] = $url_params[$key]; } } } // Get posts $query = new WP_Query($args); $posts = array(); foreach ($query->posts as $post) { $edit_url = get_edit_post_link($post->ID, 'raw'); // Add URL parameters and nonce if (!empty($url_params)) { $edit_url = add_query_arg($url_params, $edit_url); $edit_url = add_query_arg('ai_editor_navigation_nonce', wp_create_nonce('ai-editor-navigation-nonce'), $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 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();