DismissNotices.php
1.24 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
<?php
namespace WPML\Notices;
class DismissNotices implements \IWPML_Backend_Action {
const OPTION = 'wpml_dismiss_notice';
const CSS_CLASS = 'wpml_dismiss_notice';
public function add_hooks() {
add_action( 'wp_ajax_wpml_dismiss_notice', [ $this, 'toggleDismiss' ] );
add_action(
'admin_enqueue_scripts',
function () {
wp_enqueue_script(
'wpml-dismiss-notice',
ICL_PLUGIN_URL . '/dist/js/notices/app.js',
[],
ICL_SITEPRESS_VERSION
);
}
);
}
public function toggleDismiss() {
$postData = wpml_collect( $_POST );
$id = $postData->get( 'id', null );
if ( ! $id ) {
return wp_send_json_error( 'ID of notice is not defined' );
}
$options = get_option( self::OPTION, [] );
$options[ $id ] = $postData->get( 'dismiss', false ) === 'true';
update_option( self::OPTION, $options );
return wp_send_json_success();
}
/**
* @param int $id
*
* @return bool
*/
public function isDismissed( $id ) {
return wpml_collect( get_option( self::OPTION, [] ) )->get( $id, false );
}
/**
* @param int $id
*
* @return string
*/
public function renderCheckbox( $id ) {
return sprintf(
'<input type="checkbox" class="%s" data-id="%s" />',
self::CSS_CLASS,
$id
);
}
}