This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
jurassic.ninja.php
180 lines (163 loc) · 4.59 KB
/
jurassic.ninja.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
<?php
/**
* Plugin Name: Jurassic Ninja
* Description: Launch ephemeral instances of WordPress + Jetpack using ServerPilot and an Ubuntu Box.
* Version: 5.26.1
* Author: Automattic
*
* @package jurassic-ninja
**/
namespace jn;
if ( ! defined( '\\ABSPATH' ) ) {
exit;
}
define( 'PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
require_once __DIR__ . '/lib/error-stuff.php';
init_or_fail_if_no_dependencies_installed();
/**
* Creates settings page, REST API extensions, cron jobs, and administrative tables.
* It also adds nonce and javascript to the /create page.
* Will not load any feature if the settings are not well configured.
*/
function init() {
require_once __DIR__ . '/lib/cron-stuff.php';
require_once __DIR__ . '/lib/db-stuff.php';
require_once __DIR__ . '/lib/settings-stuff.php';
require_once __DIR__ . '/lib/jetpack-stuff.php';
require_once __DIR__ . '/lib/stuff.php';
if ( is_cli_running() ) {
require_once __DIR__ . '/lib/class-jn-cli-command.php';
}
/**
* Done before adding settings page or anything else related to Jurassic Ninja Admin specifics.
*
* It's here so we can hook early to other filters that impace the admin pages like settings.
*
* @since 3.0
*/
do_action( 'jurassic_ninja_admin_init' );
// Create settings page.
add_settings_page();
// Settings problems include credentials and IDs not configured.
if ( ! settings_problems() ) {
// Include the JS only under the page which has the /create slug.
add_scripts();
// Serve the API root and nonce only under the page which has the /create slug.
add_rest_nonce();
// Add wp-json /create /checkin and /extend endpoints.
add_rest_api_endpoints();
// Disable temporarily. Run via crontab and Jurassic Ninja's CLI
// add_cron_job( __FILE__ );.
add_admin_bar_node();
}
/**
* Done after adding settings page and before anything else related to Jurassic Ninja specifics.
*
* It's here so we can hook to other filters after creating the REST API ednpoints, added cron tasks and admi-related stuff
*
* @since 3.0
*/
do_action( 'jurassic_ninja_init' );
// Yeah create two tables for tracking the launched sites.
create_tables( __FILE__ );
}
/**
* Is WP CLI running?
*
* @return bool
*/
function is_cli_running() {
return defined( 'WP_CLI' ) && WP_CLI;
}
/**
* Checks if the vendor directory is present and just shows a warning and quits if it's not the case.
* This can probably be removed if it makes sense to just include dependencies.
*/
function init_or_fail_if_no_dependencies_installed() {
require_once __DIR__ . '/lib/error-stuff.php';
add_error_notices();
if ( ! is_dir( __DIR__ . '/vendor' ) ) {
push_error( new \WP_Error( 'no-dependencies', __( 'Run composer install first', 'jurassic-ninja' ) ) );
} else {
init();
}
}
/**
* Adds a Topbar link to the Jurassic Ninja sites page.
*/
function add_admin_bar_node() {
add_action(
'wp_before_admin_bar_render',
function () {
global $wp_admin_bar;
$wp_admin_bar->add_node(
array(
'id' => 'wp-admin-bar-jurassic-ninja',
'title' => 'Jurassic Ninja Sites',
'href' => admin_url( 'admin.php?page=jurassic_ninja' ),
'parent' => 'site-name',
)
);
}
);
}
/**
* Adds nonce for cookie-based authentication against the REST API extensions
* that this plugin creates.
*/
function add_rest_nonce() {
add_action(
'wp_enqueue_scripts',
function () {
// Add the nonce under the /create path and
// if the user can manage options, add it also on /specialops.
if ( page_is_launching_page() ) {
wp_localize_script(
'jurassicninja.js',
'restApiSettings',
array(
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' ),
)
);
}
}
);
}
/**
* Adds javascript needed by this plugin
*/
function add_scripts() {
add_action(
'wp_enqueue_scripts',
function () {
if ( page_is_launching_page() ) {
wp_enqueue_script( 'jurassicninja.js', plugins_url( '', __FILE__ ) . '/jurassicninja.js', array( 'jquery' ), '1.1', true );
/**
* Done after enqueueing the jurassic.ninja.js file
*
* This action happens during a wp_enqueue_scripts hook.
*
* @since 3.0
*/
do_action( 'jurassic_ninja_enqueue_scripts' );
}
}
);
}
/**
* Returns true if currently on a /create or /specialops page
*
* @return boolean [description]
*/
function page_is_launching_page() {
return ( 'create' === get_page_uri() || page_is_specialops() );
}
/**
* Returns true if currently on a /specialops page
*
* @return boolean [description]
*/
function page_is_specialops() {
return is_user_logged_in() && 'specialops' === get_page_uri();
}