* @return void */ add_action('wp_ajax_wprsrv_decline_reservation', function () { if (!isset($_POST) || !isset($_POST['post_id'])) { echo 1; exit; } $post = $_POST; $postId = $post['post_id']; if (!$postId) { echo 1; exit; } $reservation = new Reservation($postId); $reservation->decline(); $reservation->getReservable()->flushCache(); echo 0; exit; }); /** * AJAX * Flush cache for a reservable. * * @since 0.1.0 * @return void */ add_action('wp_ajax_wprsrv_flush_reservable_cache', function () { if (!isset($_POST) || !isset($_POST['post_id'])) { echo 1; exit; }
/** * Actions to trigger and make when reservation statuses change. * * @since 0.1.0 * @access protected * @return void */ protected function postStatusActions() { $accepted = ['reservation_accepted', 'reservation_pending', 'reservation_declined', 'trash']; // Force pending status from other statuses than reservation statuses. add_action('transition_post_status', function ($new, $old, $post) use($accepted) { if ($new === 'trash' && $post->post_type === $this->postTypeSlug) { // Flush cache so we get updated data on a removed reservation. $reservation = new ReservationObj($post); try { $reservation->getReservable()->flushCache(); } catch (\Exception $e) { \Wprsrv\wprsrv()->logger->warning('Error when trashing a reservation and flushing reservable caches: {msg}', ['msg' => $e->getMessage()]); } } if ($post->post_type !== $this->postTypeSlug || in_array($new, $accepted)) { return; } $postData = ['ID' => $post->ID, 'post_status' => 'reservation_pending']; // This will fire the currently running hook again. $reservationId = wp_update_post($postData); }, 25, 3); add_action('wprsrv/reservation/created', [$this, 'newPendingReservation'], 25, 2); add_action('wprsrv/reservation/accepted', [$this, 'newAcceptedReservation'], 25, 2); add_action('wprsrv/reservation/declined', [$this, 'newDeclinedReservation'], 25, 2); }