This repository has been archived by the owner on Mar 26, 2019. It is now read-only.
forked from maithemewp/mai-theme-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmai-pro-engine.php
226 lines (191 loc) · 6 KB
/
mai-pro-engine.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
<?php
/**
* Plugin Name: Mai Pro Engine
* Plugin URI: https://maitheme.com/
* Description: This plugin only exists when older versions of Mai Theme or Mai Pro point to the older engine repository. Once Mai Theme Engine is installed and activated, this plugin can safely be deactivated and deleted.
*
* Version: 2.0.0
*
* Author: MaiTheme.com
* Author URI: https://maitheme.com
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
final class Mai_Engine_Installer {
/**
* @var Mai_Engine_Installer The one true Mai_Engine_Installer
* @since 1.0.0
*/
private static $instance;
private $engine_running;
private $file;
private $config;
function __construct() {
$this->engine_running = false;
$this->file = get_stylesheet_directory() . '/includes/dependencies/wp-dependencies.json';
$this->config = array(
'name' => 'Mai Theme Engine',
'host' => 'github',
'slug' => 'mai-theme-engine/mai-theme-engine.php',
'uri' => 'maithemewp/mai-theme-engine',
'branch' => 'master',
'optional' => false,
'token' => null,
);
}
/**
* Main Mai_Engine_Installer Instance.
*
* Insures that only one instance of Mai_Engine_Installer exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since 1.0.0
* @static var array $instance
* @see Mai_Engine_Installer()
* @return object | Mai_Engine_Installer The one true Mai_Engine_Installer
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
// Setup the setup
self::$instance = new Mai_Engine_Installer;
// Methods
self::$instance->setup_constants();
self::$instance->includes();
self::$instance->hooks();
}
return self::$instance;
}
/**
* Setup plugin constants.
*
* @access private
* @return void
*/
private function setup_constants() {
define( 'MAI_THEME_ENGINE_INSTALLER_VERSION', '2.0.0' );
}
/**
* Load the necessary files.
* Setup the updater.
*
* @uses https://github.com/YahnisElsts/plugin-update-checker/
* @uses https://github.com/afragen/wp-dependency-installer/
*
* @return void
*/
function includes() {
require_once plugin_dir_path( __FILE__ ) . 'includes/wp-dependency-installer.php'; // v 1.3.2
require_once plugin_dir_path( __FILE__ ) . 'includes/plugin-update-checker/plugin-update-checker.php'; // v 4.4
$updater = Puc_v4_Factory::buildUpdateChecker( 'https://github.com/maiprowp/mai-pro-engine/', __FILE__, 'mai-pro-engine' );
WP_Dependency_Installer::instance()->register( array( $this->config ) );
$this->engine_running = true;
}
/**
* Run the hooks and function.
*
* @return void
*/
public function hooks() {
add_action( 'plugins_loaded', array( $this, 'write' ) );
add_action( 'admin_init', array( $this, 'deactivate' ) );
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
}
/**
* Force install of the correct Mai Theme Engine plugin.
* Check the existing child theme json file and update as needed.
*
* @return void
*/
function write() {
// Bail if file doesn't exist.
if ( ! file_exists( $this->file ) ) {
return;
}
$update_file = false;
// Load the dependencies file.
$contents = file_get_contents( $this->file );
// Decode the JSON data into a PHP array.
$decoded = json_decode( $contents, true );
if ( empty( $decoded ) ) {
$update_file = true;
// Build the new engine location array.
$decoded[] = $this->config;
} else {
// Loop through the dependencies.
foreach ( (array) $decoded as $key => $value ) {
// Skip if not the ones we want.
if ( ! isset( $value['uri'] ) || ! in_array( $value['uri'], array( 'maiprowp/mai-pro-engine', 'bizbudding/mai-pro-engine' ) ) ) {
continue;
}
$update_file = true;
// Build the new engine location array.
$decoded[ $key ] = $this->config;
}
}
if ( ! $update_file ) {
return;
}
// Encode the array back into a JSON string.
$json = json_encode( $decoded, JSON_UNESCAPED_SLASHES );
// Save the file.
file_put_contents( $this->file, $json );
}
/**
* Check that the JSON file updated accordingly,
* and deactivate the plugin.
*/
function deactivate() {
// Bail if file doesn't exist.
if ( ! file_exists( $this->file ) ) {
return;
}
$file_correct = false;
// Load the dependencies file.
$contents = file_get_contents( $this->file );
// Decode the JSON data into a PHP array.
$decoded = json_decode( $contents, true );
// Loop through the dependencies.
foreach ( (array) $decoded as $key => $value ) {
// If depency is set correctly, deactivate this plugin.
if ( isset( $value['uri'] ) && ( 'maithemewp/mai-theme-engine' == $value['uri'] ) ) {
$file_correct = true;
}
}
// Old engine is not active, new engine is active, and the theme file has been updated.
if ( $this->engine_running && $file_correct ) {
// Deactivate plugins. Best on 'admin_init'.
deactivate_plugins( plugin_basename( __FILE__ ) );
}
}
/**
* Show an admin notice with a link to refresh the page,
* this should trigger the engine install, if it didn't trigger the first time
* during installation of this plugin.
*/
function admin_notices() {
if ( class_exists( 'Mai_Theme_Engine' ) ) {
return;
}
$notice = sprintf( '<strong>' . __( 'Please %s to complete the Mai Theme Engine installation.', 'mai-pro-engine' ) . '</strong>', '<a href="' . get_permalink() . '">click here</a>' );
printf( '<div class="notice notice-error is-dismissible"><p>%s</p></div>', $notice );
// Remove "Plugin activated" notice.
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
}
/**
* The main function for that returns Mai_Engine_Installer
*
* @return object|Mai_Engine_Installer The one true Mai_Engine_Installer Instance.
*/
function Mai_Engine_Installer() {
if ( ! is_admin() ) {
return;
}
return Mai_Engine_Installer::instance();
}
// Get Mai_Engine_Installer Running.
Mai_Engine_Installer();