permalink-manager
Last commit date
includes
2 years ago
languages
2 years ago
out
2 years ago
LICENSE.txt
2 years ago
README.txt
2 years ago
permalink-manager.php
2 years ago
permalink-manager.php
403 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Plugin Name: Permalink Manager Lite |
| 5 | * Plugin URI: https://permalinkmanager.pro?utm_source=plugin |
| 6 | * Description: Advanced plugin that allows to set up custom permalinks (bulk editors included), slugs and permastructures (WooCommerce compatible). |
| 7 | * Version: 2.4.1.5 |
| 8 | * Author: Maciej Bis |
| 9 | * Author URI: http://maciejbis.net/ |
| 10 | * License: GPL-2.0+ |
| 11 | * License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
| 12 | * Text Domain: permalink-manager |
| 13 | * Domain Path: /languages |
| 14 | * WC requires at least: 3.0.0 |
| 15 | * WC tested up to: 8.1.1 |
| 16 | */ |
| 17 | |
| 18 | // If this file is called directly or plugin is already defined, abort |
| 19 | if ( ! defined( 'WPINC' ) ) { |
| 20 | die; |
| 21 | } |
| 22 | |
| 23 | if ( ! class_exists( 'Permalink_Manager_Class' ) ) { |
| 24 | |
| 25 | // Define the directories used to load plugin files. |
| 26 | define( 'PERMALINK_MANAGER_PLUGIN_NAME', 'Permalink Manager' ); |
| 27 | define( 'PERMALINK_MANAGER_PLUGIN_SLUG', 'permalink-manager' ); |
| 28 | define( 'PERMALINK_MANAGER_VERSION', '2.4.1.5' ); |
| 29 | define( 'PERMALINK_MANAGER_FILE', __FILE__ ); |
| 30 | define( 'PERMALINK_MANAGER_DIR', untrailingslashit( dirname( __FILE__ ) ) ); |
| 31 | define( 'PERMALINK_MANAGER_BASENAME', plugin_basename( __FILE__ ) ); |
| 32 | define( 'PERMALINK_MANAGER_URL', untrailingslashit( plugins_url( '', __FILE__ ) ) ); |
| 33 | define( 'PERMALINK_MANAGER_WEBSITE', 'https://permalinkmanager.pro?utm_source=plugin' ); |
| 34 | |
| 35 | /** |
| 36 | * The base class responsible for loading the plugin data as well as any plugin subclasses and additional functions |
| 37 | */ |
| 38 | class Permalink_Manager_Class { |
| 39 | |
| 40 | public $permalink_manager_options; |
| 41 | public $sections, $functions; |
| 42 | |
| 43 | /** |
| 44 | * Get options from DB, load subclasses & hooks |
| 45 | */ |
| 46 | public function __construct() { |
| 47 | $this->include_subclasses(); |
| 48 | $this->register_init_hooks(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Include back-end classes and set their instances |
| 53 | */ |
| 54 | function include_subclasses() { |
| 55 | // WP_List_Table needed for post types & taxonomies editors |
| 56 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 57 | require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
| 58 | } |
| 59 | |
| 60 | $classes = array( |
| 61 | 'core' => array( |
| 62 | 'helper-functions' => 'Permalink_Manager_Helper_Functions', |
| 63 | 'uri-functions' => 'Permalink_Manager_URI_Functions', |
| 64 | 'uri-functions-post' => 'Permalink_Manager_URI_Functions_Post', |
| 65 | 'uri-functions-tax' => 'Permalink_Manager_URI_Functions_Tax', |
| 66 | 'admin-functions' => 'Permalink_Manager_Admin_Functions', |
| 67 | 'actions' => 'Permalink_Manager_Actions', |
| 68 | 'core-functions' => 'Permalink_Manager_Core_Functions', |
| 69 | 'gutenberg' => 'Permalink_Manager_Gutenberg', |
| 70 | 'debug' => 'Permalink_Manager_Debug_Functions', |
| 71 | 'pro-functions' => 'Permalink_Manager_Pro_Functions' |
| 72 | ), |
| 73 | 'integrations' => array( |
| 74 | 'third-parties' => 'Permalink_Manager_Third_Parties', |
| 75 | 'woocommerce' => 'Permalink_Manager_WooCommerce', |
| 76 | 'seo-plugins' => 'Permalink_Manager_SEO_Plugins', |
| 77 | 'language-plugins' => 'Permalink_Manager_Language_Plugins' |
| 78 | ), |
| 79 | 'views' => array( |
| 80 | 'ui-elements' => 'Permalink_Manager_UI_Elements', |
| 81 | 'uri-editor' => 'Permalink_Manager_URI_Editor', |
| 82 | 'tools' => 'Permalink_Manager_Tools', |
| 83 | 'permastructs' => 'Permalink_Manager_Permastructs', |
| 84 | 'settings' => 'Permalink_Manager_Settings', |
| 85 | 'debug' => 'Permalink_Manager_Debug', |
| 86 | 'pro-addons' => 'Permalink_Manager_Pro_Addons', |
| 87 | 'help' => 'Permalink_Manager_Help', |
| 88 | 'uri-editor-tax' => false, |
| 89 | 'uri-editor-post' => false |
| 90 | ) |
| 91 | ); |
| 92 | |
| 93 | // Load classes and set-up their instances |
| 94 | foreach ( $classes as $class_type => $classes_array ) { |
| 95 | foreach ( $classes_array as $class => $class_name ) { |
| 96 | $filename = PERMALINK_MANAGER_DIR . "/includes/{$class_type}/permalink-manager-{$class}.php"; |
| 97 | |
| 98 | if ( file_exists( $filename ) ) { |
| 99 | require_once $filename; |
| 100 | if ( $class_name ) { |
| 101 | $this->functions[ $class ] = new $class_name(); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Register general hooks |
| 110 | */ |
| 111 | public function register_init_hooks() { |
| 112 | // Localize plugin |
| 113 | add_action( 'init', array( $this, 'localize_me' ), 1 ); |
| 114 | |
| 115 | // Support deprecated hooks |
| 116 | add_action( 'plugins_loaded', array( $this, 'deprecated_hooks' ), 9 ); |
| 117 | |
| 118 | // Deactivate free version if Permalink Manager Pro is activated |
| 119 | add_action( 'plugins_loaded', array( $this, 'is_pro_activated' ), 9 ); |
| 120 | |
| 121 | // Load globals & options |
| 122 | add_action( 'plugins_loaded', array( $this, 'get_options_and_globals' ), 9 ); |
| 123 | |
| 124 | // Legacy support |
| 125 | add_action( 'init', array( $this, 'legacy_support' ), 2 ); |
| 126 | |
| 127 | // Default settings & alerts |
| 128 | add_filter( 'permalink_manager_options', array( $this, 'default_settings' ), 1 ); |
| 129 | add_filter( 'permalink_manager_alerts', array( $this, 'default_alerts' ), 1 ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Localize this plugin |
| 134 | */ |
| 135 | function localize_me() { |
| 136 | load_plugin_textdomain( 'permalink-manager', false, basename( dirname( __FILE__ ) ) . "/languages" ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get options values & set global variables |
| 141 | */ |
| 142 | public function get_options_and_globals() { |
| 143 | // 1. Globals with data stored in DB |
| 144 | global $permalink_manager_options, $permalink_manager_uris, $permalink_manager_permastructs, $permalink_manager_redirects, $permalink_manager_external_redirects; |
| 145 | |
| 146 | $permalink_manager_options = (array) apply_filters( 'permalink_manager_options', get_option( 'permalink-manager', array() ) ); |
| 147 | $permalink_manager_uris = (array) apply_filters( 'permalink_manager_uris', get_option( 'permalink-manager-uris', array() ) ); |
| 148 | $permalink_manager_permastructs = (array) apply_filters( 'permalink_manager_permastructs', get_option( 'permalink-manager-permastructs', array() ) ); |
| 149 | $permalink_manager_redirects = (array) apply_filters( 'permalink_manager_redirects', get_option( 'permalink-manager-redirects', array() ) ); |
| 150 | $permalink_manager_external_redirects = (array) apply_filters( 'permalink_manager_external_redirects', get_option( 'permalink-manager-external-redirects', array() ) ); |
| 151 | |
| 152 | // 2. Globals used to display additional content (eg. alerts) |
| 153 | global $permalink_manager_alerts, $permalink_manager_before_sections_html, $permalink_manager_after_sections_html; |
| 154 | |
| 155 | $permalink_manager_alerts = apply_filters( 'permalink_manager_alerts', array() ); |
| 156 | $permalink_manager_before_sections_html = apply_filters( 'permalink_manager_before_sections', '' ); |
| 157 | $permalink_manager_after_sections_html = apply_filters( 'permalink_manager_after_sections', '' ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Set the initial/default settings (including "Screen Options") |
| 162 | * |
| 163 | * @param array $settings |
| 164 | * |
| 165 | * @return array |
| 166 | */ |
| 167 | public function default_settings( $settings ) { |
| 168 | $default_settings = apply_filters( 'permalink_manager_default_options', array( |
| 169 | 'screen-options' => array( |
| 170 | 'per_page' => 20, |
| 171 | 'post_statuses' => array( 'publish' ), |
| 172 | 'group' => false |
| 173 | ), |
| 174 | 'general' => array( |
| 175 | 'auto_update_uris' => 0, |
| 176 | 'show_native_slug_field' => 0, |
| 177 | 'pagination_redirect' => 0, |
| 178 | 'sslwww_redirect' => 1, |
| 179 | 'canonical_redirect' => 1, |
| 180 | 'old_slug_redirect' => 0, |
| 181 | 'setup_redirects' => 0, |
| 182 | 'redirect' => '301', |
| 183 | 'extra_redirects' => 1, |
| 184 | 'copy_query_redirect' => 1, |
| 185 | 'trailing_slashes' => 0, |
| 186 | 'trailing_slash_redirect' => 0, |
| 187 | 'auto_fix_duplicates' => 0, |
| 188 | 'fix_language_mismatch' => 1, |
| 189 | 'wpml_support' => 1, |
| 190 | 'pmxi_support' => 1, |
| 191 | 'um_support' => 1, |
| 192 | 'yoast_breadcrumbs' => 0, |
| 193 | 'rankmath_redirect' => 1, |
| 194 | 'primary_category' => 1, |
| 195 | 'force_custom_slugs' => 0, |
| 196 | 'disable_slug_sanitization' => 0, |
| 197 | 'keep_accents' => 0, |
| 198 | 'partial_disable' => array( |
| 199 | 'post_types' => array( 'attachment', 'tribe_events', 'e-landing-page' ) |
| 200 | ), |
| 201 | 'partial_disable_strict' => 1, |
| 202 | 'ignore_drafts' => 1, |
| 203 | 'edit_uris_cap' => 'publish_posts' |
| 204 | ), |
| 205 | 'licence' => array() |
| 206 | ) ); |
| 207 | |
| 208 | // Check if settings array is empty |
| 209 | $settings_empty = empty( $settings ); |
| 210 | |
| 211 | // Apply the default settings (if empty values) in all settings sections |
| 212 | foreach ( $default_settings as $group_name => $fields ) { |
| 213 | foreach ( $fields as $field_name => $field ) { |
| 214 | if ( $settings_empty || ( ! isset( $settings[ $group_name ][ $field_name ] ) && strpos( $field_name, 'partial_disable' ) === false ) ) { |
| 215 | $settings[ $group_name ][ $field_name ] = $field; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return $settings; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Set the initial/default admin notices |
| 225 | * |
| 226 | * @param array $alerts |
| 227 | * |
| 228 | * @return array |
| 229 | */ |
| 230 | public function default_alerts( $alerts ) { |
| 231 | $default_alerts = apply_filters( 'permalink_manager_default_alerts', array( |
| 232 | 'sample-alert' => array( |
| 233 | 'txt' => '', |
| 234 | 'type' => 'notice-info', |
| 235 | 'show' => 'pro_hide', |
| 236 | 'plugin_only' => true, |
| 237 | 'until' => '2021-01-09' |
| 238 | ) |
| 239 | ) ); |
| 240 | |
| 241 | // Apply the default settings (if empty values) in all settings sections |
| 242 | return (array) $alerts + (array) $default_alerts; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Make sure that the Permalink Manager options stored in DB match the new structure |
| 247 | */ |
| 248 | function legacy_support() { |
| 249 | global $permalink_manager_permastructs, $permalink_manager_options; |
| 250 | |
| 251 | if ( isset( $permalink_manager_options['base-editor'] ) ) { |
| 252 | $new_options['post_types'] = $permalink_manager_options['base-editor']; |
| 253 | update_option( 'permalink-manager-permastructs', $new_options ); |
| 254 | } else if ( empty( $permalink_manager_permastructs['post_types'] ) && empty( $permalink_manager_permastructs['taxonomies'] ) && count( $permalink_manager_permastructs ) > 0 ) { |
| 255 | $new_options['post_types'] = $permalink_manager_permastructs; |
| 256 | update_option( 'permalink-manager-permastructs', $new_options ); |
| 257 | } |
| 258 | |
| 259 | // Adjust options structure |
| 260 | if ( ! empty( $permalink_manager_options['miscellaneous'] ) ) { |
| 261 | $permalink_manager_unfiltered_options = $permalink_manager_options; |
| 262 | |
| 263 | // Combine general & miscellaneous options |
| 264 | $permalink_manager_unfiltered_options['general'] = array_merge( $permalink_manager_unfiltered_options['general'], $permalink_manager_unfiltered_options['miscellaneous'] ); |
| 265 | |
| 266 | // Move licence key to different section |
| 267 | $permalink_manager_unfiltered_options['licence']['licence_key'] = ( ! empty( $permalink_manager_unfiltered_options['miscellaneous']['license_key'] ) ) ? $permalink_manager_unfiltered_options['miscellaneous']['license_key'] : ""; |
| 268 | } |
| 269 | |
| 270 | // Separate "Trailing slashes" & "Trailing slashes redirect" setting fields |
| 271 | if ( ! empty( $permalink_manager_options['general']['trailing_slashes'] ) && $permalink_manager_options['general']['trailing_slashes'] >= 10 ) { |
| 272 | $permalink_manager_unfiltered_options = ( ! empty( $permalink_manager_unfiltered_options ) ) ? $permalink_manager_unfiltered_options : $permalink_manager_options; |
| 273 | |
| 274 | $permalink_manager_unfiltered_options['general']['trailing_slashes_redirect'] = 1; |
| 275 | $permalink_manager_unfiltered_options['general']['trailing_slashes'] = ( $permalink_manager_options['general']['trailing_slashes'] == 10 ) ? 1 : 2; |
| 276 | } |
| 277 | |
| 278 | // Adjust WP All Import support mode |
| 279 | if ( isset( $permalink_manager_options['general']['pmxi_import_support'] ) ) { |
| 280 | $permalink_manager_unfiltered_options = ( ! empty( $permalink_manager_unfiltered_options ) ) ? $permalink_manager_unfiltered_options : $permalink_manager_options; |
| 281 | |
| 282 | $permalink_manager_unfiltered_options['general']['pmxi_support'] = ( empty( $permalink_manager_options['general']['pmxi_import_support'] ) ) ? 1 : 0; |
| 283 | unset( $permalink_manager_unfiltered_options['general']['pmxi_import_support'] ); |
| 284 | } |
| 285 | |
| 286 | // Save the settings in database |
| 287 | if ( ! empty( $permalink_manager_unfiltered_options ) ) { |
| 288 | update_option( 'permalink-manager', $permalink_manager_unfiltered_options ); |
| 289 | } |
| 290 | |
| 291 | // Remove obsolete 'permalink-manager-alerts' from wp_options table |
| 292 | if ( get_option( 'permalink-manager-alerts' ) ) { |
| 293 | delete_option( 'permalink-manager-alerts' ); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Return the array of deprecated hooks |
| 299 | * |
| 300 | * @return array |
| 301 | */ |
| 302 | function deprecated_hooks_list() { |
| 303 | return array( |
| 304 | 'permalink_manager_default_options' => 'permalink-manager-default-options', |
| 305 | 'permalink_manager_options' => 'permalink-manager-options', |
| 306 | 'permalink_manager_uris' => 'permalink-manager-uris', |
| 307 | 'permalink_manager_redirects' => 'permalink-manager-redirects', |
| 308 | 'permalink_manager_external_redirects' => 'permalink-manager-external-redirects', |
| 309 | 'permalink_manager_permastructs' => 'permalink-manager-permastructs', |
| 310 | |
| 311 | 'permalink_manager_alerts' => 'permalink-manager-alerts', |
| 312 | 'permalink_manager_before_sections' => 'permalink-manager-before-sections', |
| 313 | 'permalink_manager_sections' => 'permalink-manager-sections', |
| 314 | 'permalink_manager_after_sections' => 'permalink-manager-after-sections', |
| 315 | |
| 316 | 'permalink_manager_field_args' => 'permalink-manager-field-args', |
| 317 | 'permalink_manager_field_output' => 'permalink-manager-field-output', |
| 318 | |
| 319 | 'permalink_manager_deep_uri_detect' => 'permalink-manager-deep-uri-detect', |
| 320 | 'permalink_manager_detect_uri' => 'permalink-manager-detect-uri', |
| 321 | 'permalink_manager_detected_element_id' => 'permalink-manager-detected-initial-id', |
| 322 | 'permalink_manager_detected_term_id' => 'permalink-manager-detected-term-id', |
| 323 | 'permalink_manager_detected_post_id' => 'permalink-manager-detected-post-id', |
| 324 | |
| 325 | 'permalink_manager_primary_term' => 'permalink-manager-primary-term', |
| 326 | 'permalink_manager_disabled_post_types' => 'permalink-manager-disabled-post-types', |
| 327 | 'permalink_manager_disabled_taxonomies' => 'permalink-manager-disabled-taxonomies', |
| 328 | 'permalink_manager_endpoints' => 'permalink-manager-endpoints', |
| 329 | 'permalink_manager_filter_permalink_base' => 'permalink_manager-filter-permalink-base', |
| 330 | 'permalink_manager_force_lowercase_uris' => 'permalink-manager-force-lowercase-uris', |
| 331 | |
| 332 | 'permalink_manager_uri_editor_extra_info' => 'permalink-manager-uri-editor-extra-info', |
| 333 | 'permalink_manager_debug_fields' => 'permalink-manager-debug-fields', |
| 334 | 'permalink_manager_permastructs_fields' => 'permalink-manager-permastructs-fields', |
| 335 | 'permalink_manager_settings_fields' => 'permalink-manager-settings-fields', |
| 336 | 'permalink_manager_tools_fields' => 'permalink-manager-tools-fields', |
| 337 | |
| 338 | 'permalink_manager_uri_editor_columns' => 'permalink-manager-uri-editor-columns', |
| 339 | 'permalink_manager_uri_editor_column_content' => 'permalink-manager-uri-editor-column-content', |
| 340 | |
| 341 | 'permalink_manager_redirect_shop_archive' => 'permalink-manager-redirect-shop-archive' |
| 342 | ); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Map the deprecated hooks to their relevant equivalents. |
| 347 | */ |
| 348 | function deprecated_hooks() { |
| 349 | $deprecated_filters = $this->deprecated_hooks_list(); |
| 350 | foreach ( $deprecated_filters as $new => $old ) { |
| 351 | add_filter( $new, array( $this, 'deprecated_hooks_mapping' ), - 1000, 8 ); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Apply the deprecated filters to the relevant hooks |
| 357 | * |
| 358 | * @param mixed $data |
| 359 | * |
| 360 | * @return mixed |
| 361 | */ |
| 362 | function deprecated_hooks_mapping( $data ) { |
| 363 | $deprecated_filters = $this->deprecated_hooks_list(); |
| 364 | $filter = current_filter(); |
| 365 | |
| 366 | if ( isset( $deprecated_filters[ $filter ] ) ) { |
| 367 | if ( has_filter( $deprecated_filters[ $filter ] ) ) { |
| 368 | $args = func_get_args(); |
| 369 | $data = apply_filters_ref_array( $deprecated_filters[ $filter ], $args ); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | return $data; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Deactivate Permalink Manager Lite if Permalink Manager Pro is enabled |
| 378 | */ |
| 379 | function is_pro_activated() { |
| 380 | if ( function_exists( 'is_plugin_active' ) && is_plugin_active( 'permalink-manager/permalink-manager.php' ) && is_plugin_active( 'permalink-manager-pro/permalink-manager.php' ) ) { |
| 381 | deactivate_plugins( 'permalink-manager/permalink-manager.php' ); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Begins execution of the plugin |
| 389 | */ |
| 390 | function run_permalink_manager() { |
| 391 | global $permalink_manager; |
| 392 | |
| 393 | // Do not run when Elementor is opened |
| 394 | if ( ( ! empty( $_REQUEST['action'] ) && is_string( $_REQUEST['action'] ) && strpos( $_REQUEST['action'], 'elementor' ) !== false ) || isset( $_REQUEST['elementor-preview'] ) ) { |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | $permalink_manager = new Permalink_Manager_Class(); |
| 399 | } |
| 400 | |
| 401 | run_permalink_manager(); |
| 402 | } |
| 403 |