Skip to content

Commit 45a5640

Browse files
Merge pull request #20 from eighteen73/develop
Block WooCommerce patterns from loading in the editor
2 parents 14c0466 + d98ccf9 commit 45a5640

File tree

11 files changed

+175
-36
lines changed

11 files changed

+175
-36
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Modification to patterns.
4+
*
5+
* @package Orbit
6+
*/
7+
8+
namespace Eighteen73\Orbit\BlockEditor;
9+
10+
use Eighteen73\Orbit\Singleton;
11+
12+
/**
13+
* Patterns class.
14+
*/
15+
class Patterns {
16+
17+
use Singleton;
18+
19+
/**
20+
* Primary constructor
21+
*
22+
* @return void
23+
*/
24+
public function setup() {
25+
if ( ! apply_filters( 'orbit_enable_disable_external_patterns', true ) ) {
26+
return;
27+
}
28+
29+
add_action( 'init', [ $this, 'remove_woocommerce_patterns' ], 15 );
30+
add_filter( 'rest_dispatch_request', [ $this, 'filter_woocommerce_patterns_rest' ], 10, 4 );
31+
}
32+
33+
/**
34+
* Remove WooCommerce patterns early in the init process.
35+
*
36+
* @return void
37+
*/
38+
public function remove_woocommerce_patterns(): void {
39+
$patterns = \WP_Block_Patterns_Registry::get_instance()->get_all_registered();
40+
41+
if ( ! empty( $patterns ) ) {
42+
foreach ( $patterns as $pattern ) {
43+
if ( $this->is_woocommerce_pattern( $pattern ) ) {
44+
unregister_block_pattern( $pattern['name'] );
45+
}
46+
}
47+
}
48+
}
49+
50+
/**
51+
* Filter WooCommerce patterns from REST API responses.
52+
*
53+
* @param mixed $dispatch_result Dispatch result, will be used if not empty.
54+
* @param \WP_REST_Request $request Request used to generate the response.
55+
* @param string $route Route matched for the request.
56+
* @param array $handler Route handler used for the request.
57+
* @return mixed
58+
*/
59+
public function filter_woocommerce_patterns_rest( $dispatch_result, $request, $route, $handler ) {
60+
// Check if this is a block patterns request
61+
if ( strpos( $route, '/wp/v2/block-patterns/patterns' ) !== 0 ) {
62+
return $dispatch_result;
63+
}
64+
65+
// If we already have a result, filter it
66+
if ( ! empty( $dispatch_result ) && is_array( $dispatch_result ) ) {
67+
return $this->filter_patterns_from_response( $dispatch_result );
68+
}
69+
70+
return $dispatch_result;
71+
}
72+
73+
/**
74+
* Check if a pattern is a WooCommerce pattern.
75+
*
76+
* @param array $pattern The pattern data.
77+
* @return bool
78+
*/
79+
private function is_woocommerce_pattern( array $pattern ): bool {
80+
// Check by category
81+
if ( ! empty( $pattern['categories'] ) && in_array( 'woo-commerce', $pattern['categories'], true ) ) {
82+
return true;
83+
}
84+
85+
// Check by pattern name/slug
86+
$woo_prefixes = [
87+
'woocommerce-blocks/',
88+
'woocommerce/',
89+
'woo/',
90+
'wc-',
91+
];
92+
93+
foreach ( $woo_prefixes as $prefix ) {
94+
if ( strpos( $pattern['name'], $prefix ) === 0 ) {
95+
return true;
96+
}
97+
}
98+
99+
return false;
100+
}
101+
102+
/**
103+
* Filter WooCommerce patterns from a REST response.
104+
*
105+
* @param array $response The response data.
106+
* @return array
107+
*/
108+
private function filter_patterns_from_response( array $response ): array {
109+
if ( isset( $response['data'] ) && is_array( $response['data'] ) ) {
110+
$response['data'] = array_filter(
111+
$response['data'],
112+
function ( $pattern ) {
113+
return ! $this->is_woocommerce_pattern( $pattern );
114+
}
115+
);
116+
117+
// Re-index the array to maintain proper JSON structure
118+
$response['data'] = array_values( $response['data'] );
119+
}
120+
121+
return $response;
122+
}
123+
}

includes/classes/BrandedEmails.php renamed to includes/classes/Branding/BrandedEmails.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
* @package Orbit
66
*/
77

8-
namespace Eighteen73\Orbit;
8+
namespace Eighteen73\Orbit\Branding;
99

1010
use Exception;
11+
use Eighteen73\Orbit\Singleton;
12+
use Eighteen73\Orbit\Environment;
13+
use Eighteen73\Orbit\Utilities\Templates;
1114
use Eighteen73\Orbit\Dependencies\Pelago\Emogrifier\CssInliner;
1215

1316
/**

includes/classes/RemoteFiles.php renamed to includes/classes/Media/RemoteFiles.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
* @package Orbit
66
*/
77

8-
namespace Eighteen73\Orbit;
8+
namespace Eighteen73\Orbit\Media;
99

10+
use Eighteen73\Orbit\Environment;
11+
use Eighteen73\Orbit\Singleton;
1012
use Roots\WPConfig\Config;
1113
use Roots\WPConfig\Exceptions\UndefinedConfigKeyException;
1214

includes/classes/HealthCheck.php renamed to includes/classes/Monitoring/HealthCheck.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
* @package Orbit
66
*/
77

8-
namespace Eighteen73\Orbit;
8+
namespace Eighteen73\Orbit\Monitoring;
99

10+
use Eighteen73\Orbit\Singleton;
1011
use WP_REST_Request;
1112

1213
/**

includes/classes/OtherFilters.php

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Modifications to WooCommerce.
4+
*
5+
* @package Orbit
6+
*/
7+
8+
namespace Eighteen73\Orbit\ThirdParty;
9+
10+
use Eighteen73\Orbit\Singleton;
11+
12+
/**
13+
* Modifications to WooCommerce.
14+
*/
15+
class WooCommerce {
16+
use Singleton;
17+
18+
/**
19+
* Run on init
20+
*
21+
* @return void
22+
*/
23+
public function setup() {
24+
25+
// Force WooCommerce tracking to always be disabled.
26+
// This setting loads additional patterns from PTK.
27+
add_filter( 'option_woocommerce_allow_tracking', '__return_false' );
28+
}
29+
}

includes/classes/Templates.php renamed to includes/classes/Utilities/Templates.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
* @package Orbit
66
*/
77

8-
namespace Eighteen73\Orbit;
8+
namespace Eighteen73\Orbit\Utilities;
9+
10+
use Eighteen73\Orbit\Environment;
11+
use Eighteen73\Orbit\Singleton;
912

1013
/**
1114
* This class is built upon BE Media from Production so all due credit to those authors.

orbit.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
DisallowIndexing\DisallowIndexing::instance()->setup();
3131
Performance\Fast404::instance()->setup();
32-
32+
ThirdParty\WooCommerce::instance()->setup();
3333

3434
add_action(
3535
'init',
@@ -45,9 +45,9 @@ function () {
4545
Security\HideAuthor::instance()->setup();
4646
Security\HideVersion::instance()->setup();
4747
Security\RemoveHeadLinks::instance()->setup();
48-
BrandedEmails::instance()->setup();
49-
OtherFilters::instance()->setup();
50-
HealthCheck::instance()->setup();
51-
RemoteFiles::instance()->setup();
48+
Branding\BrandedEmails::instance()->setup();
49+
Monitoring\HealthCheck::instance()->setup();
50+
Media\RemoteFiles::instance()->setup();
51+
BlockEditor\Patterns::instance()->setup();
5252
}
5353
);

phpcs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<!-- Scan all files in directory -->
66
<file>.</file>
77
<exclude-pattern type="relative">lib/packages/*</exclude-pattern>
8-
<exclude-pattern type="relative">includes/Dependencies/*</exclude-pattern>
8+
<exclude-pattern type="relative">includes/lib/*</exclude-pattern>
99

1010
<!-- Scan only PHP files -->
1111
<arg name="extensions" value="php"/>

templates/branded-emails/email-styles.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Eighteen73\Orbit;
1111

12+
use Eighteen73\Orbit\Branding\BrandedEmails;
13+
1214
if ( ! defined( 'ABSPATH' ) ) {
1315
exit; // Exit if accessed directly.
1416
}

0 commit comments

Comments
 (0)