| @@ -241,8 +241,20 @@ | ||
| 241 | 241 | * |
| 242 | 242 | * @return mixed The result of the snippet execution. |
| 243 | 243 | * @throws Exception If the snippet cannot be executed. |
| 244 | 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 | + | |
| 245 | 257 | public function executeSnippetByName( $name, $args = [] ) { |
| 246 | 258 | try { |
| 247 | 259 | if ( empty( $name ) ) { |
| 248 | 260 | throw new Exception( 'The snippet name is required.' ); |
| @@ -275,9 +287,9 @@ | ||
| 275 | 287 | * Create a new snippet. |
| 276 | 288 | * |
| 277 | 289 | * @param string $name Name of the snippet. |
| 278 | 290 | * @param string $code Code of the snippet. |
| 279 | - * @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'. | |
| 280 | 292 | * @param array $options Additional options: |
| 281 | 293 | * - target: 'php' or 'js' (for function snippets) |
| 282 | 294 | * - description: Description of the snippet |
| 283 | 295 | * - args: Arguments for function snippets |
| @@ -282,8 +294,9 @@ | ||
| 282 | 294 | * - description: Description of the snippet |
| 283 | 295 | * - args: Arguments for function snippets |
| 284 | 296 | * - argsData: Argument data for function snippets (use 'description' for each argument's description) |
| 285 | 297 | * - behavior: 'dynamic' or 'static' (for function snippets) |
| 298 | + * - mcp: Expose this function as its own MCP tool in AI Engine (for function snippets) | |
| 286 | 299 | * - tags: Array of tags |
| 287 | 300 | * - priority: Execution priority |
| 288 | 301 | * - active: Active status (true/false) |
| 289 | 302 | * - endpoint: REST endpoint |
| @@ -304,13 +317,13 @@ | ||
| 304 | 317 | if ( empty( $code ) ) { |
| 305 | 318 | throw new Exception( 'The snippet code is required.' ); |
| 306 | 319 | } |
| 307 | 320 | |
| 308 | - $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent']; | |
| 321 | + $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent', 'content_php', 'content_js']; | |
| 309 | 322 | if ( !in_array( $scope, $validScopes ) ) { |
| 310 | 323 | throw new Exception( sprintf( 'Invalid scope. Must be one of: %s', implode( ', ', $validScopes ) ) ); |
| 311 | 324 | } |
| 312 | - | |
| 325 | + | |
| 313 | 326 | // Extract options with defaults |
| 314 | 327 | $target = $options['target'] ?? 'php'; |
| 315 | 328 | $description = $options['description'] ?? ''; |
| 316 | 329 | $args = $options['args'] ?? []; |
| @@ -315,8 +328,9 @@ | ||
| 315 | 328 | $description = $options['description'] ?? ''; |
| 316 | 329 | $args = $options['args'] ?? []; |
| 317 | 330 | $argsData = $options['argsData'] ?? []; |
| 318 | 331 | $behavior = $options['behavior'] ?? 'dynamic'; |
| 332 | + $mcp = $options['mcp'] ?? false; | |
| 319 | 333 | $tags = $options['tags'] ?? ['api']; |
| 320 | 334 | $priority = $options['priority'] ?? 10; |
| 321 | 335 | $active = $options['active'] ?? true; |
| 322 | 336 | $endpoint = $options['endpoint'] ?? ''; |
| @@ -360,8 +374,9 @@ | ||
| 360 | 374 | $params['functionTarget'] = $target; |
| 361 | 375 | $params['functionArgs'] = $args; |
| 362 | 376 | $params['functionArgsDict'] = $argsData; |
| 363 | 377 | $params['functionBehavior'] = $behavior; |
| 378 | + $params['functionMcp'] = $mcp; | |
| 364 | 379 | } |
| 365 | 380 | |
| 366 | 381 | // Add scheduled-specific params |
| 367 | 382 | if ( $scope === 'scheduled' ) { |
| @@ -403,8 +418,9 @@ | ||
| 403 | 418 | * - target: 'php' or 'js' (for function snippets) |
| 404 | 419 | * - args: Arguments (for function snippets) |
| 405 | 420 | * - argsData: Argument data (for function snippets) |
| 406 | 421 | * - behavior: 'dynamic' or 'static' (for function snippets) |
| 422 | + * - mcp: Expose this function as its own MCP tool in AI Engine (for function snippets) | |
| 407 | 423 | * - intervalHours: Hours (for scheduled snippets) |
| 408 | 424 | * - intervalMinutes: Minutes (for scheduled snippets) |
| 409 | 425 | * |
| 410 | 426 | * @return array The updated snippet data. |
| @@ -427,9 +443,9 @@ | ||
| 427 | 443 | } |
| 428 | 444 | |
| 429 | 445 | // Validate scope if provided |
| 430 | 446 | if ( isset( $params['scope'] ) ) { |
| 431 | - $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent']; | |
| 447 | + $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent', 'content_php', 'content_js']; | |
| 432 | 448 | if ( !in_array( $params['scope'], $validScopes ) ) { |
| 433 | 449 | throw new Exception( sprintf( 'Invalid scope. Must be one of: %s', implode( ', ', $validScopes ) ) ); |
| 434 | 450 | } |
| 435 | 451 | } |
| @@ -465,8 +481,12 @@ | ||
| 465 | 481 | $updateParams['functionTarget'] = $params['target'] ?? $snippet['functionTarget'] ?? 'php'; |
| 466 | 482 | $updateParams['functionArgs'] = $params['args'] ?? $snippet['functionArgs'] ?? []; |
| 467 | 483 | $updateParams['functionArgsDict'] = $params['argsData'] ?? $snippet['functionArgsDict'] ?? []; |
| 468 | 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 ); | |
| 469 | 489 | } |
| 470 | 490 | |
| 471 | 491 | // Add scheduled-specific params if it's a scheduled snippet |
| 472 | 492 | if ( $scope === 'scheduled' ) { |
| @@ -755,9 +775,9 @@ | ||
| 755 | 775 | |
| 756 | 776 | /** |
| 757 | 777 | * Get snippets by scope. |
| 758 | 778 | * |
| 759 | - * @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'. | |
| 760 | 780 | * @param array $filters Additional filters: 'active' (bool), 'tags' (array). |
| 761 | 781 | * @return array The list of snippets matching the criteria. |
| 762 | 782 | * @throws Exception If the scope is invalid. |
| 763 | 783 | */ |