insert( $wpdb->prefix . 'submex_activity', array( 'subscription_id' => $subscription_id, 'type' => $type, 'actor' => $actor ?? $this->current_actor(), 'message' => $message, 'context' => $context ? wp_json_encode( $this->scrub( $context ) ) : null, 'created_gmt' => gmdate( 'Y-m-d H:i:s' ), ), array( '%d', '%s', '%s', '%s', '%s', '%s' ) ); } /** * @return object[] */ public function for_subscription( int $subscription_id, int $limit = 50 ): array { global $wpdb; return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}submex_activity WHERE subscription_id = %d ORDER BY created_gmt DESC, id DESC LIMIT %d", $subscription_id, $limit ) ); } private function current_actor(): string { if ( ! function_exists( 'get_current_user_id' ) ) { return 'system'; } $user_id = get_current_user_id(); if ( ! $user_id ) { return 'system'; } return ( is_admin() ? 'admin:' : 'customer:' ) . $user_id; } /** * Drop anything that looks like a secret before it reaches the log. */ private function scrub( array $context ): array { $blocked = array( 'token', 'access_token', 'secret', 'password', 'card', 'number', 'cvv', 'cvc', 'pan' ); foreach ( $context as $key => $value ) { foreach ( $blocked as $needle ) { if ( false !== stripos( (string) $key, $needle ) ) { $context[ $key ] = '[redacted]'; continue 2; } } if ( is_array( $value ) ) { $context[ $key ] = $this->scrub( $value ); } } return $context; } }