class-notice.php
5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
* Displays a migration notice.
*
* @package Block_Lab
* @copyright Copyright(c) 2020, Block Lab
* @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0)
*/
namespace Block_Lab\Admin\Migration;
use Block_Lab\Component_Abstract;
/**
* Class Notice
*/
class Notice extends Component_Abstract {
/**
* The AJAX action to dismiss the migration notice.
*
* @var string
*/
const NOTICE_AJAX_ACTION = 'bl_dismiss_migration_notice';
/**
* The action of the migration notice nonce.
*
* @var string
*/
const NOTICE_NONCE_ACTION = 'bl-migration-nonce';
/**
* The name of the migration notice nonce.
*
* @var string
*/
const NOTICE_NONCE_NAME = 'bl-migration-nonce-name';
/**
* The slug of the stylesheet for the migration notice.
*
* @var string
*/
const NOTICE_STYLE_SLUG = 'block-lab-migration-notice-style';
/**
* The slug of the script for the migration notice.
*
* @var string
*/
const NOTICE_SCRIPT_SLUG = 'block-lab-migration-notice-script';
/**
* The user meta key to store whether a user has dismissed the migration notice.
*
* @var string
*/
const NOTICE_USER_META_KEY = 'block_lab_show_migration_notice';
/**
* The user meta value stored if a user has dismissed the migration notice.
*
* @var string
*/
const NOTICE_DISMISSED_META_VALUE = 'dismissed';
/**
* The capability required to see the notice.
*
* @var string
*/
const NOTICE_CAPABILITY = 'install_plugins';
/**
* Adds an action for the notice.
*/
public function register_hooks() {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
add_action( 'wp_ajax_' . self::NOTICE_AJAX_ACTION, [ $this, 'ajax_handler_migration_notice' ] );
}
/**
* Outputs the migration notice if this is on the right page and the user has the right permission.
*/
public function render_migration_notice() {
if ( ! $this->should_display_migration_notice() ) {
return;
}
// @todo: verify that this doc page exists, or change it to one that does exist.
$learn_more_link = 'https://getblocklab.com/docs/genesis-custom-blocks';
$migration_url = add_query_arg(
[
'post_type' => block_lab()->get_post_type_slug(),
'page' => 'block-lab-migration',
],
admin_url( 'edit.php' )
);
?>
<div id="bl-migration-notice" class="notice notice-info bl-notice-migration">
<?php wp_nonce_field( self::NOTICE_NONCE_ACTION, self::NOTICE_NONCE_NAME, false ); ?>
<div class="bl-migration-copy">
<p>
<?php
printf(
/* translators: %1$s: the plugin name */
esc_html__( 'The Block Lab team have moved. For future updates and improvements, migrate now to the new home of custom blocks: %1$s.', 'block-lab' ),
sprintf(
'<strong>%1$s</strong>',
esc_html__( 'Genesis Custom Blocks', 'block-lab' )
)
);
?>
<a target="_blank" rel="noopener noreferrer" class="bl-notice-migration__learn-more" href="<?php echo esc_url( $learn_more_link ); ?>">
<?php esc_html_e( 'Learn more', 'block-lab' ); ?>
</a>
</p>
</div>
<button id="bl-notice-not-now" href="#" class="bl-notice-option button button-secondary">
<?php esc_html_e( 'Not Now', 'block-lab' ); ?>
</button>
<a href="<?php echo esc_url( $migration_url ); ?>" class="bl-notice-option button button-primary">
<?php esc_html_e( 'Migrate', 'block-lab' ); ?>
</a>
</div>
<div id="bl-not-now-notice" class="notice notice-info bl-notice-migration bl-hidden">
<div class="bl-migration-copy">
<p><?php esc_html_e( "When you're ready, our migration tool is available in the main menu, under Block Lab > Migrate.", 'block-lab' ); ?></p>
</div>
<a href="#" id="bl-notice-ok" class="bl-notice-option">
<?php esc_html_e( 'Okay', 'block-lab' ); ?>
</a>
</div>
<?php
}
/**
* Enqueues the migration notice assets.
*/
public function enqueue_assets() {
if ( ! $this->should_display_migration_notice() ) {
return;
}
wp_enqueue_style(
self::NOTICE_STYLE_SLUG,
$this->plugin->get_url( 'css/admin.migration-notice.css' ),
[],
$this->plugin->get_version()
);
wp_enqueue_script(
self::NOTICE_SCRIPT_SLUG,
$this->plugin->get_url( 'js/admin.migration-notice.js' ),
[],
$this->plugin->get_version(),
true
);
}
/**
* Gets whether the migration notice should display.
*
* This should display on Block Lab > Content Blocks,
* /wp-admin/plugins.php, the Dashboard, and Block Lab > Settings.
*
* @return bool Whether the migration notice should display.
*/
public function should_display_migration_notice() {
if ( ! current_user_can( self::NOTICE_CAPABILITY ) ) {
return false;
}
// If the user has dismissed the notice, it shouldn't appear again.
if ( self::NOTICE_DISMISSED_META_VALUE === get_user_meta( get_current_user_id(), self::NOTICE_USER_META_KEY, true ) ) {
return false;
}
$screen = get_current_screen();
return (
( isset( $screen->base, $screen->post_type ) && 'edit' === $screen->base && 'block_lab' === $screen->post_type )
||
( isset( $screen->base ) && in_array( $screen->base, [ 'plugins', 'dashboard', 'block_lab_page_block-lab-settings' ], true ) )
);
}
/**
* Handles an AJAX request to not display the notice.
*
* This stores in the user meta the fact that the notice was dismissed,
* so it's not displayed again.
*/
public function ajax_handler_migration_notice() {
check_ajax_referer( self::NOTICE_NONCE_ACTION, self::NOTICE_NONCE_NAME );
if ( ! current_user_can( self::NOTICE_CAPABILITY ) ) {
wp_send_json_error();
}
update_user_meta( get_current_user_id(), self::NOTICE_USER_META_KEY, self::NOTICE_DISMISSED_META_VALUE );
wp_send_json_success();
}
}