Skip to content

Commit 1c9e822

Browse files
minor code clean-up
1 parent 14e4b61 commit 1c9e822

19 files changed

+127
-103
lines changed

koko-analytics.php

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,20 @@
4444
if (\defined('DOING_AJAX') && DOING_AJAX) {
4545
maybe_collect_request();
4646
} elseif (is_admin()) {
47-
$admin = new Admin();
48-
$admin->init();
49-
50-
$dashboard_widget = new Dashboard_Widget();
51-
$dashboard_widget->init();
47+
new Admin();
48+
new Dashboard_Widget();
5249
} else {
53-
$loader = new Script_Loader();
54-
$loader->init();
55-
50+
new Script_Loader();
5651
add_action('admin_bar_menu', 'KokoAnalytics\admin_bar_menu', 40);
5752
}
5853

59-
$dashboard = new Dashboard();
60-
$dashboard->add_hooks();
61-
62-
$aggregator = new Aggregator();
63-
$aggregator->init();
64-
65-
$plugin = new Plugin($aggregator);
66-
$plugin->init();
67-
68-
$rest = new Rest();
69-
$rest->init();
70-
71-
$shortcode = new Shortcode_Most_Viewed_Posts();
72-
$shortcode->init();
73-
74-
$site_counter_shortcode = new ShortCode_Site_Counter();
75-
$site_counter_shortcode->init();
76-
77-
$pruner = new Pruner();
78-
$pruner->init();
54+
new Dashboard;
55+
$aggregator = new Aggregator;
56+
new Plugin($aggregator);
57+
new Rest;
58+
new Shortcode_Most_Viewed_Posts;
59+
new ShortCode_Site_Counter;
60+
new Pruner;
7961

8062
if (\class_exists('WP_CLI')) {
8163
\WP_CLI::add_command('koko-analytics', 'KokoAnalytics\Command');

src/class-admin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class Admin
1212
{
13-
public function init(): void
13+
public function __construct()
1414
{
1515
global $pagenow;
1616

src/class-aggregator.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@
1212

1313
class Aggregator
1414
{
15-
/**
16-
* Indicator to prevent running running two aggregations simultaneously
17-
*/
18-
protected $running = false;
19-
20-
public function init(): void
15+
public function __construct()
2116
{
22-
add_action('koko_analytics_aggregate_stats', array( $this, 'aggregate' ));
23-
add_filter('cron_schedules', array( $this, 'add_interval' ));
24-
add_action('init', array( $this, 'maybe_setup_scheduled_event' ));
17+
add_action('koko_analytics_aggregate_stats', array($this, 'aggregate'), 10, 0);
18+
add_filter('cron_schedules', array($this, 'add_interval'), 10, 1);
19+
add_action('init', array($this, 'maybe_setup_scheduled_event'), 10, 0);
2520
}
2621

22+
/**
23+
* @param array $intervals
24+
*/
2725
public function add_interval($intervals): array
2826
{
2927
$intervals['koko_analytics_stats_aggregate_interval'] = array(
@@ -56,11 +54,6 @@ public function maybe_setup_scheduled_event(): void
5654
*/
5755
public function aggregate(): void
5856
{
59-
if ($this->running) {
60-
return;
61-
}
62-
63-
$this->running = true;
6457
update_option('koko_analytics_last_aggregation_at', time(), true);
6558

6659
// init pageview aggregator
@@ -118,7 +111,5 @@ public function aggregate(): void
118111
// tell aggregators to write their results to the database
119112
$pageview_aggregator->finish();
120113
do_action('koko_analytics_aggregate_finish');
121-
122-
$this->running = false;
123114
}
124115
}

src/class-command.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class Command
1818
* ## EXAMPLES
1919
*
2020
* wp koko-analytics aggregate
21+
*
22+
* @param array $args
23+
* @param array $assoc_args
2124
*/
2225
public function aggregate($args, $assoc_args)
2326
{

src/class-dashboard-widget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class Dashboard_Widget
1212
{
13-
public function init(): void
13+
public function __construct()
1414
{
1515
add_action('wp_dashboard_setup', array($this, 'register_dashboard_widget'), 10, 0);
1616
}

src/class-dashboard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class Dashboard
1212
{
13-
public function add_hooks()
13+
public function __construct()
1414
{
1515
add_action('init', array($this, 'maybe_show_dashboard'), 10, 0);
1616
}

src/class-migrations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function find_migrations(): array
9898
*/
9999
protected function run_migration(string $file)
100100
{
101-
if (! file_exists($file)) {
101+
if (! \file_exists($file)) {
102102
throw new Exception("Migration file $file does not exist.");
103103
}
104104

src/class-plugin.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ class Plugin
2121
public function __construct(Aggregator $aggregator)
2222
{
2323
$this->aggregator = $aggregator;
24-
}
2524

26-
public function init(): void
27-
{
28-
register_activation_hook(KOKO_ANALYTICS_PLUGIN_FILE, array( $this, 'on_activation' ));
29-
add_filter('pre_update_option_active_plugins', array( $this, 'filter_active_plugins' ));
30-
add_action('init', array( $this, 'maybe_run_db_migrations' ));
25+
register_activation_hook(KOKO_ANALYTICS_PLUGIN_FILE, array($this, 'on_activation'));
26+
add_filter('pre_update_option_active_plugins', array($this, 'filter_active_plugins'), 10, 1);
27+
add_action('init', array($this, 'maybe_run_db_migrations', 10, 0));
3128
}
3229

33-
// move koko analytics to top of active plugins
34-
// this improves performance of collecting data (if not using optimized endpoint)
30+
/**
31+
* This method moves Koko Analytics to the front of the list of currently active plugins.
32+
* This improves performance if not using the optimized endpoint.
33+
*
34+
* @param array $plugins
35+
* @return array
36+
*/
3537
public function filter_active_plugins($plugins)
3638
{
3739
if (empty($plugins)) {

src/class-pruner.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
class Pruner
1212
{
13-
public function init()
13+
public function __construct()
1414
{
15-
add_action('koko_analytics_prune_data', array( $this, 'run' ));
16-
add_action('admin_init', array( $this, 'maybe_schedule' ));
15+
add_action('koko_analytics_prune_data', array($this, 'run'), 10, 0);
16+
add_action('admin_init', array($this, 'maybe_schedule'), 10, 0);
1717
}
1818

1919
public function maybe_schedule()

src/class-rest.php

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
class Rest
1212
{
13-
public function init()
13+
public function __construct()
1414
{
15-
add_action('rest_api_init', array( $this, 'register_routes' ));
15+
add_action('rest_api_init', array($this, 'register_routes'), 10, 0);
1616
}
1717

1818
public function register_routes()
@@ -25,13 +25,13 @@ public function register_routes()
2525
'/stats',
2626
array(
2727
'methods' => 'GET',
28-
'callback' => array( $this, 'get_stats' ),
28+
'callback' => array($this, 'get_stats'),
2929
'args' => array(
3030
'start_date' => array(
31-
'validate_callback' => array( $this, 'validate_date_param' ),
31+
'validate_callback' => array($this, 'validate_date_param'),
3232
),
3333
'end_date' => array(
34-
'validate_callback' => array( $this, 'validate_date_param' ),
34+
'validate_callback' => array($this, 'validate_date_param'),
3535
),
3636
'monthly' => array(
3737
'validate_callback' => 'absint',
@@ -48,13 +48,13 @@ public function register_routes()
4848
'/totals',
4949
array(
5050
'methods' => 'GET',
51-
'callback' => array( $this, 'get_totals' ),
51+
'callback' => array($this, 'get_totals'),
5252
'args' => array(
5353
'start_date' => array(
54-
'validate_callback' => array( $this, 'validate_date_param' ),
54+
'validate_callback' => array($this, 'validate_date_param'),
5555
),
5656
'end_date' => array(
57-
'validate_callback' => array( $this, 'validate_date_param' ),
57+
'validate_callback' => array($this, 'validate_date_param'),
5858
),
5959
),
6060
'permission_callback' => function () use ($is_dashboard_public) {
@@ -68,13 +68,13 @@ public function register_routes()
6868
'/posts',
6969
array(
7070
'methods' => 'GET',
71-
'callback' => array( $this, 'get_posts' ),
71+
'callback' => array($this, 'get_posts'),
7272
'args' => array(
7373
'start_date' => array(
74-
'validate_callback' => array( $this, 'validate_date_param' ),
74+
'validate_callback' => array($this, 'validate_date_param'),
7575
),
7676
'end_date' => array(
77-
'validate_callback' => array( $this, 'validate_date_param' ),
77+
'validate_callback' => array($this, 'validate_date_param'),
7878
),
7979
),
8080
'permission_callback' => function () use ($is_dashboard_public) {
@@ -88,13 +88,13 @@ public function register_routes()
8888
'/referrers',
8989
array(
9090
'methods' => 'GET',
91-
'callback' => array( $this, 'get_referrers' ),
91+
'callback' => array($this, 'get_referrers'),
9292
'args' => array(
9393
'start_date' => array(
94-
'validate_callback' => array( $this, 'validate_date_param' ),
94+
'validate_callback' => array($this, 'validate_date_param'),
9595
),
9696
'end_date' => array(
97-
'validate_callback' => array( $this, 'validate_date_param' ),
97+
'validate_callback' => array($this, 'validate_date_param'),
9898
),
9999
),
100100
'permission_callback' => function () use ($is_dashboard_public) {
@@ -108,10 +108,10 @@ public function register_routes()
108108
'/realtime',
109109
array(
110110
'methods' => 'GET',
111-
'callback' => array( $this, 'get_realtime_pageview_count' ),
111+
'callback' => array($this, 'get_realtime_pageview_count'),
112112
'args' => array(
113113
'since' => array(
114-
'validate_callback' => array( $this, 'validate_date_param' ),
114+
'validate_callback' => array($this, 'validate_date_param'),
115115
),
116116
),
117117
'permission_callback' => function () use ($is_dashboard_public) {
@@ -138,7 +138,7 @@ private function respond($data, bool $send_cache_headers = false): \WP_REST_Resp
138138

139139
// instruct browsers to cache the response for 7 days
140140
if ($send_cache_headers) {
141-
$result->set_headers(array( 'Cache-Control' => 'max-age=604800' ));
141+
$result->set_headers(['Cache-Control' => 'max-age=604800']);
142142
}
143143
return $result;
144144
}
@@ -150,10 +150,6 @@ public function validate_date_param($param, $one, $two): bool
150150

151151
/**
152152
* Returns a daily tally of visitors and pageviews between two dates
153-
*
154-
* @param \WP_REST_Request $request
155-
*
156-
* @return \WP_REST_Response
157153
*/
158154
public function get_stats(\WP_REST_Request $request): \WP_REST_Response
159155
{
@@ -169,10 +165,6 @@ public function get_stats(\WP_REST_Request $request): \WP_REST_Response
169165

170166
/**
171167
* Returns the total number of visitos and pageviews between two dates.
172-
*
173-
* @param \WP_REST_Request $request
174-
*
175-
* @return \WP_REST_Response
176168
*/
177169
public function get_totals(\WP_REST_Request $request): \WP_REST_Response
178170
{
@@ -187,10 +179,6 @@ public function get_totals(\WP_REST_Request $request): \WP_REST_Response
187179

188180
/**
189181
* Returns the total number of pageviews and visitors per post, ordered by most pageviews first.
190-
*
191-
* @param \WP_REST_Request $request
192-
*
193-
* @return \WP_REST_Response
194182
*/
195183
public function get_posts(\WP_REST_Request $request): \WP_REST_Response
196184
{
@@ -206,10 +194,6 @@ public function get_posts(\WP_REST_Request $request): \WP_REST_Response
206194

207195
/**
208196
* Returns the total number of visitors and pageviews per referrer URL, ordered by most pageviews first.
209-
*
210-
* @param \WP_REST_Request $request
211-
*
212-
* @return \WP_REST_Response
213197
*/
214198
public function get_referrers(\WP_REST_Request $request): \WP_REST_Response
215199
{
@@ -225,9 +209,6 @@ public function get_referrers(\WP_REST_Request $request): \WP_REST_Response
225209

226210
/**
227211
* Returns the total number of recorded pageviews in the last hour
228-
*
229-
* @param \WP_REST_Request $request
230-
*
231212
* @return int|mixed
232213
*/
233214
public function get_realtime_pageview_count(\WP_REST_Request $request)

src/class-script-loader.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
class Script_Loader
1414
{
15-
public function init()
15+
public function __construct()
1616
{
17-
add_action('wp_enqueue_scripts', array( $this, 'maybe_enqueue_script' ));
18-
add_action('amp_print_analytics', array( $this, 'print_amp_analytics_tag' ));
17+
add_action('wp_enqueue_scripts', array($this, 'maybe_enqueue_script'), 10, 0);
18+
add_action('amp_print_analytics', array($this, 'print_amp_analytics_tag'), 10, 0);
1919
}
2020

2121
/**
@@ -151,6 +151,10 @@ public function print_amp_analytics_tag()
151151
echo sprintf('<amp-analytics><script type="application/json">%s</script></amp-analytics>', json_encode($config));
152152
}
153153

154+
/**
155+
* @param string $tag
156+
* @param string $handle
157+
*/
154158
public function add_async_attribute($tag, $handle)
155159
{
156160
if ($handle !== 'koko-analytics' || stripos($tag, 'defer') !== false) {

src/class-shortcode-most-viewed-posts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Shortcode_Most_Viewed_Posts
1212
{
1313
private const SHORTCODE = 'koko_analytics_most_viewed_posts';
1414

15-
public function init()
15+
public function __construct()
1616
{
1717
add_shortcode(self::SHORTCODE, array( $this, 'content' ));
1818
}

src/class-shortcode-site-counter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ShortCode_Site_Counter
2020
{
2121
const SHORTCODE = 'koko_analytics_counter';
2222

23-
public function init()
23+
public function __construct()
2424
{
2525
add_shortcode(self::SHORTCODE, array($this, 'content'));
2626
}

src/global-functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @since 1.0.25
1515
*/
16-
function koko_analyics_tracking_script()
16+
function koko_analyics_tracking_script(): void
1717
{
1818
$script_loader = new KokoAnalytics\Script_Loader();
1919
$script_loader->maybe_enqueue_script(true);

0 commit comments

Comments
 (0)