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 -241 1.30.0.2 View file →
@@ -1,241 +1,199 @@
1 -<?php
2 -
3 -function sendwp_get_server_url()
4 -{
5 - $server_url = ( defined('SENDWP_SERVER_URL') ) ? SENDWP_SERVER_URL : 'https://app.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 add_query_arg('page', 'sendwp', get_admin_url(null, 'tools.php') );
27 -}
28 -
29 -function sendwp_get_client_secret()
30 -{
31 - $secret = get_option( 'sendwp_client_secret' );
32 - if(!$secret) {
33 - $secret = sendwp_generate_secret();
34 - sendwp_set_client_secret($secret);
35 - }
36 - return $secret;
37 -}
38 -
39 -function sendwp_set_client_secret( $secret )
40 -{
41 - return update_option( 'sendwp_client_secret', $secret );
42 -}
43 -
44 -function sendwp_generate_secret( $length = 40 )
45 -{
46 - if( 0 >= $length ) $length = 40; // Min key length.
47 - if( 255 <= $length ) $length = 255; // Max key length.
48 -
49 - $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
50 - $random_string = '';
51 - for ( $i = 0; $i < $length; $i ++ ) {
52 - $random_string .= $characters[ rand( 0, strlen( $characters ) - 1 ) ];
53 - }
54 -
55 - return $random_string;
56 -}
57 -
58 -function sendwp_connect_client()
59 -{
60 - update_option('sendwp_client_connected', '1');
61 -}
62 -
63 -function sendwp_disconnect_client()
64 -{
65 - update_option('sendwp_client_connected', '0');
66 -}
67 -
68 -function sendwp_client_connected()
69 -{
70 - $connected = get_option('sendwp_client_connected', '0');
71 - if ('1' == $connected) {
72 - $connected = true;
73 - } else {
74 - $connected = false;
75 - }
76 - return $connected;
77 -}
78 -
79 -function sendwp_update_client_connection()
80 -{
81 - if (! isset($_POST['sendwp_server_request'])) return false;
82 - if (! isset($_POST['sendwp_hash']) || ! sendwp_validate_hash($_POST['sendwp_hash'])) return false;
83 - if ('disconnect' == $_POST['sendwp_server_request']) {
84 - sendwp_disconnect_client();
85 - } elseif ('connect' == $_POST['sendwp_server_request']) {
86 - sendwp_connect_client();
87 - }
88 -}
89 -
90 -function sendwp_get_client_status()
91 -{
92 - if (! isset($_POST['sendwp_request_status'])) return false;
93 - if (! isset($_POST['sendwp_hash']) || ! sendwp_validate_hash($_POST['sendwp_hash'])) return false;
94 - $response = [
95 - 'connected' => sendwp_client_connected(),
96 - 'enabled' => sendwp_forwarding_enabled()
97 - ];
98 - header('Content-Type: application/json');
99 - echo json_encode($response);
100 - die();
101 -}
102 -
103 -function sendwp_validate_hash( $hash )
104 -{
105 - return sendwp_generate_hash() === $hash;
106 -}
107 -
108 -
109 -function sendwp_get_hash()
110 -{
111 - return sendwp_generate_hash();
112 -}
113 -
114 -function sendwp_generate_hash()
115 -{
116 - $data = sendwp_get_client_name() . sendwp_get_client_secret();
117 - return hash('sha256', $data);
118 -}
119 -
120 -function sendwp_enable_forwarding()
121 -{
122 - update_option('sendwp_forwarding_enabled', '1');
123 -}
124 -
125 -function sendwp_disable_forwarding()
126 -{
127 - update_option('sendwp_forwarding_enabled', '0');
128 -}
129 -
130 -function sendwp_forwarding_disabled_notice()
131 -{
132 - if( isset( $_GET['page'] ) && 'sendwp' == $_GET['page'] ) return;
133 -
134 - if (!sendwp_forwarding_enabled() ) {
135 - wp_enqueue_style('sendwp-notices', plugins_url( 'assets/css/admin/notices.css', __DIR__));
136 - include 'admin/views/disabled-notice.html.php';
137 - }
138 -}
139 -
140 -function sendwp_forwarding_enabled()
141 -{
142 - $enabled = get_option('sendwp_forwarding_enabled', '1');
143 - if ('1' == $enabled && sendwp_client_connected()) {
144 - $enabled = true;
145 - } else {
146 - $enabled = false;
147 - }
148 - return $enabled;
149 -}
150 -
151 -function sendwp_maybe_disable_forwarding()
152 -{
153 - if (! isset($_POST['security'])) return false;
154 - if (! wp_verify_nonce($_POST['security'], 'sendwp_settings_nonce')) return false;
155 - if (isset($_POST['sendwp_forwarding'])) {
156 - if ('enable' == $_POST['sendwp_forwarding']) {
157 - sendwp_enable_forwarding();
158 - } elseif('disable' == $_POST['sendwp_forwarding']) {
159 - sendwp_disable_forwarding();
160 - }
161 - }
162 -}
163 -
164 -function sendwp_connect_pulse_monitor()
165 -{
166 - if (! wp_next_scheduled('sendwp_heartbeat')) {
167 - wp_schedule_event(time(), 'daily', 'sendwp_heartbeat');
168 - }
169 -}
170 -
171 -function sendwp_pulse_monitor()
172 -{
173 - if (get_transient('sendwp_pulse_monitor')) return false;
174 - $status = 'unknown';
175 - $request = \SendWP\API\Request::create('clients/status');
176 - $response = $request->post( [] );
177 -
178 - if(is_wp_error($response)){
179 - set_transient('sendwp_pulse_monitor', $response->get_error_message(), 10 * MINUTE_IN_SECONDS);
180 - return;
181 - }
182 -
183 - $response_body = wp_remote_retrieve_body($response);
184 - set_transient('sendwp_pulse_monitor', $response_body, 10 * MINUTE_IN_SECONDS);
185 -
186 - $status = json_decode($response_body);
187 -
188 - if ('not-connected' == $status->message) {
189 - sendwp_disconnect_client();
190 - } elseif ('connected' == $status->message) {
191 - sendwp_connect_client();
192 - }
193 -}
194 -
195 -function sendwp_last_pulse()
196 -{
197 - $timestamp = time();
198 - $saved = get_option('_transient_timeout_sendwp_pulse_monitor', $timestamp);
199 - if($saved > $timestamp) {
200 - $timestamp = $saved - 600;
201 - }
202 - $iso_date = date( 'Y-m-d H:i:s', $timestamp );
203 - $local_timestamp = get_date_from_gmt( $iso_date, 'F j, Y, g:i a' );
204 - return $local_timestamp;
205 -}
206 -
207 -function sendwp_last_pulse_result()
208 -{
209 - return get_option('_transient_sendwp_pulse_monitor', 'unknown');
210 -}
211 -
212 -function sendwp_get_client_partner_id()
213 -{
214 - if ( defined( 'SENDWP_PARTNER_ID' ) ) {
215 - return SENDWP_PARTNER_ID;
216 - }
217 -
218 - return null;
219 -}
220 -
221 -function sendwp_reset_all_plugin_data()
222 -{
223 - if (check_admin_referer('reset_sendwp_data' . sendwp_get_client_secret())) {
224 - $options = [
225 - 'sendwp_client_secret',
226 - ];
227 -
228 - foreach ($options as $option) {
229 - if (get_option($option, false) !== false) {
230 - delete_option($option);
231 - }
232 - }
233 -
234 - sendwp_set_client_secret(sendwp_generate_secret());
235 - }
236 -
237 - wp_redirect(sendwp_get_client_redirect());
238 -
239 - exit;
240 -}
241 -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 +}