�PNG  IHDR22?�� pHYs  �� OiCCPPhotoshop ICC profilexڝSgTS�=���BK���KoR RB���&*! J�!��Q�EEȠ�����Q, � ��!���������{�kּ������>���� �H3Q5� �B�������.@� $p�d!s�#�~<<+"��x� �M��0���B�\���t�8K�@z�B�@F���&S�`�cb�P-`'������{[�!�� e�Dh;��V�EX0fK�9�-0IWfH���� � 0Q��){`�##x��F�W<�+��*x��<�$9E�[-qWW.(�I+6aa�@.�y�2�4���������x����6��_-��"bb���ϫp@�t~��,/��;�m��%�h^ �u��f�@����W�p�~<�5�j>{�-�]c�K'Xt����o��(�h���w��?�G�%�fI�q^D$.Tʳ?�D��*�A��,���� �`6�B$��BB d�r`)��B(�Ͱ*`/�@4�Qh��p.�U�=p�a��(�� A�a!ڈb�X#����!�H�$ ɈQ"K�5H1R�T UH�=r9�\F��;�2����G1���Q=� �C��7�F� �dt1�����r�=�6��Ыhڏ>C�0��3�l0.��B�8, �c˱"� ���V����cϱw�E� 6wB aAHXLXN�H� $4� 7 �Q�'"��K�&���b21�XH,#��/{�C�7$�C2'��I��T��F�nR#�,��4H#���dk�9�, +ȅ����3��!�[ �b@q��S�(R�jJ��4�e�2AU��Rݨ�T5�ZB���R�Q��4u�9̓IK�����hh�i��t�ݕN��W���G���w ��Ljg(�gw��L�Ӌ�T071���oUX*�*|�� �J�&�*/T����ު U�U�T��^S}�FU3S� Ԗ�U��P�SSg�;���g�oT?�~Y��Y�L�OC�Q��_�� c�x,!k ��u�5�&���|v*�����=���9C3J3W�R�f?�q��tN �(���~���)�)�4L�1e\k����X�H�Q�G�6������E�Y��A�J'\'Gg����S�Sݧ �M=:��.�k���Dw�n��^��Lo��y���}/�T�m���G X� $� �<�5qo</���QC]�@C�a�a�ᄑ��<��F�F�i�\�$�m�mƣ&&!&KM�M�RM��)�;L;L���͢�֙5�=1�2��כ߷`ZxZ,����eI��Z�Yn�Z9Y�XUZ]�F���%ֻ�����N�N���gð�ɶ�����ۮ�m�}agbg�Ů��}�}��= ���Z~s�r:V:ޚΜ�?}����/gX���3��)�i�S��Ggg�s�󈋉K��.�>.���Ƚ�Jt�q]�z���������ۯ�6�i�ܟ�4�)�Y3s���C�Q��? ��0k߬~OCO�g��#/c/�W�װ��w��a�>�>r��>�<7�2�Y_�7��ȷ�O�o�_��C#�d�z����%g��A�[��z|!��?:�e����A���AA�������!h�쐭!��Α�i�P~���a�a��~ '���W�?�p�X�1�5w��Cs�D�D�Dޛg1O9�-J5*>�.j<�7�4�?�.fY��X�XIlK9.*�6nl������� �{�/�]py�����.,:�@L�N8��A*��%�w%� y��g"/�6ш�C\*N�H*Mz�쑼5y$�3�,幄'���L Lݛ:��v m2=:�1����qB�!M��g�g�fvˬe����n��/��k���Y- �B��TZ(�*�geWf�͉�9���+��̳�ې7�����ᒶ��KW-X潬j9�������(�x��oʿ�ܔ���Ĺd�f�f���-�[����n �ڴ �V����E�/��(ۻ��C���<��e����;?T�T�T�T6��ݵa��n��{��4���[���>ɾ�UUM�f�e�I���?�������m]�Nmq����#�׹���=TR��+�G�����w- 6 U����#pDy��� �� :�v�{���vg/jB��F�S��[b[�O�>����z�G��499�?r����C�d�&����ˮ/~�����јѡ�򗓿m|������������x31^�V���w�w��O�| (�h���SЧ��������c3-� cHRMz%������u0�`:�o�_�F5IDATx��ytUս�?�;���fN$$��@B�!�k��� -*�gE���j�O�kQ�ʠ`U�B_ �AÐ0%�Ȕ�@r3ޛ;߳��$�@��v����^�s����������$��ߢ !�W��5�̱'-٘������4�>�Rƫ$�G��0U�H}�_ _� #��k�MJ/=�9Du *ڲ�k�PQ;0��j|�*�$�^컂脛y���>�z×� B��� ��(�:$�ʔ_�6��C!B����MVx��a��ζyY����j��ƋY2�9�F�{r��r�딟F��Q��hY6��� x��[l��RC��������nt@��;P��3��� F�w��7��:�P���ɋ'���mj���Bƪ�$�Q�L��y��߷g��\���z����v5�p�v$�v櫻�`6~K �Y�X����,�:��"�o�e0II"(��,������l_�C���/4�+f�E�!q��:����u����c��樼.UE�@� �]K�W���$-�h]�JG���Dz)�`��.�A/ ���mᏣc�3jK� �!�>���$�Q�!�_�? \�C2(E �M$Z I{�U���s�ǣ���=��@Բe ޻��W�����@ �w����}�9v,)��.}*|"�s��di���,���G�]W�AC�������|<����[��ߕ������K�*�G nߴ!��‹��J�!��6(L�a)��Am��������;adf��:�xm��nh�;֦I��(���o�C~ډa?�=��s����h��E������xp����)���_���t��W�����´;8���`dz=(��@@�+,�;�`�fhl��XȌ�3,uI�~z(J����� �ZX�A����H'�À�㜦�82|��a�n�z2��I���� ���EY�h�ε<�0�q�{��C��v�˅��?"@��χ�x�WS+��N�~�KQ�J�8ل�� _.W��� !TU{�����,��"�� �;�������HI S� ��))�o�1�H�D� ���� (� ;����"Ҕ��,���� �F=���O��&�Ph>�`���y��߲ &S��P�<��'��1s�1cĂ�N����2)����@�u��tv��<�+�-&�����h��[!FSG���' � z z0��<����8d&�����E0�,��<��"!P�b4��� �{S�����X����.�囤���T��hEF��X�q0"-<����h���]�Py*����F⩍��@v�˂�Y08�� .q��a��s����(!<̫1�3�L�H��7sYW�`��qtu�\7��?�@�Y�� %Ka�x�GZ�*W�P��y�����N0r9��Z�c�%���q,��k�����!!,��>���IB��� �x1�Xu�Y���v-���ֺ��N������JP��a�2`�e5��w��u;�6�i���̃��(K���ťa��(l51��JL��O'%���]���ٖ뿉!��{*�p+f ���i8��D�ȑ���ܩ�M��V��ӟz��x�7Z�1i}�^͜���6e����x��)u�d�ˏ�V�eG�\�X1�����=n�U'�~Y����L�\,�(d!x ,�:�TW8� M`r���@��r+v�Q0��>^;˴�VbF��('����� �*O��i��j�Ӆ����^�S3&� ���{������.ی�� N;��NU%d0�Ce\r"�C�b��&t7N�7��� �ȪD�[%l�۪�$��'m?�A9tI�9>��NVL ��� 6v~M�����ъ�B���0����9$&=ώm��W�0���L���&��E���f�f��'+�9��hv��7,���M��6�D�&�^� �����8 D֮�����o��k^z��O��hP�8q�5d*��YVq3�{��������#LX4�?��jP=3�~�؅c�ݰ�̱�ұ��8̲��2�G�����j��έ�9��w7���U�X�w�85C��?!I&���o&����L���I#�^�eV�,�^t5�k� �xs˛\��U�8��."���{���ǵ�l4��ݼ�;��r"���G�/~���gJV�N糦��ZZ^k+NN�D��I$�=OE�z����|*�*{��%k���G��>�`9��b;�s���8,�qs��c��.b��q�%�>r��n�R�Ƨ�%�,�A��le#�8���}; 21�0�b�&�QX�f���q�00����$>�h�N�'�a3��A���/�r ��L5�8�GԲ'�����w;��c#�q1���h$*��V^�k��QĽ�b:i������� ����H F#�_���,��]g����Ը����`RUU���� ��"�qD���>-��ϤI�~��3�̴�����NZ[[555�eeek7�m����YF#[��Đ��R��i��L�H�r ��@��J-�Pŝ��_|�-��6y��ks�V�U~��6���w.����(��0A� 4��f��� /���C�xŊ�ٳ����z=6�����rg���mKo��o�/����a�g���?'9%9��P��斓��~���L{�� �H��{AW��B��$����v�1b�&Mb�ر��ر#�0`�`xMBp! )Z�Nmo�mZ�g�}�eذaWTVV ���'99��C5��&�&+5�bT>|8��PUUETT����&���t��~TE%��'������b�tEGG;RSS�S�N�v��ѯ��t����u1@�ޚ"f��͈���r'L�Ph��m��ƌ�74o��`�x�q�P���NRSS�X,�����l&&&��MFFA!-��`��ҷ�n��Yuu�����x<I�LB%����KԒ50�!�3�bz�����ʸKL���;---�l6[��`��������Ȏ;�����egg4��!D�T��jWujyZ��������K����%����p�^���X,6��sV=�ܖ����6�3�X ���4ҿ��A2�&����_k�;d�� �VfOIEND�B`�
Warning: session_start(): Session cannot be started after headers have already been sent in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 124

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 130

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 131

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 132

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 133

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 134

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 135
0x0x
PHP 8.2.31
Preview: FulfillmentsRenderer.php Size: 29.04 KB
/home/mciraet/www/wp-content/plugins/woocommerce/src/Admin/Features/Fulfillments/FulfillmentsRenderer.php
<?php
/**
 * WooCommerce order fulfillments renderer script.
 */

declare( strict_types=1 );

namespace Automattic\WooCommerce\Admin\Features\Fulfillments;

use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
use Automattic\WooCommerce\Utilities\OrderUtil;
use WC_Order;

/**
 * FulfillmentsRenderer class.
 */
class FulfillmentsRenderer {

	/**
	 * Fulfillments cache, that holds the fulfillments for each order to eliminate
	 * fetching fulfillment records of an order on each column render.
	 *
	 * @var array
	 */
	private array $fulfillments_cache = array();

	/**
	 * Registers the hooks related to fulfillments.
	 */
	public function register() {
		if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
			// Hook into column definitions and add the new fulfillment columns.
			add_filter( 'manage_woocommerce_page_wc-orders_columns', array( $this, 'add_fulfillment_columns' ) );
			// Hook into the column rendering and render the new fulfillment columns.
			add_action( 'manage_woocommerce_page_wc-orders_custom_column', array( $this, 'render_fulfillment_column_row_data' ), 10, 2 );
		} else {
			// For legacy orders table, hook into column definitions and add the new fulfillment columns.
			add_filter( 'manage_edit-shop_order_columns', array( $this, 'add_fulfillment_columns' ) );
			// Hook into the column rendering and render the new fulfillment columns.
			add_action( 'manage_shop_order_posts_custom_column', array( $this, 'render_fulfillment_column_row_data_legacy' ), 25, 1 );
		}
		// Hook into the admin footer to add the fulfillment drawer slot, which the React component will mount on.
		add_action( 'admin_footer', array( $this, 'render_fulfillment_drawer_slot' ) );
		// Hook into the admin enqueue scripts to load the fulfillment drawer component.
		add_action( 'admin_enqueue_scripts', array( $this, 'load_components' ) );
		// Hook into the order details page to render the fulfillment badges.
		add_action( 'woocommerce_admin_order_data_header_right', array( $this, 'render_order_details_badges' ) );
		// Hook into the order details before order table to render the fulfillment customer details.
		add_action( 'woocommerce_order_details_before_order_table', array( $this, 'render_fulfillment_customer_details' ) );
		// Initialize the renderer for bulk actions.
		add_action( 'admin_init', array( $this, 'init_admin_hooks' ) );
		// Hook into the order status text to append the fulfillment status.
		add_filter( 'woocommerce_order_details_status', array( $this, 'render_fulfillment_status_text' ), 10, 2 );
		add_filter( 'woocommerce_order_tracking_status', array( $this, 'render_fulfillment_status_text' ), 10, 2 );
	}

	/**
	 * Initialize the hooks that should run after `admin_init` hook.
	 */
	public function init_admin_hooks() {
		if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
			// For custom orders table, we need to add the bulk actions to the custom orders table.
			add_filter( 'bulk_actions-woocommerce_page_wc-orders', array( $this, 'define_fulfillment_bulk_actions' ) );
			add_filter( 'handle_bulk_actions-woocommerce_page_wc-orders', array( $this, 'handle_fulfillment_bulk_actions' ), 10, 3 );
			// For custom orders table, we need to filter the query to include fulfillment status.
			add_action( 'woocommerce_order_list_table_restrict_manage_orders', array( $this, 'render_fulfillment_filters' ) );
			add_action( 'woocommerce_order_list_table_restrict_manage_orders', array( $this, 'render_shipping_provider_filter' ) );
			add_filter( 'woocommerce_order_query_args', array( $this, 'filter_orders_list_table_query' ), 10, 1 );
			add_filter( 'woocommerce_order_list_table_prepare_items_query_args', array( $this, 'filter_orders_by_shipping_provider' ), 10, 1 );
		} else {
			// For legacy orders table, we need to add the bulk actions to the legacy orders table.
			add_filter( 'bulk_actions-edit-shop_order', array( $this, 'define_fulfillment_bulk_actions' ) );
			add_filter( 'handle_bulk_actions-edit-shop_order', array( $this, 'handle_fulfillment_bulk_actions' ), 10, 3 );
			// For legacy orders table, we need to filter the query to include fulfillment status.
			add_action( 'restrict_manage_posts', array( $this, 'render_fulfillment_filters_legacy' ) );
			add_action( 'restrict_manage_posts', array( $this, 'render_shipping_provider_filter_legacy' ) );
			add_action( 'pre_get_posts', array( $this, 'filter_legacy_orders_list_query' ) );
			add_action( 'pre_get_posts', array( $this, 'filter_legacy_orders_by_shipping_provider' ) );
		}
	}

	/**
	 * Add the fulfillment related columns to the orders table, after the order_status column.
	 *
	 * @param array $columns The columns in the orders page.
	 * @return array The modified columns.
	 */
	public function add_fulfillment_columns( $columns ) {
		$new_columns = array();
		foreach ( $columns as $column_name => $column_info ) {
			$new_columns[ $column_name ] = $column_info;
			if ( 'order_status' === $column_name ) {
				$new_columns[ $column_name ]       = 'Order Status';
				$new_columns['fulfillment_status'] = __( 'Fulfillment Status', 'woocommerce' );
				$new_columns['shipment_tracking']  = __( 'Shipment Tracking', 'woocommerce' );
				$new_columns['shipment_provider']  = __( 'Shipment Provider', 'woocommerce' );
			}
		}
		return $new_columns;
	}

	/**
	 * Render the fulfillment column row data for legacy order list support.
	 *
	 * @param string $column_name The name of the column.
	 */
	public function render_fulfillment_column_row_data_legacy( string $column_name ) {
		global $the_order;
		// This method is kept for legacy support, but the main rendering logic is now in render_fulfillment_column_row_data.
		return $this->render_fulfillment_column_row_data( $column_name, $the_order );
	}

	/**
	 * Render the fulfillment status column.
	 *
	 * @param string   $column_name The name of the column.
	 * @param WC_Order $order The order object.
	 */
	public function render_fulfillment_column_row_data( string $column_name, WC_Order $order ) {
		$fulfillments = $this->maybe_read_fulfillments( $order );

		// Render the column data based on the column name.
		switch ( $column_name ) {
			case 'fulfillment_status':
				$this->render_order_fulfillment_status_column_row_data( $order );
				break;
			case 'shipment_tracking':
				$this->render_shipment_tracking_column_row_data( $order, $fulfillments );
				break;
			case 'shipment_provider':
				$this->render_shipment_provider_column_row_data( $order, $fulfillments );
				break;
		}
	}

	/**
	 * Render the fulfillment status column row data.
	 *
	 * @param WC_Order $order The order object.
	 */
	private function render_order_fulfillment_status_column_row_data( WC_Order $order ) {
		$order_fulfillment_status = FulfillmentUtils::get_order_fulfillment_status( $order );

		echo "<div class='fulfillment-status-wrapper'>";
		$this->render_order_fulfillment_status_badge( $order, $order_fulfillment_status );
		echo '</div>';
	}

	/**
	 * Render the fulfillment status badge.
	 *
	 * @param WC_Order $order The order object.
	 * @param string   $order_fulfillment_status The fulfillment status of the order.
	 */
	private function render_order_fulfillment_status_badge( $order, string $order_fulfillment_status ) {
		$status_props = FulfillmentUtils::get_order_fulfillment_statuses()[ $order_fulfillment_status ];
		if ( ! $status_props ) {
			$status_props = array(
				'label'            => __( 'Unknown', 'woocommerce' ),
				'background_color' => '#f0f0f0',
				'text_color'       => '#000',
			);
		}

		echo '<mark class="fulfillment-status fulfillments-trigger" style="background-color:' . esc_attr( $status_props['background_color'] ) . '; color: ' . esc_attr( $status_props['text_color'] ) . ';" role="button" tabindex="0" data-order-id="' . esc_attr( (string) $order->get_id() ) . '"><span>' . esc_html( $status_props['label'] ) . '</span></mark>';
		echo "<a href='#' class='fulfillments-trigger' data-order-id='" . esc_attr( $order->get_id() ) . "' title='" . esc_attr__( 'View Fulfillments', 'woocommerce' ) . "'>
			<svg width='16' height='16' viewBox='0 0 12 14' xmlns='http://www.w3.org/2000/svg'>
				<path d='M11.8333 2.83301L9.33329 0.333008L2.24996 7.41634L1.41663 10.7497L4.74996 9.91634L11.8333 2.83301ZM5.99996 12.4163H0.166626V13.6663H5.99996V12.4163Z' />
			</svg>
		</a>";
	}

	/**
	 * Render the shipment provider column row data.
	 *
	 * @param WC_Order $order The order object.
	 * @param array    $fulfillments The fulfillments.
	 */
	private function render_shipment_provider_column_row_data( WC_Order $order, array $fulfillments ) {
		$providers = array();
		foreach ( $fulfillments as $fulfillment ) {
			$provider = $fulfillment->get_shipment_provider();
			if ( ! empty( $provider ) ) {
				$provider_name     = $fulfillment->get_meta( '_provider_name' );
				$key               = 'other' === $provider && ! empty( $provider_name )
					? $provider . '::' . $provider_name
					: $provider;
				$providers[ $key ] = $fulfillment;
			}
		}

		if ( count( $providers ) > 1 ) {
			echo '<span>' . esc_html__( 'Multiple providers', 'woocommerce' ) . '</span>';
		} elseif ( 1 === count( $providers ) ) {
			$provider_fulfillment   = reset( $providers );
			$provider_slug          = $provider_fulfillment->get_shipment_provider();
			$known_providers        = FulfillmentUtils::get_shipping_providers();
			$provider_name_meta     = $provider_fulfillment->get_meta( '_provider_name' );
			$provider_display_label = isset( $known_providers[ $provider_slug ] )
				? $known_providers[ $provider_slug ]->get_name()
				: ( ! empty( $provider_name_meta ) ? $provider_name_meta : $provider_slug );
			echo '<span>' . esc_html( $provider_display_label ) . '</span>';
		} else {
			echo '<span>--</span>';
		}
	}

	/**
	 * Render the shipment tracking column row data.
	 *
	 * @param WC_Order $order The order object.
	 * @param array    $fulfillments The fulfillments.
	 */
	private function render_shipment_tracking_column_row_data( WC_Order $order, array $fulfillments ) {
		$tracking = array();
		foreach ( $fulfillments as $fulfillment ) {
			$number = $fulfillment->get_tracking_number();
			if ( ! empty( $number ) ) {
				$tracking[] = array(
					'number' => $number,
					'url'    => $fulfillment->get_tracking_url(),
				);
			}
		}

		if ( count( $tracking ) > 1 ) {
			echo '<span>' . esc_html__( 'Multiple trackings', 'woocommerce' ) . '</span>';
		} elseif ( 1 === count( $tracking ) ) {
			$entry = $tracking[0];
			if ( ! empty( $entry['url'] ) ) {
				echo '<a href="' . esc_url( $entry['url'] ) . '" target="_blank" rel="noopener noreferrer" style="text-decoration: underline; color: #2f2f2f;">' . esc_html( $entry['number'] ) . '</a>';
			} else {
				echo '<span>' . esc_html( $entry['number'] ) . '</span>';
			}
		} else {
			echo '<span>--</span>';
		}
	}

	/**
	 * Render the fulfillment drawer.
	 */
	public function render_fulfillment_drawer_slot() {
		if ( ! $this->should_render_fulfillment_drawer() ) {
			return;
		}
		?>
		<div id="wc_order_fulfillments_panel_container"></div>
		<?php
	}

	/**
	 * Define bulk actions for fulfillments.
	 *
	 * @param array $actions Existing actions.
	 * @return array
	 */
	public function define_fulfillment_bulk_actions( $actions ) {
		$actions['fulfill'] = __( 'Mark as fulfilled', 'woocommerce' );

		return $actions;
	}

	/**
	 * Handle bulk actions for fulfillments.
	 *
	 * @param string $redirect_to The redirect URL.
	 * @param string $action The action being performed.
	 * @param array  $post_ids The post IDs being acted upon.
	 * @return string
	 */
	public function handle_fulfillment_bulk_actions( $redirect_to, $action, $post_ids ) {
		if ( 'fulfill' === $action ) {
			FulfillmentsTracker::track_fulfillment_bulk_action_used( 'fulfill_orders', count( $post_ids ) );
			foreach ( $post_ids as $post_id ) {
				$order = wc_get_order( $post_id );
				if ( ! $order ) {
					continue;
				}

				$fulfillments = $this->maybe_read_fulfillments( $order );

				// Fulfill all existing fulfillments.
				foreach ( $fulfillments as $fulfillment ) {
					$fulfillment->set_status( 'fulfilled' );
					$fulfillment->save();
				}

				// Create a fulfillment for the order, containing all remaining items in the order.
				$remaining_items = array_map(
					function ( $item ) {
						return array(
							'item_id' => $item['item_id'],
							'qty'     => $item['qty'],
						);
					},
					FulfillmentUtils::get_pending_items( $order, $fulfillments )
				);

				if ( 0 < count( $remaining_items ) ) {
					$fulfillment = new Fulfillment();
					$fulfillment->set_entity_type( WC_Order::class );
					$fulfillment->set_entity_id( (string) $order->get_id() );
					$fulfillment->set_status( 'fulfilled' );
					$fulfillment->set_items( $remaining_items );
					$fulfillment->save();
				}
			}
			$redirect_to = add_query_arg( array( 'bulk_action' => $action ), $redirect_to );
		}
		return $redirect_to;
	}

	/**
	 * Render the fulfillment status text in the order details page and the order tracking page.
	 *
	 * @param string   $order_status The order status text.
	 * @param WC_Order $order The order object.
	 *
	 * @return string The fulfillment status appended order status text.
	 */
	public function render_fulfillment_status_text( string $order_status, WC_Order $order ): string {
		$fulfillments       = $this->maybe_read_fulfillments( $order );
		$fulfillment_status = FulfillmentUtils::get_order_fulfillment_status_text( $order, $fulfillments );
		return sprintf( '%s %s', $order_status, $fulfillment_status );
	}

	/**
	 * Render the fulfillment customer details in the order details page.
	 *
	 * @param WC_Order $order The order object.
	 */
	public function render_fulfillment_customer_details( WC_Order $order ) {
		$fulfillments = $this->maybe_read_fulfillments( $order );

		if ( ! empty( $fulfillments ) ) {
			?>
<section class="woocommerce-order-details">
	<table class="woocommerce-table woocommerce-table--order-details shop_table order_details">
		<thead>
			<?php
			foreach ( $fulfillments as $index => $fulfillment ) {
				if ( ! $fulfillment->get_is_fulfilled() ) {
					continue;
				}
				?>
			<tr>
				<th class="woocommerce-table__shipment-info shipment-info" style="font-weight: normal;">
				<?php
				// Use the UTC fulfilled date when available; fall back to date_updated.
				$shipment_date_utc = $fulfillment->get_date_fulfilled()
					?? $fulfillment->get_date_updated();
				// Append ' UTC' so strtotime treats the stored value as UTC, then render in the site's timezone.
				$shipment_timestamp  = $shipment_date_utc ? strtotime( $shipment_date_utc . ' UTC' ) : false;
				$shipment_date_local = false !== $shipment_timestamp ? wp_date( 'F j, Y', $shipment_timestamp ) : '';
				printf(
					/* translators: %1$s is the shipment index, %2$s is the shipment date */
					wp_kses( __( '<b>Shipment %1$s</b> was shipped on <b>%2$s</b>', 'woocommerce' ), 'b' ),
					intval( $index ) + 1,
					esc_html( (string) $shipment_date_local )
				);
				?>
				</th>
				<th class="woocommerce-table__shipment-tracking shipment-tracking" style="font-weight: normal;">
					<?php echo wp_kses( FulfillmentUtils::get_tracking_info_html( $fulfillment ), 'a' ); ?>
				</th>
			</tr>
				<?php
			}
			?>
		</thead>
	</table>
</section>
			<?php
		}
	}

	/**
	 * Render the fulfillment badges in the order details page.
	 *
	 * @param WC_Order $order The order object.
	 */
	public function render_order_details_badges( WC_Order $order ) {
		echo '<div class="wc-order-fulfillment-badges">';

		// Get the fulfillment status for the order.
		$fulfillments             = $this->maybe_read_fulfillments( $order );
		$order_fulfillment_status = FulfillmentUtils::calculate_order_fulfillment_status( $order, $fulfillments );

		// Render order status badge.
		$order_status = $order->get_status();
		echo '<mark class="order-status status-' . esc_attr( $order_status ) . '"><span>' . esc_html( wc_get_order_status_name( $order_status ) ) . '</span></mark>';

		// Render fulfillment status badge.
		$this->render_order_fulfillment_status_badge( $order, $order_fulfillment_status );
		echo '</div>';
	}

	/**
	 * Loads the fulfillments scripts and styles.
	 */
	public function load_components() {
		if ( ! $this->should_render_fulfillment_drawer() ) {
			return;
		}

		$this->register_fulfillments_assets();
		$this->load_fulfillments_js_settings();
	}

	/**
	 * Register the fulfillment assets.
	 */
	protected function register_fulfillments_assets() {
		WCAdminAssets::register_style( 'fulfillments', 'style', array( 'wp-components' ) );
		WCAdminAssets::register_script( 'wp-admin-scripts', 'fulfillments', true );
	}

	/**
	 * Load the fulfillments JS settings.
	 *
	 * @return void
	 */
	protected function load_fulfillments_js_settings() {
		$providers_for_js = array();
		foreach ( FulfillmentUtils::get_shipping_providers() as $provider ) {
			$providers_for_js[ $provider->get_key() ] = array(
				'label' => $provider->get_name(),
				'icon'  => $provider->get_icon(),
				'value' => $provider->get_key(),
				'url'   => $provider->get_tracking_url( '__PLACEHOLDER__' ) ?? '',
			);
		}

		$fulfillment_settings = array(
			'providers'                  => $providers_for_js,
			'currency_symbols'           => get_woocommerce_currency_symbols(),
			'fulfillment_statuses'       => FulfillmentUtils::get_fulfillment_statuses(),
			'order_fulfillment_statuses' => FulfillmentUtils::get_order_fulfillment_statuses(),
		);

		wp_localize_script( 'wc-admin-fulfillments', 'wcFulfillmentSettings', $fulfillment_settings );
	}

	/**
	 * Render the fulfillment filters in the orders table.
	 */
	public function render_fulfillment_filters() {
		if ( ! self::should_render_fulfillment_drawer() ) {
			return;
		}
		?>
		<?php
		// This is a read-only filter on the admin orders table, so nonce verification is not required.
		// phpcs:ignore WordPress.Security.NonceVerification ?>
			<?php $selected_status = isset( $_GET['fulfillment_status'] ) ? sanitize_text_field( wp_unslash( $_GET['fulfillment_status'] ) ) : ''; ?>
		<select id="fulfillment-status-filter" name="fulfillment_status">
			<option value="" <?php selected( $selected_status, '' ); ?>><?php esc_html_e( 'Filter by fulfillment', 'woocommerce' ); ?></option>
				<?php foreach ( FulfillmentUtils::get_order_fulfillment_statuses() as $status => $props ) : ?>
				<option value="<?php echo esc_attr( $status ); ?>" <?php selected( $selected_status, $status ); ?>>
					<?php echo esc_html( $props['label'] ?? '' ); ?>
				</option>
			<?php endforeach; ?>
		</select>
			<?php
	}

	/**
	 * Render the fulfillment filters in the legacy orders table.
	 */
	public function render_fulfillment_filters_legacy() {
		global $typenow;

		if ( 'shop_order' !== $typenow ) {
			return;
		}

		$this->render_fulfillment_filters();
	}

	/**
	 * Apply the fulfillment status filter to the orders list.
	 *
	 * @param array $args The query arguments for the orders list.
	 * @return array The modified query arguments.
	 */
	public function filter_orders_list_table_query( $args ) {
		// This is a read-only filter on the admin orders table, so nonce verification is not required.
		// phpcs:ignore WordPress.Security.NonceVerification
		if ( isset( $_GET['fulfillment_status'] ) && ! empty( $_GET['fulfillment_status'] ) ) {
			// phpcs:ignore WordPress.Security.NonceVerification
			$fulfillment_status = sanitize_text_field( wp_unslash( $_GET['fulfillment_status'] ) );

			// Ensure the fulfillment status is one of the allowed values.
			if ( FulfillmentUtils::is_valid_order_fulfillment_status( $fulfillment_status ) ) {
				// Only track when the filter is explicitly submitted, not on pagination/refresh.
				if ( isset( $_GET['filter_action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
					FulfillmentsTracker::track_fulfillment_filter_used( 'fulfillment_status', $fulfillment_status );
				}
				$meta_query = FulfillmentUtils::get_order_fulfillment_status_meta_query( $fulfillment_status );
				if ( ! empty( $meta_query ) ) {
					if ( ! isset( $args['meta_query'] ) ) {
						$args['meta_query'] = array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
					}
					$args['meta_query'][] = $meta_query;
				}
			}
		}

		return $args;
	}

	/**
	 * Filter the legacy orders list query to include fulfillment status.
	 *
	 * @param \WP_Query $query The WP_Query object.
	 */
	public function filter_legacy_orders_list_query( $query ) {
		if (
		is_admin()
		&& $query->is_main_query()
		&& $query->get( 'post_type' ) === 'shop_order'
		&& isset( $_GET['fulfillment_status'] ) && ! empty( $_GET['fulfillment_status'] ) // phpcs:ignore WordPress.Security.NonceVerification
		) {
			$status = sanitize_text_field( wp_unslash( $_GET['fulfillment_status'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
			// Ensure the fulfillment status is one of the allowed values.
			if ( FulfillmentUtils::is_valid_order_fulfillment_status( $status ) ) {
				// Only track when the filter is explicitly submitted, not on pagination/refresh.
				if ( isset( $_GET['filter_action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
					FulfillmentsTracker::track_fulfillment_filter_used( 'fulfillment_status', $status );
				}
				$query->set(
					'meta_query',
					'no_fulfillments' === $status ?
					array(
						'relation' => 'OR',
						array(
							'key'     => '_fulfillment_status',
							'compare' => 'NOT EXISTS',
						),
					) :
					array(
						array(
							'key'     => '_fulfillment_status',
							'value'   => $status,
							'compare' => '=',
						),
					)
				);
			}
		}
	}

	/**
	 * Render the shipping provider filter dropdown in the orders table.
	 *
	 * @since 10.7.0
	 */
	public function render_shipping_provider_filter(): void {
		if ( ! self::should_render_fulfillment_drawer() ) {
			return;
		}

		$providers = FulfillmentUtils::get_shipping_providers();

		// This is a read-only filter on the admin orders table, so nonce verification is not required.
		// phpcs:ignore WordPress.Security.NonceVerification
		$selected_provider = isset( $_GET['shipping_provider'] ) ? sanitize_text_field( wp_unslash( $_GET['shipping_provider'] ) ) : '';
		?>
		<select id="shipping-provider-filter" name="shipping_provider">
			<option value="" <?php selected( $selected_provider, '' ); ?>><?php esc_html_e( 'Filter by shipping provider', 'woocommerce' ); ?></option>
			<?php foreach ( $providers as $provider ) : ?>
				<option value="<?php echo esc_attr( $provider->get_key() ); ?>" <?php selected( $selected_provider, $provider->get_key() ); ?>>
					<?php echo esc_html( $provider->get_name() ); ?>
				</option>
			<?php endforeach; ?>
			<option value="__other__" <?php selected( $selected_provider, '__other__' ); ?>><?php esc_html_e( 'Other', 'woocommerce' ); ?></option>
		</select>
		<?php
	}

	/**
	 * Render the shipping provider filter in the legacy orders table.
	 *
	 * @since 10.7.0
	 */
	public function render_shipping_provider_filter_legacy(): void {
		global $typenow;

		if ( 'shop_order' !== $typenow ) {
			return;
		}

		$this->render_shipping_provider_filter();
	}

	/**
	 * Filter orders by shipping provider for the HPOS orders list.
	 *
	 * @since 10.7.0
	 *
	 * @param array $args The query arguments for the orders list.
	 * @return array The modified query arguments.
	 */
	public function filter_orders_by_shipping_provider( $args ) {
		// This is a read-only filter on the admin orders table, so nonce verification is not required.
		// phpcs:ignore WordPress.Security.NonceVerification
		if ( ! isset( $_GET['shipping_provider'] ) || empty( $_GET['shipping_provider'] ) ) {
			return $args;
		}

		// phpcs:ignore WordPress.Security.NonceVerification
		$shipping_provider = sanitize_text_field( wp_unslash( $_GET['shipping_provider'] ) );
		$order_ids         = $this->get_order_ids_by_shipping_provider( $shipping_provider );

		if ( empty( $order_ids ) ) {
			$args['post__in'] = array( 0 );
		} elseif ( isset( $args['post__in'] ) && is_array( $args['post__in'] ) ) {
			$args['post__in'] = array_intersect( $args['post__in'], $order_ids );
			if ( empty( $args['post__in'] ) ) {
				$args['post__in'] = array( 0 );
			}
		} else {
			$args['post__in'] = $order_ids;
		}

		return $args;
	}

	/**
	 * Filter legacy orders by shipping provider.
	 *
	 * @since 10.7.0
	 *
	 * @param \WP_Query $query The WP_Query object.
	 */
	public function filter_legacy_orders_by_shipping_provider( $query ): void {
		if (
			! is_admin()
			|| ! $query->is_main_query()
			|| 'shop_order' !== $query->get( 'post_type' )
			// This is a read-only filter on the admin orders table, so nonce verification is not required.
			// phpcs:ignore WordPress.Security.NonceVerification
			|| ! isset( $_GET['shipping_provider'] )
			// phpcs:ignore WordPress.Security.NonceVerification
			|| empty( $_GET['shipping_provider'] )
		) {
			return;
		}

		// phpcs:ignore WordPress.Security.NonceVerification
		$shipping_provider = sanitize_text_field( wp_unslash( $_GET['shipping_provider'] ) );
		$order_ids         = $this->get_order_ids_by_shipping_provider( $shipping_provider );

		if ( empty( $order_ids ) ) {
			$query->set( 'post__in', array( 0 ) );
		} else {
			$existing = $query->get( 'post__in' );
			if ( ! empty( $existing ) && is_array( $existing ) ) {
				$order_ids = array_intersect( $existing, $order_ids );
				if ( empty( $order_ids ) ) {
					$order_ids = array( 0 );
				}
			}
			$query->set( 'post__in', $order_ids );
		}
	}

	/**
	 * Get order IDs that have fulfillments with a specific shipping provider.
	 *
	 * @since 10.7.0
	 *
	 * @param string $shipping_provider The shipping provider key to filter by.
	 * @return array Array of order IDs.
	 */
	private function get_order_ids_by_shipping_provider( string $shipping_provider ): array {
		global $wpdb;

		$fulfillments_table = $wpdb->prefix . 'wc_order_fulfillments';
		$meta_table         = $wpdb->prefix . 'wc_order_fulfillment_meta';

		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
		if ( '__other__' === $shipping_provider ) {
			$known_providers = FulfillmentUtils::get_shipping_providers();
			$known_keys      = array_keys( $known_providers );

			if ( empty( $known_keys ) ) {
				$results = $wpdb->get_col(
					$wpdb->prepare(
						"SELECT DISTINCT f.entity_id
						FROM {$fulfillments_table} f
						INNER JOIN {$meta_table} m ON f.fulfillment_id = m.fulfillment_id
						WHERE m.meta_key = %s
						AND m.meta_value IS NOT NULL
						AND m.meta_value != ''
						AND f.date_deleted IS NULL
						AND m.date_deleted IS NULL",
						'_shipment_provider'
					)
				);
			} else {
				$placeholders = implode( ',', array_fill( 0, count( $known_keys ), '%s' ) );
				$results      = $wpdb->get_col(
					$wpdb->prepare(
						"SELECT DISTINCT f.entity_id
						FROM {$fulfillments_table} f
						INNER JOIN {$meta_table} m ON f.fulfillment_id = m.fulfillment_id
						WHERE m.meta_key = '_shipment_provider'
						AND m.meta_value NOT IN ({$placeholders})
						AND m.meta_value IS NOT NULL
						AND m.meta_value != ''
						AND f.date_deleted IS NULL
						AND m.date_deleted IS NULL",
						...array_map( 'wp_json_encode', $known_keys )
					)
				);
			}
		} else {
			$results = $wpdb->get_col(
				$wpdb->prepare(
					"SELECT DISTINCT f.entity_id
					FROM {$fulfillments_table} f
					INNER JOIN {$meta_table} m ON f.fulfillment_id = m.fulfillment_id
					WHERE m.meta_key = '_shipment_provider'
					AND m.meta_value = %s
					AND f.date_deleted IS NULL
					AND m.date_deleted IS NULL",
					wp_json_encode( $shipping_provider )
				)
			);
		}
		// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare

		return array_map( 'absint', $results );
	}

	/**
	 * Check if the fulfillment drawer should be rendered (admin only).
	 *
	 * @return bool True if the fulfillment drawer should be rendered, false otherwise.
	 */
	protected function should_render_fulfillment_drawer(): bool {
		if ( ! is_admin() ) {
			return false;
		}

		if ( ! function_exists( 'get_current_screen' ) ) {
			return false;
		}

		$current_screen = get_current_screen();
		if ( ! $current_screen || ! $current_screen->id ) {
			return false;
		}

		return 'woocommerce_page_wc-orders' === $current_screen->id // HPOS screen.
		|| 'edit-shop_order' === $current_screen->id // Legacy screen.
		|| 'shop_order' === $current_screen->id; // Order details screen (legacy).
	}

	/**
	 * Fetches the fulfillments for the given order, caching them to avoid multiple fetches.
	 *
	 * @param WC_Order $order The order object.
	 *
	 * @return array The fulfillments for the order.
	 */
	private function maybe_read_fulfillments( WC_Order $order ): array {
		// Check if we've already fetched the fulfillments for this order.
		if ( isset( $this->fulfillments_cache[ $order->get_id() ] ) ) {
			return $this->fulfillments_cache[ $order->get_id() ];
		}

		// If not, fetch them and cache them.
		try {
			/**
			 * Fulfillments data store.
			 *
			 * @var \Automattic\WooCommerce\Admin\Features\Fulfillments\DataStore\FulfillmentsDataStore $data_store
			 */
			$data_store   = \WC_Data_Store::load( 'order-fulfillment' );
			$fulfillments = $data_store->read_fulfillments( WC_Order::class, '' . $order->get_id() );
		} catch ( \Throwable $e ) {
			wc_get_logger()->error(
				sprintf( 'Failed to load fulfillments for order %d: %s', $order->get_id(), $e->getMessage() ),
				array( 'source' => 'fulfillments' )
			);
			$fulfillments = array();
		}
		$this->fulfillments_cache[ $order->get_id() ] = $fulfillments;

		return $fulfillments;
	}
}

Directory Contents

Dirs: 2 × Files: 11
Name Size Perms Modified Actions
DataStore DIR
- drwxr-xr-x 2026-08-11 10:05:47
Edit Download
Providers DIR
- drwxr-xr-x 2026-08-11 10:05:47
Edit Download
14.27 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
1.07 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
12.36 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
8.45 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
20.38 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
29.04 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
6.09 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
15.63 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
22.99 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
46.73 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
6.34 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).
© 2026 0xNothings — Secure File Manager. All rights reserved. Built with ❤️ & Tailwind x Dark UI