| @@ -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( |
| @@ -151,9 +162,15 @@ | ||
| 151 | 162 | 'callback' => array($this, 'rest_replace'), |
| 152 | 163 | 'permission_callback' => array($this->core, 'can_access_settings') |
| 153 | 164 | )); |
| 154 | 165 | |
| 166 | + register_rest_route($this->namespace, '/snippets/delete_duplicates', array( | |
| 167 | + 'methods' => 'POST', | |
| 168 | + 'callback' => array($this, 'rest_delete_duplicates'), | |
| 169 | + 'permission_callback' => array($this->core, 'can_access_settings') | |
| 170 | + )); | |
| 155 | 171 | |
| 172 | + | |
| 156 | 173 | // SNIPPETS ENDPOINTS |
| 157 | 174 | $snippets = $this->snippet->select( |
| 158 | 175 | null, // offset |
| 159 | 176 | -1, // limit |
| @@ -428,8 +445,68 @@ | ||
| 428 | 445 | |
| 429 | 446 | return new WP_REST_Response( [ 'success' => true, 'time' => $now ], 200 ); |
| 430 | 447 | } |
| 431 | 448 | |
| 449 | + function rest_fetch_posts( $request ) { | |
| 450 | + try { | |
| 451 | + $params = $request->get_json_params(); | |
| 452 | + $search = isset($params['search']) ? $params['search'] : ''; | |
| 453 | + $offset = isset($params['offset']) ? intval($params['offset']) : 0; | |
| 454 | + $limit = isset($params['limit']) ? intval($params['limit']) : 10; | |
| 455 | + | |
| 456 | + global $wpdb; | |
| 457 | + $searchPlaceholder = $search ? '%' . $search . '%' : ''; | |
| 458 | + $where_search_clause = $search ? $wpdb->prepare( | |
| 459 | + "AND ( p.post_title LIKE %s OR p.post_content LIKE %s OR p.post_name LIKE %s ) ", | |
| 460 | + $searchPlaceholder, | |
| 461 | + $searchPlaceholder, | |
| 462 | + $searchPlaceholder | |
| 463 | + ) : ''; | |
| 464 | + | |
| 465 | + $posts = $wpdb->get_results( | |
| 466 | + $wpdb->prepare( | |
| 467 | + "SELECT p.ID, p.post_title, p.post_date, p.post_status, u.display_name as author | |
| 468 | + FROM $wpdb->posts p | |
| 469 | + LEFT JOIN $wpdb->users u ON p.post_author = u.ID | |
| 470 | + WHERE p.post_type = 'post' | |
| 471 | + AND p.post_status IN ('publish', 'draft', 'private') | |
| 472 | + $where_search_clause | |
| 473 | + ORDER BY p.post_date DESC | |
| 474 | + LIMIT %d, %d", | |
| 475 | + $offset, | |
| 476 | + $limit | |
| 477 | + ), | |
| 478 | + OBJECT | |
| 479 | + ); | |
| 480 | + | |
| 481 | + $posts_count = (int)$wpdb->get_var( | |
| 482 | + "SELECT COUNT(*) | |
| 483 | + FROM $wpdb->posts p | |
| 484 | + WHERE p.post_type = 'post' | |
| 485 | + AND p.post_status IN ('publish', 'draft', 'private') | |
| 486 | + $where_search_clause" | |
| 487 | + ); | |
| 488 | + | |
| 489 | + $data = array_map(function($post) { | |
| 490 | + return [ | |
| 491 | + 'id' => $post->ID, | |
| 492 | + 'title' => $post->post_title, | |
| 493 | + 'date' => $post->post_date, | |
| 494 | + 'author' => $post->author, | |
| 495 | + 'status' => $post->post_status | |
| 496 | + ]; | |
| 497 | + }, $posts); | |
| 498 | + | |
| 499 | + return new WP_REST_Response([ | |
| 500 | + 'success' => true, | |
| 501 | + 'data' => $data, | |
| 502 | + 'total' => $posts_count | |
| 503 | + ], 200); | |
| 504 | + } catch (Exception $e) { | |
| 505 | + return new WP_REST_Response(['success' => false, 'message' => $e->getMessage()], 500); | |
| 506 | + } | |
| 507 | + } | |
| 508 | + | |
| 432 | 509 | #endregion |
| 433 | 510 | |
| 434 | 511 | #region AI Engine |
| 435 | 512 | |
| @@ -731,8 +808,17 @@ | ||
| 731 | 808 | $result[] = $this->snippet->insert( $snippet ); |
| 732 | 809 | } |
| 733 | 810 | |
| 734 | 811 | return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200); |
| 812 | + } catch (Exception $e) { | |
| 813 | + return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage() ], 500 ); | |
| 814 | + } | |
| 815 | + } | |
| 816 | + | |
| 817 | + public function rest_delete_duplicates() { | |
| 818 | + try { | |
| 819 | + $result = $this->snippet->delete_duplicates(); | |
| 820 | + return new WP_REST_Response([ 'success' => true, 'data' => [ 'deleted' => $result ] ], 200); | |
| 735 | 821 | } catch (Exception $e) { |
| 736 | 822 | return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage() ], 500 ); |
| 737 | 823 | } |
| 738 | 824 | } |