PluginProbe
Commerce7 for WordPress / trunk
Commerce7 for WordPress vtrunk
1.8.1 1.8.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.3.4 1.3.5 1.4.0 1.4.1 All 37 releases
wp-commerce7 / includes / wordpress / load.php

load.php in Commerce7 for WordPress trunk, at includes/wordpress/load.php

156 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WordPress Canonical Support
4 *
5 * Created Date: Wednesday October 12th 2022
6 * Author: Michael Bourne
7 * -----
8 * Last Modified: Friday, July 3rd 2026, 11:39:27 am
9 * Modified By: Michael Bourne
10 * -----
11 * Copyright (c) 2022 URSA6
12 *
13 * @package wp-commerce7
14 * @author Michael Bourne
15 * @license GPL3
16 * @link https://ursa6.com
17 * @since 1.3.3
18 */
19
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit;
22 }
23
24 /**
25 * Get Commerce7 front-end route slugs.
26 *
27 * @return string[]
28 */
29 function c7wp_get_frontend_route_slugs() {
30 $options = get_option( 'c7wp_settings' );
31 if ( ! isset( $options['c7wp_frontend_routes'] ) || ! is_array( $options['c7wp_frontend_routes'] ) ) {
32 return array( 'profile', 'collection', 'product', 'club', 'checkout', 'cart', 'reservation' );
33 }
34
35 return array_values( $options['c7wp_frontend_routes'] );
36 }
37
38 /**
39 * Whether the current request targets a Commerce7 front-end route.
40 *
41 * Commerce7 uses ?page=N for collection pagination. WordPress reserves `page`
42 * for multi-page content, which triggers redirect_canonical and strips sub-routes.
43 *
44 * @param string $requested_url Optional URL passed to redirect_canonical.
45 * @return bool
46 */
47 function c7wp_is_frontend_route_request( $requested_url = '' ) {
48 $routes = c7wp_get_frontend_route_slugs();
49
50 if ( get_query_var( 'c7slug' ) ) {
51 return true;
52 }
53
54 $pagename = get_query_var( 'pagename' );
55 if ( $pagename && in_array( $pagename, $routes, true ) ) {
56 return true;
57 }
58
59 if ( $requested_url ) {
60 $parsed_url = wp_parse_url( $requested_url );
61 $path = trim( (string) ( $parsed_url['path'] ?? '' ), '/' );
62 if ( '' !== $path ) {
63 $first_segment = strtok( $path, '/' );
64 if ( $first_segment && in_array( $first_segment, $routes, true ) ) {
65 return true;
66 }
67 }
68 }
69
70 return false;
71 }
72
73 /**
74 * Whether the request includes Commerce7 pagination via ?page=N.
75 *
76 * @return bool
77 */
78 function c7wp_has_commerce7_pagination_query() {
79 return isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) && (int) $_GET['page'] > 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
80 }
81
82 /**
83 * Prevent WordPress from treating Commerce7 ?page=N as WP page pagination.
84 *
85 * @param array<string, mixed> $query_vars Parsed query variables.
86 * @return array<string, mixed>
87 */
88 add_filter(
89 'request',
90 function ( $query_vars ) {
91 if ( ! c7wp_has_commerce7_pagination_query() ) {
92 return $query_vars;
93 }
94
95 $routes = c7wp_get_frontend_route_slugs();
96 $is_c7_route = false;
97
98 if ( ! empty( $query_vars['c7slug'] ) ) {
99 $is_c7_route = true;
100 } elseif ( ! empty( $query_vars['pagename'] ) && in_array( $query_vars['pagename'], $routes, true ) ) {
101 $is_c7_route = true;
102 }
103
104 if ( $is_c7_route && isset( $query_vars['page'] ) ) {
105 unset( $query_vars['page'] );
106 }
107
108 return $query_vars;
109 }
110 );
111
112 /**
113 * Disable canonical redirects on Commerce7 routes that use ?page=N pagination.
114 *
115 * @param string|false $redirect_url Canonical redirect URL.
116 * @param string $requested_url Requested URL.
117 * @return string|false
118 */
119 add_filter(
120 'redirect_canonical',
121 function ( $redirect_url, $requested_url ) {
122 if ( ! c7wp_has_commerce7_pagination_query() ) {
123 return $redirect_url;
124 }
125
126 if ( c7wp_is_frontend_route_request( $requested_url ) ) {
127 return false;
128 }
129
130 return $redirect_url;
131 },
132 10,
133 2
134 );
135
136 add_filter(
137 'get_canonical_url',
138 function ( $canonical_url ) {
139
140 $options = get_option( 'c7wp_settings' );
141 if ( ! isset( $options['c7wp_frontend_routes'] ) || ! is_array( $options['c7wp_frontend_routes'] ) ) {
142 $product_route = 'product';
143 $collection_route = 'collection';
144 } else {
145 $product_route = $options['c7wp_frontend_routes']['product'];
146 $collection_route = $options['c7wp_frontend_routes']['collection'];
147 }
148
149 if ( is_page( array( $product_route, $collection_route ) ) ) {
150 return '';
151 }
152
153 return $canonical_url;
154 }
155 );
156