PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / trunk
Code Engine – PHP Snippets, AI Functions & Automation for WordPress vtrunk
0.5.7 0.5.6 0.5.5 0.5.4 0.5.3 0.5.2 0.5.1 0.5.0 0.4.9 0.4.8 0.4.7 0.4.6 trunk 0.0.1 0.0.2 0.2.8 0.2.9 0.3.0 0.3.1 0.3.2 0.3.3 0.3.4 0.3.5 0.3.6 0.3.7 All 33 releases
← All changes | classes/rest.php +90 -14 0.2.9trunk View file →
@@ -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
@@ -659,23 +736,13 @@
659 736 $code = $params['code'];
660 737 if ( empty( $name ) || empty( $code ) ) {
661 738 throw new Exception( __( 'Name and code are required.', 'code-engine' ) );
662 739 }
663 - $this->snippet->validate( $params );
740 +
741 + $res = $this->core->add_snippet( $params );
742 + $snippet = $res['snippet'];
743 + $result = $res['result'];
664 744
665 - $params = $this->snippet->formatParamsForDatabase( $params );
666 - $result = $this->snippet->insert( $params );
667 - $snippet = $this->snippet->select_one( $result );
668 -
669 - if( $result ) {
670 - $params['id'] = (string)$result;
671 -
672 - $this->snippet->create_or_update_function_snippet( $params );
673 - $this->snippet->create_or_update_interval_snippet( $params );
674 -
675 - $this->snippet->get_function_snippets_data( $snippet );
676 - }
677 -
678 745 return new WP_REST_Response([ 'success' => true, 'data' => $result, 'snippet' => $snippet ], 200);
679 746 } catch (Exception $e) {
680 747 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage() ], 500 );
681 748 }
@@ -741,8 +808,17 @@
741 808 $result[] = $this->snippet->insert( $snippet );
742 809 }
743 810
744 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);
745 821 } catch (Exception $e) {
746 822 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage() ], 500 );
747 823 }
748 824 }