forked from benbranyon/wp-cfm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-cfm.php
295 lines (237 loc) · 9.71 KB
/
wp-cfm.php
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
<?php
/*
Plugin Name: WP-CFM
Plugin URI: https://forumone.github.io/wp-cfm/
Description: WordPress Configuration Management
Version: 1.6
Author: Forum One
Author URI: http://forumone.com/
License: GPLv3
Copyright 2016 Forum One
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
defined( 'ABSPATH' ) or exit;
if (PHP_VERSION_ID >= 50604) {
require_once __DIR__ . '/vendor/autoload.php';
}
class WPCFM_Core
{
public $readwrite;
public $registry;
public $options;
public $helper;
private $pantheon_env = '';
private static $instance;
function __construct() {
// setup variables
define( 'WPCFM_VERSION', '1.6' );
define( 'WPCFM_DIR', dirname( __FILE__ ) );
$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 = $_SERVER['DOCUMENT_ROOT'] . '/private/config';
$config_url = WP_HOME . '/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 = [];
// If we are in a Pantheon environment, set the 3 instances slugs out of the box.
if ( !empty( $this->pantheon_env ) ) {
$environments = ['dev', 'test', 'live'];
}
return apply_filters( 'wpcfm_multi_env', $environments );
}
/**
* Defines the current environment.
* @return string
*/
private function set_current_env() {
// Get Compare Env when rendering the settings page.
if ( !wp_doing_ajax() || !defined( 'WP_CLI' ) ) {
$compare_env = filter_input( INPUT_GET, "compare_env", FILTER_SANITIZE_STRING );
if ( $compare_env ) {
define( 'WPCFM_COMPARE_ENV', $compare_env );
}
}
// Get Compare Env when doing AJAX.
if ( wp_doing_ajax() ) {
$compare_env = filter_input( INPUT_POST, "compare_env", FILTER_SANITIZE_STRING );
if ( $compare_env && in_array( $compare_env, WPCFM_REGISTER_MULTI_ENV ) ) {
return $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',
[]
);
// 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__ ), [ 'jquery' ], WPCFM_VERSION );
wp_enqueue_script( 'wpcfm-diff-match-patch', plugins_url( "assets/js/pretty-text-diff/diff_match_patch{$min}.js", __FILE__ ), [ 'jquery' ], WPCFM_VERSION );
wp_enqueue_script( 'wpcfm-pretty-text-diff', plugins_url( "assets/js/pretty-text-diff/jquery.pretty-text-diff{$min}.js", __FILE__ ), [ 'jquery' ], WPCFM_VERSION );
wp_enqueue_script( 'wpcfm-admin-js', plugins_url( "assets/js/admin{$min}.js", __FILE__ ), [ 'jquery', 'wpcfm-multiselect', 'wpcfm-pretty-text-diff' ], WPCFM_VERSION );
// Safely get env value from plugin backend URL, if exists.
$compare_env = filter_input( INPUT_GET, "compare_env", FILTER_SANITIZE_STRING );
wp_localize_script( 'wpcfm-admin-js', 'compare_env', $compare_env );
wp_enqueue_style( 'wpcfm-admin', plugins_url( "assets/css/admin{$min}.css", __FILE__ ), [], WPCFM_VERSION );
}
}
/**
* Route to the correct edit screen
*/
function settings_page() {
include( WPCFM_DIR . '/templates/page-settings.php' );
}
/**
* i18n support
*/
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/' );
}
}
}
WPCFM();
/**
* Allow direct access to WPCFM classes
* For example, use WPCFM()->options to access WPCFM_Options
*/
function WPCFM() {
return WPCFM_Core::instance();
}