| 1 |
<?php |
| 2 |
/* |
| 3 |
Description: Extends the functionality of Profile Builder by allowing you to set a maximum character length for the custom input and textarea fields. |
| 4 |
*/ |
| 5 |
|
| 6 |
/* |
| 7 |
* Function that enqueues the necessary scripts |
| 8 |
* |
| 9 |
* @since v.1.0.0 |
| 10 |
*/ |
| 11 |
function wppb_mcl_scripts($hook) { |
| 12 |
if ( $hook == 'profile-builder_page_manage-fields' ) { |
| 13 |
wp_enqueue_script( 'wppb-max-char-length', plugin_dir_url(__FILE__) . 'assets/js/main.js', array( 'jquery', 'wppb-manage-fields-live-change' ) ); |
| 14 |
} |
| 15 |
} |
| 16 |
add_action( 'admin_enqueue_scripts', 'wppb_mcl_scripts' ); |
| 17 |
|
| 18 |
/* |
| 19 |
* Function that adds maximum character length to the field properties in manage fields |
| 20 |
* |
| 21 |
* @since v.1.0.0 |
| 22 |
* |
| 23 |
* @param array $fields - The current field properties |
| 24 |
* |
| 25 |
* @return array - The field properties that now include the maximum character length property |
| 26 |
*/ |
| 27 |
function wppb_mcl_manage_field( $fields ) { |
| 28 |
$max_length_manage_field = array( 'type' => 'text', 'slug' => 'maximum-character-length', 'title' => __( 'Maximum Character Length', 'profile-builder' ), 'description' => __( "Specify the maximum number of characters a user can type in this field", 'profile-builder' ) ); |
| 29 |
array_push( $fields, $max_length_manage_field ); |
| 30 |
|
| 31 |
return $fields; |
| 32 |
} |
| 33 |
add_filter( 'wppb_manage_fields', 'wppb_mcl_manage_field' ); |
| 34 |
|
| 35 |
/* |
| 36 |
* Function that checks to see if the maximum character length that the user entered is |
| 37 |
* a number, we don't want random strings |
| 38 |
* |
| 39 |
* @since v.1.0.0 |
| 40 |
* |
| 41 |
* @param string $message - The error messages that will be displayed to the user |
| 42 |
* @param array $posted_values - The information the user has entered for the field |
| 43 |
* |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
function wppb_mcl_check_max_character_length( $message, $posted_values ) { |
| 47 |
|
| 48 |
if ( isset( $posted_values['maximum-character-length'] ) && !is_numeric( $posted_values['maximum-character-length'] ) && !empty( $posted_values['maximum-character-length'] ) ) |
| 49 |
$message .= __( "The entered character number is not numerical\n", 'profile-builder' ); |
| 50 |
|
| 51 |
return $message; |
| 52 |
} |
| 53 |
add_filter( 'wppb_check_extra_manage_fields', 'wppb_mcl_check_max_character_length', 10, 2 ); |
| 54 |
|
| 55 |
/* |
| 56 |
* Function that changes the default maximum character length with the one |
| 57 |
* the user has set |
| 58 |
* |
| 59 |
* @since v.1.0.0 |
| 60 |
* |
| 61 |
* @param int $default_value |
| 62 |
* @param array $field |
| 63 |
* |
| 64 |
* @return int |
| 65 |
*/ |
| 66 |
function wppb_mcl_set_max_character_length( $default_value, $field = '' ) { |
| 67 |
$output = $default_value; |
| 68 |
|
| 69 |
if( isset( $field['maximum-character-length'] ) && !empty( $field['maximum-character-length'] ) ) { |
| 70 |
$output = (int)$field['maximum-character-length']; |
| 71 |
} |
| 72 |
|
| 73 |
return $output; |
| 74 |
} |
| 75 |
add_filter( 'wppb_maximum_character_length', 'wppb_mcl_set_max_character_length', 10 , 2); |
| 76 |
|