admin
7 years ago
analytics
7 years ago
emails
7 years ago
fields
7 years ago
providers
7 years ago
templates
7 years ago
class-conditional-logic-core.php
7 years ago
class-fields.php
7 years ago
class-form.php
7 years ago
class-frontend.php
7 years ago
class-install.php
9 years ago
class-logging.php
7 years ago
class-preview.php
7 years ago
class-process.php
7 years ago
class-providers.php
8 years ago
class-smart-tags.php
7 years ago
class-templates.php
8 years ago
class-widget.php
7 years ago
functions.php
7 years ago
integrations.php
7 years ago
class-logging.php
338 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class for logging events and errors |
| 4 | * |
| 5 | * This class is forked from Easy Digital Downloads / Pippin Williamson. |
| 6 | * |
| 7 | * @link https://github.com/easydigitaldownloads/Easy-Digital-Downloads/blob/master/includes/class-edd-logging.php |
| 8 | * @package WPForms |
| 9 | * @author WPForms |
| 10 | * @since 1.0.0 |
| 11 | * @license GPL-2.0+ |
| 12 | * @copyright Copyright (c) 2016, WPForms LLC |
| 13 | */ |
| 14 | class WPForms_Logging { |
| 15 | |
| 16 | /** |
| 17 | * Set up the logging class. |
| 18 | * |
| 19 | * @since 1.0.0 |
| 20 | */ |
| 21 | public function __construct() { |
| 22 | |
| 23 | // Create the log post type |
| 24 | add_action( 'init', array( $this, 'register_post_type' ), 1 ); |
| 25 | |
| 26 | // Create types taxonomy and default types |
| 27 | add_action( 'init', array( $this, 'register_taxonomy' ), 1 ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Registers the log post type. |
| 32 | * |
| 33 | * @since 1.0.0 |
| 34 | */ |
| 35 | public function register_post_type() { |
| 36 | |
| 37 | $log_args = array( |
| 38 | 'labels' => array( 'name' => esc_html__( 'WPForms Logs', 'wpforms' ), 'menu_name' => esc_html__( 'Logs', 'wpforms' ) ), |
| 39 | 'public' => false, |
| 40 | 'exclude_from_search' => true, |
| 41 | 'publicly_queryable' => false, |
| 42 | 'show_ui' => false, |
| 43 | 'query_var' => false, |
| 44 | 'rewrite' => false, |
| 45 | 'capability_type' => 'post', |
| 46 | 'supports' => array( 'title', 'editor' ), |
| 47 | 'can_export' => false, |
| 48 | 'show_in_menu' => 'wpforms-overview', |
| 49 | 'show_in_admin_bar' => false, |
| 50 | ); |
| 51 | |
| 52 | if ( wpforms_debug() ) { |
| 53 | $log_args['show_ui'] = true; |
| 54 | } |
| 55 | |
| 56 | register_post_type( 'wpforms_log', apply_filters( 'wpforms_log_cpt', $log_args ) ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Registers the Log Type taxonomy. |
| 61 | * |
| 62 | * @since 1.0.0 |
| 63 | */ |
| 64 | public function register_taxonomy() { |
| 65 | |
| 66 | register_taxonomy( 'wpforms_log_type', 'wpforms_log', array( 'public' => false ) ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Log types. |
| 71 | * |
| 72 | * @since 1.0.0 |
| 73 | * @return array |
| 74 | */ |
| 75 | public function log_types() { |
| 76 | |
| 77 | $terms = array( |
| 78 | 'payment', |
| 79 | 'provider', |
| 80 | 'spam', |
| 81 | 'entry', |
| 82 | 'error', |
| 83 | 'conditional_logic' |
| 84 | ); |
| 85 | |
| 86 | return apply_filters( 'wpforms_log_types', $terms ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Check if a log type is valid. |
| 91 | * |
| 92 | * @since 1.0.0 |
| 93 | * @param string $type |
| 94 | * @return bool |
| 95 | */ |
| 96 | function valid_type( $type ) { |
| 97 | |
| 98 | return in_array( $type, $this->log_types() ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Create new log entry. |
| 103 | * |
| 104 | * This is just a simple and fast way to log something. Use $this->insert_log() |
| 105 | * if you need to store custom meta data |
| 106 | * |
| 107 | * @since 1.0.0 |
| 108 | * @param string $title Log entry title |
| 109 | * @param string $message Log entry message |
| 110 | * @param int $parent Log entry parent |
| 111 | * @param string $type Log type (default: null) |
| 112 | * @return int Log ID |
| 113 | */ |
| 114 | public function add( $title = '', $message = '', $parent = 0, $type = null, $meta = '' ) { |
| 115 | |
| 116 | $log_data = array( |
| 117 | 'post_title' => $title, |
| 118 | 'post_content' => $message, |
| 119 | 'post_parent' => $parent, |
| 120 | 'log_type' => $type |
| 121 | ); |
| 122 | return $this->insert_log( $log_data, $meta ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Easily retrieves log items for a particular object ID. |
| 127 | * |
| 128 | * @since 1.0.0 |
| 129 | * @param int $object_id (default: 0) |
| 130 | * @param string $type Log type (default: null) |
| 131 | * @param int $paged Page number (default: null) |
| 132 | * @return array Array of the connected logs |
| 133 | */ |
| 134 | public function get_logs( $object_id = 0, $type = null, $paged = null ) { |
| 135 | |
| 136 | return $this->get_connected_logs( array( 'post_parent' => $object_id, 'paged' => $paged, 'log_type' => $type ) ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Stores a log entry. |
| 141 | * |
| 142 | * @since 1.0.0 |
| 143 | * @param array $log_data Log entry data |
| 144 | * @param array $log_meta Log entry meta |
| 145 | * @return int The ID of the newly created log item |
| 146 | */ |
| 147 | function insert_log( $log_data = array(), $log_meta = array() ) { |
| 148 | |
| 149 | $defaults = array( |
| 150 | 'post_type' => 'wpforms_log', |
| 151 | 'post_status' => 'publish', |
| 152 | 'post_parent' => 0, |
| 153 | 'post_content' => '', |
| 154 | 'log_type' => false |
| 155 | ); |
| 156 | $args = wp_parse_args( $log_data, $defaults ); |
| 157 | |
| 158 | do_action( 'wpforms_pre_insert_log', $log_data, $log_meta ); |
| 159 | |
| 160 | // Store the log entry |
| 161 | $log_id = wp_insert_post( $args ); |
| 162 | |
| 163 | // Set the log type, if any |
| 164 | if ( $log_data['log_type'] && $this->valid_type( $log_data['log_type'] ) ) { |
| 165 | wp_set_object_terms( $log_id, $log_data['log_type'], 'wpforms_log_type', false ); |
| 166 | } |
| 167 | |
| 168 | // Set log meta, if any |
| 169 | if ( $log_id && ! empty( $log_meta ) ) { |
| 170 | foreach ( (array) $log_meta as $key => $meta ) { |
| 171 | update_post_meta( $log_id, '_wpforms_log_' . sanitize_key( $key ), $meta ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | do_action( 'wpforms_post_insert_log', $log_id, $log_data, $log_meta ); |
| 176 | |
| 177 | return $log_id; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Update and existing log item. |
| 182 | * |
| 183 | * @since 1.0.0 |
| 184 | * @param array $log_data Log entry data |
| 185 | * @param array $log_meta Log entry meta |
| 186 | * @return bool True if successful, false otherwise |
| 187 | */ |
| 188 | public function update_log( $log_data = array(), $log_meta = array() ) { |
| 189 | |
| 190 | do_action( 'wpforms_pre_update_log', $log_data, $log_meta ); |
| 191 | |
| 192 | $defaults = array( |
| 193 | 'post_type' => 'wpforms_log', |
| 194 | 'post_status' => 'publish', |
| 195 | 'post_parent' => 0 |
| 196 | ); |
| 197 | |
| 198 | $args = wp_parse_args( $log_data, $defaults ); |
| 199 | |
| 200 | // Store the log entry |
| 201 | $log_id = wp_update_post( $args ); |
| 202 | |
| 203 | if ( $log_id && ! empty( $log_meta ) ) { |
| 204 | foreach ( (array) $log_meta as $key => $meta ) { |
| 205 | if ( ! empty( $meta ) ) |
| 206 | update_post_meta( $log_id, '_wpforms_log_' . sanitize_key( $key ), $meta ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | do_action( 'wpforms_post_update_log', $log_id, $log_data, $log_meta ); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Retrieve all connected logs. |
| 215 | * |
| 216 | * Used for retrieving logs related to particular items, such as a specific purchase. |
| 217 | * |
| 218 | * @since 1.0.0 |
| 219 | * @param array $args Query arguments |
| 220 | * @return mixed array if logs were found, false otherwise |
| 221 | */ |
| 222 | public function get_connected_logs( $args = array() ) { |
| 223 | $defaults = array( |
| 224 | 'post_type' => 'wpforms_log', |
| 225 | 'posts_per_page' => 20, |
| 226 | 'post_status' => 'publish', |
| 227 | 'paged' => get_query_var( 'paged' ), |
| 228 | 'log_type' => false |
| 229 | ); |
| 230 | |
| 231 | $query_args = wp_parse_args( $args, $defaults ); |
| 232 | |
| 233 | if ( $query_args['log_type'] && $this->valid_type( $query_args['log_type'] ) ) { |
| 234 | $query_args['tax_query'] = array( |
| 235 | array( |
| 236 | 'taxonomy' => 'wpforms_log_type', |
| 237 | 'field' => 'slug', |
| 238 | 'terms' => $query_args['log_type'] |
| 239 | ) |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | $logs = get_posts( $query_args ); |
| 244 | |
| 245 | if ( $logs ) |
| 246 | return $logs; |
| 247 | |
| 248 | // No logs found |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Retrieves number of log entries connected to particular object ID |
| 254 | * |
| 255 | * @since 1.0.0 |
| 256 | * @param int $object_id (default: 0) |
| 257 | * @param string $type Log type (default: null) |
| 258 | * @param array $meta_query Log meta query (default: null) |
| 259 | * @param array $date_query Log data query (default: null) (since 1.9) |
| 260 | * @return int Log count |
| 261 | */ |
| 262 | public function get_log_count( $object_id = 0, $type = null, $meta_query = null, $date_query = null ) { |
| 263 | |
| 264 | global $pagenow, $typenow; |
| 265 | |
| 266 | $query_args = array( |
| 267 | 'post_parent' => $object_id, |
| 268 | 'post_type' => 'wpforms_log', |
| 269 | 'posts_per_page' => -1, |
| 270 | 'post_status' => 'publish', |
| 271 | 'fields' => 'ids', |
| 272 | ); |
| 273 | |
| 274 | if ( ! empty( $type ) && $this->valid_type( $type ) ) { |
| 275 | $query_args['tax_query'] = array( |
| 276 | array( |
| 277 | 'taxonomy' => 'wpforms_log_type', |
| 278 | 'field' => 'slug', |
| 279 | 'terms' => $type |
| 280 | ) |
| 281 | ); |
| 282 | } |
| 283 | |
| 284 | if ( ! empty( $meta_query ) ) { |
| 285 | $query_args['meta_query'] = $meta_query; |
| 286 | } |
| 287 | |
| 288 | if ( ! empty( $date_query ) ) { |
| 289 | $query_args['date_query'] = $date_query; |
| 290 | } |
| 291 | |
| 292 | $logs = new WP_Query( $query_args ); |
| 293 | |
| 294 | return (int) $logs->post_count; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Delete a log. |
| 299 | * |
| 300 | * @since 1.0.0 |
| 301 | * @param int $object_id (default: 0) |
| 302 | * @param string $type Log type (default: null) |
| 303 | * @param array $meta_query Log meta query (default: null) |
| 304 | */ |
| 305 | public function delete_logs( $object_id = 0, $type = null, $meta_query = null ) { |
| 306 | |
| 307 | $query_args = array( |
| 308 | 'post_parent' => $object_id, |
| 309 | 'post_type' => 'wpforms_log', |
| 310 | 'posts_per_page' => -1, |
| 311 | 'post_status' => 'publish', |
| 312 | 'fields' => 'ids' |
| 313 | ); |
| 314 | |
| 315 | if ( ! empty( $type ) && $this->valid_type( $type ) ) { |
| 316 | $query_args['tax_query'] = array( |
| 317 | array( |
| 318 | 'taxonomy' => 'wpforms_log_type', |
| 319 | 'field' => 'slug', |
| 320 | 'terms' => $type, |
| 321 | ) |
| 322 | ); |
| 323 | } |
| 324 | |
| 325 | if ( ! empty( $meta_query ) ) { |
| 326 | $query_args['meta_query'] = $meta_query; |
| 327 | } |
| 328 | |
| 329 | $logs = get_posts( $query_args ); |
| 330 | |
| 331 | if ( $logs ) { |
| 332 | foreach ( $logs as $log ) { |
| 333 | wp_delete_post( $log, true ); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 |