| 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 |
$experiments = $this->parse_experiments( $args[0] ); |
| 33 |
$plural = $this->get_plural( $experiments ); |
| 34 |
$success = 'Experiment' . $plural . ' activated successfully'; |
| 35 |
$error = 'Cannot activate experiment' . $plural; |
| 36 |
|
| 37 |
if ( $is_network ) { |
| 38 |
$success .= " for site {$site}"; |
| 39 |
$error .= " for site {$site}"; |
| 40 |
} |
| 41 |
|
| 42 |
$experiments_manager = Plugin::instance()->experiments; |
| 43 |
if ( ! $this->check_experiments_exist( $experiments_manager, $experiments ) ) { |
| 44 |
\WP_CLI::error( 'Experiments do not exist' . $args[0] ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( $is_network ) { |
| 48 |
$this->foreach_sites( $this->update_experiment_state, $experiments, Experiments_Manager::STATE_ACTIVE, $is_network, $success, $error ); |
| 49 |
} else { |
| 50 |
$this->update_experiment_state( $experiments, Experiments_Manager::STATE_ACTIVE, $is_network, $success, $error ); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Deactivate an Experiment |
| 56 |
* |
| 57 |
* ## EXAMPLES |
| 58 |
* |
| 59 |
* 1. wp elementor experiments deactivate container |
| 60 |
* - This will deactivate the Container experiment. |
| 61 |
* |
| 62 |
* @param array $args |
| 63 |
* @param array|null $assoc_args - Arguments from WP CLI command. |
| 64 |
*/ |
| 65 |
public function deactivate( $args, $assoc_args ) { |
| 66 |
if ( empty( $args[0] ) ) { |
| 67 |
\WP_CLI::error( 'Please specify an experiment.' ); |
| 68 |
} |
| 69 |
|
| 70 |
$is_network = $this->is_network( $assoc_args ); |
| 71 |
|
| 72 |
$experiments = $this->parse_experiments( $args[0] ); |
| 73 |
$plural = $this->get_plural( $experiments ); |
| 74 |
$success = 'Experiment' . $plural . ' deactivated successfully'; |
| 75 |
$error = 'Cannot deactivate experiment' . $plural; |
| 76 |
|
| 77 |
$experiments_manager = Plugin::instance()->experiments; |
| 78 |
if ( ! $this->check_experiments_exist( $experiments_manager, $experiments ) ) { |
| 79 |
\WP_CLI::error( 'Experiments do not exist' ); |
| 80 |
} |
| 81 |
|
| 82 |
if ( $is_network ) { |
| 83 |
$this->foreach_sites( $this->update_experiment_state, $experiments, Experiments_Manager::STATE_INACTIVE, $is_network, $success, $error ); |
| 84 |
} else { |
| 85 |
$this->update_experiment_state( $experiments, Experiments_Manager::STATE_INACTIVE, $is_network, $success, $error ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Experiment Status |
| 91 |
* |
| 92 |
* ## EXAMPLES |
| 93 |
* |
| 94 |
* 1. wp elementor experiments status container |
| 95 |
* - This will return the status of Container experiment. (active/inactive) |
| 96 |
* |
| 97 |
* @param array $args |
| 98 |
*/ |
| 99 |
public function status( $args ) { |
| 100 |
if ( empty( $args[0] ) ) { |
| 101 |
\WP_CLI::error( 'Please specify an experiment.' ); |
| 102 |
} |
| 103 |
|
| 104 |
$experiments_manager = Plugin::$instance->experiments; |
| 105 |
$experiments_status = $experiments_manager->is_feature_active( $args[0] ) ? 'active' : 'inactive'; |
| 106 |
|
| 107 |
\WP_CLI::line( $experiments_status ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Determine if the current website is a multisite. |
| 112 |
* |
| 113 |
* @param array|null $assoc_args - Arguments from WP CLI command. |
| 114 |
* |
| 115 |
* @return bool |
| 116 |
*/ |
| 117 |
private function is_network( $assoc_args ) { |
| 118 |
return ! empty( $assoc_args['network'] ) && is_multisite(); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Iterate over network sites and execute a callback. |
| 123 |
* |
| 124 |
* @param callable $callback - Callback to execute. Gets the site name & id as parameters. |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
private function foreach_sites( callable $callback, $experiments, $state, $is_network, $success, $error ) { |
| 129 |
$blog_ids = get_sites( [ |
| 130 |
'fields' => 'ids', |
| 131 |
'number' => 0, |
| 132 |
] ); |
| 133 |
|
| 134 |
foreach ( $blog_ids as $blog_id ) { |
| 135 |
switch_to_blog( $blog_id ); |
| 136 |
|
| 137 |
$callback( get_option( 'home' ), $experiments, $state, $is_network, $success, $error ); |
| 138 |
|
| 139 |
restore_current_blog(); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* @param string $experiments_str comma delimited string of experiments |
| 145 |
* |
| 146 |
* @return array array of experiments |
| 147 |
*/ |
| 148 |
private function parse_experiments( $experiments_str ) { |
| 149 |
return explode( ',', $experiments_str ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* @param array $experiments experiments |
| 154 |
* |
| 155 |
* @return string plural |
| 156 |
*/ |
| 157 |
private function get_plural( $experiments ) { |
| 158 |
return count( $experiments ) > 0 ? 's' : ''; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @param Experiments_Manager $experiments_manager manager |
| 163 |
* @param array $experiments experiments |
| 164 |
* |
| 165 |
* @return bool true when all experiments exist, otherwise false |
| 166 |
*/ |
| 167 |
private function check_experiments_exist( $experiments_manager, $experiments ) { |
| 168 |
foreach ( $experiments as $experiment ) { |
| 169 |
$feature = $experiments_manager->get_features( $experiment ); |
| 170 |
if ( ! $feature ) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
} |
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
private function update_experiment_state( $experiments, $state, $is_network, $success_message, $error_message, $site_id = '' ) { |
| 178 |
if ( $is_network ) { |
| 179 |
$success_message .= " for site {$site}"; |
| 180 |
$error_message .= " for site {$site}"; |
| 181 |
} |
| 182 |
|
| 183 |
$experiments_manager = Plugin::instance()->experiments; |
| 184 |
foreach ( $experiments as $experiment ) { |
| 185 |
$option = $experiments_manager->get_feature_option_key( $experiment ); |
| 186 |
update_option( $option, $state ); |
| 187 |
} |
| 188 |
|
| 189 |
try { |
| 190 |
\WP_CLI::success( $success_message ); |
| 191 |
} catch ( \Exception $e ) { |
| 192 |
\WP_CLI::error( $error_message ); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|