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-page.php
209 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_PageBase extends \WBCR\Factory_Templates_759\Pages\PageBase { |
| 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_menu_short_description = __( 'Plugin debug report', 'robin-image-optimizer' ); |
| 38 | |
| 39 | add_action( 'wp_ajax_wbcr_factory_logger_359_' . $plugin->getPrefix() . 'logs_cleanup', [ $this, 'ajax_cleanup' ] ); |
| 40 | |
| 41 | parent::__construct( $plugin ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * {@inheritdoc} |
| 46 | * |
| 47 | * @return void |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public function assets( $scripts, $styles ) { |
| 51 | parent::assets( $scripts, $styles ); |
| 52 | |
| 53 | $this->styles->add( FACTORY_LOGGER_359_URL . '/assets/css/logger.css' ); |
| 54 | $this->scripts->add( FACTORY_LOGGER_359_URL . '/assets/js/logger.js', [ 'jquery' ], 'wbcr_factory_logger_359', FACTORY_LOGGER_359_VERSION ); |
| 55 | $this->scripts->localize( |
| 56 | 'wbcr_factory_logger_359', |
| 57 | [ |
| 58 | 'clean_logs_nonce' => wp_create_nonce( 'wbcr_factory_logger_359_clean_logs' ), |
| 59 | 'plugin_prefix' => $this->plugin->getPrefix(), |
| 60 | ] |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * {@inheritdoc} |
| 66 | */ |
| 67 | public function getMenuTitle() { |
| 68 | return __( 'Plugin Log', 'robin-image-optimizer' ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * {@inheritdoc} |
| 73 | */ |
| 74 | public function showPageContent() { |
| 75 | $buttons = " |
| 76 | <div class='btn-group'> |
| 77 | <a href='" . wp_nonce_url( $this->getActionUrl( 'export' ), 'export-' . $this->plugin->getPluginName() ) . "' |
| 78 | class='button button-primary'>" . __( 'Export Debug Information', 'robin-image-optimizer' ) . "</a> |
| 79 | <a href='#' |
| 80 | class='button button-secondary' |
| 81 | onclick='wbcr_factory_logger_359_LogCleanup(this);return false;' |
| 82 | data-working='" . __( 'Working...', 'robin-image-optimizer' ) . "'>" . |
| 83 | sprintf( |
| 84 | '%1$s %2$s', |
| 85 | __( 'Clear Logs', 'robin-image-optimizer' ), |
| 86 | '(<span id="wbcr-log-size">' . $this->get_log_size_formatted() . '</span>)' |
| 87 | ) . ' |
| 88 | </a> |
| 89 | </div>'; |
| 90 | ?> |
| 91 | <div class="wbcr-factory-page-group-header" style="margin-top:0;"> |
| 92 | <strong><?php _e( 'Plugin Log', 'robin-image-optimizer' ); ?></strong> |
| 93 | <p> |
| 94 | <?php _e( 'Track plugin activity here. Share this log with support to troubleshoot issues.', 'robin-image-optimizer' ); ?> |
| 95 | </p> |
| 96 | </div> |
| 97 | <div class="wbcr-factory-page-group-body" style="padding: 0 20px"> |
| 98 | <?php $this->render_support_upsell_banner(); ?> |
| 99 | <?php echo $buttons; ?> |
| 100 | <div class="wbcr-log-viewer" id="wbcr-log-viewer"> |
| 101 | <?php echo $this->plugin->logger->prettify(); ?> |
| 102 | </div> |
| 103 | </div> |
| 104 | <?php |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Render the support banner for debug log troubleshooting. |
| 109 | * |
| 110 | * @return void |
| 111 | */ |
| 112 | protected function render_support_upsell_banner() { |
| 113 | $is_premium = function_exists( 'wrio_is_license_activate' ) && wrio_is_license_activate(); |
| 114 | |
| 115 | $url = $is_premium |
| 116 | ? tsdk_utmify( tsdk_translate_link( 'http://themeisle.com/contact' ), 'debug-page', 'debug-support-banner' ) |
| 117 | : 'https://wordpress.org/support/plugin/robin-image-optimizer/'; |
| 118 | |
| 119 | if ( empty( $url ) ) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | $support_message = $is_premium |
| 124 | ? __( 'Contact our support team for priority assistance.', 'robin-image-optimizer' ) |
| 125 | : __( 'Please open a topic on the WordPress.org support forum.', 'robin-image-optimizer' ); |
| 126 | $button_label = $is_premium |
| 127 | ? __( 'Contact Support', 'robin-image-optimizer' ) |
| 128 | : __( 'Open Support Forum', 'robin-image-optimizer' ); |
| 129 | ?> |
| 130 | <div id="tsdk_banner" class="robin-banner"></div> |
| 131 | <div id="WBCR"> |
| 132 | <div class="wrio-errorlog-support-banner"> |
| 133 | <div class="wrio-errorlog-support-icon"> |
| 134 | <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 135 | <circle cx="12" cy="12" r="10" stroke="#2196F3" stroke-width="2" fill="none"></circle> |
| 136 | <path d="M12 8v4m0 4h.01" stroke="#2196F3" stroke-width="2" stroke-linecap="round"></path> |
| 137 | </svg> |
| 138 | </div> |
| 139 | <div class="wrio-errorlog-support-content"> |
| 140 | <p class="wrio-errorlog-support-title"><?php esc_html_e( 'Need help troubleshooting?', 'robin-image-optimizer' ); ?></p> |
| 141 | <p class="wrio-errorlog-support-subtitle"><?php echo esc_html( $support_message ); ?></p> |
| 142 | </div> |
| 143 | <a href="<?php echo esc_url( $url ); ?>" class="wrio-errorlog-support-button" target="_blank" rel="noopener"> |
| 144 | <?php echo esc_html( $button_label ); ?> |
| 145 | </a> |
| 146 | </div> |
| 147 | </div> |
| 148 | <?php |
| 149 | } |
| 150 | |
| 151 | public function ajax_cleanup() { |
| 152 | check_admin_referer( 'wbcr_factory_logger_359_clean_logs', 'nonce' ); |
| 153 | |
| 154 | if ( ! current_user_can( 'manage_options' ) ) { |
| 155 | wp_die( -1 ); |
| 156 | } |
| 157 | |
| 158 | if ( ! $this->plugin->logger->clean_up() ) { |
| 159 | wp_send_json_error( |
| 160 | [ |
| 161 | 'message' => esc_html__( 'Failed to clear logs. Please try again later.', 'robin-image-optimizer' ), |
| 162 | 'type' => 'danger', |
| 163 | ] |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | wp_send_json( |
| 168 | [ |
| 169 | 'message' => esc_html__( 'Logs cleared successfully.', 'robin-image-optimizer' ), |
| 170 | 'type' => 'success', |
| 171 | ] |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Processing log export action in form of ZIP archive. |
| 177 | */ |
| 178 | public function exportAction() { |
| 179 | if ( |
| 180 | ! ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'export-' . $this->plugin->getPluginName() ) ) || |
| 181 | ! $this->plugin->current_user_can() |
| 182 | ) { |
| 183 | wp_die( esc_html__( 'You do not have sufficient permissions to perform this action!', 'robin-image-optimizer' ) ); |
| 184 | } |
| 185 | |
| 186 | $export = new WBCR\Factory_Logger_359\Log_Export( $this->plugin->logger ); |
| 187 | |
| 188 | if ( $export->prepare() ) { |
| 189 | $export->download( true ); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Get log size formatted. |
| 195 | * |
| 196 | * @return false|string |
| 197 | */ |
| 198 | private function get_log_size_formatted() { |
| 199 | |
| 200 | try { |
| 201 | return size_format( $this->plugin->logger->get_total_size() ); |
| 202 | } catch ( \Exception $exception ) { |
| 203 | $this->plugin->logger->error( sprintf( 'Failed to get total log size as exception was thrown: %s', $exception->getMessage() ) ); |
| 204 | } |
| 205 | |
| 206 | return ''; |
| 207 | } |
| 208 | } |
| 209 |