| @@ -101,28 +101,40 @@ | ||
| 101 | 101 | * @return array The list of snippets. |
| 102 | 102 | */ |
| 103 | 103 | public function getSnippets( $safe = true, $scope = null ) { |
| 104 | 104 | try { |
| 105 | - // Don't log here to avoid infinite loops during initialization | |
| 105 | + | |
| 106 | + $allSnippets = $this->snippet_module->select( 0, 9999, | |
| 107 | + [ ['accessor' => 'scope', 'value' => $scope] ] | |
| 108 | + , [] ); | |
| 109 | + | |
| 110 | + $snippets = $allSnippets['data'] ?? []; | |
| 106 | 111 | |
| 107 | - // If scope is specified, use getSnippetsByScope | |
| 108 | - if ( $scope ) { | |
| 109 | - $snippets = $this->getSnippetsByScope( $scope ); | |
| 110 | - } else { | |
| 111 | - // Get all snippets from the database | |
| 112 | - $allSnippets = $this->snippet_module->select( 0, 9999, [], [] ); | |
| 113 | - $snippets = $allSnippets['data'] ?? []; | |
| 114 | - } | |
| 115 | 112 | |
| 116 | 113 | if ( $safe ) { |
| 114 | + | |
| 117 | 115 | $snippets = array_filter( $snippets, function( $snippet ) { |
| 118 | - $name = $snippet['name']; | |
| 119 | - if ( !preg_match( '/^[a-zA-Z0-9_-]{1,64}$/', $name ) ) { | |
| 120 | - if ( $this->debug ) { | |
| 121 | - $this->core->log( sprintf( 'API [GetSnippets]: Filtered out snippet with invalid name: %s', $name ) ); | |
| 116 | + | |
| 117 | + if( $snippet['scope'] === 'function' ) { | |
| 118 | + | |
| 119 | + if( array_key_exists( 'functionName', $snippet ) ) | |
| 120 | + { | |
| 121 | + $name = $snippet['functionName']; | |
| 122 | + if ( !preg_match( '/^[a-zA-Z0-9_-]{1,64}$/', $name ) ) { | |
| 123 | + if ( $this->debug ) { | |
| 124 | + $this->core->log( sprintf( 'API [GetSnippets]: Filtered out snippet with invalid name: %s', $name ) ); | |
| 125 | + } | |
| 126 | + return false; | |
| 127 | + } | |
| 128 | + } else { | |
| 129 | + if ( $this->debug ) { | |
| 130 | + $this->core->log( 'API [GetSnippets]: Filtered out snippet with missing functionName.' ); | |
| 131 | + } | |
| 132 | + return false; | |
| 122 | 133 | } |
| 123 | - return false; | |
| 134 | + | |
| 124 | 135 | } |
| 136 | + | |
| 125 | 137 | return true; |
| 126 | 138 | } ); |
| 127 | 139 | } |
| 128 | 140 | |
| @@ -143,9 +155,9 @@ | ||
| 143 | 155 | * @param bool $safe Whether to filter out snippets with invalid names. |
| 144 | 156 | * @return array The list of function snippets only. |
| 145 | 157 | */ |
| 146 | 158 | public function get_functions( $safe = true ) { |
| 147 | - error_log( 'Code Engine API: get_functions() is deprecated. Please use getSnippets() instead.' ); | |
| 159 | + $this->core->log( '⚠️ API Warning: get_functions() is deprecated. Please use getSnippets() instead.' ); | |
| 148 | 160 | // Return only function snippets for backward compatibility |
| 149 | 161 | return $this->getSnippets( $safe, 'function' ); |
| 150 | 162 | } |
| 151 | 163 | |
| @@ -725,9 +737,9 @@ | ||
| 725 | 737 | * @throws Exception If the scope is invalid. |
| 726 | 738 | */ |
| 727 | 739 | public function getSnippetsByScope( $scope, $filters = [] ) { |
| 728 | 740 | try { |
| 729 | - $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent']; | |
| 741 | + $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent', 'content_js', 'content_php']; | |
| 730 | 742 | if ( !in_array( $scope, $validScopes ) ) { |
| 731 | 743 | throw new Exception( sprintf( 'Invalid scope. Must be one of: %s', implode( ', ', $validScopes ) ) ); |
| 732 | 744 | } |
| 733 | 745 | |