CYS - Filter out patterns with external dependencies (#48618)

* Filter out patterns with external dependencies

* Add changefile(s) from automation for the following project(s): woocommerce

* Fix test

* Remove excluding patterns by id

* Bring back url

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Alba Rincón 2024-07-08 08:53:06 +02:00 committed by GitHub
parent 68532ce433
commit c763bdf695
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 99 additions and 38 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
CYS - Filter out patterns with external dependencies.

View File

@ -11,11 +11,6 @@ use WP_Upgrader;
class PTKPatternsStore {
const TRANSIENT_NAME = 'ptk_patterns';
// Some patterns need to be excluded because they have dependencies which
// are not installed by default (like Jetpack). Otherwise, the user
// would see an error when trying to insert them in the editor.
const EXCLUDED_PATTERNS = array( '13923', '14781', '14779', '13666', '13664', '13660', '13588', '14922', '14880', '13596', '13967', '13958', '15050', '15027' );
const CATEGORY_MAPPING = array(
'testimonials' => 'reviews',
);
@ -121,17 +116,16 @@ class PTKPatternsStore {
}
/**
* Filter patterns to exclude those with the given IDs.
* Filter the patterns that have external dependencies.
*
* @param array $patterns The patterns to filter.
* @param array $pattern_ids The pattern IDs to exclude.
* @return array
*/
private function filter_patterns( array $patterns, array $pattern_ids ) {
private function filter_patterns( array $patterns ) {
return array_values(
array_filter(
$patterns,
function ( $pattern ) use ( $pattern_ids ) {
function ( $pattern ) {
if ( ! isset( $pattern['ID'] ) ) {
return true;
}
@ -140,7 +134,11 @@ class PTKPatternsStore {
return false;
}
return ! in_array( (string) $pattern['ID'], $pattern_ids, true );
if ( $this->has_external_dependencies( $pattern ) ) {
return false;
}
return true;
}
)
);
@ -213,7 +211,7 @@ class PTKPatternsStore {
return;
}
$patterns = $this->filter_patterns( $patterns, self::EXCLUDED_PATTERNS );
$patterns = $this->filter_patterns( $patterns );
$patterns = $this->map_categories( $patterns );
set_transient( self::TRANSIENT_NAME, $patterns );
@ -253,4 +251,25 @@ class PTKPatternsStore {
$patterns
);
}
/**
* Check if the pattern has external dependencies.
*
* @param array $pattern The pattern to check.
*
* @return bool
*/
private function has_external_dependencies( $pattern ) {
if ( ! isset( $pattern['dependencies'] ) || ! is_array( $pattern['dependencies'] ) ) {
return false;
}
foreach ( $pattern['dependencies'] as $dependency ) {
if ( 'woocommerce' !== $dependency ) {
return true;
}
}
return false;
}
}

View File

@ -166,33 +166,6 @@ class PTKPatternsStoreTest extends \WP_UnitTestCase {
$this->assertEquals( $expected_patterns, $patterns );
}
/**
* Test fetch_patterns should filter out the excluded patterns.
*/
public function test_fetch_patterns_should_filter_out_the_excluded_patterns() {
update_option( 'woocommerce_allow_tracking', 'yes' );
$api_patterns = array(
array(
'title' => 'My pattern',
'slug' => 'my-pattern',
),
array(
'ID' => PTKPatternsStore::EXCLUDED_PATTERNS[0],
'title' => 'Excluded pattern',
'slug' => 'excluded-pattern',
),
);
$this->ptk_client
->expects( $this->once() )
->method( 'fetch_patterns' )
->willReturn( $api_patterns );
$this->pattern_store->fetch_patterns();
$this->assertEquals( array( $api_patterns[0] ), get_transient( PTKPatternsStore::TRANSIENT_NAME ) );
}
/**
* Test fetch_patterns should register testimonials category as reviews.
*/
@ -240,6 +213,71 @@ class PTKPatternsStoreTest extends \WP_UnitTestCase {
$this->assertEquals( $expected_patterns, get_transient( PTKPatternsStore::TRANSIENT_NAME ) );
}
/**
* Test fetch_patterns should filter out the patterns with dependencies.
*/
public function test_fetch_patterns_should_filter_out_the_patterns_with_dependencies_diff_than_woocommerce() {
update_option( 'woocommerce_allow_tracking', 'yes' );
$ptk_patterns = array(
array(
'ID' => 1,
'title' => 'No deps',
),
array(
'ID' => 2,
'title' => 'Jetpack dep',
'dependencies' => [ 'jetpack' ],
),
array(
'ID' => 3,
'title' => 'Jetpack and WooCommerce dep',
'dependencies' => [ 'woocommerce', 'jetpack' ],
),
array(
'ID' => 4,
'title' => 'WooCommerce dep',
'dependencies' => [ 'woocommerce' ],
),
array(
'ID' => 5,
'title' => 'Empty deps',
'dependencies' => [],
),
);
$expected_patterns = array(
array(
'ID' => 1,
'title' => 'No deps',
),
array(
'ID' => 4,
'title' => 'WooCommerce dep',
'dependencies' => [ 'woocommerce' ],
),
array(
'ID' => 5,
'title' => 'Empty deps',
'dependencies' => [],
),
);
$this->ptk_client
->expects( $this->once() )
->method( 'fetch_patterns' )
->willReturnOnConsecutiveCalls(
$ptk_patterns,
array()
);
$this->pattern_store->fetch_patterns();
$patterns = get_transient( PTKPatternsStore::TRANSIENT_NAME );
$this->assertEquals( $expected_patterns, $patterns );
$this->assertEquals( $expected_patterns, get_transient( PTKPatternsStore::TRANSIENT_NAME ) );
}
/**
* Asserts that the response is an error with the expected error code and message.
*