PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.4
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.4
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / admin-core / inc / meta-ops.php

meta-ops.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.4, at admin-core/inc/meta-ops.php

289 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CartFlows Step Meta Helper.
4 *
5 * @package CartFlows
6 */
7
8 namespace CartflowsAdmin\AdminCore\Inc;
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * Class StepMeta.
17 */
18 class MetaOps {
19
20 /**
21 * Save Meta fields - Common Function.
22 *
23 * @param int $post_id post id.
24 * @param array $post_meta options to store.
25 * @param string $action action to check nonce.
26 *
27 * @return void
28 */
29 public static function save_meta_fields( $post_id, $post_meta, $action = '' ) {
30
31 if ( ! check_ajax_referer( $action, 'security', false ) ) {
32 $response_data = array( 'message' => __( 'Nonce validation failed', 'cartflows' ) );
33 wp_send_json_error( $response_data );
34 }
35
36 if ( ! ( $post_id && is_array( $post_meta ) ) ) {
37 return;
38 }
39
40 $allowed_html = array(
41 'a' => array(
42 'href' => array(),
43 ),
44 'br' => array(),
45 'strong' => array(),
46 'p' => array(),
47 'span' => array(),
48 );
49
50 foreach ( $post_meta as $key => $data ) {
51
52 if ( ! isset( $_POST[ $key ] ) ) {
53 continue;
54 }
55
56 $meta_value = false;
57
58 // Sanitize values.
59 $sanitize_filter = ( isset( $data['sanitize'] ) ) ? $data['sanitize'] : 'FILTER_DEFAULT';
60
61 switch ( $sanitize_filter ) {
62 case 'FILTER_SANITIZE_STRING':
63 $meta_value = isset( $_POST[ $key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) : '';
64 break;
65
66 case 'FILTER_SANITIZE_URL':
67 $meta_value = filter_input( INPUT_POST, $key, FILTER_SANITIZE_URL );
68 break;
69
70 case 'FILTER_SANITIZE_NUMBER_INT':
71 $meta_value = filter_input( INPUT_POST, $key, FILTER_SANITIZE_NUMBER_INT );
72 break;
73
74 case 'FILTER_CARTFLOWS_ARRAY':
75 if ( isset( $_POST[ $key ] ) && is_array( $_POST[ $key ] ) ) {
76 $meta_value = array_map( 'sanitize_text_field', wp_unslash( $_POST[ $key ] ) );
77 }
78 break;
79
80 case 'FILTER_SANITIZE_COLOR':
81 // Sanitizes a hex color with #.
82 $meta_value = sanitize_hex_color( $_POST[ $key ] );
83 break;
84
85 case 'FILTER_SANITIZE_FONT_FAMILY':
86 // FILTER_FLAG_NO_ENCODE_QUOTES - Do not encode the single and double quotes.
87 $meta_value = isset( $_POST[ $key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) : '';
88 break;
89
90 case 'FILTER_WP_KSES':
91 // It allow only tags that are defined in $allowed_html variable.
92 $meta_value = wp_kses( $_POST[ $key ], $allowed_html );
93 break;
94
95 case 'FILTER_SCRIPT':
96 // Custom JS/CSS sinks are output raw on the front end. Restrict authoring to
97 // users with `unfiltered_html` so per-plugin caps cannot grant script write
98 // access to lower roles (e.g. Editors via the role manager).
99 if ( ! current_user_can( 'unfiltered_html' ) ) {
100 continue 2;
101 }
102 // Reason for ignoring phpcs rule: Here we are saving the custom JS/CSS script. Encoding it before saving to db. No escaping function working here.
103 // We first decode any existing HTML entities to prevent double-encoding on multiple saves, then encode once.
104 $meta_value = htmlentities( html_entity_decode( wp_unslash( $_POST[ $key ] ), ENT_QUOTES, 'UTF-8' ), ENT_QUOTES, 'UTF-8' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
105 break;
106
107 case 'FILTER_WP_KSES_POST':
108 // wp_kses_post() allow only the same tags that are allowed in WP Posts.
109 $meta_value = wp_kses_post( $_POST[ $key ] );
110 break;
111
112 case 'FILTER_CARTFLOWS_CHECKOUT_PRODUCTS':
113 if ( isset( $_POST[ $key ] ) && is_array( $_POST[ $key ] ) ) {
114 $i = 0;
115 $q = 0;
116 $checkout_products = array();
117
118 $post_data = wc_clean( $_POST[ $key ] );
119
120 foreach ( $post_data as $p_index => $p_data ) {
121 if ( ! array_key_exists( 'product', $p_data ) ) {
122 continue;
123 }
124 foreach ( $p_data as $i_key => $i_value ) {
125
126 if ( is_array( $i_value ) ) {
127 foreach ( $i_value as $q_key => $q_value ) {
128 $checkout_products[ $i ][ $i_key ][ $q ] = array_map( 'sanitize_text_field', $q_value );
129
130 $q++;
131 }
132 } else {
133 $checkout_products[ $i ][ $i_key ] = sanitize_text_field( $i_value );
134 }
135 }
136
137 $i++;
138 }
139
140 // Preserve original delete-on-empty semantics (line ~270): only override $meta_value when products were collected.
141 if ( ! empty( $checkout_products ) ) {
142 $meta_value = $checkout_products;
143 }
144 }
145 break;
146
147 case 'FILTER_CARTFLOWS_CHECKOUT_FIELDS':
148 $count = 10;
149 $ordered_fields = array();
150 $billing_shipping_fields = array();
151
152 if ( isset( $_POST[ $key ] ) && is_array( $_POST[ $key ] ) ) {
153 // Ignoring sanitization rule as we are here to sanitize user input.
154 $post_data = $_POST[ $key ]; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
155
156 if ( 'wcf_field_order_billing' == $key || 'wcf_field_order_shipping' == $key ) {
157
158 $type_of_fields = ltrim( $key, 'wcf_field_order_' );
159 $billing_shipping_fields = \Cartflows_Helper::get_checkout_fields( $type_of_fields, $post_id );
160
161 foreach ( $post_data as $field_key_name => $value ) {
162
163 if ( isset( $billing_shipping_fields[ $field_key_name ] ) ) {
164
165 $is_custom_field = isset( $billing_shipping_fields[ $field_key_name ]['custom'] ) ? $billing_shipping_fields[ $field_key_name ]['custom'] : false;
166
167 $ordered_fields[ $field_key_name ] = $billing_shipping_fields[ $field_key_name ];
168
169 $ordered_fields[ $field_key_name ]['priority'] = $count;
170 $count += 10;
171 $placeholder_text = ! empty( $value['placeholder'] ) ? wc_clean( stripslashes( $value['placeholder'] ) ) : '';
172
173 $ordered_fields[ $field_key_name ]['width'] = intval( $value['width'] );
174 $ordered_fields[ $field_key_name ]['label'] = wp_kses_post( trim( stripslashes( $value['label'] ) ) );
175 $ordered_fields[ $field_key_name ]['placeholder'] = $placeholder_text;
176 $ordered_fields[ $field_key_name ]['default'] = wp_kses_post( trim( stripslashes( $value['default'] ) ) );
177 $ordered_fields[ $field_key_name ]['required'] = 'yes' === $value['required'] ? true : false;
178 $ordered_fields[ $field_key_name ]['optimized'] = 'yes' === $value['optimized'] ? true : false;
179 $ordered_fields[ $field_key_name ]['enabled'] = 'yes' === $value['enabled'] ? true : false;
180 $ordered_fields[ $field_key_name ]['options'] = '';
181
182 if ( isset( $value['options'] ) && $value['options'] ) {
183 $options = explode( '|', $value['options'] );
184 $options = AdminHelper::sanitize_array_values( $options );
185 $ordered_fields[ $field_key_name ]['options'] = array_combine( array_keys( $options ), $options );
186
187 }
188
189 if ( ! empty( $value['custom_attributes'] ) ) {
190 $ordered_fields[ $field_key_name ]['custom_attributes'] = wc_clean( $value['custom_attributes'] );
191 }
192
193 if ( true === $is_custom_field ) {
194
195 $ordered_fields[ $field_key_name ]['show_in_email'] = isset( $value['show_in_email'] ) && 'yes' === $value['show_in_email'] ? true : false;
196 }
197 }
198 }
199
200 $meta_value = $ordered_fields;
201 }
202 }
203
204 break;
205
206 case 'FILTER_CARTFLOWS_OPTIN_FIELDS':
207 $count = 10;
208 $ordered_fields = array();
209 $billing_shipping_fields = array();
210
211 if ( isset( $_POST[ $key ] ) && is_array( $_POST[ $key ] ) ) {
212 // Ignoring sanitization rule as we are here to sanitize user input.
213 $post_data = $_POST[ $key ]; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
214
215 if ( 'wcf-optin-fields-billing' === $key ) {
216
217 $type_of_fields = 'billing';
218 $billing_shipping_fields = \Cartflows_Helper::get_optin_fields( $type_of_fields, $post_id );
219
220 foreach ( $post_data as $field_key_name => $value ) {
221 if ( isset( $billing_shipping_fields[ $field_key_name ] ) ) {
222
223 $is_custom_field = isset( $billing_shipping_fields[ $field_key_name ]['custom'] ) ? $billing_shipping_fields[ $field_key_name ]['custom'] : false;
224
225 $ordered_fields[ $field_key_name ] = $billing_shipping_fields[ $field_key_name ];
226 $placeholder_text = ! empty( $value['placeholder'] ) ? wc_clean( stripslashes( $value['placeholder'] ) ) : '';
227 $ordered_fields[ $field_key_name ]['priority'] = $count;
228 $count += 10;
229
230 $ordered_fields[ $field_key_name ]['width'] = intval( $value['width'] );
231 $ordered_fields[ $field_key_name ]['label'] = wp_kses_post( trim( stripslashes( $value['label'] ) ) );
232 $ordered_fields[ $field_key_name ]['placeholder'] = $placeholder_text;
233 $ordered_fields[ $field_key_name ]['default'] = wp_kses_post( trim( stripslashes( $value['default'] ) ) );
234 $ordered_fields[ $field_key_name ]['required'] = 'yes' === $value['required'] ? true : false;
235 $ordered_fields[ $field_key_name ]['enabled'] = 'yes' === $value['enabled'] ? true : false;
236 $ordered_fields[ $field_key_name ]['options'] = '';
237
238 if ( isset( $value['options'] ) && $value['options'] ) {
239
240 $options = explode( '|', $value['options'] );
241 $options = AdminHelper::sanitize_array_values( $options );
242 $ordered_fields[ $field_key_name ]['options'] = array_combine( array_keys( $options ), $options );
243
244 }
245
246 if ( ! empty( $value['custom_attributes'] ) ) {
247 $ordered_fields[ $field_key_name ]['custom_attributes'] = wc_clean( $value['custom_attributes'] );
248 }
249
250 if ( true === $is_custom_field ) {
251
252 $ordered_fields[ $field_key_name ]['show_in_email'] = isset( $value['show_in_email'] ) && 'yes' === $value['show_in_email'] ? true : false;
253 }
254 }
255 }
256
257 $meta_value = $ordered_fields;
258 }
259 }
260 break;
261
262 default:
263 if ( 'FILTER_DEFAULT' === $sanitize_filter ) {
264 $meta_value = isset( $_POST[ $key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) : '';
265 } else {
266 $meta_value = apply_filters( 'cartflows_admin_save_meta_field_values', $meta_value, $post_id, $key, $sanitize_filter, $action );
267 }
268
269 break;
270 }
271
272 if ( is_null( $meta_value ) ) {
273 continue;
274 }
275
276 if ( false !== $meta_value ) {
277 update_post_meta( $post_id, $key, $meta_value );
278 // Track first checkout configured event.
279 if ( 'wcf-checkout-products' === $key ) {
280 \Cartflows_Helper::set_analytics_flag( 'first_checkout_configured', $post_id );
281 }
282 } else {
283 // To delete the wcf-checkout-products if empty.
284 delete_post_meta( $post_id, $key );
285 }
286 }
287 }
288 }
289