| 1 |
<?php |
| 2 |
|
| 3 |
use WPCoder\Dashboard\DBManager; |
| 4 |
use WPCoder\Dashboard\FolderManager; |
| 5 |
use WPCoder\WPCoder; |
| 6 |
|
| 7 |
defined( 'ABSPATH' ) || exit; |
| 8 |
|
| 9 |
class WPCoder_Lite_Tools { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var false|mixed|null |
| 13 |
*/ |
| 14 |
private $options; |
| 15 |
|
| 16 |
public function __construct() { |
| 17 |
$options = get_option( '_wp_coder_tools', [] ); |
| 18 |
$this->options = $options; |
| 19 |
|
| 20 |
if ( array_key_exists( 'enable_tracking_tool', $options ) ) { |
| 21 |
add_action( 'wp_head', [ $this, 'add_tracking' ] ); |
| 22 |
} |
| 23 |
|
| 24 |
if ( array_key_exists( 'enable_google_adsense', $options ) ) { |
| 25 |
add_action( 'wp_head', array( $this, 'add_adsense_code' ) ); |
| 26 |
} |
| 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 |
|
| 51 |
} |
| 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 |
} |
| 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 |
|
| 79 |
public function add_tracking(): void { |
| 80 |
|
| 81 |
$user = wp_get_current_user(); |
| 82 |
|
| 83 |
if ( ! empty( $user->roles ) ) { |
| 84 |
foreach ( $user->roles as $role ) { |
| 85 |
if ( array_key_exists( 'disable_tracking_tool_user_' . $role, $this->options ) ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// Google Analytics code |
| 92 |
if ( ! empty( $this->options['tracking_tool_google'] ) ) { ?> |
| 93 |
<script async |
| 94 |
src="https://www.googletagmanager.com/gtag/js?id=<?php echo esc_attr( $this->options['tracking_tool_google'] ); ?>"></script> |
| 95 |
<script> |
| 96 |
window.dataLayer = window.dataLayer || []; |
| 97 |
|
| 98 |
function gtag() { |
| 99 |
dataLayer.push(arguments); |
| 100 |
} |
| 101 |
|
| 102 |
gtag("js", new Date()); |
| 103 |
gtag("config", "<?php echo esc_attr( $this->options['tracking_tool_google'] ); ?>"); |
| 104 |
</script> |
| 105 |
<?php } |
| 106 |
|
| 107 |
// Facebook Pixel code |
| 108 |
if ( ! empty( $this->options['tracking_tool_facebook'] ) ) { ?> |
| 109 |
<script> |
| 110 |
!function (f, b, e, v, n, t, s) { |
| 111 |
if (f.fbq) return; |
| 112 |
n = f.fbq = function () { |
| 113 |
n.callMethod ? |
| 114 |
n.callMethod.apply(n, arguments) : n.queue.push(arguments) |
| 115 |
}; |
| 116 |
if (!f._fbq) f._fbq = n; |
| 117 |
n.push = n; |
| 118 |
n.loaded = !0; |
| 119 |
n.version = '2.0'; |
| 120 |
n.queue = []; |
| 121 |
t = b.createElement(e); |
| 122 |
t.async = !0; |
| 123 |
t.src = v; |
| 124 |
s = b.getElementsByTagName(e)[0]; |
| 125 |
s.parentNode.insertBefore(t, s) |
| 126 |
}(window, document, 'script', |
| 127 |
'https://connect.facebook.net/en_US/fbevents.js'); |
| 128 |
fbq('init', '<?php echo esc_attr( $this->options['tracking_tool_facebook'] ); ?>'); |
| 129 |
fbq('track', 'PageView'); |
| 130 |
</script> |
| 131 |
<noscript> |
| 132 |
<img height="1" width="1" style="display:none" |
| 133 |
src="https://www.facebook.com/tr?id=<?php echo esc_attr( $this->options['tracking_tool_facebook'] ); ?>&ev=PageView&noscript=1"/> |
| 134 |
</noscript> |
| 135 |
<?php } |
| 136 |
|
| 137 |
// Pintrest Pixel code |
| 138 |
if ( ! empty( $this->options['tracking_tool_pintrest'] ) ) { ?> |
| 139 |
<script type="text/javascript"> |
| 140 |
!function (e) { |
| 141 |
if (!window.pintrk) { |
| 142 |
window.pintrk = function () { |
| 143 |
window.pintrk.queue.push(Array.prototype.slice.call(arguments)) |
| 144 |
}; |
| 145 |
var n = window.pintrk; |
| 146 |
n.queue = [], n.version = "3.0"; |
| 147 |
var t = document.createElement("script"); |
| 148 |
t.async = !0, t.src = e; |
| 149 |
var r = document.getElementsByTagName("script")[0]; |
| 150 |
r.parentNode.insertBefore(t, r) |
| 151 |
} |
| 152 |
}("https://s.pinimg.com/ct/core.js"); |
| 153 |
pintrk('load', '<?php echo esc_attr( $this->options['tracking_tool_pintrest'] ); ?>'); |
| 154 |
pintrk('page'); |
| 155 |
</script> |
| 156 |
<noscript> |
| 157 |
<img height="1" width="1" style="display:none;" alt="" |
| 158 |
src="https://ct.pinterest.com/v3/?tid=<?php echo esc_attr( $this->options['tracking_tool_pintrest'] ); ?>&noscript=1"/> |
| 159 |
</noscript> |
| 160 |
<?php } |
| 161 |
} |
| 162 |
|
| 163 |
public function add_adsense_code(): void { |
| 164 |
|
| 165 |
if ( empty( $this->options['google_adsense_publisher'] ) ) { |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
$user = wp_get_current_user(); |
| 170 |
|
| 171 |
if ( ! empty( $user->roles ) ) { |
| 172 |
foreach ( $user->roles as $role ) { |
| 173 |
if ( array_key_exists( 'disable_google_adsense_user_' . $role, $this->options ) ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 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 |
| 235 |
} |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
new WPCoder_Lite_Tools; |