PluginProbe
WP EXtra – One Click Optimize / trunk
WP EXtra – One Click Optimize vtrunk
8.7.1 8.6.8 8.7.0 trunk 5.9 8.0 8.5.0 8.5.4 8.5.5 8.6.0 8.6.1 8.6.2 8.6.3 8.6.5
wp-extra / src / Modules / Frontend / Code.php

Code.php in WP EXtra – One Click Optimize trunk, at src/Modules/Frontend/Code.php

116 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WPEXtra\Modules\Frontend;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 use WPEXtra\Settings;
9 use WPEXtra\Helper;
10 use WPEXtra\Base;
11
12 class Code extends Base {
13
14 private static $css_hooked = false;
15
16 public function __construct() {
17 parent::__construct();
18 }
19
20 protected $features = [
21 'code_header',
22 'code_body',
23 'code_footer',
24 'css_all',
25 'css_tablet',
26 'css_mobile',
27 ];
28
29 public function code_header() {
30 add_action('wp_head', [$this, 'insertHeaderCode']);
31 }
32
33 public function insertHeaderCode() {
34 if (!is_admin()) {
35 $code = Helper::get_option('code_header');
36 if (!empty($code)) {
37 echo wp_unslash($code) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
38 }
39 }
40 }
41
42 public function code_body() {
43 if (function_exists('wp_body_open')) {
44 add_action('wp_body_open', [$this, 'insertBodyCode']);
45 }
46 }
47
48 public function insertBodyCode() {
49 if (!is_admin()) {
50 $code = Helper::get_option('code_body');
51 if (!empty($code)) {
52 echo wp_unslash($code) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
53 }
54 }
55 }
56
57 public function code_footer() {
58 add_action('wp_footer', [$this, 'insertFooterCode']);
59 }
60
61 public function insertFooterCode() {
62 if (!is_admin()) {
63 $code = Helper::get_option('code_footer');
64 if (!empty($code)) {
65 echo wp_unslash($code) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
66 }
67 }
68 }
69
70 public function css_all() {
71 $this->hook_custom_css();
72 }
73
74 public function css_tablet() {
75 $this->hook_custom_css();
76 }
77
78 public function css_mobile() {
79 $this->hook_custom_css();
80 }
81
82 private function hook_custom_css() {
83 if (!self::$css_hooked) {
84 self::$css_hooked = true;
85 add_action('wp_head', [$this, 'renderCustomCSS'], 100);
86 }
87 }
88
89 public function renderCustomCSS() {
90 if (is_admin()) {
91 return;
92 }
93
94 $all_css = Helper::get_option('css_all');
95 $tablet_css = Helper::get_option('css_tablet');
96 $mobile_css = Helper::get_option('css_mobile');
97
98 $output = '';
99
100 if (!empty($all_css)) {
101 $output .= wp_strip_all_tags($all_css) . "\n";
102 }
103 if (!empty($tablet_css)) {
104 $output .= '@media (max-width: 1024px) {' . wp_strip_all_tags($tablet_css) . "}\n";
105 }
106 if (!empty($mobile_css)) {
107 $output .= '@media (max-width: 767px) {' . wp_strip_all_tags($mobile_css) . "}\n";
108 }
109
110 $output = trim($output);
111 if (!empty($output)) {
112 echo '<style id="wpex-custom-css" type="text/css">' . Helper::minifyCSS($output) . '</style>' . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
113 }
114 }
115 }
116