PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
← All changes | includes/custom-tables/logs.php +1079 -0 5.8.4 → 6.0.3 View file →
@@ -1,0 +1,1079 @@
1 +<?php
2 +/**
3 + * Logs
4 + *
5 + * @package AutomatorWP\Custom_Tables\Logs
6 + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]>
7 + * @since 1.0.0
8 + */
9 +// Exit if accessed directly
10 +if( !defined( 'ABSPATH' ) ) exit;
11 +
12 +/**
13 + * Custom Table Labels
14 + *
15 + * @since 1.0.0
16 + *
17 + * @return array
18 + */
19 +function automatorwp_logs_labels() {
20 +
21 + return array(
22 + 'singular' => __( 'Log', 'automatorwp' ),
23 + 'plural' => __( 'Logs', 'automatorwp' ),
24 + 'labels' => array(
25 + 'list_menu_title' => __( 'Logs', 'automatorwp' ),
26 + ),
27 + );
28 +
29 +}
30 +add_filter( 'ct_automatorwp_logs_labels', 'automatorwp_logs_labels' );
31 +
32 +/**
33 + * Parse query args for logs
34 + *
35 + * @since 1.0.0
36 + *
37 + * @param string $where
38 + * @param CT_Query $ct_query
39 + *
40 + * @return string
41 + */
42 +function automatorwp_logs_query_where( $where, $ct_query ) {
43 +
44 + global $ct_table;
45 +
46 + if( $ct_table->name !== 'automatorwp_logs' ) {
47 + return $where;
48 + }
49 +
50 + $table_name = $ct_table->db->table_name;
51 +
52 + // Shorthand
53 + $qv = $ct_query->query_vars;
54 +
55 + // Type
56 + $where .= automatorwp_custom_table_where( $qv, 'type', 'type', 'string' );
57 +
58 + // Object ID
59 + $where .= automatorwp_custom_table_where( $qv, 'object_id', 'object_id', 'integer' );
60 +
61 + // User ID
62 + $where .= automatorwp_custom_table_where( $qv, 'user_id', 'user_id', 'integer' );
63 +
64 + // Post ID
65 + $where .= automatorwp_custom_table_where( $qv, 'post_id', 'post_id', 'integer' );
66 +
67 + return $where;
68 +}
69 +add_filter( 'ct_query_where', 'automatorwp_logs_query_where', 10, 2 );
70 +
71 +/**
72 + * Define the search fields for logs
73 + *
74 + * @since 1.0.0
75 + *
76 + * @param array $search_fields
77 + *
78 + * @return array
79 + */
80 +function automatorwp_logs_search_fields( $search_fields ) {
81 +
82 + $search_fields[] = 'title';
83 +
84 + return $search_fields;
85 +
86 +}
87 +add_filter( 'ct_query_automatorwp_logs_search_fields', 'automatorwp_logs_search_fields' );
88 +
89 +/**
90 + * Parse search query args for logs
91 + *
92 + * @since 1.0.0
93 + *
94 + * @param string $search
95 + * @param CT_Query $ct_query
96 + *
97 + * @return string
98 + */
99 +function automatorwp_logs_query_search( $search, $ct_query ) {
100 +
101 + global $ct_table, $wpdb;
102 +
103 + if( $ct_table->name !== 'automatorwp_logs' ) {
104 + return $search;
105 + }
106 +
107 + $table_name = $ct_table->db->table_name;
108 +
109 + // Shorthand
110 + $qv = $ct_query->query_vars;
111 +
112 + // Check if is search and query is not filtered by an specific user
113 + if( isset( $qv['s'] ) && ! empty( $qv['s'] ) && ! isset( $qv['user_id'] ) ) {
114 +
115 + // Made a user sub-search to retrieve them
116 + $users = get_users( array(
117 + 'search' => sprintf( '*%s*', $qv['s'] ),
118 + 'search_columns' => array(
119 + 'user_login',
120 + 'user_email',
121 + 'display_name',
122 + ),
123 + 'fields' => 'ID',
124 + ) );
125 +
126 + if( ! empty( $users ) ) {
127 + $search .= " AND ( {$table_name}.user_id IN (" . implode( ',', array_map( 'absint', $users ) ) . ") )";
128 + }
129 +
130 + }
131 +
132 + return $search;
133 +
134 +}
135 +add_filter( 'ct_query_search', 'automatorwp_logs_query_search', 10, 2 );
136 +
137 +/**
138 + * Define the field for logs views
139 + *
140 + * @since 1.0.0
141 + *
142 + * @param string $field_id
143 + *
144 + * @return string
145 + */
146 +function automatorwp_logs_views_field( $field_id = '' ) {
147 + return 'type';
148 +}
149 +add_filter( 'ct_list_automatorwp_logs_views_field', 'automatorwp_logs_views_field' );
150 +
151 +/**
152 + * Define the field labels for logs views
153 + *
154 + * @since 1.0.0
155 + *
156 + * @param array $field_labels
157 + *
158 + * @return array
159 + */
160 +function automatorwp_logs_views_field_labels( $field_labels = array() ) {
161 + return automatorwp_get_log_types();
162 +}
163 +add_filter( 'ct_list_automatorwp_logs_views_field_labels', 'automatorwp_logs_views_field_labels' );
164 +
165 +/**
166 + * Columns for logs list view
167 + *
168 + * @since 1.0.0
169 + *
170 + * @param array $columns
171 + *
172 + * @return array
173 + */
174 +function automatorwp_manage_logs_columns( $columns = array() ) {
175 +
176 + $columns['title'] = __( 'Title', 'automatorwp' );
177 + $columns['type'] = __( 'Type', 'automatorwp' );
178 + $columns['object_id'] = __( 'Automation', 'automatorwp' );
179 + $columns['user_id'] = __( 'User', 'automatorwp' );
180 + $columns['post_id'] = __( 'Post', 'automatorwp' );
181 + $columns['date'] = __( 'Date', 'automatorwp' );
182 +
183 + return $columns;
184 +}
185 +add_filter( 'manage_automatorwp_logs_columns', 'automatorwp_manage_logs_columns' );
186 +
187 +/**
188 + * Sortable columns for logs list view
189 + *
190 + * @since 1.0.0
191 + *
192 + * @param array $sortable_columns
193 + *
194 + * @return array
195 + */
196 +function automatorwp_manage_logs_sortable_columns( $sortable_columns ) {
197 +
198 + $sortable_columns['title'] = array( 'title', false );
199 + $sortable_columns['type'] = array( 'type', false );
200 + $sortable_columns['object_id'] = array( 'object_id', false );
201 + $sortable_columns['user_id'] = array( 'user_id', false );
202 + $sortable_columns['post_id'] = array( 'post_id', false );
203 + $sortable_columns['date'] = array( 'date', true );
204 +
205 + return $sortable_columns;
206 +
207 +}
208 +add_filter( 'manage_automatorwp_logs_sortable_columns', 'automatorwp_manage_logs_sortable_columns' );
209 +
210 +/**
211 + * Columns rendering for logs list view
212 + *
213 + * @since 1.0.0
214 + *
215 + * @param string $column_name
216 + * @param integer $object_id
217 + */
218 +function automatorwp_manage_logs_custom_column( $column_name, $object_id ) {
219 +
220 + // Setup vars
221 + $log = ct_get_object( $object_id );
222 +
223 + switch( $column_name ) {
224 + case 'title':
225 +
226 + automatorwp_get_log_integration_icon( $log );
227 +
228 + $title = ! empty( $log->title ) ? $log->title : __( '(No title)', 'automatorwp' ); ?>
229 + <strong><a href="<?php echo ct_get_edit_link( 'automatorwp_logs', $log->id ); ?>"><?php echo esc_html( $title ); ?></a></strong>
230 + <?php
231 +
232 + break;
233 + case 'type':
234 + $types = automatorwp_get_log_types();
235 + $type = isset( $types[$log->type] ) ? $types[$log->type] : $log->type;
236 + ?>
237 +
238 + <span class="automatorwp-log-type automatorwp-log-type-<?php echo esc_attr( $log->type ); ?>"><?php echo esc_html( $type ); ?></span>
239 +
240 + <?php
241 +
242 + break;
243 + case 'object_id':
244 +
245 + $automation_id = false;
246 +
247 + switch ( $log->type ) {
248 + case 'automation':
249 + case 'anonymous':
250 + $automation_id = $log->object_id;
251 + break;
252 + case 'trigger':
253 + $trigger = automatorwp_get_trigger_object( $log->object_id );
254 +
255 + if( $trigger ) {
256 + $automation_id = $trigger->automation_id;
257 + }
258 + break;
259 + case 'action':
260 + $action = automatorwp_get_action_object( $log->object_id );
261 +
262 + if( $action ) {
263 + $automation_id = $action->automation_id;
264 + }
265 + break;
266 + case 'filter':
267 + $item_type = automatorwp_get_log_meta( $log->id, 'item_type', true );
268 +
269 + if( $item_type === 'trigger' ) {
270 + $object = automatorwp_get_trigger_object( $log->object_id );
271 + } else if( $item_type === 'action' ) {
272 + $object = automatorwp_get_action_object( $log->object_id );
273 + }
274 +
275 + if( $object ) {
276 + $automation_id = $object->automation_id;
277 + }
278 + break;
279 +
280 + }
281 +
282 + $automation = automatorwp_get_automation_object( $automation_id );
283 +
284 + if( $automation ) {
285 +
286 + $title = ! empty( $automation->title ) ? $automation->title : __( '(No title)', 'automatorwp' ); ?>
287 + <a href="<?php echo ct_get_edit_link( 'automatorwp_automations', $automation->id ); ?>"><?php echo esc_html( $title ); ?></a>
288 + <?php
289 +
290 + } else {
291 + echo '&nbsp;';
292 + }
293 + break;
294 + case 'user_id':
295 + $user = get_userdata( $log->user_id );
296 +
297 + if( $user ) {
298 +
299 + if( current_user_can( 'edit_users' ) ) {
300 + ?>
301 +
302 + <a href="<?php echo get_edit_user_link( $user->ID ); ?>"><?php echo $user->display_name . ' (' . $user->user_login . ')'; ?></a>
303 +
304 + <?php
305 + } else {
306 + echo $user->display_name;
307 + }
308 +
309 + } else {
310 + echo '&nbsp;';
311 + }
312 + break;
313 + case 'post_id':
314 + $post = get_post( $log->post_id );
315 +
316 + if( $post ) {
317 +
318 + if( current_user_can( 'edit_post', $post->ID ) ) {
319 + ?>
320 +
321 + <a href="<?php echo get_edit_post_link( $post->ID ); ?>"><?php echo $post->post_title; ?></a>
322 +
323 + <?php
324 + } else {
325 + echo $post->post_title;
326 + }
327 +
328 + } else {
329 + echo '&nbsp;';
330 + }
331 + break;
332 + case 'date':
333 + $date = date( 'Y/m/d', strtotime( $log->date ) );
334 + $time = date( 'H:i:s', strtotime( $log->date ) );
335 + ?>
336 +
337 + <span class="automatorwp-log-date"><?php echo $date; ?></span> <span class="automatorwp-log-time"><?php echo $time; ?></span>
338 +
339 + <?php
340 + break;
341 + }
342 +}
343 +add_action( 'manage_automatorwp_logs_custom_column', 'automatorwp_manage_logs_custom_column', 10, 2 );
344 +
345 +/**
346 + * Place the title at top of edit log screen
347 + *
348 + * @since 1.0.0
349 + *
350 + * @param stdClass $log
351 + */
352 +function automatorwp_logs_edit_form_top( $log ) {
353 + global $ct_table;
354 +
355 + if( $ct_table->name !== 'automatorwp_logs' ) {
356 + return;
357 + }
358 +
359 + ?>
360 + <div class="automatorwp-log-title-preview">
361 + <h1>
362 + <?php automatorwp_get_log_integration_icon( $log ); ?>
363 + <?php echo ( ! empty( $log->title ) ? esc_html( $log->title ) : __( '(No title)', 'automatorwp' ) ); ?>
364 + </h1>
365 + </div>
366 + <?php
367 +}
368 +add_action( 'ct_edit_form_top', 'automatorwp_logs_edit_form_top' );
369 +
370 +/**
371 + * Logs meta boxes
372 + *
373 + * @since 1.0.0
374 + */
375 +function automatorwp_add_logs_meta_boxes() {
376 + remove_meta_box( 'submitdiv', 'automatorwp_logs', 'side' );
377 + add_meta_box( 'automatorwp_log_details', __( 'Log Details', 'automatorwp' ), 'automatorwp_log_details_meta_box', 'automatorwp_logs', 'side', 'default' );
378 + add_meta_box( 'automatorwp_log_data', __( 'Log Data', 'automatorwp' ), 'automatorwp_log_data_meta_box', 'automatorwp_logs', 'normal', 'default' );
379 +}
380 +add_action( 'add_meta_boxes', 'automatorwp_add_logs_meta_boxes' );
381 +
382 +/**
383 + * Render the log details meta box
384 + *
385 + * @since 1.0.0
386 + *
387 + * @param stdClass $log The current log
388 + */
389 +function automatorwp_log_details_meta_box( $log = null ) {
390 +
391 + global $ct_table;
392 +
393 + // Setup vars
394 + $log_types = automatorwp_get_log_types();
395 +
396 + ?>
397 + <div class="submitbox" id="submitpost" style="margin: -6px -12px -12px;">
398 +
399 + <div id="minor-publishing">
400 +
401 + <div id="misc-publishing-actions">
402 +
403 + <div class="misc-pub-section misc-pub-post-status">
404 + <?php echo __( 'Type:', 'automatorwp' ); ?> <span id="post-status-display"><?php echo isset( $log_types[$log->type] ) ? $log_types[$log->type] : esc_html( $log->type ) ; ?></span>
405 + </div>
406 +
407 + <div class="misc-pub-section misc-pub-post-user" id="user">
408 + <?php $user = get_userdata( $log->user_id ); ?>
409 + <?php echo __( 'User:', 'automatorwp' ); ?> <span id="post-user-display"><?php
410 + if( $user ) {
411 + if( current_user_can( 'edit_users' ) ) {
412 + ?>
413 +
414 + <a href="<?php echo get_edit_user_link( $user->ID ); ?>"><?php echo $user->display_name . ' (' . $user->user_login . ')'; ?></a>
415 +
416 + <?php
417 + } else {
418 + echo $user->display_name;
419 + }
420 + } else {
421 + echo '';
422 + } ?></span>
423 + <?php do_action( 'automatorwp_log_details_meta_box_after_user', $log ); ?>
424 + </div>
425 +
426 + <div class="misc-pub-section curtime misc-pub-curtime">
427 + <span id="timestamp"><?php echo __( 'Date:', 'automatorwp' ); ?> <b><?php echo date( 'Y/m/d H:i:s', strtotime( $log->date ) ); ?></b></span>
428 + </div>
429 +
430 + </div>
431 +
432 + </div>
433 +
434 + <div id="major-publishing-actions">
435 +
436 + <div id="delete-action">
437 + <?php
438 + printf(
439 + '<a href="%s" class="submitdelete deletion" onclick="%s" aria-label="%s">%s</a>',
440 + ct_get_delete_link( $ct_table->name, $log->id ),
441 + "return confirm('" .
442 + esc_attr( __( "Are you sure you want to delete this item?\\n\\nClick \\'Cancel\\' to go back, \\'OK\\' to confirm the delete." ) ) .
443 + "');",
444 + esc_attr( __( 'Delete', 'automatorwp' ) ),
445 + __( 'Delete', 'automatorwp' )
446 + );
447 + ?>
448 + </div>
449 +
450 + <div class="clear"></div>
451 +
452 + </div>
453 +
454 + </div>
455 + <?php
456 +}
457 +
458 +/**
459 + * Render the log data meta box
460 + *
461 + * @since 1.0.0
462 + *
463 + * @param stdClass $object The current object
464 + */
465 +function automatorwp_log_data_meta_box( $object = null ) {
466 +
467 + $log_fields = automatorwp_get_log_fields( $object );
468 +
469 + // Setup the CMB2 form
470 + $cmb2 = new CMB2( array(
471 + 'id' => 'automatorwp_logs_form',
472 + 'object_types' => array( 'automatorwp_logs' ),
473 + 'classes' => 'automatorwp-form automatorwp-log-form',
474 + 'hookup' => false,
475 + ), $object->id );
476 +
477 + // Setup the options fields
478 + foreach ( $log_fields as $field_id => $field ) {
479 +
480 + $field['id'] = $field_id;
481 +
482 + if( $field['type'] !== 'title' && ! isset( $field['before_field'] ) ) {
483 + $field['before_field'] = 'automatorwp_log_field_cb';
484 + }
485 +
486 + if( ! isset( $field['attributes'] ) ) {
487 + $field['attributes'] = array();
488 + }
489 +
490 + if( $field['type'] !== 'title' ) {
491 + $field['type'] = 'text';
492 + }
493 +
494 + $field['attributes']['type'] = 'hidden';
495 +
496 + // Update to use tooltips
497 + if( isset( $field['desc'] ) && $field['type'] !== 'title' ) {
498 + $field['tooltip'] = $field['desc'];
499 + $field['label_cb'] = 'cmb_tooltip_label_cb';
500 + unset( $field['desc'] );
501 + }
502 +
503 + // Add the field to the form
504 + $cmb2->add_field( $field );
505 +
506 + }
507 +
508 + // Render the form
509 + CMB2_Hookup::enqueue_cmb_css();
510 + CMB2_Hookup::enqueue_cmb_js();
511 + $cmb2->show_form();
512 +
513 + ?>
514 +
515 + <?php
516 +}
517 +
518 +/**
519 + * Get log fields
520 + *
521 + * @since 1.0.0
522 + *
523 + * @param stdClass $log The log object
524 + *
525 + * @return array
526 + */
527 +function automatorwp_get_log_fields( $log ) {
528 +
529 + // Setup the log field
530 + $log_fields = array();
531 + $object = false;
532 +
533 + switch( $log->type ) {
534 + case 'trigger':
535 + $log_fields = array(
536 + 'object_id' => array(
537 + 'name' => __( 'Trigger:', 'automatorwp' ),
538 + 'desc' => __( 'Trigger assigned to this log.', 'automatorwp' ),
539 + 'type' => 'text',
540 + ),
541 + 'automation' => array(
542 + 'name' => __( 'Automation:', 'automatorwp' ),
543 + 'desc' => __( 'Trigger\'s automation.', 'automatorwp' ),
544 + 'type' => 'text',
545 + ),
546 + );
547 +
548 + $object = automatorwp_get_trigger_object( $log->object_id );
549 + break;
550 + case 'action':
551 + $log_fields = array(
552 + 'object_id' => array(
553 + 'name' => __( 'Action:', 'automatorwp' ),
554 + 'desc' => __( 'Action assigned to this log.', 'automatorwp' ),
555 + 'type' => 'text',
556 + ),
557 + 'automation' => array(
558 + 'name' => __( 'Automation:', 'automatorwp' ),
559 + 'desc' => __( 'Action\'s automation.', 'automatorwp' ),
560 + 'type' => 'text',
561 + ),
562 + );
563 +
564 + $object = automatorwp_get_action_object( $log->object_id );
565 + break;
566 + case 'filter':
567 + $log_fields = array(
568 + 'object_id' => array(
569 + 'name' => __( 'Filter:', 'automatorwp' ),
570 + 'desc' => __( 'Filter assigned to this log.', 'automatorwp' ),
571 + 'type' => 'text',
572 + ),
573 + 'automation' => array(
574 + 'name' => __( 'Automation:', 'automatorwp' ),
575 + 'desc' => __( 'Filter\'s automation.', 'automatorwp' ),
576 + 'type' => 'text',
577 + ),
578 + );
579 +
580 + $item_type = automatorwp_get_log_meta( $log->id, 'item_type', true );
581 +
582 + if( $item_type === 'trigger' ) {
583 + $object = automatorwp_get_trigger_object( $log->object_id );
584 + } else if( $item_type === 'action' ) {
585 + $object = automatorwp_get_action_object( $log->object_id );
586 + }
587 + break;
588 + case 'automation':
589 + $log_fields = array(
590 + 'object_id' => array(
591 + 'name' => __( 'Automation:', 'automatorwp' ),
592 + 'desc' => __( 'Automation assigned to this log.', 'automatorwp' ),
593 + 'type' => 'text',
594 + ),
595 + );
596 +
597 + $object = automatorwp_get_automation_object( $log->object_id );
598 + break;
599 + }
600 +
601 + $log_fields['post_id'] = array(
602 + 'name' => __( 'Post:', 'automatorwp' ),
603 + 'desc' => __( 'Post assigned to this log.', 'automatorwp' ),
604 + 'type' => 'text',
605 + );
606 +
607 + /**
608 + * Filter to override the log if needed
609 + *
610 + * @since 6.0.0
611 + *
612 + * @param stdClass $log The log object
613 + * @param stdClass $object The trigger/action/automation object attached to the log
614 + *
615 + * @return stdClass
616 + */
617 + $log = apply_filters( 'automatorwp_log_fields_log', $log, $object );
618 +
619 + /**
620 + * Filter to override the object if needed
621 + *
622 + * @since 6.0.0
623 + *
624 + * @param stdClass $object The trigger/action/automation object attached to the log
625 + * @param stdClass $log The log object
626 + *
627 + * @return stdClass
628 + */
629 + $object = apply_filters( 'automatorwp_log_fields_object', $object, $log );
630 +
631 +
632 + /**
633 + * Filter to set custom log fields
634 + *
635 + * @since 1.0.0
636 + *
637 + * @param array $log_fields The log fields
638 + * @param stdClass $log The log object
639 + * @param stdClass $object The trigger/action/automation object attached to the log
640 + *
641 + * @return array
642 + */
643 + return apply_filters( 'automatorwp_log_fields', $log_fields, $log, $object );
644 +
645 +}
646 +
647 +/**
648 + * Callback used to render action on logs
649 + *
650 + * @since 1.0.0
651 + *
652 + * @param array $field_args
653 + * @param CMB2_Field $field
654 + */
655 +function automatorwp_log_field_cb( $field_args, $field ) {
656 +
657 + global $ct_table;
658 +
659 + // Setup vars
660 + $primary_key = $ct_table->db->primary_key;
661 + $log_id = absint( $_GET[$primary_key] );
662 + $log = ct_get_object( $log_id );
663 + $field_id = $field_args['id'];
664 + $value = $field->value();
665 +
666 + if( ! is_array( $value ) ) {
667 + $value = str_replace( '\n', "\n", $value );
668 + $value = stripslashes_deep( $value );
669 +
670 + $wpautop = ( isset( $field_args['wpautop'] ) ? $field_args['wpautop'] : false );
671 +
672 + if( $wpautop ) {
673 + $value = wpautop( $value );
674 + }
675 + }
676 +
677 + /**
678 + * Filters the field value display
679 + *
680 + * @since 1.0.0
681 + *
682 + * @param string $value Field value
683 + * @param array $field_args Field parameters
684 + * @param CMB2_Field $field Field object
685 + * @param stdClass $log Log object
686 + *
687 + * @return string
688 + */
689 + $value = apply_filters( "automatorwp_log_{$field_id}_field_value_display", $value, $field_args, $field, $log );
690 +
691 + // Check options_cb parameter
692 + if( isset( $field_args['options_cb'] ) && is_callable( $field_args['options_cb'] ) ) {
693 + $field_args['options'] = call_user_func( $field['options_cb'], (object) $field );
694 + }
695 +
696 + // Check options parameter
697 + if( isset( $field_args['options'] )
698 + && is_array( $field_args['options'] )
699 + && ! is_array( $value )
700 + && isset( $field_args['options'][$value] ) ) {
701 + // Set as value the option display instead
702 + $value = $field_args['options'][$value];
703 + }
704 +
705 + // if empty value, set it as a space
706 + if( empty( $value ) ) {
707 + $value = '&nbsp;';
708 + }
709 +
710 + // Check if value is an array
711 + if( is_array( $value ) ) {
712 + $value = automatorwp_log_array_display( $value );
713 + }
714 +
715 + echo $value;
716 +
717 +}
718 +
719 +/**
720 + * Setup the display HTML for array values
721 + *
722 + * @since 1.0.0
723 + *
724 + * @param array $value
725 + * @param int $level
726 + *
727 + * @return string
728 + */
729 +function automatorwp_log_array_display( $value, $level = 0 ) {
730 +
731 + // Check if not is an associative array
732 + if( $level === 0 && array_keys( $value ) === range( 0, count( $value ) - 1 ) && ! is_array( $value[0] ) ) {
733 + // Implode array values by a comma-separated list
734 + return implode( ', ', $value );
735 + }
736 +
737 + $padding_char = '&nbsp&nbsp&nbsp&nbsp';
738 + $new_value = '';
739 +
740 + // Only place the keys on first level
741 + if( $level !== 0 ) {
742 + $new_value .= '[' . '<br>';
743 + }
744 +
745 + foreach ( $value as $k => $v ) {
746 +
747 + // Add a inner padding per level
748 + $new_value .= str_repeat( $padding_char, $level );
749 +
750 + if ( is_array( $v ) ) {
751 +
752 + // Check if not is an associative array
753 + if( array_keys( $v ) === range( 0, count( $v ) - 1 ) && ! is_array( $v[0] ) ) {
754 + $v = array_map( 'esc_html', $v );
755 +
756 + // Implode array values by a comma-separated list
757 + $new_value .= esc_html( $k ) . ': [ ' . implode( ', ', $v ) . ' ]<br>';
758 + } else {
759 + // Display all sub arrays
760 + $new_value .= esc_html( $k ) . ': ' . automatorwp_log_array_display( $v, $level + 1 ) . '<br>';
761 + }
762 +
763 + } else {
764 + $new_value .= esc_html( $k ) . ': ' . esc_html( $v ) . '<br>';
765 + }
766 +
767 + }
768 +
769 + // Only place the keys on first level
770 + if( $level !== 0 ) {
771 + // Add a outer padding
772 + $new_value .= str_repeat( $padding_char, max( $level - 1, 0 ) ) . ']';
773 + }
774 +
775 + return $new_value;
776 +
777 +}
778 +
779 +/**
780 + * Object id field display
781 + *
782 + * @since 1.0.0
783 + *
784 + * @param string $value Field value
785 + * @param array $field_args Field parameters
786 + * @param CMB2_Field $field Field object
787 + * @param stdClass $log Log object
788 + *
789 + * @return string
790 + */
791 +function automatorwp_log_object_id_field_display( $value, $field_args, $field, $log ) {
792 +
793 + switch ( $log->type ) {
794 + case 'trigger':
795 + $trigger = automatorwp_get_trigger_object( $log->object_id );
796 +
797 + if( $trigger ) {
798 + $value = $trigger->title;
799 + }
800 + break;
801 + case 'action':
802 + $action = automatorwp_get_action_object( $log->object_id );
803 +
804 + if( $action ) {
805 + $value = $action->title;
806 + }
807 + break;
808 + case 'filter':
809 + $item_type = automatorwp_get_log_meta( $log->id, 'item_type', true );
810 +
811 + if( $item_type === 'trigger' ) {
812 + $object = automatorwp_get_trigger_object( $log->object_id );
813 + } else if( $item_type === 'action' ) {
814 + $object = automatorwp_get_action_object( $log->object_id );
815 + }
816 +
817 + if( $object ) {
818 + $value = $object->title;
819 + }
820 + break;
821 + case 'automation':
822 + $automation = automatorwp_get_automation_object( $log->object_id );
823 +
824 + if( $automation ) {
825 + $title = ! empty( $automation->title ) ? $automation->title : __( '(No title)', 'automatorwp' );
826 + $value = '<a href="' . ct_get_edit_link( 'automatorwp_automations', $automation->id ) . '">' . esc_html( $title ) . '</a>';
827 + }
828 + break;
829 + }
830 +
831 + return $value;
832 +
833 +}
834 +add_filter( 'automatorwp_log_object_id_field_value_display', 'automatorwp_log_object_id_field_display', 10, 4 );
835 +
836 +/**
837 + * Post id field display
838 + *
839 + * @since 1.0.0
840 + *
841 + * @param string $value Field value
842 + * @param array $field_args Field parameters
843 + * @param CMB2_Field $field Field object
844 + * @param stdClass $log Log object
845 + *
846 + * @return string
847 + */
848 +function automatorwp_log_post_id_field_display( $value, $field_args, $field, $log ) {
849 +
850 + $post = get_post( $value );
851 +
852 + if( $post ) {
853 +
854 + if( current_user_can( 'edit_post', $post->ID ) ) {
855 + $value = '<a href="' . get_edit_post_link( $post->ID ) . '">' . $post->post_title . '</a>';
856 + } else {
857 + $value = $post->post_title;
858 + }
859 +
860 + } else {
861 + $value = __( '(No post assigned)', 'automatorwp' );
862 + }
863 +
864 + return $value;
865 +
866 +}
867 +add_filter( 'automatorwp_log_post_id_field_value_display', 'automatorwp_log_post_id_field_display', 10, 4 );
868 +
869 +/**
870 + * User id field display
871 + *
872 + * @since 1.0.0
873 + *
874 + * @param string $value Field value
875 + * @param array $field_args Field parameters
876 + * @param CMB2_Field $field Field object
877 + * @param stdClass $log Log object
878 + *
879 + * @return string
880 + */
881 +function automatorwp_log_user_id_field_display( $value, $field_args, $field, $log ) {
882 +
883 + ct_setup_table( 'automatorwp_logs' );
884 + $value = ct_get_object_meta( $log->id, 'user_id', true );
885 + ct_reset_setup_table();
886 +
887 + $user = get_userdata( $value );
888 +
889 + if( $user ) {
890 +
891 + if( current_user_can( 'edit_user', $user->ID ) ) {
892 + $value = '<a href="' . get_edit_user_link( $user->ID ) . '">' . $user->display_name . '</a>';
893 + } else {
894 + $value = $user->display_name;
895 + }
896 +
897 + } else {
898 + $value = __( '(No user assigned)', 'automatorwp' );
899 + }
900 +
901 + return $value;
902 +
903 +}
904 +add_filter( 'automatorwp_log_user_id_field_value_display', 'automatorwp_log_user_id_field_display', 10, 4 );
905 +
906 +/**
907 + * User id (from log meta "user_id") field display
908 + *
909 + * @since 1.0.0
910 + *
911 + * @param string $value Field value
912 + * @param array $field_args Field parameters
913 + * @param CMB2_Field $field Field object
914 + * @param stdClass $log Log object
915 + *
916 + * @return string
917 + */
918 +function automatorwp_log_user_id_meta_field_display( $value, $field_args, $field, $log ) {
919 +
920 + ct_setup_table( 'automatorwp_logs' );
921 + $value = ct_get_object_meta( $log->id, 'user_id', true );
922 + ct_reset_setup_table();
923 +
924 + $user = get_userdata( $value );
925 +
926 + if( $user ) {
927 +
928 + if( current_user_can( 'edit_user', $user->ID ) ) {
929 + $value = '<a href="' . get_edit_user_link( $user->ID ) . '">' . $user->display_name . '</a>';
930 + } else {
931 + $value = $user->display_name;
932 + }
933 +
934 + } else {
935 + $value = __( '(No user assigned)', 'automatorwp' );
936 + }
937 +
938 + return $value;
939 +
940 +}
941 +add_filter( 'automatorwp_log_user_id_meta_field_value_display', 'automatorwp_log_user_id_meta_field_display', 10, 4 );
942 +
943 +/**
944 + * Automation field display
945 + *
946 + * @since 1.0.0
947 + *
948 + * @param string $value Field value
949 + * @param array $field_args Field parameters
950 + * @param CMB2_Field $field Field object
951 + * @param stdClass $log Log object
952 + *
953 + * @return string
954 + */
955 +function automatorwp_log_automation_field_display( $value, $field_args, $field, $log ) {
956 +
957 + $automation_id = false;
958 +
959 + switch ( $log->type ) {
960 + case 'trigger':
961 + $trigger = automatorwp_get_trigger_object( $log->object_id );
962 +
963 + if( $trigger ) {
964 + $automation_id = $trigger->automation_id;
965 + }
966 + break;
967 + case 'action':
968 + $action = automatorwp_get_action_object( $log->object_id );
969 +
970 + if( $action ) {
971 + $automation_id = $action->automation_id;
972 + }
973 + break;
974 + case 'filter':
975 + $item_type = automatorwp_get_log_meta( $log->id, 'item_type', true );
976 +
977 + if( $item_type === 'trigger' ) {
978 + $object = automatorwp_get_trigger_object( $log->object_id );
979 + } else if( $item_type === 'action' ) {
980 + $object = automatorwp_get_action_object( $log->object_id );
981 + }
982 +
983 + if( $object ) {
984 + $automation_id = $object->automation_id;
985 + }
986 + break;
987 + case 'automation':
988 + $automation_id = $log->object_id;
989 + break;
990 + }
991 +
992 + $automation = automatorwp_get_automation_object( $automation_id );
993 +
994 + if( $automation ) {
995 +
996 + $title = ! empty( $automation->title ) ? $automation->title : __( '(No title)', 'automatorwp' );
997 + $value = '<a href="' . ct_get_edit_link( 'automatorwp_automations', $automation_id ) . '">' . esc_html( $title ) . '</a>';
998 +
999 + }
1000 +
1001 + return $value;
1002 +
1003 +}
1004 +add_filter( 'automatorwp_log_automation_field_value_display', 'automatorwp_log_automation_field_display', 10, 4 );
1005 +
1006 +/**
1007 + * Display the "Clear all logs" button
1008 + *
1009 + * @since 1.0.0
1010 + *
1011 + * @param array $views
1012 + *
1013 + * @return array
1014 + */
1015 +function automatorwp_logs_clear_all_logs_button( $views ) {
1016 +
1017 + global $ct_table;
1018 +
1019 + if( $ct_table->name !== 'automatorwp_logs' ) {
1020 + return $views;
1021 + }
1022 +
1023 + $url = $ct_table->views->list->get_link();
1024 + $url = add_query_arg( array( 'automatorwp-action' => 'delete_all_logs' ), $url );
1025 + $url = add_query_arg( '_wpnonce', wp_create_nonce( 'automatorwp_delete_all_logs' ), $url );
1026 +
1027 + echo sprintf(
1028 + '<a href="%s" class="button automatorwp-button-danger automatorwp-clear-all-logs-button" onclick="%s" aria-label="%s">%s</a>',
1029 + $url,
1030 + "return confirm('" .
1031 + esc_attr( __( "Are you sure you want to delete all logs?\\n\\nClick \\'Cancel\\' to go back, \\'OK\\' to confirm the deletion.", 'automatorwp' ) ) .
1032 + "');",
1033 + esc_attr( __( 'Clear all logs', 'automatorwp' ) ),
1034 + __( 'Clear all logs', 'automatorwp' )
1035 + );
1036 +
1037 + return $views;
1038 +}
1039 +add_filter( 'views_logs', 'automatorwp_logs_clear_all_logs_button' );
1040 +
1041 +/**
1042 + * Process the deletion of all logs
1043 + *
1044 + * @since 1.0.0
1045 + */
1046 +function automatorwp_action_delete_all_logs() {
1047 +
1048 + global $wpdb;
1049 +
1050 + // Nonce check
1051 + if ( ! isset( $_REQUEST['_wpnonce'] ) ) {
1052 + wp_die( __( 'You are not allowed to perform this.', 'automatorwp' ) );
1053 + }
1054 +
1055 + if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'automatorwp_delete_all_logs' ) ) {
1056 + wp_die( __( 'You are not allowed to perform this.', 'automatorwp' ) );
1057 + }
1058 +
1059 + $ct_table = ct_setup_table( 'automatorwp_logs' );
1060 +
1061 + // If user can not delete it, bail
1062 + if ( ! current_user_can( $ct_table->cap->delete_items ) ) {
1063 + wp_die( __( 'You are not allowed to perform this.', 'automatorwp' ) );
1064 + }
1065 +
1066 + $logs = AutomatorWP()->db->logs;
1067 + $logs_meta = AutomatorWP()->db->logs_meta;
1068 +
1069 + // Delete all logs
1070 + $wpdb->query( "DELETE l FROM {$logs} AS l WHERE 1=1" );
1071 +
1072 + // Delete orphaned log metas
1073 + $wpdb->query( "DELETE lm FROM {$logs_meta} lm LEFT JOIN {$logs} l ON l.id = lm.id WHERE l.id IS NULL" );
1074 +
1075 + // Redirect to logs list view
1076 + wp_redirect( $ct_table->views->list->get_link() );
1077 + return;
1078 +}
1079 +add_action( 'automatorwp_action_delete_all_logs', 'automatorwp_action_delete_all_logs' );