includes(); $this->init_hooks(); } private function includes() { if ( is_admin() ) { include_once WPS_AA_PATH . 'includes/admin/class-options.php'; include_once WPS_AA_PATH . 'includes/admin/promos.php'; } } private function init_hooks() { add_action( 'wp_head', array( $this, 'frontend_js' ), 9999 ); } public function get_instances() { $instances = get_option( 'wps_aa_instances' ); if ( empty( $instances ) && is_admin() ) { // Set a default instance in the admin $key = $this->generate_key(); $instances = array( $key => array( 'label' => '', 'page' => '', 'init' => '', 'allowed_countries' => array(), 'fields' => WPS_AA()->get_available_fields() ) ); //update_option( 'wps_aa_instances', $instances ); } 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(); 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'] ) && $page_id != $instance['page'] ) { continue; } $available_instances[] = $instance; } return $available_instances; } public function get_available_fields() { $fields = array( '10' => array( 'key' => 'address1', 'label' => 'Address 1', 'selector' => '', 'format' => '' ), '20' => array( 'key' => 'address2', 'label' => 'Address 2', 'selector' => '', 'format' => '' ), '30' => array( 'key' => 'locality', 'label' => 'Locality (City)', 'selector' => '', 'format' => '' ), '40' => array( 'key' => 'administrative_area_level_1', 'label' => 'Administrative Area Level 1 (State/Region/Province)', 'selector' => '', 'format' => '' ), '50' => array( 'key' => 'postal_code', 'label' => 'Postal Code', 'selector' => '', 'format' => '' ), '5' => array( 'key' => 'country', 'label' => 'Country', 'selector' => '', 'format' => '' ), ); $fields = apply_filters( 'wps_aa_available_fields', $fields ); ksort( $fields ); return $fields; } public function frontend_js() { $available_instances = $this->available_instances(); $google_api_key = get_option( 'wps_aa_google_api_key' ); if ( !empty( $available_instances ) ) { wp_enqueue_script( 'wps-aa-google-maps', 'https://maps.googleapis.com/maps/api/js?key=' . $google_api_key . '&libraries=places&v=weekly', '', WPS_AA_VERSION, true ); wp_enqueue_script( 'wps-aa-frontend', WPS_AA_URL . 'assets/js/frontend.js', array( 'jquery' ), WPS_AA_VERSION, true ); $available_fields = $this->get_available_fields(); $instances_data = array(); foreach ( $available_instances as $instance ) { $fields = array(); foreach ( $available_fields as $field ) { $fields[ $field['key'] ] = array( 'selector' => $instance['fields'][ $field['key'] ]['selector'], 'format' => ( isset( $instance['fields'][ $field['key'] ]['format'] ) ) ? $instance['fields'][ $field['key'] ]['format'] : 'long_name' ); } $instances_data[] = array( 'init' => $instance['init'], 'allowed_countries' => ( !empty( $instance['allowed_countries'] ) ) ? $instance['allowed_countries'] : '', 'fields' => $fields ); } wp_localize_script( 'wps-aa-frontend', 'wps_aa_vars', array( 'instances' => $instances_data ) ); } } public function generate_key() { return md5(uniqid(time())); } }