Passed
Push — master ( 46e064...13ee52 )
by Brian
04:28
created

wpinv_error_log()   B

Complexity

Conditions 7
Paths 33

Size

Total Lines 33
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 33
rs 8.8333
cc 7
nc 33
nop 5
1
<?php
2
/**
3
 * Contains error functions.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
 
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Returns the errors as html
13
 *
14
 * @param clear whether or not to clear the errors.
0 ignored issues
show
Bug introduced by
The type whether was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
 * @since  1.0.19
16
 */
17
function getpaid_get_errors_html( $clear = true ) {
18
19
    $errors = '';
20
    foreach ( wpinv_get_errors() as $error ) {
21
        $type     = 'error';
22
23
        if ( is_array( $error ) ) {
24
            $type  = $error['type'];
25
            $error = $error['text'];
26
        }
27
28
        $errors .= aui()->alert(
29
            array(
30
                'content'     => wp_kses_post( $error ),
31
                'type'        => $type,
32
            )
33
        );
34
35
    }
36
37
    if ( $clear ){
38
        wpinv_clear_errors();
39
    }
40
41
    return $errors;
42
43
}
44
45
/**
46
 * Prints (then clears) all available errors.
47
 */
48
function wpinv_print_errors() {
49
    echo getpaid_get_errors_html();
50
}
51
52
/**
53
 * Returns all available errors.
54
 * 
55
 * @return array
56
 */
57
function wpinv_get_errors() {
58
    $errors = getpaid_session()->get( 'wpinv_errors' );
59
    return is_array( $errors ) ? $errors : array();
60
}
61
62
/**
63
 * Adds an error to the list of errors.
64
 * 
65
 * @param string $error_id The error id.
66
 * @param string $error_message The error message.
67
 * @param string $type Either error, info, warning, primary, dark, light or success.
68
 */
69
function wpinv_set_error( $error_id, $error_message, $type = 'error' ) {
70
71
    $errors              = wpinv_get_errors();
72
    $errors[ $error_id ] = array(
73
        'type' =>  $type,
74
        'text' =>  $error_message,
75
    );
76
77
    getpaid_session()->set( 'wpinv_errors', $errors );
78
}
79
80
/**
81
 * Checks if there is an error.
82
 * 
83
 */
84
function wpinv_has_errors() {
85
    return count( wpinv_get_errors() ) > 0;
86
}
87
88
/**
89
 * Clears all error.
90
 * 
91
 */
92
function wpinv_clear_errors() {
93
    getpaid_session()->set( 'wpinv_errors', null );
94
}
95
96
/**
97
 * Clears a single error.
98
 * 
99
 */
100
function wpinv_unset_error( $error_id ) {
101
    $errors = wpinv_get_errors();
102
103
    if ( isset( $errors[ $error_id ] ) ) {
104
        unset( $errors[ $error_id ] );
105
    }
106
107
    getpaid_session()->set( 'wpinv_errors', $errors );
108
}
109
110
/**
111
 * Wrapper for _doing_it_wrong().
112
 *
113
 * @since  1.0.19
114
 * @param string $function Function used.
115
 * @param string $message Message to log.
116
 * @param string $version Version the message was added in.
117
 */
118
function getpaid_doing_it_wrong( $function, $message, $version ) {
119
120
	$message .= ' Backtrace: ' . wp_debug_backtrace_summary();
121
122
	if ( is_ajax() || defined( 'REST_REQUEST' ) ) {
0 ignored issues
show
Bug introduced by
The function is_ajax was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
	if ( /** @scrutinizer ignore-call */ is_ajax() || defined( 'REST_REQUEST' ) ) {
Loading history...
123
		do_action( 'doing_it_wrong_run', $function, $message, $version );
124
		error_log( "{$function} was called incorrectly. {$message}. This message was added in version {$version}." );
125
	} else {
126
		_doing_it_wrong( $function, $message, $version );
127
	}
128
129
}
130
131
/**
132
 * Logs a debugging message.
133
 * 
134
 * @param string $log The message to log.
135
 * @param string $title The title of the message.
136
 * @param string $file The file from which the error was logged.
137
 * @param string $line The line that contains the error.
138
 * @param bool $exit Whether or not to exit function execution.
139
 */
140
function wpinv_error_log( $log, $title = '', $file = '', $line = '', $exit = false ) {
141
    
142
    if ( true === apply_filters( 'wpinv_log_errors', true ) ) {
143
144
        // Ensure the log is a scalar.
145
        if ( ! is_scalar( $log ) ) {
0 ignored issues
show
introduced by
The condition is_scalar($log) is always true.
Loading history...
146
            $log = print_r( $log, true );
147
        }
148
149
        // Add title.
150
        if ( ! empty( $title ) ) {
151
            $log  = $title . ' ' . trim( $log );
152
        }
153
154
        // Add the file to the label.
155
        if ( ! empty( $file ) ) {
156
            $log .= ' in ' . $file;
157
        }
158
159
        // Add the line number to the label.
160
        if ( ! empty( $line ) ) {
161
            $log .= ' on line ' . $line;
162
        }
163
164
        // Log the message.
165
        error_log( trim ( $log ) );
166
167
        // ... and a backtrace.
168
        error_log( 'Backtrace ' . wp_debug_backtrace_summary() );
169
170
        // Maybe exit.
171
        if ( $exit ) {
172
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
173
        }
174
175
    }
176
177
}
178