forked from benbranyon/wp-cfm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-cfm.php
More file actions
340 lines (281 loc) · 9.31 KB
/
Copy pathwp-cfm.php
File metadata and controls
340 lines (281 loc) · 9.31 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* WP-CFM
*
* @category General
* @package WPCFM
* @author Forum One <wordpress@forumone.com>
* @copyright 2016 Forum One
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
* @link https://github.com/forumone
*
* @wordpress-plugin
* Plugin Name: WP-CFM
* Plugin URI: https://forumone.github.io/wp-cfm/
* Description: WordPress Configuration Management
* Version: 1.7.10
* Requires at least: 5.0
* Requires PHP: 7.4
* Author: Forum One
* Author URI: http://forumone.com/
* Text Domain: wp-cfm
* Domain Path: /languages
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* GitHub Plugin URI: https://github.com/forumone/wp-cfm
*/
defined( 'ABSPATH' ) or exit;
if ( PHP_VERSION_ID >= 50604 ) {
require_once __DIR__ . '/vendor/autoload.php';
}
class WPCFM_Core {
const VERSION = '1.7.10';
public $readwrite;
public $registry;
public $options;
public $helper;
private $pantheon_env = '';
private static $instance;
function __construct() {
// setup variables
define( 'WPCFM_VERSION', self::VERSION );
define( 'WPCFM_DIR', __DIR__ );
$config_dir = WP_CONTENT_DIR . '/config';
$config_url = WP_CONTENT_URL . '/config';
// Check if we are on Pantheon hosting environment.
if ( defined( 'PANTHEON_ENVIRONMENT' ) ) {
// Set the Pantheon environment to test or live
if ( in_array( PANTHEON_ENVIRONMENT, array( 'test', 'live' ) ) ) {
$this->pantheon_env = PANTHEON_ENVIRONMENT;
// Otherwise, default to dev for dev and multidev
} else {
$this->pantheon_env = 'dev';
}
// Change the config directory to private/config on Pantheon
$config_dir = untrailingslashit( $_ENV['DOCROOT'] ) . '/private/config';
$config_url = home_url() . '/private/config';
}
// Register multiple environments.
define( 'WPCFM_REGISTER_MULTI_ENV', $this->set_multi_env() );
// If multiple environments were defined.
if ( ! empty( WPCFM_REGISTER_MULTI_ENV ) ) {
// Set the current environment where the WordPress site is running.
define( 'WPCFM_CURRENT_ENV', $this->set_current_env() );
// If we have an env name, append it to create a subfolder inside wp-content/config/ directory.
if ( ! empty( WPCFM_CURRENT_ENV ) ) {
$config_dir .= '/' . WPCFM_CURRENT_ENV;
$config_url .= '/' . WPCFM_CURRENT_ENV;
}
}
define( 'WPCFM_CONFIG_DIR', apply_filters( 'wpcfm_config_dir', $config_dir ) );
define( 'WPCFM_CONFIG_URL', apply_filters( 'wpcfm_config_url', $config_url ) );
if ( PHP_VERSION_ID < 50604 ) {
define( 'WPCFM_CONFIG_FORMAT', 'json' );
define( 'WPCFM_CONFIG_FORMAT_REQUESTED', apply_filters( 'wpcfm_config_format', 'json' ) );
} else {
define( 'WPCFM_CONFIG_FORMAT', apply_filters( 'wpcfm_config_format', 'json' ) );
}
define( 'WPCFM_CONFIG_USE_YAML_DIFF', apply_filters( 'wpcfm_config_use_yaml_diff', true ) );
define( 'WPCFM_URL', plugins_url( '', __FILE__ ) );
// WP is loaded
add_action( 'init', array( $this, 'init' ), 1 );
}
/**
* Enables multi environment feature on WP-CFM.
*
* @return array
*/
private function set_multi_env() {
$environments = array();
// If we are in a Pantheon environment, set the 3 instances slugs out of the box.
if ( ! empty( $this->pantheon_env ) ) {
$environments = array( 'dev', 'test', 'live' );
}
return apply_filters( 'wpcfm_multi_env', $environments );
}
/**
* Defines the current environment.
*
* @return string
*/
private function set_current_env() {
$compare_env = null;
// Get Compare Env when rendering the settings page.
if ( ! wp_doing_ajax() || ! defined( 'WP_CLI' ) ) {
if ( isset( $_GET['compare_env'] ) ) {
$compare_env = $this->filter_string_polyfill( $_GET['compare_env'] );
}
if ( ! empty( $compare_env ) ) {
define( 'WPCFM_COMPARE_ENV', $compare_env );
}
}
// Get Compare Env when doing AJAX.
if ( wp_doing_ajax() ) {
if ( isset( $_POST['compare_env'] ) ) {
$compare_env = $this->filter_string_polyfill( $_POST['compare_env'] );
if ( ! empty( $compare_env ) && in_array( $compare_env, WPCFM_REGISTER_MULTI_ENV ) ) {
return strval( $compare_env );
}
}
}
return apply_filters( 'wpcfm_current_env', $this->pantheon_env );
}
/**
* Initialize the singleton
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Initialize classes and WP hooks
*/
function init() {
// i18n
$this->load_textdomain();
// hooks
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'network_admin_menu', array( $this, 'network_admin_menu' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
// includes
foreach ( array( 'options', 'readwrite', 'registry', 'helper', 'ajax' ) as $class ) {
include WPCFM_DIR . "/includes/class-$class.php";
}
// WP-CLI
if ( defined( 'WP_CLI' ) && WP_CLI ) {
include WPCFM_DIR . '/includes/class-wp-cli.php';
}
$this->options = new WPCFM_Options();
$this->readwrite = new WPCFM_Readwrite();
$this->registry = new WPCFM_Registry();
$this->helper = new WPCFM_Helper();
$ajax = new WPCFM_Ajax();
// Make sure is_plugin_active() is available
include_once ABSPATH . 'wp-admin/includes/plugin.php';
// Third party integrations
$integrations = scandir( WPCFM_DIR . '/includes/integrations' );
foreach ( $integrations as $filename ) {
if ( '.' != substr( $filename, 0, 1 ) ) {
include WPCFM_DIR . "/includes/integrations/$filename";
}
}
// Set Plugin's options tracked with WP-CFM to load their values from the bundled JSON files.
if ( apply_filters( 'wpcfm_is_ssot', false ) ) {
$this->set_as_ssot();
}
}
/**
* Set WP-CFM file bundle's config as the Single Source of Truth.
* Override DB values for all tracked options.
*/
private function set_as_ssot() {
$file_bundles = WPCFM()->helper->get_file_bundles();
if ( $file_bundles ) {
$plugin_opts = array_reduce(
array_column( $file_bundles, 'config' ),
'array_merge',
array()
);
// Loop available plugin options and a pre_option_{$option} filter for them.
foreach ( $plugin_opts as $key => $value ) {
add_filter(
'pre_option_' . $key,
function ( $pre ) use ( $value ) {
return maybe_unserialize( $value );
}
);
}
}
}
/**
* Register the settings page
*/
function admin_menu() {
add_options_page( 'WP-CFM', 'WP-CFM', 'manage_options', 'wpcfm', array( $this, 'settings_page' ) );
}
/**
* Register the multi-site settings page
*/
function network_admin_menu() {
add_submenu_page( 'settings.php', 'WP-CFM', 'WP-CFM', 'manage_options', 'wpcfm', array( $this, 'settings_page' ) );
}
/**
* Enqueue WP-CFM admin styles and javascript.
*/
function admin_scripts( $hook ) {
// Exit this funtion if doing AJAX.
if ( wp_doing_ajax() ) {
return;
}
if ( 'settings_page_wpcfm' == $hook ) {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_style( 'media-views' );
wp_enqueue_script( 'wpcfm-multiselect', plugins_url( "assets/js/multiselect/jquery.multiselect{$min}.js", __FILE__ ), array( 'jquery' ), WPCFM_VERSION );
wp_enqueue_script( 'wpcfm-diff-match-patch', plugins_url( "assets/js/pretty-text-diff/diff_match_patch{$min}.js", __FILE__ ), array( 'jquery' ), WPCFM_VERSION );
wp_enqueue_script( 'wpcfm-pretty-text-diff', plugins_url( "assets/js/pretty-text-diff/jquery.pretty-text-diff{$min}.js", __FILE__ ), array( 'jquery' ), WPCFM_VERSION );
wp_enqueue_script( 'wpcfm-admin-js', plugins_url( "assets/js/admin{$min}.js", __FILE__ ), array( 'jquery', 'wpcfm-multiselect', 'wpcfm-pretty-text-diff' ), WPCFM_VERSION );
// Safely get env value from plugin backend URL, if exists.
$compare_env = isset( $_GET['compare_env'] )
? sanitize_text_field( $_GET['compare_env'] )
: '';
wp_localize_script(
'wpcfm-admin-js',
'compare_env',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'env' => $compare_env,
'wpcfm_ajax_nonce' => wp_create_nonce( 'wpcfm_ajax_nonce' ),
)
);
wp_enqueue_style( 'wpcfm-admin', plugins_url( "assets/css/admin{$min}.css", __FILE__ ), array(), WPCFM_VERSION );
}
}
/**
* Route to the correct edit screen
*/
function settings_page() {
include WPCFM_DIR . '/templates/page-settings.php';
}
/**
* i18n support.
*
* @return void
*/
function load_textdomain() {
$locale = apply_filters( 'plugin_locale', get_locale(), 'wpcfm' );
$mofile = WP_LANG_DIR . '/wpcfm-' . $locale . '.mo';
if ( file_exists( $mofile ) ) {
load_textdomain( 'wpcfm', $mofile );
} else {
load_plugin_textdomain( 'wpcfm', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
}
/**
* Deprecated `FILTER_SANITIZE_STRING` polyfill.
*
* @param string $string A raw string that should be sanitized.
*
* @return string|bool
*/
private function filter_string_polyfill( string $string ) {
$str = preg_replace( '/\x00|<[^>]*>?/', '', $string );
if ( empty( $str ) ) {
return false;
}
return str_replace(
array( "'", '"' ),
array( ''', '"' ),
$str
);
}
}
WPCFM();
/**
* Allow direct access to WPCFM classes
* For example, use WPCFM()->options to access WPCFM_Options
*/
function WPCFM() {
return WPCFM_Core::instance();
}