Skip to content

Commit

Permalink
Add jetpack endpoint for jetpack product data (#33095)
Browse files Browse the repository at this point in the history
* changelog

* Add barebones infrastructure for querying product data in the plugin

* changelog

* Fix product data api call

* Fix incorrect function call

* Update api_version

* Update version number

* Force wpcom endpoint

* Update route setup

* Remove useless alias

* Remove logs

* Remove backup specific endpoint

* Fixup project versions

* Fix composer lock files

* changelog

* Remove useless comment
  • Loading branch information
CodeyGuyDylan authored Sep 20, 2023
1 parent 5797577 commit b0c967a
Show file tree
Hide file tree
Showing 33 changed files with 215 additions and 28 deletions.
25 changes: 20 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions projects/packages/my-jetpack/_inc/hooks/use-product-data/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useSelect } from '@wordpress/data';
import { STORE_ID } from '../../state/store';

/**
* React custom hook to get the product data
*
* @returns {object} product data
*/
export default function useProductData() {
return {
productData: useSelect( select => select( STORE_ID ).getProductData() ),
fetchingProductData: useSelect( select => select( STORE_ID ).isFetchingProductData() ),
};
}
12 changes: 12 additions & 0 deletions projects/packages/my-jetpack/_inc/state/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const SET_CHAT_AVAILABILITY_IS_FETCHING = 'SET_CHAT_AVAILABILITY_IS_FETCHING';
const SET_CHAT_AVAILABILITY = 'SET_CHAT_AVAILABILITY';
const SET_CHAT_AUTHENTICATION_IS_FETCHING = 'SET_CHAT_AUTHENTICATION_IS_FETCHING';
const SET_CHAT_AUTHENTICATION = 'SET_CHAT_AUTHENTICATION';
const SET_PRODUCT_DATA_IS_FETCHING = 'SET_PRODUCT_DATA_IS_FETCHING';
const SET_PRODUCT_DATA = 'SET_PRODUCT_DATA';

const SET_GLOBAL_NOTICE = 'SET_GLOBAL_NOTICE';
const CLEAN_GLOBAL_NOTICE = 'CLEAN_GLOBAL_NOTICE';
Expand All @@ -40,6 +42,10 @@ const setChatAuthenticationIsFetching = isFetching => {
return { type: SET_CHAT_AUTHENTICATION_IS_FETCHING, isFetching };
};

const setProductDataIsFetching = isFetching => {
return { type: SET_PRODUCT_DATA_IS_FETCHING, isFetching };
};

const fetchPurchases = () => {
return { type: FETCH_PURCHASES };
};
Expand Down Expand Up @@ -70,6 +76,8 @@ const setAvailableLicenses = availableLicenses => {

const setProduct = product => ( { type: SET_PRODUCT, product } );

const setProductData = productData => ( { type: SET_PRODUCT_DATA, productData } );

const setRequestProductError = ( productId, error ) => ( {
type: SET_PRODUCT_REQUEST_ERROR,
productId,
Expand Down Expand Up @@ -271,6 +279,8 @@ const actions = {
setPurchases,
setChatAvailability,
setChatAuthentication,
setProductDataIsFetching,
setProductData,
setAvailableLicensesIsFetching,
fetchAvailableLicenses,
setAvailableLicenses,
Expand Down Expand Up @@ -300,5 +310,7 @@ export {
SET_CHAT_AVAILABILITY_IS_FETCHING,
SET_CHAT_AUTHENTICATION,
SET_CHAT_AUTHENTICATION_IS_FETCHING,
SET_PRODUCT_DATA_IS_FETCHING,
SET_PRODUCT_DATA,
actions as default,
};
1 change: 1 addition & 0 deletions projects/packages/my-jetpack/_inc/state/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const REST_API_NAMESPACE = 'my-jetpack/v1';
export const REST_API_SITE_PURCHASES_ENDPOINT = `${ REST_API_NAMESPACE }/site/purchases`;
export const REST_API_SITE_PRODUCTS_ENDPOINT = `${ REST_API_NAMESPACE }/site/products`;
export const REST_API_SITE_PRODUCT_DATA_ENDPOINT = `${ REST_API_NAMESPACE }/site/product-data`;
export const REST_API_CHAT_AVAILABILITY_ENDPOINT = `${ REST_API_NAMESPACE }/chat/availability`;
export const REST_API_CHAT_AUTHENTICATION_ENDPOINT = `${ REST_API_NAMESPACE }/chat/authentication`;
export const PRODUCTS_THAT_NEEDS_INITIAL_FETCH = [ 'scan' ];
Expand Down
22 changes: 22 additions & 0 deletions projects/packages/my-jetpack/_inc/state/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
CLEAN_GLOBAL_NOTICE,
SET_PRODUCT_STATS,
SET_IS_FETCHING_PRODUCT_STATS,
SET_PRODUCT_DATA_IS_FETCHING,
SET_PRODUCT_DATA,
} from './actions';

const products = ( state = {}, action ) => {
Expand Down Expand Up @@ -77,6 +79,25 @@ const products = ( state = {}, action ) => {
}
};

const productData = ( state = {}, action ) => {
switch ( action.type ) {
case SET_PRODUCT_DATA_IS_FETCHING:
return {
...state,
isFetching: action.isFetching,
};

case SET_PRODUCT_DATA:
return {
...state,
items: action?.productData || {},
};

default:
return state;
}
};

const purchases = ( state = {}, action ) => {
switch ( action.type ) {
case SET_PURCHASES_IS_FETCHING:
Expand Down Expand Up @@ -213,6 +234,7 @@ const stats = ( state = {}, action ) => {

const reducers = combineReducers( {
products,
productData,
purchases,
chatAvailability,
chatAuthentication,
Expand Down
14 changes: 14 additions & 0 deletions projects/packages/my-jetpack/_inc/state/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
REST_API_SITE_PRODUCTS_ENDPOINT,
REST_API_CHAT_AVAILABILITY_ENDPOINT,
REST_API_CHAT_AUTHENTICATION_ENDPOINT,
REST_API_SITE_PRODUCT_DATA_ENDPOINT,
PRODUCTS_THAT_NEEDS_INITIAL_FETCH,
} from './constants';
import resolveProductStatsRequest from './stats-resolvers';
Expand Down Expand Up @@ -129,6 +130,19 @@ const myJetpackResolvers = {
dispatch.setAvailableLicensesIsFetching( false );
}
},

getProductData: () => {
return async ( { dispatch } ) => {
dispatch.setProductDataIsFetching( true );

try {
dispatch.setProductData( await apiFetch( { path: REST_API_SITE_PRODUCT_DATA_ENDPOINT } ) );
dispatch.setProductDataIsFetching( false );
} catch ( error ) {
dispatch.setProductDataIsFetching( false );
}
};
},
};

const getProductStats = {
Expand Down
6 changes: 6 additions & 0 deletions projects/packages/my-jetpack/_inc/state/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ const productSelectors = {
getProductsThatRequiresUserConnection,
};

const productDataSelectors = {
getProductData: state => state.productData?.items || {},
isFetchingProductData: state => state.productData?.isFetching || false,
};

const purchasesSelectors = {
getPurchases: state => state.purchases?.items || [],
isRequestingPurchases: state => state.purchases?.isFetching || false,
Expand Down Expand Up @@ -134,6 +139,7 @@ const selectors = {
...purchasesSelectors,
...chatAvailabilitySelectors,
...chatAuthenticationSelectors,
...productDataSelectors,
...availableLicensesSelectors,
...noticeSelectors,
...pluginSelectors,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add barebones infrastructure for querying jetpack product data
2 changes: 1 addition & 1 deletion projects/packages/my-jetpack/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"link-template": "https://github.com/Automattic/jetpack-my-jetpack/compare/${old}...${new}"
},
"branch-alias": {
"dev-trunk": "3.5.x-dev"
"dev-trunk": "3.6.x-dev"
},
"version-constants": {
"::PACKAGE_VERSION": "src/class-initializer.php"
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/my-jetpack/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-my-jetpack",
"version": "3.5.0",
"version": "3.6.0-alpha",
"description": "WP Admin page with information and configuration shared among all Jetpack stand-alone plugins",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/my-jetpack/#readme",
"bugs": {
Expand Down
3 changes: 2 additions & 1 deletion projects/packages/my-jetpack/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Initializer {
*
* @var string
*/
const PACKAGE_VERSION = '3.5.0';
const PACKAGE_VERSION = '3.6.0-alpha';

/**
* HTML container ID for the IDC screen on My Jetpack page.
Expand Down Expand Up @@ -231,6 +231,7 @@ public static function register_rest_endpoints() {
new REST_Products();
new REST_Purchases();
new REST_Zendesk_Chat();
new REST_Product_Data();
new REST_AI();

register_rest_route(
Expand Down
58 changes: 58 additions & 0 deletions projects/packages/my-jetpack/src/class-rest-product-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Sets up the Product Data REST API endpoints.
*
* @package automattic/my-jetpack
*/

namespace Automattic\Jetpack\My_Jetpack;

use Automattic\Jetpack\Connection\Client;
use WP_Error;

/**
* Registers the REST routes for Product Data
*/
class REST_Product_Data {
/**
* Constructor.
*/
public function __construct() {
register_rest_route(
'my-jetpack/v1',
'site/product-data',
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => __CLASS__ . '::get_all_product_data',
'permission_callback' => __CLASS__ . '::permissions_callback',
)
);
}

/**
* Checks if the user has the correct permissions
*/
public static function permissions_callback() {
return current_user_can( 'manage_options' );
}

/**
* Gets the product data for all products
*
* @return array|WP_Error
*/
public static function get_all_product_data() {
$site_id = \Jetpack_Options::get_option( 'id' );
$wpcom_endpoint = sprintf( 'sites/%d/jetpack-product-data?locale=%2$s&force=wpcom', $site_id, get_user_locale() );
$api_version = '2';
$response = Client::wpcom_json_api_request_as_blog( $wpcom_endpoint, $api_version, array(), null, 'wpcom' );
$response_code = wp_remote_retrieve_response_code( $response );
$body = json_decode( wp_remote_retrieve_body( $response ) );

if ( is_wp_error( $response ) || empty( $response['body'] ) || 200 !== $response_code ) {
return new WP_Error( 'site_products_data_fetch_failed', 'Site products data fetch failed', array( 'status' => $response_code ? $response_code : 400 ) );
}

return rest_ensure_response( $body, 200 );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Add bi-yearly constants for complete and videopress in config
2 changes: 1 addition & 1 deletion projects/packages/videopress/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-videopress",
"version": "0.17.0",
"version": "0.17.1-alpha",
"description": "VideoPress package",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/videopress/#readme",
"bugs": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/videopress/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* The Package_Version class.
*/
class Package_Version {
const PACKAGE_VERSION = '0.17.0';
const PACKAGE_VERSION = '0.17.1-alpha';

const PACKAGE_SLUG = 'videopress';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added


4 changes: 2 additions & 2 deletions projects/plugins/backup/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added


4 changes: 2 additions & 2 deletions projects/plugins/boost/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other


Loading

0 comments on commit b0c967a

Please sign in to comment.