| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Admin\Menus; |
| 4 |
|
| 5 |
use Code_Snippets\Admin\Contextual_Help; |
| 6 |
use function Code_Snippets\code_snippets; |
| 7 |
use const Code_Snippets\PLUGIN_FILE; |
| 8 |
use const Code_Snippets\PLUGIN_VERSION; |
| 9 |
|
| 10 |
/** |
| 11 |
* This class handles the import admin menu. |
| 12 |
* |
| 13 |
* @since 2.4.0 |
| 14 |
* @package Code_Snippets |
| 15 |
*/ |
| 16 |
class Import_Menu extends Admin_Menu { |
| 17 |
|
| 18 |
/** |
| 19 |
* Class constructor |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
parent::__construct( |
| 23 |
'import', |
| 24 |
_x( 'Import', 'menu label', 'code-snippets' ), |
| 25 |
__( 'Import Snippets', 'code-snippets' ) |
| 26 |
); |
| 27 |
|
| 28 |
add_action( 'admin_init', [ $this, 'register_importer' ] ); |
| 29 |
add_action( 'load-importer-code-snippets', [ $this, 'load' ] ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Executed when the menu is loaded |
| 34 |
*/ |
| 35 |
public function load() { |
| 36 |
parent::load(); |
| 37 |
|
| 38 |
$contextual_help = new Contextual_Help( 'import' ); |
| 39 |
$contextual_help->load(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Add the importer to the Tools > Import menu |
| 44 |
*/ |
| 45 |
public function register_importer() { |
| 46 |
if ( ! defined( 'WP_LOAD_IMPORTERS' ) || ! code_snippets()->current_user_can() ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
register_importer( |
| 51 |
'code-snippets', |
| 52 |
__( 'Code Snippets', 'code-snippets' ), |
| 53 |
__( 'Import snippets from a code snippets export file', 'code-snippets' ), |
| 54 |
[ $this, 'render' ] |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Empty implementation for enqueue_assets. |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public function enqueue_assets() { |
| 64 |
wp_enqueue_script( |
| 65 |
'code-snippets-import', |
| 66 |
plugins_url( 'dist/import.js', PLUGIN_FILE ), |
| 67 |
[ |
| 68 |
'react', |
| 69 |
'react-dom', |
| 70 |
'react-jsx-runtime', |
| 71 |
'wp-i18n', |
| 72 |
'wp-components', |
| 73 |
], |
| 74 |
PLUGIN_VERSION, |
| 75 |
true |
| 76 |
); |
| 77 |
|
| 78 |
wp_enqueue_style( |
| 79 |
'code-snippets-import', |
| 80 |
plugins_url( 'dist/import.css', PLUGIN_FILE ), |
| 81 |
[], |
| 82 |
PLUGIN_VERSION |
| 83 |
); |
| 84 |
|
| 85 |
code_snippets()->localize_script( 'code-snippets-import' ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Render Import menu UI. |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function render() { |
| 94 |
echo '<div id="import-container" class="wrap"></div>'; |
| 95 |
} |
| 96 |
} |
| 97 |
|