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
code-snippets / php / Integration / Evaluate_Content.php

Evaluate_Content.php in Code Snippets 4.0.0-beta.2, at php/Integration/Evaluate_Content.php

154 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets\Integration;
4
5 use Code_Snippets\Core\DB;
6 use Code_Snippets\Flat_Files\Snippet_Files;
7 use function Code_Snippets\code_snippets;
8
9 /**
10 * Class for evaluating content snippets.
11 *
12 * @package Code_Snippets
13 */
14 class Evaluate_Content {
15
16 /**
17 * Database class.
18 *
19 * @var DB
20 */
21 private DB $db;
22
23 /**
24 * Cached list of active snippets, indexed by scope.
25 *
26 * @var ?array[]
27 */
28 private ?array $active_snippets = null;
29
30 /**
31 * Class constructor.
32 *
33 * @param DB $db Database class instance.
34 */
35 public function __construct( DB $db ) {
36 $this->db = $db;
37 add_action( 'init', [ $this, 'init' ] );
38 }
39
40 /**
41 * Initialize class functions.
42 */
43 public function init() {
44 add_action( 'wp_head', [ $this, 'load_head_content' ] );
45 add_action( 'wp_body_open', [ $this, 'load_body_content' ] );
46 add_action( 'wp_footer', [ $this, 'load_footer_content' ] );
47 }
48
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 /**
69 * Print snippet code fetched from the database from a certain scope.
70 *
71 * @param string $scope Name of scope to print.
72 */
73 private function print_content_snippets( string $scope ) {
74 if ( is_null( $this->active_snippets ) ) {
75 if ( Snippet_Files::is_active() ) {
76 $this->populate_active_snippets_from_flat_files();
77 } else {
78 $this->populate_active_snippets();
79 }
80 }
81
82 if ( ! isset( $this->active_snippets[ $scope ] ) ) {
83 return;
84 }
85
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' );
89 }
90
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";
96 }
97 }
98 }
99
100 /**
101 * Print head content snippets.
102 *
103 * @return void
104 */
105 public function load_head_content() {
106 $this->print_content_snippets( 'head-content' );
107 }
108
109 /**
110 * Print body content snippets (requires theme to call wp_body_open).
111 *
112 * @return void
113 */
114 public function load_body_content() {
115 $this->print_content_snippets( 'body-content' );
116 }
117
118 /**
119 * Print footer content snippets.
120 *
121 * @return void
122 */
123 public function load_footer_content() {
124 $this->print_content_snippets( 'footer-content' );
125 }
126
127 /**
128 * Populate the active snippets from flat files.
129 *
130 * @return void
131 */
132 private function populate_active_snippets_from_flat_files() {
133 $handler = code_snippets()->snippet_handler_registry->get_handler( 'html' );
134 $dir_name = $handler->get_dir_name();
135 $ext = $handler->get_file_extension();
136
137 $scopes = [ 'head-content', 'body-content', 'footer-content' ];
138 $all_snippets = Snippet_Files::get_active_snippets_from_flat_files( $scopes, $dir_name );
139
140 foreach ( $all_snippets as $snippet ) {
141 $scope = $snippet['scope'];
142
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;
148
149 $this->active_snippets[ $scope ][] = $snippet;
150 }
151 }
152 }
153 }
154