-
Notifications
You must be signed in to change notification settings - Fork 68
Feat: add Useful Plugins in the admin setting and Cimo download notice in the editor #3635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1427503
init
kaeizen 7de0280
lazy load cimo notice, display notice in image control
kaeizen df79ec5
code rabbit's qa fixes
kaeizen 4e39c6f
fix to coderabbit's qa
kaeizen 59de0d4
Apply suggestion from @bfintal
bfintal 9bb3459
Merge branch 'develop' into feat/cimo-notice
kaeizen 8dcef44
add useful plugins
kaeizen d75f1db
remove error logs, add guard if pluginData is not available
kaeizen d5caa3b
fix functions to localize script
kaeizen 032a901
check for wp error
kaeizen b62a7e4
fix to coderabbit's qa
kaeizen 8a68b33
minor fix
kaeizen 18da223
reset status
kaeizen 8a6b867
tweaked css
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import { | ||
| cimo, i18n, ajaxUrl, | ||
| } from 'stackable' | ||
| import { createRoot } from '~stackable/util' | ||
|
|
||
| import { __ } from '@wordpress/i18n' | ||
| import { Dashicon } from '@wordpress/components' | ||
| import domReady from '@wordpress/dom-ready' | ||
| import { | ||
| useState, useRef, useEffect, | ||
| } from '@wordpress/element' | ||
| import { models } from '@wordpress/api' | ||
|
|
||
| const CimoDownloadNotice = props => { | ||
| const [ data, setData ] = useState( { status: cimo?.status, action: cimo?.action } ) | ||
| const pollCountRef = useRef( 0 ) | ||
|
|
||
| const onDismiss = () => { | ||
| const settings = new models.Settings( { stackable_hide_cimo_notice: true } ) // eslint-disable-line camelcase | ||
| settings.save() | ||
| props?.onDismiss?.() | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Polls the Cimo plugin status to detect installation or activation state changes | ||
| const pollStatus = ( action, pollOnce = false ) => { | ||
| fetch( ajaxUrl, { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
| body: new URLSearchParams( { | ||
| action: 'stackable_check_cimo_status', | ||
| // eslint-disable-next-line camelcase | ||
| user_action: action, | ||
| } ), | ||
| credentials: 'same-origin', | ||
| } ).then( res => res.json() ).then( _data => { | ||
| pollCountRef.current += 1 | ||
|
|
||
| if ( data.status !== _data.status ) { | ||
| setData( _data ) | ||
|
|
||
| // Update the global stackable.cimo status/action variables | ||
| // so new image block selections reflect the latest Cimo installation state | ||
| // eslint-disable-next-line no-undef | ||
| stackable.cimo.status = _data.status | ||
| // eslint-disable-next-line no-undef | ||
| stackable.cimo.action = _data.action | ||
| } | ||
|
|
||
| // Stop polling if it has reached 3 attempts, or plugin status indicates installation/activation is complete | ||
| if ( pollOnce || pollCountRef.current >= 3 || | ||
| ( action === 'install' && ( _data.status === 'installed' || _data.status === 'activated' ) ) || | ||
| ( action === 'activate' && _data.status === 'activated' ) | ||
| ) { | ||
| return | ||
| } | ||
|
|
||
| setTimeout( () => { | ||
| pollStatus( action ) | ||
| }, 3000 * pollCountRef.current ) | ||
| } ) | ||
| } | ||
|
|
||
| useEffect( () => { | ||
| const _media = wp.media | ||
| const old = _media.view.MediaFrame.Select | ||
|
|
||
| // When the media library closes, check and update the Cimo plugin status | ||
| // to ensure the UI reflects the latest installation or activation state. | ||
| _media.view.MediaFrame.Select = old.extend( { | ||
| initialize() { | ||
| old.prototype.initialize.apply( this, arguments ) | ||
|
|
||
| this.on( 'close', () => { | ||
| pollCountRef.current = 0 | ||
| if ( data.status === 'activated' ) { | ||
| return | ||
| } | ||
|
|
||
| if ( data.status === 'not_installed' ) { | ||
| pollStatus( 'install', true ) | ||
| return | ||
| } | ||
|
|
||
| pollStatus( 'activate', true ) | ||
| } ) | ||
| }, | ||
| } ) | ||
| }, [] ) | ||
|
|
||
| const onActionClick = async () => { | ||
| pollCountRef.current = 0 | ||
|
|
||
| if ( data.status === 'not_installed' ) { | ||
| setData( { status: 'installing', action: '' } ) | ||
| setTimeout( () => { | ||
| pollStatus( 'install' ) | ||
| }, 3000 ) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| setData( { status: 'activating', action: '' } ) | ||
| setTimeout( () => { | ||
| pollStatus( 'activate' ) | ||
| }, 3000 ) | ||
| } | ||
|
|
||
| return ( <> | ||
| <button aria-label="dismiss" onClick={ onDismiss }><Dashicon icon="no" /></button> | ||
| { data.status === 'activated' | ||
| ? <p> | ||
| { __( 'Cimo Image Optimizer has been activated. Please refresh this page to begin optimizing your images automatically.', i18n ) } | ||
| </p> | ||
| : <p> { __( 'Instantly optimize images as you upload them with Cimo Image Optimizer.', i18n ) } | ||
| | ||
| { data.status === 'installing' | ||
| ? <span> { __( 'Installing', i18n ) }</span> | ||
| : ( data.status === 'activating' | ||
| ? <span>{ __( 'Activating', i18n ) } </span> | ||
| : <a href={ data.action } target="_blank" rel="noreferrer" onClick={ onActionClick }> | ||
| { data.status === 'installed' ? __( 'Activate now', i18n ) : __( 'Install now', i18n ) } | ||
| </a> | ||
| ) | ||
| } | ||
| </p> | ||
| } | ||
| </> ) | ||
| } | ||
|
|
||
| const CimoDownloadNoticeWrapper = props => { | ||
| return <div className="stk-cimo-notice"> <CimoDownloadNotice { ...props } /> </div> | ||
| } | ||
|
|
||
| export default CimoDownloadNoticeWrapper | ||
|
|
||
| domReady( () => { | ||
| if ( ! cimo || cimo.status === 'activated' || cimo.hideNotice || typeof wp === 'undefined' || ! wp.media || ! wp.media.view || | ||
| ! wp.media.view.Attachment || ! wp.media.view.Attachment.Details | ||
| ) { | ||
| return | ||
| } | ||
|
|
||
| const CurrentDetailsView = wp.media.view.Attachment.Details | ||
|
|
||
| // Display the Cimo download notice in the media library | ||
| const CustomDetailsView = CurrentDetailsView.extend( { | ||
| render() { | ||
| const result = CurrentDetailsView.prototype.render.apply( this, arguments ) | ||
|
|
||
| const details = this.el.querySelector( '.attachment-info .details' ) | ||
| if ( details && ! this.el.querySelector( '.stk-cimo-notice' ) ) { | ||
| const noticeDiv = document.createElement( 'div' ) | ||
| noticeDiv.className = 'stk-cimo-notice' | ||
|
|
||
| const onDismiss = () => { | ||
| if ( noticeDiv && noticeDiv.parentNode ) { | ||
| noticeDiv.parentNode.removeChild( noticeDiv ) | ||
| } | ||
| } | ||
|
|
||
| createRoot( noticeDiv ).render( <CimoDownloadNotice onDismiss={ onDismiss } /> ) | ||
| details.insertAdjacentElement( 'afterend', noticeDiv ) | ||
| } | ||
|
|
||
| return result | ||
| }, | ||
| } ) | ||
|
|
||
| wp.media.view.Attachment.Details = CustomDetailsView | ||
| } ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.