      1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155:

    <?php

    namespace WPGMZA;

    /**
     * This module loads the Google Maps API unconditionally (as opposed to the GoogleMapsAPILoader)
     * @deprecated This functionality will be merged into one class with GoogleMapsAPILoader
     * @see GoogleMapsAPILoader
     */
    class GoogleMapsLoader
    {
        private static $googleAPILoadCalled = false;

        /**
         * This will be handled by the Factory class
         * @deprecated
         */
        public static function _createInstance()
        {
            return new GoogleMapsLoader();
        }

        /**
         * This will be handled by the Factory class
         * @deprecated
         */
        public static function createInstance()
        {
            return static::_createInstance();
        }

        /**
         * Gets the parameters to be sent to the Google Maps API load call
         * @return array An array of key value parameters to be passed to the load URL
         */
        protected function getGoogleMapsAPIParams()
        {
            global $wpgmza;

            // Locale
            $locale = get_locale();
            $suffix = '.com';

            switch($locale)
            {
                case 'he_IL':
                    // Hebrew correction
                    $locale = 'iw';
                    break;

                case 'zh_CN':
                    // Chinese integration
                    $suffix = '.cn';
                    break;
            }

            $locale = substr($locale, 0, 2);

            // Default params for google maps
            $params = array(
                'v'         => 'quarterly',
                'language'  => $locale,
                'suffix'    => $suffix
            );

            // Libraries
            $libraries = array('geometry', 'places', 'visualization');
            $params['libraries'] = implode(',', $libraries);

            // API Version
            /*if(!empty(Plugin::$settings->api_version))
            {
                // Force 3.28 if the user has a setting below this
                if(version_compare(Plugin::$settings->api_version, '3.29', '<'))
                {
                    $params['v'] = '3.29';

                    // Force greedy gesture behaviour (the default before 3.27) if the user had this set
                    if(version_compare(Plugin::$settings->api_version, '3.27', '<'))
                        Plugin::$settings->force_greedy_gestures = true;
                }
                else
                    $params['v'] = Plugin::$settings->api_version;
            }

            */

            // API Key
            //if(!empty($wpgmza->settings->google_maps_api_key))
                //$params['key'] = $wpgmza->settings->google_maps_api_key;

            //if($wpgmza->getCurrentPage() == 'map-edit')
                //$params['libraries'] = 'drawing';

            $key = get_option('wpgmza_google_maps_api_key');
            if(!empty($key))
                $params['key'] = $key;

            $params = apply_filters( 'wpgmza_google_maps_api_params', $params );

            return $params;
        }

        /**
         * This function loads the Google API if it hasn't been called already
         * @return void
         */
        public function loadGoogleMaps()
        {
            global $wpgmza;

            if(GoogleMapsLoader::$googleAPILoadCalled)
                return;

            $apiLoader = new GoogleMapsAPILoader();
            if(!$apiLoader->isIncludeAllowed())
                return;

            $params = $this->getGoogleMapsAPIParams();

            $suffix = $params['suffix'];
            unset($params['suffix']);

            $url = '//maps.google' . $suffix . '/maps/api/js?' . http_build_query($params);

            wp_enqueue_script('wpgmza_api_call', $url);

            GoogleMapsLoader::$googleAPILoadCalled = true;

            add_filter('script_loader_tag', array($this, 'preventOtherGoogleMapsTag'), 9999999, 3);
        }

        /**
         * Gets the HTML for the settings panel for this module, which appears in the general settings tab.
         * @return string The HTML string for the settings panel
         */
        public function preventOtherGoogleMapsTag($tag, $handle, $src)
        {
            if(preg_match('/maps\.google/i', $src))
            {
                if($handle != 'wpgmza_api_call') {
                    return '';
                }

                if(!preg_match('/\?.+$/', $src))
                    return str_replace($src, $src . '?' . http_build_query($this->getGoogleMapsAPIParams()), $tag);
            }

            return $tag;
        }

    }