Skip to content

Commit df79ec5

Browse files
committed
code rabbit's qa fixes
1 parent 7de0280 commit df79ec5

File tree

2 files changed

+67
-22
lines changed

2 files changed

+67
-22
lines changed

src/lazy-components/cimo/index.js

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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 => {
134158
export default CimoDownloadNoticeWrapper
135159

136160
domReady( () => {
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' )

src/lazy-components/cimo/index.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class Stackable_Cimo_Notice {
1616

1717
function __construct() {
1818
add_action( 'admin_init', array( $this, 'register_settings' ) );
19-
add_action( 'rest_api_init', array( $this, 'register_settings' ) );
2019

2120
// For polling the status
2221
add_action('wp_ajax_stackable_check_cimo_status', array( $this, 'check_cimo_status' ) );
@@ -34,7 +33,7 @@ public function register_settings() {
3433
array(
3534
'type' => 'boolean',
3635
'description' => __( 'Hides the Cimo download notice.', STACKABLE_I18N ),
37-
'sanitize_callback' => 'sanitize_text_field',
36+
'sanitize_callback' => 'rest_sanitize_boolean',
3837
'show_in_rest' => true,
3938
'default' => false,
4039
)
@@ -108,6 +107,7 @@ public function enqueue_script() {
108107
$data = array(
109108
'status' => $cimo_status,
110109
'action' => html_entity_decode( $cimo_action ),
110+
'nonce' => wp_create_nonce( 'stackable_cimo_status' )
111111
);
112112

113113
// Expose the Cimo plugin status and action URL for use in JS
@@ -141,12 +141,29 @@ public function localize_hide_cimo_notice( $args ) {
141141
* Used for polling Cimo plugin status changes via AJAX in the admin UI.
142142
*/
143143
function check_cimo_status() {
144-
$action = sanitize_text_field( $_POST['user_action'] );
144+
// Verify nonce
145+
if ( ! check_ajax_referer( 'stackable_cimo_status', 'nonce', false ) ) {
146+
wp_send_json_error( array( 'status' => 'error', 'message' => 'Security check failed.' ), 403 );
147+
return;
148+
}
149+
150+
$action = isset( $_POST['user_action'] ) ? sanitize_text_field( $_POST['user_action'] ) : '';
145151
$response = array(
146152
'status' => 'activated',
147153
'action' => ''
148154
);
149155

156+
if ( ! $action || ( $action !== 'install' && $action !== 'activate' ) ) {
157+
wp_send_json_error( array( 'status' => 'error', 'message' => 'Invalid request action.' ), 400 );
158+
return;
159+
}
160+
161+
if ( ( $action === 'install' && ! current_user_can( 'install_plugins' ) ) ||
162+
( $action === 'activate' && ! current_user_can( 'activate_plugins' ) ) ) {
163+
wp_send_json_error( array( 'status' => 'error', 'message' => 'Insufficient permissions.' ), 403 );
164+
return;
165+
}
166+
150167
if ( $action === 'install' && ! self::is_plugin_installed() ) {
151168
$response[ 'status' ] = 'installing';
152169
} else if ( ! self::is_plugin_activated() ) {
@@ -163,7 +180,7 @@ function check_cimo_status() {
163180
) ) : '';
164181
}
165182

166-
wp_send_json( $response );
183+
wp_send_json_success( $response );
167184
}
168185
}
169186

0 commit comments

Comments
 (0)