|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Error Tracking |
|
4
|
|
|
* |
|
5
|
|
|
* @package Give |
|
6
|
|
|
* @subpackage Functions/Errors |
|
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
|
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
|
9
|
|
|
* @since 1.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get Errors |
|
19
|
|
|
* |
|
20
|
|
|
* Retrieves all error messages stored during the checkout process. |
|
21
|
|
|
* If errors exist, they are returned. |
|
22
|
|
|
* |
|
23
|
|
|
* @since 1.0 |
|
24
|
|
|
* @uses Give_Session::get() |
|
25
|
|
|
* @return mixed array if errors are present, false if none found |
|
26
|
|
|
*/ |
|
27
|
|
|
function give_get_errors() { |
|
28
|
|
|
return Give()->session->get( 'give_errors' ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Set Error |
|
33
|
|
|
* |
|
34
|
|
|
* Stores an error in a session var. |
|
35
|
|
|
* |
|
36
|
|
|
* @since 1.0 |
|
37
|
|
|
* @uses Give_Session::get() |
|
38
|
|
|
* |
|
39
|
|
|
* @param int $error_id ID of the error being set. |
|
40
|
|
|
* @param string $error_message Message to store with the error. |
|
41
|
|
|
* |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
function give_set_error( $error_id, $error_message ) { |
|
45
|
|
|
$errors = give_get_errors(); |
|
46
|
|
|
if ( ! $errors ) { |
|
47
|
|
|
$errors = array(); |
|
48
|
|
|
} |
|
49
|
|
|
$errors[ $error_id ] = $error_message; |
|
50
|
|
|
Give()->session->set( 'give_errors', $errors ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Clears all stored errors. |
|
55
|
|
|
* |
|
56
|
|
|
* @since 1.0 |
|
57
|
|
|
* @uses Give_Session::set() |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
function give_clear_errors() { |
|
61
|
|
|
Give()->session->set( 'give_errors', null ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Removes (unsets) a stored error |
|
66
|
|
|
* |
|
67
|
|
|
* @since 1.0 |
|
68
|
|
|
* @uses Give_Session::set() |
|
69
|
|
|
* |
|
70
|
|
|
* @param int $error_id ID of the error being set. |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
|
function give_unset_error( $error_id ) { |
|
75
|
|
|
$errors = give_get_errors(); |
|
76
|
|
|
if ( $errors ) { |
|
77
|
|
|
unset( $errors[ $error_id ] ); |
|
78
|
|
|
Give()->session->set( 'give_errors', $errors ); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Register die handler for give_die() |
|
84
|
|
|
* |
|
85
|
|
|
* @since 1.0 |
|
86
|
|
|
* @return string |
|
|
|
|
|
|
87
|
|
|
*/ |
|
88
|
|
|
function _give_die_handler() { |
|
89
|
|
|
if ( defined( 'GIVE_UNIT_TESTS' ) ) { |
|
90
|
|
|
return '_give_die_handler'; |
|
91
|
|
|
} else { |
|
92
|
|
|
die(); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Wrapper function for wp_die(). This function adds filters for wp_die() which |
|
98
|
|
|
* kills execution of the script using wp_die(). This allows us to then to work |
|
99
|
|
|
* with functions using give_die() in the unit tests. |
|
100
|
|
|
* |
|
101
|
|
|
* @since 1.0 |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $message Message to store with the error. |
|
104
|
|
|
* @param string $title Error title. |
|
105
|
|
|
* @param int $status HTTP status code.. |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
function give_die( $message = '', $title = '', $status = 400 ) { |
|
110
|
|
|
add_filter( 'wp_die_ajax_handler', '_give_die_handler', 10, 3 ); |
|
111
|
|
|
add_filter( 'wp_die_handler', '_give_die_handler', 10, 3 ); |
|
112
|
|
|
wp_die( $message, $title, array( 'response' => $status ) ); |
|
113
|
|
|
} |
|
114
|
|
|
|
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.