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
|
<?php namespace Imagify\Imagifybeat;
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
/** * Imagifybeat core. * * @since 1.9.3 * @author Grégory Viguier */ class Core { use \Imagify\Traits\InstanceGetterTrait;
/** * Class init: launch hooks. * * @since 1.9.3 * @access public * @author Grégory Viguier */ public function init() { add_action( 'wp_ajax_imagifybeat', [ $this, 'core_handler' ], 1 ); add_filter( 'imagifybeat_refresh_nonces', [ $this, 'refresh_imagifybeat_nonces' ] ); }
/** * Ajax handler for the Imagifybeat API. * * Runs when the user is logged in. * * @since 1.9.3 * @access public * @author Grégory Viguier */ public function core_handler() { if ( empty( $_POST['_nonce'] ) ) { wp_send_json_error(); }
$data = []; $response = []; $nonce_state = wp_verify_nonce( wp_unslash( $_POST['_nonce'] ), 'imagifybeat-nonce' );
// Screen_id is the same as $current_screen->id and the JS global 'pagenow'. if ( ! empty( $_POST['screen_id'] ) ) { $screen_id = sanitize_key( $_POST['screen_id'] ); } else { $screen_id = 'front'; }
if ( ! empty( $_POST['data'] ) ) { $data = wp_unslash( (array) $_POST['data'] ); }
if ( 1 !== $nonce_state ) { /** * Filters the nonces to send. * * @since 1.9.3 * @author Grégory Viguier * * @param array $response The Imagifybeat response. * @param array $data The $_POST data sent. * @param string $screen_id The screen id. */ $response = apply_filters( 'imagifybeat_refresh_nonces', $response, $data, $screen_id );
if ( false === $nonce_state ) { // User is logged in but nonces have expired. $response['nonces_expired'] = true; wp_send_json( $response ); } }
if ( ! empty( $data ) ) { /** * Filters the Imagifybeat response received. * * @since 1.9.3 * @author Grégory Viguier * * @param array $response The Imagifybeat response. * @param array $data The $_POST data sent. * @param string $screen_id The screen id. */ $response = apply_filters( 'imagifybeat_received', $response, $data, $screen_id ); }
/** * Filters the Imagifybeat response sent. * * @since 1.9.3 * @author Grégory Viguier * * @param array $response The Imagifybeat response. * @param string $screen_id The screen id. */ $response = apply_filters( 'imagifybeat_send', $response, $screen_id );
/** * Fires when Imagifybeat ticks in logged-in environments. * * Allows the transport to be easily replaced with long-polling. * * @since 1.9.3 * @author Grégory Viguier * * @param array $response The Imagifybeat response. * @param string $screen_id The screen id. */ do_action( 'imagifybeat_tick', $response, $screen_id );
// Send the current time according to the server. $response['server_time'] = time();
wp_send_json( $response ); }
/** * Add the latest Imagifybeat nonce to the Imagifybeat response. * * @since 1.9.3 * @access public * @author Grégory Viguier * * @param array $response The Imagifybeat response. * @return array The Imagifybeat response. */ public function refresh_imagifybeat_nonces( $response ) { // Refresh the Imagifybeat nonce. $response['imagifybeat_nonce'] = wp_create_nonce( 'imagifybeat-nonce' ); return $response; }
/** * Get Imagifybeat settings. * * @since 1.9.3 * @access public * @author Grégory Viguier * * @return array */ public function get_settings() { global $pagenow;
$settings = [];
if ( ! is_admin() ) { $settings['ajaxurl'] = admin_url( 'admin-ajax.php', 'relative' ); }
if ( is_user_logged_in() ) { $settings['nonce'] = wp_create_nonce( 'imagifybeat-nonce' ); }
if ( 'customize.php' === $pagenow ) { $settings['screenId'] = 'customize'; }
/** * Filters the Imagifybeat settings. * * @since 1.9.3 * @author Grégory Viguier * * @param array $settings Imagifybeat settings array. */ return (array) apply_filters( 'imagifybeat_settings', $settings ); } }
|