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