Skip to content

Commit

Permalink
update: filter admin notices by scope
Browse files Browse the repository at this point in the history
  • Loading branch information
mralaminahamed committed Nov 25, 2024
1 parent bde68e9 commit 4064478
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
45 changes: 12 additions & 33 deletions includes/REST/AdminNoticeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ public function register_routes() {
'permission_callback' => [ $this, 'check_permission' ],
'args' => [
'scope' => [
'description' => __( 'Notice context', 'dokan-lite' ),
'description' => __( 'Choose notice scope: "local" displays only on Dokan pages, "global" displays across the entire site.', 'dokan-lite' ),
'type' => 'string',
'enum' => [ 'local', 'global' ],
'required' => false,
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
],
],
],
Expand All @@ -68,21 +70,20 @@ public function register_routes() {
*/
public function dokan_get_admin_notices( WP_REST_Request $request ) {
$notice_scope = $request->get_param( 'scope' );
$notice_scope = $notice_scope ?? 'local';
$notice_scope = ! empty( $notice_scope ) ? $notice_scope : 'local';

$notices = Helper::dokan_get_admin_notices();
$notices = array_map(
function ( $notice ) {
$notice['scope'] = $notice['scope'] ?? 'local';

return $notice;
}, $notices
);

// Filter notices by scope
$filtered_notices = $this->filter_notices_by_scope( $notices, $notice_scope );
$filter_notices = array_filter(
$notices,
function ( $notice ) use ( $notice_scope ) {
return $notice['scope'] === ( $notice_scope ?? 'local' );
}
);
$filter_notices = array_values( $filter_notices );

return rest_ensure_response( $filtered_notices );
return rest_ensure_response( $filter_notices );
}

/**
Expand All @@ -95,26 +96,4 @@ public function get_promo_notices() {

return rest_ensure_response( $notices );
}

/**
* Filter notices by allowed types
*
* @since DOKAN_SINCE
*
* @param array $notices
* @param string $allowed_scope
*
* @return array
*/
private function filter_notices_by_scope( array $notices, string $allowed_scope = '' ): array {
if ( empty( $allowed_scope ) ) {
return $notices;
}

return array_filter(
$notices, function ( $notice ) use ( $allowed_scope ) {
return $notice['scope'] === $allowed_scope;
}
);
}
}
3 changes: 0 additions & 3 deletions src/admin/components/AdminNotice.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ export default {
xhr.setRequestHeader( 'X-WP-Nonce', dokan_promo.rest.nonce );
},
} ).done( response => {
if ( ! Array.isArray( response ) ){
response = Object.entries( response ).map( ( [ id, data ] ) => ( { id, ...data } ) );
}
this.notices = response.filter( notice => notice.description || notice.title );
this.startAutoSlide();
});
Expand Down

0 comments on commit 4064478

Please sign in to comment.