| @@ -1,0 +1,371 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Log | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Log | |
| 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 | + * Get log registered types | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * | |
| 17 | + * @return array | |
| 18 | + */ | |
| 19 | +function automatorwp_get_log_types() { | |
| 20 | + | |
| 21 | + return apply_filters( 'automatorwp_log_types', array( | |
| 22 | + 'automation' => __( 'Automation', 'automatorwp' ), | |
| 23 | + 'anonymous' => __( 'Anonymous', 'automatorwp' ), | |
| 24 | + 'trigger' => __( 'Trigger', 'automatorwp' ), | |
| 25 | + 'action' => __( 'Action', 'automatorwp' ), | |
| 26 | + 'filter' => __( 'Filter', 'automatorwp' ), | |
| 27 | + ) ); | |
| 28 | + | |
| 29 | +} | |
| 30 | + | |
| 31 | +/** | |
| 32 | + * Insert a new log | |
| 33 | + * | |
| 34 | + * @since 1.0.0 | |
| 35 | + * | |
| 36 | + * @param array $log_data The log data to insert | |
| 37 | + * @param array $log_meta The log meta data to insert | |
| 38 | + * | |
| 39 | + * @return int|WP_Error The log ID on success. The value 0 or WP_Error on failure. | |
| 40 | + */ | |
| 41 | +function automatorwp_insert_log( $log_data = array(), $log_meta = array() ) { | |
| 42 | + | |
| 43 | + global $wpdb; | |
| 44 | + | |
| 45 | + $log_data = wp_parse_args( $log_data, array( | |
| 46 | + 'title' => '', | |
| 47 | + 'type' => '', | |
| 48 | + 'object_id' => 0, | |
| 49 | + 'user_id' => 0, | |
| 50 | + 'post_id' => 0, | |
| 51 | + 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ), | |
| 52 | + ) ); | |
| 53 | + | |
| 54 | + $ct_table = ct_setup_table( 'automatorwp_logs' ); | |
| 55 | + | |
| 56 | + // Store log entry | |
| 57 | + $log_id = $ct_table->db->insert( $log_data ); | |
| 58 | + | |
| 59 | + // If log correctly inserted, insert all meta data received | |
| 60 | + if( $log_id && ! empty( $log_meta ) ) { | |
| 61 | + | |
| 62 | + $log_data['log_id'] = $log_id; | |
| 63 | + | |
| 64 | + $metas = array(); | |
| 65 | + | |
| 66 | + foreach( $log_meta as $meta_key => $meta_value ) { | |
| 67 | + $original_meta_key = $meta_key; | |
| 68 | + $original_meta_value = $meta_value; | |
| 69 | + | |
| 70 | + // Sanitize vars | |
| 71 | + $meta_key = sanitize_key( $meta_key ); | |
| 72 | + $meta_key = wp_unslash( $meta_key ); | |
| 73 | + $meta_value = wp_unslash( $meta_value ); | |
| 74 | + $meta_value = esc_sql( $meta_value ); | |
| 75 | + $meta_value = sanitize_meta( $meta_key, $meta_value, $ct_table->name ); | |
| 76 | + $meta_value = maybe_serialize( $meta_value ); | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * Filter to override the meta value | |
| 80 | + * | |
| 81 | + * @since 1.0.0 | |
| 82 | + * | |
| 83 | + * @param string $meta_value The parsed meta value | |
| 84 | + * @param string $meta_key The parsed meta key | |
| 85 | + * @param string $original_meta_value The original meta value | |
| 86 | + * @param string $original_meta_key The original meta key | |
| 87 | + * @param array $log_data The log data (includes the log_id key) | |
| 88 | + * @param array $log_meta The original log meta data | |
| 89 | + * | |
| 90 | + * @return mixed | |
| 91 | + */ | |
| 92 | + $meta_value = apply_filters( 'automatorwp_insert_log_meta_value', $meta_value, $meta_key, $original_meta_value, $original_meta_key, $log_data, $log_meta ); | |
| 93 | + | |
| 94 | + // Setup the insert value | |
| 95 | + $metas[] = $wpdb->prepare( '%d, %s, %s', array( $log_id, $meta_key, $meta_value ) ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + $logs_meta = AutomatorWP()->db->logs_meta; | |
| 99 | + $metas = implode( '), (', $metas ); | |
| 100 | + | |
| 101 | + // Since the log is recently inserted, is faster to run a single query to insert all metas instead of insert them one-by-one | |
| 102 | + $wpdb->query( "INSERT INTO {$logs_meta} (id, meta_key, meta_value) VALUES ({$metas})" ); | |
| 103 | + | |
| 104 | + } | |
| 105 | + | |
| 106 | + ct_reset_setup_table(); | |
| 107 | + | |
| 108 | + // Flush cache to prevent meta data cached values | |
| 109 | + wp_cache_flush(); | |
| 110 | + | |
| 111 | + return $log_id; | |
| 112 | + | |
| 113 | +} | |
| 114 | + | |
| 115 | +/** | |
| 116 | + * Get the log object data | |
| 117 | + * | |
| 118 | + * @since 1.0.0 | |
| 119 | + * | |
| 120 | + * @param int $log_id The log ID | |
| 121 | + * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to | |
| 122 | + * a object, an associative array, or a numeric array, respectively. Default OBJECT. | |
| 123 | + * @return array|stdClass|null | |
| 124 | + */ | |
| 125 | +function automatorwp_get_log_object( $log_id, $output = OBJECT ) { | |
| 126 | + | |
| 127 | + ct_setup_table( 'automatorwp_logs' ); | |
| 128 | + | |
| 129 | + $log = ct_get_object( $log_id ); | |
| 130 | + | |
| 131 | + ct_reset_setup_table(); | |
| 132 | + | |
| 133 | + return $log; | |
| 134 | + | |
| 135 | +} | |
| 136 | + | |
| 137 | +/** | |
| 138 | + * Get the log object data | |
| 139 | + * | |
| 140 | + * @since 1.0.0 | |
| 141 | + * | |
| 142 | + * @param int $log_id The log ID | |
| 143 | + * @param string $meta_key Optional. The meta key to retrieve. By default, returns | |
| 144 | + * data for all keys. Default empty. | |
| 145 | + * @param bool $single Optional. Whether to return a single value. Default false. | |
| 146 | + * | |
| 147 | + * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. | |
| 148 | + */ | |
| 149 | +function automatorwp_get_log_meta( $log_id, $meta_key = '', $single = false ) { | |
| 150 | + | |
| 151 | + ct_setup_table( 'automatorwp_logs' ); | |
| 152 | + | |
| 153 | + $meta_value = ct_get_object_meta( $log_id, $meta_key, $single ); | |
| 154 | + | |
| 155 | + ct_reset_setup_table(); | |
| 156 | + | |
| 157 | + return $meta_value; | |
| 158 | + | |
| 159 | +} | |
| 160 | + | |
| 161 | +/** | |
| 162 | + * Get the log integration icon HTML markup | |
| 163 | + * | |
| 164 | + * @since 1.0.0 | |
| 165 | + * | |
| 166 | + * @param stdClass $log The log object | |
| 167 | + */ | |
| 168 | +function automatorwp_get_log_integration_icon( $log ) { | |
| 169 | + | |
| 170 | + if( in_array( $log->type, array( 'trigger', 'action' ) ) ) { | |
| 171 | + | |
| 172 | + // Get the trigger or action | |
| 173 | + ct_setup_table( "automatorwp_{$log->type}s" ); | |
| 174 | + $object = ct_get_object( $log->object_id ); | |
| 175 | + ct_reset_setup_table(); | |
| 176 | + | |
| 177 | + if( $object->type === 'filter' ) { | |
| 178 | + automatorwp_get_log_filter_icon( $log, $log->type, 'passed' ); | |
| 179 | + return; | |
| 180 | + } | |
| 181 | + | |
| 182 | + $type_args = automatorwp_automation_item_type_args( $object, $log->type ); | |
| 183 | + | |
| 184 | + if( $type_args ) { | |
| 185 | + $integration = automatorwp_get_integration( $type_args['integration'] ); | |
| 186 | + | |
| 187 | + if( $integration ) : | |
| 188 | + | |
| 189 | + if( $log->type === 'action' && $object->type === 'automatorwp_anonymous_user' ) { | |
| 190 | + $integration['icon'] = AUTOMATORWP_URL . 'assets/img/automatorwp-anonymous.svg'; | |
| 191 | + } | |
| 192 | + | |
| 193 | + $integration = apply_filters( 'automatorwp_get_log_integration_icon_integration', $integration, $object, $log->type, $log ); | |
| 194 | + | |
| 195 | + ?> | |
| 196 | + | |
| 197 | + <div class="automatorwp-integration-icon cmb-tooltip"> | |
| 198 | + <img src="<?php echo esc_attr( $integration['icon'] ); ?>" alt="<?php echo esc_attr( $integration['label'] ); ?>"> | |
| 199 | + <span class="cmb-tooltip-desc cmb-tooltip-top"><?php echo esc_html( $integration['label'] ); ?></span> | |
| 200 | + </div> | |
| 201 | + | |
| 202 | + <?php endif; | |
| 203 | + } else { ?> | |
| 204 | + | |
| 205 | + <div class="automatorwp-integration-icon cmb-tooltip"> | |
| 206 | + <img src="<?php echo esc_attr( AUTOMATORWP_URL . 'assets/img/integration-missing.svg' ); ?>" alt="<?php echo esc_attr( __( 'Missing plugin', 'automatorwp' ) ); ?>"> | |
| 207 | + <span class="cmb-tooltip-desc cmb-tooltip-top"><?php echo esc_html( $log->type ); ?></span> | |
| 208 | + </div> | |
| 209 | + | |
| 210 | + <?php } | |
| 211 | + | |
| 212 | + } else if( $log->type === 'filter' ) { | |
| 213 | + | |
| 214 | + $item_type = automatorwp_get_log_meta( $log->id, 'item_type', true ); | |
| 215 | + | |
| 216 | + automatorwp_get_log_filter_icon( $log, $item_type, 'not-passed' ); | |
| 217 | + | |
| 218 | + } else { | |
| 219 | + | |
| 220 | + $icon = AUTOMATORWP_URL . 'integrations/automatorwp/assets/automatorwp.svg'; | |
| 221 | + | |
| 222 | + if( $log->type === 'anonymous' ) { | |
| 223 | + $icon = AUTOMATORWP_URL . 'assets/img/automatorwp-anonymous.svg'; | |
| 224 | + } | |
| 225 | + | |
| 226 | + /** | |
| 227 | + * Available filter to override log default icon | |
| 228 | + * | |
| 229 | + * @since 1.2.4 | |
| 230 | + * | |
| 231 | + * @param string $icon The icon URL | |
| 232 | + * @param stdClass $log The log object | |
| 233 | + * | |
| 234 | + * @return string | |
| 235 | + */ | |
| 236 | + $icon = apply_filters( 'automatorwp_get_log_default_icon', $icon, $log ); | |
| 237 | + | |
| 238 | + /** | |
| 239 | + * Available filter to override log default icon title | |
| 240 | + * | |
| 241 | + * @since 1.2.4 | |
| 242 | + * | |
| 243 | + * @param string $title The icon title attribute | |
| 244 | + * @param stdClass $log The log object | |
| 245 | + * | |
| 246 | + * @return string | |
| 247 | + */ | |
| 248 | + $title = apply_filters( 'automatorwp_get_log_default_icon_title', 'AutomatorWP', $log ); ?> | |
| 249 | + | |
| 250 | + <div class="automatorwp-integration-icon cmb-tooltip"> | |
| 251 | + <img src="<?php echo esc_attr( $icon ); ?>" alt="<?php echo esc_attr( $title ); ?>"> | |
| 252 | + <span class="cmb-tooltip-desc cmb-tooltip-top"><?php echo esc_attr( $title ); ?></span> | |
| 253 | + </div> | |
| 254 | + | |
| 255 | + <?php } | |
| 256 | + | |
| 257 | +} | |
| 258 | + | |
| 259 | +/** | |
| 260 | + * Get the filter log integration icon HTML markup | |
| 261 | + * | |
| 262 | + * @since 1.0.0 | |
| 263 | + * | |
| 264 | + * @param stdClass $log The log object | |
| 265 | + * @param string $item_type The item type (trigger|action) | |
| 266 | + * @param string $class The icon class (passed|not-passed) | |
| 267 | + */ | |
| 268 | +function automatorwp_get_log_filter_icon( $log, $item_type, $class ) { | |
| 269 | + | |
| 270 | + if( in_array( $item_type, array( 'action', 'trigger' ) ) ) { | |
| 271 | + | |
| 272 | + // Get the trigger or action | |
| 273 | + ct_setup_table( "automatorwp_{$item_type}s" ); | |
| 274 | + $filter = ct_get_object_meta( $log->object_id, 'filter', true ); | |
| 275 | + ct_reset_setup_table(); | |
| 276 | + | |
| 277 | + $filter_args = automatorwp_get_filter( $filter ); | |
| 278 | + | |
| 279 | + $message = __( 'Filter not passed', 'automatorwp' ); | |
| 280 | + | |
| 281 | + if( $class === 'passed' ) { | |
| 282 | + $message = __( 'Filter passed', 'automatorwp' ); | |
| 283 | + } | |
| 284 | + | |
| 285 | + if( $filter_args ) { | |
| 286 | + $integration = automatorwp_get_integration( $filter_args['integration'] ); | |
| 287 | + | |
| 288 | + if( $integration ) : ?> | |
| 289 | + | |
| 290 | + <div class="automatorwp-integration-icon cmb-tooltip"> | |
| 291 | + <img src="<?php echo esc_attr( $integration['icon'] ); ?>" title="<?php echo esc_attr( $integration['label'] ); ?>" alt="<?php echo esc_attr( $integration['label'] ); ?>"> | |
| 292 | + <span class="cmb-tooltip-desc cmb-tooltip-top"><?php echo esc_attr( $message ); ?></span> | |
| 293 | + <div class="automatorwp-filter-icon automatorwp-filter-icon-<?php echo $class; ?>"> | |
| 294 | + <img src="<?php echo esc_attr( AUTOMATORWP_URL . 'integrations/filter/assets/filter-icon.svg' ); ?>" title="<?php echo esc_attr( $message ); ?>"> | |
| 295 | + </div> | |
| 296 | + </div> | |
| 297 | + | |
| 298 | + <?php endif; | |
| 299 | + } else { ?> | |
| 300 | + | |
| 301 | + <div class="automatorwp-integration-icon cmb-tooltip"> | |
| 302 | + <img src="<?php echo esc_attr( AUTOMATORWP_URL . 'assets/img/integration-missing.svg' ); ?>" title="<?php echo esc_attr( __( 'Missing plugin', 'automatorwp' ) ); ?>"> | |
| 303 | + <span class="cmb-tooltip-desc cmb-tooltip-top"><?php echo esc_attr( __( 'Filter not passed', 'automatorwp' ) ); ?></span> | |
| 304 | + <div class="automatorwp-filter-icon automatorwp-filter-icon-<?php echo $class; ?>"> | |
| 305 | + <img src="<?php echo esc_attr( AUTOMATORWP_URL . 'integrations/filter/assets/filter-icon.svg' ); ?>" title="<?php echo esc_attr( __( 'Filter not passed', 'automatorwp' ) ); ?>"> | |
| 306 | + </div> | |
| 307 | + </div> | |
| 308 | + | |
| 309 | + <?php } | |
| 310 | + | |
| 311 | + } | |
| 312 | + | |
| 313 | +} | |
| 314 | + | |
| 315 | +/** | |
| 316 | + * Get the last object log | |
| 317 | + * | |
| 318 | + * @since 1.0.0 | |
| 319 | + * | |
| 320 | + * @param int $object_id The object ID | |
| 321 | + * @param string $type The object type | |
| 322 | + * | |
| 323 | + * @return stdClass|false | |
| 324 | + */ | |
| 325 | +function automatorwp_get_object_last_log( $object_id, $type ) { | |
| 326 | + | |
| 327 | + global $wpdb; | |
| 328 | + | |
| 329 | + $object_id = absint( $object_id ); | |
| 330 | + | |
| 331 | + // Check the object ID | |
| 332 | + if( $object_id === 0 ) { | |
| 333 | + return false; | |
| 334 | + } | |
| 335 | + | |
| 336 | + $types = automatorwp_get_log_types(); | |
| 337 | + | |
| 338 | + // Check the type | |
| 339 | + if( ! isset( $types[$type] ) ) { | |
| 340 | + return false; | |
| 341 | + } | |
| 342 | + | |
| 343 | + $cache = automatorwp_get_cache( 'object_last_log', array(), false ); | |
| 344 | + | |
| 345 | + // If result already cached, return it | |
| 346 | + if( isset( $cache[$type] ) | |
| 347 | + && isset( $cache[$type][$object_id] ) ) { | |
| 348 | + return $cache[$type][$object_id]; | |
| 349 | + } | |
| 350 | + | |
| 351 | + $ct_table = ct_setup_table( 'automatorwp_logs' ); | |
| 352 | + | |
| 353 | + $log = $wpdb->get_row( | |
| 354 | + "SELECT * | |
| 355 | + FROM {$ct_table->db->table_name} AS l | |
| 356 | + WHERE 1=1 | |
| 357 | + AND l.object_id = {$object_id} | |
| 358 | + AND l.type = '{$type}' | |
| 359 | + ORDER BY l.date DESC | |
| 360 | + LIMIT 1" | |
| 361 | + ); | |
| 362 | + | |
| 363 | + ct_reset_setup_table(); | |
| 364 | + | |
| 365 | + $cache[$type][$object_id] = $log; | |
| 366 | + | |
| 367 | + automatorwp_set_cache( 'object_last_log', $cache ); | |
| 368 | + | |
| 369 | + return $log; | |
| 370 | + | |
| 371 | +} | |