Skip to content

Commit

Permalink
[refactor] [readable] state checking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
osmansufy committed Nov 21, 2024
1 parent 3d3c9d3 commit 22d192e
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 3 deletions.
30 changes: 29 additions & 1 deletion includes/Admin/Notices/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 );
Expand Down Expand Up @@ -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;
}
}
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
12 changes: 11 additions & 1 deletion 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,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,
],
],
],
]
);
Expand All @@ -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 );
}
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: 'all'
}
},
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}`,
method: 'get',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', dokan_promo.rest.nonce );
Expand Down
13 changes: 13 additions & 0 deletions src/admin/notice/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script setup>
import AdminNotice from "admin/components/AdminNotice.vue";
</script>

<template>
<AdminNotice interval="10000" endpoint="admin"></AdminNotice>

</template>

<style scoped lang="less">
</style>
11 changes: 11 additions & 0 deletions src/admin/notice/main.js
Original file line number Diff line number Diff line change
@@ -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 ),
} );
}
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

0 comments on commit 22d192e

Please sign in to comment.