1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Log events based on plugin settings. Extend the WP_Logging class for the purposes of Object Sync for Salesforce. |
4
|
|
|
* |
5
|
|
|
* @class Object_Sync_Sf_Logging |
6
|
|
|
* @package Object_Sync_Salesforce |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
defined( 'ABSPATH' ) || exit; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Object_Sync_Sf_Logging class. |
13
|
|
|
*/ |
14
|
|
|
class Object_Sync_Sf_Logging extends WP_Logging { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Current version of the plugin |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
public $version; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The main plugin file |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $file; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Global object of `$wpdb`, the WordPress database |
32
|
|
|
* |
33
|
|
|
* @var object |
34
|
|
|
*/ |
35
|
|
|
public $wpdb; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The plugin's slug so we can include it when necessary |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $slug; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The plugin's prefix when saving options to the database |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
public $option_prefix; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The setting value for whether logging is enabled |
53
|
|
|
* |
54
|
|
|
* @var bool |
55
|
|
|
*/ |
56
|
|
|
public $enabled; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Which statuses to log, from the settings value |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
public $statuses_to_log; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The name of the schedule to prune logs |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
public $schedule_name; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Whether the plugin is in debug mode |
74
|
|
|
* |
75
|
|
|
* @var bool |
76
|
|
|
*/ |
77
|
|
|
public $debug; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The name of the capability used when registering the logging post type. |
81
|
|
|
* |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
public $capability; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Constructor for logging class |
88
|
|
|
*/ |
89
|
|
|
public function __construct() { |
90
|
|
|
$this->version = object_sync_for_salesforce()->version; |
91
|
|
|
$this->file = object_sync_for_salesforce()->file; |
92
|
|
|
$this->wpdb = object_sync_for_salesforce()->wpdb; |
93
|
|
|
$this->slug = object_sync_for_salesforce()->slug; |
94
|
|
|
$this->option_prefix = object_sync_for_salesforce()->option_prefix; |
95
|
|
|
|
96
|
|
|
$this->enabled = filter_var( get_option( $this->option_prefix . 'enable_logging', false ), FILTER_VALIDATE_BOOLEAN ); |
|
|
|
|
97
|
|
|
$this->statuses_to_log = maybe_unserialize( get_option( $this->option_prefix . 'statuses_to_log', array() ) ); |
|
|
|
|
98
|
|
|
$this->statuses_to_log = empty( $this->statuses_to_log ) ? array() : $this->statuses_to_log; |
99
|
|
|
|
100
|
|
|
$this->schedule_name = 'wp_logging_prune_routine'; |
101
|
|
|
|
102
|
|
|
$this->capability = 'configure_salesforce'; |
103
|
|
|
|
104
|
|
|
// use the option value for whether we're in debug mode. |
105
|
|
|
$this->debug = filter_var( get_option( $this->option_prefix . 'debug_mode', false ), FILTER_VALIDATE_BOOLEAN ); |
106
|
|
|
|
107
|
|
|
add_action( 'plugins_loaded', array( $this, 'init' ) ); |
|
|
|
|
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Initialize. This creates a schedule for pruning logs, and also the custom content type |
113
|
|
|
*/ |
114
|
|
|
public function init() { |
115
|
|
|
$this->configure_debugging(); |
116
|
|
|
if ( true === $this->enabled ) { |
117
|
|
|
add_filter( 'cron_schedules', array( $this, 'add_prune_interval' ) ); |
|
|
|
|
118
|
|
|
add_filter( 'wp_log_types', array( $this, 'set_log_types' ), 10, 1 ); |
119
|
|
|
add_filter( 'wp_logging_should_we_prune', array( $this, 'set_prune_option' ), 10, 1 ); |
120
|
|
|
add_filter( 'wp_logging_prune_when', array( $this, 'set_prune_age' ), 10, 1 ); |
121
|
|
|
add_filter( 'wp_logging_prune_query_args', array( $this, 'set_prune_args' ), 10, 1 ); |
122
|
|
|
add_filter( 'wp_logging_post_type_args', array( $this, 'set_log_visibility' ), 10, 1 ); |
123
|
|
|
add_filter( 'pre_wp_unique_post_slug', array( $this, 'set_log_slug' ), 10, 5 ); |
124
|
|
|
|
125
|
|
|
// add a filter to check for other plugins that might be filtering the log screen. |
126
|
|
|
$are_logs_filtered = apply_filters( 'wp_logging_manage_logs_filtered', false ); |
|
|
|
|
127
|
|
|
add_filter( 'wp_logging_manage_logs_filtered', '__return_true' ); |
128
|
|
|
|
129
|
|
|
if ( false === $are_logs_filtered ) { |
130
|
|
|
// add a sortable Type column to the posts admin. |
131
|
|
|
add_filter( 'manage_edit-wp_log_columns', array( $this, 'type_column' ), 10, 1 ); |
132
|
|
|
add_filter( 'manage_edit-wp_log_sortable_columns', array( $this, 'sortable_columns' ), 10, 1 ); |
133
|
|
|
add_action( 'manage_wp_log_posts_custom_column', array( $this, 'type_column_content' ), 10, 2 ); |
|
|
|
|
134
|
|
|
|
135
|
|
|
// filter the log posts admin by log type. |
136
|
|
|
add_filter( 'parse_query', array( $this, 'posts_filter' ), 10, 1 ); |
137
|
|
|
add_action( 'restrict_manage_posts', array( $this, 'restrict_logs_by_type' ), 10, 1 ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// when the schedule might change. |
141
|
|
|
add_action( 'update_option_' . $this->option_prefix . 'logs_how_often_unit', array( $this, 'check_log_schedule' ), 10, 3 ); |
142
|
|
|
add_action( 'update_option_' . $this->option_prefix . 'logs_how_often_number', array( $this, 'check_log_schedule' ), 10, 3 ); |
143
|
|
|
|
144
|
|
|
$this->save_log_schedule(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Configure log settings based on debug status. |
150
|
|
|
*/ |
151
|
|
|
private function configure_debugging() { |
152
|
|
|
// set debug log status based on the plugin's debug mode setting. |
153
|
|
|
if ( true === $this->debug ) { |
154
|
|
|
$this->statuses_to_log[] = 'debug'; |
155
|
|
|
$this->enabled = true; |
156
|
|
|
} else { |
157
|
|
|
if ( in_array( 'debug', $this->statuses_to_log, true ) ) { |
158
|
|
|
$delete_value = 'debug'; |
159
|
|
|
$this->statuses_to_log = array_filter( |
160
|
|
|
$this->statuses_to_log, |
161
|
|
|
function( $e ) use ( $delete_value ) { |
162
|
|
|
return ( $e !== $delete_value ); |
163
|
|
|
} |
164
|
|
|
); |
165
|
|
|
update_option( $this->option_prefix . 'statuses_to_log', $this->statuses_to_log ); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Set visibility for the post type |
172
|
|
|
* |
173
|
|
|
* @param array $log_args The post arguments. |
174
|
|
|
* @return array $log_args |
175
|
|
|
*/ |
176
|
|
|
public function set_log_visibility( $log_args ) { |
177
|
|
|
// set public to true overrides the WP_DEBUG setting that is the default on the class |
178
|
|
|
// capabilities makes it so (currently) only admin users can see the log posts in their admin view |
179
|
|
|
// note: a public value of true is required to show Logs as a nav menu item on the admin. |
180
|
|
|
// however, if we don't set exclude_from_search to true and publicly_queryable to false, logs *can* appear in search results. |
181
|
|
|
$log_args['public'] = true; |
182
|
|
|
$log_args['publicly_queryable'] = false; |
183
|
|
|
$log_args['exclude_from_search'] = true; |
184
|
|
|
$log_args['capabilities'] = array( |
185
|
|
|
'edit_post' => $this->capability, |
186
|
|
|
'read_post' => $this->capability, |
187
|
|
|
'delete_post' => $this->capability, |
188
|
|
|
'edit_posts' => $this->capability, |
189
|
|
|
'edit_others_posts' => $this->capability, |
190
|
|
|
'delete_posts' => $this->capability, |
191
|
|
|
'publish_posts' => $this->capability, |
192
|
|
|
'read_private_posts' => $this->capability, |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
$log_args = apply_filters( $this->option_prefix . 'logging_post_type_args', $log_args ); |
|
|
|
|
196
|
|
|
|
197
|
|
|
return $log_args; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Create a (probably unique) post name for logs in a more performant manner than wp_unique_post_slug(). |
202
|
|
|
* |
203
|
|
|
* @param string $override_slug Short-circuit return value. |
204
|
|
|
* @param string $slug The desired slug (post_name). |
205
|
|
|
* @param int $post_ID The post ID. |
206
|
|
|
* @param string $post_status The post status. |
207
|
|
|
* @param string $post_type The post type. |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public function set_log_slug( $override_slug, $slug, $post_ID, $post_status, $post_type ) { |
211
|
|
|
if ( 'wp_log' === $post_type ) { |
212
|
|
|
$override_slug = uniqid( $post_type . '-', true ) . '-' . wp_generate_password( 32, false ); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
return $override_slug; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Add a Type column to the posts admin for this post type |
219
|
|
|
* |
220
|
|
|
* @param array $columns the columns for the post list table. |
221
|
|
|
* @return array $columns |
222
|
|
|
*/ |
223
|
|
|
public function type_column( $columns ) { |
224
|
|
|
$columns['type'] = __( 'Type', 'object-sync-for-salesforce' ); |
|
|
|
|
225
|
|
|
return $columns; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Make the Type column in the posts admin for this post type sortable |
230
|
|
|
* |
231
|
|
|
* @param array $columns the sortable columns for the post list table. |
232
|
|
|
* @return array $columns |
233
|
|
|
*/ |
234
|
|
|
public function sortable_columns( $columns ) { |
235
|
|
|
$columns['type'] = 'type'; |
236
|
|
|
return $columns; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Add the content for the Type column in the posts admin for this post type |
241
|
|
|
* |
242
|
|
|
* @param string $column_name the value for the type column on the list table. |
243
|
|
|
* @param int $post_id the ID of the currently listed post in the table. |
244
|
|
|
*/ |
245
|
|
|
public function type_column_content( $column_name, $post_id ) { |
246
|
|
|
if ( 'type' !== $column_name ) { |
247
|
|
|
return; |
248
|
|
|
} |
249
|
|
|
// get wp_log_type. |
250
|
|
|
$terms = wp_get_post_terms( |
|
|
|
|
251
|
|
|
$post_id, |
252
|
|
|
'wp_log_type', |
253
|
|
|
array( |
254
|
|
|
'fields' => 'names', |
255
|
|
|
) |
256
|
|
|
); |
257
|
|
|
if ( is_array( $terms ) ) { |
258
|
|
|
echo esc_attr( $terms[0] ); |
|
|
|
|
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Filter log posts by the taxonomy from the dropdown when a value is present |
264
|
|
|
* |
265
|
|
|
* @param object $query the current WP query for the list table. |
266
|
|
|
*/ |
267
|
|
|
public function posts_filter( $query ) { |
268
|
|
|
global $pagenow; |
269
|
|
|
$type = 'wp_log'; |
270
|
|
|
$taxonomy = 'wp_log_type'; |
271
|
|
|
if ( is_admin() && 'edit.php' === $pagenow ) { |
|
|
|
|
272
|
|
|
if ( isset( $_GET['post_type'] ) && esc_attr( $_GET['post_type'] ) === $type ) { |
|
|
|
|
273
|
|
|
if ( isset( $_GET[ $taxonomy ] ) && '' !== $_GET[ $taxonomy ] ) { |
274
|
|
|
$query->post_type = $type; |
275
|
|
|
$query->tax_query = array( |
276
|
|
|
array( |
277
|
|
|
'taxonomy' => $taxonomy, |
278
|
|
|
'field' => 'slug', |
279
|
|
|
'terms' => esc_attr( $_GET[ $taxonomy ] ), |
280
|
|
|
), |
281
|
|
|
); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Add a filter form for the log admin so we can filter by wp_log_type taxonomy values |
289
|
|
|
* |
290
|
|
|
* @param string $post_type what type of log we want to show. |
291
|
|
|
*/ |
292
|
|
|
public function restrict_logs_by_type( $post_type ) { |
293
|
|
|
$type = 'wp_log'; |
294
|
|
|
$taxonomy = 'wp_log_type'; |
295
|
|
|
// only add filter to post type you want. |
296
|
|
|
if ( 'wp_log' === $post_type ) { |
297
|
|
|
// get wp_log_type. |
298
|
|
|
$terms = get_terms( |
|
|
|
|
299
|
|
|
array( |
300
|
|
|
'taxonomy' => $taxonomy, |
301
|
|
|
'hide_empty' => true, |
302
|
|
|
) |
303
|
|
|
); |
304
|
|
|
if ( is_wp_error( $terms ) || empty( $terms ) ) { |
|
|
|
|
305
|
|
|
// no terms, or the taxonomy doesn't exist, skip. |
306
|
|
|
return; |
307
|
|
|
} |
308
|
|
|
?> |
309
|
|
|
<select name="wp_log_type"> |
310
|
|
|
<option value=""><?php esc_html_e( 'All log types ', 'object-sync-for-salesforce' ); ?></option> |
|
|
|
|
311
|
|
|
<?php |
312
|
|
|
$current_log_type = isset( $_GET[ $taxonomy ] ) ? esc_attr( $_GET[ $taxonomy ] ) : ''; |
|
|
|
|
313
|
|
|
foreach ( $terms as $key => $term ) { |
314
|
|
|
printf( |
315
|
|
|
'<option value="%s"%s>%s</option>', |
316
|
|
|
esc_attr( $term->slug ), |
317
|
|
|
selected( $term->slug, $current_log_type, false ), |
|
|
|
|
318
|
|
|
esc_html( $term->name ) |
|
|
|
|
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
?> |
322
|
|
|
</select> |
323
|
|
|
<?php |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* When the cron settings change, clear the relevant schedule |
329
|
|
|
* |
330
|
|
|
* @param string $old_value Previous option value. |
331
|
|
|
* @param string $new_value New option value. |
332
|
|
|
* @param string $option Name of option. |
333
|
|
|
*/ |
334
|
|
|
public function check_log_schedule( $old_value, $new_value, $option ) { |
335
|
|
|
$clear_schedule = false; |
336
|
|
|
$schedule_unit = get_option( $this->option_prefix . 'logs_how_often_unit', '' ); |
|
|
|
|
337
|
|
|
$schedule_number = filter_var( get_option( $this->option_prefix . 'logs_how_often_number', '' ), FILTER_VALIDATE_INT ); |
338
|
|
|
if ( $this->option_prefix . 'logs_how_often_unit' === $option ) { |
339
|
|
|
$old_frequency = $this->get_schedule_frequency( $old_value, $schedule_number ); |
340
|
|
|
$new_frequency = $this->get_schedule_frequency( $new_value, $schedule_number ); |
341
|
|
|
$old_key = $old_frequency['key']; |
342
|
|
|
$new_key = $new_frequency['key']; |
343
|
|
|
if ( $old_key !== $new_key ) { |
344
|
|
|
$clear_schedule = true; |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
if ( $this->option_prefix . 'logs_how_often_number' === $option ) { |
348
|
|
|
$old_frequency = $this->get_schedule_frequency( $schedule_unit, $old_value ); |
349
|
|
|
$new_frequency = $this->get_schedule_frequency( $schedule_unit, $new_value ); |
350
|
|
|
$old_key = $old_frequency['key']; |
351
|
|
|
$new_key = $new_frequency['key']; |
352
|
|
|
if ( $old_key !== $new_key ) { |
353
|
|
|
$clear_schedule = true; |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
if ( true === $clear_schedule ) { |
357
|
|
|
wp_clear_scheduled_hook( $this->schedule_name ); |
|
|
|
|
358
|
|
|
$this->save_log_schedule(); |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Save a cron schedule |
364
|
|
|
*/ |
365
|
|
|
public function save_log_schedule() { |
366
|
|
|
global $pagenow; |
367
|
|
|
if ( ( 'options.php' !== $pagenow ) && ( ! isset( $_GET['page'] ) || $this->slug . '-admin' !== $_GET['page'] ) ) { |
368
|
|
|
return; |
369
|
|
|
} |
370
|
|
|
$schedule_unit = get_option( $this->option_prefix . 'logs_how_often_unit', '' ); |
|
|
|
|
371
|
|
|
$schedule_number = filter_var( get_option( $this->option_prefix . 'logs_how_often_number', '' ), FILTER_VALIDATE_INT ); |
372
|
|
|
$frequency = $this->get_schedule_frequency( $schedule_unit, $schedule_number ); |
373
|
|
|
$key = $frequency['key']; |
374
|
|
|
if ( ! wp_next_scheduled( $this->schedule_name ) ) { |
|
|
|
|
375
|
|
|
wp_schedule_event( time(), $key, $this->schedule_name ); |
|
|
|
|
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Add interval to wp schedules based on admin settings |
381
|
|
|
* |
382
|
|
|
* @param array $schedules An array of scheduled cron items. |
383
|
|
|
* @return array $frequency |
384
|
|
|
*/ |
385
|
|
|
public function add_prune_interval( $schedules ) { |
386
|
|
|
|
387
|
|
|
$schedule_unit = get_option( $this->option_prefix . 'logs_how_often_unit', '' ); |
|
|
|
|
388
|
|
|
$schedule_number = filter_var( get_option( $this->option_prefix . 'logs_how_often_number', '' ), FILTER_VALIDATE_INT ); |
389
|
|
|
$frequency = $this->get_schedule_frequency( $schedule_unit, $schedule_number ); |
390
|
|
|
$key = $frequency['key']; |
391
|
|
|
$seconds = $frequency['seconds']; |
392
|
|
|
|
393
|
|
|
$schedules[ $key ] = array( |
394
|
|
|
'interval' => $seconds * $schedule_number, |
395
|
|
|
'display' => 'Every ' . $schedule_number . ' ' . $schedule_unit, |
396
|
|
|
); |
397
|
|
|
|
398
|
|
|
return $schedules; |
399
|
|
|
|
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Convert the schedule frequency from the admin settings into an array |
404
|
|
|
* interval must be in seconds for the class to use it |
405
|
|
|
* |
406
|
|
|
* @param string $unit A unit of time. |
407
|
|
|
* @param string $number The number of those units. |
408
|
|
|
* @return array |
409
|
|
|
*/ |
410
|
|
|
public function get_schedule_frequency( $unit, $number ) { |
411
|
|
|
|
412
|
|
|
switch ( $unit ) { |
413
|
|
|
case 'minutes': |
414
|
|
|
$seconds = 60; |
415
|
|
|
break; |
416
|
|
|
case 'hours': |
417
|
|
|
$seconds = 3600; |
418
|
|
|
break; |
419
|
|
|
case 'days': |
420
|
|
|
$seconds = 86400; |
421
|
|
|
break; |
422
|
|
|
default: |
423
|
|
|
$seconds = 0; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
$key = $unit . '_' . $number; |
427
|
|
|
|
428
|
|
|
return array( |
429
|
|
|
'key' => $key, |
430
|
|
|
'seconds' => $seconds, |
431
|
|
|
); |
432
|
|
|
|
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Set terms for Salesforce logs |
437
|
|
|
* |
438
|
|
|
* @param array $terms An array of string log types in the WP_Logging class. |
439
|
|
|
* @return array $terms |
440
|
|
|
*/ |
441
|
|
|
public function set_log_types( $terms ) { |
442
|
|
|
$terms[] = 'salesforce'; |
443
|
|
|
return $terms; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Should logs be pruned at all? |
448
|
|
|
* |
449
|
|
|
* @param string $should_we_prune Whether to prune old log items. |
450
|
|
|
* @return string $should_we_prune Whether to prune old log items. |
451
|
|
|
*/ |
452
|
|
|
public function set_prune_option( $should_we_prune ) { |
453
|
|
|
$should_we_prune = get_option( $this->option_prefix . 'prune_logs', $should_we_prune ); |
|
|
|
|
454
|
|
|
$should_we_prune = filter_var( $should_we_prune, FILTER_VALIDATE_BOOLEAN ); |
455
|
|
|
return $should_we_prune; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Set how often to prune the Salesforce logs |
460
|
|
|
* |
461
|
|
|
* @param string $how_old How old the oldest non-pruned log items should be allowed to be. |
462
|
|
|
* @return string $how_old |
463
|
|
|
*/ |
464
|
|
|
public function set_prune_age( $how_old ) { |
465
|
|
|
$value = get_option( $this->option_prefix . 'logs_how_old', '' ) . ' ago'; |
|
|
|
|
466
|
|
|
if ( '' !== $value ) { |
467
|
|
|
return $value; |
468
|
|
|
} else { |
469
|
|
|
return $how_old; |
470
|
|
|
} |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* Set arguments for only getting the Salesforce logs |
475
|
|
|
* |
476
|
|
|
* @param array $args Argument array for get_posts determining what posts are eligible for pruning. |
477
|
|
|
* @return array $args |
478
|
|
|
*/ |
479
|
|
|
public function set_prune_args( $args ) { |
480
|
|
|
$args['wp_log_type'] = 'salesforce'; |
481
|
|
|
$number_to_prune = get_option( $this->option_prefix . 'logs_how_many_number', '' ); |
|
|
|
|
482
|
|
|
if ( '' !== $number_to_prune ) { |
483
|
|
|
$args['posts_per_page'] = filter_var( $number_to_prune, FILTER_SANITIZE_NUMBER_INT ); |
484
|
|
|
} |
485
|
|
|
return $args; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Setup new log entry |
490
|
|
|
* |
491
|
|
|
* Check and see if we should log anything, and if so, send it to add() |
492
|
|
|
* |
493
|
|
|
* @access public |
494
|
|
|
* @since 1.0 |
495
|
|
|
* |
496
|
|
|
* @param string|array $title_or_params A log post title, or the full array of parameters. |
497
|
|
|
* @param string $message The log message. |
498
|
|
|
* @param string|0 $trigger The type of log triggered. Usually one of: debug, notice, warning, error. |
|
|
|
|
499
|
|
|
* @param int $parent The parent WordPress object. |
500
|
|
|
* @param string $status The log status. |
501
|
|
|
* |
502
|
|
|
* @uses self::add() |
503
|
|
|
* @see Object_Sync_Sf_Mapping::__construct() the location of the parameters that define the logging triggers. |
504
|
|
|
* |
505
|
|
|
* @return void |
506
|
|
|
*/ |
507
|
|
|
public function setup( $title_or_params, $message = '', $trigger = 0, $parent = 0, $status = '' ) { |
508
|
|
|
|
509
|
|
|
if ( is_array( $title_or_params ) ) { |
510
|
|
|
$title = $title_or_params['title']; |
511
|
|
|
$message = $title_or_params['message']; |
512
|
|
|
$trigger = $title_or_params['trigger']; |
513
|
|
|
$parent = $title_or_params['parent']; |
514
|
|
|
$status = $title_or_params['status']; |
515
|
|
|
} else { |
516
|
|
|
$title = $title_or_params; |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
if ( true === $this->enabled && in_array( $status, $this->statuses_to_log, true ) ) { |
520
|
|
|
$triggers_to_log = (array) maybe_unserialize( get_option( $this->option_prefix . 'triggers_to_log', array() ) ); |
|
|
|
|
521
|
|
|
if ( 0 === $trigger || in_array( $trigger, $triggers_to_log, true ) ) { |
522
|
|
|
$this->add( $title, $message, $parent ); |
523
|
|
|
} elseif ( is_array( $trigger ) && array_intersect( $trigger, $triggers_to_log ) ) { |
524
|
|
|
$this->add( $title, $message, $parent ); |
525
|
|
|
} elseif ( true === $this->debug ) { |
526
|
|
|
// if the plugin is in debug mode, treat all triggers as triggers to log. |
527
|
|
|
$this->add( $title, $message, $parent ); |
528
|
|
|
} |
529
|
|
|
} |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Create new log entry |
534
|
|
|
* |
535
|
|
|
* This is just a simple and fast way to log something. Use self::insert_log() |
536
|
|
|
* if you need to store custom meta data |
537
|
|
|
* |
538
|
|
|
* @access public |
539
|
|
|
* @since 1.0 |
540
|
|
|
* |
541
|
|
|
* @param string $title A log post title. |
542
|
|
|
* |
543
|
|
|
* @uses self::insert_log() |
544
|
|
|
* @param string $message The log message. |
545
|
|
|
* @param int $parent The parent WordPress object. |
546
|
|
|
* @param string $type The type of log message; defaults to 'salesforce'. |
547
|
|
|
* |
548
|
|
|
* @return int The ID of the new log entry |
549
|
|
|
*/ |
550
|
|
|
public static function add( $title = '', $message = '', $parent = 0, $type = 'salesforce' ) { |
551
|
|
|
|
552
|
|
|
$log_data = array( |
553
|
|
|
'post_title' => esc_html( $title ), |
|
|
|
|
554
|
|
|
'post_content' => wp_kses_post( $message ), |
|
|
|
|
555
|
|
|
'post_parent' => absint( $parent ), |
|
|
|
|
556
|
|
|
'log_type' => esc_attr( $type ), |
|
|
|
|
557
|
|
|
); |
558
|
|
|
|
559
|
|
|
return self::insert_log( $log_data ); |
560
|
|
|
|
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* Easily retrieves log items for a particular object ID |
566
|
|
|
* |
567
|
|
|
* @access private |
568
|
|
|
* @since 1.0 |
569
|
|
|
* |
570
|
|
|
* @param int $object_id A WordPress object ID. |
571
|
|
|
* @param string $type The type of log item; defaults to 'salesforce' because that's the type of logs we create. |
572
|
|
|
* @param int $paged show which page of results we want. |
573
|
|
|
* |
574
|
|
|
* @uses self::get_connected_logs() |
575
|
|
|
* |
576
|
|
|
* @return array |
577
|
|
|
*/ |
578
|
|
|
public static function get_logs( $object_id = 0, $type = 'salesforce', $paged = null ) { |
579
|
|
|
return self::get_connected_logs( |
580
|
|
|
array( |
581
|
|
|
'post_parent' => (int) $object_id, |
582
|
|
|
'paged' => (int) $paged, |
583
|
|
|
'log_type' => (string) $type, |
584
|
|
|
) |
585
|
|
|
); |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* Retrieve all connected logs |
591
|
|
|
* |
592
|
|
|
* Used for retrieving logs related to particular items, such as a specific purchase. |
593
|
|
|
* |
594
|
|
|
* @access private |
595
|
|
|
* @since 1.0 |
596
|
|
|
* |
597
|
|
|
* @param Array $args An array of arguments for get_posts(). |
598
|
|
|
* |
599
|
|
|
* @uses wp_parse_args() |
600
|
|
|
* @uses get_posts() |
601
|
|
|
* @uses get_query_var() |
602
|
|
|
* @uses self::valid_type() |
603
|
|
|
* |
604
|
|
|
* @return array / false |
605
|
|
|
*/ |
606
|
|
|
public static function get_connected_logs( $args = array() ) { |
607
|
|
|
|
608
|
|
|
$defaults = array( |
609
|
|
|
'post_parent' => 0, |
610
|
|
|
'post_type' => 'wp_log', |
611
|
|
|
'posts_per_page' => 10, |
612
|
|
|
'post_status' => 'publish', |
613
|
|
|
'paged' => get_query_var( 'paged' ), |
|
|
|
|
614
|
|
|
'log_type' => 'salesforce', |
615
|
|
|
); |
616
|
|
|
|
617
|
|
|
$query_args = wp_parse_args( $args, $defaults ); |
|
|
|
|
618
|
|
|
|
619
|
|
|
if ( $query_args['log_type'] && self::valid_type( $query_args['log_type'] ) ) { |
620
|
|
|
|
621
|
|
|
$query_args['tax_query'] = array( |
622
|
|
|
array( |
623
|
|
|
'taxonomy' => 'wp_log_type', |
624
|
|
|
'field' => 'slug', |
625
|
|
|
'terms' => $query_args['log_type'], |
626
|
|
|
), |
627
|
|
|
); |
628
|
|
|
|
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
$logs = get_posts( $query_args ); |
|
|
|
|
632
|
|
|
|
633
|
|
|
if ( $logs ) { |
634
|
|
|
return $logs; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
// no logs found. |
638
|
|
|
return false; |
639
|
|
|
|
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
|
643
|
|
|
/** |
644
|
|
|
* Retrieves number of log entries connected to particular object ID |
645
|
|
|
* |
646
|
|
|
* @access private |
647
|
|
|
* @since 1.0 |
648
|
|
|
* |
649
|
|
|
* @param int $object_id A WordPress object ID. |
650
|
|
|
* @param string $type The type of log item; defaults to 'salesforce' because that's the type of logs we create. |
651
|
|
|
* @param array $meta_query A WordPress meta query, parseable by WP_Meta_Query. |
652
|
|
|
* |
653
|
|
|
* @uses WP_Query() |
654
|
|
|
* @uses self::valid_type() |
655
|
|
|
* |
656
|
|
|
* @return int |
657
|
|
|
*/ |
658
|
|
|
public static function get_log_count( $object_id = 0, $type = 'salesforce', $meta_query = null ) { |
659
|
|
|
|
660
|
|
|
$query_args = array( |
661
|
|
|
'post_parent' => (int) $object_id, |
662
|
|
|
'post_type' => 'wp_log', |
663
|
|
|
'posts_per_page' => 100, |
664
|
|
|
'post_status' => 'publish', |
665
|
|
|
); |
666
|
|
|
|
667
|
|
|
if ( ! empty( $type ) && self::valid_type( $type ) ) { |
668
|
|
|
|
669
|
|
|
$query_args['tax_query'] = array( |
670
|
|
|
array( |
671
|
|
|
'taxonomy' => 'wp_log_type', |
672
|
|
|
'field' => 'slug', |
673
|
|
|
'terms' => sanitize_key( $type ), |
|
|
|
|
674
|
|
|
), |
675
|
|
|
); |
676
|
|
|
|
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
if ( ! empty( $meta_query ) ) { |
680
|
|
|
$query_args['meta_query'] = $meta_query; |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
$logs = new WP_Query( $query_args ); |
|
|
|
|
684
|
|
|
|
685
|
|
|
return (int) $logs->post_count; |
686
|
|
|
|
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
} |
690
|
|
|
|