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