isset( $raw['user_id'] ) ? $raw['user_id'] : 0, 'course_id' => isset( $raw['course_id'] ) ? $raw['course_id'] : 0, 'action' => isset( $raw['action'] ) ? $raw['action'] : '', 'event_time' => gmdate( 'Y-m-d H:i:s' ), ) ); if ( null === $event ) { return null; } $context = $this->capture_context(); $context['hook'] = isset( $raw['hook'] ) ? sanitize_text_field( (string) $raw['hook'] ) : ''; if ( isset( $raw['metadata'] ) && is_array( $raw['metadata'] ) ) { $context['integration_metadata'] = $raw['metadata']; } $context = apply_filters( 'evlfl_event_context', $context, $event ); if ( ! is_array( $context ) ) { $context = array(); } return array( 'event' => $event, 'context' => $context, 'state' => isset( $raw['state'] ) && is_array( $raw['state'] ) ? $raw['state'] : array(), ); } /** * Snapshot request context. * * @return array */ public function capture_context() { $uid = function_exists( 'get_current_user_id' ) ? (int) get_current_user_id() : 0; $type = 'frontend'; if ( function_exists( 'wp_doing_cron' ) && wp_doing_cron() ) { $type = 'cron'; } elseif ( defined( 'WP_CLI' ) && WP_CLI ) { $type = 'wp_cli'; } elseif ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { $type = 'rest_api'; } elseif ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) { $type = 'ajax'; } elseif ( function_exists( 'is_admin' ) && is_admin() ) { $type = 'admin'; } $privileged = $uid > 0 && ( user_can( $uid, 'manage_options' ) || user_can( $uid, 'edit_users' ) ); return array( 'request_type' => $type, 'current_user_id' => $uid, 'privileged' => $privileged, 'woocommerce_hook' => $this->woocommerce_hook(), ); } /** * Name of the WooCommerce order hook this change is running inside, or '' when none. * * LearnDash's WooCommerce integration enrolls and unenrolls from inside WooCommerce order * hooks, so finding one on the current hook stack is direct evidence of the origin. This only * reads the stack; it never calls WooCommerce. * * @return string */ private function woocommerce_hook() { global $wp_current_filter; if ( empty( $wp_current_filter ) || ! is_array( $wp_current_filter ) ) { return ''; } $prefixes = array( 'woocommerce_order_status_', 'woocommerce_payment_complete', 'woocommerce_checkout_order_processed', 'woocommerce_store_api_checkout_order_processed', 'woocommerce_subscription_', ); foreach ( $wp_current_filter as $hook ) { if ( ! is_string( $hook ) ) { continue; } foreach ( $prefixes as $prefix ) { if ( 0 === strpos( $hook, $prefix ) ) { return sanitize_key( $hook ); } } } return ''; } }