diff --git a/composer.json b/composer.json index 83920df3..96b5743f 100644 --- a/composer.json +++ b/composer.json @@ -88,6 +88,11 @@ "vendor/npm-asset/chartist-plugin-tooltips-updated/dist/chartist-plugin-tooltip.min.js": "js/" } }, + "autoload": { + "psr-4": { + "Pluginkollektiv\\Statify\\": "inc/" + } + }, "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true diff --git a/inc/class-statify-api.php b/inc/Api.php similarity index 95% rename from inc/class-statify-api.php rename to inc/Api.php index 8a12a538..381713de 100644 --- a/inc/class-statify-api.php +++ b/inc/Api.php @@ -1,6 +1,6 @@ get_param( 'refresh' ); - $stats = Statify_Dashboard::get_stats( $refresh ); + $stats = Dashboard::get_stats( $refresh ); $visits = $stats['visits']; $stats['visits'] = array(); diff --git a/inc/class-statify-backend.php b/inc/Backend.php similarity index 91% rename from inc/class-statify-backend.php rename to inc/Backend.php index 5c8209da..26d49176 100644 --- a/inc/class-statify-backend.php +++ b/inc/Backend.php @@ -1,6 +1,6 @@ esc_url_raw( rest_url( Statify_Api::REST_NAMESPACE . '/' . Statify_Api::REST_ROUTE_TRACK ) ), + 'url' => esc_url_raw( rest_url( Api::REST_NAMESPACE . '/' . Api::REST_ROUTE_TRACK ) ), ); if ( Statify::TRACKING_METHOD_JAVASCRIPT_WITH_NONCE_CHECK === self::$_options['snippet'] ) { $script_data['nonce'] = wp_create_nonce( 'statify_track' ); @@ -159,7 +162,7 @@ public static function amp_post_template_analytics( $analytics ) { private static function make_amp_config() { $cfg = array( 'requests' => array( - 'pageview' => rest_url( Statify_Api::REST_NAMESPACE . '/' . Statify_Api::REST_ROUTE_TRACK ), + 'pageview' => rest_url( Api::REST_NAMESPACE . '/' . Api::REST_ROUTE_TRACK ), ), 'extraUrlParams' => array( 'referrer' => '${documentReferrer}', diff --git a/inc/class-statify-install.php b/inc/Install.php similarity index 87% rename from inc/class-statify-install.php rename to inc/Install.php index ee7ecf44..d98a8666 100644 --- a/inc/class-statify-install.php +++ b/inc/Install.php @@ -1,6 +1,6 @@ + + + diff --git a/statify.php b/statify.php index 23de54f6..98b01c25 100644 --- a/statify.php +++ b/statify.php @@ -7,7 +7,7 @@ * Author URI: https://pluginkollektiv.org/ * Plugin URI: https://statify.pluginkollektiv.org/ * License: GPLv3 or later - * Version: 1.9.0 + * Version: 2.0.0 * * @package WordPress */ @@ -20,72 +20,14 @@ define( 'STATIFY_FILE', __FILE__ ); define( 'STATIFY_DIR', dirname( __FILE__ ) ); define( 'STATIFY_BASE', plugin_basename( __FILE__ ) ); -define( 'STATIFY_VERSION', '1.9.0' ); +define( 'STATIFY_VERSION', '2.0.0' ); /* Hooks */ -add_action( - 'plugins_loaded', - array( - 'Statify', - 'init', - ) -); -register_activation_hook( - STATIFY_FILE, - array( - 'Statify_Install', - 'init', - ) -); -register_deactivation_hook( - STATIFY_FILE, - array( - 'Statify_Deactivate', - 'init', - ) -); -register_uninstall_hook( - STATIFY_FILE, - array( - 'Statify_Uninstall', - 'init', - ) -); +add_action( 'plugins_loaded', array( 'Pluginkollektiv\Statify\Statify', 'init' ) ); +register_activation_hook( STATIFY_FILE, array( 'Pluginkollektiv\Statify\Install', 'init' ) ); +register_deactivation_hook( STATIFY_FILE, array( 'Pluginkollektiv\\Statify\Deactivate', 'init' ) ); +register_uninstall_hook( STATIFY_FILE, array( 'Pluginkollektiv\\Statify\Uninstall', 'init' ) ); /* Composer Autoload */ require __DIR__ . '/vendor/autoload.php'; - -/* Autoload */ -spl_autoload_register( 'statify_autoload' ); - -/** - * Include classes via autoload. - * - * @param string $class Name of an class-file name, without file extension. - */ -function statify_autoload( $class ) { - - $plugin_classes = array( - 'Statify', - 'Statify_Api', - 'Statify_Backend', - 'Statify_Frontend', - 'Statify_Dashboard', - 'Statify_Install', - 'Statify_Uninstall', - 'Statify_Deactivate', - 'Statify_Settings', - 'Statify_Table', - 'Statify_XMLRPC', - 'Statify_Cron', - ); - - if ( in_array( $class, $plugin_classes, true ) ) { - require_once sprintf( - '%s/inc/class-%s.php', - STATIFY_DIR, - strtolower( str_replace( '_', '-', $class ) ) - ); - } -} diff --git a/tests/test-api-tracking.php b/tests/test-api-tracking.php index ceacac7b..d73bdd86 100644 --- a/tests/test-api-tracking.php +++ b/tests/test-api-tracking.php @@ -5,6 +5,12 @@ * @package Statify */ +namespace Pluginkollektiv\Statify; + +use DateTime; +use WP_REST_Request; +use WP_UnitTestCase; + /** * Class Test_Api_Tracking. * Tests for JavaScript based tracking using WP API. @@ -19,7 +25,7 @@ public function set_up() { parent::set_up(); // "Install" Statify, i.e. create tables and options. - Statify_Install::init(); + Install::init(); } /** @@ -35,16 +41,16 @@ public function test_register_route() { do_action( 'rest_api_init' ); $this->assertArrayNotHasKey( - '/' . Statify_Api::REST_NAMESPACE . '/' . Statify_Api::REST_ROUTE_TRACK, + '/' . Api::REST_NAMESPACE . '/' . Api::REST_ROUTE_TRACK, $wp_rest_server->get_routes(), 'REST route should not have been registered with JS disabled' ); // Enable JS tracking. - $this->init_statify_tracking( Statify_Frontend::TRACKING_METHOD_JAVASCRIPT_WITH_NONCE_CHECK ); + $this->init_statify_tracking( Statify::TRACKING_METHOD_JAVASCRIPT_WITH_NONCE_CHECK ); do_action( 'rest_api_init' ); $this->assertArrayHasKey( - '/' . Statify_Api::REST_NAMESPACE . '/' . Statify_Api::REST_ROUTE_TRACK, + '/' . Api::REST_NAMESPACE . '/' . Api::REST_ROUTE_TRACK, $wp_rest_server->get_routes(), 'REST route should have been registered with JS enabled' ); @@ -59,7 +65,7 @@ public function test_track_api() { // Initialize a valid Statify tracking request. $request = new WP_REST_Request( 'POST', - '/' . Statify_Api::REST_NAMESPACE . '/' . Statify_Api::REST_ROUTE_TRACK + '/' . Api::REST_NAMESPACE . '/' . Api::REST_ROUTE_TRACK ); $request->set_header( 'Content-Type', 'appliction/json;charset=utf-8' ); $request->set_param( 'target', '/' ); @@ -67,7 +73,7 @@ public function test_track_api() { $request->set_param( 'nonce', wp_create_nonce( 'statify_track' ) ); // Initialize Statify with JS tracking enabled. - $this->init_statify_tracking( Statify_Frontend::TRACKING_METHOD_JAVASCRIPT_WITH_NONCE_CHECK ); + $this->init_statify_tracking( Statify::TRACKING_METHOD_JAVASCRIPT_WITH_NONCE_CHECK ); do_action( 'rest_api_init' ); $response = $wp_rest_server->dispatch( $request ); @@ -108,7 +114,7 @@ public function test_track_api() { $this->assertEquals( 1, $stats['visits'][0]['count'], 'Visit count should not be higher after AJAX request failed' ); // Now disable JS tracking. - $this->init_statify_tracking( Statify_Frontend::TRACKING_METHOD_DEFAULT ); + $this->init_statify_tracking( Statify::TRACKING_METHOD_DEFAULT ); // The REST routes for testing are not reset, so the endpoint is evaluated and does not return 404 here. $response = $wp_rest_server->dispatch( $request ); @@ -118,7 +124,7 @@ public function test_track_api() { $this->assertEquals( 1, $stats['referrer'][0]['count'], 'Unexpected referrer count' ); // Re-enable JS without nonce. - $this->init_statify_tracking( Statify_Frontend::TRACKING_METHOD_JAVASCRIPT_WITHOUT_NONCE_CHECK ); + $this->init_statify_tracking( Statify::TRACKING_METHOD_JAVASCRIPT_WITHOUT_NONCE_CHECK ); $response = $wp_rest_server->dispatch( $request ); $this->assertEquals( 204, $response->get_status(), 'API request should return 204 with JS enabled' ); @@ -136,7 +142,7 @@ public function test_track_api() { /* Allow tracking for logged-in users. This also check the overruled check, i.e. the login status is not reset without nonce. */ - $this->init_statify_tracking( Statify_Frontend::TRACKING_METHOD_JAVASCRIPT_WITHOUT_NONCE_CHECK, true ); + $this->init_statify_tracking( Statify::TRACKING_METHOD_JAVASCRIPT_WITHOUT_NONCE_CHECK, true ); $response = $wp_rest_server->dispatch( $request ); $this->assertEquals( 204, $response->get_status(), 'API request should return 204 with JS enabled and logged in' ); $stats = $this->get_stats(); diff --git a/tests/test-cron.php b/tests/test-cron.php index 9d8d689e..f960c058 100644 --- a/tests/test-cron.php +++ b/tests/test-cron.php @@ -5,6 +5,11 @@ * @package Statify */ +namespace Pluginkollektiv\Statify; + +use DateTime; +use WP_UnitTestCase; + /** * Class Test_Cron. * Tests for cron integration. @@ -19,7 +24,7 @@ public function set_up() { parent::set_up(); // "Install" Statify, i.e. create tables and options. - Statify_Install::init(); + Install::init(); } /** @@ -32,7 +37,7 @@ public function test_cronjob() { // Initialize normal cycle, configure storage period of 3 days. $this->init_statify_widget( 3 ); $this->assertNotFalse( - has_action( 'statify_cleanup', array( 'Statify_Cron', 'cleanup_data' ) ), + has_action( 'statify_cleanup', array( 'Pluginkollektiv\Statify\Cron', 'cleanup_data' ) ), 'Statify cleanup cron job should be registered in normal cycle (always)' ); @@ -40,7 +45,7 @@ public function test_cronjob() { define( 'DOING_CRON', true ); Statify::init(); $this->assertNotFalse( - has_action( 'statify_cleanup', array( 'Statify_Cron', 'cleanup_data' ) ), + has_action( 'statify_cleanup', array( 'Pluginkollektiv\Statify\Cron', 'cleanup_data' ) ), 'Statify cleanup cron job was not registered' ); @@ -62,7 +67,7 @@ public function test_cronjob() { } // Run the cron job. - Statify_Cron::cleanup_data(); + Cron::cleanup_data(); // Verify that 2 days have been deleted. $stats = $this->get_stats(); diff --git a/tests/test-dashboard.php b/tests/test-dashboard.php index 3811a46a..36ad0e08 100644 --- a/tests/test-dashboard.php +++ b/tests/test-dashboard.php @@ -5,6 +5,11 @@ * @package Statify */ +namespace Pluginkollektiv\Statify; + +use DateTime; +use WP_UnitTestCase; + /** * Class Test_Dashboard. * Tests for dashboard integration. @@ -21,9 +26,11 @@ public function set_up() { set_current_screen( 'dashboard' ); // "Install" Statify, i.e. create tables and options. - Statify_Install::init(); + Install::init(); - if ( ! function_exists( 'wp_add_dashboard_widget' ) ) { + /* This function is typically defined in the root namespace. However, the hierarchy prefers this overridden + instance here, so it will be used for testing purposes. */ + if ( ! function_exists( 'Pluginkollektiv\Statify\wp_add_dashboard_widget' ) ) { global $widget_capture; $widget_capture = array(); @@ -57,13 +64,13 @@ public function test_init() { // Anonymous users do not have the "edit_dashboard" capability, i.e. can't see the stats. wp_set_current_user( 0 ); - Statify_Dashboard::init(); + Dashboard::init(); $this->assertFalse( - has_action( 'admin_print_styles', array( Statify_Dashboard::class, 'add_style' ) ), + has_action( 'admin_print_styles', array( Dashboard::class, 'add_style' ) ), 'Styles unexpectedly added' ); $this->assertFalse( - has_action( 'admin_print_scripts', array( Statify_Dashboard::class, 'add_js' ) ), + has_action( 'admin_print_scripts', array( Dashboard::class, 'add_js' ) ), 'Scripts unexpectedly added' ); @@ -71,26 +78,26 @@ public function test_init() { wp_get_current_user()->add_cap( 'edit_dashboard' ); wp_set_current_user( 1 ); - Statify_Dashboard::init(); + Dashboard::init(); $this->assertCount( 5, $widget_capture, 'No widget registered' ); $this->assertEquals( 'statify_dashboard', $widget_capture['widget_id'], 'Unexpected widget ID' ); $this->assertEquals( 'Statify', $widget_capture['widget_name'], 'Unexpected widget name' ); $this->assertEquals( - array( Statify_Dashboard::class, 'print_frontview' ), + array( Dashboard::class, 'print_frontview' ), $widget_capture['callback'], 'Unexpected widget callback' ); $this->assertEquals( - array( Statify_Dashboard::class, 'print_backview' ), + array( Dashboard::class, 'print_backview' ), $widget_capture['control_callback'], 'Unexpected control callback' ); $this->assertNotFalse( - has_action( 'admin_print_styles', array( Statify_Dashboard::class, 'add_style' ) ), + has_action( 'admin_print_styles', array( Dashboard::class, 'add_style' ) ), 'Styles not added' ); $this->assertNotFalse( - has_action( 'admin_print_scripts', array( Statify_Dashboard::class, 'add_js' ) ), + has_action( 'admin_print_scripts', array( Dashboard::class, 'add_js' ) ), 'Scripts not added' ); } @@ -119,7 +126,7 @@ function ( $original ) use ( &$original_capture, &$override ) { $widget_capture = array(); $override = true; - Statify_Dashboard::init(); + Dashboard::init(); $this->assertCount( 5, $widget_capture, 'Widget should not have been registered' ); $this->assertFalse( $original_capture, 'Expected original result to be FALSE' ); @@ -128,7 +135,7 @@ function ( $original ) use ( &$original_capture, &$override ) { wp_get_current_user()->add_cap( 'edit_dashboard' ); $override = false; - Statify_Dashboard::init(); + Dashboard::init(); $this->assertEmpty( $widget_capture, 'Widget should not have been registered' ); $this->assertTrue( $original_capture, 'Expected original result to be TRUE' ); } @@ -254,13 +261,13 @@ public function test_get_stats() { // Finally we add another entry in the database, but utilize the transient cache (4min should be enough for the test case). $this->insert_test_data( $date1->format( 'Y-m-d' ), 'https://example.com/', '/example/', 1 ); - $stats4 = Statify_Dashboard::get_stats(); + $stats4 = Dashboard::get_stats(); $this->assertEquals( $stats3, $stats4, 'Stats expected to be equal, is the transient cache active?' ); // Add more data and force reload. We now should see that fallback ordering by URL works. $this->insert_test_data( $date1->format( 'Y-m-d' ), 'https://example.com/', '/', 1 ); $this->insert_test_data( $date1->format( 'Y-m-d' ), 'https://example.net/', '/', 1 ); - $stats5 = Statify_Dashboard::get_stats( true ); + $stats5 = Dashboard::get_stats( true ); $this->assertEquals( 18, $stats5['visit_totals']['since_beginning']['count'], 'Unexpected total since beginning' ); $this->assertEquals( 2, $stats5['referrer'][1]['count'], 'Unexpected 2nd referrer count' ); $this->assertEquals( 'example.com', $stats5['referrer'][1]['host'], 'Unexpected 2nd referrer hostname' ); diff --git a/tests/test-frontend.php b/tests/test-frontend.php index f790761f..ddca6675 100644 --- a/tests/test-frontend.php +++ b/tests/test-frontend.php @@ -5,6 +5,10 @@ * @package Statify */ +namespace Pluginkollektiv\Statify; + +use WP_UnitTestCase; + /** * Class Test_Frontend. * Tests for frontend integration. @@ -17,22 +21,22 @@ class Test_Frontend extends WP_UnitTestCase { */ public function test_wp_footer() { // Disable JS tracking. - $this->init_statify_tracking( Statify_Frontend::TRACKING_METHOD_DEFAULT ); + $this->init_statify_tracking( Statify::TRACKING_METHOD_DEFAULT ); $this->assertNotFalse( - has_action( 'wp_footer', array( 'Statify_Frontend', 'wp_footer' ) ), + has_action( 'wp_footer', array( 'Pluginkollektiv\Statify\Frontend', 'wp_footer' ) ), 'Statify footer action not registered' ); - Statify_Frontend::wp_footer(); + Frontend::wp_footer(); $this->assertFalse( wp_script_is( 'statify-js', 'enqueued' ), 'Statify JS should not be enqueued if JS tracking is disabled' ); // Enable JS tracking. - $this->init_statify_tracking( Statify_Frontend::TRACKING_METHOD_JAVASCRIPT_WITH_NONCE_CHECK ); + $this->init_statify_tracking( Statify::TRACKING_METHOD_JAVASCRIPT_WITH_NONCE_CHECK ); - Statify_Frontend::wp_footer(); + Frontend::wp_footer(); $this->assertTrue( wp_script_is( 'statify-js', 'enqueued' ), 'Statify JS must be equeued if JS tracking is enabled' @@ -54,12 +58,12 @@ public function test_query_vars() { $this->assertNotFalse( has_action( 'query_vars', - array( 'Statify_Frontend', 'query_vars' ) + array( 'Pluginkollektiv\Statify\Frontend', 'query_vars' ) ), 'Statify query_vars action not registered' ); - $vars = Statify_Frontend::query_vars( array() ); + $vars = Frontend::query_vars( array() ); $this->assertCount( 2, $vars, 'Unexpected number of query vars' ); $this->assertContains( 'statify_referrer', $vars, 'Referrer variable not declared' ); $this->assertContains( 'statify_target', $vars, 'Target variable not declared' ); diff --git a/tests/test-statify.php b/tests/test-statify.php index 769b59f9..98eff24a 100644 --- a/tests/test-statify.php +++ b/tests/test-statify.php @@ -5,6 +5,10 @@ * @package Statify */ +namespace Pluginkollektiv\Statify; + +use WP_UnitTestCase; + /** * Class Test_Statify. * Tests for Statify core class. @@ -19,7 +23,7 @@ public function set_up() { parent::set_up(); // "Install" Statify, i.e. create tables and options. - Statify_Install::init(); + Install::init(); } /** diff --git a/tests/test-tracking.php b/tests/test-tracking.php index 980f6b85..d837811a 100644 --- a/tests/test-tracking.php +++ b/tests/test-tracking.php @@ -5,6 +5,11 @@ * @package Statify */ +namespace Pluginkollektiv\Statify; + +use DateTime; +use WP_UnitTestCase; + /** * Class Test_Tracking. * Tests for non-JavaScript tracking and tracking-related mechanisms. @@ -19,7 +24,7 @@ public function set_up() { parent::set_up(); // "Install" Statify, i.e. create tables and options. - Statify_Install::init(); + Install::init(); } /** @@ -35,7 +40,7 @@ public function test_default_tracking() { $this->assertNotFalse( has_action( 'template_redirect', - array( 'Statify_Frontend', 'track_visit' ) + array( 'Pluginkollektiv\Statify\Frontend', 'track_visit' ) ), 'Statify tracking action not registered' ); @@ -45,7 +50,7 @@ public function test_default_tracking() { $_SERVER['HTTP_REFERER'] = 'https://statify.pluginkollektiv.org/'; $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNotNull( $stats, 'Stats should be filled after tracking' ); @@ -68,7 +73,7 @@ public function test_default_tracking() { $_SERVER['REQUEST_URI'] = '/'; $_SERVER['HTTP_REFERER'] = 'https://statify.pluginkollektiv.org/documentation/'; $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0'; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNotNull( $stats, 'Stats should be filled after tracking' ); @@ -88,7 +93,7 @@ public function test_default_tracking() { // Request to invalid target should not be tracked. $_SERVER['REQUEST_URI'] = ''; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertEquals( 2, $stats['visits'][0]['count'], 'Unexpected visit count' ); @@ -97,7 +102,7 @@ public function test_default_tracking() { $_SERVER['REQUEST_URI'] = '/?foo=bar'; $_SERVER['HTTP_REFERER'] = home_url(); - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertEquals( 3, $stats['visits'][0]['count'], 'Unexpected visit count' ); $this->assertEquals( 2, count( $stats['target'] ), 'Unexpected number of targets' ); @@ -109,8 +114,8 @@ public function test_default_tracking() { // If JavaScript tracking is enabled, the regular request should not be tracked. $_SERVER['REQUEST_URI'] = '/'; - $this->init_statify_tracking( Statify_Frontend::TRACKING_METHOD_JAVASCRIPT_WITH_NONCE_CHECK, false ); - Statify_Frontend::track_visit(); + $this->init_statify_tracking( Statify::TRACKING_METHOD_JAVASCRIPT_WITH_NONCE_CHECK, false ); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertEquals( 3, $stats['visits'][0]['count'], 'Unexpected visit count' ); } @@ -132,31 +137,31 @@ public function test_skip_tracking() { $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'; $wp_query->is_robots = true; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNull( $stats, 'Robots should not be tracked' ); $wp_query->is_robots = false; $wp_query->is_trackback = true; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNull( $stats, 'Trackbacks should not be tracked.' ); $wp_query->is_trackback = false; $wp_query->is_preview = true; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNull( $stats, 'Previews should not be tracked.' ); $wp_query->is_preview = false; $wp_query->is_404 = true; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNull( $stats, '404 should not be tracked.' ); $wp_query->is_404 = false; $wp_query->is_feed = true; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNull( $stats, 'Feeds should not be tracked.' ); @@ -164,7 +169,7 @@ public function test_skip_tracking() { $wp_query->is_feed = false; if ( function_exists( 'is_favicon' ) ) { $wp_query->is_favicon = true; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNull( $stats, 'Favicons should not be tracked.' ); $wp_query->is_favicon = false; @@ -173,12 +178,12 @@ public function test_skip_tracking() { // Sitemap XML and XSL for WP 5.5 and above. if ( version_compare( $wp_version, '5.5', '>=' ) ) { set_query_var( 'sitemap', 'index' ); - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNull( $stats, 'Sitemap XML should not be tracked.' ); set_query_var( 'sitemap', null ); set_query_var( 'sitemap-stylesheet', 'sitemap' ); - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNull( $stats, 'Sitemap XSL should not be tracked.' ); } @@ -241,7 +246,7 @@ public function test_bot_tracking() { foreach ( $bot_uas as $bot_ua ) { $_SERVER['HTTP_USER_AGENT'] = $bot_ua; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNull( $stats, 'Bot exclusion failed for user agent: ' . $bot_ua ); } @@ -260,19 +265,19 @@ public function test_disallowed_referer() { "example.com\nstatify.pluginkollektiv.org\nexample.net" ); - $this->init_statify_tracking( Statify_Frontend::TRACKING_METHOD_DEFAULT, false, true ); + $this->init_statify_tracking( Statify::TRACKING_METHOD_DEFAULT, false, true ); // Basically a valid request. $_SERVER['REQUEST_URI'] = '/'; $_SERVER['HTTP_REFERER'] = 'https://statify.pluginkollektiv.org/'; $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNull( $stats, 'Tracking for blacklisted referrer succeeded' ); $this->init_statify_tracking(); - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNotNull( $stats, 'Blacklist evaluated when not enabled' ); } @@ -303,7 +308,7 @@ function ( $previous_result ) use ( &$capture, &$filter_result ) { } ); - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertEquals( 1, $stats['visits'][0]['count'], 'Filter result NULL should not affect counting' ); $this->assertNull( $capture, 'Initial filter should receive NULL value as previous result' ); @@ -311,7 +316,7 @@ function ( $previous_result ) use ( &$capture, &$filter_result ) { // Explicitly blacklist request. $filter_result = true; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertEquals( 1, $stats['visits'][0]['count'], 'Filter result FALSE should prevent request from being tracked' ); @@ -319,14 +324,14 @@ function ( $previous_result ) use ( &$capture, &$filter_result ) { $filter_result = null; $wp_query->is_404 = true; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertEquals( 1, $stats['visits'][0]['count'], 'Filter result NULL should not affect built-in filters' ); // We now explicitly NOT skip the request. $filter_result = false; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertEquals( 2, $stats['visits'][0]['count'], 'Filter result TRUE should force counting' ); } @@ -356,7 +361,7 @@ function ( $data, $id ) use ( &$capture ) { 2 ); - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNotNull( $stats['visits'][0]['count'], 'Request not tracked' ); $this->assertNotEmpty( $capture, 'Hook stativy__visit_saved has not fired' ); @@ -385,14 +390,14 @@ public function test_track_users() { $_SERVER['HTTP_REFERER'] = 'https://statify.pluginkollektiv.org/'; $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'; - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNull( $stats, 'Logged-in user should not be tracked' ); // Re-initialize Statify, enabling logged-in user tracking. - $this->init_statify_tracking( Statify_Frontend::TRACKING_METHOD_DEFAULT, true ); + $this->init_statify_tracking( Statify::TRACKING_METHOD_DEFAULT, true ); - Statify_Frontend::track_visit(); + Frontend::track_visit(); $stats = $this->get_stats(); $this->assertNotNull( $stats, 'Logged-in user should be tracked' ); } diff --git a/tests/trait-statify-test-support.php b/tests/trait-statify-test-support.php index 43915f6d..31318c92 100644 --- a/tests/trait-statify-test-support.php +++ b/tests/trait-statify-test-support.php @@ -5,6 +5,8 @@ * @package Statify */ +namespace Pluginkollektiv\Statify; + /** * Trait Statify_Test_Support. * @@ -79,7 +81,7 @@ protected function init_statify( $args = array() ) { protected function get_stats() { delete_transient( 'statify_data' ); - return Statify_Dashboard::get_stats(); + return Dashboard::get_stats(); } /** diff --git a/views/widget-back.php b/views/widget-back.php index 61b24dd9..b6ff249b 100644 --- a/views/widget-back.php +++ b/views/widget-back.php @@ -7,8 +7,10 @@ * @package Statify */ +namespace Pluginkollektiv\Statify; + // Quit if accessed outside WP context. -class_exists( 'Statify' ) || exit; ?> +class_exists( 'Pluginkollektiv\Statify\Statify' ) || exit; ?>