| 1 |
<?php |
| 2 |
|
| 3 |
// Do not allow the file to be called directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* This class is used to handle anything multisite related. |
| 10 |
*/ |
| 11 |
class P_Multisite extends P_Core { |
| 12 |
|
| 13 |
/** |
| 14 |
* Stores any errors. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
public $error = ''; |
| 19 |
|
| 20 |
/** |
| 21 |
* Add the actions required for the multisite functionality. |
| 22 |
* |
| 23 |
* @param Patchstack $core |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function __construct( $core ) { |
| 27 |
parent::__construct( $core ); |
| 28 |
if ( ! is_super_admin() ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
// When sites are activated. |
| 33 |
if ( isset( $_POST['patchstack_do'], $_POST['PatchstackNonce'], $_POST['sites'] ) && $_POST['patchstack_do'] == 'do_licenses' && wp_verify_nonce( $_POST['PatchstackNonce'], 'patchstack-multisite-activation' ) ) { |
| 34 |
$this->activate_licenses(); |
| 35 |
} |
| 36 |
|
| 37 |
// When we need to re-run the migration of a specific site. |
| 38 |
if ( isset( $_GET['site'], $_GET['PatchstackNonce'] ) && wp_verify_nonce( $_GET['PatchstackNonce'], 'patchstack-migration' ) ) { |
| 39 |
$this->run_migration(); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* When a user selects sites that need to be activated. |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
private function activate_licenses() { |
| 49 |
if ( empty( $_POST['sites'] ) ) { |
| 50 |
$this->error = '<span style="color: #ff6262;">Please select at least 1 site to be activated.</span><br /><br />'; |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
// Determine which sites are already activated and skip those. |
| 55 |
$activate = []; |
| 56 |
$sites = get_sites(); |
| 57 |
foreach ( $sites as $site ) { |
| 58 |
if ( in_array( $site->siteurl, $_POST['sites'] ) && get_blog_option( $site->id, 'patchstack_clientid' ) == '' ) { |
| 59 |
array_push( $activate, $site->siteurl ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// Make sure there is a site that can be activated. |
| 64 |
if ( count( $activate ) == 0 ) { |
| 65 |
$this->error = '<span style="color: #ff6262;">None of the selected sites need activation.</span><br /><br />'; |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
// Add the site to the app and retrieve the license for each site. |
| 70 |
$licenses = $this->plugin->api->get_site_licenses( [ 'sites' => $activate ] ); |
| 71 |
|
| 72 |
// Did an error happen during the multisite license activation? |
| 73 |
if ( isset( $licenses['error'] ) ) { |
| 74 |
$this->error = '<span style="color: #ff6262;">' . $licenses['error'] . '</span><br /><br />'; |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
// Activate licenses on given sites |
| 79 |
$sites = get_sites(); |
| 80 |
foreach ( $sites as $site ) { |
| 81 |
if ( isset( $licenses[ $site->siteurl ] ) ) { |
| 82 |
$this->plugin->activation->activate_multisite_license( $site, $licenses[ $site->siteurl ] ); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* This will be called when the network admin clicks on the "Sites" button. |
| 89 |
* It will show all sites. |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function sites_section_callback() { |
| 94 |
require_once dirname( __FILE__ ) . '/views/pages/multisite-table.php'; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Re-run the migration for a specific multisite site. |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
private function run_migration( ) { |
| 103 |
// Must be a number. |
| 104 |
if ( !ctype_digit( $_GET['site'] ) ) { |
| 105 |
exit; |
| 106 |
} |
| 107 |
|
| 108 |
// Site must be valid and exists. |
| 109 |
$site = get_site( $_GET['site'] ); |
| 110 |
if ( is_null( $site ) ) { |
| 111 |
exit; |
| 112 |
} |
| 113 |
|
| 114 |
// Perform base migration. |
| 115 |
$this->plugin->activation->migrate( null, $site->id ); |
| 116 |
|
| 117 |
// Perform specific version migrations. |
| 118 |
$versions = array('3.0.1', '3.0.2'); |
| 119 |
foreach ( $versions as $version ) { |
| 120 |
$this->plugin->activation->migrate( $version, $site->id ); |
| 121 |
} |
| 122 |
|
| 123 |
wp_safe_redirect( add_query_arg( [ 'success' => '1', 'site' => $site->id ], remove_query_arg( [ 'PatchstackNonce', 'site' ] ) ) ); |
| 124 |
exit; |
| 125 |
} |
| 126 |
} |
| 127 |
|