1: <?php
2: /**
3: * Error Tracking
4: *
5: * @package EPL
6: * @subpackage Functions/ErrorTracking
7: * @copyright Copyright (c) 2020, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * Print Errors
19: *
20: * @since 3.0
21: * @uses epl_get_errors()
22: * @uses epl_clear_errors()
23: * @return void
24: */
25: function epl_print_errors() {
26: $errors = epl_get_errors();
27: if ( $errors ) {
28: $classes = apply_filters(
29: 'epl_error_class',
30: array(
31: 'epl_errors',
32: 'epl-alert',
33: 'epl-alert-error',
34: )
35: );
36: echo '<div class="' . wp_kses_post( implode( ' ', $classes ) ) . '">';
37: // Loop error codes and display errors.
38: foreach ( $errors as $error_id => $error ) {
39: echo '<p class="epl_error" id="epl_error_' . esc_attr( $error_id ) . '"><strong>' . esc_html__( 'Error', 'easy-property-listings' ) . '</strong>: ' . wp_kses_post( $error ) . '</p>';
40: }
41: echo '</div>';
42: epl_clear_errors();
43: }
44: }
45:
46: /**
47: * Get Errors
48: *
49: * If errors exist, they are returned.
50: *
51: * @since 3.0
52: * @since 3.1.15 switched to EPL Session class for session handling.
53: * @return mixed array if errors are present, false if none found
54: */
55: function epl_get_errors() {
56: $errors = EPL()->session->get( 'epl_errors' );
57: $errors = apply_filters( 'epl_errors', $errors );
58: return $errors;
59: }
60:
61: /**
62: * Set Error
63: *
64: * Stores an error in a session var.
65: *
66: * @since 3.0
67: * @since 3.1.15 switched to EPL Session class for session handling.
68: * @param int $error_id ID of the error being set.
69: * @param string $error_message Message to store with the error.
70: * @return void
71: */
72: function epl_set_error( $error_id, $error_message ) {
73: $errors = epl_get_errors();
74: if ( ! $errors ) {
75: $errors = array();
76: }
77: $errors[ $error_id ] = $error_message;
78: EPL()->session->set( 'epl_errors', $errors );
79: }
80:
81: /**
82: * Clears all stored errors.
83: *
84: * @since 3.0
85: * @since 3.1.15 switched to EPL Session class for session handling.
86: * @return void
87: */
88: function epl_clear_errors() {
89: EPL()->session->set( 'epl_errors', null );
90: }
91:
92: /**
93: * Removes (unsets) a stored error
94: *
95: * @since 3.0
96: * @since 3.1.15 switched to EPL Session class for session handling.
97: * @param int $error_id ID of the error being set.
98: */
99: function epl_unset_error( $error_id ) {
100: $errors = epl_get_errors();
101: if ( $errors ) {
102: unset( $errors[ $error_id ] );
103: EPL()->session->set( 'epl_errors', $errors );
104: }
105: }
106:
107: /**
108: * Pretty var dump
109: *
110: * @param string $variable ID of the variable.
111: * @param bool $terminate Die or not.
112: * @since 3.0
113: */
114: function epl_var_dump( $variable, $terminate = false ) {
115: echo '<pre class="epl_var_dump">';
116: var_dump( $variable ); //phpcs:ignore
117: echo '</pre>';
118: if ( $terminate ) {
119: die();
120: }
121: }
122:
123: /**
124: * Pretty print_r
125: *
126: * @param array $variable Array to be printed.
127: * @param bool $terminate Die or not.
128: * @since 3.0
129: */
130: function epl_print_r( $variable, $terminate = false ) {
131: echo '<pre class="epl_print_r">';
132: print_r( $variable );//phpcs:ignore
133: echo '</pre>';
134: if ( $terminate ) {
135: die();
136: }
137: }
138: