PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.4.8
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.4.8
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 +60 -9 0.2.90.4.8 View file →
@@ -142,15 +142,15 @@
142 142 foreach ( $functions_snippet as $function_snippet ) {
143 143 if ( $function_snippet['snippetId'] == $snippetId ) {
144 144
145 145 $snippet['functionName'] = $function_snippet['name'];
146 - if ( !isset( $snippet['functionBehavior'] ) || empty( $snippet['functionBehavior'] ) ) {
146 + if ( !isset( $function_snippet['behavior'] ) || empty( $function_snippet['behavior'] ) ) {
147 147 $snippet['functionBehavior'] = 'dynamic';
148 148 }
149 149 else {
150 150 $snippet['functionBehavior'] = $function_snippet['behavior'];
151 151 }
152 - if ( !isset( $snippet['functionTarget'] ) || empty( $snippet['functionTarget'] ) ) {
152 + if ( !isset( $function_snippet['target'] ) || empty( $function_snippet['target'] ) ) {
153 153 $snippet['functionTarget'] = 'PHP';
154 154 }
155 155 else {
156 156 $snippet['functionTarget'] = $function_snippet['target'];
@@ -223,9 +223,9 @@
223 223 if ( !empty( $argName ) ) {
224 224 $argData = $params['functionArgsDict'][$argName];
225 225 $snippet['args'][] = [
226 226 'name' => $argName,
227 - 'desc' => $argData['desc'],
227 + 'desc' => $argData['description'] ?? $argData['desc'] ?? '', // Support both 'description' and 'desc'
228 228 'default' => $argData['default'],
229 229 'required' => empty( $argData['default'] ),
230 230 'type' => $argData['type'],
231 231 ];
@@ -332,8 +332,29 @@
332 332 }
333 333 return null;
334 334 }
335 335
336 + public function get_function_by_name( $name, $options = [] )
337 + {
338 + $functions = $this->get_functions( );
339 +
340 + foreach ( $functions as $function ) {
341 + if ( $function['name'] == $name ) {
342 +
343 + if ( array_key_exists( 'php_ready_args', $options ) && $options['php_ready_args'] === false ) {
344 + $function['args'] = array_map( function ( $arg ) {
345 + $arg['name'] = ltrim( $arg['name'], '$' );
346 + return $arg;
347 + }, $function['args'] );
348 + }
349 +
350 + return $function;
351 + }
352 + }
353 +
354 + return null;
355 + }
356 +
336 357 #endregion
337 358
338 359 #region Utilities
339 360
@@ -369,8 +390,9 @@
369 390 $scopes = [
370 391 "global" => "persistent",
371 392 "front-end" => "frontend",
372 393 "admin" => "backend",
394 + "content" => "content_php",
373 395 ];
374 396
375 397 if ( array_key_exists( $params['scope'], $scopes ) ) {
376 398 $params['scope'] = $scopes[$params['scope']];
@@ -515,9 +537,9 @@
515 537 return $tag !== '';
516 538 } );
517 539 }
518 540 $params['tags'] = $tags ? implode( ',', $tags ) : '';
519 - $params['code'] = $this->sanitize_code( $params['code'] );
541 + //$params['code'] = $this->sanitize_code( $params['code'] );
520 542 return $params;
521 543 }
522 544
523 545 /**
@@ -528,9 +550,9 @@
528 550 */
529 551 private function formatParamsForFront( $params )
530 552 {
531 553 // Separate the scope tags from the tags
532 - $scopes = ['backend', 'frontend', 'function', 'persistent', 'scheduled'];
554 + $scopes = ['backend', 'frontend', 'function', 'persistent', 'scheduled', 'content_php', 'content_js'];
533 555
534 556 if ( isset( $params['tags'] ) && !empty( $params['tags'] ) ) {
535 557
536 558 $tags = array_map( function ( $tag ) use ( $scopes ) {
@@ -564,9 +586,12 @@
564 586 $stats[$scope] = $this->wpdb->get_var( $this->wpdb->prepare( "SELECT COUNT( * ) FROM $this->table_name WHERE scope = %s", $scope ) );
565 587 }
566 588 $stats['all'] += $stats[$scope];
567 589 }
568 -
590 +
591 + // Combined count for the "Content" filter (PHP + JS). Not added to 'all', already counted above.
592 + $stats['content'] = (int) $stats['content_php'] + (int) $stats['content_js'];
593 +
569 594 return $stats;
570 595 }
571 596
572 597 public function import( )
@@ -589,15 +614,34 @@
589 614
590 615 return count( $snippets );
591 616 }
592 617
593 - private function sanitize_code( $code )
618 + public function sanitize_code( $code )
594 619 {
595 - $code = preg_replace( '/<\?php/', '', $code, 1 );
596 620 $code = ltrim( $code );
621 +
622 + $first_chats = substr( $code, 0, 5 );
623 + if( $first_chats === '<?php' ) {
624 + $code = substr( $code, 5 );
625 + $code = ltrim( $code );
626 + }
627 +
597 628 return $code;
598 629 }
599 630
631 + public function delete_duplicates() {
632 + // Delete snippets that have the same code and scope, keeping only the most recent one ( based on the updated column )
633 + $query = "DELETE t1 FROM $this->table_name t1
634 + INNER JOIN $this->table_name t2
635 + WHERE t1.id < t2.id
636 + AND t1.code = t2.code
637 + AND t1.scope = t2.scope";
638 +
639 + $this->wpdb->query( $query );
640 +
641 + return $this->wpdb->rows_affected;
642 + }
643 +
600 644 #endregion
601 645
602 646 #region Snippet CRUD
603 647
@@ -628,8 +672,11 @@
628 672 }
629 673 else if ( $filter['accessor'] === 'scope' && $filter['value'] === 'global' ) {
630 674 $freshFilters[] = [ 'accessor' => 'scope', 'value' => ['backend', 'frontend', 'persistent'] ];
631 675 }
676 + else if ( $filter['accessor'] === 'scope' && $filter['value'] === 'content' ) {
677 + $freshFilters[] = [ 'accessor' => 'scope', 'value' => ['content_php', 'content_js'] ];
678 + }
632 679 else {
633 680 $freshFilters[] = $filter;
634 681 }
635 682 }
@@ -666,9 +713,9 @@
666 713 $scopes = array_map( function( $scope ) {
667 714 return esc_sql( $scope );
668 715 }, $filter['value'] );
669 716 $where[] = "scope IN ('" . implode( "', '", $scopes ) . "')";
670 - } else {
717 + } else if ( !empty( $filter['value'] ) ) {
671 718 $value = esc_sql( $filter['value'] );
672 719 $where[] = $this->wpdb->prepare( "scope = %s", $value );
673 720 }
674 721 }
@@ -853,8 +900,12 @@
853 900 }
854 901
855 902 function check_db( )
856 903 {
904 + if ( $this->db_check ) {
905 + return true;
906 + }
907 +
857 908 if ( $this->does_table_exist( $this->table_name ) ) {
858 909 $this->check_columns( );
859 910 $this->db_check = true;
860 911 } else {