Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/elementor/modules/interactions/validation.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + namespace Elementor\Modules\Interactions;
4 +
5 + if ( ! defined( 'ABSPATH' ) ) {
6 + exit; // Exit if accessed directly.
7 + }
8 +
9 + class Validation {
10 + private $valid_ids = [];
11 + private $elements_to_interactions_counter = [];
12 + private $max_number_of_interactions = 5;
13 +
14 + public function __construct( Presets $presets ) {
15 + $this->valid_ids = array_column( $presets->list(), 'value' );
16 + }
17 +
18 + public function sanitize( $document ) {
19 + return $this->sanitize_document_data( $document );
20 + }
21 +
22 + public function validate() {
23 + foreach ( $this->elements_to_interactions_counter as $element_id => $number_of_interactions ) {
24 + if ( $number_of_interactions > $this->max_number_of_interactions ) {
25 + throw new \Exception(
26 + sprintf(
27 + // translators: %1 is the element ID and %2 is the maximum number of interactions
28 + esc_html__( 'Element %1$s has more than %2$d interactions', 'elementor' ),
29 + esc_html( $element_id ),
30 + esc_html( $this->max_number_of_interactions )
31 + )
32 + );
33 + }
34 + }
35 +
36 + return true;
37 + }
38 +
39 + private function sanitize_document_data( $data ) {
40 + if ( isset( $data['elements'] ) && is_array( $data['elements'] ) ) {
41 + $data['elements'] = $this->sanitize_elements_interactions( $data['elements'] );
42 + }
43 +
44 + return $data;
45 + }
46 +
47 + private function sanitize_elements_interactions( $elements ) {
48 + if ( ! is_array( $elements ) ) {
49 + return $elements;
50 + }
51 +
52 + foreach ( $elements as &$element ) {
53 + if ( isset( $element['interactions'] ) ) {
54 + $element['interactions'] = $this->sanitize_interactions( $element['interactions'], $element['id'] );
55 + }
56 +
57 + if ( isset( $element['elements'] ) && is_array( $element['elements'] ) ) {
58 + $element['elements'] = $this->sanitize_elements_interactions( $element['elements'] );
59 + }
60 + }
61 +
62 + return $elements;
63 + }
64 +
65 + private function decode_interactions( $interactions ) {
66 + if ( is_array( $interactions ) ) {
67 + return isset( $interactions['items'] ) ? $interactions['items'] : [];
68 + }
69 +
70 + if ( is_string( $interactions ) ) {
71 + $decoded = json_decode( $interactions, true );
72 + if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) {
73 + return isset( $decoded['items'] ) ? $decoded['items'] : [];
74 + }
75 + }
76 +
77 + return [];
78 + }
79 +
80 + private function increment_interactions_counter_for( $element_id ) {
81 + if ( ! array_key_exists( $element_id, $this->elements_to_interactions_counter ) ) {
82 + $this->elements_to_interactions_counter[ $element_id ] = 0;
83 + }
84 +
85 + ++$this->elements_to_interactions_counter[ $element_id ];
86 +
87 + return $this;
88 + }
89 +
90 + private function sanitize_interactions( $interactions, $element_id ) {
91 + $sanitized = [
92 + 'items' => [],
93 + 'version' => 1,
94 + ];
95 +
96 + $list_of_interactions = $this->decode_interactions( $interactions );
97 +
98 + foreach ( $list_of_interactions as $interaction ) {
99 + $animation_id = null;
100 +
101 + if ( is_string( $interaction ) ) {
102 + $animation_id = $interaction;
103 + } elseif ( is_array( $interaction ) && isset( $interaction['animation']['animation_id'] ) ) {
104 + $animation_id = $interaction['animation']['animation_id'];
105 + }
106 +
107 + if ( $animation_id && $this->is_valid_animation_id( $animation_id ) ) {
108 + $sanitized['items'][] = $interaction;
109 + $this->increment_interactions_counter_for( $element_id );
110 + }
111 + }
112 +
113 + if ( empty( $sanitized['items'] ) ) {
114 + return [];
115 + }
116 +
117 + return wp_json_encode( $sanitized );
118 + }
119 +
120 + private function is_valid_animation_id( $animation_id ) {
121 + if ( ! is_string( $animation_id ) || empty( $animation_id ) ) {
122 + return false;
123 + }
124 +
125 + $sanitized_id = sanitize_text_field( $animation_id );
126 +
127 + if ( $sanitized_id !== $animation_id ) {
128 + return false;
129 + }
130 +
131 + return in_array( $animation_id, $this->valid_ids, true );
132 + }
133 + }
134 +