PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.12.3
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.12.3
1.8.13 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 All 211 releases
charitable / includes / logger / class-charitable-log-record.php

class-charitable-log-record.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.12.3, at includes/logger/class-charitable-log-record.php

202 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable Log Record class.
4 *
5 * Value object for a single log entry.
6 *
7 * @package Charitable/Classes/Charitable_Log_Record
8 * @author David Bisset
9 * @copyright Copyright (c) 2023, WP Charitable LLC
10 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
11 * @since 1.8.11
12 */
13
14 // Exit if accessed directly.
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 if ( ! class_exists( 'Charitable_Log_Record' ) ) :
20
21 /**
22 * Charitable_Log_Record
23 *
24 * @since 1.8.11
25 */
26 class Charitable_Log_Record {
27
28 /**
29 * Record data.
30 *
31 * @since 1.8.11
32 *
33 * @var object
34 */
35 private $data;
36
37 /**
38 * Constructor.
39 *
40 * @since 1.8.11
41 *
42 * @param object $row Database row object from $wpdb->get_row().
43 */
44 public function __construct( $row ) {
45 $this->data = $row;
46 }
47
48 /**
49 * Create a new record from input data.
50 *
51 * @since 1.8.11
52 *
53 * @param string $title Log title.
54 * @param string|array $message Log message.
55 * @param array $args Additional arguments.
56 * @return array Record data ready for insertion.
57 */
58 public static function create( $title, $message, $args = array() ) {
59 $defaults = array(
60 'types' => 'log',
61 'level' => 'info',
62 'source' => 'core',
63 'campaign_id' => 0,
64 'donation_id' => 0,
65 'donor_id' => 0,
66 'user_id' => 0,
67 'object_id' => 0,
68 'object_type' => '',
69 );
70
71 $args = wp_parse_args( $args, $defaults );
72
73 // Sanitize title.
74 $title = sanitize_text_field( $title );
75
76 // Format message — arrays/objects become <pre> HTML.
77 if ( is_array( $message ) || is_object( $message ) ) {
78 $message = '<pre>' . esc_html( print_r( $message, true ) ) . '</pre>'; // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
79 } else {
80 $message = wp_kses(
81 (string) $message,
82 array(
83 'pre' => array(),
84 'code' => array(),
85 'strong' => array(),
86 'em' => array(),
87 'br' => array(),
88 'a' => array(
89 'href' => array(),
90 'target' => array(),
91 ),
92 )
93 );
94 }
95
96 // Normalize types to comma-separated string.
97 $types = $args['types'];
98 if ( is_array( $types ) ) {
99 $types = implode( ',', array_map( 'sanitize_key', $types ) );
100 } else {
101 $types = sanitize_key( $types );
102 }
103
104 return array(
105 'title' => $title,
106 'message' => $message,
107 'types' => $types,
108 'level' => sanitize_key( $args['level'] ),
109 'source' => sanitize_key( $args['source'] ),
110 'create_at' => gmdate( 'Y-m-d H:i:s' ),
111 'campaign_id' => absint( $args['campaign_id'] ),
112 'donation_id' => absint( $args['donation_id'] ),
113 'donor_id' => absint( $args['donor_id'] ),
114 'user_id' => absint( $args['user_id'] ),
115 'object_id' => absint( $args['object_id'] ),
116 'object_type' => sanitize_key( $args['object_type'] ),
117 );
118 }
119
120 /**
121 * Get a property from the record.
122 *
123 * @since 1.8.11
124 *
125 * @param string $key Property name.
126 * @return mixed
127 */
128 public function __get( $key ) {
129 return isset( $this->data->$key ) ? $this->data->$key : null;
130 }
131
132 /**
133 * Check if a property is set.
134 *
135 * @since 1.8.11
136 *
137 * @param string $key Property name.
138 * @return bool
139 */
140 public function __isset( $key ) {
141 return isset( $this->data->$key );
142 }
143
144 /**
145 * Get the record types.
146 *
147 * @since 1.8.11
148 *
149 * @param string $view 'key' for slug array, 'label' for translated labels.
150 * @return array
151 */
152 public function get_types( $view = 'key' ) {
153 $types = array_filter( explode( ',', $this->data->types ) );
154
155 if ( 'label' === $view ) {
156 $all_types = Charitable_Log::get_log_types();
157 $labels = array();
158 foreach ( $types as $type ) {
159 $labels[] = isset( $all_types[ $type ] ) ? $all_types[ $type ] : $type;
160 }
161 return $labels;
162 }
163
164 return $types;
165 }
166
167 /**
168 * Get the formatted date.
169 *
170 * @since 1.8.11
171 *
172 * @param string $format 'short', 'full', or 'sql'.
173 * @return string
174 */
175 public function get_date( $format = 'short' ) {
176 $timestamp = strtotime( $this->data->create_at );
177
178 switch ( $format ) {
179 case 'full':
180 return date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $timestamp );
181 case 'sql':
182 return $this->data->create_at;
183 case 'short':
184 default:
185 return date_i18n( 'M j, Y g:i a', $timestamp );
186 }
187 }
188
189 /**
190 * Convert to array for batch insert.
191 *
192 * @since 1.8.11
193 *
194 * @return array
195 */
196 public function to_array() {
197 return (array) $this->data;
198 }
199 }
200
201 endif;
202