Skip to content

Commit

Permalink
Add locale independent wc_get_permalink_structure function
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Feb 7, 2017
1 parent b08449e commit 09f2c6b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 53 deletions.
68 changes: 37 additions & 31 deletions includes/admin/class-wc-admin-permalink-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
*/
class WC_Admin_Permalink_Settings {

/**
* Permalink settings.
*
* @var array
*/
private $permalinks = array();

/**
* Hook in tabs.
*/
Expand Down Expand Up @@ -57,35 +64,34 @@ public function settings_init() {
'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
}

/**
* 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
}

/**
* 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
}

Expand All @@ -95,9 +101,6 @@ public function product_attribute_slug_input() {
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' ) );
Expand All @@ -112,24 +115,24 @@ public function 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>
Expand Down Expand Up @@ -169,35 +172,34 @@ public function settings_save() {

// 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' );
Expand All @@ -208,6 +210,10 @@ public function settings_save() {
}

update_option( 'woocommerce_permalinks', $permalinks );

if ( function_exists( 'restore_current_locale' ) ) {
restore_current_locale();
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-breadcrumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function generate() {
* 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 );

Expand Down
22 changes: 8 additions & 14 deletions includes/class-wc-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function register_taxonomies() {

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' ) ),
Expand Down Expand Up @@ -101,8 +101,8 @@ public static function register_taxonomies() {
'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,
),
Expand Down Expand Up @@ -140,7 +140,7 @@ public static function register_taxonomies() {
'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,
),
) )
Expand Down Expand Up @@ -225,7 +225,7 @@ public static function register_taxonomies() {

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,
);
Expand All @@ -243,19 +243,13 @@ public static function register_taxonomies() {
* 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',
Expand Down Expand Up @@ -293,7 +287,7 @@ public static function register_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',
Expand Down
44 changes: 37 additions & 7 deletions includes/wc-core-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,11 +809,10 @@ function flush_rewrite_rules_on_shop_page_save( $post_id ) {
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 ] );
Expand All @@ -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 );

Expand Down Expand Up @@ -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 );
}
}
Expand Down Expand Up @@ -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;
}

0 comments on commit 09f2c6b

Please sign in to comment.