PluginProbe ʕ •ᴥ•ʔ
Permalink Manager Lite / 2.5.1.2
Permalink Manager Lite v2.5.1.2
2.6.0 2.5.5 2.5.4 2.5.3.4 2.2.18 2.2.19.2 2.2.19.3 2.2.19.3.1 2.2.2 2.2.20 2.2.20.1 2.2.20.3 2.2.4 2.2.5 2.2.6 2.2.7.2 2.2.7.3 2.2.7.5 2.2.7.6 2.2.8.4 2.2.8.5 2.2.8.6 2.2.8.7 2.2.8.9 2.2.9.1 2.2.9.2 2.2.9.2.1 2.2.9.3 2.2.9.4 2.2.9.6 2.2.9.7 2.2.9.9 2.3.0 2.3.1.1 2.4.0 2.4.1 2.4.1.2 2.4.1.3 2.4.1.4 2.4.1.5 2.4.1.6 2.4.2 2.4.2.1 2.4.3 2.4.3.1 2.4.3.2 2.4.3.3 2.4.3.4 2.4.4 2.4.4.1 2.4.4.2 2.4.4.3 2.5.0 2.5.1 2.5.1.1 2.5.1.2 2.5.1.3 2.5.1.4 2.5.2 2.5.2.1 2.5.2.2 2.5.2.3 2.5.2.4 2.5.3 2.5.3.1 2.5.3.2 2.5.3.3 trunk 0.2 0.3 0.3.1 0.3.2 0.3.3 0.3.4 0.4 0.4.1 0.4.2 0.4.3 0.4.4 0.4.6 0.4.7 0.4.8 0.4.9 0.5.3 0.5.4 1.0.0 1.0.1 1.0.4 1.1.0 1.1.1 1.1.2 1.11.6.3 2.0.0 2.0.3 2.0.4 2.0.4.3 2.0.5.1 2.0.5.2 2.0.5.3 2.0.5.3.1 2.0.5.4 2.0.5.4a 2.0.5.5 2.0.5.6 2.0.5.6.1 2.0.5.7 2.0.5.9a 2.0.6.2.1 2.0.6.2a 2.0.6.3 2.1.0 2.1.1 2.1.2.4 2.2.0 2.2.1.1 2.2.1.2 2.2.11 2.2.12 2.2.13.1 2.2.14 2.2.15.1 2.2.16 2.2.17
permalink-manager / includes / views / permalink-manager-permastructures.php
permalink-manager / includes / views Last commit date
permalink-manager-debug.php 1 year ago permalink-manager-permastructures.php 1 year ago permalink-manager-settings.php 1 year ago permalink-manager-tools.php 1 year ago permalink-manager-ui-elements.php 1 year ago permalink-manager-uri-editor-post.php 1 year ago permalink-manager-uri-editor.php 1 year ago
permalink-manager-permastructures.php
316 lines
1 <?php
2
3 /**
4 * Display the page where the slugs could be regenerated or replaced
5 */
6 class Permalink_Manager_Permastructs {
7
8 public function __construct() {
9 add_filter( 'permalink_manager_sections', array( $this, 'add_admin_section' ), 2 );
10 }
11
12 /**
13 * Add a new section to the Permalink Manager UI
14 *
15 * @param array $admin_sections
16 *
17 * @return array
18 */
19 public function add_admin_section( $admin_sections ) {
20 $admin_sections['permastructs'] = array(
21 'name' => __( 'Permastructures', 'permalink-manager' ),
22 'function' => array( 'class' => 'Permalink_Manager_Permastructs', 'method' => 'output' )
23 );
24
25 return $admin_sections;
26 }
27
28 /**
29 * Return an array of fields that will be used to adjust the permastructure settings
30 *
31 * @return array
32 */
33 public function get_fields() {
34 $post_types = Permalink_Manager_Helper_Functions::get_post_types_array( 'full' );
35 $taxonomies = Permalink_Manager_Helper_Functions::get_taxonomies_array( 'full' );
36
37 // Display additional information in Permalink Manager Lite
38 if ( ! Permalink_Manager_Admin_Functions::is_pro_active() ) {
39 /* translators: %s: Permalink Manager Pro website */
40 $pro_text = sprintf( __( 'To edit taxonomy permalinks, <a href="%s" target="_blank">Permalink Manager Pro</a> is required.', 'permalink-manager' ), PERMALINK_MANAGER_WEBSITE );
41 $pro_text = sprintf( '<div class="alert info">%s</div>', $pro_text );
42 }
43
44 // 1. Get fields
45 $fields = array(
46 'post_types' => array(
47 'section_name' => __( 'Post types', 'permalink-manager' ),
48 'container' => 'row',
49 'fields' => array()
50 ),
51 'taxonomies' => array(
52 'section_name' => __( 'Taxonomies', 'permalink-manager' ),
53 'container' => 'row',
54 'fields' => array(),
55 'append_content' => ( ! empty( $pro_text ) ) ? $pro_text : ''
56 )
57 );
58
59 // 2. Add a separate section for WooCommerce content types
60 if ( class_exists( 'WooCommerce' ) ) {
61 $fields['woocommerce'] = array(
62 'section_name' => "<i class=\"woocommerce-icon woocommerce-cart\"></i> " . __( 'WooCommerce', 'permalink-manager' ),
63 'container' => 'row',
64 'fields' => array(),
65 'append_content' => ( ! empty( $pro_text ) ) ? $pro_text : ''
66 );
67 }
68
69 // 3A. Add permastructure fields for post types
70 foreach ( $post_types as $post_type ) {
71 if ( $post_type['name'] == 'shop_coupon' ) {
72 continue;
73 }
74
75 $fields["post_types"]["fields"][ $post_type['name'] ] = self::get_single_permastructure_field( $post_type, false, false );
76 }
77
78 // 3B. Add permastructure fields for taxonomies
79 foreach ( $taxonomies as $taxonomy ) {
80 $taxonomy_name = $taxonomy['name'];
81
82 // Check if taxonomy exists
83 if ( ! taxonomy_exists( $taxonomy_name ) ) {
84 continue;
85 }
86
87 $fields["taxonomies"]["fields"][ $taxonomy_name ] = self::get_single_permastructure_field( $taxonomy, true, isset( $pro_text ) );
88 }
89
90 // 4. Separate WooCommerce CPT & custom taxonomies
91 if ( class_exists( 'WooCommerce' ) ) {
92 $woocommerce_fields = array( 'product' => 'post_types', 'product_tag' => 'taxonomies', 'product_cat' => 'taxonomies' );
93 $woocommerce_attributes = wc_get_attribute_taxonomies();
94
95 foreach ( $woocommerce_attributes as $woocommerce_attribute ) {
96 $woocommerce_fields["pa_{$woocommerce_attribute->attribute_name}"] = 'taxonomies';
97 }
98
99 foreach ( $woocommerce_fields as $field => $field_type ) {
100 if ( empty( $fields[ $field_type ]["fields"][ $field ] ) ) {
101 continue;
102 }
103
104 $fields["woocommerce"]["fields"][ $field ] = $fields[ $field_type ]["fields"][ $field ];
105 unset( $fields[ $field_type ]["fields"][ $field ] );
106 }
107 }
108
109 return apply_filters( 'permalink_manager_permastructs_fields', $fields );
110 }
111
112 /**
113 * Get the row of the permastructure field for single content type
114 *
115 * @param $content_type
116 * @param bool $is_tax
117 * @param bool $pro_alert
118 *
119 * @return string
120 */
121 function get_single_permastructure_field( $content_type, $is_tax = false, $pro_alert = false ) {
122 global $permalink_manager_permastructs;
123
124 if ( empty( $content_type['name'] ) ) {
125 return '';
126 }
127
128 $content_group = ( $is_tax ) ? "taxonomies" : "post_types";
129 $content_type_name = $content_type['name'];
130 $content_type_label = $content_type['label'];
131
132 $siteurl = Permalink_Manager_Helper_Functions::get_permalink_base();
133 $tags_container_id = sprintf( 'permastruct-tags-%s-%s', $content_group, $content_type_name );
134 $available_tags = self::get_all_structure_tags( $content_type_name, $is_tax );
135
136 // Get permastructures
137 $permastructures = ( ! empty( $permalink_manager_permastructs[ $content_group ] ) ) ? $permalink_manager_permastructs[ $content_group ] : array();
138 $default_permastruct = trim( Permalink_Manager_Permastructure_Functions::get_default_permastruct( $content_type_name ), "/" );
139 $current_permastruct = isset( $permastructures[ $content_type_name ] ) ? $permastructures[ $content_type_name ] : $default_permastruct;
140
141 // Append extra attributes
142 $field_atts = array(
143 'value' => $current_permastruct,
144 'input_class' => 'permastruct-field',
145 'disabled' => $pro_alert,
146 'placeholder' => $default_permastruct,
147 'extra_atts' => " data-default=\"{$default_permastruct}\""
148 );
149
150 $field_name = sprintf( '%s[%s]', $content_group, $content_type_name );
151 $permastruct_field = sprintf( "<div class=\"permastruct-field-container\"><span><code>%s/</code></span><span>%s</span></div>", $siteurl, Permalink_Manager_UI_Elements::generate_option_field( $field_name, $field_atts ) );
152
153 $buttons = sprintf( "<p class=\"permastruct-buttons\">
154 <span><a href=\"#\" class=\"button button-small button-secondary extra-settings\"><span class=\"dashicons dashicons-admin-settings\"></span> %s</a></span>
155 <span><a href=\"/?TB_inline&width=800&height=600&inlineId=%s\" class=\"button button-small button-secondary show-tags thickbox\"><span class=\"dashicons dashicons-tag\"></span> %s</a></span>
156 </p>", __( "Extra settings", "permalink-manager" ), $tags_container_id, __( "Available tags", "permalink-manager" ) );
157
158 $language_fields = '';
159 $languages = Permalink_Manager_Language_Plugins::get_all_languages( true );
160 if ( $languages ) {
161 $language_fields = sprintf( "<h4>%s</h4><p class=\"permastruct-instruction\">%s</p>", __( "Permastructure translations", "permalink-manager" ), __( "If you would like to translate the permastructures and set-up different permalink structure per language, please fill in the fields below. Otherwise the permastructure set for default language (see field above) will be applied.", "permalink-manager" ) );
162
163 foreach ( $languages as $lang => $name ) {
164 $current_lang_permastruct = isset( $permastructures["{$content_type_name}_{$lang}"] ) ? $permastructures["{$content_type_name}_{$lang}"] : '';
165
166 $lang_field_atts = array_merge( $field_atts, array( 'value' => $current_lang_permastruct, 'extra_atts' => 'data-default=""', 'placeholder' => $current_permastruct ) );
167 $lang_field_name = str_replace( "]", "_{$lang}]", $field_name );
168
169 $language_fields .= sprintf( "<label>%s</label><div class=\"permastruct-field-container\"><span><code>%s/</code></span><span>%s</span></div>", $name, Permalink_Manager_Language_Plugins::prepend_lang_prefix( $siteurl, '', $lang ), Permalink_Manager_UI_Elements::generate_option_field( $lang_field_name, $lang_field_atts ) );
170 }
171 }
172
173 $default_permastruct_row = sprintf( "<p class=\"default-permastruct-row columns-container\">
174 <span class=\"column-2_4\"><strong>%s:</strong> %s</span>
175 <span class=\"column-2_4\"><a href=\"#\" class=\"restore-default\"><span class=\"dashicons dashicons-image-rotate\"></span> %s</a></span>
176 </p>", __( "Default permastructure", "permalink-manager" ), esc_html( $default_permastruct ), __( "Restore default permastructure", "permalink-manager" ) );
177
178 $permastructure_settings = sprintf( "<h4>%s</h4><div class=\"settings-container\">%s</div>", __( "Permastructure settings", "permalink-manager" ), Permalink_Manager_UI_Elements::generate_option_field( "permastructure-settings[do_not_append_slug][$content_group][{$content_type_name}]", array( 'type' => 'single_checkbox', 'default' => 1, 'checkbox_label' => __( "Do not automatically append the slug", "permalink-manager" ) ) ) );
179
180 // Combine all HTML chunks
181 $html = sprintf( "<div class=\"permastruct-container\">%s%s</div>", $permastruct_field, $buttons );
182 $html .= sprintf( "<div class=\"permastruct-settings\">%s%s%s</div>", $language_fields, $default_permastruct_row, $permastructure_settings );
183 $html .= sprintf( '<div id="%s" style="display:none;">%s</div>', $tags_container_id, $available_tags );
184
185 $label_tag = sprintf( "<th><label for=\"%s\">%s</label></th>", $field_name, $content_type_label );
186
187 return sprintf( "<tr id=\"%s_%s\" data-field=\"%s\" class=\"%s\">%s<td><fieldset>%s</fieldset></td></tr>", esc_attr( $content_group ), esc_attr( $content_type_name ), esc_attr( $field_name ), 'field-container permastruct-row', $label_tag, $html );
188 }
189
190
191 /**
192 * Get the array with settings and render the HTML output
193 */
194 public function output() {
195 $sidebar = "<div class=\"notice notice-warning inline\"><p>";
196 $sidebar .= __( 'The current permastructures settings will be automatically applied <strong>only to the new posts & terms</strong>.', 'permalink-manager' );
197 $sidebar .= '<br />';
198 /* translators: %s: Regenerate/reset admin URL */
199 $sidebar .= sprintf( __( 'To apply the <strong>new format to existing posts and terms</strong>, please use "<a href="%s">Regenerate/reset</a>" tool after you update the permastructure settings below.', 'permalink-manager' ), admin_url( 'tools.php?page=permalink-manager&section=tools&subsection=regenerate_slugs' ) );
200 $sidebar .= "</div>";
201
202 return Permalink_Manager_UI_Elements::get_the_form( self::get_fields(), '', array( 'text' => __( 'Save permastructures', 'permalink-manager' ), 'class' => 'primary margin-top' ), $sidebar, array( 'action' => 'permalink-manager', 'name' => 'permalink_manager_permastructs' ) );
203 }
204
205 /**
206 * Get a list of all structure tags
207 *
208 * @param string $content_type
209 * @param bool $is_taxonomy
210 *
211 * @return string
212 */
213 static function get_all_structure_tags( $content_type = '', $is_taxonomy = false ) {
214 if ( empty( $content_type ) ) {
215 return '';
216 }
217
218 $html = '';
219
220 $tags_groups = array(
221 'slug' => array(
222 'heading' => __( 'Native slug & title', 'permalink-manager' ),
223 'description' => __( 'The native slug is generated from the initial title and will not update automatically if the title is changed later. You can use the %native_title% tag to replace native slugs with actual titles, even if another content item has the same title.', 'permalink-manager' )
224 ),
225 'meta' => array(
226 'heading' => __( 'Meta data', 'permalink-manager' ),
227 'description' => __( 'Using meta tags, you may add post-specific information like item IDs or author names to permalinks. This might be beneficial to news, events, and time-sensitive content where you can use date-based tags.', 'permalink-manager' )
228 ),
229 'taxonomies' => array(
230 'heading' => __( 'Taxonomies', 'permalink-manager' ),
231 'description' => __( 'Custom permalinks can include taxonomy-based slugs such as categories, tags, and custom taxonomy terms. If a post belongs to multiple terms, the lowest-level one is used unless a specific term is selected as primary.', 'permalink-manager' )
232 ),
233 'custom_fields' => array(
234 'heading' => __( 'Custom fields', 'permalink-manager' ),
235 'description' => __( 'Permalinks can be modified with custom fields to dynamically include extra data. For example, you can append product SKUs to WooCommerce URLs or include geolocation details in your custom post types\' permalinks.', 'permalink-manager' ),
236 'pro' => true
237 )
238 );
239
240 if ( ! $is_taxonomy ) {
241 $post_type_tag = Permalink_Manager_Permastructure_Functions::get_post_tag( $content_type );
242 $post_type_taxonomies = get_taxonomies( array( 'object_type' => array( $content_type ) ), 'objects' );
243
244 $tags_groups['slug']['tags'] = array(
245 $post_type_tag,
246 ( $content_type !== 'post' ) ? '%postname%' : '',
247 '%native_title%'
248 );
249
250 $tags_groups['meta']['tags'] = array(
251 '%post_id%',
252 '%author%',
253 '%year%',
254 '%monthnum%',
255 '%monthname%',
256 '%day%',
257 '%hour%',
258 '%minute%',
259 '%second%',
260 '%post_type%'
261 );
262
263 if ( ! empty( $post_type_taxonomies ) ) {
264 $tags_groups['taxonomies']['tags'] = array();
265
266 foreach ( $post_type_taxonomies as $post_type_taxonomy ) {
267 $tags_groups['taxonomies']['tags'][] = sprintf('%%%s%%', $post_type_taxonomy->name);
268 }
269 }
270 } else {
271 $taxonomy_tag = sprintf( '%%%s%%', $content_type );
272
273 $tags_groups['slug']['tags'] = array(
274 $taxonomy_tag,
275 '%term_name%',
276 '%native_title%'
277 );
278
279 $tags_groups['meta']['tags'] = array(
280 '%term_id%',
281 '%taxonomy%'
282 );
283 }
284
285 $tags_groups['custom_fields']['tags'] = array(
286 '%__custom_field_name%'
287 );
288
289 foreach ( $tags_groups as $tags_group ) {
290 if ( empty( $tags_group['tags'] ) ) {
291 continue;
292 }
293
294 $html .= sprintf( '<h3>%s</h3>', $tags_group['heading'] );
295
296 if ( ! empty( $tags_group['pro'] ) ) {
297 $pro_text = Permalink_Manager_UI_Elements::pro_text( true );
298 $html .= ( ! empty( $pro_text ) ) ? sprintf( '<span class="notice notice-error inline">%s</span>', $pro_text ) : '';
299 }
300
301 $html .= '<div class="permastruct-tag-container">';
302 $html .= sprintf( '<div><p class="description">%s</p></div>', $tags_group['description'] );
303
304 $html .= '<div><div class="permastruct-tag-buttons">';
305 foreach ( $tags_group['tags'] as $tag ) {
306 $html .= ( ! empty( $tag ) ) ? sprintf( '<button type="button" class="button button-small button-secondary" data-original-text="%1$s">%1$s</button>', $tag ) : '';
307 }
308 $html .= '</div></div>';
309 $html .= '</div>';
310 }
311
312 return sprintf( "<div class=\"structure-tags-list\">%s</div>", $html );
313 }
314
315 }
316