File size: 2,401 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Note: This is used by env.js and so do not require( 'debug' ) in the global context otherwise it prevents env.js setting debug up

const Config = require( '../config' );
const constants = require( './constants' );
const settingsFile = require( './settings-file' );

/**
 * Module variables
 */
let settings = false;

function Settings() {
	this.settings = false;
}

Settings.prototype._getAll = function () {
	if ( this.settings === false ) {
		this.settings = settingsFile.load();
	}

	return this.settings;
};

Settings.prototype.isDebug = function () {
	if ( typeof this._getAll().debug !== 'undefined' ) {
		return this._getAll().debug;
	}
	return Config.debug.enabled_by_default;
};

/*
 * Get a single setting value
 * If no setting is present then fall back to the `default_settings`
 * If no default setting then fall back to false
 */
Settings.prototype.getSetting = function ( setting ) {
	if ( process.env.WP_DESKTOP_BASE_URL !== undefined && setting === constants.LAST_LOCATION ) {
		return process.env.WP_DESKTOP_BASE_URL;
	}

	const value = this._getAll()[ setting ];

	if ( typeof value === 'undefined' ) {
		if ( typeof Config.default_settings[ setting ] !== 'undefined' ) {
			return Config.default_settings[ setting ];
		}
		return false;
	}

	return value;
};

/*
 * Get a group of settings
 */
Settings.prototype.getSettingGroup = function ( existing, group, values ) {
	const settingsGroup = this._getAll()[ group ];

	if ( typeof settingsGroup !== 'undefined' ) {
		if ( values instanceof Array ) {
			const updated = {};
			for ( let x = 0; x < values.length; x++ ) {
				const value = values[ x ];
				existing[ value ] = settingsGroup[ value ];
				updated[ value ] = settingsGroup[ value ];
			}
		} else {
			return settingsGroup;
		}
	}

	return existing;
};

Settings.prototype.saveSetting = function ( group, groupData ) {
	if ( process.env.WP_DESKTOP_DEBUG !== undefined && group === constants.LAST_LOCATION ) {
		return;
	}
	this.settings = settingsFile.save( group, groupData );
};

// Do not send function and DOM objects (exception in Electron v9).
Settings.prototype.toRenderer = function () {
	const all = this._getAll();
	const exported = {};
	if ( all ) {
		for ( const [ key ] of Object.entries( all ) ) {
			exported[ key ] = this.getSetting( key );
		}
	}
	return exported;
};

if ( ! settings ) {
	settings = new Settings();
}

module.exports = settings;