Repository navigation
Expand file tree
/
Copy pathowa-plugin.php
More file actions
1195 lines (879 loc) · 32 KB
/
Copy pathowa-plugin.php
File metadata and controls
1195 lines (879 loc) · 32 KB
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Open Web Analytics
Plugin URI: http://www.openwebanalytics.com
Description: This plugin enables Wordpress blog owners to use the Open Web Analytics Framework.
Author: Peter Adams
Version: 2.4.0
Requires at least: 5.2
Requires PHP: 8.2
Author URI: http://www.openwebanalytics.com
*/
//
// Open Web Analytics - An Open Source Web Analytics Framework
//
// Copyright 2008 Peter Adams. All rights reserved.
//
// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Id$
//
// if this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// Define the plugin path constant
define('OWA_WP_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
// Hook package creation
add_action('plugins_loaded', [ 'owaWp_plugin', 'getInstance' ], 10 );
/////////////////////////////////////////////////////////////////////////////////
require_once( OWA_WP_PLUGIN_PATH .'/vendor/autoload.php' );
use owaWp\module;
use owaWp\util;
// page caches purge a site when its tag changes; Proxy Cache Purge reads its events on init
owaWp\cache::register();
/**
* OWA WordPress Plugin Class
*
*/
class owaWp_plugin extends module {
// cmd array
var $cmds = [];
// plugin options
var $options = [];
// SDK singleton
var $owaSdk = '';
var $adminMsgs = [];
// why the last site list could not be loaded
var $lastSitesError = '';
// shown with the Website ID list: why it is empty or short
var $sitesNotice = '';
// OWA returned its list with the saved credentials
var $sitesLoaded = false;
// OWA 2.x TrackerBundle::SITE_ID_PATTERN: the IDs it publishes a bundle for
const BUNDLE_SITE_ID_PATTERN = '/^[A-Za-z0-9_-]{1,64}$/D';
/**
* Constructor
*
*/
function __construct() {
// bail if this isn't a request type that OWA needs ot be loaded on.
if ( ! $this->isProperWordPressRequest() ) {
return;
}
// load parent constructor
$params = array();
$params['module_name'] = 'owa-wordpress';
parent::__construct( $params );
}
private function isProperWordPressRequest() {
// cron requests
if ( wp_doing_cron() ) {
return false;
}
// REST requests are not excluded: REST_REQUEST is not defined yet at
// plugins_loaded, and the block editor saves posts over REST, so
// excluding them would stop post actions from being tracked.
return true;
}
/**
* Singelton
*/
static function getInstance() {
static $o;
if ( ! isset( $o ) ) {
$o = new owaWp_plugin();
}
return $o;
}
function _init() {
// setup plugin options
$this->initOptions();
// the SDK serves the Website ID list on the settings page, which a
// site with tracking off needs too
$this->initOwaSdk();
// register WordPress hooks and filters
if ( $this->getOption('enable') ) {
// insert javascript tracking tag on all pages/posts
add_action('wp_head', array( $this,'insertTrackingTag' ), 100 );
// insert javascript tracker on admin pages (1.x only).
if ( $this->getOwaVersion() === 1 && $this->getOption('trackAdminPages') ) {
add_action('admin_head', array( $this,'insertTrackingTag' ), 100 );
}
add_action('admin_notices', array( $this, 'showNag') );
}
// the settings pages (React, src/js) and the REST routes they use
owaWp\admin::register( $this );
owaWp\rest::register( $this );
}
function addNag( $message, $class = 'notice-warning' ) {
if ( $message ) {
$this->adminMsgs[] = ['message' => $message, 'class' => $class ?: 'notice-warning'];
}
}
function showNag() {
$allowed_html = array(
'a' => array(
'href' => array(),
'title' => array(),
),
'br' => array(),
'em' => array(),
'strong' => array(),
'b' => array(),
);
$messages = $this->adminMsgs;
// the settings page shows these itself, for the version chosen on it
if ( ! $this->isSettingsScreen() ) {
foreach ( $this->setupNotices() as $notice ) {
$messages[] = [ 'message' => $notice, 'class' => 'notice-warning' ];
}
}
if ( $messages ) {
foreach ( $messages as $msg ) {
$message = wp_kses( $msg['message'], $allowed_html );
$class = esc_attr( $msg['class'] );
printf( '<div class="notice %s"><p>%s</p></div>', $class, $message );
}
}
}
private function initOptions() {
// defaults first: they are used down stream in all other hooks and classes.
foreach ( $this->registerOptions() as $key => $option ) {
if ( array_key_exists( 'default_value', $option ) ) {
$this->options[ $key ] = $option['default_value'];
}
}
// fetch plugin options from DB and override defaults.
$options = get_option( 'owa_wp' );
if ( is_array( $options ) ) {
$this->options = array_merge( $this->options, $options );
}
$this->applyNetworkOptions();
}
/**
* Determines the title of the page being requested
*
* @param string $page_type
* @return string $title
*/
function getPageTitle() {
$page_type = $this->getPageType();
if ( $page_type == "Home" ) {
$title = get_bloginfo( "name" );
} elseif ( $page_type == "Search Results" ) {
$title = "Search Results for \"" . get_search_query( false ) . "\"";
} else {
$title = wp_title($sep = '', $display = 0);
}
// WordPress titles carry HTML entities (– etc.); send the text a
// browser shows in its title bar.
return trim( html_entity_decode( wp_strip_all_tags( (string) $title ), ENT_QUOTES, 'UTF-8' ) );
}
/**
* Builds one owa_cmds.push() statement. Arguments are JSON encoded, so a
* quote, backslash or </script> in a title cannot leave the string.
*/
function makeCmd( $name, ...$args ) {
return sprintf( 'owa_cmds.push(%s);', wp_json_encode( array_merge( [ $name ], $args ), JSON_HEX_TAG | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE ) );
}
function setPageTitleCmd() {
$title = $this->getPageTitle();
// wp_title() is empty on a static front page; leave the tracker to
// read document.title rather than overwrite it with nothing.
if ( $title !== '' ) {
$this->cmds[] = $this->makeCmd( 'setPageTitle', $title );
}
}
function setUserNameCmd() {
if ( ! is_user_logged_in() ) {
return;
}
$current_user = wp_get_current_user();
$this->cmds[] = $this->makeCmd( 'setUserName', $current_user->user_login );
}
/**
* Determines the type of WordPress page
*
* @return string $type
*/
function getPageType() {
if ( is_home() ) {
$type = "Home";
} elseif ( is_attachment() ){
$type = "Attachment";
} elseif ( is_page() ) {
$type = "Page";
// general page catch, should be after more specific post types
} elseif ( is_single() ) {
$type = "Post";
} elseif ( is_feed() ) {
$type = "Feed";
} elseif ( is_author() ) {
$type = "Author";
} elseif ( is_category() ) {
$type = "Category";
} elseif ( is_search() ) {
$type = "Search Results";
} elseif ( is_month() ) {
$type = "Month";
} elseif ( is_day() ) {
$type = "Day";
} elseif ( is_year() ) {
$type = "Year";
} elseif ( is_time() ) {
$type = "Time";
} elseif ( is_tag() ) {
$type = "Tag";
} elseif ( is_tax() ) {
$type = "Taxonomy";
// general archive catch, should be after specific archive types
} elseif ( is_archive() ) {
$type = "Archive";
} elseif ( is_admin() ) {
$type = "Admin";
} else {
$type = '(not set)';
}
return $type;
}
function setDebugCmd() {
$this->cmds[] = $this->makeCmd( 'setDebug', true );
}
function setSiteIdCmd() {
$this->cmds[] = $this->makeCmd( 'setSiteId', (string) $this->getOption('siteId') );
}
function setPageTypeCmd() {
$this->cmds[] = $this->makeCmd( 'setPageType', $this->getPageType() );
}
function setTrackPageViewCmd() {
$this->cmds[] = $this->makeCmd( 'trackPageView' );
}
function setTrackClicksCmd() {
$this->cmds[] = $this->makeCmd( 'trackClicks' );
}
function setTrackDomstreamsCmd() {
$this->cmds[] = $this->makeCmd( 'trackDomStream' );
}
function cmdsToString() {
$out = '';
foreach ( $this->cmds as $cmd ) {
$out .= $cmd . " \n";
}
return $out;
}
/**
* Reads the options again, after they are saved, and the SDK with them.
*/
function reloadOptions() {
$this->options = [];
$this->owaSdk = '';
$this->initOptions();
$this->initOwaSdk();
}
/**
* Network mode: the network's server and credentials replace the site's,
* and when the network configures each site's Profile, so does the
* Website ID.
*/
function applyNetworkOptions() {
if ( ! owaWp\network::isActive() ) {
return;
}
$network = owaWp\network::options();
$this->options['owaVersion'] = '2';
$this->options['owaEndpoint'] = $network['owaEndpoint'];
$this->options['appPassword'] = $network['appPassword'];
if ( $network['assignProfiles'] ) {
$this->options['siteId'] = $network['assignedSiteIds'][ get_current_blog_id() ] ?? '';
// the network's switch for this site is the only on/off
$this->options['enable'] = owaWp\network::isSiteOn( $network['disabledSiteIds'], get_current_blog_id() ) ? 1 : 0;
}
}
/**
* The site list, fetched with the network's credentials (network mode).
*
* @return array|null
*/
function listSitesWithNetworkCredentials() {
$network = owaWp\network::options();
$config = apply_filters( 'owa_wp_sdk_config', [
'credentials' => [ 'app_password' => $network['appPassword'] ],
'instance_url' => $network['owaEndpoint'],
] );
$client = ( new OwaSdk\sdk( $config ) )->createSites();
$sites = $client->listSites();
$this->lastSitesError = is_array( $sites ) ? '' : ( implode( '; ', $client->getLastErrors() ) ?: 'no response' );
return $sites;
}
function getLastSitesError() {
return $this->lastSitesError;
}
/**
* The OWA server version the settings are for: 1 or 2.
*/
function getOwaVersion() {
return (string) $this->getOption( 'owaVersion' ) === '2' ? 2 : 1;
}
/**
* @param int|null $version 1 or 2; the saved version by default
*/
function isOwaReadyToTrack( $version = null ) {
$version = $version ?? $this->getOwaVersion();
if ( ! $this->getOption( 'owaEndpoint' ) || ! $this->getOption( 'siteId' ) ) {
return false;
}
// 2.x loads <siteId>.js, so the ID has to be a file name OWA can publish
if ( $version === 2 && ! preg_match( self::BUNDLE_SITE_ID_PATTERN, (string) $this->getOption( 'siteId' ) ) ) {
return false;
}
return true;
}
/**
* Credentials for the Website ID list are present.
*
* @param int|null $version 1 or 2; the saved version by default
*/
function isOwaSdkReady( $version = null ) {
$version = $version ?? $this->getOwaVersion();
if ( ! $this->getOption( 'owaEndpoint' ) ) {
return false;
}
if ( $version === 2 ) {
return (bool) $this->getOption( 'appPassword' );
}
return $this->getOption( 'apiKey' ) && $this->getOption( 'authKey' );
}
// init the OWA SDK
function initOwaSdk() {
if ( ! empty( $this->owaSdk ) ) {
return;
}
if ( ! $this->isOwaSdkReady() ) {
return;
}
$credentials = $this->getOwaVersion() === 2
? [ 'app_password' => $this->getOption( 'appPassword' ) ]
: [ 'api_key' => $this->getOption( 'apiKey' ), 'auth_key' => $this->getOption( 'authKey' ) ];
/**
* The OWA SDK's configuration. For example, an install that serves OWA
* over plain http and allows application passwords there can set
* 'allow_insecure_app_password' => true; the SDK refuses otherwise.
*/
$config = apply_filters( 'owa_wp_sdk_config', [
'credentials' => $credentials,
'instance_url' => $this->getOption( 'owaEndpoint' ),
] );
$this->owaSdk = new OwaSdk\sdk( $config );
}
/**
* Insert Tracking Tag
*
* Adds javascript tracking tag int <head> of all pages.
*
*/
function insertTrackingTag() {
// without an endpoint the tracker URL resolves against the page itself
// (a 404 per page view); without a site ID OWA has nothing to log to.
if ( ! $this->isOwaReadyToTrack() ) {
return;
}
if ( is_admin() ) {
// 2.x tags public pages only
if ( $this->getOwaVersion() === 2 || ! $this->getOption( 'trackAdminPages' ) || $this->isOwnAdminScreen() ) {
return;
}
}
// Don't log if the page request is a preview - Wordpress 2.x or greater
if ( function_exists( 'is_preview' ) && is_preview() ) {
return;
}
// dont log customizer previews either.
if ( function_exists( 'is_customize_preview' ) && is_customize_preview() ) {
return;
}
if ( $this->getOption( 'excludeLoggedInUsers' ) && is_user_logged_in() ) {
return;
}
// set user name in tracking for names users with wp-admin accounts
if ( $this->getOption( 'trackNamedUsers') ) {
$this->setUserNameCmd();
}
if ( $this->getOwaVersion() === 2 ) {
// the bundle sets the site ID, the Profile's features and the page view
$this->setPageTypeCmd();
$this->setPageTitleCmd();
echo $this->getBundleSnippet( $this->cmdsToString() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$this->cmds = [];
return;
}
// set any cmds
if ( $this->getOption('debug') ) {
$this->setDebugCmd();
}
$this->setSiteIdCmd();
$this->setPageTypeCmd();
$this->setPageTitleCmd();
// set track clicks command
if ( $this->getOption('trackClicks') ) {
$this->setTrackClicksCmd();
}
// set track domstream command
if ( $this->getOption('trackDomstreams') ) {
$this->setTrackDomstreamsCmd();
}
// set track page view command
$this->setTrackPageViewCmd();
// every part of the tag is escaped where it is built.
echo $this->getTrackerSnippet( $this->cmdsToString() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$this->cmds = [];
}
/**
* The URL OWA 2.x publishes the Website ID's tracking bundle at
* (TrackerBundle::url()).
*/
function getBundleUrl() {
return rtrim( esc_url_raw( $this->getOption( 'owaEndpoint' ) ), '/' ) . '/public/tracker/' . $this->getOption( 'siteId' ) . '.js';
}
/**
* The tag OWA 2.x shows on a Website ID's Tracking Tag screen
* (js_bundle_tag.php), with the page's own commands in the queue.
*/
function getBundleSnippet( $cmds ) {
$url = $this->getBundleUrl();
$tag = '';
$scheme = wp_parse_url( $url, PHP_URL_SCHEME );
$host = wp_parse_url( $url, PHP_URL_HOST );
if ( $scheme && $host ) {
$port = wp_parse_url( $url, PHP_URL_PORT );
$origin = $scheme . '://' . $host . ( $port ? ':' . $port : '' );
$tag .= sprintf( '<link rel="preconnect" href="%s">' . "\n", esc_attr( $origin ) );
$tag .= sprintf( '<link rel="dns-prefetch" href="%s">' . "\n", esc_attr( $origin ) );
}
$tag .= "<!-- Start Open Web Analytics Tracker -->\n";
$tag .= "<script>var owa_cmds = owa_cmds || [];\n" . $cmds . "</script>\n";
$tag .= sprintf( '<script async src="%s"></script>' . "\n", esc_url( $url ) );
$tag .= "<!-- End Open Web Analytics Code -->\n";
return $tag;
}
/**
* The Website ID's Tracking Tag screen in OWA 2.x, where its tracker
* settings are made.
*/
function getTagSettingsUrl() {
return trailingslashit( esc_url_raw( $this->getOption( 'owaEndpoint' ) ) )
. 'index.php?owa_do=base.sitesInvocation&owa_siteId=' . rawurlencode( (string) $this->getOption( 'siteId' ) );
}
/**
* Network mode: the Profiles a site may choose from, those that share its
* base domain. When the network configures each site's Profile, only the
* one it assigned, shown read-only.
*/
function filterSitesForNetwork( array $sites ) {
if ( ! owaWp\network::options()['assignProfiles'] ) {
return owaWp\network::filterByBaseDomain( $sites, owaWp\network::currentHost() );
}
$assigned = (string) $this->getOption( 'siteId' );
if ( $assigned === '' ) {
$this->sitesNotice = 'The network administrator has not assigned this site an Observation Profile, so it is not tracked.';
}
return array_filter( $sites, fn( $site ) => owaWp\network::rowSiteId( $site ) === $assigned );
}
/**
* Network mode: where the server and credentials come from, for the
* settings page.
*/
function getNetworkNote() {
if ( ! owaWp\network::isActive() ) {
return '';
}
return owaWp\network::options()['assignProfiles']
? sprintf( 'The network administrator manages the OWA server (%s), configures this site\'s Observation Profile and has tracking on this site turned %s.', $this->getOption( 'owaEndpoint' ), $this->getOption( 'enable' ) ? 'on' : 'off' )
: sprintf( 'The network administrator manages the OWA server (%s) and its credentials. The list shows the Observation Profiles that share this site\'s domain.', $this->getOption( 'owaEndpoint' ) );
}
/**
* What is left to set up before this site is tracked, from the saved
* settings, worded for a version (the saved one by default).
*
* @param int|null $version 1 or 2
* @return string[]
*/
function setupNotices( $version = null ) {
$version = $version ?? $this->getOwaVersion();
$notices = [];
if ( ! $this->isOwaSdkReady( $version ) ) {
$notices[] = $version === 2
? 'Enter your OWA Endpoint and an Application Password to load the list of Observation Profiles from OWA.'
: 'Enter your OWA Endpoint, API Key and Secret Auth Key to load the list of websites from OWA.';
}
// where the network sets the Observation Profile, the site admin cannot, and the page says so
$assigned = owaWp\network::isActive() && owaWp\network::options()['assignProfiles'];
// the picker shows once the credentials are in
if ( ! $assigned && $this->isOwaSdkReady( $version ) && ! $this->isOwaReadyToTrack( $version ) ) {
$notices[] = $version === 2
? 'Open Web Analytics requires that you select an Observation Profile before tracking can begin.'
: 'Open Web Analytics requires that you select a Website ID before tracking can begin.';
}
return $notices;
}
/**
* The site's OWA > Settings screen.
*/
function isSettingsScreen() {
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
return $screen && $screen->id === 'toplevel_page_owa-wordpress';
}
/**
* The plugin's own admin screens.
*/
function isOwnAdminScreen() {
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
return $screen && in_array( $screen->id, [ 'toplevel_page_owa-wordpress', 'owa_page_owa-analytics', 'toplevel_page_' . owaWp\network::MENU_SLUG . '-network' ], true );
}
/**
* The tracking tag OWA 1.14 generates on its Tracking Tag screen, with the
* plugin's commands in place of the server's.
*/
function getTrackerSnippet( $cmds ) {
$base_url = esc_url_raw( $this->getOption('owaEndpoint') );
$tag = '';
$host = wp_parse_url( $base_url, PHP_URL_HOST );
if ( $host ) {
$port = wp_parse_url( $base_url, PHP_URL_PORT );
$origin = '//' . $host . ( $port ? ':' . $port : '' );
$tag .= sprintf( '<link rel="preconnect" href="%s">' . "\n", esc_attr( $origin ) );
$tag .= sprintf( '<link rel="dns-prefetch" href="%s">' . "\n", esc_attr( $origin ) );
}
$tag .= "<!-- Start Open Web Analytics Tracker -->\n";
$tag .= '<script type="text/javascript">' . "\n";
$tag .= "//<![CDATA[\n";
$tag .= 'var owa_baseUrl = ' . wp_json_encode( $base_url, JSON_HEX_TAG | JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) . ";\n";
$tag .= "var owa_cmds = owa_cmds || [];\n";
$tag .= $cmds;
$tag .= "
(function() {
var _owa = document.createElement('script'); _owa.async = true;
owa_baseUrl = ('https:' == document.location.protocol ? window.owa_baseSecUrl || owa_baseUrl.replace(/http:/, 'https:') : owa_baseUrl );
_owa.src = owa_baseUrl + 'public/base/dist/owa.tracker.js';
var _owa_p = document.head || document.getElementsByTagName('head')[0] || document.documentElement;
_owa_p.appendChild(_owa);
}());
";
$tag .= "//]]>\n";
$tag .= "</script>\n";
$tag .= "<!-- End Open Web Analytics Code -->\n";
return $tag;
}
public function registerOptions() {
$settings = array(
'owaVersion' => array(
'default_value' => '1',
'field' => array(
'type' => 'radio',
'title' => 'OWA Server Version',
'page_name' => 'owa-wordpress',
'section' => 'general',
'description' => 'Which Open Web Analytics server this site reports to. 2.x (beta) loads the Observation Profile's tracking bundle; its tracker settings are made on the OWA server.',
'label_for' => 'OWA server version',
'options' => [ '1' => '1.x', '2' => '2.x (beta)' ],
'error_message' => ''
)
),
'enable' => array(
'default_value' => true,
'field' => array(
'type' => 'boolean',
'title' => 'Enable OWA ',
'page_name' => 'owa-wordpress',
'section' => 'general',
'description' => 'Enable OWA.',
'label_for' => 'Enable OWA.',
'error_message' => 'You must select On or Off.'
)
),
'apiKey' => array(
'default_value' => '',
'field' => array(
'owa_version' => '1',
'class' => 'owa-wp-v1',
'type' => 'secret',
'title' => 'API Key',
'page_name' => 'owa-wordpress',
'section' => 'general',
'description' => 'API key for accessing your OWA instance.',
'label_for' => 'OWA API Key',
'length' => 70,
'error_message' => ''
)
),
'authKey' => array(
'default_value' => '',
'field' => array(
'owa_version' => '1',
'class' => 'owa-wp-v1',
'type' => 'secret',
'title' => 'Secret Auth Key',
'page_name' => 'owa-wordpress',
'section' => 'general',
'description' => 'The secret OWA_AUTH_KEY found the owa-config.php file of your OWA Server instance.',
'label_for' => 'OWA Auth Key',
'length' => 70,
'error_message' => ''
)
),
'appPassword' => array(
'default_value' => '',
'field' => array(
'owa_version' => '2',
'class' => 'owa-wp-v2',
'type' => 'app_password',
'title' => 'Application Password',
'page_name' => 'owa-wordpress',
'section' => 'general',
'description' => 'An application password from your OWA 2.x server (My Preferences > Application Passwords). A read-only one is enough. OWA refuses it over plain http unless the server allows that.',
'label_for' => 'Application password',
'length' => 60,
'error_message' => ''
)
),
'owaEndpoint' => array(
'default_value' => '',
'field' => array(
'type' => 'url',
'title' => 'OWA Endpoint',
'page_name' => 'owa-wordpress',
'section' => 'general',
'description' => 'The URL of your OWA instance (e.g. https://owa.example.com/path/to/owa/), the OWA_PUBLIC_URL in its owa-config.php. Saving checks that OWA answers there. Without https:// or http://, https is tried first.',
'label_for' => 'OWA Endpoint',
'length' => 70,
'endWithSlash' => true,
'error_message' => ''
)
),
'siteId' => array(
'default_value' => '',
'field' => array(
'type' => 'select',
'title' => 'Website ID',
'title_v2' => 'Observation Profile',
'page_name' => 'owa-wordpress',
'section' => 'general',
'description' => 'Select the ID of the website you want to track. New tracked websites can be added via the OWA server admin interface.',
'description_v2' => 'Select the Observation Profile you want to track. New Observation Profiles can be added in OWA.',
'label_for' => 'Tracked website ID',
'length' => 90,
'error_message' => '',
'options' => []
)
),
'trackClicks' => array(
'default_value' => true,
'field' => array(
'owa_version' => '1',
'class' => 'owa-wp-v1',
'type' => 'boolean',
'title' => 'Track Clicks',
'page_name' => 'owa-wordpress',
'section' => 'tracking',
'description' => 'Track the clicks visitors make on your web pages.',
'label_for' => 'Track clicks within a web page',
'error_message' => 'You must select On or Off.'
)
),
'trackDomstreams' => array(
'default_value' => false,
'field' => array(
'owa_version' => '1',
'class' => 'owa-wp-v1',
'type' => 'boolean',
'title' => 'Track Domstreams',
'page_name' => 'owa-wordpress',
'section' => 'tracking',
'description' => 'Record visitor mouse movements on each web page.',
'label_for' => 'Record mouse movements',
'error_message' => 'You must select On or Off.'
)
),
'trackNamedUsers' => array(
'default_value' => true,
'field' => array(
'type' => 'boolean',
'title' => 'Track Named Users',
'page_name' => 'owa-wordpress',
'section' => 'tracking',
'description' => 'Send the user name (login) of logged-in WordPress users with their page views.',
'label_for' => 'Track named users',
'error_message' => 'You must select On or Off.'
)
),
'excludeLoggedInUsers' => array(
'default_value' => true,
'field' => array(
'type' => 'boolean',
'title' => 'Don't Track Logged-in Users',
'page_name' => 'owa-wordpress',
'section' => 'tracking',
'description' => 'No tracking tag for visitors who are logged in to WordPress.',
'label_for' => 'Do not track logged-in users',
'error_message' => ''
)
),
'trackAdminPages' => array(
'default_value' => false,
'field' => array(
'owa_version' => '1',
'class' => 'owa-wp-v1',
'type' => 'boolean',
'title' => 'Track WP Admin Pages',
'page_name' => 'owa-wordpress',
'section' => 'tracking',
'description' => 'Track WordPress admin interface pages (/wp-admin...)',
'label_for' => 'Track WP admin pages',