PluginProbe
Gift Up Gift Cards for WordPress and WooCommerce / trunk
Gift Up Gift Cards for WordPress and WooCommerce vtrunk
3.2.2 3.1.7 3.1.8 3.2 3.2.1 wp5.2 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.2 1.2.1 1.2.2 1.2.3 1.2.4 All 131 releases
gift-up / includes / class-giftup-diagnostics.php

class-giftup-diagnostics.php in Gift Up Gift Cards for WordPress and WooCommerce trunk, at includes/class-giftup-diagnostics.php

355 lines 14.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class GiftUp_Diagnostics {
4 // Diagnostics output
5 private static $diagnostics = "";
6
7 public function new_group() {
8 self::$diagnostics .= "<HR size=1 color=red style='margin: 2rem 0; padding: 0'>";
9 }
10
11 public function append( $debug_msg ) {
12 self::$diagnostics .= "<div>" . $debug_msg . "</div>";
13 }
14
15 public static function render(){
16 $debug = GiftUp()->options->get_woocommerce_diagnostics_mode();
17
18 global $wp_version;
19
20 if ( !wp_doing_ajax() && $debug ) {
21 echo "<div style='border: 5px solid red; color: red; padding: 2rem; background-color: rgba(255, 0, 0, 0.2);'>";
22 echo " <h6 style='margin: 0 0 1rem 0; padding: 0;'>Gift Up diagnostics</h6>";
23 echo "Gift Up plugin version: " . GIFTUP_VERSION . "<BR>";
24 echo "WordPress version: " . $wp_version . "<BR>";
25 echo "PHP version: " . phpversion() . "<BR>";
26
27 $wc_version = GiftUp()->diagnostics->woocommerce_installed_version();
28 if ($wc_version !== null) {
29 echo "WooCommerce version: " . $wc_version . "<BR>";
30
31 if (GiftUp()->options->get_woocommerce_enabled() == true) {
32 echo "WooCommerce integration enabled<br>";
33 echo "Apply to shipping: " . (GiftUp()->options->get_woocommerce_apply_to_shipping() ? "true" : "false") . "<br>";
34 echo "Apply to taxes: " . (GiftUp()->options->get_woocommerce_apply_to_taxes() ? "true" : "false") . "<br>";
35 echo "Gift card applied: " . (WC()->session->get( GIFTUP_ACCEPTED_GIFTCARD_CODE )) . "<br>";
36 }
37 }
38
39 echo self::$diagnostics;
40 echo "</div>";
41 }
42 }
43
44 public function woocommerce_installed_version() {
45 $version = null;
46
47 try {
48 if ( defined( 'WC_VERSION' ) ) {
49 $version = WC_VERSION;
50 } else if ( defined( 'WOOCOMMERCE_VERSION' ) ) {
51 $version = WOOCOMMERCE_VERSION;
52 } else {
53 $version = GiftUp()->diagnostics->get_woo_version_number();
54 }
55
56 return $version;
57 } catch (exception $e) {
58 return null;
59 }
60
61 return null;
62 }
63
64 public function is_woocommerce_activated() {
65 try {
66 if ( file_exists ( WP_PLUGIN_DIR . '/woocommerce/woocommerce.php' ) ) {
67 return is_plugin_active( 'woocommerce/woocommerce.php' );
68 }
69 } catch (exception $e) {
70 return class_exists( 'woocommerce' );
71 }
72
73 return true; // assumed active
74 }
75
76 private function get_woo_version_number() {
77 try {
78 if ( file_exists ( WP_PLUGIN_DIR . '/woocommerce/woocommerce.php' ) ) {
79 $plugin_data = get_file_data( WP_PLUGIN_DIR . '/woocommerce/woocommerce.php', array( 'Version' => 'Version' ) );
80
81 if ( is_array($plugin_data) && isset($plugin_data['Version']) ) {
82 return $plugin_data['Version'];
83 }
84
85 if ( ! function_exists( 'get_plugins' ) ) {
86 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
87 }
88
89 $plugin_folder = get_plugins( '/' . 'woocommerce' );
90 $plugin_file = 'woocommerce.php';
91
92 if ( isset( $plugin_folder[$plugin_file]['Version'] ) ) {
93 return $plugin_folder[$plugin_file]['Version'];
94 } else {
95 return null;
96 }
97 }
98 } catch (exception $e) {
99 return null;
100 }
101
102 return null;
103 }
104
105 public function get_plugins_list() {
106 try {
107 $format = "<div>{{Title}} by {{Author}} (version: {{Version}})</div>";
108 $cache = 5;
109 $by_author = 'false';
110
111 $plugins = $this->get_plugin_list_data( $cache );
112
113 // Extract each plugin and format the output.
114 $output = '';
115 foreach ( $plugins as $plugin_file => $plugin_data ) {
116 if ( is_plugin_active( $plugin_file ) ) {
117 $output .= $this->format_plugin_list( $plugin_data, $format );
118 }
119 }
120
121 return $output;
122 } catch (exception $e) {
123 return $e->getMessage();
124 }
125 }
126
127 private function get_plugin_list_data( $cache ) {
128 // Attempt to get plugin list from cache.
129 if ( ! $cache ) {
130 $cache = 'no';
131 }
132
133 $plugins = false;
134 $cache_key = 'plugins_list';
135
136 if ( is_numeric( $cache ) ) {
137 $plugins = get_transient( $cache_key );
138 }
139
140 // If not using cache, generate a new list and cache that.
141
142 if ( ! $plugins ) {
143 $plugins = get_plugins();
144
145 if ( ( '' !== $plugins ) && ( is_numeric( $cache ) ) ) {
146 set_transient( $cache_key, $plugins, MINUTE_IN_SECONDS * $cache );
147 }
148 }
149
150 return $plugins;
151 }
152
153 private function format_plugin_list( $plugin_data, $format ) {
154
155 // Allowed tag.
156 $plugins_allowedtags = array(
157 'a' => array(
158 'href' => array(),
159 'title' => array(),
160 ),
161 'abbr' => array( 'title' => array() ),
162 'acronym' => array( 'title' => array() ),
163 'code' => array(),
164 'em' => array(),
165 'strong' => array(),
166 );
167
168 // Sanitize all displayed data.
169 $plugin_data['Title'] = wp_kses( $plugin_data['Title'], $plugins_allowedtags );
170 $plugin_data['PluginURI'] = wp_kses( $plugin_data['PluginURI'], $plugins_allowedtags );
171 $plugin_data['AuthorURI'] = wp_kses( $plugin_data['AuthorURI'], $plugins_allowedtags );
172 $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $plugins_allowedtags );
173 $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $plugins_allowedtags );
174
175 // Replace the tags.
176 $format = $this->replace_plugin_list_tags( $plugin_data, $format );
177
178 return $format;
179 }
180
181 private function replace_plugin_list_tags( $plugin_data, $format ) {
182 $format = strtr(
183 $format,
184 array(
185 '{{Title}}' => $plugin_data['Title'],
186 '{{PluginURI}}' => $plugin_data['PluginURI'],
187 '{{AuthorURI}}' => $plugin_data['AuthorURI'],
188 '{{Version}}' => $plugin_data['Version'],
189 '{{Description}}' => $plugin_data['Description'],
190 '{{Author}}' => $plugin_data['Author'],
191 '{{LinkedTitle}}' => "<a href='" . $plugin_data['PluginURI'] . "'>" . $plugin_data['Title'] . '</a>',
192 '#Title#' => $plugin_data['Title'],
193 '#PluginURI#' => $plugin_data['PluginURI'],
194 '#AuthorURI#' => $plugin_data['AuthorURI'],
195 '#Version#' => $plugin_data['Version'],
196 '#Description#' => $plugin_data['Description'],
197 '#Author#' => $plugin_data['Author'],
198 '#LinkedTitle#' => "<a href='" . $plugin_data['PluginURI'] . "'>'" . $plugin_data['Title'] . '</a>',
199 '{{' => '<',
200 '}}' => '>',
201 '{' => '<',
202 '}' => '>',
203 )
204 );
205
206 return $format;
207 }
208
209 public function test_curl( $api_key ) {
210 // Let's test the authenticated Gift Up! Ping endpoint
211 $api_response = GiftUp()->api->invoke( '/ping', 'GET', null, $api_key );
212
213 if ( $api_response->success ) {
214 return array(
215 'message' => 'Something odd is going on. Your API key works, but is not returning the correct data when we query Gift Up\'s API to get your account details.',
216 'status' => 'error'
217 );
218 }
219
220 $args = array(
221 'timeout' => 15,
222 'headers' => array(
223 'accept' => '*/*',
224 'user-agent' => 'WordPress/GiftUp-WordPress-Plugin'
225 )
226 );
227
228 // Now let's do a TLS check via https://www.howsmyssl.com/a/check
229 $tls_1_2_enabled = false;
230 $tls_1_2_response = wp_remote_get( "https://www.howsmyssl.com/a/check", $args );
231
232 $tls_1_2_response_body = wp_remote_retrieve_body( $tls_1_2_response );
233 $tls_1_2_response_code = wp_remote_retrieve_response_code( $tls_1_2_response );
234 $tls_1_2_response_success = $tls_1_2_response_code >= 200 && $tls_1_2_response_code < 300;
235
236 if ( $tls_1_2_response_success ) {
237 $tls_result = json_decode( $tls_1_2_response_body, true );
238
239 if ( $tls_result['tls_version'] == 'TLS 1.12' || $tls_result['tls_version'] == 'TLS 1.13' ) {
240 $tls_1_2_enabled = true;
241 } else {
242 preg_match_all('![\d,\.]+!', $tls_result['tls_version'], $matches);
243 if ( is_array( $matches ) ) {
244 $tls_1_2_enabled = floatval( $matches[0][0] ) >= 1.2;
245 }
246 }
247 }
248
249 // Let's check access to https://www.example.com
250 $example_response = wp_remote_get( "https://www.example.com", $args );
251 $example_response_code = wp_remote_retrieve_response_code( $example_response );
252 $example_response_success = $example_response_code >= 200 && $example_response_code < 300;
253
254 // Let's also test the anonymous Gift Up! Ping endpoint
255 $ping_response = GiftUp()->api->invoke( '/ping', 'HEAD' );
256
257 if ( $ping_response->success ) {
258 $message = 'The API key you have entered does not work, please enter a valid Gift Up! API key.';
259 $message = $message . '<br>--';
260 $message = $message . '<br>Accessing: https://api.giftup.app/ping';
261 $message = $message . '<br>With API key: "<span style="word-break: break-all">' . $api_key . '</span>"';
262 $message = $message . '<br>Response code: ' . $api_response->code;
263
264 if ( $api_response->renderable_body ) {
265 $message = $message . '<br>Response body: ' . $api_response->renderable_body;
266 }
267
268 $message = $message . $this->add_diagnostics( $tls_1_2_response_body );
269
270 return array(
271 'message' => $message,
272 'status' => 'error'
273 );
274 }
275 else if ( ! $tls_1_2_enabled ) {
276 $message = 'We cannot access the Gift Up! API to validate your API key because it appears that your server is incapable of making outbound cURL requests using TLS 1.2. ';
277 $message = $message . '<br>Please upgrading your PHP to version 5.5.19 or higher and cURL version 7.34.0 or higher/OpenSSL @ 1.0.1 or higher. ';
278 $message = $message . '<br><br>There is a great plugin for testing your WordPress installation\'s capability here: <a href="https://wordpress.org/plugins/tls-1-2-compatibility-test/">https://wordpress.org/plugins/tls-1-2-compatibility-test/</a>';
279 $message = $message . '<br>--';
280 $message = $message . '<br>Accessing: https://api.giftup.app/';
281 $message = $message . '<br>Response code: ' . $api_response->code;
282
283 if ( $api_response->renderable_body ) {
284 $message = $message . '<br>Response body: ' . $api_response->renderable_body;
285 }
286
287 $message = $message . $this->add_diagnostics( $tls_1_2_response_body );
288
289 return array(
290 'message' => $message,
291 'status' => 'error'
292 );
293 }
294 else if ( $example_response_success ) {
295 $message = 'We cannot access the Gift Up! API at the moment to validate your API key, please try again in a few moments';
296 $message = $message . '<br>--';
297 $message = $message . '<br>Accessing: https://api.giftup.app/';
298 $message = $message . '<br>Response code: ' . $api_response->code;
299
300 if ( $api_response->renderable_body ) {
301 $message = $message . '<br>Response body: ' . $api_response->renderable_body;
302 }
303
304 $message = $message . $this->add_diagnostics( $tls_1_2_response_body );
305
306 return array(
307 'message' => $message,
308 'status' => 'error'
309 );
310 }
311 else {
312 $message = 'Your Wordpress instance is unable to make ourbound cUrl requests to any external web service/API, including the Gift Up! API at https://api.giftup.app. ';
313 $message = $message . '<br>--';
314 $message = $message . '<br>How to resolve:';
315 $message = $message . '<br>Please review any WordPress security plugins and ensure they are configured to allow outbound cUrl requests and also review your webhost\'s security system/firewall settings to ensure that it is configured to allow your WordPress instance to send/receive on port 443.';
316 $message = $message . '<br>--';
317 $message = $message . '<br>Accessing: https://api.giftup.app/ping';
318 $message = $message . '<br>Response code: ' . $api_response->code;
319
320 if ( $api_response->renderable_body ) {
321 $message = $message . '<br>Response body: ' . $api_response->renderable_body;
322 }
323
324 $message = $message . $this->add_diagnostics( $tls_1_2_response_body );
325
326 return array(
327 'message' => $message,
328 'status' => 'error'
329 );
330 }
331 }
332
333 private static function add_diagnostics( $tls_1_2_response_body ) {
334 $message = "<br>DEBUG information<br>--";
335
336 try {
337 if (!function_exists('curl_version')) {
338 $message = $message . '<br>cURL not installed.';
339 } else {
340 $curl_version = curl_version();
341 $message = $message . '<br>cURL version installed: ' . $curl_version['ssl_version'];
342 }
343 if (function_exists('phpversion')) {
344 $message = $message . '<br>PHP version installed: ' . phpversion();
345 }
346 $message = $message . '<br>TLS check response: <span style="word-break: break-all">' . $tls_1_2_response_body . '</span>';
347 }
348 catch (exception $e) {
349 return $message;
350 }
351
352 return $message;
353 }
354 }
355