Skip to content

Commit 2cf4b4e

Browse files
instead of modifying the string, filter a list of filenames to include
1 parent aa0f1f3 commit 2cf4b4e

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

src/class-endpoint-installer.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,35 @@ public static function get_file_name(): string
1717
return rtrim(ABSPATH, '/') . '/koko-analytics-collect.php';
1818
}
1919

20-
public static function get_file_contents(): string
20+
public static function make_relative_to_abspath(string $path): string
2121
{
22-
$settings = get_settings();
23-
$upload_dir = get_upload_dir();
24-
2522
// make path relative to ABSPATH again
26-
if (str_starts_with($upload_dir, ABSPATH)) {
27-
$upload_dir = ltrim(substr($upload_dir, strlen(ABSPATH)), '/');
23+
if (str_starts_with($path, ABSPATH)) {
24+
$path = ltrim(substr($path, strlen(ABSPATH)), '/');
2825
}
26+
return $path;
27+
}
28+
29+
public static function get_file_contents(): string
30+
{
31+
$settings = get_settings();
32+
$upload_dir = self::make_relative_to_abspath(get_upload_dir());
2933
$wp_timezone_string = wp_timezone_string();
30-
$functions_filename = KOKO_ANALYTICS_PLUGIN_DIR . '/src/collect-functions.php';
3134
$excluded_ip_addresses_string = var_export($settings['exclude_ip_addresses'], true);
3235

33-
// make path relative to ABSPATH again
34-
if (str_starts_with($functions_filename, ABSPATH)) {
35-
$functions_filename = ltrim(substr($functions_filename, strlen(ABSPATH)), '/');
36-
}
37-
38-
$content = <<<EOT
36+
// create require statements for all necessary files
37+
$files = [
38+
'wp-includes/plugin.php',
39+
KOKO_ANALYTICS_PLUGIN_DIR . '/src/collect-functions.php',
40+
];
41+
$files = apply_filters('koko_analytics_endpoint_files', $files);
42+
$files = array_map([self::class, 'make_relative_to_abspath'], $files);
43+
$require_statements = array_reduce($files, function ($result, $f) {
44+
$result .= "require '$f';\n";
45+
return $result;
46+
}, '');
47+
48+
return <<<EOT
3949
<?php
4050
/**
4151
* @package koko-analytics
@@ -49,8 +59,8 @@ public static function get_file_contents(): string
4959
define('KOKO_ANALYTICS_UPLOAD_DIR', '$upload_dir');
5060
define('KOKO_ANALYTICS_TIMEZONE', '$wp_timezone_string');
5161
52-
// path to functions.php file in Koko Analytics plugin directory
53-
require '$functions_filename';
62+
// required files
63+
$require_statements
5464
5565
// check if IP address is on list of addresses to ignore
5666
if (!isset(\$_POST['test']) && in_array(KokoAnalytics\get_client_ip(), $excluded_ip_addresses_string)) {
@@ -61,7 +71,6 @@ public static function get_file_contents(): string
6171
KokoAnalytics\collect_request();
6272
6373
EOT;
64-
return apply_filters('koko_analytics_endpoint_file_contents', $content);
6574
}
6675

6776
/**

src/collect-functions.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ function extract_pageview_data(array $raw): array
5151
$referrer_url,
5252
];
5353

54-
if (function_exists('KokoAnalytics\Pro\filter_pageview_data')) {
55-
$data = \KokoAnalytics\Pro\filter_pageview_data($data);
56-
}
57-
58-
return $data;
54+
return apply_filters('koko_analytics_pageview_data', $data);
5955
}
6056

6157
function extract_event_data(array $raw): array

0 commit comments

Comments
 (0)