Skip to content

Commit 872a6bc

Browse files
Add city drop-down to WooCommerce addresses:
#138 Solved: Wrong behavior in sale price dates | #142
1 parent e813177 commit 872a6bc

33 files changed

+39211
-480
lines changed

assets/js/city-select.js

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
jQuery( function($) {
2+
3+
// wc_city_select_params is required to continue, ensure the object exists
4+
// wc_country_select_params is used for select2 texts. This one is added by WC
5+
if ( typeof wc_country_select_params === 'undefined' || typeof wc_city_select_params === 'undefined' ) {
6+
return false;
7+
}
8+
9+
function getEnhancedSelectFormatString() {
10+
var formatString = {
11+
formatMatches: function( matches ) {
12+
if ( 1 === matches ) {
13+
return wc_country_select_params.i18n_matches_1;
14+
}
15+
16+
return wc_country_select_params.i18n_matches_n.replace( '%qty%', matches );
17+
},
18+
formatNoMatches: function() {
19+
return wc_country_select_params.i18n_no_matches;
20+
},
21+
formatAjaxError: function() {
22+
return wc_country_select_params.i18n_ajax_error;
23+
},
24+
formatInputTooShort: function( input, min ) {
25+
var number = min - input.length;
26+
27+
if ( 1 === number ) {
28+
return wc_country_select_params.i18n_input_too_short_1;
29+
}
30+
31+
return wc_country_select_params.i18n_input_too_short_n.replace( '%qty%', number );
32+
},
33+
formatInputTooLong: function( input, max ) {
34+
var number = input.length - max;
35+
36+
if ( 1 === number ) {
37+
return wc_country_select_params.i18n_input_too_long_1;
38+
}
39+
40+
return wc_country_select_params.i18n_input_too_long_n.replace( '%qty%', number );
41+
},
42+
formatSelectionTooBig: function( limit ) {
43+
if ( 1 === limit ) {
44+
return wc_country_select_params.i18n_selection_too_long_1;
45+
}
46+
47+
return wc_country_select_params.i18n_selection_too_long_n.replace( '%qty%', limit );
48+
},
49+
formatLoadMore: function() {
50+
return wc_country_select_params.i18n_load_more;
51+
},
52+
formatSearching: function() {
53+
return wc_country_select_params.i18n_searching;
54+
}
55+
};
56+
57+
return formatString;
58+
}
59+
60+
// Select2 Enhancement if it exists
61+
if ( $().select2 ) {
62+
var wc_city_select_select2 = function() {
63+
$( 'select.city_select:visible' ).each( function() {
64+
var select2_args = $.extend({
65+
placeholderOption: 'first',
66+
width: '100%'
67+
}, getEnhancedSelectFormatString() );
68+
69+
$( this ).select2( select2_args );
70+
});
71+
};
72+
73+
wc_city_select_select2();
74+
75+
$( document.body ).bind( 'city_to_select', function() {
76+
wc_city_select_select2();
77+
});
78+
}
79+
80+
/* City select boxes */
81+
var cities_json = wc_city_select_params.cities.replace( /"/g, '"' );
82+
var cities = $.parseJSON( cities_json );
83+
84+
$( 'body' ).on( 'country_to_state_changing', function(e, country, $container) {
85+
var $statebox = $container.find( '#billing_state, #shipping_state, #calc_shipping_state' );
86+
var state = $statebox.val();
87+
$( document.body ).trigger( 'state_changing', [country, state, $container ] );
88+
});
89+
90+
$( 'body' ).on( 'change', 'select.state_select, #calc_shipping_state', function() {
91+
var $container = $( this ).closest( 'div' );
92+
var country = $container.find( '#billing_country, #shipping_country, #calc_shipping_country' ).val();
93+
var state = $( this ).val();
94+
95+
$( document.body ).trigger( 'state_changing', [country, state, $container ] );
96+
});
97+
98+
$( 'body' ).on( 'state_changing', function(e, country, state, $container) {
99+
var $citybox = $container.find( '#billing_city, #shipping_city, #calc_shipping_city' );
100+
101+
if ( cities[ country ] ) {
102+
/* if the country has no states */
103+
if( cities[country] instanceof Array) {
104+
cityToSelect( $citybox, cities[ country ] );
105+
} else if ( state ) {
106+
if ( cities[ country ][ state ] ) {
107+
cityToSelect( $citybox, cities[ country ][ state ] );
108+
} else {
109+
cityToInput( $citybox );
110+
}
111+
} else {
112+
disableCity( $citybox );
113+
}
114+
} else {
115+
cityToInput( $citybox );
116+
}
117+
});
118+
119+
/* Ajax replaces .cart_totals (child of .cart-collaterals) on shipping calculator */
120+
if ( $( '.cart-collaterals' ).length && $( '#calc_shipping_state' ).length ) {
121+
var calc_observer = new MutationObserver( function() {
122+
$( '#calc_shipping_state' ).change();
123+
});
124+
calc_observer.observe( document.querySelector( '.cart-collaterals' ), { childList: true });
125+
}
126+
127+
function cityToInput( $citybox ) {
128+
if ( $citybox.is('input') ) {
129+
$citybox.prop( 'disabled', false );
130+
return;
131+
}
132+
133+
var input_name = $citybox.attr( 'name' );
134+
var input_id = $citybox.attr( 'id' );
135+
var placeholder = $citybox.attr( 'placeholder' );
136+
137+
$citybox.parent().find( '.select2-container' ).remove();
138+
139+
$citybox.replaceWith( '<input type="text" class="input-text" name="' + input_name + '" id="' + input_id + '" placeholder="' + placeholder + '" />' );
140+
}
141+
142+
function disableCity( $citybox ) {
143+
$citybox.val( '' ).change();
144+
$citybox.prop( 'disabled', true );
145+
}
146+
147+
function cityToSelect( $citybox, current_cities ) {
148+
var value = $citybox.val();
149+
150+
if ( $citybox.is('input') ) {
151+
var input_name = $citybox.attr( 'name' );
152+
var input_id = $citybox.attr( 'id' );
153+
var placeholder = $citybox.attr( 'placeholder' );
154+
155+
$citybox.replaceWith( '<select name="' + input_name + '" id="' + input_id + '" class="city_select" placeholder="' + placeholder + '"></select>' );
156+
//we have to assign the new object, because of replaceWith
157+
$citybox = $('#'+input_id);
158+
} else {
159+
$citybox.prop( 'disabled', false );
160+
}
161+
162+
var options = '';
163+
for( var index in current_cities ) {
164+
if ( current_cities.hasOwnProperty( index ) ) {
165+
var cityName = current_cities[ index ];
166+
options = options + '<option value="' + cityName + '">' + cityName + '</option>';
167+
}
168+
}
169+
170+
$citybox.html( '<option value="">' + wc_city_select_params.i18n_select_city_text + '</option>' + options );
171+
172+
if ( $('option[value="'+value+'"]', $citybox).length ) {
173+
$citybox.val( value ).change();
174+
} else {
175+
$citybox.val( '' ).change();
176+
}
177+
178+
$( document.body ).trigger( 'city_to_select' );
179+
}
180+
});

includes/admin/gutenberg-jalali-calendar.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
*/
3030
if ( ! function_exists( 'wpp_gutenberg_jalali_calendar_editor_assets' ) ) {
3131
function wpp_gutenberg_jalali_calendar_editor_assets() {
32-
wp_enqueue_script(
33-
'wpp_gutenberg_jalali_calendar_editor_scripts',
32+
wp_enqueue_script( 'wpp_gutenberg_jalali_calendar_editor_scripts',
3433
WP_PARSI_URL . 'assets/js/gutenberg-jalali-calendar.build.js',
3534
array(
3635
'wp-plugins',

includes/admin/lists-fix.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ function wpp_enqueue_admin_scripts() {
2020
global $wpp_months_name;
2121

2222
wp_enqueue_script( 'wpp_admin', WP_PARSI_URL . 'assets/js/admin.js', false, WP_PARSI_VER );
23-
wp_localize_script( 'wpp_admin', 'WPP_I18N',
23+
wp_localize_script(
24+
'wpp_admin',
25+
'WPP_I18N',
2426
array(
2527
'months' => $wpp_months_name
2628
)
@@ -78,10 +80,12 @@ function wpp_restrict_posts() {
7880
$post_status_w .= " AND post_status <> 'trash'";
7981
}
8082

81-
$sql = "SELECT DISTINCT date( post_date ) AS date
83+
$sql = "
84+
SELECT DISTINCT date( post_date ) AS date
8285
FROM $wpdb->posts
8386
WHERE post_type='$post_type' $post_status_w AND date( post_date ) <> '0000-00-00'
84-
ORDER BY post_date";
87+
ORDER BY post_date
88+
";
8589

8690
$list = $wpdb->get_col( $sql );
8791

includes/admin/styles-fix.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ function wpp_fix_editor_rtl() {
3232
* @since 2.0
3333
*/
3434
function wpp_fix_tinymce_font() {
35+
if( wpp_is_active( 'enable_fonts' ) ){
3536
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || wpp_is_active( 'dev_mode' ) ? '' : '.min';
36-
3737
add_editor_style( WP_PARSI_URL . "assets/css/editor$suffix.css" );
38+
}
3839
}
3940

40-
add_filter( 'init', 'wpp_fix_tinymce_font', 9 );
41+
add_filter( 'init', 'wpp_fix_tinymce_font', 9 );

includes/fixes-permalinks.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,19 @@ function wpp_pre_get_posts( $query ) {
248248
}
249249

250250
if ( $out ) {
251+
if ( ! isset( $var ) ) {
252+
return $query;
253+
}
254+
251255
preg_match_all( '!\d+!', $var, $matches );
252256

253257
$var = $matches[0];
254258

255-
$query->set( 'year', $var[0] );
256-
$query->set( 'monthnum', $var[1] );
257-
$query->set( 'day', $var[2] );
259+
if ( !empty($var) ) {
260+
$query->set('year', $var[0]);
261+
$query->set('monthnum', $var[1]);
262+
$query->set('day', $var[2]);
263+
}
258264

259265
$query->is_404 = false;
260266
$query->query_vars['error'] = '';

includes/general.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ function fix_number( $content ) {
187187
* @return array|string|string[]
188188
*/
189189
function fix_arabic( $content ) {
190-
return str_replace( array( 'ي', 'ك', '٤', '٥', '٦', 'ة' ), array( 'ی', 'ک', '۴', '۵', '۶', 'ه' ), $content );
190+
return str_replace( array( 'ي', 'ك', 'ة', '٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'), array('ی', 'ک', 'ه', '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'), $content );
191191
}
192192

193193
/**
@@ -225,4 +225,4 @@ function parsidate_check_format( $format ) {
225225
'Y-m-d\TH:i:s+00:00', // eq `DATE_W3C` @SEE: http://jochenhebbrecht.be/site/node/761
226226
'Y-m-d\TH:i:sP',
227227
) );
228-
}
228+
}

includes/install.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ function wpp_install() {
2020
update_option( 'wpp_settings', array() );
2121
}
2222

23-
register_activation_hook( WP_PARSI_ROOT, 'wpp_install' );
23+
register_activation_hook( WP_PARSI_ROOT, 'wpp_install' );

includes/parsidate.php

+7-29
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
* @package WP-Parsidate
1313
* @subpackage DateConversation
1414
*/
15-
1615
class WPP_ParsiDate {
1716
protected static $instance;
1817

19-
public $sesson = array( 'بهار', 'تابستان', 'پاییز', 'زمستان' );
18+
public $sessions = array( 'بهار', 'تابستان', 'پاییز', 'زمستان' );
2019

2120
public $persian_day_names = array( 'یکشنبه', 'دوشنبه', 'سه شنبه', 'چهارشنبه', 'پنجشنبه', 'جمعه', 'شنبه' );
2221
public $persian_day_small = array( 'ی', 'د', 'س', 'چ', 'پ', 'ج', 'ش' );
@@ -41,25 +40,6 @@ function __construct() {
4140
private function setup_vars() {
4241
global $wpp_months_name;
4342
switch ( wpp_get_option( 'months_name_type' ) ) {
44-
case 'persian':
45-
case '':
46-
case null:
47-
$wpp_months_name = apply_filters( 'wpp_name_of_months', array(
48-
'',
49-
'فروردین',
50-
'اردیبهشت',
51-
'خرداد',
52-
'تیر',
53-
'مرداد',
54-
'شهریور',
55-
'مهر',
56-
'آبان',
57-
'آذر',
58-
'دی',
59-
'بهمن',
60-
'اسفند'
61-
), 'persian' );
62-
break;
6343
case 'dari':
6444
$wpp_months_name = apply_filters( 'wpp_name_of_months', array(
6545
'',
@@ -227,28 +207,26 @@ public function persian_date( $format, $date = 'now', $lang = 'per' ) {
227207
$mon = $date['mon'];
228208
switch ( $mon ) {
229209
case( $mon < 4 ):
230-
$out .= $this->sesson[0];
210+
$out .= $this->sessions[0];
231211
break;
232212
case( $mon < 7 ):
233-
$out .= $this->sesson[1];
213+
$out .= $this->sessions[1];
234214
break;
235215
case( $mon < 10 ):
236-
$out .= $this->sesson[2];
216+
$out .= $this->sessions[2];
237217
break;
238218
case( $mon > 9 ):
239-
$out .= $this->sesson[3];
219+
$out .= $this->sessions[3];
240220
break;
241221
}
242222
break;
223+
case 'M':
243224
case'F':
244225
$out .= $wpp_months_name[ $date['mon'] ];
245226
break;
246227
case'm':
247228
$out .= ( $date['mon'] < 10 ) ? '0' . $date['mon'] : $date['mon'];
248229
break;
249-
case'M':
250-
$out .= $wpp_months_name[ $date['mon'] ];
251-
break;
252230
case'n':
253231
$out .= $date['mon'];
254232
break;
@@ -510,4 +488,4 @@ function gregdate( $input, $datetime ) {
510488
$bndate = WPP_ParsiDate::getInstance();
511489

512490
return $bndate->gregorian_date( $input, $datetime );
513-
}
491+
}

includes/plugins/acf-fields/class-wpp-acf-datepicker-v4.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ function input_admin_enqueue_scripts() {
8787

8888
wp_enqueue_script( 'wpp_jalali_datepicker', WP_PARSI_URL . 'assets/js/jalalidatepicker.min.js', array( 'acf-input' ), WP_PARSI_VER );
8989
wp_enqueue_style( 'wpp_jalali_datepicker', WP_PARSI_URL . "assets/css/jalalidatepicker$suffix.css", array( 'acf-input' ), WP_PARSI_VER );
90-
91-
do_action( 'wpp_jalai_datepicker_enqueued', 'acf-4' );
90+
91+
do_action( 'wpp_jalai_datepicker_enqueued', 'acf-4' );
9292
}
9393

9494
/**

includes/plugins/acf-fields/class-wpp-acf-datepicker-v5.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ function input_admin_enqueue_scripts() {
9393
* @since 4.0.0
9494
*/
9595
function load_value( $value, $post_id, $field ) {
96-
if ( ! wpp_is_active( 'acf_persian_date' ) ) {
97-
$value = parsidate( 'Y-m-d', $value );
96+
if ( ! wpp_is_active( 'acf_persian_date' ) && ! empty( $value ) ) {
97+
$value = parsidate( 'Y-m-d', $value, 'en' );
9898
}
9999

100100
return apply_filters( 'wpp_acf_after_load_jalali_date', $value );
@@ -111,7 +111,7 @@ function load_value( $value, $post_id, $field ) {
111111
* @since 4.0.0
112112
*/
113113
function update_value( $value, $post_id, $field ) {
114-
if ( ! wpp_is_active( 'acf_persian_date' ) ) {
114+
if ( ! wpp_is_active( 'acf_persian_date' ) && ! empty( $value ) ) {
115115
$value = gregdate( 'Y-m-d', $value );
116116
}
117117

@@ -179,4 +179,4 @@ function update_field( $field ) {
179179
}
180180
}
181181

182-
new WPP_acf_field_jalali_datepicker( $this->settings );
182+
new WPP_acf_field_jalali_datepicker( $this->settings );

0 commit comments

Comments
 (0)