PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.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.0, at classes/Imagifybeat/Core.php

173 lines 4.0 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 defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
5
6 /**
7 * Imagifybeat core.
8 *
9 * @since 1.9.3
10 * @author Grégory Viguier
11 */
12 class Core {
13 use \Imagify\Traits\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( 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'] );
54 }
55
56 if ( 1 !== $nonce_state ) {
57 /**
58 * Filters the nonces to send.
59 *
60 * @since 1.9.3
61 * @author Grégory Viguier
62 *
63 * @param array $response The Imagifybeat response.
64 * @param array $data The $_POST data sent.
65 * @param string $screen_id The screen id.
66 */
67 $response = apply_filters( 'imagifybeat_refresh_nonces', $response, $data, $screen_id );
68
69 if ( false === $nonce_state ) {
70 // User is logged in but nonces have expired.
71 $response['nonces_expired'] = true;
72 wp_send_json( $response );
73 }
74 }
75
76 if ( ! empty( $data ) ) {
77 /**
78 * Filters the Imagifybeat response received.
79 *
80 * @since 1.9.3
81 * @author Grégory Viguier
82 *
83 * @param array $response The Imagifybeat response.
84 * @param array $data The $_POST data sent.
85 * @param string $screen_id The screen id.
86 */
87 $response = apply_filters( 'imagifybeat_received', $response, $data, $screen_id );
88 }
89
90 /**
91 * Filters the Imagifybeat response sent.
92 *
93 * @since 1.9.3
94 * @author Grégory Viguier
95 *
96 * @param array $response The Imagifybeat response.
97 * @param string $screen_id The screen id.
98 */
99 $response = apply_filters( 'imagifybeat_send', $response, $screen_id );
100
101 /**
102 * Fires when Imagifybeat ticks in logged-in environments.
103 *
104 * Allows the transport to be easily replaced with long-polling.
105 *
106 * @since 1.9.3
107 * @author Grégory Viguier
108 *
109 * @param array $response The Imagifybeat response.
110 * @param string $screen_id The screen id.
111 */
112 do_action( 'imagifybeat_tick', $response, $screen_id );
113
114 // Send the current time according to the server.
115 $response['server_time'] = time();
116
117 wp_send_json( $response );
118 }
119
120 /**
121 * Add the latest Imagifybeat nonce to the Imagifybeat response.
122 *
123 * @since 1.9.3
124 * @access public
125 * @author Grégory Viguier
126 *
127 * @param array $response The Imagifybeat response.
128 * @return array The Imagifybeat response.
129 */
130 public function refresh_imagifybeat_nonces( $response ) {
131 // Refresh the Imagifybeat nonce.
132 $response['imagifybeat_nonce'] = wp_create_nonce( 'imagifybeat-nonce' );
133 return $response;
134 }
135
136 /**
137 * Get Imagifybeat settings.
138 *
139 * @since 1.9.3
140 * @access public
141 * @author Grégory Viguier
142 *
143 * @return array
144 */
145 public function get_settings() {
146 global $pagenow;
147
148 $settings = [];
149
150 if ( ! is_admin() ) {
151 $settings['ajaxurl'] = admin_url( 'admin-ajax.php', 'relative' );
152 }
153
154 if ( is_user_logged_in() ) {
155 $settings['nonce'] = wp_create_nonce( 'imagifybeat-nonce' );
156 }
157
158 if ( 'customize.php' === $pagenow ) {
159 $settings['screenId'] = 'customize';
160 }
161
162 /**
163 * Filters the Imagifybeat settings.
164 *
165 * @since 1.9.3
166 * @author Grégory Viguier
167 *
168 * @param array $settings Imagifybeat settings array.
169 */
170 return (array) apply_filters( 'imagifybeat_settings', $settings );
171 }
172 }
173