Completed
Push — master ( a2de1a...74708a )
by Zack
10:53
created

GravityView_Logging::enable_gform_logging()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 3 and the first side effect is on line 126.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
11
12
		add_action( 'gravityview_log_debug', array( $this, 'log_debug'), 10, 2 );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
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 3
	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 3
		if( ob_get_level() > 0 ) {
76 3
			$function = 'var_export';
77
		} else {
78
			$function = 'print_r';
79
		}
80
81 3
		return $function;
82
	}
83
84 3
	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 3
		$function = self::get_print_function();
87
88
		$notice = array(
89 3
			'message' => $function( $message, true ),
90 3
			'data' => $data,
91
		);
92
93 3
		if( !in_array( $notice, self::$notices ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
94 3
			self::$notices[] = $notice;
95
		}
96
97 3
		if ( class_exists("GFLogging") ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
Coding Style Comprehensibility introduced by
The string literal GFLogging does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
98
			GFLogging::include_logger();
99
	        GFLogging::log_message( 'gravityview', $function( $message, true ) . $function($data, true), KLogger::DEBUG );
100
	    }
101 3
	}
102
103
	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
		$function = self::get_print_function();
106
107
		$data['backtrace'] = function_exists('wp_debug_backtrace_summary') ? wp_debug_backtrace_summary( null, 3 ) : '';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
108
109
		$error = array(
110
			'message' => $message,
111
			'data' => $data,
112
		);
113
114
		if( !in_array( $error, self::$errors ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
115
			self::$errors[] = $error;
116
		}
117
118
		if ( class_exists("GFLogging") ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
Coding Style Comprehensibility introduced by
The string literal GFLogging does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
119
		    GFLogging::include_logger();
120
		    GFLogging::log_message( 'gravityview', $function ( $message, true ) . $function ($data, true), KLogger::ERROR );
121
		}
122
	}
123
124
}
125
126
new GravityView_Logging;