PluginProbe
WP Coder – Insert & Manage Code Snippets / 4.2
WP Coder – Insert & Manage Code Snippets v4.2
4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 3.5.1 All 39 releases
wp-coder / includes / snippets / class-tools.php

class-tools.php in WP Coder – Insert & Manage Code Snippets 4.2, at includes/snippets/class-tools.php

169 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $enabled = (bool) get_option( '_wpcoder_enable_debug_log' );
29 if ( $enabled ) {
30 add_action( 'plugins_loaded', [ $this, 'debug_log' ] );
31 add_action( 'admin_bar_menu', [ $this, 'admin_menu_debug' ], 90 );
32 }
33 }
34
35 public function debug_log(): void {
36 error_reporting( E_ALL );
37 ini_set( 'log_errors', 1 );
38 ini_set( 'error_log', WP_CONTENT_DIR . '/debug.log' ); // Log file path
39 }
40
41 public function admin_menu_debug( $wp_admin_bar ): void {
42 if ( ! current_user_can( 'unfiltered_html' ) ) {
43 return;
44 }
45
46 $log_file = WP_CONTENT_DIR . '/debug.log';
47 if ( ! file_exists( $log_file ) || filesize( $log_file ) === 0 ) {
48 return;
49 }
50
51 $wp_admin_bar->add_node( [
52 'id' => 'wpcoder-debug-log-link',
53 'title' => '<span class="ab-icon dashicons-admin-tools" aria-hidden="true"></span><span class="ab-label">Debug Log</span>',
54 'href' => admin_url( 'admin.php?page=' . WPCoder::SLUG . '-debug' ),
55 'meta' => [
56 'title' => 'View Debug Log', // tooltip
57 ],
58 ] );
59 }
60
61 public function add_tracking(): void {
62
63 $user = wp_get_current_user();
64
65 if ( ! empty( $user->roles ) ) {
66 foreach ( $user->roles as $role ) {
67 if ( array_key_exists( 'disable_tracking_tool_user_' . $role, $this->options ) ) {
68 return;
69 }
70 }
71 }
72
73 // Google Analytics code
74 if ( ! empty( $this->options['tracking_tool_google'] ) ) { ?>
75 <script async
76 src="https://www.googletagmanager.com/gtag/js?id=<?php echo esc_attr( $this->options['tracking_tool_google'] ); ?>"></script>
77 <script>
78 window.dataLayer = window.dataLayer || [];
79
80 function gtag() {
81 dataLayer.push(arguments);
82 }
83
84 gtag("js", new Date());
85 gtag("config", "<?php echo esc_attr( $this->options['tracking_tool_google'] ); ?>");
86 </script>
87 <?php }
88
89 // Facebook Pixel code
90 if ( ! empty( $this->options['tracking_tool_facebook'] ) ) { ?>
91 <script>
92 !function (f, b, e, v, n, t, s) {
93 if (f.fbq) return;
94 n = f.fbq = function () {
95 n.callMethod ?
96 n.callMethod.apply(n, arguments) : n.queue.push(arguments)
97 };
98 if (!f._fbq) f._fbq = n;
99 n.push = n;
100 n.loaded = !0;
101 n.version = '2.0';
102 n.queue = [];
103 t = b.createElement(e);
104 t.async = !0;
105 t.src = v;
106 s = b.getElementsByTagName(e)[0];
107 s.parentNode.insertBefore(t, s)
108 }(window, document, 'script',
109 'https://connect.facebook.net/en_US/fbevents.js');
110 fbq('init', '<?php echo esc_attr( $this->options['tracking_tool_facebook'] ); ?>');
111 fbq('track', 'PageView');
112 </script>
113 <noscript>
114 <img height="1" width="1" style="display:none"
115 src="https://www.facebook.com/tr?id=<?php echo esc_attr( $this->options['tracking_tool_facebook'] ); ?>&ev=PageView&noscript=1"/>
116 </noscript>
117 <?php }
118
119 // Pintrest Pixel code
120 if ( ! empty( $this->options['tracking_tool_pintrest'] ) ) { ?>
121 <script type="text/javascript">
122 !function (e) {
123 if (!window.pintrk) {
124 window.pintrk = function () {
125 window.pintrk.queue.push(Array.prototype.slice.call(arguments))
126 };
127 var n = window.pintrk;
128 n.queue = [], n.version = "3.0";
129 var t = document.createElement("script");
130 t.async = !0, t.src = e;
131 var r = document.getElementsByTagName("script")[0];
132 r.parentNode.insertBefore(t, r)
133 }
134 }("https://s.pinimg.com/ct/core.js");
135 pintrk('load', '<?php echo esc_attr( $this->options['tracking_tool_pintrest'] ); ?>');
136 pintrk('page');
137 </script>
138 <noscript>
139 <img height="1" width="1" style="display:none;" alt=""
140 src="https://ct.pinterest.com/v3/?tid=<?php echo esc_attr( $this->options['tracking_tool_pintrest'] ); ?>&noscript=1"/>
141 </noscript>
142 <?php }
143 }
144
145 public function add_adsense_code(): void {
146
147 if ( empty( $this->options['google_adsense_publisher'] ) ) {
148 return;
149 }
150
151 $user = wp_get_current_user();
152
153 if ( ! empty( $user->roles ) ) {
154 foreach ( $user->roles as $role ) {
155 if ( array_key_exists( 'disable_google_adsense_user_' . $role, $this->options ) ) {
156 return;
157 }
158 }
159 }
160
161 // phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript
162 echo '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-' . esc_attr( $this->options['google_adsense_publisher'] ) . '" crossorigin="anonymous"></script>';
163 // phpcs:enable
164 }
165
166
167 }
168
169 new WPCoder_Lite_Tools;