permalink-manager-actions.php
5 years ago
permalink-manager-admin-functions.php
5 years ago
permalink-manager-core-functions.php
5 years ago
permalink-manager-debug.php
5 years ago
permalink-manager-gutenberg.php
5 years ago
permalink-manager-helper-functions.php
5 years ago
permalink-manager-language-plugins.php
5 years ago
permalink-manager-third-parties.php
5 years ago
permalink-manager-uri-functions-post.php
5 years ago
permalink-manager-uri-functions.php
5 years ago
permalink-manager-admin-functions.php
1056 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_basename = PERMALINK_MANAGER_BASENAME; |
| 10 | |
| 11 | public function __construct() { |
| 12 | add_action( 'admin_menu', array($this, 'add_menu_page') ); |
| 13 | add_action( 'admin_init', array($this, 'init') ); |
| 14 | add_action( 'admin_bar_menu', array($this, 'fix_customize_url'), 41); |
| 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'), 99, 2 ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Hooks that should be triggered with "admin_init" |
| 25 | */ |
| 26 | public function init() { |
| 27 | // Additional links in "Plugins" page |
| 28 | add_filter( "plugin_action_links_{$this->plugin_basename}", array($this, "plugins_page_links") ); |
| 29 | add_filter( "plugin_row_meta", array($this, "plugins_page_meta"), 10, 2 ); |
| 30 | |
| 31 | // Detect current section |
| 32 | $this->sections = apply_filters('permalink_manager_sections', array()); |
| 33 | $this->get_current_section(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Fix Customize URL |
| 38 | */ |
| 39 | public function fix_customize_url($wp_admin_bar) { |
| 40 | $object = get_queried_object(); |
| 41 | |
| 42 | $customize = $wp_admin_bar->get_node('customize'); |
| 43 | |
| 44 | if(empty($customize->href)) { return; } |
| 45 | |
| 46 | $_REQUEST['customize_url'] = true; |
| 47 | if(!empty($object->ID)) { |
| 48 | $new_url = get_permalink($object->ID); |
| 49 | } else if(!empty($object->term_id)) { |
| 50 | |
| 51 | } |
| 52 | $_REQUEST['customize_url'] = false; |
| 53 | |
| 54 | if(!empty($new_url)) { |
| 55 | $new_url = urlencode_deep($new_url); |
| 56 | $customize_url = preg_replace('/url=([^&]+)/', "url={$new_url}", $customize->href); |
| 57 | |
| 58 | $wp_admin_bar->add_node(array( |
| 59 | 'id' => 'customize', |
| 60 | 'href' => $customize_url, |
| 61 | )); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get current section (only in plugin sections) |
| 67 | */ |
| 68 | public function get_current_section() { |
| 69 | global $active_section, $active_subsection, $current_admin_tax; |
| 70 | |
| 71 | // 1. Get current section |
| 72 | if(isset($_GET['page']) && $_GET['page'] == PERMALINK_MANAGER_PLUGIN_SLUG) { |
| 73 | if(isset($_POST['section'])) { |
| 74 | $this->active_section = $_POST['section']; |
| 75 | } else if(isset($_GET['section'])) { |
| 76 | $this->active_section = $_GET['section']; |
| 77 | } else { |
| 78 | $sections_names = array_keys($this->sections); |
| 79 | $this->active_section = $sections_names[0]; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // 2. Get current subsection |
| 84 | if($this->active_section && isset($this->sections[$this->active_section]['subsections'])) { |
| 85 | if(isset($_POST['subsection'])) { |
| 86 | $this->active_subsection = $_POST['subsection']; |
| 87 | } else if(isset($_GET['subsection'])) { |
| 88 | $this->active_subsection = $_GET['subsection']; |
| 89 | } else { |
| 90 | $subsections_names = array_keys($this->sections[$this->active_section]['subsections']); |
| 91 | $this->active_subsection = $subsections_names[0]; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Check if current admin page is related to taxonomies |
| 96 | if(substr($this->active_subsection, 0, 4) == 'tax_') { |
| 97 | $current_admin_tax = substr($this->active_subsection, 4, strlen($this->active_subsection)); |
| 98 | } else { |
| 99 | $current_admin_tax = false; |
| 100 | } |
| 101 | |
| 102 | // Set globals |
| 103 | $active_section = $this->active_section; |
| 104 | $active_subsection = $this->active_subsection; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Add menu page. |
| 109 | */ |
| 110 | public function add_menu_page() { |
| 111 | $this->menu_name = add_management_page( __('Permalink Manager', 'permalink-manager'), __('Permalink Manager', 'permalink-manager'), 'manage_options', PERMALINK_MANAGER_PLUGIN_SLUG, array($this, 'display_section') ); |
| 112 | |
| 113 | add_action( 'admin_init', array($this, 'enqueue_styles' ) ); |
| 114 | add_action( 'admin_init', array($this, 'enqueue_scripts' ) ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Register the CSS file for the dashboard. |
| 119 | */ |
| 120 | public function enqueue_styles() { |
| 121 | wp_enqueue_style( 'permalink-manager-plugins', PERMALINK_MANAGER_URL . '/out/permalink-manager-plugins.css', array(), PERMALINK_MANAGER_VERSION, 'all' ); |
| 122 | wp_enqueue_style( 'permalink-manager', PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.css', array('permalink-manager-plugins'), PERMALINK_MANAGER_VERSION, 'all' ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Register the JavaScript file for the dashboard. |
| 127 | */ |
| 128 | public function enqueue_scripts() { |
| 129 | wp_enqueue_script( 'permalink-manager-plugins', PERMALINK_MANAGER_URL . '/out/permalink-manager-plugins.js', array( 'jquery', ), PERMALINK_MANAGER_VERSION, false ); |
| 130 | wp_enqueue_script( 'permalink-manager', PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.js', array( 'jquery', 'permalink-manager-plugins' ), PERMALINK_MANAGER_VERSION, false ); |
| 131 | |
| 132 | wp_localize_script( 'permalink-manager', 'permalink_manager', array( |
| 133 | 'ajax_url' => admin_url('admin-ajax.php'), |
| 134 | 'url' => PERMALINK_MANAGER_URL, |
| 135 | 'confirm' => __('Are you sure? This action cannot be undone!', 'permalink-manager'), |
| 136 | 'spinners' => admin_url('images')) |
| 137 | ); |
| 138 | |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get admin url for the plugin |
| 143 | */ |
| 144 | public static function get_admin_url($append = '') { |
| 145 | //return menu_page_url(PERMALINK_MANAGER_PLUGIN_SLUG, false) . $append; |
| 146 | $admin_page = sprintf("tools.php?page=%s", PERMALINK_MANAGER_PLUGIN_SLUG . $append); |
| 147 | |
| 148 | return admin_url($admin_page); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Additional links on "Plugins" page |
| 153 | */ |
| 154 | public function plugins_page_links($links) { |
| 155 | $new_links = array( |
| 156 | sprintf('<a href="%s">%s</a>', $this->get_admin_url(), __( 'URI Editor', 'permalink-manager' )), |
| 157 | sprintf('<a href="%s">%s</a>', $this->get_admin_url('§ion=settings'), __( 'Settings', 'permalink-manager' )), |
| 158 | ); |
| 159 | |
| 160 | return array_merge($links, $new_links); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Additional meta links on "Plugins" page |
| 165 | */ |
| 166 | public function plugins_page_meta($links, $file) { |
| 167 | if($file == PERMALINK_MANAGER_BASENAME) { |
| 168 | $new_links = array( |
| 169 | 'doc' => sprintf('<a href="%s?utm_source=plugin_admin_page" target="_blank">%s</a>', 'https://permalinkmanager.pro/docs/', __( 'Documentation', 'permalink-manager' )) |
| 170 | ); |
| 171 | |
| 172 | if(!defined('PERMALINK_MANAGER_PRO')) { |
| 173 | $new_links['upgrade'] = sprintf('<a href="%s?utm_source=plugin_admin_page" target="_blank"><strong>%s</strong></a>', PERMALINK_MANAGER_WEBSITE, __( 'Buy Permalink Manager Pro', 'permalink-manager' )); |
| 174 | } |
| 175 | |
| 176 | $links = array_merge($links, $new_links); |
| 177 | } |
| 178 | |
| 179 | return $links; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Generate the fields |
| 184 | */ |
| 185 | static public function generate_option_field($input_name, $args) { |
| 186 | global $permalink_manager_options, $permalink_manager_permastructs; |
| 187 | |
| 188 | // Reset $fields variables |
| 189 | $fields = $section_name = $field_name = ''; |
| 190 | |
| 191 | // Allow to filter the $args |
| 192 | $args = apply_filters('permalink_manager_field_args', $args, $input_name); |
| 193 | |
| 194 | $field_type = (isset($args['type'])) ? $args['type'] : 'text'; |
| 195 | $default = (isset($args['default'])) ? $args['default'] : ''; |
| 196 | $label = (isset($args['label'])) ? $args['label'] : ''; |
| 197 | $rows = (isset($args['rows'])) ? "rows=\"{$rows}\"" : "rows=\"5\""; |
| 198 | $description = (isset($args['before_description'])) ? $args['before_description'] : ""; |
| 199 | $description .= (isset($args['description'])) ? "<p class=\"field-description description\">{$args['description']}</p>" : ""; |
| 200 | $description .= (isset($args['after_description'])) ? $args['after_description'] : ""; |
| 201 | $description .= (isset($args['pro'])) ? sprintf("<p class=\"field-description description alert info\">%s</p>", (self::pro_text(true))) : ""; |
| 202 | $append_content = (isset($args['append_content'])) ? "{$args['append_content']}" : ""; |
| 203 | |
| 204 | // Input attributes |
| 205 | $input_atts = (isset($args['input_class'])) ? "class='{$args['input_class']}'" : ''; |
| 206 | $input_atts .= (isset($args['readonly'])) ? " readonly='readonly'" : ''; |
| 207 | $input_atts .= (isset($args['disabled'])) ? " disabled='disabled'" : ''; |
| 208 | $input_atts .= (isset($args['placeholder'])) ? " placeholder='{$args['placeholder']}'" : ''; |
| 209 | $input_atts .= (isset($args['extra_atts'])) ? " {$args['extra_atts']}" : ''; |
| 210 | |
| 211 | // Display the field if the related class exists |
| 212 | if(!empty($args['class_exists'])) { |
| 213 | $related_classes = (array) $args['class_exists']; |
| 214 | $related_classes_exist = 0; |
| 215 | |
| 216 | foreach($related_classes as $related_class) { |
| 217 | if(class_exists($related_class)) { |
| 218 | $related_classes_exist = 1; |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // Do not display if the related class it not found |
| 224 | if(empty($related_classes_exist)) { |
| 225 | $field_type = $args['container_class'] = 'hidden'; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // Check the container classes |
| 230 | $container_class = (isset($args['container_class'])) ? " class=\"{$args['container_class']} field-container\"" : " class=\"field-container\""; |
| 231 | |
| 232 | // Get the field value (if it is not set in $args) |
| 233 | if(isset($args['value']) && empty($args['value']) == false) { |
| 234 | $value = $args['value']; |
| 235 | } else { |
| 236 | // Extract the section and field name from $input_name |
| 237 | preg_match('/([^\[]+)(?:\[([^\[]+)\])(?:\[([^\[]+)\])?/', $input_name, $field_section_and_name); |
| 238 | |
| 239 | if($field_section_and_name) { |
| 240 | $section_name = $field_section_and_name[1]; |
| 241 | if(!empty($field_section_and_name[3])) { |
| 242 | $field_name = $field_section_and_name[2]; |
| 243 | $subsection_name = $field_section_and_name[3]; |
| 244 | $value = (isset($permalink_manager_options[$section_name][$field_name][$subsection_name])) ? $permalink_manager_options[$section_name][$field_name][$subsection_name] : $default; |
| 245 | } else { |
| 246 | $field_name = $field_section_and_name[2]; |
| 247 | $value = (isset($permalink_manager_options[$section_name][$field_name])) ? $permalink_manager_options[$section_name][$field_name] : $default; |
| 248 | } |
| 249 | } else { |
| 250 | $value = (isset($permalink_manager_options[$input_name])) ? $permalink_manager_options[$input_name] : $default; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | switch($field_type) { |
| 255 | case 'checkbox' : |
| 256 | $fields .= '<div class="checkboxes">'; |
| 257 | foreach($args['choices'] as $choice_value => $choice) { |
| 258 | $input_template = "<label for='%s[]'><input type='checkbox' %s value='%s' name='%s[]' %s /> %s</label>"; |
| 259 | |
| 260 | if(empty($choice['label']) && is_array($choice)) { |
| 261 | if(in_array($choice_value, array('post_types', 'taxonomies'))) { |
| 262 | $group_labela = array('post_types' => __('Post types', 'permalink-manager'), 'taxonomies' => __('Taxonomies', 'permalink-manager')); |
| 263 | $fields .= sprintf('<p>%s</p>', $group_labela[$choice_value]); |
| 264 | } |
| 265 | |
| 266 | foreach($choice as $sub_choice_value => $sub_choice) { |
| 267 | $label = (!empty($sub_choice['label'])) ? $sub_choice['label'] : $sub_choice; |
| 268 | $atts = (!empty($value[$choice_value]) && in_array($sub_choice_value, $value[$choice_value])) ? "checked='checked'" : ""; |
| 269 | $atts .= (!empty($sub_choice['atts'])) ? " {$sub_choice['atts']}" : ""; |
| 270 | |
| 271 | $fields .= sprintf($input_template, $input_name, $input_atts, $sub_choice_value, "{$input_name}[{$choice_value}]", $atts, $label); |
| 272 | } |
| 273 | } else { |
| 274 | $label = (!empty($choice['label'])) ? $choice['label'] : $choice; |
| 275 | $atts = (is_array($value) && in_array($choice_value, $value)) ? "checked='checked'" : ""; |
| 276 | $atts .= (!empty($choice['atts'])) ? " {$choice['atts']}" : ""; |
| 277 | |
| 278 | $fields .= sprintf($input_template, $input_name, $input_atts, $choice_value, $input_name, $atts, $label); |
| 279 | } |
| 280 | } |
| 281 | $fields .= '</div>'; |
| 282 | |
| 283 | // Add helper checkboxes for bulk actions |
| 284 | if(isset($args['select_all']) || isset($args['unselect_all'])) { |
| 285 | $select_all_label = (!empty($args['select_all'])) ? $args['select_all'] : __('Select all', 'permalink-manager'); |
| 286 | $unselect_all_label = (!empty($args['unselect_all'])) ? $args['unselect_all'] : __('Unselect all', 'permalink-manager'); |
| 287 | |
| 288 | $fields .= "<p class=\"checkbox_actions extra-links\">"; |
| 289 | $fields .= (isset($args['select_all'])) ? "<a href=\"#\" class=\"select_all\">{$select_all_label}</a> " : ""; |
| 290 | $fields .= (isset($args['unselect_all'])) ? "<a href=\"#\" class=\"unselect_all\">{$unselect_all_label}</a>" : ""; |
| 291 | $fields .= "</p>"; |
| 292 | } |
| 293 | break; |
| 294 | |
| 295 | case 'single_checkbox' : |
| 296 | $fields .= '<div class="single_checkbox">'; |
| 297 | if(is_array($value)) { |
| 298 | $input_key = preg_replace('/(.*)(?:\[([^\[]+)\])$/', '$2', $input_name); |
| 299 | $checked = (!empty($value[$input_key])) ? "checked='checked'" : ""; |
| 300 | } else { |
| 301 | $checked = ($value == 1) ? "checked='checked'" : ""; |
| 302 | } |
| 303 | $checkbox_label = (isset($args['checkbox_label'])) ? $args['checkbox_label'] : ''; |
| 304 | |
| 305 | $fields .= "<input type='hidden' {$input_atts} value='0' name='{$input_name}' />"; |
| 306 | $fields .= "<label for='{$input_name}'><input type='checkbox' {$input_atts} value='1' name='{$input_name}' {$checked} /> {$checkbox_label}</label>"; |
| 307 | $fields .= '</div>'; |
| 308 | break; |
| 309 | |
| 310 | case 'radio' : |
| 311 | $fields .= '<div class="radios">'; |
| 312 | foreach($args['choices'] as $choice_value => $choice) { |
| 313 | $label = (is_array($choice)) ? $choice['label'] : $choice; |
| 314 | $atts = ($choice_value == $value) ? "checked='checked'" : ""; |
| 315 | $atts .= (!empty($choice['atts'])) ? " {$choice['atts']}" : ""; |
| 316 | |
| 317 | $fields .= "<label for='{$input_name}[]'><input type='radio' {$input_atts} value='{$choice_value}' name='{$input_name}[]' {$atts} /> {$label}</label>"; |
| 318 | } |
| 319 | $fields .= '</div>'; |
| 320 | break; |
| 321 | |
| 322 | case 'select' : |
| 323 | $fields .= '<span class="select">'; |
| 324 | $fields .= "<select name='{$input_name}' {$input_atts}>"; |
| 325 | foreach($args['choices'] as $choice_value => $choice) { |
| 326 | $label = (is_array($choice)) ? $choice['label'] : $choice; |
| 327 | $atts = ($choice_value == $value) ? "selected='selected'" : ""; |
| 328 | $atts .= (!empty($choice['atts'])) ? " {$choice['atts']}" : ""; |
| 329 | |
| 330 | $fields .= "<option value='{$choice_value}' {$atts}>{$label}</option>"; |
| 331 | } |
| 332 | $fields .= '</select>'; |
| 333 | $fields .= '</span>'; |
| 334 | break; |
| 335 | |
| 336 | case 'number' : |
| 337 | $fields .= "<input type='number' {$input_atts} value='{$value}' name='{$input_name}' />"; |
| 338 | break; |
| 339 | |
| 340 | case 'hidden' : |
| 341 | $fields .= "<input type='hidden' {$input_atts} value='{$value}' name='{$input_name}' />"; |
| 342 | break; |
| 343 | |
| 344 | case 'textarea' : |
| 345 | $fields .= "<textarea {$input_atts} name='{$input_name}' {$rows}>{$value}</textarea>"; |
| 346 | break; |
| 347 | |
| 348 | case 'pre' : |
| 349 | $fields .= "<pre {$input_atts}>{$value}</pre>"; |
| 350 | break; |
| 351 | |
| 352 | case 'info' : |
| 353 | $fields .= "<div {$input_atts}>{$value}</div>"; |
| 354 | break; |
| 355 | |
| 356 | case 'clearfix' : |
| 357 | return "<div class=\"clearfix\"></div>"; |
| 358 | |
| 359 | case 'permastruct' : |
| 360 | $siteurl = Permalink_Manager_Helper_Functions::get_permalink_base(); |
| 361 | |
| 362 | if(!empty($args['post_type'])) { |
| 363 | $type = $args['post_type']; |
| 364 | $type_name = $type['name']; |
| 365 | $content_type = 'post_types'; |
| 366 | |
| 367 | $permastructures = (!empty($permalink_manager_permastructs['post_types'])) ? $permalink_manager_permastructs['post_types'] : array(); |
| 368 | } else if(!empty($args['taxonomy'])) { |
| 369 | $type = $args['taxonomy']; |
| 370 | $type_name = $type['name']; |
| 371 | $content_type = "taxonomies"; |
| 372 | |
| 373 | $permastructures = (!empty($permalink_manager_permastructs['taxonomies'])) ? $permalink_manager_permastructs['taxonomies'] : array(); |
| 374 | } else { |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | // Get permastructures |
| 379 | $default_permastruct = trim(Permalink_Manager_Helper_Functions::get_default_permastruct($type_name), "/"); |
| 380 | $current_permastruct = isset($permastructures[$type_name]) ? $permastructures[$type_name] : $default_permastruct; |
| 381 | |
| 382 | // Append extra attributes |
| 383 | $input_atts .= " data-default=\"{$default_permastruct}\""; |
| 384 | $input_atts .= " placeholder=\"{$default_permastruct}\""; |
| 385 | |
| 386 | $fields .= "<div class=\"all-permastruct-container\">"; |
| 387 | |
| 388 | // 1. Default permastructure |
| 389 | $fields .= "<div class=\"permastruct-container\">"; |
| 390 | $fields .= "<span><code>{$siteurl}/</code></span>"; |
| 391 | $fields .= "<span><input type='text' {$input_atts} value='{$current_permastruct}' name='{$input_name}'/></span>"; |
| 392 | $fields .= "</div>"; |
| 393 | |
| 394 | $fields .= "<div class=\"permastruct-toggle\">"; |
| 395 | |
| 396 | // 2A. Permastructure for each language |
| 397 | $languages = (array) Permalink_Manager_Language_Plugins::get_all_languages(true); |
| 398 | if($languages) { |
| 399 | $fields .= sprintf( |
| 400 | "<h4>%s</h4><p class=\"permastruct-instruction\">%s</p>", |
| 401 | __("Permastructure translations", "permalink-manager"), |
| 402 | __("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") |
| 403 | ); |
| 404 | |
| 405 | foreach($languages as $lang => $name) { |
| 406 | $current_lang_permastruct = isset($permastructures["{$type_name}_{$lang}"]) ? $permastructures["{$type_name}_{$lang}"] : ''; |
| 407 | |
| 408 | $fields .= "<label>{$name}</label>"; |
| 409 | $fields .= "<div class=\"permastruct-container\">"; |
| 410 | $fields .= "<span><code>{$siteurl}/</code></span>"; |
| 411 | $fields .= sprintf("<span><input type='text' %s value='%s' name='%s'/></span>", $input_atts, $current_lang_permastruct, str_replace("]", "_{$lang}]", $input_name)); |
| 412 | $fields .= "</div>"; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | // 2B. Restore default permalinks |
| 417 | $fields .= sprintf( |
| 418 | "<p class=\"default-permastruct-row columns-container\"><span class=\"column-2_4\"><strong>%s:</strong> %s</span><span class=\"column-2_4\"><a href=\"#\" class=\"restore-default\"><span class=\"dashicons dashicons-image-rotate\"></span> %s</a></span></p>", |
| 419 | __("Default permastructure", "permalink-manager"), esc_html($default_permastruct), |
| 420 | __("Restore default permastructure", "permalink-manager") |
| 421 | ); |
| 422 | |
| 423 | // 2B. Do not auto-append slug field |
| 424 | $fields .= sprintf( |
| 425 | "<h4>%s</h4><div class=\"settings-container\">%s</div>", |
| 426 | __("Permastructure settings", "permalink-manager"), |
| 427 | self::generate_option_field("permastructure-settings[do_not_append_slug][$content_type][{$type_name}]", array('type' => 'single_checkbox', 'checkbox_label' => __("Do not automatically append the slug", "permalink-manager"))) |
| 428 | ); |
| 429 | |
| 430 | $fields .= "</div>"; |
| 431 | |
| 432 | // 3. Show toggle button |
| 433 | $fields .= sprintf( |
| 434 | "<p class=\"permastruct-toggle-button\"><a href=\"#\"><span class=\"dashicons dashicons-admin-settings\"></span> %s</a></p>", |
| 435 | __("Show additional settings", "permalink-manager") |
| 436 | ); |
| 437 | |
| 438 | $fields .= "</div>"; |
| 439 | |
| 440 | break; |
| 441 | |
| 442 | default : |
| 443 | $fields .= "<input type='text' {$input_atts} value='{$value}' name='{$input_name}'/>"; |
| 444 | } |
| 445 | |
| 446 | // Get the final HTML output |
| 447 | if(isset($args['container']) && $args['container'] == 'tools') { |
| 448 | $html = "<div{$container_class}>"; |
| 449 | $html .= "<h4>{$label}</h4>"; |
| 450 | $html .= "<div class='{$input_name}-container'>{$fields}</div>"; |
| 451 | $html .= $description; |
| 452 | $html .= $append_content; |
| 453 | $html .= "</div>"; |
| 454 | } else if(isset($args['container']) && $args['container'] == 'row') { |
| 455 | $html = sprintf("<tr id=\"%s\" data-field=\"%s\" %s>", esc_attr(preg_replace('/(?:.*\[)(.*)(?:\].*)/', '$1', $input_name)), $input_name, $container_class); |
| 456 | $html .= sprintf("<th><label for=\"%s\">%s</label></th>", $input_name, $args['label']); |
| 457 | $html .= sprintf("<td><fieldset>%s%s</fieldset></td>", $fields, $description); |
| 458 | $html .= "</tr>"; |
| 459 | $html .= ($append_content) ? "<tr class=\"appended-row\"><td colspan=\"2\">{$append_content}</td></tr>" : ""; |
| 460 | } else if(isset($args['container']) && $args['container'] == 'screen-options') { |
| 461 | $html = "<fieldset data-field=\"{$input_name}\" {$container_class}><legend>{$args['label']}</legend>"; |
| 462 | $html .= "<div class=\"field-content\">{$fields}{$description}</div>"; |
| 463 | $html .= ($append_content) ? "<div class=\"appended-row\">{$append_content}</div>" : ""; |
| 464 | $html .= "</fieldset>"; |
| 465 | } else { |
| 466 | $html = $fields . $append_content; |
| 467 | } |
| 468 | |
| 469 | return apply_filters('permalink_manager_field_output', $html); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Display hidden field to indicate posts or taxonomies admin sections |
| 474 | */ |
| 475 | static public function section_type_field($type = 'post') { |
| 476 | return self::generate_option_field('content_type', array('value' => $type, 'type' => 'hidden')); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Display the form |
| 481 | */ |
| 482 | static public function get_the_form($fields = array(), $container = '', $button = array(), $sidebar = '', $nonce = array(), $wrap = false, $form_class = '') { |
| 483 | // 1. Check if the content will be displayed in columns and button details |
| 484 | switch($container) { |
| 485 | case 'columns-3' : |
| 486 | $wrapper_class = 'columns-container'; |
| 487 | $form_column_class = 'column column-2_3'; |
| 488 | $sidebar_class = 'column column-1_3'; |
| 489 | break; |
| 490 | |
| 491 | // there will be more cases in future ... |
| 492 | default : |
| 493 | $form_column_class = 'form'; |
| 494 | $sidebar_class = 'sidebar'; |
| 495 | $wrapper_class = $form_column_class = ''; |
| 496 | } |
| 497 | |
| 498 | // 2. Process the array with button and nonce field settings |
| 499 | $button_text = (!empty($button['text'])) ? $button['text'] : ''; |
| 500 | $button_class = (!empty($button['class'])) ? $button['class'] : ''; |
| 501 | $button_attributes = (!empty($button['attributes'])) ? $button['attributes'] : ''; |
| 502 | $nonce_action = (!empty($nonce['action'])) ? $nonce['action'] : ''; |
| 503 | $nonce_name = (!empty($nonce['name'])) ? $nonce['name'] : ''; |
| 504 | $form_classes = (!empty($form_class)) ? $form_class : ''; |
| 505 | |
| 506 | // 2. Now get the HTML output (start section row container) |
| 507 | $html = ($wrapper_class) ? "<div class=\"{$wrapper_class}\">" : ''; |
| 508 | |
| 509 | // 3. Display some notes |
| 510 | if($sidebar_class && $sidebar) { |
| 511 | $html .= "<div class=\"{$sidebar_class}\">"; |
| 512 | $html .= "<div class=\"section-notes\">"; |
| 513 | $html .= $sidebar; |
| 514 | $html .= "</div>"; |
| 515 | $html .= "</div>"; |
| 516 | } |
| 517 | |
| 518 | // 4. Start fields' section |
| 519 | $html .= ($form_column_class) ? "<div class=\"{$form_column_class}\">" : ""; |
| 520 | $html .= "<form method=\"POST\" class=\"{$form_classes}\">"; |
| 521 | $html .= ($wrap) ? "<table class=\"form-table\">" : ""; |
| 522 | |
| 523 | // Loop through all fields assigned to this section |
| 524 | foreach($fields as $field_name => $field) { |
| 525 | $field_name = (!empty($field['name'])) ? $field['name'] : $field_name; |
| 526 | |
| 527 | // A. Display table row |
| 528 | if(isset($field['container']) && $field['container'] == 'row') { |
| 529 | $row_output = ""; |
| 530 | |
| 531 | // Loop through all fields assigned to this section |
| 532 | if(isset($field['fields'])) { |
| 533 | foreach($field['fields'] as $section_field_id => $section_field) { |
| 534 | $section_field_name = (!empty($section_field['name'])) ? $section_field['name'] : "{$field_name}[$section_field_id]"; |
| 535 | $section_field['container'] = 'row'; |
| 536 | |
| 537 | $row_output .= self::generate_option_field($section_field_name, $section_field); |
| 538 | } |
| 539 | } else { |
| 540 | $row_output .= self::generate_option_field($field_name, $field); |
| 541 | } |
| 542 | |
| 543 | if(isset($field['section_name'])) { |
| 544 | $html .= "<h3>{$field['section_name']}</h3>"; |
| 545 | $html .= (isset($field['append_content'])) ? $field['append_content'] : ""; |
| 546 | $html .= (isset($field['description'])) ? "<p class=\"description\">{$field['description']}</p>" : ""; |
| 547 | $html .= "<table class=\"form-table\" data-field=\"{$field_name}\">{$row_output}</table>"; |
| 548 | } else { |
| 549 | $html .= $row_output; |
| 550 | } |
| 551 | } |
| 552 | // B. Display single field |
| 553 | else { |
| 554 | $html .= self::generate_option_field($field_name, $field); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | $html .= ($wrap) ? "</table>" : ""; |
| 559 | |
| 560 | // End the fields' section + add button & nonce fields |
| 561 | if($nonce_action && $nonce_name) { |
| 562 | $html .= wp_nonce_field($nonce_action, $nonce_name, true, true); |
| 563 | $html .= self::generate_option_field('pm_session_id', array('value' => uniqid(), 'type' => 'hidden')); |
| 564 | } |
| 565 | $html .= ($button_text) ? get_submit_button($button_text, $button_class, '', false, $button_attributes) : ""; |
| 566 | $html .= '</form>'; |
| 567 | $html .= ($form_column_class) ? "</div>" : ""; |
| 568 | |
| 569 | // 5. End the section row container |
| 570 | $html .= ($wrapper_class) ? "</div>" : ""; |
| 571 | |
| 572 | return $html; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Display the plugin sections. |
| 577 | */ |
| 578 | public function display_section() { |
| 579 | global $wpdb, $permalink_manager_before_sections_html, $permalink_manager_after_sections_html; |
| 580 | |
| 581 | $html = "<div id=\"permalink-manager\" class=\"wrap\">"; |
| 582 | |
| 583 | $donate_link = (self::is_pro_active()) ? "" : sprintf("<a href=\"%s\" target=\"_blank\" class=\"page-title-action\">%s</a>", PERMALINK_MANAGER_DONATE, __("Donate", "permalink-manager")); |
| 584 | $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); |
| 585 | |
| 586 | // Display the tab navigation |
| 587 | $html .= "<div id=\"permalink-manager-tab-nav\" class=\"nav-tab-wrapper\">"; |
| 588 | foreach($this->sections as $section_name => $section_properties) { |
| 589 | $active_class = ($this->active_section === $section_name) ? 'nav-tab-active nav-tab' : 'nav-tab'; |
| 590 | $section_url = $this->get_admin_url("§ion={$section_name}"); |
| 591 | |
| 592 | $html .= sprintf("<a href=\"%s\" class=\"%s section_%s\">%s</a>", $section_url, $active_class, $section_name, $section_properties['name']); |
| 593 | } |
| 594 | |
| 595 | // Upgrade to Pro version |
| 596 | $html .= (self::is_pro_active() == false) ? sprintf("<a href=\"%s\" target=\"_blank\" class=\"nav-tab section_upgrade\">%s</a>", 'https://permalinkmanager.pro/buy-permalink-manager-pro/?utm_source=plugin_upgrade', __('Upgrade to PRO', 'permalink-manager')) : ''; |
| 597 | $html .= "</div>"; |
| 598 | |
| 599 | // Now display the active section |
| 600 | $html .= "<div id=\"permalink-manager-sections\">"; |
| 601 | $active_section_array = (isset($this->sections[$this->active_section])) ? $this->sections[$this->active_section] : ""; |
| 602 | |
| 603 | // Display addidional navigation for subsections |
| 604 | if(isset($this->sections[$this->active_section]['subsections'])) { |
| 605 | $html .= "<ul class=\"subsubsub\">"; |
| 606 | foreach ($this->sections[$this->active_section]['subsections'] as $subsection_name => $subsection) { |
| 607 | $active_class = ($this->active_subsection === $subsection_name) ? 'current' : ''; |
| 608 | $subsection_url = $this->get_admin_url("§ion={$this->active_section}&subsection={$subsection_name}"); |
| 609 | |
| 610 | $html .= "<li><a href=\"{$subsection_url}\" class=\"{$active_class}\">{$subsection['name']}</a></li>"; |
| 611 | } |
| 612 | $html .= "</ul>"; |
| 613 | } |
| 614 | |
| 615 | // A. Execute the function assigned to the subsection |
| 616 | if(isset($active_section_array['subsections'][$this->active_subsection]['function'])) { |
| 617 | $class_name = $active_section_array['subsections'][$this->active_subsection]['function']['class']; |
| 618 | $section_object = new $class_name(); |
| 619 | |
| 620 | $section_content = call_user_func(array($section_object, $active_section_array['subsections'][$this->active_subsection]['function']['method'])); |
| 621 | } |
| 622 | // B. Execute the function assigned to the section |
| 623 | else if(isset($active_section_array['function'])) { |
| 624 | $class_name = $active_section_array['function']['class']; |
| 625 | $section_object = new $class_name(); |
| 626 | |
| 627 | $section_content = call_user_func(array($section_object, $active_section_array['function']['method'])); |
| 628 | } |
| 629 | // C. Display the raw HTMl output of subsection |
| 630 | else if(isset($active_section_array['subsections'][$this->active_subsection]['html'])) { |
| 631 | $section_content = (isset($active_section_array['subsections'][$this->active_subsection]['html'])) ? $active_section_array['subsections'][$this->active_subsection]['html'] : ""; |
| 632 | } |
| 633 | // D. Try to display the raw HTMl output of section |
| 634 | else { |
| 635 | $section_content = (isset($active_section_array['html'])) ? $active_section_array['html'] : ""; |
| 636 | } |
| 637 | |
| 638 | $html .= "<div class=\"single-section\" data-section=\"{$this->active_section}\" id=\"{$this->active_section}\">{$section_content}</div>"; |
| 639 | $html .= "</div>"; |
| 640 | |
| 641 | // Display alerts and another content if needed and close .wrap container |
| 642 | $html .= $permalink_manager_after_sections_html; |
| 643 | $html .= "</div>"; |
| 644 | |
| 645 | echo $html; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Display the table with updated slugs after one of the actions is triggered |
| 650 | */ |
| 651 | static function display_updated_slugs($updated_array, $return_array = false, $display_full_table = true) { |
| 652 | global $permalink_manager_before_sections_html, $permalink_manager_after_sections_html; |
| 653 | |
| 654 | $updated_slugs_count = 0; |
| 655 | $html = $main_content = $alert = ""; |
| 656 | |
| 657 | if(is_array($updated_array)) { |
| 658 | // Check if slugs should be displayed |
| 659 | $first_slug = reset($updated_array); |
| 660 | $show_slugs = (!empty($_POST['mode']) && $_POST['mode'] == 'slugs') ? true : false; |
| 661 | |
| 662 | $header_footer = '<tr>'; |
| 663 | $header_footer .= '<th class="column-primary">' . __('Title', 'permalink-manager') . '</th>'; |
| 664 | if($show_slugs) { |
| 665 | $header_footer .= (isset($first_slug['old_slug'])) ? '<th>' . __('Old Slug', 'permalink-manager') . '</th>' : ""; |
| 666 | $header_footer .= (isset($first_slug['new_slug'])) ? '<th>' . __('New Slug', 'permalink-manager') . '</th>' : ""; |
| 667 | } else { |
| 668 | $header_footer .= '<th>' . __('Old URI', 'permalink-manager') . '</th>'; |
| 669 | $header_footer .= '<th>' . __('New URI', 'permalink-manager') . '</th>'; |
| 670 | } |
| 671 | $header_footer .= '</tr>'; |
| 672 | |
| 673 | foreach($updated_array as $row) { |
| 674 | // Odd/even class |
| 675 | $updated_slugs_count++; |
| 676 | $alternate_class = ($updated_slugs_count % 2 == 1) ? ' class="alternate"' : ''; |
| 677 | |
| 678 | // Taxonomy |
| 679 | if(!empty($row['tax'])) { |
| 680 | $term_link = get_term_link(intval($row['ID']), $row['tax']); |
| 681 | $permalink = (is_wp_error($term_link)) ? "-" : $term_link; |
| 682 | } else { |
| 683 | $permalink = get_permalink($row['ID']); |
| 684 | } |
| 685 | |
| 686 | // Decode permalink |
| 687 | $permalink = rawurldecode(rawurldecode($permalink)); |
| 688 | |
| 689 | $main_content .= "<tr{$alternate_class}>"; |
| 690 | $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>'; |
| 691 | if($show_slugs) { |
| 692 | $main_content .= (isset($row['old_slug'])) ? '<td data-colname="' . __('Old Slug', 'permalink-manager') . '">' . rawurldecode($row['old_slug']) . '</td>' : ""; |
| 693 | $main_content .= (isset($row['new_slug'])) ? '<td data-colname="' . __('New Slug', 'permalink-manager') . '">' . rawurldecode($row['new_slug']) . '</td>' : ""; |
| 694 | } else { |
| 695 | $main_content .= '<td data-colname="' . __('Old URI', 'permalink-manager') . '">' . rawurldecode($row['old_uri']) . '</td>'; |
| 696 | $main_content .= '<td data-colname="' . __('New URI', 'permalink-manager') . '">' . rawurldecode($row['new_uri']) . '</td>'; |
| 697 | } |
| 698 | $main_content .= '</tr>'; |
| 699 | } |
| 700 | |
| 701 | // Merge header, footer and content |
| 702 | if($display_full_table) { |
| 703 | $html = '<h3 id="updated-list">' . __('List of updated items', 'permalink-manager') . '</h3>'; |
| 704 | $html .= '<table class="widefat wp-list-table updated-slugs-table">'; |
| 705 | $html .= "<thead>{$header_footer}</thead><tbody>{$main_content}</tbody><tfoot>{$header_footer}</tfoot>"; |
| 706 | } else { |
| 707 | $html = $main_content; |
| 708 | } |
| 709 | |
| 710 | $html .= '</table>'; |
| 711 | } |
| 712 | |
| 713 | // 3. Display the alert |
| 714 | if(isset($updated_slugs_count)) { |
| 715 | if($updated_slugs_count > 0) { |
| 716 | $alert_content = sprintf( _n( '<strong class="updated_count">%d</strong> slug was updated!', '<strong class="updated_count">%d</strong> slugs were updated!', $updated_slugs_count, 'permalink-manager' ), $updated_slugs_count ) . ' '; |
| 717 | $alert_content .= sprintf( __( '<a %s>Click here</a> to go to the list of updated slugs', 'permalink-manager' ), "href=\"#updated-list\""); |
| 718 | |
| 719 | $alert = self::get_alert_message($alert_content, 'updated updated_slugs'); |
| 720 | } else { |
| 721 | $alert = self::get_alert_message(__( '<strong>No slugs</strong> were updated!', 'permalink-manager' ), 'error updated_slugs'); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | if($return_array) { |
| 726 | return array( |
| 727 | 'html' => $html, |
| 728 | 'alert' => $alert |
| 729 | ); |
| 730 | } else { |
| 731 | $permalink_manager_before_sections_html .= $alert; |
| 732 | |
| 733 | return $html; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * "Quick Edit" Box |
| 739 | */ |
| 740 | public static function quick_edit_column_form($is_taxonomy = false) { |
| 741 | $html = self::generate_option_field('permalink-manager-quick-edit', array('value' => true, 'type' => 'hidden')); |
| 742 | $html .= "<fieldset class=\"inline-edit-permalink\">"; |
| 743 | $html .= sprintf("<legend class=\"inline-edit-legend\">%s</legend>", __("Permalink Manager", "permalink-manager")); |
| 744 | |
| 745 | $html .= "<div class=\"inline-edit-col\">"; |
| 746 | $html .= sprintf("<label class=\"inline-edit-group\"><span class=\"title\">%s</span><span class=\"input-text-wrap\">%s</span></label>", |
| 747 | __("Current URI", "permalink-manager"), |
| 748 | self::generate_option_field("custom_uri", array("input_class" => "custom_uri", "value" => '')) |
| 749 | ); |
| 750 | $html .= "</div>"; |
| 751 | |
| 752 | $html .= "</fieldset>"; |
| 753 | |
| 754 | // Append nonce field & element ID |
| 755 | $html .= self::generate_option_field("permalink-manager-edit-uri-element-id", array("type" => "hidden", "input_class" => "permalink-manager-edit-uri-element-id", "value" => "")); |
| 756 | $html .= wp_nonce_field( 'permalink-manager-edit-uri-box', 'permalink-manager-nonce', true, false ); |
| 757 | |
| 758 | return $html; |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Hide "Custom URI" column |
| 763 | */ |
| 764 | function quick_edit_hide_column($hidden, $screen) { |
| 765 | $hidden[] = 'permalink-manager-col'; |
| 766 | return $hidden; |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * Display "Permalink Manager" box |
| 771 | */ |
| 772 | public static function display_uri_box($element, $gutenberg = false) { |
| 773 | global $permalink_manager_options, $permalink_manager_uris; |
| 774 | |
| 775 | // Check the user capabilities |
| 776 | $edit_uris_cap = (!empty($permalink_manager_options['general']['edit_uris_cap'])) ? $permalink_manager_options['general']['edit_uris_cap'] : 'publish_posts'; |
| 777 | if(!current_user_can($edit_uris_cap)) { |
| 778 | return; |
| 779 | } |
| 780 | |
| 781 | if(!empty($element->ID)) { |
| 782 | $id = $element_id = $element->ID; |
| 783 | $native_slug = $element->post_name; |
| 784 | $is_draft = (!empty($element->post_status) && (in_array($element->post_status, array('draft', 'auto-draft')))) ? true : false; |
| 785 | $is_front_page = Permalink_Manager_Helper_Functions::is_front_page($id); |
| 786 | |
| 787 | $auto_update_val = get_post_meta($id, "auto_update_uri", true); |
| 788 | |
| 789 | // Get URIs |
| 790 | $uri = Permalink_Manager_URI_Functions_Post::get_post_uri($id, true, $is_draft); |
| 791 | $default_uri = Permalink_Manager_URI_Functions_Post::get_default_post_uri($id); |
| 792 | $native_uri = Permalink_Manager_URI_Functions_Post::get_default_post_uri($id, true); |
| 793 | } else if(class_exists('Permalink_Manager_URI_Functions_Tax')) { |
| 794 | $id = $element->term_id; |
| 795 | $element_id = "tax-{$id}"; |
| 796 | $native_slug = $element->slug; |
| 797 | |
| 798 | $auto_update_val = get_term_meta($id, "auto_update_uri", true); |
| 799 | |
| 800 | // Get URIs |
| 801 | $uri = Permalink_Manager_URI_Functions_Tax::get_term_uri($element->term_id, true); |
| 802 | $default_uri = Permalink_Manager_URI_Functions_Tax::get_default_term_uri($element->term_id); |
| 803 | $native_uri = Permalink_Manager_URI_Functions_Tax::get_default_term_uri($element->term_id, true); |
| 804 | } else { |
| 805 | return; |
| 806 | } |
| 807 | |
| 808 | // Auto-update settings |
| 809 | $auto_update_def_val = $permalink_manager_options["general"]["auto_update_uris"]; |
| 810 | $auto_update_def_label = ($auto_update_def_val) ? __("Yes", "permalink-manager") : __("No", "permalink-manager"); |
| 811 | $auto_update_choices = array( |
| 812 | 0 => array("label" => sprintf(__("Use global settings [%s]", "permalink-manager"), $auto_update_def_label), "atts" => "data-auto-update=\"{$auto_update_def_val}\""), |
| 813 | 1 => array("label" => __("Yes", "permalink-manager"), "atts" => "data-auto-update=\"1\""), |
| 814 | -1 => array("label" => __("No", "permalink-manager"), "atts" => "data-auto-update=\"0\""), |
| 815 | -2 => array("label" => __("No (ignore this URI in bulk tools)", "permalink-manager"), "atts" => "data-auto-update=\"2\"") |
| 816 | ); |
| 817 | |
| 818 | // Decode default URI |
| 819 | $default_uri = rawurldecode($default_uri); |
| 820 | |
| 821 | // Start HTML output |
| 822 | // 1. Button |
| 823 | if(!$gutenberg) { |
| 824 | $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")); |
| 825 | |
| 826 | $html .= "<div id=\"permalink-manager\" class=\"postbox permalink-manager-edit-uri-box\" style=\"display: none;\">"; |
| 827 | |
| 828 | // 2. The heading |
| 829 | $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>"; |
| 830 | $html .= sprintf("<h2><span>%s</span></h2>", __("Permalink Manager", "permalink-manager")); |
| 831 | |
| 832 | // 3. The fields container [start] |
| 833 | $html .= "<div class=\"inside\">"; |
| 834 | } else { |
| 835 | $html = "<div class=\"permalink-manager-gutenberg permalink-manager-edit-uri-box\">"; |
| 836 | } |
| 837 | |
| 838 | // 4. Custom URI |
| 839 | if(!empty($is_front_page)) { |
| 840 | $custom_uri_field = self::generate_option_field("custom_uri", array("type" => "hidden", "extra_atts" => "data-default=\"{$default_uri}\" data-element-id=\"{$element_id}\"", "input_class" => "widefat custom_uri", "value" => rawurldecode($uri))); |
| 841 | $custom_uri_field .= __("The custom URI cannot be edited on frontpage.", "permalink-manager"); |
| 842 | } else { |
| 843 | $custom_uri_field = self::generate_option_field("custom_uri", array("extra_atts" => "data-default=\"{$default_uri}\" data-element-id=\"{$element_id}\"", "input_class" => "widefat custom_uri", "value" => rawurldecode($uri))); |
| 844 | $custom_uri_field .= sprintf('<p class="uri_locked hidden">%s %s</p>', '<span class="dashicons dashicons-lock"></span>', __('The above permalink will be automatically updated to "Default URI" and is locked for editing.', 'permalink-manager')); |
| 845 | } |
| 846 | |
| 847 | $html .= sprintf("<div class=\"custom_uri_container\"><p><label for=\"custom_uri\" class=\"strong\">%s %s</label></p><span>%s</span><span class=\"duplicated_uri_alert\"></span></div>", |
| 848 | __("Current URI", "permalink-manager"), |
| 849 | ($element->ID) ? self::help_tooltip(__("If custom URI is not defined, a default URI will be set (see below). The custom URI can be edited only if 'Auto-update the URI' feature is not enabled.", "permalink-manager")) : "", |
| 850 | $custom_uri_field |
| 851 | ); |
| 852 | |
| 853 | // 5. Auto-update URI |
| 854 | if(empty($is_front_page)) { |
| 855 | if(!empty($auto_update_choices)) { |
| 856 | $html .= sprintf("<div><p><label for=\"auto_auri\" class=\"strong\">%s %s</label></p><span>%s</span></div>", |
| 857 | __("Auto-update \"Current URI\"", "permalink-manager"), |
| 858 | self::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")), |
| 859 | self::generate_option_field("auto_update_uri", array("type" => "select", "input_class" => "widefat auto_update", "value" => $auto_update_val, "choices" => $auto_update_choices)) |
| 860 | ); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | // 6. Native slug |
| 865 | if(!empty($element->ID) && !empty($permalink_manager_options["general"]["show_native_slug_field"])) { |
| 866 | $native_slug_field = self::generate_option_field("native_slug", array("extra_atts" => "data-default=\"{$native_slug}\" data-element-id=\"{$element_id}\"", "input_class" => "widefat native_slug", "value" => rawurldecode($native_slug))); |
| 867 | |
| 868 | $html .= sprintf("<div class=\"native_slug_container\"><p><label for=\"native_slug\" class=\"strong\">%s %s</label></p><span>%s</span></div>", |
| 869 | __("Native slug", "permalink-manager"), |
| 870 | self::help_tooltip(__("The native slug is by default automatically used in native permalinks (when Permalink Manager is disabled).", "permalink-manager")), |
| 871 | $native_slug_field |
| 872 | ); |
| 873 | } |
| 874 | |
| 875 | if(empty($is_front_page)) { |
| 876 | // 7. Default URI |
| 877 | $html .= sprintf( |
| 878 | "<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>", |
| 879 | __("Default URI", "permalink-manager"), esc_html($default_uri), |
| 880 | __("Restore Default URI", "permalink-manager") |
| 881 | ); |
| 882 | |
| 883 | // 8. Native URI info |
| 884 | if(!empty($permalink_manager_options['general']['redirect']) && ((!empty($element->post_status) && in_array($element->post_status, array('auto-draft', 'trash', 'draft'))) == false)) { |
| 885 | $native_permalink = trim(Permalink_Manager_Helper_Functions::get_permalink_base($element), "/") . "/"; |
| 886 | $native_permalink .= $native_uri; |
| 887 | |
| 888 | $html .= sprintf( |
| 889 | "<div class=\"default-permalink-row columns-container\"><span><strong>%s</strong> <a href=\"%s\">%s</a></span></div>", |
| 890 | __("Automatic redirect for native URI enabled:", "permalink-manager"), |
| 891 | $native_permalink, |
| 892 | rawurldecode($native_uri) |
| 893 | ); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | // 9. Custom redirects |
| 898 | $html .= ($element->ID) ? self::display_redirect_panel($id) : self::display_redirect_panel("tax-{$id}"); |
| 899 | |
| 900 | // 10. Extra save button for Gutenberg |
| 901 | if($gutenberg) { |
| 902 | $html .= sprintf( |
| 903 | "<div class=\"default-permalink-row save-row columns-container hidden\"><div><a href=\"#\" class=\"button button-primary\" id=\"permalink-manager-save-button\">%s</a></div></div>", |
| 904 | __("Save permalink", "permalink-manager") |
| 905 | ); |
| 906 | } else { |
| 907 | $html .= "</div>"; |
| 908 | } |
| 909 | |
| 910 | $html .= "</div>"; |
| 911 | |
| 912 | // 11. Append nonce field, element ID & native slug |
| 913 | $html .= self::generate_option_field("permalink-manager-edit-uri-element-slug", array("type" => "hidden", "value" => $native_slug)); |
| 914 | $html .= self::generate_option_field("permalink-manager-edit-uri-element-id", array("type" => "hidden", "value" => $element_id)); |
| 915 | $html .= wp_nonce_field('permalink-manager-edit-uri-box', 'permalink-manager-nonce', true, false); |
| 916 | |
| 917 | return $html; |
| 918 | } |
| 919 | |
| 920 | /** |
| 921 | * Display the redirect panel |
| 922 | */ |
| 923 | public static function display_redirect_panel($element_id) { |
| 924 | global $permalink_manager_options, $permalink_manager_redirects; |
| 925 | |
| 926 | // Heading |
| 927 | $html = "<div class=\"permalink-manager redirects-row redirects-panel columns-container\">"; |
| 928 | |
| 929 | $html .= sprintf( |
| 930 | "<div><a class=\"button\" href=\"#\" id=\"toggle-redirect-panel\">%s</a></div>", |
| 931 | __("Manage redirects", "permalink-manager") |
| 932 | ); |
| 933 | |
| 934 | $html .= "<div id=\"redirect-panel-inside\">"; |
| 935 | if(class_exists('Permalink_Manager_Pro_Addons')) { |
| 936 | $html .= Permalink_Manager_Pro_Addons::display_redirect_form($element_id); |
| 937 | } else { |
| 938 | $html .= self::pro_text(true); |
| 939 | } |
| 940 | $html .= "</div>"; |
| 941 | |
| 942 | $html .= "</div>"; |
| 943 | |
| 944 | return $html; |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * Display error/info message |
| 949 | */ |
| 950 | public static function get_alert_message($alert_content = "", $alert_type = "", $dismissable = true, $id = false) { |
| 951 | // Ignore empty messages (just in case) |
| 952 | if(empty($alert_content) || empty($alert_type)) { |
| 953 | return ""; |
| 954 | } |
| 955 | |
| 956 | $class = ($dismissable) ? "is-dismissible" : ""; |
| 957 | $alert_id = ($id) ? " data-alert_id=\"{$id}\"" : ""; |
| 958 | |
| 959 | $html = sprintf( "<div class=\"{$alert_type} permalink-manager-notice notice {$class}\"{$alert_id}> %s</div>", wpautop($alert_content) ); |
| 960 | |
| 961 | return $html; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * Help tooltip |
| 966 | */ |
| 967 | static function help_tooltip($text = '') { |
| 968 | $html = " <a href=\"#\" title=\"{$text}\" class=\"help_tooltip\"><span class=\"dashicons dashicons-editor-help\"></span></a>"; |
| 969 | return $html; |
| 970 | } |
| 971 | |
| 972 | /** |
| 973 | * Display global notices (throughout wp-admin dashboard) |
| 974 | */ |
| 975 | function display_global_notices() { |
| 976 | global $permalink_manager_alerts, $active_section; |
| 977 | |
| 978 | $html = ""; |
| 979 | if(!empty($permalink_manager_alerts) && is_array($permalink_manager_alerts)) { |
| 980 | foreach($permalink_manager_alerts as $alert_id => $alert) { |
| 981 | if(!empty($alert['show'])) { |
| 982 | // Hide notice in Permalink Manager Pro |
| 983 | if(defined('PERMALINK_MANAGER_PRO') && $alert['show'] == 'pro_hide') { continue; } |
| 984 | |
| 985 | // Display the notice only on the plugin pages |
| 986 | if(empty($active_section) && !empty($alert['plugin_only'])) { continue; } |
| 987 | |
| 988 | // Check if the notice did not expire |
| 989 | if(isset($alert['until']) && (time() > strtotime($alert['until']))) { continue; } |
| 990 | |
| 991 | $html .= self::get_alert_message($alert['txt'], $alert['type'], true, $alert_id); |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | echo $html; |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * Hide global notices (AJAX) |
| 1001 | */ |
| 1002 | function hide_global_notice() { |
| 1003 | global $permalink_manager_alerts; |
| 1004 | |
| 1005 | // Get the ID of the alert |
| 1006 | $alert_id = (!empty($_REQUEST['alert_id'])) ? sanitize_title($_REQUEST['alert_id']) : ""; |
| 1007 | if(!empty($permalink_manager_alerts[$alert_id])) { |
| 1008 | $permalink_manager_alerts[$alert_id]['show'] = 0; |
| 1009 | } |
| 1010 | |
| 1011 | update_option( 'permalink-manager-alerts', $permalink_manager_alerts); |
| 1012 | } |
| 1013 | |
| 1014 | /** |
| 1015 | * Display notices generated by Permalink Manager tools |
| 1016 | */ |
| 1017 | function display_plugin_notices() { |
| 1018 | global $permalink_manager_before_sections_html; |
| 1019 | |
| 1020 | echo $permalink_manager_before_sections_html; |
| 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * Check if Permalink Manager Pro is active |
| 1025 | */ |
| 1026 | public static function is_pro_active($return_text = false) { |
| 1027 | if(defined('PERMALINK_MANAGER_PRO') && PERMALINK_MANAGER_PRO == true) { |
| 1028 | $is_pro = true; |
| 1029 | } else { |
| 1030 | $is_pro = false; |
| 1031 | } |
| 1032 | |
| 1033 | // Check if license is active |
| 1034 | if(class_exists('Permalink_Manager_Pro_Functions')) { |
| 1035 | $exp_date = Permalink_Manager_Pro_Functions::get_expiration_date(true); |
| 1036 | |
| 1037 | $is_pro = ($exp_date > 1) ? false : true; |
| 1038 | } else { |
| 1039 | $is_pro = false; |
| 1040 | } |
| 1041 | |
| 1042 | return $is_pro; |
| 1043 | } |
| 1044 | |
| 1045 | static function pro_text($text_only = false) { |
| 1046 | if(class_exists('Permalink_Manager_Pro_Functions')) { |
| 1047 | $text = Permalink_Manager_Pro_Functions::get_expiration_date(false, true); |
| 1048 | } else { |
| 1049 | $text = sprintf(__('This functionality is available only in <a href="%s" target="_blank">Permalink Manager Pro</a>.', 'permalink-manager'), PERMALINK_MANAGER_WEBSITE); |
| 1050 | } |
| 1051 | |
| 1052 | return ($text_only) ? $text : sprintf("<div class=\"alert info\"> %s</div>", wpautop($text, 'alert', false)); |
| 1053 | } |
| 1054 | |
| 1055 | } |
| 1056 |