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-datepicker-v5.php
188 lines
| 1 | <?php |
| 2 | |
| 3 | use WPParsidate\Helper\Assets; |
| 4 | use WPParsidate\Settings\Settings; |
| 5 | |
| 6 | defined( 'ABSPATH' ) || exit( 'No direct script access allowed' ); |
| 7 | |
| 8 | /** |
| 9 | * Adds Jalali Datepicker field to ACF |
| 10 | * |
| 11 | * @package WP-Parsidate |
| 12 | * @subpackage Plugins/ACF/WPP_acf_field_jalali_datepicker |
| 13 | * |
| 14 | * @since 4.0.0 |
| 15 | */ |
| 16 | class WPP_acf_field_jalali_datepicker extends acf_field { |
| 17 | |
| 18 | /** |
| 19 | * Hooks required tags |
| 20 | */ |
| 21 | function __construct() { |
| 22 | $this->name = 'jalali_datepicker'; |
| 23 | |
| 24 | $this->label = esc_html__( 'Date', 'wp-parsidate' ); |
| 25 | |
| 26 | $this->category = esc_html__( 'Parsidate', 'wp-parsidate' ); |
| 27 | |
| 28 | $this->defaults = array( |
| 29 | 'placeholder' => 'YYYY-MM-DD', |
| 30 | ); |
| 31 | |
| 32 | $this->l10n = array( |
| 33 | 'error' => esc_html__( 'Error! Please select a valid date.', '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__( 'Placeholder', 'wp-parsidate' ), |
| 49 | 'instructions' => esc_html__( 'Show custom placeholder', 'wp-parsidate' ), |
| 50 | 'type' => 'text', |
| 51 | 'name' => 'placeholder', |
| 52 | ) ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Create the HTML interface for your field |
| 57 | * |
| 58 | * @param $field (array) the $field being edited |
| 59 | * |
| 60 | * @since 4.0.0 |
| 61 | */ |
| 62 | function render_field( $field ) { ?> |
| 63 | <input type="text" name="<?php echo esc_attr( $field['name'] ) ?>" |
| 64 | value="<?php echo esc_attr( $field['value'] ) ?>" class="date-picker" autocomplete="off" |
| 65 | placeholder="<?php echo esc_attr( $field['placeholder'] ) ?>"/> |
| 66 | <?php |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * This action is called in the admin_enqueue_scripts action on the edit screen where your field is created. |
| 71 | * Use this action to add CSS + JavaScript to assist your render_field() action. |
| 72 | * |
| 73 | * @since 4.0.0 |
| 74 | */ |
| 75 | function input_admin_enqueue_scripts() { |
| 76 | $pluginVersion = Assets::getVersion(); |
| 77 | $debugName = WP_PARSI_DEBUG_MODE ? '' : '.min'; |
| 78 | |
| 79 | if ( ! wp_script_is( 'wpp-jalali-datepicker' ) ) { |
| 80 | wp_enqueue_script( 'wpp_jalali_datepicker', Assets::url( 'js-admin/jalalidatepicker.min.js' ), |
| 81 | array( 'acf-input' ), $pluginVersion, [ 'in_footer' => true ] ); |
| 82 | wp_enqueue_style( 'wpp_jalali_datepicker', |
| 83 | Assets::url( 'css-admin/jalalidatepicker' . $debugName . '.css' ), |
| 84 | array( 'acf-input' ), $pluginVersion ); |
| 85 | |
| 86 | do_action( 'wpp_jalali_datepicker_enqueued', 'acf-5' ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * This filter is applied to the $value after it is loaded from the db |
| 92 | * |
| 93 | * @param $value (mixed) the value found in the database |
| 94 | * @param $post_id (mixed) the $post_id from which the value was loaded |
| 95 | * @param $field (array) the field array holding all the field options |
| 96 | * |
| 97 | * @return int|string $value |
| 98 | * @since 4.0.0 |
| 99 | */ |
| 100 | function load_value( $value, $post_id, $field ) { |
| 101 | if ( ! empty( $value ) && ! Settings::get( 'save_persian_date', false, 'acf' ) ) { |
| 102 | $value = parsidate( 'Y-m-d', $value, 'en' ); |
| 103 | } |
| 104 | |
| 105 | return apply_filters( 'wpp_acf_after_load_jalali_date', $value ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * This filter is applied to the $value before it is saved in the db |
| 110 | * |
| 111 | * @param $value (mixed) the value found in the database |
| 112 | * @param $post_id (mixed) the $post_id from which the value was loaded |
| 113 | * @param $field (array) the field array holding all the field options |
| 114 | * |
| 115 | * @return false|string $value |
| 116 | * @since 4.0.0 |
| 117 | */ |
| 118 | function update_value( $value, $post_id, $field ) { |
| 119 | if ( ! empty( $value ) && ! Settings::get( 'save_persian_date', false, 'acf' ) ) { |
| 120 | $value = gregdate( 'Y-m-d', $value ); |
| 121 | } |
| 122 | |
| 123 | return apply_filters( 'wpp_acf_before_update_jalali_date', $value ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * This filter is applied to the $value after it is loaded from the db and, before it is returned to the template |
| 128 | * |
| 129 | * @param $value (mixed) the value which was loaded from the database |
| 130 | * @param $post_id (mixed) the $post_id from which the value was loaded |
| 131 | * @param $field (array) the field array holding all the field options |
| 132 | * |
| 133 | * @return mixed $value (mixed) the modified value |
| 134 | * @since 4.0.0 |
| 135 | */ |
| 136 | function format_value( $value, $post_id, $field ) { |
| 137 | if ( empty( $value ) ) { |
| 138 | return $value; |
| 139 | } |
| 140 | |
| 141 | return apply_filters( 'wpp_acf_before_jalali_date_render', $value ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * This filter is used to perform validation on the value prior to saving. |
| 146 | * All values are validated regardless of the field's required setting. This allows you to validate and return |
| 147 | * messages to the user if the value is not correct |
| 148 | * |
| 149 | * @param $valid (boolean) validation status based on the value and the field's required setting |
| 150 | * @param $value (mixed) the $_POST value |
| 151 | * @param $field (array) the field array holding all the field options |
| 152 | * @param $input (string) the corresponding input name for $_POST value |
| 153 | * |
| 154 | * @return true|false |
| 155 | * @since 4.0.0 |
| 156 | * |
| 157 | */ |
| 158 | function validate_value( $valid, $value, $field, $input ) { |
| 159 | return apply_filters( 'wpp_acf_validate_date_value', $valid, $value, $field, $input ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * This filter is applied to the $field after it is loaded from the database |
| 164 | * |
| 165 | * @param $field (array) the field array holding all the field options |
| 166 | * |
| 167 | * @return mixed $field |
| 168 | * @since 4.0.0 |
| 169 | */ |
| 170 | function load_field( $field ) { |
| 171 | return apply_filters( 'wpp_acf_after_load_date_field', $field ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * This filter is applied to the $field before it is saved to the database |
| 176 | * |
| 177 | * @param $field (array) the field array holding all the field options |
| 178 | * |
| 179 | * @return mixed $field |
| 180 | * @since 4.0.0 |
| 181 | */ |
| 182 | function update_field( $field ) { |
| 183 | return apply_filters( 'wpp_acf_before_save_date_field', $field ); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | new WPP_acf_field_jalali_datepicker(); |
| 188 |