PluginProbe
Code Snippets / 3.7.0
Code Snippets v3.7.0
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 3.0.1 All 64 releases
code-snippets / php / evaluation / class-evaluate-content.php

class-evaluate-content.php in Code Snippets 3.7.0, at php/evaluation/class-evaluate-content.php

81 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Evaluation;
4
5 use Code_Snippets\DB;
6 use Code_Snippets\Snippet;
7
8 /**
9 * Class for evaluating content snippets.
10 *
11 * @package Code_Snippets
12 */
13 class Evaluate_Content {
14
15 /**
16 * Database class.
17 *
18 * @var DB
19 */
20 private DB $db;
21
22 /**
23 * Cached list of active snippets.
24 *
25 * @var ?Snippet[]
26 */
27 private ?array $active_snippets = null;
28
29 /**
30 * Class constructor.
31 *
32 * @param DB $db Database class instance.
33 */
34 public function __construct( DB $db ) {
35 $this->db = $db;
36 add_action( 'init', array( $this, 'init' ) );
37 }
38
39 /**
40 * Initialise class functions.
41 */
42 public function init() {
43 add_action( 'wp_head', [ $this, 'load_head_content' ] );
44 add_action( 'wp_footer', [ $this, 'load_footer_content' ] );
45 }
46
47 /**
48 * Print snippet code fetched from the database from a certain scope.
49 *
50 * @param string $scope Name of scope to print.
51 */
52 private function print_content_snippets( string $scope ) {
53 $scopes = [ 'head-content', 'footer-content' ];
54
55 if ( is_null( $this->active_snippets ) ) {
56 $this->active_snippets = $this->db->fetch_active_snippets( $scopes );
57 }
58
59 foreach ( $this->active_snippets as $snippet ) {
60 if ( $scope === $snippet['scope'] ) {
61 // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
62 echo "\n", $snippet['code'], "\n";
63 }
64 }
65 }
66
67 /**
68 * Print head content snippets.
69 */
70 public function load_head_content() {
71 $this->print_content_snippets( 'head-content' );
72 }
73
74 /**
75 * Print footer content snippets.
76 */
77 public function load_footer_content() {
78 $this->print_content_snippets( 'footer-content' );
79 }
80 }
81