-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathplayground.php
209 lines (185 loc) · 5.17 KB
/
playground.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
<?php
/**
* Plugin Name: Sandbox Site powered by Playground
* Plugin URI: https://github.com/WordPress/playground-tools/tree/trunk/packages/playground
* Description: Clone your WordPress site in Playground. Preview plugins and safely experiment without affecting your live site.
* Author: WordPress Contributors
* Version: 0.1.8
* Requires PHP: 8.0
* License: GPLv2
* Text Domain: playground
*/
namespace WordPress\Playground;
defined('ABSPATH') || exit;
const PLAYGROUND_ADMIN_PAGE_SLUG = 'playground';
const ADMIN_PAGE_CAPABILITY = 'manage_options';
global $wp_version;
define('PLAYGROUND_VERSION', $wp_version);
define('PLAYGROUND_WP_VERSION', $wp_version);
define('PLAYGROUND_PHP_VERSION', implode('.', sscanf(phpversion(), '%d.%d')));
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/src/playground-zip.php';
require __DIR__ . '/src/playground-export.php';
add_action('admin_menu', __NAMESPACE__ . '\plugin_menu');
add_action('admin_init', __NAMESPACE__ . '\init');
add_action('plugins_loaded', __NAMESPACE__ . '\plugins_loaded');
/**
* Initialize the plugin.
*/
function init()
{
add_action('admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_scripts');
add_filter('plugin_install_action_links', __NAMESPACE__ . '\plugin_install_action_links', 10, 2);
}
/**
* Enqueue scripts and styles for the plugin.
*
* @param string $current_screen_id The current screen ID.
*/
function enqueue_scripts($current_screen_id)
{
if ($current_screen_id !== 'tools_page_' . PLAYGROUND_ADMIN_PAGE_SLUG) {
return;
}
wp_enqueue_style('playground', plugin_dir_url(__FILE__) . 'assets/css/playground.css', [], PLAYGROUND_VERSION);
wp_register_script('playground', plugin_dir_url(__FILE__) . 'assets/js/playground.js', [], PLAYGROUND_VERSION, true);
wp_localize_script('playground', 'playground', [
'zipUrl' => esc_url(get_download_page_url()),
'wpVersion' => PLAYGROUND_WP_VERSION,
'phpVersion' => PLAYGROUND_PHP_VERSION,
'playgroundPackageUrl' => apply_filters(
'playground_package_url',
esc_url('https://playground.wordpress.net/client/index.js'),
),
'playgroundRemoteUrl' => apply_filters(
'playground_remote_url',
esc_url('https://playground.wordpress.net/remote.html'),
),
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
'pluginSlug' => isset($_GET['pluginSlug']) ? sanitize_text_field($_GET['pluginSlug']) : false,
'userId' => get_current_user_id(),
]);
wp_enqueue_script('playground');
}
/**
* Get the URL to download the playground package.
*
* @return string The URL to download the playground package.
*/
function get_download_page_url()
{
return add_query_arg(
[
'download' => 1,
],
admin_url('tools.php?page=' . PLAYGROUND_ADMIN_PAGE_SLUG)
);
}
/**
* Collect the WordPress package and package it into a zip file.
*/
function plugins_loaded()
{
global $pagenow;
if ('tools.php' !== $pagenow) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if (!isset($_GET['page']) || PLAYGROUND_ADMIN_PAGE_SLUG !== $_GET['page']) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if (!isset($_GET['download'])) {
return;
}
download_snapshot();
}
/**
* Download the Playground snapshot.
*/
function download_snapshot()
{
if (!is_admin()) {
return;
}
if (!current_user_can(ADMIN_PAGE_CAPABILITY)) {
return;
}
zip_collect();
wp_die();
}
/**
* Add the WordPress Playground page to the Tools menu.
*/
function plugin_menu()
{
add_submenu_page(
'tools.php',
'WordPress Playground',
'Sandbox Site',
ADMIN_PAGE_CAPABILITY,
PLAYGROUND_ADMIN_PAGE_SLUG,
__NAMESPACE__ . '\render_playground_page',
NULL
);
add_menu_page(
'WordPress Playground Redirect',
'Sandbox Site (experimental)',
ADMIN_PAGE_CAPABILITY,
PLAYGROUND_ADMIN_PAGE_SLUG . '_redirect',
__NAMESPACE__ . '\redirect_playground_page',
NULL
);
}
/**
* Redirect to the WordPress Playground page.
*/
function redirect_playground_page()
{
$target = admin_url('tools.php?page=' . PLAYGROUND_ADMIN_PAGE_SLUG);
echo "<script>window.location = '$target';</script>";
exit;
}
/**
* Render the WordPress Playground page.
*/
function render_playground_page()
{
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if (isset($_GET['download'])) {
return;
}
include __DIR__ . '/templates/playground-page.php';
}
/**
* Add a "Preview Now" button to the plugin install screen.
*
* @param array $action_links An array of plugin action links.
* @param array $plugin The plugin data.
* @return array The modified array of plugin action links.
*/
function plugin_install_action_links($action_links, $plugin)
{
$preview_url = add_query_arg(
[
'pluginSlug' => esc_attr($plugin['slug']),
],
admin_url('tools.php?page=' . PLAYGROUND_ADMIN_PAGE_SLUG)
);
$preview_button = sprintf(
'<a class="preview-now button" data-slug="%s" href="%s" aria-label="%s" data-name="%s">%s</a>',
esc_attr($plugin['slug']),
$preview_url,
esc_attr(
sprintf(
/* translators: %s: Plugin name. */
__('Preview %s now', 'playground'),
$plugin['name']
)
),
esc_attr($plugin['name']),
__('Preview in Sandbox', 'playground')
);
array_unshift($action_links, $preview_button);
return $action_links;
}