Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"micromatch": "4.0.8",
"nanoid": "3.3.18",
"postcss": "8.5.26",
"proxy-addr": "2.0.8",
"shell-quote": "1.11.0",
"svgo": "3.3.4",
"websocket-driver": "0.7.5",
"yaml": "1.10.3"
Expand Down
43 changes: 41 additions & 2 deletions packages/wp/Tests/BricksSynchronizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ function get_post_types( $args = array(), $output = 'names' ) {

if ( ! function_exists( 'get_posts' ) ) {
function get_posts( $args = array() ) {
$meta_keys = array_column( array_filter( $args['meta_query'] ?? array(), 'is_array' ), 'key' );
$post_ids = array();
$meta_keys = array();
foreach ( array_filter( $args['meta_query'] ?? array(), 'is_array' ) as $clause ) {
$meta_keys = array_merge( $meta_keys, (array) $clause['key'] );
}
$post_ids = array();

foreach ( $GLOBALS['cf_test_post_meta'] as $post_id => $post_meta ) {
if ( array_intersect( $meta_keys, array_keys( $post_meta ) ) ) {
Expand Down Expand Up @@ -447,6 +450,42 @@ public function testSelectorsThatSanitizeToTheSameIdProduceOneClass(): void {
);
}

public function testReferenceSweepFindsEveryBricksMetaKeyAndSkipsUnrelatedPosts(): void {
$meta_keys = array(
'_bricks_page_header_2',
'_bricks_page_content_2',
'_bricks_page_footer_2',
'_bricks_page_settings',
'_bricks_template_settings',
);

foreach ( $meta_keys as $index => $meta_key ) {
$GLOBALS['cf_test_post_meta'][ $index + 1 ] = array(
$meta_key => array( '_cssGlobalClasses' => array( 'removed_c', 'keep-me' ) ),
);
}
$GLOBALS['cf_test_post_meta'][6] = array( '_bricks_page_content_2' => '' );
$GLOBALS['cf_test_post_meta'][7] = array( '_bricks_page_settings' => array() );
$GLOBALS['cf_test_post_meta'][8] = array(
'unrelated_meta' => array( '_cssGlobalClasses' => array( 'removed_c' ) ),
);

$method = new ReflectionMethod( BricksFunctions::class, 'remove_class_references' );
$method->setAccessible( true );
$method->invoke( $this->createBricksFunctions(), array( 'removed_c' ) );

$this->assertSame( array( range( 1, 7 ) ), $GLOBALS['cf_test_meta_primes'] );
foreach ( $meta_keys as $index => $meta_key ) {
$this->assertSame(
array( 'keep-me' ),
$GLOBALS['cf_test_post_meta'][ $index + 1 ][ $meta_key ]['_cssGlobalClasses']
);
}
$this->assertSame( '', $GLOBALS['cf_test_post_meta'][6]['_bricks_page_content_2'] );
$this->assertSame( array(), $GLOBALS['cf_test_post_meta'][7]['_bricks_page_settings'] );
$this->assertSame( array( 'removed_c' ), $GLOBALS['cf_test_post_meta'][8]['unrelated_meta']['_cssGlobalClasses'] );
}

public function testReferenceSweepIsBatchedAndRunsAfterClassesArePersisted(): void {
$GLOBALS['cf_test_options'][ BricksFunctions::BRICKS_CLASSES_OPTION ] = array(
array(
Expand Down
16 changes: 9 additions & 7 deletions packages/wp/wp/App/Bricks/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,15 @@ private function remove_class_references( array $class_ids ): void {
defined( 'BRICKS_DB_TEMPLATE_SETTINGS' ) ? constant( 'BRICKS_DB_TEMPLATE_SETTINGS' ) : '_bricks_template_settings',
);

$meta_query = array( 'relation' => 'OR' );
foreach ( $meta_keys as $meta_key ) {
$meta_query[] = array(
'key' => $meta_key,
'compare' => 'EXISTS',
);
}
// Separate OR/EXISTS clauses create one postmeta join per key, multiplying
// rows on posts with lots of metadata. Match all keys through a single join.
$meta_query = array(
array(
'key' => $meta_keys,
'compare_key' => 'IN',
'compare' => 'EXISTS',
),
);

$post_ids = get_posts(
array(
Expand Down
27 changes: 27 additions & 0 deletions scripts/test-wp-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,33 @@ if (!is_file($path)) {
WP_CLI::success("Generated stylesheet exists in the uploads directory.");
'

# Exercise the real WP_Query SQL builder: unit tests use a get_posts() stub.
# Short-circuit execution so this regression check never runs a Cartesian join.
wp_cli eval '
$queries = array();
$capture = static function ($posts, $query) use (&$queries) {
$queries[] = $query->request;
return array();
};
add_filter("posts_pre_query", $capture, 10, 2);
try {
$class = new ReflectionClass(\CoreFramework\App\Bricks\Functions::class);
$method = $class->getMethod("remove_class_references");
$method->setAccessible(true);
$method->invoke($class->newInstanceWithoutConstructor(), array("cf-e2e-removed_c"));
} finally {
remove_filter("posts_pre_query", $capture, 10);
}
global $wpdb;
if (count($queries) !== 1 || substr_count($queries[0], "JOIN $wpdb->postmeta") !== 1) {
WP_CLI::error("Bricks reference discovery must use exactly one postmeta join.");
}
if (strpos($queries[0], "$wpdb->postmeta.meta_key IN (") === false) {
WP_CLI::error("Bricks reference discovery must filter metadata keys together.");
}
WP_CLI::success("Bricks reference discovery uses one postmeta join.");
'

curl --fail --silent --show-error --cookie-jar "$COOKIE_FILE" "$SITE_URL/wp-login.php" >/dev/null
curl --fail --silent --show-error --location \
--cookie "$COOKIE_FILE" \
Expand Down
Loading