PluginProbe ʕ •ᴥ•ʔ
پارسی دیت – Parsi Date / 6.2.1
پارسی دیت – Parsi Date v6.2.1
6.2.1 6.2 6.1 5.1.6 5.1.7 5.1.8 5.1.8.2 6.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.0.0-alpha 2.1 2.1.1 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0.1 2.3.0.2 2.3.1 2.3.2 2.3.3 2.3.4 3.0.1 3.0.2 3.0.3 4.0.0 4.0.1 4.0.2 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5
wp-parsidate / inc / App / Integration / WooCommerce / WooCommerceCitySelect.php
wp-parsidate / inc / App / Integration / WooCommerce Last commit date
wc-cities 3 weeks ago wc-gateways 3 weeks ago WcGateways.php 3 weeks ago WooCommerceCitySelect.php 3 weeks ago
WooCommerceCitySelect.php
210 lines
1 <?php
2 /**
3 * Plugin Name: WC City Select
4 * Plugin URI: https://wordpress.org/plugins/wc-city-select/
5 * Description: City Select for WooCommerce. Show a dropdown select as the cities input.
6 * Version: 1.0.7
7 * Author: 8manos
8 * Author URI: http://8manos.com
9 * License: GPLv2 or later
10 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
11 *
12 * WC requires at least: 2.2
13 * WC tested up to: 7.6
14 */
15
16 namespace WPParsidate\App\Integration\WooCommerce;
17
18 defined( 'ABSPATH' ) || exit;
19
20 class WooCommerceCitySelect {
21 const VERSION = '1.0.1';
22
23 private $plugin_path;
24 private $plugin_url;
25
26 private $cities;
27 private $dropdown_cities;
28
29 public function __construct() {
30 $this->plugin_path = __DIR__ . '/wc-cities';
31 $this->plugin_url = WP_PARSI_URL;
32
33 add_filter( 'woocommerce_billing_fields', array( $this, 'billing_fields' ), 10, 2 );
34 add_filter( 'woocommerce_shipping_fields', array( $this, 'shipping_fields' ), 10, 2 );
35 add_filter( 'woocommerce_form_field_city', array( $this, 'form_field_city' ), 10, 4 );
36
37 //js scripts
38 add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
39 }
40
41 public function billing_fields( $fields, $country ) {
42 $fields['billing_city']['type'] = 'city';
43
44 return $fields;
45 }
46
47 public function shipping_fields( $fields, $country ) {
48 $fields['shipping_city']['type'] = 'city';
49
50 return $fields;
51 }
52
53 public function get_cities( $cc = null ) {
54 if ( empty( $this->cities ) ) {
55 $this->load_country_cities();
56 }
57
58 if ( ! is_null( $cc ) ) {
59 return isset( $this->cities[ $cc ] ) ? $this->cities[ $cc ] : false;
60 } else {
61 return $this->cities;
62 }
63 }
64
65 public function load_country_cities() {
66 global $cities;
67
68 // Load only the city files the shop owner wants/needs.
69 $allowed = array_merge( WC()->countries->get_allowed_countries(), WC()->countries->get_shipping_countries() );
70
71 if ( $allowed ) {
72 foreach ( $allowed as $code => $country ) {
73 if ( ! isset( $cities[ $code ] ) && file_exists( $this->get_plugin_path() . '/cities/' . $code . '.php' ) ) {
74 include( $this->get_plugin_path() . '/cities/' . $code . '.php' );
75 }
76 }
77 }
78
79 $this->cities = apply_filters( 'wc_city_select_cities', $cities );
80 }
81
82 private function add_to_dropdown( $item ) {
83 $this->dropdown_cities[] = $item;
84 }
85
86 public function form_field_city( $field, $key, $args, $value ) {
87 // Do we need a clear div?
88 if ( ( ! empty( $args['clear'] ) ) ) {
89 $after = '<div class="clear"></div>';
90 } else {
91 $after = '';
92 }
93
94 // Required markup
95 if ( $args['required'] ) {
96 $args['class'][] = 'validate-required';
97 $required = ' <abbr class="required" title="' .
98 esc_attr__( 'required', 'wp-parsidate' ) .
99 '">*</abbr>';
100 } else {
101 $required = '';
102 }
103
104 // Custom attribute handling
105 $custom_attributes = array();
106
107 if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) {
108 foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) {
109 $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
110 }
111 }
112
113 // Validate classes
114 if ( ! empty( $args['validate'] ) ) {
115 foreach ( $args['validate'] as $validate ) {
116 $args['class'][] = 'validate-' . $validate;
117 }
118 }
119
120 // field p and label
121 $field = '<p class="form-row ' . esc_attr( implode( ' ',
122 $args['class'] ) ) . '" id="' . esc_attr( $args['id'] ) . '_field">';
123
124 if ( $args['label'] ) {
125 $field .= '<label for="' . esc_attr( $args['id'] ) . '" class="' . esc_attr( implode( ' ',
126 $args['label_class'] ) ) . '">' . $args['label'] . $required . '</label>';
127 }
128
129 // Get Country
130 $country_key = $key == 'billing_city' ? 'billing_country' : 'shipping_country';
131 $current_cc = WC()->checkout->get_value( $country_key );
132 $state_key = $key == 'billing_city' ? 'billing_state' : 'shipping_state';
133 $current_sc = WC()->checkout->get_value( $state_key );
134
135 // Get country cities
136 $cities = $this->get_cities( $current_cc );
137
138 if ( is_array( $cities ) ) {
139 $field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="city_select ' . esc_attr( implode( ' ',
140 $args['input_class'] ) ) . '" ' . implode( ' ',
141 $custom_attributes ) . ' placeholder="' . esc_attr( $args['placeholder'] ) . '">
142 <option value="">' . esc_html__( 'Select an option&hellip;', 'wp-parsidate' ) . '</option>';
143
144 if ( $current_sc && $cities[ $current_sc ] ) {
145 $this->dropdown_cities = $cities[ $current_sc ];
146 } else {
147 $this->dropdown_cities = [];
148 array_walk_recursive( $cities, array( $this, 'add_to_dropdown' ) );
149 sort( $this->dropdown_cities );
150 }
151
152 foreach ( $this->dropdown_cities as $city_name ) {
153 $field .= '<option value="' . esc_attr( $city_name ) . '" ' . selected( $value, $city_name,
154 false ) . '>' . $city_name . '</option>';
155 }
156
157 $field .= '</select>';
158
159 } else {
160 $field .= '<input type="text" class="input-text ' . esc_attr( implode( ' ',
161 $args['input_class'] ) ) . '" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" ' . implode( ' ',
162 $custom_attributes ) . ' />';
163 }
164
165 // field description and close wrapper
166 if ( $args['description'] ) {
167 $field .= '<span class="description">' . esc_attr( $args['description'] ) . '</span>';
168 }
169
170 $field .= '</p>' . $after;
171
172 return $field;
173 }
174
175 public function load_scripts() {
176 if ( is_cart() || is_checkout() || is_wc_endpoint_url( 'edit-address' ) ) {
177 $city_select_path = $this->get_plugin_url() . 'assets/js/city-select.js';
178
179 wp_enqueue_script( 'wc-city-select', $city_select_path, array( 'jquery', 'woocommerce' ), self::VERSION,
180 true );
181
182 $cities = json_encode( $this->get_cities() );
183
184 wp_localize_script( 'wc-city-select',
185 'wc_city_select_params',
186 array(
187 'cities' => $cities,
188 'i18n_select_city_text' => esc_attr__( 'Select an option&hellip;', 'wp-parsidate' )
189 )
190 );
191 }
192 }
193
194 public function get_plugin_path() {
195 if ( $this->plugin_path ) {
196 return $this->plugin_path;
197 }
198
199 return $this->plugin_path = plugin_dir_path( __FILE__ );
200 }
201
202 public function get_plugin_url() {
203 if ( $this->plugin_url ) {
204 return $this->plugin_url;
205 }
206
207 return $this->plugin_url = plugin_dir_url( __FILE__ );
208 }
209 }
210