1212
1313class Script_Loader
1414{
15- public function __construct ()
16- {
17- add_action ('wp_enqueue_scripts ' , [$ this , 'maybe_enqueue_script ' ], 10 , 0 );
18- add_action ('amp_print_analytics ' , [$ this , 'print_amp_analytics_tag ' ], 10 , 0 );
19- }
20-
2115 /**
2216 * @param bool $echo Whether to use the default WP script enqueue method or print the script tag directly
2317 */
24- public function maybe_enqueue_script (bool $ echo = false )
18+ public static function maybe_enqueue_script (bool $ echo = false )
2519 {
2620 $ load_script = apply_filters ('koko_analytics_load_tracking_script ' , true );
2721 if (! $ load_script ) {
@@ -33,20 +27,20 @@ public function maybe_enqueue_script(bool $echo = false)
3327 }
3428
3529 // TODO: Handle "term" requests so we track both terms and post types.
36- add_filter ('script_loader_tag ' , [ $ this , 'add_async_attribute ' ], 20 , 2 );
30+ add_filter ('script_loader_tag ' , [ Script_Loader::class , 'add_async_attribute ' ], 20 , 2 );
3731
3832 if (false === $ echo ) {
3933 // Print configuration object early on in the HTML so scripts can modify it
4034 if (did_action ('wp_head ' )) {
41- $ this -> print_js_object ();
35+ self :: print_js_object ();
4236 } else {
43- add_action ('wp_head ' , [ $ this , 'print_js_object ' ], 1 , 0 );
37+ add_action ('wp_head ' , [ Script_Loader::class , 'print_js_object ' ], 1 , 0 );
4438 }
4539
4640 // Enqueue the actual tracking script (in footer, if possible)
4741 wp_enqueue_script ('koko-analytics ' , plugins_url ('assets/dist/js/script.js ' , KOKO_ANALYTICS_PLUGIN_FILE ), [], KOKO_ANALYTICS_VERSION , true );
4842 } else {
49- $ this -> print_js_object ();
43+ self :: print_js_object ();
5044 echo '<script defer src=" ' , plugins_url ('assets/dist/js/script.js?ver= ' . KOKO_ANALYTICS_VERSION , KOKO_ANALYTICS_PLUGIN_FILE ), '"></script> ' ;
5145 }
5246 }
@@ -57,7 +51,7 @@ public function maybe_enqueue_script(bool $echo = false)
5751 *
5852 * @return int
5953 */
60- private function get_post_id (): int
54+ private static function get_post_id (): int
6155 {
6256 if (is_singular ()) {
6357 return get_queried_object_id ();
@@ -70,7 +64,7 @@ private function get_post_id(): int
7064 return -1 ;
7165 }
7266
73- private function get_tracker_url (): string
67+ private static function get_tracker_url (): string
7468 {
7569 if (defined ('KOKO_ANALYTICS_CUSTOM_ENDPOINT ' ) && KOKO_ANALYTICS_CUSTOM_ENDPOINT ) {
7670 return site_url (KOKO_ANALYTICS_CUSTOM_ENDPOINT );
@@ -81,37 +75,37 @@ private function get_tracker_url(): string
8175 return using_custom_endpoint () ? site_url ('/koko-analytics-collect.php ' ) : admin_url ('admin-ajax.php?action=koko_analytics_collect ' );
8276 }
8377
84- private function get_cookie_path (): string
78+ private static function get_cookie_path (): string
8579 {
8680 $ home_url = home_url ();
8781 return parse_url ($ home_url , PHP_URL_PATH ) ?? '/ ' ;
8882 }
8983
90- public function print_js_object ()
84+ public static function print_js_object ()
9185 {
9286 $ settings = get_settings ();
9387 $ script_config = [
9488 // the URL of the tracking endpoint
95- 'url ' => $ this -> get_tracker_url (),
89+ 'url ' => self :: get_tracker_url (),
9690 'site_url ' => get_home_url (),
9791
9892 // ID of the current post (or -1 in case of non-singular type)
99- 'post_id ' => $ this -> get_post_id (),
93+ 'post_id ' => self :: get_post_id (),
10094
10195 // wether to set a cookie
10296 'use_cookie ' => (int ) $ settings ['use_cookie ' ],
10397
10498 // path to store the cookie in (will be subdirectory if website root is in subdirectory)
105- 'cookie_path ' => $ this -> get_cookie_path (),
99+ 'cookie_path ' => self :: get_cookie_path (),
106100 ];
107101 echo '<script>window.koko_analytics = ' , json_encode ($ script_config ), ';</script> ' ;
108102 }
109103
110- public function print_amp_analytics_tag ()
104+ public static function print_amp_analytics_tag ()
111105 {
112106 $ settings = get_settings ();
113- $ post_id = $ this -> get_post_id ();
114- $ tracker_url = $ this -> get_tracker_url ();
107+ $ post_id = self :: get_post_id ();
108+ $ tracker_url = self :: get_tracker_url ();
115109 $ posts_viewed = isset ($ _COOKIE ['_koko_analytics_pages_viewed ' ]) ? explode (', ' , $ _COOKIE ['_koko_analytics_pages_viewed ' ]) : [];
116110 $ data = [
117111 'sc ' => $ settings ['use_cookie ' ], // inform tracker endpoint to set cookie server-side
@@ -139,7 +133,7 @@ public function print_amp_analytics_tag()
139133 * @param string $tag
140134 * @param string $handle
141135 */
142- public function add_async_attribute ($ tag , $ handle )
136+ public static function add_async_attribute ($ tag , $ handle )
143137 {
144138 if ($ handle !== 'koko-analytics ' || strpos ($ tag , ' defer ' ) !== false ) {
145139 return $ tag ;
0 commit comments