| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\Onboarding; |
| 4 |
|
| 5 |
use Elementor\Tracker; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class Features_Usage { |
| 12 |
|
| 13 |
const ONBOARDING_FEATURES_OPTION = '_elementor_onboarding_features'; |
| 14 |
|
| 15 |
public function register() { |
| 16 |
if ( ! Tracker::is_allow_track() ) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
add_filter( 'elementor/tracker/send_tracking_data_params', function ( array $params ) { |
| 21 |
$params['usages']['onboarding_features'] = $this->get_usage_data(); |
| 22 |
|
| 23 |
return $params; |
| 24 |
} ); |
| 25 |
} |
| 26 |
|
| 27 |
public function save_onboarding_features( $raw_post_data ) { |
| 28 |
if ( empty( $raw_post_data ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$post_data = json_decode( $raw_post_data, true ); |
| 33 |
|
| 34 |
if ( empty( $post_data['features'] ) ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
$sanitized_features = $this->sanitize_features( $post_data['features'] ); |
| 39 |
|
| 40 |
if ( null === $sanitized_features ) { |
| 41 |
return [ |
| 42 |
'status' => 'error', |
| 43 |
'payload' => [ |
| 44 |
'error_message' => esc_html__( 'There was a problem saving your selected features.', 'elementor' ), |
| 45 |
], |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
update_option( static::ONBOARDING_FEATURES_OPTION, $sanitized_features ); |
| 50 |
|
| 51 |
return [ |
| 52 |
'status' => 'success', |
| 53 |
'payload' => [], |
| 54 |
]; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Sanitize the features selection payload. |
| 59 |
* |
| 60 |
* Expects an associative array of known tiers, each holding a list of |
| 61 |
* free-text feature labels (see assets/js/utils/utils.js), e.g.: |
| 62 |
* [ 'essential' => [ 'Templates & Theme Builder' ], 'advanced' => [], 'one' => [] ]. |
| 63 |
* |
| 64 |
* @param mixed $features |
| 65 |
* |
| 66 |
* @return array|null Sanitized array, or null if the payload shape is invalid. |
| 67 |
*/ |
| 68 |
private function sanitize_features( $features ) { |
| 69 |
if ( ! is_array( $features ) ) { |
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
$allowed_tiers = [ 'essential', 'advanced', 'one' ]; |
| 74 |
$sanitized = []; |
| 75 |
|
| 76 |
foreach ( $features as $tier => $selected_labels ) { |
| 77 |
if ( ! in_array( $tier, $allowed_tiers, true ) || ! is_array( $selected_labels ) ) { |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
$sanitized[ $tier ] = array_values( array_map( function ( $label ) { |
| 82 |
return is_string( $label ) ? sanitize_text_field( $label ) : null; |
| 83 |
}, $selected_labels ) ); |
| 84 |
|
| 85 |
if ( in_array( null, $sanitized[ $tier ], true ) ) { |
| 86 |
return null; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return $sanitized; |
| 91 |
} |
| 92 |
|
| 93 |
private function get_usage_data() { |
| 94 |
return get_option( static::ONBOARDING_FEATURES_OPTION, [] ); |
| 95 |
} |
| 96 |
} |
| 97 |
|