PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.0
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / Imagifybeat / Core.php

Core.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.0, at classes/Imagifybeat/Core.php

169 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\Imagifybeat;
3
4 use Imagify\Traits\InstanceGetterTrait;
5
6 /**
7 * Imagifybeat core.
8 *
9 * @since 1.9.3
10 * @author Grégory Viguier
11 */
12 final class Core {
13 use InstanceGetterTrait;
14
15 /**
16 * Class init: launch hooks.
17 *
18 * @since 1.9.3
19 * @access public
20 * @author Grégory Viguier
21 */
22 public function init() {
23 add_action( 'wp_ajax_imagifybeat', [ $this, 'core_handler' ], 1 );
24 add_filter( 'imagifybeat_refresh_nonces', [ $this, 'refresh_imagifybeat_nonces' ] );
25 }
26
27 /**
28 * Ajax handler for the Imagifybeat API.
29 *
30 * Runs when the user is logged in.
31 *
32 * @since 1.9.3
33 * @access public
34 * @author Grégory Viguier
35 */
36 public function core_handler() {
37 if ( empty( $_POST['_nonce'] ) ) {
38 wp_send_json_error();
39 }
40
41 $data = [];
42 $response = [];
43 $nonce_state = wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_nonce'] ) ), 'imagifybeat-nonce' );
44
45 // Screen_id is the same as $current_screen->id and the JS global 'pagenow'.
46 if ( ! empty( $_POST['screen_id'] ) ) {
47 $screen_id = sanitize_key( $_POST['screen_id'] );
48 } else {
49 $screen_id = 'front';
50 }
51
52 if ( ! empty( $_POST['data'] ) ) {
53 $data = wp_unslash( (array) $_POST['data'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
54 }
55
56 if ( 1 !== $nonce_state ) {
57 /**
58 * Filters the nonces to send.
59 *
60 * @since 1.9.3
61 *
62 * @param array $response The Imagifybeat response.
63 * @param array $data The $_POST data sent.
64 * @param string $screen_id The screen id.
65 */
66 $response = wpm_apply_filters_typed( 'array', 'imagifybeat_refresh_nonces', $response, $data, $screen_id );
67
68 if ( false === $nonce_state ) {
69 // User is logged in but nonces have expired.
70 $response['nonces_expired'] = true;
71 wp_send_json( $response );
72 }
73 }
74
75 if ( ! empty( $data ) ) {
76 /**
77 * Filters the Imagifybeat response received.
78 *
79 * @since 1.9.3
80 *
81 * @param array $response The Imagifybeat response.
82 * @param array $data The $_POST data sent.
83 * @param string $screen_id The screen id.
84 */
85 $response = wpm_apply_filters_typed( 'array', 'imagifybeat_received', $response, $data, $screen_id );
86 }
87
88 /**
89 * Filters the Imagifybeat response sent.
90 *
91 * @since 1.9.3
92 *
93 * @param array $response The Imagifybeat response.
94 * @param string $screen_id The screen id.
95 */
96 $response = wpm_apply_filters_typed( 'array', 'imagifybeat_send', $response, $screen_id );
97
98 /**
99 * Fires when Imagifybeat ticks in logged-in environments.
100 *
101 * Allows the transport to be easily replaced with long-polling.
102 *
103 * @since 1.9.3
104 * @author Grégory Viguier
105 *
106 * @param array $response The Imagifybeat response.
107 * @param string $screen_id The screen id.
108 */
109 do_action( 'imagifybeat_tick', $response, $screen_id );
110
111 // Send the current time according to the server.
112 $response['server_time'] = time();
113
114 wp_send_json( $response );
115 }
116
117 /**
118 * Add the latest Imagifybeat nonce to the Imagifybeat response.
119 *
120 * @since 1.9.3
121 * @access public
122 * @author Grégory Viguier
123 *
124 * @param array $response The Imagifybeat response.
125 * @return array The Imagifybeat response.
126 */
127 public function refresh_imagifybeat_nonces( $response ) {
128 // Refresh the Imagifybeat nonce.
129 $response['imagifybeat_nonce'] = wp_create_nonce( 'imagifybeat-nonce' );
130 return $response;
131 }
132
133 /**
134 * Get Imagifybeat settings.
135 *
136 * @since 1.9.3
137 * @access public
138 * @author Grégory Viguier
139 *
140 * @return array
141 */
142 public function get_settings() {
143 global $pagenow;
144
145 $settings = [];
146
147 if ( ! is_admin() ) {
148 $settings['ajaxurl'] = admin_url( 'admin-ajax.php', 'relative' );
149 }
150
151 if ( is_user_logged_in() ) {
152 $settings['nonce'] = wp_create_nonce( 'imagifybeat-nonce' );
153 }
154
155 if ( 'customize.php' === $pagenow ) {
156 $settings['screenId'] = 'customize';
157 }
158
159 /**
160 * Filters the Imagifybeat settings.
161 *
162 * @since 1.9.3
163 *
164 * @param array $settings Imagifybeat settings array.
165 */
166 return (array) wpm_apply_filters_typed( 'array', 'imagifybeat_settings', $settings );
167 }
168 }
169