forked from WordPress/theme-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire-gutenberg.php
261 lines (224 loc) · 7.24 KB
/
require-gutenberg.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
<?php
/**
* Require Gutenberg
*
* @package WPThemes/Require-Gutenberg-FSE
*/
if ( ! class_exists( 'WPThemes_Require_Gutenberg' ) ) {
/**
* Init Gutenberg requirement.
*
* @since 1.0.0
*/
class WPThemes_Require_Gutenberg {
/**
* Add hooks for this class.
*
* @access public
*
* @since 1.0.0
*
* @return void
*/
public function run() {
// Make sure we have the plugin-checking functions we need.
if ( ! function_exists( 'is_plugin_active' ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// Add the notice.
add_action( 'admin_notices', [ $this, 'admin_notice' ] );
// Print scripts in the footer.
add_action( 'admin_footer', [ $this, 'the_script' ] );
// Print styles in the footer.
add_action( 'admin_footer', [ $this, 'the_styles' ] );
// Handle activating Gutenberg via AJAX.
add_action( 'wp_ajax_wpthemes_require_gutenberg_activate_plugin', [ $this, 'activate_plugin' ] );
}
/**
* The admin notice.
*
* @access public
*
* @return void
*/
public function admin_notice() {
// Figure out the current step we're on.
$active_step = false;
if ( ! $this->is_plugin_installed() ) {
$active_step = 'install';
} elseif ( ! $this->is_plugin_active() ) {
$active_step = 'activate';
}
// Early exit if there's nothing to do.
if ( ! $active_step ) {
return;
}
?>
<div class="notice notice-warning require-gutenberg-notice-wrapper notice-alt active-step-<?php echo esc_attr( $active_step ); ?>">
<p><?php esc_html_e( 'This is an experimental theme and requires the Gutenberg plugin to be installed.', 'textdomain' ); ?></p>
<div class="require-gutenberg require-gutenberg-install">
<p><?php esc_html_e( 'The Gutenberg plugin is not installed. Click the button below to install it.', 'textdomain' ); ?></p>
<p><button class="button" onclick="wpThemesRequireGutenberg.installPlugin();" aria-label="<?php esc_attr_e( 'Install Gutenberg', 'textdomain' ); ?>"><?php esc_html_e( 'Install Gutenberg', 'textdomain' ); ?></button></p>
</div>
<div class="require-gutenberg require-gutenberg-activate">
<p><?php esc_html_e( 'The Gutenberg plugin is installed but not activated. Click the button below to enable the plugin.', 'textdomain' ); ?></p>
<p><button class="button" onclick="wpThemesRequireGutenberg.activatePlugin();"><?php esc_html_e( 'Activate Plugin.', 'textdomain' ); ?></button></p>
</div>
<div class="require-gutenberg require-gutenberg-success">
<p><?php esc_html_e( 'Congratulations! All steps required were completed. Enjoy your Full Site Editing experience.', 'textdomain' ); ?></p>
</div>
</div>
<?php
}
/**
* Print styles for our notice.
*
* @access public
*
* @return void
*/
public function the_styles() {
?>
<style>
.notice .require-gutenberg { position: relative; display: none; }
.notice.active-step-install .require-gutenberg-install { display: block; }
.notice.active-step-activate .require-gutenberg-activate { display: block; }
.notice.active-step-success .require-gutenberg-success { display: block; }
.notice .require-gutenberg:after { display: none; }
</style>
<?php
}
/**
* Print script for our notice.
*
* @access public
*
* @return void
*/
public function the_script() {
?>
<script>
wpThemesRequireGutenberg = {
/**
* Install Gutenberg.
*/
installPlugin: function() {
// Tweak the button.
jQuery( '.notice .require-gutenberg-install .button' )
.html( '<?php esc_html_e( 'Installing Gutenberg...', 'textdomain' ); ?>' )
.addClass( 'updating-message' )
.attr( 'aria-label', '<?php esc_attr_e( 'Installing Gutenberg...', 'textdomain' ); ?>' );
// Install the plugin.
wp.updates.installPlugin( {
slug: 'gutenberg',
success: function() {
// Remove the "install" step.
jQuery( '.notice .require-gutenberg-install' ).remove();
// Switch classes and move on to the next step.
jQuery( '.require-gutenberg-notice-wrapper' )
.removeClass( 'active-step-install' )
.addClass( 'active-step-activate' );
},
error: function( e ) {
// Remove previous errors.
jQuery( '.require-gutenberg-install .error' ).remove();
jQuery( '.require-gutenberg-install' )
.append( '<div class="error"><p><?php esc_html_e( 'An error occured:', 'textdomain' ); ?></p><p>' + e.message + '</p><p><?php esc_html_e( 'Please check your browser console for more details', 'textdomain' ); ?></p></div>' );
console.log( e );
}
} );
},
/**
* Activate the Gutenberg plugin.
*/
activatePlugin: function() {
// AJAX request to activate the plugin.
jQuery.get( ajaxurl, {
action: 'wpthemes_require_gutenberg_activate_plugin',
nonce: '<?php echo esc_html( wp_create_nonce( 'wpthemes_require_gutenberg' ) ); ?>'
}, function( response ) {
if ( 'success' === response ) {
// Remove the activate step.
jQuery( '.notice .require-gutenberg-activate' ).remove();
// Continue to next step.
jQuery( '.require-gutenberg-notice-wrapper' )
.removeClass( 'active-step-activate' )
.removeClass( 'notice-warning' )
.addClass( 'notice-success' )
.addClass( 'active-step-success' );
} else {
// There was an error.
jQuery( '.require-gutenberg-activate' )
.append( '<div class="error"><p><?php esc_html_e( 'An error occured', 'textdomain' ); ?>:</p><p><?php esc_html_e( 'Could not activate the plugin. Please go to the plugins page on your dashboard and manually activate the plugin.', 'textdomain' ); ?></p></div>' );
}
} );
},
}
</script>
<?php
}
/**
* Check if the plugin is installed.
*
* @access public
*
* @since 1.0.0
*
* @return bool
*/
public function is_plugin_installed() {
$plugins = get_plugins();
return isset( $plugins['gutenberg/gutenberg.php'] );
}
/**
* Check if the plugin is active.
*
* @access public
*
* @since 1.0.0
*
* @return bool
*/
public function is_plugin_active() {
return defined( 'GUTENBERG_VERSION' ) || is_plugin_active( 'gutenberg/gutenberg.php' );
}
/**
* Activates the Gutenberg plugin.
*
* @access public
*
* @return void
*/
public function activate_plugin() {
// Early exit if the user doesn't have the capability to activate plugins.
if ( ! current_user_can( 'activate_plugins' ) ) {
wp_die();
}
// Security check.
check_ajax_referer( 'wpthemes_require_gutenberg', 'nonce' );
// Activate plugin.
$result = activate_plugin( 'gutenberg/gutenberg.php' );
// Plugin was successfully activated. Exit with success message.
if ( ! is_wp_error( $result ) ) {
wp_die( 'success' );
}
// Something went wrong, exit with error message.
wp_die( 'error' );
}
}
}
if ( ! function_exists( 'wpthemes_require_gutenberg_fse' ) ) {
/**
* Load the Gutenberg requirement.
*
* @return void
*/
function wpthemes_require_gutenberg_fse() {
// Instantiate the object.
$init = new WPThemes_Require_Gutenberg();
// Run our actions.
$init->run();
}
}
// Run Gutenberg requirement.
wpthemes_require_gutenberg_fse();