| @@ -8,9 +8,17 @@ | ||
| 8 | 8 | /** |
| 9 | 9 | * This class is used to handle anything multisite related. |
| 10 | 10 | */ |
| 11 | 11 | class P_Multisite extends P_Core { |
| 12 | + | |
| 12 | 13 | /** |
| 14 | + * Stores any errors. | |
| 15 | + * | |
| 16 | + * @var string | |
| 17 | + */ | |
| 18 | + public $error = ''; | |
| 19 | + | |
| 20 | + /** | |
| 13 | 21 | * Add the actions required for the multisite functionality. |
| 14 | 22 | * |
| 15 | 23 | * @param Patchstack $core |
| 16 | 24 | * @return void |
| @@ -20,8 +28,13 @@ | ||
| 20 | 28 | if ( ! is_super_admin() ) { |
| 21 | 29 | return; |
| 22 | 30 | } |
| 23 | 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 | + | |
| 24 | 37 | // When we need to re-run the migration of a specific site. |
| 25 | 38 | if ( isset( $_GET['site'], $_GET['PatchstackNonce'] ) && wp_verify_nonce( $_GET['PatchstackNonce'], 'patchstack-migration' ) ) { |
| 26 | 39 | $this->run_migration(); |
| 27 | 40 | } |
| @@ -27,8 +40,52 @@ | ||
| 27 | 40 | } |
| 28 | 41 | } |
| 29 | 42 | |
| 30 | 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 | + /** | |
| 31 | 88 | * This will be called when the network admin clicks on the "Sites" button. |
| 32 | 89 | * It will show all sites. |
| 33 | 90 | * |
| 34 | 91 | * @return void |
| @@ -43,14 +100,14 @@ | ||
| 43 | 100 | * @return void |
| 44 | 101 | */ |
| 45 | 102 | private function run_migration( ) { |
| 46 | 103 | // Must be a number. |
| 47 | - if ( ! isset( $_GET['site'] ) || ! is_scalar( $_GET['site'] ) || ! ctype_digit( (string) $_GET['site'] ) ) { | |
| 104 | + if ( !ctype_digit( $_GET['site'] ) ) { | |
| 48 | 105 | exit; |
| 49 | 106 | } |
| 50 | 107 | |
| 51 | 108 | // Site must be valid and exists. |
| 52 | - $site = function_exists( 'get_site' ) ? get_site( $_GET['site'] ) : null; | |
| 109 | + $site = get_site( $_GET['site'] ); | |
| 53 | 110 | if ( is_null( $site ) ) { |
| 54 | 111 | exit; |
| 55 | 112 | } |
| 56 | 113 | |