| 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 Code_Snippets\Model\Snippet; |
| 8 |
use function Code_Snippets\code_snippets; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class for evaluating content snippets. |
| 12 |
* |
| 13 |
* @package Code_Snippets |
| 14 |
*/ |
| 15 |
class Evaluate_Content { |
| 16 |
|
| 17 |
/** |
| 18 |
* Database class. |
| 19 |
* |
| 20 |
* @var DB |
| 21 |
*/ |
| 22 |
private DB $db; |
| 23 |
|
| 24 |
/** |
| 25 |
* Cached list of active snippets. |
| 26 |
* |
| 27 |
* @var ?Snippet[] |
| 28 |
*/ |
| 29 |
private ?array $active_snippets = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Class constructor. |
| 33 |
* |
| 34 |
* @param DB $db Database class instance. |
| 35 |
*/ |
| 36 |
public function __construct( DB $db ) { |
| 37 |
$this->db = $db; |
| 38 |
add_action( 'init', array( $this, 'init' ) ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Initialise class functions. |
| 43 |
*/ |
| 44 |
public function init() { |
| 45 |
add_action( 'wp_head', [ $this, 'load_head_content' ] ); |
| 46 |
add_action( 'wp_body_open', [ $this, 'load_body_content' ] ); |
| 47 |
add_action( 'wp_footer', [ $this, 'load_footer_content' ] ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Print snippet code fetched from the database from a certain scope. |
| 52 |
* |
| 53 |
* @param string $scope Name of scope to print. |
| 54 |
*/ |
| 55 |
private function print_content_snippets( string $scope ) { |
| 56 |
if ( Snippet_Files::is_active() ) { |
| 57 |
if ( is_null( $this->active_snippets ) ) { |
| 58 |
$this->populate_active_snippets_from_flat_files(); |
| 59 |
} |
| 60 |
|
| 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' ]; |
| 68 |
|
| 69 |
if ( is_null( $this->active_snippets ) ) { |
| 70 |
$this->active_snippets = $this->db->fetch_active_snippets( $scopes ); |
| 71 |
} |
| 72 |
|
| 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 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Print head content snippets. |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function load_head_content() { |
| 88 |
$this->print_content_snippets( 'head-content' ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Print body content snippets (requires theme to call wp_body_open). |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function load_body_content() { |
| 97 |
$this->print_content_snippets( 'body-content' ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Print footer content snippets. |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function load_footer_content() { |
| 106 |
$this->print_content_snippets( 'footer-content' ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Populate the active snippets from flat files. |
| 111 |
* |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
private function populate_active_snippets_from_flat_files() { |
| 115 |
$handler = code_snippets()->snippet_handler_registry->get_handler( 'html' ); |
| 116 |
$dir_name = $handler->get_dir_name(); |
| 117 |
$ext = $handler->get_file_extension(); |
| 118 |
|
| 119 |
$scopes = [ 'head-content', 'body-content', 'footer-content' ]; |
| 120 |
$all_snippets = Snippet_Files::get_active_snippets_from_flat_files( $scopes, $dir_name ); |
| 121 |
|
| 122 |
foreach ( $all_snippets as $snippet ) { |
| 123 |
$scope = $snippet['scope']; |
| 124 |
|
| 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; |
| 129 |
|
| 130 |
$this->active_snippets[ $scope ][] = $snippet; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|