When no permalinks are set, set the defaults and avoid switching locales

This commit is contained in:
Mike Jolley 2017-10-24 16:29:04 +01:00
parent 3d18fa19d2
commit e68f087659
2 changed files with 24 additions and 24 deletions

View File

@ -194,7 +194,7 @@ class WC_Admin_Permalink_Settings {
$product_base = '/' . _x( 'product', 'slug', 'woocommerce' ) . $product_base; $product_base = '/' . _x( 'product', 'slug', 'woocommerce' ) . $product_base;
} }
} elseif ( empty( $product_base ) ) { } elseif ( empty( $product_base ) ) {
$product_base = false; $product_base = _x( 'product', 'slug', 'woocommerce' );
} }
$permalinks['product_base'] = wc_sanitize_permalink( $product_base ); $permalinks['product_base'] = wc_sanitize_permalink( $product_base );

View File

@ -886,12 +886,9 @@ add_filter( 'rewrite_rules_array', 'wc_fix_rewrite_rules' );
* @return string * @return string
*/ */
function wc_fix_product_attachment_link( $link, $post_id ) { function wc_fix_product_attachment_link( $link, $post_id ) {
$post = get_post( $post_id ); $parent_type = get_post_type( wp_get_post_parent_id( $post_id ) );
if ( 'product' === get_post_type( $post->post_parent ) ) { if ( 'product' === $parent_type || 'product_variation' === $parent_type ) {
$permalinks = wc_get_permalink_structure(); $link = home_url( '/?attachment_id=' . $post_id );
if ( preg_match( '/\/(.+)(\/%product_cat%)$/', $permalinks['product_rewrite_slug'], $matches ) ) {
$link = home_url( '/?attachment_id=' . $post->ID );
}
} }
return $link; return $link;
} }
@ -1674,33 +1671,36 @@ function wc_list_pluck( $list, $callback_or_field, $index_key = null ) {
} }
/** /**
* Get permalink settings for WooCommerce independent of the user locale. * Get permalink settings for things like products and taxonomies.
*
* As of 3.3.0, the permalink settings are stored to the option instead of
* being blank and inheritting from the locale. This speeds up page loading
* times by negating the need to switch locales on each page load.
*
* This is more inline with WP core behavior which does not localize slugs.
* *
* @since 3.0.0 * @since 3.0.0
* @return array * @return array
*/ */
function wc_get_permalink_structure() { function wc_get_permalink_structure() {
if ( did_action( 'admin_init' ) ) { $saved_permalinks = (array) get_option( 'woocommerce_permalinks', array() );
wc_switch_to_site_locale(); $permalinks = wp_parse_args( $saved_permalinks, array(
} 'product_base' => _x( 'product', 'slug', 'woocommerce' ),
'category_base' => _x( 'product-category', 'slug', 'woocommerce' ),
$permalinks = wp_parse_args( (array) get_option( 'woocommerce_permalinks', array() ), array( 'tag_base' => _x( 'product-tag', 'slug', 'woocommerce' ),
'product_base' => '',
'category_base' => '',
'tag_base' => '',
'attribute_base' => '', 'attribute_base' => '',
'use_verbose_page_rules' => false, 'use_verbose_page_rules' => false,
) ); ) );
// Ensure rewrite slugs are set. if ( $saved_permalinks !== $permalinks ) {
$permalinks['product_rewrite_slug'] = untrailingslashit( empty( $permalinks['product_base'] ) ? _x( 'product', 'slug', 'woocommerce' ) : $permalinks['product_base'] ); update_option( 'woocommerce_permalinks', $permalinks );
$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 ( did_action( 'admin_init' ) ) {
wc_restore_locale();
} }
$permalinks['product_rewrite_slug'] = untrailingslashit( $permalinks['product_base'] );
$permalinks['category_rewrite_slug'] = untrailingslashit( $permalinks['category_base'] );
$permalinks['tag_rewrite_slug'] = untrailingslashit( $permalinks['tag_base'] );
$permalinks['attribute_rewrite_slug'] = untrailingslashit( $permalinks['attribute_base'] );
return $permalinks; return $permalinks;
} }