cities ) ) { $this->load_country_cities(); } if ( ! is_null( $cc ) ) { return isset( $this->cities[ $cc ] ) ? $this->cities[ $cc ] : false; } else { return $this->cities; } } public function load_country_cities() { global $cities; // Load only the city files the shop owner wants/needs. $allowed = array_merge( WC()->countries->get_allowed_countries(), WC()->countries->get_shipping_countries() ); if ( $allowed ) { foreach ( $allowed as $code => $country ) { if ( ! isset( $cities[ $code ] ) && file_exists( $this->get_plugin_path() . '/cities/' . $code . '.php' ) ) { include( $this->get_plugin_path() . '/cities/' . $code . '.php' ); } } } $this->cities = apply_filters( 'wc_city_select_cities', $cities ); } public function form_field_city( $field, $key, $args, $value ) { // Do we need a clear div? if ( ( ! empty( $args['clear'] ) ) ) { $after = '
'; } else { $after = ''; } // Required markup if ( $args['required'] ) { $args['class'][] = 'validate-required'; $required = ' *'; } else { $required = ''; } // Custom attribute handling $custom_attributes = array(); if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) { foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) { $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"'; } } // Validate classes if ( ! empty( $args['validate'] ) ) { foreach( $args['validate'] as $validate ) { $args['class'][] = 'validate-' . $validate; } } // field p and label $field = '

'; if ( $args['label'] ) { $field .= ''; } // Get Country $country_key = $key == 'billing_city' ? 'billing_country' : 'shipping_country'; $current_cc = WC()->checkout->get_value( $country_key ); $state_key = $key == 'billing_city' ? 'billing_state' : 'shipping_state'; $current_sc = WC()->checkout->get_value( $state_key ); // Get country cities $cities = $this->get_cities( $current_cc ); if ( is_array( $cities ) ) { $field .= ''; } else { $field .= ''; } // field description and close wrapper if ( $args['description'] ) { $field .= '' . esc_attr( $args['description'] ) . ''; } $field .= '

' . $after; return $field; } public function load_scripts() { if ( is_cart() || is_checkout() || is_wc_endpoint_url( 'edit-address' ) ) { $city_select_path = $this->get_plugin_url() . 'assets/js/city-select.js'; wp_enqueue_script( 'wc-city-select', $city_select_path, array( 'jquery', 'woocommerce' ), self::VERSION, true ); $cities = json_encode( $this->get_cities() ); wp_localize_script( 'wc-city-select', 'wc_city_select_params', array( 'cities' => $cities, 'i18n_select_city_text' => esc_attr__( 'Select an option…', 'woocommerce' ) ) ); } } public function get_plugin_path() { if ( $this->plugin_path ) { return $this->plugin_path; } return $this->plugin_path = plugin_dir_path( __FILE__ ); } public function get_plugin_url() { if ( $this->plugin_url ) { return $this->plugin_url; } return $this->plugin_url = plugin_dir_url( __FILE__ ); } } $GLOBALS['wc_city_select'] = new WC_City_Select(); }