| @@ -71,8 +71,19 @@ | ||
| 71 | 71 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 72 | 72 | 'callback' => array( $this, 'rest_server_clock' ), |
| 73 | 73 | ) ); |
| 74 | 74 | |
| 75 | + register_rest_route( $this->namespace, '/fetch_posts', array( | |
| 76 | + 'methods' => 'POST', | |
| 77 | + 'permission_callback' => array( $this->core, 'can_access_features' ), | |
| 78 | + 'callback' => array( $this, 'rest_fetch_posts' ), | |
| 79 | + 'args' => array( | |
| 80 | + 'search' => array( 'required' => false ), | |
| 81 | + 'offset' => array( 'required' => false, 'default' => 0 ), | |
| 82 | + 'limit' => array( 'required' => false, 'default' => 10 ), | |
| 83 | + ) | |
| 84 | + ) ); | |
| 85 | + | |
| 75 | 86 | #endregion |
| 76 | 87 | |
| 77 | 88 | #region REST AI Engine |
| 78 | 89 | register_rest_route( $this->namespace, '/ai_read_comments', array( |
| @@ -426,8 +437,68 @@ | ||
| 426 | 437 | $now = current_datetime(); |
| 427 | 438 | $now = $now->format( 'H:i:s ( Y/m/d )' ); |
| 428 | 439 | |
| 429 | 440 | return new WP_REST_Response( [ 'success' => true, 'time' => $now ], 200 ); |
| 441 | + } | |
| 442 | + | |
| 443 | + function rest_fetch_posts( $request ) { | |
| 444 | + try { | |
| 445 | + $params = $request->get_json_params(); | |
| 446 | + $search = isset($params['search']) ? $params['search'] : ''; | |
| 447 | + $offset = isset($params['offset']) ? intval($params['offset']) : 0; | |
| 448 | + $limit = isset($params['limit']) ? intval($params['limit']) : 10; | |
| 449 | + | |
| 450 | + global $wpdb; | |
| 451 | + $searchPlaceholder = $search ? '%' . $search . '%' : ''; | |
| 452 | + $where_search_clause = $search ? $wpdb->prepare( | |
| 453 | + "AND ( p.post_title LIKE %s OR p.post_content LIKE %s OR p.post_name LIKE %s ) ", | |
| 454 | + $searchPlaceholder, | |
| 455 | + $searchPlaceholder, | |
| 456 | + $searchPlaceholder | |
| 457 | + ) : ''; | |
| 458 | + | |
| 459 | + $posts = $wpdb->get_results( | |
| 460 | + $wpdb->prepare( | |
| 461 | + "SELECT p.ID, p.post_title, p.post_date, p.post_status, u.display_name as author | |
| 462 | + FROM $wpdb->posts p | |
| 463 | + LEFT JOIN $wpdb->users u ON p.post_author = u.ID | |
| 464 | + WHERE p.post_type = 'post' | |
| 465 | + AND p.post_status IN ('publish', 'draft', 'private') | |
| 466 | + $where_search_clause | |
| 467 | + ORDER BY p.post_date DESC | |
| 468 | + LIMIT %d, %d", | |
| 469 | + $offset, | |
| 470 | + $limit | |
| 471 | + ), | |
| 472 | + OBJECT | |
| 473 | + ); | |
| 474 | + | |
| 475 | + $posts_count = (int)$wpdb->get_var( | |
| 476 | + "SELECT COUNT(*) | |
| 477 | + FROM $wpdb->posts p | |
| 478 | + WHERE p.post_type = 'post' | |
| 479 | + AND p.post_status IN ('publish', 'draft', 'private') | |
| 480 | + $where_search_clause" | |
| 481 | + ); | |
| 482 | + | |
| 483 | + $data = array_map(function($post) { | |
| 484 | + return [ | |
| 485 | + 'id' => $post->ID, | |
| 486 | + 'title' => $post->post_title, | |
| 487 | + 'date' => $post->post_date, | |
| 488 | + 'author' => $post->author, | |
| 489 | + 'status' => $post->post_status | |
| 490 | + ]; | |
| 491 | + }, $posts); | |
| 492 | + | |
| 493 | + return new WP_REST_Response([ | |
| 494 | + 'success' => true, | |
| 495 | + 'data' => $data, | |
| 496 | + 'total' => $posts_count | |
| 497 | + ], 200); | |
| 498 | + } catch (Exception $e) { | |
| 499 | + return new WP_REST_Response(['success' => false, 'message' => $e->getMessage()], 500); | |
| 500 | + } | |
| 430 | 501 | } |
| 431 | 502 | |
| 432 | 503 | #endregion |
| 433 | 504 | |