PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.4.0
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.4.0
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 +165 -17 0.2.80.4.0 View file →
@@ -59,8 +59,13 @@
59 59 ) );
60 60 #endregion
61 61
62 62 #region MISC
63 + register_rest_route( $this->namespace, '/quick_start', array(
64 + 'methods' => 'POST',
65 + 'permission_callback' => array( $this->core, 'can_access_settings' ),
66 + 'callback' => array( $this, 'rest_quick_start' ),
67 + ) );
63 68
64 69 register_rest_route( $this->namespace, '/server_clock', array(
65 70 'methods' => 'GET',
66 71 'permission_callback' => array( $this->core, 'can_access_settings' ),
@@ -66,8 +71,19 @@
66 71 'permission_callback' => array( $this->core, 'can_access_settings' ),
67 72 'callback' => array( $this, 'rest_server_clock' ),
68 73 ) );
69 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 +
70 86 #endregion
71 87
72 88 #region REST AI Engine
73 89 register_rest_route( $this->namespace, '/ai_read_comments', array(
@@ -315,10 +331,8 @@
315 331 try {
316 332 $params = $request->get_json_params();
317 333 $functions = $params['functions'];
318 334
319 - error_log( print_r( $functions, 1 ) );
320 -
321 335 if ( empty( $functions ) ) {
322 336 $functions = [];
323 337 } else {
324 338 // Decode the JSON string into a PHP array
@@ -342,8 +356,84 @@
342 356
343 357
344 358 #region MISC
345 359
360 + function rest_quick_start( $request ) {
361 + global $mwai;
362 +
363 + if ( is_null( $mwai ) || !isset( $mwai ) ) {
364 + return new WP_REST_Response([ 'success' => false, 'message' => 'AI Engine is not available.' ], 500 );
365 + }
366 +
367 + $params = $request->get_json_params();
368 + if( empty( $params['prompt'] ) ) {
369 + return new WP_REST_Response([ 'success' => false, 'message' => 'Prompt is required.' ], 500 );
370 + }
371 +
372 + $user_prompt = "<user_prompt>" . $params['prompt'] . "</user_prompt>";
373 +
374 + // Main instructions: explain the categories and how to generate the snippet
375 + $prompt_instructions = "<instruction>"
376 + . "Create a code snippet based on the user's prompt for a WordPress environment. "
377 + . "The snippet can be in PHP or JS. If the prompt does not specify the language, assume PHP. "
378 + . "Below are the categories you can use for the code snippet:\n\n"
379 + . "1. 'function': This can be either a JS or PHP function. The snippet should only contain the function itself, "
380 + . " with no code outside its declaration. The function can take parameters; for each parameter, specify a "
381 + . " default value, type ('string', 'number', 'boolean', 'mixed', etc.), required boolean, and a description.\n"
382 + . "2. 'scheduled': This can only be PHP. It’s code that will be run by a cron job for scheduled tasks. "
383 + . " If the user wants a task to run over time or on a schedule, use this category. The scheduling itself "
384 + . " is handled automatically, so you only need to generate the main code. You can specify a target hour "
385 + . " and minute.\n"
386 + . "3. 'persistent', 'back', 'front': This is a PHP-only snippet for use across the website, typically for WordPress filters or "
387 + . " action hooks. You can specify if the snippet should be 'persistent', 'front', or 'back' to indicate "
388 + . " whether it loads on the front end, back end, or both. These are know as global snippets.\n"
389 + . "4. 'content': This can be either 'content_js' or 'content_php'. A shortcode will be generated for this "
390 + . " snippet so the user can place it inside a post.\n"
391 + . "</instruction>";
392 +
393 + // Goal: specify what data to generate
394 + $prompt_goal = "<goal>"
395 + . "Generate the snippet’s code and the metadata needed to describe it, including:\n"
396 + . "- A snippet name\n"
397 + . "- A description\n"
398 + . "- The type (one of 'backend', 'frontend', 'function', 'persistent', 'scheduled', 'content_php', 'content_js')\n"
399 + . "- Tags (comma-separated)\n"
400 + . "For certain types, include additional details:\n"
401 + . "- For a 'function' type, provide the scope ('js' or 'php') and its parameters, including default values, types, required and descriptions.\n"
402 + . "- For a 'scheduled' snippet, specify the target hour and minute.\n"
403 + . "</goal>";
404 +
405 + // Warning: any additional information the user should know
406 + $prompt_warning = "<warning>"
407 + . "Dont use any <?php tags, <script> tags or markdown ``` format, just provide the raw code. "
408 + . " When using a 'scheduled' all the cron scheduling is handled automatically, so you only need to generate the main code and dont need to worry about the scheduling logic. "
409 + . "If the type is 'function', the code should only contain the function itself, with no code outside its declaration, as well as no comments outside of the function. THIS IS THE CASE ONLY FOR FUNCTIONS. For any other type, the code is invoked using eval() and should be a complete snippet of code. If the snippet is a function type the default values should NOT be deplared in the parameters, this will be done automatically. If the snippet is not a function type but your wirte a function, make sure to call the function at the end of the code. "
410 + . " If the snippet is a content type, the code should be the content of the shortcode, not the shortcode itself. and NOT a function, it should be raw code with an echo statement. There are no attributes for content snippets. The shortcode is generated automatically like [code-engine id=''] and the id is the snippet id. "
411 + . "</warning>";
412 +
413 + // Format: how to structure the final output
414 + $prompt_format = "<format>"
415 + . "Return all information in JSON format with the following keys:\n"
416 + . "- 'code': the code for the snippet. It should not be on one line only, make sure it is well formated.\n"
417 + . "- 'name': the snippet name\n"
418 + . "- 'description': a description of the snippet\n"
419 + . "- 'type': the snippet type\n"
420 + . "- 'tags': the snippet tags\n"
421 + . "- 'scope': the scope (only applicable if type is 'function')\n"
422 + . "- 'parameters': an array of parameter objects (only for 'function'), each with 'name', 'default', 'type', and 'description'\n"
423 + . "- 'target_hour': the target hour (only for 'scheduled')\n"
424 + . "- 'target_minute': the target minute (only for 'scheduled')\n"
425 + . "- 'misc': any additional information. If you had to assume some stuff, like a filter name or a thing the user didn't think about, let it know by adding messages in this key.\n"
426 + . "</format>";
427 +
428 + // Combine all prompt parts
429 + $prompt = $prompt_instructions . $prompt_goal . $prompt_format . $prompt_warning . $user_prompt;
430 +
431 + $response = $mwai->simpleJsonQuery( $prompt );
432 +
433 + return new WP_REST_Response( [ 'success' => true, 'data' => $response ], 200 );
434 + }
435 +
346 436 function rest_server_clock() {
347 437 $now = current_datetime();
348 438 $now = $now->format( 'H:i:s ( Y/m/d )' );
349 439
@@ -349,8 +439,68 @@
349 439
350 440 return new WP_REST_Response( [ 'success' => true, 'time' => $now ], 200 );
351 441 }
352 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 +
353 503 #endregion
354 504
355 505 #region AI Engine
356 506
@@ -580,23 +730,13 @@
580 730 $code = $params['code'];
581 731 if ( empty( $name ) || empty( $code ) ) {
582 732 throw new Exception( __( 'Name and code are required.', 'code-engine' ) );
583 733 }
584 - $this->snippet->validate( $params );
734 +
735 + $res = $this->core->add_snippet( $params );
736 + $snippet = $res['snippet'];
737 + $result = $res['result'];
585 738
586 - $params = $this->snippet->formatParamsForDatabase( $params );
587 - $result = $this->snippet->insert( $params );
588 - $snippet = $this->snippet->select_one( $result );
589 -
590 - if( $result ) {
591 - $params['id'] = (string)$result;
592 -
593 - $this->snippet->create_or_update_function_snippet( $params );
594 - $this->snippet->create_or_update_interval_snippet( $params );
595 -
596 - $this->snippet->get_function_snippets_data( $snippet );
597 - }
598 -
599 739 return new WP_REST_Response([ 'success' => true, 'data' => $result, 'snippet' => $snippet ], 200);
600 740 } catch (Exception $e) {
601 741 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage() ], 500 );
602 742 }
@@ -688,9 +828,17 @@
688 828 $params = $request->get_json_params();
689 829 }
690 830
691 831 try {
692 - $output = $this->core->run_snippet( null, [], $params );
832 + $output = "(Code Engine) No output.";
833 + $error = null;
834 +
835 + $scope = $params['scope'];
836 + if ( $scope === 'function' ) {
837 + $output = $this->core->run_snippet( null, [], $params );
838 + } else {
839 + $output = $this->core->run_non_fn_snippet( null, $params['code'], true );
840 + }
693 841
694 842 $error = $output['error'] ?? null;
695 843 unset( $output['error'] );
696 844