Completed
Push — master ( 18187e...a1257b )
by Zack
19:22 queued 17:03
created

GravityView_Logging::log_debug()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 3
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
final class GravityView_Logging {
4
5
	private static $errors = array();
6
	private static $notices = array();
7
8
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
9
10
		add_action( 'gravityview_log_error', array( $this, 'log_error'), 10, 2 );
11
12
		add_action( 'gravityview_log_debug', array( $this, 'log_debug'), 10, 2 );
13
14
		// Enable debug with Gravity Forms Logging Add-on
15
	    add_filter( 'gform_logging_supported', array( $this, 'enable_gform_logging' ) );
16
17
	    // Load Debug Bar integration
18
	    add_filter( 'debug_bar_panels', array( $this, 'add_debug_bar' ) );
19
20
	}
21
22
	/**
23
	 * Add integration with the Debug Bar plugin. It's awesome.
24
	 *
25
	 * @see http://wordpress.org/plugins/debug-bar/
26
	 */
27
	public function add_debug_bar( $panels ) {
28
29
		if ( ! class_exists( 'Debug_Bar_Panel' ) ) {
30
			return;
31
		}
32
33
		if ( ! class_exists( 'GravityView_Debug_Bar' ) ) {
34
			include_once( GRAVITYVIEW_DIR . 'includes/class-debug-bar.php' );
35
		}
36
37
		$panels[] = new GravityView_Debug_Bar;
38
39
		return $panels;
40
	}
41
42
	/**
43
	 * Enables debug with Gravity Forms logging add-on
44
	 * @param array $supported_plugins List of plugins
45
	 */
46
	public function enable_gform_logging( $supported_plugins ) {
47
	    $supported_plugins['gravityview'] = 'GravityView';
48
	    return $supported_plugins;
49
	}
50
51
	/**
52
	 * @static
53
	 * @return array Array of notices (with `message`, `data`, and `backtrace` keys), if any
54
	 */
55
	public static function get_notices() {
56
		return self::$notices;
57
	}
58
59
	/**
60
	 * @static
61
	 * @return array Array of errors (with `message`, `data`, and `backtrace` keys), if any
62
	 */
63
	public static function get_errors() {
64
		return self::$errors;
65
	}
66
67
	/**
68
	 * Get the name of the function to print messages for debugging
69
	 *
70
	 * This is necessary because `ob_start()` doesn't allow `print_r()` inside it.
71
	 *
72
	 * @return string "print_r" or "var_export"
73
	 */
74 199
	static function get_print_function() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
75 199
		if( ob_get_level() > 0 ) {
76 199
			$function = 'var_export';
77
		} else {
78
			$function = 'print_r';
79
		}
80
81 199
		return $function;
82
	}
83
84 196
	static function log_debug( $message = '', $data = null ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
85
86 196
		$function = self::get_print_function();
87
88
		$notice = array(
89 196
			'message' => $function( $message, true ),
90 196
			'data' => $data,
91
		);
92
93 196
		if( !in_array( $notice, self::$notices ) ) {
94 194
			self::$notices[] = $notice;
95
		}
96
97 196
		if ( class_exists("GFLogging") ) {
98 196
			GFLogging::include_logger();
99 196
	        GFLogging::log_message( 'gravityview', $function( $message, true ) . $function($data, true), KLogger::DEBUG );
100
	    }
101 196
	}
102
103 32
	static function log_error( $message = '', $data = null  ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
104
105 32
		$function = self::get_print_function();
106
107
		$error = array(
108 32
			'message' => $message,
109 32
			'data' => $data,
110 32
			'backtrace' => function_exists( 'wp_debug_backtrace_summary' ) ? wp_debug_backtrace_summary( null, 3 ) : '',
111
		);
112
113 32
		if( !in_array( $error, self::$errors ) ) {
114 32
			self::$errors[] = $error;
115
		}
116
117 32
		if ( class_exists("GFLogging") ) {
118 32
		    GFLogging::include_logger();
119 32
		    GFLogging::log_message( 'gravityview', $function ( $message, true ) . $function ( $error, true), KLogger::ERROR );
120
		}
121 32
	}
122
123
	/**
124
	 * Check whether a plugin is active
125
	 *
126
	 * @param string $plugin
127
	 *
128
	 * @since 2.5.1
129
	 *
130
	 * @param string $plugin The slug for the plugin used when setting up logging (default: "gravityview")
131
	 *
132
	 * @return bool
133
	 */
134
	static function is_logging_active( $plugin = 'gravityview' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
135
136
		if( ! class_exists( 'GFLogging') ) {
137
			return false;
138
		}
139
140
		GFLogging::include_logger();
141
142
		$plugin_setting = GFLogging::get_instance()->get_plugin_setting( $plugin );
143
144
		return ! rgempty( 'enable', $plugin_setting );
145
	}
146
147
}
148
149
new GravityView_Logging;
150