Skip to content

Commit b7eae92

Browse files
clean-up some code related to the optimized endpoint
1 parent 3015f98 commit b7eae92

10 files changed

Lines changed: 38 additions & 86 deletions

koko-analytics.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@
133133
Aggregator::setup_scheduled_event();
134134
Pruner::setup_scheduled_event();
135135
Fingerprinter::setup_scheduled_event();
136+
Endpoint_Installer::install();
136137
Plugin::setup_capabilities();
137-
Plugin::install_optimized_endpoint();
138138
Plugin::create_and_protect_uploads_dir();
139139
});
140140

@@ -143,5 +143,5 @@
143143
Aggregator::clear_scheduled_event();
144144
Pruner::clear_scheduled_event();
145145
Fingerprinter::clear_scheduled_event();
146-
Plugin::remove_optimized_endpoint();
146+
Endpoint_Installer::uninstall();
147147
});

migrations/1.0.21-unlink-custom-endpoint-and-update-option.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

migrations/1.5.4-recreate-endpoints-file.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

migrations/1.7.0-protect-uploads-dir.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,3 @@
99
if (class_exists(Plugin::class) && method_exists(Plugin::class, 'create_and_protect_uploads_dir')) {
1010
Plugin::create_and_protect_uploads_dir();
1111
}
12-
13-
// re-install optimized endpoint using latest content
14-
if (is_file(ABSPATH . '/koko-analytics-collect.php')) {
15-
@unlink(ABSPATH . '/koko-analytics-collect.php');
16-
17-
// (Maybe) create optimized endpoint file
18-
if (class_exists(Endpoint_Installer::class)) {
19-
$endpoint_installer = new Endpoint_Installer();
20-
if ($endpoint_installer->is_eligibile()) {
21-
$endpoint_installer->install();
22-
}
23-
}
24-
}

migrations/1.7.5-migrate-to-new-tracking-method-setting.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
$settings = (array) get_option('koko_analytics_settings', []);
66

7+
// migrate to new 'tracking_method' setting
78
if (isset($settings['use_cookie'])) {
89
$settings['tracking_method'] = $settings['use_cookie'] ? 'cookie' : 'none';
910
unset($settings['use_cookie']);
@@ -12,8 +13,5 @@
1213

1314
// re-install optimized endpoint file
1415
if (class_exists(KokoAnalytics\Endpoint_Installer::class)) {
15-
$endpoint_installer = new KokoAnalytics\Endpoint_Installer();
16-
if ($endpoint_installer->is_eligibile()) {
17-
$endpoint_installer->install();
18-
}
16+
KokoAnalytics\Endpoint_Installer::install();
1917
}

src/class-admin-actions.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ class Admin_Actions
1212
{
1313
public static function install_optimized_endpoint(): void
1414
{
15-
$installer = new Endpoint_Installer();
16-
$result = $installer->install();
17-
wp_safe_redirect(add_query_arg(['endpoint-installed' => $result], wp_get_referer()));
15+
wp_safe_redirect(add_query_arg([ 'endpoint-installed' => Endpoint_Installer::install() ], wp_get_referer()));
1816
exit;
1917
}
2018

@@ -120,11 +118,8 @@ public static function save_settings(): void
120118
Fingerprinter::setup_scheduled_event();
121119
}
122120

123-
// if using custom endpoint, re-create it to ensure list of IP addresses is up-to-date
124-
$endpoint_installer = new Endpoint_Installer();
125-
if (using_custom_endpoint() && $endpoint_installer->is_eligibile()) {
126-
$endpoint_installer->install();
127-
}
121+
// Re-create optimized endpoint to ensure its contents are up-to-date
122+
Endpoint_Installer::install();
128123

129124
wp_safe_redirect(add_query_arg(['settings-updated' => true], wp_get_referer()));
130125
exit;

src/class-admin-page.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public static function show_settings_page(): void
5757
}
5858

5959
$settings = get_settings();
60-
$endpoint_installer = new Endpoint_Installer();
6160
$using_custom_endpoint = using_custom_endpoint();
6261
$database_size = self::get_database_size();
6362
$user_roles = self::get_available_roles();

src/class-endpoint-installer.php

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

1111
class Endpoint_Installer
1212
{
13-
public function get_file_name(): string
13+
public static function get_file_name(): string
1414
{
1515
return rtrim(ABSPATH, '/') . '/koko-analytics-collect.php';
1616
}
1717

18-
public function get_file_contents(): string
18+
public static function get_file_contents(): string
1919
{
2020
$settings = get_settings();
2121
$upload_dir = get_upload_dir();
@@ -64,13 +64,21 @@ public function get_file_contents(): string
6464
/**
6565
* @return string|bool
6666
*/
67-
public function install()
67+
public static function install()
6868
{
69-
$file_name = $this->get_file_name();
69+
// do nothing if site is not eligible for the use of a custom endpoint (ie multisite)
70+
if (!self::is_eligibile()) {
71+
return;
72+
}
73+
74+
$file_name = self::get_file_name();
7075

7176
// attempt to overwrite file with latest contents to ensure it's up-to-date
72-
file_put_contents($file_name, $this->get_file_contents());
77+
file_put_contents($file_name, self::get_file_contents());
7378

79+
// Check if file exists
80+
// Note that we're not checking whether we were able to write to the file
81+
// To allow for users manually creating the file with the correct contents
7482
$exists = is_file($file_name);
7583
update_option('koko_analytics_use_custom_endpoint', $exists, true);
7684

@@ -81,7 +89,15 @@ public function install()
8189
return true;
8290
}
8391

84-
public function is_eligibile(): bool
92+
public static function uninstall(): void
93+
{
94+
$file_name = self::get_file_name();
95+
if (is_file($file_name)) {
96+
unlink($file_name);
97+
}
98+
}
99+
100+
public static function is_eligibile(): bool
85101
{
86102
/* Do nothing if running Multisite (because Multisite has separate uploads directory per site) */
87103
if (is_multisite()) {

src/class-plugin.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,6 @@ public static function setup_capabilities(): void
2020
}
2121
}
2222

23-
public static function install_optimized_endpoint(): void
24-
{
25-
// (maybe) create optimized endpoint file
26-
$endpoint_installer = new Endpoint_Installer();
27-
if ($endpoint_installer->is_eligibile()) {
28-
$endpoint_installer->install();
29-
}
30-
}
31-
32-
public static function remove_optimized_endpoint(): void
33-
{
34-
// delete custom endpoint file
35-
if (file_exists(rtrim(ABSPATH, '/') . '/koko-analytics-collect.php')) {
36-
unlink(rtrim(ABSPATH, '/') . '/koko-analytics-collect.php');
37-
}
38-
}
39-
4023
public static function create_and_protect_uploads_dir(): void
4124
{
4225
$filename = get_buffer_filename();

src/views/settings-page.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<?php defined('ABSPATH') or exit;
1+
<?php
2+
3+
use KokoAnalytics\Endpoint_Installer;
4+
5+
defined('ABSPATH') or exit;
26
/**
37
* @var \KokoAnalytics\Admin $this
48
* @var array $settings
@@ -150,20 +154,20 @@
150154

151155
<?php do_action('koko_analytics_show_settings_sections'); ?>
152156

153-
<?php if ($endpoint_installer->is_eligibile()) { ?>
157+
<?php if (Endpoint_Installer::is_eligibile()) { ?>
154158
<div class="ka-margin-l">
155159
<h2><?php esc_html_e('Performance', 'koko-analytics'); ?></h2>
156160
<?php if ($using_custom_endpoint) { ?>
157-
<p><?php esc_html_e('The plugin is currently using an optimized tracking endpoint. Great!', 'koko-analytics'); ?></p>
161+
<p><span style="color: green;">✓</span> <?php esc_html_e('The plugin is currently using an optimized tracking endpoint. Great!', 'koko-analytics'); ?></p>
158162
<?php } else { ?>
159163
<p><?php esc_html_e('The plugin is currently not using an optimized tracking endpoint.', 'koko-analytics'); ?></p>
160164
<form method="POST" action="">
161165
<?php wp_nonce_field('koko_analytics_install_optimized_endpoint'); ?>
162166
<input type="hidden" name="koko_analytics_action" value="install_optimized_endpoint">
163167
<input type="submit" value="<?php esc_attr_e('Create optimized endpoint file', 'koko-analytics'); ?>" class="button button-secondary">
164168
</form>
165-
<p><?php printf(esc_html__('To use one, create the file %s with the following file contents: ', 'koko-analytics'), '<code>' . $endpoint_installer->get_file_name() . '</code>'); ?></p>
166-
<textarea readonly="readonly" class="widefat" rows="18" onfocus="this.select();" spellcheck="false"><?php echo esc_html($endpoint_installer->get_file_contents()); ?></textarea>
169+
<p><?php printf(esc_html__('To use one, create the file %s with the following file contents: ', 'koko-analytics'), '<code>' . Endpoint_Installer::get_file_name() . '</code>'); ?></p>
170+
<textarea readonly="readonly" class="widefat" rows="18" onfocus="this.select();" spellcheck="false"><?php echo esc_html(Endpoint_Installer::get_file_contents()); ?></textarea>
167171
<p><?php esc_html_e('Please note that this is entirely optional and only recommended for high-traffic websites.', 'koko-analytics'); ?></p>
168172
<?php } ?>
169173
</div>

0 commit comments

Comments
 (0)