Completed
Push — develop ( e9495e...98d972 )
by Zack
17:18 queued 11s
created

GravityView_Logging::add_debug_bar()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 14
ccs 0
cts 7
cp 0
crap 12
rs 9.7998
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 197
	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 197
		if( ob_get_level() > 0 ) {
76 197
			$function = 'var_export';
77
		} else {
78
			$function = 'print_r';
79
		}
80
81 197
		return $function;
82
	}
83
84 194
	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 194
		$function = self::get_print_function();
87
88
		$notice = array(
89 194
			'message' => $function( $message, true ),
90 194
			'data' => $data,
91
		);
92
93 194
		if( !in_array( $notice, self::$notices ) ) {
94 192
			self::$notices[] = $notice;
95
		}
96
97 194
		if ( class_exists("GFLogging") ) {
98 194
			GFLogging::include_logger();
99 194
	        GFLogging::log_message( 'gravityview', $function( $message, true ) . $function($data, true), KLogger::DEBUG );
100
	    }
101 194
	}
102
103 35
	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 35
		$function = self::get_print_function();
106
107
		$error = array(
108 35
			'message' => $message,
109 35
			'data' => $data,
110 35
			'backtrace' => function_exists( 'wp_debug_backtrace_summary' ) ? wp_debug_backtrace_summary( null, 3 ) : '',
111
		);
112
113 35
		if( !in_array( $error, self::$errors ) ) {
114 35
			self::$errors[] = $error;
115
		}
116
117 35
		if ( class_exists("GFLogging") ) {
118 35
		    GFLogging::include_logger();
119 35
		    GFLogging::log_message( 'gravityview', $function ( $message, true ) . $function ( $error, true), KLogger::ERROR );
120
		}
121 35
	}
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