Merged in feature/MAW-855-import-code-into-aws (pull request #2)

code import from pantheon

* code import from pantheon
This commit is contained in:
Tony Volpe
2023-12-04 23:08:14 +00:00
parent 8c9b1312bc
commit 8f4b5efda6
4766 changed files with 185592 additions and 239967 deletions

View File

@@ -6,7 +6,7 @@ import { getSettingWithCoercion } from '@woocommerce/settings';
import { isBoolean } from '@woocommerce/types';
const filteringForPhpTemplate = getSettingWithCoercion(
'is_rendering_php_template',
'isRenderingPhpTemplate',
false,
isBoolean
);
@@ -34,6 +34,34 @@ export function getUrlParameter( name: string ) {
*/
export function changeUrl( newUrl: string ) {
if ( filteringForPhpTemplate ) {
/**
* We want to remove page number from URL whenever filters are changed.
* This will move the user to the first page of results.
*
* There are following page number formats:
* 1. query-{number}-page={number} (ex. query-1-page=2)
* - ref: https://github.com/WordPress/gutenberg/blob/5693a62214b6c76d3dc5f3f69d8aad187748af79/packages/block-library/src/query-pagination-numbers/index.php#L18
* 2. query-page={number} (ex. query-page=2)
* - ref: same as above
* 3. page/{number} (ex. page/2) (Default WordPress pagination format)
*/
newUrl = newUrl.replace(
/(?:query-(?:\d+-)?page=(\d+))|(?:page\/(\d+))/g,
''
);
/**
* If the URL ends with '?', we remove the trailing '?' from the URL.
* The trailing '?' in a URL is unnecessary and can cause the page to
* reload, which can negatively affect performance. By removing the '?',
* we prevent this unnecessary reload. This is safe to do even if there
* are query parameters, as they will not be affected by the removal
* of a trailing '?'.
*/
if ( newUrl.endsWith( '?' ) ) {
newUrl = newUrl.slice( 0, -1 );
}
window.location.href = newUrl;
} else {
window.history.replaceState( {}, '', newUrl );

View File

@@ -0,0 +1,35 @@
/**
* External dependencies
*/
import { BlockInstance } from '@wordpress/blocks';
/**
* Recursively searches through an array of `BlockInstance` objects and their nested `innerBlocks` arrays to find a block that matches a given condition.
*
* @param { { blocks: BlockInstance[], findCondition: Function } } parameters Parameters containing an array of `BlockInstance` objects to search through and a function that takes a `BlockInstance` object as its argument and returns a boolean indicating whether the block matches the desired condition.
* @return If a matching block is found, the function returns the `BlockInstance` object. If no matching block is found, the function returns `undefined`.
*/
export const findBlock = ( {
blocks,
findCondition,
}: {
blocks: BlockInstance[];
findCondition: ( block: BlockInstance ) => boolean;
} ): BlockInstance | undefined => {
for ( const block of blocks ) {
if ( findCondition( block ) ) {
return block;
}
if ( block.innerBlocks ) {
const foundChildBlock = findBlock( {
blocks: block.innerBlocks,
findCondition,
} );
if ( foundChildBlock ) {
return foundChildBlock;
}
}
}
return undefined;
};

View File

@@ -10,3 +10,4 @@ export * from './sanitize-html';
export * from './is-site-editor-page';
export * from './is-widget-editor-page';
export * from './trim-words';
export * from './find-block';

View File

@@ -1,29 +0,0 @@
/**
* Get the src of the first image attached to a product (the featured image).
*
* @param {Object} product The product object to get the images from.
* @param {Array} product.images The array of images, destructured from the product object.
* @return {string} The full URL to the image.
*/
export function getImageSrcFromProduct( product ) {
if ( ! product || ! product.images || ! product.images.length ) {
return '';
}
return product.images[ 0 ].src || '';
}
/**
* Get the ID of the first image attached to a product (the featured image).
*
* @param {Object} product The product object to get the images from.
* @param {Array} product.images The array of images, destructured from the product object.
* @return {number} The ID of the image.
*/
export function getImageIdFromProduct( product ) {
if ( ! product || ! product.images || ! product.images.length ) {
return 0;
}
return product.images[ 0 ].id || 0;
}

View File

@@ -0,0 +1,46 @@
/**
* External dependencies
*/
import type { SearchListItem } from '@woocommerce/editor-components/search-list-control/types';
import type { ProductResponseItem } from '@woocommerce/types';
/**
* Converts a Product object into a shape compatible with the `SearchListControl`
*/
export const convertProductResponseItemToSearchItem = (
product: ProductResponseItem
): SearchListItem< ProductResponseItem > => {
const { id, name, parent } = product;
return {
id,
name,
parent,
breadcrumbs: [],
children: [],
details: product,
value: product.slug,
};
};
/**
* Get the src of the first image attached to a product (the featured image).
*/
export function getImageSrcFromProduct( product: ProductResponseItem ) {
if ( ! product || ! product.images || ! product.images.length ) {
return '';
}
return product.images[ 0 ].src || '';
}
/**
* Get the ID of the first image attached to a product (the featured image).
*/
export function getImageIdFromProduct( product: ProductResponseItem ) {
if ( ! product || ! product.images || ! product.images.length ) {
return 0;
}
return product.images[ 0 ].id || 0;
}

View File

@@ -17,7 +17,7 @@ export default {
*/
columns: {
type: 'number',
default: getSetting( 'default_columns', 3 ),
default: getSetting( 'defaultColumns', 3 ),
},
/**
@@ -25,7 +25,7 @@ export default {
*/
rows: {
type: 'number',
default: getSetting( 'default_rows', 3 ),
default: getSetting( 'defaultRows', 3 ),
},
/**