permalink-manager-actions.php
8 years ago
permalink-manager-admin-functions.php
8 years ago
permalink-manager-core-functions.php
8 years ago
permalink-manager-helper-functions.php
8 years ago
permalink-manager-third-parties.php
8 years ago
permalink-manager-uri-functions-post.php
8 years ago
permalink-manager-admin-functions.php
736 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 | add_action( 'admin_notices', array($this, 'display_plugin_notices')); |
| 17 | add_action( 'admin_notices', array($this, 'display_global_notices')); |
| 18 | add_action( 'wp_ajax_dismissed_notice_handler', array($this, 'hide_global_notice') ); |
| 19 | |
| 20 | add_filter( "default_hidden_columns" , array($this, 'quick_edit_hide_column'), 10, 2 ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Hooks that should be triggered with "admin_init" |
| 25 | */ |
| 26 | public function init() { |
| 27 | // Additional link in "Plugins" page |
| 28 | add_filter( "plugin_action_links_{$this->plugin_basename}", array($this, "plugins_page_links") ); |
| 29 | |
| 30 | // Detect current section |
| 31 | $this->sections = apply_filters('permalink-manager-sections', array()); |
| 32 | $this->get_current_section(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get current section (only in plugin sections) |
| 37 | */ |
| 38 | public function get_current_section() { |
| 39 | global $active_section, $active_subsection, $current_admin_tax; |
| 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 | // Check if current admin page is related to taxonomies |
| 66 | if(substr($this->active_subsection, 0, 4) == 'tax_') { |
| 67 | $current_admin_tax = substr($this->active_subsection, 4, strlen($this->active_subsection)); |
| 68 | } else { |
| 69 | $current_admin_tax = false; |
| 70 | } |
| 71 | |
| 72 | // Set globals |
| 73 | $active_section = $this->active_section; |
| 74 | $active_subsection = $this->active_subsection; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Add menu page. |
| 79 | */ |
| 80 | public function add_menu_page() { |
| 81 | $this->menu_name = add_management_page( __('Permalink Manager', 'permalink-manager'), __('Permalink Manager', 'permalink-manager'), 'manage_options', $this->plugin_slug, array($this, 'display_section') ); |
| 82 | |
| 83 | add_action( 'admin_init', array($this, 'enqueue_styles' ) ); |
| 84 | add_action( 'admin_init', array($this, 'enqueue_scripts' ) ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Register the CSS file for the dashboard. |
| 89 | */ |
| 90 | public function enqueue_styles() { |
| 91 | wp_enqueue_style( 'permalink-manager-plugins', PERMALINK_MANAGER_URL . '/out/permalink-manager-plugins.css', array(), PERMALINK_MANAGER_VERSION, 'all' ); |
| 92 | wp_enqueue_style( 'permalink-manager', PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.css', array('permalink-manager-plugins'), PERMALINK_MANAGER_VERSION, 'all' ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Register the JavaScript file for the dashboard. |
| 97 | */ |
| 98 | public function enqueue_scripts() { |
| 99 | wp_enqueue_script( 'permalink-manager-plugins', PERMALINK_MANAGER_URL . '/out/permalink-manager-plugins.js', array( 'jquery', ), PERMALINK_MANAGER_VERSION, false ); |
| 100 | wp_enqueue_script( 'permalink-manager', PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.js', array( 'jquery', 'permalink-manager-plugins' ), PERMALINK_MANAGER_VERSION, false ); |
| 101 | |
| 102 | wp_localize_script( 'permalink-manager', 'permalink_manager', array('url' => PERMALINK_MANAGER_URL) ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get admin url for the plugin |
| 107 | */ |
| 108 | function get_admin_url($append = '') { |
| 109 | return menu_page_url( "{$this->plugin_slug}", false ) . $append; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Additional links on "Plugins" page |
| 114 | */ |
| 115 | public function plugins_page_links($links) { |
| 116 | $links[] = sprintf('<a href="%s">%s</a>', $this->get_admin_url(), __( 'URI Editor', 'permalink-manager' )); |
| 117 | if(!defined('PERMALINK_MANAGER_PRO')) { |
| 118 | $links[] = sprintf('<a href="%s" target="_blank">%s</a>', PERMALINK_MANAGER_WEBSITE, __( 'Buy Permalink Manager Pro', 'permalink-manager' )); |
| 119 | } |
| 120 | return $links; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Generate the fields |
| 125 | */ |
| 126 | static public function generate_option_field($input_name, $args) { |
| 127 | global $permalink_manager_options; |
| 128 | |
| 129 | // Reset $fields variables |
| 130 | $fields = $section_name = $field_name = ''; |
| 131 | |
| 132 | // Allow to filter the $args |
| 133 | $args = apply_filters('permalink-manager-field-args', $args, $input_name); |
| 134 | |
| 135 | $field_type = (isset($args['type'])) ? $args['type'] : 'text'; |
| 136 | $default = (isset($args['default'])) ? $args['default'] : ''; |
| 137 | $label = (isset($args['label'])) ? $args['label'] : ''; |
| 138 | $rows = (isset($args['rows'])) ? "rows=\"{$rows}\"" : "rows=\"5\""; |
| 139 | $container_class = (isset($args['container_class'])) ? " class=\"{$args['container_class']} field-container\"" : " class=\"field-container\""; |
| 140 | $description = (isset($args['before_description'])) ? $args['before_description'] : ""; |
| 141 | $description .= (isset($args['description'])) ? "<p class=\"field-description description\">{$args['description']}</p>" : ""; |
| 142 | $description .= (isset($args['after_description'])) ? $args['after_description'] : ""; |
| 143 | $description .= (isset($args['pro'])) ? sprintf("<p class=\"field-description description alert info\">%s</p>", (Permalink_Manager_Admin_Functions::pro_text(true))) : ""; |
| 144 | $append_content = (isset($args['append_content'])) ? "{$args['append_content']}" : ""; |
| 145 | |
| 146 | // Input attributes |
| 147 | $input_atts = (isset($args['input_class'])) ? "class='{$args['input_class']}'" : ''; |
| 148 | $input_atts .= (isset($args['readonly'])) ? " readonly='readonly'" : ''; |
| 149 | $input_atts .= (isset($args['disabled'])) ? " disabled='disabled'" : ''; |
| 150 | $input_atts .= (isset($args['placeholder'])) ? " placeholder='{$args['placeholder']}'" : ''; |
| 151 | $input_atts .= (isset($args['extra_atts'])) ? " {$args['extra_atts']}" : ''; |
| 152 | |
| 153 | // Get the field value (if it is not set in $args) |
| 154 | if(isset($args['value']) && empty($args['value']) == false) { |
| 155 | $value = $args['value']; |
| 156 | } else { |
| 157 | // Extract the section and field name from $input_name |
| 158 | preg_match("/(.*)\[(.*)\]/", $input_name, $field_section_and_name); |
| 159 | |
| 160 | if($field_section_and_name) { |
| 161 | $section_name = $field_section_and_name[1]; |
| 162 | $field_name = $field_section_and_name[2]; |
| 163 | $value = (isset($permalink_manager_options[$section_name][$field_name])) ? $permalink_manager_options[$section_name][$field_name] : $default; |
| 164 | } else { |
| 165 | $value = (isset($permalink_manager_options[$input_name])) ? $permalink_manager_options[$input_name] : $default; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | switch($field_type) { |
| 170 | case 'checkbox' : |
| 171 | $fields .= '<div class="checkboxes">'; |
| 172 | foreach($args['choices'] as $choice_value => $choice) { |
| 173 | $input_template = "<label for='%s[]'><input type='checkbox' %s value='%s' name='%s[]' %s /> %s</label>"; |
| 174 | |
| 175 | if(empty($choice['label']) && is_array($choice)) { |
| 176 | foreach($choice as $sub_choice_value => $sub_choice) { |
| 177 | $label = (!empty($sub_choice['label'])) ? $sub_choice['label'] : $sub_choice; |
| 178 | $atts = (!empty($value[$choice_value]) && in_array($sub_choice_value, $value[$choice_value])) ? "checked='checked'" : ""; |
| 179 | $atts .= (!empty($sub_choice['atts'])) ? " {$sub_choice['atts']}" : ""; |
| 180 | |
| 181 | $fields .= sprintf($input_template, $input_name, $input_atts, $sub_choice_value, "{$input_name}[{$choice_value}]", $atts, $label); |
| 182 | } |
| 183 | } else { |
| 184 | $label = (!empty($choice['label'])) ? $choice['label'] : $choice; |
| 185 | $atts = (is_array($value) && in_array($choice_value, $value)) ? "checked='checked'" : ""; |
| 186 | $atts .= (!empty($choice['atts'])) ? " {$choice['atts']}" : ""; |
| 187 | |
| 188 | $fields .= sprintf($input_template, $input_name, $input_atts, $choice_value, $input_name, $atts, $label); |
| 189 | } |
| 190 | } |
| 191 | $fields .= '</div>'; |
| 192 | |
| 193 | // Add helper checkboxes for bulk actions |
| 194 | if(isset($args['select_all']) || isset($args['unselect_all'])) { |
| 195 | $select_all_label = (!empty($args['select_all'])) ? $args['select_all'] : __('Select all', 'permalink-manager'); |
| 196 | $unselect_all_label = (!empty($args['unselect_all'])) ? $args['unselect_all'] : __('Unselect all', 'permalink-manager'); |
| 197 | |
| 198 | $fields .= "<p class=\"checkbox_actions extra-links\">"; |
| 199 | $fields .= (isset($args['select_all'])) ? "<a href=\"#\" class=\"select_all\">{$select_all_label}</a> " : ""; |
| 200 | $fields .= (isset($args['unselect_all'])) ? "<a href=\"#\" class=\"unselect_all\">{$unselect_all_label}</a>" : ""; |
| 201 | $fields .= "</p>"; |
| 202 | } |
| 203 | break; |
| 204 | |
| 205 | case 'single_checkbox' : |
| 206 | $fields .= '<div class="single_checkbox">'; |
| 207 | $checked = ($value == 1) ? "checked='checked'" : ""; |
| 208 | $checkbox_label = (isset($args['checkbox_label'])) ? $args['checkbox_label'] : ''; |
| 209 | |
| 210 | $fields .= "<input type='hidden' {$input_atts} value='0' name='{$input_name}' checked=\"checked\" />"; |
| 211 | $fields .= "<label for='{$input_name}'><input type='checkbox' {$input_atts} value='1' name='{$input_name}' {$checked} /> {$checkbox_label}</label>"; |
| 212 | $fields .= '</div>'; |
| 213 | break; |
| 214 | |
| 215 | case 'radio' : |
| 216 | $fields .= '<div class="radios">'; |
| 217 | foreach($args['choices'] as $choice_value => $choice) { |
| 218 | $label = (is_array($choice)) ? $choice['label'] : $choice; |
| 219 | $atts = ($choice_value == $value) ? "checked='checked'" : ""; |
| 220 | $atts .= (!empty($choice['atts'])) ? " {$choice['atts']}" : ""; |
| 221 | |
| 222 | $fields .= "<label for='{$input_name}[]'><input type='radio' {$input_atts} value='{$choice_value}' name='{$input_name}[]' {$atts} /> {$label}</label>"; |
| 223 | } |
| 224 | $fields .= '</div>'; |
| 225 | break; |
| 226 | |
| 227 | case 'select' : |
| 228 | $fields .= '<span class="select">'; |
| 229 | $fields .= "<select name='{$input_name}' {$input_atts}>"; |
| 230 | foreach($args['choices'] as $choice_value => $choice) { |
| 231 | $label = (is_array($choice)) ? $choice['label'] : $choice; |
| 232 | $atts = ($choice_value == $value) ? "selected='selected'" : ""; |
| 233 | $atts .= (!empty($choice['atts'])) ? " {$choice['atts']}" : ""; |
| 234 | |
| 235 | $fields .= "<option value='{$choice_value}' {$atts}>{$label}</option>"; |
| 236 | } |
| 237 | $fields .= '</select>'; |
| 238 | $fields .= '</span>'; |
| 239 | break; |
| 240 | |
| 241 | case 'number' : |
| 242 | $fields .= "<input type='number' {$input_atts} value='{$value}' name='{$input_name}' />"; |
| 243 | break; |
| 244 | |
| 245 | case 'hidden' : |
| 246 | $fields .= "<input type='hidden' {$input_atts} value='{$value}' name='{$input_name}' />"; |
| 247 | break; |
| 248 | |
| 249 | case 'textarea' : |
| 250 | $fields .= "<textarea {$input_atts} name='{$input_name}' {$rows}>{$value}</textarea>"; |
| 251 | break; |
| 252 | |
| 253 | case 'pre' : |
| 254 | $fields .= "<pre {$input_atts}>{$value}</pre>"; |
| 255 | break; |
| 256 | |
| 257 | case 'info' : |
| 258 | $fields .= "<div {$input_atts}>{$value}</div>"; |
| 259 | break; |
| 260 | |
| 261 | case 'clearfix' : |
| 262 | return "<div class=\"clearfix\"></div>"; |
| 263 | |
| 264 | case 'permastruct' : |
| 265 | $siteurl = get_option('home'); |
| 266 | $fields .= "<div class=\"permastruct-container\"><span><code>{$siteurl}/</code></span><span><input type='text' {$input_atts} value='{$value}' name='{$input_name}'/></span></div>"; |
| 267 | break; |
| 268 | |
| 269 | default : |
| 270 | $fields .= "<input type='text' {$input_atts} value='{$value}' name='{$input_name}'/>"; |
| 271 | } |
| 272 | |
| 273 | // Get the final HTML output |
| 274 | if(isset($args['container']) && $args['container'] == 'tools') { |
| 275 | $html = "<div{$container_class}>"; |
| 276 | $html .= "<h4>{$label}</h4>"; |
| 277 | $html .= "<div class='{$input_name}-container'>{$fields}</div>"; |
| 278 | $html .= $description; |
| 279 | $html .= $append_content; |
| 280 | $html .= "</div>"; |
| 281 | } else if(isset($args['container']) && $args['container'] == 'row') { |
| 282 | $html = "<tr data-field=\"{$input_name}\" {$container_class}><th><label for='{$input_name}'>{$args['label']}</label></th>"; |
| 283 | $html .= "<td><fieldset>{$fields}{$description}</fieldset></td></tr>"; |
| 284 | $html .= ($append_content) ? "<tr class=\"appended-row\"><td colspan=\"2\">{$append_content}</td></tr>" : ""; |
| 285 | } else if(isset($args['container']) && $args['container'] == 'screen-options') { |
| 286 | $html = "<fieldset data-field=\"{$input_name}\" {$container_class}><legend>{$args['label']}</legend>"; |
| 287 | $html .= "<div class=\"field-content\">{$fields}{$description}</div>"; |
| 288 | $html .= ($append_content) ? "<div class=\"appended-row\">{$append_content}</div>" : ""; |
| 289 | $html .= "</fieldset>"; |
| 290 | } else { |
| 291 | $html = $fields . $append_content; |
| 292 | } |
| 293 | |
| 294 | return apply_filters('permalink-manager-field-output', $html); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Display hidden field to indicate posts or taxonomies admin sections |
| 299 | */ |
| 300 | static public function section_type_field($type = 'post') { |
| 301 | return self::generate_option_field('content_type', array('value' => $type, 'type' => 'hidden')); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Display the form |
| 306 | */ |
| 307 | static public function get_the_form($fields = array(), $container = '', $button = array(), $sidebar = '', $nonce = array(), $wrap = false) { |
| 308 | // 1. Check if the content will be displayed in columns and button details |
| 309 | switch($container) { |
| 310 | case 'columns-3' : |
| 311 | $wrapper_class = 'columns-container'; |
| 312 | $form_column_class = 'column column-2_3'; |
| 313 | $sidebar_class = 'column column-1_3'; |
| 314 | break; |
| 315 | |
| 316 | // there will be more cases in future ... |
| 317 | default : |
| 318 | $form_column_class = 'form'; |
| 319 | $sidebar_class = 'sidebar'; |
| 320 | $wrapper_class = $form_column_class = ''; |
| 321 | } |
| 322 | |
| 323 | // 2. Process the array with button and nonce field settings |
| 324 | $button_text = (!empty($button['text'])) ? $button['text'] : ''; |
| 325 | $button_class = (!empty($button['class'])) ? $button['class'] : ''; |
| 326 | $button_attributes = (!empty($button['attributes'])) ? $button['attributes'] : ''; |
| 327 | $nonce_action = (!empty($nonce['action'])) ? $nonce['action'] : ''; |
| 328 | $nonce_name = (!empty($nonce['name'])) ? $nonce['name'] : ''; |
| 329 | |
| 330 | // 2. Now get the HTML output (start section row container) |
| 331 | $html = ($wrapper_class) ? "<div class=\"{$wrapper_class}\">" : ''; |
| 332 | |
| 333 | // 3. Display some notes |
| 334 | if($sidebar_class && $sidebar) { |
| 335 | $html .= "<div class=\"{$sidebar_class}\">"; |
| 336 | $html .= "<div class=\"section-notes\">"; |
| 337 | $html .= $sidebar; |
| 338 | $html .= "</div>"; |
| 339 | $html .= "</div>"; |
| 340 | } |
| 341 | |
| 342 | // 4. Start fields' section |
| 343 | $html .= ($form_column_class) ? "<div class=\"{$form_column_class}\">" : ""; |
| 344 | $html .= "<form method=\"POST\">"; |
| 345 | $html .= ($wrap) ? "<table class=\"form-table\">" : ""; |
| 346 | |
| 347 | // Loop through all fields assigned to this section |
| 348 | foreach($fields as $field_name => $field) { |
| 349 | $field_name = (!empty($field['name'])) ? $field['name'] : $field_name; |
| 350 | |
| 351 | // A. Display table row |
| 352 | if(isset($field['container']) && $field['container'] == 'row') { |
| 353 | $row_output = ""; |
| 354 | |
| 355 | // Loop through all fields assigned to this section |
| 356 | if(isset($field['fields'])) { |
| 357 | foreach($field['fields'] as $section_field_id => $section_field) { |
| 358 | $section_field_name = (!empty($section_field['name'])) ? $section_field['name'] : "{$field_name}[$section_field_id]"; |
| 359 | $section_field['container'] = 'row'; |
| 360 | |
| 361 | $row_output .= self::generate_option_field($section_field_name, $section_field); |
| 362 | } |
| 363 | } else { |
| 364 | $row_output .= self::generate_option_field($field_name, $field); |
| 365 | } |
| 366 | |
| 367 | if(isset($field['section_name'])) { |
| 368 | $html .= "<h3>{$field['section_name']}</h3>"; |
| 369 | $html .= (isset($field['append_content'])) ? $field['append_content'] : ""; |
| 370 | $html .= (isset($field['description'])) ? "<p class=\"description\">{$field['description']}</p>" : ""; |
| 371 | $html .= "<table class=\"form-table\" data-field=\"{$field_name}\">{$row_output}</table>"; |
| 372 | } else { |
| 373 | $html .= $row_output; |
| 374 | } |
| 375 | } |
| 376 | // B. Display single field |
| 377 | else { |
| 378 | $html .= self::generate_option_field($field_name, $field); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | $html .= ($wrap) ? "</table>" : ""; |
| 383 | |
| 384 | // End the fields' section + add button & nonce fields |
| 385 | $html .= ($nonce_action && $nonce_name) ? wp_nonce_field($nonce_action, $nonce_name, true, true) : ""; |
| 386 | $html .= ($button_text) ? get_submit_button($button_text, $button_class, '', false, $button_attributes) : ""; |
| 387 | $html .= '</form>'; |
| 388 | $html .= ($form_column_class) ? "</div>" : ""; |
| 389 | |
| 390 | // 5. End the section row container |
| 391 | $html .= ($wrapper_class) ? "</div>" : ""; |
| 392 | |
| 393 | return $html; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Display the plugin sections. |
| 398 | */ |
| 399 | public function display_section() { |
| 400 | global $wpdb, $permalink_manager_before_sections_html, $permalink_manager_after_sections_html; |
| 401 | |
| 402 | $html = "<div id=\"permalink-manager\" class=\"wrap\">"; |
| 403 | |
| 404 | $donate_link = sprintf("<a href=\"%s\" target=\"_blank\" class=\"page-title-action\">%s</a>", PERMALINK_MANAGER_DONATE, __("Donate", "permalink-manager")); |
| 405 | $html .= sprintf("<h2 id=\"plugin-name-heading\">%s <a href=\"http://maciejbis.net\" class=\"author-link\" target=\"_blank\">%s</a> %s</h2>", PERMALINK_MANAGER_PLUGIN_NAME, __("by Maciej Bis", "permalink-manager"), $donate_link); |
| 406 | |
| 407 | // Display the tab navigation |
| 408 | $html .= "<div id=\"permalink-manager-tab-nav\" class=\"nav-tab-wrapper\">"; |
| 409 | foreach($this->sections as $section_name => $section_properties) { |
| 410 | $active_class = ($this->active_section === $section_name) ? 'nav-tab-active nav-tab' : 'nav-tab'; |
| 411 | $section_url = $this->get_admin_url("§ion={$section_name}"); |
| 412 | |
| 413 | $html .= "<a href=\"{$section_url}\" class=\"{$active_class} section_{$section_name}\">{$section_properties['name']}</a>"; |
| 414 | } |
| 415 | $html .= "</div>"; |
| 416 | |
| 417 | // Now display the active section |
| 418 | $html .= "<div id=\"permalink-manager-sections\">"; |
| 419 | $active_section_array = (isset($this->sections[$this->active_section])) ? $this->sections[$this->active_section] : ""; |
| 420 | |
| 421 | // Display addidional navigation for subsections |
| 422 | if(isset($this->sections[$this->active_section]['subsections'])) { |
| 423 | $html .= "<ul class=\"subsubsub\">"; |
| 424 | foreach ($this->sections[$this->active_section]['subsections'] as $subsection_name => $subsection) { |
| 425 | $active_class = ($this->active_subsection === $subsection_name) ? 'current' : ''; |
| 426 | $subsection_url = $this->get_admin_url("§ion={$this->active_section}&subsection={$subsection_name}"); |
| 427 | |
| 428 | $html .= "<li><a href=\"{$subsection_url}\" class=\"{$active_class}\">{$subsection['name']}</a></li>"; |
| 429 | } |
| 430 | $html .= "</ul>"; |
| 431 | } |
| 432 | |
| 433 | // A. Execute the function assigned to the subsection |
| 434 | if(isset($active_section_array['subsections'][$this->active_subsection]['function'])) { |
| 435 | $class_name = $active_section_array['subsections'][$this->active_subsection]['function']['class']; |
| 436 | $section_object = new $class_name(); |
| 437 | |
| 438 | $section_content = call_user_func(array($section_object, $active_section_array['subsections'][$this->active_subsection]['function']['method'])); |
| 439 | } |
| 440 | // B. Execute the function assigned to the section |
| 441 | else if(isset($active_section_array['function'])) { |
| 442 | $class_name = $active_section_array['function']['class']; |
| 443 | $section_object = new $class_name(); |
| 444 | |
| 445 | $section_content = call_user_func(array($section_object, $active_section_array['function']['method'])); |
| 446 | } |
| 447 | // C. Display the raw HTMl output of subsection |
| 448 | else if(isset($active_section_array['subsections'][$this->active_subsection]['html'])) { |
| 449 | $section_content = (isset($active_section_array['subsections'][$this->active_subsection]['html'])) ? $active_section_array['subsections'][$this->active_subsection]['html'] : ""; |
| 450 | } |
| 451 | // D. Try to display the raw HTMl output of section |
| 452 | else { |
| 453 | $section_content = (isset($active_section_array['html'])) ? $active_section_array['html'] : ""; |
| 454 | } |
| 455 | |
| 456 | $html .= "<div class=\"single-section\" data-section=\"{$this->active_section}\" id=\"{$this->active_section}\">{$section_content}</div>"; |
| 457 | $html .= "</div>"; |
| 458 | |
| 459 | // Display alerts and another content if needed and close .wrap container |
| 460 | $html .= $permalink_manager_after_sections_html; |
| 461 | $html .= "</div>"; |
| 462 | |
| 463 | echo $html; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Display error/info message |
| 468 | */ |
| 469 | public static function get_alert_message($alert_content = "", $alert_type = "", $dismissable = true, $id = false) { |
| 470 | // Ignore empty messages (just in case) |
| 471 | if(empty($alert_content) || empty($alert_type)) { |
| 472 | return ""; |
| 473 | } |
| 474 | |
| 475 | $class = ($dismissable) ? "is-dismissible" : ""; |
| 476 | $alert_id = ($id) ? " data-alert_id=\"{$id}\"" : ""; |
| 477 | |
| 478 | $html = sprintf( "<div class=\"{$alert_type} permalink-manager-notice notice {$class}\"{$alert_id}> %s</div>", wpautop($alert_content) ); |
| 479 | |
| 480 | return $html; |
| 481 | } |
| 482 | |
| 483 | static function pro_text($text_only = false) { |
| 484 | $text = sprintf(__('This functionality is available only in <a href="%s" target="_blank">Permalink Manager Pro</a>.', 'permalink-manager'), PERMALINK_MANAGER_WEBSITE); |
| 485 | |
| 486 | return ($text_only) ? $text : sprintf("<div class=\"alert info\"> %s</div>", wpautop($text, 'alert', false)); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Help tooltip |
| 491 | */ |
| 492 | static function help_tooltip($text = '') { |
| 493 | $html = " <a href=\"#\" title=\"{$text}\" class=\"help_tooltip\"><span class=\"dashicons dashicons-editor-help\"></span></a>"; |
| 494 | return $html; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Display the table with updated slugs after one of the actions is triggered |
| 499 | */ |
| 500 | static function display_updated_slugs($updated_array, $uri_type = 'post') { |
| 501 | // Check if slugs should be displayed |
| 502 | $first_slug = reset($updated_array); |
| 503 | |
| 504 | $header_footer = '<tr>'; |
| 505 | $header_footer .= '<th class="column-primary">' . __('Title', 'permalink-manager') . '</th>'; |
| 506 | $header_footer .= '<th>' . __('Old URI', 'permalink-manager') . '</th>'; |
| 507 | $header_footer .= '<th>' . __('New URI', 'permalink-manager') . '</th>'; |
| 508 | $header_footer .= (isset($first_slug['old_slug'])) ? '<th>' . __('Old Slug', 'permalink-manager') . '</th>' : ""; |
| 509 | $header_footer .= (isset($first_slug['new_slug'])) ? '<th>' . __('New Slug', 'permalink-manager') . '</th>' : ""; |
| 510 | $header_footer .= '</tr>'; |
| 511 | |
| 512 | $updated_slugs_count = 0; |
| 513 | $main_content = ""; |
| 514 | foreach($updated_array as $row) { |
| 515 | // Odd/even class |
| 516 | $updated_slugs_count++; |
| 517 | $alternate_class = ($updated_slugs_count % 2 == 1) ? ' class="alternate"' : ''; |
| 518 | |
| 519 | // Taxonomy |
| 520 | if(!empty($row['tax'])) { |
| 521 | $term_link = get_term_link(intval($row['ID']), $row['tax']); |
| 522 | $permalink = (is_wp_error($term_link)) ? "-" : $term_link; |
| 523 | } else { |
| 524 | $permalink = get_permalink($row['ID']); |
| 525 | } |
| 526 | |
| 527 | $main_content .= "<tr{$alternate_class}>"; |
| 528 | $main_content .= '<td class="row-title column-primary" data-colname="' . __('Title', 'permalink-manager') . '">' . $row['item_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>'; |
| 529 | $main_content .= '<td data-colname="' . __('Old URI', 'permalink-manager') . '">' . urldecode($row['old_uri']) . '</td>'; |
| 530 | $main_content .= '<td data-colname="' . __('New URI', 'permalink-manager') . '">' . urldecode($row['new_uri']) . '</td>'; |
| 531 | $main_content .= (isset($row['old_slug'])) ? '<td data-colname="' . __('Old Slug', 'permalink-manager') . '">' . urldecode($row['old_slug']) . '</td>' : ""; |
| 532 | $main_content .= (isset($row['new_slug'])) ? '<td data-colname="' . __('New Slug', 'permalink-manager') . '">' . urldecode($row['new_slug']) . '</td>' : ""; |
| 533 | $main_content .= '</tr>'; |
| 534 | } |
| 535 | |
| 536 | // Merge header, footer and content |
| 537 | $html = '<h3 id="updated-list">' . __('List of updated items', 'permalink-manager') . '</h3>'; |
| 538 | $html .= '<table class="widefat wp-list-table updated-slugs-table">'; |
| 539 | $html .= "<thead>{$header_footer}</thead><tbody>{$main_content}</tbody><tfoot>{$header_footer}</tfoot>"; |
| 540 | $html .= '</table>'; |
| 541 | |
| 542 | return $html; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * "Quick Edit" Box |
| 547 | */ |
| 548 | public static function quick_edit_column_form($is_taxonomy = false) { |
| 549 | $html = Permalink_Manager_Admin_Functions::generate_option_field('permalink-manager-quick-edit', array('value' => true, 'type' => 'hidden')); |
| 550 | $html .= "<fieldset class=\"inline-edit-permalink\">"; |
| 551 | $html .= sprintf("<legend class=\"inline-edit-legend\">%s</legend>", __("Permalink Manager", "permalink-manager")); |
| 552 | |
| 553 | $html .= "<div class=\"inline-edit-col\">"; |
| 554 | $html .= sprintf("<label class=\"inline-edit-group\"><span class=\"title\">%s</span><span class=\"input-text-wrap\">%s</span></label>", |
| 555 | __("Current URI", "permalink-manager"), |
| 556 | Permalink_Manager_Admin_Functions::generate_option_field("custom_uri", array("input_class" => "custom_uri", "value" => '')) |
| 557 | ); |
| 558 | $html .= "</div>"; |
| 559 | |
| 560 | $html .= "</fieldset>"; |
| 561 | |
| 562 | // Append nonce field |
| 563 | $html .= wp_nonce_field( 'permalink-manager-edit-uri-box', 'permalink-manager-nonce', true, false ); |
| 564 | |
| 565 | return $html; |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Display "Permalink Manager" box |
| 570 | */ |
| 571 | public static function display_uri_box($element, $default_uri, $uri, $native_uri, $home_with_prefix) { |
| 572 | global $permalink_manager_options; |
| 573 | |
| 574 | if(!empty($element->ID)) { |
| 575 | $id = $element->ID; |
| 576 | |
| 577 | // Auto-update settings |
| 578 | $auto_update_val = get_post_meta($id, "auto_update_uri", true); |
| 579 | $auto_update_def_val = $permalink_manager_options["general"]["auto_update_uris"]; |
| 580 | $auto_update_def_label = ($auto_update_def_val) ? __("Yes", "permalink-manager") : __("No", "permalink-manager"); |
| 581 | $auto_update_choices = array( |
| 582 | 0 => array("label" => sprintf(__("Use global settings [%s]", "permalink-manager"), $auto_update_def_label), "atts" => "data-auto-update=\"{$auto_update_def_val}\""), |
| 583 | -1 => array("label" => __("No", "permalink-manager"), "atts" => "data-auto-update=\"0\""), |
| 584 | 1 => array("label" => __("Yes", "permalink-manager"), "atts" => "data-auto-update=\"1\"") |
| 585 | ); |
| 586 | } else { |
| 587 | $id = $element->term_id; |
| 588 | } |
| 589 | |
| 590 | // Decode default URI |
| 591 | $default_uri = urldecode($default_uri); |
| 592 | |
| 593 | // 1. Button |
| 594 | $html = sprintf("<span><button type=\"button\" class=\"button button-small hide-if-no-js\" id=\"permalink-manager-toggle\">%s</button></span>", __("Permalink Manager", "permalink-manager")); |
| 595 | |
| 596 | $html .= "<div id=\"permalink-manager\" class=\"postbox permalink-manager-edit-uri-box\" style=\"display: none;\">"; |
| 597 | |
| 598 | // 2. The heading |
| 599 | $html .= "<a class=\"close-button\"><span class=\"screen-reader-text\">" . __("Close: ", "permalink-manager") . __("Permalink Manager", "permalink-manager") . "</span><span class=\"close-icon\" aria-hidden=\"false\"></span></a>"; |
| 600 | $html .= sprintf("<h2><span>%s</span></h2>", __("Permalink Manager", "permalink-manager")); |
| 601 | |
| 602 | // 3. The fields container [start] |
| 603 | $html .= "<div class=\"inside\">"; |
| 604 | |
| 605 | // 4. Custom URI |
| 606 | $html .= sprintf("<div><label for=\"custom_uri\" class=\"strong\">%s %s</label><span>%s</span></div>", |
| 607 | __("Current URI", "permalink-manager"), |
| 608 | ($element->ID) ? Permalink_Manager_Admin_Functions::help_tooltip(__("The custom URI can be edited only if 'Auto-update the URI' feature is not enabled.", "permalink-manager")) : "", |
| 609 | Permalink_Manager_Admin_Functions::generate_option_field("custom_uri", array("extra_atts" => "data-default=\"{$default_uri}\"", "input_class" => "widefat custom_uri", "value" => urldecode($uri))) |
| 610 | ); |
| 611 | |
| 612 | // 5. Auto-update URI |
| 613 | if(!empty($auto_update_choices)) { |
| 614 | $html .= sprintf("<div><label for=\"auto_auri\" class=\"strong\">%s %s</label><span>%s</span></div>", |
| 615 | __("Auto-update the URI", "permalink-manager"), |
| 616 | Permalink_Manager_Admin_Functions::help_tooltip(__("If enabled, the 'Current URI' field will be automatically changed to 'Default URI' (displayed below) after the post is saved or updated.", "permalink-manager")), |
| 617 | Permalink_Manager_Admin_Functions::generate_option_field("auto_update_uri", array("type" => "select", "input_class" => "widefat auto_update", "value" => $auto_update_val, "choices" => $auto_update_choices)) |
| 618 | ); |
| 619 | } |
| 620 | |
| 621 | // 6. Default URI |
| 622 | $html .= sprintf( |
| 623 | "<div class=\"default-permalink-row columns-container\"><span class=\"column-3_4\"><strong>%s:</strong> %s</span><span class=\"column-1_4\"><a href=\"#\" class=\"restore-default\"><span class=\"dashicons dashicons-image-rotate\"></span> %s</a></span></div>", |
| 624 | __("Default URI", "permalink-manager"), esc_html($default_uri), |
| 625 | __("Restore to Default URI", "permalink-manager") |
| 626 | ); |
| 627 | |
| 628 | // 7. Native URI info |
| 629 | if(!empty($permalink_manager_options['general']['redirect']) && ((!empty($element->post_status) && in_array($element->post_status, array('auto-draft', 'trash', 'draft'))) == false)) { |
| 630 | $html .= sprintf( |
| 631 | "<div class=\"default-permalink-row columns-container\"><span><strong>%s</strong> <a href=\"%s\">%s</a></span></div>", |
| 632 | __("Automatic redirect for native URI enabled:", "permalink-manager"), |
| 633 | "{$home_with_prefix}{$native_uri}", |
| 634 | urldecode($native_uri) |
| 635 | ); |
| 636 | } |
| 637 | |
| 638 | // 8. Custom redirects |
| 639 | $html .= ($element->ID) ? self::display_redirect_panel($id) : self::display_redirect_panel("tax-{$id}"); |
| 640 | |
| 641 | $html .= "</div>"; |
| 642 | $html .= "</div>"; |
| 643 | |
| 644 | // 9. Append nonce field |
| 645 | $html .= wp_nonce_field('permalink-manager-edit-uri-box', 'permalink-manager-nonce', true, false); |
| 646 | |
| 647 | return $html; |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * Display the redirect panel |
| 652 | */ |
| 653 | public static function display_redirect_panel($element_id) { |
| 654 | global $permalink_manager_options, $permalink_manager_redirects; |
| 655 | |
| 656 | // Heading |
| 657 | $html = sprintf( |
| 658 | "<div class=\"permalink-manager redirects-row redirects-panel columns-container\"><div class=\"heading\"><span class=\"dashicons dashicons-redo\"></span> <a href=\"#\" id=\"toggle-redirect-panel\">%s</a></span></div>", |
| 659 | __("Add Extra Redirects", "permalink-manager") |
| 660 | ); |
| 661 | |
| 662 | $html .= "<div id=\"redirect-panel-inside\">"; |
| 663 | |
| 664 | // Table |
| 665 | if(class_exists('Permalink_Manager_Pro_Addons')) { |
| 666 | $html .= Permalink_Manager_Pro_Addons::display_redirect_form($element_id); |
| 667 | } else { |
| 668 | $html .= self::pro_text(true); |
| 669 | } |
| 670 | |
| 671 | $html .= "</div></div>"; |
| 672 | |
| 673 | return $html; |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Hide "Custom URI" column |
| 678 | */ |
| 679 | function quick_edit_hide_column($hidden, $screen) { |
| 680 | $hidden[] = 'permalink-manager-col'; |
| 681 | return $hidden; |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Display global notices (throughout wp-admin dashboard) |
| 686 | */ |
| 687 | function display_global_notices() { |
| 688 | global $permalink_manager_alerts, $active_section; |
| 689 | |
| 690 | $html = ""; |
| 691 | if(!empty($permalink_manager_alerts) && is_array($permalink_manager_alerts)) { |
| 692 | foreach($permalink_manager_alerts as $alert_id => $alert) { |
| 693 | if(!empty($alert['show'])) { |
| 694 | // Hide notice in Permalink Manager Pro |
| 695 | if(defined('PERMALINK_MANAGER_PRO') && $alert['show'] == 'pro_hide') { continue; } |
| 696 | |
| 697 | // Display the notice only on the plugin pages |
| 698 | if(empty($active_section) && !empty($alert['plugin_only'])) { continue; } |
| 699 | |
| 700 | // Check if the notice did not expire |
| 701 | if(isset($alert['until']) && (time() > strtotime($alert['until']))) { continue; } |
| 702 | |
| 703 | $html .= self::get_alert_message($alert['txt'], $alert['type'], true, $alert_id); |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | echo $html; |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Hide global notices (AJAX) |
| 713 | */ |
| 714 | function hide_global_notice() { |
| 715 | global $permalink_manager_alerts; |
| 716 | |
| 717 | // Get the ID of the alert |
| 718 | $alert_id = (!empty($_REQUEST['alert_id'])) ? sanitize_title($_REQUEST['alert_id']) : ""; |
| 719 | if(!empty($permalink_manager_alerts[$alert_id])) { |
| 720 | $permalink_manager_alerts[$alert_id]['show'] = 0; |
| 721 | } |
| 722 | |
| 723 | update_option( 'permalink-manager-alerts', $permalink_manager_alerts); |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Display notices generated by Permalink Manager tools |
| 728 | */ |
| 729 | function display_plugin_notices() { |
| 730 | global $permalink_manager_before_sections_html; |
| 731 | |
| 732 | echo $permalink_manager_before_sections_html; |
| 733 | } |
| 734 | |
| 735 | } |
| 736 |