Skip to content
This repository has been archived by the owner on Sep 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #255 from Automattic/fix/254/allow_jpcrm_versions
Browse files Browse the repository at this point in the history
Resolves #254 - allow custom JPCRM builds/versions
  • Loading branch information
kraftbj authored Apr 19, 2022
2 parents eb3b75a + 9e43b23 commit 9b34b0d
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 66 deletions.
4 changes: 3 additions & 1 deletion docs/specialops.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ Recommended `/specialops` page content:
<div class="checkbox"><label><input type="checkbox" data-feature="vaultpress">&nbsp;Include VaultPress</label></div>
</li>
<li>
<div class="checkbox"><label><input type="checkbox" data-feature="zero-bs-crm">&nbsp;Include Jetpack CRM</label></div>
<div class="checkbox"><label><input type="checkbox" data-feature="jpcrm">&nbsp;Include Jetpack CRM</label>
[jn_jpcrm_options]
</div>
</li>
<li>
<div class="checkbox"><label><input type="checkbox" data-feature="jetpack-debug-helper">&nbsp;Include Jetpack Debug Helper</label></div>
Expand Down
63 changes: 0 additions & 63 deletions features/jetpack-crm-master.php

This file was deleted.

162 changes: 162 additions & 0 deletions features/jetpack-crm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php
/**
* CRM for everyone.
*
* @package jurassic-ninja
*/

namespace jn;

add_action(
'jurassic_ninja_init',
function () {

$defaults = array(
'jpcrm' => false,
'jpcrm-build' => false,
'jpcrm-version' => false,
);

add_action(
'jurassic_ninja_add_features_before_auto_login',
function ( &$app, $features, $domain ) use ( $defaults ) {

$features = array_merge( $defaults, $features );

// Don't install if Jetpack CRM isn't selected.
if ( ! $features['jpcrm'] ) {
return;
}

if ( $features['jpcrm-version'] ) {

// Install specified version of Jetpack CRM from WP.org repo.
debug( '%s: Installing Jetpack CRM version %s from WP.org repo', $domain, $features['jpcrm-version'] );
add_jpcrm_from_wporg( $features['jpcrm-version'] );

} elseif ( $features['jpcrm-build'] ) {

// Install custom build of Jetpack CRM.
debug( '%s: Installing Jetpack CRM from %s', $domain, $features['jpcrm-build'] );
add_jpcrm_from_custom_build( $features['jpcrm-build'] );

} else {

// Install current version of Jetpack CRM from WP.org repo.
debug( '%s: Installing Jetpack CRM from WP.org', $domain );
add_directory_plugin( 'zero-bs-crm' );

}

},
10,
3
);

add_filter(
'jurassic_ninja_rest_create_request_features',
function ( $features, $json_params ) {

if ( isset( $json_params['jpcrm'] ) ) {
$features['jpcrm'] = $json_params['jpcrm'];
}

if ( isset( $json_params['jpcrm-version'] ) ) {
$features['jpcrm-version'] = $json_params['jpcrm-version'];
}

if ( isset( $json_params['jpcrm-build'] ) ) {
$features['jpcrm-build'] = $json_params['jpcrm-build'];
}

return $features;
},
10,
2
);
}
);

/**
* Installs and activates a specified version of Jetpack CRM from the WP.org plugin repo.
*
* @param string $version Version of Jetpack CRM.
*/
function add_jpcrm_from_wporg( $version ) {

// Verify we have a valid version number.
if ( ! version_compare( $version, '1.0.0', '>=' ) ) {
return new \WP_Error( 'bad_version_number', 'Bad version number.', array( 'status' => 404 ) );
}

$cmd = "wp plugin install zero-bs-crm --version=$version --activate";

add_filter(
'jurassic_ninja_feature_command',
function ( $s ) use ( $cmd ) {
return "$s && $cmd";
}
);

}

/**
* Installs and activates a specified build of Jetpack CRM from our custom build URL.
*
* @param string $build Hash of build to use.
*/
function add_jpcrm_from_custom_build( $build ) {

// phpcs:disable Squiz.PHP.CommentedOutCode.Found

/*
* Require commit SHA-1 hash (40 char long hex).
* if ( ! preg_match( '/^[A-Fa-f0-9]{40}$/', $build ) ) {
* return new \WP_Error( 'bad_commit_hash', 'Invalid commit hash.', array( 'status' => 404 ) );
* }
*/
// phpcs:enable

$clean_build = str_replace( '/', '_', $build );

// note that this public link is in a public repo
$jpcrm_build_base_url = 'https://jetpackcrm-builds.s3.amazonaws.com/builds/';
$jpcrm_build_url = $jpcrm_build_base_url . 'zero-bs-crm-' . $clean_build . '.zip';

$cmd = "wp plugin install $jpcrm_build_url --activate";

add_filter(
'jurassic_ninja_feature_command',
function ( $s ) use ( $cmd ) {
return "$s && $cmd";
}
);
}

/**
* Register a shortcode which renders Jetpack Licensing controls suitable for SpecialOps usage.
*/
\add_shortcode(
'jn_jpcrm_options',
function () {
ob_start();
?>
<style>
.jn-jpcrm-options ul {
list-style-type: none;
}
.jn-jpcrm-options input[type="text"] {
margin-left: 25px;
}
</style>
<div class="jn-jpcrm-options" style="display:none;">
<ul>
<li><label><input type="radio" name="jpcrm-options" checked /> WP.org</label></li>
<li><label><input type="radio" name="jpcrm-options" data-feature="jpcrm-version" /> Version: <input type="text" id="jpcrm-version" placeholder="4.10.1"></label></li>
<li><label><input type="radio" name="jpcrm-options" data-feature="jpcrm-build" /> Build: <input type="text" id="jpcrm-build" placeholder="fix/314/rationalise_pi"></label></li>
</ul>
</div>
<?php
return ob_get_clean();
}
);
1 change: 0 additions & 1 deletion features/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
function () {
$whitelist = array(
'amp' => 'AMP',
'zero-bs-crm' => 'Jetpack CRM',
'classic-editor' => 'Classic Editor',
'code-snippets' => 'Code Snippets',
'config-constants' => 'Config Constants',
Expand Down
16 changes: 16 additions & 0 deletions jurassicninja.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ function collectFeaturesFromFormInputs() {
},
[]
);

// get selected JPCRM option and value
selected_jpcrm_option = document.querySelector( "input[type='radio'][name='jpcrm-options']:checked" );
if ( selected_jpcrm_option.dataset.feature ) {
features[selected_jpcrm_option.dataset.feature] = selected_jpcrm_option.nextElementSibling.value;
}

return features;
}

Expand Down Expand Up @@ -353,16 +360,25 @@ function toggleJetpackProducts() {
$jetpack_products.toggle( $jetpack_toggle.is( ':checked' ) );
}

function toggleJPCRMOptions() {
const $jpcrm_toggle = jQuery( '[data-feature=jpcrm]' );
const $jpcrm_options = jQuery( '.jn-jpcrm-options' );
$jpcrm_options.toggle( $jpcrm_toggle.is( ':checked' ) );
}

function hookJetpackBranches() {
const $jetpack_toggle = jQuery( '[data-feature=jetpack]' );
const $jetpack_beta_toggle = jQuery( '[data-feature=jetpack-beta]' );
const $jpcrm_toggle = jQuery( '[data-feature=jpcrm]' );
const $branches_list = jQuery('#jetpack_beta_branches_group');
const $search_input = jQuery('#jetpack_branch');
const search_results = document.getElementById('jetpack_branches');

$jetpack_toggle.change( toggleJetpackProducts );
toggleJetpackProducts();

$jpcrm_toggle.change( toggleJPCRMOptions );

let onchange;
if ( $branches_list.length ) {
onchange = () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/stuff.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function require_feature_files() {
'/features/jetpack-beta.php',
'/features/wc-smooth-generator.php',
'/features/woocommerce-beta-tester.php',
'/features/jetpack-crm-master.php',
'/features/jetpack-crm.php',
'/features/jetpack-debug-helper.php',
'/features/client-example.php',
'/features/my-jetpack.php',
Expand Down

0 comments on commit 9b34b0d

Please sign in to comment.