Skip to content

Commit

Permalink
Merge pull request #7487 from google/enhancement/7390-improve-product…
Browse files Browse the repository at this point in the history
…-reports

Update product base paths to support permalink regular expressions.
  • Loading branch information
tofumatt authored Aug 22, 2023
2 parents f1a8483 + 17df57c commit 77eb692
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion assets/js/googlesitekit/datastore/site/info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe( 'core/site site info', () => {
label: 'Post',
},
],
productBasePaths: [ '/product/' ],
productBasePaths: [ '^/product/' ],
};
const entityInfoVar = '_googlesitekitEntityData';
const entityInfo = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function PopularProductsWidget( props ) {
dimensionFilters: {
pagePath: {
filterType: 'stringFilter',
matchType: 'BEGINS_WITH',
matchType: 'PARTIAL_REGEXP',
value: productBasePaths,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ const reportOptions = {
dimensionFilters: {
pagePath: {
filterType: 'stringFilter',
matchType: 'BEGINS_WITH',
value: [ '/product/' ],
matchType: 'PARTIAL_REGEXP',
value: [ '^/product/' ],
},
},
metrics: [ { name: 'screenPageViews' } ],
Expand Down
6 changes: 5 additions & 1 deletion includes/Core/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,11 @@ private function get_product_base_paths() {
$permalink_template = home_url( $permastruct );
$product_url_path = URL::parse( $permalink_template, PHP_URL_PATH );
list( $product_base_path ) = explode( '%product%', $product_url_path, 2 );
$product_base_paths[] = $product_base_path;
$product_base_path = str_replace( $wp_rewrite->rewritecode, $wp_rewrite->rewritereplace, $product_base_path );
if ( strpos( $product_base_path, '^' ) !== 0 ) {
$product_base_path = '^' . $product_base_path;
}
$product_base_paths[] = $product_base_path;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export const provideSiteInfo = ( registry, extraData = {} ) => {
label: 'Media',
},
],
productBasePaths: [ '/product/' ],
productBasePaths: [ '^/product/' ],
keyMetricsSetupCompleted: false,
};

Expand Down
40 changes: 30 additions & 10 deletions tests/phpunit/integration/Core/Assets/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,14 @@ public function test_base_data__product_base_paths__hidden_post_type() {
/**
* @dataProvider data_product_base_paths
*/
public function test_base_data__product_base_paths( $post_type_args, $expected ) {
public function test_base_data__product_base_paths( $args, $expected ) {
$this->enable_feature( 'userInput' );
$this->set_permalink_structure( '/%postname%/' );
register_post_type( 'product', $post_type_args );
register_post_type( 'product', $args['post_type_args'] );

if ( ! empty( $args['product_permastruct'] ) ) {
add_permastruct( 'product', $args['product_permastruct'] );
}

$data = $this->get_inline_base_data();

Expand All @@ -267,19 +271,35 @@ public function test_base_data__product_base_paths( $post_type_args, $expected )

public function data_product_base_paths() {
return array(
'public post type' => array(
'public post type' => array(
array(
'post_type_args' =>
array(
'public' => true,
'rewrite' => true,
),
),
array( '^/product/' ),
),
'custom rewrite rule' => array(
array(
'public' => true,
'rewrite' => true,
'post_type_args' => array(
'public' => true,
'rewrite' => array( 'slug' => 'fancy-products' ),
),
),
array( '/product/' ),
array( '^/fancy-products/' ),
),
'custom rewrite rule' => array(
'custom product permastruct' => array(
array(
'public' => true,
'rewrite' => array( 'slug' => 'fancy-products' ),
'post_type_args' =>
array(
'public' => true,
'rewrite' => true,
),
'product_permastruct' => '/product/%year%/%monthnum%/%category%/',
),
array( '/fancy-products/' ),
array( '^/product/([0-9]{4})/([0-9]{1,2})/%category%/' ),
),
);
}
Expand Down

0 comments on commit 77eb692

Please sign in to comment.