�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: FulfillmentsDataStore.php Size: 19.32 KB
/home/mciraet/www/wp-content/plugins/woocommerce/src/Admin/Features/Fulfillments/DataStore/FulfillmentsDataStore.php
<?php
/**
 * Class FulfillmentsDataStore file.
 *
 * @package WooCommerce\DataStores
 */

declare( strict_types=1 );

namespace Automattic\WooCommerce\Admin\Features\Fulfillments\DataStore;

use Automattic\WooCommerce\Admin\Features\Fulfillments\Fulfillment;
use Automattic\WooCommerce\Admin\Features\Fulfillments\FulfillmentUtils;
use WC_Meta_Data;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * WC Order Item Product Data Store
 *
 * @version  9.9.0
 */
class FulfillmentsDataStore extends \WC_Data_Store_WP implements \WC_Object_Data_Store_Interface, FulfillmentsDataStoreInterface {

	/**
	 * Method to create a new fulfillment in the database.
	 *
	 * @param Fulfillment $data The fulfillment object to create.
	 *
	 * @return void
	 *
	 * @throws \Exception If the fulfillment data is invalid.
	 * @throws \Exception If the fulfillment can't be created.
	 */
	public function create( &$data ): void {
		// Validate the fulfillment data.
		if ( ! $data->get_entity_type() ) {
			throw new \Exception( esc_html__( 'Invalid entity type.', 'woocommerce' ) );
		}
		if ( ! $data->get_entity_id() ) {
			throw new \Exception( esc_html__( 'Invalid entity ID.', 'woocommerce' ) );
		}
		if ( ! FulfillmentUtils::is_valid_fulfillment_status( $data->get_status() ) ) {
			throw new \Exception( esc_html__( 'Invalid fulfillment status.', 'woocommerce' ) );
		}

		$this->validate_items( $data );

		// Set fulfillment properties.
		$data->set_date_updated( current_time( 'mysql' ) );

		/**
		 * Filter to modify the fulfillment data before it is created.
		 *
		 * @since 10.1.0
		 */
		$data = apply_filters( 'woocommerce_fulfillment_before_create', $data );

		$is_fulfill_action = $data->get_is_fulfilled();
		// If the fulfillment is fulfilled, set the fulfilled date.
		if ( $is_fulfill_action ) {
			$data->set_date_fulfilled( current_time( 'mysql' ) );

			/**
			 * Filter to modify the fulfillment data before it is fulfilled.
			 *
			 * @since 10.1.0
			 */
			$data = apply_filters(
				'woocommerce_fulfillment_before_fulfill',
				$data
			);
		}

		// Save the fulfillment to the database.
		global $wpdb;
		$rows_inserted = $wpdb->insert(
			$wpdb->prefix . 'wc_order_fulfillments',
			array(
				'entity_type'  => $data->get_entity_type(),
				'entity_id'    => $data->get_entity_id(),
				'status'       => $data->get_status() ?? 'unfulfilled',
				'is_fulfilled' => $data->get_is_fulfilled() ? 1 : 0,
				'date_updated' => $data->get_date_updated(),
				'date_deleted' => $data->get_date_deleted(),
			),
			array( '%s', '%s', '%s', '%d', '%s', '%s' )
		);

		// Check for errors.
		if ( false === $rows_inserted ) {
			throw new \Exception( esc_html__( 'Failed to insert fulfillment.', 'woocommerce' ) );
		}

		// Set the ID of the fulfillment object.
		$data_id = $wpdb->insert_id;

		$data->set_id( $data_id );

		// Save the metadata for the fulfillment to the database.
		$data->save_meta_data();

		// Apply changes let's the object know that the current object reflects the database and no "changes" exist between the two.
		$data->apply_changes();
		$data->set_object_read( true );

		if ( ! doing_action( 'woocommerce_fulfillment_after_create' ) ) {
			/**
			* Action to perform after a fulfillment is created.
			*
			* @param Fulfillment $data The fulfillment object that was created.
			*
			* @since 10.1.0
			*/
			do_action( 'woocommerce_fulfillment_after_create', $data );
		}

		if ( $is_fulfill_action && ! doing_action( 'woocommerce_fulfillment_after_fulfill' ) ) {
			/**
			 * Action to perform after a fulfillment is fulfilled.
			 *
			 * @since 10.1.0
			 */
			do_action( 'woocommerce_fulfillment_after_fulfill', $data );
		}
	}

	/**
	 * Method to read a fulfillment from the database.
	 *
	 * @param Fulfillment $data The fulfillment object to read.
	 *
	 * @return void
	 *
	 * @throws \Exception If the fulfillment data can't be read.
	 */
	public function read( &$data ): void {
		// Read the fulfillment from the database.
		global $wpdb;

		$data_id          = $data->get_id();
		$fulfillment_data = $wpdb->get_row(
			$wpdb->prepare(
				"SELECT * FROM {$wpdb->prefix}wc_order_fulfillments WHERE fulfillment_id = %d",
				$data_id
			),
			ARRAY_A
		);

		if ( empty( $fulfillment_data ) ) {
			throw new \Exception( esc_html__( 'Fulfillment not found.', 'woocommerce' ) );
		}

		// Use set_props_from_storage() so already-UTC date columns are not re-normalized as site-local.
		$data->set_props_from_storage( array_diff_key( $fulfillment_data, array( 'fulfillment_id' => true ) ) );
		$data->set_id( (int) $fulfillment_data['fulfillment_id'] );
		$data->read_meta_data( true );
		$data->set_object_read( true );
		$data->snapshot_meta();
	}

	/**
	 * Method to update an existing fulfillment in the database.
	 *
	 * @param Fulfillment $data The fulfillment object to update.
	 *
	 * @return void
	 *
	 * @throws \Exception If the fulfillment can't be updated.
	 */
	public function update( &$data ): void {
		// If the fulfillment is deleted, do nothing.
		if ( $data->get_date_deleted() ) {
			return;
		}

		// Update the fulfillment in the database.
		$data_id = $data->get_id();

		if ( ! FulfillmentUtils::is_valid_fulfillment_status( $data->get_status() ) ) {
			throw new \Exception( esc_html__( 'Invalid fulfillment status.', 'woocommerce' ) );
		}

		$this->validate_items( $data );

		/**
		 * Filter to modify the fulfillment data before it is updated.
		 *
		 * @param Fulfillment $data The fulfillment object that is being updated.
		 *
		 * @since 10.1.0
		 */
		$data = apply_filters( 'woocommerce_fulfillment_before_update', $data );

		// If the fulfillment is fulfilled, set the fulfilled date.
		$is_fulfill_action = false;
		if ( $data->get_is_fulfilled() && empty( $data->get_date_fulfilled() ) ) {
			$is_fulfill_action = true;
			$data->set_date_fulfilled( current_time( 'mysql' ) );

			/**
			 * Filter to modify the fulfillment data before it is fulfilled.
			 *
			 * @param Fulfillment $data The fulfillment object that is being fulfilled.
			 *
			 * @since 10.1.0
			 */
			$data = apply_filters(
				'woocommerce_fulfillment_before_fulfill',
				$data
			);
		}

		global $wpdb;

		// Capture changes and previous status before set_date_updated (which always
		// changes) and before apply_changes resets the tracking.
		$changes         = $data->get_changes();
		$previous_status = $data->get_data()['status'] ?? 'unfulfilled';

		$data->set_date_updated( current_time( 'mysql' ) );

		$wpdb->update(
			$wpdb->prefix . 'wc_order_fulfillments',
			array(
				'entity_type'  => $data->get_entity_type(),
				'entity_id'    => $data->get_entity_id(),
				'status'       => $data->get_status(),
				'is_fulfilled' => $data->get_is_fulfilled() ? 1 : 0,
				'date_updated' => $data->get_date_updated(),
				'date_deleted' => $data->get_date_deleted(),
			),
			array(
				'fulfillment_id' => $data_id,
				'date_deleted'   => null,
			),
			array( '%s', '%s', '%s', '%d', '%s', '%s' ),
			array( '%d' )
		);

		// Check for errors.
		if ( $wpdb->last_error ) {
			throw new \Exception( esc_html__( 'Failed to update fulfillment.', 'woocommerce' ) );
		}

		// Update the metadata for the fulfillment.
		$data->save_meta_data();
		$data->apply_changes();

		$data->set_object_read( true );

		if ( ! doing_action( 'woocommerce_fulfillment_after_update' ) ) {
			/**
			 * Action to perform after a fulfillment is updated.
			 *
			 * @param Fulfillment $data            The fulfillment object that was updated.
			 * @param array       $changes         The changes that were applied, as returned by
			 *                                     Fulfillment::get_changes() before save. Core data
			 *                                     props at top level, meta changes under 'meta_data'.
			 * @param string      $previous_status The fulfillment status before the update.
			 *
			 * @since 10.1.0
			 * @since 10.7.0 Added $changes and $previous_status parameters.
			 */
			do_action( 'woocommerce_fulfillment_after_update', $data, $changes, $previous_status );
		}

		if ( $is_fulfill_action && ! doing_action( 'woocommerce_fulfillment_after_fulfill' ) ) {
			/**
			 * Action to perform after a fulfillment is fulfilled.
			 *
			 * @param Fulfillment $data The fulfillment object that was fulfilled.
			 *
			 * @since 10.1.0
			 */
			do_action( 'woocommerce_fulfillment_after_fulfill', $data );
		}
	}

	/**
	 * Method to delete a fulfillment from the database.
	 *
	 * @param Fulfillment $data The fulfillment object to delete.
	 * @param array       $args Optional arguments to pass to the delete method.
	 *
	 * @return void
	 *
	 * @throws \Exception If the fulfillment can't be deleted.
	 */
	public function delete( &$data, $args = array() ): void {
		// If the record is already deleted, do nothing.
		if ( $data->get_date_deleted() ) {
			return;
		}

		/**
		 * Filter to modify the fulfillment data before it is updated.
		 *
		 * @since 10.1.0
		 */
		$data = apply_filters( 'woocommerce_fulfillment_before_delete', $data );

		// Soft Delete the fulfillment from the database.
		global $wpdb;

		$data_id = $data->get_id();

		// Route through the setter so the stored value is normalized to UTC,
		// then read it back for the direct DB write below.
		$data->set_date_deleted( current_time( 'mysql' ) );
		$deletion_time = $data->get_date_deleted();

		$wpdb->update(
			$wpdb->prefix . 'wc_order_fulfillments',
			array( 'date_deleted' => $deletion_time ),
			array(
				'fulfillment_id' => $data_id,
				'date_deleted'   => null,
			),
			array( '%s' ),
			array( '%d' )
		);

		// Check for errors.
		if ( $wpdb->last_error ) {
			throw new \Exception( esc_html__( 'Failed to delete fulfillment.', 'woocommerce' ) );
		}
		$data->apply_changes();
		$data->set_object_read( true );

		if ( ! doing_action( 'woocommerce_fulfillment_after_delete' ) ) {
			/**
			 * Action to perform after a fulfillment is deleted.
			 *
			 * @since 10.1.0
			 */
			do_action( 'woocommerce_fulfillment_after_delete', $data );
		}

		// Set the fulfillment object to a fresh state.
		$data = new Fulfillment();
	}

	/**
	 * Method to read the metadata for a fulfillment.
	 *
	 * @param Fulfillment $data The fulfillment object to read.
	 * @return array
	 *
	 * @throws \Exception If the fulfillment is not saved.
	 */
	public function read_meta( &$data ): array {
		if ( ! $data->get_id() ) {
			throw new \Exception( esc_html__( 'Invalid fulfillment.', 'woocommerce' ) );
		}

		// Read the metadata for the fulfillment.
		global $wpdb;

		$data_id   = $data->get_id();
		$meta_data = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT * FROM {$wpdb->prefix}wc_order_fulfillment_meta WHERE fulfillment_id = %d",
				$data_id
			),
			OBJECT
		);

		return array_map(
			function ( $meta ) {
				$meta->meta_value = json_decode( $meta->meta_value, true ) ?? $meta->meta_value;
				return $meta;
			},
			$meta_data
		);
	}

	/**
	 * Method to delete the metadata for a fulfillment.
	 *
	 * @param Fulfillment  $data The fulfillment object to delete.
	 * @param WC_Meta_Data $meta Meta object (containing at least ->id).
	 *
	 * @return void
	 *
	 * @throws \Exception If the fulfillment or meta is not saved.
	 */
	public function delete_meta( &$data, $meta ): void {
		// Check if the fulfillment and meta are saved.
		$data_id = $data->get_id();

		// Prevent deletion of metadata from a deleted fulfillment.
		if ( $data->get_date_deleted() ) {
			throw new \Exception( esc_html__( 'Cannot delete meta from a deleted fulfillment.', 'woocommerce' ) );
		}

		$meta_id = $meta->id;
		if ( ! is_numeric( $data_id ) || $data_id <= 0 || ! is_numeric( $meta_id ) || $meta_id <= 0 ) {
			throw new \Exception( esc_html__( 'Invalid fulfillment or meta.', 'woocommerce' ) );
		}

		// Delete the metadata for the fulfillment.
		global $wpdb;

		$wpdb->delete(
			$wpdb->prefix . 'wc_order_fulfillment_meta',
			array(
				'fulfillment_id' => $data_id,
				'meta_id'        => $meta_id,
			),
			array(
				'%d',
				'%d',
			)
		);
	}

	/**
	 * Method to add metadata for a fulfillment.
	 *
	 * @param Fulfillment  $data The fulfillment object to save.
	 * @param WC_Meta_Data $meta Meta object (containing at least ->id).
	 * @return int meta ID or WP_Error on failure.
	 *
	 * @throws \Exception If the fulfillment or meta is not saved.
	 */
	public function add_meta( &$data, $meta ): int {
		// Add the metadata for the fulfillment.
		global $wpdb;

		// Prevent adding metadata to a deleted fulfillment.
		if ( $data->get_date_deleted() ) {
			throw new \Exception( esc_html__( 'Cannot add meta to a deleted fulfillment.', 'woocommerce' ) );
		}

		// Data ID can't be something wrong as this function is called after the meta is read.
		// See WC_Data::save_meta_data().
		$data_id = $data->get_id();

		$wpdb->insert(
			$wpdb->prefix . 'wc_order_fulfillment_meta',
			array(
				'fulfillment_id' => $data_id,
				'meta_key'       => $meta->key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
				'meta_value'     => wp_json_encode( $meta->value ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
			),
			array(
				'%d',
				'%s',
				'%s',
			)
		);

		// Note: There is no error check on WC_Data::save_meta_data(), and it expects us to return an ID in all cases.
		// If there's an error, we should return null to indicate we didn't save it.
		if ( $wpdb->last_error ) {
			throw new \Exception( esc_html__( 'Failed to insert fulfillment meta.', 'woocommerce' ) );
		}

		return $wpdb->insert_id;
	}

	/**
	 * Method to save the metadata for a fulfillment.
	 *
	 * @param Fulfillment  $data The fulfillment object to save.
	 * @param WC_Meta_Data $meta Meta object (containing at least ->id).
	 *
	 * @return int Number of rows updated.
	 *
	 * @throws \Exception If the fulfillment or meta is not saved.
	 */
	public function update_meta( &$data, $meta ): int {
		// Update the metadata for the fulfillment.
		global $wpdb;

		$data_id = $data->get_id();

		// Prevent updating metadata for a deleted fulfillment.
		if ( $data->get_date_deleted() ) {
			throw new \Exception( esc_html__( 'Cannot update meta for a deleted fulfillment.', 'woocommerce' ) );
		}

		$rows_updated = $wpdb->update(
			$wpdb->prefix . 'wc_order_fulfillment_meta',
			array(
				'meta_value' => wp_json_encode( $meta->value ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
			),
			array(
				'fulfillment_id' => $data_id,
				'meta_id'        => $meta->id,
				'meta_key'       => $meta->key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
			),
			array(
				'%s',
			),
			array(
				'%d',
				'%d',
				'%s',
			)
		);

		// Check for errors.
		if ( $wpdb->last_error ) {
			throw new \Exception( esc_html__( 'Failed to update fulfillment meta.', 'woocommerce' ) );
		}

		return $rows_updated;
	}

	/**
	 * Method to read the fulfillment data.
	 *
	 * @param string $entity_type The entity type.
	 * @param string $entity_id The entity ID.
	 * @param bool   $with_deleted Whether to include deleted fulfillments in the results.
	 *
	 * @return Fulfillment[] Fulfillment object.
	 *
	 * @throws \Exception If the fulfillment data can't be read.
	 */
	public function read_fulfillments( string $entity_type, string $entity_id, bool $with_deleted = false ): array {
		// Read the fulfillment data from the database.
		global $wpdb;

		if ( ! $with_deleted ) {
			$fulfillment_data = $wpdb->get_results(
				$wpdb->prepare(
					"SELECT * FROM {$wpdb->prefix}wc_order_fulfillments WHERE entity_type = %s AND entity_id = %s AND date_deleted IS NULL",
					$entity_type,
					$entity_id
				),
				ARRAY_A
			);
		} else {
			$fulfillment_data = $wpdb->get_results(
				$wpdb->prepare(
					"SELECT * FROM {$wpdb->prefix}wc_order_fulfillments WHERE entity_type = %s AND entity_id = %s",
					$entity_type,
					$entity_id
				),
				ARRAY_A
			);
		}

		if ( is_wp_error( $fulfillment_data ) ) {
			throw new \Exception( esc_html__( 'Failed to read fulfillment data.', 'woocommerce' ) );
		}

		// Create Fulfillment objects from the data.
		$fulfillments = array();
		foreach ( $fulfillment_data as $data ) {
			// Note: Don't initialize with ID, it will cause a re-read from the database.
			// Set the ID directly after the object is created.
			$fulfillment = new Fulfillment();
			$fulfillment->set_id( $data['fulfillment_id'] );
			// Use set_props_from_storage() so already-UTC date columns are not re-normalized as site-local.
			$fulfillment->set_props_from_storage( $data );
			$fulfillment->apply_changes();
			$fulfillment->set_object_read( true );

			// Read the metadata for the fulfillment.
			$fulfillment->read_meta_data( true );
			$fulfillment->snapshot_meta();

			$fulfillments[] = $fulfillment;
		}

		return $fulfillments;
	}

	/**
	 * Hard-delete all fulfillment records (and their metadata) for a given entity.
	 *
	 * This is used when an order is permanently deleted to prevent orphaned rows.
	 *
	 * @since 10.7.0
	 *
	 * @param string $entity_type The entity type (e.g. 'WC_Order').
	 * @param string $entity_id   The entity ID.
	 *
	 * @return int The number of fulfillment records deleted.
	 *
	 * @throws \RuntimeException If a database query fails.
	 * @throws \Throwable If the deletion fails.
	 */
	public function delete_by_entity( string $entity_type, string $entity_id ): int {
		global $wpdb;

		wc_transaction_query( 'start' );

		try {
			// Delete metadata for all fulfillments belonging to this entity.
			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table names are safe.
			$result = $wpdb->query(
				$wpdb->prepare(
					"DELETE m FROM {$wpdb->prefix}wc_order_fulfillment_meta m INNER JOIN {$wpdb->prefix}wc_order_fulfillments f ON m.fulfillment_id = f.fulfillment_id WHERE f.entity_type = %s AND f.entity_id = %s",
					$entity_type,
					$entity_id
				)
			);

			if ( false === $result ) {
				throw new \RuntimeException( 'Failed to delete fulfillment metadata: ' . $wpdb->last_error );
			}

			// Delete the fulfillment records themselves.
			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name is safe.
			$rows_deleted = $wpdb->query(
				$wpdb->prepare(
					"DELETE FROM {$wpdb->prefix}wc_order_fulfillments WHERE entity_type = %s AND entity_id = %s",
					$entity_type,
					$entity_id
				)
			);

			if ( false === $rows_deleted ) {
				throw new \RuntimeException( 'Failed to delete fulfillment records: ' . $wpdb->last_error );
			}

			wc_transaction_query( 'commit' );
		} catch ( \Throwable $e ) {
			wc_transaction_query( 'rollback' );
			throw $e;
		}

		return (int) $rows_deleted;
	}

	/**
	 * Method to validate the items in a fulfillment.
	 *
	 * @param Fulfillment $data The fulfillment object to validate.
	 *
	 * @return void
	 *
	 * @throws \Exception If the fulfillment data is invalid.
	 */
	private function validate_items( Fulfillment $data ): void {
		$items = $data->get_meta( '_items', true );
		if ( empty( $items ) ) {
			throw new \Exception( esc_html__( 'The fulfillment should contain at least one item.', 'woocommerce' ) );
		}

		if ( ! is_array( $items ) ) {
			throw new \Exception( esc_html__( 'The fulfillment items should be an array.', 'woocommerce' ) );
		}

		foreach ( $data->get_items() as $item ) {
			if ( ! isset( $item['item_id'] )
				// The item ID and qty should be set.
				|| ! isset( $item['qty'] )
				// The item ID should be integers.
				|| ! is_int( $item['item_id'] )
				// Allow the qty to be a float too.
				|| ( ! is_int( $item['qty'] ) && ! is_float( $item['qty'] ) )
				// The item ID and qty should be greater than 0.
				|| $item['item_id'] <= 0
				|| $item['qty'] <= 0
				) {
				throw new \Exception( esc_html__( 'Invalid item.', 'woocommerce' ) );
			}
		}
	}
}

Directory Contents

Dirs: 0 × Files: 2
Name Size Perms Modified Actions
19.32 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
667 B 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