WPCapture.php
128 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor\WPDesk\Logger\WP; |
| 4 | |
| 5 | class WPCapture |
| 6 | { |
| 7 | /** @var string */ |
| 8 | private $filename; |
| 9 | const LOG_DIR = 'wpdesk-logs'; |
| 10 | public function __construct($filename) |
| 11 | { |
| 12 | $this->filename = $filename; |
| 13 | } |
| 14 | /** |
| 15 | * Add notice for directory. |
| 16 | * |
| 17 | * @param string $dir Directory. |
| 18 | */ |
| 19 | private function add_notice_for_dir($dir) |
| 20 | { |
| 21 | new \FSVendor\WPDesk\Notice\Notice(sprintf( |
| 22 | // Translators: directory. |
| 23 | __('Can not enable WP Desk Debug log! Cannot create directory %s or this directory is not writeable!', 'flexible-shipping'), |
| 24 | $dir |
| 25 | ), \FSVendor\WPDesk\Notice\Notice::NOTICE_TYPE_ERROR); |
| 26 | } |
| 27 | /** |
| 28 | * Add notice for file. |
| 29 | * |
| 30 | * @param string $file File.. |
| 31 | */ |
| 32 | private function add_notice_for_file($file) |
| 33 | { |
| 34 | new \FSVendor\WPDesk\Notice\Notice(sprintf( |
| 35 | // Translators: directory. |
| 36 | __('Can not enable WP Desk Debug log! Cannot create file %s!', 'flexible-shipping'), |
| 37 | $file |
| 38 | ), \FSVendor\WPDesk\Notice\Notice::NOTICE_TYPE_ERROR); |
| 39 | } |
| 40 | /** |
| 41 | * Is debug log writable. |
| 42 | * |
| 43 | * @return bool |
| 44 | */ |
| 45 | private function is_debug_log_writable_or_show_notice() |
| 46 | { |
| 47 | $log_dir = $this->get_log_dir(); |
| 48 | $log_file = $this->get_log_file(); |
| 49 | $index_file = $this->get_index_file(); |
| 50 | if (!file_exists($log_dir)) { |
| 51 | if (!mkdir($log_dir, 0777, \true)) { |
| 52 | $this->add_notice_for_dir($log_dir); |
| 53 | return \false; |
| 54 | } |
| 55 | } |
| 56 | if (!file_exists($index_file)) { |
| 57 | $index_html = fopen($index_file, 'w'); |
| 58 | if (\false === $index_html) { |
| 59 | $this->add_notice_for_file($index_file); |
| 60 | return \false; |
| 61 | } else { |
| 62 | fclose($index_html); |
| 63 | } |
| 64 | } |
| 65 | if (!file_exists($log_file)) { |
| 66 | $log = fopen($log_file, 'w'); |
| 67 | if (\false === $log) { |
| 68 | $this->add_notice_for_file($log_file); |
| 69 | return \false; |
| 70 | } else { |
| 71 | fclose($log); |
| 72 | } |
| 73 | } |
| 74 | if (!is_writable($log_file)) { |
| 75 | $this->add_notice_for_file($log_file); |
| 76 | return \false; |
| 77 | } |
| 78 | return \true; |
| 79 | } |
| 80 | /** |
| 81 | * Init debug log file. |
| 82 | */ |
| 83 | public function init_debug_log_file() |
| 84 | { |
| 85 | if ($this->is_debug_log_writable_or_show_notice()) { |
| 86 | ini_set('log_errors', 1); |
| 87 | ini_set('error_log', $this->get_log_file()); |
| 88 | } |
| 89 | } |
| 90 | /** |
| 91 | * Get uploads dir. |
| 92 | * |
| 93 | * @return string |
| 94 | */ |
| 95 | private function get_uploads_dir() |
| 96 | { |
| 97 | $upload_dir = wp_upload_dir(); |
| 98 | return untrailingslashit($upload_dir['basedir']); |
| 99 | } |
| 100 | /** |
| 101 | * Get log dir. |
| 102 | * |
| 103 | * @return string |
| 104 | */ |
| 105 | private function get_log_dir() |
| 106 | { |
| 107 | return trailingslashit($this->get_uploads_dir()) . self::LOG_DIR; |
| 108 | } |
| 109 | /** |
| 110 | * Get log file. |
| 111 | * |
| 112 | * @return string |
| 113 | */ |
| 114 | public function get_log_file() |
| 115 | { |
| 116 | return trailingslashit($this->get_log_dir()) . $this->filename; |
| 117 | } |
| 118 | /** |
| 119 | * Get log file. |
| 120 | * |
| 121 | * @return string |
| 122 | */ |
| 123 | private function get_index_file() |
| 124 | { |
| 125 | return trailingslashit($this->get_log_dir()) . 'index.html'; |
| 126 | } |
| 127 | } |
| 128 |