PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.0
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.0
1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 1.8.0 All 210 releases
charitable / includes / admin / campaign-builder / debug.php

debug.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.0, at includes/admin/campaign-builder/debug.php

242 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helper logging and debug functions within the campaign builder.
4 *
5 * @package Charitable
6 * @author David Bisset
7 * @copyright Copyright (c) 2023, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.8.0
10 * @version 1.8.0
11 */
12
13 use Charitable\Logger\Log;
14
15 /**
16 * Check whether plugin works in a debug mode.
17 *
18 * @since 1.8.0
19 *
20 * @return bool
21 */
22 function charitable_builder_debug() {
23
24 $debug = false;
25
26 if ( ( ( defined( 'CHARITABLE_BUILDER_DEBUG' ) && true === CHARITABLE_BUILDER_DEBUG ) || ( defined( 'CHARITABLE_DEBUG' ) && true === CHARITABLE_DEBUG ) ) && is_super_admin() ) {
27 $debug = true;
28 }
29
30 return apply_filters( 'charitable_builder_debug', $debug );
31 }
32
33 /**
34 * Helper function to display debug data.
35 *
36 * @since 1.8.0
37 *
38 * @param mixed $data What to dump, can be any type.
39 * @param bool $echo_output Whether to print or return. Default is to print.
40 *
41 * @return string|void
42 */
43 function charitable_builder_debug_data( $data, $echo_output = true ) {
44
45 if ( ! charitable_builder_debug() ) {
46 return;
47 }
48
49 if ( is_array( $data ) || is_object( $data ) ) {
50 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
51 $data = print_r( $data, true );
52 }
53
54 $output = sprintf(
55 '<style>
56 .charitable-debug {
57 line-height: 0;
58 }
59 .charitable-debug textarea {
60 background: #f6f7f7 !important;
61 margin: 20px 0 0 0;
62 width: 100%%;
63 height: 3500px;
64 font-size: 12px;
65 font-family: Consolas, Menlo, Monaco, monospace;
66 direction: ltr;
67 unicode-bidi: embed;
68 line-height: 1.4;
69 padding: 10px;
70 border-radius: 0;
71 border-color: #c3c4c7;
72 }
73 .postbox .charitable-debug {
74 padding-top: 12px;
75 }
76 .postbox .charitable-debug:first-of-type {
77 padding-top: 6px;
78 }
79 .postbox .charitable-debug textarea {
80 margin-top: 0 !important;
81 }
82 </style>
83 <div class="charitable-debug">
84 <textarea readonly>=================== CHARITABLE DEBUG ===================%s</textarea>
85 </div>',
86 "\n\n" . $data
87 );
88
89 /**
90 * Allow developers to determine whether the debug data should be displayed.
91 * Works only in debug mode (`CHARITABLE_DEBUG` constant is `true`).
92 *
93 * @since 1.8.0
94 *
95 * @param bool $allow_display True by default.
96 */
97 $allow_display = apply_filters( 'charitable_debug_data_allow_display', true );
98
99 if ( $echo_output && $allow_display ) {
100 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
101 echo $output;
102 } else {
103 return $output;
104 }
105 }
106
107 /**
108 * Helper function that returns debug info via ajax/json.
109 *
110 * @since 1.8.0
111 *
112 * @return string|void
113 */
114 function charitable_update_debug_window_ajax() {
115
116 // data to post is likely be submitted form data from an ajax request.
117 $data = false;
118
119 if ( isset( $_POST['data'] ) && is_string( $_POST['data'] ) ) { // @codingStandardsIgnoreLine
120 $data = json_decode( html_entity_decode( stripslashes( $_POST['data'] ) ) ); // @codingStandardsIgnoreLine
121 } elseif ( isset( $_POST['data'] ) && is_array( $_POST['data'] ) ) { // @codingStandardsIgnoreLine
122 $data = print_r( $_POST['data'], true ); // @codingStandardsIgnoreLine
123 }
124
125 $output = sprintf(
126 '<textarea readonly>=================== ' . esc_html__( 'CHARITABLE', 'charitable' ) . ' DEBUG ===================%s</textarea>',
127 "\n\n" . $data
128 );
129
130 wp_send_json_success( $output );
131 exit;
132 }
133 add_action( 'wp_ajax_charitable_update_debug_window_ajax', 'charitable_update_debug_window_ajax' );
134 add_action( 'wp_ajax_nopriv_charitable_update_debug_window_ajax', 'charitable_update_debug_window_ajax' );
135
136 /**
137 * Log helper.
138 *
139 * @since 1.8.0
140 *
141 * @param string $title Title of a log message.
142 * @param mixed $message Content of a log message.
143 * @param array $args Expected keys: form_id, meta, parent.
144 */
145 function charitable_builder_log( $title = '', $message = '', $args = [] ) {
146
147 // Skip if logs disabled in Tools -> Logs.
148 if ( ! charitable_setting( 'logs-enable', false ) ) {
149 return;
150 }
151
152 // Require log title.
153 if ( empty( $title ) ) {
154 return;
155 }
156
157 /**
158 * Compare error levels to determine if we should log.
159 * Current supported levels:
160 * - Conditional Logic (conditional_logic)
161 * - Entries (entry)
162 * - Errors (error)
163 * - Payments (payment)
164 * - Providers (provider)
165 * - Security (security)
166 * - Spam (spam)
167 * - Log (log)
168 */
169 $types = ! empty( $args['type'] ) ? (array) $args['type'] : [ 'error' ];
170
171 // Skip invalid logs types.
172 $log_types = Log::get_log_types();
173
174 foreach ( $types as $key => $type ) {
175 if ( ! isset( $log_types[ $type ] ) ) {
176 unset( $types[ $key ] );
177 }
178 }
179
180 if ( empty( $types ) ) {
181 return;
182 }
183
184 // Make arrays and objects look nice.
185 if ( is_array( $message ) || is_object( $message ) ) {
186 $message = '<pre>' . print_r( $message, true ) . '</pre>'; // phpcs:ignore
187 }
188
189 // Filter logs types from Tools -> Logs page.
190 $logs_types = charitable_setting( 'logs-types', false );
191
192 if ( $logs_types && empty( array_intersect( $logs_types, $types ) ) ) {
193 return;
194 }
195
196 // Filter user roles from Tools -> Logs page.
197 $current_user = function_exists( 'wp_get_current_user' ) ? wp_get_current_user() : null;
198 $current_user_id = $current_user ? $current_user->ID : 0;
199 $current_user_roles = $current_user ? $current_user->roles : [];
200 $logs_user_roles = charitable_setting( 'logs-user-roles', false );
201
202 if ( $logs_user_roles && empty( array_intersect( $logs_user_roles, $current_user_roles ) ) ) {
203 return;
204 }
205
206 // Filter logs users from Tools -> Logs page.
207 $logs_users = charitable_setting( 'logs-users', false );
208
209 if ( $logs_users && ! in_array( $current_user_id, $logs_users, true ) ) {
210 return;
211 }
212
213 $log = charitable()->get( 'log' );
214
215 if ( ! method_exists( $log, 'add' ) ) {
216 return;
217 }
218 // Create log entry.
219 $log->add(
220 $title,
221 $message,
222 $types,
223 isset( $args['form_id'] ) ? absint( $args['form_id'] ) : 0,
224 isset( $args['parent'] ) ? absint( $args['parent'] ) : 0,
225 $current_user_id
226 );
227 }
228
229 /**
230 * Wrapper for set_time_limit to see if it is enabled.
231 *
232 * @since 1.8.0
233 *
234 * @param int $limit Time limit.
235 */
236 function charitable_set_time_limit( $limit = 0 ) {
237
238 if ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved
239 @set_time_limit( $limit ); // @codingStandardsIgnoreLine
240 }
241 }
242