| @@ -24,11 +24,59 @@ | ||
| 24 | 24 | if ( array_key_exists( 'enable_google_adsense', $options ) ) { |
| 25 | 25 | add_action( 'wp_head', array( $this, 'add_adsense_code' ) ); |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | + if ( array_key_exists( 'enable_gtm', $options ) ) { | |
| 29 | + add_action( 'wp_head', [ $this, 'add_gtm_head' ], 1 ); | |
| 30 | + add_action( 'wp_body_open', [ $this, 'add_gtm_body' ], 1 ); | |
| 31 | + } | |
| 32 | + | |
| 33 | + $enabled = (bool) get_option( '_wpcoder_enable_debug_log' ); | |
| 34 | + if ( $enabled ) { | |
| 35 | + add_action( 'plugins_loaded', [ $this, 'debug_log' ] ); | |
| 36 | + add_action( 'admin_bar_menu', [ $this, 'admin_menu_debug' ], 90 ); | |
| 37 | + } | |
| 38 | + | |
| 39 | + if ( array_key_exists( 'show_page_debug_info', $options ) ) { | |
| 40 | + new WPCoder_Page_Info; | |
| 41 | + } | |
| 42 | + | |
| 43 | + if ( array_key_exists( 'theme_switcher', $options ) ) { | |
| 44 | + new WPCoder_Theme_Switcher; | |
| 45 | + } | |
| 46 | + | |
| 47 | + if ( array_key_exists( 'markdown_editor', $options ) ) { | |
| 48 | + new WPCoder_Markdown_Editor($options); | |
| 49 | + } | |
| 50 | + | |
| 28 | 51 | } |
| 29 | 52 | |
| 53 | + public function debug_log(): void { | |
| 54 | + error_reporting( E_ALL ); | |
| 55 | + ini_set( 'log_errors', 1 ); | |
| 56 | + ini_set( 'error_log', WP_CONTENT_DIR . '/debug.log' ); // Log file path | |
| 57 | + } | |
| 30 | 58 | |
| 59 | + public function admin_menu_debug( $wp_admin_bar ): void { | |
| 60 | + if ( ! current_user_can( 'unfiltered_html' ) ) { | |
| 61 | + return; | |
| 62 | + } | |
| 63 | + | |
| 64 | + $log_file = WP_CONTENT_DIR . '/debug.log'; | |
| 65 | + if ( ! file_exists( $log_file ) || filesize( $log_file ) === 0 ) { | |
| 66 | + return; | |
| 67 | + } | |
| 68 | + | |
| 69 | + $wp_admin_bar->add_node( [ | |
| 70 | + 'id' => 'wpcoder-debug-log-link', | |
| 71 | + 'title' => '<span class="ab-icon dashicons-admin-tools" aria-hidden="true"></span><span class="ab-label">Debug Log</span>', | |
| 72 | + 'href' => admin_url( 'admin.php?page=' . WPCoder::SLUG . '-debug' ), | |
| 73 | + 'meta' => [ | |
| 74 | + 'title' => 'View Debug Log', // tooltip | |
| 75 | + ], | |
| 76 | + ] ); | |
| 77 | + } | |
| 78 | + | |
| 31 | 79 | public function add_tracking(): void { |
| 32 | 80 | |
| 33 | 81 | $user = wp_get_current_user(); |
| 34 | 82 | |
| @@ -111,13 +159,13 @@ | ||
| 111 | 159 | </noscript> |
| 112 | 160 | <?php } |
| 113 | 161 | } |
| 114 | 162 | |
| 115 | - public function add_adsense_code() { | |
| 163 | + public function add_adsense_code(): void { | |
| 116 | 164 | |
| 117 | - if(empty( $this->options['google_adsense_publisher'] )) { | |
| 118 | - return; | |
| 119 | - } | |
| 165 | + if ( empty( $this->options['google_adsense_publisher'] ) ) { | |
| 166 | + return; | |
| 167 | + } | |
| 120 | 168 | |
| 121 | 169 | $user = wp_get_current_user(); |
| 122 | 170 | |
| 123 | 171 | if ( ! empty( $user->roles ) ) { |
| @@ -127,11 +175,65 @@ | ||
| 127 | 175 | } |
| 128 | 176 | } |
| 129 | 177 | } |
| 130 | 178 | |
| 131 | - $output = '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-'.esc_attr($this->options['google_adsense_publisher']).'" crossorigin="anonymous"></script>'; | |
| 132 | - echo $output; | |
| 179 | + // phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript | |
| 180 | + echo '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-' . esc_attr( $this->options['google_adsense_publisher'] ) . '" crossorigin="anonymous"></script>'; | |
| 181 | + // phpcs:enable | |
| 182 | + } | |
| 183 | + | |
| 184 | + public function add_gtm_head(): void { | |
| 185 | + | |
| 186 | + if ( empty( $this->options['gtm_container_id'] ) ) { | |
| 187 | + return; | |
| 188 | + } | |
| 189 | + | |
| 190 | + $user = wp_get_current_user(); | |
| 191 | + | |
| 192 | + if ( ! empty( $user->roles ) ) { | |
| 193 | + foreach ( $user->roles as $role ) { | |
| 194 | + if ( array_key_exists( 'disable_gtm_user_' . $role, $this->options ) ) { | |
| 195 | + return; | |
| 196 | + } | |
| 197 | + } | |
| 198 | + } | |
| 199 | + | |
| 200 | + $container_id = esc_attr( $this->options['gtm_container_id'] ); | |
| 201 | + ?> | |
| 202 | + <!-- Google Tag Manager --> | |
| 203 | + <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': | |
| 204 | + new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], | |
| 205 | + j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= | |
| 206 | + 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); | |
| 207 | + })(window,document,'script','dataLayer','<?php echo $container_id; ?>');</script> | |
| 208 | + <!-- End Google Tag Manager --> | |
| 209 | + <?php | |
| 210 | + } | |
| 211 | + | |
| 212 | + public function add_gtm_body(): void { | |
| 213 | + | |
| 214 | + if ( empty( $this->options['gtm_container_id'] ) ) { | |
| 215 | + return; | |
| 216 | + } | |
| 217 | + | |
| 218 | + $user = wp_get_current_user(); | |
| 219 | + | |
| 220 | + if ( ! empty( $user->roles ) ) { | |
| 221 | + foreach ( $user->roles as $role ) { | |
| 222 | + if ( array_key_exists( 'disable_gtm_user_' . $role, $this->options ) ) { | |
| 223 | + return; | |
| 224 | + } | |
| 225 | + } | |
| 226 | + } | |
| 227 | + | |
| 228 | + $container_id = esc_attr( $this->options['gtm_container_id'] ); | |
| 229 | + ?> | |
| 230 | + <!-- Google Tag Manager (noscript) --> | |
| 231 | + <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<?php echo $container_id; ?>" | |
| 232 | + height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> | |
| 233 | + <!-- End Google Tag Manager (noscript) --> | |
| 234 | + <?php | |
| 133 | 235 | } |
| 134 | 236 | |
| 135 | 237 | } |
| 136 | 238 | |
| 137 | 239 | new WPCoder_Lite_Tools; |