forked from madcitymultimedia-Admin/vaultpress
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvaultpress.php
More file actions
3066 lines (2668 loc) · 105 KB
/
vaultpress.php
File metadata and controls
3066 lines (2668 loc) · 105 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: VaultPress
* Plugin URI: http://vaultpress.com/?utm_source=plugin-uri&utm_medium=plugin-description&utm_campaign=1.0
* Description: Protect your content, themes, plugins, and settings with <strong>realtime backup</strong> and <strong>automated security scanning</strong> from <a href="http://vaultpress.com/?utm_source=wp-admin&utm_medium=plugin-description&utm_campaign=1.0" rel="nofollow">VaultPress</a>. Activate, enter your registration key, and never worry again. <a href="http://vaultpress.com/help/?utm_source=wp-admin&utm_medium=plugin-description&utm_campaign=1.0" rel="nofollow">Need some help?</a>
* Version: 3.0.1-alpha
* Author: Automattic
* Author URI: http://vaultpress.com/?utm_source=author-uri&utm_medium=plugin-description&utm_campaign=1.0
* License: GPL2+
* Text Domain: vaultpress
* Domain Path: /languages/
*
* @package automattic/vaultpress
*/
// don't call the file directly.
defined( 'ABSPATH' ) || die();
define( 'VAULTPRESS__MINIMUM_PHP_VERSION', '7.0' );
define( 'VAULTPRESS__VERSION', '3.0.1-alpha' );
define( 'VAULTPRESS__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
/**
* Load all the packages.
*
* We want to fail gracefully if `composer install` has not been executed yet, so we are checking for the autoloader.
* If the autoloader is not present, let's log the failure, pause VaultPress, and display a nice admin notice.
*/
$loader = VAULTPRESS__PLUGIN_DIR . 'vendor/autoload_packages.php';
if ( is_readable( $loader ) ) {
require $loader;
} else {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log(
wp_kses(
__( 'Your installation of VaultPress is incomplete. If you installed it from GitHub, please run <code>composer install</code>.', 'vaultpress' ),
array( 'code' => true )
)
);
}
// Add a red bubble notification to My Jetpack if the installation is bad.
add_filter(
'my_jetpack_red_bubble_notification_slugs',
function ( $slugs ) {
$slugs['vaultpress-plugin-bad-installation'] = array(
'data' => array(
'plugin' => 'VaultPress',
),
);
return $slugs;
}
);
/**
* Outputs an admin notice for folks running VaultPress without having run `composer install`.
*/
function vaultpress_admin_missing_autoloader() {
if ( get_current_screen()->id !== 'plugins' ) {
return;
}
?>
<div class="notice notice-error is-dismissible">
<p>
<?php
echo wp_kses(
__( 'Your installation of VaultPress is incomplete. If you installed it from GitHub, please run <code>composer install</code>.', 'vaultpress' ),
array( 'code' => true )
);
?>
</p>
</div>
<?php
}
add_action( 'admin_notices', 'vaultpress_admin_missing_autoloader' );
return;
}
/**
* Main VaultPress class.
*/
class VaultPress {
var $option_name = 'vaultpress';
var $auto_register_option = 'vaultpress_auto_register';
var $db_version = 4;
var $plugin_version = VAULTPRESS__VERSION;
function __construct() {
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
$this->options_blog_id = get_current_blog_id();
$options = get_option( $this->option_name );
if ( !is_array( $options ) )
$options = array();
$defaults = array(
'db_version' => 0,
'key' => '',
'secret' => '',
'connection' => false,
'service_ips_cidr' => false
);
$this->options = wp_parse_args( $options, $defaults );
$this->reset_pings();
$this->upgrade();
$this->add_global_actions_and_filters();
if ( is_admin() ) {
$this->add_admin_actions_and_filters();
}
if ( $this->is_registered() ) {
$do_not_backup = $this->get_option( 'do_not_backup' ) || $this->get_option( 'do_not_send_backup_pings' );
if ( $do_not_backup )
$this->add_vp_required_filters();
else
$this->add_listener_actions_and_filters();
}
}
static function &init() {
static $instance = false;
if ( !$instance ) {
$instance = new VaultPress();
}
return $instance;
}
static function register( $registration_key ) {
$vp = self::init();
$nonce = wp_create_nonce( 'vp_register_' . $registration_key );
$args = array( 'registration_key' => $registration_key, 'nonce' => $nonce );
$response = $vp->contact_service( 'register', $args );
// Check for an error
if ( ! empty( $response['faultCode'] ) )
return new WP_Error( $response['faultCode'], $response['faultString'] );
// Validate result
if ( empty( $response['key'] ) || empty( $response['secret'] ) || empty( $response['nonce'] ) || $nonce != $response['nonce'] )
return new WP_Error( 1, __( 'There was a problem trying to register your VaultPress subscription.' ) );
// Store the result, force a connection test.
$vp->update_option( 'key', $response['key'] );
$vp->update_option( 'secret', $response['secret'] );
$vp->check_connection( true );
return true;
}
function activate( $network_wide ) {
$type = $network_wide ? 'network' : 'single';
$this->update_option( 'activated', $type );
// force a connection check after an activation
$this->clear_connection();
if ( get_option( 'vaultpress_auto_connect' ) ) {
$this->register_via_jetpack( true );
}
}
function deactivate() {
if ( $this->is_registered() )
$this->contact_service( 'plugin_status', array( 'vp_plugin_status' => 'deactivated' ) );
}
function upgrade() {
$current_db_version = $this->get_option( 'db_version' );
if ( $current_db_version < 1 ) {
$this->options['connection'] = get_option( 'vaultpress_connection' );
$this->options['key'] = get_option( 'vaultpress_key' );
$this->options['secret'] = get_option( 'vaultpress_secret' );
$this->options['service_ips'] = get_option( 'vaultpress_service_ips' );
// remove old options
$old_options = array(
'vaultpress_connection',
'vaultpress_hostname',
'vaultpress_key',
'vaultpress_secret',
'vaultpress_service_ips',
'vaultpress_timeout',
'vp_allow_remote_execution',
'vp_debug_request_signing',
'vp_disable_firewall',
);
foreach ( $old_options as $option )
delete_option( $option );
$this->options['db_version'] = $this->db_version;
$this->update_options();
}
if ( $current_db_version < 2 ) {
$this->delete_option( 'timeout' );
$this->delete_option( 'disable_firewall' );
$this->update_option( 'db_version', $this->db_version );
$this->clear_connection();
}
if ( $current_db_version < 3 ) {
$this->update_firewall();
$this->update_option( 'db_version', $this->db_version );
$this->clear_connection();
}
if ( $current_db_version < 4 ) {
$this->update_firewall();
$this->update_option( 'db_version', $this->db_version );
$this->clear_connection();
}
}
function get_option( $key ) {
if ( 'hostname' == $key ) {
if ( defined( 'VAULTPRESS_HOSTNAME' ) )
return VAULTPRESS_HOSTNAME;
else
return 'vaultpress.com';
}
if ( 'timeout' == $key ) {
if ( defined( 'VAULTPRESS_TIMEOUT' ) )
return VAULTPRESS_TIMEOUT;
else
return 60;
}
if ( 'disable_firewall' == $key ) {
if ( defined( 'VAULTPRESS_DISABLE_FIREWALL' ) )
return VAULTPRESS_DISABLE_FIREWALL;
else
return false;
}
if ( ( 'key' == $key || 'secret' == $key ) && empty( $this->options[$key] ) ) {
return '';
}
// allow_forwarded_for can be overrided by config, or stored in or out of the vp option
if ( 'allow_forwarded_for' === $key ) {
if ( defined( 'ALLOW_FORWARDED_FOR' ) ) {
return ALLOW_FORWARDED_FOR;
}
$standalone_option = get_option( 'vaultpress_allow_forwarded_for' );
if ( ! empty( $standalone_option ) ) {
return $standalone_option;
}
}
if ( isset( $this->options[$key] ) )
return $this->options[$key];
return false;
}
function update_option( $key, $value ) {
if ( 'allow_forwarded_for' === $key ) {
update_option( 'vaultpress_allow_forwarded_for', $value );
if ( isset( $this->options[ $key ] ) ) {
unset( $this->options[ $key ] );
$this->update_options();
}
return;
}
$this->options[$key] = $value;
$this->update_options();
}
function delete_option( $key ) {
if ( 'allow_forwarded_for' === $key ) {
delete_option( 'vaultpress_allow_forwarded_for' );
}
unset( $this->options[$key] );
$this->update_options();
}
function update_options() {
// Avoid overwriting the VaultPress option if current blog_id has changed since reading it
if ( get_current_blog_id() !== $this->options_blog_id ) {
return;
}
update_option( $this->option_name, $this->options );
}
function admin_init() {
if ( !current_user_can( 'manage_options' ) )
return;
load_plugin_textdomain( 'vaultpress', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
function admin_head() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Array of hooks where we want to hook our notices.
$notice_hooks = array( 'user_admin_notices' );
/*
* In the VaultPress dashboard, move the notices.
*/
$screen = get_current_screen();
if (
! is_null( $screen )
&& in_array(
$screen->id,
array( 'jetpack_page_vaultpress', 'toplevel_page_vaultpress' ),
true
)
) {
$notice_hooks[] = 'vaultpress_notices';
} else {
$notice_hooks[] = 'admin_notices';
}
if ( $activated = $this->get_option( 'activated' ) ) {
if ( 'network' == $activated ) {
add_action( 'network_admin_notices', array( $this, 'activated_notice' ) );
} else {
foreach ( $notice_hooks as $filter ) {
add_action( $filter, array( $this, 'activated_notice' ) );
}
}
}
// ask the user to connect their site w/ VP
if ( !$this->is_registered() ) {
foreach ( $notice_hooks as $filter ) {
add_action( $filter, array( $this, 'connect_notice' ) );
}
// if we have an error make sure to let the user know about it
} else {
$error_code = $this->get_option( 'connection_error_code' );
if ( ! empty( $error_code ) ) {
foreach ( $notice_hooks as $filter ) {
add_action( $filter, array( $this, 'error_notice' ) );
}
}
}
}
function admin_menu() {
// if Jetpack is loaded then we need to wait for that menu to be added
if ( class_exists( 'Jetpack' ) )
add_action( 'jetpack_admin_menu', array( $this, 'load_menu' ) );
else
$this->load_menu();
}
function load_menu() {
if ( class_exists( 'Jetpack' ) ) {
$hook = add_submenu_page( 'jetpack', 'VaultPress', 'VaultPress', 'manage_options', 'vaultpress', array( $this, 'ui' ) );
} else {
$hook = add_menu_page( 'VaultPress', 'VaultPress', 'manage_options', 'vaultpress', array( $this, 'ui' ), 'div' );
}
add_action( "load-$hook", array( $this, 'ui_load' ) );
add_action( 'admin_print_styles', array( $this, 'styles' ) );
}
function styles() {
if ( !current_user_can( 'manage_options' ) || !is_admin() )
return;
wp_enqueue_style( 'vaultpress-nav', plugins_url( '/nav-styles.css', __FILE__ ), false, date( 'Ymd' ) );
if ( isset( $_GET['page'] ) && 'vaultpress' == $_GET['page'] )
wp_enqueue_style( 'vaultpress', plugins_url( '/styles.css', __FILE__ ), false, date( 'Ymd' ) );
}
// display a security threat notice if one exists
function toolbar( $wp_admin_bar ) {
global $wp_version;
// these new toolbar functions were introduced in 3.3
// http://codex.wordpress.org/Function_Reference/add_node
if ( version_compare( $wp_version, '3.3', '<') )
return;
if ( !current_user_can( 'manage_options' ) )
return;
$messages = $this->get_messages();
if ( !empty( $messages['security_notice_count'] ) ) {
$count = (int)$messages['security_notice_count'];
if ( $count > 0 ) {
$count = number_format( $count, 0 );
$wp_admin_bar->add_node( array(
'id' => 'vp-notice',
'title' => '<span class="ab-icon"></span>' .
sprintf( _n( '%s Security Threat', '%s Security Threats', $count , 'vaultpress'), $count ),
'parent' => 'top-secondary',
'href' => sprintf( 'https://dashboard.vaultpress.com/%d/security/', $messages['site_id'] ),
'meta' => array(
'title' => __( 'Visit VaultPress Security' , 'vaultpress'),
'onclick' => 'window.open( this.href ); return false;',
'class' => 'error'
),
) );
}
}
}
// get any messages from the VP servers
function get_messages( $force_reload = false ) {
$last_contact = $this->get_option( 'messages_last_contact' );
// only run the messages check every 30 minutes
if ( ( time() - (int)$last_contact ) > 1800 || $force_reload ) {
$messages = base64_decode( $this->contact_service( 'messages', array() ) );
$messages = unserialize( $messages );
$this->update_option( 'messages_last_contact', time() );
$this->update_option( 'messages', $messages );
} else {
$messages = $this->get_option( 'messages' );
}
return $messages;
}
function server_url() {
if ( !isset( $this->_server_url ) ) {
$scheme = is_ssl() ? 'https' : 'http';
$this->_server_url = sprintf( '%s://%s/', $scheme, $this->get_option( 'hostname' ) );
}
return $this->_server_url;
}
/**
* Show message if plugin is activated but not connected to VaultPress.
*/
function connect_notice() {
$screen = get_current_screen();
/**
* Do not display any error message if we don't have any info about the page.
*/
if ( is_null( $screen ) ) {
return;
}
/**
* Only display errors on specific pages:
* - the main dashboard.
* - the Jetpack dashboard.
* - the plugins screen.
*/
if (
in_array(
$screen->id,
array(
'dashboard',
'toplevel_page_jetpack',
'plugins',
),
true
)
) {
$message = sprintf(
wp_kses(
/* translators: URLs to VaultPress' dashboard page. */
__( 'To back up and secure your site, enter your registration key. <a href="%1$s">Register VaultPress or purchase a plan.</a>', 'vaultpress' ),
array(
'a' => array(
'href' => array(),
),
)
),
admin_url( 'admin.php?page=vaultpress' )
);
$this->ui_message( $message, 'notice', __( 'VaultPress needs your attention!', 'vaultpress' ) );
}
}
// show message after activation
function activated_notice() {
if ( 'network' == $this->get_option( 'activated' ) ) {
$message = sprintf(
__( 'Each site will need to be registered with VaultPress separately. You can purchase new keys from your <a href="%1$s">VaultPress Dashboard</a>.', 'vaultpress' ),
'https://dashboard.vaultpress.com/'
);
$this->ui_message( $message, 'activated', __( 'VaultPress has been activated across your network!', 'vaultpress' ) );
// key and secret already exist in db
} elseif ( $this->is_registered() ) {
if ( $this->check_connection() ) {
}
}
$this->delete_option( 'activated' );
}
/**
* Display an error notice when something is wrong with our VaultPress installation.
*/
public function error_notice() {
$error_message = $this->get_option( 'connection_error_message' );
// link to the VaultPress page if we're not already there.
if (
! isset( $_GET['page'] )
|| 'vaultpress' != $_GET['page']
) {
$error_message .= sprintf(
' <a href="%s">%s</a>',
admin_url( 'admin.php?page=vaultpress' ),
esc_html__( 'Visit the VaultPress page', 'vaultpress' )
);
}
$screen = get_current_screen();
/*
* Do not display any error message if we don't have a message,
* or have no info about the page.
*/
if ( is_null( $screen ) || empty( $error_message ) ) {
return;
}
/*
* Only display errors on specific pages:
* - the main dashboard.
* - the VaultPress and Jetpack dashboards.
* - the plugins screen.
*/
if (
in_array(
$screen->id,
array(
'dashboard',
'toplevel_page_jetpack',
'jetpack_page_vaultpress',
'toplevel_page_vaultpress',
'plugins',
),
true
)
) {
$this->ui_message( $error_message, 'error' );
}
}
/**
* Adds the main wrappers and the header, and defers controls to ui_render to decide which view to render.
*/
function ui() {
$ui_state = $this->ui_render();
?>
<div id="jp-plugin-container">
<?php $this->ui_masthead( $ui_state[ 'dashboard_link' ] ); ?>
<div class="vp-wrap">
<?php
/**
* Allow the display of custom notices.
*
* @since 2.0.0
*/
do_action( 'vaultpress_notices' );
?>
<?php echo $ui_state[ 'ui' ]; // This content is sanitized when it's produced. ?>
</div>
<?php $this->ui_footer(); ?>
</div>
<?php
}
/**
* Decides which UI view to render and executes it.
*
* @return array $args {
* An array of options to render the dashboard.
*
* @type string $ui Dashboard markup.
* @type string $dashboard_link Whether to show the link to the VaultPress dashboard.
* }
*/
function ui_render() {
ob_start();
if ( $this->is_localhost() ) {
$this->update_option( 'connection', time() );
$this->update_option( 'connection_error_code', 'error_localhost' );
$this->update_option(
'connection_error_message',
esc_html__( 'Hostnames such as localhost or 127.0.0.1 can not be reached by vaultpress.com and will not work with the service. Sites must be publicly accessible in order to work with VaultPress.', 'vaultpress' )
);
$this->error_notice();
return array( 'ui' => ob_get_clean(), 'dashboard_link' => false );
}
if ( ! empty( $_GET[ 'error' ] ) ) {
$this->error_notice();
$this->clear_connection();
}
if ( ! $this->is_registered() ) {
$this->ui_register();
return array( 'ui' => ob_get_clean(), 'dashboard_link' => false );
}
$status = $this->contact_service( 'status' );
if ( ! $status ) {
$error_code = $this->get_option( 'connection_error_code' );
if ( 0 == $error_code ) {
$this->ui_fatal_error();
} else {
$this->ui_register();
}
return array( 'ui' => ob_get_clean(), 'dashboard_link' => 0 != $error_code );
}
$ticker = $this->contact_service( 'ticker' );
if ( is_array( $ticker ) && isset( $ticker[ 'faultCode' ] ) ) {
$this->error_notice();
$this->ui_register();
return array( 'ui' => ob_get_clean(), 'dashboard_link' => true );
}
$this->ui_main();
return array( 'ui' => ob_get_clean(), 'dashboard_link' => true );
}
function ui_load() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( isset( $_POST['action'] ) && 'delete-vp-settings' == $_POST['action'] ) {
check_admin_referer( 'delete_vp_settings' );
$ai_ping_queue_size = $this->ai_ping_queue_size();
if ( ! empty( $ai_ping_queue_size->option_count ) && $ai_ping_queue_size->option_count > 1 ) {
$this->ai_ping_queue_delete();
}
delete_option( $this->option_name );
delete_option( 'vaultpress_service_ips_external_cidr' );
delete_option( '_vp_signatures' );
delete_option( '_vp_config_option_name_ignore' );
delete_option( '_vp_config_post_meta_name_ignore' );
delete_option( '_vp_config_should_ignore_files' );
delete_option( '_vp_current_scan' );
delete_option( 'vaultpress_auto_register' );
wp_redirect( admin_url( 'admin.php?page=vaultpress&delete-vp-settings=1' ) );
exit();
}
// run code that might be updating the registration key
if ( isset( $_POST['action'] ) && 'register' == $_POST['action'] ) {
check_admin_referer( 'vaultpress_register' );
// reset the connection info so messages don't cross
$this->clear_connection();
// if registering via Jetpack, get a key...
if ( isset( $_POST['key_source'] ) && 'jetpack' === $_POST['key_source'] ) {
$registration_key = $this->get_key_via_jetpack();
if ( is_wp_error( $registration_key ) ) {
$this->update_option( 'connection_error_code', -2 );
$this->update_option(
'connection_error_message',
sprintf( __('<strong>Failed to register VaultPress via Jetpack</strong>: %s. If you’re still having issues please <a href="%1$s">contact the VaultPress Safekeepers</a>.', 'vaultpress' ),
esc_html( $registration_key->get_error_message() ), 'http://vaultpress.com/contact/' )
);
wp_redirect( admin_url( 'admin.php?page=vaultpress&error=true' ) );
exit();
}
} else {
$registration_key = trim( $_POST[ 'registration_key' ] );
}
if ( empty( $registration_key ) ) {
$this->update_option( 'connection_error_code', 1 );
$this->update_option(
'connection_error_message',
sprintf(
__( '<strong>That\'s not a valid registration key.</strong> Head over to the <a href="%1$s" title="Sign in to your VaultPress Dashboard">VaultPress Dashboard</a> to find your key.', 'vaultpress' ),
'https://dashboard.vaultpress.com/'
)
);
wp_redirect( admin_url( 'admin.php?page=vaultpress&error=true' ) );
exit();
}
// try to register the plugin
$nonce = wp_create_nonce( 'vp_register_' . $registration_key );
$args = array( 'registration_key' => $registration_key, 'nonce' => $nonce );
$response = $this->contact_service( 'register', $args );
// we received an error from the VaultPress servers
if ( !empty( $response['faultCode'] ) ) {
$this->update_option( 'connection_error_code', $response['faultCode'] );
$this->update_option( 'connection_error_message', $response['faultString'] );
wp_redirect( admin_url( 'admin.php?page=vaultpress&error=true' ) );
exit();
}
// make sure the returned data looks valid
if ( empty( $response['key'] ) || empty( $response['secret'] ) || empty( $response['nonce'] ) || $nonce != $response['nonce'] ) {
$this->update_option( 'connection_error_code', 1 );
$this->update_option( 'connection_error_message', sprintf( __( 'There was a problem trying to register your subscription. Please try again. If you’re still having issues please <a href="%1$s">contact the VaultPress Safekeepers</a>.', 'vaultpress' ), 'http://vaultpress.com/contact/' ) );
wp_redirect( admin_url( 'admin.php?page=vaultpress&error=true' ) );
exit();
}
// need to update these values in the db so the servers can try connecting to the plugin
$this->update_option( 'key', $response['key'] );
$this->update_option( 'secret', $response['secret'] );
if ( $this->check_connection( true ) ) {
wp_redirect( admin_url( 'admin.php?page=vaultpress' ) );
exit();
}
// reset the key and secret
$this->update_option( 'key', '' );
$this->update_option( 'secret', '' );
wp_redirect( admin_url( 'admin.php?page=vaultpress&error=true' ) );
exit();
}
}
function ui_register() {
?>
<div class="vp-notice__wide">
<div class="dops-card">
<img src="<?php echo esc_url( plugins_url( 'images/security.svg', __FILE__ ) ); ?>" alt="VaultPress">
<h2><?php _e( 'The VaultPress plugin requires a subscription.', 'vaultpress' ); ?></h2>
<p><?php _e( 'Get realtime backups, automated security scanning, and support from WordPress experts.', 'vaultpress' ); ?></p>
<a class="dops-button is-primary" href="https://vaultpress.com/plans/?utm_source=plugin-unregistered&utm_medium=view-plans-and-pricing&utm_campaign=1.0-plugin" target="_blank" rel="noopener noreferrer"><?php _e( 'View plans and pricing', 'vaultpress' ); ?></a>
</div>
</div>
<div class="jp-dash-section-header">
<div class="jp-dash-section-header__label">
<h2 class="jp-dash-section-header__name">
<?php esc_html_e( 'Management', 'vaultpress' ); ?>
</h2>
</div>
</div>
<div class="vp-row">
<div class="vp-col">
<div class="dops-card dops-section-header is-compact">
<?php esc_html_e( 'Registration key', 'vaultpress' ) ?>
</div>
<div class="dops-card">
<form method="post" action="">
<fieldset>
<p>
<?php esc_html_e( 'Paste your registration key below:', 'vaultpress' ); ?>
</p>
<p>
<textarea class="dops-textarea" placeholder="<?php esc_attr_e( __( 'Enter your key here...', 'vaultpress' ) ); ?>" name="registration_key"></textarea>
</p>
<button class="dops-button is-compact"><?php _e( 'Register ', 'vaultpress' ); ?></button>
<input type="hidden" name="action" value="register" />
<?php wp_nonce_field( 'vaultpress_register' ); ?>
</fieldset>
</form>
</div>
</div>
<div class="vp-col">
<?php $this->ui_delete_vp_settings_button(); ?>
</div>
</div>
<?php
}
/**
* Renders the top header.
*
* @param bool $show_nav Whether to show navigation.
*/
function ui_masthead( $show_nav = true ) {
?>
<div class="jp-masthead">
<div class="jp-masthead__inside-container">
<div class="jp-masthead__logo-container">
<a class="jp-masthead__logo-link" href="https://vaultpress.com">
<img src="<?php echo esc_url( plugins_url( 'images/vaultpress.svg', __FILE__ ) ); ?>" alt="VaultPress">
</a>
</div>
<?php if ( $show_nav ) : ?>
<div class="jp-masthead__nav">
<div class="dops-button-group">
<a href="https://dashboard.vaultpress.com" class="dops-button is-compact" target="_blank" rel="noopener noreferrer">
<?php _e( 'Visit Dashboard', 'vaultpress' ); ?>
</a>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php
}
/**
* Renders the footer.
*/
function ui_footer() {
?>
<div class="jp-footer">
<div class="jp-footer__a8c-attr-container">
<svg role="img" class="jp-footer__a8c-attr" x="0" y="0" viewBox="0 0 935 38.2" enable-background="new 0 0 935 38.2" aria-labelledby="a8c-svg-title"><title id="a8c-svg-title">An Automattic Airline</title>
<path d="M317.1 38.2c-12.6 0-20.7-9.1-20.7-18.5v-1.2c0-9.6 8.2-18.5 20.7-18.5 12.6 0 20.8 8.9 20.8 18.5v1.2C337.9 29.1 329.7 38.2 317.1 38.2zM331.2 18.6c0-6.9-5-13-14.1-13s-14 6.1-14 13v0.9c0 6.9 5 13.1 14 13.1s14.1-6.2 14.1-13.1V18.6zM175 36.8l-4.7-8.8h-20.9l-4.5 8.8h-7L157 1.3h5.5L182 36.8H175zM159.7 8.2L152 23.1h15.7L159.7 8.2zM212.4 38.2c-12.7 0-18.7-6.9-18.7-16.2V1.3h6.6v20.9c0 6.6 4.3 10.5 12.5 10.5 8.4 0 11.9-3.9 11.9-10.5V1.3h6.7V22C231.4 30.8 225.8 38.2 212.4 38.2zM268.6 6.8v30h-6.7v-30h-15.5V1.3h37.7v5.5H268.6zM397.3 36.8V8.7l-1.8 3.1 -14.9 25h-3.3l-14.7-25 -1.8-3.1v28.1h-6.5V1.3h9.2l14 24.4 1.7 3 1.7-3 13.9-24.4h9.1v35.5H397.3zM454.4 36.8l-4.7-8.8h-20.9l-4.5 8.8h-7l19.2-35.5h5.5l19.5 35.5H454.4zM439.1 8.2l-7.7 14.9h15.7L439.1 8.2zM488.4 6.8v30h-6.7v-30h-15.5V1.3h37.7v5.5H488.4zM537.3 6.8v30h-6.7v-30h-15.5V1.3h37.7v5.5H537.3zM569.3 36.8V4.6c2.7 0 3.7-1.4 3.7-3.4h2.8v35.5L569.3 36.8 569.3 36.8zM628 11.3c-3.2-2.9-7.9-5.7-14.2-5.7 -9.5 0-14.8 6.5-14.8 13.3v0.7c0 6.7 5.4 13 15.3 13 5.9 0 10.8-2.8 13.9-5.7l4 4.2c-3.9 3.8-10.5 7.1-18.3 7.1 -13.4 0-21.6-8.7-21.6-18.3v-1.2c0-9.6 8.9-18.7 21.9-18.7 7.5 0 14.3 3.1 18 7.1L628 11.3zM321.5 12.4c1.2 0.8 1.5 2.4 0.8 3.6l-6.1 9.4c-0.8 1.2-2.4 1.6-3.6 0.8l0 0c-1.2-0.8-1.5-2.4-0.8-3.6l6.1-9.4C318.7 11.9 320.3 11.6 321.5 12.4L321.5 12.4z"></path>
<path d="M37.5 36.7l-4.7-8.9H11.7l-4.6 8.9H0L19.4 0.8H25l19.7 35.9H37.5zM22 7.8l-7.8 15.1h15.9L22 7.8zM82.8 36.7l-23.3-24 -2.3-2.5v26.6h-6.7v-36H57l22.6 24 2.3 2.6V0.8h6.7v35.9H82.8z"></path>
<path d="M719.9 37l-4.8-8.9H694l-4.6 8.9h-7.1l19.5-36h5.6l19.8 36H719.9zM704.4 8l-7.8 15.1h15.9L704.4 8zM733 37V1h6.8v36H733zM781 37c-1.8 0-2.6-2.5-2.9-5.8l-0.2-3.7c-0.2-3.6-1.7-5.1-8.4-5.1h-12.8V37H750V1h19.6c10.8 0 15.7 4.3 15.7 9.9 0 3.9-2 7.7-9 9 7 0.5 8.5 3.7 8.6 7.9l0.1 3c0.1 2.5 0.5 4.3 2.2 6.1V37H781zM778.5 11.8c0-2.6-2.1-5.1-7.9-5.1h-13.8v10.8h14.4c5 0 7.3-2.4 7.3-5.2V11.8zM794.8 37V1h6.8v30.4h28.2V37H794.8zM836.7 37V1h6.8v36H836.7zM886.2 37l-23.4-24.1 -2.3-2.5V37h-6.8V1h6.5l22.7 24.1 2.3 2.6V1h6.8v36H886.2zM902.3 37V1H935v5.6h-26v9.2h20v5.5h-20v10.1h26V37H902.3z"></path>
</svg>
</div>
<ul class="jp-footer__links">
<li class="jp-footer__link-item">
<a href="https://vaultpress.com" class="jp-footer__link" title="<?php esc_attr_e( 'VaultPress version', 'vaultpress' ) ?>" target="_blank" rel="noopener noreferrer">
<?php printf( 'VaultPress %s', $this->plugin_version ); ?>
</a>
</li>
<li class="jp-footer__link-item">
<a href="https://wordpress.com/tos/" class="jp-footer__link" title="<?php esc_attr_e( 'Terms of service', 'vaultpress' ) ?>" target="_blank" rel="noopener noreferrer">
<?php esc_html_e( 'Terms', 'vaultpress' ); ?>
</a>
</li>
</ul>
<div class="jp-power">
<a
href="<?php echo class_exists( 'Jetpack_Admin_Page' ) ? esc_url( admin_url( 'admin.php?page=jetpack' ) ) : 'https://jetpack.com' ?>"
class="jp-power__text-link"
target="_blank"
rel="noopener noreferrer"
>
<span class="jp-power__text"><?php esc_html_e( 'Powered by', 'vaultpress') ?></span> <?php echo $this->ui_logo(); ?>
</a>
</div>
</div>
<?php
}
function ui_main() {
$response = base64_decode( $this->contact_service( 'plugin_ui' ) );
echo $response;
$this->ui_delete_vp_settings_button();
}
function ui_fatal_error() {
$this->render_notice(
sprintf(
'<strong>' . __( 'We can\'t connect to %1$s.', 'vaultpress' ) . '</strong><br/>' .
__( 'Please make sure that your website is accessible via the Internet. Please contact the VaultPress support if you still have issues.' ),
esc_html( $this->get_option( 'hostname' ) )
),
'is-warning',
array(
'label' => __( 'Contact support' ),
'url' => 'https://vaultpress.com/contact/',
)
);
}
function ui_message( $message, $type = 'notice', $heading = '' ) {
$level = 'is-warning';
if ( empty( $heading ) ) {
switch ( $type ) {
case 'error':
$level = 'is-error';
$heading = __( 'Oops... there seems to be a problem.', 'vaultpress' );
break;
case 'success':
$level = 'is-success';
$heading = __( 'Yay! Things look good.', 'vaultpress' );
break;
default:
$heading = __( 'VaultPress needs your attention!', 'vaultpress' );
break;
}
}
$classes = in_array( get_current_screen()->parent_base, array( 'jetpack', 'vaultpress' ), true )
? ''
: "notice notice-$type";
$this->render_notice(
"<strong>$heading</strong><br/>$message",
$level,
array(),
$classes
);
}
/**
* Renders a notice. Can have
*
* @param string $content Notice main content.
* @param string $level Can be is-info, is-warning, is-error. By default, it's is-info.
* @param array $action {
* Arguments to display a linked action button in the notice.
*
* @type string $label The action button label.
* @type string $url The action button link.
* }
* @param string $classes This is added as a CSS class to the root node. Useful to pass WP core classes for notices.
*/
function render_notice( $content, $level = 'is-info', $action = array(), $classes = '' ) {
$allowed_html = array(
'a' => array( 'href' => true, 'target' => 'blank', 'rel' => 'noopener noreferrer' ),
'br' => true,
'strong' => true,
);
?>
<div class="dops-notice vp-notice <?php echo esc_attr( "$level $classes" ) ?>">
<span class="dops-notice__icon-wrapper">
<svg class="gridicon gridicons-info dops-notice__icon" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z" />
</svg>
</span>
<span class="dops-notice__content">
<span class="dops-notice__text"><?php echo wp_kses( $content, $allowed_html ) ?></span>
</span>
<?php if ( ! empty( $action ) ) : ?>
<a class="dops-notice__action" href="<?php echo esc_attr( $action['url'] ) ?>" target="_blank" rel="noopener noreferrer">
<span><?php echo esc_html( $action['label'] ) ?></span>
</a>
<?php endif; ?>
</div>
<?php
}
function ui_delete_vp_settings_button() {
?>
<div class="dops-card dops-section-header is-compact">
<?php _e( 'Reset settings', 'vaultpress' ); ?>
</div>
<?php
if ( isset( $_GET['delete-vp-settings'] ) && 1 == (int) $_GET['delete-vp-settings'] ) {
?>
<div class="dops-card">
<p><?php esc_html_e( 'All VaultPress settings have been deleted.', 'vaultpress' ); ?></p>
</div>
<?php
} else {
?>
<div class="dops-card">
<p><?php esc_html_e( 'Click this button to reset all VaultPress options in the database. You can try this if VaultPress is reporting a connection error.', 'vaultpress' ); ?></p>
<p><strong>
<?php
printf(
wp_kses(
/* translators: URLs to VaultPress dashboard. */
__( 'Warning: this button will unregister VaultPress and disconnect it from your site. If you intend on registering the plugin again, you can find your registration key <a href="%1$s" target="_blank" rel="noopener noreferrer">here</a>.', 'vaultpress' ),
array(
'a' => array(
'href' => array(),
'target' => array(),
'rel' => array(),
),
)
),
'https://dashboard.vaultpress.com/account/'
);
?>
</strong></p>
<form
onsubmit="return confirm( '<?php esc_html_e( 'Do you really want to reset all options?', 'vaultpress' ) ?>' );"
method="post"
action="">
<button class="dops-button is-scary is-compact"><?php esc_html_e( 'Delete all settings', 'vaultpress' ); ?></button>
<input type="hidden" name="action" value="delete-vp-settings"/>
<?php wp_nonce_field( 'delete_vp_settings' ); ?>
</form>
</div>
<?php
}
}
/**
* Render the Jetpack logo
*/