PluginProbe ʕ •ᴥ•ʔ
Permalink Manager Lite / 0.5.3
Permalink Manager Lite v0.5.3
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 / core / permalink-manager-admin-functions.php
permalink-manager / includes / core Last commit date
permalink-manager-admin-functions.php 9 years ago permalink-manager-helper-functions.php 9 years ago permalink-manager-post-uri-functions.php 9 years ago permalink-manager-uri-actions.php 9 years ago
permalink-manager-admin-functions.php
489 lines
1 <?php
2
3 /**
4 * Additional back-end functions related to Wordpress Dashboard UI
5 */
6 class Permalink_Manager_Admin_Functions extends Permalink_Manager_Class {
7
8 public $menu_name, $sections, $active_section, $active_subsection;
9 public $plugin_slug = PERMALINK_MANAGER_PLUGIN_SLUG;
10 public $plugin_basename = PERMALINK_MANAGER_BASENAME;
11
12 public function __construct() {
13 add_action( "admin_menu", array($this, "add_menu_page") );
14 add_action( 'admin_init', array($this, 'init') );
15
16 // Allow to edit URIs in posts, pages & CPT elements
17 add_filter( 'get_sample_permalink_html', array($this, 'edit_uri_box'), 999, 4 );
18 }
19
20 /**
21 * Hooks that should be triggered with "admin_init"
22 */
23 public function init() {
24 // Additional link in "Plugins" page
25 add_filter( "plugin_action_links_{$this->plugin_basename}", array($this, "plugins_page_links") );
26
27 // Detect current section
28 $this->sections = apply_filters('permalink-manager-sections', array());
29 $this->get_current_section();
30
31 // Save settings
32 $this->save_settings();
33 }
34
35 /**
36 * Get current section (only in plugin sections)
37 */
38 public function get_current_section() {
39 global $active_section, $active_subsection;
40
41 // 1. Get current section
42 if(isset($_GET['page']) && $_GET['page'] == $this->plugin_slug) {
43 if(isset($_POST['section'])) {
44 $this->active_section = $_POST['section'];
45 } else if(isset($_GET['section'])) {
46 $this->active_section = $_GET['section'];
47 } else {
48 $sections_names = array_keys($this->sections);
49 $this->active_section = $sections_names[0];
50 }
51 }
52
53 // 2. Get current subsection
54 if($this->active_section && isset($this->sections[$this->active_section]['subsections'])) {
55 if(isset($_POST['subsection'])) {
56 $this->active_subsection = $_POST['subsection'];
57 } else if(isset($_GET['subsection'])) {
58 $this->active_subsection = $_GET['subsection'];
59 } else {
60 $subsections_names = array_keys($this->sections[$this->active_section]['subsections']);
61 $this->active_subsection = $subsections_names[0];
62 }
63 }
64
65 // Set globals
66 $active_section = $this->active_section;
67 $active_subsection = $this->active_subsection;
68 }
69
70 /**
71 * Save settings
72 */
73 public function save_settings() {
74 global $permalink_manager_options, $permalink_manager_permastructs;
75
76 // Check if the form was submitted
77 if(empty($_POST)) { return; }
78
79 $post_fields = $_POST;
80 $new_options = array();
81 $helper_functions = new Permalink_Manager_Helper_Functions();
82
83 // 1. Sanitize the field values
84 foreach($post_fields as $option_name => $option_value) {
85 //$new_options[$option_name] = $helper_functions->multidimensional_array_map('sanitize_text_field', $option_value);
86 $new_options[$option_name] = $option_value;
87 }
88
89 // 2A. Save options/settings
90 if(isset($_POST['permalink-manager-options']) && wp_verify_nonce($_POST['permalink-manager-options'], 'save_settings')) {
91 // Override the global with settings
92 $permalink_manager_options = $new_options = array_filter($new_options);
93
94 // Save the settings in database
95 update_option('permalink-manager', $new_options);
96 }
97 // 2B. Save permastructs
98 else if(isset($_POST['permalink-manager-permastructs']) && wp_verify_nonce($_POST['permalink-manager-permastructs'], 'save_settings')) {
99 // Trim the trailing slashes & remove empty permastructures
100 $new_options = $helper_functions->multidimensional_array_map('untrailingslashit', $new_options['permastructures']);
101 $new_options = array_filter($new_options);
102
103 // Override the global with settings
104 $permalink_manager_permastructs = $new_options;
105
106 // Save the settings in database
107 update_option('permalink-manager-permastructs', $new_options);
108 }
109 }
110
111 /**
112 * Add menu page.
113 */
114 public function add_menu_page() {
115 $this->menu_name = add_management_page( __('Permalink Manager', 'permalink-manager'), __('Permalink Manager', 'permalink-manager'), 'manage_options', $this->plugin_slug, array($this, 'display_section') );
116
117 // Make sure thata the CSS and JS files are loaded only on plugin admin page.
118 add_action( 'admin_print_styles-' . $this->menu_name, array($this, 'enqueue_styles' ) );
119 add_action( 'admin_print_scripts-' . $this->menu_name, array($this, 'enqueue_scripts' ) );
120 }
121
122 /**
123 * Register the CSS file for the dashboard.
124 */
125 public function enqueue_styles() {
126 wp_enqueue_style( $this->plugin_slug, PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.css', array(), PERMALINK_MANAGER_VERSION, 'all' );
127 }
128
129 /**
130 * Register the JavaScript file for the dashboard.
131 */
132 public function enqueue_scripts() {
133 wp_enqueue_script( $this->plugin_slug, PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.js', array( 'jquery' ), PERMALINK_MANAGER_VERSION, false );
134 }
135
136 /**
137 * Get admin url for the plugin
138 */
139 function get_admin_url($append = '') {
140 return menu_page_url( "{$this->plugin_slug}", false ) . $append;
141 }
142
143 /**
144 * Additional links on "Plugins" page
145 */
146 public function plugins_page_links($links) {
147 $links[] = '<a href="' . $this->get_admin_url() .'">' . __( 'Go To Permalink Manager', 'permalink-manager' ) . '</a>';
148 return $links;
149 }
150
151 /**
152 * Generate the fields
153 */
154 static public function generate_option_field($input_name, $args) {
155 global $permalink_manager_options;
156
157 // Reset $fields variables
158 $fields = $section_name = $field_name = '';
159
160 // Allow to filter the $args
161 $args = apply_filters('permalink-manager-field-args', $args, $input_name);
162
163 $default = (isset($args['default'])) ? $args['default'] : '';
164 $label = (isset($args['label'])) ? $args['label'] : '';
165 $placeholder = (isset($args['placeholder'])) ? "placeholder=\"{$args['placeholder']}\"" : '';
166 $readonly = (isset($args['readonly'])) ? "readonly=\"readonly\"" : '';
167 $rows = (isset($args['rows'])) ? "rows=\"{$rows}\"" : "rows=\"5\"";
168 $input_class = (isset($args['input_class'])) ? "class=\"{$args['input_class']}\"" : '';
169 $container_class = (isset($args['container_class'])) ? " class=\"{$args['container_class']} field-container\"" : " class=\"field-container\"";
170 $description = (isset($args['description'])) ? "<p class=\"field-description description\">{$args['description']}</p>" : "";
171 $append_content = (isset($args['append_content'])) ? "{$args['append_content']}" : "";
172
173 // Get the field value (if it is not set in $args)
174 if(isset($args['value']) && empty($args['value']) == false) {
175 $value = $args['value'];
176 } else {
177 // Extract the section and field name from $input_name
178 preg_match("/(.*)\[(.*)\]/", $input_name, $field_section_and_name);
179
180 if($field_section_and_name) {
181 $section_name = $field_section_and_name[1];
182 $field_name = $field_section_and_name[2];
183 $value = (isset($permalink_manager_options[$section_name][$field_name])) ? $permalink_manager_options[$section_name][$field_name] : $default;
184 } else {
185 $value = (isset($permalink_manager_options[$input_name])) ? $permalink_manager_options[$input_name] : $default;
186 }
187 }
188
189 switch($args['type']) {
190 case 'checkbox' :
191 $fields .= '<div class="checkboxes">';
192 foreach($args['choices'] as $choice_value => $checkbox_label) {
193 $checked = (is_array($value) && in_array($choice_value, $value)) ? "checked='checked'" : "";
194 $fields .= "<label for='{$input_name}[]'><input type='checkbox' {$input_class} value='{$choice_value}' name='{$input_name}[]' {$checked} /> {$checkbox_label}</label>";
195 }
196 $fields .= '</div>';
197
198 // Add helper checkboxes for bulk actions
199 if(isset($args['select_all']) || isset($args['unselect_all'])) {
200 $select_all_label = (!empty($args['select_all'])) ? $args['select_all'] : __('Select all', 'permalink-manager');
201 $unselect_all_label = (!empty($args['unselect_all'])) ? $args['unselect_all'] : __('Unselect all', 'permalink-manager');
202
203 $fields .= "<p class=\"checkbox_actions extra-links\">";
204 $fields .= (isset($args['select_all'])) ? "<a href=\"#\" class=\"select_all\">{$select_all_label}</a>&nbsp;" : "";
205 $fields .= (isset($args['unselect_all'])) ? "<a href=\"#\" class=\"unselect_all\">{$unselect_all_label}</a>" : "";
206 $fields .= "</p>";
207 }
208 break;
209
210 case 'radio' :
211 $fields .= '<div class="radios">';
212 foreach($args['choices'] as $choice_value => $checkbox_label) {
213 $checked = ($choice_value == $value) ? "checked='checked'" : "";
214 $fields .= "<label for='{$input_name}[]'><input type='radio' {$input_class} value='{$choice_value}' name='{$input_name}[]' {$checked} /> {$checkbox_label}</label>";
215 }
216 $fields .= '</div>';
217 break;
218
219 case 'select' :
220 $fields .= '<div class="select">';
221 $fields .= "<select name='{$input_name}' {$input_class}>";
222 foreach($args['choices'] as $choice_value => $checkbox_label) {
223 $selected = ($choice_value == $value) ? "selected='selected'" : "";
224 $fields .= "<option value='{$choice_value}' {$selected} />{$checkbox_label}</option>";
225 }
226 $fields .= '</select>';
227 $fields .= '</div>';
228 break;
229
230 case 'number' :
231 $fields .= "<input type='number' {$input_class} value='{$value}' name='{$input_name}' />";
232 break;
233
234 case 'textarea' :
235 $fields .= "<textarea {$input_class} name='{$input_name}' {$placeholder} {$readonly} {$rows}>{$value}</textarea>";
236 break;
237
238 case 'pre' :
239 $fields .= "<pre {$input_class}>{$value}</pre>";
240 break;
241
242 case 'clearfix' :
243 return "<div class=\"clearfix\"></div>";
244
245 default :
246 $fields .= "<input type='text' {$input_class} value='{$value}' name='{$input_name}' {$placeholder} {$readonly}/>";
247 }
248
249 // Get the final HTML output
250 if(isset($args['container']) && $args['container'] == 'tools') {
251 $output = "<div{$container_class}>";
252 $output .= "<h4>{$label}</h4>";
253 $output .= "<div class='{$input_name}-container'>{$fields}</div>";
254 $output .= $description;
255 $output .= $append_content;
256 $output .= "</div>";
257 } else if(isset($args['container']) && $args['container'] == 'row') {
258 $output = "<tr><th><label for='{$input_name}'>{$args['label']}</label></th>";
259 $output .= "<td>{$fields}{$description}</td></tr>";
260 $output .= ($append_content) ? "<tr class=\"appended-row\"><td colspan=\"2\">{$append_content}</td></tr>" : "";
261 } else {
262 $output = $fields . $append_content;
263 }
264
265 return apply_filters('permalink-manager-field-output', $output);
266 }
267
268 /**
269 * Allow to edit URIs from "Edit Post" admin pages
270 */
271 function edit_uri_box($html, $id, $new_title, $new_slug) {
272 global $post, $permalink_manager_uris;
273
274 // Do not change anything if post is not saved yet
275 if(empty($post->post_name)) return $html;
276 $default_uri = trim(str_replace(home_url("/"), "", get_permalink($id)), "/");
277 $uri = (!empty($permalink_manager_uris[$id])) ? $permalink_manager_uris[$id] : $default_uri;
278
279 $html = preg_replace("/(<strong>(.*)<\/strong>)(.*)/is", "$1 ", $html);
280 $html .= home_url("/") . " <span id=\"editable-post-name\"><input type='text' value='{$uri}' name='custom_uri'/></span>";
281 return $html;
282 }
283
284 /**
285 * Display the form
286 */
287 static public function get_the_form($fields = array(), $container = '', $button = array(), $sidebar = '', $nonce = array()) {
288 // 1. Check if the content will be displayed in columns and button details
289 switch($container) {
290 case 'columns-3' :
291 $wrapper_class = 'columns-container';
292 $form_column_class = 'column column-2_3';
293 $sidebar_class = 'column column-1_3';
294 break;
295 // there will be more cases in future ...
296 default :
297 $wrapper_class = $form_column_class = $sidebar_class = '';
298 }
299
300 // 2. Process the array with button and nonce field settings
301 $button_text = (!empty($button['text'])) ? $button['text'] : '';
302 $button_class = (!empty($button['class'])) ? $button['class'] : '';
303 $nonce_action = (!empty($nonce['action'])) ? $nonce['action'] : '';
304 $nonce_name = (!empty($nonce['name'])) ? $nonce['name'] : '';
305
306 // 2. Now get the HTML output (start section row container)
307 $output = ($wrapper_class) ? "<div class=\"{$wrapper_class}\">" : '';
308
309 // 3. Start fields' section
310 $output .= ($form_column_class) ? "<div class=\"{$form_column_class}\">" : "";
311 $output .= "<form method=\"POST\">";
312
313 // Loop through all fields assigned to this section
314 foreach($fields as $field_name => $field) {
315 $field['container'] = 'tools';
316
317 // A. Detect fields group
318 if(isset($field['group'])) {
319 $output .= "<div class=\"columns-container\">";
320 // Loop through all fields assigned to this section
321 foreach($field['group'] as $groupped_field_name => $groupped_field) {
322 $groupped_field['container'] = 'tools';
323 $output .= self::generate_option_field($groupped_field_name, $groupped_field);
324 }
325 $output .= "</div>";
326 }
327 // B. Detect section
328 else if(isset($field['section_name'])) {
329 $output .= "<h4>{$field['section_name']}</h4>";
330 $output .= (isset($section['description'])) ? "<p class=\"description\">{$field['description']}</p>" : "";
331 $output .= "<table class=\"form-table\">";
332
333 // Loop through all fields assigned to this section
334 foreach($field['fields'] as $section_field_id => $section_field) {
335 $section_field_name = "{$field_name}[$section_field_id]";
336 $section_field['container'] = 'row';
337
338 $output .= self::generate_option_field($section_field_name, $section_field);
339 }
340
341 $output .= "</table>";
342 }
343 // C. Display single field
344 else {
345 $output .= self::generate_option_field($field_name, $field);
346 }
347 }
348
349 // End the fields' section + add button & nonce fields
350 $output .= ($nonce_action && $nonce_name) ? wp_nonce_field($nonce_action, $nonce_name, true, true) : "";
351 $output .= ($button_text) ? get_submit_button($button_text, $button_class, '', false) : "";
352 $output .= '</form>';
353 $output .= ($form_column_class) ? "</div>" : "";
354
355 // 4. Display some notes
356 if($sidebar_class && $sidebar) {
357 $output .= "<div class=\"{$sidebar_class}\">";
358 $output .= "<div class=\"section-notes\">";
359 $output .= $sidebar;
360 $output .= "</div>";
361 $output .= "</div>";
362 }
363
364 // 5. End the section row container
365 $output .= ($wrapper_class) ? "</div>" : "";
366
367 return $output;
368 }
369
370 /**
371 * Display the plugin sections.
372 */
373 public function display_section() {
374 global $wpdb, $permalink_manager_before_sections_html, $permalink_manager_after_sections_html;
375
376 $output = "<div id=\"permalink-manager\" class=\"wrap\">";
377
378 // Display alerts and another content if needed and the plugin header
379 $output .= $permalink_manager_before_sections_html;
380 $output .= "<h2 id=\"plugin-name-heading\">" . PERMALINK_MANAGER_PLUGIN_NAME . " <a href=\"" . PERMALINK_MANAGER_WEBSITE ."\" target=\"_blank\">" . __('by Maciej Bis', 'permalink-manager') . "</a></h2>";
381
382 // Display the tab navigation
383 $output .= "<div id=\"permalink-manager-tab-nav\" class=\"nav-tab-wrapper\">";
384 foreach($this->sections as $section_name => $section_properties) {
385 $active_class = ($this->active_section === $section_name) ? 'nav-tab-active nav-tab' : 'nav-tab';
386 $section_url = $this->get_admin_url("&section={$section_name}");
387
388 $output .= "<a href=\"{$section_url}\" class=\"{$active_class}\">{$section_properties['name']}</a>";
389 }
390 $output .= "</div>";
391
392 // Display addidional navigation for subsections
393 if(isset($this->sections[$this->active_section]['subsections'])) {
394 $output .= "<ul class=\"subsubsub\">";
395 foreach ($this->sections[$this->active_section]['subsections'] as $subsection_name => $subsection) {
396 $active_class = ($this->active_subsection === $subsection_name) ? 'current' : '';
397 $subsection_url = $this->get_admin_url("&section={$this->active_section}&subsection={$subsection_name}");
398
399 $output .= "<li><a href=\"{$subsection_url}\" class=\"{$active_class}\">{$subsection['name']}</a></li>";
400 }
401 $output .= "</ul>";
402 }
403
404 // Now display the active section
405 $output .= "<div id=\"permalink-manager-sections\">";
406 $active_section_array = (isset($this->sections[$this->active_section])) ? $this->sections[$this->active_section] : "";
407
408 // A. Execute the function assigned to the subsection
409 if(isset($active_section_array['subsections'][$this->active_subsection]['function'])) {
410 $class_name = $active_section_array['subsections'][$this->active_subsection]['function']['class'];
411 $section_object = new $class_name();
412
413 $section_content = call_user_func(array($section_object, $active_section_array['subsections'][$this->active_subsection]['function']['method']));
414 }
415 // B. Execute the function assigned to the section
416 else if(isset($active_section_array['function'])) {
417 $class_name = $active_section_array['function']['class'];
418 $section_object = new $class_name();
419
420 $section_content = call_user_func(array($section_object, $active_section_array['function']['method']));
421 }
422 // C. Display the raw HTMl output
423 else {
424 $section_content = (isset($active_section_array['html'])) ? $active_section_array['html'] : "";
425 }
426
427 $output .= "<div data-section=\"{$this->active_section}\" id=\"{$this->active_section}\">{$section_content}</div>";
428 $output .= "</div>";
429
430 // Display alerts and another content if needed and close .wrap container
431 $output .= $permalink_manager_after_sections_html;
432 $output .= "</div>";
433
434 echo $output;
435 }
436
437 /**
438 * Display error/info message
439 */
440 static function get_alert_message($alert_content, $alert_type, $dismissable = true) {
441 $class = ($dismissable) ? "is-dismissible" : "";
442 $output = sprintf( "<div class=\"{$alert_type} notice {$class}\"> %s</div>", wpautop($alert_content) );
443
444 return $output;
445 }
446
447 /**
448 * Display the table with updated slugs after one of the actions is triggered
449 */
450 static function display_updated_slugs($updated_array) {
451 // Check if slugs should be displayed
452 $first_slug = reset($updated_array);
453
454 $header_footer = '<tr>';
455 $header_footer .= '<th class="column-primary">' . __('Title', 'permalink-manager') . '</th>';
456 $header_footer .= '<th>' . __('Old URI', 'permalink-manager') . '</th>';
457 $header_footer .= '<th>' . __('New URI', 'permalink-manager') . '</th>';
458 $header_footer .= (isset($first_slug['old_slug'])) ? '<th>' . __('Old Slug', 'permalink-manager') . '</th>' : "";
459 $header_footer .= (isset($first_slug['new_slug'])) ? '<th>' . __('New Slug', 'permalink-manager') . '</th>' : "";
460 $header_footer .= '</tr>';
461
462 $updated_slugs_count = 0;
463 $main_content = "";
464 foreach($updated_array as $row) {
465 // Odd/even class
466 $updated_slugs_count++;
467 $alternate_class = ($updated_slugs_count % 2 == 1) ? ' class="alternate"' : '';
468 $permalink = home_url("{$row['new_uri']}");
469
470 $main_content .= "<tr{$alternate_class}>";
471 $main_content .= '<td class="row-title column-primary" data-colname="' . __('Title', 'permalink-manager') . '">' . $row['post_title'] . "<a target=\"_blank\" href=\"{$permalink}\"><span class=\"small\">{$permalink}</span></a>" . '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __('Show more details', 'permalink-manager') . '</span></button></td>';
472 $main_content .= '<td data-colname="' . __('Old URI', 'permalink-manager') . '">' . $row['old_uri'] . '</td>';
473 $main_content .= '<td data-colname="' . __('New URI', 'permalink-manager') . '">' . $row['new_uri'] . '</td>';
474 $main_content .= (isset($row['old_slug'])) ? '<td data-colname="' . __('Old Slug', 'permalink-manager') . '">' . $row['old_slug'] . '</td>' : "";
475 $main_content .= (isset($row['new_slug'])) ? '<td data-colname="' . __('New Slug', 'permalink-manager') . '">' . $row['new_slug'] . '</td>' : "";
476 $main_content .= '</tr>';
477 }
478
479 // Merge header, footer and content
480 $output = '<h3 id="updated-list">' . __('List of updated items', 'permalink-manager') . '</h3>';
481 $output .= '<table class="widefat wp-list-table updated-slugs-table">';
482 $output .= "<thead>{$header_footer}</thead><tbody>{$main_content}</tbody><tfoot>{$header_footer}</tfoot>";
483 $output .= '</table>';
484
485 return $output ;
486 }
487
488 }
489