feat(01-01): create main plugin class with activation/deactivation
- Add main singleton class DDHH_JM_Job_Manager - Implement activation handler with WP/PHP version checks - Implement deactivation handler with rewrite flush - Use transient-based rewrite flush to avoid multiple flushes - Add comprehensive security checks and documentation Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
100
includes/class-activator.php
Normal file
100
includes/class-activator.php
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Plugin Activation Handler
|
||||||
|
*
|
||||||
|
* Handles plugin activation logic including version checks and initial setup.
|
||||||
|
*
|
||||||
|
* @package DDHH_Job_Manager
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Exit if accessed directly.
|
||||||
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class DDHH_JM_Activator
|
||||||
|
*
|
||||||
|
* Handles plugin activation.
|
||||||
|
*/
|
||||||
|
class DDHH_JM_Activator {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimum WordPress version required.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const MIN_WP_VERSION = '6.0';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimum PHP version required.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const MIN_PHP_VERSION = '7.4';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Activate the plugin.
|
||||||
|
*
|
||||||
|
* Runs on plugin activation. Checks system requirements and sets up initial plugin state.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public static function activate() {
|
||||||
|
// Check WordPress version.
|
||||||
|
self::check_wordpress_version();
|
||||||
|
|
||||||
|
// Check PHP version.
|
||||||
|
self::check_php_version();
|
||||||
|
|
||||||
|
// Store plugin version in options.
|
||||||
|
update_option( 'ddhh_jm_version', DDHH_JM_VERSION );
|
||||||
|
|
||||||
|
// Set a transient to trigger rewrite flush on next init.
|
||||||
|
// This is better than flushing immediately during activation.
|
||||||
|
set_transient( 'ddhh_jm_flush_rewrite_rules', 1, 60 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if WordPress version meets minimum requirement.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
private static function check_wordpress_version() {
|
||||||
|
global $wp_version;
|
||||||
|
|
||||||
|
if ( version_compare( $wp_version, self::MIN_WP_VERSION, '<' ) ) {
|
||||||
|
wp_die(
|
||||||
|
sprintf(
|
||||||
|
/* translators: 1: Plugin name, 2: Required WordPress version, 3: Current WordPress version */
|
||||||
|
esc_html__( '%1$s requires WordPress version %2$s or higher. You are running version %3$s.', 'ddhh-job-manager' ),
|
||||||
|
'<strong>Digital Dabei Job Manager</strong>',
|
||||||
|
self::MIN_WP_VERSION,
|
||||||
|
$wp_version
|
||||||
|
),
|
||||||
|
esc_html__( 'Plugin Activation Error', 'ddhh-job-manager' ),
|
||||||
|
array( 'back_link' => true )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if PHP version meets minimum requirement.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
private static function check_php_version() {
|
||||||
|
if ( version_compare( PHP_VERSION, self::MIN_PHP_VERSION, '<' ) ) {
|
||||||
|
wp_die(
|
||||||
|
sprintf(
|
||||||
|
/* translators: 1: Plugin name, 2: Required PHP version, 3: Current PHP version */
|
||||||
|
esc_html__( '%1$s requires PHP version %2$s or higher. You are running version %3$s.', 'ddhh-job-manager' ),
|
||||||
|
'<strong>Digital Dabei Job Manager</strong>',
|
||||||
|
self::MIN_PHP_VERSION,
|
||||||
|
PHP_VERSION
|
||||||
|
),
|
||||||
|
esc_html__( 'Plugin Activation Error', 'ddhh-job-manager' ),
|
||||||
|
array( 'back_link' => true )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
103
includes/class-ddhh-job-manager.php
Normal file
103
includes/class-ddhh-job-manager.php
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Main Plugin Class
|
||||||
|
*
|
||||||
|
* The core plugin class that orchestrates all functionality.
|
||||||
|
*
|
||||||
|
* @package DDHH_Job_Manager
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Exit if accessed directly.
|
||||||
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class DDHH_JM_Job_Manager
|
||||||
|
*
|
||||||
|
* Main plugin class using singleton pattern.
|
||||||
|
*/
|
||||||
|
class DDHH_JM_Job_Manager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The single instance of the class.
|
||||||
|
*
|
||||||
|
* @var DDHH_JM_Job_Manager|null
|
||||||
|
*/
|
||||||
|
private static $instance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the singleton instance.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @return DDHH_JM_Job_Manager
|
||||||
|
*/
|
||||||
|
public static function get_instance() {
|
||||||
|
if ( null === self::$instance ) {
|
||||||
|
self::$instance = new self();
|
||||||
|
}
|
||||||
|
return self::$instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor to prevent direct instantiation.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
private function __construct() {
|
||||||
|
$this->register_hooks();
|
||||||
|
$this->check_rewrite_flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prevent cloning of the instance.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
private function __clone() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prevent unserializing of the instance.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public function __wakeup() {
|
||||||
|
throw new Exception( 'Cannot unserialize singleton' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register activation and deactivation hooks.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
private function register_hooks() {
|
||||||
|
// Load activator and deactivator classes.
|
||||||
|
require_once DDHH_JM_PLUGIN_DIR . 'includes/class-activator.php';
|
||||||
|
require_once DDHH_JM_PLUGIN_DIR . 'includes/class-deactivator.php';
|
||||||
|
|
||||||
|
// Register activation hook.
|
||||||
|
register_activation_hook(
|
||||||
|
DDHH_JM_PLUGIN_FILE,
|
||||||
|
array( 'DDHH_JM_Activator', 'activate' )
|
||||||
|
);
|
||||||
|
|
||||||
|
// Register deactivation hook.
|
||||||
|
register_deactivation_hook(
|
||||||
|
DDHH_JM_PLUGIN_FILE,
|
||||||
|
array( 'DDHH_JM_Deactivator', 'deactivate' )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if rewrite rules need to be flushed.
|
||||||
|
*
|
||||||
|
* This is triggered by a transient set during activation.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
private function check_rewrite_flush() {
|
||||||
|
if ( get_transient( 'ddhh_jm_flush_rewrite_rules' ) ) {
|
||||||
|
flush_rewrite_rules();
|
||||||
|
delete_transient( 'ddhh_jm_flush_rewrite_rules' );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
36
includes/class-deactivator.php
Normal file
36
includes/class-deactivator.php
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Plugin Deactivation Handler
|
||||||
|
*
|
||||||
|
* Handles plugin deactivation logic.
|
||||||
|
*
|
||||||
|
* @package DDHH_Job_Manager
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Exit if accessed directly.
|
||||||
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class DDHH_JM_Deactivator
|
||||||
|
*
|
||||||
|
* Handles plugin deactivation.
|
||||||
|
*/
|
||||||
|
class DDHH_JM_Deactivator {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deactivate the plugin.
|
||||||
|
*
|
||||||
|
* Runs on plugin deactivation. Cleans up transients and flushes rewrite rules.
|
||||||
|
* Does NOT delete user data as the user may reactivate the plugin.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public static function deactivate() {
|
||||||
|
// Remove the rewrite flush transient if it exists.
|
||||||
|
delete_transient( 'ddhh_jm_flush_rewrite_rules' );
|
||||||
|
|
||||||
|
// Flush rewrite rules to clean up any custom rules added by the plugin.
|
||||||
|
flush_rewrite_rules();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user