PluginProbe ʕ •ᴥ•ʔ
Permalink Manager Lite / 0.4.9
Permalink Manager Lite v0.4.9
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 / inc / permalink-manager-helper-functions.php
permalink-manager / inc Last commit date
permalink-manager-actions.php 9 years ago permalink-manager-base-editor.php 9 years ago permalink-manager-editor.php 9 years ago permalink-manager-helper-functions.php 9 years ago permalink-manager-screen-options.php 9 years ago
permalink-manager-helper-functions.php
308 lines
1 <?php
2
3 /**
4 * Additional functions used in classes and another subclasses
5 */
6
7 class Permalink_Manager_Helper_Functions extends Permalink_Manager_Class {
8
9 public function __construct() {}
10
11 /**
12 * Display error/info message
13 */
14 static function display_alert($alert_content, $alert_type, $before_tabs = false) {
15 $output = sprintf( "<div class='{$alert_type} is-dismissible notice'><p> %s </p></div>", $alert_content );
16
17 if($before_tabs) {
18 add_filter('permalink-manager-before-tabs', function( $arg ) use ( $output ) {
19 return $output;
20 });
21 } else {
22 return $output;
23 }
24 }
25
26 /**
27 * Get post_types array
28 */
29 static function get_post_types_array($format = null, $cpt = null) {
30 $post_types = get_post_types( array('public' => true), 'objects' );
31
32 $post_types_array = array();
33 if($format == 'full') {
34 foreach ( $post_types as $post_type ) {
35 $post_types_array[$post_type->name] = array('label' => $post_type->labels->name, 'name' => $post_type->name);
36 }
37 } else {
38 foreach ( $post_types as $post_type ) {
39 $post_types_array[$post_type->name] = $post_type->labels->name;
40 }
41 }
42
43 return (empty($cpt)) ? $post_types_array : $post_types_array[$cpt];
44 }
45
46 /**
47 * Generate the fields
48 */
49 static public function generate_option_field($name, $args, $group = null) {
50 // Load values from options if needed
51 $saved_values = get_option('permalink-manager');
52
53 // Reset $fields variable
54 $fields = '';
55
56 // Load default value
57 $default_value = (isset($args['default'])) ? $args['default'] : '';
58 $label = (isset($args['label'])) ? $args['label'] : '';
59 $placeholder = (isset($args['placeholder'])) ? "placeholder=\"{$args['placeholder']}\"" : '';
60 $input_class = (isset($args['input_class'])) ? "class=\"{$args['input_class']}\"" : '';
61 $container_class = (isset($args['container_class'])) ? " class=\"{$args['container_class']} field-container\"" : " class=\"field-container\"";
62 $input_name = ($group) ? "permalink-manager[{$group}][{$name}]" : $name;
63 $desc = (isset($args['desc'])) ? "<p class=\"field-desc\">{$args['desc']}</p>" : "";
64
65 switch($args['type']) {
66 case 'checkbox' :
67 $fields .= '<div class="checkboxes">';
68 foreach($args['choices'] as $value => $checkbox_label) {
69 $all_checked = (isset($saved_values[$group][$name])) ? $saved_values[$group][$name] : $args['default'];
70 $checked = in_array($value, $all_checked) ? "checked='checked'" : "";
71 $fields .= "<label for='{$input_name}[]'><input type='checkbox' {$input_class} value='{$value}' name='{$input_name}[]' {$checked} /> {$checkbox_label}</label>";
72 }
73 $fields .= '</div>';
74 break;
75
76 case 'radio' :
77 $fields .= '<div class="radios">';
78 foreach($args['choices'] as $value => $checkbox_label) {
79 $all_checked = (isset($saved_values[$group][$name])) ? $saved_values[$group][$name] : $args['default'];
80 $checked = in_array($value, $all_checked) ? "checked='checked'" : "";
81 $fields .= "<label for='{$input_name}[]'><input type='radio' {$input_class} value='{$value}' name='{$input_name}[]' {$checked} /> {$checkbox_label}</label>";
82 }
83 $fields .= '</div>';
84 break;
85
86 case 'number' :
87 $value = (isset($saved_values[$group][$name])) ? $saved_values[$group][$name] : $default_value;
88 $fields .= "<input type='number' {$input_class} value='{$value}' name='{$input_name}' />";
89 break;
90
91 case 'clearfix' :
92 return "<div class=\"clearfix\"></div>";
93
94 default :
95 $value = (isset($saved_values[$group][$name])) ? $saved_values[$group][$name] : $default_value;
96 $fields .= "<input type='text' {$input_class} value='{$value}' name='{$input_name}' {$placeholder}/>";
97 }
98
99 // Get all variables into one final variable
100 if(isset($group) && (in_array($group, array('regenerate_slugs', 'find-replace')))) {
101 $output = "<div{$container_class}>";
102 $output .= "<h4>{$label}</h4>";
103 $output .= "<div class='metabox-prefs'><div class='{$name}-container'>{$fields}</div></div>";
104 $output .= $desc;
105 $output .= "</div>";
106 } else if (isset($args['without_label']) && $args['without_label'] == true) {
107 $output = $fields;
108 } else {
109 $output = "<tr><th><label for='{$input_name}'>{$args['label']}</label></th>";
110 $output .= "<td>{$fields}</td>";
111 }
112
113 return $output;
114 }
115
116 /**
117 * Save option
118 */
119 static function save_option($field = null, $value = null) {
120 $options = get_option('permalink-manager', array());
121 if($field) {
122 $options[$field] = $value;
123 update_option('permalink-manager', $options);
124 }
125 }
126
127 /**
128 * Display the permalink in a better way
129 */
130 static function get_correct_permalink($id) {
131 $old_uris = get_option('permalink-manager-uris');
132 $permalink = isset($old_uris[$id]) ? home_url('/') . $old_uris[$id] : get_permalink($id);
133
134 return $permalink;
135 }
136
137 /**
138 * Check if the provided slug is unique and then update it with SQL query.
139 */
140 static function update_slug_by_id($slug, $id) {
141 global $wpdb;
142
143 // Update slug and make it unique
144 $slug = (empty($slug)) ? sanitize_title(get_the_title($id)) : $slug;
145 $new_slug = wp_unique_post_slug($slug, $id, get_post_status($id), get_post_type($id), null);
146 $wpdb->query("UPDATE $wpdb->posts SET post_name = '$new_slug' WHERE ID = '$id'");
147
148 return $new_slug;
149 }
150
151 /**
152 * Get permastruct
153 */
154 static function get_default_permastruct($post_type = 'page', $remove_post_tag = false) {
155 global $wp_rewrite;
156
157 // Get default permastruct
158 if($post_type == 'page') {
159 $permastruct = $wp_rewrite->get_page_permastruct();
160 } else if($post_type == 'post') {
161 $permastruct = get_option('permalink_structure');
162 } else {
163 $permastruct = $wp_rewrite->get_extra_permastruct($post_type);
164 }
165
166 return ($remove_post_tag) ? trim(str_replace(array("%postname%", "%pagename%", "%{$post_type}%"), "", $permastruct), "/") : $permastruct;
167 }
168
169 static function get_uri($post_id, $get_default = false, $remove_slug = true) {
170
171 // Load all bases & post
172 $post = isset($post_id->post_type) ? $post_id : get_post($post_id);
173 $post_id = $post->ID;
174 $post_type = $post->post_type;
175 $post_name = $post->post_name;
176
177 $all_uris = get_option('permalink-manager-uris');
178 $all_permastructures = get_option('permalink-manager-permastructs');
179 $options = get_option('permalink-manager');
180 $default_permastruct = Permalink_Manager_Helper_Functions::get_default_permastruct($post_type);
181
182 if($get_default) {
183
184 if($get_default === 'native') {
185 $permastruct = $default_permastruct;
186 } else if($all_permastructures) {
187 $permastruct = isset($all_permastructures[$post_type]) ? $all_permastructures[$post_type] : $default_permastruct;
188 } else {
189 $permastruct = isset($options['base-editor'][$post_type]) ? $options['base-editor'][$post_type] : $default_permastruct;
190 }
191
192 // Get options
193 $default_base = ($permastruct) ? trim($permastruct, '/') : "";
194
195 // Get the date
196 $date = explode(" ",date('Y m d H i s', strtotime($post->post_date)));
197
198 // Get the category (if needed)
199 $category = '';
200 if ( strpos($default_base, '%category%') !== false ) {
201 $cats = get_the_category($post->ID);
202 if ( $cats ) {
203 usort($cats, '_usort_terms_by_ID'); // order by ID
204 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
205 $category_object = get_term( $category_object, 'category' );
206 $category = $category_object->slug;
207 if ( $parent = $category_object->parent )
208 $category = get_category_parents($parent, false, '/', true) . $category;
209 }
210 // show default category in permalinks, without having to assign it explicitly
211 if ( empty($category) ) {
212 $default_category = get_term( get_option( 'default_category' ), 'category' );
213 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
214 }
215 }
216
217 // Get the author (if needed)
218 $author = '';
219 if ( strpos($default_base, '%author%') !== false ) {
220 $authordata = get_userdata($post->post_author);
221 $author = $authordata->user_nicename;
222 }
223
224 // Fix for hierarchical CPT (start)
225 $full_slug = get_page_uri($post);
226 $post_type_tag = Permalink_Manager_Helper_Functions::get_post_tag($post_type);
227
228 // Do the replacement (post tag is removed now to enable support for hierarchical CPT)
229 $tags = array('%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', '%post_id%', '%category%', '%author%', $post_type_tag);
230 $replacements = array($date[0], $date[1], $date[2], $date[3], $date[4], $date[5], $post->ID, $category, $author, '');
231 $default_uri = str_replace($tags, $replacements, "{$default_base}/{$full_slug}");
232
233 // Replace custom taxonomies
234 $terms = get_taxonomies( array('public' => true, '_builtin' => false), 'names', 'and' );
235 $taxonomies = $terms;
236 if ( $taxonomies ) {
237 foreach($taxonomies as $taxonomy) {
238 $tag = "%{$taxonomy}%";
239 $terms = wp_get_object_terms($post->ID, $taxonomy);
240 if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
241 $replacement = $terms[0]->slug;
242 $default_uri = str_replace($tag, $replacement, $default_uri);
243 }
244 }
245 }
246 } else {
247 $default_uri = str_replace(home_url("/"), "", get_permalink($post_id));
248 }
249
250 $uri = isset($all_uris[$post_id]) ? $all_uris[$post_id] : $default_uri;
251 // Remove post_name from base (last part of string)
252 $uri = ($remove_slug) ? str_replace($post_name, '', $uri) : $uri;
253
254 $final_uri = ($get_default) ? $default_uri : $uri;
255
256 // Clean URI
257 $final_uri = preg_replace('/\s+/', '', $final_uri);
258 $final_uri = str_replace('//', '/', $final_uri);
259 $final_uri = trim($final_uri, "/");
260
261 return $final_uri;
262 }
263
264 /**
265 * Structure Tags & Rewrite functions
266 */
267 static function get_all_structure_tags($code = true, $seperator = ', ', $hide_slug_tags = true) {
268 global $wp_rewrite;
269
270 $tags = $wp_rewrite->rewritecode;
271 $output = "";
272 $last_tag_index = count($tags);
273 $i = 1;
274
275 // Hide slug tags
276 if($hide_slug_tags) {
277 $post_types = Permalink_Manager_Helper_Functions::get_post_types_array();
278 foreach($post_types as $post_type => $post_type_name) {
279 $post_type_tag = Permalink_Manager_Helper_Functions::get_post_tag($post_type);
280 // Find key with post type tag from rewritecode
281 $key = array_search($post_type_tag, $tags);
282 if($key) { unset($tags[$key]); }
283 }
284 }
285
286 foreach($tags as $tag) {
287 $sep = ($last_tag_index == $i) ? "" : $seperator;
288 $output .= ($code) ? "<code>{$tag}</code>{$sep}" : "{$tag}{$sep}";
289 $i++;
290 }
291
292 return $output;
293 }
294
295 static function get_post_tag($post_type) {
296 // Get the post type (with fix for posts & pages)
297 if($post_type == 'page') {
298 $post_type_tag = '%pagename%';
299 } else if ($post_type == 'post') {
300 $post_type_tag = '%postname%';
301 } else {
302 $post_type_tag = "%{$post_type}%";
303 }
304 return $post_type_tag;
305 }
306
307 }
308