-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrank-math-api-manager.php
More file actions
1875 lines (1615 loc) · 55.7 KB
/
rank-math-api-manager.php
File metadata and controls
1875 lines (1615 loc) · 55.7 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: Rank Math API Manager
* Plugin URI: https://devora.no/plugins/rankmath-api-manager
* Description: A WordPress extension that manages the update of Rank Math metadata (SEO Title, SEO Description, Canonical URL, Focus Keyword) via the REST API for WordPress posts and WooCommerce products.
* Version: 1.0.9.1
* Author: Devora AS
* Author URI: https://devora.no
* License: GPL v3 or later
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: rank-math-api-manager
* Update URI: https://github.com/devora-as/rank-math-api-manager
* Requires at least: 5.0
* Tested up to: 6.9.3
* Requires PHP: 7.4
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Define plugin constants
define('RANK_MATH_API_VERSION', '1.0.9.1');
define('RANK_MATH_API_PLUGIN_FILE', __FILE__);
define('RANK_MATH_API_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('RANK_MATH_API_PLUGIN_URL', plugin_dir_url(__FILE__));
/**
* Main Plugin Class
*
* @since 1.0.7
*/
class Rank_Math_API_Manager_Extended {
/**
* Plugin instance
*
* @var Rank_Math_API_Manager_Extended
*/
private static $instance = null;
/**
* Plugin data
*
* @var array
*/
private $plugin_data = null;
/**
* GitHub repository information
*
* @var array
*/
private $github_repo = array(
'owner' => 'devora-as',
'repo' => 'rank-math-api-manager',
'api_url' => 'https://api.github.com/repos/devora-as/rank-math-api-manager/releases/latest'
);
/**
* GitHub API authentication token
*
* @var string|null
*/
private $github_token = null;
/**
* Get plugin instance
*
* @return Rank_Math_API_Manager_Extended
*/
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
$this->init_plugin_data();
$this->init_hooks();
}
/**
* Initialize plugin data
*
* @since 1.0.7
*/
private function init_plugin_data() {
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$this->plugin_data = get_plugin_data( RANK_MATH_API_PLUGIN_FILE );
if ( ! is_array( $this->plugin_data ) ) {
$this->plugin_data = array();
}
// Initialize GitHub token for higher rate limits
$this->init_github_auth();
}
/**
* Initialize GitHub authentication
*
* @since 1.0.8
*/
private function init_github_auth() {
// Check for GitHub token in WordPress options (secure storage)
$this->github_token = get_option( 'rank_math_api_github_token' );
// If no token, check for environment variable
if ( ! $this->github_token && defined( 'RANK_MATH_GITHUB_TOKEN' ) ) {
$this->github_token = RANK_MATH_GITHUB_TOKEN;
}
}
/**
* Initialize WordPress hooks
*/
private function init_hooks() {
// Check dependencies first
add_action( 'plugins_loaded', [ $this, 'check_dependencies' ], 5 );
// Monitor plugin activation/deactivation
add_action( 'activated_plugin', [ $this, 'on_plugin_activated' ] );
add_action( 'deactivated_plugin', [ $this, 'on_plugin_deactivated' ] );
add_action( 'admin_init', [ $this, 'handle_notice_actions' ] );
add_action( 'admin_init', [ $this, 'register_plugin_settings' ] );
add_action( 'rank_math_api_telemetry_heartbeat', [ $this, 'send_scheduled_telemetry' ] );
// Auto-update system hooks
add_filter( 'pre_set_site_transient_update_plugins', [ $this, 'check_for_update' ] );
add_filter( 'plugins_api', [ $this, 'plugin_info' ], 10, 3 );
// Only register core functionality hooks if dependencies are met
if ( $this->are_dependencies_met() ) {
add_action( 'rest_api_init', [ $this, 'register_meta_fields' ] );
add_action( 'rest_api_init', [ $this, 'register_api_routes' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ] );
}
// Admin notices and operator actions.
add_action( 'admin_notices', [ $this, 'render_admin_notices' ] );
}
/**
* Check if required plugins are active
*
* @since 1.0.7
* @return bool True if all dependencies are met
*/
public function are_dependencies_met() {
$status = $this->get_dependency_status();
return $status['dependencies_met'];
}
/**
* Get list of required plugins
*
* @since 1.0.7
* @return array Array of required plugins
*/
private function get_required_plugins() {
return array(
array(
'name' => 'Rank Math SEO',
'file' => 'seo-by-rank-math/rank-math.php',
'version' => '1.0.0',
'url' => 'https://wordpress.org/plugins/seo-by-rank-math/',
'description' => 'Required for SEO metadata management'
)
);
}
/**
* Get supported post types for Rank Math updates.
*
* @since 1.0.8
* @return array
*/
private function get_allowed_post_types() {
$post_types = array( 'post' );
if ( class_exists( 'WooCommerce' ) ) {
$post_types[] = 'product';
}
return $post_types;
}
/**
* Get supported Rank Math meta field definitions.
*
* @since 1.0.8
* @return array
*/
private function get_supported_meta_fields() {
return array(
'rank_math_title' => array(
'description' => 'SEO Title',
'sanitize_callback' => array( $this, 'sanitize_rank_math_text_field' ),
),
'rank_math_description' => array(
'description' => 'SEO Description',
'sanitize_callback' => array( $this, 'sanitize_rank_math_text_field' ),
),
'rank_math_canonical_url' => array(
'description' => 'Canonical URL',
'sanitize_callback' => array( $this, 'sanitize_rank_math_canonical_url' ),
),
'rank_math_focus_keyword' => array(
'description' => 'Focus Keyword',
'sanitize_callback' => array( $this, 'sanitize_rank_math_focus_keyword' ),
),
);
}
/**
* Check whether a post target is supported by this plugin.
*
* @since 1.0.8
* @param int $post_id Post ID.
* @return bool
*/
private function is_supported_post_target( $post_id ) {
$post = get_post( $post_id );
if ( ! $post ) {
return false;
}
return in_array( $post->post_type, $this->get_allowed_post_types(), true );
}
/**
* Sanitize Rank Math text-based fields to mirror Rank Math behavior.
*
* @since 1.0.8
* @param mixed $value Raw field value.
* @return string
*/
public function sanitize_rank_math_text_field( $value ) {
if ( ! is_scalar( $value ) ) {
return '';
}
return wp_filter_nohtml_kses( (string) $value );
}
/**
* Sanitize Rank Math canonical URL values.
*
* @since 1.0.8
* @param mixed $value Raw field value.
* @return string
*/
public function sanitize_rank_math_canonical_url( $value ) {
if ( ! is_scalar( $value ) ) {
return '';
}
return esc_url_raw( (string) $value );
}
/**
* Sanitize Rank Math focus keyword values.
*
* @since 1.0.8
* @param mixed $value Raw field value.
* @return string
*/
public function sanitize_rank_math_focus_keyword( $value ) {
if ( ! is_scalar( $value ) ) {
return '';
}
return sanitize_text_field( (string) $value );
}
/**
* Check if a specific plugin is active
*
* @since 1.0.7
* @param string $plugin_file Plugin file path
* @return bool True if plugin is active
*/
private function is_plugin_active( $plugin_file ) {
if ( ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
return is_plugin_active( $plugin_file );
}
/**
* Check plugin dependencies and store status
*
* @since 1.0.7
*/
public function check_dependencies() {
$dependencies_met = $this->are_dependencies_met();
$current_status = get_option( 'rank_math_api_dependencies_status', false );
// Update status if it changed
if ( $current_status !== $dependencies_met ) {
update_option( 'rank_math_api_dependencies_status', $dependencies_met, false );
}
}
/**
* Get the plugin directory slug from the active installation.
*
* @since 1.0.9.1
* @return string
*/
private function get_plugin_directory_slug() {
return dirname( plugin_basename( RANK_MATH_API_PLUGIN_FILE ) );
}
/**
* Check whether the plugin is installed in the expected directory.
*
* @since 1.0.9.1
* @return bool
*/
private function is_standard_plugin_directory() {
return 'rank-math-api-manager' === $this->get_plugin_directory_slug();
}
/**
* Register plugin settings.
*
* @since 1.0.9.1
*/
public function register_plugin_settings() {
register_setting(
'rank_math_api_manager',
'rank_math_api_telemetry_settings',
array(
'type' => 'array',
'sanitize_callback' => array( $this, 'sanitize_telemetry_settings' ),
'default' => $this->get_default_telemetry_settings(),
'show_in_rest' => false,
)
);
}
/**
* Get the default telemetry settings.
*
* @since 1.0.9.1
* @return array
*/
private function get_default_telemetry_settings() {
return array(
'enabled' => true,
'site_id' => '',
);
}
/**
* Sanitize telemetry settings before persisting them.
*
* @since 1.0.9.1
* @param mixed $settings Raw settings.
* @return array
*/
public function sanitize_telemetry_settings( $settings ) {
$current_settings = $this->get_telemetry_settings();
$settings = is_array( $settings ) ? $settings : array();
$current_settings['enabled'] = ! empty( $settings['enabled'] );
if ( empty( $current_settings['site_id'] ) ) {
$current_settings['site_id'] = wp_generate_uuid4();
}
return $current_settings;
}
/**
* Get the persisted telemetry settings merged with defaults.
*
* @since 1.0.9.1
* @return array
*/
private function get_telemetry_settings() {
$settings = get_option( 'rank_math_api_telemetry_settings', array() );
$settings = is_array( $settings ) ? $settings : array();
return wp_parse_args( $settings, $this->get_default_telemetry_settings() );
}
/**
* Persist telemetry settings.
*
* @since 1.0.9.1
* @param array $settings Settings to save.
*/
private function update_telemetry_settings( array $settings ) {
update_option( 'rank_math_api_telemetry_settings', $settings, false );
}
/**
* Ensure the anonymous telemetry site ID exists.
*
* @since 1.0.9.1
* @return string
*/
private function ensure_telemetry_site_id() {
$settings = $this->get_telemetry_settings();
if ( empty( $settings['site_id'] ) ) {
$settings['site_id'] = wp_generate_uuid4();
$this->update_telemetry_settings( $settings );
}
return $settings['site_id'];
}
/**
* Schedule the recurring telemetry heartbeat.
*
* @since 1.0.9.1
*/
private function schedule_telemetry_heartbeat() {
if ( ! wp_next_scheduled( 'rank_math_api_telemetry_heartbeat' ) ) {
wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'rank_math_api_telemetry_heartbeat' );
}
}
/**
* Get the current notice event queue.
*
* @since 1.0.9.1
* @return array
*/
private function get_notice_events() {
$events = get_option( 'rank_math_api_notice_events', array() );
return is_array( $events ) ? $events : array();
}
/**
* Queue a transient admin notice event.
*
* @since 1.0.9.1
* @param string $notice_id Notice identifier.
*/
public function queue_notice_event( $notice_id ) {
$notice_id = sanitize_key( $notice_id );
if ( '' === $notice_id ) {
return;
}
$events = $this->get_notice_events();
$events[ $notice_id ] = time();
update_option( 'rank_math_api_notice_events', $events, false );
}
/**
* Check whether a transient notice event is queued.
*
* @since 1.0.9.1
* @param string $notice_id Notice identifier.
* @return bool
*/
private function has_notice_event( $notice_id ) {
$events = $this->get_notice_events();
return isset( $events[ $notice_id ] );
}
/**
* Consume a transient notice event after it has been displayed.
*
* @since 1.0.9.1
* @param string $notice_id Notice identifier.
*/
private function consume_notice_event( $notice_id ) {
$events = $this->get_notice_events();
if ( isset( $events[ $notice_id ] ) ) {
unset( $events[ $notice_id ] );
update_option( 'rank_math_api_notice_events', $events, false );
}
}
/**
* Get site-wide dismissed notices.
*
* @since 1.0.9.1
* @return array
*/
private function get_site_dismissed_notices() {
$dismissed_notices = get_option( 'rank_math_api_dismissed_notices', array() );
return is_array( $dismissed_notices ) ? $dismissed_notices : array();
}
/**
* Mark a site-wide notice as dismissed.
*
* @since 1.0.9.1
* @param string $notice_id Notice identifier.
*/
private function dismiss_notice_for_site( $notice_id ) {
$dismissed_notices = $this->get_site_dismissed_notices();
$dismissed_notices[ $notice_id ] = RANK_MATH_API_VERSION;
update_option( 'rank_math_api_dismissed_notices', $dismissed_notices, false );
}
/**
* Check whether a site-wide notice has been dismissed.
*
* @since 1.0.9.1
* @param string $notice_id Notice identifier.
* @return bool
*/
private function is_notice_dismissed_for_site( $notice_id ) {
$dismissed_notices = $this->get_site_dismissed_notices();
return isset( $dismissed_notices[ $notice_id ] );
}
/**
* Get the user-meta key used for notice dismissals.
*
* @since 1.0.9.1
* @param string $notice_id Notice identifier.
* @return string
*/
private function get_user_notice_meta_key( $notice_id ) {
return 'rank_math_api_dismissed_' . sanitize_key( $notice_id );
}
/**
* Mark a notice as dismissed for the current user.
*
* @since 1.0.9.1
* @param string $notice_id Notice identifier.
*/
private function dismiss_notice_for_user( $notice_id ) {
$user_id = get_current_user_id();
if ( $user_id ) {
update_user_meta( $user_id, $this->get_user_notice_meta_key( $notice_id ), RANK_MATH_API_VERSION );
}
}
/**
* Check whether the current user has dismissed a notice.
*
* @since 1.0.9.1
* @param string $notice_id Notice identifier.
* @return bool
*/
private function is_notice_dismissed_for_user( $notice_id ) {
$user_id = get_current_user_id();
if ( ! $user_id ) {
return false;
}
return '' !== (string) get_user_meta( $user_id, $this->get_user_notice_meta_key( $notice_id ), true );
}
/**
* Build a URL for a protected notice action.
*
* @since 1.0.9.1
* @param string $notice_action Action identifier.
* @param string $notice_id Notice identifier.
* @return string
*/
private function get_notice_action_url( $notice_action, $notice_id ) {
$url = add_query_arg(
array(
'rank_math_api_notice_action' => sanitize_key( $notice_action ),
'rank_math_api_notice_id' => sanitize_key( $notice_id ),
),
admin_url( 'plugins.php' )
);
return wp_nonce_url( $url, 'rank_math_api_notice_action', 'rank_math_api_notice_nonce' );
}
/**
* Handle notice actions coming from WordPress admin links.
* Redirects only when a known action was handled to avoid redirecting on invalid params.
*
* @since 1.0.9.1
*/
public function handle_notice_actions() {
if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
return;
}
$notice_action = isset( $_GET['rank_math_api_notice_action'] ) ? sanitize_key( wp_unslash( $_GET['rank_math_api_notice_action'] ) ) : '';
$notice_id = isset( $_GET['rank_math_api_notice_id'] ) ? sanitize_key( wp_unslash( $_GET['rank_math_api_notice_id'] ) ) : '';
$nonce = isset( $_GET['rank_math_api_notice_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['rank_math_api_notice_nonce'] ) ) : '';
if ( '' === $notice_action || '' === $notice_id || ! wp_verify_nonce( $nonce, 'rank_math_api_notice_action' ) ) {
return;
}
$handled = false;
switch ( $notice_action ) {
case 'dismiss':
$dismissible_notice_ids = array( 'folder_name_notice', 'telemetry_privacy_notice' );
if ( ! in_array( $notice_id, $dismissible_notice_ids, true ) ) {
break;
}
if ( 'folder_name_notice' === $notice_id ) {
$this->dismiss_notice_for_user( $notice_id );
} else {
$this->dismiss_notice_for_site( $notice_id );
}
$handled = true;
break;
case 'telemetry_opt_out':
$settings = $this->get_telemetry_settings();
$settings['enabled'] = false;
$this->update_telemetry_settings( $settings );
$this->dismiss_notice_for_site( 'telemetry_privacy_notice' );
$this->queue_notice_event( 'telemetry_disabled' );
$handled = true;
break;
case 'telemetry_keep_enabled':
$this->dismiss_notice_for_site( 'telemetry_privacy_notice' );
$this->queue_notice_event( 'telemetry_enabled' );
$handled = true;
break;
}
if ( ! $handled ) {
return;
}
wp_safe_redirect(
remove_query_arg(
array(
'rank_math_api_notice_action',
'rank_math_api_notice_id',
'rank_math_api_notice_nonce',
),
wp_get_referer() ? wp_get_referer() : admin_url( 'plugins.php' )
)
);
exit;
}
/**
* Determine whether the current screen should render a notice.
*
* @since 1.0.9.1
* @param array $notice Notice definition.
* @return bool
*/
private function should_render_notice_on_current_screen( array $notice ) {
if ( empty( $notice['screen_ids'] ) ) {
return true;
}
if ( ! function_exists( 'get_current_screen' ) ) {
return true;
}
$screen = get_current_screen();
if ( ! $screen || empty( $screen->id ) ) {
return true;
}
return in_array( $screen->id, $notice['screen_ids'], true );
}
/**
* Build the active admin notices for the current request.
*
* @since 1.0.9.1
* @return array
*/
private function get_active_admin_notices() {
$notices = array();
$telemetry_settings = $this->get_telemetry_settings();
if ( ! $this->are_dependencies_met() ) {
$notices[] = array(
'id' => 'dependency_issues',
'type' => 'error',
'message' => $this->get_dependency_notice_markup(),
'screen_ids' => array(),
);
}
if ( $this->has_notice_event( 'dependency_restored' ) ) {
$notices[] = array(
'id' => 'dependency_restored',
'type' => 'success',
'message' => '<p><strong>' . esc_html__( 'Rank Math API Manager', 'rank-math-api-manager' ) . '</strong>: ' . esc_html__( 'Dependencies are now met and plugin functionality is enabled again.', 'rank-math-api-manager' ) . '</p>',
'screen_ids' => array( 'plugins', 'dashboard' ),
'consume' => true,
);
}
if ( $this->has_notice_event( 'dependency_deactivated' ) ) {
$notices[] = array(
'id' => 'dependency_deactivated',
'type' => 'warning',
'message' => '<p><strong>' . esc_html__( 'Rank Math API Manager', 'rank-math-api-manager' ) . '</strong>: ' . esc_html__( 'A required dependency has been deactivated. Rank Math API Manager functionality is currently disabled.', 'rank-math-api-manager' ) . '</p>',
'screen_ids' => array( 'plugins', 'dashboard' ),
'consume' => true,
);
}
if ( $this->has_notice_event( 'telemetry_enabled' ) ) {
$notices[] = array(
'id' => 'telemetry_enabled',
'type' => 'success',
'message' => '<p><strong>' . esc_html__( 'Rank Math API Manager', 'rank-math-api-manager' ) . '</strong>: ' . esc_html__( 'Anonymous telemetry remains enabled. Only the documented minimal payload is sent to Devora Update API.', 'rank-math-api-manager' ) . '</p>',
'screen_ids' => array( 'plugins', 'dashboard' ),
'consume' => true,
);
}
if ( $this->has_notice_event( 'telemetry_disabled' ) ) {
$notices[] = array(
'id' => 'telemetry_disabled',
'type' => 'success',
'message' => '<p><strong>' . esc_html__( 'Rank Math API Manager', 'rank-math-api-manager' ) . '</strong>: ' . esc_html__( 'Anonymous telemetry has been disabled for this site.', 'rank-math-api-manager' ) . '</p>',
'screen_ids' => array( 'plugins', 'dashboard' ),
'consume' => true,
);
}
if ( ! $this->is_standard_plugin_directory() && ! $this->is_notice_dismissed_for_user( 'folder_name_notice' ) ) {
$notices[] = array(
'id' => 'folder_name_notice',
'type' => 'warning',
'message' => $this->get_folder_name_notice_markup(),
'screen_ids' => array( 'plugins', 'dashboard' ),
'actions' => array(
array(
'label' => __( 'Dismiss', 'rank-math-api-manager' ),
'url' => $this->get_notice_action_url( 'dismiss', 'folder_name_notice' ),
'class' => 'button button-secondary',
),
),
);
}
if ( ! $this->is_notice_dismissed_for_site( 'telemetry_privacy_notice' ) && ! empty( $telemetry_settings['enabled'] ) ) {
$notices[] = array(
'id' => 'telemetry_privacy_notice',
'type' => 'info',
'message' => $this->get_telemetry_notice_markup(),
'screen_ids' => array( 'plugins', 'dashboard' ),
'actions' => array(
array(
'label' => __( 'Keep enabled', 'rank-math-api-manager' ),
'url' => $this->get_notice_action_url( 'telemetry_keep_enabled', 'telemetry_privacy_notice' ),
'class' => 'button button-primary',
),
array(
'label' => __( 'Disable anonymous telemetry', 'rank-math-api-manager' ),
'url' => $this->get_notice_action_url( 'telemetry_opt_out', 'telemetry_privacy_notice' ),
'class' => 'button button-secondary',
),
),
);
}
return $notices;
}
/**
* Render active admin notices through a shared renderer.
*
* @since 1.0.9.1
*/
public function render_admin_notices() {
if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
return;
}
foreach ( $this->get_active_admin_notices() as $notice ) {
if ( ! $this->should_render_notice_on_current_screen( $notice ) ) {
continue;
}
$this->render_admin_notice( $notice );
if ( ! empty( $notice['consume'] ) ) {
$this->consume_notice_event( $notice['id'] );
}
}
}
/**
* Render a single admin notice.
*
* @since 1.0.9.1
* @param array $notice Notice definition.
*/
private function render_admin_notice( array $notice ) {
$classes = array(
'notice',
'notice-' . ( isset( $notice['type'] ) ? sanitize_html_class( $notice['type'] ) : 'info' ),
'rank-math-api-notice',
);
echo '<div class="' . esc_attr( implode( ' ', $classes ) ) . '">';
// Allow details/summary for expandable content (e.g. folder reinstall steps); WP 5.0 post list does not include them.
$allowed = wp_kses_allowed_html( 'post' );
$allowed['details'] = array( 'class' => true );
$allowed['summary'] = array();
$allowed['div'] = array_merge( isset( $allowed['div'] ) ? $allowed['div'] : array(), array( 'class' => true ) );
echo wp_kses( $notice['message'], $allowed );
if ( ! empty( $notice['actions'] ) && is_array( $notice['actions'] ) ) {
echo '<p class="rank-math-api-notice-actions">';
foreach ( $notice['actions'] as $action ) {
$label = isset( $action['label'] ) ? $action['label'] : '';
$url = isset( $action['url'] ) ? $action['url'] : '';
$class = isset( $action['class'] ) ? $action['class'] : 'button button-secondary';
echo '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $url ) . '">' . esc_html( $label ) . '</a> ';
}
echo '</p>';
}
echo '</div>';
}
/**
* Build the dependency notice markup.
*
* @since 1.0.9.1
* @return string
*/
private function get_dependency_notice_markup() {
$status = $this->get_dependency_status();
ob_start();
?>
<p><strong><?php echo esc_html__( 'Rank Math API Manager - Dependency Issues', 'rank-math-api-manager' ); ?></strong></p>
<?php if ( ! empty( $status['missing_plugins'] ) ) : ?>
<p><?php echo esc_html__( 'The following required plugins are missing or inactive:', 'rank-math-api-manager' ); ?></p>
<ul>
<?php foreach ( $status['missing_plugins'] as $plugin ) : ?>
<li>
<strong><?php echo esc_html( $plugin['name'] ); ?></strong> - <?php echo esc_html( $plugin['description'] ); ?>
<?php if ( $this->is_plugin_installed( $plugin['file'] ) ) : ?>
<a href="<?php echo esc_url( admin_url( 'plugins.php' ) ); ?>"><?php echo esc_html__( 'Activate Plugin', 'rank-math-api-manager' ); ?></a>
<?php else : ?>
<a href="<?php echo esc_url( $plugin['url'] ); ?>" target="_blank" rel="noreferrer noopener"><?php echo esc_html__( 'Install Plugin', 'rank-math-api-manager' ); ?></a>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php if ( ! empty( $status['configuration_issues'] ) ) : ?>
<p><?php echo esc_html__( 'Configuration issues detected:', 'rank-math-api-manager' ); ?></p>
<ul>
<?php foreach ( $status['configuration_issues'] as $issue ) : ?>
<li>
<strong><?php echo esc_html( $issue['plugin'] ); ?></strong>: <?php echo esc_html( $issue['issue'] ); ?>
<?php if ( isset( $issue['debug'] ) && is_array( $issue['debug'] ) ) : ?>
<br>
<small>
<strong><?php echo esc_html__( 'Debug Info', 'rank-math-api-manager' ); ?>:</strong>
<?php
$debug_parts = array();
foreach ( $issue['debug'] as $key => $value ) {
$debug_parts[] = $key . ': ' . ( is_bool( $value ) ? ( $value ? 'Yes' : 'No' ) : $value );
}
echo esc_html( implode( ', ', $debug_parts ) );
?>
</small>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php if ( ! empty( $status['recommendations'] ) ) : ?>
<p><strong><?php echo esc_html__( 'Recommendations:', 'rank-math-api-manager' ); ?></strong></p>
<ul>
<?php foreach ( $status['recommendations'] as $recommendation ) : ?>
<li><?php echo esc_html( $recommendation ); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<p><?php echo esc_html__( 'Rank Math API Manager functionality is currently disabled until all dependencies are met.', 'rank-math-api-manager' ); ?></p>
<?php
return (string) ob_get_clean();
}
/**
* Build the non-standard plugin folder notice markup.
* Uses <details>/<summary> so steps are hidden by default and expand in place (no JS).
*
* @since 1.0.9.1
* @return string
*/
private function get_folder_name_notice_markup() {
$current_directory = $this->get_plugin_directory_slug();
$releases_url = 'https://github.com/Devora-AS/rank-math-api-manager/releases';
$summary = esc_html__( 'Show reinstall steps', 'rank-math-api-manager' );
$step1 = esc_html__( 'Deactivate the plugin.', 'rank-math-api-manager' );
$step2 = esc_html__( 'Delete it (Plugins → Deactivate → Delete).', 'rank-math-api-manager' );
$step3 = sprintf(
/* translators: %s: link to GitHub Releases */
esc_html__( 'Download the latest rank-math-api-manager.zip from %s.', 'rank-math-api-manager' ),
'<a href="' . esc_url( $releases_url ) . '" target="_blank" rel="noreferrer noopener">' . esc_html__( 'GitHub Releases', 'rank-math-api-manager' ) . '</a>'
);
$step4 = esc_html__( 'Plugins → Add New → Upload Plugin → choose the ZIP → Install Now → Activate.', 'rank-math-api-manager' );
// Escape translatable parts so translations cannot inject HTML (translation safety).
$after = esc_html( __( 'The plugin will then be in wp-content/plugins/', 'rank-math-api-manager' ) )
. '<code>rank-math-api-manager</code>'
. esc_html( __( '/.', 'rank-math-api-manager' ) );
$intro = sprintf(
'<p><strong>%1$s</strong>: %2$s %3$s <code>%4$s</code>. %5$s <code>rank-math-api-manager</code>.</p>',
esc_html__( 'Rank Math API Manager', 'rank-math-api-manager' ),
esc_html__( 'This site is running the plugin from a legacy folder name.', 'rank-math-api-manager' ),
esc_html__( 'Current folder:', 'rank-math-api-manager' ),
esc_html( $current_directory ),
esc_html__( 'New releases use the folder', 'rank-math-api-manager' )
);
$steps = sprintf(
'<ol class="rank-math-api-reinstall-steps"><li>%1$s</li><li>%2$s</li><li>%3$s</li><li>%4$s</li></ol><p>%5$s</p>',
$step1,
$step2,
$step3,
$step4,
$after
);
return $intro
. '<details class="rank-math-api-notice-details">'
. '<summary>' . $summary . '</summary>'
. '<div class="rank-math-api-notice-details-content">' . $steps . '</div>'
. '</details>';
}
/**
* Build the telemetry privacy notice markup.
*
* @since 1.0.9.1
* @return string
*/
private function get_telemetry_notice_markup() {
$privacy_doc = 'https://github.com/devora-as/rank-math-api-manager/blob/main/docs/telemetry-and-privacy.md';
return sprintf(
'<p><strong>%1$s</strong>: %2$s</p><p>%3$s</p><p>%4$s <a href="%5$s" target="_blank" rel="noreferrer noopener">%6$s</a>.</p>',
esc_html__( 'Rank Math API Manager', 'rank-math-api-manager' ),
esc_html__( 'Anonymous telemetry is enabled to help Devora validate update health and compatibility.', 'rank-math-api-manager' ),
esc_html__( 'The plugin sends only the plugin slug, plugin version, WordPress version, PHP version, event type, timestamp, and an anonymous site ID. No site URL, email addresses, usernames, SEO content, or authentication data is sent.', 'rank-math-api-manager' ),
esc_html__( 'You can keep it enabled or disable it for this site at any time. See the full privacy note in', 'rank-math-api-manager' ),
esc_url( $privacy_doc ),
esc_html__( 'Telemetry and Privacy', 'rank-math-api-manager' )
);
}
/**
* Check if a plugin is installed (but not necessarily active)
*
* @since 1.0.7
* @param string $plugin_file Plugin file path
* @return bool True if plugin is installed
*/
private function is_plugin_installed( $plugin_file ) {
$plugins = get_plugins();
return isset( $plugins[ $plugin_file ] );
}
/**
* Check if Rank Math is properly configured