| 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 ' '; |
| 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 ' '; |
| 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 ' '; |
| 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> |
| 338 |
<br> |
| 339 |
<span class="automatorwp-log-time"><?php echo $time; ?></span> |
| 340 |
|
| 341 |
<?php |
| 342 |
break; |
| 343 |
} |
| 344 |
} |
| 345 |
add_action( 'manage_automatorwp_logs_custom_column', 'automatorwp_manage_logs_custom_column', 10, 2 ); |
| 346 |
|
| 347 |
/** |
| 348 |
* Place the title at top of edit log screen |
| 349 |
* |
| 350 |
* @since 1.0.0 |
| 351 |
* |
| 352 |
* @param stdClass $log |
| 353 |
*/ |
| 354 |
function automatorwp_logs_edit_form_top( $log ) { |
| 355 |
global $ct_table; |
| 356 |
|
| 357 |
if( $ct_table->name !== 'automatorwp_logs' ) { |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
?> |
| 362 |
<div class="automatorwp-log-title-preview"> |
| 363 |
<h1> |
| 364 |
<?php automatorwp_get_log_integration_icon( $log ); ?> |
| 365 |
<?php echo ( ! empty( $log->title ) ? esc_html( $log->title ) : __( '(No title)', 'automatorwp' ) ); ?> |
| 366 |
</h1> |
| 367 |
</div> |
| 368 |
<?php |
| 369 |
} |
| 370 |
add_action( 'ct_edit_form_top', 'automatorwp_logs_edit_form_top' ); |
| 371 |
|
| 372 |
/** |
| 373 |
* Logs meta boxes |
| 374 |
* |
| 375 |
* @since 1.0.0 |
| 376 |
*/ |
| 377 |
function automatorwp_add_logs_meta_boxes() { |
| 378 |
remove_meta_box( 'submitdiv', 'automatorwp_logs', 'side' ); |
| 379 |
add_meta_box( 'automatorwp_log_details', __( 'Log Details', 'automatorwp' ), 'automatorwp_log_details_meta_box', 'automatorwp_logs', 'side', 'default' ); |
| 380 |
add_meta_box( 'automatorwp_log_data', __( 'Log Data', 'automatorwp' ), 'automatorwp_log_data_meta_box', 'automatorwp_logs', 'normal', 'default' ); |
| 381 |
} |
| 382 |
add_action( 'add_meta_boxes', 'automatorwp_add_logs_meta_boxes' ); |
| 383 |
|
| 384 |
/** |
| 385 |
* Render the log details meta box |
| 386 |
* |
| 387 |
* @since 1.0.0 |
| 388 |
* |
| 389 |
* @param stdClass $object The current object |
| 390 |
*/ |
| 391 |
function automatorwp_log_details_meta_box( $object = null ) { |
| 392 |
|
| 393 |
global $ct_table; |
| 394 |
|
| 395 |
// Setup vars |
| 396 |
$log_types = automatorwp_get_log_types(); |
| 397 |
|
| 398 |
?> |
| 399 |
<div class="submitbox" id="submitpost" style="margin: -6px -12px -12px;"> |
| 400 |
|
| 401 |
<div id="minor-publishing"> |
| 402 |
|
| 403 |
<div id="misc-publishing-actions"> |
| 404 |
|
| 405 |
<div class="misc-pub-section misc-pub-post-status"> |
| 406 |
<?php echo __( 'Type:', 'automatorwp' ); ?> <span id="post-status-display"><?php echo isset( $log_types[$object->type] ) ? $log_types[$object->type] : esc_html( $object->type ) ; ?></span> |
| 407 |
</div> |
| 408 |
|
| 409 |
<div class="misc-pub-section misc-pub-post-user" id="user"> |
| 410 |
<?php $user = get_userdata( $object->user_id ); ?> |
| 411 |
<?php echo __( 'User:', 'automatorwp' ); ?> <span id="post-user-display"><?php |
| 412 |
if( $user ) { |
| 413 |
if( current_user_can( 'edit_users' ) ) { |
| 414 |
?> |
| 415 |
|
| 416 |
<a href="<?php echo get_edit_user_link( $user->ID ); ?>"><?php echo $user->display_name . ' (' . $user->user_login . ')'; ?></a> |
| 417 |
|
| 418 |
<?php |
| 419 |
} else { |
| 420 |
echo $user->display_name; |
| 421 |
} |
| 422 |
} else { |
| 423 |
echo ''; |
| 424 |
} ?></span> |
| 425 |
</div> |
| 426 |
|
| 427 |
<div class="misc-pub-section curtime misc-pub-curtime"> |
| 428 |
<span id="timestamp"><?php echo __( 'Date:', 'automatorwp' ); ?> <b><?php echo date( 'Y/m/d H:i:s', strtotime( $object->date ) ); ?></b></span> |
| 429 |
</div> |
| 430 |
|
| 431 |
</div> |
| 432 |
|
| 433 |
</div> |
| 434 |
|
| 435 |
<div id="major-publishing-actions"> |
| 436 |
|
| 437 |
<div id="delete-action"> |
| 438 |
<?php |
| 439 |
printf( |
| 440 |
'<a href="%s" class="submitdelete deletion" onclick="%s" aria-label="%s">%s</a>', |
| 441 |
ct_get_delete_link( $ct_table->name, $object->id ), |
| 442 |
"return confirm('" . |
| 443 |
esc_attr( __( "Are you sure you want to delete this item?\\n\\nClick \\'Cancel\\' to go back, \\'OK\\' to confirm the delete." ) ) . |
| 444 |
"');", |
| 445 |
esc_attr( __( 'Delete permanently' ) ), |
| 446 |
__( 'Delete Permanently' ) |
| 447 |
); |
| 448 |
?> |
| 449 |
</div> |
| 450 |
|
| 451 |
<div class="clear"></div> |
| 452 |
|
| 453 |
</div> |
| 454 |
|
| 455 |
</div> |
| 456 |
<?php |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* Render the log data meta box |
| 461 |
* |
| 462 |
* @since 1.0.0 |
| 463 |
* |
| 464 |
* @param stdClass $object The current object |
| 465 |
*/ |
| 466 |
function automatorwp_log_data_meta_box( $object = null ) { |
| 467 |
|
| 468 |
$log_fields = automatorwp_get_log_fields( $object ); |
| 469 |
|
| 470 |
// Setup the CMB2 form |
| 471 |
$cmb2 = new CMB2( array( |
| 472 |
'id' => 'automatorwp_logs_form', |
| 473 |
'object_types' => array( 'automatorwp_logs' ), |
| 474 |
'classes' => 'automatorwp-form automatorwp-log-form', |
| 475 |
'hookup' => false, |
| 476 |
), $object->id ); |
| 477 |
|
| 478 |
// Setup the options fields |
| 479 |
foreach ( $log_fields as $field_id => $field ) { |
| 480 |
|
| 481 |
$field['id'] = $field_id; |
| 482 |
|
| 483 |
if( $field['type'] !== 'title' && ! isset( $field['before_field'] ) ) { |
| 484 |
$field['before_field'] = 'automatorwp_log_field_cb'; |
| 485 |
} |
| 486 |
|
| 487 |
if( ! isset( $field['attributes'] ) ) { |
| 488 |
$field['attributes'] = array(); |
| 489 |
} |
| 490 |
|
| 491 |
if( $field['type'] !== 'title' ) { |
| 492 |
$field['type'] = 'text'; |
| 493 |
} |
| 494 |
|
| 495 |
$field['attributes']['type'] = 'hidden'; |
| 496 |
|
| 497 |
// Add the field to the form |
| 498 |
$cmb2->add_field( $field ); |
| 499 |
|
| 500 |
} |
| 501 |
|
| 502 |
// Render the form |
| 503 |
CMB2_Hookup::enqueue_cmb_css(); |
| 504 |
CMB2_Hookup::enqueue_cmb_js(); |
| 505 |
$cmb2->show_form(); |
| 506 |
|
| 507 |
?> |
| 508 |
|
| 509 |
<?php |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Get log fields |
| 514 |
* |
| 515 |
* @since 1.0.0 |
| 516 |
* |
| 517 |
* @param stdClass $log The log object |
| 518 |
* |
| 519 |
* @return array |
| 520 |
*/ |
| 521 |
function automatorwp_get_log_fields( $log ) { |
| 522 |
|
| 523 |
// Setup the log field |
| 524 |
$log_fields = array(); |
| 525 |
$object = false; |
| 526 |
|
| 527 |
switch( $log->type ) { |
| 528 |
case 'trigger': |
| 529 |
$log_fields = array( |
| 530 |
'object_id' => array( |
| 531 |
'name' => __( 'Trigger:', 'automatorwp' ), |
| 532 |
'desc' => __( 'Trigger assigned to this log.', 'automatorwp' ), |
| 533 |
'type' => 'text', |
| 534 |
), |
| 535 |
'automation' => array( |
| 536 |
'name' => __( 'Automation:', 'automatorwp' ), |
| 537 |
'desc' => __( 'Trigger\'s automation.', 'automatorwp' ), |
| 538 |
'type' => 'text', |
| 539 |
), |
| 540 |
); |
| 541 |
|
| 542 |
$object = automatorwp_get_trigger_object( $log->object_id ); |
| 543 |
break; |
| 544 |
case 'action': |
| 545 |
$log_fields = array( |
| 546 |
'object_id' => array( |
| 547 |
'name' => __( 'Action:', 'automatorwp' ), |
| 548 |
'desc' => __( 'Action assigned to this log.', 'automatorwp' ), |
| 549 |
'type' => 'text', |
| 550 |
), |
| 551 |
'automation' => array( |
| 552 |
'name' => __( 'Automation:', 'automatorwp' ), |
| 553 |
'desc' => __( 'Action\'s automation.', 'automatorwp' ), |
| 554 |
'type' => 'text', |
| 555 |
), |
| 556 |
); |
| 557 |
|
| 558 |
$object = automatorwp_get_action_object( $log->object_id ); |
| 559 |
break; |
| 560 |
case 'filter': |
| 561 |
$log_fields = array( |
| 562 |
'object_id' => array( |
| 563 |
'name' => __( 'Filter:', 'automatorwp' ), |
| 564 |
'desc' => __( 'Filter assigned to this log.', 'automatorwp' ), |
| 565 |
'type' => 'text', |
| 566 |
), |
| 567 |
'automation' => array( |
| 568 |
'name' => __( 'Automation:', 'automatorwp' ), |
| 569 |
'desc' => __( 'Filter\'s automation.', 'automatorwp' ), |
| 570 |
'type' => 'text', |
| 571 |
), |
| 572 |
); |
| 573 |
|
| 574 |
$item_type = automatorwp_get_log_meta( $log->id, 'item_type', true ); |
| 575 |
|
| 576 |
if( $item_type === 'trigger' ) { |
| 577 |
$object = automatorwp_get_trigger_object( $log->object_id ); |
| 578 |
} else if( $item_type === 'action' ) { |
| 579 |
$object = automatorwp_get_action_object( $log->object_id ); |
| 580 |
} |
| 581 |
break; |
| 582 |
case 'automation': |
| 583 |
$log_fields = array( |
| 584 |
'object_id' => array( |
| 585 |
'name' => __( 'Automation:', 'automatorwp' ), |
| 586 |
'desc' => __( 'Automation assigned to this log.', 'automatorwp' ), |
| 587 |
'type' => 'text', |
| 588 |
), |
| 589 |
); |
| 590 |
|
| 591 |
$object = automatorwp_get_automation_object( $log->object_id ); |
| 592 |
break; |
| 593 |
} |
| 594 |
|
| 595 |
$log_fields['post_id'] = array( |
| 596 |
'name' => __( 'Post:', 'automatorwp' ), |
| 597 |
'desc' => __( 'Post assigned to this log.', 'automatorwp' ), |
| 598 |
'type' => 'text', |
| 599 |
); |
| 600 |
|
| 601 |
/** |
| 602 |
* Filter to set custom log fields |
| 603 |
* |
| 604 |
* @since 1.0.0 |
| 605 |
* |
| 606 |
* @param array $log_fields The log fields |
| 607 |
* @param stdClass $log The log object |
| 608 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 609 |
* |
| 610 |
* @return array |
| 611 |
*/ |
| 612 |
return apply_filters( 'automatorwp_log_fields', $log_fields, $log, $object ); |
| 613 |
|
| 614 |
} |
| 615 |
|
| 616 |
/** |
| 617 |
* Callback used to render action on logs |
| 618 |
* |
| 619 |
* @since 1.0.0 |
| 620 |
* |
| 621 |
* @param array $field_args |
| 622 |
* @param CMB2_Field $field |
| 623 |
*/ |
| 624 |
function automatorwp_log_field_cb( $field_args, $field ) { |
| 625 |
|
| 626 |
global $ct_table; |
| 627 |
|
| 628 |
// Setup vars |
| 629 |
$primary_key = $ct_table->db->primary_key; |
| 630 |
$log_id = absint( $_GET[$primary_key] ); |
| 631 |
$log = ct_get_object( $log_id ); |
| 632 |
$field_id = $field_args['id']; |
| 633 |
$value = $field->value(); |
| 634 |
|
| 635 |
if( ! is_array( $value ) ) { |
| 636 |
$value = str_replace( '\n', "\n", $value ); |
| 637 |
$value = stripslashes_deep( $value ); |
| 638 |
|
| 639 |
$wpautop = ( isset( $field_args['wpautop'] ) ? $field_args['wpautop'] : false ); |
| 640 |
|
| 641 |
if( $wpautop ) { |
| 642 |
$value = wpautop( $value ); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* Filters the field value display |
| 648 |
* |
| 649 |
* @since 1.0.0 |
| 650 |
* |
| 651 |
* @param string $value Field value |
| 652 |
* @param array $field_args Field parameters |
| 653 |
* @param CMB2_Field $field Field object |
| 654 |
* @param stdClass $log Log object |
| 655 |
* |
| 656 |
* @return string |
| 657 |
*/ |
| 658 |
$value = apply_filters( "automatorwp_log_{$field_id}_field_value_display", $value, $field_args, $field, $log ); |
| 659 |
|
| 660 |
// Check options_cb parameter |
| 661 |
if( isset( $field_args['options_cb'] ) && is_callable( $field_args['options_cb'] ) ) { |
| 662 |
$field_args['options'] = call_user_func( $field['options_cb'], (object) $field ); |
| 663 |
} |
| 664 |
|
| 665 |
// Check options parameter |
| 666 |
if( isset( $field_args['options'] ) |
| 667 |
&& is_array( $field_args['options'] ) |
| 668 |
&& ! is_array( $value ) |
| 669 |
&& isset( $field_args['options'][$value] ) ) { |
| 670 |
// Set as value the option display instead |
| 671 |
$value = $field_args['options'][$value]; |
| 672 |
} |
| 673 |
|
| 674 |
// if empty value, set it as a space |
| 675 |
if( empty( $value ) ) { |
| 676 |
$value = ' '; |
| 677 |
} |
| 678 |
|
| 679 |
// Check if value is an array |
| 680 |
if( is_array( $value ) ) { |
| 681 |
$value = automatorwp_log_array_display( $value ); |
| 682 |
} |
| 683 |
|
| 684 |
echo $value; |
| 685 |
|
| 686 |
} |
| 687 |
|
| 688 |
/** |
| 689 |
* Setup the display HTML for array values |
| 690 |
* |
| 691 |
* @since 1.0.0 |
| 692 |
* |
| 693 |
* @param array $value |
| 694 |
* @param int $level |
| 695 |
* |
| 696 |
* @return string |
| 697 |
*/ |
| 698 |
function automatorwp_log_array_display( $value, $level = 0 ) { |
| 699 |
|
| 700 |
// Check if not is an associative array |
| 701 |
if( $level === 0 && array_keys( $value ) === range( 0, count( $value ) - 1 ) && ! is_array( $value[0] ) ) { |
| 702 |
// Implode array values by a comma-separated list |
| 703 |
return implode( ', ', $value ); |
| 704 |
} |
| 705 |
|
| 706 |
$padding_char = '    '; |
| 707 |
$new_value = ''; |
| 708 |
|
| 709 |
// Only place the keys on first level |
| 710 |
if( $level !== 0 ) { |
| 711 |
$new_value .= '[' . '<br>'; |
| 712 |
} |
| 713 |
|
| 714 |
foreach ( $value as $k => $v ) { |
| 715 |
|
| 716 |
// Add a inner padding per level |
| 717 |
$new_value .= str_repeat( $padding_char, $level ); |
| 718 |
|
| 719 |
if ( is_array( $v ) ) { |
| 720 |
|
| 721 |
// Check if not is an associative array |
| 722 |
if( array_keys( $v ) === range( 0, count( $v ) - 1 ) && ! is_array( $v[0] ) ) { |
| 723 |
$v = array_map( 'esc_html', $v ); |
| 724 |
|
| 725 |
// Implode array values by a comma-separated list |
| 726 |
$new_value .= esc_html( $k ) . ': [ ' . implode( ', ', $v ) . ' ]<br>'; |
| 727 |
} else { |
| 728 |
// Display all sub arrays |
| 729 |
$new_value .= esc_html( $k ) . ': ' . automatorwp_log_array_display( $v, $level + 1 ) . '<br>'; |
| 730 |
} |
| 731 |
|
| 732 |
} else { |
| 733 |
$new_value .= esc_html( $k ) . ': ' . esc_html( $v ) . '<br>'; |
| 734 |
} |
| 735 |
|
| 736 |
} |
| 737 |
|
| 738 |
// Only place the keys on first level |
| 739 |
if( $level !== 0 ) { |
| 740 |
// Add a outer padding |
| 741 |
$new_value .= str_repeat( $padding_char, max( $level - 1, 0 ) ) . ']'; |
| 742 |
} |
| 743 |
|
| 744 |
return $new_value; |
| 745 |
|
| 746 |
} |
| 747 |
|
| 748 |
/** |
| 749 |
* Object id field display |
| 750 |
* |
| 751 |
* @since 1.0.0 |
| 752 |
* |
| 753 |
* @param string $value Field value |
| 754 |
* @param array $field_args Field parameters |
| 755 |
* @param CMB2_Field $field Field object |
| 756 |
* @param stdClass $log Log object |
| 757 |
* |
| 758 |
* @return string |
| 759 |
*/ |
| 760 |
function automatorwp_log_object_id_field_display( $value, $field_args, $field, $log ) { |
| 761 |
|
| 762 |
switch ( $log->type ) { |
| 763 |
case 'trigger': |
| 764 |
$trigger = automatorwp_get_trigger_object( $log->object_id ); |
| 765 |
|
| 766 |
if( $trigger ) { |
| 767 |
$value = $trigger->title; |
| 768 |
} |
| 769 |
break; |
| 770 |
case 'action': |
| 771 |
$action = automatorwp_get_action_object( $log->object_id ); |
| 772 |
|
| 773 |
if( $action ) { |
| 774 |
$value = $action->title; |
| 775 |
} |
| 776 |
break; |
| 777 |
case 'filter': |
| 778 |
$item_type = automatorwp_get_log_meta( $log->id, 'item_type', true ); |
| 779 |
|
| 780 |
if( $item_type === 'trigger' ) { |
| 781 |
$object = automatorwp_get_trigger_object( $log->object_id ); |
| 782 |
} else if( $item_type === 'action' ) { |
| 783 |
$object = automatorwp_get_action_object( $log->object_id ); |
| 784 |
} |
| 785 |
|
| 786 |
if( $object ) { |
| 787 |
$value = $object->title; |
| 788 |
} |
| 789 |
break; |
| 790 |
case 'automation': |
| 791 |
$automation = automatorwp_get_automation_object( $log->object_id ); |
| 792 |
|
| 793 |
if( $automation ) { |
| 794 |
$title = ! empty( $automation->title ) ? $automation->title : __( '(No title)', 'automatorwp' ); |
| 795 |
$value = '<a href="' . ct_get_edit_link( 'automatorwp_automations', $automation->id ) . '">' . esc_html( $title ) . '</a>'; |
| 796 |
} |
| 797 |
break; |
| 798 |
} |
| 799 |
|
| 800 |
return $value; |
| 801 |
|
| 802 |
} |
| 803 |
add_filter( 'automatorwp_log_object_id_field_value_display', 'automatorwp_log_object_id_field_display', 10, 4 ); |
| 804 |
|
| 805 |
/** |
| 806 |
* Post id field display |
| 807 |
* |
| 808 |
* @since 1.0.0 |
| 809 |
* |
| 810 |
* @param string $value Field value |
| 811 |
* @param array $field_args Field parameters |
| 812 |
* @param CMB2_Field $field Field object |
| 813 |
* @param stdClass $log Log object |
| 814 |
* |
| 815 |
* @return string |
| 816 |
*/ |
| 817 |
function automatorwp_log_post_id_field_display( $value, $field_args, $field, $log ) { |
| 818 |
|
| 819 |
$post = get_post( $value ); |
| 820 |
|
| 821 |
if( $post ) { |
| 822 |
|
| 823 |
if( current_user_can( 'edit_post', $post->ID ) ) { |
| 824 |
$value = '<a href="' . get_edit_post_link( $post->ID ) . '">' . $post->post_title . '</a>'; |
| 825 |
} else { |
| 826 |
$value = $post->post_title; |
| 827 |
} |
| 828 |
|
| 829 |
} else { |
| 830 |
$value = __( '(No post assigned)', 'automatorwp' ); |
| 831 |
} |
| 832 |
|
| 833 |
return $value; |
| 834 |
|
| 835 |
} |
| 836 |
add_filter( 'automatorwp_log_post_id_field_value_display', 'automatorwp_log_post_id_field_display', 10, 4 ); |
| 837 |
|
| 838 |
/** |
| 839 |
* Automation field display |
| 840 |
* |
| 841 |
* @since 1.0.0 |
| 842 |
* |
| 843 |
* @param string $value Field value |
| 844 |
* @param array $field_args Field parameters |
| 845 |
* @param CMB2_Field $field Field object |
| 846 |
* @param stdClass $log Log object |
| 847 |
* |
| 848 |
* @return string |
| 849 |
*/ |
| 850 |
function automatorwp_log_automation_field_display( $value, $field_args, $field, $log ) { |
| 851 |
|
| 852 |
$automation_id = false; |
| 853 |
|
| 854 |
switch ( $log->type ) { |
| 855 |
case 'trigger': |
| 856 |
$trigger = automatorwp_get_trigger_object( $log->object_id ); |
| 857 |
|
| 858 |
if( $trigger ) { |
| 859 |
$automation_id = $trigger->automation_id; |
| 860 |
} |
| 861 |
break; |
| 862 |
case 'action': |
| 863 |
$action = automatorwp_get_action_object( $log->object_id ); |
| 864 |
|
| 865 |
if( $action ) { |
| 866 |
$automation_id = $action->automation_id; |
| 867 |
} |
| 868 |
break; |
| 869 |
case 'filter': |
| 870 |
$item_type = automatorwp_get_log_meta( $log->id, 'item_type', true ); |
| 871 |
|
| 872 |
if( $item_type === 'trigger' ) { |
| 873 |
$object = automatorwp_get_trigger_object( $log->object_id ); |
| 874 |
} else if( $item_type === 'action' ) { |
| 875 |
$object = automatorwp_get_action_object( $log->object_id ); |
| 876 |
} |
| 877 |
|
| 878 |
if( $object ) { |
| 879 |
$automation_id = $object->automation_id; |
| 880 |
} |
| 881 |
break; |
| 882 |
case 'automation': |
| 883 |
$automation_id = $log->object_id; |
| 884 |
break; |
| 885 |
} |
| 886 |
|
| 887 |
$automation = automatorwp_get_automation_object( $automation_id ); |
| 888 |
|
| 889 |
if( $automation ) { |
| 890 |
|
| 891 |
$title = ! empty( $automation->title ) ? $automation->title : __( '(No title)', 'automatorwp' ); |
| 892 |
$value = '<a href="' . ct_get_edit_link( 'automatorwp_automations', $automation_id ) . '">' . esc_html( $title ) . '</a>'; |
| 893 |
|
| 894 |
} |
| 895 |
|
| 896 |
return $value; |
| 897 |
|
| 898 |
} |
| 899 |
add_filter( 'automatorwp_log_automation_field_value_display', 'automatorwp_log_automation_field_display', 10, 4 ); |
| 900 |
|
| 901 |
/** |
| 902 |
* Display the "Clear all logs" button |
| 903 |
* |
| 904 |
* @since 1.0.0 |
| 905 |
* |
| 906 |
* @param array $views |
| 907 |
* |
| 908 |
* @return array |
| 909 |
*/ |
| 910 |
function automatorwp_logs_clear_all_logs_button( $views ) { |
| 911 |
|
| 912 |
global $ct_table; |
| 913 |
|
| 914 |
if( $ct_table->name !== 'automatorwp_logs' ) { |
| 915 |
return $views; |
| 916 |
} |
| 917 |
|
| 918 |
$url = $ct_table->views->list->get_link(); |
| 919 |
$url = add_query_arg( array( 'automatorwp-action' => 'delete_all_logs' ), $url ); |
| 920 |
$url = add_query_arg( '_wpnonce', wp_create_nonce( 'automatorwp_delete_all_logs' ), $url ); |
| 921 |
|
| 922 |
echo sprintf( |
| 923 |
'<a href="%s" class="button automatorwp-button-danger automatorwp-clear-all-logs-button" onclick="%s" aria-label="%s">%s</a>', |
| 924 |
$url, |
| 925 |
"return confirm('" . |
| 926 |
esc_attr( __( "Are you sure you want to delete all logs?\\n\\nClick \\'Cancel\\' to go back, \\'OK\\' to confirm the deletion.", 'automatorwp' ) ) . |
| 927 |
"');", |
| 928 |
esc_attr( __( 'Clear all logs', 'automatorwp' ) ), |
| 929 |
__( 'Clear all logs', 'automatorwp' ) |
| 930 |
); |
| 931 |
|
| 932 |
return $views; |
| 933 |
} |
| 934 |
add_filter( 'views_logs', 'automatorwp_logs_clear_all_logs_button' ); |
| 935 |
|
| 936 |
/** |
| 937 |
* Process the deletion of all logs |
| 938 |
* |
| 939 |
* @since 1.0.0 |
| 940 |
*/ |
| 941 |
function automatorwp_action_delete_all_logs() { |
| 942 |
|
| 943 |
global $wpdb; |
| 944 |
|
| 945 |
// Nonce check |
| 946 |
if ( ! isset( $_REQUEST['_wpnonce'] ) ) { |
| 947 |
wp_die( __( 'You are not allowed to perform this.', 'automatorwp' ) ); |
| 948 |
} |
| 949 |
|
| 950 |
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'automatorwp_delete_all_logs' ) ) { |
| 951 |
wp_die( __( 'You are not allowed to perform this.', 'automatorwp' ) ); |
| 952 |
} |
| 953 |
|
| 954 |
$ct_table = ct_setup_table( 'automatorwp_logs' ); |
| 955 |
|
| 956 |
// If user can not delete it, bail |
| 957 |
if ( ! current_user_can( $ct_table->cap->delete_items ) ) { |
| 958 |
wp_die( __( 'You are not allowed to perform this.', 'automatorwp' ) ); |
| 959 |
} |
| 960 |
|
| 961 |
$logs = AutomatorWP()->db->logs; |
| 962 |
$logs_meta = AutomatorWP()->db->logs_meta; |
| 963 |
|
| 964 |
// Delete all logs |
| 965 |
$wpdb->query( "DELETE l FROM {$logs} AS l WHERE 1=1" ); |
| 966 |
|
| 967 |
// Delete orphaned log metas |
| 968 |
$wpdb->query( "DELETE lm FROM {$logs_meta} lm LEFT JOIN {$logs} l ON l.id = lm.id WHERE l.id IS NULL" ); |
| 969 |
|
| 970 |
// Redirect to logs list view |
| 971 |
wp_redirect( $ct_table->views->list->get_link() ); |
| 972 |
return; |
| 973 |
} |
| 974 |
add_action( 'automatorwp_action_delete_all_logs', 'automatorwp_action_delete_all_logs' ); |