| 1 |
<?php |
| 2 |
|
| 3 |
namespace Enable\Cors; |
| 4 |
|
| 5 |
/* |
| 6 |
|-------------------------------------------------------------------------- |
| 7 |
| If this file is called directly, abort. |
| 8 |
|-------------------------------------------------------------------------- |
| 9 |
*/ |
| 10 |
if ( ! defined( 'Enable\Cors\SLUG' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
use Enable\Cors\Helpers\Option; |
| 15 |
use Enable\Cors\Helpers\Headers; |
| 16 |
use Enable\Cors\Helpers\Htaccess; |
| 17 |
|
| 18 |
|
| 19 |
final class Plugin { |
| 20 |
|
| 21 |
|
| 22 |
/** |
| 23 |
* It will load during activation |
| 24 |
*/ |
| 25 |
public static function activate(): void { |
| 26 |
// enable plugin's auto-update. |
| 27 |
self::enable_updates(); |
| 28 |
// set default option. |
| 29 |
Option::add_default(); |
| 30 |
// modify htaccess. |
| 31 |
( new Htaccess() )->modify(); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Enable plugin's auto-update on activation. |
| 36 |
*/ |
| 37 |
private static function enable_updates(): void { |
| 38 |
$auto_updates = (array) get_site_option( 'auto_update_plugins', array() ); |
| 39 |
$plugin = plugin_basename( FILE ); |
| 40 |
if ( false === in_array( $plugin, $auto_updates, true ) ) { |
| 41 |
$auto_updates[] = $plugin; |
| 42 |
update_site_option( 'auto_update_plugins', $auto_updates ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* It will load during deactivation |
| 48 |
*/ |
| 49 |
public static function deactivate(): void { |
| 50 |
self::disable_updates(); |
| 51 |
( new Htaccess() )->restore(); |
| 52 |
Option::delete(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Disable auto-update deactivation or uninstall |
| 57 |
*/ |
| 58 |
private static function disable_updates(): void { |
| 59 |
$auto_updates = (array) get_site_option( 'auto_update_plugins', array() ); |
| 60 |
$plugin = plugin_basename( FILE ); |
| 61 |
$update = array_diff( $auto_updates, array( $plugin ) ); |
| 62 |
update_site_option( 'auto_update_plugins', $update ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Plugin Initiator. |
| 67 |
* |
| 68 |
* This function is the entry point for the plugin. It sets up the |
| 69 |
* admin page and adds the necessary hooks for the plugin to work. |
| 70 |
*/ |
| 71 |
public static function init(): void { |
| 72 |
try { |
| 73 |
if ( is_admin() ) { |
| 74 |
// Add the settings link to the plugin actions. |
| 75 |
add_filter( 'plugin_action_links_' . plugin_basename( FILE ), array( self::class, 'actions' ) ); |
| 76 |
// Initialize the admin page. |
| 77 |
new AdminPage(); |
| 78 |
} |
| 79 |
|
| 80 |
// Initialize the Settings API. |
| 81 |
add_action( |
| 82 |
'rest_api_init', |
| 83 |
static function () { |
| 84 |
new SettingsApi(); |
| 85 |
} |
| 86 |
); |
| 87 |
|
| 88 |
// Get the option instance. |
| 89 |
$option = new Option(); |
| 90 |
|
| 91 |
// If the plugin is not enabled or the method is not allowed, return. |
| 92 |
if ( ! $option->is_enable() || ! $option->is_method_allowed() ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
// Add the CORS headers. |
| 97 |
Headers::add( $option ); |
| 98 |
|
| 99 |
// Remove the default CORS headers from the REST API. |
| 100 |
add_action( |
| 101 |
'rest_api_init', |
| 102 |
static function () { |
| 103 |
remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' ); |
| 104 |
|
| 105 |
// Add a new filter to add the CORS headers. |
| 106 |
add_filter( |
| 107 |
'rest_pre_serve_request', |
| 108 |
static function ( $value ) { |
| 109 |
// Get the option instance. |
| 110 |
$option = new Option(); |
| 111 |
|
| 112 |
// Add the CORS headers. |
| 113 |
Headers::add( $option ); |
| 114 |
|
| 115 |
// Return the value. |
| 116 |
return $value; |
| 117 |
} |
| 118 |
); |
| 119 |
} |
| 120 |
); |
| 121 |
|
| 122 |
// Run the upgrade routines. |
| 123 |
Upgrade::run(); |
| 124 |
} catch ( \Throwable $th ) { |
| 125 |
error_log( $th->getMessage() ); // phpcs:ignore |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* This PHP function adds a "Settings" link to an array of actions. |
| 131 |
* |
| 132 |
* @param array $actions collections. |
| 133 |
*/ |
| 134 |
public static function actions( array $actions ): array { |
| 135 |
$actions[] = sprintf( '<a href="%s">%s</a>', esc_url( get_admin_url( null, 'admin.php?page=enable-cors' ) ), esc_attr__( 'Settings', 'enable-cors' ) ); |
| 136 |
|
| 137 |
return $actions; |
| 138 |
} |
| 139 |
} |
| 140 |
|