/** * Fired on WP plugin deactivation hook. No output allowed. * * @todo Refactor plugin cache flushing to a separate method, or maybe even a * class. * @static * @since 0.1.0 * @return void */ public static function deactivate() { // Begin output buffering. ob_start(); $self = new self(false); $self->logger->notice('Deactivating wprsrv plugin...'); $reservables = new \WP_Query(['post_type' => 'reservable', 'post_status' => 'all', 'posts_per_page' => -1, 'no_found_rows' => 1, 'nopaging' => 1, 'fields' => 'ids']); if ($reservables->have_posts()) { foreach ($reservables->posts as $post_id) { $reservable = new Reservable($post_id); $reservable->flushCache(); } } // Flush rewrite rules. flush_rewrite_rules(); // Get rid of buffer. ob_end_clean(); }
* @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; } $post = $_POST; $postId = $post['post_id']; if (!$postId) { echo 1; exit; } $reservable = new Reservable($postId); $reservable->flushCache(); echo 0; exit; }); /** * AJAX * Get a reservation calendar for a certain month and reservable. * * @since 0.4.0 * @return void */ add_action('wp_ajax_wprsrv_get_reservation_calendar_view', function () { if (!isset($_POST) || !isset($_POST['reservable_id'])) { echo 1; exit; }
/** * Custom saving logic for reservables. * * @see self::addHooks() * @see:wphook save_post * @since 0.1.0 * @todo Make this method simpler and extract some parts elsewhere. * * @param Integer $post_id Post ID being saved. * @param \WP_Post $post Post being saved. * @param Boolean $update Is this an update? * * @return void */ public function saveReservable($post_id, $post, $update) { if (wp_is_post_revision($post_id)) { return; } if (get_post_type($post_id) !== 'reservable') { return; } $postData = $_POST; if (!isset($postData['wprsrv'])) { return; } $wprsrvData = $postData['wprsrv']; $reservable = new ReservableObj($post); $keysAvailable = ['reservable_active', 'reservable_singleday', 'reservable_disabled_days', 'reservable_disabled_weekdays', 'reservable_loggedin_only', 'reservable_overlapping_days']; // Set null values if not given on update. foreach ($keysAvailable as $key) { if (array_key_exists($key, $wprsrvData)) { continue; } switch ($key) { case 'reservable_active': $reservable->setActive(false); break; case 'reservable_singleday': $reservable->setSingleDay(false); break; case 'reservable_disabled_days': $reservable->setDisabledDaysAdminData(false); break; case 'reservable_disabled_weekdays': $reservable->setDisabledWeekdaysData(false); break; case 'reservable_loggedin_only': $reservable->setLoginRequired(false); break; case 'reservable_overlapping_days': $reservable->setAllowOverlappingReservations(false); break; } } // Update values with given POST. foreach ($wprsrvData as $key => $value) { switch ($key) { case 'reservable_active': $reservable->setActive($value === 'on' ? true : false); break; case 'reservable_singleday': $reservable->setSingleDay($value === 'on' ? true : false); break; case 'reservable_disabled_days': $disabled = []; for ($i = 0; $i < count($value['start']); $i++) { if (empty($value['start'][$i])) { continue; } $range = ['start' => $value['start'][$i], 'end' => $value['end'][$i], 'reservation_id' => 0]; $disabled[] = $range; } $reservable->setDisabledDaysAdminData($disabled); break; case 'reservable_disabled_weekdays': $wdays = array_map(function ($item) { return preg_replace('%[^a-zöäå]%', '', $item); }, $value); $reservable->setDisabledWeekdaysData($wdays); break; case 'reservable_loggedin_only': $reservable->setLoginRequired($value === 'on' ? true : false); break; case 'reservable_overlapping_days': $reservable->setAllowOverlappingReservations($value === 'on' ? true : false); break; } } // Flush caches on save. if ($update) { $reservable->flushCache(); } /** * Hook fired when a reservable is saved to the database. * * Allows doing additional saving logic for reservables. * * @since 0.1.0 * * @param Integer $id The reservable post ID. * @param ReservableObj $reservable The reservable object. * @param Boolean $update Is this an update or a new reservable? */ do_action('wprsrv/reservable/save', $reservable->ID, $reservable, $update); }