summaryrefslogtreecommitdiff
blob: 4493fa6c5e49f403e7e4cbc4f6e89b4ba1b707b4 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php

if ( ! class_exists( 'WMobilePack_Options' ) ) {

    /**
     * Overall Option Management class
     *
     * Instantiates all the options and offers a number of utility methods to work with the options
     *
     */
    class WMobilePack_Options
    {

        /* ----------------------------------*/
        /* Properties						 */
        /* ----------------------------------*/

        public static $prefix = 'wmpack_';
        public static $transient_prefix = 'wmp_';

        public static $options = array(

            // themes
            'theme' => 1,
            'color_scheme' => 1,
            'custom_colors' => array(),
            'theme_timestamp' => '',
            'font_headlines' => 1,
            'font_subtitles' => 1,
            'font_paragraphs' => 1,

            // images
            'logo' => '',
            'icon' => '',
            'cover' => '',

            // content
            'inactive_categories' => array(),
            'inactive_pages' => array(),
            'ordered_categories' => array(),
            'ordered_pages' => array(), // this option is @deprecated starting from v2.2

            // other settings
            'display_mode' => 'normal',
            'google_analytics_id' => '',
            'display_website_link' => 1,
            'posts_per_page' => 'auto',

            // premium accounts with api key
            'premium_api_key'		 => '',
            'premium_config_path'	 => '',
            'premium_active'		 => 0,

            // administrative
            'joined_waitlists' => array(),
            'whats_new_updated' => 0,
            'whats_new_last_updated' => 0,
            'upgrade_notice_updated' => 0, // if we should display the upgrade notice
            'upgrade_notice_last_updated' => 0, // upgrade timestamp
            'allow_tracking' => 0
        );

        /* ----------------------------------*/
        /* Methods							 */
        /* ----------------------------------*/

        /**
         *
         * Unserialize an option that was previously serialized.
         *
         * @param $option_name
         * @param $option_value
         * @return mixed
         */
        protected static function unserialize_data($option_name, $option_value){

            if (in_array($option_name, array('inactive_categories', 'inactive_pages', 'ordered_categories', 'ordered_pages', 'joined_waitlists'))) {

                $data = @unserialize($option_value);

                if ($data !== false) {
                    return $data;
                }
            }

            return $option_value;
        }

        /**
         *
         * The get_setting method is used to read an option value (or options) from the database.
         *
         * If the $option param is an array, the method will return an array with the values,
         * otherwise it will return only the requested option value.
         *
         * As of version 2.2, the method will automatically unserialize strings that were serialized.
         *
         * @param $option - array / string
         * @return mixed
         */
        public static function get_setting($option)
        {

            // if the passed param is an array, return an array with all the settings
            if (is_array($option)) {

                $wmp_settings = array();

                foreach ($option as $option_name) {

                    if (get_option(self::$prefix . $option_name) === false) {
                        $wmp_settings[$option_name] = self::$options[$option_name];
                    } else {
                        $wmp_settings[$option_name] = self::unserialize_data($option_name, get_option(self::$prefix . $option_name));
                    }
                }

                // return array
                return $wmp_settings;

            } elseif (is_string($option)) { // if option is a string, return the value of the option

                // check if the option is added in the db
                if (get_option(self::$prefix . $option) === false) {
                    $wmp_setting = self::$options[$option];
                } else {
                    $wmp_setting = self::unserialize_data($option, get_option(self::$prefix . $option));
                }

                return $wmp_setting;
            }
        }


        /**
         *
         * The save_settings method is used to save an option value (or options) in the database.
         *
         * @param $option - array / string
         * @param $option_value - optional, mandatory only when $option is a string
         *
         * @return bool
         *
         */
        public static function save_settings($option, $option_value = '')
        {

            if (current_user_can('manage_options')) {

                if (is_array($option) && !empty($option)) {

                    // set option not saved variable
                    $option_not_saved = false;

                    foreach ($option as $option_name => $option_value) {

                        if (array_key_exists($option_name, self::$options))
                            add_option(self::$prefix . $option_name, $option_value);
                        else
                            $option_not_saved = true; // there is at least one option not in the default list
                    }

                    if (!$option_not_saved)
                        return true;
                    else
                        return false; // there was an error

                } elseif (is_string($option) && $option_value != '') {

                    if (array_key_exists($option, self::$options))
                        return add_option(self::$prefix . $option, $option_value);

                }
            }

            return false;
        }

        /**
         *
         * The update_settings method is used to update the setting/settings of the plugin in options table in the database.
         *
         * @param $option - array / string
         * @param $option_value - optional, mandatory only when $option is a string
         *
         * @return bool
         *
         */
        public static function update_settings($option, $option_value = null)
        {

            if (current_user_can('manage_options')) {

                if (is_array($option) && !empty($option)) {

                    $option_not_updated = false;

                    foreach ($option as $option_name => $option_value) {

                        // set option not saved variable
                        if (array_key_exists($option_name, self::$options))
                            update_option(self::$prefix . $option_name, $option_value);
                        else
                            $option_not_updated = true; // there is at least one option not in the default list
                    }

                    if (!$option_not_updated)
                        return true;
                    else
                        return false; // there was an error

                } elseif (is_string($option) && $option_value !== null) {

                    if (array_key_exists($option, self::$options))
                        return update_option(self::$prefix . $option, $option_value);

                }
            }

            return false;
        }


        /**
         *
         * The delete_settings method is used to delete the setting/settings of the plugin from the options table in the database.
         *
         * @param $option - array / string
         *
         * @return bool
         *
         */
        public static function delete_settings($option)
        {

            if (current_user_can('manage_options')) {

                if (is_array($option) && !empty($option)) {

                    foreach ($option as $option_name => $option_value) {

                        if (array_key_exists($option_name, self::$options))
                            delete_option(self::$prefix . $option_name);
                    }

                    return true;

                } elseif (is_string($option)) {

                    if (array_key_exists($option, self::$options))
                        return delete_option(self::$prefix . $option);

                }
            }
        }


        /**
         *
         * Delete all transients and temporary data when the plugin is deactivated
         *
         * @todo Prefix WPMP_Tracking_Hash with the transient prefix instead of WPMP
         *
         */
        public static function deactivate()
        {

            // delete tracking hash
            delete_option('WPMP_Tracking_Hash');

            // remove transients
            foreach (array('whats_new_updates', 'news_updates', 'more_updates', 'premium_config_path', 'tracking_cache') as $transient_name) {

                if (get_transient(self::$transient_prefix.$transient_name) !== false)
                    delete_transient(self::$transient_prefix.$transient_name);
            }

            self::update_settings('allow_tracking', 0);
        }

        /**
         *
         * Delete all options and transients when the plugin is uninstalled
         *
         */
        public static function uninstall()
        {

            // delete plugin settings
            self::delete_settings(self::$options);

            // remove pages settings
            global $wpdb;
            $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '".self::$prefix."page_%'" );
        }
    }
}