helper.php
| 1 | <?php |
| 2 | /** |
| 3 | * Sureforms Submit Class file. |
| 4 | * |
| 5 | * @package sureforms. |
| 6 | * @since 0.0.1 |
| 7 | */ |
| 8 | |
| 9 | namespace SRFM\Inc; |
| 10 | |
| 11 | use SRFM\Inc\Traits\Get_Instance; |
| 12 | use WP_Error; |
| 13 | use WP_REST_Request; |
| 14 | use WP_Post_Type; |
| 15 | use WP_Query; |
| 16 | use WP_Post; |
| 17 | |
| 18 | if ( ! defined( 'ABSPATH' ) ) { |
| 19 | exit; // Exit if accessed directly. |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Sureforms Helper Class. |
| 24 | * |
| 25 | * @since 0.0.1 |
| 26 | */ |
| 27 | class Helper { |
| 28 | use Get_Instance; |
| 29 | |
| 30 | /** |
| 31 | * Sureforms SVGs. |
| 32 | * |
| 33 | * @var mixed srfm_svgs |
| 34 | */ |
| 35 | private static $srfm_svgs = null; |
| 36 | |
| 37 | /** |
| 38 | * Get common error message. |
| 39 | * |
| 40 | * @since 0.0.2 |
| 41 | * @return array<string> |
| 42 | */ |
| 43 | public static function get_common_err_msg() { |
| 44 | return [ |
| 45 | 'required' => __( 'This field is required.', 'sureforms' ), |
| 46 | 'unique' => __( 'Value needs to be unique.', 'sureforms' ), |
| 47 | ]; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * Checks if current value is string or else returns default value |
| 53 | * |
| 54 | * @param mixed $data data which need to be checked if is string. |
| 55 | * |
| 56 | * @since 0.0.1 |
| 57 | * @return string |
| 58 | */ |
| 59 | public static function get_string_value( $data ) { |
| 60 | if ( is_scalar( $data ) ) { |
| 61 | return (string) $data; |
| 62 | } elseif ( is_object( $data ) && method_exists( $data, '__toString' ) ) { |
| 63 | return $data->__toString(); |
| 64 | } elseif ( is_null( $data ) ) { |
| 65 | return ''; |
| 66 | } else { |
| 67 | return ''; |
| 68 | } |
| 69 | } |
| 70 | /** |
| 71 | * Checks if current value is number or else returns default value |
| 72 | * |
| 73 | * @param mixed $value data which need to be checked if is string. |
| 74 | * @param int $base value can be set is $data is not a string, defaults to empty string. |
| 75 | * |
| 76 | * @since 0.0.1 |
| 77 | * @return int |
| 78 | */ |
| 79 | public static function get_integer_value( $value, $base = 10 ) { |
| 80 | if ( is_numeric( $value ) ) { |
| 81 | return (int) $value; |
| 82 | } elseif ( is_string( $value ) ) { |
| 83 | $trimmed_value = trim( $value ); |
| 84 | return intval( $trimmed_value, $base ); |
| 85 | } else { |
| 86 | return 0; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Checks if current value is an array or else returns default value |
| 92 | * |
| 93 | * @param mixed $data Data which needs to be checked if it is an array. |
| 94 | * |
| 95 | * @since 0.0.3 |
| 96 | * @return array<mixed> |
| 97 | */ |
| 98 | public static function get_array_value( $data ) { |
| 99 | if ( is_array( $data ) ) { |
| 100 | return $data; |
| 101 | } elseif ( is_null( $data ) ) { |
| 102 | return []; |
| 103 | } else { |
| 104 | return (array) $data; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Extracts the field type from the dynamic field key ( or field slug ). |
| 110 | * |
| 111 | * @param string $field_key Dynamic field key. |
| 112 | * @since 0.0.6 |
| 113 | * @return string Extracted field type. |
| 114 | */ |
| 115 | public static function get_field_type_from_key( $field_key ) { |
| 116 | |
| 117 | if ( false === strpos( $field_key, '-lbl-' ) ) { |
| 118 | return ''; |
| 119 | } |
| 120 | |
| 121 | return trim( explode( '-', $field_key )[1] ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the proper sanitize callback functions according to the field type. |
| 126 | * |
| 127 | * @param string $field_type HTML field type. |
| 128 | * @since 0.0.6 |
| 129 | * @return callable Returns sanitize callbacks according to the provided field type. |
| 130 | */ |
| 131 | public static function get_field_type_sanitize_function( $field_type ) { |
| 132 | $callbacks = apply_filters( |
| 133 | 'srfm_field_type_sanitize_functions', |
| 134 | [ |
| 135 | 'url' => 'esc_url_raw', |
| 136 | 'input' => 'sanitize_text_field', |
| 137 | 'number' => [ __CLASS__, 'sanitize_number' ], |
| 138 | 'email' => 'sanitize_email', |
| 139 | 'textarea' => 'sanitize_textarea_field', |
| 140 | ] |
| 141 | ); |
| 142 | |
| 143 | return isset( $callbacks[ $field_type ] ) ? $callbacks[ $field_type ] : 'sanitize_text_field'; |
| 144 | |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Sanitizes a numeric value. |
| 149 | * |
| 150 | * This function checks if the input value is numeric. If it is numeric, it sanitizes |
| 151 | * the value to ensure it's a float or integer, allowing for fractions and thousand separators. |
| 152 | * If the value is not numeric, it sanitizes it as a text field. |
| 153 | * |
| 154 | * @param mixed $value The value to be sanitized. |
| 155 | * @since 0.0.6 |
| 156 | * @return integer|float|string The sanitized value. |
| 157 | */ |
| 158 | public static function sanitize_number( $value ) { |
| 159 | if ( ! is_numeric( $value ) ) { |
| 160 | // phpcs:ignore /** @phpstan-ignore-next-line */ |
| 161 | return sanitize_text_field( $value ); // If it is not numeric, then let user get some sanitized data to view. |
| 162 | } |
| 163 | |
| 164 | // phpcs:ignore /** @phpstan-ignore-next-line */ |
| 165 | return sanitize_text_field( filter_var( $value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND ) ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * This function sanitizes the submitted form data according to the field type. |
| 170 | * |
| 171 | * @param array<mixed> $form_data $form_data User submitted form data. |
| 172 | * @since 0.0.6 |
| 173 | * @return array<mixed> $result Sanitized form data. |
| 174 | */ |
| 175 | public static function sanitize_by_field_type( $form_data ) { |
| 176 | $result = []; |
| 177 | |
| 178 | if ( empty( $form_data ) || ! is_array( $form_data ) ) { |
| 179 | return $result; |
| 180 | } |
| 181 | |
| 182 | foreach ( $form_data as $field_key => &$value ) { |
| 183 | $field_type = self::get_field_type_from_key( $field_key ); |
| 184 | $sanitize_function = self::get_field_type_sanitize_function( $field_type ); |
| 185 | $sanitized_data = is_array( $value ) ? self::sanitize_by_field_type( $value ) : call_user_func( $sanitize_function, $value ); |
| 186 | |
| 187 | $result[ $field_key ] = $sanitized_data; |
| 188 | } |
| 189 | |
| 190 | return $result; |
| 191 | |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * This function performs array_map for multi dimensional array |
| 196 | * |
| 197 | * @param string $function function name to be applied on each element on array. |
| 198 | * @param array<mixed> $data_array array on which function needs to be performed. |
| 199 | * @return array<mixed> |
| 200 | * @since 0.0.1 |
| 201 | */ |
| 202 | public static function sanitize_recursively( $function, $data_array ) { |
| 203 | $response = []; |
| 204 | if ( is_array( $data_array ) ) { |
| 205 | if ( ! is_callable( $function ) ) { |
| 206 | return $data_array; |
| 207 | } |
| 208 | foreach ( $data_array as $key => $data ) { |
| 209 | $val = is_array( $data ) ? self::sanitize_recursively( $function, $data ) : $function( $data ); |
| 210 | $response[ $key ] = $val; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return $response; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Generates common markup liked label, etc |
| 219 | * |
| 220 | * @param int|string $form_id form id. |
| 221 | * @param string $type Type of form markup. |
| 222 | * @param string $label Label for the form markup. |
| 223 | * @param string $slug Slug for the form markup. |
| 224 | * @param string $block_id Block id for the form markup. |
| 225 | * @param bool $required If field is required or not. |
| 226 | * @param string $help Help for the form markup. |
| 227 | * @param string $error_msg Error message for the form markup. |
| 228 | * @param bool $is_unique Check if the field is unique. |
| 229 | * @param string $duplicate_msg Duplicate message for field. |
| 230 | * @param bool $override Override for error markup. |
| 231 | * @return string |
| 232 | * @since 0.0.1 |
| 233 | */ |
| 234 | public static function generate_common_form_markup( $form_id, $type, $label = '', $slug = '', $block_id = '', $required = false, $help = '', $error_msg = '', $is_unique = false, $duplicate_msg = '', $override = false ) { |
| 235 | $duplicate_msg = $duplicate_msg ? ' data-unique-msg="' . $duplicate_msg . '"' : ''; |
| 236 | |
| 237 | $markup = ''; |
| 238 | $show_labels_as_placeholder = get_post_meta( self::get_integer_value( $form_id ), '_srfm_use_label_as_placeholder', true ); |
| 239 | $show_labels_as_placeholder = $show_labels_as_placeholder ? self::get_string_value( $show_labels_as_placeholder ) : false; |
| 240 | |
| 241 | switch ( $type ) { |
| 242 | case 'label': |
| 243 | $markup = $label ? '<label for="srfm-' . $slug . '-' . esc_attr( $block_id ) . '" class="srfm-block-label">' . htmlspecialchars_decode( esc_html( $label ) ) . ( $required ? '<span class="srfm-required"> *</span>' : '' ) . '</label>' : ''; |
| 244 | break; |
| 245 | case 'help': |
| 246 | $markup = $help ? '<div class="srfm-description" id="srfm-description-' . esc_attr( $block_id ) . '">' . esc_html( $help ) . '</div>' : ''; |
| 247 | break; |
| 248 | case 'error': |
| 249 | $markup = $required || $override ? '<div class="srfm-error-message" id="srfm-error-' . esc_attr( $block_id ) . '" data-error-msg="' . esc_attr( $error_msg ) . '"' . esc_attr( $duplicate_msg ) . '>' . esc_html( $error_msg ) . '</div>' : ''; |
| 250 | break; |
| 251 | case 'is_unique': |
| 252 | $markup = $is_unique ? '<div class="srfm-error">' . esc_html( $duplicate_msg ) . '</div>' : ''; |
| 253 | break; |
| 254 | case 'placeholder': |
| 255 | $markup = $label && '1' === $show_labels_as_placeholder ? $label . ( $required ? ' *' : '' ) : ''; |
| 256 | break; |
| 257 | default: |
| 258 | $markup = ''; |
| 259 | } |
| 260 | |
| 261 | return $markup; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | /** |
| 266 | * Get an SVG Icon |
| 267 | * |
| 268 | * @since 0.0.1 |
| 269 | * @param string $icon the icon name. |
| 270 | * @param string $class if the baseline class should be added. |
| 271 | * @param string $html Custom attributes inside svg wrapper. |
| 272 | * @return string |
| 273 | */ |
| 274 | public static function fetch_svg( $icon = '', $class = '', $html = '' ) { |
| 275 | $class = $class ? ' ' . $class : ''; |
| 276 | |
| 277 | $output = '<span class="srfm-icon' . $class . '" ' . $html . '>'; |
| 278 | if ( ! self::$srfm_svgs ) { |
| 279 | ob_start(); |
| 280 | |
| 281 | include_once SRFM_DIR . 'assets/svg/svgs.json'; |
| 282 | self::$srfm_svgs = json_decode( self::get_string_value( ob_get_clean() ), true ); |
| 283 | self::$srfm_svgs = apply_filters( 'srfm_svg_icons', self::$srfm_svgs ); |
| 284 | } |
| 285 | |
| 286 | $output .= isset( self::$srfm_svgs[ $icon ] ) ? self::$srfm_svgs[ $icon ] : ''; |
| 287 | $output .= '</span>'; |
| 288 | |
| 289 | return $output; |
| 290 | } |
| 291 | |
| 292 | |
| 293 | /** |
| 294 | * Encrypt data using base64. |
| 295 | * |
| 296 | * @param string $input The input string which needs to be encrypted. |
| 297 | * @since 0.0.1 |
| 298 | * @return string The encrypted string. |
| 299 | */ |
| 300 | public static function encrypt( $input ) { |
| 301 | // If the input is empty or not a string, then abandon ship. |
| 302 | if ( empty( $input ) || ! is_string( $input ) ) { |
| 303 | return ''; |
| 304 | } |
| 305 | |
| 306 | // Encrypt the input and return it. |
| 307 | $base_64 = base64_encode( $input ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 308 | $encode = rtrim( $base_64, '=' ); |
| 309 | return $encode; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Decrypt data using base64. |
| 314 | * |
| 315 | * @param string $input The input string which needs to be decrypted. |
| 316 | * @since 0.0.1 |
| 317 | * @return string The decrypted string. |
| 318 | */ |
| 319 | public static function decrypt( $input ) { |
| 320 | // If the input is empty or not a string, then abandon ship. |
| 321 | if ( empty( $input ) || ! is_string( $input ) ) { |
| 322 | return ''; |
| 323 | } |
| 324 | |
| 325 | // Decrypt the input and return it. |
| 326 | $base_64 = $input . str_repeat( '=', strlen( $input ) % 4 ); |
| 327 | $decode = base64_decode( $base_64 ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 328 | return $decode; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Update an option from the database. |
| 333 | * |
| 334 | * @param string $key The option key. |
| 335 | * @param mixed $value The value to update. |
| 336 | * @param bool $network_override Whether to allow the network_override admin setting to be overridden on subsites. |
| 337 | * @since 0.0.1 |
| 338 | * @return bool True if the option was updated, false otherwise. |
| 339 | */ |
| 340 | public static function update_admin_settings_option( $key, $value, $network_override = false ) { |
| 341 | // Update the site-wide option if we're in the network admin, and return the updated status. |
| 342 | return $network_override && is_multisite() ? update_site_option( $key, $value ) : update_option( $key, $value ); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Update an option from the database. |
| 347 | * |
| 348 | * @param int|string $post_id post id / form id. |
| 349 | * @param string $key meta key name. |
| 350 | * @param bool $single single or multiple. |
| 351 | * @param mixed $default default value. |
| 352 | * |
| 353 | * @since 0.0.1 |
| 354 | * @return string Meta value. |
| 355 | */ |
| 356 | public static function get_meta_value( $post_id, $key, $single = true, $default = '' ) { |
| 357 | $meta_value = get_post_meta( self::get_integer_value( $post_id ), $key, $single ) ? self::get_string_value( get_post_meta( self::get_integer_value( $post_id ), $key, $single ) ) : self::get_string_value( $default ); |
| 358 | return $meta_value; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Wrapper for the WordPress's get_post_meta function with the support for default values. |
| 363 | * |
| 364 | * @param int|string $post_id Post ID. |
| 365 | * @param string $key The meta key to retrieve. |
| 366 | * @param mixed $default Default value. |
| 367 | * @param boolean $single Optional. Whether to return a single value. |
| 368 | * @since 0.0.8 |
| 369 | * @return mixed Meta value. |
| 370 | */ |
| 371 | public static function get_post_meta( $post_id, $key, $default = null, $single = true ) { |
| 372 | $meta_value = get_post_meta( self::get_integer_value( $post_id ), $key, $single ); |
| 373 | return $meta_value ? $meta_value : $default; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Returns query params data for instant form live preview. |
| 378 | * |
| 379 | * @since 0.0.8 |
| 380 | * @return array<mixed> Live preview data. |
| 381 | */ |
| 382 | public static function get_instant_form_live_data() { |
| 383 | $srfm_live_mode_data = isset( $_GET['live_mode'] ) && current_user_can( 'edit_posts' ) ? self::sanitize_recursively( 'sanitize_text_field', wp_unslash( $_GET ) ) : []; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 384 | |
| 385 | return $srfm_live_mode_data ? array_map( |
| 386 | // Normalize falsy values. |
| 387 | function( $live_data ) { |
| 388 | return 'false' === $live_data ? false : $live_data; |
| 389 | }, |
| 390 | $srfm_live_mode_data |
| 391 | ) : []; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | /** |
| 396 | * Default dynamic block value. |
| 397 | * |
| 398 | * @since 0.0.1 |
| 399 | * @return string[] Meta value. |
| 400 | */ |
| 401 | public static function default_dynamic_block_option() { |
| 402 | |
| 403 | $common_err_msg = self::get_common_err_msg(); |
| 404 | |
| 405 | $default_values = [ |
| 406 | 'srfm_url_block_required_text' => $common_err_msg['required'], |
| 407 | 'srfm_input_block_required_text' => $common_err_msg['required'], |
| 408 | 'srfm_input_block_unique_text' => $common_err_msg['unique'], |
| 409 | 'srfm_address_block_required_text' => $common_err_msg['required'], |
| 410 | 'srfm_phone_block_required_text' => $common_err_msg['required'], |
| 411 | 'srfm_phone_block_unique_text' => $common_err_msg['unique'], |
| 412 | 'srfm_number_block_required_text' => $common_err_msg['required'], |
| 413 | 'srfm_textarea_block_required_text' => $common_err_msg['required'], |
| 414 | 'srfm_multi_choice_block_required_text' => $common_err_msg['required'], |
| 415 | 'srfm_checkbox_block_required_text' => $common_err_msg['required'], |
| 416 | 'srfm_gdpr_block_required_text' => $common_err_msg['required'], |
| 417 | 'srfm_email_block_required_text' => $common_err_msg['required'], |
| 418 | 'srfm_email_block_unique_text' => $common_err_msg['unique'], |
| 419 | 'srfm_dropdown_block_required_text' => $common_err_msg['required'], |
| 420 | ]; |
| 421 | |
| 422 | return apply_filters( 'srfm_default_dynamic_block_option', $default_values, $common_err_msg ); |
| 423 | |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Get default dynamic block value. |
| 428 | * |
| 429 | * @param string $key meta key name. |
| 430 | * @since 0.0.1 |
| 431 | * @return string Meta value. |
| 432 | */ |
| 433 | public static function get_default_dynamic_block_option( $key ) { |
| 434 | $default_dynamic_values = self::default_dynamic_block_option(); |
| 435 | $option = get_option( 'get_default_dynamic_block_option', $default_dynamic_values ); |
| 436 | |
| 437 | if ( is_array( $option ) && array_key_exists( $key, $option ) ) { |
| 438 | return $option[ $key ]; |
| 439 | } else { |
| 440 | return ''; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Checks whether a given request has appropriate permissions. |
| 446 | * |
| 447 | * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
| 448 | * @since 0.0.1 |
| 449 | */ |
| 450 | public static function get_items_permissions_check() { |
| 451 | if ( current_user_can( 'edit_posts' ) ) { |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | foreach ( get_post_types( [ 'show_in_rest' => true ], 'objects' ) as $post_type ) { |
| 456 | /** |
| 457 | * The post type. |
| 458 | * |
| 459 | * @var WP_Post_Type $post_type |
| 460 | */ |
| 461 | if ( current_user_can( $post_type->cap->edit_posts ) ) { |
| 462 | return true; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return new WP_Error( |
| 467 | 'rest_cannot_view', |
| 468 | __( 'Sorry, you are not allowed to perform this action.', 'sureforms' ), |
| 469 | [ 'status' => \rest_authorization_required_code() ] |
| 470 | ); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Check if the current user has a given capability. |
| 475 | * |
| 476 | * @param string $capability The capability to check. |
| 477 | * @since 0.0.3 |
| 478 | * @return bool Whether the current user has the given capability or role. |
| 479 | */ |
| 480 | public static function current_user_can( $capability = '' ) { |
| 481 | |
| 482 | if ( ! function_exists( 'current_user_can' ) ) { |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | if ( ! is_string( $capability ) || empty( $capability ) ) { |
| 487 | $capability = 'edit_posts'; |
| 488 | } |
| 489 | |
| 490 | return current_user_can( $capability ); |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Get all the entries for the given form ids. The entries are older than the given days_old. |
| 495 | * |
| 496 | * @param int $days_old The number of days old the entries should be. |
| 497 | * @param array<int> $sf_form_ids The form ids for which the entries need to be fetched. |
| 498 | * @since 0.0.2 |
| 499 | * @return array<int|WP_Post> the entries matching the criteria. |
| 500 | */ |
| 501 | public static function get_entries_from_form_ids( $days_old = 0, $sf_form_ids = [] ) { |
| 502 | |
| 503 | $entries = []; |
| 504 | |
| 505 | foreach ( $sf_form_ids as $form_id ) { |
| 506 | $args = [ |
| 507 | 'post_type' => 'sureforms_entry', |
| 508 | 'post_status' => 'publish', |
| 509 | 'date_query' => [ |
| 510 | [ |
| 511 | 'before' => $days_old . ' days ago', |
| 512 | ], |
| 513 | ], |
| 514 | 'meta_query' // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query. -- We require meta_query for this function to work. |
| 515 | => [ |
| 516 | [ |
| 517 | 'key' => '_srfm_entry_form_id', |
| 518 | 'value' => $form_id, |
| 519 | 'compare' => '=', |
| 520 | ], |
| 521 | ], |
| 522 | ]; |
| 523 | |
| 524 | $query = new WP_Query( $args ); |
| 525 | |
| 526 | // store all the entries in an single array. |
| 527 | $entries = array_merge( $entries, $query->posts ); |
| 528 | } |
| 529 | |
| 530 | return $entries; |
| 531 | |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Decode block attributes. |
| 536 | * The function reverses the effect of serialize_block_attributes() |
| 537 | * |
| 538 | * @link https://developer.wordpress.org/reference/functions/serialize_block_attributes/ |
| 539 | * @param string $encoded_data the encoded block attribute. |
| 540 | * @since 0.0.2 |
| 541 | * @return string decoded block attribute |
| 542 | */ |
| 543 | public static function decode_block_attribute( $encoded_data = '' ) { |
| 544 | $decoded_data = preg_replace( '/\\\\u002d\\\\u002d/', '--', self::get_string_value( $encoded_data ) ); |
| 545 | $decoded_data = preg_replace( '/\\\\u003c/', '<', self::get_string_value( $decoded_data ) ); |
| 546 | $decoded_data = preg_replace( '/\\\\u003e/', '>', self::get_string_value( $decoded_data ) ); |
| 547 | $decoded_data = preg_replace( '/\\\\u0026/', '&', self::get_string_value( $decoded_data ) ); |
| 548 | $decoded_data = preg_replace( '/\\\\\\\\"/', '"', self::get_string_value( $decoded_data ) ); |
| 549 | return self::get_string_value( $decoded_data ); |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * Map slugs to submission data. |
| 554 | * |
| 555 | * @param array<mixed> $submission_data submission_data. |
| 556 | * @since 0.0.3 |
| 557 | * @return array<mixed> |
| 558 | */ |
| 559 | public static function map_slug_to_submission_data( $submission_data = [] ) { |
| 560 | $mapped_data = []; |
| 561 | foreach ( $submission_data as $key => $value ) { |
| 562 | $label = explode( '-lbl-', $key )[1]; |
| 563 | $slug = implode( '-', array_slice( explode( '-', $label ), 1 ) ); |
| 564 | $mapped_data[ $slug ] = $value; |
| 565 | } |
| 566 | return $mapped_data; |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Get forms options. Shows all the available forms in the dropdown. |
| 571 | * |
| 572 | * @since 0.0.5 |
| 573 | * @param string $key Determines the type of data to return. |
| 574 | * @return array<mixed> |
| 575 | */ |
| 576 | public static function get_sureforms( $key = '' ) { |
| 577 | $forms = get_posts( |
| 578 | apply_filters( |
| 579 | 'srfm_get_sureforms_query_args', |
| 580 | [ |
| 581 | 'post_type' => SRFM_FORMS_POST_TYPE, |
| 582 | 'posts_per_page' => -1, |
| 583 | 'post_status' => 'publish', |
| 584 | ] |
| 585 | ) |
| 586 | ); |
| 587 | |
| 588 | $options = []; |
| 589 | |
| 590 | foreach ( $forms as $form ) { |
| 591 | if ( $form instanceof WP_Post ) { |
| 592 | if ( 'all' === $key ) { |
| 593 | $options[ $form->ID ] = $form; |
| 594 | } elseif ( ! empty( $key ) && is_string( $key ) && isset( $form->$key ) ) { |
| 595 | $options[ $form->ID ] = $form->$key; |
| 596 | } else { |
| 597 | $options[ $form->ID ] = $form->post_title; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | return $options; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Get all the forms. |
| 607 | * |
| 608 | * @since 0.0.5 |
| 609 | * @return array<mixed> |
| 610 | */ |
| 611 | public static function get_sureforms_title_with_ids() { |
| 612 | $form_options = self::get_sureforms(); |
| 613 | |
| 614 | foreach ( $form_options as $key => $value ) { |
| 615 | $form_options[ $key ] = $value . ' #' . $key; |
| 616 | } |
| 617 | |
| 618 | return $form_options; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Get the CSS variables based on different field spacing sizes. |
| 623 | * |
| 624 | * @param string|null $field_spacing The field spacing size or boolean false to return complete sizes array. |
| 625 | * |
| 626 | * @since 0.0.7 |
| 627 | * @return array<string|mixed> |
| 628 | */ |
| 629 | public static function get_css_vars( $field_spacing = null ) { |
| 630 | /** |
| 631 | * $sizes - Field Spacing Sizes Variables. |
| 632 | * The array contains the CSS variables for different field spacing sizes. |
| 633 | * Each key corresponds to the field spacing size, and the value is an array of CSS variables. |
| 634 | * |
| 635 | * For future variables depending on the field spacing size, add the variable to the array respectively. |
| 636 | */ |
| 637 | $sizes = apply_filters( |
| 638 | 'srfm_css_vars_sizes', |
| 639 | [ |
| 640 | 'small' => [ |
| 641 | '--srfm-row-gap-between-blocks' => '16px', |
| 642 | // Address block gap and spacing variables. |
| 643 | '--srfm-col-gap-between-fields' => '12px', |
| 644 | '--srfm-row-gap-between-fields' => '12px', |
| 645 | '--srfm-gap-below-address-label' => '12px', |
| 646 | // Dropdown Variables. |
| 647 | '--srfm-dropdown-font-size' => '14px', |
| 648 | '--srfm-dropdown-gap-between-input-menu' => '4px', |
| 649 | '--srfm-dropdown-badge-padding' => '2px 6px', |
| 650 | '--srfm-dropdown-multiselect-font-size' => '12px', |
| 651 | '--srfm-dropdown-multiselect-line-height' => '16px', |
| 652 | '--srfm-dropdown-padding-right' => '12px', |
| 653 | // initial padding and from 20px - 12px for dropdown arrow width and 8px for gap before dropdown arrow. |
| 654 | '--srfm-dropdown-padding-right-icon' => 'calc( var( --srfm-dropdown-padding-right ) + 20px )', |
| 655 | '--srfm-dropdown-multiselect-padding' => '8px var( --srfm-dropdown-padding-right-icon ) 8px 8px', |
| 656 | // Input Field Variables. |
| 657 | '--srfm-input-height' => '40px', |
| 658 | '--srfm-input-field-padding' => '10px 12px', |
| 659 | '--srfm-input-field-font-size' => '14px', |
| 660 | '--srfm-input-field-line-height' => '20px', |
| 661 | '--srfm-input-field-margin' => '4px 0', |
| 662 | // Checkbox and GDPR Variables. |
| 663 | '--srfm-check-ctn-width' => '16px', |
| 664 | '--srfm-check-ctn-height' => '16px', |
| 665 | '--srfm-check-svg-size' => '10px', |
| 666 | '--srfm-checkbox-margin-top-frontend' => '2px', |
| 667 | '--srfm-checkbox-margin-top-editor' => '3px', |
| 668 | '--srfm-check-gap' => '8px', |
| 669 | '--srfm-checkbox-description-margin-left' => '24px', |
| 670 | // Phone Number field variables. |
| 671 | '--srfm-flag-section-padding' => '10px 0 10px 12px', |
| 672 | '--srfm-gap-between-icon-text' => '8px', |
| 673 | // Label Variables. |
| 674 | '--srfm-label-font-size' => '14px', |
| 675 | '--srfm-label-line-height' => '20px', |
| 676 | // Description Variables. |
| 677 | '--srfm-description-font-size' => '12px', |
| 678 | '--srfm-description-line-height' => '16px', |
| 679 | // Button Variables. |
| 680 | '--srfm-btn-padding' => '8px 14px', |
| 681 | '--srfm-btn-font-size' => '14px', |
| 682 | '--srfm-btn-line-height' => '20px', |
| 683 | // Multi Choice Variables. |
| 684 | '--srfm-multi-choice-horizontal-padding' => '16px', |
| 685 | '--srfm-multi-choice-vertical-padding' => '16px', |
| 686 | '--srfm-multi-choice-internal-option-gap' => '8px', |
| 687 | '--srfm-multi-choice-vertical-svg-size' => '32px', |
| 688 | '--srfm-multi-choice-horizontal-image-size' => '20px', |
| 689 | '--srfm-multi-choice-vertical-image-size' => '100px', |
| 690 | '--srfm-multi-choice-outer-padding' => '0', |
| 691 | ], |
| 692 | 'medium' => [ |
| 693 | '--srfm-row-gap-between-blocks' => '20px', |
| 694 | // Address block gap and spacing variables. |
| 695 | '--srfm-col-gap-between-fields' => '16px', |
| 696 | '--srfm-row-gap-between-fields' => '16px', |
| 697 | '--srfm-gap-below-address-label' => '14px', |
| 698 | // Input Field Variables. |
| 699 | '--srfm-input-height' => '44px', |
| 700 | '--srfm-input-field-font-size' => '16px', |
| 701 | '--srfm-input-field-line-height' => '24px', |
| 702 | '--srfm-input-field-margin' => '6px 0', |
| 703 | // Checkbox and GDPR Variables. |
| 704 | '--srfm-checkbox-margin-top-frontend' => '4px', |
| 705 | '--srfm-checkbox-margin-top-editor' => '6px', |
| 706 | '--srfm-checkbox-description-margin-left' => '24px', |
| 707 | // Label Variables. |
| 708 | '--srfm-label-font-size' => '16px', |
| 709 | '--srfm-label-line-height' => '24px', |
| 710 | // Description Variables. |
| 711 | '--srfm-description-font-size' => '14px', |
| 712 | '--srfm-description-line-height' => '20px', |
| 713 | // Button Variables. |
| 714 | '--srfm-btn-padding' => '10px 14px', |
| 715 | '--srfm-btn-font-size' => '16px', |
| 716 | '--srfm-btn-line-height' => '24px', |
| 717 | // Multi Choice Variables. |
| 718 | '--srfm-multi-choice-horizontal-padding' => '20px', |
| 719 | '--srfm-multi-choice-vertical-padding' => '20px', |
| 720 | '--srfm-multi-choice-vertical-svg-size' => '40px', |
| 721 | '--srfm-multi-choice-horizontal-image-size' => '24px', |
| 722 | '--srfm-multi-choice-vertical-image-size' => '120px', |
| 723 | '--srfm-multi-choice-outer-padding' => '2px', |
| 724 | ], |
| 725 | 'large' => [ |
| 726 | '--srfm-row-gap-between-blocks' => '24px', |
| 727 | // Address Block Gap and Spacing Variables. |
| 728 | '--srfm-col-gap-between-fields' => '16px', |
| 729 | '--srfm-row-gap-between-fields' => '20px', |
| 730 | '--srfm-gap-below-address-label' => '16px', |
| 731 | // Dropdown Variables. |
| 732 | '--srfm-dropdown-font-size' => '16px', |
| 733 | '--srfm-dropdown-gap-between-input-menu' => '6px', |
| 734 | '--srfm-dropdown-badge-padding' => '6px 6px', |
| 735 | '--srfm-dropdown-multiselect-font-size' => '14px', |
| 736 | '--srfm-dropdown-multiselect-line-height' => '20px', |
| 737 | '--srfm-dropdown-padding-right' => '14px', |
| 738 | // Input Field Variables. |
| 739 | '--srfm-input-height' => '48px', |
| 740 | '--srfm-input-field-padding' => '10px 14px', |
| 741 | '--srfm-input-field-font-size' => '18px', |
| 742 | '--srfm-input-field-line-height' => '28px', |
| 743 | '--srfm-input-field-margin' => '8px 0', |
| 744 | // Checkbox and GDPR Variables. |
| 745 | '--srfm-check-ctn-width' => '20px', |
| 746 | '--srfm-check-ctn-height' => '20px', |
| 747 | '--srfm-check-svg-size' => '14px', |
| 748 | '--srfm-check-gap' => '10px', |
| 749 | '--srfm-checkbox-margin-top-frontend' => '4px', |
| 750 | '--srfm-checkbox-margin-top-editor' => '5px', |
| 751 | '--srfm-checkbox-description-margin-left' => '30px', |
| 752 | // Label Variables. |
| 753 | '--srfm-label-font-size' => '18px', |
| 754 | '--srfm-label-line-height' => '28px', |
| 755 | // Description Variables. |
| 756 | '--srfm-description-font-size' => '16px', |
| 757 | '--srfm-description-line-height' => '24px', |
| 758 | // Button Variables. |
| 759 | '--srfm-btn-padding' => '10px 14px', |
| 760 | '--srfm-btn-font-size' => '18px', |
| 761 | '--srfm-btn-line-height' => '28px', |
| 762 | // Multi Choice Variables. |
| 763 | '--srfm-multi-choice-horizontal-padding' => '24px', |
| 764 | '--srfm-multi-choice-vertical-padding' => '24px', |
| 765 | '--srfm-multi-choice-internal-option-gap' => '12px', |
| 766 | '--srfm-multi-choice-vertical-svg-size' => '48px', |
| 767 | '--srfm-multi-choice-horizontal-image-size' => '28px', |
| 768 | '--srfm-multi-choice-vertical-image-size' => '140px', |
| 769 | '--srfm-multi-choice-outer-padding' => '4px', |
| 770 | ], |
| 771 | ] |
| 772 | ); |
| 773 | // Return complete sizes array if field_spacing is false. Required in case of JS for Editor changes. |
| 774 | if ( ! $field_spacing ) { |
| 775 | return $sizes; |
| 776 | } |
| 777 | |
| 778 | $selected_size = $sizes['small']; |
| 779 | if ( 'small' !== $field_spacing && isset( $sizes[ $field_spacing ] ) ) { |
| 780 | $selected_size = array_merge( $selected_size, $sizes[ $field_spacing ] ); |
| 781 | } |
| 782 | |
| 783 | return $selected_size; |
| 784 | } |
| 785 | |
| 786 | } |
| 787 |