Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic function to determine if URL is a store page #45299

Merged
merged 9 commits into from Mar 18, 2024

Conversation

chihsuan
Copy link
Member

@chihsuan chihsuan commented Mar 5, 2024

Submission Review Guidelines:

Changes proposed in this Pull Request:

Closes #45110.

The function is_store_page is added to WCAdminHelper and is used to determine if a URL is a store page.

Store pages are defined as:

  • Shop Page
  • Cart Page
  • Checkout Page
  • My Account Page
  • Terms and Conditions Page
  • Privacy Policy Page

Additionally, the following autogenerated pages should be included:

  • Product
  • Product Categories (taxonomy=product_cat)
  • Product Tags (taxonomy=product_tag)

The function ignores the domain and protocol of the URL and only checks the path and query string so that users can only pass $_SERVER['REQUEST_URI'] to the function for checking if the current page is a store page.

How to test the changes in this Pull Request:

Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:

  1. Create a fresh site
  2. Install WooCommerce Smooth Generator and Code Snippets plugins
  3. Either skip or complete Core profiler
  4. Go to /wp-admin/tools.php?page=smoothgenerator > Generate 10 products OR run wp wc generate products 10
  5. Go to /wp-admin/admin.php?page=snippets
  6. Add new Snippet
use Automattic\WooCommerce\Admin\WCAdminHelper;

function maybe_add_banner_to_footer() {
	if ( wp_doing_ajax() || wp_doing_cron() || WC()->is_rest_api_request() ) {
		return;
	}

	$is_store = WCAdminHelper::is_store_page();
	if ( $is_store ) {
    	echo '<div style="position: fixed; bottom: 0; background-color: red; color: white; width: 100%; text-align: center;">This is a store page!</div>';
	}
}

add_action('wp_footer', 'maybe_add_banner_to_footer');
  1. Select Only run on site front-end and activate
  2. Go to frontend
  3. Confirm the This is a store page! red banner is displayed on the store pages (Shop, Cart, Checkout, My Account, Terms and Conditions, Privacy Policy, Product, Product Categories, Product Tags) and not displayed on other pages (Home, Sample Page, etc.)
  4. Go to /wp-admin/admin.php?page=wc-settings&tab=advanced
  5. Change Terms and conditions page to Sample Page
  6. Go to Sample Page
  7. Confirm the banner is displayed on the Sample Page
  8. Go to /wp-admin/options-permalink.php
  9. Change Product permalinks to Shop base with category
  10. Change Product category base to test-category
  11. Go to a product page
  12. Confirm the banner is still displayed on the product page
  13. Go to a product category page
Screenshot 2024-03-05 at 11 32 37

Changelog entry

  • Automatically create a changelog entry from the details below.

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

Comment

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Mar 5, 2024
@chihsuan chihsuan self-assigned this Mar 5, 2024
Copy link
Contributor

github-actions bot commented Mar 5, 2024

Test Results Summary

Commit SHA: 095c963

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 37s
E2E Tests343001003537m 35s

To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

@chihsuan chihsuan requested review from a team, psealock and rjchow March 5, 2024 04:25
Copy link
Contributor

github-actions bot commented Mar 5, 2024

Hi @rjchow,

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

Copy link
Contributor

@psealock psealock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good @chihsuan. What about a page with a WooCommerce block, such as the All Product block. Would that count as a store page?

@chihsuan
Copy link
Member Author

What about a page with a WooCommerce block, such as the All Product block. Would that count as a store page?

Good question. 👍 I think it would but I'm not very sure. @adrianduffell, what do you think? This makes me think that we may need to add a filter so that 3rd party developers can add their own store pages to the list of store pages.

@adrianduffell
Copy link
Contributor

What about a page with a WooCommerce block, such as the All Product block. Would that count as a store page?

It's a good point. I think we could tackle this after the MVP. For now, the "restrict to store pages only" setting will link to external documentation that lists the pages which are included. We could add a note in the documentation about blocks not being supported yet.

We would need to come up with a strategy to detect pages with a WooCommerce block without a performance hit on every front-end request. Perhaps we could build an index of block pages each time a page gets saved. The filter also sounds like a good idea 👍

Copy link
Contributor

@rjchow rjchow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some comments on possible performance implications, not too sure if they are impactful

* @param string $url URL to normalize.
*/
private static function get_normalized_url_path( $url ) {
$quey = wp_parse_url( $url, PHP_URL_QUERY );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$quey = wp_parse_url( $url, PHP_URL_QUERY );
$query = wp_parse_url( $url, PHP_URL_QUERY );

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d84d9f2

$normalized_path = self::get_normalized_url_path( $url );

// WC store pages.
$store_pages = array(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can add a filter here for extensibility, what do you think?

Copy link
Member Author

@chihsuan chihsuan Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Added a filter here 095c963 🙂

}

// Check product, category and tag pages.
$permalink_structure = wc_get_permalink_structure();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure how big of an impact it is on performance but there's a get_option() call in wc_get_permalink_structure()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I think it's a minor impact. If it becomes a problem, we can optimize it later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From ChatGPT:

The impact of calling wc_get_permalink_structure() on performance depends on various factors such as server configuration, database size, and the context in which the function is being called.

  1. Caching: If the result of wc_get_permalink_structure() is cached, subsequent calls to the function within the same request will have minimal impact on performance. WordPress itself caches the permalink structure, so in most cases, calling this function won't cause a significant performance hit.

  2. Database: The function involves querying the database to retrieve the permalink structure. The impact may be noticeable on large or busy websites with heavy database loads. However, for most websites, especially those with caching mechanisms in place, the impact should be minimal.

  3. Context: If the function is called frequently or in a critical performance path, such as within a loop that executes many times, the cumulative impact may become more significant. In such cases, it's advisable to call the function sparingly or optimize the code structure to minimize redundant calls.

  4. Server Resources: The impact also depends on the server resources available. On high-traffic websites or under heavy server load, the impact may be more noticeable compared to low-traffic websites or under light server load.

Overall, calling wc_get_permalink_structure() typically has a minor impact on performance in most WordPress environments, especially when cached properly. However, it's essential to consider the specific context and optimize code accordingly if performance issues arise.

'shop' => wc_get_page_id( 'shop' ),
'cart' => wc_get_page_id( 'cart' ),
'checkout' => wc_get_page_id( 'checkout' ),
'privacy' => wc_privacy_policy_page_id(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure how big of an impact it is on performance but there's a get_option() call in wc_privacy_policy_page_id()

Copy link
Contributor

@psealock psealock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is testing well, nice work @chihsuan you've covered a lot of scenarios.

I couldn't spot any issues with the code (other than @rjchow's comments) 🚀

@chihsuan chihsuan requested a review from rjchow March 18, 2024 06:19
Copy link
Contributor

@rjchow rjchow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the feedback!

@chihsuan chihsuan merged commit 63a0d5e into trunk Mar 18, 2024
33 checks passed
@chihsuan chihsuan deleted the add/is-store-page branch March 18, 2024 08:29
@github-actions github-actions bot added this to the 8.8.0 milestone Mar 18, 2024
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Mar 18, 2024
@alvarothomas alvarothomas added needs: external testing Indicates if the PR requires further testing conducted by testers external to the development team. and removed needs: analysis Indicates if the PR requires a PR testing scrub session. labels Mar 19, 2024
@alvarothomas alvarothomas added the status: analysis complete Indicates if a PR has been analysed by Solaris label Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: external testing Indicates if the PR requires further testing conducted by testers external to the development team. plugin: woocommerce Issues related to the WooCommerce Core plugin. status: analysis complete Indicates if a PR has been analysed by Solaris
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Launch Your Store] Add generic function to determine if URL is a store page
5 participants