Migrations
3 years ago
ajax
3 years ago
models
3 years ago
queries
3 years ago
sql
3 years ago
tables
3 years ago
utils
3 years ago
campaign_builder.php
3 years ago
capability_manager.php
3 years ago
chart.php
3 years ago
chart_geo.php
3 years ago
chart_svg.php
3 years ago
current_resource.php
3 years ago
dashboard_options.php
3 years ago
dashboard_widget.php
3 years ago
email_reports.php
3 years ago
filters.php
3 years ago
freemius.php
3 years ago
geo_database.php
3 years ago
geo_database_download_job.php
3 years ago
health_check.php
3 years ago
independent_analytics.php
3 years ago
known_referrers.php
3 years ago
pdf.php
3 years ago
query.php
3 years ago
quick_stats.php
3 years ago
real_time.php
3 years ago
rest_api.php
3 years ago
settings.php
3 years ago
track_resource_changes.php
3 years ago
view_counter.php
3 years ago
health_check.php
122 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | class Health_Check |
| 6 | { |
| 7 | const REST_LAST_HEALTHY_AT = 'iawp_rest_last_healthy_at'; |
| 8 | private $error = null; |
| 9 | |
| 10 | public function __construct() |
| 11 | { |
| 12 | $this->error = $this->plugin_health_check(); |
| 13 | |
| 14 | // Only run the rest check if the plugin checks have passed |
| 15 | if (!isset($this->error) && $this->should_run_rest_health_check()) { |
| 16 | $rest_error = $this->rest_health_check(); |
| 17 | |
| 18 | if (isset($rest_error)) { |
| 19 | $this->error = $rest_error; |
| 20 | $this->update_wp_option(false); |
| 21 | } else { |
| 22 | $this->update_wp_option(true); |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | public function healthy(): bool |
| 28 | { |
| 29 | return empty($this->error); |
| 30 | } |
| 31 | |
| 32 | public function error(): ?string |
| 33 | { |
| 34 | return $this->error; |
| 35 | } |
| 36 | |
| 37 | private function update_wp_option($healthy) |
| 38 | { |
| 39 | if ($healthy) { |
| 40 | $now = new \DateTime('now'); |
| 41 | delete_option(self::REST_LAST_HEALTHY_AT); |
| 42 | add_option(self::REST_LAST_HEALTHY_AT, $now->format('c')); |
| 43 | } else { |
| 44 | delete_option(self::REST_LAST_HEALTHY_AT); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @return string|null Returns a string error message if the health check fails |
| 50 | */ |
| 51 | private function plugin_health_check(): ?string |
| 52 | { |
| 53 | $active_plugins = get_option('active_plugins'); |
| 54 | |
| 55 | if (in_array('disable-wp-rest-api/disable-wp-rest-api.php', $active_plugins)) { |
| 56 | return __('The "Disable WP REST API" plugin needs to be deactivated because Independent Analytics uses the REST API to record visits.', 'iawp'); |
| 57 | } |
| 58 | if (in_array('all-in-one-wp-security-and-firewall/wp-security.php', $active_plugins)) { |
| 59 | $settings = get_option('aio_wp_security_configs', []); |
| 60 | if (array_key_exists('aiowps_disallow_unauthorized_rest_requests', $settings)) { |
| 61 | if ($settings['aiowps_disallow_unauthorized_rest_requests'] == 1) { |
| 62 | return __('The "All In WP Security" plugin is blocking REST API requests, which Independent Analytics needs to record views. Please disable this setting via the WP Security > Miscellaneous menu.', 'iawp'); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | if (in_array('disable-json-api/disable-json-api.php', $active_plugins)) { |
| 67 | $settings = get_option('disable_rest_api_options', []); |
| 68 | if (array_key_exists('roles', $settings)) { |
| 69 | if ($settings['roles']['none']['default_allow'] == false) { |
| 70 | if ($settings['roles']['none']['allow_list']['/iawp/search'] == false) { |
| 71 | return __('The "Disable REST API" plugin is blocking REST API requests for unauthenticated users, which Independent Analytics needs to record views. Please enable the /iawp/search route, so Independent Analytics can track your visitors.', 'iawp'); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | if (in_array('disable-xml-rpc-api/disable-xml-rpc-api.php', $active_plugins)) { |
| 77 | $settings = get_option('dsxmlrpc-settings'); |
| 78 | if (array_key_exists('json-rest-api', $settings)) { |
| 79 | if ($settings['json-rest-api'] == 1) { |
| 80 | return __('The "Disable XML-RPC-API" plugin is blocking REST API requests, which Independent Analytics needs to record views. Please visit the Security Settings menu and turn off the "Disable JSON REST API" option, so Independent Analytics can track your visitors.', 'iawp'); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | if (in_array('wpo-tweaks/wordpress-wpo-tweaks.php', $active_plugins)) { |
| 85 | return __('The "WPO Tweaks & Optimizations" plugin needs to be deactivated because it is disabling the REST API, which Independent Analytics uses to record visits.', 'iawp'); |
| 86 | } |
| 87 | if (in_array('all-in-one-intranet/basic_all_in_one_intranet.php', $active_plugins)) { |
| 88 | return __('The "All-In-One Intranet" plugin needs to be deactivated because it is disabling the REST API, which Independent Analytics uses to record visits. You may want to try the "My Private Site" plugin instead.', 'iawp'); |
| 89 | } |
| 90 | |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | private function rest_health_check(): ?string |
| 95 | { |
| 96 | $url = get_rest_url() . 'iawp/search'; |
| 97 | $request = wp_remote_post($url); |
| 98 | |
| 99 | if (is_wp_error($request) || wp_remote_retrieve_response_code($request) !== 200 || wp_remote_retrieve_header($request, 'X-IAWP') !== 'IAWP') { |
| 100 | return __('It looks like you\'re blocking REST API requests with a plugin or code snippet. The REST API needs to be enabled because this is how Independent Analytics records visits.', 'iawp'); |
| 101 | } |
| 102 | |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | private function should_run_rest_health_check(): bool |
| 107 | { |
| 108 | try { |
| 109 | $last_healthy_at = get_option(self::REST_LAST_HEALTHY_AT, date('c', 0)); |
| 110 | $last_healthy_at = new \DateTime($last_healthy_at); |
| 111 | } catch (\Exception $e) { |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | $now = new \DateTime(); |
| 116 | $diff = $last_healthy_at->diff($now); |
| 117 | $hours_ago = $diff->h + ($diff->days * 24); |
| 118 | |
| 119 | return $hours_ago >= 1; |
| 120 | } |
| 121 | } |
| 122 |