plugin_name = __CLASS__; register_activation_hook(__FILE__, [$this, 'add_settings']); $this->hook('plugins_loaded'); parent::__construct(); } public static function get_instance() { if (!isset(self::$instance)) { $c = __CLASS__; self::$instance = new $c(); } return self::$instance; } /** * "plugins_loaded" action hook; called after all active plugins/pluggable functions * are loaded. */ public function plugins_loaded() { $this->hook('init'); $this->hook('wp_enqueue_scripts', 'enqueue_scripts'); if (class_exists('WooCommerce')) { $settings = $this->get_option(); if ($settings['woocommerce_enabled']) { $this->hook('woocommerce_checkout_fields'); $this->hook('woocommerce_admin_order_data_after_shipping_address'); $this->hook('woocommerce_admin_order_data_after_billing_address'); $this->hook('woocommerce_locate_template', 3); } } } /** * plugin activation / "activate_[pluginname]" action hook; called when the plugin is * first activated. * * Defines and sets up the default settings and options for the plugin. The default set * of options are configurable, at activation time, via the 'what3words_searchbox_default_settings' * filter hook. */ public function add_settings() { $settings = $this->get_option(); if (!is_array($settings)) { $settings = apply_filters(WHAT3WORDS_SEARCHBOX_SETTINGS_FILTER, [ 'version' => self::VERSION, 'api_key' => '', 'input_selectors' => '', 'input_placeholder' => 'Search for a 3 word address', 'suggestions' => 3, 'lang' => $this->get_default_language(), 'multilingual' => true, 'country_filter' => false, 'country_filter_selector' => '', 'country_code' => 'GB', 'text_direction' => 'ltr', 'woocommerce_enabled' => false ] ); update_option($this->plugin_name, $settings); } } /** * "init" action hook; called to initialise the plugin */ function init() { $lang_dir = '3-word-address-validation-field/languages'; load_plugin_textdomain ('what3words-searchbox', false, $lang_dir); } /** * "wp_enqueue_scripts" action hook; used when enqueuing items that are meant * to appear on the front end. Despite the name, it is used for enqueuing both * scripts and styles. */ public function enqueue_scripts() { $handle = 'what3words-searchbox-autosuggest-css'; $src = WHAT3WORDS_SEARCHBOX_URL . 'assets/css/jquery.w3w-autosuggest-plugin.min.css'; $deps = []; $ver = NULL; $in_footer = false; wp_enqueue_style($handle, $src, $deps, $ver, $in_footer); $handle = 'what3words-searchbox-autosuggest-js'; $src = WHAT3WORDS_SEARCHBOX_URL . 'assets/js/jquery.w3w-autosuggest-plugin.bundle.js'; $deps = ['jquery']; $in_footer = true; wp_enqueue_script($handle, $src, $deps, $ver, $in_footer); $handle = 'what3words-searchbox-js'; $src = WHAT3WORDS_SEARCHBOX_URL . 'assets/js/what3words-searchbox.js'; $deps = ['jquery', 'what3words-searchbox-autosuggest-js']; $in_footer = true; wp_enqueue_script($handle, $src, $deps, $ver, $in_footer); $settings = $this->get_option(); if (isset($settings['api_key']) && !empty($settings['api_key'])) { $selectors = []; if (isset($settings['input_selectors']) && !empty($settings['input_selectors'])) { $selectors[] = $settings['input_selectors']; } if (isset($settings['woocommerce_enabled']) && $settings['woocommerce_enabled']) { $selectors[] = '#shipping_w3w,#billing_w3w'; } if (!empty($selectors)) { $data['input_selectors'] = implode(',', $selectors); $data['api_key'] = $settings['api_key']; $data['multilingual'] = $settings['multilingual']; $data['lang'] = $settings['lang']; $data['suggestions'] = $settings['suggestions']; $data['input_placeholder'] = $settings['input_placeholder']; $data['text_direction'] = $settings['text_direction']; $data['country_filter'] = $settings['country_filter']; $data['country_code'] = $settings['country_code']; $data['country_filter_selector'] = $settings['country_filter_selector']; $data['disabled_selector_bool'] = $settings['disabled_selector_bool']; $data['disabled_selector'] = $settings['disabled_selector']; wp_localize_script($handle, 'What3wordsSearchbox', $data); } } } /** * "woocommerce_checkout_fields" filter hook; enables custom checkout fields. */ public function woocommerce_checkout_fields($fields) { $fields['shipping']['shipping_w3w'] = [ 'label' => __('Delivery 3 word address', 'what3words-searchbox'), 'placeholder' => _x('Delivery 3 word address', 'placeholder', 'what3words-searchbox'), 'required' => false, 'class' => ['form-row-wide'], 'clear' => true ]; $fields['billing']['billing_w3w'] = [ 'label' => __('Billing 3 word address', 'what3words-searchbox'), 'placeholder' => _x('Billing 3 word address', 'placeholder', 'what3words-searchbox'), 'required' => false, 'class' => ['form-row-wide'], 'clear' => true ]; return $fields; } /** * "woocommerce_admin_order_data_after_shipping_address" action hook; displays * custom order edit field values after the shipping address. */ public function woocommerce_admin_order_data_after_shipping_address($order) { echo '

' . __('w3w Address', 'what3words-searchbox') . ': ' . get_post_meta($order->id, '_shipping_w3w', true) . '

'; } /** * "woocommerce_admin_order_data_after_billing_address" action hook; displays * custom order edit field values after the billing address. */ public function woocommerce_admin_order_data_after_billing_address($order) { echo '

' . __('w3w Address', 'what3words-searchbox') . ': ' . get_post_meta($order->id, '_billing_w3w', true) . '

'; } /** * "woocommerce_locate_template" filter hook; adds a 4th template directory to used * the plugin's template. */ public function woocommerce_locate_template($template, $name, $path) { global $woocommerce; // Uurrrrghhhh $saved_template = $template; if (!$path) { $path = $woocommerce->template_url; } $plugin_path = untrailingslashit(WHAT3WORDS_SEARCHBOX_PATH) . '/templates/woocommerce/'; // Look within passed path within the theme - this is priority $template = locate_template([ $path . $name, $name ]); if (!$template && file_exists($plugin_path . $name)) { $template = $plugin_path . $name; } if (!$template) { $template = $saved_template; } return $template; } public function get_languages() { if ($this->languages === null) { $this->languages = json_decode(file_get_contents(WHAT3WORDS_SEARCHBOX_PATH . '/assets/json/lang-codes.json'), true); } return $this->languages; } public function get_country_codes($lang='en') { if ($this->countries === null) { $this->countries = json_decode(file_get_contents(WHAT3WORDS_SEARCHBOX_PATH . '/assets/json/country-codes.json'), true); } $codes = []; foreach ($this->countries as $country) { $key = 'name_' . $lang; $name = (isset($country[$key]) && !empty($country[$key]) ? $country[$key] : $country['name_en']); $codes[] = ['iso' => strtolower($country['ISO']), 'name' => $name]; } uasort($codes, function($a, $b) { return strcmp($a['name'], $b['name']); }); return $codes; } private function get_default_language() { $site_lang = substr(get_bloginfo('language'), 0, 2); foreach ($this->get_languages() as $lang) { if ($site_lang === $lang['code']) { return $lang['code']; } } return 'en'; } } // end-class What3wordsSearchbox } // end-if (!class_exists('What3wordsSearchbox')) What3wordsSearchbox::get_instance(); What3wordsSearchboxAdmin::get_instance(); ?>