From 22d192ee8158e843d2f68f29d1ada62d7d335a97 Mon Sep 17 00:00:00 2001 From: osman sufy Date: Thu, 21 Nov 2024 18:15:27 +0600 Subject: [PATCH 01/14] [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', From c446b6b2f87d069fdaa521cc12f625825ef9e2ed Mon Sep 17 00:00:00 2001 From: osman sufy Date: Fri, 22 Nov 2024 12:28:35 +0600 Subject: [PATCH 02/14] [change] [filter] admin notices --- includes/Admin/Notices/Helper.php | 2 +- includes/REST/AdminNoticeController.php | 6 +++--- src/admin/components/AdminNotice.vue | 4 ++-- src/admin/notice/App.vue | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/includes/Admin/Notices/Helper.php b/includes/Admin/Notices/Helper.php index 63dd2ca234..07354af4bd 100644 --- a/includes/Admin/Notices/Helper.php +++ b/includes/Admin/Notices/Helper.php @@ -30,7 +30,7 @@ public static function dokan_get_admin_notices( $notice_scope = 'all' ) { if ( empty( $notices ) ) { return $notices; } - $allowed_types = self::$scope_type_mapping[ $notice_scope ] ?? []; + $allowed_types = apply_filters( 'dokan_admin_notices_scope', self::$scope_type_mapping[ $notice_scope ] ) ?? []; $notices = self::filter_notices_by_type( $notices, $allowed_types ); uasort( $notices, [ self::class, 'dokan_sort_notices_by_priority' ] ); diff --git a/includes/REST/AdminNoticeController.php b/includes/REST/AdminNoticeController.php index a5c94f503e..5308591480 100644 --- a/includes/REST/AdminNoticeController.php +++ b/includes/REST/AdminNoticeController.php @@ -40,7 +40,7 @@ public function register_routes() { 'args' => [ 'type' => [ 'description' => __( 'Type of notices', 'dokan-lite' ), - 'scope' => 'string', + 'context' => 'string', 'required' => false, ], ], @@ -66,8 +66,8 @@ public function register_routes() { * @return WP_REST_Response */ public function dokan_get_admin_notices( WP_REST_Request $request ) { - $notices = Helper::dokan_get_admin_notices(); - $notice_scope = $request->get_param( 'scope' ); + $notice_scope = $request->get_param( 'context' ); + $notices = Helper::dokan_get_admin_notices( $notice_scope ); return rest_ensure_response( $notices ); } diff --git a/src/admin/components/AdminNotice.vue b/src/admin/components/AdminNotice.vue index 37ba63858d..d522005348 100644 --- a/src/admin/components/AdminNotice.vue +++ b/src/admin/components/AdminNotice.vue @@ -62,7 +62,7 @@ export default { type: Number, default: 5000 }, - scope: { + context: { type: String, default: 'all' } @@ -87,7 +87,7 @@ export default { methods: { fetch() { $.ajax( { - 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}?context=${this.context}`, 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 index de247084e5..4201c441de 100644 --- a/src/admin/notice/App.vue +++ b/src/admin/notice/App.vue @@ -4,7 +4,7 @@ import AdminNotice from "admin/components/AdminNotice.vue"; From 6332d2367fdb5b58311be90278891c53b1f80ba0 Mon Sep 17 00:00:00 2001 From: osman sufy Date: Fri, 22 Nov 2024 15:56:59 +0600 Subject: [PATCH 03/14] [replace] args of notice api --- includes/REST/AdminNoticeController.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/includes/REST/AdminNoticeController.php b/includes/REST/AdminNoticeController.php index 5308591480..cc0ced4b44 100644 --- a/includes/REST/AdminNoticeController.php +++ b/includes/REST/AdminNoticeController.php @@ -38,10 +38,11 @@ public function register_routes() { 'callback' => [ $this, 'dokan_get_admin_notices' ], 'permission_callback' => [ $this, 'check_permission' ], 'args' => [ - 'type' => [ - 'description' => __( 'Type of notices', 'dokan-lite' ), - 'context' => 'string', + 'context' => [ + 'description' => __( 'Notice context', 'dokan-lite' ), + 'type' => 'string', 'required' => false, + 'default' => 'all', ], ], ], From 69bb50bce6465736c46e8f549445d29066437f6d Mon Sep 17 00:00:00 2001 From: osman sufy Date: Fri, 22 Nov 2024 16:00:16 +0600 Subject: [PATCH 04/14] [replace] scope to context --- includes/Admin/Notices/Helper.php | 4 ++-- includes/REST/AdminNoticeController.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/Admin/Notices/Helper.php b/includes/Admin/Notices/Helper.php index 07354af4bd..c22c1ae9c8 100644 --- a/includes/Admin/Notices/Helper.php +++ b/includes/Admin/Notices/Helper.php @@ -24,13 +24,13 @@ class Helper { * * @return array | void */ - public static function dokan_get_admin_notices( $notice_scope = 'all' ) { + public static function dokan_get_admin_notices( $notice_context = 'all' ) { $notices = apply_filters( 'dokan_admin_notices', [] ); if ( empty( $notices ) ) { return $notices; } - $allowed_types = apply_filters( 'dokan_admin_notices_scope', self::$scope_type_mapping[ $notice_scope ] ) ?? []; + $allowed_types = apply_filters( 'dokan_admin_notices_scope', self::$scope_type_mapping[ $notice_context ] ) ?? []; $notices = self::filter_notices_by_type( $notices, $allowed_types ); uasort( $notices, [ self::class, 'dokan_sort_notices_by_priority' ] ); diff --git a/includes/REST/AdminNoticeController.php b/includes/REST/AdminNoticeController.php index cc0ced4b44..ad093e2099 100644 --- a/includes/REST/AdminNoticeController.php +++ b/includes/REST/AdminNoticeController.php @@ -67,8 +67,8 @@ public function register_routes() { * @return WP_REST_Response */ public function dokan_get_admin_notices( WP_REST_Request $request ) { - $notice_scope = $request->get_param( 'context' ); - $notices = Helper::dokan_get_admin_notices( $notice_scope ); + $notice_context = $request->get_param( 'context' ); + $notices = Helper::dokan_get_admin_notices( $notice_context ); return rest_ensure_response( $notices ); } From aeda08495019d3798e34edd3725b67934624f13c Mon Sep 17 00:00:00 2001 From: osman sufy Date: Fri, 22 Nov 2024 16:23:39 +0600 Subject: [PATCH 05/14] [refactor] app js code --- src/admin/notice/App.vue | 6 +++++- src/admin/notice/main.js | 3 +-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/admin/notice/App.vue b/src/admin/notice/App.vue index 4201c441de..5f15d973a2 100644 --- a/src/admin/notice/App.vue +++ b/src/admin/notice/App.vue @@ -4,7 +4,11 @@ import AdminNotice from "admin/components/AdminNotice.vue"; diff --git a/src/admin/notice/main.js b/src/admin/notice/main.js index 0b0d5494ae..a83bac2e2b 100644 --- a/src/admin/notice/main.js +++ b/src/admin/notice/main.js @@ -1,6 +1,5 @@ import App from './App.vue'; -// eslint-disable-next-line import/no-extraneous-dependencies -import $ from 'jquery'; +const { jQuery: $ } = window; import Vue from 'vue'; if ( $( '#dokan-admin-notices' ).length ) { From 0ca4711e4ecbe5a240375efc16411d781764972a Mon Sep 17 00:00:00 2001 From: osman sufy Date: Fri, 22 Nov 2024 17:07:02 +0600 Subject: [PATCH 06/14] [refactor] [readable] state checking logic --- includes/Admin/Notices/Helper.php | 29 ++++++++++--------------- includes/REST/AdminNoticeController.php | 6 ++--- src/admin/components/AdminNotice.vue | 6 ++--- src/admin/notice/App.vue | 2 +- 4 files changed, 18 insertions(+), 25 deletions(-) diff --git a/includes/Admin/Notices/Helper.php b/includes/Admin/Notices/Helper.php index c22c1ae9c8..b644e60d28 100644 --- a/includes/Admin/Notices/Helper.php +++ b/includes/Admin/Notices/Helper.php @@ -10,12 +10,6 @@ * @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 @@ -24,15 +18,14 @@ class Helper { * * @return array | void */ - public static function dokan_get_admin_notices( $notice_context = 'all' ) { + public static function dokan_get_admin_notices( $notice_scope = 'local' ) { $notices = apply_filters( 'dokan_admin_notices', [] ); if ( empty( $notices ) ) { return $notices; } - $allowed_types = apply_filters( 'dokan_admin_notices_scope', self::$scope_type_mapping[ $notice_context ] ) ?? []; - $notices = self::filter_notices_by_type( $notices, $allowed_types ); + $notices = self::filter_notices_by_scope( $notices, $notice_scope ); uasort( $notices, [ self::class, 'dokan_sort_notices_by_priority' ] ); return array_values( $notices ); @@ -168,19 +161,19 @@ private static function dokan_sort_notices_by_priority( $current_notice, $next_n * Filter notices by allowed types * * @param array $notices - * @param array $allowed_types + * @param string $allowed_scope * * @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 ); - } - ); + private static function filter_notices_by_scope( array $notices, string $allowed_scope = '' ): array { + if ( 'local' === $allowed_scope || empty( $allowed_scope ) ) { + return $notices; } - return $notices; + return array_filter( + $notices, function ( $notice ) use ( $allowed_scope ) { + return $notice['scope'] === $allowed_scope; + } + ); } } diff --git a/includes/REST/AdminNoticeController.php b/includes/REST/AdminNoticeController.php index ad093e2099..7bc55b1798 100644 --- a/includes/REST/AdminNoticeController.php +++ b/includes/REST/AdminNoticeController.php @@ -38,7 +38,7 @@ public function register_routes() { 'callback' => [ $this, 'dokan_get_admin_notices' ], 'permission_callback' => [ $this, 'check_permission' ], 'args' => [ - 'context' => [ + 'scope' => [ 'description' => __( 'Notice context', 'dokan-lite' ), 'type' => 'string', 'required' => false, @@ -67,8 +67,8 @@ public function register_routes() { * @return WP_REST_Response */ public function dokan_get_admin_notices( WP_REST_Request $request ) { - $notice_context = $request->get_param( 'context' ); - $notices = Helper::dokan_get_admin_notices( $notice_context ); + $notice_context = $request->get_param( 'scope' ); + $notices = Helper::dokan_get_admin_notices( $notice_context ); return rest_ensure_response( $notices ); } diff --git a/src/admin/components/AdminNotice.vue b/src/admin/components/AdminNotice.vue index d522005348..6464fd35a5 100644 --- a/src/admin/components/AdminNotice.vue +++ b/src/admin/components/AdminNotice.vue @@ -62,9 +62,9 @@ export default { type: Number, default: 5000 }, - context: { + scope: { type: String, - default: 'all' + default: '' } }, @@ -87,7 +87,7 @@ export default { methods: { fetch() { $.ajax( { - url: `${dokan_promo.rest.root}${dokan_promo.rest.version}/admin/notices/${this.endpoint}?context=${this.context}`, + 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 index 5f15d973a2..f0c4882d33 100644 --- a/src/admin/notice/App.vue +++ b/src/admin/notice/App.vue @@ -5,7 +5,7 @@ import AdminNotice from "admin/components/AdminNotice.vue";