/** * Clear transients. * * @since 3.0.0 */ public function clear_cache() { $id = isset($_POST['id']) ? is_array($_POST['id']) ? array_map('intval', $_POST['id']) : intval($_POST['id']) : ''; if (!empty($id)) { simcal_delete_feed_transients($id); } }
/** * Validate and save the meta box fields. * * @since 3.0.0 * * @param int $post_id * @param \WP_Post $post * * @return void */ public static function save($post_id, $post) { /* ====================== * * Calendar type and view * * ====================== */ // Unlink existing terms for feed type and calendar type. wp_delete_object_term_relationships($post_id, array('calendar_feed', 'calendar_type')); // Set the feed type as term. $feed_type = isset($_POST['_feed_type']) ? sanitize_title(stripslashes($_POST['_feed_type'])) : apply_filters('simcal_default_feed_type', 'google'); wp_set_object_terms($post_id, $feed_type, 'calendar_feed'); // Set the calendar type as a term. $calendar_type = isset($_POST['_calendar_type']) ? sanitize_title(stripslashes($_POST['_calendar_type'])) : apply_filters('simcal_default_calendar_type', 'default-calendar'); wp_set_object_terms($post_id, $calendar_type, 'calendar_type'); // Set the calendar type view as post meta. $calendar_view = isset($_POST['_calendar_view']) ? $_POST['_calendar_view'] : ''; if ($calendar_view && is_array($calendar_view)) { $views = array_map('sanitize_title', $calendar_view); update_post_meta($post_id, '_calendar_view', $views); } /* ===================== * * Events settings panel * * ===================== */ // Calendar opening. $calendar_begins = isset($_POST['_calendar_begins']) ? sanitize_key($_POST['_calendar_begins']) : 'this_month'; update_post_meta($post_id, '_calendar_begins', $calendar_begins); $calendar_begins_nth = isset($_POST['_calendar_begins_nth']) ? absint($_POST['_calendar_begins_nth']) : 2; update_post_meta($post_id, '_calendar_begins_nth', $calendar_begins_nth); $calendar_begins_custom_date = isset($_POST['_calendar_begins_custom_date']) ? sanitize_title($_POST['_calendar_begins_custom_date']) : ''; update_post_meta($post_id, '_calendar_begins_custom_date', $calendar_begins_custom_date); // Feed earliest events date. $earliest_events = isset($_POST['_feed_earliest_event_date']) ? sanitize_key($_POST['_feed_earliest_event_date']) : ''; update_post_meta($post_id, '_feed_earliest_event_date', $earliest_events); $earliest_events_range = isset($_POST['_feed_earliest_event_date_range']) ? max(absint($_POST['_feed_earliest_event_date_range']), 1) : 1; update_post_meta($post_id, '_feed_earliest_event_date_range', $earliest_events_range); // Feed latest events date. $latest_events = isset($_POST['_feed_latest_event_date']) ? sanitize_key($_POST['_feed_latest_event_date']) : ''; update_post_meta($post_id, '_feed_latest_event_date', $latest_events); $latest_events_range = isset($_POST['_feed_latest_event_date_range']) ? max(absint($_POST['_feed_latest_event_date_range']), 1) : 1; update_post_meta($post_id, '_feed_latest_event_date_range', $latest_events_range); /* ======================= * * Calendar settings panel * * ======================= */ // Static calendar. $static = isset($_POST['_calendar_is_static']) ? 'yes' : 'no'; update_post_meta($post_id, '_calendar_is_static', $static); // No events message. $message = isset($_POST['_no_events_message']) ? wp_kses_post($_POST['_no_events_message']) : ''; update_post_meta($post_id, '_no_events_message', $message); /* ======================= * * Advanced settings panel * * ======================= */ // Timezone. $feed_timezone_setting = isset($_POST['_feed_timezone_setting']) ? sanitize_key($_POST['_feed_timezone_setting']) : 'use_calendar'; update_post_meta($post_id, '_feed_timezone_setting', $feed_timezone_setting); $default_timezone = simcal_get_wp_timezone(); $feed_timezone = $default_timezone ? $default_timezone : 'UTC'; $feed_timezone = isset($_POST['_feed_timezone']) ? sanitize_text_field($_POST['_feed_timezone']) : $feed_timezone; update_post_meta($post_id, '_feed_timezone', $feed_timezone); // Date format. $date_format_setting = isset($_POST['_calendar_date_format_setting']) ? sanitize_key($_POST['_calendar_date_format_setting']) : 'use_site'; update_post_meta($post_id, '_calendar_date_format_setting', $date_format_setting); $date_format = isset($_POST['_calendar_date_format']) ? sanitize_text_field(trim($_POST['_calendar_date_format'])) : get_option('date_format'); update_post_meta($post_id, '_calendar_date_format', $date_format); $date_format_php = isset($_POST['_calendar_date_format_php']) ? sanitize_text_field(trim($_POST['_calendar_date_format_php'])) : get_option('date_format'); update_post_meta($post_id, '_calendar_date_format_php', $date_format_php); // Time format. $time_format_setting = isset($_POST['_calendar_time_format_setting']) ? sanitize_key($_POST['_calendar_time_format_setting']) : 'use_site'; update_post_meta($post_id, '_calendar_time_format_setting', $time_format_setting); $time_format = isset($_POST['_calendar_time_format']) ? sanitize_text_field(trim($_POST['_calendar_time_format'])) : get_option('time_format'); update_post_meta($post_id, '_calendar_time_format', $time_format); $time_format_php = isset($_POST['_calendar_time_format_php']) ? sanitize_text_field(trim($_POST['_calendar_time_format_php'])) : get_option('time_format'); update_post_meta($post_id, '_calendar_time_format_php', $time_format_php); // Date-time separator. $datetime_separator = isset($_POST['_calendar_datetime_separator']) ? sanitize_text_field($_POST['_calendar_datetime_separator']) : ' '; update_post_meta($post_id, '_calendar_datetime_separator', $datetime_separator); // Week start. $week_start_setting = isset($_POST['_calendar_week_starts_on_setting']) ? sanitize_key($_POST['_calendar_week_starts_on_setting']) : 'use_site'; update_post_meta($post_id, '_calendar_week_starts_on_setting', $week_start_setting); $week_start = isset($_POST['_calendar_week_starts_on']) ? intval($_POST['_calendar_week_starts_on']) : get_option('start_of_week'); update_post_meta($post_id, '_calendar_week_starts_on', $week_start); // Cache interval. $cache = 7200; if (isset($_POST['_feed_cache_user_amount']) && isset($_POST['_feed_cache_user_unit'])) { $amount = is_numeric($_POST['_feed_cache_user_amount']) ? absint($_POST['_feed_cache_user_amount']) : 1; $unit = is_numeric($_POST['_feed_cache_user_unit']) ? absint($_POST['_feed_cache_user_unit']) : 3600; update_post_meta($post_id, '_feed_cache_user_amount', $amount); update_post_meta($post_id, '_feed_cache_user_unit', $unit); $cache = $amount * $unit; } update_post_meta($post_id, '_feed_cache', $cache); /* ============= * * Miscellaneous * * ============= */ // Update version. update_post_meta($post_id, '_calendar_version', SIMPLE_CALENDAR_VERSION); // Action hook. do_action('simcal_process_settings_meta', $post_id); // Clear cache. simcal_delete_feed_transients($post_id); }
/** * Upon posts deletion. * * Delete transients and notices when a calendar is deleted. * * @since 3.0.0 * * @param $post_id */ public function upon_deletion($post_id) { $post_type = get_post_type($post_id); if ('calendar' == $post_type) { $notices = get_option('simple-calendar_admin_notices', array()); if (!empty($notices) && isset($notices['calendar_' . strval($post_id)])) { unset($notices['calendar_' . strval($post_id)]); update_option('simple-calendar_admin_notices', $notices); } simcal_delete_feed_transients($post_id); } }
/** * Update to current version. * * Runs all the update scripts through version steps. * * @since 3.0.0 */ public function run_updates() { do_action('simcal_before_update', $this->installed_ver); if (!is_null($this->installed_ver)) { if (version_compare($this->installed_ver, $this->new_ver) === -1) { $post_type = version_compare($this->installed_ver, '3.0.0') === -1 ? 'gce_feed' : 'calendar'; $this->posts = $this->get_posts($post_type); foreach ($this->update_path as $update_to) { if (version_compare($this->installed_ver, $update_to, '<')) { $this->update($update_to); } } } simcal_delete_feed_transients(); } else { new Post_Types(); flush_rewrite_rules(); } do_action('simcal_updated', $this->new_ver); // Redirect to a welcome page if new install or major update. if (is_null($this->installed_ver)) { set_transient('_simple-calendar_activation_redirect', 'fresh', 60); } else { $major_new = substr($this->new_ver, 0, strrpos($this->new_ver, '.')); $major_old = substr($this->installed_ver, 0, strrpos($this->installed_ver, '.')); if (version_compare($major_new, $major_old, '>')) { set_transient('_simple-calendar_activation_redirect', 'update', 60); } elseif ($major_old == $major_new) { $version = explode('.', $this->new_ver); end($version); if (0 === intval(current($version))) { set_transient('_simple-calendar_activation_redirect', 'update', 60); } } } $this->admin_redirects(); update_option('simple-calendar_version', $this->new_ver); }
/** * Get events from multiple calendars. * * @since 3.0.0 * * @return array */ public function get_events() { $ids = $this->calendars_ids; $events = get_transient('_simple-calendar_feed_id_' . strval($this->post_id) . '_' . $this->type); if (empty($events) && !empty($ids) && is_array($ids)) { $events = array(); foreach ($ids as $cal_id) { $calendar = simcal_get_calendar(intval($cal_id)); simcal_delete_feed_transients($cal_id); if ($calendar instanceof Calendar) { // Sometimes the calendars might have events at the same time from different calendars // When merging the arrays together some of the events will be lost because the keys are the same and one will overwrite the other // This snippet checks if the key already exists in the master events array and if it does it subtracts 1 from it to make the key unique and then unsets the original key. foreach ($calendar->events as $k => $v) { $calendar->events[$this->update_array_timestamp($events, $k)] = $v; } $events = is_array($calendar->events) ? $events + $calendar->events : $events; } } if (!empty($events)) { // Trim events to set the earliest one as specified in feed settings. $earliest_event = intval($this->time_min); if ($earliest_event > 0) { $events = $this->array_filter_key($events, array($this, 'filter_events_before')); } // Trim events to set the latest one as specified in feed settings. $latest_event = intval($this->time_max); if ($latest_event > 0) { $events = $this->array_filter_key($events, array($this, 'filter_events_after')); } set_transient('_simple-calendar_feed_id_' . strval($this->post_id) . '_' . $this->type, $events, absint($this->cache)); } } // Sort events by start time before returning uasort($events, array($this, 'sort_by_start_time')); return $events; }
/** * Bulk actions. * * @since 3.0.0 */ public function bulk_actions() { // Clear an individual feed cache. // @todo Convert the clear cache request to ajax. if (isset($_REQUEST['clear_cache'])) { $id = intval($_REQUEST['clear_cache']); if ($id > 0) { simcal_delete_feed_transients($id); } wp_redirect(remove_query_arg('clear_cache')); } // Duplicate a feed post type. if (isset($_REQUEST['duplicate_feed'])) { $id = intval($_REQUEST['duplicate_feed']); if ($id > 0) { $this->duplicate_feed($id); } wp_redirect(remove_query_arg('duplicate_feed')); } $bulk_actions = new Bulk_Actions('calendar'); $bulk_actions->register_bulk_action(array('menu_text' => __('Clear cache', 'google-calendar-events'), 'action_name' => 'clear_calendars_cache', 'callback' => function ($post_ids) { simcal_delete_feed_transients($post_ids); }, 'admin_notice' => __('Cache cleared.', 'google-calendar-events'))); $bulk_actions->init(); }