Skip to content
123 changes: 123 additions & 0 deletions includes/classes/BlockEditor/Patterns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Modification to patterns.
*
* @package Orbit
*/

namespace Eighteen73\Orbit\BlockEditor;

use Eighteen73\Orbit\Singleton;

/**
* Patterns class.
*/
class Patterns {

use Singleton;

/**
* Primary constructor
*
* @return void
*/
public function setup() {
if ( ! apply_filters( 'orbit_enable_disable_external_patterns', true ) ) {
return;
}

add_action( 'init', [ $this, 'remove_woocommerce_patterns' ], 15 );
add_filter( 'rest_dispatch_request', [ $this, 'filter_woocommerce_patterns_rest' ], 10, 4 );
}

/**
* Remove WooCommerce patterns early in the init process.
*
* @return void
*/
public function remove_woocommerce_patterns(): void {
$patterns = \WP_Block_Patterns_Registry::get_instance()->get_all_registered();

if ( ! empty( $patterns ) ) {
foreach ( $patterns as $pattern ) {
if ( $this->is_woocommerce_pattern( $pattern ) ) {
unregister_block_pattern( $pattern['name'] );
}
}
}
}

/**
* Filter WooCommerce patterns from REST API responses.
*
* @param mixed $dispatch_result Dispatch result, will be used if not empty.
* @param \WP_REST_Request $request Request used to generate the response.
* @param string $route Route matched for the request.
* @param array $handler Route handler used for the request.
* @return mixed
*/
public function filter_woocommerce_patterns_rest( $dispatch_result, $request, $route, $handler ) {
// Check if this is a block patterns request
if ( strpos( $route, '/wp/v2/block-patterns/patterns' ) !== 0 ) {
return $dispatch_result;
}

// If we already have a result, filter it
if ( ! empty( $dispatch_result ) && is_array( $dispatch_result ) ) {
return $this->filter_patterns_from_response( $dispatch_result );
}

return $dispatch_result;
}

/**
* Check if a pattern is a WooCommerce pattern.
*
* @param array $pattern The pattern data.
* @return bool
*/
private function is_woocommerce_pattern( array $pattern ): bool {
// Check by category
if ( ! empty( $pattern['categories'] ) && in_array( 'woo-commerce', $pattern['categories'], true ) ) {
return true;
}

// Check by pattern name/slug
$woo_prefixes = [
'woocommerce-blocks/',
'woocommerce/',
'woo/',
'wc-',
];

foreach ( $woo_prefixes as $prefix ) {
if ( strpos( $pattern['name'], $prefix ) === 0 ) {
return true;
}
}

return false;
}

/**
* Filter WooCommerce patterns from a REST response.
*
* @param array $response The response data.
* @return array
*/
private function filter_patterns_from_response( array $response ): array {
if ( isset( $response['data'] ) && is_array( $response['data'] ) ) {
$response['data'] = array_filter(
$response['data'],
function ( $pattern ) {
return ! $this->is_woocommerce_pattern( $pattern );
}
);

// Re-index the array to maintain proper JSON structure
$response['data'] = array_values( $response['data'] );
}

return $response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
* @package Orbit
*/

namespace Eighteen73\Orbit;
namespace Eighteen73\Orbit\Branding;

use Exception;
use Eighteen73\Orbit\Singleton;
use Eighteen73\Orbit\Environment;
use Eighteen73\Orbit\Utilities\Templates;
use Eighteen73\Orbit\Dependencies\Pelago\Emogrifier\CssInliner;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
* @package Orbit
*/

namespace Eighteen73\Orbit;
namespace Eighteen73\Orbit\Media;

use Eighteen73\Orbit\Environment;
use Eighteen73\Orbit\Singleton;
use Roots\WPConfig\Config;
use Roots\WPConfig\Exceptions\UndefinedConfigKeyException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* @package Orbit
*/

namespace Eighteen73\Orbit;
namespace Eighteen73\Orbit\Monitoring;

use Eighteen73\Orbit\Singleton;
use WP_REST_Request;

/**
Expand Down
26 changes: 0 additions & 26 deletions includes/classes/OtherFilters.php

This file was deleted.

29 changes: 29 additions & 0 deletions includes/classes/ThirdParty/WooCommerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Modifications to WooCommerce.
*
* @package Orbit
*/

namespace Eighteen73\Orbit\ThirdParty;

use Eighteen73\Orbit\Singleton;

/**
* Modifications to WooCommerce.
*/
class WooCommerce {
use Singleton;

/**
* Run on init
*
* @return void
*/
public function setup() {

// Force WooCommerce tracking to always be disabled.
// This setting loads additional patterns from PTK.
add_filter( 'option_woocommerce_allow_tracking', '__return_false' );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* @package Orbit
*/

namespace Eighteen73\Orbit;
namespace Eighteen73\Orbit\Utilities;

use Eighteen73\Orbit\Environment;
use Eighteen73\Orbit\Singleton;

/**
* This class is built upon BE Media from Production so all due credit to those authors.
Expand Down
10 changes: 5 additions & 5 deletions orbit.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

DisallowIndexing\DisallowIndexing::instance()->setup();
Performance\Fast404::instance()->setup();

ThirdParty\WooCommerce::instance()->setup();

add_action(
'init',
Expand All @@ -45,9 +45,9 @@ function () {
Security\HideAuthor::instance()->setup();
Security\HideVersion::instance()->setup();
Security\RemoveHeadLinks::instance()->setup();
BrandedEmails::instance()->setup();
OtherFilters::instance()->setup();
HealthCheck::instance()->setup();
RemoteFiles::instance()->setup();
Branding\BrandedEmails::instance()->setup();
Monitoring\HealthCheck::instance()->setup();
Media\RemoteFiles::instance()->setup();
BlockEditor\Patterns::instance()->setup();
}
);
2 changes: 2 additions & 0 deletions templates/branded-emails/email-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Eighteen73\Orbit;

use Eighteen73\Orbit\Branding\BrandedEmails;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
Expand Down
2 changes: 2 additions & 0 deletions templates/branded-emails/email-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Eighteen73\Orbit;

use Eighteen73\Orbit\Utilities\Templates;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
Expand Down