class-wpp-acf-datepicker-v4.php
1 month ago
class-wpp-acf-datepicker-v5.php
3 months ago
class-wpp-acf-timepicker-v4.php
1 month ago
class-wpp-acf-timepicker-v5.php
3 months ago
class-wpp-acf-timepicker-v5.php
201 lines
| 1 | <?php |
| 2 | |
| 3 | use WPParsidate\Helper\Assets; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || exit( 'No direct script access allowed' ); |
| 6 | |
| 7 | /** |
| 8 | * Adds Timepicker field to ACF |
| 9 | * |
| 10 | * @package WP-Parsidate |
| 11 | * @subpackage Plugins/ACF/WPP_acf_field_wpp_timepicker |
| 12 | * |
| 13 | * @since 4.0.0 |
| 14 | */ |
| 15 | class WPP_acf_field_wpp_timepicker extends acf_field { |
| 16 | |
| 17 | /** |
| 18 | * Hooks required tags |
| 19 | */ |
| 20 | function __construct() { |
| 21 | $this->name = 'wpp_timepicker'; |
| 22 | |
| 23 | $this->label = esc_html__( 'Time', 'wp-parsidate' ); |
| 24 | |
| 25 | $this->category = esc_html__( 'Parsidate', 'wp-parsidate' ); |
| 26 | |
| 27 | $this->defaults = array( |
| 28 | 'time-format' => '12 hours', |
| 29 | 'placeholder' => 'HH:MM', |
| 30 | ); |
| 31 | |
| 32 | $this->l10n = array( |
| 33 | 'error' => esc_html__( 'Error! Please select a valid time.', 'wp-parsidate' ), |
| 34 | ); |
| 35 | |
| 36 | parent::__construct(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Create extra settings for your field. These are visible when editing a field |
| 41 | * |
| 42 | * @param $field (array) the $field being edited |
| 43 | * |
| 44 | * @since 4.0.0 |
| 45 | */ |
| 46 | function render_field_settings( $field ) { |
| 47 | acf_render_field_setting( $field, array( |
| 48 | 'label' => esc_html__( 'Time Format', 'wp-parsidate' ), |
| 49 | 'instructions' => esc_html__( 'Display time picker in 24 or 12 hours format', 'wp-parsidate' ), |
| 50 | 'type' => 'select', |
| 51 | 'name' => 'time-format', |
| 52 | 'choices' => array( |
| 53 | '12hours' => '12 hours', |
| 54 | '24hours' => '24 hours', |
| 55 | ) |
| 56 | ) ); |
| 57 | |
| 58 | acf_render_field_setting( $field, array( |
| 59 | 'label' => esc_html__( 'Placeholder', 'wp-parsidate' ), |
| 60 | 'instructions' => esc_html__( 'Show custom placeholder', 'wp-parsidate' ), |
| 61 | 'type' => 'text', |
| 62 | 'name' => 'placeholder', |
| 63 | ) ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Create the HTML interface for your field |
| 68 | * |
| 69 | * @param $field (array) the $field being edited |
| 70 | * |
| 71 | * @since 4.0.0 |
| 72 | */ |
| 73 | function render_field( $field ) { ?> |
| 74 | <div class="wpp-time-picker"> |
| 75 | <input type="text" id="<?php echo esc_attr( $field['key'] ) ?>" |
| 76 | name="<?php echo esc_attr( $field['name'] ) ?>" dir="ltr" |
| 77 | value="<?php echo esc_attr( $field['value'] ) ?>" autocomplete="off" |
| 78 | placeholder="<?php echo esc_attr( $field['placeholder'] ) ?>"/> |
| 79 | <script> |
| 80 | jQuery(document).ready(function ($) { |
| 81 | $('#<?php echo esc_attr( $field['key'] ) ?>').timepicki({ |
| 82 | show_meridian:<?php echo '24hours' === $field['time-format'] ? 'true' : 'false'; ?>, |
| 83 | disable_keyboard_mobile: true |
| 84 | }) |
| 85 | }) |
| 86 | </script> |
| 87 | </div> |
| 88 | <?php |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * This action is called in the admin_enqueue_scripts action on the edit screen where your field is created. |
| 93 | * Use this action to add CSS + JavaScript to assist your render_field() action. |
| 94 | * |
| 95 | * @since 4.0.0 |
| 96 | */ |
| 97 | function input_admin_enqueue_scripts() { |
| 98 | $pluginVersion = Assets::getVersion(); |
| 99 | $debugName = WP_PARSI_DEBUG_MODE ? '' : '.min'; |
| 100 | |
| 101 | wp_enqueue_script( 'wpp_jalali_datepicker', Assets::url( 'js-admin/jalalidatepicker.min.js' ), |
| 102 | array( 'acf-input' ), $pluginVersion, [ 'in_footer' => true ] ); |
| 103 | wp_enqueue_style( 'wpp_jalali_datepicker', Assets::url( 'css-admin/jalalidatepicker' . $debugName . '.css' ), |
| 104 | array( 'acf-input' ), $pluginVersion ); |
| 105 | |
| 106 | // Remove jquery time picker to avoid conflict with woocommerce |
| 107 | wp_dequeue_style( 'acf-timepicker' ); |
| 108 | wp_dequeue_script( 'acf-timepicker' ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * This filter is applied to the $value after it is loaded from the db |
| 113 | * |
| 114 | * @param $value (mixed) the value found in the database |
| 115 | * @param $post_id (mixed) the $post_id from which the value was loaded |
| 116 | * @param $field (array) the field array holding all the field options |
| 117 | * |
| 118 | * @return int|string $value |
| 119 | * @since 4.0.0 |
| 120 | */ |
| 121 | function load_value( $value, $post_id, $field ) { |
| 122 | return apply_filters( 'wpp_acf_after_load_time', $value ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * This filter is applied to the $value before it is saved in the db |
| 127 | * |
| 128 | * @param $value (mixed) the value found in the database |
| 129 | * @param $post_id (mixed) the $post_id from which the value was loaded |
| 130 | * @param $field (array) the field array holding all the field options |
| 131 | * |
| 132 | * @return false|string $value |
| 133 | * @since 4.0.0 |
| 134 | */ |
| 135 | function update_value( $value, $post_id, $field ) { |
| 136 | return apply_filters( 'wpp_acf_before_update_time', $value ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * This filter is applied to the $value after it is loaded from the db and, before it is returned to the template |
| 141 | * |
| 142 | * @param $value (mixed) the value which was loaded from the database |
| 143 | * @param $post_id (mixed) the $post_id from which the value was loaded |
| 144 | * @param $field (array) the field array holding all the field options |
| 145 | * |
| 146 | * @return mixed $value (mixed) the modified value |
| 147 | * @since 4.0.0 |
| 148 | */ |
| 149 | function format_value( $value, $post_id, $field ) { |
| 150 | if ( empty( $value ) ) { |
| 151 | return $value; |
| 152 | } |
| 153 | |
| 154 | return apply_filters( 'wpp_acf_before_time_render', $value ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * This filter is used to perform validation on the value prior to saving. |
| 159 | * All values are validated regardless of the field's required setting. This allows you to validate and return |
| 160 | * messages to the user if the value is not correct |
| 161 | * |
| 162 | * @param $valid (boolean) validation status based on the value and the field's required setting |
| 163 | * @param $value (mixed) the $_POST value |
| 164 | * @param $field (array) the field array holding all the field options |
| 165 | * @param $input (string) the corresponding input name for $_POST value |
| 166 | * |
| 167 | * @return true|false |
| 168 | * @since 4.0.0 |
| 169 | * |
| 170 | */ |
| 171 | function validate_value( $valid, $value, $field, $input ) { |
| 172 | return apply_filters( 'wpp_acf_validate_time_value', $valid, $value, $field, $input ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * This filter is applied to the $field after it is loaded from the database |
| 177 | * |
| 178 | * @param $field (array) the field array holding all the field options |
| 179 | * |
| 180 | * @return mixed $field |
| 181 | * @since 4.0.0 |
| 182 | */ |
| 183 | function load_field( $field ) { |
| 184 | return apply_filters( 'wpp_acf_after_load_time_field', $field ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * This filter is applied to the $field before it is saved to the database |
| 189 | * |
| 190 | * @param $field (array) the field array holding all the field options |
| 191 | * |
| 192 | * @return mixed $field |
| 193 | * @since 4.0.0 |
| 194 | */ |
| 195 | function update_field( $field ) { |
| 196 | return apply_filters( 'wpp_acf_before_save_time_field', $field ); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | new WPP_acf_field_wpp_timepicker(); |
| 201 |