-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathPage.php
746 lines (686 loc) · 24.3 KB
/
Page.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
<?php /** @noinspection HtmlUnknownTarget */
/**
* Plausible Analytics | Settings API.
* @since 1.3.0
* @package WordPress
* @subpackage Plausible Analytics
*/
namespace Plausible\Analytics\WP\Admin\Settings;
use Exception;
use Plausible\Analytics\WP\Client;
use Plausible\Analytics\WP\ClientFactory;
use Plausible\Analytics\WP\Helpers;
class Page extends API {
const OPTION_NOT_AVAILABLE_IN_CE_HOOK = [
[
'label' => '',
'slug' => 'option_not_available_in_ce',
'type' => 'hook',
],
];
const PROXY_WARNING_HOOK = [
'label' => '',
'slug' => 'proxy_warning',
'type' => 'hook',
];
const OPTION_DISABLED_BY_PROXY_HOOK = [
'label' => '',
'slug' => 'option_disabled_by_proxy',
'type' => 'hook',
];
const API_TOKEN_MISSING_HOOK = [
'label' => '',
'slug' => 'api_token_missing',
'type' => 'hook',
];
const OPTION_DISABLED_BY_MISSING_API_TOKEN_HOOK = [
'label' => '',
'slug' => 'option_disabled_by_missing_api_token',
'type' => 'hook',
];
const ENABLE_ANALYTICS_DASH_NOTICE = [
'label' => '',
'slug' => 'enable_analytics_dashboard_notice',
'type' => 'hook',
'hook_type' => 'success',
];
const CAP_GOALS = 'goals';
const CAP_PROPS = 'props';
const CAP_FUNNELS = 'funnels';
const CAP_REVENUE = 'revenue';
/**
* @var array|array[] $fields
*/
public $fields = [];
/**
* @var ClientFactory $client_factory
*/
private $client_factory;
/**
* @var Client $client
*/
private $client;
/**
* Constructor.
* @since 1.3.0
* @access public
* @return void
* @throws Exception
*/
public function __construct() {
$this->init();
$settings = Helpers::get_settings();
$this->client_factory = new ClientFactory();
$this->client = $this->client_factory->build();
$this->fields = [
'general' => [
[
'label' => esc_html__( 'Connect your website with Plausible Analytics', 'plausible-analytics' ),
'slug' => 'connect_to_plausible_analytics',
'type' => 'group',
'desc' => sprintf(
wp_kses(
__(
'Ensure your domain name matches the one in <a href="%s" target="_blank">your Plausible account</a>, then <a class="hover:cursor-pointer underline plausible-create-api-token">create a Plugin Token</a> (link opens in a new window) and paste it into the \'Plugin Token\' field.',
'plausible-analytics'
),
'post'
),
Helpers::get_hosted_domain_url() . '/sites'
),
'fields' => [
[
'label' => esc_html__( 'Domain name', 'plausible-analytics' ),
'slug' => 'domain_name',
'type' => 'text',
'value' => Helpers::get_domain(),
],
[
'label' => esc_html__( 'Plugin Token', 'plausible-analytics' ) .
' - ' .
'<a class="hover:cursor-pointer underline plausible-create-api-token">' .
__( 'Create Token', 'plausible-analytics' ) .
'</a>',
'slug' => 'api_token',
'type' => 'text',
'value' => $settings[ 'api_token' ],
],
[
'label' => empty( $settings[ 'domain_name' ] ) || empty( $settings[ 'api_token' ] ) ? esc_html__( 'Connect', 'plausible-analytics' ) :
esc_html__( 'Connected', 'plausible-analytics' ),
'slug' => 'connect_plausible_analytics',
'type' => 'button',
'disabled' => empty( $settings[ 'domain_name' ] ) || empty( $settings[ 'api_token' ] ) || ! $this->client instanceof Client || $this->client->is_api_token_valid(),
],
],
],
[
'label' => esc_html__( 'Enhanced measurements', 'plausible-analytics' ),
'slug' => 'enhanced_measurements',
'type' => 'group',
// translators: %1$s replaced with <code>outbound-links</code>.
'desc' => esc_html__(
'Enable enhanced measurements that you\'d like to track.',
'plausible-analytics'
),
'fields' => [
'404' => [
'label' => esc_html__( '404 error pages', 'plausible-analytics' ),
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-404-error-pages',
'slug' => 'enhanced_measurements',
'type' => 'checkbox',
'value' => '404',
'caps' => [ self::CAP_GOALS ],
],
'outbound-links' => [
'label' => esc_html__( 'Outbound links', 'plausible-analytics' ),
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-external-link-clicks',
'slug' => 'enhanced_measurements',
'type' => 'checkbox',
'value' => 'outbound-links',
'caps' => [ self::CAP_GOALS ],
],
'file-downloads' => [
'label' => esc_html__( 'File downloads', 'plausible-analytics' ),
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-file-downloads',
'slug' => 'enhanced_measurements',
'type' => 'checkbox',
'value' => 'file-downloads',
'caps' => [ self::CAP_GOALS ],
],
'search' => [
'label' => esc_html__( 'Search queries', 'plausible-analytics' ),
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-enable-site-search-tracking',
'slug' => 'enhanced_measurements',
'type' => 'checkbox',
'value' => 'search',
'caps' => [ self::CAP_GOALS, self::CAP_PROPS ],
],
'tagged-events' => [
'label' => esc_html__( 'Custom events', 'plausible-analytics' ),
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-setup-custom-events-to-track-goal-conversions',
'slug' => 'enhanced_measurements',
'type' => 'checkbox',
'value' => 'tagged-events',
'caps' => [ self::CAP_GOALS ],
],
'revenue' => [
'label' => esc_html__( 'Ecommerce revenue', 'plausible-analytics' ),
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-ecommerce-revenue',
'slug' => 'enhanced_measurements',
'type' => 'checkbox',
'value' => 'revenue',
'caps' => [ self::CAP_GOALS, self::CAP_FUNNELS, self::CAP_PROPS, self::CAP_REVENUE ],
],
'pageview-props' => [
'label' => esc_html__( 'Authors and categories', 'plausible-analytics' ),
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-send-custom-properties',
'slug' => 'enhanced_measurements',
'type' => 'checkbox',
'value' => 'pageview-props',
'caps' => [ self::CAP_PROPS ],
],
'form-completions' => [
'label' => esc_html__( 'Form completions', 'plausible-analytics' ),
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-form-completions',
'slug' => 'enhanced_measurements',
'type' => 'checkbox',
'value' => 'form-completions',
'caps' => [ self::CAP_GOALS ],
],
'hash' => [
'label' => esc_html__( 'Hash-based routing', 'plausible-analytics' ),
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-enable-hash-based-url-tracking',
'slug' => 'enhanced_measurements',
'type' => 'checkbox',
'value' => 'hash',
'caps' => [],
],
'compat' => [
'label' => esc_html__( 'IE compatibility', 'plausible-analytics' ),
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-visitors-who-use-internet-explorer',
'slug' => 'enhanced_measurements',
'type' => 'checkbox',
'value' => 'compat',
'caps' => [],
],
],
],
[
'label' => esc_html__( 'Bypass ad blockers', 'plausible-analytics' ),
'slug' => 'bypass_ad_blockers',
'type' => 'group',
'desc' => sprintf(
wp_kses(
__(
'Concerned about ad blockers? You can run the Plausible script as a first-party connection from your domain name to count visitors who use ad blockers. The proxy uses WordPress\' API with a randomly generated endpoint, starting with <code>%1$s</code> and %2$s. <a href="%3$s" target="_blank">Learn more »</a>',
'plausible-analytics'
),
wp_kses_allowed_html( 'post' )
),
get_site_url( null, rest_get_url_prefix() ),
empty(
Helpers::get_settings()[ 'proxy_enabled' ]
) ? 'a random directory/file for storing the JS file' : 'a JS file, called <code>' . str_replace(
ABSPATH,
'',
Helpers::get_proxy_resource( 'cache_dir' ) . Helpers::get_proxy_resource(
'file_alias'
) . '.js</code>'
),
'https://plausible.io/wordpress-analytics-plugin#how-to-enable-a-proxy-to-get-more-accurate-stats'
),
'toggle' => '',
'fields' => [
[
'label' => esc_html__( 'Enable proxy', 'plausible-analytics' ),
'slug' => 'proxy_enabled',
'type' => 'checkbox',
'value' => 'on',
'disabled' => ! empty( Helpers::get_settings()[ 'self_hosted_domain' ] ),
],
],
],
[
'label' => esc_html__( 'View your stats in your WordPress dashboard', 'plausible-analytics' ),
'slug' => 'is_shared_link',
'type' => 'group',
'desc' => esc_html__(
'View your site statistics within your WordPress Dashboard.',
'plausible-analytics'
),
'toggle' => '',
'fields' => [
[
'label' => esc_html__( 'View stats in WordPress', 'plausible-analytics' ),
'slug' => 'enable_analytics_dashboard',
'type' => 'checkbox',
'value' => 'on',
'disabled' => empty( Helpers::get_settings()[ 'api_token' ] ) && empty( Helpers::get_settings()[ 'self_hosted_domain' ] ),
],
],
],
[
'label' => esc_html__( 'Exclude specific pages from being tracked', 'plausible-analytics' ),
'slug' => 'is_exclude_pages',
'type' => 'group',
'desc' => sprintf(
'%1$s <a href="%2$s" target="_blank">%3$s</a>',
esc_html__(
'Exclude certain pages from being tracked. You can use an asterisk (*) to match patterns in your page URLs.',
'plausible-analytics'
),
esc_url(
'https://plausible.io/wordpress-analytics-plugin#how-to-exclude-specific-pages-from-being-tracked'
),
esc_html__( 'See syntax »', 'plausible-analytics' )
),
'toggle' => '',
'fields' => [
[
'label' => esc_html__( 'Excluded pages', 'plausible-analytics' ),
'slug' => 'excluded_pages',
'type' => 'textarea',
'value' => $settings[ 'excluded_pages' ],
'placeholder' => esc_html__(
'E.g.',
'plausible-analytics'
) . '/example-page/, *keyword*, /directory*',
],
[
'label' => __( 'Save', 'plausible-analytics' ),
'slug' => 'save-excluded-pages',
'type' => 'button',
],
],
],
[
'label' => esc_html__( 'Track analytics for user roles', 'plausible-analytics' ),
'slug' => 'tracked_user_roles',
'type' => 'group',
'desc' => esc_html__(
'By default, visits from logged in users aren\'t tracked. If you want to track visits for certain user roles then please specify them below.',
'plausible-analytics'
),
'toggle' => false,
'fields' => $this->build_user_roles_array( 'tracked_user_roles' ),
],
[
'label' => esc_html__( 'Show stats dashboard to additional user roles', 'plausible-analytics' ),
'slug' => 'expand_dashboard_access',
'type' => 'group',
'desc' => esc_html__(
'By default, the stats dashboard is only available to logged in administrators. If you want the dashboard to be available for other logged in users, then please specify them below.',
'plausible-analytics'
),
'toggle' => false,
'fields' => $this->build_user_roles_array( 'expand_dashboard_access', [ 'administrator' => true ] ),
],
[
'label' => esc_html__( 'Disable menu in toolbar', 'plausible-analytics' ),
'slug' => 'disable_toolbar_menu',
'type' => 'group',
'desc' => esc_html__(
'Check this option if you don\'t want the Plausible Analytics menu item to be added to the toolbar at the top of the screen.',
'plausible-analytics'
),
'toggle' => false,
'add_sub_array' => false,
'fields' => [
'disable_toolbar_menu' => [
'label' => esc_html__( 'Disable toolbar menu', 'plausible-analytics' ),
'slug' => 'disable_toolbar_menu',
'type' => 'checkbox',
'value' => 'on',
],
],
],
],
'self-hosted' => [
[
'label' => esc_html__( 'Plausible Community Edition', 'plausible-analytics' ),
'slug' => 'is_self_hosted',
'type' => 'group',
'desc' => sprintf(
'%1$s <a href="%2$s" target="_blank">%3$s</a>',
wp_kses(
__(
'If you\'re using Plausible Community Edition on your own infrastructure, enter the domain name where you installed it to enable the integration with your self-hosted instance. Multisites can use the <code>PLAUSIBLE_SELF_HOSTED_DOMAIN</code> constant to define the URL for all subsites at once.',
'plausible-analytics'
),
'post'
),
esc_url( 'https://plausible.io/self-hosted-web-analytics/' ),
esc_html__( 'Learn more about Plausible Community Edition.', 'plausible-analytics' )
),
'toggle' => '',
'fields' => [
[
'label' => esc_html__( 'Domain name', 'plausible-analytics' ),
'slug' => 'self_hosted_domain',
'type' => 'text',
'value' => defined( 'PLAUSIBLE_SELF_HOSTED_DOMAIN' ) ? PLAUSIBLE_SELF_HOSTED_DOMAIN : $settings[ 'self_hosted_domain' ],
'placeholder' => 'e.g. ' . Helpers::get_domain(),
'disabled' => Helpers::proxy_enabled(),
],
[
'label' => __( 'Save', 'plausible-analytics' ),
'slug' => 'save-self-hosted',
'type' => 'button',
'disabled' => Helpers::proxy_enabled(),
],
],
],
[
'label' => esc_html__( 'View stats in your WordPress dashboard', 'plausible-analytics' ),
'slug' => 'self_hosted_shared_link',
'type' => 'group',
'desc' => sprintf(
'<ol><li>' . __(
'<a href="%s" target="_blank">Create a secure and private shared link</a> in your Plausible account.',
'plausible-analytics'
) . '<li>' . __(
'Paste the shared link in the text box to view your stats in your WordPress dashboard.',
'plausible-analytics'
) . '</li>' . '</li></ol>',
esc_url( 'https://plausible.io/docs/embed-dashboard' )
),
'fields' => [
[
'label' => esc_html__( 'Shared link', 'plausible-analytics' ),
'slug' => 'self_hosted_shared_link',
'type' => 'text',
'value' => $settings[ 'self_hosted_shared_link' ],
'placeholder' => sprintf(
wp_kses( __( 'E.g. %s/share/%s?auth=XXXXXXXXXXXX', 'plausible-analytics' ), 'post' ),
Helpers::get_hosted_domain_url(),
Helpers::get_domain()
),
'disabled' => Helpers::proxy_enabled(),
],
[
'label' => __( 'Save', 'plausible-analytics' ),
'slug' => 'save-self-hosted-shared-link',
'type' => 'button',
'disabled' => Helpers::proxy_enabled(),
],
],
],
],
];
/**
* If self-hosted domain setting has a value, add option disabled notice to Ecommerce revenue toggle.
*/
if ( ! empty( $settings[ 'self_hosted_domain' ] ) ) {
$fields = $this->fields[ 'general' ][ 1 ][ 'fields' ];
array_splice( $fields, 6, 0, self::OPTION_NOT_AVAILABLE_IN_CE_HOOK );
$this->fields[ 'general' ][ 1 ][ 'fields' ] = $fields;
}
/**
* If proxy is enabled, or self-hosted domain has a value, display warning box.
* @see self::proxy_warning()
*/
if ( Helpers::proxy_enabled() || ! empty( $settings[ 'self_hosted_domain' ] ) ) {
$this->fields[ 'general' ][ 2 ][ 'fields' ][] = self::PROXY_WARNING_HOOK;
}
/**
* If proxy is enabled, disable Self-hosted fields and display a warning.
*/
if ( Helpers::proxy_enabled() ) {
$this->fields[ 'self-hosted' ][ 0 ][ 'fields' ][] = self::OPTION_DISABLED_BY_PROXY_HOOK;
$this->fields[ 'self-hosted' ][ 1 ][ 'fields' ][] = self::OPTION_DISABLED_BY_PROXY_HOOK;
}
/**
* No Plugin Token is entered.
*/
if ( empty( $settings[ 'api_token' ] ) ) {
$this->fields[ 'general' ][ 0 ][ 'fields' ][] = self::API_TOKEN_MISSING_HOOK;
$this->fields[ 'general' ][ 3 ][ 'fields' ][] = self::OPTION_DISABLED_BY_MISSING_API_TOKEN_HOOK;
}
/**
* If View Stats is enabled, display notice.
*/
if ( ! empty( $settings[ 'api_token' ] ) && ! empty( $settings[ 'enable_analytics_dashboard' ] ) ) {
$this->fields[ 'general' ][ 3 ][ 'fields' ][] = self::ENABLE_ANALYTICS_DASH_NOTICE;
}
}
/**
* Init action hooks.
* @return void
*/
private function init() {
/**
* WP Core hooks
*/
add_action( 'admin_menu', [ $this, 'register_menu' ] );
add_action( 'in_admin_header', [ $this, 'add_background_color' ] );
/**
* Hooks that run on settings page.
*/
new Hooks();
}
/**
* Load all available user roles as a list (sorted alphabetically) of checkboxes to be processed by the Settings
* API.
*
* @param string $slug
*
* @return array
*/
private function build_user_roles_array( $slug, $disable_elements = [] ) {
$wp_roles = wp_roles()->roles ?? [];
foreach ( $wp_roles as $id => $role ) {
$roles_array[ $id ] = [
'label' => $role[ 'name' ] ?? '',
'slug' => $slug,
'type' => 'checkbox',
'value' => $id,
];
if ( in_array( $id, array_keys( $disable_elements ), true ) ) {
$roles_array[ $id ][ 'disabled' ] = true;
if ( ! empty( $disable_elements[ $id ] ) ) {
$roles_array[ $id ][ 'checked' ] = $disable_elements[ $id ];
}
}
}
ksort( $roles_array, SORT_STRING );
return $roles_array;
}
/**
* Register Menu.
* @since 1.0.0
* @access public
* @return void
*/
public function register_menu() {
$user = wp_get_current_user();
$user_roles = $user->roles ?? [];
$is_allowed = false;
/**
* By default, only allow administrators to access the Statistics page.
*/
$capabilities = 'manage_options';
/**
* Let's see if current user is allowed to access the Stats page.
*/
foreach ( $user_roles as $user_role ) {
if ( in_array( $user_role, Helpers::get_settings()[ 'expand_dashboard_access' ], true ) ) {
$is_allowed = true;
break;
}
}
/**
* If current user role is allowed to access, overwrite $capabilities with this user's capabilities.
*/
if ( $is_allowed ) {
if ( isset( $user->caps ) ) {
reset( $user->caps );
$capabilities = key( $user->caps );
}
}
$settings = Helpers::get_settings();
/**
* Don't show the Analytics dashboard, if View Stats is disabled.
*/
if ( ! empty( $settings[ 'enable_analytics_dashboard' ] ) || ( ! empty( $settings[ 'self_hosted_domain' ] ) && ! empty( $settings[ 'self_hosted_shared_link' ] ) ) ) {
// Setup `Analytics` page under Dashboard.
add_dashboard_page(
esc_html__( 'Analytics', 'plausible-analytics' ),
esc_html__( 'Analytics', 'plausible-analytics' ),
$capabilities,
'plausible_analytics_statistics',
[
$this,
'render_analytics_dashboard',
]
);
}
// Setup `Plausible Analytics` page under Settings.
add_options_page(
esc_html__( 'Plausible Analytics', 'plausible-analytics' ),
esc_html__( 'Plausible Analytics', 'plausible-analytics' ),
'manage_options',
'plausible_analytics',
[
$this,
'settings_page',
]
);
}
/**
* A little hack to add some classes to the core #wpcontent div.
* @return void
*/
public function add_background_color() {
if ( array_key_exists( 'page', $_GET ) && $_GET[ 'page' ] == 'plausible_analytics' ) {
echo "<script>document.getElementById('wpcontent').classList += 'px-2.5 bg-gray-50 dark:bg-gray-85'; </script>";
}
}
/**
* Statistics Page via Embed feature.
* @since 1.2.0
* @access public
* @return void
*/
public function render_analytics_dashboard() {
global $current_user;
$settings = Helpers::get_settings();
$analytics_enabled = $settings[ 'enable_analytics_dashboard' ];
$shared_link = $settings[ 'shared_link' ] ?: '';
$self_hosted = ! empty( $settings [ 'self_hosted_domain' ] );
if ( $self_hosted ) {
$shared_link = $settings[ 'self_hosted_shared_link' ];
}
$has_access = false;
$user_roles_have_access = ! empty( $settings[ 'expand_dashboard_access' ] ) ? array_merge(
[ 'administrator' ],
$settings[ 'expand_dashboard_access' ]
) : [ 'administrator' ];
foreach ( $current_user->roles as $role ) {
if ( in_array( $role, $user_roles_have_access, true ) ) {
$has_access = true;
}
}
// Show error, if not having access.
if ( ! $has_access ) :
?>
<div class="plausible-analytics-statistics-not-loaded">
<?php
echo sprintf(
'%1$s',
esc_html__(
'You don\'t have sufficient privileges to access the analytics dashboard. Please contact administrator of the website to grant you the access.',
'plausible-analytics'
)
);
return;
?>
</div>
<?php
endif;
/**
* Prior to this version, the default value would contain an example "auth" key, i.e. XXXXXXXXX.
* When this option was saved to the database, underlying code would fail, throwing a CORS related error in browsers.
* Now, we explicitly check for the existence of this example "auth" key, and display a human-readable error message to
* those who haven't properly set it up.
* @since v1.2.5
* For self-hosters the View Stats option doesn't need to be enabled, if a Shared Link is entered, we can assume they want to View Stats.
* For regular users, the shared link is provisioned by the API, so it shouldn't be empty.
* @since v2.0.3
*/
if ( ( ! $self_hosted && ! empty( $analytics_enabled ) && ! empty( $shared_link ) ) || ( $self_hosted && ! empty( $shared_link ) ) || strpos( $shared_link, 'XXXXXX' ) !== false ) {
$page_url = isset( $_GET[ 'page-url' ] ) ? esc_url( $_GET[ 'page-url' ] ) : '';
// Append individual page URL if it exists.
if ( $shared_link && $page_url ) {
$shared_link .= "&page={$page_url}";
}
$hosted_domain = Helpers::get_hosted_domain_url();
?>
<div id="plausible-analytics-stats">
<iframe plausible-embed=""
src="<?php echo "{$shared_link}&embed=true&theme=light&background=transparent"; ?>"
loading="lazy" style="border: 0; width: 100%; height: 1750px; "></iframe>
<script async src="<?php echo $hosted_domain; ?>/js/embed.host.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
let iframe = '';
// Give iframe a chance to load.
setTimeout(function () {
iframe = document.getElementById('iFrameResizer0');
/**
* Adblocker active.
*/
if (iframe === null) {
let div = document.getElementById('plausible-analytics-stats');
div.innerHTML = '<p style="color: red;"><strong><?php echo __(
"Plausible Analytics\' statistics couldn\'t be loaded. Please disable your ad blocker.",
'plausible-analytics'
); ?></strong></p>';
}
},
1500
);
});
</script>
</div>
<?php
} else {
?>
<div class="plausible-analytics-statistics-not-loaded">
<p>
<?php if ( $settings[ 'self_hosted_domain' ] ) : ?>
<?php echo sprintf(
__(
'Please enter your <em>Shared Link</em> under <a href="%s">Self-Hosted Settings</a>.',
'plausible-analytics'
),
admin_url( 'options-general.php?page=plausible_analytics&tab=self-hosted' )
); ?>
<?php else: ?>
<?php echo sprintf(
__(
'Please <a href="%s">click here</a> to enable <strong>View Stats in WordPress</strong>.',
'plausible-analytics'
),
admin_url( 'options-general.php?page=plausible_analytics#is_shared_link' )
);
?>
<?php endif; ?>
</p>
</div>
<?php
}
}
private function token_has_cap( $caps ) {
static $stored_caps = [];
if ( empty( $stored_caps ) ) {
$stored_caps = get_option( 'plausible_analytics_api_token_caps', [] );
}
foreach ( $caps as $cap ) {
if ( empty( $stored_caps[ $cap ] ) ) {
return false;
}
}
return true;
}
}