From 09f2c6b89a5366e83d66f3d392fd514eda0e86b0 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 7 Feb 2017 16:16:45 +0000 Subject: [PATCH] Add locale independent wc_get_permalink_structure function --- .../class-wc-admin-permalink-settings.php | 68 ++++++++++--------- includes/class-wc-breadcrumb.php | 2 +- includes/class-wc-post-types.php | 22 +++--- includes/wc-core-functions.php | 44 ++++++++++-- 4 files changed, 83 insertions(+), 53 deletions(-) diff --git a/includes/admin/class-wc-admin-permalink-settings.php b/includes/admin/class-wc-admin-permalink-settings.php index 61a6e7287b1..8b2a13566ec 100644 --- a/includes/admin/class-wc-admin-permalink-settings.php +++ b/includes/admin/class-wc-admin-permalink-settings.php @@ -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' ); ?> - + - + - /attribute-name/attribute/ + /attribute-name/attribute/ 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 { - + - + - + - @@ -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(); + } } } } diff --git a/includes/class-wc-breadcrumb.php b/includes/class-wc-breadcrumb.php index 46ee84c8b5c..3894502a555 100644 --- a/includes/class-wc-breadcrumb.php +++ b/includes/class-wc-breadcrumb.php @@ -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 ); diff --git a/includes/class-wc-post-types.php b/includes/class-wc-post-types.php index ebb8e256fcd..f951954d956 100644 --- a/includes/class-wc-post-types.php +++ b/includes/class-wc-post-types.php @@ -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', diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index 37af4ba8153..8f048366a4f 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -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; +}