@@ -18,32 +18,57 @@ const CimoDownloadNotice = props => {
1818 const onDismiss = ( ) => {
1919 const settings = new models . Settings ( { stackable_hide_cimo_notice : true } ) // eslint-disable-line camelcase
2020 settings . save ( )
21+
22+ if ( cimo ) {
23+ cimo . hideNotice = true
24+ }
25+
26+ // Update the global stackable.cimo hideNotice variable
27+ if ( typeof window !== 'undefined' && window . stackable ?. cimo ) {
28+ window . stackable . cimo . hideNotice = true
29+ }
30+
2131 props ?. onDismiss ?. ( )
2232 }
2333
2434 // Polls the Cimo plugin status to detect installation or activation state changes
25- const pollStatus = ( action , pollOnce = false ) => {
35+ const pollStatus = ( action , link , pollOnce = false ) => {
2636 fetch ( ajaxUrl , {
2737 method : 'POST' ,
2838 headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } ,
2939 body : new URLSearchParams ( {
3040 action : 'stackable_check_cimo_status' ,
3141 // eslint-disable-next-line camelcase
3242 user_action : action ,
43+ nonce : cimo . nonce ,
3344 } ) ,
3445 credentials : 'same-origin' ,
35- } ) . then ( res => res . json ( ) ) . then ( _data => {
46+ } ) . then ( res => res . json ( ) ) . then ( res => {
47+ if ( ! res . success ) {
48+ setData ( { status : 'error' , action : '' } )
49+
50+ const errorMessage = res ?. data ?. message ? res . data . message : 'Server error'
51+
52+ throw new Error ( 'Stackable: ' + errorMessage )
53+ }
54+
55+ if ( pollCountRef . current === 0 && link ) {
56+ window . open ( link , '_blank' )
57+ }
58+
3659 pollCountRef . current += 1
3760
61+ const _data = res . data
62+
3863 if ( data . status !== _data . status ) {
3964 setData ( _data )
4065
4166 // Update the global stackable.cimo status/action variables
4267 // so new image block selections reflect the latest Cimo installation state
43- // eslint-disable-next-line no-undef
44- stackable . cimo . status = _data . status
45- // eslint-disable-next-line no-undef
46- stackable . cimo . action = _data . action
68+ if ( typeof window !== 'undefined' && window . stackable ?. cimo ) {
69+ window . stackable . cimo . status = _data . status
70+ window . stackable . cimo . action = _data . action
71+ }
4772 }
4873
4974 // Stop polling if it has reached 3 attempts, or plugin status indicates installation/activation is complete
@@ -57,6 +82,9 @@ const CimoDownloadNotice = props => {
5782 setTimeout ( ( ) => {
5883 pollStatus ( action )
5984 } , 3000 * pollCountRef . current )
85+ } ) . catch ( e => {
86+ // eslint-disable-next-line no-console
87+ console . error ( e . message )
6088 } )
6189 }
6290
@@ -77,32 +105,28 @@ const CimoDownloadNotice = props => {
77105 }
78106
79107 if ( data . status === 'not_installed' ) {
80- pollStatus ( 'install' , true )
108+ pollStatus ( 'install' , null , true )
81109 return
82110 }
83111
84- pollStatus ( 'activate' , true )
112+ pollStatus ( 'activate' , null , true )
85113 } )
86114 } ,
87115 } )
88116 } , [ ] )
89117
90- const onActionClick = async ( ) => {
118+ const onActionClick = e => {
119+ e . preventDefault ( )
91120 pollCountRef . current = 0
92121
93122 if ( data . status === 'not_installed' ) {
94123 setData ( { status : 'installing' , action : '' } )
95- setTimeout ( ( ) => {
96- pollStatus ( 'install' )
97- } , 3000 )
98-
124+ pollStatus ( 'install' , e . currentTarget . href )
99125 return
100126 }
101127
102128 setData ( { status : 'activating' , action : '' } )
103- setTimeout ( ( ) => {
104- pollStatus ( 'activate' )
105- } , 3000 )
129+ pollStatus ( 'activate' , e . currentTarget . href )
106130 }
107131
108132 return ( < >
@@ -134,8 +158,8 @@ const CimoDownloadNoticeWrapper = props => {
134158export default CimoDownloadNoticeWrapper
135159
136160domReady ( ( ) => {
137- if ( ! cimo || cimo . status === 'activated' || cimo . hideNotice || typeof wp === 'undefined' || ! wp . media || ! wp . media . view ||
138- ! wp . media . view . Attachment || ! wp . media . view . Attachment . Details
161+ if ( ! cimo || cimo . status === 'activated' || cimo . hideNotice ||
162+ typeof wp === 'undefined' || ! wp ? .media ? .view ? .Attachment ? .Details
139163 ) {
140164 return
141165 }
@@ -147,6 +171,10 @@ domReady( () => {
147171 render ( ) {
148172 const result = CurrentDetailsView . prototype . render . apply ( this , arguments )
149173
174+ if ( cimo ?. hideNotice ) {
175+ return result
176+ }
177+
150178 const details = this . el . querySelector ( '.attachment-info .details' )
151179 if ( details && ! this . el . querySelector ( '.stk-cimo-notice' ) ) {
152180 const noticeDiv = document . createElement ( 'div' )
0 commit comments