Skip to content
Snippets Groups Projects
Select Git revision
  • 2d5cb9e473bb8ede4fa2290b74f03115aba275f5
  • master default protected
  • feat/message-handling
  • feat/request-id
  • 1.13.2
  • 1.13.1
  • 1.13.0
  • 1.12.1
  • 1.12.0
  • 1.11.0
  • 1.11.0-beta2
  • 1.11.0-beta1
  • 1.10.2
  • 1.10.1
  • 1.10.0
  • 1.9.0
  • 1.8.0
  • 1.7.4
  • 1.7.3
  • 1.7.2
  • 1.7.1
  • 1.7.0
  • 1.6.2
  • 1.6.2-beta2
24 results

WPCapture.php

Blame
  • WPCapture.php 2.75 KiB
    <?php
    
    namespace WPDesk\Logger\WP;
    
    class WPCapture {
        /** @var string */
    	private $filename;
    
        const LOG_DIR = 'wpdesk-logs';
    
    	public function __construct($filename) {
    		$this->filename = $filename;
    	}
    
    	/**
    	 * Add notice for directory.
    	 *
    	 * @param string $dir Directory.
    	 */
    	private function add_notice_for_dir( $dir ) {
    		new \WPDesk\Notice\Notice(
    			sprintf(
    			// Translators: directory.
    				__(
    					'Can not enable WP Desk Debug log! Cannot create directory %s or this directory is not writeable!',
    					'wp-logs'
    				),
    				$dir
    			),
    			\WPDesk\Notice\Notice::NOTICE_TYPE_ERROR
    		);
    	}
    
    	/**
    	 * Add notice for file.
    	 *
    	 * @param string $file File..
    	 */
    	private function add_notice_for_file( $file ) {
    		new \WPDesk\Notice\Notice(
    			sprintf(
    			// Translators: directory.
    				__(
    					'Can not enable WP Desk Debug log! Cannot create file %s!',
    					'wp-logs'
    				),
    				$file
    			),
    			\WPDesk\Notice\Notice::NOTICE_TYPE_ERROR
    		);
    	}
    
    	/**
    	 * Is debug log writable.
    	 *
    	 * @return bool
    	 */
    	private function is_debug_log_writable_or_show_notice() {
    		$log_dir    = $this->get_log_dir();
    		$log_file   = $this->get_log_file();
    		$index_file = $this->get_index_file();
    		if ( ! file_exists( $log_dir ) ) {
    			if ( ! mkdir( $log_dir, 0777, true ) ) {
    				$this->add_notice_for_dir( $log_dir );
    
    				return false;
    			}
    		}
    		if ( ! file_exists( $index_file ) ) {
    			$index_html = fopen( $index_file, 'w' );
    			if ( false === $index_html ) {
    				$this->add_notice_for_file( $index_file );
    
    				return false;
    			} else {
    				fclose( $index_html );
    			}
    		}
    		if ( ! file_exists( $log_file ) ) {
    			$log = fopen( $log_file, 'w' );
    			if ( false === $log ) {
    				$this->add_notice_for_file( $log_file );
    
    				return false;
    			} else {
    				fclose( $log );
    			}
    		}
    
            if (!is_writable($log_file)) {
                $this->add_notice_for_file($log_file);
                return false;
            }
            return true;
    	}
    
    	/**
    	 * Init debug log file.
    	 */
    	public function init_debug_log_file() {
    		if ( $this->is_debug_log_writable_or_show_notice() ) {
    			ini_set( 'log_errors', 1 );
    			ini_set( 'error_log', $this->get_log_file() );
    		}
    	}
    
    	/**
    	 * Get uploads dir.
    	 *
    	 * @return string
    	 */
    	private function get_uploads_dir() {
    		$upload_dir = wp_upload_dir();
    
    		return untrailingslashit( $upload_dir['basedir'] );
    	}
    
    	/**
    	 * Get log dir.
    	 *
    	 * @return string
    	 */
    	private function get_log_dir() {
    		return trailingslashit( $this->get_uploads_dir() ) . self::LOG_DIR;
    	}
    
    	/**
    	 * Get log file.
    	 *
    	 * @return string
    	 */
    	public function get_log_file() {
    		return trailingslashit( $this->get_log_dir() ) . $this->filename;
    	}
    
    	/**
    	 * Get log file.
    	 *
    	 * @return string
    	 */
    	private function get_index_file() {
    		return trailingslashit( $this->get_log_dir() ) . 'index.html';
    	}
    }