image-hub.php
85 lines
| 1 | <?php |
| 2 | |
| 3 | use Kubio\PluginsManager; |
| 4 | use Kubio\Core\ThirdPartyPluginAssetLoaderInEditor; |
| 5 | use Kubio\Flags; |
| 6 | |
| 7 | class KubioImageHubIntegration { |
| 8 | private static $PLUGIN_SLUG = 'image-hub'; |
| 9 | |
| 10 | |
| 11 | public function __construct() { |
| 12 | ThirdPartyPluginAssetLoaderInEditor::addPlugin('image-hub', array($this, 'getIsPluginActive'), false); |
| 13 | |
| 14 | // no need to show the modal if we already have the plugin or if not enabled yet |
| 15 | if ( $this->getIsPluginActive()) { |
| 16 | return; |
| 17 | } |
| 18 | |
| 19 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_media_library_scripts' ) ); |
| 20 | |
| 21 | add_action( 'wp_ajax_kubio-image-hub-install-plugin', array( $this, 'install_image_hub_plugin' ) ); |
| 22 | add_filter( 'script_loader_tag', array( $this, 'add_module_attribute' ), 10, 2 ); |
| 23 | |
| 24 | |
| 25 | |
| 26 | } |
| 27 | |
| 28 | public function getIsPluginActive() { |
| 29 | $plugin_manager = PluginsManager::getInstance(); |
| 30 | return $plugin_manager->isPluginActive( self::$PLUGIN_SLUG ); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | public function enqueue_media_library_scripts() { |
| 37 | |
| 38 | wp_enqueue_style( |
| 39 | 'kubio-image-hub-integration-style', |
| 40 | plugin_dir_url( __FILE__ ) . 'styles.css', |
| 41 | array(), |
| 42 | filemtime( plugin_dir_path( __FILE__ ) . 'styles.css' ) |
| 43 | ); |
| 44 | |
| 45 | wp_enqueue_script( |
| 46 | 'kubio-image-hub-integration-media-modal', |
| 47 | plugin_dir_url( __FILE__ ) . 'media-modal.js', |
| 48 | array( 'wp-i18n', 'wp-components', 'wp-element', 'media-views', 'kubio-utils', 'wp-dom-ready' ), |
| 49 | filemtime( plugin_dir_path( __FILE__ ) . 'media-modal.js' ), |
| 50 | true |
| 51 | ); |
| 52 | |
| 53 | wp_enqueue_style( 'kubio-utils' ); |
| 54 | wp_enqueue_style( 'wp-components' ); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | public function add_module_attribute( $tag, $handle ) { |
| 59 | if ( 'kubio-image-hub-integration-media-modal' === $handle ) { |
| 60 | return str_replace( 'src', 'type="module" src', $tag ); |
| 61 | } |
| 62 | return $tag; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | public function install_image_hub_plugin() { |
| 67 | check_ajax_referer( 'kubio_ajax_nonce' ); |
| 68 | if(!current_user_can( 'install_plugins' )) { |
| 69 | wp_send_json_error('Not allowed'); |
| 70 | } |
| 71 | $plugin_manager = PluginsManager::getInstance(); |
| 72 | $plugin_manager->installPlugin( self::$PLUGIN_SLUG ); |
| 73 | $plugin_manager->activatePlugin( self::$PLUGIN_SLUG, false ); |
| 74 | |
| 75 | wp_send_json_success( 'Plugin installed successfully' ); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | public static function init() { |
| 80 | new self(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | KubioImageHubIntegration::init(); |
| 85 |