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 #269 from Automattic/fix/268/add_jpcrm_data_genera…
Browse files Browse the repository at this point in the history
…tion_support

Resolves #268 - add support for prepopulated data in Jetpack CRM
  • Loading branch information
kraftbj authored Sep 15, 2022
2 parents 30da3d7 + 48e4589 commit e8aa137
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
96 changes: 91 additions & 5 deletions features/jetpack-crm.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ function () {
'jpcrm' => false,
'jpcrm-build' => false,
'jpcrm-version' => false,
'jpcrm-populate-crm-data' => false,
'jpcrm-populate-woo-data' => false,
);

add_action(
Expand Down Expand Up @@ -48,6 +50,16 @@ function ( &$app, $features, $domain ) use ( $defaults ) {

}

if ( $features['jpcrm-populate-crm-data'] || $features['jpcrm-populate-woo-data'] ) {
add_jpcrm_sdg();
if ( $features['jpcrm-populate-crm-data'] ) {
populate_crm_data();
}
if ( $features['jpcrm-populate-woo-data'] ) {
populate_woo_data();
}
}

},
10,
3
Expand All @@ -69,6 +81,14 @@ function ( $features, $json_params ) {
$features['jpcrm-build'] = $json_params['jpcrm-build'];
}

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

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

return $features;
},
10,
Expand Down Expand Up @@ -101,11 +121,11 @@ function ( $s ) use ( $cmd ) {
}

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

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

Expand All @@ -117,11 +137,11 @@ function add_jpcrm_from_custom_build( $build ) {
*/
// phpcs:enable

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

// 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';
$jpcrm_build_url = $jpcrm_build_base_url . 'zero-bs-crm-' . $clean_branch . '.zip';

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

Expand All @@ -133,6 +153,50 @@ function ( $s ) use ( $cmd ) {
);
}

/**
* Installs and activates Jetpack CRM Sample Data Generator.
*/
function add_jpcrm_sdg() {
$jpcrm_sdg_url = 'https://jetpackcrm-builds.s3.amazonaws.com/jpcrm-sdg/jpcrm-sdg.zip';

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

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

/**
* Populates Jetpack CRM with data from JPCRM SDG.
*/
function populate_crm_data() {
$cmd = 'wp jpcrmsdg --objtype=all';

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

/**
* Populates Woo with data from JPCRM SDG.
*/
function populate_woo_data() {
$cmd = 'wp jpcrmsdg --objtype=woo';

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.
*/
Expand All @@ -154,8 +218,30 @@ function () {
<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>
<li><label><input type="checkbox" name="jpcrm-options" data-feature="jpcrm-populate-crm-data" /> Populate CRM data</label></li>
<li style="display:none"><label><input type="checkbox" name="jpcrm-options" data-feature="jpcrm-populate-woo-data" /> Populate Woo data</label></li>
</ul>
</div>
<script>
// hide/show "populate Woo data" option depending on Woo plugin selection
function jpcrm_toggle_woo_populate_button(e) {
if (e.target.dataset['feature'] && e.target.dataset['feature'] === 'woocommerce') {
if (!e.target.checked) {
document.querySelector('[data-feature="jpcrm-populate-woo-data"]').checked = false;
}
document.querySelector('[data-feature="jpcrm-populate-woo-data"]').parentElement.parentElement.style.display = e.target.checked ? '' : 'none';
}
}

// select radio button associated with input
function jpcrm_select_associated_radio_button(e) {
e.target.parentElement.children[0].checked = true;
}

document.querySelector('.entry-content').addEventListener('click', jpcrm_toggle_woo_populate_button);
document.querySelectorAll('#jpcrm-version, #jpcrm-build').forEach(i => i.addEventListener('click', jpcrm_select_associated_radio_button));

</script>
<?php
return ob_get_clean();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/stuff.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function require_feature_files() {
* @param string $php_version The PHP version to run the app on.
* @param array $requested_features {
* Array of features to enable.
*
*
* @type boolean $config-constants Should we add the Config Constants plugin to the site?
* @type boolean $auto_ssl Should we add Let's Encrypt-based SSL for the site?
* @type boolean $ssl Should we add the configured SSL certificate for the site?
Expand Down

0 comments on commit e8aa137

Please sign in to comment.