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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactor to provide actions and filters instead of having every…
…thing done inside the launch_wordpress() function (#99) * Introduce actions and filters * Factor out handling of features like plugins multisite and ssl into own files * Make settings hookable * Add some docs * Add create_endpoint_feature_defaults filter * add jurassic_ninja_rest_create_request_features filter * Add jurassic_ninja_created_site_url filter * move plugin features to individual files * introduce collectFeaturesFromQueryString * remove js check that is now made server side for jetpack branches * Remove unnecessary filter jurassic_ninja_features Defaults are implemented by each portion of code implementing hooks * Fix default for empty search query * Update license to GPL-2 * Bump version to 3.0 * Dockument all actions * Document all filters * fix path in features readme * Move features directory to root and make their loading be filterable * README improvements * Improve development.md * Remove word filtering as we now control the set of words
- Loading branch information
Showing
23 changed files
with
1,326 additions
and
507 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
|
||
The regular life cycle of a Jurassic Ninja site | ||
|
||
``` | ||
* User gets to the Create page | ||
* The JS on the create page fires a request to REST API endpoint: `/wp-json/jurassicninja/create`. | ||
* The PHP function `launch_wordpress()` gets called and the new site is launched with all the features. | ||
* The REST request finishes with an URL for the created site. | ||
* That URL is shown to the user. | ||
* The user visits the site, gets auto-logged in, acquiring a WordPress cookie. | ||
* The site is purged 7 days after that first auto login or 7 days after the user entered credentials the last time. This can happen if the user signed out and signed in again. | ||
``` | ||
|
||
The `launch_wordpress()` function can be explained as: | ||
|
||
``` | ||
2. Collects requested features and merges with defaults. | ||
3. Decide if we need to do something regarding the requested Features (`jurassic_ninja_do_feature_conditions` action). | ||
4. Generates a random subdomain. | ||
5. Creates a user. Hookable via the `jurassic_ninja_create_sysuser` action. | ||
6. Launches a new environment for a PHP/MysQL app and installs WordPress. Hookable via the `jurassic_ninja_create_app` action. | ||
7. Add features that need to be added before enabling the autologin mechanism (`jurassic_ninja_add_features_before_auto_login` action). | ||
8. Enable autologin. | ||
9. Add features that need to be added after enabling the autologin mechanism (`jurassic_ninja_add_features_before_auto_login` action). | ||
``` | ||
|
||
|
||
### Adding Features | ||
|
||
Basically you can go an peek the `features` directory which contains a few files that make use of the available actions and filters. | ||
|
||
But the main hooks to look are: | ||
|
||
* jurassic_ninja_init (Action). | ||
* jurassic_ninja_admin_init (Action). | ||
* jurassic_ninja_settings_options_page (Filter). | ||
* jurassic_ninja_settings_options_page_default_plugins (Filter). | ||
* jurassic_ninja_rest_feature_defaults (Filter). | ||
* jurassic_ninja_rest_create_request_features (Filter ). | ||
* jurassic_ninja_do_feature_conditions (Action). | ||
* jurassic_ninja_add_features_before_auto_login (Action). | ||
* jurassic_ninja_add_features_after_auto_login (Action). | ||
* jurassic_ninja_feature_command (Filter). | ||
* jurassic_ninja_created_site_url (Filter). | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#### features | ||
|
||
Features are whatever we put on to of a regular site. | ||
|
||
A regular site would be defined as: | ||
|
||
`Latest WordPress stable, single site installation running over http`. | ||
|
||
The main file (stuff.php) defines a few hooks that allow us to build features on top of the site launching flow. | ||
|
||
Some features are: | ||
|
||
- Enabling of SSL. (`ssl.php`). | ||
- Addition of plugins. (`plugins.php`). | ||
- Setting of wp-config constants like WP_DEBU and WP_DEBUG_LOG. (`wp-debug-log.php`). | ||
- Launching multisite installations instead of single site( '') | ||
|
||
Refer to the [API](docs/development.md#API) section of the development doc to find out about existing hooks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace jn; | ||
|
||
add_action( 'jurassic_ninja_init', function() { | ||
$defaults = [ | ||
'config-constants' => false, | ||
]; | ||
|
||
add_action( 'jurassic_ninja_add_features_before_auto_login', function( &$app, $features, $domain ) use ( $defaults ) { | ||
$features = array_merge( $defaults, $features ); | ||
if ( $features['config-constants'] ) { | ||
debug( '%s: Adding Config Constants Plugin', $domain ); | ||
add_config_constants_plugin(); | ||
} | ||
}, 10, 3 ); | ||
|
||
} ); | ||
|
||
/** | ||
* Installs and activates the Config Constants plugin on the site. | ||
*/ | ||
function add_config_constants_plugin() { | ||
$cmd = 'wp plugin install config-constants --activate'; | ||
add_filter( 'jurassic_ninja_feature_command', function ( $s ) use ( $cmd ) { | ||
return "$s && $cmd"; | ||
} ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace jn; | ||
|
||
add_action( 'jurassic_ninja_init', function() { | ||
$defaults = [ | ||
'gutenberg' => false, | ||
]; | ||
|
||
add_action( 'jurassic_ninja_add_features_before_auto_login', function( &$app, $features, $domain ) use ( $defaults ) { | ||
$features = array_merge( $defaults, $features ); | ||
if ( $features['gutenberg'] ) { | ||
debug( '%s: Adding Gutenberg', $domain ); | ||
add_gutenberg_plugin(); | ||
} | ||
}, 10, 3 ); | ||
|
||
} ); | ||
|
||
/** | ||
* Installs and activates Gutenberg Plugin on the site. | ||
*/ | ||
function add_gutenberg_plugin() { | ||
$cmd = 'wp plugin install gutenberg --activate'; | ||
add_filter( 'jurassic_ninja_feature_command', function ( $s ) use ( $cmd ) { | ||
return "$s && $cmd"; | ||
} ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace jn; | ||
|
||
add_action( 'jurassic_ninja_init', function() { | ||
$defaults = [ | ||
'jetpack-beta' => false, | ||
'branch' => false, | ||
]; | ||
|
||
add_action( 'jurassic_ninja_add_features_before_auto_login', function( &$app, $features, $domain ) use ( $defaults ) { | ||
$features = array_merge( $defaults, $features ); | ||
if ( $features['jetpack-beta'] ) { | ||
debug( '%s: Adding Jetpack Beta Tester Plugin', $domain ); | ||
add_jetpack_beta_plugin(); | ||
} | ||
|
||
if ( $features['branch'] ) { | ||
debug( '%s: Activating Jetpack %s branch in Beta plugin', $domain, $features['branch'] ); | ||
activate_jetpack_branch( $features['branch'] ); | ||
} | ||
}, 10, 3 ); | ||
|
||
add_filter( 'jurassic_ninja_rest_feature_defaults', function( $defaults ) { | ||
return array_merge( $defaults, [ | ||
'jetpack-beta' => (bool) settings( 'add_jetpack_beta_by_default', false ), | ||
] ); | ||
} ); | ||
|
||
add_filter( 'jurassic_ninja_rest_create_request_features', function( $features, $json_params ) { | ||
$branch = isset( $json_params['branch'] ) ? $json_params['branch'] : 'master'; | ||
if ( isset( $json_params['jetpack-beta'] ) && $json_params['jetpack-beta'] ) { | ||
$url = get_jetpack_beta_url( $branch ); | ||
|
||
if ( null === $url ) { | ||
return new \WP_Error( | ||
'failed_to_launch_site_with_branch', | ||
esc_html__( 'Invalid branch name or not ready yet: ' . $branch ), | ||
[ | ||
'status' => 400, | ||
] | ||
); | ||
} | ||
$features['jetpack-beta'] = $json_params['jetpack-beta']; | ||
$features['branch'] = $branch; | ||
} | ||
|
||
return $features; | ||
}, 10, 2 ); | ||
} ); | ||
|
||
add_action( 'jurassic_ninja_admin_init', function() { | ||
add_filter( 'jurassic_ninja_settings_options_page_default_plugins', function( $fields ) { | ||
$field = [ | ||
'add_jetpack_beta_by_default' => [ | ||
'id' => 'add_jetpack_beta_by_default', | ||
'title' => __( 'Add Jetpack Beta Tester plugin to every launched WordPress', 'jurassic-ninja' ), | ||
'text' => __( 'Install and activate Jetpack Beta Tester on launch', 'jurassic-ninja' ), | ||
'type' => 'checkbox', | ||
'checked' => false, | ||
], | ||
]; | ||
return array_merge( $fields, $field ); | ||
}, 10 ); | ||
} ); | ||
|
||
/** | ||
* Installs and activates Jetpack Beta Tester plugin on the site. | ||
*/ | ||
function add_jetpack_beta_plugin() { | ||
$jetpack_beta_plugin_url = JETPACK_BETA_PLUGIN_URL; | ||
$cmd = "wp plugin install $jetpack_beta_plugin_url --activate" ; | ||
add_filter( 'jurassic_ninja_feature_command', function ( $s ) use ( $cmd ) { | ||
return "$s && $cmd"; | ||
} ); | ||
} | ||
|
||
/** | ||
* Activates jetpack branch in Beta plugin | ||
*/ | ||
function activate_jetpack_branch( $branch_name ) { | ||
$cmd = "wp jetpack-beta branch activate $branch_name"; | ||
add_filter( 'jurassic_ninja_feature_command', function ( $s ) use ( $cmd ) { | ||
return "$s && $cmd"; | ||
} ); | ||
} | ||
|
||
function get_jetpack_beta_url( $branch_name ) { | ||
$branch_name = str_replace( '/', '_', $branch_name ); | ||
$manifest_url = 'https://betadownload.jetpack.me/jetpack-branches.json'; | ||
$manifest = json_decode( wp_remote_retrieve_body( wp_remote_get( $manifest_url ) ) ); | ||
|
||
if ( ( 'rc' === $branch_name || 'master' === $branch_name ) && isset( $manifest->{$branch_name}->download_url ) ) { | ||
return $manifest->{$branch_name}->download_url; | ||
} | ||
|
||
if ( isset( $manifest->pr->{$branch_name}->download_url ) ) { | ||
return $manifest->pr->{$branch_name}->download_url; | ||
} | ||
} |
Oops, something went wrong.