Skip to content

Commit 8fac265

Browse files
write list of excluded ip addresses to optimized endpoint. closes #243
1 parent d3d0f65 commit 8fac265

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

src/class-admin-actions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace KokoAnalytics;
1010

11+
use Burst\Frontend\Endpoint;
12+
1113
class Admin_Actions
1214
{
1315
public static function install_optimized_endpoint(): void
@@ -120,6 +122,12 @@ public static function save_settings(): void
120122
Fingerprinter::setup_scheduled_event();
121123
}
122124

125+
// if using custom endpoint, re-create it to ensure list of IP addresses is up-to-date
126+
$endpoint_installer = new Endpoint_Installer();
127+
if (using_custom_endpoint() && $endpoint_installer->is_eligibile()) {
128+
$endpoint_installer->install();
129+
}
130+
123131
wp_safe_redirect(add_query_arg(['settings-updated' => true], wp_get_referer()));
124132
exit;
125133
}

src/class-endpoint-installer.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function get_file_name(): string
1717

1818
public function get_file_contents(): string
1919
{
20+
$settings = get_settings();
2021
$upload_dir = get_upload_dir();
2122

2223
// make path relative to ABSPATH again
@@ -25,6 +26,7 @@ public function get_file_contents(): string
2526
}
2627
$wp_timezone_string = wp_timezone_string();
2728
$functions_filename = KOKO_ANALYTICS_PLUGIN_DIR . '/src/collect-functions.php';
29+
$excluded_ip_addresses_string = var_export($settings['exclude_ip_addresses'], true);
2830

2931
// make path relative to ABSPATH again
3032
if (str_starts_with($functions_filename, ABSPATH)) {
@@ -48,6 +50,11 @@ public function get_file_contents(): string
4850
// path to functions.php file in Koko Analytics plugin directory
4951
require '$functions_filename';
5052
53+
// check if IP address is on list of addresses to ignore
54+
if (!isset(\$_GET['test']) && in_array(KokoAnalytics\get_client_ip(), $excluded_ip_addresses_string)) {
55+
exit;
56+
}
57+
5158
// function call to collect the request data
5259
KokoAnalytics\collect_request();
5360
@@ -61,16 +68,16 @@ public function install()
6168
{
6269
$file_name = $this->get_file_name();
6370

64-
/* Attempt to put the file into place if it does not exist already */
65-
if (! is_file($file_name)) {
66-
$created = file_put_contents($file_name, $this->get_file_contents());
67-
if (! $created) {
68-
return __('Error creating file', 'koko-analytics');
69-
}
71+
// attempt to overwrite file with latest contents to ensure it's up-to-date
72+
file_put_contents($file_name, $this->get_file_contents());
73+
74+
$exists = is_file($file_name);
75+
update_option('koko_analytics_use_custom_endpoint', $exists, true);
76+
77+
if (! $exists) {
78+
return __('Error creating file', 'koko-analytics');
7079
}
7180

72-
// File was successfully created, so let's start using it from now on
73-
update_option('koko_analytics_use_custom_endpoint', true, true);
7481
return true;
7582
}
7683

tests/FunctionsTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public function testExtractPageviewData(): void
2323
$this->assertEquals(extract_pageview_data(['p' => '1', 'r' => 'not an url']), []);
2424

2525
// complete and valid
26-
foreach ([
26+
foreach (
27+
[
2728
[['p' => '1'], ['p', null, 1, 1, 1, '']],
2829

2930
] as [$input, $expected]

0 commit comments

Comments
 (0)