Skip to content

Commit d48c95d

Browse files
lazy load script loader
1 parent ccc1f5f commit d48c95d

File tree

3 files changed

+28
-31
lines changed

3 files changed

+28
-31
lines changed

autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
'KokoAnalytics\\Notice_Pro' => '/src/class-notice-pro.php',
1919
'KokoAnalytics\\Pageview_Aggregator' => '/src/class-pageview-aggregator.php',
2020
'KokoAnalytics\\Rest' => '/src/class-rest.php',
21+
'KokoAnalytics\\Script_Loader' => '/src/class-script-loader.php',
2122
'KokoAnalytics\\Stats' => '/src/class-stats.php',
2223
'KokoAnalytics\\Shortcode_Most_Viewed_Posts' => '/src/class-shortcode-most-viewed-posts.php',
2324
'KokoAnalytics\\Shortcode_Site_Counter' => '/src/class-shortcode-site-counter.php',
2425
'KokoAnalytics\\Stats' => '/src/class-stats.php',
25-
'KokoAnalytics\\Widget_Most_Viewed_Posts' => '/src/class-widget-most-viewed-posts.php',
2626
];
2727

2828
if (isset($classmap[$class])) {

koko-analytics.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,15 @@
7171
// wp-admin only
7272
new Admin();
7373
new Dashboard_Widget();
74-
} else {
75-
// frontend only
76-
require __DIR__ . '/src/class-script-loader.php';
77-
new Script_Loader();
78-
add_action('admin_bar_menu', 'KokoAnalytics\admin_bar_menu', 40, 1);
79-
$times[] = ['Script_Loader', microtime(true)];
8074
}
8175

76+
// script loader
77+
add_action('wp_enqueue_scripts', [Script_Loader::class, 'maybe_enqueue_script'], 10, 0);
78+
add_action('amp_print_analytics', [Script_Loader::class, 'print_amp_analytics_tag'], 10, 0);
79+
add_action('admin_bar_menu', 'KokoAnalytics\admin_bar_menu', 40, 1);
80+
$times[] = ['Script_Loader', microtime(true)];
81+
82+
// query loop block
8283
require __DIR__ . '/src/class-query-loop-block.php';
8384
new QueryLoopBlock();
8485
$times[] = ['QueryLoopBlock', microtime(true)];
@@ -87,6 +88,7 @@
8788
add_action('rest_api_init', [Rest::class, 'register_routes'], 10, 0);
8889
$times[] = ['Rest', microtime(true)];
8990

91+
// pruner
9092
require __DIR__ . '/src/class-pruner.php';
9193
new Pruner();
9294
$times[] = ['Pruner', microtime(true)];
@@ -96,8 +98,6 @@
9698
$times[] = ['Command', microtime(true)];
9799
}
98100

99-
100-
101101
// register shortcodes
102102
add_shortcode('koko_analytics_most_viewed_posts', [Shortcode_Most_Viewed_Posts::class, 'content']);
103103
$times[] = ['Shortcode_Most_Viewed_Posts', microtime(true)];
@@ -136,5 +136,8 @@
136136
// for ($i = count($times) - 1; $i > 0; $i--) {
137137
// $times[$i][1] = $times[$i][1] - $times[$i-1][1];
138138
// }
139+
// usort($times, function ($a, $b) {
140+
// return $a[1] === $b[1] ? 0 : ($a[1] > $b[1] ? -1 : 1);
141+
// });
139142

140143
// dump($times);

src/class-script-loader.php

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,10 @@
1212

1313
class 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

Comments
 (0)