-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathclass-fs-clone-manager.php
More file actions
1660 lines (1387 loc) · 63.4 KB
/
Copy pathclass-fs-clone-manager.php
File metadata and controls
1660 lines (1387 loc) · 63.4 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
/**
* @package Freemius
* @copyright Copyright (c) 2015, Freemius, Inc.
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Manages the detection of clones and provides the logged-in WordPress user with options for manually resolving them.
*
* @since 2.5.0
*
* @property int $clone_identification_timestamp
* @property int $temporary_duplicate_mode_selection_timestamp
* @property int $temporary_duplicate_notice_shown_timestamp
* @property string $request_handler_id
* @property int $request_handler_timestamp
* @property int $request_handler_retries_count
* @property bool $hide_manual_resolution
* @property array $new_blog_install_map
*/
class FS_Clone_Manager {
/**
* @var FS_Option_Manager
*/
private $_storage;
/**
* @var FS_Option_Manager
*/
private $_network_storage;
/**
* @var FS_Admin_Notices
*/
private $_notices;
/**
* @var FS_Logger
*/
protected $_logger;
/**
* @var int 3 minutes
*/
const CLONE_RESOLUTION_MAX_EXECUTION_TIME = 180;
/**
* @var int
*/
const CLONE_RESOLUTION_MAX_RETRIES = 3;
/**
* @var int
*/
const TEMPORARY_DUPLICATE_PERIOD = WP_FS__TIME_WEEK_IN_SEC * 2;
/**
* @var string
*/
const OPTION_NAME = 'clone_resolution';
/**
* @var string
*/
const OPTION_MANAGER_NAME = 'clone_management';
/**
* @var string
*/
const OPTION_TEMPORARY_DUPLICATE = 'temporary_duplicate';
/**
* @var string
*/
const OPTION_LONG_TERM_DUPLICATE = 'long_term_duplicate';
/**
* @var string
*/
const OPTION_NEW_HOME = 'new_home';
#--------------------------------------------------------------------------------
#region Singleton
#--------------------------------------------------------------------------------
/**
* @var FS_Clone_Manager
*/
private static $_instance;
/**
* @return FS_Clone_Manager
*/
static function instance() {
if ( ! isset( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
#endregion
private function __construct() {
$this->_storage = FS_Option_Manager::get_manager( WP_FS___OPTION_PREFIX . self::OPTION_MANAGER_NAME, true );
$this->_network_storage = FS_Option_Manager::get_manager( WP_FS___OPTION_PREFIX . self::OPTION_MANAGER_NAME, true, true );
$this->maybe_migrate_options();
$this->_notices = FS_Admin_Notices::instance( 'global_clone_resolution_notices', '', '', true );
$this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . '_clone_manager', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
}
/**
* Migrate clone resolution options from 2.5.0 array-based structure, to a new flat structure.
*
* The reason this logic is not in a separate migration script is that we want to be 100% sure data is migrated before any execution of clone logic.
*
* @todo Delete this one in the future.
*/
private function maybe_migrate_options() {
$storages = array(
$this->_storage,
$this->_network_storage
);
foreach ( $storages as $storage ) {
$clone_data = $storage->get_option( self::OPTION_NAME );
if ( is_array( $clone_data ) && ! empty( $clone_data ) ) {
foreach ( $clone_data as $key => $val ) {
if ( ! is_null( $val ) ) {
$storage->set_option( $key, $val );
}
}
$storage->unset_option( self::OPTION_NAME, true );
}
}
}
/**
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*/
function _init() {
if ( is_admin() ) {
if ( Freemius::is_admin_post() ) {
add_action( 'admin_post_fs_clone_resolution', array( $this, '_handle_clone_resolution' ) );
}
if ( Freemius::is_ajax() ) {
Freemius::add_ajax_action_static( 'handle_clone_resolution', array( $this, '_clone_resolution_action_ajax_handler' ) );
} else {
if (
empty( $this->get_clone_identification_timestamp() ) &&
(
! fs_is_network_admin() ||
! ( $this->is_clone_resolution_options_notice_shown() || $this->is_temporary_duplicate_notice_shown() )
)
) {
$this->hide_clone_admin_notices();
} else if ( ! Freemius::is_cron() && ! Freemius::is_admin_post() ) {
$this->try_resolve_clone_automatically();
$this->maybe_show_clone_admin_notice();
add_action( 'admin_footer', array( $this, '_add_clone_resolution_javascript' ) );
}
}
}
}
/**
* Retrieves the timestamp that was stored when a clone was identified.
*
* @return int|null
*/
function get_clone_identification_timestamp() {
return $this->get_option( 'clone_identification_timestamp', true );
}
/**
* @author Leo Fajardo (@leorw)
* @since 2.5.1
*
* @param string $sdk_last_version
*/
function maybe_update_clone_resolution_support_flag( $sdk_last_version ) {
if ( null !== $this->hide_manual_resolution ) {
return;
}
$this->hide_manual_resolution = (
! empty( $sdk_last_version ) &&
version_compare( $sdk_last_version, '2.5.0', '<' )
);
}
/**
* Stores the time when a clone was identified.
*/
function store_clone_identification_timestamp() {
$this->clone_identification_timestamp = time();
}
/**
* Retrieves the timestamp for the temporary duplicate mode's expiration.
*
* @return int
*/
function get_temporary_duplicate_expiration_timestamp() {
$temporary_duplicate_mode_start_timestamp = $this->was_temporary_duplicate_mode_selected() ?
$this->temporary_duplicate_mode_selection_timestamp :
$this->get_clone_identification_timestamp();
return ( $temporary_duplicate_mode_start_timestamp + self::TEMPORARY_DUPLICATE_PERIOD );
}
/**
* Determines if the SDK should handle clones. The SDK handles clones only up to 3 times with 3 min interval.
*
* @return bool
*/
private function should_handle_clones() {
if ( ! isset( $this->request_handler_timestamp ) ) {
return true;
}
if ( $this->request_handler_retries_count >= self::CLONE_RESOLUTION_MAX_RETRIES ) {
return false;
}
// Give the logic that handles clones enough time to finish (it is given 3 minutes for now).
return ( time() > ( $this->request_handler_timestamp + self::CLONE_RESOLUTION_MAX_EXECUTION_TIME ) );
}
/**
* @author Leo Fajardo (@leorw)
* @since 2.5.1
*
* @return bool
*/
function should_hide_manual_resolution() {
return ( true === $this->hide_manual_resolution );
}
/**
* Executes the clones handler logic if it should be executed, i.e., based on the return value of the should_handle_clones() method.
*
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*/
function maybe_run_clone_resolution() {
if ( ! $this->should_handle_clones() ) {
return;
}
$this->request_handler_retries_count = isset( $this->request_handler_retries_count ) ?
( $this->request_handler_retries_count + 1 ) :
1;
$this->request_handler_timestamp = time();
$handler_id = ( rand() . microtime() );
$this->request_handler_id = $handler_id;
// Add cookies to trigger request with the same user access permissions.
$cookies = array();
foreach ( $_COOKIE as $name => $value ) {
$cookies[] = new WP_Http_Cookie( array(
'name' => $name,
'value' => $value,
) );
}
wp_remote_post(
admin_url( 'admin-post.php' ),
array(
'method' => 'POST',
'body' => array(
'action' => 'fs_clone_resolution',
'handler_id' => $handler_id,
),
'timeout' => 0.01,
'blocking' => false,
'sslverify' => false,
'cookies' => $cookies,
)
);
}
/**
* Executes the clones handler logic.
*
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*/
function _handle_clone_resolution() {
$handler_id = fs_request_get( 'handler_id' );
if ( empty( $handler_id ) ) {
return;
}
if (
! isset( $this->request_handler_id ) ||
$this->request_handler_id !== $handler_id
) {
return;
}
if ( ! $this->try_automatic_resolution() ) {
$this->clear_temporary_duplicate_notice_shown_timestamp();
}
}
#--------------------------------------------------------------------------------
#region Automatic Clone Resolution
#--------------------------------------------------------------------------------
/**
* @var array All installs cache.
*/
private $all_installs;
/**
* Checks if a given instance's install is a clone of another subsite in the network.
*
* @author Vova Feldman (@svovaf)
*
* @return FS_Site
*/
private function find_network_subsite_clone_install( Freemius $instance ) {
if ( ! is_multisite() ) {
// Not a multi-site network.
return null;
}
if ( ! isset( $this->all_installs ) ) {
$this->all_installs = Freemius::get_all_modules_sites();
}
// Check if there's another blog that has the same site.
$module_type = $instance->get_module_type();
$sites_by_module_type = ! empty( $this->all_installs[ $module_type ] ) ?
$this->all_installs[ $module_type ] :
array();
$slug = $instance->get_slug();
$sites_by_slug = ! empty( $sites_by_module_type[ $slug ] ) ?
$sites_by_module_type[ $slug ] :
array();
$current_blog_id = get_current_blog_id();
$current_install = $instance->get_site();
foreach ( $sites_by_slug as $site ) {
if (
$current_install->id == $site->id &&
$current_blog_id != $site->blog_id
) {
// Clone is identical to an install on another subsite in the network.
return $site;
}
}
return null;
}
/**
* Tries to find a different install of the context product that is associated with the current URL and loads it.
*
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*
* @param Freemius $instance
* @param string $url
*
* @return object
*/
private function find_other_install_by_url( Freemius $instance, $url ) {
$result = $instance->get_api_user_scope()->get( "/plugins/{$instance->get_id()}/installs.json?url=" . urlencode( $url ) . "&all=true", true );
$current_install = $instance->get_site();
if ( $instance->is_api_result_object( $result, 'installs' ) ) {
foreach ( $result->installs as $install ) {
if ( $install->id == $current_install->id ) {
continue;
}
if (
$instance->is_only_premium() &&
! FS_Plugin_License::is_valid_id( $install->license_id )
) {
continue;
}
// When searching for installs by a URL, the API will first strip any paths and search for any matching installs by the subdomain. Therefore, we need to test if there's a match between the current URL and the install's URL before continuing.
if ( $url !== fs_strip_url_protocol( untrailingslashit( $install->url ) ) ) {
continue;
}
// Found a different install that is associated with the current URL, load it and replace the current install with it if no updated install is found.
return $install;
}
}
return null;
}
/**
* Delete the current install associated with a given instance and opt-in/activate-license to create a fresh install.
*
* @author Vova Feldman (@svovaf)
* @since 2.5.0
*
* @param Freemius $instance
* @param string|false $license_key
*
* @return bool TRUE if successfully connected. FALSE if failed and had to restore install from backup.
*/
private function delete_install_and_connect( Freemius $instance, $license_key = false ) {
$user = Freemius::_get_user_by_id( $instance->get_site()->user_id );
$instance->delete_current_install( true );
if ( ! is_object( $user ) ) {
// Get logged-in WordPress user.
$current_user = Freemius::_get_current_wp_user();
// Find the relevant FS user by email address.
$user = Freemius::_get_user_by_email( $current_user->user_email );
}
if ( is_object( $user ) ) {
// When a clone is found, we prefer to use the same user of the original install for the opt-in.
$instance->install_with_user( $user, $license_key, false, false );
} else {
// If no user is found, activate with the license.
$instance->opt_in(
false,
false,
false,
$license_key
);
}
if ( is_object( $instance->get_site() ) ) {
// Install successfully created.
return true;
}
// Restore from backup.
$instance->restore_backup_site();
return false;
}
/**
* Try to resolve the clone situation automatically.
*
* @param Freemius $instance
* @param string $current_url
* @param bool $is_localhost
* @param bool|null $is_clone_of_network_subsite
*
* @return bool If managed to automatically resolve the clone.
*/
private function try_resolve_clone_automatically_by_instance(
Freemius $instance,
$current_url,
$is_localhost,
$is_clone_of_network_subsite = null
) {
// Try to find a different install of the context product that is associated with the current URL.
$associated_install = $this->find_other_install_by_url( $instance, $current_url );
if ( is_object( $associated_install ) ) {
// Replace the current install with a different install that is associated with the current URL.
$instance->store_site( new FS_Site( clone $associated_install ) );
$instance->sync_install( array( 'is_new_site' => true ), true );
return true;
}
if ( ! $instance->is_premium() ) {
// For free products, opt-in with the context user to create new install.
return $this->delete_install_and_connect( $instance );
}
$license = $instance->_get_license();
$can_activate_license = ( is_object( $license ) && ! $license->is_utilized( $is_localhost ) );
if ( ! $can_activate_license ) {
// License can't be activated, therefore, can't be automatically resolved.
return false;
}
if ( ! WP_FS__IS_LOCALHOST_FOR_SERVER && ! $is_localhost ) {
$is_clone_of_network_subsite = ( ! is_null( $is_clone_of_network_subsite ) ) ?
$is_clone_of_network_subsite :
is_object( $this->find_network_subsite_clone_install( $instance ) );
if ( ! $is_clone_of_network_subsite ) {
return false;
}
}
// If the site is a clone of another subsite in the network, or a localhost one, try to auto activate the license.
return $this->delete_install_and_connect( $instance, $license->secret_key );
}
/**
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*/
private function try_resolve_clone_automatically() {
$clone_action = $this->get_clone_resolution_action_from_config();
if ( ! empty( $clone_action ) ) {
$this->try_resolve_clone_automatically_by_config( $clone_action );
return;
}
$this->try_automatic_resolution();
}
/**
* Tries to resolve the clone situation automatically based on the config in the wp-config.php file.
*
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*
* @param string $clone_action
*/
private function try_resolve_clone_automatically_by_config( $clone_action ) {
$fs_instances = array();
if ( self::OPTION_LONG_TERM_DUPLICATE === $clone_action ) {
$instances = Freemius::_get_all_instances();
foreach ( $instances as $instance ) {
if ( ! $instance->is_registered() ) {
continue;
}
if ( ! $instance->is_clone() ) {
continue;
}
$license = $instance->has_features_enabled_license() ?
$instance->_get_license() :
null;
if (
is_object( $license ) &&
! $license->is_utilized(
( WP_FS__IS_LOCALHOST_FOR_SERVER || FS_Site::is_localhost_by_address( Freemius::get_unfiltered_site_url() ) )
)
) {
$fs_instances[] = $instance;
}
}
if ( empty( $fs_instances ) ) {
return;
}
}
$this->resolve_cloned_sites( $clone_action, $fs_instances );
}
/**
* @author Leo Fajard (@leorw)
* @since 2.5.0
*
* @return string|null
*/
private function get_clone_resolution_action_from_config() {
if ( ! defined( 'FS__RESOLVE_CLONE_AS' ) ) {
return null;
}
if ( ! in_array(
FS__RESOLVE_CLONE_AS,
array(
self::OPTION_NEW_HOME,
self::OPTION_TEMPORARY_DUPLICATE,
self::OPTION_LONG_TERM_DUPLICATE,
)
) ) {
return null;
}
return FS__RESOLVE_CLONE_AS;
}
/**
* Tries to recover the install of a newly created subsite or resolve it if it's a clone.
*
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*
* @param Freemius $instance
*/
function maybe_resolve_new_subsite_install_automatically( Freemius $instance ) {
if ( ! $instance->is_user_in_admin() ) {
// Try to recover an install or resolve a clone only when there's a user in admin to prevent doing it prematurely (e.g., the install can get replaced with clone data again).
return;
}
if ( ! is_multisite() ) {
return;
}
$new_blog_install_map = $this->new_blog_install_map;
if ( empty( $new_blog_install_map ) || ! is_array( $new_blog_install_map ) ) {
return;
}
$is_network_admin = fs_is_network_admin();
if ( ! $is_network_admin ) {
// If not in network admin, handle the current site.
$blog_id = get_current_blog_id();
} else {
// If in network admin, handle only the first site.
$blog_ids = array_keys( $new_blog_install_map );
$blog_id = $blog_ids[0];
}
if ( ! isset( $new_blog_install_map[ $blog_id ] ) ) {
// There's no site to handle.
return;
}
$expected_install_id = $new_blog_install_map[ $blog_id ]['install_id'];
$current_install = $instance->get_install_by_blog_id( $blog_id );
$current_install_id = is_object( $current_install ) ?
$current_install->id :
null;
if ( $expected_install_id == $current_install_id ) {
// Remove the current site's information from the map to prevent handling it again.
$this->remove_new_blog_install_info_from_storage( $blog_id );
return;
}
require_once WP_FS__DIR_INCLUDES . '/class-fs-lock.php';
$lock = new FS_Lock( self::OPTION_NAME . '_subsite' );
if ( ! $lock->try_lock(60) ) {
return;
}
$instance->switch_to_blog( $blog_id );
$current_url = untrailingslashit( Freemius::get_unfiltered_site_url( null, true ) );
$current_install_url = is_object( $current_install ) ?
fs_strip_url_protocol( untrailingslashit( $current_install->url ) ) :
null;
// This can be `false` even if the install is a clone as the URL can be updated as part of the cloning process.
$is_clone = ( ! is_null( $current_install_url ) && $current_url !== $current_install_url );
if ( ! FS_Site::is_valid_id( $expected_install_id ) ) {
$expected_install = null;
} else {
$expected_install = $instance->fetch_install_by_id( $expected_install_id );
}
if ( FS_Api::is_api_result_entity( $expected_install ) ) {
// Replace the current install with the expected install.
$instance->store_site( new FS_Site( clone $expected_install ) );
$instance->sync_install( array( 'is_new_site' => true ), true );
} else {
$network_subsite_clone_install = null;
if ( ! $is_clone ) {
// It is possible that `$is_clone` is `false` but the install is actually a clone as the following call checks the install ID and not the URL.
$network_subsite_clone_install = $this->find_network_subsite_clone_install( $instance );
}
if ( $is_clone || is_object( $network_subsite_clone_install ) ) {
// If there's no expected install (or it couldn't be fetched) and the current install is a clone, try to resolve the clone automatically.
$is_localhost = FS_Site::is_localhost_by_address( $current_url );
$resolved = $this->try_resolve_clone_automatically_by_instance( $instance, $current_url, $is_localhost, is_object( $network_subsite_clone_install ) );
if ( ! $resolved && is_object( $network_subsite_clone_install ) ) {
if ( empty( $this->get_clone_identification_timestamp() ) ) {
$this->store_clone_identification_timestamp();
}
// Since the clone couldn't be identified based on the URL, replace the stored install with the cloned install so that the manual clone resolution notice will appear.
$instance->store_site( clone $network_subsite_clone_install );
}
}
}
$instance->restore_current_blog();
// Remove the current site's information from the map to prevent handling it again.
$this->remove_new_blog_install_info_from_storage( $blog_id );
$lock->unlock();
}
/**
* If a new install was created after creating a new subsite, its ID is stored in the blog-install map so that it can be recovered in case it's replaced with a clone install (e.g., when the newly created subsite is a clone). The IDs of the clone subsites that were created while not running this version of the SDK or a higher version will also be stored in the said map so that the clone manager can also try to resolve them later on.
*
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*
* @param int $blog_id
* @param FS_Site $site
*/
function store_blog_install_info( $blog_id, $site = null ) {
$new_blog_install_map = $this->new_blog_install_map;
if (
empty( $new_blog_install_map ) ||
! is_array( $new_blog_install_map )
) {
$new_blog_install_map = array();
}
$install_id = null;
if ( is_object( $site ) ) {
$install_id = $site->id;
}
$new_blog_install_map[ $blog_id ] = array( 'install_id' => $install_id );
$this->new_blog_install_map = $new_blog_install_map;
}
/**
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*
* @param int $blog_id
*/
private function remove_new_blog_install_info_from_storage( $blog_id ) {
$new_blog_install_map = $this->new_blog_install_map;
unset( $new_blog_install_map[ $blog_id ] );
$this->new_blog_install_map = $new_blog_install_map;
}
/**
* Tries to resolve all clones automatically.
*
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*
* @return bool If managed to automatically resolve all clones.
*/
private function try_automatic_resolution() {
$this->_logger->entrance();
require_once WP_FS__DIR_INCLUDES . '/class-fs-lock.php';
$lock = new FS_Lock( self::OPTION_NAME );
/**
* Try to acquire lock for the next 60 sec based on the thread ID.
*/
if ( ! $lock->try_lock( 60 ) ) {
return false;
}
$current_url = untrailingslashit( Freemius::get_unfiltered_site_url( null, true ) );
$is_localhost = FS_Site::is_localhost_by_address( $current_url );
$require_manual_resolution = false;
$instances = Freemius::_get_all_instances();
foreach ( $instances as $instance ) {
if ( ! $instance->is_registered() ) {
continue;
}
if ( ! $instance->is_clone() ) {
continue;
}
if ( ! $this->try_resolve_clone_automatically_by_instance( $instance, $current_url, $is_localhost ) ) {
$require_manual_resolution = true;
}
}
// Create a 1-day lock.
$lock->lock( WP_FS__TIME_24_HOURS_IN_SEC );
return ( ! $require_manual_resolution );
}
#endregion
#--------------------------------------------------------------------------------
#region Manual Clone Resolution
#--------------------------------------------------------------------------------
/**
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*/
function _add_clone_resolution_javascript() {
$vars = array( 'ajax_action' => Freemius::get_ajax_action_static( 'handle_clone_resolution' ) );
fs_require_once_template( 'clone-resolution-js.php', $vars );
}
/**
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*/
function _clone_resolution_action_ajax_handler() {
$this->_logger->entrance();
check_ajax_referer( Freemius::get_ajax_action_static( 'handle_clone_resolution' ), 'security' );
$clone_action = fs_request_get( 'clone_action' );
$blog_id = is_multisite() ?
fs_request_get( 'blog_id' ) :
0;
if ( is_multisite() && $blog_id == get_current_blog_id() ) {
$blog_id = 0;
}
if ( empty( $clone_action ) ) {
Freemius::shoot_ajax_failure( array(
'message' => fs_text_inline( 'Invalid clone resolution action.', 'invalid-clone-resolution-action-error' ),
'redirect_url' => '',
) );
}
$result = $this->resolve_cloned_sites( $clone_action, array(), $blog_id );
Freemius::shoot_ajax_success( $result );
}
/**
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*
* @param string $clone_action
* @param Freemius[] $fs_instances
* @param int $blog_id
*
* @return array
*/
private function resolve_cloned_sites( $clone_action, $fs_instances = array(), $blog_id = 0 ) {
$this->_logger->entrance();
$result = array();
$instances_with_clone = array();
$instances_with_clone_count = 0;
$install_by_instance_id = array();
$instances = ( ! empty( $fs_instances ) ) ?
$fs_instances :
Freemius::_get_all_instances();
$should_switch_to_blog = ( $blog_id > 0 );
foreach ( $instances as $instance ) {
if ( $should_switch_to_blog ) {
$instance->switch_to_blog( $blog_id );
}
if ( $instance->is_registered() && $instance->is_clone() ) {
$instances_with_clone[] = $instance;
$instances_with_clone_count ++;
$install_by_instance_id[ $instance->get_id() ] = $instance->get_site();
}
}
if ( self::OPTION_TEMPORARY_DUPLICATE === $clone_action ) {
$this->store_temporary_duplicate_timestamp();
} else {
$redirect_url = '';
foreach ( $instances_with_clone as $instance ) {
if ( $should_switch_to_blog ) {
$instance->switch_to_blog( $blog_id );
}
$has_error = false;
if ( self::OPTION_NEW_HOME === $clone_action ) {
$instance->sync_install( array( 'is_new_site' => true ), true );
if ( $instance->is_clone() ) {
$has_error = true;
}
} else {
$instance->_handle_long_term_duplicate();
if ( ! is_object( $instance->get_site() ) ) {
$has_error = true;
}
}
if ( $has_error && 1 === $instances_with_clone_count ) {
$redirect_url = $instance->get_activation_url();
}
}
$result = ( array( 'redirect_url' => $redirect_url ) );
}
foreach ( $instances_with_clone as $instance ) {
if ( $should_switch_to_blog ) {
$instance->switch_to_blog( $blog_id );
}
// No longer a clone, send an update.
if ( ! $instance->is_clone() ) {
$instance->send_clone_resolution_update(
$clone_action,
$install_by_instance_id[ $instance->get_id() ]
);
}
}
if ( 'temporary_duplicate_license_activation' !== $clone_action ) {
$this->remove_clone_resolution_options_notice();
} else {
$this->remove_temporary_duplicate_notice();
}
if ( $should_switch_to_blog ) {
foreach ( $instances as $instance ) {
$instance->restore_current_blog();
}
}
return $result;
}
/**
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*/
private function hide_clone_admin_notices() {
$this->remove_clone_resolution_options_notice( false );
$this->remove_temporary_duplicate_notice( false );
}
/**
* @author Leo Fajardo (@leorw)
* @since 2.5.0
*/
function maybe_show_clone_admin_notice() {
$this->_logger->entrance();
if ( fs_is_network_admin() ) {
$existing_notice_ids = $this->maybe_remove_notices();
if ( ! empty( $existing_notice_ids ) ) {
fs_enqueue_local_style( 'fs_clone_resolution_notice', '/admin/clone-resolution.css' );
}
return;
}
$first_instance_with_clone = null;
$site_urls = array();
$sites_with_license_urls = array();
$sites_with_premium_version_count = 0;
$product_ids = array();
$product_titles = array();
$instances = Freemius::_get_all_instances();
foreach ( $instances as $instance ) {
if ( ! $instance->is_registered() ) {
continue;
}
if ( ! $instance->is_clone( true ) ) {
continue;
}
$install = $instance->get_site();
$site_urls[] = $install->url;
$product_ids[] = $instance->get_id();
$product_titles[] = $instance->get_plugin_title();