permalink-manager-actions.php
3 months ago
permalink-manager-admin-functions.php
1 month ago
permalink-manager-core-functions.php
3 months ago
permalink-manager-debug.php
3 months ago
permalink-manager-gutenberg.php
3 months ago
permalink-manager-helper-functions.php
1 month ago
permalink-manager-permastructures-functions.php
3 months ago
permalink-manager-uri-functions-post.php
3 months ago
permalink-manager-uri-functions.php
1 month ago
permalink-manager-admin-functions.php
330 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Additional functions related to WordPress Admin Dashboard UI |
| 9 | */ |
| 10 | class Permalink_Manager_Admin_Functions { |
| 11 | |
| 12 | public $sections, $active_section, $active_subsection; |
| 13 | |
| 14 | public function __construct() { |
| 15 | add_action( 'admin_menu', array( $this, 'add_menu_page' ) ); |
| 16 | add_action( 'admin_init', array( $this, 'init' ) ); |
| 17 | add_action( 'admin_bar_menu', array( $this, 'fix_customize_url' ), 41 ); |
| 18 | |
| 19 | add_action( 'admin_notices', array( $this, 'display_plugin_notices' ) ); |
| 20 | add_action( 'admin_notices', array( $this, 'display_global_notices' ) ); |
| 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_" . PERMALINK_MANAGER_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 | * Use the native URL for "Customize" button in the admin bar |
| 38 | * |
| 39 | * @param WP_Admin_Bar $wp_admin_bar |
| 40 | */ |
| 41 | public function fix_customize_url( $wp_admin_bar ) { |
| 42 | global $permalink_manager_ignore_permalink_filters; |
| 43 | |
| 44 | $object = get_queried_object(); |
| 45 | $customize = $wp_admin_bar->get_node( 'customize' ); |
| 46 | |
| 47 | if ( empty( $customize->href ) ) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | $permalink_manager_ignore_permalink_filters = true; |
| 52 | if ( ! empty( $object->ID ) && is_a( $object, 'WP_Post' ) ) { |
| 53 | $new_url = get_permalink( $object->ID ); |
| 54 | } else if ( ! empty( $object->taxonomy ) && is_a( $object, 'WP_Term' ) ) { |
| 55 | $new_url = get_term_link( $object, $object->taxonomy ); |
| 56 | } |
| 57 | $permalink_manager_ignore_permalink_filters = false; |
| 58 | |
| 59 | if ( ! empty( $new_url ) ) { |
| 60 | // The original permalink should be already encoded via "utf8_uri_encode()" in "sanitize_title_with_dashes()" function, so there is no need to encode them once again |
| 61 | $new_url = filter_var( $new_url, FILTER_SANITIZE_URL ); |
| 62 | $customize_url = preg_replace( '/url=([^&]+)/', "url={$new_url}", $customize->href ); |
| 63 | |
| 64 | $wp_admin_bar->add_node( array( |
| 65 | 'id' => 'customize', |
| 66 | 'href' => $customize_url, |
| 67 | ) ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get current section of Permalink Manager admin panel |
| 73 | */ |
| 74 | public function get_current_section() { |
| 75 | global $active_section, $active_subsection, $current_admin_tax; |
| 76 | |
| 77 | // 1. Get current section |
| 78 | if ( isset( $_GET['page'] ) && $_GET['page'] == PERMALINK_MANAGER_PLUGIN_SLUG ) { |
| 79 | if ( isset( $_POST['section'] ) ) { |
| 80 | $this->active_section = sanitize_title_with_dashes( $_POST['section'] ); |
| 81 | } else if ( isset( $_GET['section'] ) ) { |
| 82 | $this->active_section = sanitize_title_with_dashes( $_GET['section'] ); |
| 83 | } else { |
| 84 | $sections_names = array_keys( $this->sections ); |
| 85 | $this->active_section = $sections_names[0]; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // 2. Get current subsection |
| 90 | if ( $this->active_section && isset( $this->sections[ $this->active_section ]['subsections'] ) ) { |
| 91 | if ( isset( $_POST['subsection'] ) ) { |
| 92 | $this->active_subsection = sanitize_title_with_dashes( $_POST['subsection'] ); |
| 93 | } else if ( isset( $_GET['subsection'] ) ) { |
| 94 | $this->active_subsection = sanitize_title_with_dashes( $_GET['subsection'] ); |
| 95 | } else { |
| 96 | $subsections_names = array_keys( $this->sections[ $this->active_section ]['subsections'] ); |
| 97 | $this->active_subsection = $subsections_names[0]; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // 3. Check if current admin page is related to taxonomies |
| 102 | if ( ! empty( $this->active_subsection ) && substr( $this->active_subsection, 0, 4 ) == 'tax_' ) { |
| 103 | $current_admin_tax = substr( $this->active_subsection, 4, strlen( $this->active_subsection ) ); |
| 104 | } else { |
| 105 | $current_admin_tax = false; |
| 106 | } |
| 107 | |
| 108 | // Set globals |
| 109 | $active_section = $this->active_section; |
| 110 | $active_subsection = $this->active_subsection; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Add "Tools -> Permalink Manager" to the admin sidebar menu |
| 115 | */ |
| 116 | public function add_menu_page() { |
| 117 | add_management_page( __( 'Permalink Manager', 'permalink-manager' ), __( 'Permalink Manager', 'permalink-manager' ), 'manage_options', PERMALINK_MANAGER_PLUGIN_SLUG, array( $this, 'display_section' ) ); |
| 118 | |
| 119 | add_action( 'admin_init', array( $this, 'enqueue_cssjs' ) ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Display the plugin sections |
| 124 | */ |
| 125 | public function display_section() { |
| 126 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 127 | echo Permalink_Manager_UI_Elements::get_plugin_sections_html( $this->sections, $this->active_section, $this->active_subsection ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Register the CSS & JS files for the plugin's dashboard |
| 132 | */ |
| 133 | public function enqueue_cssjs() { |
| 134 | wp_enqueue_style( 'permalink-manager-plugins', PERMALINK_MANAGER_URL . '/out/permalink-manager-plugins.css', array(), PERMALINK_MANAGER_VERSION ); |
| 135 | wp_enqueue_style( 'permalink-manager', PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.css', array( 'permalink-manager-plugins' ), PERMALINK_MANAGER_VERSION ); |
| 136 | |
| 137 | wp_enqueue_script( 'permalink-manager-plugins', PERMALINK_MANAGER_URL . '/out/permalink-manager-plugins.js', array( 'jquery', ), PERMALINK_MANAGER_VERSION, array( 'in_footer' => false ) ); |
| 138 | wp_enqueue_script( 'permalink-manager', PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.js', array( 'jquery', 'permalink-manager-plugins' ), PERMALINK_MANAGER_VERSION, array( 'in_footer' => false ) ); |
| 139 | |
| 140 | if ( isset( $_GET['section'] ) && $_GET['section'] === 'permastructs' ) { |
| 141 | wp_enqueue_script( 'thickbox' ); |
| 142 | wp_enqueue_style( 'thickbox' ); |
| 143 | } |
| 144 | |
| 145 | wp_localize_script( 'permalink-manager', 'permalink_manager', array( |
| 146 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 147 | 'url' => PERMALINK_MANAGER_URL, |
| 148 | 'confirm' => __( 'Are you sure? This action cannot be undone!', 'permalink-manager' ), |
| 149 | 'spinners' => admin_url( 'images' ) |
| 150 | ) ); |
| 151 | |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get the URL of the plugin's dashboard |
| 156 | * |
| 157 | * @param string $append |
| 158 | * |
| 159 | * @return string |
| 160 | */ |
| 161 | public static function get_admin_url( $append = '' ) { |
| 162 | //return menu_page_url(PERMALINK_MANAGER_PLUGIN_SLUG, false) . $append; |
| 163 | $admin_page = sprintf( "tools.php?page=%s", PERMALINK_MANAGER_PLUGIN_SLUG . $append ); |
| 164 | |
| 165 | return admin_url( $admin_page ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Add shortcut links for Permalink Manager on "Plugins" page |
| 170 | * |
| 171 | * @param array $links |
| 172 | * |
| 173 | * @return array |
| 174 | */ |
| 175 | public function plugins_page_links( $links ) { |
| 176 | $new_links = array( |
| 177 | sprintf( '<a href="%s">%s</a>', $this->get_admin_url(), __( 'URI Editor', 'permalink-manager' ) ), |
| 178 | sprintf( '<a href="%s">%s</a>', $this->get_admin_url( '§ion=settings' ), __( 'Settings', 'permalink-manager' ) ), |
| 179 | ); |
| 180 | |
| 181 | return array_merge( $links, $new_links ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Add shortcut meta links for Permalink Manager on "Plugins" page |
| 186 | * |
| 187 | * @param array $links |
| 188 | * @param string $file |
| 189 | * |
| 190 | * @return array |
| 191 | */ |
| 192 | public function plugins_page_meta( $links, $file ) { |
| 193 | if ( $file == PERMALINK_MANAGER_BASENAME ) { |
| 194 | $new_links = array( |
| 195 | 'doc' => sprintf( '<a href="%s?utm_source=plugin_admin_page" target="_blank">%s</a>', 'https://permalinkmanager.pro/docs/', __( 'Documentation', 'permalink-manager' ) ) |
| 196 | ); |
| 197 | |
| 198 | if ( ! defined( 'PERMALINK_MANAGER_PRO' ) ) { |
| 199 | $new_links['upgrade'] = sprintf( '<a href="%s" target="_blank"><strong>%s</strong></a>', PERMALINK_MANAGER_PROMO, __( 'Buy Permalink Manager Pro', 'permalink-manager' ) ); |
| 200 | } |
| 201 | |
| 202 | $links = array_merge( $links, $new_links ); |
| 203 | } |
| 204 | |
| 205 | return $links; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Check if URI Editor should be displayed for current user |
| 210 | * |
| 211 | * @return bool |
| 212 | */ |
| 213 | public static function current_user_can_edit_uris() { |
| 214 | global $permalink_manager_options; |
| 215 | |
| 216 | $edit_uris_cap = ( ! empty( $permalink_manager_options['general']['edit_uris_cap'] ) ) ? $permalink_manager_options['general']['edit_uris_cap'] : 'publish_posts'; |
| 217 | |
| 218 | return current_user_can( $edit_uris_cap ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Display global notices (throughout wp-admin dashboard) |
| 223 | */ |
| 224 | function display_global_notices() { |
| 225 | global $permalink_manager_alerts, $active_section; |
| 226 | |
| 227 | $html = ""; |
| 228 | if ( ! empty( $permalink_manager_alerts ) && is_array( $permalink_manager_alerts ) ) { |
| 229 | foreach ( $permalink_manager_alerts as $alert_id => $alert ) { |
| 230 | $dismissed_transient_name = sprintf( 'permalink-manager-notice_%s', sanitize_title( $alert_id ) ); |
| 231 | $dismissed = get_transient( $dismissed_transient_name ); |
| 232 | |
| 233 | // Check if alert was dismissed |
| 234 | if ( empty( $dismissed ) ) { |
| 235 | // Hide notice in Permalink Manager Pro |
| 236 | if ( defined( 'PERMALINK_MANAGER_PRO' ) && $alert['show'] == 'pro_hide' ) { |
| 237 | continue; |
| 238 | } |
| 239 | |
| 240 | // Display the notice only on the plugin pages |
| 241 | if ( empty( $active_section ) && ! empty( $alert['plugin_only'] ) ) { |
| 242 | continue; |
| 243 | } |
| 244 | |
| 245 | // Check if the notice did not expire |
| 246 | if ( isset( $alert['until'] ) && ( time() > strtotime( $alert['until'] ) ) ) { |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | $html .= Permalink_Manager_UI_Elements::get_alert_message( $alert['txt'], $alert['type'], true, $alert_id ); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | echo wp_kses_post( $html ); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Display notices generated by Permalink Manager tools |
| 260 | */ |
| 261 | function display_plugin_notices() { |
| 262 | global $permalink_manager_before_sections_html; |
| 263 | |
| 264 | echo wp_kses_post( $permalink_manager_before_sections_html ); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Get the list of all duplicated redirects and custom permalinks |
| 269 | * |
| 270 | * @param bool $include_custom_uris |
| 271 | * |
| 272 | * @return array |
| 273 | */ |
| 274 | public static function get_all_duplicates( $include_custom_uris = true ) { |
| 275 | global $permalink_manager_redirects; |
| 276 | |
| 277 | // Make sure that both variables are arrays |
| 278 | $all_uris = ( $include_custom_uris ) ? Permalink_Manager_URI_Functions::get_all_uris() : array(); |
| 279 | $permalink_manager_redirects = ( is_array( $permalink_manager_redirects ) ) ? $permalink_manager_redirects : array(); |
| 280 | |
| 281 | // Convert redirects list, so it can be merged with custom permalinks array |
| 282 | foreach ( $permalink_manager_redirects as $element_id => $redirects ) { |
| 283 | if ( is_array( $redirects ) ) { |
| 284 | foreach ( $redirects as $index => $uri ) { |
| 285 | $all_uris["redirect-{$index}_{$element_id}"] = $uri; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // Count duplicates |
| 291 | $duplicates_groups = array(); |
| 292 | $duplicates_list = array_count_values( $all_uris ); |
| 293 | $duplicates_list = array_filter( $duplicates_list, function ( $x ) { |
| 294 | return $x >= 2; |
| 295 | } ); |
| 296 | |
| 297 | // Assign keys to duplicates (group them) |
| 298 | if ( count( $duplicates_list ) > 0 ) { |
| 299 | foreach ( $duplicates_list as $duplicated_uri => $count ) { |
| 300 | $duplicated_ids = array_keys( $all_uris, $duplicated_uri ); |
| 301 | |
| 302 | // Ignore duplicates in different langauges |
| 303 | if ( Permalink_Manager_URI_Functions::is_uri_duplicated( $duplicated_uri, $duplicated_ids[0], $duplicated_ids ) ) { |
| 304 | $duplicates_groups[ $duplicated_uri ] = $duplicated_ids; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return $duplicates_groups; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Check if Permalink Manager Pro is active |
| 314 | * |
| 315 | * @return bool |
| 316 | */ |
| 317 | public static function is_pro_active() { |
| 318 | if ( defined( 'PERMALINK_MANAGER_PRO' ) && class_exists( 'Permalink_Manager_Pro_License' ) ) { |
| 319 | // Check if license is active |
| 320 | $exp_date = Permalink_Manager_Pro_License::get_expiration_date( true ); |
| 321 | |
| 322 | $is_pro = ( $exp_date > 2 ) ? false : true; |
| 323 | } else { |
| 324 | $is_pro = false; |
| 325 | } |
| 326 | |
| 327 | return $is_pro; |
| 328 | } |
| 329 | } |
| 330 |