| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets; |
| 4 |
|
| 5 |
use WP_REST_Server; |
| 6 |
use const Code_Snippets\REST_API_NAMESPACE; |
| 7 |
|
| 8 |
class Plugins_Import_Manager { |
| 9 |
|
| 10 |
const VERSION = 1; |
| 11 |
|
| 12 |
private $plugin_importers = []; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
$this->init_plugin_importers(); |
| 16 |
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); |
| 17 |
} |
| 18 |
|
| 19 |
private function init_plugin_importers() { |
| 20 |
$this->plugin_importers = [ |
| 21 |
'insert-headers-and-footers' => new Insert_Headers_And_Footers_Importer(), |
| 22 |
'header-footer-code-manager' => new Header_Footer_Code_Manager_Importer(), |
| 23 |
'insert-php-code-snippet' => new Insert_PHP_Code_Snippet_Importer(), |
| 24 |
]; |
| 25 |
} |
| 26 |
|
| 27 |
public function get_importer( string $source ) { |
| 28 |
return $this->plugin_importers[ $source ] ?? null; |
| 29 |
} |
| 30 |
|
| 31 |
public function get_importers() { |
| 32 |
if ( empty( $this->plugin_importers ) ) { |
| 33 |
$this->init_plugin_importers(); |
| 34 |
} |
| 35 |
|
| 36 |
$plugins_list = []; |
| 37 |
|
| 38 |
foreach ( $this->plugin_importers as $importer ) { |
| 39 |
$plugins_list[] = [ |
| 40 |
'name' => $importer->get_name(), |
| 41 |
'title' => $importer->get_title(), |
| 42 |
'is_active' => $importer::is_active(), |
| 43 |
]; |
| 44 |
} |
| 45 |
|
| 46 |
return $plugins_list; |
| 47 |
} |
| 48 |
|
| 49 |
public function register_rest_routes() { |
| 50 |
$namespace = REST_API_NAMESPACE . self::VERSION; |
| 51 |
|
| 52 |
register_rest_route( $namespace, 'importers', [ |
| 53 |
'methods' => WP_REST_Server::READABLE, |
| 54 |
'callback' => [ $this, 'get_importers' ], |
| 55 |
'permission_callback' => function() { |
| 56 |
return current_user_can( 'manage_options' ); |
| 57 |
}, |
| 58 |
] ); |
| 59 |
} |
| 60 |
} |
| 61 |
|