| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Core\Experiments; |
| 4 |
|
| 5 |
use Elementor\Core\Experiments\Manager as Experiments_Manager; |
| 6 |
use Elementor\Plugin; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly |
| 10 |
} |
| 11 |
|
| 12 |
class Wp_Cli extends \WP_CLI_Command { |
| 13 |
|
| 14 |
/** |
| 15 |
* Activate an Experiment |
| 16 |
* |
| 17 |
* ## EXAMPLES |
| 18 |
* |
| 19 |
* 1. wp elementor experiments activate container |
| 20 |
* - This will activate the Container experiment. |
| 21 |
* |
| 22 |
* @param array $args |
| 23 |
* @param array|null $assoc_args - Arguments from WP CLI command. |
| 24 |
*/ |
| 25 |
public function activate( $args, $assoc_args ) { |
| 26 |
if ( empty( $args[0] ) ) { |
| 27 |
\WP_CLI::error( 'Please specify an experiment.' ); |
| 28 |
} |
| 29 |
|
| 30 |
$is_network = $this->is_network( $assoc_args ); |
| 31 |
|
| 32 |
// Activate experiment callback. |
| 33 |
$activate = function ( $site = '', $id = null ) use ( $args, $is_network ) { |
| 34 |
$success = 'Experiment activated successfully'; |
| 35 |
$error = 'Cannot activate experiment'; |
| 36 |
|
| 37 |
if ( $is_network ) { |
| 38 |
$success .= " for site {$site}"; |
| 39 |
$error .= " for site {$site}"; |
| 40 |
} |
| 41 |
|
| 42 |
$experiments_manager = Plugin::instance()->experiments; |
| 43 |
$option = $experiments_manager->get_feature_option_key( $args[0] ); |
| 44 |
|
| 45 |
update_option( $option, Experiments_Manager::STATE_ACTIVE ); |
| 46 |
|
| 47 |
try { |
| 48 |
\WP_CLI::success( $success ); |
| 49 |
} catch ( \Exception $e ) { |
| 50 |
\WP_CLI::error( $error ); |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
if ( $is_network ) { |
| 55 |
$this->foreach_sites( $activate ); |
| 56 |
} else { |
| 57 |
$activate(); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Deactivate an Experiment |
| 63 |
* |
| 64 |
* ## EXAMPLES |
| 65 |
* |
| 66 |
* 1. wp elementor experiments deactivate container |
| 67 |
* - This will deactivate the Container experiment. |
| 68 |
* |
| 69 |
* @param array $args |
| 70 |
* @param array|null $assoc_args - Arguments from WP CLI command. |
| 71 |
*/ |
| 72 |
public function deactivate( $args, $assoc_args ) { |
| 73 |
if ( empty( $args[0] ) ) { |
| 74 |
\WP_CLI::error( 'Please specify an experiment.' ); |
| 75 |
} |
| 76 |
|
| 77 |
$is_network = $this->is_network( $assoc_args ); |
| 78 |
|
| 79 |
// Activate experiment callback. |
| 80 |
$activate = function ( $site = '' ) use ( $args, $is_network ) { |
| 81 |
$success = 'Experiment deactivated successfully'; |
| 82 |
$error = 'Cannot deactivate experiment'; |
| 83 |
|
| 84 |
if ( $is_network ) { |
| 85 |
$success .= " for site {$site}"; |
| 86 |
$error .= " for site {$site}"; |
| 87 |
} |
| 88 |
|
| 89 |
$experiments_manager = Plugin::instance()->experiments; |
| 90 |
$option = $experiments_manager->get_feature_option_key( $args[0] ); |
| 91 |
|
| 92 |
update_option( $option, Experiments_Manager::STATE_INACTIVE ); |
| 93 |
|
| 94 |
try { |
| 95 |
\WP_CLI::success( $success ); |
| 96 |
} catch ( \Exception $e ) { |
| 97 |
\WP_CLI::error( $error ); |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
if ( $is_network ) { |
| 102 |
$this->foreach_sites( $activate ); |
| 103 |
} else { |
| 104 |
$activate(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Determine if the current website is a multisite. |
| 110 |
* |
| 111 |
* @param array|null $assoc_args - Arguments from WP CLI command. |
| 112 |
* |
| 113 |
* @return bool |
| 114 |
*/ |
| 115 |
private function is_network( $assoc_args ) { |
| 116 |
return ! empty( $assoc_args['network'] ) && is_multisite(); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Iterate over network sites and execute a callback. |
| 121 |
* |
| 122 |
* @param callable $callback - Callback to execute. Gets the site name & id as parameters. |
| 123 |
* |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
private function foreach_sites( callable $callback ) { |
| 127 |
/** @var \WP_Site[] $sites */ |
| 128 |
$sites = get_sites(); |
| 129 |
|
| 130 |
foreach ( $sites as $keys => $site ) { |
| 131 |
// Cast $blog as an array instead of object |
| 132 |
$blog_id = $site->blog_id; |
| 133 |
|
| 134 |
switch_to_blog( $blog_id ); |
| 135 |
|
| 136 |
$callback( get_option( 'home' ), $blog_id ); |
| 137 |
|
| 138 |
restore_current_blog(); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|