class-logger-admin-page.php
7 months ago
class-logger-impressive-lite.php
7 months ago
class-logger-impressive-page.php
4 months ago
class-logger-impressive-lite.php
170 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * The class is responsible for the operation of the logs page for a lite interface |
| 10 | * |
| 11 | * @version 1.0 |
| 12 | */ |
| 13 | class Wbcr_FactoryLogger359_Lite extends \WBCR\Factory_Templates_759\ImpressiveLite { |
| 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->page_menu_short_description = __( 'Plugin debug report', 'robin-image-optimizer' ); |
| 37 | |
| 38 | add_action( |
| 39 | 'wp_ajax_wbcr_factory_logger_359_' . $plugin->getPrefix() . 'logs_cleanup', |
| 40 | [ |
| 41 | $this, |
| 42 | 'ajax_cleanup', |
| 43 | ] |
| 44 | ); |
| 45 | |
| 46 | parent::__construct( $plugin ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * {@inheritdoc} |
| 51 | * |
| 52 | * @return void |
| 53 | * @since 1.0.0 |
| 54 | */ |
| 55 | public function assets( $scripts, $styles ) { |
| 56 | parent::assets( $scripts, $styles ); |
| 57 | |
| 58 | $this->styles->add( FACTORY_LOGGER_359_URL . '/assets/css/logger.css' ); |
| 59 | $this->scripts->add( FACTORY_LOGGER_359_URL . '/assets/js/logger.js', [ 'jquery' ], 'wbcr_factory_logger_359', FACTORY_LOGGER_359_VERSION ); |
| 60 | $this->scripts->localize( |
| 61 | 'wbcr_factory_logger_359', |
| 62 | [ |
| 63 | 'clean_logs_nonce' => wp_create_nonce( 'wbcr_factory_logger_359_clean_logs' ), |
| 64 | 'plugin_prefix' => $this->plugin->getPrefix(), |
| 65 | ] |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * {@inheritdoc} |
| 71 | */ |
| 72 | public function getMenuTitle() { |
| 73 | return __( 'Plugin Log', 'robin-image-optimizer' ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * {@inheritdoc} |
| 78 | */ |
| 79 | public function showPageContent() { |
| 80 | $buttons = " |
| 81 | <div class='btn-group'> |
| 82 | <a href='" . wp_nonce_url( $this->getActionUrl( 'export' ), 'export-' . $this->plugin->getPluginName() ) . "' |
| 83 | class='button button-primary'>" . __( 'Export Debug Information', 'robin-image-optimizer' ) . "</a> |
| 84 | <a href='#' |
| 85 | class='button button-secondary' |
| 86 | onclick='wbcr_factory_logger_359_LogCleanup(this);return false;' |
| 87 | data-working='" . __( 'Working...', 'robin-image-optimizer' ) . "'>" . |
| 88 | sprintf( |
| 89 | '%1$s %2$s', |
| 90 | __( 'Clear Logs', 'robin-image-optimizer' ), |
| 91 | '(<span id="wbcr-log-size">' . $this->get_log_size_formatted() . '</span>)' |
| 92 | ) . ' |
| 93 | </a> |
| 94 | </div>'; |
| 95 | ?> |
| 96 | <div class="wbcr-factory-page-group-header" style="margin-top:0;"> |
| 97 | <strong><?php _e( 'Plugin Log', 'robin-image-optimizer' ); ?></strong> |
| 98 | <p> |
| 99 | <?php _e( 'Track plugin activity here. Share this log with support to troubleshoot issues.', 'robin-image-optimizer' ); ?> |
| 100 | </p> |
| 101 | </div> |
| 102 | <div class="wbcr-factory-page-group-body" style="padding: 0 20px"> |
| 103 | <?php echo $buttons; ?> |
| 104 | <div class="wbcr-log-viewer" id="wbcr-log-viewer"> |
| 105 | <?php echo $this->plugin->logger->prettify(); ?> |
| 106 | </div> |
| 107 | </div> |
| 108 | <?php |
| 109 | } |
| 110 | |
| 111 | public function ajax_cleanup() { |
| 112 | check_admin_referer( 'wbcr_factory_logger_359_clean_logs', 'nonce' ); |
| 113 | |
| 114 | if ( ! current_user_can( 'manage_options' ) ) { |
| 115 | wp_die( - 1 ); |
| 116 | } |
| 117 | |
| 118 | if ( ! $this->plugin->logger->clean_up() ) { |
| 119 | wp_send_json_error( |
| 120 | [ |
| 121 | 'message' => esc_html__( 'Failed to clear logs. Please try again later.', 'robin-image-optimizer' ), |
| 122 | 'type' => 'danger', |
| 123 | ] |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | wp_send_json( |
| 128 | [ |
| 129 | 'message' => esc_html__( 'Logs cleared successfully.', 'robin-image-optimizer' ), |
| 130 | 'type' => 'success', |
| 131 | ] |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Processing log export action in form of ZIP archive. |
| 137 | */ |
| 138 | public function exportAction() { |
| 139 | if ( |
| 140 | ! ( isset( $_GET['_wpnonce'] ) && |
| 141 | wp_verify_nonce( $_GET['_wpnonce'], 'export-' . $this->plugin->getPluginName() ) ) || |
| 142 | ! $this->plugin->current_user_can() |
| 143 | ) { |
| 144 | wp_die( esc_html__( 'You do not have sufficient permissions to perform this action!', 'robin-image-optimizer' ) ); |
| 145 | } |
| 146 | |
| 147 | $export = new WBCR\Factory_Logger_359\Log_Export( $this->plugin->logger ); |
| 148 | |
| 149 | if ( $export->prepare() ) { |
| 150 | $export->download( true ); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get log size formatted. |
| 156 | * |
| 157 | * @return false|string |
| 158 | */ |
| 159 | private function get_log_size_formatted() { |
| 160 | |
| 161 | try { |
| 162 | return size_format( $this->plugin->logger->get_total_size() ); |
| 163 | } catch ( \Exception $exception ) { |
| 164 | $this->plugin->logger->error( sprintf( 'Failed to get total log size as exception was thrown: %s', $exception->getMessage() ) ); |
| 165 | } |
| 166 | |
| 167 | return ''; |
| 168 | } |
| 169 | } |
| 170 |