nitropack
Last commit date
classes
6 months ago
languages
7 months ago
nitropack-sdk
6 months ago
view
6 months ago
advanced-cache.php
1 year ago
batcache-compat.php
1 year ago
constants.php
7 months ago
diagnostics.php
11 months ago
functions.php
6 months ago
helpers.php
1 year ago
main.php
6 months ago
readme.txt
6 months ago
uninstall.php
7 months ago
diagnostics.php
220 lines
| 1 | <?php |
| 2 | |
| 3 | defined( 'ABSPATH' ) or die( 'Someone made a boo boo!' ); |
| 4 | |
| 5 | use \NitroPack\SDK\Api\ResponseStatus; |
| 6 | |
| 7 | if ( ! function_exists( 'get_plugins' ) ) { |
| 8 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 9 | } |
| 10 | |
| 11 | $np_diag_functions = array( |
| 12 | 'general-info-status' => 'npdiag_get_general_info', |
| 13 | 'active-plugins-status' => 'npdiag_get_active_plugins', |
| 14 | 'conflicting-plugins-status' => 'npdiag_get_conflicting_plugins', |
| 15 | 'user-config-status' => 'npdiag_get_user_config', |
| 16 | 'dir-info-status' => 'npdiag_get_dir_info', |
| 17 | 'getexternalcache' => 'npdiag_detect_third_party_cache' |
| 18 | |
| 19 | ); |
| 20 | |
| 21 | function npdiag_helper_trailingslashit( $string ) { |
| 22 | return rtrim( $string, '/\\' ) . '/'; |
| 23 | } |
| 24 | |
| 25 | function npdiag_compare_webhooks( $nitro_sdk ) { |
| 26 | try { |
| 27 | $siteConfig = nitropack_get_site_config(); |
| 28 | if ( ! empty( $siteConfig['siteId'] ) ) { |
| 29 | $WHToken = nitropack_generate_webhook_token( $siteConfig['siteId'] ); |
| 30 | $constructedWH = new \NitroPack\Url\Url( strtolower( get_home_url() ) ) . '?nitroWebhook=config&token=' . $WHToken; |
| 31 | $storedWH = $nitro_sdk->getApi()->getWebhook( "config" ); |
| 32 | $matchResult = ( $constructedWH == $storedWH ) ? __( 'OK', 'nitropack' ) : __( 'Warning: Webhooks do not match this site', 'nitropack' ); |
| 33 | } else { |
| 34 | $debugMsg = empty( $_SERVER["HTTP_HOST"] ) ? "HTTP_HOST is not defined. " : ""; |
| 35 | $debugMsg .= empty( $_SERVER["REQUEST_URI"] ) ? "REQUEST_URI is not defined. " : ""; |
| 36 | $debugMsg .= empty( $debugMsg ) ? 'URL used to match config was: ' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] : ""; |
| 37 | $matchResult = __( 'Site config cannot be found, because ', 'nitropack' ) . $debugMsg; |
| 38 | } |
| 39 | return $matchResult; |
| 40 | } catch (\Exception $e) { |
| 41 | return $e->getMessage(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | function npdiag_poll_api( $nitro_sdk ) { |
| 46 | $pollResult = array( |
| 47 | ResponseStatus::OK => __( 'OK', 'nitropack' ), |
| 48 | ResponseStatus::ACCEPTED => __( 'OK', 'nitropack' ), |
| 49 | ResponseStatus::BAD_REQUEST => __( 'Bad request.', 'nitropack' ), |
| 50 | ResponseStatus::PAYMENT_REQUIRED => __( 'Payment required. Please, contact NP support for details.', 'nitropack' ), |
| 51 | ResponseStatus::FORBIDDEN => __( 'Site disabled. Please, contact NP support for details.', 'nitropack' ), |
| 52 | ResponseStatus::NOT_FOUND => __( 'URL used for the API poll request returned 404. Please ignore this.', 'nitropack' ), |
| 53 | ResponseStatus::CONFLICT => __( 'Conflict. There is another operation, which prevents accepting optimization requests at the moment. Please, contact NP support for details.', 'nitropack' ), |
| 54 | ResponseStatus::RUNTIME_ERROR => __( 'Runtime error.', 'nitropack' ), |
| 55 | ResponseStatus::SERVICE_UNAVAILABLE => __( 'Service unavailable.', 'nitropack' ), |
| 56 | ResponseStatus::UNKNOWN => __( 'Unknown.', 'nitropack' ) |
| 57 | ); |
| 58 | |
| 59 | try { |
| 60 | $referer = isset( $_SERVER["HTTP_REFERER"] ) ? $_SERVER["HTTP_REFERER"] : ''; |
| 61 | $apiResponseCode = $nitro_sdk->getApi()->getCache( get_home_url(), __( 'NitroPack Diagnostic Agent', 'nitropack' ), array(), false, 'default', $referer )->getStatus(); |
| 62 | return $pollResult[ $apiResponseCode ]; |
| 63 | } catch (\Exception $e) { |
| 64 | return 'Error: ' . $e->getMessage(); |
| 65 | } |
| 66 | |
| 67 | } |
| 68 | |
| 69 | function npdiag_backlog_status( $nitro_sdk ) { |
| 70 | return $nitro_sdk->backlog->exists() ? 'Warning' : 'OK'; |
| 71 | } |
| 72 | |
| 73 | function npdiag_get_general_info() { |
| 74 | global $wp_version; |
| 75 | if ( null !== $nitro = get_nitropack_sdk() ) { |
| 76 | $probe_result = "OK"; |
| 77 | try { |
| 78 | $nitro->fetchConfig(); |
| 79 | } catch (\Exception $e) { |
| 80 | $probe_result = __( 'Error: ', 'nitropack' ) . $e->getMessage(); |
| 81 | } |
| 82 | } else { |
| 83 | $probe_result = __( 'Error: Cannot get an SDK instance', 'nitropack' ); |
| 84 | } |
| 85 | |
| 86 | $third_party_residual_cache = npdiag_detect_third_party_cache(); |
| 87 | |
| 88 | $info = array( |
| 89 | 'Nitro_WP_version' => ! empty( $wp_version ) ? $wp_version : get_bloginfo( 'version' ), |
| 90 | 'Nitro_Version' => defined( 'NITROPACK_VERSION' ) ? NITROPACK_VERSION : __( 'Undefined', 'nitropack' ), |
| 91 | 'Nitro_SDK_Connection' => $probe_result, |
| 92 | 'Nitro_API_Polling' => $nitro ? npdiag_poll_api( $nitro ) : __( 'Error: Cannot get an SDK instance', 'nitropack' ), |
| 93 | 'Nitro_SDK_Version' => defined( 'NitroPack\SDK\Nitropack::VERSION' ) ? NitroPack\SDK\Nitropack::VERSION : __( 'Undefined', 'nitropack' ), |
| 94 | 'Nitro_WP_Cache' => defined( 'WP_CACHE' ) ? ( WP_CACHE ? __( 'OK for drop-in', 'nitropack' ) : __( 'Turned off', 'nitropack' ) ) : __( 'Undefined', 'nitropack' ), |
| 95 | 'Advanced_Cache_Version' => defined( 'NITROPACK_ADVANCED_CACHE_VERSION' ) ? NITROPACK_ADVANCED_CACHE_VERSION : __( 'Undefined', 'nitropack' ), |
| 96 | 'Nitro_Absolute_Path' => defined( 'ABSPATH' ) ? ABSPATH : __( 'Undefined', 'nitropack' ), |
| 97 | 'Nitro_Plugin_Directory' => defined( 'NITROPACK_PLUGIN_DIR' ) ? NITROPACK_PLUGIN_DIR : dirname( __FILE__ ), |
| 98 | 'Nitro_Data_Directory' => defined( 'NITROPACK_DATA_DIR' ) ? NITROPACK_DATA_DIR : __( 'Undefined', 'nitropack' ), |
| 99 | 'Nitro_Plugin_Data_Directory' => defined( 'NITROPACK_PLUGIN_DATA_DIR' ) ? NITROPACK_PLUGIN_DATA_DIR : __( 'Undefined', 'nitropack' ), |
| 100 | 'Nitro_Config_File' => defined( 'NITROPACK_CONFIG_FILE' ) ? NITROPACK_CONFIG_FILE : __( 'Undefined', 'nitropack' ), |
| 101 | 'Nitro_Backlog_File_Status' => $nitro ? npdiag_backlog_status( $nitro ) : __( 'Error: Cannot get an SDK instance', 'nitropack' ), |
| 102 | 'Nitro_Webhooks' => $nitro ? npdiag_compare_webhooks( $nitro ) : __( 'Error: Cannot get an SDK instance', 'nitropack' ), |
| 103 | 'Nitro_Connectivity_Requirements' => nitropack_check_func_availability( 'stream_socket_client' ) ? __( 'OK', 'nitropack' ) : __( 'Warning: "stream_socket_client" function is disabled.', 'nitropack' ), |
| 104 | 'Residual_Cache_Found_For' => $third_party_residual_cache, |
| 105 | ); |
| 106 | |
| 107 | if ( defined( "NITROPACK_VERSION" ) && defined( "NITROPACK_ADVANCED_CACHE_VERSION" ) && NITROPACK_VERSION == NITROPACK_ADVANCED_CACHE_VERSION && nitropack_is_dropin_cache_allowed() ) { |
| 108 | $info['Nitro_Cache_Method'] = 'drop-in'; |
| 109 | } elseif ( defined( 'EZOIC_INTEGRATION_VERSION' ) ) { |
| 110 | $info['Nitro_Cache_Method'] = 'plugin-ezoic'; |
| 111 | } else { |
| 112 | $info['Nitro_Cache_Method'] = 'plugin'; |
| 113 | } |
| 114 | |
| 115 | return $info; |
| 116 | } |
| 117 | |
| 118 | function npdiag_get_active_plugins() { |
| 119 | if ( is_admin() ) { |
| 120 | $info = array(); |
| 121 | $raw_installed_list = get_plugins(); |
| 122 | $raw_active_list = get_option( 'active_plugins' ); |
| 123 | foreach ( $raw_installed_list as $pkey => $pval ) { |
| 124 | if ( in_array( $pkey, $raw_active_list ) ) { |
| 125 | $info[ $pval['Name'] ] = $pval['Version']; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return $info; |
| 131 | } |
| 132 | |
| 133 | function npdiag_get_user_config() { |
| 134 | if ( defined( 'NITROPACK_CONFIG_FILE' ) ) { |
| 135 | if ( file_exists( NITROPACK_CONFIG_FILE ) ) { |
| 136 | $info = json_decode( file_get_contents( NITROPACK_CONFIG_FILE ) ); |
| 137 | if ( ! $info ) { |
| 138 | $info = __( 'Config found, but unable to get contents.', 'nitropack' ); |
| 139 | } |
| 140 | } else { |
| 141 | $info = __( 'Config file not found.', 'nitropack' ); |
| 142 | } |
| 143 | } else { |
| 144 | $info = __( 'Config file constant is not defined.', 'nitropack' ); |
| 145 | } |
| 146 | |
| 147 | return $info; |
| 148 | } |
| 149 | |
| 150 | function npdiag_get_dir_info() { |
| 151 | $siteConfig = nitropack_get_site_config(); |
| 152 | $siteID = $siteConfig['siteId']; |
| 153 | // DoI = Directories of Interest |
| 154 | $DoI = array( |
| 155 | 'WP_Content_Dir_Writable' => defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ( defined( 'ABSPATH' ) ? ABSPATH . '/wp-content' : __( 'Undefined', 'nitropack' ) ), |
| 156 | 'Nitro_Data_Dir_Writable' => defined( 'NITROPACK_DATA_DIR' ) ? NITROPACK_DATA_DIR : npdiag_helper_trailingslashit( WP_CONTENT_DIR ) . 'nitropack', |
| 157 | 'Nitro_siteID_Dir_Writable' => defined( 'NITROPACK_DATA_DIR' ) ? NITROPACK_DATA_DIR . "/$siteID" : npdiag_helper_trailingslashit( WP_CONTENT_DIR ) . "nitropack/$siteID", |
| 158 | 'Nitro_Plugin_Dir_Writable' => defined( 'NITROPACK_PLUGIN_DIR' ) ? NITROPACK_PLUGIN_DIR : dirname( __FILE__ ), |
| 159 | 'Nitro_Plugin_Data_Dir_Writable' => defined( 'NITROPACK_PLUGIN_DATA_DIR' ) ? NITROPACK_PLUGIN_DATA_DIR : npdiag_helper_trailingslashit( WP_CONTENT_DIR ) . 'nitropack', |
| 160 | ); |
| 161 | |
| 162 | $info = array(); |
| 163 | foreach ( $DoI as $doi_dir => $dpath ) { |
| 164 | if ( is_dir( $dpath ) ) { |
| 165 | $info[ $doi_dir ] = is_writeable( $dpath ) ? true : false; |
| 166 | } else if ( is_file( $dpath ) ) { |
| 167 | $info[ $doi_dir ] = $dpath . __( ' is a file not a directory', 'nitropack' ); |
| 168 | } else { |
| 169 | $info[ $doi_dir ] = __( 'Directory not found', 'nitropack' ); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return $info; |
| 174 | } |
| 175 | |
| 176 | function npdiag_get_conflicting_plugins() { |
| 177 | $conflictingPlugins = \NitroPack\WordPress\ConflictingPlugins::getInstance(); |
| 178 | $info = $conflictingPlugins->nitropack_get_conflicting_plugins(); |
| 179 | if ( ! empty( $info ) ) { |
| 180 | return $info; |
| 181 | } else { |
| 182 | return $info = __( 'None detected', 'nitropack' ); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | function npdiag_detect_third_party_cache() { |
| 187 | $info = \NitroPack\Integration\Plugin\RC::detectThirdPartyCaches(); |
| 188 | if ( ! empty( $info ) ) { |
| 189 | return $info; |
| 190 | } else { |
| 191 | return $info = __( 'Not found', 'nitropack' ); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | function nitropack_generate_report() { |
| 196 | global $np_diag_functions; |
| 197 | nitropack_verify_ajax_nonce( $_REQUEST ); |
| 198 | try { |
| 199 | $ar = ! empty( $_POST["toggled"] ) ? $_POST["toggled"] : NULL; |
| 200 | if ( $ar !== NULL ) { |
| 201 | $diag_data = array( 'report-time-stamp' => date( "Y-m-d H:i:s" ) ); |
| 202 | foreach ( $ar as $func_name => $func_allowed ) { |
| 203 | if ( (boolean) $func_allowed ) { |
| 204 | $diag_data[ $func_name ] = call_user_func( $np_diag_functions[ $func_name ] ); |
| 205 | } |
| 206 | } |
| 207 | $str = json_encode( $diag_data, JSON_PRETTY_PRINT ); |
| 208 | $filename = 'nitropack_diag_file.txt'; |
| 209 | nitropack_header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); |
| 210 | nitropack_header( "Content-Type: text/plain" ); |
| 211 | nitropack_header( "Content-Length: " . strlen( $str ) ); |
| 212 | echo $str; |
| 213 | exit; |
| 214 | } |
| 215 | } catch (\Exception $e) { |
| 216 | //exception handling here |
| 217 | } |
| 218 | |
| 219 | } |
| 220 |