own_order_ids(); if ( empty( $mine ) ) { return $query; } $mine = array_diff( $mine, array( absint( $exclude_order_id ) ) ); if ( empty( $mine ) ) { return $query; } $ids = implode( ',', array_map( 'absint', $mine ) ); return $query . " AND stock_table.`order_id` NOT IN ( {$ids} )"; } public function cap_reservation_minutes( $minutes, $order = null ) { if ( ! apply_filters( 'aieo_stock_hold_guard_enabled', true ) ) { return $minutes; } $cap = (int) apply_filters( 'aieo_stock_reservation_max_minutes', $this->configured_cap(), $order ); if ( $cap <= 0 ) { return $minutes; } $minutes = (int) $minutes; return ( $minutes > $cap ) ? $cap : $minutes; } public function remember_store_api_order( $order, $request = null ) { unset( $request ); if ( $order instanceof WC_Order ) { $this->remember( (int) $order->get_id() ); } } public function remember_classic_order( $order_id ) { $this->remember( (int) $order_id ); } private function remember( $order_id ) { if ( $order_id <= 0 ) { return; } if ( ! function_exists( 'WC' ) || ! WC() || ! WC()->session ) { return; } $ids = WC()->session->get( self::SESSION_ORDERS_KEY, array() ); if ( ! is_array( $ids ) ) { $ids = array(); } $ids = array_values( array_unique( array_merge( array( $order_id ), array_map( 'absint', $ids ) ) ) ); if ( count( $ids ) > self::MAX_REMEMBERED ) { $ids = array_slice( $ids, 0, self::MAX_REMEMBERED ); } WC()->session->set( self::SESSION_ORDERS_KEY, $ids ); $this->own_ids_memo = null; } private function configured_cap() { $settings = get_option( 'aieo_dmm_settings', array() ); if ( is_array( $settings ) && isset( $settings['stock_reservation_minutes'] ) ) { return max( 0, (int) $settings['stock_reservation_minutes'] ); } return self::DEFAULT_RESERVE_MINUTES; } private function own_order_ids() { if ( $this->own_ids_memo !== null ) { return $this->own_ids_memo; } $ids = array(); if ( function_exists( 'WC' ) && WC() && WC()->session ) { $draft = absint( WC()->session->get( 'store_api_draft_order', 0 ) ); if ( $draft > 0 ) { $ids[] = $draft; } $awaiting = absint( WC()->session->get( 'order_awaiting_payment', 0 ) ); if ( $awaiting > 0 ) { $ids[] = $awaiting; } $remembered = WC()->session->get( self::SESSION_ORDERS_KEY, array() ); if ( is_array( $remembered ) ) { foreach ( $remembered as $id ) { $id = absint( $id ); if ( $id > 0 ) { $ids[] = $id; } } } } foreach ( $this->identity_order_ids() as $id ) { $ids[] = $id; } $this->own_ids_memo = array_values( array_unique( $ids ) ); return $this->own_ids_memo; } private function identity_order_ids() { if ( ! function_exists( 'wc_get_orders' ) ) { return array(); } if ( ! apply_filters( 'aieo_stock_hold_guard_identity_enabled', true ) ) { return array(); } $customer = 0; if ( function_exists( 'is_user_logged_in' ) && is_user_logged_in() ) { $customer = get_current_user_id(); } elseif ( function_exists( 'WC' ) && WC() && WC()->customer ) { $email = (string) WC()->customer->get_billing_email(); if ( '' !== $email && is_email( $email ) ) { $customer = $email; } } if ( empty( $customer ) ) { return array(); } $minutes = max( (int) get_option( 'woocommerce_hold_stock_minutes', 60 ), (int) $this->configured_cap() ); $after = time() - ( ( $minutes * MINUTE_IN_SECONDS ) + DAY_IN_SECONDS ); $ids = wc_get_orders( array( 'customer' => $customer, 'status' => self::IDENTITY_STATUSES, 'limit' => self::IDENTITY_MAX, 'return' => 'ids', 'orderby' => 'date', 'order' => 'DESC', 'date_created' => '>' . $after, ) ); return is_array( $ids ) ? array_map( 'absint', $ids ) : array(); } } }