class-wc-admin-permalink-settings.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Adds settings to the permalinks admin settings page
  4. *
  5. * @class WC_Admin_Permalink_Settings
  6. * @author WooThemes
  7. * @category Admin
  8. * @package WooCommerce/Admin
  9. * @version 2.3.0
  10. */
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. }
  14. if ( class_exists( 'WC_Admin_Permalink_Settings', false ) ) {
  15. return new WC_Admin_Permalink_Settings();
  16. }
  17. /**
  18. * WC_Admin_Permalink_Settings Class.
  19. */
  20. class WC_Admin_Permalink_Settings {
  21. /**
  22. * Permalink settings.
  23. *
  24. * @var array
  25. */
  26. private $permalinks = array();
  27. /**
  28. * Hook in tabs.
  29. */
  30. public function __construct() {
  31. $this->settings_init();
  32. $this->settings_save();
  33. }
  34. /**
  35. * Init our settings.
  36. */
  37. public function settings_init() {
  38. add_settings_section( 'woocommerce-permalink', __( 'Product permalinks', 'woocommerce' ), array( $this, 'settings' ), 'permalink' );
  39. add_settings_field(
  40. 'woocommerce_product_category_slug',
  41. __( 'Product category base', 'woocommerce' ),
  42. array( $this, 'product_category_slug_input' ),
  43. 'permalink',
  44. 'optional'
  45. );
  46. add_settings_field(
  47. 'woocommerce_product_tag_slug',
  48. __( 'Product tag base', 'woocommerce' ),
  49. array( $this, 'product_tag_slug_input' ),
  50. 'permalink',
  51. 'optional'
  52. );
  53. add_settings_field(
  54. 'woocommerce_product_attribute_slug',
  55. __( 'Product attribute base', 'woocommerce' ),
  56. array( $this, 'product_attribute_slug_input' ),
  57. 'permalink',
  58. 'optional'
  59. );
  60. $this->permalinks = wc_get_permalink_structure();
  61. }
  62. /**
  63. * Show a slug input box.
  64. */
  65. public function product_category_slug_input() {
  66. ?>
  67. <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' ); ?>" />
  68. <?php
  69. }
  70. /**
  71. * Show a slug input box.
  72. */
  73. public function product_tag_slug_input() {
  74. ?>
  75. <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' ); ?>" />
  76. <?php
  77. }
  78. /**
  79. * Show a slug input box.
  80. */
  81. public function product_attribute_slug_input() {
  82. ?>
  83. <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>
  84. <?php
  85. }
  86. /**
  87. * Show the settings.
  88. */
  89. public function settings() {
  90. /* translators: %s: Home URL */
  91. 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( '/' ) ) ) ) );
  92. $shop_page_id = wc_get_page_id( 'shop' );
  93. $base_slug = urldecode( ( $shop_page_id > 0 && get_post( $shop_page_id ) ) ? get_page_uri( $shop_page_id ) : _x( 'shop', 'default-slug', 'woocommerce' ) );
  94. $product_base = _x( 'product', 'default-slug', 'woocommerce' );
  95. $structures = array(
  96. 0 => '',
  97. 1 => '/' . trailingslashit( $base_slug ),
  98. 2 => '/' . trailingslashit( $base_slug ) . trailingslashit( '%product_cat%' ),
  99. );
  100. ?>
  101. <table class="form-table wc-permalink-structure">
  102. <tbody>
  103. <tr>
  104. <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>
  105. <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>
  106. </tr>
  107. <?php if ( $shop_page_id ) : ?>
  108. <tr>
  109. <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>
  110. <td><code><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $base_slug ); ?>/sample-product/</code></td>
  111. </tr>
  112. <tr>
  113. <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>
  114. <td><code><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $base_slug ); ?>/product-category/sample-product/</code></td>
  115. </tr>
  116. <?php endif; ?>
  117. <tr>
  118. <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 ); ?> />
  119. <?php esc_html_e( 'Custom base', 'woocommerce' ); ?></label></th>
  120. <td>
  121. <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>
  122. </td>
  123. </tr>
  124. </tbody>
  125. </table>
  126. <?php wp_nonce_field( 'wc-permalinks', 'wc-permalinks-nonce' ); ?>
  127. <script type="text/javascript">
  128. jQuery( function() {
  129. jQuery('input.wctog').change(function() {
  130. jQuery('#woocommerce_permalink_structure').val( jQuery( this ).val() );
  131. });
  132. jQuery('.permalink-structure input').change(function() {
  133. jQuery('.wc-permalink-structure').find('code.non-default-example, code.default-example').hide();
  134. if ( jQuery(this).val() ) {
  135. jQuery('.wc-permalink-structure code.non-default-example').show();
  136. jQuery('.wc-permalink-structure input').removeAttr('disabled');
  137. } else {
  138. jQuery('.wc-permalink-structure code.default-example').show();
  139. jQuery('.wc-permalink-structure input:eq(0)').click();
  140. jQuery('.wc-permalink-structure input').attr('disabled', 'disabled');
  141. }
  142. });
  143. jQuery('.permalink-structure input:checked').change();
  144. jQuery('#woocommerce_permalink_structure').focus( function(){
  145. jQuery('#woocommerce_custom_selection').click();
  146. } );
  147. } );
  148. </script>
  149. <?php
  150. }
  151. /**
  152. * Save the settings.
  153. */
  154. public function settings_save() {
  155. if ( ! is_admin() ) {
  156. return;
  157. }
  158. // We need to save the options ourselves; settings api does not trigger save for the permalinks page.
  159. 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.
  160. wc_switch_to_site_locale();
  161. $permalinks = (array) get_option( 'woocommerce_permalinks', array() );
  162. $permalinks['category_base'] = wc_sanitize_permalink( wp_unslash( $_POST['woocommerce_product_category_slug'] ) ); // WPCS: input var ok, sanitization ok.
  163. $permalinks['tag_base'] = wc_sanitize_permalink( wp_unslash( $_POST['woocommerce_product_tag_slug'] ) ); // WPCS: input var ok, sanitization ok.
  164. $permalinks['attribute_base'] = wc_sanitize_permalink( wp_unslash( $_POST['woocommerce_product_attribute_slug'] ) ); // WPCS: input var ok, sanitization ok.
  165. // Generate product base.
  166. $product_base = isset( $_POST['product_permalink'] ) ? wc_clean( wp_unslash( $_POST['product_permalink'] ) ) : ''; // WPCS: input var ok, sanitization ok.
  167. if ( 'custom' === $product_base ) {
  168. if ( isset( $_POST['product_permalink_structure'] ) ) { // WPCS: input var ok.
  169. $product_base = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', trim( wp_unslash( $_POST['product_permalink_structure'] ) ) ) ); // WPCS: input var ok, sanitization ok.
  170. } else {
  171. $product_base = '/';
  172. }
  173. // This is an invalid base structure and breaks pages.
  174. if ( '/%product_cat%/' === trailingslashit( $product_base ) ) {
  175. $product_base = '/' . _x( 'product', 'slug', 'woocommerce' ) . $product_base;
  176. }
  177. } elseif ( empty( $product_base ) ) {
  178. $product_base = _x( 'product', 'slug', 'woocommerce' );
  179. }
  180. $permalinks['product_base'] = wc_sanitize_permalink( $product_base );
  181. // Shop base may require verbose page rules if nesting pages.
  182. $shop_page_id = wc_get_page_id( 'shop' );
  183. $shop_permalink = ( $shop_page_id > 0 && get_post( $shop_page_id ) ) ? get_page_uri( $shop_page_id ) : _x( 'shop', 'default-slug', 'woocommerce' );
  184. if ( $shop_page_id && stristr( trim( $permalinks['product_base'], '/' ), $shop_permalink ) ) {
  185. $permalinks['use_verbose_page_rules'] = true;
  186. }
  187. update_option( 'woocommerce_permalinks', $permalinks );
  188. wc_restore_locale();
  189. }
  190. }
  191. }
  192. return new WC_Admin_Permalink_Settings();