ACF
3 weeks ago
WooCommerce
3 weeks ago
ACF.php
3 weeks ago
BulkyBulkEditProductsWooCommerce.php
3 weeks ago
EDD.php
3 weeks ago
Elementor.php
3 weeks ago
Formello.php
3 weeks ago
Integration.php
3 weeks ago
JetEngine.php
3 weeks ago
LimitLoginAttempts.php
3 weeks ago
MonsterInsights.php
3 weeks ago
RankMath.php
3 weeks ago
SchemaPro.php
3 weeks ago
UltimateMember.php
3 weeks ago
WooCommerce.php
3 weeks ago
ACF.php
192 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Makes ACF compatible with WP-Parsidate plugin |
| 4 | * |
| 5 | * @package WP-Parsidate |
| 6 | * @subpackage Plugins/ACF |
| 7 | * @since 4.0.0 |
| 8 | * @author Morteza Gransayeh |
| 9 | */ |
| 10 | |
| 11 | namespace WPParsidate\App\Integration; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | use WPParsidate\Addons\Addon; |
| 16 | use WPParsidate\Helper\Date; |
| 17 | |
| 18 | class ACF extends Addon { |
| 19 | public string $addonID = 'acf'; |
| 20 | public string $currentTab = 'integration'; |
| 21 | |
| 22 | public function initM1Action(): void { |
| 23 | if ( $this->getSetting( 'fix_date', false ) ) { |
| 24 | add_action( 'acf/include_field_types', [ $this, 'includeField' ] ); // v5 |
| 25 | add_action( 'acf/register_fields', [ $this, 'includeField' ] ); // v4 |
| 26 | |
| 27 | add_action( 'acf/render_field_settings', [ $this, 'addOptionToDatePickerSettings' ] ); |
| 28 | add_filter( 'acf/update_value', [ $this, 'updateDatePickerValue' ], 10, 3 ); |
| 29 | add_filter( 'acf/load_value', [ $this, 'loadDatePickerValue' ], 10, 3 ); |
| 30 | add_filter( 'acf/load_field', [ $this, 'loadDatePickerField' ] ); |
| 31 | add_filter( 'acf/pre_format_value', [ $this, 'formatDatePickerValue' ], 10, 5 ); |
| 32 | add_action( 'admin_enqueue_scripts', [ $this, 'fixDatePickerScript' ], 99999 ); |
| 33 | } |
| 34 | |
| 35 | add_filter( 'wp_parsidate_hook_deactivator_raw_list', [ $this, 'addDateHooksToDeactivator' ] ); |
| 36 | } |
| 37 | |
| 38 | public function addDateHooksToDeactivator( $rawList ): string { |
| 39 | $rawList .= "\nwp_date,acf_format_date\nwp_date,render_field,acf_field_date_picker\ndate_i18n,acf_format_date\ndate_i18n,render_field,acf_field_date_picker"; |
| 40 | |
| 41 | return $rawList; |
| 42 | } |
| 43 | |
| 44 | public function fixDatePickerScript(): void { |
| 45 | wp_add_inline_script( 'wpp_jalali_datepicker', "document.addEventListener('DOMContentLoaded', function () { |
| 46 | setTimeout(function () { |
| 47 | function wppdAcfJalaliDatePickerMirror() { |
| 48 | jQuery('.acf-date-picker input.hasDatepicker').off('keyup change','**').on('keyup change', function () { |
| 49 | let acfDatePickerParent = jQuery(this).closest('div.acf-date-picker'); |
| 50 | if (acfDatePickerParent.length) |
| 51 | acfDatePickerParent.children('input[type=\"hidden\"]').val(jQuery(this).val().replaceAll('-', '')); |
| 52 | }); |
| 53 | } |
| 54 | wppdAcfJalaliDatePickerMirror(); |
| 55 | jQuery('.acf-button.acf-repeater-add-row').on('click', function () { |
| 56 | setTimeout(function () {wppdAcfJalaliDatePickerMirror()}, 1000); |
| 57 | }); |
| 58 | }, 2000); |
| 59 | });" ); |
| 60 | } |
| 61 | |
| 62 | public function addOptionToDatePickerSettings( $field ): void { |
| 63 | if ( $field['type'] === 'date_picker' ) { |
| 64 | acf_render_field_setting( $field, array( |
| 65 | 'label' => esc_html__( 'Convert to Shamsi Date', 'wp-parsidate' ), |
| 66 | 'instructions' => '', |
| 67 | 'name' => 'jalali_date', |
| 68 | 'type' => 'true_false', |
| 69 | 'ui' => 1, |
| 70 | ) ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public function formatDatePickerValue( $currentValue, $value, $postID, $field, $escapeHTML ): ?string { |
| 75 | if ( $field['type'] === 'date_picker' && $field['jalali_date'] && ! is_admin() ) { |
| 76 | return $escapeHTML ? esc_html( $escapeHTML ) : $value; |
| 77 | } |
| 78 | |
| 79 | return $currentValue; |
| 80 | } |
| 81 | |
| 82 | public function loadDatePickerField( $field ) { |
| 83 | if ( $field['type'] === 'date_picker' && is_admin() ) { |
| 84 | $field['display_format'] = 'Y-m-d'; |
| 85 | } |
| 86 | |
| 87 | return $field; |
| 88 | } |
| 89 | |
| 90 | public function loadDatePickerValue( $value, $postID, $field ) { |
| 91 | if ( $field['type'] === 'date_picker' && ! empty( $value ) ) { |
| 92 | if ( ! $field['jalali_date'] && ! is_admin() ) { |
| 93 | return $value; |
| 94 | } |
| 95 | |
| 96 | $value = Date::changeDateFormat( $value, 'Ymd', 'Y-m-d' ); |
| 97 | $value = parsidate( is_admin() ? 'Y-m-d' : $field['return_format'], $value, ! is_admin() ); |
| 98 | } |
| 99 | |
| 100 | return $value; |
| 101 | } |
| 102 | |
| 103 | public function updateDatePickerValue( $value, $postID, $field ) { |
| 104 | if ( $field['type'] === 'date_picker' && ! empty( $value ) ) { |
| 105 | if ( is_numeric( $value ) && strlen( $value ) === 8 ) { |
| 106 | $year = substr( $value, 0, 4 ); |
| 107 | $month = substr( $value, 4, 2 ); |
| 108 | $day = substr( $value, 6, 2 ); |
| 109 | |
| 110 | $value = "$year-$month-$day"; |
| 111 | } |
| 112 | |
| 113 | $value = gregdate( 'Ymd', $value ); |
| 114 | } |
| 115 | |
| 116 | return $value; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * This function will include the field type class |
| 121 | * |
| 122 | * @param $version (int) major ACF version. Defaults to false |
| 123 | * |
| 124 | * @return void |
| 125 | * @since 4.0.0 |
| 126 | */ |
| 127 | public function includeField( $version = false ): void { |
| 128 | $version = $version ? (int) $version : 4; |
| 129 | |
| 130 | include_once( 'ACF/class-wpp-acf-datepicker-v' . (float) $version . '.php' ); |
| 131 | //include_once( 'ACF/class-wpp-acf-timepicker-v' . (float) $version . '.php' ); |
| 132 | } |
| 133 | |
| 134 | public function addSectionSettings( $sections ) { |
| 135 | $sections[ $this->addonID ] = array( |
| 136 | 'title' => esc_html__( 'Advanced Custom Fields', 'wp-parsidate' ), |
| 137 | 'desc' => esc_html__( 'ParsiDate integration for Advanced Custom Fields (ACF)', 'wp-parsidate' ), |
| 138 | 'settings_key' => $this->addonID, |
| 139 | 'settings' => [ |
| 140 | 'acf_start_grid' => array( |
| 141 | 'id' => 'edd_start_grid', |
| 142 | 'title' => esc_html__( 'Advanced Custom Fields', 'wp-parsidate' ), |
| 143 | 'type' => 'startGrid', |
| 144 | ), |
| 145 | 'fix_date' => array( |
| 146 | 'id' => 'fix_date', |
| 147 | 'title' => esc_html__( 'Jalali Datepicker', 'wp-parsidate' ), |
| 148 | 'type' => 'toggle', |
| 149 | 'default' => false, |
| 150 | 'sanitize' => 'bool' |
| 151 | ), |
| 152 | 'save_persian_date' => array( |
| 153 | 'id' => 'save_persian_date', |
| 154 | 'title' => esc_html__( 'Save dates in Jalali format (Not recommended)', 'wp-parsidate' ), |
| 155 | 'type' => 'toggle', |
| 156 | 'default' => false, |
| 157 | 'sanitize' => 'bool' |
| 158 | ), |
| 159 | 'acf_end_grid' => array( |
| 160 | 'type' => 'endGrid', |
| 161 | ) |
| 162 | ] |
| 163 | ); |
| 164 | |
| 165 | return $sections; |
| 166 | } |
| 167 | |
| 168 | public function info(): array { |
| 169 | $svg = '<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 256 256"><path fill="url(#a)" d="M56.96 7.041 7.04 56.97A23.95 23.95 0 0 0 0 73.932v170.066C0 250.639 5.36 256 12 256h232c6.64 0 12-5.361 12-12.002V12.002C256 5.36 250.64 0 244 0H73.96C67.6 0 61.48 2.52 57 7.041z"/><g filter="url(#b)"><path fill="#fff" d="M181.129 168.663h-18.533V94.079h49.403v17.368h-30.87v13.309h29.096v16.936h-29.096v26.974z"/><path fill="#002447" d="M157.707 137.362h18.37c-2.718 18.983-18.677 31.538-38.011 31.538-21.172 0-38.41-15.871-38.41-37.243a36.8 36.8 0 0 1 2.831-14.483 36.7 36.7 0 0 1 8.294-12.19A38.47 38.47 0 0 1 138.066 94.1c19.155 0 35.56 12.652 37.892 31.331h-18.351c-5.518-21.159-39.9-19.09-39.9 6.226 0 25.318 34.813 27.183 40 5.711z" opacity=".05"/><path fill="#fff" d="M153.948 137.362c-3.152 10.68-14.369 17.03-25.41 14.371-11.048-2.669-17.97-13.379-15.676-24.266 2.29-10.89 12.973-18.065 24.196-16.248a20.03 20.03 0 0 1 11.997 6.237 20.56 20.56 0 0 1 4.79 7.972h17.814c-2.332-18.722-18.782-31.328-37.892-31.328a38.47 38.47 0 0 0-27.291 10.878 36.7 36.7 0 0 0-8.299 12.195 36.8 36.8 0 0 0-2.83 14.49c0 21.372 17.138 37.237 38.426 37.237 19.319 0 35.232-12.555 37.999-31.538h-17.827z"/><path fill="#002447" d="M98.588 157.812H72.796L68.58 168.65H48.842L79.048 94h13.164l31.407 74.675H102.78l-4.198-10.863zm-18.46-18.478-.647 1.685h12.522l-.434-1.265-5.83-16.008z" opacity=".05"/><path fill="#fff" d="M93.704 157.812h-25.75l-4.213 10.838H44L74.209 94h13.164l31.407 74.675H97.95zm-18.418-18.478-.644 1.685h12.522l-.437-1.265-5.827-16.008-5.611 15.588z"/></g><defs><radialGradient id="a" cx="0" cy="0" r="1" gradientTransform="rotate(45)scale(362.039)" gradientUnits="userSpaceOnUse"><stop stop-color="#0ecad4"/><stop offset="1" stop-color="#006bd6"/></radialGradient><filter id="b" width="183.999" height="90.9" x="36" y="86" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feColorMatrix in="SourceAlpha" result="hardAlpha" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0"/><feOffset/><feGaussianBlur stdDeviation="4"/><feColorMatrix values="0 0 0 0 0 0 0 0 0 0.141176 0 0 0 0 0.278431 0 0 0 0.1 0"/><feBlend in2="BackgroundImageFix" result="effect1_dropShadow_84_8417"/><feBlend in="SourceGraphic" in2="effect1_dropShadow_84_8417" result="shape"/></filter></defs></svg>'; |
| 170 | |
| 171 | return array( |
| 172 | 'id' => $this->addonID, |
| 173 | 'title' => esc_html__( 'Advanced Custom Fields', 'wp-parsidate' ), |
| 174 | 'desc' => esc_html__( 'ParsiDate integration for Advanced Custom Fields (ACF)', 'wp-parsidate' ), |
| 175 | 'force_enable' => true, |
| 176 | 'icon' => $svg, |
| 177 | 'image_link' => 'https://wordpress.org/plugins/advanced-custom-fields/', |
| 178 | 'tags' => [ esc_html__( 'Meta', 'wp-parsidate' ) ], |
| 179 | 'cat' => 'integration', |
| 180 | 'settings_key' => $this->addonID, |
| 181 | 'requires_plugins' => [ |
| 182 | 'advanced-custom-fields/acf.php' => array( |
| 183 | 'is_wp_plugin' => true, |
| 184 | 'is_free' => true, |
| 185 | 'plugin_link' => 'https://wordpress.org/plugins/advanced-custom-fields/', |
| 186 | 'class_check' => 'ACF' |
| 187 | ) |
| 188 | ] |
| 189 | ); |
| 190 | } |
| 191 | } |
| 192 |