PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.5.6
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.5.6
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
← All changes | classes/api.php +52 -7 0.3.70.5.6 View file →
@@ -171,9 +171,9 @@
171 171 *
172 172 * @return mixed The result of the snippet execution.
173 173 * @throws Exception If the snippet cannot be executed.
174 174 */
175 - public function executeSnippet( $id, $args = [] ) {
175 + public function executeSnippet( $id, $args = [], $reply = null ) {
176 176 try {
177 177 if ( empty( $id ) ) {
178 178 throw new Exception( 'The snippet ID is required.' );
179 179 }
@@ -186,9 +186,34 @@
186 186 $snippet = $this->snippet_module->get_function( $id );
187 187 if ( empty( $snippet ) ) {
188 188 throw new Exception( sprintf( 'Snippet with ID %d not found.', $id ) );
189 189 }
190 -
190 +
191 + $query = $reply ? $reply->query : null;
192 + $array_query = [];
193 + if( $query ) {
194 + $array_query['envId'] = $query->envId ?? 'No EnvId in Query';
195 + $array_query['botId'] = $query->botId ?? 'No BotId in Query';
196 + $array_query['customId'] = $query->customId ?? 'No CustomId in Query';
197 + $array_query['embeddingsEnvId'] = $query->embeddingsEnvId ?? 'No EmbeddingsEnvId in Query';
198 +
199 +
200 + $array_query['chatId'] = $query->chatId ?? 'No ChatId in Query';
201 + $array_query['session'] = $query->session ?? 'No Session in Query';
202 + $array_query['scope'] = $query->scope ?? 'No Scope in Query';
203 +
204 + $array_query['instructions'] = $query->instructions ?? 'No Instructions in Query';
205 + $array_query['context'] = $query->context ?? 'No Context in Query';
206 +
207 + $array_query['messages'] = $query->messages ? json_encode( $query->messages ) : 'No Messages in Query';
208 + $array_query['message'] = $query->message ?? 'No Message in Query';
209 +
210 + $array_query['model'] = $query->model ?? 'No Model in Query';
211 + $array_query['feature'] = $query->feature ?? 'No Feature in Query';
212 + }
213 +
214 + $args['__mwai_query'] = $array_query;
215 +
191 216 $output = $this->core->run_snippet( $id, $args );
192 217
193 218 if ( $this->debug ) {
194 219 $shortOutput = is_string( $output ) ? substr( $output, 0, 100 ) : json_encode( $output );
@@ -216,8 +241,20 @@
216 241 *
217 242 * @return mixed The result of the snippet execution.
218 243 * @throws Exception If the snippet cannot be executed.
219 244 */
245 + /**
246 + * Declare every active PHP function snippet in the current request so they can call
247 + * one another, without executing any of them. Idempotent and cheap (runs once per
248 + * request). executeSnippet()/executeSnippetByName() already call this internally, so
249 + * you only need it to make function snippets callable from your own custom PHP.
250 + *
251 + * @return void
252 + */
253 + public function loadFunctions() {
254 + $this->core->define_all_functions();
255 + }
256 +
220 257 public function executeSnippetByName( $name, $args = [] ) {
221 258 try {
222 259 if ( empty( $name ) ) {
223 260 throw new Exception( 'The snippet name is required.' );
@@ -250,9 +287,9 @@
250 287 * Create a new snippet.
251 288 *
252 289 * @param string $name Name of the snippet.
253 290 * @param string $code Code of the snippet.
254 - * @param string $scope Scope of the snippet: 'function', 'backend', 'frontend', 'scheduled', 'persistent'.
291 + * @param string $scope Scope of the snippet: 'function', 'backend', 'frontend', 'scheduled', 'persistent', 'content_php', 'content_js'.
255 292 * @param array $options Additional options:
256 293 * - target: 'php' or 'js' (for function snippets)
257 294 * - description: Description of the snippet
258 295 * - args: Arguments for function snippets
@@ -257,8 +294,9 @@
257 294 * - description: Description of the snippet
258 295 * - args: Arguments for function snippets
259 296 * - argsData: Argument data for function snippets (use 'description' for each argument's description)
260 297 * - behavior: 'dynamic' or 'static' (for function snippets)
298 + * - mcp: Expose this function as its own MCP tool in AI Engine (for function snippets)
261 299 * - tags: Array of tags
262 300 * - priority: Execution priority
263 301 * - active: Active status (true/false)
264 302 * - endpoint: REST endpoint
@@ -279,13 +317,13 @@
279 317 if ( empty( $code ) ) {
280 318 throw new Exception( 'The snippet code is required.' );
281 319 }
282 320
283 - $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent'];
321 + $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent', 'content_php', 'content_js'];
284 322 if ( !in_array( $scope, $validScopes ) ) {
285 323 throw new Exception( sprintf( 'Invalid scope. Must be one of: %s', implode( ', ', $validScopes ) ) );
286 324 }
287 -
325 +
288 326 // Extract options with defaults
289 327 $target = $options['target'] ?? 'php';
290 328 $description = $options['description'] ?? '';
291 329 $args = $options['args'] ?? [];
@@ -290,8 +328,9 @@
290 328 $description = $options['description'] ?? '';
291 329 $args = $options['args'] ?? [];
292 330 $argsData = $options['argsData'] ?? [];
293 331 $behavior = $options['behavior'] ?? 'dynamic';
332 + $mcp = $options['mcp'] ?? false;
294 333 $tags = $options['tags'] ?? ['api'];
295 334 $priority = $options['priority'] ?? 10;
296 335 $active = $options['active'] ?? true;
297 336 $endpoint = $options['endpoint'] ?? '';
@@ -335,8 +374,9 @@
335 374 $params['functionTarget'] = $target;
336 375 $params['functionArgs'] = $args;
337 376 $params['functionArgsDict'] = $argsData;
338 377 $params['functionBehavior'] = $behavior;
378 + $params['functionMcp'] = $mcp;
339 379 }
340 380
341 381 // Add scheduled-specific params
342 382 if ( $scope === 'scheduled' ) {
@@ -378,8 +418,9 @@
378 418 * - target: 'php' or 'js' (for function snippets)
379 419 * - args: Arguments (for function snippets)
380 420 * - argsData: Argument data (for function snippets)
381 421 * - behavior: 'dynamic' or 'static' (for function snippets)
422 + * - mcp: Expose this function as its own MCP tool in AI Engine (for function snippets)
382 423 * - intervalHours: Hours (for scheduled snippets)
383 424 * - intervalMinutes: Minutes (for scheduled snippets)
384 425 *
385 426 * @return array The updated snippet data.
@@ -402,9 +443,9 @@
402 443 }
403 444
404 445 // Validate scope if provided
405 446 if ( isset( $params['scope'] ) ) {
406 - $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent'];
447 + $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent', 'content_php', 'content_js'];
407 448 if ( !in_array( $params['scope'], $validScopes ) ) {
408 449 throw new Exception( sprintf( 'Invalid scope. Must be one of: %s', implode( ', ', $validScopes ) ) );
409 450 }
410 451 }
@@ -440,8 +481,12 @@
440 481 $updateParams['functionTarget'] = $params['target'] ?? $snippet['functionTarget'] ?? 'php';
441 482 $updateParams['functionArgs'] = $params['args'] ?? $snippet['functionArgs'] ?? [];
442 483 $updateParams['functionArgsDict'] = $params['argsData'] ?? $snippet['functionArgsDict'] ?? [];
443 484 $updateParams['functionBehavior'] = $params['behavior'] ?? $snippet['functionBehavior'] ?? 'dynamic';
485 + // select_one() doesn't carry function metadata, so read the current MCP flag
486 + // from the stored function to preserve it when the caller doesn't set it.
487 + $existingFn = $this->snippet_module->get_function( $id );
488 + $updateParams['functionMcp'] = $params['mcp'] ?? ( $existingFn['mcp'] ?? false );
444 489 }
445 490
446 491 // Add scheduled-specific params if it's a scheduled snippet
447 492 if ( $scope === 'scheduled' ) {
@@ -730,9 +775,9 @@
730 775
731 776 /**
732 777 * Get snippets by scope.
733 778 *
734 - * @param string $scope The scope to filter by: 'backend', 'frontend', 'function', 'scheduled', 'persistent'.
779 + * @param string $scope The scope to filter by: 'backend', 'frontend', 'function', 'scheduled', 'persistent', 'content_php', 'content_js'.
735 780 * @param array $filters Additional filters: 'active' (bool), 'tags' (array).
736 781 * @return array The list of snippets matching the criteria.
737 782 * @throws Exception If the scope is invalid.
738 783 */