@@ -17,25 +17,35 @@ public static function get_file_name(): string
17
17
return rtrim (ABSPATH , '/ ' ) . '/koko-analytics-collect.php ' ;
18
18
}
19
19
20
- public static function get_file_contents ( ): string
20
+ public static function make_relative_to_abspath ( string $ path ): string
21
21
{
22
- $ settings = get_settings ();
23
- $ upload_dir = get_upload_dir ();
24
-
25
22
// 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 )), '/ ' );
28
25
}
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 ());
29
33
$ wp_timezone_string = wp_timezone_string ();
30
- $ functions_filename = KOKO_ANALYTICS_PLUGIN_DIR . '/src/collect-functions.php ' ;
31
34
$ excluded_ip_addresses_string = var_export ($ settings ['exclude_ip_addresses ' ], true );
32
35
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
39
49
<?php
40
50
/**
41
51
* @package koko-analytics
@@ -49,8 +59,8 @@ public static function get_file_contents(): string
49
59
define('KOKO_ANALYTICS_UPLOAD_DIR', ' $ upload_dir');
50
60
define('KOKO_ANALYTICS_TIMEZONE', ' $ wp_timezone_string');
51
61
52
- // path to functions.php file in Koko Analytics plugin directory
53
- require ' $ functions_filename ';
62
+ // required files
63
+ $ require_statements
54
64
55
65
// check if IP address is on list of addresses to ignore
56
66
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
61
71
KokoAnalytics\collect_request();
62
72
63
73
EOT ;
64
- return apply_filters ('koko_analytics_endpoint_file_contents ' , $ content );
65
74
}
66
75
67
76
/**
0 commit comments