integrations
7 years ago
class-strong-form.php
7 years ago
class-strong-log.php
7 years ago
class-strong-mail.php
7 years ago
class-strong-templates.php
7 years ago
class-strong-testimonials-order.php
7 years ago
class-strong-testimonials-privacy.php
7 years ago
class-strong-testimonials-render.php
7 years ago
class-strong-testimonials-shortcode-average.php
7 years ago
class-strong-testimonials-shortcode-count.php
7 years ago
class-strong-testimonials-shortcode.php
7 years ago
class-strong-view-display.php
7 years ago
class-strong-view-form.php
7 years ago
class-strong-view-slideshow.php
7 years ago
class-strong-view.php
7 years ago
class-walker-strong-category-checklist-front.php
7 years ago
deprecated.php
7 years ago
filters.php
7 years ago
functions-activation.php
7 years ago
functions-content.php
7 years ago
functions-image.php
7 years ago
functions-rating.php
7 years ago
functions-template-form.php
7 years ago
functions-template.php
7 years ago
functions-views.php
7 years ago
functions.php
7 years ago
l10n-polylang.php
7 years ago
l10n-wpml.php
7 years ago
post-types.php
7 years ago
retro.php
7 years ago
scripts.php
7 years ago
widget2.php
7 years ago
class-strong-log.php
142 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Debug Logger |
| 4 | */ |
| 5 | |
| 6 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 7 | |
| 8 | if ( ! class_exists( 'Strong_Log' ) ) : |
| 9 | |
| 10 | class Strong_Log { |
| 11 | |
| 12 | public $name; |
| 13 | |
| 14 | public $filename; |
| 15 | |
| 16 | public $action; |
| 17 | |
| 18 | public function __construct( $name ) { |
| 19 | $this->set_name( $name ); |
| 20 | $this->set_filename(); |
| 21 | $this->set_log_action(); |
| 22 | $this->add_actions(); |
| 23 | } |
| 24 | |
| 25 | private function set_name( $name = 'main' ) { |
| 26 | $this->name = $name; |
| 27 | } |
| 28 | |
| 29 | private function set_filename() { |
| 30 | $this->filename = apply_filters( "strong_log_{$this->name}_filename", |
| 31 | str_replace( '_', '-', "strong-{$this->name}-debug.log" ) ); |
| 32 | } |
| 33 | |
| 34 | private function set_log_action() { |
| 35 | $this->action = "strong_log_{$this->name}"; |
| 36 | } |
| 37 | |
| 38 | public function add_actions() { |
| 39 | add_action( 'init', array( $this, 'init' ), 20 ); |
| 40 | add_action( 'shutdown', array( $this, 'on_shutdown' ), 20 ); |
| 41 | } |
| 42 | |
| 43 | public function init() { |
| 44 | // TODO Make admin check optional. |
| 45 | if ( $this->is_enabled() && current_user_can( 'administrator' ) ) { |
| 46 | add_action( $this->action, array( $this, 'debug_log' ), 10, 3 ); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | private function is_enabled() { |
| 51 | $options = get_option( "wpmtst_{$this->name}_debug" ); |
| 52 | $is_enabled = ( isset( $options['log'] ) && $options['log'] ); |
| 53 | |
| 54 | return apply_filters( $this->action, $is_enabled ); |
| 55 | } |
| 56 | |
| 57 | public function get_log_file_path() { |
| 58 | return $this->get_log_file_base( 'basedir' ) . $this->filename; |
| 59 | } |
| 60 | |
| 61 | public function get_log_file_url() { |
| 62 | return $this->get_log_file_base( 'baseurl' ) . $this->filename; |
| 63 | } |
| 64 | |
| 65 | public function get_log_file_base( $base = 'basedir' ) { |
| 66 | $upload_dir = wp_upload_dir(); |
| 67 | |
| 68 | if ( isset( $upload_dir[ $base ] ) ) { |
| 69 | $log_file_base = $upload_dir[ $base ]; |
| 70 | } else { |
| 71 | $log_file_base = $upload_dir['basedir']; |
| 72 | } |
| 73 | |
| 74 | return trailingslashit( $log_file_base ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Debug log entries. |
| 79 | * |
| 80 | * @param $entry |
| 81 | * @param string $label |
| 82 | * @param string $function |
| 83 | */ |
| 84 | public function debug_log( $entry, $label = '', $function = '' ) { |
| 85 | $this->log( $entry, $label, $function ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Disable debug logging on shutdown. |
| 90 | */ |
| 91 | public function on_shutdown() { |
| 92 | if ( get_transient( $this->action ) ) { |
| 93 | do_action( $this->action, str_repeat( '-', 50 ), '', current_filter() ); |
| 94 | delete_transient( $this->action ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Generic logging function. |
| 100 | * |
| 101 | * @param array|string $data |
| 102 | * @param string $label |
| 103 | * @param string $function |
| 104 | */ |
| 105 | public function log( $data, $label = '', $function = '' ) { |
| 106 | |
| 107 | $entry = '[' . date('Y-m-d H:i:s') . ']'; |
| 108 | |
| 109 | if ( wp_doing_ajax() ) { |
| 110 | $entry .= ' | DOING_AJAX'; |
| 111 | } |
| 112 | |
| 113 | if ( $function ) { |
| 114 | $entry .= ' | FN: ' . $function; |
| 115 | } |
| 116 | |
| 117 | $entry .= ' | '; |
| 118 | |
| 119 | if ( $label ) { |
| 120 | $entry .= $label . ' = '; |
| 121 | } |
| 122 | |
| 123 | if ( is_array( $data ) || is_object( $data ) ) { |
| 124 | $entry .= print_r( $data, true ); |
| 125 | } elseif ( is_bool( $data ) ) { |
| 126 | $entry .= ( $entry ? 'true' : 'false' ) . PHP_EOL; |
| 127 | } else { |
| 128 | $entry .= $data . PHP_EOL; |
| 129 | } |
| 130 | |
| 131 | //$entry .= PHP_EOL; |
| 132 | |
| 133 | error_log( $entry, 3, $this->get_log_file_path() ); |
| 134 | |
| 135 | set_transient( $this->action, true ); |
| 136 | |
| 137 | } |
| 138 | |
| 139 | } |
| 140 | |
| 141 | endif; |
| 142 |