PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / admin / permalink-settings.php

permalink-settings.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/admin/permalink-settings.php

173 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Admin;
4
5 use StoreEngine\Utils\Formatting;
6 use StoreEngine\Utils\Helper;
7
8 class PermalinkSettings {
9
10 /**
11 * Permalink settings.
12 *
13 * @var array
14 */
15 private array $permalinks = array();
16
17 public static function init() {
18 $self = new self();
19 $self->settings_init();
20 $self->settings_save();
21 }
22
23 /**
24 * Init our settings.
25 */
26 public function settings_init() {
27 add_settings_section( 'storeengine-permalink', __( 'StoreEngine Product permalinks', 'storeengine' ), [ $this, 'settings' ], 'permalink' );
28
29 add_settings_field(
30 'storeengine_product_category_slug',
31 __( 'Product category base', 'storeengine' ),
32 [ $this, 'product_category_slug_input' ],
33 'permalink',
34 'optional'
35 );
36 add_settings_field(
37 'storeengine_product_tag_slug',
38 __( 'Product tag base', 'storeengine' ),
39 [ $this, 'product_tag_slug_input' ],
40 'permalink',
41 'optional'
42 );
43 $this->permalinks = Helper::get_permalink_structure();
44 }
45
46 public function product_category_slug_input() {
47 ?>
48 <input name="storeengine_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', 'storeengine' ); ?>" />
49 <?php
50 }
51
52 public function product_tag_slug_input() {
53 ?>
54 <input name="storeengine_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', 'storeengine' ); ?>" />
55 <?php
56 }
57
58 /**
59 * Show the settings.
60 */
61 public function settings() {
62 /* translators: %s: Home URL */
63 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.', 'storeengine' ), esc_url( home_url( '/' ) ) ) ) );
64
65 $shop_page_id = (int) Helper::get_settings( 'shop_page' );
66 $base_slug = urldecode( ( $shop_page_id > 0 && get_post( $shop_page_id ) ) ? get_page_uri( $shop_page_id ) : _x( 'shop', 'default-slug', 'storeengine' ) );
67 $product_base = _x( 'product', 'default-slug', 'storeengine' );
68
69 $structures = array(
70 0 => '',
71 1 => '/' . trailingslashit( $base_slug ),
72 2 => '/' . trailingslashit( $base_slug ) . trailingslashit( '%product_category%' ),
73 );
74
75 ?>
76 <table class="form-table storeengine-permalink-structure">
77 <tbody>
78 <tr>
79 <th><label><input name="storeengine_product_permalink" type="radio" value="<?php echo esc_attr( $structures[0] ); ?>" class="storeenginetog" <?php checked( $structures[0], $this->permalinks['product_base'] ); ?> /> <?php esc_html_e( 'Default', 'storeengine' ); ?></label></th>
80 <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>
81 </tr>
82 <?php if ( $shop_page_id ) : ?>
83 <tr>
84 <th><label><input name="storeengine_product_permalink" type="radio" value="<?php echo esc_attr( $structures[1] ); ?>" class="storeenginetog" <?php checked( $structures[1], $this->permalinks['product_base'] ); ?> /> <?php esc_html_e( 'Products base', 'storeengine' ); ?></label></th>
85 <td><code><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $base_slug ); ?>/sample-product/</code></td>
86 </tr>
87 <?php endif; ?>
88 <tr>
89 <th><label><input name="storeengine_product_permalink" id="storeengine_custom_selection" type="radio" value="custom" class="tog" <?php checked( in_array( $this->permalinks['product_base'], $structures, true ), false ); ?> />
90 <?php esc_html_e( 'Custom base', 'storeengine' ); ?></label></th>
91 <td>
92 <input name="storeengine_product_permalink_structure" id="storeengine_product_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.', 'storeengine' ); ?></span>
93 </td>
94 </tr>
95 </tbody>
96 </table>
97 <?php wp_nonce_field( 'storeengine-permalinks', 'storeengine-permalinks-nonce' ); ?>
98 <script type="text/javascript">
99 jQuery( function() {
100 jQuery('input.storeenginetog').on( 'change', function() {
101 jQuery('#storeengine_product_permalink_structure').val( jQuery( this ).val() );
102 });
103 jQuery('.permalink-structure input').on( 'change', function() {
104 jQuery('.storeengine-permalink-structure').find('code.non-default-example, code.default-example').hide();
105 if ( jQuery(this).val() ) {
106 jQuery('.storeengine-permalink-structure code.non-default-example').show();
107 jQuery('.storeengine-permalink-structure input').prop('disabled', false);
108 } else {
109 jQuery('.storeengine-permalink-structure code.default-example').show();
110 jQuery('.storeengine-permalink-structure input:eq(0)').trigger( 'click' );
111 jQuery('.storeengine-permalink-structure input').attr('disabled', 'disabled');
112 }
113 });
114 jQuery('.permalink-structure input:checked').trigger( 'change' );
115 jQuery('#storeengine_permalink_structure').on( 'focus', function(){
116 jQuery('#storeengine_custom_selection').trigger( 'click' );
117 } );
118 } );
119 </script>
120 <?php
121 }
122
123 /**
124 * Save the settings.
125 */
126 public function settings_save() {
127 if ( ! is_admin() ) {
128 return;
129 }
130
131 // We need to save the options ourselves; settings api does not trigger save for the permalinks page.
132 if ( isset( $_POST['permalink_structure'], $_POST['storeengine-permalinks-nonce'], $_POST['storeengine_product_category_slug'], $_POST['storeengine_product_tag_slug'] ) && wp_verify_nonce( sanitize_text_field(wp_unslash( $_POST['storeengine-permalinks-nonce'] )), 'storeengine-permalinks' ) ) {
133 // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
134 $permalinks = (array) get_option( 'storeengine_permalinks', array() );
135 $permalinks['category_base'] = Formatting::sanitize_permalink( wp_unslash( $_POST['storeengine_product_category_slug'] ) );
136 $permalinks['tag_base'] = Formatting::sanitize_permalink( wp_unslash( $_POST['storeengine_product_tag_slug'] ) );
137 // phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
138
139 // Generate product base.
140 $product_base = isset( $_POST['storeengine_product_permalink'] ) ? sanitize_text_field( wp_unslash( $_POST['storeengine_product_permalink'] ) ) : '';
141
142 if ( 'custom' === $product_base ) {
143 if ( isset( $_POST['storeengine_product_permalink_structure'] ) ) {
144 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
145 $product_base = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', trim( wp_unslash( $_POST['storeengine_product_permalink_structure'] ) ) ) );
146 } else {
147 $product_base = '/';
148 }
149
150 // This is an invalid base structure and breaks pages.
151 if ( '/%product_category%/' === trailingslashit( $product_base ) ) {
152 $product_base = '/' . _x( 'product', 'slug', 'storeengine' ) . $product_base;
153 }
154 } elseif ( empty( $product_base ) ) {
155 $product_base = _x( 'product', 'slug', 'storeengine' );
156 }
157
158 $permalinks['product_base'] = Formatting::sanitize_permalink( $product_base );
159
160 // Shop base may require verbose page rules if nesting pages.
161 $shop_page_id = (int) Helper::get_settings( 'shop_page' );
162 $shop_permalink = ( $shop_page_id > 0 && get_post( $shop_page_id ) ) ? get_page_uri( $shop_page_id ) : _x( 'shop', 'default-slug', 'storeengine' );
163
164 if ( $shop_page_id && stristr( trim( $permalinks['product_base'], '/' ), $shop_permalink ) ) {
165 $permalinks['use_verbose_page_rules'] = true;
166 }
167
168 update_option( 'storeengine_permalinks', $permalinks );
169 }//end if
170 }
171
172 }
173