WC_Admin_Permalink_Settings

WC_Admin_Permalink_Settings Class.

Defined (1)

The class is defined in the following location(s).

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