Plugin Directory

Changeset 2103605


Ignore:
Timestamp:
06/10/2019 11:46:12 PM (7 years ago)
Author:
ferdev
Message:

Update trunk to 1.1.8

Location:
woocommerce-accommodation-bookings/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • woocommerce-accommodation-bookings/trunk/changelog.txt

    r2100652 r2103605  
    11*** Changelog ***
     2
     3= 1.1.8 - 2019-06-10 =
     4* Fix - Adds accommodation bookings support for WooCommerce Bookings Availability extension
    25
    36= 1.1.7 - 2019-06-03 =
  • woocommerce-accommodation-bookings/trunk/includes/class-wc-accommodation-booking.php

    r1919134 r2103605  
    1818        add_filter( 'get_booking_products_terms', array( $this, 'add_accommodation_to_booking_product_terms' ) );
    1919        add_filter( 'get_booking_products_args', array( $this, 'add_accommodation_to_booking_products_args' ) );
     20        add_filter( 'woocommerce_bookings_product_rest_endpoint', array( $this, 'add_accommodation_to_booking_products_args' ) );
     21        add_filter( 'get_booking_products_args_for_slots_rest_endpoint', array( $this, 'add_accommodation_to_booking_products_args' ) );
     22        add_filter( 'woocommerce_bookings_product_type_rest_check', array( $this, 'validate_rest_product_type' ), 10, 2 );
    2023
    2124        add_action( 'woocommerce_new_booking', array( $this, 'update_start_end_time' ) );
     
    4346        $types[] = 'accommodation-booking';
    4447        return $types;
     48    }
     49
     50    /**
     51     * Hooks into woocommerce_bookings_product_type_rest_check and verifies that product is a correct bookings type
     52     */
     53    public function validate_rest_product_type( $is_product_valid, $product ) {
     54        return $is_product_valid || 'accommodation-booking' === $product->get_type();
    4555    }
    4656
  • woocommerce-accommodation-bookings/trunk/includes/class-wc-product-accommodation-booking.php

    r2100652 r2103605  
    188188
    189189    /**
     190     * Find available and booked blocks for specific resources (if any) and return them as array.
     191     *
     192     * @param  array  $blocks
     193     * @param  array  $intervals
     194     * @param  integer $resource_id
     195     * @param  integer $from The starting date for the set of blocks
     196     * @param  integer $to
     197     * @return array
     198     */
     199    public function get_time_slots( $blocks, $resource_id = 0, $from = 0, $to = 0, $include_sold_out = false ) {
     200        $bookable_product = $this;
     201
     202        $transient_name               = 'book_ts_' . md5( http_build_query( array( $bookable_product->get_id(), $resource_id, $from, $to ) ) );
     203        $available_slots              = get_transient( $transient_name );
     204        $booking_slots_transient_keys = array_filter( (array) get_transient( 'booking_slots_transient_keys' ) );
     205
     206        if ( ! isset( $booking_slots_transient_keys[ $bookable_product->get_id() ] ) ) {
     207            $booking_slots_transient_keys[ $bookable_product->get_id() ] = array();
     208        }
     209
     210        $booking_slots_transient_keys[ $bookable_product->get_id() ][] = $transient_name;
     211
     212        // Give array of keys a long ttl because if it expires we won't be able to flush the keys when needed.
     213        // We can't use 0 to never expire because then WordPress will autoload the option on every page.
     214        set_transient( 'booking_slots_transient_keys', $booking_slots_transient_keys, YEAR_IN_SECONDS );
     215
     216        if ( false === $available_slots ) {
     217            if ( empty( $intervals ) ) {
     218                $interval         = $bookable_product->get_min_duration();
     219                $intervals        = array( $interval, 1 );
     220            }
     221
     222            list( $interval, $base_interval ) = $intervals;
     223
     224            $existing_bookings = WC_Bookings_Controller::get_all_existing_bookings( $bookable_product, $from, $to );
     225
     226            $booking_resource = $resource_id ? $bookable_product->get_resource( $resource_id ) : null;
     227            $available_slots  = array();
     228            $has_qty          = ! is_null( $booking_resource ) ? $booking_resource->has_qty() : false;
     229            $has_resources    = $bookable_product->has_resources();
     230
     231            foreach ( $blocks as $block ) {
     232                $check_in  = WC_Product_Accommodation_Booking::get_check_times( 'in' );
     233                $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out' );
     234                // Blocks for accommodation products are initially calculated as days but the actuall time blocks are shifted by check in and checkout times.
     235                $block_start_time = strtotime( "{$check_in}", $block );
     236                $block_end_time =  strtotime( "{$check_out}", strtotime( "+1 days", $block ) );
     237                $resources = array();
     238
     239                // Figure out how much qty have, either based on combined resource quantity,
     240                // single resource, or just product.
     241                if ( $has_resources && ( ! is_a( $booking_resource, 'WC_Product_Booking_Resource' ) || ! $has_qty ) ) {
     242                    $available_qty = 0;
     243
     244                    foreach ( $bookable_product->get_resources() as $resource ) {
     245
     246                        if ( ! $bookable_product->check_availability_rules_against_time( $block_start_time, $block_end_time, $block, $resource->get_id() ) ) {
     247                            continue;
     248                        }
     249
     250                        $qty = $resource->get_qty();
     251                        $available_qty += $qty;
     252                        $resources[ $resource->get_id() ] = $qty;
     253                    }
     254                } elseif ( $has_resources && $has_qty ) {
     255                    // Only include if it is available for this selection. We set this block to be bookable by default, unless some of the rules apply.
     256                    if ( ! $bookable_product->check_availability_rules_against_time( $block_start_time, $block_end_time, $booking_resource->get_id() ) ) {
     257                        continue;
     258                    }
     259
     260                    $qty = $booking_resource->get_qty();
     261                    $available_qty = $qty;
     262                    $resources[ $booking_resource->get_id() ] = $qty;
     263                } else {
     264                    $available_qty = $bookable_product->get_qty();
     265                    $resources[0] = $bookable_product->get_qty();
     266                }
     267
     268                $qty_booked_in_block = 0;
     269
     270                foreach ( $existing_bookings as $existing_booking ) {
     271                    if ( $existing_booking->is_within_block( $block_start_time, $block_end_time ) ) {
     272                        $qty_to_add = $bookable_product->has_person_qty_multiplier() ? max( 1, array_sum( $existing_booking->get_persons() ) ) : 1;
     273                        if ( $has_resources ) {
     274                            if ( $existing_booking->get_resource_id() === absint( $resource_id ) ) {
     275                                // Include the quantity to subtract if an existing booking matches the selected resource id
     276                                $qty_booked_in_block += $qty_to_add;
     277                                $resources[ $resource_id ] = ( isset( $resources[ $resource_id ] ) ? $resources[ $resource_id ] : 0 ) - $qty_to_add;
     278                            } elseif ( ( is_null( $booking_resource ) || ! $has_qty ) && $existing_booking->get_resource() ) {
     279                                // Include the quantity to subtract if the resource is auto selected (null/resource id empty)
     280                                // but the existing booking includes a resource
     281                                $qty_booked_in_block += $qty_to_add;
     282                                $resources[ $existing_booking->get_resource_id() ] = ( isset( $resources[ $existing_booking->get_resource_id() ] ) ? $resources[ $existing_booking->get_resource_id() ] : 0 ) - $qty_to_add;
     283                            }
     284                        } else {
     285                            $qty_booked_in_block += $qty_to_add;
     286                            $resources[0] = ( isset( $resources[0] ) ? $resources[0] : 0 ) - $qty_to_add;
     287                        }
     288                    }
     289                }
     290
     291                $available_slots[ $block ] = array(
     292                    'booked'    => $qty_booked_in_block,
     293                    'available' => $available_qty - $qty_booked_in_block,
     294                    'resources' => $resources,
     295                );
     296            }
     297
     298
     299            set_transient( $transient_name, $available_slots );
     300        }
     301
     302        return $available_slots;
     303    }
     304
     305
     306    /**
    190307     * Get an array of blocks within in a specified date range
    191308     *
  • woocommerce-accommodation-bookings/trunk/readme.txt

    r2100652 r2103605  
    44Requires at least: 4.1
    55Tested up to: 5.0
    6 Stable tag: 1.1.7
     6Stable tag: 1.1.8
    77License: GNU General Public License v3.0
    88License URI: http://www.gnu.org/licenses/gpl-3.0.html
     
    3737== Changelog ==
    3838
     39= 1.1.8 - 2019-06-10 =
     40* Fix - Adds accommodation bookings support for WooCommerce Bookings Availability extension
     41
    3942= 1.1.7 - 2019-06-03 =
    4043* Fix - Mismatch function declaration causing PHP warnings.
  • woocommerce-accommodation-bookings/trunk/woocommerce-accommodation-bookings.php

    r2100652 r2103605  
    44 * Plugin URI: https://woocommerce.com/products/woocommerce-accommodation-bookings/
    55 * Description: An accommodations add-on for the WooCommerce Bookings extension.
    6  * Version: 1.1.7
     6 * Version: 1.1.8
    77 * Author: WooCommerce
    88 * Author URI: https://woocommerce.com
     
    2222
    2323require_once( 'includes/class-wc-accommodation-bookings-plugin.php' );
    24 $plugin = new WC_Accommodation_Bookings_Plugin( __FILE__, '1.1.7' );
     24$plugin = new WC_Accommodation_Bookings_Plugin( __FILE__, '1.1.8' );
    2525$plugin->run();
Note: See TracChangeset for help on using the changeset viewer.