Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 174
0.00% covered (danger)
0.00%
0 / 29
CRAP
0.00% covered (danger)
0.00%
0 / 2
zeroBS_temp_ext_legacy_notice
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
WHWPConfigLib
0.00% covered (danger)
0.00%
0 / 153
0.00% covered (danger)
0.00%
0 / 23
6642
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
132
 validateAndUpdate
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 initCreate
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
42
 resetToDefaults
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 getAll
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 update
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 delete
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 dmzGetMigrationSet
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 dmzGet
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
30
 dmzDelete
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
56
 dmzUpdate
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 dmzGetConfig
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 dmzDeleteConfig
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 dmzUpdateConfig
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 loadDMZFromDB
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 saveToDB
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
42
 loadFromDB
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 uninstall
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 createBackup
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 killBackups
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBKs
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 reloadFromBK
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
WHWPConfigExtensionsLib
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
42
 get
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 delete
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 update
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAll
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/*
3 * Jetpack CRM
4 * https://jetpackcrm.com
5 */
6
7class WHWPConfigLib {
8
9    #} Main settings storage
10    private $settings;
11    private $settingsKey         = false;
12    private $settingsVer         = false;
13    private $settingsDefault     = false;
14    private $settingsPlugin      = false;
15    private $settingsPluginVer   = false;
16    private $settingsPluginDBVer = false;
17
18    #} DMZ Settings
19    private $settingsDMZRegister;
20    private $settingsDMZKey = false;
21    private $settingsDMZ;
22
23    #} :)
24    private $whlibVer = '2.0';
25
26    #} added "protected" list of setting keys that don't get reset when resetting to default
27    private $settingsProtected = false;
28
29    #} Constructor
30    function __construct( $config = array() ) {
31
32        #} localise any passed config
33        if ( is_array( $config ) ) {
34
35            if ( isset( $config['conf_key'] ) ) {
36                $this->settingsKey = $config['conf_key'];
37            }
38            if ( isset( $config['conf_ver'] ) ) {
39                $this->settingsVer = $config['conf_ver'];
40            }
41            if ( isset( $config['conf_defaults'] ) ) {
42                $this->settingsDefault = $config['conf_defaults'];
43            }
44            if ( isset( $config['conf_plugin'] ) ) {
45                $this->settingsPlugin = $config['conf_plugin'];
46            }
47            if ( isset( $config['conf_pluginver'] ) ) {
48                $this->settingsPluginVer = $config['conf_pluginver'];
49            }
50            if ( isset( $config['conf_plugindbver'] ) ) {
51                $this->settingsPluginDBVer = $config['conf_plugindbver'];
52            }
53            if ( isset( $config['conf_dmzkey'] ) ) {
54                $this->settingsDMZKey = $config['conf_dmzkey'];
55            }
56            if ( isset( $config['conf_protected'] ) ) {
57                $this->settingsProtected = $config['conf_protected'];
58            }
59        } else {
60            exit( 'WHConfigLib initiated incorrectly.' );
61        }
62
63        #} define dmz settings key
64        #} Set by passed config now $this->settingsDMZKey = $this->settingsKey . '_dmzregister';
65
66        #} Load direct
67        $this->loadFromDB();
68        $this->loadDMZFromDB();
69
70        #} Fill any missing vars
71        $this->validateAndUpdate();
72
73        #} If empty it's first run so init from defaults
74        if ( empty( $this->settings ) ) {
75            $this->initCreate();
76        }
77    }
78
79    #} Checks through defaults + existing and adds defaults where unset
80    function validateAndUpdate() {
81
82        foreach ( $this->settingsDefault as $key => $val ) {
83            if ( ! isset( $this->settings[ $key ] ) ) {
84                $this->update( $key, $val );
85            }
86        }
87    }
88
89    #} Initial Create
90    function initCreate() {
91
92        #} If properly initialised!
93        if ( $settingsKey !== false && $settingsVer !== false && $settingsDefault !== false && $settingsPlugin !== false && $settingsPluginVer !== false ) {
94
95            #} Create + save initial from default
96            #} Following have to be set out of props
97            $defaultOptions               = $this->settingsDefault;
98            $defaultOptions['settingsID'] = $this->settingsVer;
99            $defaultOptions['plugin']     = $this->settingsPlugin;
100            $defaultOptions['version']    = $this->settingsPluginVer;
101            $defaultOptions['db_version'] = $this->settingsPluginDBVer;
102
103            #} Pass back to settings, and save
104            $this->settings = $defaultOptions;
105            $this->saveToDB();
106
107            #} else brutal exit!
108        } else {
109            exit( 'WHConfigLib initiated incorrectly.' );
110        }
111    }
112
113    #} Reset to defaults
114    function resetToDefaults() {
115
116        #} reset to default opts
117        #} NOW with added protection :) any protected field keys wont get re-written
118
119        #} Copy any protected keys over the new reset settings (if is set)
120        $existingSettings = $this->settings;
121        $newSettings      = $this->settingsDefault;
122        if ( isset( $this->settingsProtected ) && is_array( $this->settingsProtected ) ) {
123            foreach ( $this->settingsProtected as $protectedKey ) {
124
125                #} If isset
126                if ( isset( $existingSettings[ $protectedKey ] ) ) {
127
128                    #} Pass it along
129                    $newSettings[ $protectedKey ] = $existingSettings[ $protectedKey ];
130
131                }
132            }
133        }
134
135        #} Save em down
136        $this->settings = $newSettings;
137        $this->saveToDB();
138    }
139
140    #} Get all options as object
141    function getAll( $hardRefresh = false ) {
142
143        if ( $hardRefresh ) {
144            $this->loadFromDB();
145        }
146
147        return $this->settings;
148    }
149
150    #} Get single option
151    function get( $key, $freshFromDB = false ) {
152
153        if ( empty( $key ) === true ) {
154            return false;
155        }
156
157        global $zbs;
158            // db-loading way (ONLY WORKS DB2!)
159            return $zbs->DAL->getSetting(
160                array(
161                    'key'         => $key,
162                    'fullDetails' => false,
163                )
164            );
165    }
166
167    #} Add/Update *brutally
168    function update( $key, $val = '' ) {
169
170        if ( empty( $key ) === true ) {
171            return false;
172        }
173
174        global $zbs;
175
176            #} Don't even check existence as I guess it doesn't matter?
177            $this->settings[ $key ] = $val;
178            $zbs->DAL->updateSetting( $key, $val );
179    }
180
181    #} Delete option
182    function delete( $key ) {
183
184        if ( empty( $key ) === true ) {
185            return false;
186        }
187
188        // remove from settings
189        unset( $this->settings[ $key ] );
190
191        // delete from db
192        global $zbs;
193        return $zbs->DAL->deleteSetting( array( 'key' => $key ) );
194    }
195
196    #} ==================================
197    #} DMZ Config additions
198    #} 2 layers:
199    #} DMZConfig = whole object
200    #} DMZConfigValue = object.value
201    #} ==================================
202
203    #} Get all DMZ (temp func for migration routine DB2, not to be generally used)
204    function dmzGetMigrationSet() {
205
206        return array( $this->settingsDMZKey, $this->settingsDMZRegister, $this->settingsDMZ );
207    }
208
209    #} Get single option
210    function dmzGet( $dmzKey, $confKey ) {
211
212        if ( empty( $dmzKey ) === true || empty( $confKey ) === true ) {
213            return false;
214        }
215
216        #} Assumes it's loaded!?
217        if ( isset( $this->settingsDMZ[ $dmzKey ] ) ) {
218
219            if ( isset( $this->settingsDMZ[ $dmzKey ][ $confKey ] ) ) {
220
221                return $this->settingsDMZ[ $dmzKey ][ $confKey ];
222
223            }
224        }
225
226        return false;
227    }
228
229    #} Delete option
230    function dmzDelete( $dmzKey, $confKey ) {
231
232        if ( empty( $dmzKey ) === true || empty( $confKey ) === true ) {
233            return false;
234        }
235
236        $existingSettings = $this->dmzGetConfig( $dmzKey );
237        $newSettings      = array();
238        if ( isset( $existingSettings ) && is_array( $existingSettings ) ) {
239            foreach ( $existingSettings as $k => $v ) {
240                if ( $k != $confKey ) {
241                    $newSettings[ $k ] = $v;
242                }
243            }
244        }
245
246        #} Brutal
247        $this->settingsDMZ[ $dmzKey ] = $newSettings;
248
249        #} Save down
250        $this->saveToDB();
251    }
252
253    #} Add/Update *brutally
254    function dmzUpdate( $dmzKey, $confKey, $val = '' ) {
255
256        if ( empty( $dmzKey ) === true || empty( $confKey ) === true ) {
257            return false;
258        }
259
260        #} if not set, create
261        if ( ! isset( $this->settingsDMZ[ $dmzKey ] ) ) {
262
263            #} add to register
264            $this->settingsDMZRegister[ $dmzKey ] = $dmzKey;
265
266            #} Create as arr
267            $this->settingsDMZ[ $dmzKey ] = array();
268
269        }
270
271        #} Don't even check existence as I guess it doesn't matter?
272        $this->settingsDMZ[ $dmzKey ][ $confKey ] = $val;
273
274        #} Save down
275        $this->saveToDB();
276    }
277
278    #} Get alls option
279    function dmzGetConfig( $dmzKey ) {
280
281        if ( empty( $dmzKey ) === true ) {
282            return false;
283        }
284
285        #} Assumes it's loaded!?
286        if ( isset( $this->settingsDMZ[ $dmzKey ] ) ) {
287
288            return $this->settingsDMZ[ $dmzKey ];
289
290        }
291
292        return false;
293    }
294
295    #} Delete Config
296    function dmzDeleteConfig( $dmzKey ) {
297
298        if ( empty( $dmzKey ) === true ) {
299            return false;
300        }
301
302        #} Brutal
303        unset( $this->settingsDMZ[ $dmzKey ] );
304        unset( $this->settingsDMZRegister[ $dmzKey ] );
305
306        #} Save down
307        $this->saveToDB();
308    }
309
310    #} Add/Update Config *brutally
311    function dmzUpdateConfig( $dmzKey, $config ) {
312
313        if ( empty( $dmzKey ) === true || empty( $config ) === true ) {
314            return false;
315        }
316
317        #} if not set, create
318        if ( ! isset( $this->settingsDMZ[ $dmzKey ] ) ) {
319
320            #} add to register
321            $this->settingsDMZRegister[ $dmzKey ] = $dmzKey;
322
323        }
324
325        // DEBUG echo 'type "'.$this->settingsDMZRegister.'" = "'.$dmzKey.'" ("'.gettype($this->settingsDMZRegister).'")<br>';
326
327        #} Just brutally override
328        $this->settingsDMZ[ $dmzKey ] = $config;
329
330        #} Save down
331        $this->saveToDB();
332    }
333
334    #} Load/Reload DMZ options from db
335    function loadDMZFromDB() {
336
337        global $zbs;
338
339            #} Load the register
340            $this->settingsDMZRegister = $zbs->DAL->setting( $this->settingsDMZKey, array() );
341
342            // DEBUG echo 'loaded reg = "'; print_r($this->settingsDMZRegister); echo '"!';
343
344            #} This catches weirdo mis-saves?!
345        if ( ! is_array( $this->settingsDMZRegister ) ) {
346            $this->settingsDMZRegister = array();
347        }
348
349            #} Load anything logged in register
350        if ( is_array( $this->settingsDMZRegister ) && count( $this->settingsDMZRegister ) > 0 ) {
351            foreach ( $this->settingsDMZRegister as $regEntry ) {
352
353                    #} Load it
354                    $this->settingsDMZ[ $regEntry ] = $zbs->DAL->setting( $this->settingsDMZKey . '_' . $regEntry );
355
356            }
357        }
358            return $this->settingsDMZ;
359    }
360
361    #} / DMZ Fields
362
363    #} Save back to db
364    function saveToDB() {
365
366        global $zbs;
367
368            // DAL2 saves individually :)
369            $u = array();
370        if ( count( $this->settings ) > 0 ) {
371            foreach ( $this->settings as $settingKey => $settingVal ) {
372
373                $u[] = $zbs->DAL->updateSetting( $settingKey, $settingVal );
374
375            }
376        }
377
378            #} Also any DMZ's! (Brutal big saves - whole objs)
379
380                #} save register
381                $zbs->DAL->updateSetting( $this->settingsDMZKey, $this->settingsDMZRegister );
382                // DEBUG echo 'saved dmzregister:'; print_r($this->settingsDMZRegister); echo '!';
383        if ( isset( $this->settingsDMZRegister ) && is_array( $this->settingsDMZRegister ) ) {
384            foreach ( $this->settingsDMZRegister as $dmzKey ) { # => $dmzVal
385
386                        $u[] = $zbs->DAL->updateSetting( $this->settingsDMZKey . '_' . $dmzKey, $this->settingsDMZ[ $dmzKey ] );
387
388            }
389        }
390
391                return $u;
392    }
393
394    #} Load/Reload from db
395    function loadFromDB() {
396
397        global $zbs;
398
399            $this->settings = $zbs->DAL->getSettings(
400                array(
401                    'autoloadOnly' => true,
402                    'fullDetails'  => false,
403                )
404            );
405            return $this->settings;
406    }
407
408    #} Uninstall func - effectively creates a bk then removes its main setting
409    function uninstall() {
410
411        #} Set uninstall flag
412        $this->settings['uninstall'] = time();
413
414        #} Backup
415        $this->createBackup( 'Pre-UnInstall Backup' );
416
417        #} Blank it out
418        $this->settings = null;
419
420            // DAL2
421            // leave for now
422            return true;
423    }
424
425    #} Backup existing settings obj (ripped from sgv2.0)
426    function createBackup( $backupLabel = '' ) {
427
428        // Left this the same for DAL2 - is still storing backups in wp db
429
430        $existingBK = get_option( $this->settingsKey . '_bk' );
431        if ( ! is_array( $existingBK ) ) {
432            $existingBK = array();
433        }
434        $existingBK[ time() ] = array(
435            'main'   => $this->settings,
436            'dmzreg' => $this->settingsDMZRegister,
437            'dmz'    => $this->settingsDMZ,
438        );
439        if ( ! empty( $backupLabel ) ) {
440            $existingBK[ time() ]['backupLabel'] = sanitize_text_field( $backupLabel ); #} For named settings bk
441        }
442        update_option( $this->settingsKey . '_bk', $existingBK, false );
443        return $existingBK[ time() ];
444    }
445
446    #} Kills all bks
447    function killBackups() {
448
449        return delete_option( $this->settingsKey . '_bk' );
450    }
451
452    #} Retrieve BKs
453    function getBKs() {
454
455        $x = get_option( $this->settingsKey . '_bk' );
456
457        if ( is_array( $x ) ) {
458            return $x;
459        } else {
460            return array();
461        }
462    }
463
464    #} Reload from BK (bkkey will be a timestamp, use getBKs to list these keys)
465    function reloadFromBK( $bkkey ) {
466
467        $backups = get_option( $this->settingsKey . '_bk' );
468
469        if ( isset( $backups[ $bkkey ] ) ) {
470            if ( is_array( $backups[ $bkkey ] ) ) {
471
472                #} kill existing settings and use backed up ones
473                $this->settings = $backups[ $bkkey ];
474
475                #} Save
476                $this->saveToDB();
477
478                return true;
479
480            }
481        }
482
483        return false;
484    }
485}
486
487    #} This is a wrapper/factory class which simplifies using DMZ fields for extension plugins
488class WHWPConfigExtensionsLib {
489
490    #} key holder
491    private $extperma         = false;
492    private $settingsObj      = false;
493    private $existingSettings = false;
494
495    #} Constructor
496    function __construct( $extperma = '', $defaultConfig = array() ) {
497
498        if ( ! empty( $extperma ) ) {
499
500            #} store
501            $this->extperma = 'ext_' . $extperma;
502
503            #} initiate settings obj as a dmz set
504            // WH move to $zbs->settings
505            // ALSO now covered by // LEGACY SUPPORT (see ZeroBSCRM.php)
506            global $zbs;
507
508            if ( isset( $zbs->settings ) && ! empty( $zbs->settings ) ) {
509
510                $existingSettings = $zbs->settings->dmzGetConfig( $this->extperma );
511
512            } else {
513
514                // legacy - older plugins not using new init hooks
515                // pass back empty for now (will break them)
516                // and notify
517                $existingSettings = array();
518
519                // notify
520                // can't do directly, as this is PRE INIT (no logged in user)
521                // so delay... zeroBSCRM_notifyme_insert_notification(get_current_user_id(),-999,-1,'extension.update');
522                // weird this doesn't work either... day of it.add_action('before_zerobscrm_init', 'zeroBS_temp_ext_legacy_notice');
523                // gonna do this grossly:
524                if ( ! defined( 'ZBSTEMPLEGACYNOTICE' ) ) {
525                    define( 'ZBSTEMPLEGACYNOTICE', 1 );
526                }
527
528                // this is a hotfix 2.50.1 to work with // LEGACY SUPPORT
529                // ... it'll add the setting to a pile to be reconstructed post init :)
530                global $zbsLegacySupport;
531                $zbsLegacySupport['extsettingspostinit'][ $this->extperma ] = $defaultConfig;
532
533            }
534
535            #} Create if not existing
536            if ( ! is_array( $existingSettings ) ) {
537
538                #} init
539                $zbs->settings->dmzUpdateConfig( $this->extperma, $defaultConfig );
540
541            }
542        } else {
543            exit( 'WHConfigLib initiated incorrectly.' );
544        }
545    }
546
547    #} passthrough funcs
548
549    function get( $key ) {
550
551        // global $zbs;
552        // return $zeroBSCRM_Settings->dmzGet($this->extperma,$key);
553
554        // WH move to $zbs->settings
555        global $zbs;
556        return $zbs->settings->dmzGet( $this->extperma, $key );
557    }
558
559    function delete( $key ) {
560
561        // global $zbs;
562        // return $zeroBSCRM_Settings->dmzDelete($this->extperma,$key);
563
564        // WH move to $zbs->settings
565        global $zbs;
566        return $zbs->settings->dmzDelete( $this->extperma, $key );
567    }
568
569    function update( $key, $val = '' ) {
570
571        // global $zbs;
572        // return $zeroBSCRM_Settings->dmzUpdate($this->extperma,$key,$val);
573
574        // WH move to $zbs->settings
575        global $zbs;
576        return $zbs->settings->dmzUpdate( $this->extperma, $key, $val );
577    }
578
579    function getAll() {
580
581        // global $zbs;
582        // return $zeroBSCRM_Settings->dmzGetConfig($this->extperma);
583
584        // WH move to $zbs->settings
585        global $zbs;
586        return $zbs->settings->dmzGetConfig( $this->extperma );
587    }
588}
589
590function zeroBS_temp_ext_legacy_notice() {
591
592    // add one menu item, even if multiple ext.
593    if ( ! defined( 'ZBSLEGACYSET' ) ) {
594        $o = get_option( 'zbs_temp_legacy_update_msg' );
595        if ( $o == false ) {
596            zeroBSCRM_notifyme_insert_notification( get_current_user_id(), -999, -1, 'extension.update' );
597            update_option( 'zbs_temp_legacy_update_msg', 1, false );
598        }
599        define( 'ZBSLEGACYSET', 1 );
600    }
601}