Skip to content

Commit

Permalink
Merge branch 'optimize-runtime'
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Jan 24, 2025
2 parents 9c848b5 + 1925b12 commit c3a073d
Show file tree
Hide file tree
Showing 22 changed files with 211 additions and 247 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-test-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ jobs:

- name: Run benchmark
run: php tests/benchmarks/run-benchmark.php
<<<<<<< HEAD

=======
>>>>>>> optimize-runtime
8 changes: 5 additions & 3 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

spl_autoload_register(function ($class) {
static $classmap = [
'KokoAnalytics\\Actions' => '/src/class-actions.php',
'KokoAnalytics\\Admin' => '/src/class-admin.php',
'KokoAnalytics\\Aggregator' => '/src/class-aggregator.php',
'KokoAnalytics\\Command' => '/src/class-command.php',
'KokoAnalytics\\Chart_View' => '/src/class-chart-view.php',
'KokoAnalytics\\Dashboard' => '/src/class-dashboard.php',
'KokoAnalytics\\Dashboard_Widget' => '/src/class-dashboard-widget.php',
'KokoAnalytics\\Dashboard' => '/src/class-dashboard.php',
'KokoAnalytics\\Data_Exporter' => '/src/class-data-exporter.php',
'KokoAnalytics\\Data_Importer' => '/src/class-data-importer.php',
'KokoAnalytics\\Endpoint_Installer' => '/src/class-endpoint-installer.php',
Expand All @@ -21,12 +22,13 @@
'KokoAnalytics\\Pageview_Aggregator' => '/src/class-pageview-aggregator.php',
'KokoAnalytics\\Plugin' => '/src/class-plugin.php',
'KokoAnalytics\\Pruner' => '/src/class-pruner.php',
'KokoAnalytics\\QueryLoopBlock' => '/src/class-query-loop-block.php',
'KokoAnalytics\\Rest' => '/src/class-rest.php',
'KokoAnalytics\\Script_Loader' => '/src/class-script-loader.php',
'KokoAnalytics\\ShortCode_Site_Counter' => '/src/class-shortcode-site-counter.php',
'KokoAnalytics\\Stats' => '/src/class-stats.php',
'KokoAnalytics\\Shortcode_Most_Viewed_Posts' => '/src/class-shortcode-most-viewed-posts.php',
'KokoAnalytics\\Shortcode_Site_Counter' => '/src/class-shortcode-site-counter.php',
'KokoAnalytics\\Stats' => '/src/class-stats.php',
'KokoAnalytics\\Query_Loop_Block' => '/src/class-query-loop-block.php',
'KokoAnalytics\\Widget_Most_Viewed_Posts' => '/src/class-widget-most-viewed-posts.php',
];

Expand Down
4 changes: 3 additions & 1 deletion bin/create-package
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ zip -r "$PACKAGE_FILE" "$PLUGIN_SLUG" \
-x "$PLUGIN_SLUG/vendor/*" \
-x "$PLUGIN_SLUG/node_modules/*" \
-x "$PLUGIN_SLUG/tests/*" \
-x "$PLUGIN_SLUG/webpack.config*.js" \
-x "$PLUGIN_SLUG/webpack.config.js" \
-x "$PLUGIN_SLUG/eslint.config.mjs" \
-x "$PLUGIN_SLUG/*.json" \
-x "$PLUGIN_SLUG/*.lock" \
-x "$PLUGIN_SLUG/phpcs.xml" \
-x "$PLUGIN_SLUG/phpunit.xml.dist" \
-x "$PLUGIN_SLUG/*.sh" \
-x "$PLUGIN_SLUG/assets/src/*" \
-x "$PLUGIN_SLUG/assets/dist/img/screenshot-*" \
Expand Down
106 changes: 80 additions & 26 deletions koko-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,89 @@
}

// Maybe run any pending database migrations
$migrations = new Migrations('koko_analytics_version', KOKO_ANALYTICS_VERSION, KOKO_ANALYTICS_PLUGIN_DIR . '/migrations/');
add_action('init', [$migrations, 'maybe_run'], 10, 0);
add_action('init', function () {
if (\version_compare(get_option('koko_analytics_version', '0.0.0'), KOKO_ANALYTICS_VERSION, '>=')) {
return;
}

$migrations = new Migrations('koko_analytics_version', KOKO_ANALYTICS_VERSION, KOKO_ANALYTICS_PLUGIN_DIR . '/migrations/');
$migrations->maybe_run();
}, 10, 0);

// aggregator
add_filter('cron_schedules', function ($schedules) {
$schedules['koko_analytics_stats_aggregate_interval'] = [
'interval' => 60, // 60 seconds
'display' => esc_html__('Every minute', 'koko-analytics'),
];
return $schedules;
}, 10, 1);
add_action('koko_analytics_aggregate_stats', [Aggregator::class, 'run'], 10, 0);

// ajax collection endpoint (only used in case optimized endpoint is not installed)
add_action('init', 'KokoAnalytics\maybe_collect_request', 0, 0);

// script loader
add_action('wp_enqueue_scripts', [Script_Loader::class, 'maybe_enqueue_script'], 10, 0);
add_action('amp_print_analytics', [Script_Loader::class, 'print_amp_analytics_tag'], 10, 0);
add_action('admin_bar_menu', 'KokoAnalytics\admin_bar_menu', 40, 1);

// query loop block
add_action('admin_enqueue_scripts', [Query_Loop_Block::class, 'admin_enqueue_scripts']);
add_filter('pre_render_block', [Query_Loop_Block::class, 'pre_render_block'], 10, 3);

// init REST API endpoint
add_action('rest_api_init', [Rest::class, 'register_routes'], 10, 0);

// pruner
add_action('koko_analytics_prune_data', [Pruner::class, 'run'], 10, 0);

// WP CLI command
if (\class_exists('WP_CLI')) {
\WP_CLI::add_command('koko-analytics', 'KokoAnalytics\Command');
}

new Aggregator();
new Plugin();
// register shortcodes
add_shortcode('koko_analytics_most_viewed_posts', [Shortcode_Most_Viewed_Posts::class, 'content']);
add_shortcode('koko_analytics_counter', [Shortcode_Site_Counter::class, 'content']);

if (\defined('DOING_AJAX') && DOING_AJAX) {
// ajax only
add_action('init', 'KokoAnalytics\maybe_collect_request', 1, 0);
} elseif (is_admin()) {
// wp-admin only
new Admin();
new Dashboard_Widget();
} else {
// frontend only
new Script_Loader();
add_action('admin_bar_menu', 'KokoAnalytics\admin_bar_menu', 40, 1);
}
// run koko_analytics_action=[a-z] hooks
add_action('init', [Actions::class, 'run'], 10, 0);

new QueryLoopBlock();
new Dashboard();
new Rest();
new Shortcode_Most_Viewed_Posts();
new ShortCode_Site_Counter();
new Pruner();
// maybe show standalone dashboard
add_action('wp', function () {
if (!isset($_GET['koko-analytics-dashboard'])) {
return;
}

if (\class_exists('WP_CLI')) {
\WP_CLI::add_command('koko-analytics', 'KokoAnalytics\Command');
}
$settings = get_settings();
if (!$settings['is_dashboard_public'] && !current_user_can('view_koko_analytics')) {
return;
}

(new Dashboard())->show_standalone_dashboard_page();
});

add_action('widgets_init', 'KokoAnalytics\widgets_init');
// register most viewed posts widget
add_action('widgets_init', [Widget_Most_Viewed_Posts::class, 'register']);
add_action('koko_analytics_test_custom_endpoint', 'KokoAnalytics\test_custom_endpoint');

if (\is_admin()) {
new Admin();
new Dashboard_Widget();
}

// on plugin activation
register_activation_hook(__FILE__, function () {
Aggregator::setup_scheduled_event();
Pruner::setup_scheduled_event();
Plugin::setup_capabilities();
Plugin::install_optimized_endpoint();
});

// on plugin deactivation
register_deactivation_hook(__FILE__, function () {
Aggregator::clear_scheduled_event();
Pruner::clear_scheduled_event();
Plugin::remove_optimized_endpoint();
});
41 changes: 41 additions & 0 deletions src/class-actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* @package koko-analytics
* @license GPL-3.0+
* @author Danny van Kooten
*/

namespace KokoAnalytics;

class Actions
{
public static function run(): void
{
$actions = [];

if (isset($_GET['koko_analytics_action'])) {
$actions[] = trim($_GET['koko_analytics_action']);
}

if (isset($_POST['koko_analytics_action'])) {
$actions[] = trim($_POST['koko_analytics_action']);
}

if (empty($actions)) {
return;
}

if (!current_user_can('manage_koko_analytics')) {
return;
}

// fire all supplied action hooks
foreach ($actions as $action) {
do_action("koko_analytics_{$action}");
}

wp_safe_redirect(remove_query_arg('koko_analytics_action'));
exit;
}
}
48 changes: 11 additions & 37 deletions src/class-aggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,12 @@

class Aggregator
{
public function __construct()
{
add_filter('cron_schedules', [$this, 'add_interval'], 10, 1);
add_action('koko_analytics_aggregate_stats', [$this, 'aggregate'], 10, 0);
add_action('koko_analytics_save_settings', [$this, 'setup_scheduled_event'], 10, 0);

register_activation_hook(KOKO_ANALYTICS_PLUGIN_FILE, [$this, 'setup_scheduled_event']);
register_deactivation_hook(KOKO_ANALYTICS_PLUGIN_FILE, [$this, 'clear_scheduled_event']);
}

/**
* @param array $intervals
*/
public function add_interval($intervals): array
{
$intervals['koko_analytics_stats_aggregate_interval'] = [
'interval' => 60, // 60 seconds
'display' => esc_html__('Every minute', 'koko-analytics'),
];
return $intervals;
}

public function setup_scheduled_event(): void
{
if (! wp_next_scheduled('koko_analytics_aggregate_stats')) {
wp_schedule_event(time() + 60, 'koko_analytics_stats_aggregate_interval', 'koko_analytics_aggregate_stats');
}
}

public function clear_scheduled_event(): void
{
wp_clear_scheduled_hook('koko_analytics_aggregate_stats');
}

/**
* Reads the buffer file into memory and moves data into the MySQL database (in bulk)
*
* @throws Exception
*/
public function aggregate(): void
public static function run(): void
{
update_option('koko_analytics_last_aggregation_at', \time(), true);

Expand Down Expand Up @@ -110,9 +76,17 @@ public function aggregate(): void
// tell aggregators to write their results to the database
$pageview_aggregator->finish();
do_action('koko_analytics_aggregate_finish');
}

public static function setup_scheduled_event(): void
{
if (! wp_next_scheduled('koko_analytics_aggregate_stats')) {
wp_schedule_event(time() + 60, 'koko_analytics_stats_aggregate_interval', 'koko_analytics_aggregate_stats');
}
}

// ensure scheduled event is ready to go again
$this->setup_scheduled_event();
public static function clear_scheduled_event(): void
{
wp_clear_scheduled_hook('koko_analytics_aggregate_stats');
}
}
19 changes: 0 additions & 19 deletions src/class-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,6 @@

class Dashboard
{
public function __construct()
{
add_action('wp', [$this, 'maybe_show_dashboard'], 10, 0);
}

public function maybe_show_dashboard(): void
{
if (!isset($_GET['koko-analytics-dashboard'])) {
return;
}

$settings = get_settings();
if (!$settings['is_dashboard_public'] && !current_user_can('view_koko_analytics')) {
return;
}

$this->show_standalone_dashboard_page();
}

public function show_standalone_dashboard_page(): void
{
require __DIR__ . '/views/standalone.php';
Expand Down
40 changes: 8 additions & 32 deletions src/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,30 @@

class Plugin
{
public function __construct()
{
register_activation_hook(KOKO_ANALYTICS_PLUGIN_FILE, [$this, 'on_activation']);
add_action('init', [$this, 'maybe_run_actions'], 20, 0);
}

public function on_activation(): void
public static function setup_capabilities(): void
{
// add capabilities to administrator role (if it exists)
$role = get_role('administrator');
if ($role) {
$role->add_cap('view_koko_analytics');
$role->add_cap('manage_koko_analytics');
}
}

public static function install_optimized_endpoint(): void
{
// (maybe) create optimized endpoint file
$endpoint_installer = new Endpoint_Installer();
if ($endpoint_installer->is_eligibile()) {
$endpoint_installer->install();
}
}

public function maybe_run_actions(): void
public static function remove_optimized_endpoint(): void
{
$actions = [];

if (isset($_GET['koko_analytics_action'])) {
$actions[] = trim($_GET['koko_analytics_action']);
}

if (isset($_POST['koko_analytics_action'])) {
$actions[] = trim($_POST['koko_analytics_action']);
// delete custom endpoint file
if (file_exists(ABSPATH . '/koko-analytics-collect.php')) {
unlink(ABSPATH . '/koko-analytics-collect.php');
}

if (empty($actions)) {
return;
}

if (!current_user_can('manage_koko_analytics')) {
return;
}

// fire all supplied action hooks
foreach ($actions as $action) {
do_action("koko_analytics_{$action}");
}

wp_safe_redirect(remove_query_arg('koko_analytics_action'));
exit;
}
}
13 changes: 3 additions & 10 deletions src/class-pruner.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,19 @@

class Pruner
{
public function __construct()
{
add_action('koko_analytics_prune_data', [$this, 'run'], 10, 0);
register_activation_hook(KOKO_ANALYTICS_PLUGIN_FILE, [$this, 'setup_scheduled_event']);
register_deactivation_hook(KOKO_ANALYTICS_PLUGIN_FILE, [$this, 'clear_scheduled_event']);
}

public function setup_scheduled_event(): void
public static function setup_scheduled_event(): void
{
if (! wp_next_scheduled('koko_analytics_prune_data')) {
wp_schedule_event(time() + DAY_IN_SECONDS, 'daily', 'koko_analytics_prune_data');
}
}

public function clear_scheduled_event(): void
public static function clear_scheduled_event(): void
{
wp_clear_scheduled_hook('koko_analytics_prune_data');
}

public function run()
public static function run()
{
/** @var \wpdb $wpdb */
global $wpdb;
Expand Down
Loading

0 comments on commit c3a073d

Please sign in to comment.