Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/trunk' into update/node-20
Browse files Browse the repository at this point in the history
  • Loading branch information
anomiex committed Nov 7, 2023
2 parents c61f02a + ee82ada commit d7226a9
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 46 deletions.
13 changes: 12 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,16 @@
"**/.cache/**",
"**/jetpack_vendor/**/jetpack_vendor/**",
"**/jetpack_vendor/**"
]
],
"phpCodeSniffer.autoExecutable": true,
"phpCodeSniffer.standard": "Automatic",
"phpCodeSniffer.exclude": [
"**/.git/**",
"**/.svn/**",
"**/.hg/**",
"**/.cache/**",
"**/jetpack_vendor/**",
"**/vendor/**",
],
"prettier.prettierPath": "tools/js-tools/node_modules/prettier/index.cjs"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Hack tests to pass by avoiding calling `Modules::disable()` from packages/status, which has an undeclared dependency on `Jetpack_Options` which is currently in packages/connection.


5 changes: 5 additions & 0 deletions projects/packages/lazy-images/src/lazy-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public static function instance() {
* @return bool
*/
public static function should_force_deactivate() {
// Don't try disabling when running phpunit tests. A bug in packages/status makes that fail.
if ( Constants::is_true( 'IS_JETPACK_LAZY_IMAGES_TESTS' ) ) {
return false;
}

// If Gutenberg is not installed,
// check if we run a version of WP that would conflict with Lazy Images.
if ( ! Constants::is_true( 'IS_GUTENBERG_PLUGIN' ) ) {
Expand Down
2 changes: 2 additions & 0 deletions projects/packages/lazy-images/tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package automattic/jetpack-lazy-images
*/

define( 'IS_JETPACK_LAZY_IMAGES_TESTS', true ); // flag for hack

/**
* Load the composer autoloader.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,8 @@ public function test_should_force_deactivate( $version_details, $expected ) {
global $wp_version;
$previous_version = $wp_version;

Constants::set_constant( 'IS_JETPACK_LAZY_IMAGES_TESTS', false ); // disable hack for this test

Constants::set_constant( 'IS_GUTENBERG_PLUGIN', $version_details['gutenberg'] );
Constants::set_constant( 'GUTENBERG_VERSION', $version_details['gutenberg_version'] );
if ( true === $version_details['is_gutenberg_dev'] ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

AI Assistant: register isFetching state in the plans store
60 changes: 60 additions & 0 deletions projects/plugins/jetpack/extensions/store/wordpress-com/actions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
/**
* External dependencies
*/
import apiFetch from '@wordpress/api-fetch';
/**
* Types
*/
import type { Plan } from './types';
import type { AiFeatureProps } from './types';
import type { SiteAIAssistantFeatureEndpointResponseProps } from '../../types';

/**
* Map the response from the `sites/$site/ai-assistant-feature`
* endpoint to the AI Assistant feature props.
* @param { SiteAIAssistantFeatureEndpointResponseProps } response - The response from the endpoint.
* @returns { AiFeatureProps } The AI Assistant feature props.
*/
export function mapAIFeatureResponseToAiFeatureProps(
response: SiteAIAssistantFeatureEndpointResponseProps
): AiFeatureProps {
return {
hasFeature: !! response[ 'has-feature' ],
isOverLimit: !! response[ 'is-over-limit' ],
requestsCount: response[ 'requests-count' ],
requestsLimit: response[ 'requests-limit' ],
requireUpgrade: !! response[ 'site-require-upgrade' ],
errorMessage: response[ 'error-message' ],
errorCode: response[ 'error-code' ],
upgradeType: response[ 'upgrade-type' ],
usagePeriod: {
currentStart: response[ 'usage-period' ]?.[ 'current-start' ],
nextStart: response[ 'usage-period' ]?.[ 'next-start' ],
requestsCount: response[ 'usage-period' ]?.[ 'requests-count' ] || 0,
},
currentTier: {
value: response[ 'current-tier' ]?.value || 1,
},
};
}

const actions = {
setPlans( plans: Array< Plan > ) {
Expand All @@ -25,6 +59,32 @@ const actions = {
feature,
};
},

/**
* Thunk action to fetch the AI Assistant feature from the API.
*
* @returns {Function} The thunk action.
*/
fetchAiAssistantFeature() {
return async ( { dispatch } ) => {
// Dispatch isFetching action.
dispatch( { type: 'REQUEST_AI_ASSISTANT_FEATURE' } );

try {
const response: SiteAIAssistantFeatureEndpointResponseProps = await apiFetch( {
path: '/wpcom/v2/jetpack-ai/ai-assistant-feature',
} );

// Store the feature in the store.
dispatch(
actions.storeAiAssistantFeature( mapAIFeatureResponseToAiFeatureProps( response ) )
);
} catch ( err ) {
// @todo: Handle error.
console.error( err ); // eslint-disable-line no-console
}
};
},
};

export default actions;
97 changes: 52 additions & 45 deletions projects/plugins/jetpack/extensions/store/wordpress-com/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import apiFetch from '@wordpress/api-fetch';
import { createReduxStore, register } from '@wordpress/data';
/**
* Internal dependencies
Expand All @@ -10,44 +9,36 @@ import actions from './actions';
/**
* Types
*/
import type { AiFeatureProps, PlanStateProps } from './types';
import type { SiteAIAssistantFeatureEndpointResponseProps } from '../../types';
import type { PlanStateProps } from './types';

const store = 'wordpress-com/plans';

const INITIAL_STATE: PlanStateProps = {
plans: [],
features: {},
};

/**
* Map the response from the `sites/$site/ai-assistant-feature`
* endpoint to the AI Assistant feature props.
* @param { SiteAIAssistantFeatureEndpointResponseProps } response - The response from the endpoint.
* @returns { AiFeatureProps } The AI Assistant feature props.
*/
function mapAIFeatureResponseToAiFeatureProps(
response: SiteAIAssistantFeatureEndpointResponseProps
): AiFeatureProps {
return {
hasFeature: !! response[ 'has-feature' ],
isOverLimit: !! response[ 'is-over-limit' ],
requestsCount: response[ 'requests-count' ],
requestsLimit: response[ 'requests-limit' ],
requireUpgrade: !! response[ 'site-require-upgrade' ],
errorMessage: response[ 'error-message' ],
errorCode: response[ 'error-code' ],
upgradeType: response[ 'upgrade-type' ],
usagePeriod: {
currentStart: response[ 'usage-period' ]?.[ 'current-start' ],
nextStart: response[ 'usage-period' ]?.[ 'next-start' ],
requestsCount: response[ 'usage-period' ]?.[ 'requests-count' ] || 0,
},
currentTier: {
value: response[ 'current-tier' ]?.value || 1,
features: {
aiAssistant: {
hasFeature: false,
isOverLimit: false,
requestsCount: 0,
requestsLimit: 0,
requireUpgrade: false,
errorMessage: '',
errorCode: '',
upgradeType: 'default',
currentTier: {
value: 1,
},
usagePeriod: {
currentStart: '',
nextStart: '',
requestsCount: 0,
},
_meta: {
isRequesting: false,
},
},
};
}
},
};

const wordpressPlansStore = createReduxStore( store, {
__experimentalUseThunks: true,
Expand All @@ -60,12 +51,33 @@ const wordpressPlansStore = createReduxStore( store, {
plans: action.plans,
};

case 'REQUEST_AI_ASSISTANT_FEATURE':
return {
...state,
features: {
...state.features,
aiAssistant: {
...state.features.aiAssistant,
_meta: {
...state.features.aiAssistant._meta,
isRequesting: true,
},
},
},
};

case 'STORE_AI_ASSISTANT_FEATURE': {
return {
...state,
features: {
...state.features,
aiAssistant: action.feature,
aiAssistant: {
...action.feature,
_meta: {
...state.features.aiAssistant._meta,
isRequesting: false,
},
},
},
};
}
Expand Down Expand Up @@ -117,18 +129,13 @@ const wordpressPlansStore = createReduxStore( store, {
return actions.setPlans( plans );
},

getAiAssistantFeature:
() =>
async ( { dispatch } ) => {
const response: SiteAIAssistantFeatureEndpointResponseProps = await apiFetch( {
path: '/wpcom/v2/jetpack-ai/ai-assistant-feature',
} );
getAiAssistantFeature: ( state: PlanStateProps ) => {
if ( state?.features?.aiAssistant ) {
return;
}

// Store the feature in the store.
dispatch(
actions.storeAiAssistantFeature( mapAIFeatureResponseToAiFeatureProps( response ) )
);
},
return actions.fetchAiAssistantFeature();
},
},
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ export type AiFeatureProps = {
nextStart: string;
requestsCount: number;
};
_meta?: {
isRequesting: boolean;
};
};

0 comments on commit d7226a9

Please sign in to comment.