media-modal.js
337 lines
| 1 | function mediaModalInit() { |
| 2 | // @ts-ignore |
| 3 | const { __ } = wp.i18n; |
| 4 | const getStaticAssetURL = ( rel ) => { |
| 5 | // @ts-ignore |
| 6 | const path = top.kubioUtilsData?.staticAssetsURL; |
| 7 | return path ? `${ path }/${ rel.replace( /^\/|\/$/g, '' ) }` : null; |
| 8 | }; |
| 9 | |
| 10 | const imageHubSvgUrl = getStaticAssetURL( |
| 11 | 'admin-pages/image-hub-logo-color.svg' |
| 12 | ); |
| 13 | |
| 14 | // @ts-ignore |
| 15 | top.imageHubMediaTabInited = true; |
| 16 | top.imageHubMediaTabInitedFromKubio = true; |
| 17 | const getNewMediaTabObject = ( FrameSelect ) => { |
| 18 | return { |
| 19 | initialize() { |
| 20 | FrameSelect.prototype.initialize.apply( this, arguments ); |
| 21 | |
| 22 | const CustomState = wp.media.controller.State.extend( { |
| 23 | insert() { |
| 24 | this.frame.close(); |
| 25 | }, |
| 26 | } ); |
| 27 | |
| 28 | this.states.add( [ |
| 29 | new CustomState( { |
| 30 | id: 'demo-image-hub-media-modal-tab', |
| 31 | search: false, |
| 32 | title: __( 'Free Images', 'kubio' ), |
| 33 | } ), |
| 34 | ] ); |
| 35 | |
| 36 | this.on( |
| 37 | 'content:render:image-hub-media-modal-tab-content', |
| 38 | this.renderContent, |
| 39 | this |
| 40 | ); |
| 41 | const self = this; |
| 42 | |
| 43 | const onReInsertBrowserButtonIfNeeded = () => { |
| 44 | const node = self?.$el?.[ 0 ]; |
| 45 | if ( ! node ) { |
| 46 | return; |
| 47 | } |
| 48 | let browserButton = node.querySelector( 'button.browser' ); |
| 49 | if ( browserButton ) { |
| 50 | return; |
| 51 | } |
| 52 | browserButton = document.createElement( 'button' ); |
| 53 | |
| 54 | const buttonParent = node.querySelector( '.upload-ui' ); |
| 55 | if ( ! buttonParent ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | buttonParent.appendChild( browserButton ); |
| 60 | |
| 61 | browserButton.outerHTML = |
| 62 | '<button type="button" class="browser button button-hero" style="position: relative; z-index: 1;" id="__wp-uploader-id-1" aria-labelledby="__wp-uploader-id-1 post-upload-info">Select Files</button>'; |
| 63 | }; |
| 64 | this.on( 'ready', onReInsertBrowserButtonIfNeeded ); |
| 65 | //adds the icon to the tab |
| 66 | this.on( 'router:create:browse', function ( routerView ) { |
| 67 | const onSelectBrowseButtonIfImageHubActive = ( { |
| 68 | parentNode, |
| 69 | imageHubTabNode, |
| 70 | } ) => { |
| 71 | if ( imageHubTabNode.classList.contains( 'active' ) ) { |
| 72 | const browseButton = |
| 73 | parentNode.querySelector( '#menu-item-browse' ); |
| 74 | if ( browseButton ) { |
| 75 | browseButton.click(); |
| 76 | } |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | // Wait for next tick so DOM is rendered |
| 81 | setTimeout( () => { |
| 82 | const libraryType = |
| 83 | self?.options?.library?.type?.[ 0 ] || 'image'; |
| 84 | const isImage = libraryType === 'image'; |
| 85 | |
| 86 | const node = routerView?.view?.$el?.[ 0 ]; |
| 87 | if ( ! node ) { |
| 88 | return; |
| 89 | } |
| 90 | const imageHubTabNode = node.querySelector( |
| 91 | '#menu-item-image-hub-media-modal-tab-content' |
| 92 | ); |
| 93 | if ( ! imageHubTabNode ) { |
| 94 | return; |
| 95 | } |
| 96 | imageHubTabNode.setAttribute( |
| 97 | 'data-type', |
| 98 | libraryType |
| 99 | ); |
| 100 | if ( ! isImage ) { |
| 101 | onSelectBrowseButtonIfImageHubActive( { |
| 102 | imageHubTabNode, |
| 103 | parentNode: node, |
| 104 | } ); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | const shouldOpenImageHubTab = |
| 109 | top.kubioOpenImageHubOnNextOpenedMediaPicker; |
| 110 | top.kubioOpenImageHubOnNextOpenedMediaPicker = false; |
| 111 | if ( shouldOpenImageHubTab ) { |
| 112 | imageHubTabNode.click(); |
| 113 | } else { |
| 114 | //if the media picker is opened and the active tab is image hub but the free |
| 115 | //images was not used pick the media library |
| 116 | onSelectBrowseButtonIfImageHubActive( { |
| 117 | imageHubTabNode, |
| 118 | parentNode: node, |
| 119 | } ); |
| 120 | } |
| 121 | |
| 122 | const svgParent = document.createElement( 'div' ); |
| 123 | svgParent.innerHTML = `<img height="20px" width="20px" src="${ imageHubSvgUrl }" alt="Image Hub">`; |
| 124 | |
| 125 | imageHubTabNode.prepend( svgParent ); |
| 126 | }, 10 ); |
| 127 | } ); |
| 128 | }, |
| 129 | |
| 130 | browseRouter( routerView ) { |
| 131 | FrameSelect.prototype.browseRouter.apply( this, arguments ); |
| 132 | |
| 133 | routerView.set( { |
| 134 | 'image-hub-media-modal-tab-content': { |
| 135 | text: __( 'Free Images', 'kubio' ), |
| 136 | priority: 120, |
| 137 | }, |
| 138 | } ); |
| 139 | }, |
| 140 | |
| 141 | renderContent() { |
| 142 | const CustomView = wp.media.View.extend( { |
| 143 | tagName: 'div', |
| 144 | className: 'demo-image-hub-media-modal-tab', |
| 145 | render() { |
| 146 | this.$el.empty(); |
| 147 | renderHTMLContent( this.el ); // Inject your custom content |
| 148 | return this; |
| 149 | }, |
| 150 | } ); |
| 151 | |
| 152 | const view = new CustomView(); |
| 153 | this.content.set( view ); |
| 154 | }, |
| 155 | }; |
| 156 | }; |
| 157 | |
| 158 | const MediaFrameSelect = wp.media.view.MediaFrame.Select; |
| 159 | |
| 160 | wp.media.view.MediaFrame.Select = MediaFrameSelect.extend( |
| 161 | getNewMediaTabObject( MediaFrameSelect ) |
| 162 | ); |
| 163 | |
| 164 | const MediaFramePost = wp.media.view.MediaFrame.Post; |
| 165 | |
| 166 | wp.media.view.MediaFrame.Post = MediaFramePost.extend( |
| 167 | getNewMediaTabObject( MediaFramePost ) |
| 168 | ); |
| 169 | |
| 170 | let imageHubInstalled = false; |
| 171 | |
| 172 | function renderHTMLContent() { |
| 173 | setTimeout( () => { |
| 174 | const mediaLibraryRoot = document.querySelector( |
| 175 | '.demo-image-hub-media-modal-tab' |
| 176 | ); |
| 177 | if ( ! mediaLibraryRoot ) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | if ( ! imageHubInstalled ) { |
| 182 | renderHTMLContentWithoutPlugin( mediaLibraryRoot ); |
| 183 | } else { |
| 184 | renderHTMLContentWithPlugin( mediaLibraryRoot ); |
| 185 | } |
| 186 | }, 10 ); |
| 187 | } |
| 188 | function renderHTMLContentWithPlugin( mediaLibraryRoot ) { |
| 189 | const rootDiv = document.createElement( 'div' ); |
| 190 | rootDiv.setAttribute( 'id', 'demo-image-hub-media-modal-root' ); |
| 191 | mediaLibraryRoot.appendChild( rootDiv ); |
| 192 | // @ts-ignore |
| 193 | const imageHubMediaTabReactApp = top.imageHubMediaTabReactApp; |
| 194 | if ( ! imageHubMediaTabReactApp ) { |
| 195 | return; |
| 196 | } |
| 197 | imageHubMediaTabReactApp( rootDiv ); |
| 198 | } |
| 199 | |
| 200 | const fetchImageHubAssets = async () => { |
| 201 | return new Promise( ( resolve, reject ) => { |
| 202 | // @ts-ignore |
| 203 | const { loadAssetsFromGutenberg } = top?.kubio?.utils || {}; |
| 204 | if ( ! loadAssetsFromGutenberg ) { |
| 205 | reject( 'Could not find loadAssetsFromGutenberg function ' ); |
| 206 | return; |
| 207 | } |
| 208 | const onAssetsLoaded = () => { |
| 209 | resolve(); |
| 210 | }; |
| 211 | |
| 212 | loadAssetsFromGutenberg( { |
| 213 | pluginIdString: 'image-hub', |
| 214 | pluginUrlString: 'image-hub', |
| 215 | onAssetsLoaded, |
| 216 | } ); |
| 217 | } ); |
| 218 | }; |
| 219 | |
| 220 | const onRenderImageHubAfterInstall = () => { |
| 221 | const container = document.querySelector( |
| 222 | '.demo-image-hub-media-modal-tab' |
| 223 | ); |
| 224 | if ( ! container ) { |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | container.innerHTML = ''; |
| 229 | renderHTMLContentWithPlugin( container ); |
| 230 | }; |
| 231 | const onImageHubInstalled = async () => { |
| 232 | try { |
| 233 | imageHubInstalled = true; |
| 234 | await fetchImageHubAssets(); |
| 235 | onRenderImageHubAfterInstall(); |
| 236 | } catch ( e ) { |
| 237 | console.error( e ); |
| 238 | alert( __( 'Encountered error on install', 'kubio' ) ); |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | function renderHTMLContentWithoutPlugin( mediaLibraryRoot ) { |
| 243 | const content = ` |
| 244 | <div class="demo-image-hub-centered-container"> |
| 245 | <img src="${ getStaticAssetURL( |
| 246 | 'admin-pages/image-hub-logo-color.svg' |
| 247 | ) }" /> |
| 248 | <h1>${ __( |
| 249 | 'Image Hub - Free images from Unsplash, Pexels, Pixabay and more', |
| 250 | 'kubio' |
| 251 | ) }</h1> |
| 252 | <p> |
| 253 | ${ __( 'Search and insert images from', 'kubio' ) } |
| 254 | <a href="https://unsplash.com" target="_blank">${ __( |
| 255 | 'Unsplash', |
| 256 | 'kubio' |
| 257 | ) }</a>, |
| 258 | <a href="https://openverse.org" target="_blank">${ __( |
| 259 | 'Openverse', |
| 260 | 'kubio' |
| 261 | ) }</a>, |
| 262 | <a href="https://pixabay.com" target="_blank">${ __( |
| 263 | 'Pixabay', |
| 264 | 'kubio' |
| 265 | ) }</a>, |
| 266 | <a href="https://giphy.com" target="_blank">${ __( |
| 267 | 'Giphy', |
| 268 | 'kubio' |
| 269 | ) }</a>, |
| 270 | ${ __( 'and', 'kubio' ) } |
| 271 | <a href="https://www.pexels.com" target="_blank">${ __( |
| 272 | 'Pexels', |
| 273 | 'kubio' |
| 274 | ) }</a> |
| 275 | ${ __( |
| 276 | 'directly in WordPress. No need to visit external websites—find, preview, and download images straight from the media library or editor.', |
| 277 | 'kubio' |
| 278 | ) } |
| 279 | </p> |
| 280 | <button id="demo-image-hub-activate" class="components-button is-primary">${ __( |
| 281 | 'Install Image Hub Plugin', |
| 282 | 'kubio' |
| 283 | ) }</button> |
| 284 | </div> |
| 285 | `; |
| 286 | |
| 287 | mediaLibraryRoot.innerHTML = content; |
| 288 | const button = mediaLibraryRoot.querySelector( |
| 289 | '#demo-image-hub-activate' |
| 290 | ); |
| 291 | let pendingInstalling = false; |
| 292 | setTimeout( () => { |
| 293 | function outerListener() { |
| 294 | if ( pendingInstalling ) { |
| 295 | return; |
| 296 | } |
| 297 | pendingInstalling = true; |
| 298 | button.innerHTML = __( 'Installing…', 'kubio' ); |
| 299 | button.classList.add( 'is-busy' ); |
| 300 | // @ts-ignore |
| 301 | wp.ajax |
| 302 | .send( 'kubio-image-hub-install-plugin', { |
| 303 | data: { |
| 304 | // @ts-ignore |
| 305 | _wpnonce: top?.kubioUtilsData?.kubio_ajax_nonce, |
| 306 | }, |
| 307 | // @ts-ignore |
| 308 | } ) |
| 309 | .done( function ( response ) { |
| 310 | onImageHubInstalled(); |
| 311 | pendingInstalling = false; |
| 312 | } ) |
| 313 | .fail( function ( error ) { |
| 314 | console.error( |
| 315 | 'Error handling Instant Images plugin:', |
| 316 | error |
| 317 | ); |
| 318 | alert( |
| 319 | __( 'Failed to install image hub plugin', 'kubio' ) |
| 320 | ); |
| 321 | pendingInstalling = false; |
| 322 | button.classList.remove( 'is-busy' ); |
| 323 | } ); |
| 324 | } |
| 325 | |
| 326 | button.addEventListener( 'click', outerListener ); |
| 327 | }, 1 ); |
| 328 | } |
| 329 | } |
| 330 | const domReady = wp.domReady; |
| 331 | |
| 332 | domReady( () => { |
| 333 | if ( ! top.imageHubMediaTabInited ) { |
| 334 | mediaModalInit(); |
| 335 | } |
| 336 | } ); |
| 337 |