|
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
|
|
|
|
|
357
|
|
|
if ( false === wp_get_scheduled_event( $this->schedule_name ) ) { |
|
|
|
|
|
|
358
|
|
|
$clear_schedule = false; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
if ( true === $clear_schedule ) { |
|
362
|
|
|
wp_clear_scheduled_hook( $this->schedule_name ); |
|
|
|
|
|
|
363
|
|
|
$this->save_log_schedule(); |
|
364
|
|
|
} |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* Save a cron schedule |
|
369
|
|
|
*/ |
|
370
|
|
|
public function save_log_schedule() { |
|
371
|
|
|
global $pagenow; |
|
372
|
|
|
if ( ( 'options.php' !== $pagenow ) && ( ! isset( $_GET['page'] ) || $this->slug . '-admin' !== $_GET['page'] ) ) { |
|
373
|
|
|
return; |
|
374
|
|
|
} |
|
375
|
|
|
$schedule_unit = get_option( $this->option_prefix . 'logs_how_often_unit', '' ); |
|
|
|
|
|
|
376
|
|
|
$schedule_number = filter_var( get_option( $this->option_prefix . 'logs_how_often_number', '' ), FILTER_VALIDATE_INT ); |
|
377
|
|
|
$frequency = $this->get_schedule_frequency( $schedule_unit, $schedule_number ); |
|
378
|
|
|
$key = $frequency['key']; |
|
379
|
|
|
if ( ! wp_next_scheduled( $this->schedule_name ) ) { |
|
|
|
|
|
|
380
|
|
|
wp_schedule_event( time(), $key, $this->schedule_name ); |
|
|
|
|
|
|
381
|
|
|
} |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
/** |
|
385
|
|
|
* Add interval to wp schedules based on admin settings |
|
386
|
|
|
* |
|
387
|
|
|
* @param array $schedules An array of scheduled cron items. |
|
388
|
|
|
* @return array $frequency |
|
389
|
|
|
*/ |
|
390
|
|
|
public function add_prune_interval( $schedules ) { |
|
391
|
|
|
|
|
392
|
|
|
$schedule_unit = get_option( $this->option_prefix . 'logs_how_often_unit', '' ); |
|
|
|
|
|
|
393
|
|
|
$schedule_number = filter_var( get_option( $this->option_prefix . 'logs_how_often_number', '' ), FILTER_VALIDATE_INT ); |
|
394
|
|
|
$frequency = $this->get_schedule_frequency( $schedule_unit, $schedule_number ); |
|
395
|
|
|
$key = $frequency['key']; |
|
396
|
|
|
$seconds = $frequency['seconds']; |
|
397
|
|
|
|
|
398
|
|
|
$schedules[ $key ] = array( |
|
399
|
|
|
'interval' => $seconds * $schedule_number, |
|
400
|
|
|
'display' => 'Every ' . $schedule_number . ' ' . $schedule_unit, |
|
401
|
|
|
); |
|
402
|
|
|
|
|
403
|
|
|
return $schedules; |
|
404
|
|
|
|
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* Convert the schedule frequency from the admin settings into an array |
|
409
|
|
|
* interval must be in seconds for the class to use it |
|
410
|
|
|
* |
|
411
|
|
|
* @param string $unit A unit of time. |
|
412
|
|
|
* @param string $number The number of those units. |
|
413
|
|
|
* @return array |
|
414
|
|
|
*/ |
|
415
|
|
|
public function get_schedule_frequency( $unit, $number ) { |
|
416
|
|
|
|
|
417
|
|
|
switch ( $unit ) { |
|
418
|
|
|
case 'minutes': |
|
419
|
|
|
$seconds = 60; |
|
420
|
|
|
break; |
|
421
|
|
|
case 'hours': |
|
422
|
|
|
$seconds = 3600; |
|
423
|
|
|
break; |
|
424
|
|
|
case 'days': |
|
425
|
|
|
$seconds = 86400; |
|
426
|
|
|
break; |
|
427
|
|
|
default: |
|
428
|
|
|
$seconds = 0; |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
$key = $unit . '_' . $number; |
|
432
|
|
|
|
|
433
|
|
|
return array( |
|
434
|
|
|
'key' => $key, |
|
435
|
|
|
'seconds' => $seconds, |
|
436
|
|
|
); |
|
437
|
|
|
|
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
/** |
|
441
|
|
|
* Set terms for Salesforce logs |
|
442
|
|
|
* |
|
443
|
|
|
* @param array $terms An array of string log types in the WP_Logging class. |
|
444
|
|
|
* @return array $terms |
|
445
|
|
|
*/ |
|
446
|
|
|
public function set_log_types( $terms ) { |
|
447
|
|
|
$terms[] = 'salesforce'; |
|
448
|
|
|
return $terms; |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
/** |
|
452
|
|
|
* Should logs be pruned at all? |
|
453
|
|
|
* |
|
454
|
|
|
* @param string $should_we_prune Whether to prune old log items. |
|
455
|
|
|
* @return string $should_we_prune Whether to prune old log items. |
|
456
|
|
|
*/ |
|
457
|
|
|
public function set_prune_option( $should_we_prune ) { |
|
458
|
|
|
$should_we_prune = get_option( $this->option_prefix . 'prune_logs', $should_we_prune ); |
|
|
|
|
|
|
459
|
|
|
$should_we_prune = filter_var( $should_we_prune, FILTER_VALIDATE_BOOLEAN ); |
|
460
|
|
|
return $should_we_prune; |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
/** |
|
464
|
|
|
* Set how often to prune the Salesforce logs |
|
465
|
|
|
* |
|
466
|
|
|
* @param string $how_old How old the oldest non-pruned log items should be allowed to be. |
|
467
|
|
|
* @return string $how_old |
|
468
|
|
|
*/ |
|
469
|
|
|
public function set_prune_age( $how_old ) { |
|
470
|
|
|
$value = get_option( $this->option_prefix . 'logs_how_old', '' ) . ' ago'; |
|
|
|
|
|
|
471
|
|
|
if ( '' !== $value ) { |
|
472
|
|
|
return $value; |
|
473
|
|
|
} else { |
|
474
|
|
|
return $how_old; |
|
475
|
|
|
} |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
/** |
|
479
|
|
|
* Set arguments for only getting the Salesforce logs |
|
480
|
|
|
* |
|
481
|
|
|
* @param array $args Argument array for get_posts determining what posts are eligible for pruning. |
|
482
|
|
|
* @return array $args |
|
483
|
|
|
*/ |
|
484
|
|
|
public function set_prune_args( $args ) { |
|
485
|
|
|
$args['wp_log_type'] = 'salesforce'; |
|
486
|
|
|
$number_to_prune = get_option( $this->option_prefix . 'logs_how_many_number', '' ); |
|
|
|
|
|
|
487
|
|
|
if ( '' !== $number_to_prune ) { |
|
488
|
|
|
$args['posts_per_page'] = filter_var( $number_to_prune, FILTER_SANITIZE_NUMBER_INT ); |
|
489
|
|
|
} |
|
490
|
|
|
return $args; |
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
|
|
/** |
|
494
|
|
|
* Setup new log entry |
|
495
|
|
|
* |
|
496
|
|
|
* Check and see if we should log anything, and if so, send it to add() |
|
497
|
|
|
* |
|
498
|
|
|
* @access public |
|
499
|
|
|
* @since 1.0 |
|
500
|
|
|
* |
|
501
|
|
|
* @param string|array $title_or_params A log post title, or the full array of parameters. |
|
502
|
|
|
* @param string $message The log message. |
|
503
|
|
|
* @param string|0 $trigger The type of log triggered. Usually one of: debug, notice, warning, error. |
|
|
|
|
|
|
504
|
|
|
* @param int $parent The parent WordPress object. |
|
505
|
|
|
* @param string $status The log status. |
|
506
|
|
|
* |
|
507
|
|
|
* @uses self::add() |
|
508
|
|
|
* @see Object_Sync_Sf_Mapping::__construct() the location of the parameters that define the logging triggers. |
|
509
|
|
|
* |
|
510
|
|
|
* @return void |
|
511
|
|
|
*/ |
|
512
|
|
|
public function setup( $title_or_params, $message = '', $trigger = 0, $parent = 0, $status = '' ) { |
|
513
|
|
|
|
|
514
|
|
|
if ( is_array( $title_or_params ) ) { |
|
515
|
|
|
$title = $title_or_params['title']; |
|
516
|
|
|
$message = $title_or_params['message']; |
|
517
|
|
|
$trigger = $title_or_params['trigger']; |
|
518
|
|
|
$parent = $title_or_params['parent']; |
|
519
|
|
|
$status = $title_or_params['status']; |
|
520
|
|
|
} else { |
|
521
|
|
|
$title = $title_or_params; |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
if ( true === $this->enabled && in_array( $status, $this->statuses_to_log, true ) ) { |
|
525
|
|
|
$triggers_to_log = (array) maybe_unserialize( get_option( $this->option_prefix . 'triggers_to_log', array() ) ); |
|
|
|
|
|
|
526
|
|
|
if ( 0 === $trigger || in_array( $trigger, $triggers_to_log, true ) ) { |
|
527
|
|
|
$this->add( $title, $message, $parent ); |
|
528
|
|
|
} elseif ( is_array( $trigger ) && array_intersect( $trigger, $triggers_to_log ) ) { |
|
529
|
|
|
$this->add( $title, $message, $parent ); |
|
530
|
|
|
} elseif ( true === $this->debug ) { |
|
531
|
|
|
// if the plugin is in debug mode, treat all triggers as triggers to log. |
|
532
|
|
|
$this->add( $title, $message, $parent ); |
|
533
|
|
|
} |
|
534
|
|
|
} |
|
535
|
|
|
} |
|
536
|
|
|
|
|
537
|
|
|
/** |
|
538
|
|
|
* Create new log entry |
|
539
|
|
|
* |
|
540
|
|
|
* This is just a simple and fast way to log something. Use self::insert_log() |
|
541
|
|
|
* if you need to store custom meta data |
|
542
|
|
|
* |
|
543
|
|
|
* @access public |
|
544
|
|
|
* @since 1.0 |
|
545
|
|
|
* |
|
546
|
|
|
* @param string $title A log post title. |
|
547
|
|
|
* |
|
548
|
|
|
* @uses self::insert_log() |
|
549
|
|
|
* @param string $message The log message. |
|
550
|
|
|
* @param int $parent The parent WordPress object. |
|
551
|
|
|
* @param string $type The type of log message; defaults to 'salesforce'. |
|
552
|
|
|
* |
|
553
|
|
|
* @return int The ID of the new log entry |
|
554
|
|
|
*/ |
|
555
|
|
|
public static function add( $title = '', $message = '', $parent = 0, $type = 'salesforce' ) { |
|
556
|
|
|
|
|
557
|
|
|
$log_data = array( |
|
558
|
|
|
'post_title' => esc_html( $title ), |
|
|
|
|
|
|
559
|
|
|
'post_content' => wp_kses_post( $message ), |
|
|
|
|
|
|
560
|
|
|
'post_parent' => absint( $parent ), |
|
|
|
|
|
|
561
|
|
|
'log_type' => esc_attr( $type ), |
|
|
|
|
|
|
562
|
|
|
); |
|
563
|
|
|
|
|
564
|
|
|
return self::insert_log( $log_data ); |
|
565
|
|
|
|
|
566
|
|
|
} |
|
567
|
|
|
|
|
568
|
|
|
|
|
569
|
|
|
/** |
|
570
|
|
|
* Easily retrieves log items for a particular object ID |
|
571
|
|
|
* |
|
572
|
|
|
* @access private |
|
573
|
|
|
* @since 1.0 |
|
574
|
|
|
* |
|
575
|
|
|
* @param int $object_id A WordPress object ID. |
|
576
|
|
|
* @param string $type The type of log item; defaults to 'salesforce' because that's the type of logs we create. |
|
577
|
|
|
* @param int $paged show which page of results we want. |
|
578
|
|
|
* |
|
579
|
|
|
* @uses self::get_connected_logs() |
|
580
|
|
|
* |
|
581
|
|
|
* @return array |
|
582
|
|
|
*/ |
|
583
|
|
|
public static function get_logs( $object_id = 0, $type = 'salesforce', $paged = null ) { |
|
584
|
|
|
return self::get_connected_logs( |
|
585
|
|
|
array( |
|
586
|
|
|
'post_parent' => (int) $object_id, |
|
587
|
|
|
'paged' => (int) $paged, |
|
588
|
|
|
'log_type' => (string) $type, |
|
589
|
|
|
) |
|
590
|
|
|
); |
|
591
|
|
|
} |
|
592
|
|
|
|
|
593
|
|
|
|
|
594
|
|
|
/** |
|
595
|
|
|
* Retrieve all connected logs |
|
596
|
|
|
* |
|
597
|
|
|
* Used for retrieving logs related to particular items, such as a specific purchase. |
|
598
|
|
|
* |
|
599
|
|
|
* @access private |
|
600
|
|
|
* @since 1.0 |
|
601
|
|
|
* |
|
602
|
|
|
* @param Array $args An array of arguments for get_posts(). |
|
603
|
|
|
* |
|
604
|
|
|
* @uses wp_parse_args() |
|
605
|
|
|
* @uses get_posts() |
|
606
|
|
|
* @uses get_query_var() |
|
607
|
|
|
* @uses self::valid_type() |
|
608
|
|
|
* |
|
609
|
|
|
* @return array / false |
|
610
|
|
|
*/ |
|
611
|
|
|
public static function get_connected_logs( $args = array() ) { |
|
612
|
|
|
|
|
613
|
|
|
$defaults = array( |
|
614
|
|
|
'post_parent' => 0, |
|
615
|
|
|
'post_type' => 'wp_log', |
|
616
|
|
|
'posts_per_page' => 10, |
|
617
|
|
|
'post_status' => 'publish', |
|
618
|
|
|
'paged' => get_query_var( 'paged' ), |
|
|
|
|
|
|
619
|
|
|
'log_type' => 'salesforce', |
|
620
|
|
|
); |
|
621
|
|
|
|
|
622
|
|
|
$query_args = wp_parse_args( $args, $defaults ); |
|
|
|
|
|
|
623
|
|
|
|
|
624
|
|
|
if ( $query_args['log_type'] && self::valid_type( $query_args['log_type'] ) ) { |
|
625
|
|
|
|
|
626
|
|
|
$query_args['tax_query'] = array( |
|
627
|
|
|
array( |
|
628
|
|
|
'taxonomy' => 'wp_log_type', |
|
629
|
|
|
'field' => 'slug', |
|
630
|
|
|
'terms' => $query_args['log_type'], |
|
631
|
|
|
), |
|
632
|
|
|
); |
|
633
|
|
|
|
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
$logs = get_posts( $query_args ); |
|
|
|
|
|
|
637
|
|
|
|
|
638
|
|
|
if ( $logs ) { |
|
639
|
|
|
return $logs; |
|
640
|
|
|
} |
|
641
|
|
|
|
|
642
|
|
|
// no logs found. |
|
643
|
|
|
return false; |
|
644
|
|
|
|
|
645
|
|
|
} |
|
646
|
|
|
|
|
647
|
|
|
|
|
648
|
|
|
/** |
|
649
|
|
|
* Retrieves number of log entries connected to particular object ID |
|
650
|
|
|
* |
|
651
|
|
|
* @access private |
|
652
|
|
|
* @since 1.0 |
|
653
|
|
|
* |
|
654
|
|
|
* @param int $object_id A WordPress object ID. |
|
655
|
|
|
* @param string $type The type of log item; defaults to 'salesforce' because that's the type of logs we create. |
|
656
|
|
|
* @param array $meta_query A WordPress meta query, parseable by WP_Meta_Query. |
|
657
|
|
|
* |
|
658
|
|
|
* @uses WP_Query() |
|
659
|
|
|
* @uses self::valid_type() |
|
660
|
|
|
* |
|
661
|
|
|
* @return int |
|
662
|
|
|
*/ |
|
663
|
|
|
public static function get_log_count( $object_id = 0, $type = 'salesforce', $meta_query = null ) { |
|
664
|
|
|
|
|
665
|
|
|
$query_args = array( |
|
666
|
|
|
'post_parent' => (int) $object_id, |
|
667
|
|
|
'post_type' => 'wp_log', |
|
668
|
|
|
'posts_per_page' => 100, |
|
669
|
|
|
'post_status' => 'publish', |
|
670
|
|
|
); |
|
671
|
|
|
|
|
672
|
|
|
if ( ! empty( $type ) && self::valid_type( $type ) ) { |
|
673
|
|
|
|
|
674
|
|
|
$query_args['tax_query'] = array( |
|
675
|
|
|
array( |
|
676
|
|
|
'taxonomy' => 'wp_log_type', |
|
677
|
|
|
'field' => 'slug', |
|
678
|
|
|
'terms' => sanitize_key( $type ), |
|
|
|
|
|
|
679
|
|
|
), |
|
680
|
|
|
); |
|
681
|
|
|
|
|
682
|
|
|
} |
|
683
|
|
|
|
|
684
|
|
|
if ( ! empty( $meta_query ) ) { |
|
685
|
|
|
$query_args['meta_query'] = $meta_query; |
|
686
|
|
|
} |
|
687
|
|
|
|
|
688
|
|
|
$logs = new WP_Query( $query_args ); |
|
|
|
|
|
|
689
|
|
|
|
|
690
|
|
|
return (int) $logs->post_count; |
|
691
|
|
|
|
|
692
|
|
|
} |
|
693
|
|
|
|
|
694
|
|
|
} |
|
695
|
|
|
|