      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: 156: 157: 158:

    <?php

    namespace WPGMZA;

    require_once(plugin_dir_path(__DIR__) . 'lib/codecabin/class.settings.php');

    class GlobalSettings extends \codecabin\Settings
    {
        const TABLE_NAME = 'wpgmza_global_settings';
        const LEGACY_TABLE_NAME = 'WPGMZA_OTHER_SETTINGS';

        private $updatingLegacySettings = false;

        public function __construct()
        {
            $self = $this;

            $legacy_settings_exist = (get_option(GlobalSettings::LEGACY_TABLE_NAME) ? true : false);
            $settings_exist = (get_option(GlobalSettings::TABLE_NAME) ? true : false);

            if($legacy_settings_exist && !$settings_exist)
                $this->migrate();

            \codecabin\Settings::__construct(GlobalSettings::TABLE_NAME);

            $this->wpgmza_google_maps_api_key = get_option('wpgmza_google_maps_api_key');

            if(!$legacy_settings_exist && !$settings_exist)
                $this->install();

            // Legacy Pro support. Users with older Pro will lose settings
            add_filter('pre_update_option_WPGMZA_OTHER_SETTINGS', array($this, 'onPreUpdateLegacySettings'), 10, 2);
        }

        public function __get($name)
        {
            if($name == 'useLegacyHTML')
                return true;

            return \codecabin\Settings::__get($name);
        }

        // TODO: This should inherit from Factory when traits are available
        public static function createInstance()
        {
            $class = get_called_class();
            $args = func_get_args();
            $count = count($args);
            $filter = "wpgmza_create_$class";

            if(empty($args))
                $filter_args = array($filter, null);
            else
                $filter_args = array_merge(array($filter), $args);

            $override = call_user_func_array('apply_filters', $filter_args);

            if($override)
                return $override;

            $reflect = new \ReflectionClass($class);
            $instance = $reflect->newInstanceArgs($args);

            return $instance;
        }

        public function getDefaults()
        {
            $settings = apply_filters('wpgmza_plugin_get_default_settings', array(
                'engine'                => 'google-maps',
                'google_maps_api_key'   => get_option('wpgmza_google_maps_api_key'),
                'default_marker_icon'   => Marker::DEFAULT_ICON,
                'developer_mode'        => false
            ));

            return $settings;
        }

        public function onPreUpdateLegacySettings($new_value, $old_value)
        {
            // Merge legacy settings into this settings
            if(!$this->updatingLegacySettings)
                $this->set($new_value);

            return $new_value;
        }

        protected function update()
        {
            /*echo "<pre>";
            debug_print_backtrace();
            echo "</pre>";*/

            \codecabin\Settings::update();

            // Legacy Pro support
            $this->updatingLegacySettings = true;

            //var_dump($this->wpgmza_settings_map_full_screen_control);

            //if(empty($this->wpgmza_settings_map_full_screen_control))
                //throw new \Exception('why');

            $legacy = $this->toArray();

            //var_dump($legacy['wpgmza_settings_map_full_screen_control']);

            //var_dump("Updating " . GlobalSettings::LEGACY_TABLE_NAME, $legacy);

            //if(empty($legacy['wpgmza_settings_map_full_screen_control']))
                //throw new \Exception('Can you not');

            update_option(GlobalSettings::LEGACY_TABLE_NAME, $legacy);

            //var_dump("Read back ", get_option(GlobalSettings::LEGACY_TABLE_NAME));

            $this->updatingLegacySettings = false;
        }

        protected function install()
        {
            $this->set( $this->getDefaults() );
        }

        protected function migrate()
        {
            $legacy = get_option(GlobalSettings::LEGACY_TABLE_NAME);

            $json = json_encode($legacy);

            update_option(GlobalSettings::TABLE_NAME, $json);
        }

        public function jsonSerialize()
        {
            $src = \codecabin\Settings::jsonSerialize();
            $data = clone $src;

            if(isset($data->wpgmza_settings_ugm_email_address))
                unset($data->wpgmza_settings_ugm_email_address);

            return $data;
        }

        public function toArray()
        {
            $src = \codecabin\Settings::toArray();
            $data = (array)$src;

            if(isset($data['wpgmza_settings_ugm_email_address']))
                unset($data['wpgmza_settings_ugm_email_address']);

            return $data;
        }
    }