-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathelementor-datepicker-localization.php
211 lines (192 loc) · 8.16 KB
/
elementor-datepicker-localization.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Plugin Name: Elementor Datepicker Localization
* Plugin URI: https://github.com/creame/elementor-datepicker-localization/
* Description: Load current site locale for Elementor form datepicker.
* Version: 1.3.0
* Author: Creame
* Author URI: https://crea.me
*/
class ElementorDatepickerLocalization {
private $locale;
private $format;
private $time24;
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
$this->locale = apply_filters( 'elementor/datepicker/locale', $this->get_locale() );
$this->format = apply_filters( 'elementor/datepicker/format', 'Y-m-d' );
$this->time24 = apply_filters( 'elementor/datepicker/24h', false ) ? 'true' : 'false';
if ( 'default' !== $this->locale ) {
// Register script
add_action( 'wp_enqueue_scripts', array( $this, 'script_register' ) );
// Enqueue if date field is present
add_filter( 'elementor_pro/forms/render/item/date', array( $this, 'script_enqueue' ) );
}
// Apply locale and format
add_action( 'wp_footer', array( $this, 'datepicker_settings' ), 99 );
if ( 'Y-m-d' !== $this->format ) {
// Change native date output format
add_action( 'elementor_pro/forms/process/date', array( $this, 'change_date' ), 10, 2 );
}
}
public function script_register() {
wp_register_script( 'flatpickr_localize', "https://npmcdn.com/flatpickr/dist/l10n/{$this->locale}.js", array( 'flatpickr' ) );
}
public function script_enqueue( $item ) {
if ( ! isset( $item['use_native_date'] ) || 'yes' !== $item['use_native_date'] ) {
wp_enqueue_script( 'flatpickr_localize' );
remove_filter( 'elementor_pro/forms/render/item/date', array( $this, 'script_enqueue' ) );
}
return $item;
}
public function datepicker_settings() {
if ( wp_script_is( 'flatpickr', 'enqueued' ) ) {
$lang = wp_script_is( 'flatpickr_localize', 'enqueued' ) ? str_replace( '-', '_', $this->locale ) : '';
echo '<script>' .
"flatpickr.setDefaults({dateFormat:'$this->format', time_24hr:$this->time24}); " .
( $lang ? "flatpickr.localize(flatpickr.l10ns.$lang); " : '' ) .
( 'Y-m-d' !== $this->format ? "jQuery('.elementor-date-field').removeAttr('pattern');" : '' ) .
'</script>';
}
}
public function get_locale() {
// Relation WordPress languages with flatpickr languages
$locales = array(
'af' => '', // 'Afrikaans'
'ar' => 'ar', // 'Arabic'
'ary' => 'ar', // 'Moroccan Arabic'
'as' => '', // 'Assamese'
'azb' => 'az', // 'South Azerbaijani'
'az' => 'az', // 'Azerbaijani'
'bel' => 'be', // 'Belarusian'
'bg_BG' => 'bg', // 'Bulgarian'
'bn_BD' => 'bn', // 'Bengali (Bangladesh)'
'bo' => '', // 'Tibetan'
'bs_BA' => 'bs', // 'Bosnian'
'ca' => 'cat', // 'Catalan'
'ceb' => '', // 'Cebuano'
'cs_CZ' => 'cs', // 'Czech'
'cy' => 'cy', // 'Welsh'
'da_DK' => 'da', // 'Danish'
'de_CH_informal' => 'de', // 'German (Switzerland, Informal)'
'de_CH' => 'de', // 'German (Switzerland)'
'de_DE' => 'de', // 'German'
'de_DE_formal' => 'de', // 'German (Formal)'
'de_AT' => 'de', // 'German (Austria)'
'dzo' => '', // 'Dzongkha'
'el' => 'gr', // 'Greek'
'en_GB' => 'en', // 'English (UK)'
'en_AU' => 'en', // 'English (Australia)'
'en_CA' => 'en', // 'English (Canada)'
'en_ZA' => 'en', // 'English (South Africa)'
'en_NZ' => 'en', // 'English (New Zealand)'
'eo' => 'eo', // 'Esperanto'
'es_CL' => 'es', // 'Spanish (Chile)'
'es_ES' => 'es', // 'Spanish (Spain)'
'es_MX' => 'es', // 'Spanish (Mexico)'
'es_GT' => 'es', // 'Spanish (Guatemala)'
'es_CR' => 'es', // 'Spanish (Costa Rica)'
'es_CO' => 'es', // 'Spanish (Colombia)'
'es_PE' => 'es', // 'Spanish (Peru)'
'es_VE' => 'es', // 'Spanish (Venezuela)'
'es_AR' => 'es', // 'Spanish (Argentina)'
'et' => 'et', // 'Estonian'
'eu' => 'es', // 'Basque'
'fa_IR' => 'fa', // 'Persian'
'fi' => 'fi', // 'Finnish'
'fr_CA' => 'fr', // 'French (Canada)'
'fr_FR' => 'fr', // 'French (France)'
'fr_BE' => 'fr', // 'French (Belgium)'
'fur' => '', // 'Friulian'
'gd' => 'ga', // 'Scottish Gaelic'
'gl_ES' => 'es', // 'Galician'
'gu' => '', // 'Gujarati'
'haz' => '', // 'Hazaragi'
'he_IL' => 'he', // 'Hebrew'
'hi_IN' => 'hi', // 'Hindi'
'hr' => 'hr', // 'Croatian'
'hsb' => '', // 'Upper Sorbian'
'hu_HU' => 'hu', // 'Hungarian'
'hy' => '', // 'Armenian'
'id_ID' => 'id', // 'Indonesian'
'is_IS' => 'is', // 'Icelandic'
'it_IT' => 'it', // 'Italian'
'ja' => 'ja', // 'Japanese'
'jv_ID' => '', // 'Javanese'
'ka_GE' => 'ka', // 'Georgian'
'kab' => '', // 'Kabyle'
'kk' => 'kz', // 'Kazakh'
'km' => 'km', // 'Khmer'
'kn' => '', // 'Kannada'
'ko_KR' => 'ko', // 'Korean'
'ckb' => '', // 'Kurdish (Sorani)'
'lo' => '', // 'Lao'
'lt_LT' => 'lt', // 'Lithuanian'
'lv' => 'lv', // 'Latvian'
'mk_MK' => 'mk', // 'Macedonian'
'ml_IN' => '', // 'Malayalam'
'mn' => 'mn', // 'Mongolian'
'mr' => '', // 'Marathi'
'ms_MY' => 'ms', // 'Malay'
'my_MM' => 'my', // 'Myanmar (Burmese)'
'nb_NO' => 'no', // 'Norwegian (Bokmål)'
'ne_NP' => '', // 'Nepali'
'nl_NL' => 'nl', // 'Dutch'
'nl_NL_formal' => 'nl', // 'Dutch (Formal)'
'nl_BE' => 'nl', // 'Dutch (Belgium)'
'nn_NO' => 'no', // 'Norwegian (Nynorsk)'
'oci' => '', // 'Occitan'
'pa_IN' => 'pa', // 'Punjabi'
'pl_PL' => 'pl', // 'Polish'
'ps' => '', // 'Pashto'
'pt_BR' => 'pt', // 'Portuguese (Brazil)'
'pt_AO' => 'pt', // 'Portuguese (Angola)'
'pt_PT' => 'pt', // 'Portuguese (Portugal)'
'pt_PT_ao90' => 'pt', // 'Portuguese (Portugal, AO90)'
'rhg' => '', // 'Rohingya'
'ro_RO' => 'ro', // 'Romanian'
'ru_RU' => 'ru', // 'Russian'
'sah' => '', // 'Sakha'
'si_LK' => 'si', // 'Sinhala'
'sk_SK' => 'sk', // 'Slovak'
'skr' => '', // 'Saraiki'
'sl_SI' => 'sl', // 'Slovenian'
'sq' => 'sq', // 'Albanian'
'sr_RS' => 'sr', // 'Serbian'
'sv_SE' => 'sv', // 'Swedish'
'sw' => '', // 'Swahili'
'szl' => '', // 'Silesian'
'ta_IN' => '', // 'Tamil'
'te' => '', // 'Telugu'
'th' => 'th', // 'Thai'
'tl' => '', // 'Tagalog'
'tr_TR' => 'tr', // 'Turkish'
'tt_RU' => '', // 'Tatar'
'tah' => '', // 'Tahitian'
'ug_CN' => '', // 'Uighur'
'uk' => 'uk', // 'Ukrainian'
'ur' => '', // 'Urdu'
'uz_UZ' => '', // 'Uzbek'
'vi' => 'vn', // 'Vietnamese'
'zh_HK' => 'zh', // 'Chinese (Hong Kong)'
'zh_TW' => 'zh-tw', // 'Chinese (Taiwan)'
'zh_CN' => 'zh', // 'Chinese (China)'
);
$wp_locale = get_locale();
$locale = array_key_exists( $wp_locale, $locales ) ? $locales[ $wp_locale ] : 'default';
// English or none use default (en) lang
if ( $locale === 'en' || $locale === '' ) {
$locale = 'default';
}
return $locale;
}
public function change_date( $field, $record ) {
$unix = strtotime( str_replace( '/', '-', $field['raw_value'] ) );
if ($unix) {
$record->update_field( $field['id'], 'value', date( $this->format, $unix ) );
}
}
}
$elementor_datepickr_localization = new ElementorDatepickerLocalization();