説明なし

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

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