first commit

This commit is contained in:
Ryan Ariana
2024-05-06 11:04:37 +07:00
commit aee061ddba
7322 changed files with 2918816 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
<?php
namespace Elementor\Core\Experiments\Exceptions;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Dependency_Exception extends \Exception {
}

View File

@@ -0,0 +1,133 @@
<?php
namespace Elementor\Core\Experiments;
use Elementor\Modules\System_Info\Reporters\Base;
use Elementor\Plugin;
/**
* Elementor experiments report.
*
* Elementor experiment report handler class responsible for generating a report for
* the experiments included in Elementor and their status.
*/
class Experiments_Reporter extends Base {
/**
* Get experiments reporter title.
*
* @return string Reporter title.
*/
public function get_title() {
return esc_html__( 'Elementor Experiments', 'elementor' );
}
/**
* Get experiments report fields.
*
* @return array Required report fields with field ID and field label.
*/
public function get_fields() {
return [
'experiments' => '',
];
}
/**
* Get Experiments.
*/
public function get_experiments() {
$result = [];
$experiments_manager = Plugin::$instance->experiments;
// TODO: Those keys should be at `$experiments_manager`.
$tracking_keys = [
'default',
'state',
'tags',
];
foreach ( $experiments_manager->get_features() as $feature_name => $feature_data ) {
$data_to_collect = [];
// Extract only tracking keys.
foreach ( $tracking_keys as $tracking_key ) {
if ( empty( $feature_data[ $tracking_key ] ) ) {
continue;
}
$data_to_collect[ $tracking_key ] = $feature_data[ $tracking_key ];
}
$result[ $feature_name ] = $data_to_collect;
}
return [
'value' => $result,
];
}
/**
* Get Raw Experiments.
*
* Retrieve a string containing the list of Elementor experiments and each experiment's status (active/inactive).
* The string is formatted in a non-table structure, and it is meant for export/download of the system info reports.
*
* @return array
*/
public function get_raw_experiments() {
$experiments = Plugin::$instance->experiments->get_features();
$output = '';
$is_first_item = true;
foreach ( $experiments as $experiment ) {
// If the state is default, add the default state to the string.
$state = Plugin::$instance->experiments->get_feature_state_label( $experiment );
// The first item automatically has a tab character before it. Add tabs only to the rest of the items.
if ( ! $is_first_item ) {
$output .= "\t";
}
$title = isset( $experiment['title'] ) ? $experiment['title'] : $experiment['name'];
$output .= $title . ': ' . $state . PHP_EOL;
$is_first_item = false;
}
return [
'value' => $output,
];
}
/**
* Get HTML Experiments.
*
* Retrieve the list of Elementor experiments and each experiment's status (active/inactive), in HTML table format.
*
* @return array
*/
public function get_html_experiments() {
$experiments = Plugin::$instance->experiments->get_features();
$output = '';
foreach ( $experiments as $experiment ) {
// If the state is default, add the default state to the string.
$state = Plugin::$instance->experiments->get_feature_state_label( $experiment );
$title = isset( $experiment['title'] ) ? $experiment['title'] : $experiment['name'];
$output .= '<tr><td>' . esc_html( $title ) . ': </td>';
$output .= '<td>' . esc_html( $state ) . '</td>';
$output .= '</tr>';
}
return [
'value' => $output,
];
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,31 @@
<?php
namespace Elementor\Core\Experiments;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Non_Existing_Dependency {
private $feature_id;
public function __construct( $feature_id ) {
$this->feature_id = $feature_id;
}
public function get_name() {
return $this->feature_id;
}
public function get_title() {
return $this->feature_id;
}
public function is_hidden() {
return false;
}
public static function instance( $feature_id ) {
return new static( $feature_id );
}
}

View File

@@ -0,0 +1,195 @@
<?php
namespace Elementor\Core\Experiments;
use Elementor\Core\Experiments\Manager as Experiments_Manager;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Wp_Cli extends \WP_CLI_Command {
/**
* Activate an Experiment
*
* ## EXAMPLES
*
* 1. wp elementor experiments activate container
* - This will activate the Container experiment.
*
* @param array $args
* @param array|null $assoc_args - Arguments from WP CLI command.
*/
public function activate( $args, $assoc_args ) {
if ( empty( $args[0] ) ) {
\WP_CLI::error( 'Please specify an experiment.' );
}
$is_network = $this->is_network( $assoc_args );
$experiments = $this->parse_experiments( $args[0] );
$plural = $this->get_plural( $experiments );
$success = 'Experiment' . $plural . ' activated successfully';
$error = 'Cannot activate experiment' . $plural;
if ( $is_network ) {
$success .= " for site {$site}";
$error .= " for site {$site}";
}
$experiments_manager = Plugin::instance()->experiments;
if ( ! $this->check_experiments_exist( $experiments_manager, $experiments ) ) {
\WP_CLI::error( 'Experiments do not exist' . $args[0] );
}
if ( $is_network ) {
$this->foreach_sites( $this->update_experiment_state, $experiments, Experiments_Manager::STATE_ACTIVE, $is_network, $success, $error );
} else {
$this->update_experiment_state( $experiments, Experiments_Manager::STATE_ACTIVE, $is_network, $success, $error );
}
}
/**
* Deactivate an Experiment
*
* ## EXAMPLES
*
* 1. wp elementor experiments deactivate container
* - This will deactivate the Container experiment.
*
* @param array $args
* @param array|null $assoc_args - Arguments from WP CLI command.
*/
public function deactivate( $args, $assoc_args ) {
if ( empty( $args[0] ) ) {
\WP_CLI::error( 'Please specify an experiment.' );
}
$is_network = $this->is_network( $assoc_args );
$experiments = $this->parse_experiments( $args[0] );
$plural = $this->get_plural( $experiments );
$success = 'Experiment' . $plural . ' deactivated successfully';
$error = 'Cannot deactivate experiment' . $plural;
$experiments_manager = Plugin::instance()->experiments;
if ( ! $this->check_experiments_exist( $experiments_manager, $experiments ) ) {
\WP_CLI::error( 'Experiments do not exist' );
}
if ( $is_network ) {
$this->foreach_sites( $this->update_experiment_state, $experiments, Experiments_Manager::STATE_INACTIVE, $is_network, $success, $error );
} else {
$this->update_experiment_state( $experiments, Experiments_Manager::STATE_INACTIVE, $is_network, $success, $error );
}
}
/**
* Experiment Status
*
* ## EXAMPLES
*
* 1. wp elementor experiments status container
* - This will return the status of Container experiment. (active/inactive)
*
* @param array $args
*/
public function status( $args ) {
if ( empty( $args[0] ) ) {
\WP_CLI::error( 'Please specify an experiment.' );
}
$experiments_manager = Plugin::$instance->experiments;
$experiments_status = $experiments_manager->is_feature_active( $args[0] ) ? 'active' : 'inactive';
\WP_CLI::line( $experiments_status );
}
/**
* Determine if the current website is a multisite.
*
* @param array|null $assoc_args - Arguments from WP CLI command.
*
* @return bool
*/
private function is_network( $assoc_args ) {
return ! empty( $assoc_args['network'] ) && is_multisite();
}
/**
* Iterate over network sites and execute a callback.
*
* @param callable $callback - Callback to execute. Gets the site name & id as parameters.
*
* @return void
*/
private function foreach_sites( callable $callback, $experiments, $state, $is_network, $success, $error ) {
$blog_ids = get_sites( [
'fields' => 'ids',
'number' => 0,
] );
foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
$callback( get_option( 'home' ), $experiments, $state, $is_network, $success, $error );
restore_current_blog();
}
}
/**
* @param string $experiments_str comma delimeted string of experiments
*
* @return array array of experiments
*/
private function parse_experiments( $experiments_str ) {
return explode( ',', $experiments_str );
}
/**
* @param array $experiments experiments
*
* @return string plural
*/
private function get_plural( $experiments ) {
return count( $experiments ) > 0 ? 's' : '';
}
/**
* @param Experiments_Manager $experiments_manager manager
* @param array $experiments experiments
*
* @return bool true when all experiments exist, otherwise false
*/
private function check_experiments_exist( $experiments_manager, $experiments ) {
foreach ( $experiments as $experiment ) {
$feature = $experiments_manager->get_features( $experiment );
if ( ! $feature ) {
return false;
}
}
return true;
}
private function update_experiment_state( $experiments, $state, $is_network, $success_message, $error_message, $site_id = '' ) {
if ( $is_network ) {
$success_message .= " for site {$site}";
$error_message .= " for site {$site}";
}
$experiments_manager = Plugin::instance()->experiments;
foreach ( $experiments as $experiment ) {
$option = $experiments_manager->get_feature_option_key( $experiment );
update_option( $option, $state );
}
try {
\WP_CLI::success( $success_message );
} catch ( \Exception $e ) {
\WP_CLI::error( $error_message );
}
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Elementor\Core\Experiments;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Wrap_Core_Dependency {
private $feature_data;
public function __construct( $feature_data ) {
$this->feature_data = $feature_data;
}
public function get_name() {
return $this->feature_data['name'];
}
public function get_title() {
return $this->feature_data['title'];
}
public function is_hidden() {
return $this->feature_data['hidden'];
}
public static function instance( $feature_data ) {
return new static( $feature_data );
}
}