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 #206 from Automattic/add/jetpack-licensing-product…
Browse files Browse the repository at this point in the history
…s-endpoint
  • Loading branch information
kraftbj authored Dec 3, 2020
2 parents a17667c + fafcf6c commit 6c6e861
Showing 1 changed file with 55 additions and 16 deletions.
71 changes: 55 additions & 16 deletions features/jetpack-licensing.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,49 @@ function sanitize_products( $products ) {
return $products;
}

/**
* Get all Jetpack product families that we can issue licenses for from the API.
*
* @return array|WP_Error The available products.
*/
function fetch_product_families() {
$response = wp_remote_get( 'https://public-api.wordpress.com/wpcom/v2/jetpack-licensing/product-families' );
$status = wp_remote_retrieve_response_code( $response );
$body = wp_remote_retrieve_body( $response );

if ( 200 !== $status ) {
return new WP_Error(
'get_products_request_failed',
// Translators: %s = error message returned by an API
sprintf( __( 'Get products request failed with: %s', 'jurassic-ninja' ), $body ),
[
'status' => $status,
]
);
}

return json_decode( $body );
}

/**
* Get all Jetpack product families that we can issue licenses for.
*
* @return array The available products.
*/
function get_product_families() {
$option_key = 'jurassic_ninja_jetpack_licensing_products';
$families = fetch_product_families();

if ( is_wp_error( $families ) || empty( $families ) ) {
// Fallback to the last successful fetch result.
return get_option( $option_key );
}

update_option( $option_key, $families );

return $families;
}

/**
* Issue a license.
*
Expand Down Expand Up @@ -284,16 +327,7 @@ function revoke_licenses( $license_keys ) {
* Register a shortcode which renders Jetpack Licensing controls suitable for SpecialOps usage.
*/
add_shortcode( 'jn_jetpack_products_list', function () {
$products = [
'personal' => 'Personal',
'premium' => 'Premium',
'professional' => 'Professional',
'jetpack-backup-daily' => 'Jetpack Backup Daily',
'jetpack-backup-realtime' => 'Jetpack Backup Realtime',
'jetpack-scan' => 'Jetpack Scan',
'jetpack-anti-spam' => 'Jetpack Anti Spam',
];

$families = get_product_families();
ob_start();
?>
<style>
Expand All @@ -313,16 +347,21 @@ function revoke_licenses( $license_keys ) {
padding: 0;
}
</style>
<div class="jn-jetpack-products-list" style="margin: 0 0 32px 32px;">
<div class="jn-jetpack-products-list">
<label><?php esc_html_e( 'Optionally, select Jetpack products to issue licenses for:', 'jurassic-ninja' ); ?></label>

<ul>
<?php foreach ( $products as $slug => $label ) : ?>
<li>
<label><input type="checkbox" data-feature="jetpack-products" value="<?php echo esc_attr( $slug ); ?>"/> <?php echo esc_html( $label ); ?>
</label>
</li>
<?php foreach ( $families as $family ) : ?>
<?php foreach ( $family->products as $product ) : ?>
<li>
<label>
<input type="checkbox" data-feature="jetpack-products" value="<?php echo esc_attr( $product->slug ); ?>"/> <?php echo esc_html( $product->name ); ?>
</label>
</li>
<?php endforeach; ?>
<?php endforeach; ?>
</ul>

<label><?php esc_html_e( 'Other:', 'jurassic-ninja' ); ?></label>
<input type="text" data-feature="jetpack-products" value="" placeholder="<?php esc_attr_e( 'Comma-separated list of Jetpack products', 'jurassic-ninja' ); ?>" />
</div>
Expand Down

0 comments on commit 6c6e861

Please sign in to comment.