builder
2 years ago
plugin-updates
8 years ago
settings
2 years ago
views
2 years ago
class-evf-admin-addons.php
4 years ago
class-evf-admin-assets.php
2 years ago
class-evf-admin-builder.php
7 years ago
class-evf-admin-deactivation-feedback.php
3 years ago
class-evf-admin-editor.php
4 years ago
class-evf-admin-entries-table-list.php
3 years ago
class-evf-admin-entries.php
4 years ago
class-evf-admin-form-templates.php
3 years ago
class-evf-admin-forms-table-list.php
3 years ago
class-evf-admin-forms.php
3 years ago
class-evf-admin-import-export.php
4 years ago
class-evf-admin-menus.php
2 years ago
class-evf-admin-notices.php
3 years ago
class-evf-admin-settings.php
2 years ago
class-evf-admin-tools.php
4 years ago
class-evf-admin-welcome.php
2 years ago
class-evf-admin.php
2 years ago
evf-admin-functions.php
3 years ago
class-evf-admin-tools.php
200 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Debug/Status page |
| 4 | * |
| 5 | * @package EverestForms/Admin/Tools |
| 6 | * @version 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Admin_Tools Class. |
| 13 | */ |
| 14 | class EVF_Admin_Tools { |
| 15 | |
| 16 | /** |
| 17 | * Handles output of the reports page in admin. |
| 18 | */ |
| 19 | public static function output() { |
| 20 | include_once dirname( __FILE__ ) . '/views/html-admin-page-tools.php'; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Show the import page. |
| 25 | */ |
| 26 | public static function import() { |
| 27 | include_once dirname( __FILE__ ) . '/views/html-admin-page-import.php'; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Show the export page. |
| 32 | */ |
| 33 | public static function export() { |
| 34 | include_once dirname( __FILE__ ) . '/views/html-admin-page-export.php'; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Show the logs page. |
| 39 | */ |
| 40 | public static function status_logs() { |
| 41 | self::status_logs_file(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Show the log page contents for file log handler. |
| 46 | */ |
| 47 | public static function status_logs_file() { |
| 48 | $logs = self::scan_log_files(); |
| 49 | |
| 50 | if ( ! empty( $_REQUEST['log_file'] ) && isset( $logs[ sanitize_title( wp_unslash( $_REQUEST['log_file'] ) ) ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 51 | $viewed_log = $logs[ sanitize_title( wp_unslash( $_REQUEST['log_file'] ) ) ]; // phpcs:ignore WordPress.Security.NonceVerification |
| 52 | } elseif ( ! empty( $logs ) ) { |
| 53 | $viewed_log = current( $logs ); |
| 54 | } |
| 55 | |
| 56 | if ( ! empty( $_REQUEST['handle'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 57 | self::remove_log(); |
| 58 | } |
| 59 | |
| 60 | // Remove All Logs. |
| 61 | if ( ! empty( $_REQUEST['handle_all'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 62 | self::remove_all_logs(); |
| 63 | } |
| 64 | |
| 65 | include_once 'views/html-admin-page-tools-logs.php'; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Retrieve metadata from a file. Based on WP Core's get_file_data function. |
| 70 | * |
| 71 | * @since 1.0.0 |
| 72 | * @param string $file Path to the file. |
| 73 | * @return string |
| 74 | */ |
| 75 | public static function get_file_version( $file ) { |
| 76 | // Avoid notices if file does not exist. |
| 77 | if ( ! file_exists( $file ) ) { |
| 78 | return ''; |
| 79 | } |
| 80 | |
| 81 | // We don't need to write to the file, so just open for reading. |
| 82 | $fp = fopen( $file, 'r' ); // @codingStandardsIgnoreLine |
| 83 | |
| 84 | // Pull only the first 8kiB of the file in. |
| 85 | $file_data = fread( $fp, 8192 ); // @codingStandardsIgnoreLine |
| 86 | |
| 87 | // PHP will close file handle, but we are good citizens. |
| 88 | fclose( $fp ); // @codingStandardsIgnoreLine |
| 89 | |
| 90 | // Make sure we catch CR-only line endings. |
| 91 | $file_data = str_replace( "\r", "\n", $file_data ); |
| 92 | $version = ''; |
| 93 | |
| 94 | if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( '@version', '/' ) . '(.*)$/mi', $file_data, $match ) && $match[1] ) { |
| 95 | $version = _cleanup_header_comment( $match[1] ); |
| 96 | } |
| 97 | |
| 98 | return $version; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return the log file handle. |
| 103 | * |
| 104 | * @param string $filename Filename to get the handle for. |
| 105 | * @return string |
| 106 | */ |
| 107 | public static function get_log_file_handle( $filename ) { |
| 108 | return substr( $filename, 0, strlen( $filename ) > 48 ? strlen( $filename ) - 48 : strlen( $filename ) - 4 ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Scan the template files. |
| 113 | * |
| 114 | * @param string $template_path Path to the template directory. |
| 115 | * @return array |
| 116 | */ |
| 117 | public static function scan_template_files( $template_path ) { |
| 118 | $files = @scandir( $template_path ); // @codingStandardsIgnoreLine |
| 119 | $result = array(); |
| 120 | |
| 121 | if ( ! empty( $files ) ) { |
| 122 | |
| 123 | foreach ( $files as $key => $value ) { |
| 124 | |
| 125 | if ( ! in_array( $value, array( '.', '..' ), true ) ) { |
| 126 | |
| 127 | if ( is_dir( $template_path . DIRECTORY_SEPARATOR . $value ) ) { |
| 128 | $sub_files = self::scan_template_files( $template_path . DIRECTORY_SEPARATOR . $value ); |
| 129 | foreach ( $sub_files as $sub_file ) { |
| 130 | $result[] = $value . DIRECTORY_SEPARATOR . $sub_file; |
| 131 | } |
| 132 | } else { |
| 133 | $result[] = $value; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | return $result; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Scan the log files. |
| 143 | * |
| 144 | * @return array |
| 145 | */ |
| 146 | public static function scan_log_files() { |
| 147 | $files = @scandir( EVF_LOG_DIR ); // @codingStandardsIgnoreLine |
| 148 | $result = array(); |
| 149 | |
| 150 | if ( ! empty( $files ) ) { |
| 151 | |
| 152 | foreach ( $files as $key => $value ) { |
| 153 | |
| 154 | if ( ! in_array( $value, array( '.', '..' ), true ) ) { |
| 155 | if ( ! is_dir( $value ) && strstr( $value, '.log' ) ) { |
| 156 | $result[ sanitize_title( $value ) ] = $value; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return $result; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Remove/delete the chosen file. |
| 167 | */ |
| 168 | public static function remove_log() { |
| 169 | if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'remove_log' ) ) { |
| 170 | wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'everest-forms' ) ); |
| 171 | } |
| 172 | |
| 173 | if ( ! empty( $_REQUEST['handle'] ) ) { |
| 174 | $log_handler = new EVF_Log_Handler_File(); |
| 175 | $log_handler->remove( sanitize_text_field( wp_unslash( $_REQUEST['handle'] ) ) ); |
| 176 | } |
| 177 | |
| 178 | wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=evf-tools&tab=logs' ) ) ); |
| 179 | exit(); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Remove/delete all logs. |
| 184 | */ |
| 185 | public static function remove_all_logs() { |
| 186 | if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'remove_all_logs' ) ) { |
| 187 | wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'everest-forms' ) ); |
| 188 | } |
| 189 | |
| 190 | if ( ! empty( $_REQUEST['handle_all'] ) ) { |
| 191 | $log_handler = new EVF_Log_Handler_File(); |
| 192 | $log_handler->remove_all(); |
| 193 | } |
| 194 | |
| 195 | wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=evf-tools&tab=logs' ) ) ); |
| 196 | exit(); |
| 197 | } |
| 198 | |
| 199 | } |
| 200 |