| 1 |
<?php |
| 2 |
|
| 3 |
namespace Evaluation; |
| 4 |
|
| 5 |
use Code_Snippets\DB; |
| 6 |
use Code_Snippets\Snippet; |
| 7 |
use Code_Snippets\Settings; |
| 8 |
use Code_Snippets\Snippet_Files; |
| 9 |
use function Code_Snippets\code_snippets; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class for evaluating content snippets. |
| 13 |
* |
| 14 |
* @package Code_Snippets |
| 15 |
*/ |
| 16 |
class Evaluate_Content { |
| 17 |
|
| 18 |
/** |
| 19 |
* Database class. |
| 20 |
* |
| 21 |
* @var DB |
| 22 |
*/ |
| 23 |
private DB $db; |
| 24 |
|
| 25 |
/** |
| 26 |
* Cached list of active snippets. |
| 27 |
* |
| 28 |
* @var ?Snippet[] |
| 29 |
*/ |
| 30 |
private ?array $active_snippets = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* Class constructor. |
| 34 |
* |
| 35 |
* @param DB $db Database class instance. |
| 36 |
*/ |
| 37 |
public function __construct( DB $db ) { |
| 38 |
$this->db = $db; |
| 39 |
add_action( 'init', array( $this, 'init' ) ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Initialise class functions. |
| 44 |
*/ |
| 45 |
public function init() { |
| 46 |
if ( Snippet_Files::is_active() ) { |
| 47 |
add_action( 'wp_head', [ $this, 'load_head_content_from_flat_files' ] ); |
| 48 |
add_action( 'wp_footer', [ $this, 'load_footer_content_from_flat_files' ] ); |
| 49 |
} else { |
| 50 |
add_action( 'wp_head', [ $this, 'load_head_content' ] ); |
| 51 |
add_action( 'wp_footer', [ $this, 'load_footer_content' ] ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Print snippet code fetched from the database from a certain scope. |
| 57 |
* |
| 58 |
* @param string $scope Name of scope to print. |
| 59 |
*/ |
| 60 |
private function print_content_snippets( string $scope ) { |
| 61 |
$scopes = [ 'head-content', 'footer-content' ]; |
| 62 |
|
| 63 |
if ( is_null( $this->active_snippets ) ) { |
| 64 |
$this->active_snippets = $this->db->fetch_active_snippets( $scopes ); |
| 65 |
} |
| 66 |
|
| 67 |
foreach ( $this->active_snippets as $snippet ) { |
| 68 |
if ( $scope === $snippet['scope'] ) { |
| 69 |
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 70 |
echo "\n", $snippet['code'], "\n"; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Print head content snippets. |
| 77 |
*/ |
| 78 |
public function load_head_content() { |
| 79 |
$this->print_content_snippets( 'head-content' ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Print footer content snippets. |
| 84 |
*/ |
| 85 |
public function load_footer_content() { |
| 86 |
$this->print_content_snippets( 'footer-content' ); |
| 87 |
} |
| 88 |
|
| 89 |
public function load_head_content_from_flat_files() { |
| 90 |
$this->load_content_snippets_from_flat_files( 'head-content' ); |
| 91 |
} |
| 92 |
|
| 93 |
public function load_footer_content_from_flat_files() { |
| 94 |
$this->load_content_snippets_from_flat_files( 'footer-content' ); |
| 95 |
} |
| 96 |
|
| 97 |
private function populate_active_snippets_from_flat_files() { |
| 98 |
$handler = code_snippets()->snippet_handler_registry->get_handler( 'html' ); |
| 99 |
$dir_name = $handler->get_dir_name(); |
| 100 |
$ext = $handler->get_file_extension(); |
| 101 |
|
| 102 |
$scopes = [ 'head-content', 'footer-content' ]; |
| 103 |
$all_snippets = Snippet_Files::get_active_snippets_from_flat_files( $scopes, $dir_name ); |
| 104 |
|
| 105 |
foreach ( $all_snippets as $snippet ) { |
| 106 |
$scope = $snippet['scope']; |
| 107 |
|
| 108 |
// Add file path information to the snippet for later use |
| 109 |
$table_name = Snippet_Files::get_hashed_table_name( $snippet['table'] ); |
| 110 |
$base_path = Snippet_Files::get_base_dir( $table_name, $dir_name ); |
| 111 |
$snippet['file_path'] = $base_path . '/' . $snippet['id'] . '.' . $ext; |
| 112 |
|
| 113 |
$this->active_snippets[ $scope ][] = $snippet; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
private function load_content_snippets_from_flat_files( string $scope ) { |
| 118 |
if ( is_null( $this->active_snippets ) ) { |
| 119 |
$this->populate_active_snippets_from_flat_files(); |
| 120 |
} |
| 121 |
|
| 122 |
if ( ! isset( $this->active_snippets[ $scope ] ) ) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
foreach ( $this->active_snippets[ $scope ] as $snippet ) { |
| 127 |
require_once $snippet['file_path']; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|