1 <?php
  2 /**
  3  * Adds settings to the permalinks admin settings page.
  4  *
  5  * @author      WooThemes
  6  * @category    Admin
  7  * @package     WooCommerce/Admin
  8  * @version     2.1.0
  9  */
 10 
 11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 12 
 13 if ( ! class_exists( 'WC_Admin_Permalink_Settings' ) ) :
 14 
 15 /**
 16  * WC_Admin_Permalink_Settings Class
 17  */
 18 class WC_Admin_Permalink_Settings {
 19 
 20     /**
 21      * Hook in tabs.
 22      */
 23     public function __construct() {
 24         add_action( 'admin_init', array( $this, 'settings_init' ) );
 25         add_action( 'admin_init', array( $this, 'settings_save' ) );
 26     }
 27 
 28     /**
 29      * Init our settings
 30      */
 31     public function settings_init() {
 32         // Add a section to the permalinks page
 33         add_settings_section( 'woocommerce-permalink', __( 'Product permalink base', 'woocommerce' ), array( $this, 'settings' ), 'permalink' );
 34 
 35         // Add our settings
 36         add_settings_field(
 37             'woocommerce_product_category_slug',        // id
 38             __( 'Product category base', 'woocommerce' ),   // setting title
 39             array( $this, 'product_category_slug_input' ),  // display callback
 40             'permalink',                                // settings page
 41             'optional'                                  // settings section
 42         );
 43         add_settings_field(
 44             'woocommerce_product_tag_slug',             // id
 45             __( 'Product tag base', 'woocommerce' ),    // setting title
 46             array( $this, 'product_tag_slug_input' ),  // display callback
 47             'permalink',                                // settings page
 48             'optional'                                  // settings section
 49         );
 50         add_settings_field(
 51             'woocommerce_product_attribute_slug',       // id
 52             __( 'Product attribute base', 'woocommerce' ),  // setting title
 53             array( $this, 'product_attribute_slug_input' ),  // display callback
 54             'permalink',                                // settings page
 55             'optional'                                  // settings section
 56         );
 57     }
 58 
 59     /**
 60      * Show a slug input box.
 61      */
 62     public function product_category_slug_input() {
 63         $permalinks = get_option( 'woocommerce_permalinks' );
 64         ?>
 65         <input name="woocommerce_product_category_slug" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['category_base'] ) ) echo esc_attr( $permalinks['category_base'] ); ?>" placeholder="<?php echo _x('product-category', 'slug', 'woocommerce') ?>" />
 66         <?php
 67     }
 68 
 69     /**
 70      * Show a slug input box.
 71      */
 72     public function product_tag_slug_input() {
 73         $permalinks = get_option( 'woocommerce_permalinks' );
 74         ?>
 75         <input name="woocommerce_product_tag_slug" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['tag_base'] ) ) echo esc_attr( $permalinks['tag_base'] ); ?>" placeholder="<?php echo _x('product-tag', 'slug', 'woocommerce') ?>" />
 76         <?php
 77     }
 78 
 79     /**
 80      * Show a slug input box.
 81      */
 82     public function product_attribute_slug_input() {
 83         $permalinks = get_option( 'woocommerce_permalinks' );
 84         ?>
 85         <input name="woocommerce_product_attribute_slug" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['attribute_base'] ) ) echo esc_attr( $permalinks['attribute_base'] ); ?>" /><code>/attribute-name/attribute/</code>
 86         <?php
 87     }
 88 
 89     /**
 90      * Show the settings
 91      */
 92     public function settings() {
 93         echo wpautop( __( 'These settings control the permalinks used for products. These settings only apply when <strong>not using "default" permalinks above</strong>.', 'woocommerce' ) );
 94 
 95         $permalinks = get_option( 'woocommerce_permalinks' );
 96         $product_permalink = $permalinks['product_base'];
 97 
 98         // Get shop page
 99         $shop_page_id   = wc_get_page_id( 'shop' );
100         $base_slug      = ( $shop_page_id > 0 && get_page( $shop_page_id ) ) ? get_page_uri( $shop_page_id ) : _x( 'shop', 'default-slug', 'woocommerce' );
101         $product_base   = _x( 'product', 'default-slug', 'woocommerce' );
102 
103         $structures = array(
104             0 => '',
105             1 => '/' . trailingslashit( $product_base ),
106             2 => '/' . trailingslashit( $base_slug ),
107             3 => '/' . trailingslashit( $base_slug ) . trailingslashit( '%product_cat%' )
108         );
109         ?>
110         <table class="form-table">
111             <tbody>
112                 <tr>
113                     <th><label><input name="product_permalink" type="radio" value="<?php echo $structures[0]; ?>" class="wctog" <?php checked( $structures[0], $product_permalink ); ?> /> <?php _e( 'Default', 'woocommerce' ); ?></label></th>
114                     <td><code><?php echo home_url(); ?>/?product=sample-product</code></td>
115                 </tr>
116                 <tr>
117                     <th><label><input name="product_permalink" type="radio" value="<?php echo $structures[1]; ?>" class="wctog" <?php checked( $structures[1], $product_permalink ); ?> /> <?php _e( 'Product', 'woocommerce' ); ?></label></th>
118                     <td><code><?php echo home_url(); ?>/<?php echo $product_base; ?>/sample-product/</code></td>
119                 </tr>
120                 <?php if ( $shop_page_id ) : ?>
121                     <tr>
122                         <th><label><input name="product_permalink" type="radio" value="<?php echo $structures[2]; ?>" class="wctog" <?php checked( $structures[2], $product_permalink ); ?> /> <?php _e( 'Shop base', 'woocommerce' ); ?></label></th>
123                         <td><code><?php echo home_url(); ?>/<?php echo $base_slug; ?>/sample-product/</code></td>
124                     </tr>
125                     <tr>
126                         <th><label><input name="product_permalink" type="radio" value="<?php echo $structures[3]; ?>" class="wctog" <?php checked( $structures[3], $product_permalink ); ?> /> <?php _e( 'Shop base with category', 'woocommerce' ); ?></label></th>
127                         <td><code><?php echo home_url(); ?>/<?php echo $base_slug; ?>/product-category/sample-product/</code></td>
128                     </tr>
129                 <?php endif; ?>
130                 <tr>
131                     <th><label><input name="product_permalink" id="woocommerce_custom_selection" type="radio" value="custom" class="tog" <?php checked( in_array( $product_permalink, $structures ), false ); ?> />
132                         <?php _e( 'Custom Base', 'woocommerce' ); ?></label></th>
133                     <td>
134                         <input name="product_permalink_structure" id="woocommerce_permalink_structure" type="text" value="<?php echo esc_attr( $product_permalink ); ?>" 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>
135                     </td>
136                 </tr>
137             </tbody>
138         </table>
139         <script type="text/javascript">
140             jQuery(function(){
141                 jQuery('input.wctog').change(function() {
142                     jQuery('#woocommerce_permalink_structure').val( jQuery(this).val() );
143                 });
144 
145                 jQuery('#woocommerce_permalink_structure').focus(function(){
146                     jQuery('#woocommerce_custom_selection').click();
147                 });
148             });
149         </script>
150         <?php
151     }
152 
153     /**
154      * Save the settings
155      */
156     public function settings_save() {
157         if ( ! is_admin() )
158             return;
159 
160         // We need to save the options ourselves; settings api does not trigger save for the permalinks page
161         if ( isset( $_POST['permalink_structure'] ) || isset( $_POST['category_base'] ) && isset( $_POST['product_permalink'] ) ) {
162             // Cat and tag bases
163             $woocommerce_product_category_slug = wc_clean( $_POST['woocommerce_product_category_slug'] );
164             $woocommerce_product_tag_slug = wc_clean( $_POST['woocommerce_product_tag_slug'] );
165             $woocommerce_product_attribute_slug = wc_clean( $_POST['woocommerce_product_attribute_slug'] );
166 
167             $permalinks = get_option( 'woocommerce_permalinks' );
168             if ( ! $permalinks )
169                 $permalinks = array();
170 
171             $permalinks['category_base']    = untrailingslashit( $woocommerce_product_category_slug );
172             $permalinks['tag_base']         = untrailingslashit( $woocommerce_product_tag_slug );
173             $permalinks['attribute_base']   = untrailingslashit( $woocommerce_product_attribute_slug );
174 
175             // Product base
176             $product_permalink = wc_clean( $_POST['product_permalink'] );
177 
178             if ( $product_permalink == 'custom' ) {
179                 // Get permalink without slashes
180                 $product_permalink = trim( wc_clean( $_POST['product_permalink_structure'] ), '/' );
181 
182                 // This is an invalid base structure and breaks pages
183                 if ( '%product_cat%' == $product_permalink ) {
184                     $product_permalink = _x( 'product', 'slug', 'woocommerce' ) . '/' . $product_permalink;
185                 }
186 
187                 // Prepending slash
188                 $product_permalink = '/' . $product_permalink;
189             } elseif ( empty( $product_permalink ) ) {
190                 $product_permalink = false;
191             }
192 
193             $permalinks['product_base'] = untrailingslashit( $product_permalink );
194 
195             update_option( 'woocommerce_permalinks', $permalinks );
196         }
197     }
198 }
199 
200 endif;
201 
202 return new WC_Admin_Permalink_Settings();
WooCommerce API documentation generated by ApiGen 2.8.0