PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.3.0
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.3.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 +94 -17 0.2.80.3.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' ),
@@ -315,10 +320,8 @@
315 320 try {
316 321 $params = $request->get_json_params();
317 322 $functions = $params['functions'];
318 323
319 - error_log( print_r( $functions, 1 ) );
320 -
321 324 if ( empty( $functions ) ) {
322 325 $functions = [];
323 326 } else {
324 327 // Decode the JSON string into a PHP array
@@ -342,8 +345,84 @@
342 345
343 346
344 347 #region MISC
345 348
349 + function rest_quick_start( $request ) {
350 + global $mwai;
351 +
352 + if ( is_null( $mwai ) || !isset( $mwai ) ) {
353 + return new WP_REST_Response([ 'success' => false, 'message' => 'AI Engine is not available.' ], 500 );
354 + }
355 +
356 + $params = $request->get_json_params();
357 + if( empty( $params['prompt'] ) ) {
358 + return new WP_REST_Response([ 'success' => false, 'message' => 'Prompt is required.' ], 500 );
359 + }
360 +
361 + $user_prompt = "<user_prompt>" . $params['prompt'] . "</user_prompt>";
362 +
363 + // Main instructions: explain the categories and how to generate the snippet
364 + $prompt_instructions = "<instruction>"
365 + . "Create a code snippet based on the user's prompt for a WordPress environment. "
366 + . "The snippet can be in PHP or JS. If the prompt does not specify the language, assume PHP. "
367 + . "Below are the categories you can use for the code snippet:\n\n"
368 + . "1. 'function': This can be either a JS or PHP function. The snippet should only contain the function itself, "
369 + . " with no code outside its declaration. The function can take parameters; for each parameter, specify a "
370 + . " default value, type ('string', 'number', 'boolean', 'mixed', etc.), required boolean, and a description.\n"
371 + . "2. 'scheduled': This can only be PHP. It’s code that will be run by a cron job for scheduled tasks. "
372 + . " If the user wants a task to run over time or on a schedule, use this category. The scheduling itself "
373 + . " is handled automatically, so you only need to generate the main code. You can specify a target hour "
374 + . " and minute.\n"
375 + . "3. 'persistent', 'back', 'front': This is a PHP-only snippet for use across the website, typically for WordPress filters or "
376 + . " action hooks. You can specify if the snippet should be 'persistent', 'front', or 'back' to indicate "
377 + . " whether it loads on the front end, back end, or both. These are know as global snippets.\n"
378 + . "4. 'content': This can be either 'content_js' or 'content_php'. A shortcode will be generated for this "
379 + . " snippet so the user can place it inside a post.\n"
380 + . "</instruction>";
381 +
382 + // Goal: specify what data to generate
383 + $prompt_goal = "<goal>"
384 + . "Generate the snippet’s code and the metadata needed to describe it, including:\n"
385 + . "- A snippet name\n"
386 + . "- A description\n"
387 + . "- The type (one of 'backend', 'frontend', 'function', 'persistent', 'scheduled', 'content_php', 'content_js')\n"
388 + . "- Tags (comma-separated)\n"
389 + . "For certain types, include additional details:\n"
390 + . "- For a 'function' type, provide the scope ('js' or 'php') and its parameters, including default values, types, required and descriptions.\n"
391 + . "- For a 'scheduled' snippet, specify the target hour and minute.\n"
392 + . "</goal>";
393 +
394 + // Warning: any additional information the user should know
395 + $prompt_warning = "<warning>"
396 + . "Dont use any <?php tags, <script> tags or markdown ``` format, just provide the raw code. "
397 + . " 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. "
398 + . "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. "
399 + . " 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. "
400 + . "</warning>";
401 +
402 + // Format: how to structure the final output
403 + $prompt_format = "<format>"
404 + . "Return all information in JSON format with the following keys:\n"
405 + . "- 'code': the code for the snippet. It should not be on one line only, make sure it is well formated.\n"
406 + . "- 'name': the snippet name\n"
407 + . "- 'description': a description of the snippet\n"
408 + . "- 'type': the snippet type\n"
409 + . "- 'tags': the snippet tags\n"
410 + . "- 'scope': the scope (only applicable if type is 'function')\n"
411 + . "- 'parameters': an array of parameter objects (only for 'function'), each with 'name', 'default', 'type', and 'description'\n"
412 + . "- 'target_hour': the target hour (only for 'scheduled')\n"
413 + . "- 'target_minute': the target minute (only for 'scheduled')\n"
414 + . "- '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"
415 + . "</format>";
416 +
417 + // Combine all prompt parts
418 + $prompt = $prompt_instructions . $prompt_goal . $prompt_format . $prompt_warning . $user_prompt;
419 +
420 + $response = $mwai->simpleJsonQuery( $prompt );
421 +
422 + return new WP_REST_Response( [ 'success' => true, 'data' => $response ], 200 );
423 + }
424 +
346 425 function rest_server_clock() {
347 426 $now = current_datetime();
348 427 $now = $now->format( 'H:i:s ( Y/m/d )' );
349 428
@@ -580,23 +659,13 @@
580 659 $code = $params['code'];
581 660 if ( empty( $name ) || empty( $code ) ) {
582 661 throw new Exception( __( 'Name and code are required.', 'code-engine' ) );
583 662 }
584 - $this->snippet->validate( $params );
663 +
664 + $res = $this->core->add_snippet( $params );
665 + $snippet = $res['snippet'];
666 + $result = $res['result'];
585 667
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 668 return new WP_REST_Response([ 'success' => true, 'data' => $result, 'snippet' => $snippet ], 200);
600 669 } catch (Exception $e) {
601 670 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage() ], 500 );
602 671 }
@@ -688,9 +757,17 @@
688 757 $params = $request->get_json_params();
689 758 }
690 759
691 760 try {
692 - $output = $this->core->run_snippet( null, [], $params );
761 + $output = "(Code Engine) No output.";
762 + $error = null;
763 +
764 + $scope = $params['scope'];
765 + if ( $scope === 'function' ) {
766 + $output = $this->core->run_snippet( null, [], $params );
767 + } else {
768 + $output = $this->core->run_non_fn_snippet( null, $params['code'], true );
769 + }
693 770
694 771 $error = $output['error'] ?? null;
695 772 unset( $output['error'] );
696 773