| 1 |
<?php |
| 2 |
declare( strict_types=1 ); |
| 3 |
|
| 4 |
namespace GutSlider; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Main plugin singleton class. |
| 12 |
* |
| 13 |
* Orchestrates the initialization of all plugin components including |
| 14 |
* block registration, asset enqueuing, REST API, and style generation. |
| 15 |
* |
| 16 |
* @package GutSlider |
| 17 |
* @since 3.0.0 |
| 18 |
*/ |
| 19 |
final class Plugin { |
| 20 |
|
| 21 |
/** |
| 22 |
* Singleton instance. |
| 23 |
* |
| 24 |
* @since 3.0.0 |
| 25 |
* @var self|null |
| 26 |
*/ |
| 27 |
private static ?self $instance = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* Private constructor to prevent direct instantiation. |
| 31 |
* |
| 32 |
* @since 3.0.0 |
| 33 |
*/ |
| 34 |
private function __construct() {} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get the singleton instance. |
| 38 |
* |
| 39 |
* Creates and initializes the plugin instance on first call. |
| 40 |
* Subsequent calls return the existing instance. |
| 41 |
* |
| 42 |
* @since 3.0.0 |
| 43 |
* |
| 44 |
* @return self The plugin instance. |
| 45 |
*/ |
| 46 |
public static function get_instance(): self { |
| 47 |
if ( null === self::$instance ) { |
| 48 |
self::$instance = new self(); |
| 49 |
self::$instance->init(); |
| 50 |
} |
| 51 |
|
| 52 |
return self::$instance; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Initialize all plugin components. |
| 57 |
* |
| 58 |
* Instantiates each component class which registers its own hooks |
| 59 |
* internally. Also sets up the activation redirect handler. |
| 60 |
* |
| 61 |
* @since 3.0.0 |
| 62 |
* |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
private function init(): void { |
| 66 |
// Activation redirect. |
| 67 |
add_action( 'admin_init', array( $this, 'maybe_redirect_after_activation' ) ); |
| 68 |
|
| 69 |
// Initialize components. |
| 70 |
new Blocks\BlocksCategory(); |
| 71 |
new Blocks\RegisterBlocks(); |
| 72 |
new Blocks\Pattern(); |
| 73 |
new Assets\EnqueueAssets(); |
| 74 |
new Assets\LoadFonts(); |
| 75 |
new Style\DynamicStyle(); |
| 76 |
new Api\BlocksApi(); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Handle post-activation redirect to the welcome page. |
| 81 |
* |
| 82 |
* Checks for the activation flag in options and redirects to the |
| 83 |
* plugin's admin page on first admin page load after activation. |
| 84 |
* |
| 85 |
* @since 3.0.0 |
| 86 |
* |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function maybe_redirect_after_activation(): void { |
| 90 |
if ( get_option( 'gutsliderblocks_do_activation_redirect', false ) ) { |
| 91 |
delete_option( 'gutsliderblocks_do_activation_redirect' ); |
| 92 |
wp_safe_redirect( admin_url( 'admin.php?page=gutslider-blocks' ) ); |
| 93 |
exit; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Set the activation redirect flag. |
| 99 |
* |
| 100 |
* Called on plugin activation via register_activation_hook. |
| 101 |
* |
| 102 |
* @since 3.0.0 |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public static function on_activation(): void { |
| 107 |
add_option( 'gutsliderblocks_do_activation_redirect', true ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Clean up plugin data on deactivation. |
| 112 |
* |
| 113 |
* Removes generated CSS files and their directory from the |
| 114 |
* WordPress uploads folder. |
| 115 |
* |
| 116 |
* @since 3.0.0 |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public static function cleanup(): void { |
| 121 |
$upload_dir = wp_upload_dir(); |
| 122 |
$css_dir = $upload_dir['basedir'] . '/gutslider-styles'; |
| 123 |
|
| 124 |
if ( ! file_exists( $css_dir ) ) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
global $wp_filesystem; |
| 129 |
|
| 130 |
if ( empty( $wp_filesystem ) ) { |
| 131 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 132 |
WP_Filesystem(); |
| 133 |
} |
| 134 |
|
| 135 |
$files = glob( $css_dir . '/*' ); |
| 136 |
|
| 137 |
if ( is_array( $files ) ) { |
| 138 |
foreach ( $files as $file ) { |
| 139 |
if ( is_file( $file ) ) { |
| 140 |
$wp_filesystem->delete( $file ); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
$wp_filesystem->rmdir( $css_dir ); |
| 146 |
|
| 147 |
// Remove parent directory if empty. |
| 148 |
$parent_dir = dirname( $css_dir ); |
| 149 |
if ( file_exists( $parent_dir ) ) { |
| 150 |
$parent_contents = glob( "$parent_dir/*" ); |
| 151 |
if ( is_array( $parent_contents ) && count( $parent_contents ) === 0 ) { |
| 152 |
$wp_filesystem->rmdir( $parent_dir ); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|