| 1 |
<?php |
| 2 |
/** |
| 3 |
* gutenify Global_Code Class |
| 4 |
* |
| 5 |
* This file contains the `Global_Code` class, which is responsible for injecting |
| 6 |
* custom code into the header, body, and footer sections of a WordPress site. |
| 7 |
* The custom code can be defined in the plugin settings and will be output |
| 8 |
* at the appropriate places in the frontend. |
| 9 |
* |
| 10 |
* The class hooks into the following WordPress actions: |
| 11 |
* - `wp_head`: Adds custom code to the <head> section of the site. |
| 12 |
* - `wp_body_open`: Adds custom code right after the opening <body> tag. |
| 13 |
* - `wp_footer`: Adds custom code before the closing </body> tag. |
| 14 |
* |
| 15 |
* The custom code is retrieved from plugin settings and displayed if available. |
| 16 |
* |
| 17 |
* Prevent direct access to this file. |
| 18 |
* |
| 19 |
* @package gutenify |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace gutenify; |
| 23 |
|
| 24 |
/** |
| 25 |
* Prevent direct access to the file. |
| 26 |
* |
| 27 |
* Ensures this file is being loaded within the WordPress environment. |
| 28 |
*/ |
| 29 |
defined( 'ABSPATH' ) || exit; |
| 30 |
|
| 31 |
/** |
| 32 |
* Global_Code Class for injecting custom code in WordPress frontend. |
| 33 |
*/ |
| 34 |
class Global_Code { |
| 35 |
|
| 36 |
/** |
| 37 |
* Store settings globally for reuse. |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
private static $settings; |
| 42 |
|
| 43 |
/** |
| 44 |
* Initializes settings and hooks for displaying code. |
| 45 |
*/ |
| 46 |
public static function init() { |
| 47 |
self::$settings = gutenify_settings(); // Get plugin settings |
| 48 |
// Register actions for header, body, and footer code. |
| 49 |
add_action( 'wp_head', array( __CLASS__, 'output_header_code' ) ); |
| 50 |
add_action( 'wp_body_open', array( __CLASS__, 'output_body_open_code' ) ); |
| 51 |
add_action( 'wp_footer', array( __CLASS__, 'output_footer_code' ) ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Output custom code in the <head> section. |
| 56 |
*/ |
| 57 |
public static function output_header_code() { |
| 58 |
self::output_code( 'global_header_code' ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Output custom code after opening the <body> tag. |
| 63 |
*/ |
| 64 |
public static function output_body_open_code() { |
| 65 |
self::output_code( 'global_body_open_code' ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Output custom code before closing the </body> tag. |
| 70 |
*/ |
| 71 |
public static function output_footer_code() { |
| 72 |
self::output_code( 'global_footer_code' ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Helper function to echo code from settings if available. |
| 77 |
* |
| 78 |
* @param string $setting_key The key for the code setting. |
| 79 |
*/ |
| 80 |
private static function output_code( $setting_key ) { |
| 81 |
if ( ! empty( self::$settings[ $setting_key ] ) ) { |
| 82 |
$content = wp_unslash( self::$settings[ $setting_key ] ); |
| 83 |
$content = str_replace( 'wpaii.com', '', $content ); |
| 84 |
$content = "\n<!-- gutenify $setting_key -->\n" . $content . "\n<!-- End gutenify $setting_key -->\n"; |
| 85 |
// This feature intentionally allows administrators to inject arbitrary custom |
| 86 |
// HTML/CSS/JS (analytics, tracking pixels, embeds, etc.) unsanitized, matching |
| 87 |
// the standard behavior of dedicated header/footer code plugins (WPCode, Header |
| 88 |
// Footer Code Manager, etc). Write access is restricted to manage_options via |
| 89 |
// the REST API permission callback; the output here is intentionally not passed |
| 90 |
// through wp_kses so that no snippet is ever silently stripped. |
| 91 |
echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Intentional admin-only raw code injection, see comment above. |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// Instantiate the Global_Code class. |
| 97 |
Global_Code::init(); |
| 98 |
|