PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.4.2
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.4.2
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 +75 -14 0.2.90.4.2 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(
@@ -428,8 +439,68 @@
428 439
429 440 return new WP_REST_Response( [ 'success' => true, 'time' => $now ], 200 );
430 441 }
431 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 + }
501 + }
502 +
432 503 #endregion
433 504
434 505 #region AI Engine
435 506
@@ -659,22 +730,12 @@
659 730 $code = $params['code'];
660 731 if ( empty( $name ) || empty( $code ) ) {
661 732 throw new Exception( __( 'Name and code are required.', 'code-engine' ) );
662 733 }
663 - $this->snippet->validate( $params );
664 -
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 - }
734 +
735 + $res = $this->core->add_snippet( $params );
736 + $snippet = $res['snippet'];
737 + $result = $res['result'];
677 738
678 739 return new WP_REST_Response([ 'success' => true, 'data' => $result, 'snippet' => $snippet ], 200);
679 740 } catch (Exception $e) {
680 741 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage() ], 500 );