2013-07-24 16:01:36 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* Adds settings to the permalinks admin settings page
2013-07-24 16:01:36 +00:00
*
2015-02-04 16:27:06 +00:00
* @ class WC_Admin_Permalink_Settings
2014-11-30 06:52:32 +00:00
* @ author WooThemes
* @ category Admin
2020-08-05 16:36:24 +00:00
* @ package WooCommerce\Admin
2015-02-04 16:27:06 +00:00
* @ version 2.3 . 0
2013-07-24 16:01:36 +00:00
*/
2014-09-20 19:52:30 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2015-02-04 16:27:06 +00:00
exit ;
2014-09-20 19:52:30 +00:00
}
2013-07-24 16:01:36 +00:00
2017-12-05 14:02:40 +00:00
if ( class_exists ( 'WC_Admin_Permalink_Settings' , false ) ) {
return new WC_Admin_Permalink_Settings ();
}
2013-07-24 16:01:36 +00:00
/**
2015-11-03 12:28:01 +00:00
* WC_Admin_Permalink_Settings Class .
2013-07-24 16:01:36 +00:00
*/
class WC_Admin_Permalink_Settings {
2017-02-07 16:16:45 +00:00
/**
* Permalink settings .
*
* @ var array
*/
private $permalinks = array ();
2013-07-24 16:01:36 +00:00
/**
* Hook in tabs .
*/
public function __construct () {
2014-06-04 10:16:19 +00:00
$this -> settings_init ();
$this -> settings_save ();
2013-07-24 16:01:36 +00:00
}
/**
2015-02-04 16:27:06 +00:00
* Init our settings .
2013-07-24 16:01:36 +00:00
*/
public function settings_init () {
2016-10-12 10:16:30 +00:00
add_settings_section ( 'woocommerce-permalink' , __ ( 'Product permalinks' , 'woocommerce' ), array ( $this , 'settings' ), 'permalink' );
2013-07-24 16:01:36 +00:00
add_settings_field (
2017-12-05 14:02:40 +00:00
'woocommerce_product_category_slug' ,
__ ( 'Product category base' , 'woocommerce' ),
array ( $this , 'product_category_slug_input' ),
'permalink' ,
'optional'
2013-07-24 16:01:36 +00:00
);
add_settings_field (
2017-12-05 14:02:40 +00:00
'woocommerce_product_tag_slug' ,
__ ( 'Product tag base' , 'woocommerce' ),
array ( $this , 'product_tag_slug_input' ),
'permalink' ,
'optional'
2013-07-24 16:01:36 +00:00
);
add_settings_field (
2017-12-05 14:02:40 +00:00
'woocommerce_product_attribute_slug' ,
__ ( 'Product attribute base' , 'woocommerce' ),
array ( $this , 'product_attribute_slug_input' ),
'permalink' ,
'optional'
2013-07-24 16:01:36 +00:00
);
2017-02-07 16:16:45 +00:00
$this -> permalinks = wc_get_permalink_structure ();
2013-07-24 16:01:36 +00:00
}
/**
* Show a slug input box .
*/
public function product_category_slug_input () {
?>
2018-03-05 18:59:17 +00:00
< 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' ); ?> " />
2013-07-24 16:01:36 +00:00
< ? php
}
/**
* Show a slug input box .
*/
public function product_tag_slug_input () {
?>
2018-03-05 18:59:17 +00:00
< 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' ); ?> " />
2013-07-24 16:01:36 +00:00
< ? php
}
/**
* Show a slug input box .
*/
public function product_attribute_slug_input () {
?>
2017-02-07 16:16:45 +00:00
< 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 >
2013-07-24 16:01:36 +00:00
< ? php
}
/**
2015-02-04 16:27:06 +00:00
* Show the settings .
2013-07-24 16:01:36 +00:00
*/
public function settings () {
2017-12-05 13:41:56 +00:00
/* translators: %s: Home URL */
2017-12-05 14:02:40 +00:00
echo wp_kses_post ( wpautop ( sprintf ( __ ( 'If you like, you may enter custom structures for your product URLs here. For example, using <code>shop</code> would make your product links like <code>%sshop/sample-product/</code>. This setting affects product URLs only, not things such as product categories.' , 'woocommerce' ), esc_url ( home_url ( '/' ) ) ) ) );
2013-07-24 16:01:36 +00:00
2018-03-05 18:59:17 +00:00
$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' ) );
$product_base = _x ( 'product' , 'default-slug' , 'woocommerce' );
2013-07-24 16:01:36 +00:00
$structures = array (
0 => '' ,
2015-10-05 11:15:35 +00:00
1 => '/' . trailingslashit ( $base_slug ),
2016-08-27 01:46:45 +00:00
2 => '/' . trailingslashit ( $base_slug ) . trailingslashit ( '%product_cat%' ),
2013-07-24 16:01:36 +00:00
);
?>
2015-10-05 11:15:35 +00:00
< table class = " form-table wc-permalink-structure " >
2013-07-24 16:01:36 +00:00
< tbody >
< tr >
2017-12-05 14:02:40 +00:00
< 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 esc_html_e( 'Default', 'woocommerce' ); ?></label></th>
2015-10-05 11:15:35 +00:00
< 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>
2013-07-24 16:01:36 +00:00
</ tr >
< ? php if ( $shop_page_id ) : ?>
< tr >
2017-12-05 14:02:40 +00:00
< 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 esc_html_e( 'Shop base', 'woocommerce' ); ?></label></th>
2015-05-19 12:54:35 +00:00
< td >< code >< ? php echo esc_html ( home_url () ); ?> /<?php echo esc_html( $base_slug ); ?>/sample-product/</code></td>
2013-07-24 16:01:36 +00:00
</ tr >
< tr >
2017-12-05 14:02:40 +00:00
< 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 esc_html_e( 'Shop base with category', 'woocommerce' ); ?></label></th>
2015-05-19 12:54:35 +00:00
< td >< code >< ? php echo esc_html ( home_url () ); ?> /<?php echo esc_html( $base_slug ); ?>/product-category/sample-product/</code></td>
2013-07-24 16:01:36 +00:00
</ tr >
< ? php endif ; ?>
< tr >
2017-12-05 14:02:40 +00:00
< 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 , true ), false ); ?> />
< ? php esc_html_e ( 'Custom base' , 'woocommerce' ); ?> </label></th>
2013-07-24 16:01:36 +00:00
< td >
2017-12-05 14:02:40 +00:00
< 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 esc_html_e ( 'Enter a custom base to use. A base must be set or WordPress will use default instead.' , 'woocommerce' ); ?> </span>
2013-07-24 16:01:36 +00:00
</ td >
</ tr >
</ tbody >
</ table >
2017-12-05 14:02:40 +00:00
< ? php wp_nonce_field ( 'wc-permalinks' , 'wc-permalinks-nonce' ); ?>
2013-07-24 16:01:36 +00:00
< script type = " text/javascript " >
2014-11-30 06:52:32 +00:00
jQuery ( function () {
2021-02-04 20:31:28 +00:00
jQuery ( 'input.wctog' ) . on ( 'change' , function () {
2014-11-30 06:52:32 +00:00
jQuery ( '#woocommerce_permalink_structure' ) . val ( jQuery ( this ) . val () );
2013-07-24 16:01:36 +00:00
});
2021-02-04 20:31:28 +00:00
jQuery ( '.permalink-structure input' ) . on ( 'change' , function () {
2015-10-05 11:15:35 +00:00
jQuery ( '.wc-permalink-structure' ) . find ( 'code.non-default-example, code.default-example' ) . hide ();
if ( jQuery ( this ) . val () ) {
jQuery ( '.wc-permalink-structure code.non-default-example' ) . show ();
2021-02-04 20:31:28 +00:00
jQuery ( '.wc-permalink-structure input' ) . prop ( 'disabled' , false );
2015-10-05 11:15:35 +00:00
} else {
jQuery ( '.wc-permalink-structure code.default-example' ) . show ();
2021-02-04 20:31:28 +00:00
jQuery ( '.wc-permalink-structure input:eq(0)' ) . trigger ( 'click' );
2015-10-05 11:15:35 +00:00
jQuery ( '.wc-permalink-structure input' ) . attr ( 'disabled' , 'disabled' );
}
});
2021-02-04 20:31:28 +00:00
jQuery ( '.permalink-structure input:checked' ) . trigger ( 'change' );
jQuery ( '#woocommerce_permalink_structure' ) . on ( 'focus' , function (){
jQuery ( '#woocommerce_custom_selection' ) . trigger ( 'click' );
2014-11-30 06:52:32 +00:00
} );
} );
2013-07-24 16:01:36 +00:00
</ script >
< ? php
}
/**
2015-02-04 16:27:06 +00:00
* Save the settings .
2013-07-24 16:01:36 +00:00
*/
public function settings_save () {
2014-06-04 06:46:21 +00:00
if ( ! is_admin () ) {
2013-07-24 16:01:36 +00:00
return ;
2014-06-04 06:46:21 +00:00
}
2013-07-24 16:01:36 +00:00
2016-05-24 23:15:15 +00:00
// We need to save the options ourselves; settings api does not trigger save for the permalinks page.
2017-12-05 14:02:40 +00:00
if ( isset ( $_POST [ 'permalink_structure' ], $_POST [ 'wc-permalinks-nonce' ], $_POST [ 'woocommerce_product_category_slug' ], $_POST [ 'woocommerce_product_tag_slug' ], $_POST [ 'woocommerce_product_attribute_slug' ] ) && wp_verify_nonce ( wp_unslash ( $_POST [ 'wc-permalinks-nonce' ] ), 'wc-permalinks' ) ) { // WPCS: input var ok, sanitization ok.
2017-06-05 13:18:39 +00:00
wc_switch_to_site_locale ();
2013-07-24 16:01:36 +00:00
2017-02-07 16:16:45 +00:00
$permalinks = ( array ) get_option ( 'woocommerce_permalinks' , array () );
2017-12-05 14:02:40 +00:00
$permalinks [ 'category_base' ] = wc_sanitize_permalink ( wp_unslash ( $_POST [ 'woocommerce_product_category_slug' ] ) ); // WPCS: input var ok, sanitization ok.
$permalinks [ 'tag_base' ] = wc_sanitize_permalink ( wp_unslash ( $_POST [ 'woocommerce_product_tag_slug' ] ) ); // WPCS: input var ok, sanitization ok.
$permalinks [ 'attribute_base' ] = wc_sanitize_permalink ( wp_unslash ( $_POST [ 'woocommerce_product_attribute_slug' ] ) ); // WPCS: input var ok, sanitization ok.
2013-07-24 16:01:36 +00:00
2017-02-07 16:16:45 +00:00
// Generate product base.
2017-12-05 14:02:40 +00:00
$product_base = isset ( $_POST [ 'product_permalink' ] ) ? wc_clean ( wp_unslash ( $_POST [ 'product_permalink' ] ) ) : '' ; // WPCS: input var ok, sanitization ok.
2013-07-24 16:01:36 +00:00
2017-02-07 16:16:45 +00:00
if ( 'custom' === $product_base ) {
2017-12-05 14:02:40 +00:00
if ( isset ( $_POST [ 'product_permalink_structure' ] ) ) { // WPCS: input var ok.
$product_base = preg_replace ( '#/+#' , '/' , '/' . str_replace ( '#' , '' , trim ( wp_unslash ( $_POST [ 'product_permalink_structure' ] ) ) ) ); // WPCS: input var ok, sanitization ok.
2016-06-08 10:30:02 +00:00
} else {
2017-02-07 16:16:45 +00:00
$product_base = '/' ;
2016-06-08 10:30:02 +00:00
}
2014-02-14 10:43:15 +00:00
2016-05-24 23:15:15 +00:00
// This is an invalid base structure and breaks pages.
2017-02-28 00:16:23 +00:00
if ( '/%product_cat%/' === trailingslashit ( $product_base ) ) {
2017-02-07 16:16:45 +00:00
$product_base = '/' . _x ( 'product' , 'slug' , 'woocommerce' ) . $product_base ;
2014-02-14 10:43:15 +00:00
}
2017-02-07 16:16:45 +00:00
} elseif ( empty ( $product_base ) ) {
2017-10-24 15:29:04 +00:00
$product_base = _x ( 'product' , 'slug' , 'woocommerce' );
2013-07-24 16:01:36 +00:00
}
2017-02-07 16:16:45 +00:00
$permalinks [ 'product_base' ] = wc_sanitize_permalink ( $product_base );
2013-07-24 16:01:36 +00:00
2016-05-24 23:15:15 +00:00
// Shop base may require verbose page rules if nesting pages.
2014-06-04 06:46:21 +00:00
$shop_page_id = wc_get_page_id ( 'shop' );
2014-10-06 09:55:45 +00:00
$shop_permalink = ( $shop_page_id > 0 && get_post ( $shop_page_id ) ) ? get_page_uri ( $shop_page_id ) : _x ( 'shop' , 'default-slug' , 'woocommerce' );
2014-11-30 06:52:32 +00:00
2018-02-09 15:25:33 +00:00
if ( $shop_page_id && stristr ( trim ( $permalinks [ 'product_base' ], '/' ), $shop_permalink ) ) {
2014-06-04 06:46:21 +00:00
$permalinks [ 'use_verbose_page_rules' ] = true ;
}
2013-07-24 16:01:36 +00:00
update_option ( 'woocommerce_permalinks' , $permalinks );
2017-06-05 13:18:39 +00:00
wc_restore_locale ();
2013-07-24 16:01:36 +00:00
}
}
}
2014-09-20 19:52:30 +00:00
return new WC_Admin_Permalink_Settings ();