array('error' => $error)); $data['is_empty'] = true; echo json_encode($data); exit; } function _geoip_detect_ajax_get_data($options = array()) { $info = geoip_detect2_get_info_from_current_ip(['en'], $options); $data = $info->jsonSerialize(); // For privacy reasons, do not emit the nb of credits left (Maxmind Precision) unset($data['maxmind']); if (is_array($data['subdivisions'])) { $data['most_specific_subdivision'] = end($data['subdivisions']); } return $data; } /** * Call this function if you want to register the JS script only for specific pages */ function geoip_detect2_enqueue_javascript() { wp_enqueue_script('geoip-detect-js'); } function _geoip_detect_parcel_get_dist_js($handle) { $urlFile = GEOIP_PLUGIN_DIR . '/js/dist/parcel.json'; if (!is_readable($urlFile)) return false; $json = file_get_contents($urlFile); $urls = json_decode($json, true); if (isset($urls[$handle])) return '/js/dist' .$urls[$handle]; return false; } function _geoip_detect_register_javascript() { // What about CORS usage? // if (!get_option('geoip-detect-ajax_enabled')) { // return; // } if (!get_option('geoip-detect-ajax_enabled')) { return; } $file_uri = _geoip_detect_parcel_get_dist_js('frontendJS'); if (!$file_uri) { if (WP_DEBUG) { trigger_error('Warning by the geoip-detect-Plugin: the file frontend.js could not be found, JS API will not work.', E_USER_NOTICE); die(); } return; } wp_register_script('geoip-detect-js', GEOIP_DETECT_PLUGIN_URI . $file_uri, array('jquery'), GEOIP_DETECT_VERSION, true); $data = [ 'ajaxurl' => admin_url('/admin-ajax.php'), 'default_locales' => apply_filters('geoip_detect2_locales', null), 'do_body_classes' => (bool) get_option('geoip-detect-set_css_country'), 'cookie_name' => 'geoip-detect-result', /* If you don't want to use the cookie cache, empty this value via the filter */ 'cookie_duration_in_days' => 1, /* If you set this to 0, then the cookie will expire when the window closes. */ ]; $data = apply_filters('geoip_detect2_ajax_localize_script_data', $data); wp_localize_script('geoip-detect-js', 'geoip_detect', [ 'options' => $data ] ); if ((get_option('geoip-detect-ajax_enqueue_js') || get_option('geoip-detect-set_css_country')) && !is_admin()) { geoip_detect2_enqueue_javascript(); } } add_action('wp_enqueue_scripts', '_geoip_detect_register_javascript');