t status should be visible. * * @since 4.7.0 * * @param object $status Post status. * @return bool True if the post status is visible, otherwise false. */ protected function check_read_permission( $status ) { if ( true === $status->public ) { return true; } if ( false === $status->internal || 'trash' === $status->name ) { $types = get_post_types( array( 'show_in_rest' => true ), 'objects' ); foreach ( $types as $type ) { if ( current_user_can( $type->cap->edit_posts ) ) { return true; } } } return false; } /** * Retrieves a specific post status. * * @since 4.7.0 * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function get_item( $request ) { $obj = get_post_status_object( $request['status'] ); if ( empty( $obj ) ) { return new WP_Error( 'rest_status_invalid', __( 'Invalid status.' ), array( 'status' => 404 ) ); } $data = $this->prepare_item_for_response( $obj, $request ); return rest_ensure_response( $data ); } /** * Prepares a post status object for serialization. * * @since 4.7.0 * @since 5.9.0 Renamed `$status` to `$item` to match parent class for PHP 8 named parameter support. * * @param stdClass $item Post status data. * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response Post status data. */ public function prepare_item_for_response( $item, $request ) { // Restores the more descriptive, specific name for use within this method. $status = $item; $fields = $this->get_fields_for_response( $request ); $data = array(); if ( in_array( 'name', $fields, true ) ) { $data['name'] = $status->label; } if ( in_array( 'private', $fields, true ) ) { $data['private'] = (bool) $status->private; } if ( in_array( 'protected', $fields, true ) ) { $data['protected'] = (bool) $status->protected; } if ( in_array( 'public', $fields, true ) ) { $data['public'] = (bool) $status->public; } if ( in_array( 'queryable', $fields, true ) ) { $data['queryable'] = (bool) $status->publicly_queryable; } if ( in_array( 'show_in_list', $fields, true ) ) { $data['show_in_list'] = (bool) $status->show_in_admin_all_list; } if ( in_array( 'slug', $fields, true ) ) { $data['slug'] = $status->name; } if ( in_array( 'date_floating', $fields, true ) ) { $data['date_floating'] = $status->date_floating; } $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object( $data, $request ); $data = $this->filter_response_by_context( $data, $context ); $response = rest_ensure_response( $data ); $rest_url = rest_url( rest_get_route_for_post_type_items( 'post' ) ); if ( 'publish' === $status->name ) { $response->add_link( 'archives', $rest_url ); } else { $response->add_link( 'archives', add_query_arg( 'status', $status->name, $rest_url ) ); } /** * Filters a post status returned from the REST API. * * Allows modification of the status data right before it is returned. * * @since 4.7.0 * * @param WP_REST_Response $response The response object. * @param object $status The original post status object. * @param WP_REST_Request $request Request used to generate the response. */ return apply_filters( 'rest_prepare_status', $response, $status, $request ); } /** * Retrieves the post status' schema, conforming to JSON Schema. * * @since 4.7.0 * * @return array Item schema data. */ public function get_item_schema() { if ( $this->schema ) { return $this->add_additional_fields_schema( $this->schema ); } $schema = array( '$schema' => 'http://json-schema.org/draft-04/schema#', 'title' => 'status', 'type' => 'object', 'properties' => array( 'name' => array( 'description' => __( 'The title for the status.' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), 'private' => array( 'description' => __( 'Whether posts with this status should be private.' ), 'type' => 'boolean', 'context' => array( 'edit' ), 'readonly' => true, ), 'protected' => array( 'description' => __( 'Whether posts with this status should be protected.' ), 'type' => 'boolean', 'context' => array( 'edit' ), 'readonly' => true, ), 'public' => array( 'description' => __( 'Whether posts of this status should be shown in the front end of the site.' ), 'type' => 'boolean', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'queryable' => array( 'description' => __( 'Whether posts with this status should be publicly-queryable.' ), 'type' => 'boolean', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'show_in_list' => array( 'description' => __( 'Whether to include posts in the edit listing for their post type.' ), 'type' => 'boolean', 'context' => array( 'edit' ), 'readonly' => true, ), 'slug' => array( 'description' => __( 'An alphanumeric identifier for the status.' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), 'date_floating' => array( 'description' => __( 'Whether posts of this status may have floating published dates.' ), 'type' => 'boolean', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), ), ); $this->schema = $schema; return $this->add_additional_fields_schema( $this->schema ); } /** * Retrieves the query params for collections. * * @since 4.7.0 * * @return array Collection parameters. */ public function get_collection_params() { return array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); } } merce_register_form', array( $this, 'stamp_html_element' ) ); // Update order based on submitted fields. add_action( 'woocommerce_checkout_order_created', function ( $order ) { // Check if this order already has any attribution data to prevent duplicates attribution data. if ( $this->has_attribution( $order ) ) { return; } // Nonce check is handled by WooCommerce before woocommerce_checkout_order_created hook. // phpcs:ignore WordPress.Security.NonceVerification $params = $this->get_unprefixed_field_values( $_POST ); /** * Run an action to save order attribution data. * * @since 8.5.0 * * @param WC_Order $order The order object. * @param array $params Unprefixed order attribution data. */ do_action( 'woocommerce_order_save_attribution_data', $order, $params ); } ); add_action( 'woocommerce_order_save_attribution_data', function ( $order, $data ) { $source_data = $this->get_source_values( $data ); $this->send_order_tracks( $source_data, $order ); $this->set_order_source_data( $source_data, $order ); }, 10, 2 ); add_action( 'user_register', function ( $customer_id ) { try { $customer = new WC_Customer( $customer_id ); $this->set_customer_source_data( $customer ); } catch ( Exception $e ) { $this->log( $e->getMessage(), __METHOD__, WC_Log_Levels::ERROR ); } } ); // Add origin data to the order table. add_action( 'admin_init', function () { $this->register_order_origin_column(); } ); add_action( 'woocommerce_new_order', function ( $order_id, $order ) { $this->maybe_set_admin_source( $order ); }, 2, 10 ); } /** * If the order is created in the admin, set the source type and origin to admin/Web admin. * Only execute this if the order is created in the admin interface (or via ajax in the admin interface). * * @param WC_Order $order The recently created order object. * * @since 8.5.0 */ private function maybe_set_admin_source( WC_Order $order ) { // For ajax requests, bail if the referer is not an admin page. $http_referer = esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ?? '' ) ); $referer_is_admin = 0 === strpos( $http_referer, get_admin_url() ); if ( ! $referer_is_admin && wp_doing_ajax() ) { return; } // If not admin interface page, bail. if ( ! is_admin() ) { return; } $order->add_meta_data( $this->get_meta_prefixed_field_name( 'source_type' ), 'admin' ); $order->save(); } /** * Get all of the field names. * * @return array */ public function get_field_names(): array { return $this->field_names; } /** * Get the prefix for the fields. * * @return string */ public function get_prefix(): string { return $this->field_prefix; } /** * Scripts & styles for custom source tracking and cart tracking. */ private function enqueue_scripts_and_styles() { wp_enqueue_script( 'sourcebuster-js', plugins_url( "assets/js/sourcebuster/sourcebuster{$this->get_script_suffix()}.js", WC_PLUGIN_FILE ), array(), Constants::get_constant( 'WC_VERSION' ), true ); wp_enqueue_script( 'wc-order-attribution', plugins_url( "assets/js/frontend/order-attribution{$this->get_script_suffix()}.js", WC_PLUGIN_FILE ), // Technically we do depend on 'wp-data', 'wc-blocks-checkout' for blocks checkout, // but as implementing conditional dependency on the server-side would be too complex, // we resolve this condition at the client-side. array( 'sourcebuster-js' ), Constants::get_constant( 'WC_VERSION' ), true ); /** * Filter the lifetime of the cookie used for source tracking. * * @since 8.5.0 * * @param float $lifetime The lifetime of the Sourcebuster cookies in months. * * The default value forces Sourcebuster into making the cookies valid for the current session only. */ $lifetime = (float) apply_filters( 'wc_order_attribution_cookie_lifetime_months', 0.00001 ); /** * Filter the session length for source tracking. * * @since 8.5.0 * * @param int $session_length The session length in minutes. */ $session_length = (int) apply_filters( 'wc_order_attribution_session_length_minutes', 30 ); /** * Filter to enable base64 encoding for cookie values. * * @since 9.0.0 * * @param bool $use_base64_cookies True to enable base64 encoding, default is false. */ $use_base64_cookies = apply_filters( 'wc_order_attribution_use_base64_cookies', false ); /** * Filter to allow tracking. * * @since 8.5.0 * * @param bool $allow_tracking True to allow tracking, false to disable. */ $allow_tracking = wc_bool_to_string( apply_filters( 'wc_order_attribution_allow_tracking', true ) ); // Create Order Attribution JS namespace with parameters. $namespace = array( 'params' => array( 'lifetime' => $lifetime, 'session' => $session_length, 'base64' => $use_base64_cookies, 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'prefix' => $this->field_prefix, 'allowTracking' => 'yes' === $allow_tracking, ), 'fields' => $this->fields, ); wp_localize_script( 'wc-order-attribution', 'wc_order_attribution', $namespace ); } /** * Enqueue the stylesheet for admin pages. * * @return void */ private function enqueue_admin_scripts_and_styles() { $screen = get_current_screen(); if ( $screen->id !== $this->get_order_screen_id() ) { return; } // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter wp_enqueue_script( 'woocommerce-order-attribution-admin-js', plugins_url( "assets/js/admin/order-attribution-admin{$this->get_script_suffix()}.js", WC_PLUGIN_FILE ), array( 'jquery' ), Constants::get_constant( 'WC_VERSION' ) ); } /** * Display the origin column in the orders table. * * @param int $order_id The order ID. * * @return void */ private function display_origin_column( $order_id ): void { try { // Ensure we've got a valid order. $order = $this->get_hpos_order_object( $order_id ); $this->output_origin_column( $order ); } catch ( Exception $e ) { return; } } /** * Output the translated origin label for the Origin column in the orders table. * * Default to "Unknown" if no origin is set. * * @param WC_Order $order The order object. * * @return void */ private function output_origin_column( WC_Order $order ) { $source_type = $order->get_meta( $this->get_meta_prefixed_field_name( 'source_type' ) ); $source = $order->get_meta( $this->get_meta_prefixed_field_name( 'utm_source' ) ); $origin = $this->get_origin_label( $source_type, $source ); echo esc_html( $origin ); } /** * Handles the `` element for checkout forms. * * @since 9.0.0 * @deprecated 10.5.0 Use stamp_html_element() instead. * * @return void */ public function stamp_checkout_html_element_once() { wc_deprecated_function( __METHOD__, '10.5.0', 'stamp_html_element' ); $this->stamp_html_element(); } /** * Output `` element that contributes the order attribution values to the enclosing form. * * Used for customer register forms and checkout forms. * * Note: By default, this method may output multiple instances of the element when called * multiple times (e.g., during checkout form pre-generation and actual rendering). * The JavaScript layer will remove duplicate elements and ensure only one set of data is submitted. * * @return void */ public function stamp_html_element() { /** * Filter to allow sites to opt back into single-output behavior. * * @since 10.5.0 * * @param bool $allow_multiple_elements True to allow multiple elements (new behavior), false for single element (old behavior). */ $allow_multiple = apply_filters( 'wc_order_attribution_allow_multiple_elements', true ); // If single-output mode is enabled, use the static flag to prevent multiple outputs. if ( ! $allow_multiple && self::$is_stamp_html_called ) { return; } printf( '' ); if ( ! $allow_multiple ) { self::$is_stamp_html_called = true; } } /** * Save source data for a Customer object. * * @param WC_Customer $customer The customer object. * * @return void */ private function set_customer_source_data( WC_Customer $customer ) { // Nonce check is handled before user_register hook. // phpcs:ignore WordPress.Security.NonceVerification foreach ( $this->get_source_values( $this->get_unprefixed_field_values( $_POST ) ) as $key => $value ) { $customer->add_meta_data( $this->get_meta_prefixed_field_name( $key ), $value ); } $customer->save_meta_data(); } /** * Save source data for an Order object. * * @param array $source_data The source data. * @param WC_Order $order The order object. * * @return void */ private function set_order_source_data( array $source_data, WC_Order $order ) { // If all the values are empty, bail. if ( empty( array_filter( $source_data ) ) ) { return; } foreach ( $source_data as $key => $value ) { $order->add_meta_data( $this->get_meta_prefixed_field_name( $key ), $value ); } $order->save_meta_data(); } /** * Log a message as a debug log entry. * * @param string $message The message to log. * @param string $method The method that is logging the message. * @param string $level The log level. */ private function log( string $message, string $method, string $level = WC_Log_Levels::DEBUG ) { /** * Filter to enable debug mode. * * @since 8.5.0 * * @param string $enabled 'yes' to enable debug mode, 'no' to disable. */ if ( 'yes' !== apply_filters( 'wc_order_attribution_debug_mode_enabled', 'no' ) ) { return; } $this->logger->log( $level, sprintf( '%s %s', $method, $message ), array( 'source' => 'woocommerce-order-attribution' ) ); } /** * Send order source data to Tracks. * * @param array $source_data The source data. * @param WC_Order $order The order object. * * @return void */ private function send_order_tracks( array $source_data, WC_Order $order ) { $origin_label = $this->get_origin_label( $source_data['source_type'] ?? '', $source_data['utm_source'] ?? '', false ); $tracks_data = array( 'order_id' => $order->get_id(), 'source_type' => $source_data['source_type'] ?? '', 'medium' => $source_data['utm_medium'] ?? '', 'source' => $source_data['utm_source'] ?? '', 'device_type' => strtolower( $source_data['device_type'] ?? 'unknown' ), 'origin_label' => strtolower( $origin_label ), 'session_pages' => $source_data['session_pages'] ?? 0, 'session_count' => $source_data['session_count'] ?? 0, 'order_total' => $order->get_total(), 'customer_registered' => $order->get_customer_id() ? 'yes' : 'no', ); if ( function_exists( 'wc_admin_record_tracks_event' ) ) { wc_admin_record_tracks_event( 'order_attribution', $tracks_data ); } } /** * Get the screen ID for the orders page. * * @return string */ private function get_order_screen_id(): string { return OrderUtil::custom_orders_table_usage_is_enabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order'; } /** * Register the origin column in the orders table. * * This accounts for the differences in hooks based on whether HPOS is enabled or not. * * @return void */ private function register_order_origin_column() { $screen_id = $this->get_order_screen_id(); $add_column = function ( $columns ) { $columns['origin'] = esc_html__( 'Origin', 'woocommerce' ); return $columns; }; // HPOS and non-HPOS use different hooks. add_filter( "manage_{$screen_id}_columns", $add_column ); add_filter( "manage_edit-{$screen_id}_columns", $add_column ); $display_column = function ( $column_name, $order_id ) { if ( 'origin' !== $column_name ) { return; } $this->display_origin_column( $order_id ); }; // HPOS and non-HPOS use different hooks. add_action( "manage_{$screen_id}_custom_column", $display_column, 10, 2 ); add_action( "manage_{$screen_id}_posts_custom_column", $display_column, 10, 2 ); } /** * Check if this order already has any attribution data * * @param WC_Order $order The order object. * * @return bool * @since 9.8.0 */ public function has_attribution( $order ) { foreach ( $this->field_names as $field ) { if ( $order->meta_exists( $this->get_meta_prefixed_field_name( $field ) ) ) { return true; } } return false; } }
Fatal error: Uncaught Automattic\WooCommerce\Internal\DependencyManagement\ContainerException: Attempt to get an instance of class 'Automattic\WooCommerce\Internal\Orders\OrderAttributionController', which doesn't exist. in /htdocs/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php:105 Stack trace: #0 /htdocs/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php(78): Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->get_core('Automattic\\WooC...', Array) #1 /htdocs/wp-content/plugins/woocommerce/src/Container.php(68): Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->get('Automattic\\WooC...') #2 /htdocs/wp-content/plugins/woocommerce/includes/class-woocommerce.php(388): Automattic\WooCommerce\Container->get('Automattic\\WooC...') #3 /htdocs/wp-content/plugins/woocommerce/includes/class-woocommerce.php(264): WooCommerce->init_hooks() #4 /htdocs/wp-content/plugins/woocommerce/includes/class-woocommerce.php(165): WooCommerce->__construct() #5 /htdocs/wp-content/plugins/woocommerce/woocommerce.php(47): WooCommerce::instance() #6 /htdocs/wp-content/plugins/woocommerce/woocommerce.php(62): WC() #7 /htdocs/wp-settings.php(560): include_once('/htdocs/wp-cont...') #8 /htdocs/wp-config.php(98): require_once('/htdocs/wp-sett...') #9 /htdocs/wp-load.php(50): require_once('/htdocs/wp-conf...') #10 /htdocs/wp-blog-header.php(13): require_once('/htdocs/wp-load...') #11 /htdocs/index.php(17): require('/htdocs/wp-blog...') #12 {main} thrown in /htdocs/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php on line 105