| 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\Database\Tables\Entries; |
| 12 |
use SRFM\Inc\Traits\Get_Instance; |
| 13 |
use WP_Error; |
| 14 |
use WP_Post; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; // Exit if accessed directly. |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Sureforms Helper Class. |
| 22 |
* |
| 23 |
* @since 0.0.1 |
| 24 |
*/ |
| 25 |
class Helper { |
| 26 |
use Get_Instance; |
| 27 |
|
| 28 |
/** |
| 29 |
* Allowed HTML tags for SVG. |
| 30 |
* |
| 31 |
* @var array<string, array<string, bool>> |
| 32 |
*/ |
| 33 |
public static $allowed_tags_svg = [ |
| 34 |
'span' => [ |
| 35 |
'class' => true, |
| 36 |
'aria-hidden' => true, |
| 37 |
], |
| 38 |
'svg' => [ |
| 39 |
'xmlns' => true, |
| 40 |
'width' => true, |
| 41 |
'height' => true, |
| 42 |
'viewBox' => true, |
| 43 |
'fill' => true, |
| 44 |
], |
| 45 |
'path' => [ |
| 46 |
'd' => true, |
| 47 |
'stroke' => true, |
| 48 |
'stroke-opacity' => true, |
| 49 |
'stroke-width' => true, |
| 50 |
'stroke-linecap' => true, |
| 51 |
'stroke-linejoin' => true, |
| 52 |
], |
| 53 |
]; |
| 54 |
|
| 55 |
/** |
| 56 |
* Sureforms SVGs. |
| 57 |
* |
| 58 |
* @var mixed srfm_svgs |
| 59 |
*/ |
| 60 |
private static $srfm_svgs = null; |
| 61 |
|
| 62 |
/** |
| 63 |
* Get common error message. |
| 64 |
* |
| 65 |
* @since 0.0.2 |
| 66 |
* @return array<string> |
| 67 |
*/ |
| 68 |
public static function get_common_err_msg() { |
| 69 |
return [ |
| 70 |
'required' => __( 'This field is required.', 'sureforms' ), |
| 71 |
'unique' => __( 'Value needs to be unique.', 'sureforms' ), |
| 72 |
]; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Convert a file URL to a file path. |
| 77 |
* |
| 78 |
* @param string $file_url The URL of the file. |
| 79 |
* |
| 80 |
* @since 1.3.0 |
| 81 |
* @return string The file path. |
| 82 |
*/ |
| 83 |
public static function convert_fileurl_to_filepath( $file_url ) { |
| 84 |
static $upload_dir = null; |
| 85 |
if ( ! $upload_dir ) { |
| 86 |
// Internally cache the upload directory. |
| 87 |
$upload_dir = wp_get_upload_dir(); |
| 88 |
} |
| 89 |
return wp_normalize_path( str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], $file_url ) ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Checks if current value is string or else returns default value |
| 94 |
* |
| 95 |
* @param mixed $data data which need to be checked if is string. |
| 96 |
* |
| 97 |
* @since 0.0.1 |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
public static function get_string_value( $data ) { |
| 101 |
if ( is_scalar( $data ) ) { |
| 102 |
return (string) $data; |
| 103 |
} |
| 104 |
if ( is_object( $data ) && method_exists( $data, '__toString' ) ) { |
| 105 |
return $data->__toString(); |
| 106 |
} |
| 107 |
if ( is_null( $data ) ) { |
| 108 |
return ''; |
| 109 |
} |
| 110 |
return ''; |
| 111 |
} |
| 112 |
/** |
| 113 |
* Checks if current value is number or else returns default value |
| 114 |
* |
| 115 |
* @param mixed $value data which need to be checked if is string. |
| 116 |
* @param int $base value can be set is $data is not a string, defaults to empty string. |
| 117 |
* |
| 118 |
* @since 0.0.1 |
| 119 |
* @return int |
| 120 |
*/ |
| 121 |
public static function get_integer_value( $value, $base = 10 ) { |
| 122 |
if ( is_numeric( $value ) ) { |
| 123 |
return (int) $value; |
| 124 |
} |
| 125 |
if ( is_string( $value ) ) { |
| 126 |
$trimmed_value = trim( $value ); |
| 127 |
return intval( $trimmed_value, $base ); |
| 128 |
} |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Validate a date string in Y-m-d format. |
| 134 |
* |
| 135 |
* @param string $date The date string to validate. |
| 136 |
* @since 2.6.0 |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
public static function validate_date( string $date ): bool { |
| 140 |
$d = \DateTime::createFromFormat( 'Y-m-d', $date ); |
| 141 |
return $d && $d->format( 'Y-m-d' ) === $date; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns a boolean representation of the given value. |
| 146 |
* |
| 147 |
* @param mixed $data Data which needs to be converted to boolean. |
| 148 |
* |
| 149 |
* @since 2.5.2 |
| 150 |
* @return bool |
| 151 |
*/ |
| 152 |
public static function get_boolean_value( $data ) { |
| 153 |
return (bool) $data; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Checks if current value is an array or else returns default value |
| 158 |
* |
| 159 |
* @param mixed $data Data which needs to be checked if it is an array. |
| 160 |
* |
| 161 |
* @since 0.0.3 |
| 162 |
* @return array |
| 163 |
*/ |
| 164 |
public static function get_array_value( $data ) { |
| 165 |
if ( is_array( $data ) ) { |
| 166 |
return $data; |
| 167 |
} |
| 168 |
if ( is_null( $data ) ) { |
| 169 |
return []; |
| 170 |
} |
| 171 |
return (array) $data; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Extracts the field type from the dynamic field key ( or field slug ). |
| 176 |
* |
| 177 |
* @param string $field_key Dynamic field key. |
| 178 |
* @since 0.0.6 |
| 179 |
* @return string Extracted field type. |
| 180 |
*/ |
| 181 |
public static function get_field_type_from_key( $field_key ) { |
| 182 |
|
| 183 |
if ( false === strpos( $field_key, '-lbl-' ) ) { |
| 184 |
return ''; |
| 185 |
} |
| 186 |
|
| 187 |
return trim( explode( '-', $field_key )[1] ); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Extracts the field label from the dynamic field key ( or field slug ). |
| 192 |
* |
| 193 |
* @param string $field_key Dynamic field key. |
| 194 |
* @since 1.1.1 |
| 195 |
* @return string Extracted field label. |
| 196 |
*/ |
| 197 |
public static function get_field_label_from_key( $field_key ) { |
| 198 |
if ( false === strpos( $field_key, '-lbl-' ) ) { |
| 199 |
return ''; |
| 200 |
} |
| 201 |
|
| 202 |
$label = explode( '-lbl-', $field_key )[1]; |
| 203 |
// Getting the encrypted label. we are removing the block slug here. |
| 204 |
$label = explode( '-', $label )[0]; |
| 205 |
|
| 206 |
return $label ? html_entity_decode( self::decrypt( $label ) ) : ''; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Extracts the block ID from the dynamic field key ( or field slug ). |
| 211 |
* |
| 212 |
* @param string $field_key Dynamic field key. |
| 213 |
* @since 1.6.1 |
| 214 |
* @return string Extracted block ID. |
| 215 |
*/ |
| 216 |
public static function get_block_id_from_key( $field_key ) { |
| 217 |
// Check if the key contains the block ID identifier. |
| 218 |
if ( strpos( $field_key, 'srfm-' ) === 0 && strpos( $field_key, '-lbl-' ) === false ) { |
| 219 |
return ''; // Return empty if the key format is invalid. |
| 220 |
} |
| 221 |
|
| 222 |
$parts = explode( '-lbl-', $field_key ); |
| 223 |
if ( isset( $parts[0] ) ) { |
| 224 |
$block_id = explode( '-', $parts[0] ); |
| 225 |
if ( is_array( $block_id ) && ! empty( $block_id ) ) { |
| 226 |
return end( $block_id ); |
| 227 |
} |
| 228 |
} |
| 229 |
return ''; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Returns the proper sanitize callback functions according to the field type. |
| 234 |
* |
| 235 |
* @param string $field_type HTML field type. |
| 236 |
* @since 0.0.6 |
| 237 |
* @return callable Returns sanitize callbacks according to the provided field type. |
| 238 |
*/ |
| 239 |
public static function get_field_type_sanitize_function( $field_type ) { |
| 240 |
$callbacks = apply_filters( |
| 241 |
'srfm_field_type_sanitize_functions', |
| 242 |
[ |
| 243 |
'url' => 'esc_url_raw', |
| 244 |
'input' => 'sanitize_text_field', |
| 245 |
'number' => [ self::class, 'sanitize_number' ], |
| 246 |
'email' => 'sanitize_email', |
| 247 |
'textarea' => [ self::class, 'sanitize_textarea' ], |
| 248 |
] |
| 249 |
); |
| 250 |
|
| 251 |
return $callbacks[ $field_type ] ?? 'sanitize_text_field'; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Sanitizes a numeric value. |
| 256 |
* |
| 257 |
* This function checks if the input value is numeric. If it is numeric, it sanitizes |
| 258 |
* the value to ensure it's a float or integer, allowing for fractions and thousand separators. |
| 259 |
* If the value is not numeric, it sanitizes it as a text field. |
| 260 |
* |
| 261 |
* @param mixed $value The value to be sanitized. |
| 262 |
* @since 0.0.6 |
| 263 |
* @return int|float|string The sanitized value. |
| 264 |
*/ |
| 265 |
public static function sanitize_number( $value ) { |
| 266 |
if ( ! is_numeric( $value ) ) { |
| 267 |
// phpcs:ignore /** @phpstan-ignore-next-line */ |
| 268 |
return sanitize_text_field( $value ); // If it is not numeric, then let user get some sanitized data to view. |
| 269 |
} |
| 270 |
|
| 271 |
// phpcs:ignore /** @phpstan-ignore-next-line */ |
| 272 |
return sanitize_text_field( filter_var( $value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND ) ); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* This function sanitizes the submitted form data according to the field type. |
| 277 |
* |
| 278 |
* @param array<mixed> $form_data $form_data User submitted form data. |
| 279 |
* @since 0.0.6 |
| 280 |
* @return array<mixed> $result Sanitized form data. |
| 281 |
*/ |
| 282 |
public static function sanitize_by_field_type( $form_data ) { |
| 283 |
$result = []; |
| 284 |
|
| 285 |
if ( empty( $form_data ) || ! is_array( $form_data ) ) { |
| 286 |
return $result; |
| 287 |
} |
| 288 |
|
| 289 |
foreach ( $form_data as $field_key => &$value ) { |
| 290 |
$field_type = self::get_field_type_from_key( $field_key ); |
| 291 |
$sanitize_function = self::get_field_type_sanitize_function( $field_type ); |
| 292 |
$sanitized_data = is_array( $value ) ? self::sanitize_by_field_type( $value ) : call_user_func( $sanitize_function, $value ); |
| 293 |
|
| 294 |
$result[ $field_key ] = $sanitized_data; |
| 295 |
} |
| 296 |
|
| 297 |
return $result; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* This function performs array_map for multi dimensional array |
| 302 |
* |
| 303 |
* @param string $function function name to be applied on each element on array. |
| 304 |
* @param array<mixed> $data_array array on which function needs to be performed. |
| 305 |
* @return array<mixed> |
| 306 |
* @since 0.0.1 |
| 307 |
*/ |
| 308 |
public static function sanitize_recursively( $function, $data_array ) { |
| 309 |
$response = []; |
| 310 |
if ( is_array( $data_array ) ) { |
| 311 |
if ( ! is_callable( $function ) ) { |
| 312 |
return $data_array; |
| 313 |
} |
| 314 |
foreach ( $data_array as $key => $data ) { |
| 315 |
$val = is_array( $data ) ? self::sanitize_recursively( $function, $data ) : $function( $data ); |
| 316 |
$response[ $key ] = $val; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
return $response; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Generates common markup liked label, etc |
| 325 |
* |
| 326 |
* @param int|string $form_id form id. |
| 327 |
* @param string $type Type of form markup. |
| 328 |
* @param string $label Label for the form markup. |
| 329 |
* @param string $slug Slug for the form markup. |
| 330 |
* @param string $block_id Block id for the form markup. |
| 331 |
* @param bool $required If field is required or not. |
| 332 |
* @param string $help Help for the form markup. |
| 333 |
* @param string $error_msg Error message for the form markup. |
| 334 |
* @param bool $is_unique Check if the field is unique. |
| 335 |
* @param string $duplicate_msg Duplicate message for field. |
| 336 |
* @param bool $override Override for error markup. |
| 337 |
* @return string |
| 338 |
* @since 0.0.1 |
| 339 |
*/ |
| 340 |
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 ) { |
| 341 |
$duplicate_msg = $duplicate_msg ? ' data-unique-msg="' . esc_attr( $duplicate_msg ) . '"' : ''; |
| 342 |
|
| 343 |
$markup = ''; |
| 344 |
$show_labels_as_placeholder = get_post_meta( self::get_integer_value( $form_id ), '_srfm_use_label_as_placeholder', true ); |
| 345 |
$show_labels_as_placeholder = $show_labels_as_placeholder ? self::get_string_value( $show_labels_as_placeholder ) : false; |
| 346 |
|
| 347 |
$required_sign = apply_filters( 'srfm_value_after_label_placeholder', ' *' ); |
| 348 |
|
| 349 |
if ( ! is_string( $required_sign ) ) { |
| 350 |
$required_sign = ' *'; |
| 351 |
} |
| 352 |
|
| 353 |
switch ( $type ) { |
| 354 |
case 'label': |
| 355 |
if ( $label ) { |
| 356 |
ob_start(); |
| 357 |
?> |
| 358 |
<label id="srfm-label-<?php echo esc_attr( $block_id ); ?>" for="srfm-<?php echo esc_attr( $slug ); ?>-<?php echo esc_attr( $block_id ); ?>" class="srfm-block-label"> |
| 359 |
<?php echo wp_kses_post( $label ); ?> |
| 360 |
<?php if ( $required ) { ?> |
| 361 |
<span class="srfm-required" aria-hidden="true"> *</span> |
| 362 |
<?php } ?> |
| 363 |
</label> |
| 364 |
<?php |
| 365 |
$markup = ob_get_clean(); |
| 366 |
} |
| 367 |
break; |
| 368 |
case 'help': |
| 369 |
if ( $help ) { |
| 370 |
ob_start(); |
| 371 |
?> |
| 372 |
<div class="srfm-description" id="srfm-description-<?php echo esc_attr( $block_id ); ?>"> |
| 373 |
<?php echo wp_kses_post( $help ); ?> |
| 374 |
</div> |
| 375 |
<?php |
| 376 |
$markup = ob_get_clean(); |
| 377 |
} |
| 378 |
break; |
| 379 |
case 'error': |
| 380 |
if ( $required || $override ) { |
| 381 |
ob_start(); |
| 382 |
?> |
| 383 |
<div class="srfm-error-message" data-srfm-id="srfm-error-<?php echo esc_attr( $block_id ); ?>" data-error-msg="<?php echo esc_attr( $error_msg ); ?>"<?php echo $duplicate_msg; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> |
| 384 |
<?php echo esc_html( $error_msg ); ?> |
| 385 |
</div> |
| 386 |
<?php |
| 387 |
$markup = ob_get_clean(); |
| 388 |
} |
| 389 |
break; |
| 390 |
case 'is_unique': |
| 391 |
if ( $is_unique ) { |
| 392 |
ob_start(); |
| 393 |
?> |
| 394 |
<div class="srfm-error"> |
| 395 |
<?php echo esc_html( $duplicate_msg ); ?> |
| 396 |
</div> |
| 397 |
<?php |
| 398 |
$markup = ob_get_clean(); |
| 399 |
} |
| 400 |
break; |
| 401 |
case 'placeholder': |
| 402 |
$markup = $label && '1' === $show_labels_as_placeholder ? wp_kses_post( $label ) . ( $required ? esc_attr( $required_sign ) : '' ) : ''; |
| 403 |
break; |
| 404 |
case 'label_text': |
| 405 |
// This has been added for generating label text for the form markup instead of adding it in the label tag. |
| 406 |
if ( $label ) { |
| 407 |
ob_start(); |
| 408 |
?> |
| 409 |
<?php echo wp_kses_post( $label ); ?> |
| 410 |
<?php if ( $required ) { ?> |
| 411 |
<span class="srfm-required" aria-hidden="true"> *</span> |
| 412 |
<?php } ?> |
| 413 |
<?php |
| 414 |
$markup = ob_get_clean(); |
| 415 |
} |
| 416 |
break; |
| 417 |
default: |
| 418 |
$markup = ''; |
| 419 |
} |
| 420 |
|
| 421 |
return is_string( $markup ) ? $markup : ''; |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Get an SVG Icon |
| 426 |
* |
| 427 |
* @since 0.0.1 |
| 428 |
* @param string $icon the icon name. |
| 429 |
* @param string $class if the baseline class should be added. |
| 430 |
* @param string $html Custom attributes inside svg wrapper. |
| 431 |
* @return string |
| 432 |
*/ |
| 433 |
public static function fetch_svg( $icon = '', $class = '', $html = '' ) { |
| 434 |
$class = $class ? ' ' . $class : ''; |
| 435 |
|
| 436 |
if ( ! self::$srfm_svgs ) { |
| 437 |
ob_start(); |
| 438 |
|
| 439 |
include_once SRFM_DIR . 'assets/svg/svgs.json'; |
| 440 |
self::$srfm_svgs = json_decode( self::get_string_value( ob_get_clean() ), true ); |
| 441 |
self::$srfm_svgs = apply_filters( 'srfm_svg_icons', self::$srfm_svgs ); |
| 442 |
} |
| 443 |
|
| 444 |
ob_start(); |
| 445 |
?> |
| 446 |
<span class="srfm-icon<?php echo esc_attr( $class ); ?>" <?php echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> |
| 447 |
<?php echo self::$srfm_svgs[ $icon ] ?? ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 448 |
</span> |
| 449 |
<?php |
| 450 |
$output = ob_get_clean(); |
| 451 |
return is_string( $output ) ? $output : ''; |
| 452 |
} |
| 453 |
|
| 454 |
/** |
| 455 |
* Encrypt data using base64. |
| 456 |
* |
| 457 |
* @param string $input The input string which needs to be encrypted. |
| 458 |
* @since 0.0.1 |
| 459 |
* @return string The encrypted string. |
| 460 |
*/ |
| 461 |
public static function encrypt( $input ) { |
| 462 |
// If the input is empty or not a string, then abandon ship. |
| 463 |
if ( empty( $input ) || ! is_string( $input ) ) { |
| 464 |
return ''; |
| 465 |
} |
| 466 |
|
| 467 |
// Strip HTML tags to prevent them from being included in IDs and field names. |
| 468 |
$input = wp_strip_all_tags( $input ); |
| 469 |
|
| 470 |
// Encrypt the input and return it. |
| 471 |
$base_64 = base64_encode( $input ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 472 |
return rtrim( $base_64, '=' ); |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* Decrypt data using base64. |
| 477 |
* |
| 478 |
* @param string $input The input string which needs to be decrypted. |
| 479 |
* @since 0.0.1 |
| 480 |
* @return string The decrypted string. |
| 481 |
*/ |
| 482 |
public static function decrypt( $input ) { |
| 483 |
// If the input is empty or not a string, then abandon ship. |
| 484 |
if ( empty( $input ) || ! is_string( $input ) ) { |
| 485 |
return ''; |
| 486 |
} |
| 487 |
|
| 488 |
// Decrypt the input and return it. |
| 489 |
$base_64 = $input . str_repeat( '=', strlen( $input ) % 4 ); |
| 490 |
return base64_decode( $base_64 ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* Update an option from the database. |
| 495 |
* |
| 496 |
* @param string $key The option key. |
| 497 |
* @param mixed $value The value to update. |
| 498 |
* @param bool $network_override Whether to allow the network_override admin setting to be overridden on subsites. |
| 499 |
* @since 0.0.1 |
| 500 |
* @return bool True if the option was updated, false otherwise. |
| 501 |
*/ |
| 502 |
public static function update_admin_settings_option( $key, $value, $network_override = false ) { |
| 503 |
// Update the site-wide option if we're in the network admin, and return the updated status. |
| 504 |
return $network_override && is_multisite() ? update_site_option( $key, $value ) : update_option( $key, $value ); |
| 505 |
} |
| 506 |
|
| 507 |
/** |
| 508 |
* Update an option from the database. |
| 509 |
* |
| 510 |
* @param int|string $post_id post id / form id. |
| 511 |
* @param string $key meta key name. |
| 512 |
* @param bool $single single or multiple. |
| 513 |
* @param mixed $default default value. |
| 514 |
* |
| 515 |
* @since 0.0.1 |
| 516 |
* @return string Meta value. |
| 517 |
*/ |
| 518 |
public static function get_meta_value( $post_id, $key, $single = true, $default = '' ) { |
| 519 |
$srfm_live_mode_data = self::get_instant_form_live_data(); |
| 520 |
|
| 521 |
if ( isset( $srfm_live_mode_data[ $key ] ) ) { |
| 522 |
// Give priority to live mode data if we have one set from the Instant Form. |
| 523 |
return self::get_string_value( $srfm_live_mode_data[ $key ] ); |
| 524 |
} |
| 525 |
|
| 526 |
return 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 ); |
| 527 |
} |
| 528 |
|
| 529 |
/** |
| 530 |
* Wrapper for the WordPress's get_post_meta function with the support for default values. |
| 531 |
* |
| 532 |
* @param int|string $post_id Post ID. |
| 533 |
* @param string $key The meta key to retrieve. |
| 534 |
* @param mixed $default Default value. |
| 535 |
* @param bool $single Optional. Whether to return a single value. |
| 536 |
* @since 0.0.8 |
| 537 |
* @return mixed Meta value. |
| 538 |
*/ |
| 539 |
public static function get_post_meta( $post_id, $key, $default = null, $single = true ) { |
| 540 |
$meta_value = get_post_meta( self::get_integer_value( $post_id ), $key, $single ); |
| 541 |
return $meta_value ? $meta_value : $default; |
| 542 |
} |
| 543 |
|
| 544 |
/** |
| 545 |
* Returns query params data for instant form live preview. |
| 546 |
* |
| 547 |
* @since 0.0.8 |
| 548 |
* @return array<mixed> Live preview data. |
| 549 |
*/ |
| 550 |
public static function get_instant_form_live_data() { |
| 551 |
$srfm_live_mode_data = isset( $_GET['live_mode'] ) && self::current_user_can() ? self::sanitize_recursively( 'sanitize_text_field', wp_unslash( $_GET ) ) : []; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not needed here. |
| 552 |
|
| 553 |
return $srfm_live_mode_data ? array_map( |
| 554 |
// Normalize falsy values. |
| 555 |
static function( $live_data ) { |
| 556 |
return 'false' === $live_data ? false : $live_data; |
| 557 |
}, |
| 558 |
$srfm_live_mode_data |
| 559 |
) : []; |
| 560 |
} |
| 561 |
|
| 562 |
/** |
| 563 |
* Default dynamic block value. |
| 564 |
* |
| 565 |
* @since 0.0.1 |
| 566 |
* @return array<string> Meta value. |
| 567 |
*/ |
| 568 |
public static function default_dynamic_block_option() { |
| 569 |
|
| 570 |
$common_err_msg = self::get_common_err_msg(); |
| 571 |
|
| 572 |
$default_values = [ |
| 573 |
'srfm_url_block_required_text' => $common_err_msg['required'], |
| 574 |
'srfm_input_block_required_text' => $common_err_msg['required'], |
| 575 |
'srfm_input_block_unique_text' => $common_err_msg['unique'], |
| 576 |
'srfm_address_block_required_text' => $common_err_msg['required'], |
| 577 |
'srfm_phone_block_required_text' => $common_err_msg['required'], |
| 578 |
'srfm_phone_block_unique_text' => $common_err_msg['unique'], |
| 579 |
'srfm_number_block_required_text' => $common_err_msg['required'], |
| 580 |
'srfm_textarea_block_required_text' => $common_err_msg['required'], |
| 581 |
'srfm_multi_choice_block_required_text' => $common_err_msg['required'], |
| 582 |
'srfm_checkbox_block_required_text' => $common_err_msg['required'], |
| 583 |
'srfm_gdpr_block_required_text' => $common_err_msg['required'], |
| 584 |
'srfm_email_block_required_text' => $common_err_msg['required'], |
| 585 |
'srfm_email_block_unique_text' => $common_err_msg['unique'], |
| 586 |
'srfm_dropdown_block_required_text' => $common_err_msg['required'], |
| 587 |
'srfm_rating_block_required_text' => $common_err_msg['required'], |
| 588 |
]; |
| 589 |
|
| 590 |
$default_values = array_merge( $default_values, Translatable::dynamic_validation_messages() ); |
| 591 |
|
| 592 |
return apply_filters( 'srfm_default_dynamic_block_option', $default_values, $common_err_msg ); |
| 593 |
} |
| 594 |
|
| 595 |
/** |
| 596 |
* Get default dynamic block value. |
| 597 |
* |
| 598 |
* @param string $key meta key name. |
| 599 |
* @since 0.0.1 |
| 600 |
* @return string Meta value. |
| 601 |
*/ |
| 602 |
public static function get_default_dynamic_block_option( $key ) { |
| 603 |
$default_dynamic_values = self::default_dynamic_block_option(); |
| 604 |
$option = get_option( 'srfm_default_dynamic_block_option', $default_dynamic_values ); |
| 605 |
|
| 606 |
if ( is_array( $option ) && array_key_exists( $key, $option ) ) { |
| 607 |
return $option[ $key ]; |
| 608 |
} |
| 609 |
return ''; |
| 610 |
} |
| 611 |
|
| 612 |
/** |
| 613 |
* Checks whether a given request has appropriate permissions. |
| 614 |
* |
| 615 |
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
| 616 |
* @since 0.0.1 |
| 617 |
*/ |
| 618 |
public static function get_items_permissions_check() { |
| 619 |
if ( self::current_user_can() ) { |
| 620 |
return true; |
| 621 |
} |
| 622 |
|
| 623 |
return new WP_Error( |
| 624 |
'rest_cannot_view', |
| 625 |
__( 'Sorry, you are not allowed to perform this action.', 'sureforms' ), |
| 626 |
[ 'status' => \rest_authorization_required_code() ] |
| 627 |
); |
| 628 |
} |
| 629 |
|
| 630 |
/** |
| 631 |
* Check if the current user has a given capability. |
| 632 |
* |
| 633 |
* @param string $capability The capability to check. |
| 634 |
* @param array<mixed> $args Optional. Additional arguments to pass to the capability check. |
| 635 |
* |
| 636 |
* @since 0.0.3 |
| 637 |
* @return bool Whether the current user has the given capability or role. |
| 638 |
*/ |
| 639 |
public static function current_user_can( $capability = '', $args = [] ) { |
| 640 |
if ( ! function_exists( 'current_user_can' ) ) { |
| 641 |
return false; |
| 642 |
} |
| 643 |
|
| 644 |
if ( ! is_string( $capability ) || empty( $capability ) ) { |
| 645 |
$capability = 'manage_options'; |
| 646 |
} |
| 647 |
|
| 648 |
return ! empty( $args ) && is_array( $args ) && count( $args ) > 0 |
| 649 |
? current_user_can( $capability, ...$args ) |
| 650 |
: current_user_can( $capability ); |
| 651 |
} |
| 652 |
|
| 653 |
/** |
| 654 |
* Get all the entries for the given form ids. The entries are older than the given days_old. |
| 655 |
* |
| 656 |
* @param int $days_old The number of days old the entries should be. |
| 657 |
* @param array<int> $sf_form_ids The form ids for which the entries need to be fetched. |
| 658 |
* @since 0.0.2 |
| 659 |
* @return array<mixed> the entries matching the criteria. |
| 660 |
*/ |
| 661 |
public static function get_entries_from_form_ids( $days_old = 0, $sf_form_ids = [] ) { |
| 662 |
|
| 663 |
$entries = []; |
| 664 |
$days_old_date = ( new \DateTime() )->modify( "-{$days_old} days" )->format( 'Y-m-d H:i:s' ); |
| 665 |
|
| 666 |
foreach ( $sf_form_ids as $form_id ) { |
| 667 |
// args according to the get_all() function in the Entries class. |
| 668 |
$args = [ |
| 669 |
'where' => [ |
| 670 |
[ |
| 671 |
[ |
| 672 |
'key' => 'form_id', |
| 673 |
'value' => $form_id, |
| 674 |
'compare' => '=', |
| 675 |
], |
| 676 |
[ |
| 677 |
'key' => 'created_at', |
| 678 |
'value' => $days_old_date, |
| 679 |
'compare' => '<=', |
| 680 |
], |
| 681 |
], |
| 682 |
], |
| 683 |
]; |
| 684 |
|
| 685 |
// store all the entries in a single array. |
| 686 |
$entries = array_merge( $entries, Entries::get_all( $args, false ) ); |
| 687 |
} |
| 688 |
return $entries; |
| 689 |
} |
| 690 |
|
| 691 |
/** |
| 692 |
* Decode block attributes. |
| 693 |
* The function reverses the effect of serialize_block_attributes() |
| 694 |
* |
| 695 |
* @link https://developer.wordpress.org/reference/functions/serialize_block_attributes/ |
| 696 |
* @param string $encoded_data the encoded block attribute. |
| 697 |
* @since 0.0.2 |
| 698 |
* @return string decoded block attribute |
| 699 |
*/ |
| 700 |
public static function decode_block_attribute( $encoded_data = '' ) { |
| 701 |
$decoded_data = preg_replace( '/\\\\u002d\\\\u002d/', '--', self::get_string_value( $encoded_data ) ); |
| 702 |
$decoded_data = preg_replace( '/\\\\u003c/', '<', self::get_string_value( $decoded_data ) ); |
| 703 |
$decoded_data = preg_replace( '/\\\\u003e/', '>', self::get_string_value( $decoded_data ) ); |
| 704 |
$decoded_data = preg_replace( '/\\\\u0026/', '&', self::get_string_value( $decoded_data ) ); |
| 705 |
$decoded_data = preg_replace( '/\\\\\\\\"/', '"', self::get_string_value( $decoded_data ) ); |
| 706 |
return self::get_string_value( $decoded_data ); |
| 707 |
} |
| 708 |
|
| 709 |
/** |
| 710 |
* Map slugs to submission data. |
| 711 |
* |
| 712 |
* @param array<mixed> $submission_data submission_data. |
| 713 |
* @since 0.0.3 |
| 714 |
* @return array<mixed> |
| 715 |
*/ |
| 716 |
public static function map_slug_to_submission_data( $submission_data = [] ) { |
| 717 |
$mapped_data = []; |
| 718 |
foreach ( $submission_data as $key => $value ) { |
| 719 |
if ( false === strpos( $key, '-lbl-' ) ) { |
| 720 |
continue; |
| 721 |
} |
| 722 |
$label = explode( '-lbl-', $key )[1]; |
| 723 |
$slug = implode( '-', array_slice( explode( '-', $label ), 1 ) ); |
| 724 |
|
| 725 |
/** |
| 726 |
* Filters whether a field should be skipped when mapping slugs to submission data. |
| 727 |
* |
| 728 |
* This filter allows plugins or custom code to determine if a field should be excluded |
| 729 |
* from the mapped submission data array (such as for internal fields or extraneous meta). |
| 730 |
* |
| 731 |
* @since 2.0.0 |
| 732 |
* |
| 733 |
* @param bool $skip_this_field Whether to skip this field from processing. Default false. |
| 734 |
* @param array $args { |
| 735 |
* Arguments used for this field. |
| 736 |
* |
| 737 |
* @type string $key The original key of the field in the submission data array. |
| 738 |
* @type string $slug The mapped slug parsed from the field key. |
| 739 |
* @type mixed $value The value assigned to this field. |
| 740 |
* } |
| 741 |
*/ |
| 742 |
$skip_this_field = apply_filters( |
| 743 |
'srfm_map_slug_to_submission_data_should_skip', |
| 744 |
false, |
| 745 |
[ |
| 746 |
'key' => $key, |
| 747 |
'slug' => $slug, |
| 748 |
'value' => $value, |
| 749 |
] |
| 750 |
); |
| 751 |
|
| 752 |
if ( $skip_this_field ) { |
| 753 |
continue; |
| 754 |
} |
| 755 |
|
| 756 |
// Check if value is array to handle external package field functionality. |
| 757 |
// like repeater fields that need special processing. |
| 758 |
if ( is_array( $value ) && ! empty( $value ) ) { |
| 759 |
// Apply filter to allow external packages to process array values. |
| 760 |
// Returns processed data with 'is_processed' flag if successfully handled. |
| 761 |
$filtered_submission_data = apply_filters( |
| 762 |
'srfm_map_slug_to_submission_data_array', |
| 763 |
[ |
| 764 |
'value' => $value, |
| 765 |
'key' => $key, |
| 766 |
'slug' => $slug, |
| 767 |
] |
| 768 |
); |
| 769 |
if ( isset( $filtered_submission_data['is_processed'] ) && true === $filtered_submission_data['is_processed'] ) { |
| 770 |
$mapped_data[ $slug ] = $filtered_submission_data['value']; |
| 771 |
continue; |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
// If the value is an array (e.g. multi-upload field), decode each URL value. |
| 776 |
if ( is_array( $value ) ) { |
| 777 |
$mapped_data[ $slug ] = array_map( |
| 778 |
static function ( $val ) { |
| 779 |
return is_string( $val ) ? rawurldecode( $val ) : $val; |
| 780 |
}, |
| 781 |
$value |
| 782 |
); |
| 783 |
continue; |
| 784 |
} |
| 785 |
|
| 786 |
$mapped_data[ $slug ] = is_string( $value ) ? html_entity_decode( esc_attr( $value ) ) : $value; |
| 787 |
} |
| 788 |
return $mapped_data; |
| 789 |
} |
| 790 |
|
| 791 |
/** |
| 792 |
* Get forms options. Shows all the available forms in the dropdown. |
| 793 |
* |
| 794 |
* @since 0.0.5 |
| 795 |
* @param string $key Determines the type of data to return. |
| 796 |
* @return array<mixed> |
| 797 |
*/ |
| 798 |
public static function get_sureforms( $key = '' ) { |
| 799 |
$forms = get_posts( |
| 800 |
apply_filters( |
| 801 |
'srfm_get_sureforms_query_args', |
| 802 |
[ |
| 803 |
'post_type' => SRFM_FORMS_POST_TYPE, |
| 804 |
'posts_per_page' => -1, |
| 805 |
'post_status' => 'publish', |
| 806 |
] |
| 807 |
) |
| 808 |
); |
| 809 |
|
| 810 |
$options = []; |
| 811 |
|
| 812 |
foreach ( $forms as $form ) { |
| 813 |
if ( $form instanceof WP_Post ) { |
| 814 |
if ( 'all' === $key ) { |
| 815 |
$options[ $form->ID ] = $form; |
| 816 |
} elseif ( ! empty( $key ) && is_string( $key ) && isset( $form->$key ) ) { |
| 817 |
$options[ $form->ID ] = $form->$key; |
| 818 |
} else { |
| 819 |
$options[ $form->ID ] = $form->post_title; |
| 820 |
} |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
return $options; |
| 825 |
} |
| 826 |
|
| 827 |
/** |
| 828 |
* Get all the forms. |
| 829 |
* |
| 830 |
* @since 0.0.5 |
| 831 |
* @return array<mixed> |
| 832 |
*/ |
| 833 |
public static function get_sureforms_title_with_ids() { |
| 834 |
$form_options = self::get_sureforms(); |
| 835 |
|
| 836 |
foreach ( $form_options as $key => $value ) { |
| 837 |
$form_options[ $key ] = $value . ' #' . $key; |
| 838 |
} |
| 839 |
|
| 840 |
return $form_options; |
| 841 |
} |
| 842 |
|
| 843 |
/** |
| 844 |
* Get the CSS variables based on different field spacing sizes. |
| 845 |
* |
| 846 |
* @param string|null $field_spacing The field spacing size or boolean false to return complete sizes array. |
| 847 |
* |
| 848 |
* @since 0.0.7 |
| 849 |
* @return array<string|mixed> |
| 850 |
*/ |
| 851 |
public static function get_css_vars( $field_spacing = null ) { |
| 852 |
/** |
| 853 |
* $sizes - Field Spacing Sizes Variables. |
| 854 |
* The array contains the CSS variables for different field spacing sizes. |
| 855 |
* Each key corresponds to the field spacing size, and the value is an array of CSS variables. |
| 856 |
* |
| 857 |
* For future variables depending on the field spacing size, add the variable to the array respectively. |
| 858 |
*/ |
| 859 |
$sizes = apply_filters( |
| 860 |
'srfm_css_vars_sizes', |
| 861 |
[ |
| 862 |
'small' => [ |
| 863 |
'--srfm-row-gap-between-blocks' => '16px', |
| 864 |
// Address block gap and spacing variables. |
| 865 |
'--srfm-address-label-font-size' => '14px', |
| 866 |
'--srfm-address-label-line-height' => '20px', |
| 867 |
'--srfm-address-description-font-size' => '12px', |
| 868 |
'--srfm-address-description-line-height' => '16px', |
| 869 |
'--srfm-col-gap-between-fields' => '12px', |
| 870 |
'--srfm-row-gap-between-fields' => '12px', |
| 871 |
'--srfm-gap-below-address-label' => '12px', |
| 872 |
// Dropdown Variables. |
| 873 |
'--srfm-dropdown-font-size' => '14px', |
| 874 |
'--srfm-dropdown-gap-between-input-menu' => '4px', |
| 875 |
'--srfm-dropdown-badge-padding' => '2px 6px', |
| 876 |
'--srfm-dropdown-multiselect-font-size' => '12px', |
| 877 |
'--srfm-dropdown-multiselect-line-height' => '16px', |
| 878 |
'--srfm-dropdown-padding-right' => '12px', |
| 879 |
// initial padding and from 20px - 12px for dropdown arrow width and 8px for gap before dropdown arrow. |
| 880 |
'--srfm-dropdown-padding-right-icon' => 'calc( var( --srfm-dropdown-padding-right ) + 20px )', |
| 881 |
'--srfm-dropdown-multiselect-padding' => '8px var( --srfm-dropdown-padding-right-icon ) 8px 8px', |
| 882 |
// Input Field Variables. |
| 883 |
'--srfm-input-height' => '40px', |
| 884 |
'--srfm-input-field-padding' => '10px 12px', |
| 885 |
'--srfm-input-field-font-size' => '14px', |
| 886 |
'--srfm-input-field-line-height' => '20px', |
| 887 |
'--srfm-input-field-margin-top' => '4px', |
| 888 |
'--srfm-input-field-margin-bottom' => '4px', |
| 889 |
// Checkbox and GDPR Variables. |
| 890 |
'--srfm-checkbox-label-font-size' => '14px', |
| 891 |
'--srfm-checkbox-label-line-height' => '20px', |
| 892 |
'--srfm-checkbox-description-font-size' => '12px', |
| 893 |
'--srfm-checkbox-description-line-height' => '16px', |
| 894 |
'--srfm-check-ctn-width' => '16px', |
| 895 |
'--srfm-check-ctn-height' => '16px', |
| 896 |
'--srfm-check-svg-size' => '10px', |
| 897 |
'--srfm-checkbox-margin-top-frontend' => '2px', |
| 898 |
'--srfm-checkbox-margin-top-editor' => '3px', |
| 899 |
'--srfm-check-gap' => '8px', |
| 900 |
'--srfm-checkbox-description-margin-left' => '24px', |
| 901 |
// Phone Number field variables. |
| 902 |
'--srfm-flag-section-padding' => '10px 0 10px 12px', |
| 903 |
'--srfm-gap-between-icon-text' => '8px', |
| 904 |
// Label Variables. |
| 905 |
'--srfm-label-font-size' => '14px', |
| 906 |
'--srfm-label-line-height' => '20px', |
| 907 |
// Description Variables. |
| 908 |
'--srfm-description-font-size' => '12px', |
| 909 |
'--srfm-description-line-height' => '16px', |
| 910 |
// Button Variables. |
| 911 |
'--srfm-btn-padding' => '8px 14px', |
| 912 |
'--srfm-btn-font-size' => '14px', |
| 913 |
'--srfm-btn-line-height' => '20px', |
| 914 |
// Multi Choice Variables. |
| 915 |
'--srfm-multi-choice-horizontal-padding' => '16px', |
| 916 |
'--srfm-multi-choice-vertical-padding' => '16px', |
| 917 |
'--srfm-multi-choice-internal-option-gap' => '8px', |
| 918 |
'--srfm-multi-choice-vertical-svg-size' => '32px', |
| 919 |
'--srfm-multi-choice-horizontal-image-size' => '20px', |
| 920 |
'--srfm-multi-choice-vertical-image-size' => '100px', |
| 921 |
'--srfm-multi-choice-outer-padding' => '0', |
| 922 |
], |
| 923 |
'medium' => [ |
| 924 |
'--srfm-row-gap-between-blocks' => '18px', |
| 925 |
// Address block gap and spacing variables. |
| 926 |
'--srfm-address-label-font-size' => '16px', |
| 927 |
'--srfm-address-label-line-height' => '24px', |
| 928 |
'--srfm-address-description-font-size' => '14px', |
| 929 |
'--srfm-address-description-line-height' => '20px', |
| 930 |
'--srfm-col-gap-between-fields' => '16px', |
| 931 |
'--srfm-row-gap-between-fields' => '16px', |
| 932 |
'--srfm-gap-below-address-label' => '14px', |
| 933 |
// Input Field Variables. |
| 934 |
'--srfm-input-height' => '44px', |
| 935 |
'--srfm-input-field-font-size' => '16px', |
| 936 |
'--srfm-input-field-line-height' => '24px', |
| 937 |
'--srfm-input-field-margin-top' => '6px', |
| 938 |
'--srfm-input-field-margin-bottom' => '6px', |
| 939 |
// Checkbox and GDPR Variables. |
| 940 |
'--srfm-checkbox-label-font-size' => '16px', |
| 941 |
'--srfm-checkbox-label-line-height' => '24px', |
| 942 |
'--srfm-checkbox-description-font-size' => '14px', |
| 943 |
'--srfm-checkbox-description-line-height' => '20px', |
| 944 |
'--srfm-checkbox-margin-top-frontend' => '4px', |
| 945 |
'--srfm-checkbox-margin-top-editor' => '6px', |
| 946 |
'--srfm-checkbox-description-margin-left' => '24px', |
| 947 |
// Label Variables. |
| 948 |
'--srfm-label-font-size' => '16px', |
| 949 |
'--srfm-label-line-height' => '24px', |
| 950 |
// Description Variables. |
| 951 |
'--srfm-description-font-size' => '14px', |
| 952 |
'--srfm-description-line-height' => '20px', |
| 953 |
// Button Variables. |
| 954 |
'--srfm-btn-padding' => '10px 14px', |
| 955 |
'--srfm-btn-font-size' => '16px', |
| 956 |
'--srfm-btn-line-height' => '24px', |
| 957 |
// Multi Choice Variables. |
| 958 |
'--srfm-multi-choice-horizontal-padding' => '20px', |
| 959 |
'--srfm-multi-choice-vertical-padding' => '20px', |
| 960 |
'--srfm-multi-choice-vertical-svg-size' => '40px', |
| 961 |
'--srfm-multi-choice-horizontal-image-size' => '24px', |
| 962 |
'--srfm-multi-choice-vertical-image-size' => '120px', |
| 963 |
'--srfm-multi-choice-outer-padding' => '2px', |
| 964 |
], |
| 965 |
'large' => [ |
| 966 |
'--srfm-row-gap-between-blocks' => '20px', |
| 967 |
// Address Block Gap and Spacing Variables. |
| 968 |
'--srfm-address-label-font-size' => '18px', |
| 969 |
'--srfm-address-label-line-height' => '28px', |
| 970 |
'--srfm-address-description-font-size' => '16px', |
| 971 |
'--srfm-address-description-line-height' => '24px', |
| 972 |
'--srfm-col-gap-between-fields' => '16px', |
| 973 |
'--srfm-row-gap-between-fields' => '20px', |
| 974 |
'--srfm-gap-below-address-label' => '16px', |
| 975 |
// Dropdown Variables. |
| 976 |
'--srfm-dropdown-font-size' => '16px', |
| 977 |
'--srfm-dropdown-gap-between-input-menu' => '6px', |
| 978 |
'--srfm-dropdown-badge-padding' => '6px 6px', |
| 979 |
'--srfm-dropdown-multiselect-font-size' => '14px', |
| 980 |
'--srfm-dropdown-multiselect-line-height' => '20px', |
| 981 |
'--srfm-dropdown-padding-right' => '14px', |
| 982 |
// Input Field Variables. |
| 983 |
'--srfm-input-height' => '48px', |
| 984 |
'--srfm-input-field-padding' => '10px 14px', |
| 985 |
'--srfm-input-field-font-size' => '18px', |
| 986 |
'--srfm-input-field-line-height' => '28px', |
| 987 |
'--srfm-input-field-margin-top' => '8px', |
| 988 |
'--srfm-input-field-margin-bottom' => '8px', |
| 989 |
// Checkbox and GDPR Variables. |
| 990 |
'--srfm-checkbox-label-font-size' => '18px', |
| 991 |
'--srfm-checkbox-label-line-height' => '28px', |
| 992 |
'--srfm-checkbox-description-font-size' => '16px', |
| 993 |
'--srfm-checkbox-description-line-height' => '24px', |
| 994 |
'--srfm-check-ctn-width' => '20px', |
| 995 |
'--srfm-check-ctn-height' => '20px', |
| 996 |
'--srfm-check-svg-size' => '14px', |
| 997 |
'--srfm-check-gap' => '10px', |
| 998 |
'--srfm-checkbox-margin-top-frontend' => '4px', |
| 999 |
'--srfm-checkbox-margin-top-editor' => '5px', |
| 1000 |
'--srfm-checkbox-description-margin-left' => '30px', |
| 1001 |
// Label Variables. |
| 1002 |
'--srfm-label-font-size' => '18px', |
| 1003 |
'--srfm-label-line-height' => '28px', |
| 1004 |
// Description Variables. |
| 1005 |
'--srfm-description-font-size' => '16px', |
| 1006 |
'--srfm-description-line-height' => '24px', |
| 1007 |
// Button Variables. |
| 1008 |
'--srfm-btn-padding' => '10px 14px', |
| 1009 |
'--srfm-btn-font-size' => '18px', |
| 1010 |
'--srfm-btn-line-height' => '28px', |
| 1011 |
// Multi Choice Variables. |
| 1012 |
'--srfm-multi-choice-horizontal-padding' => '24px', |
| 1013 |
'--srfm-multi-choice-vertical-padding' => '24px', |
| 1014 |
'--srfm-multi-choice-internal-option-gap' => '12px', |
| 1015 |
'--srfm-multi-choice-vertical-svg-size' => '48px', |
| 1016 |
'--srfm-multi-choice-horizontal-image-size' => '28px', |
| 1017 |
'--srfm-multi-choice-vertical-image-size' => '140px', |
| 1018 |
'--srfm-multi-choice-outer-padding' => '4px', |
| 1019 |
], |
| 1020 |
] |
| 1021 |
); |
| 1022 |
// Return complete sizes array if field_spacing is false. Required in case of JS for Editor changes. |
| 1023 |
if ( ! $field_spacing ) { |
| 1024 |
return $sizes; |
| 1025 |
} |
| 1026 |
|
| 1027 |
$selected_size = $sizes['small']; |
| 1028 |
if ( 'small' !== $field_spacing && isset( $sizes[ $field_spacing ] ) ) { |
| 1029 |
$selected_size = array_merge( $selected_size, $sizes[ $field_spacing ] ); |
| 1030 |
} |
| 1031 |
|
| 1032 |
return $selected_size; |
| 1033 |
} |
| 1034 |
|
| 1035 |
/** |
| 1036 |
* Array of SureForms blocks which get have user input. |
| 1037 |
* |
| 1038 |
* @since 0.0.10 |
| 1039 |
* @return array<string> |
| 1040 |
*/ |
| 1041 |
public static function get_sureforms_blocks() { |
| 1042 |
return apply_filters( |
| 1043 |
'srfm_blocks', |
| 1044 |
[ |
| 1045 |
'srfm/input', |
| 1046 |
'srfm/email', |
| 1047 |
'srfm/textarea', |
| 1048 |
'srfm/number', |
| 1049 |
'srfm/checkbox', |
| 1050 |
'srfm/gdpr', |
| 1051 |
'srfm/phone', |
| 1052 |
'srfm/address', |
| 1053 |
'srfm/dropdown', |
| 1054 |
'srfm/multi-choice', |
| 1055 |
'srfm/radio', |
| 1056 |
'srfm/submit', |
| 1057 |
'srfm/url', |
| 1058 |
'srfm/payment', |
| 1059 |
] |
| 1060 |
); |
| 1061 |
} |
| 1062 |
|
| 1063 |
/** |
| 1064 |
* Render a site key missing error message. |
| 1065 |
* |
| 1066 |
* @param string $provider_name Name of the captcha provider (e.g., HCaptcha, Google reCAPTCHA, Turnstile). |
| 1067 |
* @since 1.7.0 |
| 1068 |
* @since 1.7.1 moved to inc/helper.php from inc/generate-form-markup.php |
| 1069 |
* @return void |
| 1070 |
*/ |
| 1071 |
public static function render_missing_sitekey_error( $provider_name ) { |
| 1072 |
$icon = self::fetch_svg( 'info_circle', '', 'aria-hidden="true"' ); |
| 1073 |
?> |
| 1074 |
<p id="sitekey-error" class="srfm-common-error-message srfm-error-message"> |
| 1075 |
<?php echo wp_kses( $icon, self::$allowed_tags_svg ); ?> |
| 1076 |
<span class="srfm-error-content"> |
| 1077 |
<?php |
| 1078 |
echo esc_html( |
| 1079 |
sprintf( |
| 1080 |
/* translators: %s: Provider name like HCaptcha, Google reCAPTCHA, Turnstile */ |
| 1081 |
__( '%s sitekey is missing. Please contact your site administrator.', 'sureforms' ), |
| 1082 |
$provider_name |
| 1083 |
) |
| 1084 |
); |
| 1085 |
?> |
| 1086 |
</span> |
| 1087 |
</p> |
| 1088 |
<?php |
| 1089 |
} |
| 1090 |
|
| 1091 |
/** |
| 1092 |
* Parse and sanitize an email list string which may contain: |
| 1093 |
* |
| 1094 |
* @param string $input email addresses. |
| 1095 |
* @since 1.13.2 |
| 1096 |
* @return string Sanitized email header string. |
| 1097 |
*/ |
| 1098 |
public static function sanitize_email_header( $input ) { |
| 1099 |
if ( empty( $input ) ) { |
| 1100 |
return ''; |
| 1101 |
} |
| 1102 |
|
| 1103 |
$parts = explode( ',', $input ); |
| 1104 |
$output = []; |
| 1105 |
|
| 1106 |
foreach ( $parts as $part ) { |
| 1107 |
$part = trim( $part ); |
| 1108 |
|
| 1109 |
// Match "Name <email>". |
| 1110 |
if ( preg_match( '/^(.*)<(.+)>$/', $part, $matches ) ) { |
| 1111 |
$name = trim( $matches[1], "\" \t\n\r\0\x0B" ); // trim quotes. |
| 1112 |
$email = sanitize_email( trim( $matches[2] ) ); |
| 1113 |
|
| 1114 |
if ( is_email( $email ) ) { |
| 1115 |
$safe_name = sanitize_text_field( $name ); |
| 1116 |
$output[] = $safe_name . ' <' . $email . '>'; |
| 1117 |
} |
| 1118 |
} else { |
| 1119 |
// Plain email case. |
| 1120 |
$email = sanitize_email( $part ); |
| 1121 |
if ( is_email( $email ) ) { |
| 1122 |
$output[] = $email; |
| 1123 |
} |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
return ! empty( $output ) ? implode( ', ', $output ) : ''; |
| 1128 |
} |
| 1129 |
|
| 1130 |
/** |
| 1131 |
* Process blocks and inner blocks. |
| 1132 |
* |
| 1133 |
* @param array<mixed> $blocks The block data. |
| 1134 |
* @param array<string> $slugs The array of existing slugs. |
| 1135 |
* @param bool $updated The array of existing slugs. |
| 1136 |
* @param string $prefix The array of existing slugs. |
| 1137 |
* @param bool $skip_checking_existing_slug Skips the checking of existing slug if passed true. More information documented inside this function. |
| 1138 |
* @since 0.0.10 |
| 1139 |
* @return array |
| 1140 |
*/ |
| 1141 |
public static function process_blocks( $blocks, &$slugs, &$updated, $prefix = '', $skip_checking_existing_slug = false ) { |
| 1142 |
|
| 1143 |
if ( ! is_array( $blocks ) ) { |
| 1144 |
return [ $blocks, $slugs, $updated ]; |
| 1145 |
} |
| 1146 |
|
| 1147 |
foreach ( $blocks as $index => $block ) { |
| 1148 |
|
| 1149 |
if ( ! is_array( $block ) ) { |
| 1150 |
continue; |
| 1151 |
} |
| 1152 |
// Checking only for SureForms blocks which can have user input. |
| 1153 |
if ( empty( $block['blockName'] ) || ! in_array( $block['blockName'], self::get_sureforms_blocks(), true ) ) { |
| 1154 |
continue; |
| 1155 |
} |
| 1156 |
|
| 1157 |
/** |
| 1158 |
* Lets continue if slug already exists. |
| 1159 |
* This will ensure that we don't update already existing slugs. |
| 1160 |
*/ |
| 1161 |
if ( isset( $block['attrs'] ) && ! empty( $block['attrs']['slug'] ) && ! in_array( $block['attrs']['slug'], $slugs, true ) ) { |
| 1162 |
|
| 1163 |
// Made it associative array, so that we can directly check it using block_id rather than mapping or using "in_array" for the checks. |
| 1164 |
$slugs[ $block['attrs']['block_id'] ] = self::get_string_value( $block['attrs']['slug'] ); |
| 1165 |
|
| 1166 |
if ( is_array( $block['innerBlocks'] ) && ! empty( $block['innerBlocks'] ) ) { |
| 1167 |
[ $blocks[ $index ]['innerBlocks'], $slugs, $updated ] = self::process_blocks( $block['innerBlocks'], $slugs, $updated, '' ); |
| 1168 |
} |
| 1169 |
continue; |
| 1170 |
} |
| 1171 |
|
| 1172 |
if ( $skip_checking_existing_slug && empty( $block['innerBlocks'] ) && isset( $slugs[ $block['attrs']['block_id'] ] ) ) { |
| 1173 |
/** |
| 1174 |
* Skip re-processing of the already process or existing slugs if above parameter "$skip_checking_existing_slug" is passed as true. |
| 1175 |
* This is helpful in the scenarios where we need to compare and verify between already saved blocks and new unsaved blocks parsed |
| 1176 |
* from the contents. |
| 1177 |
* |
| 1178 |
* However, it is also necessary to make sure if that current block is not a parent / wrapper block |
| 1179 |
* by checking "$block['innerBlocks']" empty. |
| 1180 |
* |
| 1181 |
* And finally, checking if the block-id "$block['attrs']['block_id']" is already set in the list of "$slugs", |
| 1182 |
* making sure that we are only processing the new blocks. |
| 1183 |
*/ |
| 1184 |
continue; |
| 1185 |
} |
| 1186 |
|
| 1187 |
if ( is_array( $blocks[ $index ]['attrs'] ) ) { |
| 1188 |
|
| 1189 |
$blocks[ $index ]['attrs']['slug'] = self::generate_unique_block_slug( $block, $slugs, $prefix ); |
| 1190 |
$slugs[ $block['attrs']['block_id'] ] = $blocks[ $index ]['attrs']['slug']; // Made it associative array, so that we can directly check it using block_id rather than mapping or using "in_array" for the checks. |
| 1191 |
$updated = true; |
| 1192 |
if ( is_array( $block['innerBlocks'] ) && ! empty( $block['innerBlocks'] ) ) { |
| 1193 |
|
| 1194 |
[ $blocks[ $index ]['innerBlocks'], $slugs, $updated ] = self::process_blocks( $block['innerBlocks'], $slugs, $updated, $blocks[ $index ]['attrs']['slug'] ); |
| 1195 |
|
| 1196 |
} |
| 1197 |
} |
| 1198 |
} |
| 1199 |
return [ $blocks, $slugs, $updated ]; |
| 1200 |
} |
| 1201 |
|
| 1202 |
/** |
| 1203 |
* Generates slug based on the provided block and existing slugs. |
| 1204 |
* |
| 1205 |
* @param array<mixed> $block The block data. |
| 1206 |
* @param array<string> $slugs The array of existing slugs. |
| 1207 |
* @param string $prefix The array of existing slugs. |
| 1208 |
* @since 0.0.10 |
| 1209 |
* @return string The generated unique block slug. |
| 1210 |
*/ |
| 1211 |
public static function generate_unique_block_slug( $block, $slugs, $prefix ) { |
| 1212 |
$slug = is_string( $block['blockName'] ) ? $block['blockName'] : ''; |
| 1213 |
|
| 1214 |
if ( ! empty( $block['attrs']['label'] ) && is_string( $block['attrs']['label'] ) ) { |
| 1215 |
$slug = sanitize_title( $block['attrs']['label'] ); |
| 1216 |
} |
| 1217 |
|
| 1218 |
if ( ! empty( $prefix ) ) { |
| 1219 |
$slug = $prefix . '-' . $slug; |
| 1220 |
} |
| 1221 |
|
| 1222 |
return self::generate_slug( $slug, $slugs ); |
| 1223 |
} |
| 1224 |
|
| 1225 |
/** |
| 1226 |
* This function ensures that the slug is unique. |
| 1227 |
* If the slug is already taken, it appends a number to the slug to make it unique. |
| 1228 |
* |
| 1229 |
* @param string $slug test to be converted to slug. |
| 1230 |
* @param array<string> $slugs An array of existing slugs. |
| 1231 |
* @since 0.0.10 |
| 1232 |
* @return string The unique slug. |
| 1233 |
*/ |
| 1234 |
public static function generate_slug( $slug, $slugs ) { |
| 1235 |
$slug = sanitize_title( $slug ); |
| 1236 |
|
| 1237 |
if ( ! in_array( $slug, $slugs, true ) ) { |
| 1238 |
return $slug; |
| 1239 |
} |
| 1240 |
|
| 1241 |
$index = 1; |
| 1242 |
|
| 1243 |
while ( in_array( $slug . '-' . $index, $slugs, true ) ) { |
| 1244 |
$index++; |
| 1245 |
} |
| 1246 |
|
| 1247 |
return $slug . '-' . $index; |
| 1248 |
} |
| 1249 |
|
| 1250 |
/** |
| 1251 |
* Encode data to JSON. This function will encode the data with JSON_UNESCAPED_SLASHES and JSON_UNESCAPED_UNICODE. |
| 1252 |
* |
| 1253 |
* @since 0.0.11 |
| 1254 |
* @param array<mixed> $data The data to encode. |
| 1255 |
* @return string|false The JSON representation of the value on success or false on failure. |
| 1256 |
*/ |
| 1257 |
public static function encode_json( $data ) { |
| 1258 |
return wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); |
| 1259 |
} |
| 1260 |
|
| 1261 |
/** |
| 1262 |
* Returns true if SureTriggers plugin is ready for the custom app. |
| 1263 |
* |
| 1264 |
* @since 1.0.3 |
| 1265 |
* @return bool Returns true if SureTriggers plugin is ready for the custom app. |
| 1266 |
*/ |
| 1267 |
public static function is_suretriggers_ready() { |
| 1268 |
if ( ! defined( 'SURE_TRIGGERS_FILE' ) ) { |
| 1269 |
// Probably plugin is de-activated or not installed at all. |
| 1270 |
return false; |
| 1271 |
} |
| 1272 |
|
| 1273 |
$suretriggers_data = get_option( 'suretrigger_options', [] ); |
| 1274 |
if ( ! is_array( $suretriggers_data ) || empty( $suretriggers_data['secret_key'] ) || ! is_string( $suretriggers_data['secret_key'] ) ) { |
| 1275 |
// SureTriggers is not authenticated yet. |
| 1276 |
return false; |
| 1277 |
} |
| 1278 |
|
| 1279 |
return true; |
| 1280 |
} |
| 1281 |
|
| 1282 |
/** |
| 1283 |
* Registers script translations for a specific handle. |
| 1284 |
* |
| 1285 |
* This function sets the script translations for a given script handle, allowing |
| 1286 |
* localization of JavaScript strings using the specified text domain and path. |
| 1287 |
* |
| 1288 |
* @param string $handle The script handle to apply translations to. |
| 1289 |
* @param string $domain Optional. The text domain for translations. Default is 'sureforms'. |
| 1290 |
* @param string $path Optional. The path to the translation files. Default is the 'languages' folder in the SureForms directory. |
| 1291 |
* |
| 1292 |
* @since 1.0.5 |
| 1293 |
* @return void |
| 1294 |
*/ |
| 1295 |
public static function register_script_translations( $handle, $domain = 'sureforms', $path = SRFM_DIR . 'languages' ) { |
| 1296 |
wp_set_script_translations( $handle, $domain, $path ); |
| 1297 |
} |
| 1298 |
|
| 1299 |
/** |
| 1300 |
* Validates whether the specified conditions or a single key-value pair exist in the request context. |
| 1301 |
* |
| 1302 |
* - If `$conditions` is provided as an array, it will validate all key-value pairs in `$conditions` |
| 1303 |
* against the `$_REQUEST` superglobal. |
| 1304 |
* - If `$conditions` is empty, it validates a single key-value pair from `$key` and `$value`. |
| 1305 |
* |
| 1306 |
* @param string $value The expected value to match in the request if `$conditions` is not used. |
| 1307 |
* @param string $key The key to check for in the request if `$conditions` is not used. |
| 1308 |
* @param array<string, string> $conditions An optional associative array of key-value pairs to validate. |
| 1309 |
* @since 1.1.1 |
| 1310 |
* @return bool Returns true if all conditions are met or the single key-value pair is valid, otherwise false. |
| 1311 |
*/ |
| 1312 |
public static function validate_request_context( $value, $key = 'post_type', $conditions = [] ) { |
| 1313 |
// If conditions are provided, validate all key-value pairs in the conditions array. |
| 1314 |
if ( ! empty( $conditions ) ) { |
| 1315 |
foreach ( $conditions as $condition_key => $condition_value ) { |
| 1316 |
if ( ! isset( $_REQUEST[ $condition_key ] ) || $_REQUEST[ $condition_key ] !== $condition_value ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a controlled comparison of request values. |
| 1317 |
// Return false if any condition is not satisfied. |
| 1318 |
return false; |
| 1319 |
} |
| 1320 |
} |
| 1321 |
// Return true if all conditions are satisfied. |
| 1322 |
return true; |
| 1323 |
} |
| 1324 |
|
| 1325 |
// Validate $value and $key when no conditions are provided. |
| 1326 |
if ( empty( $key ) || empty( $value ) ) { |
| 1327 |
return false; |
| 1328 |
} |
| 1329 |
|
| 1330 |
// Validate a single key-value pair when no conditions are provided. |
| 1331 |
return isset( $_REQUEST[ $key ] ) && $_REQUEST[ $key ] === $value; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not needed here. Input is validated via strict comparison. |
| 1332 |
} |
| 1333 |
|
| 1334 |
/** |
| 1335 |
* Retrieve the list of excluded fields for form data processing. |
| 1336 |
* |
| 1337 |
* This method returns an array of field keys that should be excluded when |
| 1338 |
* processing form data. |
| 1339 |
* |
| 1340 |
* @since 1.1.1 |
| 1341 |
* @return array<string> Returns the string array of excluded fields. |
| 1342 |
*/ |
| 1343 |
public static function get_excluded_fields() { |
| 1344 |
$excluded_fields = [ 'srfm-honeypot-field', 'g-recaptcha-response', 'srfm-sender-email-field', 'form-id' ]; |
| 1345 |
|
| 1346 |
return apply_filters( 'srfm_excluded_fields', $excluded_fields ); |
| 1347 |
} |
| 1348 |
|
| 1349 |
/** |
| 1350 |
* Check whether the current page is a SureForms admin page. |
| 1351 |
* |
| 1352 |
* @since 1.2.2 |
| 1353 |
* @return bool Returns true if the current page is a SureForms admin page, otherwise false. |
| 1354 |
*/ |
| 1355 |
public static function is_sureforms_admin_page() { |
| 1356 |
$current_screen = get_current_screen(); |
| 1357 |
$is_screen_sureforms_menu = self::validate_request_context( 'sureforms_menu', 'page' ); |
| 1358 |
$is_screen_add_new_form = self::validate_request_context( 'add-new-form', 'page' ); |
| 1359 |
$is_screen_sureforms_form_settings = self::validate_request_context( 'sureforms_form_settings', 'page' ); |
| 1360 |
$is_screen_sureforms_entries = self::validate_request_context( SRFM_ENTRIES, 'page' ); |
| 1361 |
$is_post_type_sureforms_form = $current_screen && SRFM_FORMS_POST_TYPE === $current_screen->post_type; |
| 1362 |
|
| 1363 |
return $is_screen_sureforms_menu || $is_screen_add_new_form || $is_screen_sureforms_form_settings || $is_screen_sureforms_entries || $is_post_type_sureforms_form; |
| 1364 |
} |
| 1365 |
|
| 1366 |
/** |
| 1367 |
* Filters and concatenates valid class names from an array. |
| 1368 |
* |
| 1369 |
* @param array<string> $class_names The array containing potential class names. |
| 1370 |
* @since 1.4.0 |
| 1371 |
* @return string The concatenated string of valid class names separated by spaces. |
| 1372 |
*/ |
| 1373 |
public static function join_strings( $class_names ) { |
| 1374 |
// Filter the array to include only valid class names. |
| 1375 |
$valid_class_names = array_filter( |
| 1376 |
$class_names, |
| 1377 |
static function ( $value ) { |
| 1378 |
return is_string( $value ) && '' !== $value && false !== $value; |
| 1379 |
} |
| 1380 |
); |
| 1381 |
|
| 1382 |
// Concatenate the valid class names with spaces and return. |
| 1383 |
return implode( ' ', $valid_class_names ); |
| 1384 |
} |
| 1385 |
/** |
| 1386 |
* Get SureForms Website URL. |
| 1387 |
* |
| 1388 |
* @param string $trail The URL trail to append to SureForms website URL. The parameter should not include a leading slash as the base URL already ends with a trailing slash. |
| 1389 |
* @param array<string, string> $utm_args Optional. An associative array of UTM parameters to append to the URL. Default empty array. Example: [ 'utm_medium' => 'dashboard']. |
| 1390 |
* @since 0.0.7 |
| 1391 |
* @return string |
| 1392 |
*/ |
| 1393 |
public static function get_sureforms_website_url( $trail, $utm_args = [] ) { |
| 1394 |
$url = SRFM_WEBSITE; |
| 1395 |
if ( ! empty( $trail ) && is_string( $trail ) ) { |
| 1396 |
$url = SRFM_WEBSITE . $trail; |
| 1397 |
} |
| 1398 |
|
| 1399 |
if ( ! is_array( $utm_args ) ) { |
| 1400 |
$utm_args = []; |
| 1401 |
} |
| 1402 |
|
| 1403 |
if ( class_exists( 'BSF_UTM_Analytics' ) ) { |
| 1404 |
$url = \BSF_UTM_Analytics::get_utm_ready_link( $url, 'sureforms', $utm_args ); |
| 1405 |
} |
| 1406 |
|
| 1407 |
return esc_url( $url ); |
| 1408 |
} |
| 1409 |
|
| 1410 |
/** |
| 1411 |
* Validates if the given string is a valid CSS class name. |
| 1412 |
* |
| 1413 |
* A valid CSS class name: |
| 1414 |
* - Does not start with a digit, hyphen, or underscore. |
| 1415 |
* - Can contain alphanumeric characters, underscores, hyphens, and Unicode letters. |
| 1416 |
* |
| 1417 |
* @param string $class_name The class name to validate. |
| 1418 |
* |
| 1419 |
* @since 1.3.1 |
| 1420 |
* @return bool True if the class name is valid, otherwise false. |
| 1421 |
*/ |
| 1422 |
public static function is_valid_css_class_name( $class_name ) { |
| 1423 |
// Regular expression to validate a Unicode-aware CSS class name. |
| 1424 |
$class_name_regex = '/^[^\d\-_][\w\p{L}\p{N}\-_]*$/u'; |
| 1425 |
|
| 1426 |
// Check if the className matches the pattern. |
| 1427 |
return preg_match( $class_name_regex, $class_name ) === 1; |
| 1428 |
} |
| 1429 |
|
| 1430 |
/** |
| 1431 |
* Get the gradient css for given gradient parameters. |
| 1432 |
* |
| 1433 |
* @param string $type The type of gradient. Default 'linear'. |
| 1434 |
* @param string $color1 The first color of the gradient. Default '#FFC9B2'. |
| 1435 |
* @param string $color2 The second color of the gradient. Default '#C7CBFF'. |
| 1436 |
* @param int $loc1 The location of the first color. Default 0. |
| 1437 |
* @param int $loc2 The location of the second color. Default 100. |
| 1438 |
* @param int $angle The angle of the gradient. Default 90. |
| 1439 |
* |
| 1440 |
* @since 1.4.4 |
| 1441 |
* @return string The gradient css. |
| 1442 |
*/ |
| 1443 |
public static function get_gradient_css( $type = 'linear', $color1 = '#FFC9B2', $color2 = '#C7CBFF', $loc1 = 0, $loc2 = 100, $angle = 90 ) { |
| 1444 |
if ( 'linear' === $type ) { |
| 1445 |
return "linear-gradient({$angle}deg, {$color1} {$loc1}%, {$color2} {$loc2}%)"; |
| 1446 |
} |
| 1447 |
return "radial-gradient({$color1} {$loc1}%, {$color2} {$loc2}%)"; |
| 1448 |
} |
| 1449 |
|
| 1450 |
/** |
| 1451 |
* Return the classes based on background and overlay type to add to the form container. |
| 1452 |
* |
| 1453 |
* @param string $background_type The background type. |
| 1454 |
* @param string $overlay_type The overlay type. |
| 1455 |
* @param string $bg_image The background image url. |
| 1456 |
* |
| 1457 |
* @since 1.4.4 |
| 1458 |
* @return string The classes to add to the form container. |
| 1459 |
*/ |
| 1460 |
public static function get_background_classes( $background_type, $overlay_type, $bg_image = '' ) { |
| 1461 |
if ( empty( $background_type ) ) { |
| 1462 |
$background_type = 'color'; |
| 1463 |
} |
| 1464 |
|
| 1465 |
$background_type_class = ''; |
| 1466 |
$overlay_class = 'image' === $background_type && ! empty( $bg_image ) && $overlay_type ? "srfm-overlay-{$overlay_type}" : ''; |
| 1467 |
|
| 1468 |
// Set the class based on the background type. |
| 1469 |
switch ( $background_type ) { |
| 1470 |
case 'image': |
| 1471 |
$background_type_class = 'srfm-bg-image'; |
| 1472 |
break; |
| 1473 |
case 'gradient': |
| 1474 |
$background_type_class = 'srfm-bg-gradient'; |
| 1475 |
break; |
| 1476 |
default: |
| 1477 |
$background_type_class = 'srfm-bg-color'; |
| 1478 |
break; |
| 1479 |
} |
| 1480 |
|
| 1481 |
return self::join_strings( [ $background_type_class, $overlay_class ] ); |
| 1482 |
} |
| 1483 |
|
| 1484 |
/** |
| 1485 |
* Custom escape function for the textarea with rich text support. |
| 1486 |
* |
| 1487 |
* @param string $content The content submitted by the user in the textarea block. |
| 1488 |
* @since 1.7.1 |
| 1489 |
* |
| 1490 |
* @return string Escaped content. |
| 1491 |
*/ |
| 1492 |
public static function esc_textarea( $content ) { |
| 1493 |
$content = wpautop( self::sanitize_textarea( $content ) ); |
| 1494 |
|
| 1495 |
return trim( str_replace( [ "\r\n", "\r", "\n" ], '', $content ) ); |
| 1496 |
} |
| 1497 |
|
| 1498 |
/** |
| 1499 |
* Custom sanitization function for the textarea with rich text support. |
| 1500 |
* |
| 1501 |
* @param string $content The content submitted by the user in the textarea block. |
| 1502 |
* @since 1.7.1 |
| 1503 |
* |
| 1504 |
* @return string Sanitized content. |
| 1505 |
*/ |
| 1506 |
public static function sanitize_textarea( $content ) { |
| 1507 |
$count = 1; |
| 1508 |
$content = convert_invalid_entities( $content ); |
| 1509 |
|
| 1510 |
// Remove the 'script' and 'style' tags recursively from the content. |
| 1511 |
while ( $count ) { |
| 1512 |
$content = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', self::get_string_value( $content ), - 1, $count ); |
| 1513 |
} |
| 1514 |
|
| 1515 |
// Disable the safe style attribute parsing for the textarea block. |
| 1516 |
add_filter( 'safe_style_css', [ self::class, 'disable_style_attr_parsing' ], 10, 1 ); |
| 1517 |
$content = wp_kses_post( self::get_string_value( $content ) ); |
| 1518 |
|
| 1519 |
// Remove the filter after sanitization to avoid affecting other blocks. |
| 1520 |
remove_filter( 'safe_style_css', [ self::class, 'disable_style_attr_parsing' ], 10 ); |
| 1521 |
|
| 1522 |
// Ensure all tags are balanced. |
| 1523 |
return force_balance_tags( $content ); |
| 1524 |
} |
| 1525 |
|
| 1526 |
/** |
| 1527 |
* Disable parsing of style attributes for the textarea block. |
| 1528 |
* |
| 1529 |
* @param array<string> $allowed_styles The allowed styles. |
| 1530 |
* @since 1.7.1 |
| 1531 |
* |
| 1532 |
* @return array An empty array to disable style attribute parsing. |
| 1533 |
*/ |
| 1534 |
public static function disable_style_attr_parsing( $allowed_styles ) { |
| 1535 |
unset( $allowed_styles ); |
| 1536 |
// Disable parsing of style attributes. |
| 1537 |
return []; |
| 1538 |
} |
| 1539 |
/** |
| 1540 |
* Strips JavaScript attributes from HTML content. |
| 1541 |
* |
| 1542 |
* @param string $html The HTML content to process. |
| 1543 |
* @param bool $remove_link_target Optional. When true, removes target and strips noopener/noreferrer from rel on links. Default false. |
| 1544 |
* @since 1.7.1 |
| 1545 |
* @since 2.5.2 Added $remove_link_target parameter. |
| 1546 |
* @return string The cleaned HTML content without JavaScript attributes. |
| 1547 |
*/ |
| 1548 |
public static function strip_js_attributes( $html, $remove_link_target = false ) { |
| 1549 |
$dom = new \DOMDocument(); |
| 1550 |
|
| 1551 |
// Suppress warnings due to malformed HTML. |
| 1552 |
libxml_use_internal_errors( true ); |
| 1553 |
$loaded = $dom->loadHTML( '<?xml encoding="utf-8" ?>' . $html ); |
| 1554 |
libxml_clear_errors(); |
| 1555 |
|
| 1556 |
if ( ! $loaded ) { |
| 1557 |
return $html; // Return original HTML if loading fails. |
| 1558 |
} |
| 1559 |
|
| 1560 |
$xpath = new \DOMXPath( $dom ); |
| 1561 |
|
| 1562 |
// 1. Remove all <script> tags. |
| 1563 |
$script_nodes = $xpath->query( '//script' ); |
| 1564 |
if ( $script_nodes instanceof \DOMNodeList ) { |
| 1565 |
foreach ( $script_nodes as $script ) { |
| 1566 |
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- This is a DOM element. |
| 1567 |
$parent_node = $script->parentNode; |
| 1568 |
if ( $parent_node instanceof \DOMNode ) { |
| 1569 |
$parent_node->removeChild( $script ); |
| 1570 |
} |
| 1571 |
} |
| 1572 |
} |
| 1573 |
|
| 1574 |
// 2. Remove all attributes that start with "on" (like onclick, onmouseover, etc.). |
| 1575 |
$elements_with_on_attrs = $xpath->query( '//*[@*[starts-with(name(), "on")]]' ); |
| 1576 |
if ( $elements_with_on_attrs instanceof \DOMNodeList ) { |
| 1577 |
foreach ( $elements_with_on_attrs as $element ) { |
| 1578 |
if ( $element instanceof \DOMElement && $element->hasAttributes() ) { |
| 1579 |
foreach ( iterator_to_array( $element->attributes ) as $attr ) { |
| 1580 |
if ( $attr instanceof \DOMAttr && stripos( $attr->name, 'on' ) === 0 ) { |
| 1581 |
$element->removeAttribute( $attr->name ); |
| 1582 |
} |
| 1583 |
} |
| 1584 |
} |
| 1585 |
} |
| 1586 |
} |
| 1587 |
|
| 1588 |
// 3. Optionally remove target and target-related rel values (noopener, noreferrer) from links. |
| 1589 |
if ( $remove_link_target ) { |
| 1590 |
$links = $xpath->query( '//a[@target]' ); |
| 1591 |
if ( $links instanceof \DOMNodeList ) { |
| 1592 |
foreach ( $links as $link ) { |
| 1593 |
if ( $link instanceof \DOMElement ) { |
| 1594 |
$link->removeAttribute( 'target' ); |
| 1595 |
$rel = $link->getAttribute( 'rel' ); |
| 1596 |
if ( $rel ) { |
| 1597 |
$cleaned_rel = trim( (string) preg_replace( '/\s+/', ' ', (string) preg_replace( '/\b(noopener|noreferrer)\b/i', '', $rel ) ) ); |
| 1598 |
if ( $cleaned_rel ) { |
| 1599 |
$link->setAttribute( 'rel', $cleaned_rel ); |
| 1600 |
} else { |
| 1601 |
$link->removeAttribute( 'rel' ); |
| 1602 |
} |
| 1603 |
} |
| 1604 |
} |
| 1605 |
} |
| 1606 |
} |
| 1607 |
} |
| 1608 |
|
| 1609 |
// Return cleaned HTML. |
| 1610 |
$body = $dom->getElementsByTagName( 'body' )->item( 0 ); |
| 1611 |
if ( $body instanceof \DOMNode ) { |
| 1612 |
$cleaned_html = $dom->saveHTML( $body ); |
| 1613 |
return is_string( $cleaned_html ) ? $cleaned_html : ''; |
| 1614 |
} |
| 1615 |
return ''; |
| 1616 |
} |
| 1617 |
|
| 1618 |
/** |
| 1619 |
* Encodes the given string with base64. |
| 1620 |
* Moved from admin class to here. |
| 1621 |
* |
| 1622 |
* @param string $logo contains svg's. |
| 1623 |
* @return string |
| 1624 |
*/ |
| 1625 |
public static function encode_svg( $logo ) { |
| 1626 |
return 'data:image/svg+xml;base64,' . base64_encode( $logo ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 1627 |
} |
| 1628 |
|
| 1629 |
/** |
| 1630 |
* Get plugin status |
| 1631 |
* |
| 1632 |
* @since 0.0.1 |
| 1633 |
* @since 1.7.0 moved to inc/helper.php from inc/admin-ajax.php |
| 1634 |
* |
| 1635 |
* @param string $plugin_init_file Plugin init file. |
| 1636 |
* @return string |
| 1637 |
*/ |
| 1638 |
public static function get_plugin_status( $plugin_init_file ) { |
| 1639 |
|
| 1640 |
$installed_plugins = get_plugins(); |
| 1641 |
|
| 1642 |
if ( ! isset( $installed_plugins[ $plugin_init_file ] ) ) { |
| 1643 |
return 'Install'; |
| 1644 |
} |
| 1645 |
if ( is_plugin_active( $plugin_init_file ) ) { |
| 1646 |
return 'Activated'; |
| 1647 |
} |
| 1648 |
return 'Installed'; |
| 1649 |
} |
| 1650 |
|
| 1651 |
/** |
| 1652 |
* Return the first installed plugin from a list, or a default if none exist. |
| 1653 |
* |
| 1654 |
* @since 2.0.0 |
| 1655 |
* |
| 1656 |
* @param array<string> $plugins_to_check Plugin file paths to check, in priority order. |
| 1657 |
* @param string $default Optional fallback plugin file path. Default empty string. |
| 1658 |
* |
| 1659 |
* @return string First installed plugin file path, or the default. |
| 1660 |
*/ |
| 1661 |
public static function get_plugin_if_installed( $plugins_to_check, $default = '' ) { |
| 1662 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 1663 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 1664 |
} |
| 1665 |
|
| 1666 |
$plugins = get_plugins(); |
| 1667 |
|
| 1668 |
foreach ( self::get_array_value( $plugins_to_check ) as $plugin_file ) { |
| 1669 |
if ( isset( $plugins[ $plugin_file ] ) ) { |
| 1670 |
return $plugin_file; |
| 1671 |
} |
| 1672 |
} |
| 1673 |
|
| 1674 |
return $default; |
| 1675 |
} |
| 1676 |
|
| 1677 |
/** |
| 1678 |
* Check which Starter Templates plugin is installed and return its main plugin file path. |
| 1679 |
* |
| 1680 |
* @since 1.7.3 |
| 1681 |
* |
| 1682 |
* @return string The main plugin file path of the installed Starter Templates plugin. |
| 1683 |
*/ |
| 1684 |
public static function check_starter_template_plugin() { |
| 1685 |
return self::get_plugin_if_installed( |
| 1686 |
[ 'astra-pro-sites/astra-pro-sites.php' ], |
| 1687 |
'astra-sites/astra-sites.php' |
| 1688 |
); |
| 1689 |
} |
| 1690 |
|
| 1691 |
/** |
| 1692 |
* Get sureforms recommended integrations. |
| 1693 |
* |
| 1694 |
* @since 0.0.1 |
| 1695 |
* @since 1.7.0 moved to inc/helper.php from inc/admin-ajax.php |
| 1696 |
* |
| 1697 |
* @return array<mixed> |
| 1698 |
*/ |
| 1699 |
public static function sureforms_get_integration() { |
| 1700 |
$suretrigger_connected = apply_filters( 'suretriggers_is_user_connected', '' ); |
| 1701 |
$logo_sure_triggers = file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/suretriggers.svg' ); |
| 1702 |
$logo_full = file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/suretriggers_full.svg' ); |
| 1703 |
$logo_sure_mails = file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/suremails.svg' ); |
| 1704 |
$logo_uae = file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/uae.svg' ); |
| 1705 |
$logo_starter_templates = file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/starterTemplates.svg' ); |
| 1706 |
$logo_sure_rank = file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/surerank.svg' ); |
| 1707 |
$logo_sure_contact = file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/surecontact.svg' ); |
| 1708 |
|
| 1709 |
$integrations = [ |
| 1710 |
'sure_contact' => [ |
| 1711 |
'title' => __( 'SureContact', 'sureforms' ), |
| 1712 |
'singleLineDescription' => __( 'Turn Emails Into Revenue with a CRM Built for Your Website!', 'sureforms' ), |
| 1713 |
'subtitle' => __( 'Send newsletters, run campaigns, set up automations, manage contacts, and see exactly how much revenue your emails generate, all in one place.', 'sureforms' ), |
| 1714 |
'status' => self::get_plugin_status( 'surecontact/surecontact.php' ), |
| 1715 |
'slug' => 'surecontact', |
| 1716 |
'path' => 'surecontact/surecontact.php', |
| 1717 |
'logo' => self::encode_svg( is_string( $logo_sure_contact ) ? $logo_sure_contact : '' ), |
| 1718 |
], |
| 1719 |
'sure_mails' => [ |
| 1720 |
'title' => __( 'SureMail', 'sureforms' ), |
| 1721 |
'singleLineDescription' => __( 'Boost Your Email Deliverability Instantly!', 'sureforms' ), |
| 1722 |
'subtitle' => __( 'Access a powerful, easy-to-use email delivery service that ensures your emails land in inboxes, not spam folders. Automate your WordPress email workflows confidently with SureMail.', 'sureforms' ), |
| 1723 |
'status' => self::get_plugin_status( 'suremails/suremails.php' ), |
| 1724 |
'slug' => 'suremails', |
| 1725 |
'path' => 'suremails/suremails.php', |
| 1726 |
'logo' => self::encode_svg( is_string( $logo_sure_mails ) ? $logo_sure_mails : '' ), |
| 1727 |
], |
| 1728 |
'sure_triggers' => [ |
| 1729 |
'title' => __( 'OttoKit', 'sureforms' ), |
| 1730 |
'singleLineDescription' => __( 'Automate your WordPress workflows effortlessly.', 'sureforms' ), |
| 1731 |
'subtitle' => __( 'Connect your WordPress plugins and favourite apps, automate tasks, and sync data effortlessly using OttoKit’s clean, visual workflow builder — no coding or complex setup required.', 'sureforms' ), |
| 1732 |
'status' => self::get_plugin_status( 'suretriggers/suretriggers.php' ), |
| 1733 |
'slug' => 'suretriggers', |
| 1734 |
'path' => 'suretriggers/suretriggers.php', |
| 1735 |
'logo' => self::encode_svg( is_string( $logo_sure_triggers ) ? $logo_sure_triggers : '' ), |
| 1736 |
'logo_full' => self::encode_svg( is_string( $logo_full ) ? $logo_full : '' ), |
| 1737 |
'connected' => $suretrigger_connected, |
| 1738 |
'connection_url' => admin_url( 'admin.php?page=suretriggers' ), |
| 1739 |
], |
| 1740 |
'starter_templates' => [ |
| 1741 |
'title' => __( 'Starter Templates', 'sureforms' ), |
| 1742 |
'singleLineDescription' => __( 'Launch Beautiful Websites in Minutes!', 'sureforms' ), |
| 1743 |
'subtitle' => __( 'Choose from professionally designed templates, import with one click, and customize effortlessly to match your brand.', 'sureforms' ), |
| 1744 |
'status' => self::get_plugin_status( self::check_starter_template_plugin() ), |
| 1745 |
'slug' => 'astra-sites', |
| 1746 |
'path' => self::check_starter_template_plugin(), |
| 1747 |
'logo' => self::encode_svg( is_string( $logo_starter_templates ) ? $logo_starter_templates : '' ), |
| 1748 |
], |
| 1749 |
]; |
| 1750 |
|
| 1751 |
$elementor_installed = self::get_plugin_if_installed( [ 'elementor/elementor.php' ] ); |
| 1752 |
|
| 1753 |
if ( $elementor_installed ) { |
| 1754 |
$integrations['uae'] = [ |
| 1755 |
'title' => __( 'Ultimate Addons for Elementor', 'sureforms' ), |
| 1756 |
'singleLineDescription' => __( 'Power Up Elementor to Build Stunning Websites Faster!', 'sureforms' ), |
| 1757 |
'subtitle' => __( 'Enhance Elementor with powerful widgets and templates. Build stunning, high-performing websites faster with creative design elements and seamless customization.', 'sureforms' ), |
| 1758 |
'status' => self::get_plugin_status( 'header-footer-elementor/header-footer-elementor.php' ), |
| 1759 |
'slug' => 'header-footer-elementor', |
| 1760 |
'path' => 'header-footer-elementor/header-footer-elementor.php', |
| 1761 |
'logo' => self::encode_svg( is_string( $logo_uae ) ? $logo_uae : '' ), |
| 1762 |
]; |
| 1763 |
} else { |
| 1764 |
$integrations['sure_rank'] = [ |
| 1765 |
'title' => __( 'SureRank', 'sureforms' ), |
| 1766 |
'singleLineDescription' => __( 'Elevate Your SEO and Climb Search Rankings Effortlessly!', 'sureforms' ), |
| 1767 |
'subtitle' => __( 'Boost your website\'s visibility with smart SEO automation. Optimize content, track keyword performance, and get actionable insights, all inside WordPress.', 'sureforms' ), |
| 1768 |
'status' => self::get_plugin_status( 'surerank/surerank.php' ), |
| 1769 |
'slug' => 'surerank', |
| 1770 |
'path' => 'surerank/surerank.php', |
| 1771 |
'logo' => self::encode_svg( is_string( $logo_sure_rank ) ? $logo_sure_rank : '' ), |
| 1772 |
]; |
| 1773 |
} |
| 1774 |
|
| 1775 |
return apply_filters( 'srfm_integrated_plugins', $integrations ); |
| 1776 |
} |
| 1777 |
|
| 1778 |
/** |
| 1779 |
* Get the current rotating plugin for the banner. |
| 1780 |
* |
| 1781 |
* Plugins rotate every 2 days. Only non-activated plugins are shown. |
| 1782 |
* Returns false if all plugins are activated. |
| 1783 |
* |
| 1784 |
* @since 2.0.0 |
| 1785 |
* @return array<string, mixed>|false The current plugin data or false if all plugins are activated. |
| 1786 |
*/ |
| 1787 |
public static function get_rotating_plugin_banner() { |
| 1788 |
$all_plugins = self::sureforms_get_integration(); |
| 1789 |
|
| 1790 |
if ( ! is_array( $all_plugins ) ) { |
| 1791 |
return false; |
| 1792 |
} |
| 1793 |
|
| 1794 |
$available_plugins = []; |
| 1795 |
|
| 1796 |
// Only include non-activated plugins. |
| 1797 |
foreach ( $all_plugins as $plugin ) { |
| 1798 |
if ( ! is_array( $plugin ) ) { |
| 1799 |
continue; |
| 1800 |
} |
| 1801 |
if ( isset( $plugin['status'] ) && is_string( $plugin['status'] ) && 'Activated' !== $plugin['status'] ) { |
| 1802 |
$available_plugins[] = $plugin; |
| 1803 |
} |
| 1804 |
} |
| 1805 |
|
| 1806 |
// Re-index the array to have sequential numeric keys. |
| 1807 |
$available_plugins = array_values( $available_plugins ); |
| 1808 |
$total_plugins = count( $available_plugins ); |
| 1809 |
|
| 1810 |
// Hide section if all plugins are active. |
| 1811 |
if ( 0 === $total_plugins ) { |
| 1812 |
return false; |
| 1813 |
} |
| 1814 |
|
| 1815 |
// Get stored rotation data. |
| 1816 |
$rotation_data = self::get_srfm_option( 'plugin_banner_rotation', [] ); |
| 1817 |
|
| 1818 |
if ( ! is_array( $rotation_data ) ) { |
| 1819 |
$rotation_data = []; |
| 1820 |
} |
| 1821 |
|
| 1822 |
// Initialize rotation data if empty. |
| 1823 |
if ( empty( $rotation_data ) ) { |
| 1824 |
$current_time = time(); |
| 1825 |
self::update_srfm_option( |
| 1826 |
'plugin_banner_rotation', |
| 1827 |
[ |
| 1828 |
'last_rotation_date' => $current_time, |
| 1829 |
'plugin_index' => 0, |
| 1830 |
] |
| 1831 |
); |
| 1832 |
return isset( $available_plugins[0] ) && is_array( $available_plugins[0] ) ? $available_plugins[0] : false; |
| 1833 |
} |
| 1834 |
|
| 1835 |
$last_rotation_date = isset( $rotation_data['last_rotation_date'] ) && is_int( $rotation_data['last_rotation_date'] ) ? $rotation_data['last_rotation_date'] : 0; |
| 1836 |
$plugin_index = isset( $rotation_data['plugin_index'] ) && is_numeric( $rotation_data['plugin_index'] ) ? intval( $rotation_data['plugin_index'] ) : 0; |
| 1837 |
|
| 1838 |
$current_time = time(); |
| 1839 |
$days_since_rotation = ( $current_time - $last_rotation_date ) / DAY_IN_SECONDS; |
| 1840 |
|
| 1841 |
// Rotate every 2 days. |
| 1842 |
if ( $days_since_rotation >= 2 ) { |
| 1843 |
// Rotate to next plugin. |
| 1844 |
++$plugin_index; |
| 1845 |
$plugin_index %= $total_plugins; |
| 1846 |
|
| 1847 |
// Update the rotation data. |
| 1848 |
self::update_srfm_option( |
| 1849 |
'plugin_banner_rotation', |
| 1850 |
[ |
| 1851 |
'last_rotation_date' => $current_time, |
| 1852 |
'plugin_index' => $plugin_index, |
| 1853 |
] |
| 1854 |
); |
| 1855 |
} |
| 1856 |
|
| 1857 |
// Ensure the index is within bounds. |
| 1858 |
if ( $plugin_index >= $total_plugins ) { |
| 1859 |
$plugin_index = 0; |
| 1860 |
} |
| 1861 |
|
| 1862 |
return isset( $available_plugins[ $plugin_index ] ) && is_array( $available_plugins[ $plugin_index ] ) ? $available_plugins[ $plugin_index ] : false; |
| 1863 |
} |
| 1864 |
|
| 1865 |
/** |
| 1866 |
* Get a value from the srfm_options array. |
| 1867 |
* |
| 1868 |
* @param string $key The key to retrieve. |
| 1869 |
* @param mixed $default The default value to return if the key does not exist. |
| 1870 |
* @since 1.8.0 |
| 1871 |
* @return mixed |
| 1872 |
*/ |
| 1873 |
public static function get_srfm_option( $key, $default = null ) { |
| 1874 |
$options = get_option( 'srfm_options', [] ); |
| 1875 |
if ( ! is_array( $options ) ) { |
| 1876 |
$options = []; |
| 1877 |
} |
| 1878 |
return array_key_exists( $key, $options ) ? $options[ $key ] : $default; |
| 1879 |
} |
| 1880 |
|
| 1881 |
/** |
| 1882 |
* Update a value in the srfm_options array. |
| 1883 |
* |
| 1884 |
* @param string $key The key to update. |
| 1885 |
* @param mixed $value The value to set. |
| 1886 |
* @since 1.8.0 |
| 1887 |
* @return void |
| 1888 |
*/ |
| 1889 |
public static function update_srfm_option( $key, $value ) { |
| 1890 |
$options = get_option( 'srfm_options', [] ); |
| 1891 |
if ( ! is_array( $options ) ) { |
| 1892 |
$options = []; |
| 1893 |
} |
| 1894 |
$options[ $key ] = $value; |
| 1895 |
update_option( 'srfm_options', $options ); |
| 1896 |
} |
| 1897 |
|
| 1898 |
/** |
| 1899 |
* Get the WordPress file types. |
| 1900 |
* |
| 1901 |
* @since 1.7.4 |
| 1902 |
* @return array<string,mixed> An associative array representing the file types. |
| 1903 |
*/ |
| 1904 |
public static function get_wp_file_types() { |
| 1905 |
$formats = []; |
| 1906 |
$mimes = get_allowed_mime_types(); |
| 1907 |
$maxsize = wp_max_upload_size() / 1048576; |
| 1908 |
if ( ! empty( $mimes ) ) { |
| 1909 |
foreach ( $mimes as $type => $mime ) { |
| 1910 |
$multiple = explode( '|', $type ); |
| 1911 |
foreach ( $multiple as $single ) { |
| 1912 |
$formats[] = $single; |
| 1913 |
} |
| 1914 |
} |
| 1915 |
} |
| 1916 |
|
| 1917 |
return [ |
| 1918 |
'formats' => $formats, |
| 1919 |
'maxsize' => $maxsize, |
| 1920 |
]; |
| 1921 |
} |
| 1922 |
|
| 1923 |
/** |
| 1924 |
* Determines if the SureForms Pro plugin is installed and active. |
| 1925 |
* |
| 1926 |
* Checks for the presence of the SRFM_PRO_VER constant. |
| 1927 |
* |
| 1928 |
* @since 1.8.0 |
| 1929 |
* |
| 1930 |
* @return bool True if the Pro plugin is active; false otherwise. |
| 1931 |
*/ |
| 1932 |
public static function has_pro() { |
| 1933 |
return defined( 'SRFM_PRO_VER' ); |
| 1934 |
} |
| 1935 |
|
| 1936 |
/** |
| 1937 |
* Verifies the request by checking the nonce and user capabilities. |
| 1938 |
* |
| 1939 |
* @param string $request_type The type of request, either 'rest' or 'ajax'. |
| 1940 |
* @param string $nonce_action The action name for the nonce. |
| 1941 |
* @param string $nonce_name The name of the nonce field. |
| 1942 |
* @param string $capability The capability required to perform the action. Default is 'manage_options'. |
| 1943 |
* |
| 1944 |
* @since 1.10.0 |
| 1945 |
* @return void |
| 1946 |
*/ |
| 1947 |
public static function verify_nonce_and_capabilities( $request_type, $nonce_action, $nonce_name, $capability = 'manage_options' ) { |
| 1948 |
|
| 1949 |
if ( ! is_string( $nonce_action ) || ! is_string( $nonce_name ) || empty( $nonce_action ) || empty( $nonce_name ) ) { |
| 1950 |
wp_send_json_error( |
| 1951 |
[ 'message' => __( 'Invalid nonce action or name.', 'sureforms' ) ], |
| 1952 |
400 |
| 1953 |
); |
| 1954 |
} |
| 1955 |
|
| 1956 |
// Verify nonce for security. |
| 1957 |
if ( 'rest' === $request_type ) { |
| 1958 |
// For REST API requests, use the WP_REST_Request object to verify the nonce. |
| 1959 |
if ( ! wp_verify_nonce( $nonce_action, $nonce_name ) ) { |
| 1960 |
wp_send_json_error( |
| 1961 |
[ 'message' => __( 'Invalid security token.', 'sureforms' ) ], |
| 1962 |
403 |
| 1963 |
); |
| 1964 |
} |
| 1965 |
} elseif ( 'ajax' === $request_type ) { |
| 1966 |
// For non-REST requests, use the standard nonce verification. |
| 1967 |
if ( ! check_ajax_referer( $nonce_action, $nonce_name, false ) ) { |
| 1968 |
wp_send_json_error( |
| 1969 |
[ 'message' => __( 'Invalid security token.', 'sureforms' ) ], |
| 1970 |
403 |
| 1971 |
); |
| 1972 |
} |
| 1973 |
} else { |
| 1974 |
// If the request type is not recognized, return an error. |
| 1975 |
wp_send_json_error( |
| 1976 |
[ 'message' => __( 'Invalid request type.', 'sureforms' ) ], |
| 1977 |
400 |
| 1978 |
); |
| 1979 |
} |
| 1980 |
|
| 1981 |
// Check user capabilities. |
| 1982 |
if ( ! current_user_can( $capability ) ) { |
| 1983 |
wp_send_json_error( |
| 1984 |
[ 'message' => esc_html__( 'You do not have permission to perform this action.', 'sureforms' ) ], |
| 1985 |
403 |
| 1986 |
); |
| 1987 |
} |
| 1988 |
} |
| 1989 |
|
| 1990 |
/** |
| 1991 |
* Get the block name from a field name by extracting the first two parts. |
| 1992 |
* |
| 1993 |
* @param string $field_name The full field name (e.g., 'srfm-text-lbl-123'). |
| 1994 |
* |
| 1995 |
* @since 1.11.0 |
| 1996 |
* @return string The block name (e.g., 'srfm-text'). |
| 1997 |
*/ |
| 1998 |
public static function get_block_name_from_field( $field_name ) { |
| 1999 |
return implode( '-', array_slice( explode( '-', explode( '-lbl-', $field_name )[0] ), 0, 2 ) ); |
| 2000 |
} |
| 2001 |
|
| 2002 |
/** |
| 2003 |
* Check if any of the top 10 popular WordPress SMTP plugins is active using array_intersect. |
| 2004 |
* |
| 2005 |
* @since 1.9.1 |
| 2006 |
* @return bool True if any SMTP plugin is active, false otherwise. |
| 2007 |
*/ |
| 2008 |
public static function is_any_smtp_plugin_active() { |
| 2009 |
$smtp_plugins = [ |
| 2010 |
'wp-mail-smtp/wp_mail_smtp.php', |
| 2011 |
'post-smtp/postman-smtp.php', |
| 2012 |
'easy-wp-smtp/easy-wp-smtp.php', |
| 2013 |
'wp-smtp/wp-smtp.php', |
| 2014 |
'newsletter/plugin.php', |
| 2015 |
'fluent-smtp/fluent-smtp.php', |
| 2016 |
'pepipost-smtp/pepipost-smtp.php', |
| 2017 |
'mail-bank/wp-mail-bank.php', |
| 2018 |
'smtp-mailer/smtp-mailer.php', |
| 2019 |
'suremails/suremails.php', |
| 2020 |
'site-mailer/site-mailer.php', |
| 2021 |
]; |
| 2022 |
|
| 2023 |
$active_plugins = (array) get_option( 'active_plugins', [] ); |
| 2024 |
// For multisite, merge sitewide active plugins. |
| 2025 |
if ( is_multisite() ) { |
| 2026 |
$network_plugins = (array) get_site_option( 'active_sitewide_plugins', [] ); |
| 2027 |
$active_plugins = array_merge( $active_plugins, array_keys( $network_plugins ) ); |
| 2028 |
} |
| 2029 |
|
| 2030 |
return (bool) array_intersect( $smtp_plugins, $active_plugins ); |
| 2031 |
} |
| 2032 |
|
| 2033 |
/** |
| 2034 |
* Apply a filter and return the filtered value only if it's a non-empty array. |
| 2035 |
* Otherwise, return the default array. |
| 2036 |
* |
| 2037 |
* @param string $filter_name The name of the filter to apply. |
| 2038 |
* @param mixed $default The default array to return if the filtered result is invalid. |
| 2039 |
* @param mixed ...$args Additional arguments to pass to the filter. |
| 2040 |
* |
| 2041 |
* @return array The filtered array if valid, otherwise the default. |
| 2042 |
*/ |
| 2043 |
public static function apply_filters_as_array( $filter_name, $default, ...$args ) { |
| 2044 |
// Ensure $default is an array. |
| 2045 |
if ( ! is_array( $default ) ) { |
| 2046 |
$default = []; |
| 2047 |
} |
| 2048 |
|
| 2049 |
// Validate the filter name. |
| 2050 |
if ( ! is_string( $filter_name ) || empty( $filter_name ) ) { |
| 2051 |
return $default; |
| 2052 |
} |
| 2053 |
|
| 2054 |
// Apply the filter with additional arguments. |
| 2055 |
$filtered = apply_filters( $filter_name, $default, ...$args ); |
| 2056 |
|
| 2057 |
// Return filtered result if it's a non-empty array. |
| 2058 |
return is_array( $filtered ) && ! empty( $filtered ) ? $filtered : $default; |
| 2059 |
} |
| 2060 |
|
| 2061 |
/** |
| 2062 |
* Get forms with entry counts for a specific time period. |
| 2063 |
* |
| 2064 |
* @param int $timestamp The timestamp to get entries after. |
| 2065 |
* @param int $limit Maximum number of forms to return (0 for all). |
| 2066 |
* @param bool $sort Whether to sort by entry count descending. |
| 2067 |
* @return array Array of form data with entry counts. |
| 2068 |
* @since 1.9.1 |
| 2069 |
*/ |
| 2070 |
public static function get_forms_with_entry_counts( $timestamp, $limit = 0, $sort = true ) { |
| 2071 |
// Get all published forms with post objects for bulk title access. |
| 2072 |
$args = [ |
| 2073 |
'post_type' => SRFM_FORMS_POST_TYPE, |
| 2074 |
'posts_per_page' => -1, |
| 2075 |
'post_status' => 'publish', |
| 2076 |
'orderby' => 'ID', |
| 2077 |
'order' => 'DESC', |
| 2078 |
'no_found_rows' => true, |
| 2079 |
'update_post_term_cache' => false, |
| 2080 |
'update_post_meta_cache' => false, |
| 2081 |
]; |
| 2082 |
|
| 2083 |
$query = new \WP_Query( $args ); |
| 2084 |
|
| 2085 |
if ( ! $query->have_posts() ) { |
| 2086 |
return []; |
| 2087 |
} |
| 2088 |
|
| 2089 |
$all_forms = []; |
| 2090 |
|
| 2091 |
// Process posts directly from the query results without touching global $post. |
| 2092 |
foreach ( $query->posts as $form ) { |
| 2093 |
// Ensure we have a valid post object. |
| 2094 |
if ( ! $form instanceof \WP_Post ) { |
| 2095 |
continue; |
| 2096 |
} |
| 2097 |
|
| 2098 |
$form_id = (int) $form->ID; |
| 2099 |
if ( $form_id <= 0 ) { |
| 2100 |
continue; |
| 2101 |
} |
| 2102 |
|
| 2103 |
// Get entries count after the timestamp for this specific form. |
| 2104 |
$entry_count = Entries::get_entries_count_after( $timestamp, $form_id ); |
| 2105 |
|
| 2106 |
// Get form title directly from post object, use "Blank Form" if empty. |
| 2107 |
$form_title = $form->post_title; |
| 2108 |
if ( empty( trim( self::get_string_value( $form_title ) ) ) ) { |
| 2109 |
$form_title = __( 'Blank Form', 'sureforms' ); |
| 2110 |
} |
| 2111 |
|
| 2112 |
$all_forms[] = [ |
| 2113 |
'form_id' => $form_id, |
| 2114 |
'title' => $form_title, |
| 2115 |
'count' => $entry_count, |
| 2116 |
]; |
| 2117 |
} |
| 2118 |
|
| 2119 |
// Sort by count descending, then by form_id descending for consistency. |
| 2120 |
if ( $sort ) { |
| 2121 |
usort( |
| 2122 |
$all_forms, |
| 2123 |
static function( $a, $b ) { |
| 2124 |
if ( $a['count'] === $b['count'] ) { |
| 2125 |
return $b['form_id'] - $a['form_id']; |
| 2126 |
} |
| 2127 |
return $b['count'] - $a['count']; |
| 2128 |
} |
| 2129 |
); |
| 2130 |
} |
| 2131 |
|
| 2132 |
// Return limited results if specified. |
| 2133 |
if ( $limit > 0 ) { |
| 2134 |
return array_slice( $all_forms, 0, $limit ); |
| 2135 |
} |
| 2136 |
|
| 2137 |
return $all_forms; |
| 2138 |
} |
| 2139 |
|
| 2140 |
/** |
| 2141 |
* Check if the given form ID is valid SureForms form ID. |
| 2142 |
* A valid form ID is a numeric value that corresponds to an existing SureForms form in the database. |
| 2143 |
* |
| 2144 |
* @since 1.9.1 |
| 2145 |
* |
| 2146 |
* @param int|string|mixed $form_id The form ID to validate. |
| 2147 |
* @return bool True if the form ID is valid, false otherwise. |
| 2148 |
*/ |
| 2149 |
public static function is_valid_form( $form_id ) { |
| 2150 |
|
| 2151 |
// Check for a valid form ID. |
| 2152 |
if ( empty( $form_id ) || ! is_numeric( $form_id ) ) { |
| 2153 |
return false; |
| 2154 |
} |
| 2155 |
|
| 2156 |
// Check if the form ID exists in the database. |
| 2157 |
$form = get_post( self::get_integer_value( $form_id ) ); |
| 2158 |
|
| 2159 |
// If the form does not exist or is not of the correct post type, return false. |
| 2160 |
if ( ! $form || ! is_a( $form, 'WP_Post' ) || SRFM_FORMS_POST_TYPE !== $form->post_type ) { |
| 2161 |
return false; |
| 2162 |
} |
| 2163 |
|
| 2164 |
return true; |
| 2165 |
} |
| 2166 |
|
| 2167 |
/** |
| 2168 |
* Get the timestamp from a string. |
| 2169 |
* |
| 2170 |
* This function uses WordPress's configured timezone (from Settings → General → Timezone) |
| 2171 |
* to ensure consistent behavior regardless of the server's timezone settings. |
| 2172 |
* |
| 2173 |
* @param string $date The date in YYYY-MM-DD format (e.g., '2026-01-10'). |
| 2174 |
* @param string $hours The hours in 12-hour format (e.g., '12', '01'-'12'). |
| 2175 |
* @param string $minutes The minutes (e.g., '00', '00'-'59'). |
| 2176 |
* @param string $meridiem The meridiem (e.g., 'AM' or 'PM'). |
| 2177 |
* |
| 2178 |
* @since 1.10.1 |
| 2179 |
* @return int|false The timestamp if successful, false otherwise. |
| 2180 |
*/ |
| 2181 |
public static function get_timestamp_from_string( $date, $hours = '12', $minutes = '00', $meridiem = 'AM' ) { |
| 2182 |
|
| 2183 |
if ( empty( $date ) || ! is_string( $date ) ) { |
| 2184 |
return false; // Invalid input. |
| 2185 |
} |
| 2186 |
|
| 2187 |
// Ensure the date is in a valid format of YYYY-MM-DD. |
| 2188 |
if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $date ) ) { |
| 2189 |
return false; // Invalid date format. |
| 2190 |
} |
| 2191 |
|
| 2192 |
$time_string = $date . ' ' . $hours . ':' . $minutes . ' ' . $meridiem; |
| 2193 |
|
| 2194 |
// Convert to timestamp using WordPress timezone. |
| 2195 |
// This ensures the date/time is interpreted in the site's configured timezone, |
| 2196 |
// not the server's timezone or PHP's default timezone. |
| 2197 |
try { |
| 2198 |
$datetime = date_create( $time_string, wp_timezone() ); |
| 2199 |
|
| 2200 |
if ( false === $datetime ) { |
| 2201 |
return false; |
| 2202 |
} |
| 2203 |
|
| 2204 |
$timestamp = $datetime->getTimestamp(); |
| 2205 |
|
| 2206 |
if ( is_int( $timestamp ) && $timestamp > 0 ) { |
| 2207 |
return $timestamp; |
| 2208 |
} |
| 2209 |
} catch ( \Exception $e ) { |
| 2210 |
// If timezone conversion fails, return false. |
| 2211 |
return false; |
| 2212 |
} |
| 2213 |
|
| 2214 |
// If conversion fails, return false. |
| 2215 |
return false; |
| 2216 |
} |
| 2217 |
|
| 2218 |
/** |
| 2219 |
* Generate a unique ID for the saved form. |
| 2220 |
* Also ensures that the generated ID does not already exist in the database table. |
| 2221 |
* |
| 2222 |
* @param class-string $class The class name where the get method is defined to check for existing IDs. |
| 2223 |
* @param int<1, max> $length The length of the random bytes to generate. Default is 8. |
| 2224 |
* @return string |
| 2225 |
* @since 2.2.0 |
| 2226 |
*/ |
| 2227 |
public static function generate_unique_id( $class, $length = 8 ) { |
| 2228 |
// Ensure length is at least 1. |
| 2229 |
$length = max( 1, $length ); |
| 2230 |
|
| 2231 |
do { |
| 2232 |
$id = bin2hex( random_bytes( $length ) ); |
| 2233 |
} while ( is_callable( [ $class, 'get' ] ) && call_user_func( [ $class, 'get' ], $id ) ); |
| 2234 |
return $id; |
| 2235 |
} |
| 2236 |
|
| 2237 |
/** |
| 2238 |
* Log error messages to the error log. |
| 2239 |
* |
| 2240 |
* This function checks if error_log function exists, validates the message, |
| 2241 |
* and logs it with the print_r second argument set to true. |
| 2242 |
* |
| 2243 |
* Logging is disabled by default. To enable logging, add this to wp-config.php: |
| 2244 |
* define( 'SRFM_LOG', true ); |
| 2245 |
* |
| 2246 |
* @param mixed $message The error message to log. Can be string or any type. |
| 2247 |
* @param string $prefix Optional prefix to add before the message. Default: 'Log :'. |
| 2248 |
* |
| 2249 |
* @since 2.0.0 |
| 2250 |
* @return void |
| 2251 |
*/ |
| 2252 |
public static function srfm_log( $message, $prefix = 'Log :' ) { |
| 2253 |
// Check if logging is enabled via SRFM_LOG constant. |
| 2254 |
if ( ! defined( 'SRFM_LOG' ) ) { |
| 2255 |
return; |
| 2256 |
} |
| 2257 |
unset( $message, $prefix ); |
| 2258 |
} |
| 2259 |
|
| 2260 |
/** |
| 2261 |
* Encodes data to base64 after JSON encoding with validation. |
| 2262 |
* |
| 2263 |
* This function checks if the data is non-empty and valid for JSON encoding. |
| 2264 |
* If data is not valid, returns an empty string. |
| 2265 |
* Otherwise, it attempts to JSON encode and then base64 encode the result. |
| 2266 |
* |
| 2267 |
* @param mixed $data The data to JSON encode and then base64 encode. |
| 2268 |
* @return string The base64-encoded JSON string, or empty string on failure. |
| 2269 |
*/ |
| 2270 |
public static function srfm_base64_json_encode( $data ) { |
| 2271 |
if ( empty( $data ) || ! is_array( $data ) ) { |
| 2272 |
return ''; |
| 2273 |
} |
| 2274 |
|
| 2275 |
$json = wp_json_encode( $data ); |
| 2276 |
if ( false === $json || '' === $json ) { |
| 2277 |
return ''; |
| 2278 |
} |
| 2279 |
|
| 2280 |
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 2281 |
return base64_encode( $json ); |
| 2282 |
} |
| 2283 |
|
| 2284 |
} |
| 2285 |
|