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/modules/snippet.php +10 -73 trunk0.3.0 View file →
@@ -137,22 +137,20 @@
137 137 $snippet['functionArgs'] = [];
138 138 $snippet['functionArgsDict'] = [];
139 139 $snippet['functionBehavior'] = '';
140 140 $snippet['functionTarget'] = 'PHP';
141 - $snippet['functionMcp'] = false;
142 141
143 142 foreach ( $functions_snippet as $function_snippet ) {
144 143 if ( $function_snippet['snippetId'] == $snippetId ) {
145 144
146 145 $snippet['functionName'] = $function_snippet['name'];
147 - $snippet['functionMcp'] = !empty( $function_snippet['mcp'] );
148 - if ( !isset( $function_snippet['behavior'] ) || empty( $function_snippet['behavior'] ) ) {
146 + if ( !isset( $snippet['functionBehavior'] ) || empty( $snippet['functionBehavior'] ) ) {
149 147 $snippet['functionBehavior'] = 'dynamic';
150 148 }
151 149 else {
152 150 $snippet['functionBehavior'] = $function_snippet['behavior'];
153 151 }
154 - if ( !isset( $function_snippet['target'] ) || empty( $function_snippet['target'] ) ) {
152 + if ( !isset( $snippet['functionTarget'] ) || empty( $snippet['functionTarget'] ) ) {
155 153 $snippet['functionTarget'] = 'PHP';
156 154 }
157 155 else {
158 156 $snippet['functionTarget'] = $function_snippet['target'];
@@ -217,9 +215,8 @@
217 215 'name' => $params['functionName'],
218 216 'behavior' => $params['functionBehavior'],
219 217 'desc' => $params['description'] ?? '',
220 218 'target' => $params['functionTarget'] ?? 'PHP',
221 - 'mcp' => !empty( $params['functionMcp'] ),
222 219 'args' => [],
223 220 ];
224 221
225 222 foreach ( $params['functionArgs'] as $argName ) {
@@ -226,9 +223,9 @@
226 223 if ( !empty( $argName ) ) {
227 224 $argData = $params['functionArgsDict'][$argName];
228 225 $snippet['args'][] = [
229 226 'name' => $argName,
230 - 'desc' => $argData['description'] ?? $argData['desc'] ?? '', // Support both 'description' and 'desc'
227 + 'desc' => $argData['desc'],
231 228 'default' => $argData['default'],
232 229 'required' => empty( $argData['default'] ),
233 230 'type' => $argData['type'],
234 231 ];
@@ -356,27 +353,8 @@
356 353
357 354 return null;
358 355 }
359 356
360 - /**
361 - * Return the snippet IDs of every function that opted in to MCP exposure.
362 - * Used by the "MCP" list filter and its count (functionMcp lives in the
363 - * functions option, not a snippets table column, so it can't be queried in SQL).
364 - *
365 - * @return int[]
366 - */
367 - public function get_mcp_function_ids()
368 - {
369 - $functions = get_option( $this->option_functions, [] );
370 - $ids = [];
371 - foreach ( $functions as $fn ) {
372 - if ( !empty( $fn['mcp'] ) && !empty( $fn['snippetId'] ) ) {
373 - $ids[] = (int) $fn['snippetId'];
374 - }
375 - }
376 - return $ids;
377 - }
378 -
379 357 #endregion
380 358
381 359 #region Utilities
382 360
@@ -412,9 +390,8 @@
412 390 $scopes = [
413 391 "global" => "persistent",
414 392 "front-end" => "frontend",
415 393 "admin" => "backend",
416 - "content" => "content_php",
417 394 ];
418 395
419 396 if ( array_key_exists( $params['scope'], $scopes ) ) {
420 397 $params['scope'] = $scopes[$params['scope']];
@@ -559,9 +536,9 @@
559 536 return $tag !== '';
560 537 } );
561 538 }
562 539 $params['tags'] = $tags ? implode( ',', $tags ) : '';
563 - //$params['code'] = $this->sanitize_code( $params['code'] );
540 + $params['code'] = $this->sanitize_code( $params['code'] );
564 541 return $params;
565 542 }
566 543
567 544 /**
@@ -572,9 +549,9 @@
572 549 */
573 550 private function formatParamsForFront( $params )
574 551 {
575 552 // Separate the scope tags from the tags
576 - $scopes = ['backend', 'frontend', 'function', 'persistent', 'scheduled', 'content_php', 'content_js'];
553 + $scopes = ['backend', 'frontend', 'function', 'persistent', 'scheduled'];
577 554
578 555 if ( isset( $params['tags'] ) && !empty( $params['tags'] ) ) {
579 556
580 557 $tags = array_map( function ( $tag ) use ( $scopes ) {
@@ -608,15 +585,9 @@
608 585 $stats[$scope] = $this->wpdb->get_var( $this->wpdb->prepare( "SELECT COUNT( * ) FROM $this->table_name WHERE scope = %s", $scope ) );
609 586 }
610 587 $stats['all'] += $stats[$scope];
611 588 }
612 -
613 - // Combined count for the "Content" filter (PHP + JS). Not added to 'all', already counted above.
614 - $stats['content'] = (int) $stats['content_php'] + (int) $stats['content_js'];
615 -
616 - // Functions exposed via MCP. A subset of 'function', so not added to 'all'.
617 - $stats['mcp'] = count( $this->get_mcp_function_ids() );
618 -
589 +
619 590 return $stats;
620 591 }
621 592
622 593 public function import( )
@@ -639,34 +610,15 @@
639 610
640 611 return count( $snippets );
641 612 }
642 613
643 - public function sanitize_code( $code )
614 + private function sanitize_code( $code )
644 615 {
616 + $code = preg_replace( '/<\?php/', '', $code, 1 );
645 617 $code = ltrim( $code );
646 -
647 - $first_chats = substr( $code, 0, 5 );
648 - if( $first_chats === '<?php' ) {
649 - $code = substr( $code, 5 );
650 - $code = ltrim( $code );
651 - }
652 -
653 618 return $code;
654 619 }
655 620
656 - public function delete_duplicates() {
657 - // Delete snippets that have the same code and scope, keeping only the most recent one ( based on the updated column )
658 - $query = "DELETE t1 FROM $this->table_name t1
659 - INNER JOIN $this->table_name t2
660 - WHERE t1.id < t2.id
661 - AND t1.code = t2.code
662 - AND t1.scope = t2.scope";
663 -
664 - $this->wpdb->query( $query );
665 -
666 - return $this->wpdb->rows_affected;
667 - }
668 -
669 621 #endregion
670 622
671 623 #region Snippet CRUD
672 624
@@ -697,11 +649,8 @@
697 649 }
698 650 else if ( $filter['accessor'] === 'scope' && $filter['value'] === 'global' ) {
699 651 $freshFilters[] = [ 'accessor' => 'scope', 'value' => ['backend', 'frontend', 'persistent'] ];
700 652 }
701 - else if ( $filter['accessor'] === 'scope' && $filter['value'] === 'content' ) {
702 - $freshFilters[] = [ 'accessor' => 'scope', 'value' => ['content_php', 'content_js'] ];
703 - }
704 653 else {
705 654 $freshFilters[] = $filter;
706 655 }
707 656 }
@@ -733,22 +682,14 @@
733 682 $where[] = $this->wpdb->prepare( "active = %d", $value );
734 683 } elseif ( $filter['accessor'] === 'endpoint' ) {
735 684 $where[] = boolval( $filter['value'] ) ? "endpoint <> ''" : "endpoint = ''";
736 685 } elseif ( $filter['accessor'] === 'scope' ) {
737 - if ( $filter['value'] === 'mcp' ) {
738 - // Not a scope: constrain to the functions opted in to MCP exposure.
739 - $ids = $this->get_mcp_function_ids();
740 - if ( empty( $ids ) ) {
741 - $where[] = '1 = 0';
742 - } else {
743 - $where[] = 'id IN (' . implode( ',', array_map( 'intval', $ids ) ) . ')';
744 - }
745 - } else if ( is_array( $filter['value'] ) ) {
686 + if ( is_array( $filter['value'] ) ) {
746 687 $scopes = array_map( function( $scope ) {
747 688 return esc_sql( $scope );
748 689 }, $filter['value'] );
749 690 $where[] = "scope IN ('" . implode( "', '", $scopes ) . "')";
750 - } else if ( !empty( $filter['value'] ) ) {
691 + } else {
751 692 $value = esc_sql( $filter['value'] );
752 693 $where[] = $this->wpdb->prepare( "scope = %s", $value );
753 694 }
754 695 }
@@ -933,12 +874,8 @@
933 874 }
934 875
935 876 function check_db( )
936 877 {
937 - if ( $this->db_check ) {
938 - return true;
939 - }
940 -
941 878 if ( $this->does_table_exist( $this->table_name ) ) {
942 879 $this->check_columns( );
943 880 $this->db_check = true;
944 881 } else {