permalink-manager
Last commit date
includes
3 months ago
languages
3 months ago
out
3 months ago
LICENSE.txt
1 year ago
README.txt
3 months ago
permalink-manager.php
3 months ago
permalink-manager.php
398 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.5.3.2 |
| 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: 10.6.0 |
| 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.5.3.2' ); |
| 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 | define( 'PERMALINK_MANAGER_PROMO', 'https://permalinkmanager.pro/features/?utm_source=plugin' ); |
| 35 | |
| 36 | /** |
| 37 | * The base class responsible for loading the plugin data as well as any plugin subclasses and additional functions |
| 38 | */ |
| 39 | class Permalink_Manager_Class { |
| 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 | 'permastructures-functions' => 'Permalink_Manager_Permastructure_Functions', |
| 70 | 'gutenberg' => 'Permalink_Manager_Gutenberg', |
| 71 | 'debug' => 'Permalink_Manager_Debug_Functions', |
| 72 | 'pro-license' => 'Permalink_Manager_Pro_License', |
| 73 | 'pro-functions' => 'Permalink_Manager_Pro_Functions' |
| 74 | ), |
| 75 | 'integrations' => array( |
| 76 | 'third-parties' => 'Permalink_Manager_Third_Parties', |
| 77 | 'woocommerce' => 'Permalink_Manager_WooCommerce', |
| 78 | 'seo-plugins' => 'Permalink_Manager_SEO_Plugins', |
| 79 | 'language-plugins' => 'Permalink_Manager_Language_Plugins' |
| 80 | ), |
| 81 | 'views' => array( |
| 82 | 'ui-elements' => 'Permalink_Manager_UI_Elements', |
| 83 | 'uri-editor' => 'Permalink_Manager_URI_Editor', |
| 84 | 'tools' => 'Permalink_Manager_Tools', |
| 85 | 'permastructures' => 'Permalink_Manager_Permastructs', |
| 86 | 'settings' => 'Permalink_Manager_Settings', |
| 87 | 'debug' => 'Permalink_Manager_Debug', |
| 88 | 'pro-addons' => 'Permalink_Manager_Pro_Addons', |
| 89 | 'help' => 'Permalink_Manager_Help', |
| 90 | 'uri-editor-tax' => false, |
| 91 | 'uri-editor-post' => false |
| 92 | ) |
| 93 | ); |
| 94 | |
| 95 | // Load classes and set-up their instances |
| 96 | foreach ( $classes as $class_type => $classes_array ) { |
| 97 | foreach ( $classes_array as $class => $class_name ) { |
| 98 | $filename = PERMALINK_MANAGER_DIR . "/includes/{$class_type}/permalink-manager-{$class}.php"; |
| 99 | |
| 100 | if ( file_exists( $filename ) ) { |
| 101 | require_once $filename; |
| 102 | if ( $class_name ) { |
| 103 | $this->functions[ $class ] = new $class_name(); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Register general hooks |
| 112 | */ |
| 113 | public function register_init_hooks() { |
| 114 | // Localize plugin |
| 115 | add_action( 'init', array( $this, 'localize_me' ), 1 ); |
| 116 | |
| 117 | // Support deprecated hooks |
| 118 | add_action( 'plugins_loaded', array( $this, 'deprecated_hooks' ), 9 ); |
| 119 | |
| 120 | // Deactivate free version if Permalink Manager Pro is activated |
| 121 | add_action( 'plugins_loaded', array( $this, 'is_pro_activated' ), 9 ); |
| 122 | |
| 123 | // Load globals & options |
| 124 | add_action( 'plugins_loaded', array( $this, 'get_options_and_globals' ), 9 ); |
| 125 | add_action( 'init', array( $this, 'get_output_globals' ), 9 ); |
| 126 | |
| 127 | // Legacy support |
| 128 | add_action( 'init', array( $this, 'legacy_support' ), 2 ); |
| 129 | |
| 130 | // Default settings & alerts |
| 131 | add_filter( 'permalink_manager_options', array( $this, 'default_settings' ), 1 ); |
| 132 | add_filter( 'permalink_manager_alerts', array( $this, 'default_alerts' ), 1 ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Localize this plugin |
| 137 | */ |
| 138 | function localize_me() { |
| 139 | // phpcs:ignore PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound -- Make sure that the same textdomain is loaded for both Lite and Pro versions |
| 140 | load_plugin_textdomain( 'permalink-manager', false, basename( dirname( __FILE__ ) ) . "/languages" ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get options values & set global variables |
| 145 | */ |
| 146 | public function get_options_and_globals() { |
| 147 | // 1. Globals with data stored in DB |
| 148 | global $permalink_manager_options, $permalink_manager_uris, $permalink_manager_permastructs, $permalink_manager_redirects, $permalink_manager_external_redirects; |
| 149 | |
| 150 | $permalink_manager_options = (array) apply_filters( 'permalink_manager_options', get_option( 'permalink-manager', array() ) ); |
| 151 | $permalink_manager_uris = (array) apply_filters( 'permalink_manager_uris', get_option( 'permalink-manager-uris', array() ) ); |
| 152 | $permalink_manager_permastructs = (array) apply_filters( 'permalink_manager_permastructs', get_option( 'permalink-manager-permastructs', array() ) ); |
| 153 | $permalink_manager_redirects = (array) apply_filters( 'permalink_manager_redirects', get_option( 'permalink-manager-redirects', array() ) ); |
| 154 | $permalink_manager_external_redirects = (array) apply_filters( 'permalink_manager_external_redirects', get_option( 'permalink-manager-external-redirects', array() ) ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Get global variables |
| 159 | */ |
| 160 | public function get_output_globals() { |
| 161 | // 2. Globals used to display additional content (e.g. alerts) |
| 162 | global $permalink_manager_alerts, $permalink_manager_before_sections_html, $permalink_manager_after_sections_html; |
| 163 | |
| 164 | $permalink_manager_alerts = apply_filters( 'permalink_manager_alerts', array() ); |
| 165 | $permalink_manager_before_sections_html = apply_filters( 'permalink_manager_before_sections', '' ); |
| 166 | $permalink_manager_after_sections_html = apply_filters( 'permalink_manager_after_sections', '' ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Set the initial/default settings (including "Screen Options") |
| 171 | * |
| 172 | * @param array $settings |
| 173 | * |
| 174 | * @return array |
| 175 | */ |
| 176 | public function default_settings( $settings ) { |
| 177 | $default_settings = apply_filters( 'permalink_manager_default_options', array( |
| 178 | 'screen-options' => array( |
| 179 | 'per_page' => 20, |
| 180 | 'post_statuses' => array( 'publish' ), |
| 181 | 'group' => false |
| 182 | ), |
| 183 | 'general' => array( |
| 184 | 'auto_update_uris' => 0, |
| 185 | 'show_native_slug_field' => 0, |
| 186 | 'pagination_redirect' => 0, |
| 187 | 'sslwww_redirect' => 1, |
| 188 | 'canonical_redirect' => 1, |
| 189 | 'old_slug_redirect' => 0, |
| 190 | 'setup_redirects' => 0, |
| 191 | 'redirect' => '301', |
| 192 | 'extra_redirects' => 1, |
| 193 | 'copy_query_redirect' => 1, |
| 194 | 'trailing_slashes' => 0, |
| 195 | 'trailing_slash_redirect' => 1, |
| 196 | 'auto_fix_duplicates' => 0, |
| 197 | 'fix_language_mismatch' => 0, |
| 198 | 'wpml_support' => 1, |
| 199 | 'wpml_translate_mode' => 0, |
| 200 | 'pmxi_support' => 1, |
| 201 | 'um_support' => 1, |
| 202 | 'yoast_breadcrumbs' => 0, |
| 203 | 'rankmath_redirect' => 1, |
| 204 | 'primary_category' => 1, |
| 205 | 'force_custom_slugs' => 0, |
| 206 | 'disable_slug_sanitization' => 0, |
| 207 | 'keep_accents' => 0, |
| 208 | 'partial_disable' => array( |
| 209 | 'post_types' => array( 'attachment', 'tribe_events', 'e-landing-page' ) |
| 210 | ), |
| 211 | 'partial_disable_strict' => 1, |
| 212 | 'ignore_drafts' => 1, |
| 213 | 'edit_uris_cap' => 'publish_posts', |
| 214 | 'force_unique_uris' => 0, |
| 215 | 'debug_mode' => 0 |
| 216 | ), |
| 217 | 'licence' => array() |
| 218 | ) ); |
| 219 | |
| 220 | // Check if settings array is empty |
| 221 | $settings_empty = empty( $settings ); |
| 222 | |
| 223 | // Apply the default settings (if empty values) in all settings sections |
| 224 | foreach ( $default_settings as $group_name => $fields ) { |
| 225 | foreach ( $fields as $field_name => $field ) { |
| 226 | if ( $settings_empty || ( ! isset( $settings[ $group_name ][ $field_name ] ) && strpos( $field_name, 'partial_disable' ) === false ) ) { |
| 227 | $settings[ $group_name ][ $field_name ] = $field; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return $settings; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Set the initial/default admin notices |
| 237 | * |
| 238 | * @param array $alerts |
| 239 | * |
| 240 | * @return array |
| 241 | */ |
| 242 | public function default_alerts( $alerts ) { |
| 243 | $default_alerts = apply_filters( 'permalink_manager_default_alerts', array( |
| 244 | 'sample-alert' => array( |
| 245 | 'txt' => '', |
| 246 | 'type' => 'notice-info', |
| 247 | 'show' => 'pro_hide', |
| 248 | 'plugin_only' => true, |
| 249 | 'until' => '2021-01-09' |
| 250 | ) |
| 251 | ) ); |
| 252 | |
| 253 | // Apply the default settings (if empty values) in all settings sections |
| 254 | return (array) $alerts + (array) $default_alerts; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Make sure that the Permalink Manager options stored in DB match the new structure |
| 259 | */ |
| 260 | function legacy_support() { |
| 261 | global $permalink_manager_permastructs, $permalink_manager_options; |
| 262 | |
| 263 | if ( isset( $permalink_manager_options['base-editor'] ) ) { |
| 264 | $new_options['post_types'] = $permalink_manager_options['base-editor']; |
| 265 | update_option( 'permalink-manager-permastructs', $new_options ); |
| 266 | } else if ( empty( $permalink_manager_permastructs['post_types'] ) && empty( $permalink_manager_permastructs['taxonomies'] ) && count( $permalink_manager_permastructs ) > 0 ) { |
| 267 | $new_options['post_types'] = $permalink_manager_permastructs; |
| 268 | update_option( 'permalink-manager-permastructs', $new_options ); |
| 269 | } |
| 270 | |
| 271 | // Separate "Trailing slashes" & "Trailing slashes redirect" setting fields |
| 272 | if ( ! empty( $permalink_manager_options['general']['trailing_slashes'] ) && $permalink_manager_options['general']['trailing_slashes'] >= 10 ) { |
| 273 | $permalink_manager_unfiltered_options = ( ! empty( $permalink_manager_unfiltered_options ) ) ? $permalink_manager_unfiltered_options : $permalink_manager_options; |
| 274 | |
| 275 | $permalink_manager_unfiltered_options['general']['trailing_slashes_redirect'] = 1; |
| 276 | $permalink_manager_unfiltered_options['general']['trailing_slashes'] = ( $permalink_manager_options['general']['trailing_slashes'] == 10 ) ? 1 : 2; |
| 277 | } |
| 278 | |
| 279 | // Save the settings in database |
| 280 | if ( ! empty( $permalink_manager_unfiltered_options ) ) { |
| 281 | update_option( 'permalink-manager', $permalink_manager_unfiltered_options ); |
| 282 | } |
| 283 | |
| 284 | // Remove obsolete 'permalink-manager-alerts' from wp_options table |
| 285 | if ( get_option( 'permalink-manager-alerts' ) ) { |
| 286 | delete_option( 'permalink-manager-alerts' ); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Return the array of deprecated hooks |
| 292 | * |
| 293 | * @return array |
| 294 | */ |
| 295 | function deprecated_hooks_list() { |
| 296 | return array( |
| 297 | 'permalink_manager_default_options' => 'permalink-manager-default-options', |
| 298 | 'permalink_manager_options' => 'permalink-manager-options', |
| 299 | 'permalink_manager_uris' => 'permalink-manager-uris', |
| 300 | 'permalink_manager_redirects' => 'permalink-manager-redirects', |
| 301 | 'permalink_manager_external_redirects' => 'permalink-manager-external-redirects', |
| 302 | 'permalink_manager_permastructs' => 'permalink-manager-permastructs', |
| 303 | |
| 304 | 'permalink_manager_alerts' => 'permalink-manager-alerts', |
| 305 | 'permalink_manager_before_sections' => 'permalink-manager-before-sections', |
| 306 | 'permalink_manager_sections' => 'permalink-manager-sections', |
| 307 | 'permalink_manager_after_sections' => 'permalink-manager-after-sections', |
| 308 | |
| 309 | 'permalink_manager_field_args' => 'permalink-manager-field-args', |
| 310 | 'permalink_manager_field_output' => 'permalink-manager-field-output', |
| 311 | |
| 312 | 'permalink_manager_deep_uri_detect' => 'permalink-manager-deep-uri-detect', |
| 313 | 'permalink_manager_detect_uri' => 'permalink-manager-detect-uri', |
| 314 | 'permalink_manager_detected_element_id' => 'permalink-manager-detected-initial-id', |
| 315 | 'permalink_manager_detected_term_id' => 'permalink-manager-detected-term-id', |
| 316 | 'permalink_manager_detected_post_id' => 'permalink-manager-detected-post-id', |
| 317 | |
| 318 | 'permalink_manager_primary_term' => 'permalink-manager-primary-term', |
| 319 | 'permalink_manager_disabled_post_types' => 'permalink-manager-disabled-post-types', |
| 320 | 'permalink_manager_disabled_taxonomies' => 'permalink-manager-disabled-taxonomies', |
| 321 | 'permalink_manager_endpoints' => 'permalink-manager-endpoints', |
| 322 | 'permalink_manager_filter_permalink_base' => 'permalink_manager-filter-permalink-base', |
| 323 | 'permalink_manager_force_lowercase_uris' => 'permalink-manager-force-lowercase-uris', |
| 324 | |
| 325 | 'permalink_manager_uri_editor_extra_info' => 'permalink-manager-uri-editor-extra-info', |
| 326 | 'permalink_manager_debug_fields' => 'permalink-manager-debug-fields', |
| 327 | 'permalink_manager_permastructs_fields' => 'permalink-manager-permastructs-fields', |
| 328 | 'permalink_manager_settings_fields' => 'permalink-manager-settings-fields', |
| 329 | 'permalink_manager_tools_fields' => 'permalink-manager-tools-fields', |
| 330 | |
| 331 | 'permalink_manager_uri_editor_columns' => 'permalink-manager-uri-editor-columns', |
| 332 | 'permalink_manager_uri_editor_column_content' => 'permalink-manager-uri-editor-column-content', |
| 333 | |
| 334 | 'permalink_manager_redirect_shop_archive' => 'permalink-manager-redirect-shop-archive' |
| 335 | ); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Map the deprecated hooks to their relevant equivalents. |
| 340 | */ |
| 341 | function deprecated_hooks() { |
| 342 | $deprecated_filters = $this->deprecated_hooks_list(); |
| 343 | foreach ( $deprecated_filters as $new => $old ) { |
| 344 | add_filter( $new, array( $this, 'deprecated_hooks_mapping' ), - 1000, 8 ); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Apply the deprecated filters to the relevant hooks |
| 350 | * |
| 351 | * @param mixed $data |
| 352 | * |
| 353 | * @return mixed |
| 354 | */ |
| 355 | function deprecated_hooks_mapping( $data ) { |
| 356 | $deprecated_filters = $this->deprecated_hooks_list(); |
| 357 | $filter = current_filter(); |
| 358 | |
| 359 | if ( isset( $deprecated_filters[ $filter ] ) ) { |
| 360 | if ( has_filter( $deprecated_filters[ $filter ] ) ) { |
| 361 | do_action( 'deprecated_function_run', $deprecated_filters[ $filter ], $filter, '2.4.3' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Use built-in mechanism for flagging |
| 362 | |
| 363 | $args = func_get_args(); |
| 364 | $data = apply_filters_ref_array( $deprecated_filters[ $filter ], $args ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return $data; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Deactivate Permalink Manager Lite if Permalink Manager Pro is enabled |
| 373 | */ |
| 374 | function is_pro_activated() { |
| 375 | if ( function_exists( 'is_plugin_active' ) && is_plugin_active( 'permalink-manager/permalink-manager.php' ) && is_plugin_active( 'permalink-manager-pro/permalink-manager.php' ) ) { |
| 376 | deactivate_plugins( 'permalink-manager/permalink-manager.php' ); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Begins execution of the plugin |
| 384 | */ |
| 385 | function run_permalink_manager() { |
| 386 | global $permalink_manager; |
| 387 | |
| 388 | // Do not run when Elementor is opened |
| 389 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- No data is processed here |
| 390 | if ( ( ! empty( $_REQUEST['action'] ) && is_string( $_REQUEST['action'] ) && strpos( $_REQUEST['action'], 'elementor' ) !== false ) || isset( $_REQUEST['elementor-preview'] ) || isset( $_REQUEST['disable-pm'] ) ) { |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | $permalink_manager = new Permalink_Manager_Class(); |
| 395 | } |
| 396 | |
| 397 | run_permalink_manager(); |
| 398 | } |