| 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 |
// data to post is likely be submitted form data from an ajax request. |
| 123 |
$data = false; |
| 124 |
|
| 125 |
if ( isset( $_POST['data'] ) && is_string( $_POST['data'] ) ) { // @codingStandardsIgnoreLine |
| 126 |
$data = json_decode( html_entity_decode( stripslashes( $_POST['data'] ) ) ); // @codingStandardsIgnoreLine |
| 127 |
} elseif ( isset( $_POST['data'] ) && is_array( $_POST['data'] ) ) { // @codingStandardsIgnoreLine |
| 128 |
$data = print_r( $_POST['data'], true ); // @codingStandardsIgnoreLine |
| 129 |
} |
| 130 |
|
| 131 |
$output = sprintf( |
| 132 |
'<textarea readonly>=================== ' . esc_html__( 'CHARITABLE', 'charitable' ) . ' DEBUG ===================%s</textarea>', |
| 133 |
"\n\n" . $data |
| 134 |
); |
| 135 |
|
| 136 |
wp_send_json_success( $output ); |
| 137 |
exit; |
| 138 |
} |
| 139 |
add_action( 'wp_ajax_charitable_update_debug_window_ajax', 'charitable_update_debug_window_ajax' ); |
| 140 |
add_action( 'wp_ajax_nopriv_charitable_update_debug_window_ajax', 'charitable_update_debug_window_ajax' ); |
| 141 |
|
| 142 |
/** |
| 143 |
* Log helper. |
| 144 |
* |
| 145 |
* @since 1.8.0 |
| 146 |
* |
| 147 |
* @param string $title Title of a log message. |
| 148 |
* @param mixed $message Content of a log message. |
| 149 |
* @param array $args Expected keys: form_id, meta, parent. |
| 150 |
*/ |
| 151 |
function charitable_builder_log( $title = '', $message = '', $args = [] ) { |
| 152 |
|
| 153 |
// Skip if logs disabled in Tools -> Logs. |
| 154 |
if ( ! charitable_setting( 'logs-enable', false ) ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
// Require log title. |
| 159 |
if ( empty( $title ) ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Compare error levels to determine if we should log. |
| 165 |
* Current supported levels: |
| 166 |
* - Conditional Logic (conditional_logic) |
| 167 |
* - Entries (entry) |
| 168 |
* - Errors (error) |
| 169 |
* - Payments (payment) |
| 170 |
* - Providers (provider) |
| 171 |
* - Security (security) |
| 172 |
* - Spam (spam) |
| 173 |
* - Log (log) |
| 174 |
*/ |
| 175 |
$types = ! empty( $args['type'] ) ? (array) $args['type'] : [ 'error' ]; |
| 176 |
|
| 177 |
// Skip invalid logs types. |
| 178 |
$log_types = Log::get_log_types(); |
| 179 |
|
| 180 |
foreach ( $types as $key => $type ) { |
| 181 |
if ( ! isset( $log_types[ $type ] ) ) { |
| 182 |
unset( $types[ $key ] ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if ( empty( $types ) ) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
// Make arrays and objects look nice. |
| 191 |
if ( is_array( $message ) || is_object( $message ) ) { |
| 192 |
$message = '<pre>' . print_r( $message, true ) . '</pre>'; // phpcs:ignore |
| 193 |
} |
| 194 |
|
| 195 |
// Filter logs types from Tools -> Logs page. |
| 196 |
$logs_types = charitable_setting( 'logs-types', false ); |
| 197 |
|
| 198 |
if ( $logs_types && empty( array_intersect( $logs_types, $types ) ) ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
// Filter user roles from Tools -> Logs page. |
| 203 |
$current_user = function_exists( 'wp_get_current_user' ) ? wp_get_current_user() : null; |
| 204 |
$current_user_id = $current_user ? $current_user->ID : 0; |
| 205 |
$current_user_roles = $current_user ? $current_user->roles : []; |
| 206 |
$logs_user_roles = charitable_setting( 'logs-user-roles', false ); |
| 207 |
|
| 208 |
if ( $logs_user_roles && empty( array_intersect( $logs_user_roles, $current_user_roles ) ) ) { |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
// Filter logs users from Tools -> Logs page. |
| 213 |
$logs_users = charitable_setting( 'logs-users', false ); |
| 214 |
|
| 215 |
if ( $logs_users && ! in_array( $current_user_id, $logs_users, true ) ) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
$log = charitable()->get( 'log' ); |
| 220 |
|
| 221 |
if ( ! method_exists( $log, 'add' ) ) { |
| 222 |
return; |
| 223 |
} |
| 224 |
// Create log entry. |
| 225 |
$log->add( |
| 226 |
$title, |
| 227 |
$message, |
| 228 |
$types, |
| 229 |
isset( $args['form_id'] ) ? absint( $args['form_id'] ) : 0, |
| 230 |
isset( $args['parent'] ) ? absint( $args['parent'] ) : 0, |
| 231 |
$current_user_id |
| 232 |
); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Wrapper for set_time_limit to see if it is enabled. |
| 237 |
* |
| 238 |
* @since 1.8.0 |
| 239 |
* |
| 240 |
* @param int $limit Time limit. |
| 241 |
*/ |
| 242 |
function charitable_set_time_limit( $limit = 0 ) { |
| 243 |
|
| 244 |
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 |
| 245 |
@set_time_limit( $limit ); // @codingStandardsIgnoreLine |
| 246 |
} |
| 247 |
} |
| 248 |
|