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

Merged
merged 14 commits into from
Nov 26, 2024
Merged
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
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
28 changes: 26 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,16 @@ public function register_routes() {
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'dokan_get_admin_notices' ],
'permission_callback' => [ $this, 'check_permission' ],
'args' => [
'scope' => [
'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 @@ -53,13 +64,26 @@ 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 ) {
$notice_scope = $request->get_param( 'scope' );
$notice_scope = ! empty( $notice_scope ) ? $notice_scope : 'local';

$notices = Helper::dokan_get_admin_notices();

return rest_ensure_response( $notices );
// Filter notices by 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( $filter_notices );
}

/**
Expand Down
3 changes: 2 additions & 1 deletion includes/Upgrade/AdminNotice.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public static function show_notice( $notices ) {
$notices[] = [
'type' => 'info',
'title' => __( 'Dokan Data Update Required', 'dokan-lite' ),
'description' => __( 'We need to update your install to the latest version', 'dokan-lite' ),
'description' => __( 'Updating your Dokan data is required to continue functional operations.', 'dokan-lite' ),
'priority' => 1,
'scope' => 'global',
'actions' => [
[
'type' => 'primary',
Expand Down
7 changes: 6 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 @@ -82,8 +86,9 @@ export default {

methods: {
fetch() {
const notice_scope = this.scope ? `?scope=${this.scope}` : '';
$.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}${notice_scope}`,
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>
14 changes: 14 additions & 0 deletions src/admin/notice/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Vue from 'vue';
import App from './App.vue';
import Mixin from '../../utils/Mixin';

const { jQuery: $ } = window;

if ( $( '#dokan-admin-notices' ).length ) {
Vue.mixin( Mixin );

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