class-logger-admin-page.php
1 year ago
class-logger-impressive-lite.php
1 year ago
class-logger-impressive-page.php
1 year ago
class-logger-admin-page.php
164 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * Класс отвечает за работу страницы логов. |
| 10 | * |
| 11 | * @author Artem Prihodko <webtemyk@yandex.ru> |
| 12 | * @copyright (c) 2020, Webcraftic |
| 13 | * @version 1.0 |
| 14 | */ |
| 15 | class Wbcr_FactoryLogger149_AdminPage extends Wbcr_FactoryPages480_AdminPage { |
| 16 | |
| 17 | /** |
| 18 | * {@inheritdoc} |
| 19 | */ |
| 20 | public $id; // Уникальный идентификатор страницы |
| 21 | |
| 22 | /** |
| 23 | * {@inheritdoc} |
| 24 | */ |
| 25 | public $page_menu_dashicon = 'dashicons-admin-tools'; |
| 26 | |
| 27 | /** |
| 28 | * {@inheritdoc} |
| 29 | */ |
| 30 | public $type = 'page'; |
| 31 | |
| 32 | /** |
| 33 | * @param Wbcr_Factory480_Plugin $plugin |
| 34 | */ |
| 35 | public function __construct( $plugin ) { |
| 36 | $this->id = $plugin->getPrefix() . "logger"; |
| 37 | |
| 38 | $this->menu_title = __( 'Plugin Log', 'wbcr_factory_logger_149' ); |
| 39 | $this->page_title = __( 'Plugin log', 'wbcr_factory_logger_149' ); |
| 40 | $this->capabilitiy = "manage_options"; |
| 41 | |
| 42 | add_action( 'wp_ajax_wbcr_factory_logger_149_'.$plugin->getPrefix().'logs_cleanup', [ $this, 'ajax_cleanup' ] ); |
| 43 | |
| 44 | parent::__construct( $plugin ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * {@inheritdoc} |
| 49 | * |
| 50 | * @return void |
| 51 | * @since 1.0.0 |
| 52 | */ |
| 53 | public function assets( $scripts, $styles ) { |
| 54 | parent::assets( $scripts, $styles ); |
| 55 | |
| 56 | $this->styles->add( FACTORY_LOGGER_149_URL . '/assets/css/logger.css' ); |
| 57 | $this->scripts->add( FACTORY_LOGGER_149_URL . '/assets/js/logger.js', [ 'jquery' ], 'wbcr_factory_logger_149', FACTORY_LOGGER_149_VERSION ); |
| 58 | wp_localize_script( 'wbcr_factory_logger_149', 'wbcr_factory_logger_149', [ |
| 59 | 'clean_logs_nonce' => wp_create_nonce( 'wbcr_factory_logger_149_clean_logs' ), |
| 60 | 'plugin_prefix' => $this->plugin->getPrefix(), |
| 61 | ] ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * {@inheritdoc} |
| 66 | */ |
| 67 | public function getMenuTitle() { |
| 68 | return __( 'Plugin Log', 'wbcr_factory_logger_149' ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Show rendered template - $template_name |
| 73 | */ |
| 74 | public function indexAction() { |
| 75 | $this->showPageContent(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * {@inheritdoc} |
| 80 | */ |
| 81 | public function showPageContent() { |
| 82 | $buttons = " |
| 83 | <div class='wbcr_factory_logger_buttons'> |
| 84 | <a href='" . wp_nonce_url( $this->getActionUrl( 'export' ), 'export-' . $this->plugin->getPluginName() ) . "' |
| 85 | class='button button-primary'>" . __( 'Export Debug Information', 'wbcr_factory_logger_149' ) . "</a> |
| 86 | <a href='#' |
| 87 | class='button button-secondary' |
| 88 | onclick='wbcr_factory_logger_149_LogCleanup(this);return false;' |
| 89 | data-working='" . __( 'Working...', 'wbcr_factory_logger_149' ) . "'>" . |
| 90 | sprintf( __( 'Clean-up Logs (<span id="wbcr-log-size">%s</span>)', 'wbcr_factory_logger_149' ), $this->get_log_size_formatted() ) . " |
| 91 | </a> |
| 92 | </div>"; |
| 93 | |
| 94 | ?> |
| 95 | <div class="wbcr_factory_logger_container"> |
| 96 | <div class="wbcr_factory_logger_page_title"> |
| 97 | <h1><?php _e( 'Logs of the', 'wbcr_factory_logger_149' ) ?> |
| 98 | <?php echo $this->plugin->getPluginTitle() . " " . $this->plugin->getPluginVersion(); ?></h1> |
| 99 | <p> |
| 100 | <?php _e( 'In this section, you can track how the plugin works. Sending this log to the developer will help you resolve possible issues.', 'wbcr_factory_logger_149' ) ?> |
| 101 | </p> |
| 102 | </div> |
| 103 | <?php echo $buttons; ?> |
| 104 | <div class="wbcr-log-viewer" id="wbcr-log-viewer"> |
| 105 | <?php echo $this->plugin->logger->prettify() ?> |
| 106 | </div> |
| 107 | <?php echo $buttons; ?> |
| 108 | </div> |
| 109 | <?php |
| 110 | } |
| 111 | |
| 112 | public function ajax_cleanup() { |
| 113 | check_admin_referer( 'wbcr_factory_logger_149_clean_logs', 'nonce' ); |
| 114 | |
| 115 | if ( ! current_user_can( 'manage_options' ) ) { |
| 116 | wp_die( - 1 ); |
| 117 | } |
| 118 | |
| 119 | if ( ! $this->plugin->logger->clean_up() ) { |
| 120 | wp_send_json_error( [ |
| 121 | 'message' => esc_html__( 'Failed to clean-up logs. Please try again later.', 'wbcr_factory_logger_149' ), |
| 122 | 'type' => 'danger', |
| 123 | ] ); |
| 124 | } |
| 125 | |
| 126 | wp_send_json( [ |
| 127 | 'message' => esc_html__( 'Logs clean-up successfully', 'wbcr_factory_logger_149' ), |
| 128 | 'type' => 'success', |
| 129 | ] ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Processing log export action in form of ZIP archive. |
| 134 | */ |
| 135 | public function exportAction() { |
| 136 | if( !(isset( $_GET[ '_wpnonce' ] ) && wp_verify_nonce( $_GET[ '_wpnonce' ], 'export-' . $this->plugin->getPluginName() )) |
| 137 | || !$this->plugin->currentUserCan() ) { |
| 138 | wp_die(__('You do not have sufficient permissions to perform this action!', 'wbcr_factory_logger_149')); |
| 139 | } |
| 140 | |
| 141 | $export = new WBCR\Factory_Logger_149\Log_Export( $this->plugin->logger ); |
| 142 | |
| 143 | if ( $export->prepare() ) { |
| 144 | $export->download( true ); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get log size formatted. |
| 150 | * |
| 151 | * @return false|string |
| 152 | */ |
| 153 | private function get_log_size_formatted() { |
| 154 | |
| 155 | try { |
| 156 | return size_format( $this->plugin->logger->get_total_size() ); |
| 157 | } catch ( \Exception $exception ) { |
| 158 | $this->plugin->logger->error( sprintf( 'Failed to get total log size as exception was thrown: %s', $exception->getMessage() ) ); |
| 159 | } |
| 160 | |
| 161 | return ''; |
| 162 | } |
| 163 | } |
| 164 |