| 1 |
<?php |
| 2 |
|
| 3 |
namespace EmailOctopus; |
| 4 |
|
| 5 |
use EmailOctopus\Ajax\Load_Forms; |
| 6 |
use EmailOctopus\Ajax\Update_Settings; |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if (!defined('ABSPATH')) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
final class Plugin |
| 14 |
{ |
| 15 |
/** |
| 16 |
* EmailOctopus plugin singleton; the "one true" plugin instance that registers hooks. |
| 17 |
*/ |
| 18 |
private static ?Plugin $instance = null; |
| 19 |
|
| 20 |
/** |
| 21 |
* Plugin AJAX handlers. |
| 22 |
* |
| 23 |
* @var string[] |
| 24 |
*/ |
| 25 |
private array $ajax_handlers = [ |
| 26 |
Update_Settings::class, |
| 27 |
Load_Forms::class, |
| 28 |
]; |
| 29 |
|
| 30 |
/** |
| 31 |
* Retrieve the EmailOctopus singleton. |
| 32 |
*/ |
| 33 |
public static function get_instance(): ?Plugin |
| 34 |
{ |
| 35 |
if (!isset(self::$instance) && !(self::$instance instanceof Plugin)) { |
| 36 |
self::$instance = new self(); |
| 37 |
} |
| 38 |
|
| 39 |
return self::$instance; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Plugin bootstrap, essentially. |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function run() |
| 48 |
{ |
| 49 |
load_plugin_textdomain('emailoctopus', false, dirname(plugin_basename(__FILE__)) . '/languages/'); |
| 50 |
|
| 51 |
$this->register_hooks(); |
| 52 |
$this->maybe_register_deprecated_hooks(); |
| 53 |
|
| 54 |
(new Frontend())->run(); |
| 55 |
|
| 56 |
if (is_admin()) { |
| 57 |
(new Admin())->run(); |
| 58 |
} |
| 59 |
|
| 60 |
new Gutenberg(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Main plugin hooks, and/or hooks for functionality used in both public-facing and admin contexts. |
| 65 |
*/ |
| 66 |
public function register_hooks() |
| 67 |
{ |
| 68 |
add_action('activated_plugin', [$this, 'activation_redirect']); |
| 69 |
|
| 70 |
add_action('init', [$this, 'register_post_type']); |
| 71 |
|
| 72 |
foreach ($this->ajax_handlers as $ajax_handler) { |
| 73 |
(new $ajax_handler())->register_handler(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Redirect to the forms page (where a welcome screen awaits) on plugin activation. |
| 79 |
* |
| 80 |
* @param string $plugin Path to the plugin file relative to the `plugins` directory. |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
public function activation_redirect($plugin) |
| 85 |
{ |
| 86 |
if ($plugin === plugin_basename(EMAILOCTOPUS_FILE)) { |
| 87 |
wp_safe_redirect(admin_url('admin.php?page=emailoctopus-forms')); |
| 88 |
exit; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Registers Deprecated hooks when pre-3.0 DB tables are found. |
| 94 |
* |
| 95 |
* @return void |
| 96 |
* |
| 97 |
* @todo Remove when pre-3.0 code is removed. |
| 98 |
*/ |
| 99 |
public function maybe_register_deprecated_hooks() |
| 100 |
{ |
| 101 |
if (!Utils::site_has_deprecated_forms()) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
(new Deprecated())->register_hooks(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* The private emailoctopus_form post type allows the mapping of emailoctopus.com form IDs to WP taxonomies, etc. |
| 110 |
* |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function register_post_type() |
| 114 |
{ |
| 115 |
register_post_type( |
| 116 |
'emailoctopus_form', |
| 117 |
[ |
| 118 |
'labels' => [ |
| 119 |
'name' => esc_html__('EmailOctopus Form', 'emailoctopus'), |
| 120 |
'singular_name' => esc_html__('EmailOctopus Form', 'emailoctopus'), |
| 121 |
], |
| 122 |
'description' => esc_html__('Private post type for mapping emailoctopus.com form data with that in WordPress.', 'emailoctopus'), |
| 123 |
'public' => false, |
| 124 |
'show_in_rest' => false, |
| 125 |
'supports' => ['custom-fields'], |
| 126 |
'rewrite' => false, |
| 127 |
] |
| 128 |
); |
| 129 |
} |
| 130 |
} |
| 131 |
|