includes(); $this->init_hooks(); } private function includes() { include_once WPS_AA_PATH . 'includes/class-picker.php'; if ( is_admin() ) { include_once WPS_AA_PATH . 'includes/admin/class-options.php'; include_once WPS_AA_PATH . 'includes/admin/class-setup.php'; include_once WPS_AA_PATH . 'includes/admin/class-setup-updates.php'; include_once WPS_AA_PATH . 'includes/admin/promos.php'; } } private function init_hooks() { add_action( 'init', array( $this, 'set_places_api_default' ) ); add_action( 'wp_head', array( $this, 'frontend_js' ), 9999 ); } /** * Places API (New) is the default, but sites from before 2.0 were all on * the legacy API and never saved this setting. Save it once so those sites * stay on legacy, and new installs start on New. */ public function set_places_api_default() { if ( false !== get_option( 'wps_aa_places_api' ) ) { return; } update_option( 'wps_aa_places_api', get_option( 'wps_aa_google_api_key' ) ? 'legacy' : 'new' ); } public function get_instances() { $instances = get_option( 'wps_aa_instances' ); if ( empty( $instances ) && is_admin() ) { // Set a default instance in the admin // A fixed key, so saving the settings page finds the instance it showed $key = 'default'; $instances = array( $key => array( 'label' => '', 'page' => '', 'init' => '', 'allowed_countries' => array(), 'fields' => array(), ), ); } // Nothing saved yet comes back as false, and the integrations add to an array if ( ! is_array( $instances ) ) { $instances = array(); } return apply_filters( 'wps_aa_instances', $instances ); } public function get_options( $force = false ) { if ( empty( $this->options ) || $force ) { $this->options = get_option( $this->prefix . 'options' ); } return apply_filters( $this->prefix . 'options', $this->options ); } public function get_option( $key ) { if ( ! empty( $this->options[ $key ] ) ) { return $this->options[ $key ]; } return false; } private function available_instances( $page_id = '' ) { if ( empty( $page_id ) ) { $page_id = get_queried_object_id(); } $instances = $this->get_instances(); $instances = array_merge( is_array( $instances ) ? $instances : array(), $this->late_instances ); // The instances this page load will use, after the ones saved in the settings $instances = apply_filters( 'wps_aa_available_instances', $instances ); if ( empty( $instances ) ) { return false; } // Check for available instances to show on the current page $available_instances = array(); foreach ( $instances as $instance ) { if ( ! empty( $instance['page'] ) ) { if ( is_array( $instance['page'] ) ) { if ( ! in_array( $page_id, $instance['page'] ) ) { continue; } } elseif ( $page_id != $instance['page'] ) { continue; } } $available_instances[] = $instance; } return $available_instances; } public function get_available_place_components() { $place_components = array( '5' => array( 'key' => 'country', 'tag' => 'Country', 'label' => 'Country', ), '10' => array( 'key' => 'address1', 'tag' => 'Address 1', 'label' => 'Address 1', ), '20' => array( 'key' => 'address2', 'tag' => 'Address 2', 'label' => 'Address 2', ), '30' => array( 'key' => 'locality', 'tag' => 'City', 'label' => 'Locality (City)', ), '40' => array( 'key' => 'administrative_area_level_1', 'tag' => 'State', 'label' => 'Administrative Area Level 1 (State/Region/Province)', ), '50' => array( 'key' => 'postal_code', 'tag' => 'Postal Code', 'label' => 'Postal Code', ), ); $place_components = apply_filters( 'wps_aa_place_components', $place_components ); ksort( $place_components ); return $place_components; } public function frontend_js() { if ( is_admin() ) { return; } $available_instances = $this->available_instances(); if ( ! empty( $available_instances ) ) { $instances_data = array(); foreach ( $available_instances as $instance ) { if ( empty( $instance['init'] ) ) { // If there is no init, no bother going any further - this is a bare minimum continue; } $fields = array(); foreach ( $instance['fields'] as $field ) { if ( ! empty( $field['selector'] ) && ! empty( $field['data'] ) ) { $fields[] = array( 'selector' => $field['selector'], 'data' => $field['data'], 'delay' => ( ! empty( $field['delay'] ) ) ? $field['delay'] : '', ); } } $instances_data[] = array( 'init' => $instance['init'], 'allowed_countries' => ( ! empty( $instance['allowed_countries'] ) ) ? $instance['allowed_countries'] : '', 'fields' => $fields, 'delay' => ( ! empty( $instance['delay'] ) ) ? $instance['delay'] : '', ); } if ( ! empty( $instances_data ) && apply_filters( 'wps_aa_load_scripts', true ) ) { wp_enqueue_script( 'wps-aa-google-maps', $this->google_maps_url(), '', '', array( 'strategy' => 'async', 'in_footer' => true, ) ); $version = ( defined( 'WPS_AA_PREMIUM_VERSION' ) ) ? WPS_AA_PREMIUM_VERSION : WPS_AA_VERSION; $places_api = ( get_option( 'wps_aa_places_api' ) === 'legacy' ) ? 'legacy' : 'new'; $dependencies = array( 'jquery', 'wps-aa-google-maps' ); if ( $places_api === 'new' ) { wp_enqueue_script( 'wps-aa-places-new', WPS_AA_URL . 'assets/js/places-new.js', array(), $version, true ); $dependencies[] = 'wps-aa-places-new'; } wp_enqueue_script( 'wps-aa-frontend', WPS_AA_URL . 'assets/js/frontend.js', $dependencies, $version, true ); $results_title = get_option( 'wps_aa_results_title' ); $args = array( 'instances' => $instances_data, 'results_title' => $results_title, 'places_api' => $places_api, ); wp_localize_script( 'wps-aa-frontend', 'wps_aa_vars', $args ); } } } /** * Set up an address field from inside a form as it is drawn. Form plugins * draw their forms in the page content, after the page head where the * scripts are normally set up, so this sets them up again for the footer. * * @param string $key Unique key for this field. * @param array $instance Same shape as a saved instance. */ public function add_instance( $key, $instance ) { $this->late_instances[ $key ] = $instance; // Forms drawn in the footer still count, as long as the footer scripts have not printed yet if ( did_action( 'wp_head' ) && ! did_action( 'wp_print_footer_scripts' ) ) { $this->frontend_js(); } } /** * The Google Maps script URL, with the site's API key and language. */ public function google_maps_url() { $url = 'https://maps.googleapis.com/maps/api/js?key=' . sanitize_text_field( get_option( 'wps_aa_google_api_key' ) ); $language = get_option( 'wps_aa_language' ); if ( ! empty( $language ) ) { $url .= '&language=' . sanitize_text_field( $language ); } return $url . '&callback=wps_aa&libraries=places&v=weekly&loading=async'; } public function generate_key() { return md5( uniqid( time() ) ); } }