| 1 |
<?php |
| 2 |
/** |
| 3 |
* Core plugin class that wires up dependencies and action hooks. |
| 4 |
* |
| 5 |
* @package Custom_404_Pro |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Plugin class. |
| 10 |
*/ |
| 11 |
class PluginClass { |
| 12 |
|
| 13 |
/** |
| 14 |
* The admin class instance. |
| 15 |
* |
| 16 |
* @var AdminClass |
| 17 |
*/ |
| 18 |
private $plugin_admin; |
| 19 |
|
| 20 |
/** |
| 21 |
* Loads dependencies and registers hooks. |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
$this->plugin_admin = ''; |
| 25 |
$this->load_dependencies(); |
| 26 |
$this->define_admin_hooks(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Includes and instantiates the admin class. |
| 31 |
*/ |
| 32 |
private function load_dependencies() { |
| 33 |
include_once plugin_dir_path( __DIR__ ) . 'admin/class-adminclass.php'; |
| 34 |
$this->plugin_admin = new AdminClass(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Brings an existing installation up to date on first load after an upgrade. |
| 39 |
* |
| 40 |
* Users who update the plugin without deactivating it first never trigger |
| 41 |
* register_activation_hook, so everything the activation path sets up has to |
| 42 |
* be repeated here: the table schema, the legacy options migration, and the |
| 43 |
* daily prune cron event. |
| 44 |
* |
| 45 |
* The routine is gated twice. |
| 46 |
* |
| 47 |
* First on request context. Applying schema changes means an ALTER TABLE, |
| 48 |
* which on a very large logs table takes seconds and blocks the request that |
| 49 |
* triggers it. Restricting it to admin, cron and WP-CLI requests means an |
| 50 |
* administrator absorbs that cost on their own page load rather than a |
| 51 |
* random visitor hitting a 404. The plugin's redirect and logging paths do |
| 52 |
* not depend on the new schema, so a front-end request that skips the |
| 53 |
* upgrade still behaves correctly. |
| 54 |
* |
| 55 |
* Then on a stored db version, so it runs once per released version rather |
| 56 |
* than on every request. On Multisite the option is per-site, so each site |
| 57 |
* in the network upgrades independently on its own first admin request. |
| 58 |
* |
| 59 |
* @since 3.12.9 |
| 60 |
* @since 3.16.0 Runs dbDelta so schema changes reach existing installs, and |
| 61 |
* no longer runs on front-end requests. |
| 62 |
*/ |
| 63 |
public function maybe_upgrade() { |
| 64 |
if ( ! defined( 'CUSTOM_404_PRO_VERSION' ) ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
if ( get_option( 'custom_404_pro_db_version' ) === CUSTOM_404_PRO_VERSION ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
if ( ! self::is_upgrade_context() ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
include_once plugin_dir_path( __FILE__ ) . 'class-activateclass.php'; |
| 75 |
|
| 76 |
// Applies any schema changes (new columns, indexes) to existing tables. |
| 77 |
ActivateClass::create_tables(); |
| 78 |
ActivateClass::maybe_migrate_legacy_options(); |
| 79 |
|
| 80 |
if ( ! wp_next_scheduled( 'custom_404_pro_prune_logs' ) ) { |
| 81 |
wp_schedule_event( time(), 'daily', 'custom_404_pro_prune_logs' ); |
| 82 |
} |
| 83 |
|
| 84 |
update_option( 'custom_404_pro_db_version', CUSTOM_404_PRO_VERSION ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Whether the current request is one that may carry out a schema upgrade. |
| 89 |
* |
| 90 |
* @since 3.16.0 |
| 91 |
* @return bool True for admin, cron and WP-CLI requests. |
| 92 |
*/ |
| 93 |
public static function is_upgrade_context(): bool { |
| 94 |
if ( is_admin() ) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
if ( function_exists( 'wp_doing_cron' ) && wp_doing_cron() ) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
return defined( 'WP_CLI' ) && WP_CLI; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Backwards-compatible alias for maybe_upgrade(). |
| 105 |
* |
| 106 |
* @deprecated 3.16.0 Use maybe_upgrade() instead. The routine now covers |
| 107 |
* schema upgrades as well as the legacy options migration. |
| 108 |
*/ |
| 109 |
public function maybe_migrate_legacy_options() { |
| 110 |
$this->maybe_upgrade(); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Loads the plugin text domain for translation. |
| 115 |
*/ |
| 116 |
public function load_textdomain() { |
| 117 |
load_plugin_textdomain( 'custom-404-pro', false, dirname( plugin_basename( __DIR__ ) ) . '/languages' ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Registers all WordPress action hooks for the plugin. |
| 122 |
*/ |
| 123 |
private function define_admin_hooks() { |
| 124 |
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); |
| 125 |
add_action( 'plugins_loaded', array( $this, 'maybe_upgrade' ) ); |
| 126 |
add_action( 'admin_menu', array( $this->plugin_admin, 'create_menu' ) ); |
| 127 |
add_action( 'admin_enqueue_scripts', array( $this->plugin_admin, 'enqueue_scripts' ) ); |
| 128 |
add_action( 'admin_enqueue_scripts', array( $this->plugin_admin, 'enqueue_styles' ) ); |
| 129 |
add_action( 'admin_init', array( $this->plugin_admin, 'custom_404_pro_admin_init' ) ); |
| 130 |
add_action( 'template_redirect', array( $this->plugin_admin, 'custom_404_pro_redirect' ) ); |
| 131 |
add_action( 'admin_notices', array( $this->plugin_admin, 'custom_404_pro_notices' ) ); |
| 132 |
add_action( 'admin_post_form-settings-global-redirect', array( $this->plugin_admin, 'form_settings_global_redirect' ) ); |
| 133 |
add_action( 'admin_post_form-settings-general', array( $this->plugin_admin, 'form_settings_general' ) ); |
| 134 |
add_action( 'custom_404_pro_prune_logs', array( $this->plugin_admin, 'run_scheduled_log_prune' ) ); |
| 135 |
} |
| 136 |
} |
| 137 |
|