From 22d192ee8158e843d2f68f29d1ada62d7d335a97 Mon Sep 17 00:00:00 2001 From: osman sufy Date: Thu, 21 Nov 2024 18:15:27 +0600 Subject: [PATCH] [refactor] [readable] state checking logic --- includes/Admin/Notices/Helper.php | 30 ++++++++++++++++++++++++- includes/Assets.php | 6 +++++ includes/REST/AdminNoticeController.php | 12 +++++++++- src/admin/components/AdminNotice.vue | 6 ++++- src/admin/notice/App.vue | 13 +++++++++++ src/admin/notice/main.js | 11 +++++++++ webpack.config.js | 1 + 7 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/admin/notice/App.vue create mode 100644 src/admin/notice/main.js diff --git a/includes/Admin/Notices/Helper.php b/includes/Admin/Notices/Helper.php index f7f81baf98..63dd2ca234 100644 --- a/includes/Admin/Notices/Helper.php +++ b/includes/Admin/Notices/Helper.php @@ -10,6 +10,12 @@ * @sience 3.3.3 */ class Helper { + private static array $scope_type_mapping = [ + 'global' => [ 'alert', 'warning', 'database' ], + 'local' => [ 'dokan', 'success', 'info', 'alert' ], + 'promo' => [ 'promo' ], + 'all' => [], // Empty array means all types are allowed + ]; /** * This method will display notices only under Dokan menu and all of its sub-menu pages @@ -18,13 +24,15 @@ class Helper { * * @return array | void */ - public static function dokan_get_admin_notices() { + public static function dokan_get_admin_notices( $notice_scope = 'all' ) { $notices = apply_filters( 'dokan_admin_notices', [] ); if ( empty( $notices ) ) { return $notices; } + $allowed_types = self::$scope_type_mapping[ $notice_scope ] ?? []; + $notices = self::filter_notices_by_type( $notices, $allowed_types ); uasort( $notices, [ self::class, 'dokan_sort_notices_by_priority' ] ); return array_values( $notices ); @@ -155,4 +163,24 @@ private static function dokan_sort_notices_by_priority( $current_notice, $next_n return -1; } + + /** + * Filter notices by allowed types + * + * @param array $notices + * @param array $allowed_types + * + * @return array + */ + private static function filter_notices_by_type( array $notices, array $allowed_types ): array { + if ( ! empty( $allowed_types ) ) { + $notices = array_filter( + $notices, function ( $notice ) use ( $allowed_types ) { + return in_array( $notice['type'], $allowed_types, true ); + } + ); + } + + return $notices; + } } diff --git a/includes/Assets.php b/includes/Assets.php index fc9e7c96a2..7f79cca66f 100644 --- a/includes/Assets.php +++ b/includes/Assets.php @@ -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' ), @@ -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' ], diff --git a/includes/REST/AdminNoticeController.php b/includes/REST/AdminNoticeController.php index e044b5fcf5..a5c94f503e 100644 --- a/includes/REST/AdminNoticeController.php +++ b/includes/REST/AdminNoticeController.php @@ -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; /** @@ -36,6 +37,13 @@ public function register_routes() { 'methods' => WP_REST_Server::READABLE, 'callback' => [ $this, 'dokan_get_admin_notices' ], 'permission_callback' => [ $this, 'check_permission' ], + 'args' => [ + 'type' => [ + 'description' => __( 'Type of notices', 'dokan-lite' ), + 'scope' => 'string', + 'required' => false, + ], + ], ], ] ); @@ -53,11 +61,13 @@ public function register_routes() { /** * Get dokan specific notices + * @param WP_REST_Request $request * * @return WP_REST_Response */ - public function dokan_get_admin_notices() { + public function dokan_get_admin_notices( WP_REST_Request $request ) { $notices = Helper::dokan_get_admin_notices(); + $notice_scope = $request->get_param( 'scope' ); return rest_ensure_response( $notices ); } diff --git a/src/admin/components/AdminNotice.vue b/src/admin/components/AdminNotice.vue index 69d02dd2ba..37ba63858d 100644 --- a/src/admin/components/AdminNotice.vue +++ b/src/admin/components/AdminNotice.vue @@ -62,6 +62,10 @@ export default { type: Number, default: 5000 }, + scope: { + type: String, + default: 'all' + } }, data() { @@ -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}`, method: 'get', beforeSend: function ( xhr ) { xhr.setRequestHeader( 'X-WP-Nonce', dokan_promo.rest.nonce ); diff --git a/src/admin/notice/App.vue b/src/admin/notice/App.vue new file mode 100644 index 0000000000..de247084e5 --- /dev/null +++ b/src/admin/notice/App.vue @@ -0,0 +1,13 @@ + + + + + diff --git a/src/admin/notice/main.js b/src/admin/notice/main.js new file mode 100644 index 0000000000..0b0d5494ae --- /dev/null +++ b/src/admin/notice/main.js @@ -0,0 +1,11 @@ +import App from './App.vue'; +// eslint-disable-next-line import/no-extraneous-dependencies +import $ from 'jquery'; +import Vue from 'vue'; + +if ( $( '#dokan-admin-notices' ).length ) { + new Vue( { + el: '#dokan-admin-notices', + render: ( h ) => h( App ), + } ); +} diff --git a/webpack.config.js b/webpack.config.js index 7e7391da77..53c5ed26fa 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -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',