PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.12
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.12
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.12, at includes/admin/campaign-builder/debug.php

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