Skip to content
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

Enhance : Admin Notice Show gloablly & extand API #2446

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion includes/Admin/Notices/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ class Helper {
*
* @return array | void
*/
public static function dokan_get_admin_notices() {
public static function dokan_get_admin_notices( $notice_scope = 'local' ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls do not modify this files. Undo all changes.

$notices = apply_filters( 'dokan_admin_notices', [] );

if ( empty( $notices ) ) {
return $notices;
}

$notices = self::filter_notices_by_scope( $notices, $notice_scope );
uasort( $notices, [ self::class, 'dokan_sort_notices_by_priority' ] );

return array_values( $notices );
Expand Down Expand Up @@ -155,4 +156,24 @@ private static function dokan_sort_notices_by_priority( $current_notice, $next_n

return -1;
}

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

return array_filter(
$notices, function ( $notice ) use ( $allowed_scope ) {
return $notice['scope'] === $allowed_scope;
}
);
}
}
6 changes: 6 additions & 0 deletions includes/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __construct() {
*/
public function load_dokan_admin_notices_scripts() {
wp_enqueue_script( 'dokan-promo-notice-js' );
wp_enqueue_script( 'dokan-admin-notice-js' );
$vue_localize_script = apply_filters(
'dokan_promo_notice_localize_script', [
'ajaxurl' => admin_url( 'admin-ajax.php' ),
Expand Down Expand Up @@ -535,6 +536,11 @@ public function get_scripts() {
'deps' => [ 'jquery', 'dokan-vue-vendor' ],
'version' => filemtime( $asset_path . 'js/dokan-promo-notice.js' ),
],
'dokan-admin-notice-js' => [
'src' => $asset_url . '/js/dokan-admin-notice.js',
'deps' => [ 'jquery', 'dokan-vue-vendor' ],
'version' => filemtime( $asset_path . 'js/dokan-admin-notice.js' ),
],
'dokan-reverse-withdrawal' => [
'src' => $asset_url . '/js/reverse-withdrawal.js',
'deps' => [ 'jquery', 'dokan-util-helper', 'dokan-vue-vendor', 'dokan-date-range-picker' ],
Expand Down
15 changes: 13 additions & 2 deletions includes/REST/AdminNoticeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use WeDevs\Dokan\Admin\Notices\Helper;
use WP_REST_Response;
use WP_REST_Server;
use WP_REST_Request;
use WeDevs\Dokan\Abstracts\DokanRESTAdminController;

/**
Expand Down Expand Up @@ -36,6 +37,14 @@ public function register_routes() {
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'dokan_get_admin_notices' ],
'permission_callback' => [ $this, 'check_permission' ],
'args' => [
'scope' => [
'description' => __( 'Notice context', 'dokan-lite' ),
'type' => 'string',
'required' => false,
'default' => 'all',
],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the default value.

],
],
]
);
Expand All @@ -53,11 +62,13 @@ public function register_routes() {

/**
* Get dokan specific notices
* @param WP_REST_Request $request
*
* @return WP_REST_Response
*/
public function dokan_get_admin_notices() {
$notices = Helper::dokan_get_admin_notices();
public function dokan_get_admin_notices( WP_REST_Request $request ) {
$notice_scope = $request->get_param( 'scope' );
$notices = Helper::dokan_get_admin_notices( $notice_scope );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retrieve all notices and perform filter operation here.


return rest_ensure_response( $notices );
}
Expand Down
6 changes: 5 additions & 1 deletion src/admin/components/AdminNotice.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export default {
type: Number,
default: 5000
},
scope: {
type: String,
default: ''
}
},

data() {
Expand All @@ -83,7 +87,7 @@ export default {
methods: {
fetch() {
$.ajax( {
url: `${dokan_promo.rest.root}${dokan_promo.rest.version}/admin/notices/${this.endpoint}`,
url: `${dokan_promo.rest.root}${dokan_promo.rest.version}/admin/notices/${this.endpoint}?scope=${this.scope}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Encode URL parameters to prevent injection.

The scope parameter should be properly encoded when used in the URL.

-url: `${dokan_promo.rest.root}${dokan_promo.rest.version}/admin/notices/${this.endpoint}?scope=${this.scope}`,
+url: `${dokan_promo.rest.root}${dokan_promo.rest.version}/admin/notices/${this.endpoint}?scope=${encodeURIComponent(this.scope)}`,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
url: `${dokan_promo.rest.root}${dokan_promo.rest.version}/admin/notices/${this.endpoint}?scope=${this.scope}`,
url: `${dokan_promo.rest.root}${dokan_promo.rest.version}/admin/notices/${this.endpoint}?scope=${encodeURIComponent(this.scope)}`,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass ?scope=${this.scope} only if the scope is not empty.

method: 'get',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', dokan_promo.rest.nonce );
Expand Down
17 changes: 17 additions & 0 deletions src/admin/notice/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script setup>

import AdminNotice from "admin/components/AdminNotice.vue";
</script>

<template>
<AdminNotice
scope="global"
:interval="10000"
endpoint="admin"
/>

</template>

<style scoped lang="less">

</style>
10 changes: 10 additions & 0 deletions src/admin/notice/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import App from './App.vue';
const { jQuery: $ } = window;
import Vue from 'vue';

if ( $( '#dokan-admin-notices' ).length ) {
new Vue( {
el: '#dokan-admin-notices',
render: ( h ) => h( App ),
} );
}
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const entryPoint = {
'./src/utils/vue-vendor.js',
],
'dokan-promo-notice': './src/promo-notice/main.js',
'dokan-admin-notice': './src/admin/notice/main.js',
'reverse-withdrawal': './assets/src/js/reverse-withdrawal.js',
'product-category-ui': './assets/src/js/product-category-ui.js',
'dokan-admin-product': './assets/src/js/dokan-admin-product.js',
Expand Down
Loading