PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.3.1
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.3.1
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 0.3.8 All 32 releases
code-engine / classes / api.php

api.php in Code Engine – PHP Snippets, AI Functions & Automation for WordPress 0.3.1, at classes/api.php

212 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Meow_MWCODE_API
4 {
5 private $core = null;
6 private $snippet = null;
7
8
9 public function __construct( $core, $snippet ) {
10 $this->core = $core;
11 $this->snippet = $snippet;
12 }
13
14 /**
15 * Get a function by its ID.
16 *
17 * The options are used to filter the functions:
18 * - 'php_ready_args' (bool): If false, the arguments will not be formatted for PHP. (no $ before the names).
19 *
20 * @param $id
21 * @param array $options
22 *
23 * @return mixed
24 */
25 public function get_function( $id, $options = [] ) {
26 $snippet = $this->snippet->get_function( $id, $options );
27 return $snippet;
28 }
29
30
31 /**
32 * Get a function by its name.
33 *
34 * The options are used to filter the functions:
35 * - 'php_ready_args' (bool): If false, the arguments will not be formatted for PHP. (no $ before the names).
36 *
37 * @param $name The name of the function to be retrieved.
38 * @param array $options
39 *
40 * @return mixed
41 */
42 public function get_function_by_name( $name, $options = [] ) {
43 $snippet = $this->snippet->get_function_by_name( $name, $options );
44 return $snippet;
45 }
46
47 /**
48 * Get all functions.
49 *
50 * @return mixed
51 *
52 */
53 public function get_functions( $safe = true ) {
54 $snippets = $this->snippet->get_functions();
55
56 if ( $safe ) {
57
58 $snippets = array_filter( $snippets, function( $snippet ) {
59 $name = $snippet['name'];
60 if ( !preg_match( '/^[a-zA-Z0-9_-]{1,64}$/', $name ) ) {
61 return false;
62 }
63
64 return true;
65 } );
66
67 }
68
69 return $snippets;
70 }
71
72
73 /**
74 * Execute a function by its ID.
75 * The arguments should be an associative array with the argument names as keys.
76 * Example: [ "$city" => " 'Tokyo' ", "$date" => "1999" ]
77 *
78 * @param $id
79 * @param $args
80 *
81 * @return mixed
82 */
83 public function execute_function( $id, $args ) {
84 $output = $this->core->run_snippet( $id, $args );
85 return $output;
86 }
87
88
89 /**
90 * Create a new callable snippet ( Function ).
91 *
92 * @param string $name Name of the function. This should be the name of the function declaration, like 'get_weather'.
93 * @param string $target Target of the snippet. This should be either 'php' or 'js'. This will be used to determine the type of the function.
94 * @param string $code Code of the snippet. This should be either a PHP or a JS function. No code outside of the function itself should be included.
95 * @param string $description Description of the snippet. This will be displayed in Code Engine and used for Function Calling.
96 * @param array $args Arguments of the function. This array should have the full name of each argument, like ['$city', '$date']. The '$' is required if the function is PHP.
97 * @param array $args_data Data of the arguments. This array should have the name of each argument as key and an array with the type, description and default value. Example: [ '$city' => ['type' => 'string', 'desc' => 'City name', 'default' => 'Tokyo' ] ]. This will be used for Function Calling.
98 * @param string $behavior Behavior of the function. This should be either 'dynamic' or 'static'. This will be used for Function Calling.
99 *
100 */
101 public function create_function( $name, $target, $code, $description, $args, $args_data, $behavior ) {
102
103 $temp_name = 'mwcode_' . time();
104
105 $params = [
106 // Predefined values
107 'id' => null,
108 'active' => 1,
109 'priority' => 10,
110 'tags' => ['api'],
111 'endpoint' => '',
112 'scope' => 'function',
113
114 // Custom values
115 'name' => $name ?? $temp_name,
116 'code' => $code ?? "function $temp_name() { return 'Hello World'; }",
117 'description' => $description ?? '',
118
119 'functionName' => $name ?? $temp_name,
120 'functionTarget' => $target ?? 'php',
121 'functionArgs' => $args ?? [],
122 'functionArgsDict' => $args_data ?? [],
123 'functionBehavior' => $behavior ?? 'dynamic',
124 ];
125
126 $res = $this->core->add_snippet( $params );
127
128 return $res;
129 }
130
131
132 /**
133 * Update a function by its name. If the function does not exist, it will return false. The parameters are the same as create_function, but except the name the rest are optional. If not specified, the current values will be used.
134 *
135 * @param string $name Name of the function. This should be the name of the function declaration, like 'get_weather'.
136 * @param string $target Target of the snippet. This should be either 'php' or 'js'. This will be used to determine the type of the function.
137 * @param string $code Code of the snippet. This should be either a PHP or a JS function. No code outside of the function itself should be included.
138 * @param string $description Description of the snippet. This will be displayed in Code Engine and used for Function Calling.
139 * @param array $args Arguments of the function. This array should have the full name of each argument, like ['$city', '$date']. The '$' is required if the function is PHP.
140 * @param array $args_data Data of the arguments. This array should have the name of each argument as key and an array with the type, description and default value. Example: [ '$city' => ['type' => 'string', 'desc' => 'City name', 'default' => 'Tokyo' ] ]. This will be used for Function Calling.
141 * @param string $behavior Behavior of the function. This should be either 'dynamic' or 'static'. This will be used for Function Calling.
142 */
143 public function update_function( $name, $target, $code, $description, $args, $args_data, $behavior ) {
144 $snippet = $this->snippet->get_function_by_name( $name );
145 if ( !$snippet ) { return false; }
146
147 $params = [
148 // Predefined values
149 'id' => $snippet['snippetId'],
150 'active' => 1,
151 'priority' => 10,
152 'tags' => ['api'],
153 'endpoint' => '',
154 'scope' => 'function',
155
156 // Custom values
157 'name' => $name ?? $snippet['name'],
158 'code' => $code ?? $snippet['code'],
159 'description' => $description ?? '',
160
161 'functionName' => $name ?? $snippet['functionName'],
162 'functionTarget' => $target ?? $snippet['functionTarget'],
163 'functionArgs' => $args ?? $snippet['functionArgs'],
164 'functionArgsDict' => $args_data ?? $snippet['functionArgsDict'],
165 'functionBehavior' => $behavior ?? $snippet['functionBehavior'],
166 ];
167
168
169 $res = $this->core->add_snippet( $params );
170
171 return $res;
172 }
173
174 /**
175 * Delete a function by its ID.
176 *
177 * @param int $id Function ID
178 * @return mixed
179 */
180 public function delete_function( $id ) {
181 $params = [
182 'id' => $id,
183 ];
184
185 $this->snippet->delete_function_snippet( $params );
186 $result = $this->snippet->delete( $params );
187
188 return $result;
189 }
190
191 /**
192 * Delete a function by its name.
193 *
194 * @param string $name Function name
195 * @return mixed
196 */
197 public function delete_function_by_name( $name ) {
198 $snippet = $this->snippet->get_function_by_name( $name );
199 if ( !$snippet ) { return false; }
200
201 $params = [
202 'id' => $snippet['snippetId'],
203 ];
204
205 $this->snippet->delete_function_snippet( $params );
206 $result = $this->snippet->delete( $params );
207
208 return $result;
209 }
210
211 }
212