PluginProbe
SendWP / 0.0.2
SendWP v0.0.2
trunk 0.0.1 0.0.2 0.0.3 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1 1.2.10 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.4.4 1.4.5 1.4.6 1.4.8
← All changes | includes/functions.php +199 -389 1.2.90.0.2 View file →
@@ -1,389 +1,199 @@
1 -<?php
2 -/**
3 - * Misc. Functions
4 - */
5 -
6 -/**
7 - * Get the Server URL
8 - *
9 - * @return String
10 - */
11 -function sendwp_get_server_url() {
12 - $server_url = ( defined( 'SENDWP_SERVER_URL' ) ) ? SENDWP_SERVER_URL : 'https://app.sendwp.com';
13 -
14 - return trailingslashit( $server_url );
15 -}
16 -
17 -/**
18 - * Get the request authentication header
19 - *
20 - * @return String
21 - */
22 -function sendwp_get_request_auth_header() {
23 - return 'Basic ' . base64_encode( sendwp_get_client_name() . ':' . sendwp_get_client_secret() );
24 -}
25 -
26 -/**
27 - * Get client name
28 - *
29 - * @return String
30 - */
31 -function sendwp_get_client_name() {
32 - $site_url = get_site_url();
33 -
34 - return parse_url( $site_url, PHP_URL_HOST );
35 -}
36 -
37 -/**
38 - * Get the client_url
39 - *
40 - * @return String
41 - */
42 -function sendwp_get_client_url() {
43 - return get_site_url();
44 -}
45 -
46 -/**
47 - * Get client redirect url
48 - *
49 - * @return String
50 - */
51 -function sendwp_get_client_redirect() {
52 - return add_query_arg( 'page', 'sendwp', get_admin_url( null, 'tools.php' ) );
53 -}
54 -
55 -/**
56 - * Get client secret
57 - *
58 - * @return String
59 - */
60 -function sendwp_get_client_secret() {
61 - $secret = get_option( 'sendwp_client_secret' );
62 -
63 - if ( ! $secret ) {
64 - $secret = sendwp_generate_secret();
65 -
66 - sendwp_set_client_secret( $secret );
67 - }
68 -
69 - return $secret;
70 -}
71 -
72 -/**
73 - * Set the client secret
74 - *
75 - * @param String $secret
76 - *
77 - * @return Boolean
78 - */
79 -function sendwp_set_client_secret( $secret ) {
80 - return update_option( 'sendwp_client_secret', $secret );
81 -}
82 -
83 -/**
84 - * Generate the secret
85 - *
86 - * @param Integer $length
87 - *
88 - * @return String
89 - */
90 -function sendwp_generate_secret( $length = 40 ) {
91 - if ( 0 >= $length ) {
92 - $length = 40; // Min key length.
93 - }
94 -
95 - if ( 255 <= $length ) {
96 - $length = 255; // Max key length.
97 - }
98 -
99 - $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
100 - $random_string = '';
101 -
102 - for ( $i = 0; $i < $length; $i ++ ) {
103 - $random_string .= $characters[ rand( 0, strlen( $characters ) - 1 ) ];
104 - }
105 -
106 - return $random_string;
107 -}
108 -
109 -/**
110 - * Connect the client
111 - *
112 - * @return Void
113 - */
114 -function sendwp_connect_client() {
115 - update_option( 'sendwp_client_connected', '1' );
116 -}
117 -
118 -/**
119 - * Disconnect the client
120 - *
121 - * @return Void
122 - */
123 -function sendwp_disconnect_client() {
124 - update_option( 'sendwp_client_connected', '0' );
125 -}
126 -
127 -/**
128 - * Check if client is connected
129 - *
130 - * @return Bool
131 - */
132 -function sendwp_client_connected() {
133 - $connected = get_option( 'sendwp_client_connected', '0' );
134 -
135 - if ( '1' === $connected || 1 === $connected ) {
136 - $connected = true;
137 - } else {
138 - $connected = false;
139 - }
140 - return $connected;
141 -}
142 -
143 -/**
144 - * Update the client connection
145 - *
146 - * @return Bool|Void
147 - */
148 -function sendwp_update_client_connection() {
149 - if ( ! isset( $_POST['sendwp_server_request'] ) ) {
150 - return false;
151 - }
152 -
153 - if ( ! isset( $_POST['sendwp_hash'] ) || ! sendwp_validate_hash( $_POST['sendwp_hash'] ) ) {
154 - return false;
155 - }
156 -
157 - if ( 'disconnect' === $_POST['sendwp_server_request'] ) {
158 - sendwp_disconnect_client();
159 - } elseif ( 'connect' === $_POST['sendwp_server_request'] ) {
160 - sendwp_connect_client();
161 - }
162 -}
163 -
164 -/**
165 - * Get client status
166 - *
167 - * @return Bool|String
168 - */
169 -function sendwp_get_client_status() {
170 - if ( ! isset( $_POST['sendwp_request_status'] ) ) {
171 - return false;
172 - }
173 -
174 - if ( ! isset( $_POST['sendwp_hash'] ) || ! sendwp_validate_hash( $_POST['sendwp_hash'] ) ) {
175 - return false;
176 - }
177 -
178 - $response = array(
179 - 'connected' => sendwp_client_connected(),
180 - 'enabled' => sendwp_forwarding_enabled(),
181 - );
182 -
183 - header( 'Content-Type: application/json' );
184 -
185 - echo json_encode( $response );
186 -
187 - die();
188 -}
189 -
190 -/**
191 - * Validate the hash
192 - *
193 - * @return Bool
194 - */
195 -function sendwp_validate_hash( $hash ) {
196 - return sendwp_generate_hash() === $hash;
197 -}
198 -
199 -/**
200 - * Get the hash
201 - *
202 - * @return String
203 - */
204 -function sendwp_get_hash() {
205 - return sendwp_generate_hash();
206 -}
207 -
208 -/**
209 - * Generate the hash
210 - *
211 - * @return String
212 - */
213 -function sendwp_generate_hash() {
214 - $data = sendwp_get_client_name() . sendwp_get_client_secret();
215 -
216 - return hash( 'sha256', $data );
217 -}
218 -
219 -/**
220 - * Enable mail forwarding
221 - *
222 - * @return Void
223 - */
224 -function sendwp_enable_forwarding() {
225 - update_option( 'sendwp_forwarding_enabled', '1' );
226 -}
227 -
228 -/**
229 - * Disable mail forwarding
230 - *
231 - * @return Void
232 - */
233 -function sendwp_disable_forwarding() {
234 - update_option( 'sendwp_forwarding_enabled', '0' );
235 -}
236 -
237 -/**
238 - * Enqueues fowarding disabled notice
239 - *
240 - * @return Void
241 - */
242 -function sendwp_forwarding_disabled_notice() {
243 - if ( isset( $_GET['page'] ) && 'sendwp' === $_GET['page'] ) {
244 - return;
245 - }
246 -
247 - if ( ! sendwp_forwarding_enabled() ) {
248 - wp_enqueue_style( 'sendwp-notices', plugins_url( 'assets/css/admin/notices.css', __DIR__ ) );
249 - include 'admin/views/disabled-notice.html.php';
250 - }
251 -}
252 -
253 -/**
254 - * Check if forwarding is enabled
255 - *
256 - * @return Bool
257 - */
258 -function sendwp_forwarding_enabled() {
259 - $enabled = get_option( 'sendwp_forwarding_enabled', '1' );
260 -
261 - if ( ( '1' === $enabled || 1 === $enabled ) && sendwp_client_connected() ) {
262 - $enabled = true;
263 - } else {
264 - $enabled = false;
265 - }
266 -
267 - return $enabled;
268 -}
269 -
270 -/**
271 - * Possibly disable mail forwarding
272 - *
273 - * @return Bool|Void
274 - */
275 -function sendwp_maybe_disable_forwarding() {
276 - if ( ! isset( $_POST['security'] ) ) {
277 - return false;
278 - }
279 -
280 - if ( ! wp_verify_nonce( $_POST['security'], 'sendwp_settings_nonce' ) ) {
281 - return false;
282 - }
283 -
284 - if ( isset( $_POST['sendwp_forwarding'] ) ) {
285 - if ( 'enable' === $_POST['sendwp_forwarding'] ) {
286 - sendwp_enable_forwarding();
287 - } elseif ( 'disable' === $_POST['sendwp_forwarding'] ) {
288 - sendwp_disable_forwarding();
289 - }
290 - }
291 -}
292 -
293 -/**
294 - * Connect pulse monitor
295 - *
296 - * @return Void
297 - */
298 -function sendwp_connect_pulse_monitor() {
299 - if ( ! wp_next_scheduled( 'sendwp_heartbeat' ) ) {
300 - wp_schedule_event( time(), 'daily', 'sendwp_heartbeat' );
301 - }
302 -}
303 -
304 -/**
305 - * Check the pulse monitor
306 - *
307 - * @return Bool|Void
308 - */
309 -function sendwp_pulse_monitor() {
310 - if ( get_transient( 'sendwp_pulse_monitor' ) ) {
311 - return false;
312 - }
313 -
314 - $status = 'unknown';
315 - $request = \SendWP\API\Request::create( 'clients/status' );
316 - $response = $request->post( array() );
317 -
318 - if ( is_wp_error( $response ) ) {
319 - set_transient( 'sendwp_pulse_monitor', $response->get_error_message(), 10 * MINUTE_IN_SECONDS );
320 -
321 - return;
322 - }
323 -
324 - $response_body = wp_remote_retrieve_body( $response );
325 -
326 - set_transient( 'sendwp_pulse_monitor', $response_body, 10 * MINUTE_IN_SECONDS );
327 -
328 - $status = json_decode( $response_body );
329 -
330 - if ( 'not-connected' === $status->message ) {
331 - sendwp_disconnect_client();
332 - } elseif ( 'connected' === $status->message ) {
333 - sendwp_connect_client();
334 - }
335 -}
336 -
337 -/**
338 - * Get last pulse result
339 - *
340 - * @return String
341 - */
342 -function sendwp_last_pulse() {
343 - $timestamp = time();
344 - $saved = get_option( '_transient_timeout_sendwp_pulse_monitor', $timestamp );
345 -
346 - if ( $saved > $timestamp ) {
347 - $timestamp = $saved - 600;
348 - }
349 -
350 - $iso_date = gmdate( 'Y-m-d H:i:s', $timestamp );
351 - $local_timestamp = get_date_from_gmt( $iso_date, 'F j, Y, g:i a' );
352 -
353 - return $local_timestamp;
354 -}
355 -
356 -/**
357 - * Return last pulse result
358 - *
359 - * @return String
360 - */
361 -function sendwp_last_pulse_result() {
362 - return get_option( '_transient_sendwp_pulse_monitor', 'unknown' );
363 -}
364 -
365 -/**
366 - * Reset all plugin data
367 - *
368 - * @return Void
369 - */
370 -function sendwp_reset_all_plugin_data() {
371 - if ( check_admin_referer( 'reset_sendwp_data' . sendwp_get_client_secret() ) ) {
372 - $options = array(
373 - 'sendwp_client_secret',
374 - );
375 -
376 - foreach ( $options as $option ) {
377 - if ( get_option( $option, false ) !== false ) {
378 - delete_option( $option );
379 - }
380 - }
381 -
382 - sendwp_set_client_secret( sendwp_generate_secret() );
383 - }
384 -
385 - wp_redirect( sendwp_get_client_redirect() );
386 -
387 - exit;
388 -}
389 -add_action( 'admin_post_sendwp_reset_plugin_data', 'sendwp_reset_all_plugin_data' );
1 +<?php
2 +
3 +function sendwp_get_server_url()
4 +{
5 + $server_url = ( defined('SENDWP_SERVER_URL') ) ? SENDWP_SERVER_URL : 'https://sendwp.com';
6 + return trailingslashit( $server_url );
7 +}
8 +
9 +function sendwp_get_request_auth_header()
10 +{
11 + return 'Basic ' . base64_encode( sendwp_get_client_name() . ':' . sendwp_get_client_secret() );
12 +}
13 +
14 +function sendwp_get_client_name()
15 +{
16 + $site_url = get_site_url();
17 + return parse_url( $site_url, PHP_URL_HOST );
18 +}
19 +function sendwp_get_client_url()
20 +{
21 + return get_site_url();
22 +}
23 +
24 +function sendwp_get_client_redirect()
25 +{
26 + return get_admin_url();
27 +}
28 +
29 +function sendwp_get_client_secret()
30 +{
31 + return get_option( 'sendwp_client_secret' );
32 +}
33 +
34 +function sendwp_set_client_secret( $secret )
35 +{
36 + return update_option( 'sendwp_client_secret', $secret );
37 +}
38 +
39 +function sendwp_generate_secret( $length = 40 )
40 +{
41 + if( 0 >= $length ) $length = 40; // Min key length.
42 + if( 255 <= $length ) $length = 255; // Max key length.
43 +
44 + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
45 + $random_string = '';
46 + for ( $i = 0; $i < $length; $i ++ ) {
47 + $random_string .= $characters[ rand( 0, strlen( $characters ) - 1 ) ];
48 + }
49 +
50 + return $random_string;
51 +}
52 +
53 +function sendwp_connect_client()
54 +{
55 + update_option('sendwp_client_connected', '1');
56 +}
57 +
58 +function sendwp_disconnect_client()
59 +{
60 + update_option('sendwp_client_connected', '0');
61 +}
62 +
63 +function sendwp_client_connected()
64 +{
65 + $connected = get_option('sendwp_client_connected', '0');
66 + if ('1' == $connected) {
67 + $connected = true;
68 + } else {
69 + $connected = false;
70 + }
71 + return $connected;
72 +}
73 +
74 +function sendwp_update_client_connection()
75 +{
76 + if (! isset($_POST['sendwp_server_request'])) return false;
77 + if (! isset($_POST['sendwp_hash']) || ! sendwp_validate_hash($_POST['sendwp_hash'])) return false;
78 + if ('disconnect' == $_POST['sendwp_server_request']) {
79 + sendwp_disconnect_client();
80 + } elseif ('connect' == $_POST['sendwp_server_request']) {
81 + sendwp_connect_client();
82 + }
83 +}
84 +
85 +function sendwp_get_client_status()
86 +{
87 + if (! isset($_POST['sendwp_request_status'])) return false;
88 + if (! isset($_POST['sendwp_hash']) || ! sendwp_validate_hash($_POST['sendwp_hash'])) return false;
89 + $response = [
90 + 'connected' => sendwp_client_connected(),
91 + 'enabled' => sendwp_forwarding_enabled()
92 + ];
93 + header('Content-Type: application/json');
94 + echo json_encode($response);
95 + die();
96 +}
97 +
98 +function sendwp_validate_hash( $hash )
99 +{
100 + return sendwp_generate_hash() === $hash;
101 +}
102 +
103 +function sendwp_generate_hash()
104 +{
105 + $data = sendwp_get_client_name() . sendwp_get_client_secret();
106 + return hash('sha256', $data);
107 +}
108 +
109 +function sendwp_enable_forwarding()
110 +{
111 + update_option('sendwp_forwarding_enabled', '1');
112 +}
113 +
114 +function sendwp_disable_forwarding()
115 +{
116 + update_option('sendwp_forwarding_enabled', '0');
117 +}
118 +
119 +function sendwp_forwarding_disabled_notice()
120 +{
121 + if( isset( $_GET['page'] ) && 'sendwp' == $_GET['page'] ) return;
122 +
123 + if (!sendwp_forwarding_enabled() ) {
124 + wp_enqueue_style('sendwp-notices', plugins_url( 'assets/css/admin/notices.css', __DIR__));
125 + include 'admin/views/disabled-notice.html.php';
126 + }
127 +}
128 +
129 +function sendwp_forwarding_enabled()
130 +{
131 + $enabled = get_option('sendwp_forwarding_enabled', '1');
132 + if ('1' == $enabled && sendwp_client_connected()) {
133 + $enabled = true;
134 + } else {
135 + $enabled = false;
136 + }
137 + return $enabled;
138 +}
139 +
140 +function sendwp_maybe_disable_forwarding()
141 +{
142 + if (! isset($_POST['security'])) return false;
143 + if (! wp_verify_nonce($_POST['security'], 'sendwp_settings_nonce')) return false;
144 + if (isset($_POST['sendwp_forwarding'])) {
145 + if ('enable' == $_POST['sendwp_forwarding']) {
146 + sendwp_enable_forwarding();
147 + } elseif('disable' == $_POST['sendwp_forwarding']) {
148 + sendwp_disable_forwarding();
149 + }
150 + }
151 +}
152 +
153 +function sendwp_connect_pulse_monitor()
154 +{
155 + if (! wp_next_scheduled('sendwp_heartbeat')) {
156 + wp_schedule_event(time(), 'daily', 'sendwp_heartbeat');
157 + }
158 +}
159 +
160 +function sendwp_pulse_monitor()
161 +{
162 + if (get_transient('sendwp_pulse_monitor')) return false;
163 + $status = 'unknown';
164 + $request = \SendWP\API\Request::create('clients/status');
165 + $response = $request->post( [] );
166 +
167 + if(is_wp_error($response)){
168 + set_transient('sendwp_pulse_monitor', $response->get_error_message(), 10 * MINUTE_IN_SECONDS);
169 + return;
170 + }
171 +
172 + $response_body = wp_remote_retrieve_body($response);
173 + set_transient('sendwp_pulse_monitor', $response_body, 10 * MINUTE_IN_SECONDS);
174 +
175 + $status = json_decode($response_body);
176 +
177 + if ('not-connected' == $status->message) {
178 + sendwp_disconnect_client();
179 + } elseif ('connected' == $status->message) {
180 + sendwp_connect_client();
181 + }
182 +}
183 +
184 +function sendwp_last_pulse()
185 +{
186 + $timestamp = time();
187 + $saved = get_option('_transient_timeout_sendwp_pulse_monitor', $timestamp);
188 + if($saved > $timestamp) {
189 + $timestamp = $saved - 600;
190 + }
191 + $iso_date = date( 'Y-m-d H:i:s', $timestamp );
192 + $local_timestamp = get_date_from_gmt( $iso_date, 'F j, Y, g:i a' );
193 + return $local_timestamp;
194 +}
195 +
196 +function sendwp_last_pulse_result()
197 +{
198 + return get_option('_transient_sendwp_pulse_monitor', 'unknown');
199 +}