$context Extra diagnostics, stored as JSON. Never user-created data. * @param string $ref Optional reference, for example a form ID or a migration name (100 characters at most). * @return int|false The row ID, or false when nothing was written. */ public static function add( $type, $level, $message, array $context = [], $ref = '' ) { global $wpdb; if ( ! in_array( $type, self::TYPES, true ) ) { return false; } if ( ! in_array( $level, self::LEVELS, true ) ) { $level = 'info'; } if ( 'debug' === $level && true !== Settings::get_value( 'advanced.debug_log' ) ) { return false; } $json = wp_json_encode( $context ); $suppress = $wpdb->suppress_errors(); $inserted = $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Own table. Database::table( 'log' ), [ 'type' => $type, 'ref' => substr( (string) $ref, 0, 100 ), 'level' => $level, 'message' => (string) $message, 'context' => false === $json ? '{}' : $json, 'created_gmt' => Clock::gmt_mysql(), ] ); $id = $inserted ? (int) $wpdb->insert_id : false; $wpdb->suppress_errors( $suppress ); return $id; } /** * Delete rows older than 30 days. A row exactly 30 days old is kept. * * @return int Number of rows deleted (0 when the table does not exist). */ public static function prune() { global $wpdb; $cutoff = gmdate( 'Y-m-d H:i:s', Clock::now() - self::RETENTION_DAYS * DAY_IN_SECONDS ); $table = esc_sql( Database::table( 'log' ) ); $suppress = $wpdb->suppress_errors(); $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE created_gmt < %s", $cutoff ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Own table; name built from $wpdb->prefix. $wpdb->suppress_errors( $suppress ); return (int) $deleted; } }