-
Notifications
You must be signed in to change notification settings - Fork 291
/
Modules.php
1007 lines (911 loc) · 27.9 KB
/
Modules.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
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
/**
* Class Google\Site_Kit\Core\Modules\Modules
*
* @package Google\Site_Kit
* @copyright 2021 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/
namespace Google\Site_Kit\Core\Modules;
use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\REST_API\REST_Route;
use Google\Site_Kit\Core\REST_API\REST_Routes;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Core\Authentication\Authentication;
use Google\Site_Kit\Core\REST_API\Exception\Invalid_Datapoint_Exception;
use Google\Site_Kit\Core\Util\Feature_Flags;
use Google\Site_Kit\Modules\AdSense;
use Google\Site_Kit\Modules\Analytics;
use Google\Site_Kit\Modules\Analytics_4;
use Google\Site_Kit\Modules\Idea_Hub;
use Google\Site_Kit\Modules\Optimize;
use Google\Site_Kit\Modules\PageSpeed_Insights;
use Google\Site_Kit\Modules\Search_Console;
use Google\Site_Kit\Modules\Site_Verification;
use Google\Site_Kit\Modules\Tag_Manager;
use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
use Exception;
/**
* Class managing the different modules.
*
* @since 1.0.0
* @access private
* @ignore
*/
final class Modules {
const OPTION_ACTIVE_MODULES = 'googlesitekit_active_modules';
/**
* Plugin context.
*
* @since 1.0.0
* @var Context
*/
private $context;
/**
* Option API instance.
*
* @since 1.0.0
* @var Options
*/
private $options;
/**
* User Option API instance.
*
* @since 1.0.0
* @var User_Options
*/
private $user_options;
/**
* Authentication instance.
*
* @since 1.0.0
* @var Authentication
*/
private $authentication;
/**
* Available modules as $slug => $module pairs.
*
* @since 1.0.0
* @var array
*/
private $modules = array();
/**
* Map of module slugs and which other modules they depend on.
*
* @since 1.0.0
* @var array
*/
private $dependencies = array();
/**
* Map of module slugs and which other modules depend on them.
*
* @since 1.0.0
* @var array
*/
private $dependants = array();
/**
* Module_Registry instance.
*
* @since 1.21.0
* @var Module_Registry
*/
private $registry;
/**
* Core module class names.
*
* @since 1.21.0
* @var string[] Core module class names.
*/
private $core_modules = array(
Site_Verification::class,
Search_Console::class,
Analytics::class,
Optimize::class,
Tag_Manager::class,
AdSense::class,
PageSpeed_Insights::class,
);
/**
* Constructor.
*
* @since 1.0.0
*
* @param Context $context Plugin context.
* @param Options $options Optional. Option API instance. Default is a new instance.
* @param User_Options $user_options Optional. User Option API instance. Default is a new instance.
* @param Authentication $authentication Optional. Authentication instance. Default is a new instance.
*/
public function __construct(
Context $context,
Options $options = null,
User_Options $user_options = null,
Authentication $authentication = null
) {
$this->context = $context;
$this->options = $options ?: new Options( $this->context );
$this->user_options = $user_options ?: new User_Options( $this->context );
$this->authentication = $authentication ?: new Authentication( $this->context, $this->options, $this->user_options );
if ( Feature_Flags::enabled( 'ga4setup' ) ) {
$this->core_modules[] = Analytics_4::class;
}
if ( Feature_Flags::enabled( 'ideaHubModule' ) ) {
$this->core_modules[] = Idea_Hub::class;
}
}
/**
* Registers functionality through WordPress hooks.
*
* @since 1.0.0
*/
public function register() {
add_filter(
'googlesitekit_modules_data',
function( $data ) {
foreach ( $this->get_available_modules() as $module ) {
$data[ $module->slug ] = $module->prepare_info_for_js();
$data[ $module->slug ]['active'] = $this->is_module_active( $module->slug );
$data[ $module->slug ]['setupComplete'] = $data[ $module->slug ]['active'] && $this->is_module_connected( $module->slug );
$data[ $module->slug ]['dependencies'] = $this->get_module_dependencies( $module->slug );
$data[ $module->slug ]['dependants'] = $this->get_module_dependants( $module->slug );
$data[ $module->slug ]['owner'] = null;
if ( current_user_can( 'list_users' ) && $module instanceof Module_With_Owner ) {
$owner_id = $module->get_owner_id();
if ( $owner_id ) {
$data[ $module->slug ]['owner'] = array(
'id' => $owner_id,
'login' => get_the_author_meta( 'user_login', $owner_id ),
);
}
}
}
return $data;
}
);
add_filter(
'googlesitekit_rest_routes',
function( $routes ) {
return array_merge( $routes, $this->get_rest_routes() );
}
);
add_filter(
'googlesitekit_apifetch_preload_paths',
function ( $paths ) {
$modules_routes = array(
'/' . REST_Routes::REST_ROOT . '/core/modules/data/list',
);
return array_merge( $paths, $modules_routes );
}
);
$available_modules = $this->get_available_modules();
array_walk(
$available_modules,
function( Module $module ) {
if ( $module instanceof Module_With_Settings ) {
$module->get_settings()->register();
}
}
);
add_filter(
'googlesitekit_assets',
function( $assets ) use ( $available_modules ) {
foreach ( $available_modules as $module ) {
if ( $module instanceof Module_With_Assets ) {
$assets = array_merge( $assets, $module->get_assets() );
}
}
return $assets;
}
);
$active_modules = $this->get_active_modules();
array_walk(
$active_modules,
function( Module $module ) {
$module->register();
}
);
add_filter(
'googlesitekit_apifetch_preload_paths',
function ( $paths ) use ( $active_modules ) {
$settings_routes = array_map(
function ( Module $module ) {
if ( $module instanceof Module_With_Settings ) {
return '/' . REST_Routes::REST_ROOT . "/modules/{$module->slug}/data/settings";
}
return null;
},
array_values( $active_modules )
);
return array_merge( $paths, array_filter( $settings_routes ) );
}
);
}
/**
* Gets the available modules.
*
* @since 1.0.0
*
* @return array Available modules as $slug => $module pairs.
*/
public function get_available_modules() {
if ( empty( $this->modules ) ) {
$module_classes = $this->get_registry()->get_all();
foreach ( $module_classes as $module_class ) {
$instance = new $module_class( $this->context, $this->options, $this->user_options, $this->authentication );
$this->modules[ $instance->slug ] = $instance;
$this->dependencies[ $instance->slug ] = array();
$this->dependants[ $instance->slug ] = array();
}
uasort(
$this->modules,
function( Module $a, Module $b ) {
if ( $a->order === $b->order ) {
return 0;
}
return ( $a->order < $b->order ) ? -1 : 1;
}
);
// Set up dependency maps.
foreach ( $this->modules as $module ) {
foreach ( $module->depends_on as $dependency ) {
if ( ! isset( $this->modules[ $dependency ] ) || $module->slug === $dependency ) {
continue;
}
$this->dependencies[ $module->slug ][] = $dependency;
$this->dependants[ $dependency ][] = $module->slug;
}
}
}
return $this->modules;
}
/**
* Gets the active modules.
*
* @since 1.0.0
*
* @return array Active modules as $slug => $module pairs.
*/
public function get_active_modules() {
$modules = $this->get_available_modules();
$option = $this->get_active_modules_option();
return array_merge(
// Force-active modules.
array_filter(
$modules,
function( Module $module ) {
return $module->force_active;
}
),
// Manually active modules.
array_intersect_key(
$modules,
array_flip( $option )
)
);
}
/**
* Gets the module identified by the given slug.
*
* @since 1.0.0
*
* @param string $slug Unique module slug.
* @return Module Module for the slug.
*
* @throws Exception Thrown when the module slug is invalid.
*/
public function get_module( $slug ) {
$modules = $this->get_available_modules();
if ( ! isset( $modules[ $slug ] ) ) {
/* translators: %s: module slug */
throw new Exception( sprintf( __( 'Invalid module slug %s.', 'google-site-kit' ), $slug ) );
}
return $modules[ $slug ];
}
/**
* Gets the list of module slugs the module with the given slug depends on.
*
* @since 1.0.0
*
* @param string $slug Unique module slug.
* @return array List of slugs for other modules that are dependencies.
*
* @throws Exception Thrown when the module slug is invalid.
*/
public function get_module_dependencies( $slug ) {
$modules = $this->get_available_modules();
if ( ! isset( $modules[ $slug ] ) ) {
/* translators: %s: module slug */
throw new Exception( sprintf( __( 'Invalid module slug %s.', 'google-site-kit' ), $slug ) );
}
return $this->dependencies[ $slug ];
}
/**
* Gets the list of module slugs that depend on the module with the given slug.
*
* @since 1.0.0
*
* @param string $slug Unique module slug.
* @return array List of slugs for other modules that are dependants.
*
* @throws Exception Thrown when the module slug is invalid.
*/
public function get_module_dependants( $slug ) {
$modules = $this->get_available_modules();
if ( ! isset( $modules[ $slug ] ) ) {
/* translators: %s: module slug */
throw new Exception( sprintf( __( 'Invalid module slug %s.', 'google-site-kit' ), $slug ) );
}
return $this->dependants[ $slug ];
}
/**
* Checks whether the module identified by the given slug is active.
*
* @since 1.0.0
*
* @param string $slug Unique module slug.
* @return bool True if module is active, false otherwise.
*/
public function is_module_active( $slug ) {
$modules = $this->get_active_modules();
return isset( $modules[ $slug ] );
}
/**
* Checks whether the module identified by the given slug is connected.
*
* @since 1.0.0
*
* @param string $slug Unique module slug.
* @return bool True if module is connected, false otherwise.
*/
public function is_module_connected( $slug ) {
try {
$module = $this->get_module( $slug );
} catch ( Exception $e ) {
return false;
}
return (bool) $module->is_connected();
}
/**
* Activates the module identified by the given slug.
*
* @since 1.0.0
*
* @param string $slug Unique module slug.
* @return bool True on success, false on failure.
*/
public function activate_module( $slug ) {
try {
$module = $this->get_module( $slug );
} catch ( Exception $e ) {
return false;
}
$option = $this->get_active_modules_option();
if ( in_array( $slug, $option, true ) ) {
return true;
}
$option[] = $slug;
$this->set_active_modules_option( $option );
if ( is_callable( array( $module, 'on_activation' ) ) ) {
call_user_func( array( $module, 'on_activation' ) );
}
return true;
}
/**
* Deactivates the module identified by the given slug.
*
* @since 1.0.0
*
* @param string $slug Unique module slug.
* @return bool True on success, false on failure.
*/
public function deactivate_module( $slug ) {
try {
$module = $this->get_module( $slug );
} catch ( Exception $e ) {
return false;
}
$option = $this->get_active_modules_option();
$key = array_search( $slug, $option, true );
if ( false === $key ) {
return true;
}
// Prevent deactivation if force-active.
if ( $module->force_active ) {
return false;
}
unset( $option[ $key ] );
$this->set_active_modules_option( array_values( $option ) );
if ( is_callable( array( $module, 'on_deactivation' ) ) ) {
call_user_func( array( $module, 'on_deactivation' ) );
}
return true;
}
/**
* Enqueues all module-specific assets.
*
* @since 1.7.0
*/
public function enqueue_assets() {
$available_modules = $this->get_available_modules();
array_walk(
$available_modules,
function( Module $module ) {
if ( $module instanceof Module_With_Assets ) {
$module->enqueue_assets();
}
}
);
}
/**
* Gets the configured module registry instance.
*
* @since 1.21.0
*
* @return Module_Registry
*/
protected function get_registry() {
if ( ! $this->registry instanceof Module_Registry ) {
$this->registry = $this->setup_registry();
}
return $this->registry;
}
/**
* Sets up a fresh module registry instance.
*
* @since 1.21.0
*
* @return Module_Registry
*/
protected function setup_registry() {
$registry = new Module_Registry();
foreach ( $this->core_modules as $core_module ) {
$registry->register( $core_module );
}
return $registry;
}
/**
* Gets related REST routes.
*
* @since 1.3.0
*
* @return array List of REST_Route objects.
*/
private function get_rest_routes() {
$can_authenticate = function() {
return current_user_can( Permissions::AUTHENTICATE );
};
$can_view_insights = function() {
// This accounts for routes that need to be called before user has completed setup flow.
if ( current_user_can( Permissions::SETUP ) ) {
return true;
}
return current_user_can( Permissions::VIEW_POSTS_INSIGHTS );
};
$can_manage_options = function() {
// This accounts for routes that need to be called before user has completed setup flow.
if ( current_user_can( Permissions::SETUP ) ) {
return true;
}
return current_user_can( Permissions::MANAGE_OPTIONS );
};
$get_module_schema = function () {
return $this->get_module_schema();
};
return array(
new REST_Route(
'core/modules/data/list',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => function( WP_REST_Request $request ) {
$modules = array_map(
array( $this, 'prepare_module_data_for_response' ),
$this->get_available_modules()
);
return new WP_REST_Response( array_values( $modules ) );
},
'permission_callback' => $can_authenticate,
),
),
array(
'schema' => $get_module_schema,
)
),
new REST_Route(
'core/modules/data/activation',
array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => function( WP_REST_Request $request ) {
$data = $request['data'];
$slug = isset( $data['slug'] ) ? $data['slug'] : '';
try {
$this->get_module( $slug );
} catch ( Exception $e ) {
return new WP_Error( 'invalid_module_slug', $e->getMessage() );
}
$modules = $this->get_available_modules();
if ( ! empty( $data['active'] ) ) {
// Prevent activation if one of the dependencies is not active.
$dependency_slugs = $this->get_module_dependencies( $slug );
foreach ( $dependency_slugs as $dependency_slug ) {
if ( ! $this->is_module_active( $dependency_slug ) ) {
/* translators: %s: module name */
return new WP_Error( 'inactive_dependencies', sprintf( __( 'Module cannot be activated because of inactive dependency %s.', 'google-site-kit' ), $modules[ $dependency_slug ]->name ), array( 'status' => 500 ) );
}
}
if ( ! $this->activate_module( $slug ) ) {
return new WP_Error( 'cannot_activate_module', __( 'An internal error occurred while trying to activate the module.', 'google-site-kit' ), array( 'status' => 500 ) );
}
} else {
// Automatically deactivate dependants.
$dependant_slugs = $this->get_module_dependants( $slug );
foreach ( $dependant_slugs as $dependant_slug ) {
if ( $this->is_module_active( $dependant_slug ) ) {
if ( ! $this->deactivate_module( $dependant_slug ) ) {
/* translators: %s: module name */
return new WP_Error( 'cannot_deactivate_dependant', sprintf( __( 'Module cannot be deactivated because deactivation of dependant %s failed.', 'google-site-kit' ), $modules[ $dependant_slug ]->name ), array( 'status' => 500 ) );
}
}
}
if ( ! $this->deactivate_module( $slug ) ) {
return new WP_Error( 'cannot_deactivate_module', __( 'An internal error occurred while trying to deactivate the module.', 'google-site-kit' ), array( 'status' => 500 ) );
}
}
return new WP_REST_Response( array( 'success' => true ) );
},
'permission_callback' => $can_manage_options,
'args' => array(
'data' => array(
'type' => 'object',
'required' => true,
),
),
),
),
array(
'schema' => $get_module_schema,
)
),
new REST_Route(
'core/modules/data/info',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => function( WP_REST_Request $request ) {
try {
$module = $this->get_module( $request['slug'] );
} catch ( Exception $e ) {
return new WP_Error( 'invalid_module_slug', $e->getMessage() );
}
return new WP_REST_Response( $this->prepare_module_data_for_response( $module ) );
},
'permission_callback' => $can_authenticate,
'args' => array(
'slug' => array(
'type' => 'string',
'description' => __( 'Identifier for the module.', 'google-site-kit' ),
'sanitize_callback' => 'sanitize_key',
),
),
),
),
array(
'schema' => $get_module_schema,
)
),
new REST_Route(
'modules/(?P<slug>[a-z0-9\-]+)/data/notifications',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => function( WP_REST_Request $request ) {
$slug = $request['slug'];
$modules = $this->get_available_modules();
if ( ! isset( $modules[ $slug ] ) ) {
return new WP_Error( 'invalid_module_slug', __( 'Invalid module slug.', 'google-site-kit' ), array( 'status' => 404 ) );
}
$notifications = array();
if ( $this->is_module_active( $slug ) ) {
$notifications = $modules[ $slug ]->get_data( 'notifications' );
if ( is_wp_error( $notifications ) ) {
// Don't consider it an error if the module does not have a 'notifications' datapoint.
if ( Invalid_Datapoint_Exception::WP_ERROR_CODE === $notifications->get_error_code() ) {
$notifications = array();
}
return $notifications;
}
}
return new WP_REST_Response( $notifications );
},
'permission_callback' => $can_authenticate,
),
),
array(
'args' => array(
'slug' => array(
'type' => 'string',
'description' => __( 'Identifier for the module.', 'google-site-kit' ),
'sanitize_callback' => 'sanitize_key',
),
),
)
),
new REST_Route(
'modules/(?P<slug>[a-z0-9\-]+)/data/settings',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => function( WP_REST_Request $request ) {
$slug = $request['slug'];
try {
$module = $this->get_module( $slug );
} catch ( Exception $e ) {
return new WP_Error( 'invalid_module_slug', __( 'Invalid module slug.', 'google-site-kit' ), array( 'status' => 404 ) );
}
if ( ! $module instanceof Module_With_Settings ) {
return new WP_Error( 'invalid_module_slug', __( 'Module does not support settings.', 'google-site-kit' ), array( 'status' => 400 ) );
}
return new WP_REST_Response( $module->get_settings()->get() );
},
'permission_callback' => $can_manage_options,
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => function( WP_REST_Request $request ) {
$slug = $request['slug'];
try {
$module = $this->get_module( $slug );
} catch ( Exception $e ) {
return new WP_Error( 'invalid_module_slug', __( 'Invalid module slug.', 'google-site-kit' ), array( 'status' => 404 ) );
}
if ( ! $module instanceof Module_With_Settings ) {
return new WP_Error( 'invalid_module_slug', __( 'Module does not support settings.', 'google-site-kit' ), array( 'status' => 400 ) );
}
$module->get_settings()->merge( (array) $request['data'] );
return new WP_REST_Response( $module->get_settings()->get() );
},
'permission_callback' => $can_manage_options,
'args' => array(
'data' => array(
'type' => 'object',
'description' => __( 'Settings to set.', 'google-site-kit' ),
'validate_callback' => function( $value ) {
return is_array( $value );
},
),
),
),
),
array(
'args' => array(
'slug' => array(
'type' => 'string',
'description' => __( 'Identifier for the module.', 'google-site-kit' ),
'sanitize_callback' => 'sanitize_key',
),
),
)
),
new REST_Route(
'modules/(?P<slug>[a-z0-9\-]+)/data/(?P<datapoint>[a-z\-]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => function( WP_REST_Request $request ) {
$slug = $request['slug'];
try {
$module = $this->get_module( $slug );
} catch ( Exception $e ) {
return new WP_Error( 'invalid_module_slug', __( 'Invalid module slug.', 'google-site-kit' ), array( 'status' => 404 ) );
}
$data = $module->get_data( $request['datapoint'], $request->get_params() );
if ( is_wp_error( $data ) ) {
return $data;
}
return new WP_REST_Response( $data );
},
'permission_callback' => $can_view_insights,
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => function( WP_REST_Request $request ) {
$slug = $request['slug'];
try {
$module = $this->get_module( $slug );
} catch ( Exception $e ) {
return new WP_Error( 'invalid_module_slug', __( 'Invalid module slug.', 'google-site-kit' ), array( 'status' => 404 ) );
}
$data = isset( $request['data'] ) ? (array) $request['data'] : array();
$data = $module->set_data( $request['datapoint'], $data );
if ( is_wp_error( $data ) ) {
return $data;
}
return new WP_REST_Response( $data );
},
'permission_callback' => $can_manage_options,
'args' => array(
'data' => array(
'type' => 'object',
'description' => __( 'Data to set.', 'google-site-kit' ),
'validate_callback' => function( $value ) {
return is_array( $value );
},
),
),
),
),
array(
'args' => array(
'slug' => array(
'type' => 'string',
'description' => __( 'Identifier for the module.', 'google-site-kit' ),
'sanitize_callback' => 'sanitize_key',
),
'datapoint' => array(
'type' => 'string',
'description' => __( 'Module data point to address.', 'google-site-kit' ),
'sanitize_callback' => 'sanitize_key',
),
),
)
),
);
}
/**
* Prepares module data for a REST response according to the schema.
*
* @since 1.3.0
*
* @param Module $module Module instance.
* @return array Module REST response data.
*/
private function prepare_module_data_for_response( Module $module ) {
$module_data = array(
'slug' => $module->slug,
'name' => $module->name,
'description' => $module->description,
'homepage' => $module->homepage,
'internal' => $module->internal,
'order' => $module->order,
'forceActive' => $module->force_active,
'active' => $this->is_module_active( $module->slug ),
'connected' => $this->is_module_connected( $module->slug ),
'dependencies' => $this->get_module_dependencies( $module->slug ),
'dependants' => $this->get_module_dependants( $module->slug ),
'owner' => null,
);
if ( current_user_can( 'list_users' ) && $module instanceof Module_With_Owner ) {
$owner_id = $module->get_owner_id();
if ( $owner_id ) {
$module_data['owner'] = array(
'id' => $owner_id,
'login' => get_the_author_meta( 'user_login', $owner_id ),
);
}
}
return $module_data;
}
/**
* Gets the REST schema for a module.
*
* @since 1.3.0
*
* @return array Module REST schema.
*/
private function get_module_schema() {
return array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'module',
'type' => 'object',
'properties' => array(
'slug' => array(
'type' => 'string',
'description' => __( 'Identifier for the module.', 'google-site-kit' ),
'readonly' => true,
),
'name' => array(
'type' => 'string',
'description' => __( 'Name of the module.', 'google-site-kit' ),
'readonly' => true,
),
'description' => array(
'type' => 'string',
'description' => __( 'Description of the module.', 'google-site-kit' ),
'readonly' => true,
),
'homepage' => array(
'type' => 'string',
'description' => __( 'The module homepage.', 'google-site-kit' ),
'format' => 'uri',
'readonly' => true,
),
'internal' => array(
'type' => 'boolean',
'description' => __( 'Whether the module is internal, thus without any UI.', 'google-site-kit' ),
'readonly' => true,
),
'active' => array(
'type' => 'boolean',
'description' => __( 'Whether the module is active.', 'google-site-kit' ),
),
'connected' => array(
'type' => 'boolean',
'description' => __( 'Whether the module setup has been completed.', 'google-site-kit' ),
'readonly' => true,
),
'dependencies' => array(
'type' => 'array',
'description' => __( 'List of slugs of other modules that the module depends on.', 'google-site-kit' ),
'items' => array(
'type' => 'string',
),
'readonly' => true,
),
'dependants' => array(
'type' => 'array',
'description' => __( 'List of slugs of other modules depending on the module.', 'google-site-kit' ),
'items' => array(
'type' => 'string',
),
'readonly' => true,
),
'owner' => array(
'type' => 'object',
'properties' => array(
'id' => array(
'type' => 'integer',
'description' => __( 'Owner ID.', 'google-site-kit' ),
'readonly' => true,
),
'login' => array(
'type' => 'string',
'description' => __( 'Owner login.', 'google-site-kit' ),
'readonly' => true,
),
),
),
),
);
}
/**
* Gets the option containing the active modules.
*
* @since 1.0.0
*
* @return array List of active module slugs.
*/
private function get_active_modules_option() {
$option = $this->options->get( self::OPTION_ACTIVE_MODULES );
if ( ! is_array( $option ) ) {
$option = $this->options->get( 'googlesitekit-active-modules' );
}
if ( ! is_array( $option ) ) {
$option = array();
}
$includes_analytics = in_array( Analytics::MODULE_SLUG, $option, true );
$includes_analytics_4 = in_array( Analytics_4::MODULE_SLUG, $option, true );
if ( $includes_analytics && ! $includes_analytics_4 ) {
$option[] = Analytics_4::MODULE_SLUG;
}
return $option;
}
/**
* Sets the option containing the active modules.
*
* @since 1.0.0
*
* @param array $option List of active module slugs.
*/
private function set_active_modules_option( array $option ) {
if ( in_array( Analytics_4::MODULE_SLUG, $option, true ) ) {