Merge pull request #13107 from woocommerce/fix-12962

Add locale independent wc_get_permalink_structure function
This commit is contained in:
Mike Jolley 2017-02-08 23:33:56 +00:00 committed by GitHub
commit a7b2a0ac7c
4 changed files with 83 additions and 53 deletions

View File

@ -20,6 +20,13 @@ if ( ! class_exists( 'WC_Admin_Permalink_Settings' ) ) :
*/
class WC_Admin_Permalink_Settings {
/**
* Permalink settings.
*
* @var array
*/
private $permalinks = array();
/**
* Hook in tabs.
*/
@ -57,15 +64,16 @@ class WC_Admin_Permalink_Settings {
'permalink', // settings page
'optional' // settings section
);
$this->permalinks = wc_get_permalink_structure();
}
/**
* Show a slug input box.
*/
public function product_category_slug_input() {
$permalinks = get_option( 'woocommerce_permalinks' );
?>
<input name="woocommerce_product_category_slug" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['category_base'] ) ) echo esc_attr( $permalinks['category_base'] ); ?>" placeholder="<?php echo esc_attr_x( 'product-category', 'slug', 'woocommerce' ) ?>" />
<input name="woocommerce_product_category_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $this->permalinks['category_base'] ); ?>" placeholder="<?php echo esc_attr_x( 'product-category', 'slug', 'woocommerce' ) ?>" />
<?php
}
@ -73,9 +81,8 @@ class WC_Admin_Permalink_Settings {
* Show a slug input box.
*/
public function product_tag_slug_input() {
$permalinks = get_option( 'woocommerce_permalinks' );
?>
<input name="woocommerce_product_tag_slug" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['tag_base'] ) ) echo esc_attr( $permalinks['tag_base'] ); ?>" placeholder="<?php echo esc_attr_x( 'product-tag', 'slug', 'woocommerce' ) ?>" />
<input name="woocommerce_product_tag_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $this->permalinks['tag_base'] ); ?>" placeholder="<?php echo esc_attr_x( 'product-tag', 'slug', 'woocommerce' ) ?>" />
<?php
}
@ -83,9 +90,8 @@ class WC_Admin_Permalink_Settings {
* Show a slug input box.
*/
public function product_attribute_slug_input() {
$permalinks = get_option( 'woocommerce_permalinks' );
?>
<input name="woocommerce_product_attribute_slug" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['attribute_base'] ) ) echo esc_attr( $permalinks['attribute_base'] ); ?>" /><code>/attribute-name/attribute/</code>
<input name="woocommerce_product_attribute_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $this->permalinks['attribute_base'] ); ?>" /><code>/attribute-name/attribute/</code>
<?php
}
@ -95,9 +101,6 @@ class WC_Admin_Permalink_Settings {
public function settings() {
echo wpautop( __( 'These settings control the permalinks used specifically for products.', 'woocommerce' ) );
$permalinks = get_option( 'woocommerce_permalinks' );
$product_permalink = isset( $permalinks['product_base'] ) ? $permalinks['product_base'] : '';
// Get shop page
$shop_page_id = wc_get_page_id( 'shop' );
$base_slug = urldecode( ( $shop_page_id > 0 && get_post( $shop_page_id ) ) ? get_page_uri( $shop_page_id ) : _x( 'shop', 'default-slug', 'woocommerce' ) );
@ -112,24 +115,24 @@ class WC_Admin_Permalink_Settings {
<table class="form-table wc-permalink-structure">
<tbody>
<tr>
<th><label><input name="product_permalink" type="radio" value="<?php echo esc_attr( $structures[0] ); ?>" class="wctog" <?php checked( $structures[0], $product_permalink ); ?> /> <?php _e( 'Default', 'woocommerce' ); ?></label></th>
<th><label><input name="product_permalink" type="radio" value="<?php echo esc_attr( $structures[0] ); ?>" class="wctog" <?php checked( $structures[0], $this->permalinks['product_base'] ); ?> /> <?php _e( 'Default', 'woocommerce' ); ?></label></th>
<td><code class="default-example"><?php echo esc_html( home_url() ); ?>/?product=sample-product</code> <code class="non-default-example"><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $product_base ); ?>/sample-product/</code></td>
</tr>
<?php if ( $shop_page_id ) : ?>
<tr>
<th><label><input name="product_permalink" type="radio" value="<?php echo esc_attr( $structures[1] ); ?>" class="wctog" <?php checked( $structures[1], $product_permalink ); ?> /> <?php _e( 'Shop base', 'woocommerce' ); ?></label></th>
<th><label><input name="product_permalink" type="radio" value="<?php echo esc_attr( $structures[1] ); ?>" class="wctog" <?php checked( $structures[1], $this->permalinks['product_base'] ); ?> /> <?php _e( 'Shop base', 'woocommerce' ); ?></label></th>
<td><code><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $base_slug ); ?>/sample-product/</code></td>
</tr>
<tr>
<th><label><input name="product_permalink" type="radio" value="<?php echo esc_attr( $structures[2] ); ?>" class="wctog" <?php checked( $structures[2], $product_permalink ); ?> /> <?php _e( 'Shop base with category', 'woocommerce' ); ?></label></th>
<th><label><input name="product_permalink" type="radio" value="<?php echo esc_attr( $structures[2] ); ?>" class="wctog" <?php checked( $structures[2], $this->permalinks['product_base'] ); ?> /> <?php _e( 'Shop base with category', 'woocommerce' ); ?></label></th>
<td><code><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $base_slug ); ?>/product-category/sample-product/</code></td>
</tr>
<?php endif; ?>
<tr>
<th><label><input name="product_permalink" id="woocommerce_custom_selection" type="radio" value="custom" class="tog" <?php checked( in_array( $product_permalink, $structures ), false ); ?> />
<th><label><input name="product_permalink" id="woocommerce_custom_selection" type="radio" value="custom" class="tog" <?php checked( in_array( $this->permalinks['product_base'], $structures ), false ); ?> />
<?php _e( 'Custom base', 'woocommerce' ); ?></label></th>
<td>
<input name="product_permalink_structure" id="woocommerce_permalink_structure" type="text" value="<?php echo esc_attr( $product_permalink ); ?>" class="regular-text code"> <span class="description"><?php _e( 'Enter a custom base to use. A base <strong>must</strong> be set or WordPress will use default instead.', 'woocommerce' ); ?></span>
<input name="product_permalink_structure" id="woocommerce_permalink_structure" type="text" value="<?php echo esc_attr( $this->permalinks['product_base'] ? trailingslashit( $this->permalinks['product_base'] ) : '' ); ?>" class="regular-text code"> <span class="description"><?php _e( 'Enter a custom base to use. A base <strong>must</strong> be set or WordPress will use default instead.', 'woocommerce' ); ?></span>
</td>
</tr>
</tbody>
@ -169,35 +172,34 @@ class WC_Admin_Permalink_Settings {
// We need to save the options ourselves; settings api does not trigger save for the permalinks page.
if ( isset( $_POST['permalink_structure'] ) ) {
$permalinks = get_option( 'woocommerce_permalinks' );
if ( ! $permalinks ) {
$permalinks = array();
if ( function_exists( 'switch_to_locale' ) ) {
switch_to_locale( get_locale() );
}
$permalinks['category_base'] = wc_sanitize_permalink( trim( $_POST['woocommerce_product_category_slug'] ) );
$permalinks['tag_base'] = wc_sanitize_permalink( trim( $_POST['woocommerce_product_tag_slug'] ) );
$permalinks['attribute_base'] = wc_sanitize_permalink( trim( $_POST['woocommerce_product_attribute_slug'] ) );
$permalinks = (array) get_option( 'woocommerce_permalinks', array() );
$permalinks['category_base'] = wc_sanitize_permalink( trim( $_POST['woocommerce_product_category_slug'] ) );
$permalinks['tag_base'] = wc_sanitize_permalink( trim( $_POST['woocommerce_product_tag_slug'] ) );
$permalinks['attribute_base'] = wc_sanitize_permalink( trim( $_POST['woocommerce_product_attribute_slug'] ) );
// Product base.
$product_permalink = isset( $_POST['product_permalink'] ) ? wc_clean( $_POST['product_permalink'] ) : '';
// Generate product base.
$product_base = isset( $_POST['product_permalink'] ) ? wc_clean( $_POST['product_permalink'] ) : '';
if ( 'custom' === $product_permalink ) {
if ( 'custom' === $product_base ) {
if ( isset( $_POST['product_permalink_structure'] ) ) {
$product_permalink = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', trim( $_POST['product_permalink_structure'] ) ) );
$product_base = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', trim( $_POST['product_permalink_structure'] ) ) );
} else {
$product_permalink = '/';
$product_base = '/';
}
// This is an invalid base structure and breaks pages.
if ( '/%product_cat%' === $product_permalink ) {
$product_permalink = '/' . _x( 'product', 'slug', 'woocommerce' ) . $product_permalink;
if ( '/%product_cat%' === $product_base ) {
$product_base = '/' . _x( 'product', 'slug', 'woocommerce' ) . $product_base;
}
} elseif ( empty( $product_permalink ) ) {
$product_permalink = false;
} elseif ( empty( $product_base ) ) {
$product_base = false;
}
$permalinks['product_base'] = wc_sanitize_permalink( $product_permalink );
$permalinks['product_base'] = wc_sanitize_permalink( $product_base );
// Shop base may require verbose page rules if nesting pages.
$shop_page_id = wc_get_page_id( 'shop' );
@ -208,6 +210,10 @@ class WC_Admin_Permalink_Settings {
}
update_option( 'woocommerce_permalinks', $permalinks );
if ( function_exists( 'restore_current_locale' ) ) {
restore_current_locale();
}
}
}
}

View File

@ -95,7 +95,7 @@ class WC_Breadcrumb {
* Prepend the shop page to shop breadcrumbs.
*/
private function prepend_shop_page() {
$permalinks = get_option( 'woocommerce_permalinks' );
$permalinks = wc_get_permalink_structure();
$shop_page_id = wc_get_page_id( 'shop' );
$shop_page = get_post( $shop_page_id );

View File

@ -47,7 +47,7 @@ class WC_Post_types {
do_action( 'woocommerce_register_taxonomy' );
$permalinks = get_option( 'woocommerce_permalinks' );
$permalinks = wc_get_permalink_structure();
register_taxonomy( 'product_type',
apply_filters( 'woocommerce_taxonomy_objects_product_type', array( 'product' ) ),
@ -101,8 +101,8 @@ class WC_Post_types {
'delete_terms' => 'delete_product_terms',
'assign_terms' => 'assign_product_terms',
),
'rewrite' => array(
'slug' => empty( $permalinks['category_base'] ) ? _x( 'product-category', 'slug', 'woocommerce' ) : $permalinks['category_base'],
'rewrite' => array(
'slug' => $permalinks['category_rewrite_slug'],
'with_front' => false,
'hierarchical' => true,
),
@ -140,7 +140,7 @@ class WC_Post_types {
'assign_terms' => 'assign_product_terms',
),
'rewrite' => array(
'slug' => empty( $permalinks['tag_base'] ) ? _x( 'product-tag', 'slug', 'woocommerce' ) : $permalinks['tag_base'],
'slug' => $permalinks['tag_rewrite_slug'],
'with_front' => false,
),
) )
@ -225,7 +225,7 @@ class WC_Post_types {
if ( 1 === $tax->attribute_public ) {
$taxonomy_data['rewrite'] = array(
'slug' => empty( $permalinks['attribute_base'] ) ? '' : trailingslashit( $permalinks['attribute_base'] ) . sanitize_title( $tax->attribute_name ),
'slug' => trailingslashit( $permalinks['attribute_rewrite_slug'] ) . sanitize_title( $tax->attribute_name ),
'with_front' => false,
'hierarchical' => true,
);
@ -243,19 +243,13 @@ class WC_Post_types {
* Register core post types.
*/
public static function register_post_types() {
if ( ! is_blog_installed() ) {
return;
}
if ( post_type_exists( 'product' ) ) {
if ( ! is_blog_installed() || post_type_exists( 'product' ) ) {
return;
}
do_action( 'woocommerce_register_post_type' );
$permalinks = get_option( 'woocommerce_permalinks' );
$product_permalink = empty( $permalinks['product_base'] ) ? _x( 'product', 'slug', 'woocommerce' ) : $permalinks['product_base'];
$permalinks = wc_get_permalink_structure();
register_post_type( 'product',
apply_filters( 'woocommerce_register_post_type_product',
@ -293,7 +287,7 @@ class WC_Post_types {
'publicly_queryable' => true,
'exclude_from_search' => false,
'hierarchical' => false, // Hierarchical causes memory issues - WP loads all records!
'rewrite' => $product_permalink ? array( 'slug' => untrailingslashit( $product_permalink ), 'with_front' => false, 'feeds' => true ) : false,
'rewrite' => $permalinks['product_rewrite_slug'] ? array( 'slug' => $permalinks['product_rewrite_slug'], 'with_front' => false, 'feeds' => true ) : false,
'query_var' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments', 'custom-fields', 'publicize', 'wpcom-markdown' ),
'has_archive' => ( $shop_page_id = wc_get_page_id( 'shop' ) ) && get_post( $shop_page_id ) ? get_page_uri( $shop_page_id ) : 'shop',

View File

@ -809,11 +809,10 @@ add_action( 'save_post', 'flush_rewrite_rules_on_shop_page_save' );
function wc_fix_rewrite_rules( $rules ) {
global $wp_rewrite;
$permalinks = get_option( 'woocommerce_permalinks' );
$product_permalink = empty( $permalinks['product_base'] ) ? _x( 'product', 'slug', 'woocommerce' ) : $permalinks['product_base'];
$permalinks = wc_get_permalink_structure();
// Fix the rewrite rules when the product permalink have %product_cat% flag.
if ( preg_match( '`/(.+)(/%product_cat%)`' , $product_permalink, $matches ) ) {
if ( preg_match( '`/(.+)(/%product_cat%)`' , $permalinks['product_rewrite_slug'], $matches ) ) {
foreach ( $rules as $rule => $rewrite ) {
if ( preg_match( '`^' . preg_quote( $matches[1], '`' ) . '/\(`', $rule ) && preg_match( '/^(index\.php\?product_cat)(?!(.*product))/', $rewrite ) ) {
unset( $rules[ $rule ] );
@ -822,7 +821,7 @@ function wc_fix_rewrite_rules( $rules ) {
}
// If the shop page is used as the base, we need to handle shop page subpages to avoid 404s.
if ( ! empty( $permalinks['use_verbose_page_rules'] ) && ( $shop_page_id = wc_get_page_id( 'shop' ) ) ) {
if ( $permalinks['use_verbose_page_rules'] && ( $shop_page_id = wc_get_page_id( 'shop' ) ) ) {
$page_rewrite_rules = array();
$subpages = wc_get_page_children( $shop_page_id );
@ -857,9 +856,8 @@ function wc_fix_product_attachment_link( $link, $post_id ) {
$post = get_post( $post_id );
if ( 'product' === get_post_type( $post->post_parent ) ) {
$permalinks = get_option( 'woocommerce_permalinks' );
$product_permalink = empty( $permalinks['product_base'] ) ? _x( 'product', 'slug', 'woocommerce' ) : $permalinks['product_base'];
if ( preg_match( '/\/(.+)(\/%product_cat%)$/' , $product_permalink, $matches ) ) {
$permalinks = wc_get_permalink_structure();
if ( preg_match( '/\/(.+)(\/%product_cat%)$/', $permalinks['product_rewrite_slug'], $matches ) ) {
$link = home_url( '/?attachment_id=' . $post->ID );
}
}
@ -1563,3 +1561,35 @@ function wc_list_pluck( $list, $callback_or_field, $index_key = null ) {
}
return $newlist;
}
/**
* Get permalink settings for WooCommerce independent of the user locale.
*
* @since 2.7.0
* @return array
*/
function wc_get_permalink_structure() {
if ( function_exists( 'switch_to_locale' ) ) {
switch_to_locale( get_locale() );
}
$permalinks = wp_parse_args( (array) get_option( 'woocommerce_permalinks', array() ), array(
'product_base' => '',
'category_base' => '',
'tag_base' => '',
'attribute_base' => '',
'use_verbose_page_rules' => false,
) );
// Ensure rewrite slugs are set.
$permalinks['product_rewrite_slug'] = untrailingslashit( empty( $permalinks['product_base'] ) ? _x( 'product', 'slug', 'woocommerce' ) : $permalinks['product_base'] );
$permalinks['category_rewrite_slug'] = untrailingslashit( empty( $permalinks['category_base'] ) ? _x( 'product-category', 'slug', 'woocommerce' ) : $permalinks['category_base'] );
$permalinks['tag_rewrite_slug'] = untrailingslashit( empty( $permalinks['tag_base'] ) ? _x( 'product-tag', 'slug', 'woocommerce' ) : $permalinks['tag_base'] );
$permalinks['attribute_rewrite_slug'] = untrailingslashit( empty( $permalinks['attribute_base'] ) ? '' : $permalinks['attribute_base'] );
if ( function_exists( 'restore_current_locale' ) ) {
restore_current_locale();
}
return $permalinks;
}