| @@ -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']; |
| @@ -390,8 +390,9 @@ | ||
| 390 | 390 | $scopes = [ |
| 391 | 391 | "global" => "persistent", |
| 392 | 392 | "front-end" => "frontend", |
| 393 | 393 | "admin" => "backend", |
| 394 | + "content" => "content_php", | |
| 394 | 395 | ]; |
| 395 | 396 | |
| 396 | 397 | if ( array_key_exists( $params['scope'], $scopes ) ) { |
| 397 | 398 | $params['scope'] = $scopes[$params['scope']]; |
| @@ -536,9 +537,9 @@ | ||
| 536 | 537 | return $tag !== ''; |
| 537 | 538 | } ); |
| 538 | 539 | } |
| 539 | 540 | $params['tags'] = $tags ? implode( ',', $tags ) : ''; |
| 540 | - $params['code'] = $this->sanitize_code( $params['code'] ); | |
| 541 | + //$params['code'] = $this->sanitize_code( $params['code'] ); | |
| 541 | 542 | return $params; |
| 542 | 543 | } |
| 543 | 544 | |
| 544 | 545 | /** |
| @@ -585,9 +586,12 @@ | ||
| 585 | 586 | $stats[$scope] = $this->wpdb->get_var( $this->wpdb->prepare( "SELECT COUNT( * ) FROM $this->table_name WHERE scope = %s", $scope ) ); |
| 586 | 587 | } |
| 587 | 588 | $stats['all'] += $stats[$scope]; |
| 588 | 589 | } |
| 589 | - | |
| 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 | + | |
| 590 | 594 | return $stats; |
| 591 | 595 | } |
| 592 | 596 | |
| 593 | 597 | public function import( ) |
| @@ -610,15 +614,34 @@ | ||
| 610 | 614 | |
| 611 | 615 | return count( $snippets ); |
| 612 | 616 | } |
| 613 | 617 | |
| 614 | - private function sanitize_code( $code ) | |
| 618 | + public function sanitize_code( $code ) | |
| 615 | 619 | { |
| 616 | - $code = preg_replace( '/<\?php/', '', $code, 1 ); | |
| 617 | 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 | + | |
| 618 | 628 | return $code; |
| 619 | 629 | } |
| 620 | 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 | + | |
| 621 | 644 | #endregion |
| 622 | 645 | |
| 623 | 646 | #region Snippet CRUD |
| 624 | 647 | |
| @@ -649,8 +672,11 @@ | ||
| 649 | 672 | } |
| 650 | 673 | else if ( $filter['accessor'] === 'scope' && $filter['value'] === 'global' ) { |
| 651 | 674 | $freshFilters[] = [ 'accessor' => 'scope', 'value' => ['backend', 'frontend', 'persistent'] ]; |
| 652 | 675 | } |
| 676 | + else if ( $filter['accessor'] === 'scope' && $filter['value'] === 'content' ) { | |
| 677 | + $freshFilters[] = [ 'accessor' => 'scope', 'value' => ['content_php', 'content_js'] ]; | |
| 678 | + } | |
| 653 | 679 | else { |
| 654 | 680 | $freshFilters[] = $filter; |
| 655 | 681 | } |
| 656 | 682 | } |
| @@ -874,8 +900,12 @@ | ||
| 874 | 900 | } |
| 875 | 901 | |
| 876 | 902 | function check_db( ) |
| 877 | 903 | { |
| 904 | + if ( $this->db_check ) { | |
| 905 | + return true; | |
| 906 | + } | |
| 907 | + | |
| 878 | 908 | if ( $this->does_table_exist( $this->table_name ) ) { |
| 879 | 909 | $this->check_columns( ); |
| 880 | 910 | $this->db_check = true; |
| 881 | 911 | } else { |