Skip to content

Commit

Permalink
add constant KOKO_ANALYTICS_CUSTOM_ENDPOINT to allow using a custom e…
Browse files Browse the repository at this point in the history
…ndpoint file location. relates to #70, closes #147
  • Loading branch information
dannyvankooten committed Jun 1, 2022
1 parent ea5c0b6 commit 9a50f9a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
3 changes: 3 additions & 0 deletions code-snippets/disable-custom-endpoint.php
@@ -0,0 +1,3 @@
<?php

define( 'KOKO_ANALYTICS_CUSTOM_ENDPOINT', false );
15 changes: 15 additions & 0 deletions code-snippets/use-different-custom-endpoint.php
@@ -0,0 +1,15 @@
<?php

# Creating the custom endpoint file
// To use a different custom endpoint file location, first create a file at your preferred location and copy over the contents from the file linked below
// file: https://github.com/ibericode/koko-analytics/blob/master/koko-analytics-collect.php

# Fix path references
// In the file, fix the relative file paths to the uploads directory and the functions.php file from the wp-content/plugins/koko-analytics directory.

# Define a constant
// To tell Koko Analytics of your custom endpoint, define the following constant:
define( 'KOKO_ANALYTICS_CUSTOM_ENDPOINT', '/path-to-your-custom-endpoint-file.php' );

# Test the result
// Finally, ensure the file is accessible through your web server and that Koko Analytics is able to use it correctly.
1 change: 0 additions & 1 deletion koko-analytics.php
Expand Up @@ -76,7 +76,6 @@
$plugin = new Plugin( $aggregator );
$plugin->init();


if ( class_exists( 'WP_CLI' ) ) {
require __DIR__ . '/src/class-command.php';
\WP_CLI::add_command( 'koko-analytics', 'KokoAnalytics\Command' );
Expand Down
8 changes: 6 additions & 2 deletions src/class-admin.php
Expand Up @@ -134,8 +134,12 @@ public function maybe_run_endpoint_installer()
return;
}

$endpoint_installer = new Endpoint_Installer();
$endpoint_installer->run();
// do not run if KOKO_ANALYTICS_CUSTOM_ENDPOINT is defined
if ( defined( 'KOKO_ANALYTICS_CUSTOM_ENDPOINT' ) ) {
return;
}

install_and_test_custom_endpoint();
}

public function maybe_run_migrations()
Expand Down
4 changes: 4 additions & 0 deletions src/class-script-loader.php
Expand Up @@ -65,6 +65,10 @@ private function get_post_id() {
}

private function get_tracker_url() {
if ( defined( 'KOKO_ANALYTICS_CUSTOM_ENDPOINT' ) && KOKO_ANALYTICS_CUSTOM_ENDPOINT ) {
return site_url( KOKO_ANALYTICS_CUSTOM_ENDPOINT );
}

// We should use site_url() here because we place the file in ABSPATH and other plugins may be filtering home_url (eg multilingual plugin)
// In any case: what we use here should match what we test when creating the optimized endpoint file.
return using_custom_endpoint() ? site_url( '/koko-analytics-collect.php' ) : admin_url( 'admin-ajax.php?action=koko_analytics_collect' );
Expand Down
9 changes: 9 additions & 0 deletions src/functions.php
Expand Up @@ -173,5 +173,14 @@ function get_realtime_pageview_count( $since = null ) {
}

function using_custom_endpoint() {
if ( defined( 'KOKO_ANALYTICS_CUSTOM_ENDPOINT' ) ) {
return KOKO_ANALYTICS_CUSTOM_ENDPOINT;
}

return get_option( 'koko_analytics_use_custom_endpoint', false );
}

function install_and_test_custom_endpoint() {
$endpoint_installer = new Endpoint_Installer();
$endpoint_installer->run();
}

0 comments on commit 9a50f9a

Please sign in to comment.