admin.php
3 weeks ago
base.php
3 weeks ago
data-importer.php
3 weeks ago
helpers.php
3 weeks ago
importer.php
3 weeks ago
manager.php
3 weeks ago
parser.php
3 weeks ago
post-data-importer.php
3 weeks ago
rest.php
3 weeks ago
updater.php
3 weeks ago
view.php
3 weeks ago
admin.php
118 lines
| 1 | <?php |
| 2 | namespace Wpai\AddonAPI; |
| 3 | |
| 4 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 | |
| 6 | class PMXI_Addon_Admin { |
| 7 | use Singleton; |
| 8 | |
| 9 | public string $url = WP_ALL_IMPORT_ROOT_URL . '/addon-api'; |
| 10 | |
| 11 | public function __construct() { |
| 12 | add_action( 'pmxi_extend_options_custom_fields', [ $this, 'render' ], 10, 2 ); |
| 13 | add_action( 'pmxi_reimport', [ $this, 'renderUpdateScreen' ], 10, 2 ); |
| 14 | add_action( 'pmxi_confirm_data_to_import', [ $this, 'renderConfirmDataToImport' ], 10, 2 ); |
| 15 | |
| 16 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueue' ] ); |
| 17 | add_filter( 'script_loader_tag', [ $this, 'add_type_attribute' ], 10, 3 ); |
| 18 | } |
| 19 | |
| 20 | public function enqueue() { |
| 21 | // Loaded as an ES module; a stray module on unrelated admin pages |
| 22 | // invalidates WordPress core's import map in Firefox (strict ordering), |
| 23 | // breaking screens such as the WP 7.0 Connectors page. Only load it on |
| 24 | // WP All Import's own pages, where it is used. |
| 25 | if ( ! $this->is_wp_all_import_page() ) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | wp_enqueue_script( 'pmxi-datepicker', $this->url . '/static/vendor/air-datepicker/air-datepicker.min.js', array(), PMXI_VERSION, true ); |
| 30 | wp_enqueue_style( 'pmxi-datepicker', $this->url . '/static/vendor/air-datepicker/air-datepicker.min.css', array(), PMXI_VERSION ); |
| 31 | |
| 32 | wp_enqueue_style( 'pmxi-addon-admin-style', $this->url . '/static/css/admin.css', array(), PMXI_VERSION ); |
| 33 | wp_enqueue_script( 'pmxi-addon-admin-script', $this->url . '/static/js/admin.js', array(), PMXI_VERSION, true ); |
| 34 | wp_localize_script( 'pmxi-addon-admin-script', 'pmxiAddon', [ |
| 35 | 'ajaxUrl' => get_rest_url( null, 'wp-all-import/v1/addon/fields' ), |
| 36 | ] ); |
| 37 | } |
| 38 | |
| 39 | private function is_wp_all_import_page(): bool { |
| 40 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 41 | if ( empty( $_GET['page'] ) || ! is_string( $_GET['page'] ) ) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 46 | return strpos( sanitize_key( wp_unslash( $_GET['page'] ) ), 'pmxi-' ) === 0; |
| 47 | } |
| 48 | |
| 49 | public function add_type_attribute( $tag, $handle, $src ) { |
| 50 | if ( 'pmxi-addon-admin-script' !== $handle ) { |
| 51 | return $tag; |
| 52 | } |
| 53 | $tag = str_replace( ' src', ' type="module" src', $tag ); |
| 54 | |
| 55 | return $tag; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Render something on the import page |
| 60 | * |
| 61 | * @param string $type |
| 62 | * @param array $importOptions |
| 63 | * |
| 64 | * @return void |
| 65 | */ |
| 66 | public function render( string $type, array $importOptions ) { |
| 67 | $subtype = $importOptions['taxonomy_type']; |
| 68 | $addons = PMXI_Addon_Manager::get_addons(); |
| 69 | |
| 70 | if ( empty( $addons ) ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | foreach ( $addons as $addon ) { |
| 75 | $view = PMXI_Addon_View::create( $addon->slug, $type, $subtype ); |
| 76 | $view->renderTabs( $importOptions ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Render the update screen |
| 82 | * |
| 83 | * @param string $type |
| 84 | * @param array $importOptions |
| 85 | * |
| 86 | * @return void |
| 87 | */ |
| 88 | |
| 89 | public function renderUpdateScreen( string $type, array $importOptions ) { |
| 90 | $subtype = $importOptions['taxonomy_type']; |
| 91 | $addons = PMXI_Addon_Manager::get_addons(); |
| 92 | |
| 93 | if ( empty( $addons ) ) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | foreach ( $addons as $addon ) { |
| 98 | $view = PMXI_Addon_View::create( $addon->slug, $type, $subtype ); |
| 99 | $view->renderUpdate( $importOptions ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public function renderConfirmDataToImport( bool $isWizard, array $importOptions ) { |
| 104 | $type = $importOptions['custom_type']; |
| 105 | $subtype = $importOptions['taxonomy_type']; |
| 106 | $addons = PMXI_Addon_Manager::get_addons(); |
| 107 | |
| 108 | if ( empty( $addons ) ) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | foreach ( $addons as $addon ) { |
| 113 | $view = PMXI_Addon_View::create( $addon->slug, $type, $subtype ); |
| 114 | $view->renderConfirmDataToImport( $importOptions ); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 |