PluginProbe
Code Snippets / 4.0.0-beta.2
Code Snippets v4.0.0-beta.2
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
← All changes | php/Integration/Evaluate_Content.php +46 -26 3.10.1 → 4.0.0-beta.2 View file →
@@ -3,9 +3,8 @@
3 3 namespace Code_Snippets\Integration;
4 4
5 5 use Code_Snippets\Core\DB;
6 6 use Code_Snippets\Flat_Files\Snippet_Files;
7 -use Code_Snippets\Model\Snippet;
8 7 use function Code_Snippets\code_snippets;
9 8
10 9 /**
11 10 * Class for evaluating content snippets.
@@ -21,11 +20,11 @@
21 20 */
22 21 private DB $db;
23 22
24 23 /**
25 - * Cached list of active snippets.
24 + * Cached list of active snippets, indexed by scope.
26 25 *
27 - * @var ?Snippet[]
26 + * @var ?array[]
28 27 */
29 28 private ?array $active_snippets = null;
30 29
31 30 /**
@@ -34,13 +33,13 @@
34 33 * @param DB $db Database class instance.
35 34 */
36 35 public function __construct( DB $db ) {
37 36 $this->db = $db;
38 - add_action( 'init', array( $this, 'init' ) );
37 + add_action( 'init', [ $this, 'init' ] );
39 38 }
40 39
41 40 /**
42 - * Initialise class functions.
41 + * Initialize class functions.
43 42 */
44 43 public function init() {
45 44 add_action( 'wp_head', [ $this, 'load_head_content' ] );
46 45 add_action( 'wp_body_open', [ $this, 'load_body_content' ] );
@@ -47,35 +46,54 @@
47 46 add_action( 'wp_footer', [ $this, 'load_footer_content' ] );
48 47 }
49 48
50 49 /**
50 + * Populate the active snippets cache.
51 + *
52 + * This function fetches active snippets from the database and stores them in the
53 + * $active_snippets property.
54 + */
55 + private function populate_active_snippets() {
56 + $scopes = [ 'head-content', 'body-content', 'footer-content' ];
57 + $snippets = $this->db->fetch_active_snippets( $scopes );
58 +
59 + foreach ( $snippets as $snippet ) {
60 + $scope = $snippet['scope'];
61 +
62 + if ( 'condition' !== $scope ) {
63 + $this->active_snippets[ $scope ][] = $snippet;
64 + }
65 + }
66 + }
67 +
68 + /**
51 69 * Print snippet code fetched from the database from a certain scope.
52 70 *
53 71 * @param string $scope Name of scope to print.
54 72 */
55 73 private function print_content_snippets( string $scope ) {
56 - if ( Snippet_Files::is_active() ) {
57 - if ( is_null( $this->active_snippets ) ) {
74 + if ( is_null( $this->active_snippets ) ) {
75 + if ( Snippet_Files::is_active() ) {
58 76 $this->populate_active_snippets_from_flat_files();
77 + } else {
78 + $this->populate_active_snippets();
59 79 }
80 + }
60 81
61 - if ( isset( $this->active_snippets[ $scope ] ) ) {
62 - foreach ( $this->active_snippets[ $scope ] as $snippet ) {
63 - require_once $snippet['file_path'];
64 - }
65 - }
66 - } else {
67 - $scopes = [ 'head-content', 'body-content', 'footer-content' ];
82 + if ( ! isset( $this->active_snippets[ $scope ] ) ) {
83 + return;
84 + }
68 85
69 - if ( is_null( $this->active_snippets ) ) {
70 - $this->active_snippets = $this->db->fetch_active_snippets( $scopes );
86 + foreach ( $this->active_snippets[ $scope ] as $snippet ) {
87 + if ( isset( $snippet['id'] ) ) {
88 + do_action( 'code_snippets/snippet_ran_on_page', (int) $snippet['id'], 'content' );
71 89 }
72 90
73 - foreach ( $this->active_snippets as $snippet ) {
74 - if ( $scope === $snippet['scope'] ) {
75 - // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
76 - echo "\n", $snippet['code'], "\n";
77 - }
91 + if ( Snippet_Files::is_active() ) {
92 + require_once $snippet['file_path'];
93 + } else {
94 + // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
95 + echo "\n", $snippet['code'], "\n";
78 96 }
79 97 }
80 98 }
81 99
@@ -121,13 +139,15 @@
121 139
122 140 foreach ( $all_snippets as $snippet ) {
123 141 $scope = $snippet['scope'];
124 142
125 - // Add file path information to the snippet for later use.
126 - $table_name = Snippet_Files::get_hashed_table_name( $snippet['table'] );
127 - $base_path = Snippet_Files::get_base_dir( $table_name, $dir_name );
128 - $snippet['file_path'] = $base_path . '/' . $snippet['id'] . '.' . $ext;
143 + if ( 'condition' !== $scope ) {
144 + // Add file path information to the snippet for later use.
145 + $table_name = Snippet_Files::get_hashed_table_name( $snippet['table'] );
146 + $base_path = Snippet_Files::get_base_dir( $table_name, $dir_name );
147 + $snippet['file_path'] = $base_path . '/' . $snippet['id'] . '.' . $ext;
129 148
130 - $this->active_snippets[ $scope ][] = $snippet;
149 + $this->active_snippets[ $scope ][] = $snippet;
150 + }
131 151 }
132 152 }
133 153 }