class-wpp-acf-datepicker-v4.php
1 month ago
class-wpp-acf-datepicker-v5.php
1 month ago
class-wpp-acf-timepicker-v4.php
1 month ago
class-wpp-acf-timepicker-v5.php
1 month ago
class-wpp-acf-datepicker-v4.php
154 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 | * Hooks required tags |
| 19 | */ |
| 20 | public function __construct() { |
| 21 | $this->name = 'jalali_datepicker'; |
| 22 | |
| 23 | $this->label = esc_html__( 'Date', 'wp-parsidate' ); |
| 24 | |
| 25 | $this->category = esc_html__( 'Parsidate', 'wp-parsidate' ); |
| 26 | |
| 27 | $this->defaults = array( |
| 28 | 'placeholder' => 'YYYY-MM-DD', |
| 29 | ); |
| 30 | |
| 31 | parent::__construct(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Create extra settings for your field. These are visible when editing a field |
| 36 | * |
| 37 | * @param $field (array) the $field being edited |
| 38 | * |
| 39 | * @since 4.0.0 |
| 40 | */ |
| 41 | public function create_options( $field ) { |
| 42 | $key = $field['name']; |
| 43 | ?> |
| 44 | <tr class="field_option field_option_<?php echo esc_html( $this->name ); ?>"> |
| 45 | <td class="label"> |
| 46 | <label><?php esc_html__( 'Placeholder', 'wp-parsidate' ); ?></label> |
| 47 | <p class="description"><?php esc_html_e( 'Show custom placeholder', 'wp-parsidate' ); ?></p> |
| 48 | </td> |
| 49 | <td> |
| 50 | <?php |
| 51 | do_action( 'acf/create_field', array( |
| 52 | 'type' => 'text', |
| 53 | 'name' => 'fields[' . $key . '][placeholder]', |
| 54 | 'value' => $field['placeholder'], |
| 55 | 'layout' => 'horizontal' |
| 56 | ) ); |
| 57 | ?> |
| 58 | </td> |
| 59 | </tr> |
| 60 | <?php |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Create the HTML interface for your field |
| 65 | * |
| 66 | * @param $field (array) the $field being edited |
| 67 | * |
| 68 | * @since 4.0.0 |
| 69 | */ |
| 70 | public function create_field( $field ) { |
| 71 | ?> |
| 72 | <div> |
| 73 | <input type="text" name="<?php echo esc_attr( $field['name'] ) ?>" |
| 74 | value="<?php echo esc_attr( $field['value'] ) ?>" class="date-picker" autocomplete="off" |
| 75 | placeholder="<?php echo esc_attr( $field['placeholder'] ) ?>"/> |
| 76 | </div> |
| 77 | <?php |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * This action is called in the admin_enqueue_scripts action on the edit screen where your field is created. |
| 82 | * Use this action to add CSS + JavaScript to assist your render_field() action. |
| 83 | * |
| 84 | * @since 4.0.0 |
| 85 | */ |
| 86 | public function input_admin_enqueue_scripts() { |
| 87 | $pluginVersion = Assets::getVersion(); |
| 88 | $debugName = WP_PARSI_DEBUG_MODE ? '' : '.min'; |
| 89 | |
| 90 | wp_enqueue_script( 'wpp_jalali_datepicker', Assets::url( 'js-admin/jalalidatepicker.min.js' ), |
| 91 | array( 'acf-input' ), $pluginVersion, [ 'in_footer' => true ] ); |
| 92 | wp_enqueue_style( 'wpp_jalali_datepicker', Assets::url( 'css-admin/jalalidatepicker' . $debugName . '.css' ), |
| 93 | array( 'acf-input' ), $pluginVersion ); |
| 94 | |
| 95 | do_action( 'wpp_jalali_datepicker_enqueued', 'acf-4' ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * This filter is applied to the $value after it is loaded from the db |
| 100 | * |
| 101 | * @param $value (mixed) the value found in the database |
| 102 | * @param $post_id (mixed) the $post_id from which the value was loaded |
| 103 | * @param $field (array) the field array holding all the field options |
| 104 | * |
| 105 | * @return int|string $value |
| 106 | * @since 4.0.0 |
| 107 | */ |
| 108 | public function load_value( $value, $post_id, $field ) { |
| 109 | if ( ! Settings::get( 'save_persian_date', false, 'acf' ) ) { |
| 110 | $value = parsidate( 'Y-m-d', $value ); |
| 111 | } |
| 112 | |
| 113 | return apply_filters( 'wpp_acf_after_load_jalali_date', $value ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * This filter is applied to the $value before it is saved in the db |
| 118 | * |
| 119 | * @param $value (mixed) the value found in the database |
| 120 | * @param $post_id (mixed) the $post_id from which the value was loaded |
| 121 | * @param $field (array) the field array holding all the field options |
| 122 | * |
| 123 | * @return false|string $value |
| 124 | * @since 4.0.0 |
| 125 | */ |
| 126 | public function update_value( $value, $post_id, $field ) { |
| 127 | if ( ! Settings::get( 'save_persian_date', false, 'acf' ) ) { |
| 128 | $value = gregdate( 'Y-m-d', $value ); |
| 129 | } |
| 130 | |
| 131 | return apply_filters( 'wpp_acf_before_update_jalali_date', $value ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * This filter is applied to the $value after it is loaded from the db and, before it is passed back to the API functions such as the_field |
| 136 | * |
| 137 | * @param $value - the value which was loaded from the database |
| 138 | * @param $post_id - the $post_id from which the value was loaded |
| 139 | * @param $field - the field array holding all the field options |
| 140 | * |
| 141 | * @return mixed|void $value - the modified value |
| 142 | * @since 4.0.0 |
| 143 | */ |
| 144 | public function format_value_for_api( $value, $post_id, $field ) { |
| 145 | if ( ! Settings::get( 'save_persian_date', false, 'acf' ) ) { |
| 146 | $value = parsidate( 'Y-m-d', $value ); |
| 147 | } |
| 148 | |
| 149 | return apply_filters( 'wpp_acf_after_load_jalali_date', $value ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | new WPP_acf_field_jalali_datepicker(); |
| 154 |