| 1 |
<?php |
| 2 |
namespace Elementor\Core\Experiments; |
| 3 |
|
| 4 |
use Elementor\Modules\System_Info\Reporters\Base; |
| 5 |
use Elementor\Plugin; |
| 6 |
|
| 7 |
/** |
| 8 |
* Elementor experiments report. |
| 9 |
* |
| 10 |
* Elementor experiment report handler class responsible for generating a report for |
| 11 |
* the experiments included in Elementor and their status. |
| 12 |
*/ |
| 13 |
class Experiments_Reporter extends Base { |
| 14 |
|
| 15 |
/** |
| 16 |
* Get experiments reporter title. |
| 17 |
* |
| 18 |
* @return string Reporter title. |
| 19 |
*/ |
| 20 |
public function get_title() { |
| 21 |
return esc_html__( 'Elementor Experiments', 'elementor' ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get experiments report fields. |
| 26 |
* |
| 27 |
* @return array Required report fields with field ID and field label. |
| 28 |
*/ |
| 29 |
public function get_fields() { |
| 30 |
return [ |
| 31 |
'experiments' => '', |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get Experiments. |
| 37 |
*/ |
| 38 |
public function get_experiments() { |
| 39 |
$result = []; |
| 40 |
|
| 41 |
$experiments_manager = Plugin::$instance->experiments; |
| 42 |
|
| 43 |
// TODO: Those keys should be at `$experiments_manager`. |
| 44 |
$tracking_keys = [ |
| 45 |
'default', |
| 46 |
'state', |
| 47 |
'tags', |
| 48 |
]; |
| 49 |
|
| 50 |
foreach ( $experiments_manager->get_features() as $feature_name => $feature_data ) { |
| 51 |
$data_to_collect = []; |
| 52 |
|
| 53 |
// Extract only tracking keys. |
| 54 |
foreach ( $tracking_keys as $tracking_key ) { |
| 55 |
if ( empty( $feature_data[ $tracking_key ] ) ) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
$data_to_collect[ $tracking_key ] = $feature_data[ $tracking_key ]; |
| 60 |
} |
| 61 |
|
| 62 |
$result[ $feature_name ] = $data_to_collect; |
| 63 |
} |
| 64 |
|
| 65 |
return [ |
| 66 |
'value' => $result, |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get Raw Experiments. |
| 72 |
* |
| 73 |
* Retrieve a string containing the list of Elementor experiments and each experiment's status (active/inactive). |
| 74 |
* The string is formatted in a non-table structure, and it is meant for export/download of the system info reports. |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public function get_raw_experiments() { |
| 79 |
$experiments = Plugin::$instance->experiments->get_features(); |
| 80 |
|
| 81 |
$output = ''; |
| 82 |
|
| 83 |
$is_first_item = true; |
| 84 |
|
| 85 |
foreach ( $experiments as $experiment ) { |
| 86 |
// If the state is default, add the default state to the string. |
| 87 |
$state = Plugin::$instance->experiments->get_feature_state_label( $experiment ); |
| 88 |
|
| 89 |
// The first item automatically has a tab character before it. Add tabs only to the rest of the items. |
| 90 |
if ( ! $is_first_item ) { |
| 91 |
$output .= "\t"; |
| 92 |
} |
| 93 |
|
| 94 |
$title = isset( $experiment['title'] ) ? $experiment['title'] : $experiment['name']; |
| 95 |
|
| 96 |
$output .= $title . ': ' . $state . PHP_EOL; |
| 97 |
|
| 98 |
$is_first_item = false; |
| 99 |
} |
| 100 |
|
| 101 |
return [ |
| 102 |
'value' => $output, |
| 103 |
]; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get HTML Experiments. |
| 108 |
* |
| 109 |
* Retrieve the list of Elementor experiments and each experiment's status (active/inactive), in HTML table format. |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public function get_html_experiments() { |
| 114 |
$experiments = Plugin::$instance->experiments->get_features(); |
| 115 |
|
| 116 |
$output = ''; |
| 117 |
|
| 118 |
foreach ( $experiments as $experiment ) { |
| 119 |
// If the state is default, add the default state to the string. |
| 120 |
$state = Plugin::$instance->experiments->get_feature_state_label( $experiment ); |
| 121 |
|
| 122 |
$title = isset( $experiment['title'] ) ? $experiment['title'] : $experiment['name']; |
| 123 |
|
| 124 |
$output .= '<tr><td>' . esc_html( $title ) . ': </td>'; |
| 125 |
$output .= '<td>' . esc_html( $state ) . '</td>'; |
| 126 |
$output .= '</tr>'; |
| 127 |
} |
| 128 |
|
| 129 |
return [ |
| 130 |
'value' => $output, |
| 131 |
]; |
| 132 |
} |
| 133 |
} |
| 134 |
|