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