is_main_query() && $query->is_post_type_archive( 'am_event' ) ) { $this_year = get_query_var('year', gmdate('Y', current_time('timestamp'))); $this_month = get_query_var('month', gmdate('m', current_time('timestamp'))); $this_day = get_query_var('day', gmdate('d', current_time('timestamp'))); if (is_year()) { // Yearly archive page $start = "$this_year-01-01 00:00:00"; $end = "$this_year-12-31 23:59:59"; $query->set( 'meta_query', am_get_archive_meta_query($start, $end) ); am_pre_modify_query($query); } else if (is_month()) { // Monthly archive page $start = "$this_year-$this_month-01 00:00:00"; $end = "$this_year-$this_month-31 23:59:59"; $query->set( 'meta_query', am_get_archive_meta_query($start, $end) ); am_pre_modify_query($query); } else if (is_day()) { // Daily archive page $start = "$this_year-$this_month-$this_day 00:00:00"; $end = "$this_year-$this_month-$this_day 23:59:59"; $query->set( 'meta_query', am_get_archive_meta_query($start, $end) ); am_pre_modify_query($query); } } } function am_pre_modify_query($query) { $query->set( 'post_type', 'am_event' ); $query->set( 'meta_key', 'am_startdate' ); $query->set( 'orderby', 'meta_value' ); $query->set( 'order', 'ASC' ); $query->set( 'year', null); $query->set( 'monthnum', null); $query->set( 'day', null); } function am_get_archive_meta_query($start_timestamp, $end_timestamp) { return array( 'relation' => 'AND', array( 'key' => 'am_enddate', 'value' => $start_timestamp, 'compare' => '>=', ), array( 'key' => 'am_startdate', 'value' => $end_timestamp, 'compare' => '<=', ), ); } function am_get_default_date_format() { return 'Y-m-d H:i:s'; } function am_register_widgets() { register_widget('AM_Upcoming_Events_Widget'); //register_widget('AM_Event_Calendar_Widget'); } function am_register_settings() { // whitelist options register_setting( 'am-events-settings-group', 'am_timepicker_minutestep' ); register_setting( 'am-events-settings-group', 'am_rewrite_slug' ); } function am_plugin_menu() { add_options_page( 'AM Events settings', 'AM Events', 'manage_options', 'am-events-settings', 'am_plugin_settings' ); } function am_plugin_settings() { if ( !current_user_can( 'manage_options' ) ) { wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); } ?>
post_type; $post_type_object = get_post_type_object($post_type); $can_publish = current_user_can($post_type_object->cap->publish_posts); $recurring_count = am_get_recurring_count($post->ID); ?>
' . sprintf( __('%d recurrent posts moved to the Trash.', 'am-events'), $post_count) . ' ' . $link . '
' ); } } } } /* * **************************************************************************** * =SAVING * *************************************************************************** */ /** * Save event meta and create recurring events. * @return type */ function am_save_event($post_id) { // Quick edit $_POST += array("am_quickedit_nonce" => ''); if ( wp_verify_nonce( $_POST["am_quickedit_nonce"], plugin_basename( __FILE__ ) ) ) { // verify if this is an auto save routine. If it is, our form has not been submitted, so we don't want to do anything if ( $_POST['post_type'] !== 'am_event' ) { return; } if ( !current_user_can( 'edit_post', $post_id ) ) { return; } am_update_post_meta_date($post_id, 'am_startdate'); am_update_post_meta_date($post_id, 'am_enddate'); } // Normal edit else { // Remove save_post action to avoid infinite loop when calling wp_insert_posts remove_action('save_post', 'am_save_event'); if (!isset($_POST['post_ID'])) return; $post_id = $_POST['post_ID']; $orig_post = get_post($post_id); if ($_POST && get_post_type($orig_post) === 'am_event') { // Check if 'Recurrent Event' has been checked if (isset($_POST['am_recurrent'])) { $recurrent = $_POST['am_recurrent']; if ($recurrent === 'yes') { // If so, create the events $recurrent_amount = $_POST['am_recurrent_amount']; $recurrence_type = $_POST['am_recurrence_type']; // Limit number of created events between 2 and 99 if ($recurrent_amount < 2 || $recurrent_amount > 99) { return; } //change recurrence id $recurrence_id = am_create_recurrence_id($post_id); update_post_meta($post_id, 'am_recurrence_id', $recurrence_id); $startdate = get_post_meta($post_id, 'am_startdate', true); $enddate = get_post_meta($post_id, 'am_enddate', true); $start = DateTime::createFromFormat(am_get_default_date_format(), $startdate); $end = DateTime::createFromFormat(am_get_default_date_format(), $enddate); for ($i = 1; $i < $recurrent_amount; $i++) { $new_post = array( 'post_title' => $orig_post->post_title, 'post_content' => $orig_post->post_content, 'post_status' => $orig_post->post_status, 'post_date' => $orig_post->post_date, 'post_author' => $orig_post->post_author, 'post_type' => $orig_post->post_type, 'post_category' => $orig_post->post_category, 'post_excerpt' => $orig_post->post_excerpt, 'comment_status' => $orig_post->comment_status, 'ping_status' => $orig_post->ping_status, 'post_password' => $orig_post->post_password, ); $new_post_id = wp_insert_post($new_post); am_copy_replace_tags($post_id, $new_post_id); set_post_thumbnail($new_post_id, get_post_thumbnail_id($post_id)); switch ($recurrence_type) { case 'am_weekly': $start->modify('+7 days'); $end->modify('+7 days'); break; case 'am_biweekly': $start->modify('+14 days'); $end->modify('+14 days'); break; default: return; } update_post_meta($new_post_id, 'am_startdate', $start->format(am_get_default_date_format())); update_post_meta($new_post_id, 'am_enddate', $end->format(am_get_default_date_format())); update_post_meta($new_post_id, 'am_recurrence_id', $recurrence_id); am_copy_replace_terms($post_id, $new_post_id, 'am_event_categories'); am_copy_replace_terms($post_id, $new_post_id, 'am_venues'); do_action( 'am_copy_event', $post_id, $new_post_id ); } // Notify user when recurrent events have been created. am_add_admin_message( '
' . sprintf(__('Created %d recurrent events.', 'am-events') . '
', $recurrent_amount) ); } } // Determine if the specified post is not a revision or auto-save if (!( wp_is_post_revision($post_id) && wp_is_post_autosave($post_id) )) { // Check if 'Update All' has been clicked if (isset($_POST['submit_all']) && $_POST['submit_all'] === 'yes') { // Update all recurring events $recurrence_id = get_post_meta($post_id, 'am_recurrence_id', true); $args = array( 'post_type' => 'am_event', 'post_status' => 'any', 'post_count' => -1, 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => 'am_recurrence_id', 'value' => $recurrence_id, 'compare' => "=", ), ), 'post__not_in' => array($post_id), //exclude current event ); $the_query = new WP_Query( $args ); $post_count = $the_query->post_count; while ($the_query->have_posts()) { $the_query->the_post(); $id = get_the_ID(); $recurrent_post = array( 'ID' => $id, 'post_title' => $orig_post->post_title, 'post_content' => $orig_post->post_content, 'post_status' => $orig_post->post_status, 'post_author' => $orig_post->post_author, 'post_excerpt' => $orig_post->post_excerpt, 'comment_status' => $orig_post->comment_status, 'ping_status' => $orig_post->ping_status, 'post_password' => $orig_post->post_password, ); remove_action('save_post', 'am_save_custom_meta'); wp_update_post( $recurrent_post ); add_action('save_post', 'am_save_custom_meta'); am_copy_replace_tags($post_id, $id); set_post_thumbnail($id, get_post_thumbnail_id($post_id)); am_copy_replace_terms($post_id, $id, 'am_event_categories'); am_copy_replace_terms($post_id, $id, 'am_venues'); do_action( 'am_copy_event', $post_id, $id ); } // Notify user when recurrent events have been created. am_add_admin_message( '' . sprintf(__('%d recurring events updated.', 'am-events') . '
', $post_count) ); } } } } } function am_update_post_meta_date($post_id, $meta_key) { if ( isset( $_REQUEST[$meta_key] ) ) { $enddate = strtotime($_REQUEST[$meta_key] ); if ($enddate) update_post_meta( $post_id, $meta_key, date( am_get_default_date_format(), $enddate)); } } /** * Copies and replaces terms from one post to another. */ function am_copy_replace_terms($from, $to, $taxonomy) { wp_delete_object_term_relationships( $to, $taxonomy ); $terms = wp_get_post_terms($from, $taxonomy); foreach ($terms as $term) { wp_set_post_terms($to, $term->term_id, $taxonomy, true); } } /** * Copies and replaces all tags from one post to another. */ function am_copy_replace_tags($from, $to) { wp_set_post_tags($to, wp_get_post_tags($from, array('fields' => 'ids')), false); } /** * Creates an id from the event's slug to group recurrent events for easy deletion */ function am_create_recurrence_id($post_id) { $slug = get_post($post_id)->post_name; $count = 0; $id = ''; do { $id = $count === 0 ? $slug : $slug . '-' . $count; $args = array( 'meta_query' => array( array( 'key' => 'am_recurrence_id', 'value' => $id, 'compare' => "=", ), ), ); $the_query = new WP_Query( $args ); } while ($the_query->have_posts()); return $id; } /* * **************************************************************************** * =COLUMNS * *************************************************************************** */ /** * Add columns to event list in administration * @param type $columns * @return type */ function am_add_event_columns($columns) { return array_merge($columns, array('am_startdate' => __('Start Date', 'am-events'), 'am_enddate' => __('End Date', 'am-events'))); } function am_custom_event_column($column) { global $post; $post_id = $post->ID; switch ($column) { case 'am_startdate': echo date(get_option('date_format') . ' ' . get_option('time_format'), strtotime(get_post_meta($post_id, 'am_startdate', true))); break; case 'am_enddate': echo date(get_option('date_format') . ' ' . get_option('time_format'), strtotime(get_post_meta($post_id, 'am_enddate', true))); break; } } /** * Register the column as sortable */ function am_register_sortable_columns($columns) { $columns['am_startdate'] = 'am_startdate'; return $columns; } function am_edit_event_load() { add_filter('request', 'am_sort_events'); } /** * Used to sort the events in the administration. */ function am_sort_events($vars) { /* Check if we're viewing the 'am_event' post type. */ if (isset($vars['post_type']) && 'am_event' === $vars['post_type']) { /* Check if 'orderby' is set to 'am_startdate'. */ if (isset($vars['orderby']) && 'am_startdate' === $vars['orderby']) { /* Merge the query vars with our custom variables. */ $vars = array_merge( $vars, array( 'meta_key' => 'am_startdate', 'orderby' => 'meta_value' ) ); } } return $vars; } /* * **************************************************************************** * =LANGUAGE FILES * *************************************************************************** */ /** * Loads the language files */ function am_load_language_files() { load_plugin_textdomain('am-events', false, dirname(plugin_basename(__FILE__)) . '/languages/'); } /* * **************************************************************************** * =CPT, CUSTOM POST TYPE * *************************************************************************** */ /** * Registers new post type 'am_event' */ function am_register_post_type() { $labels = array( 'name' => __('Events', 'am-events'), 'singular_name' => __('Event', 'am-events'), 'menu_name' => __('Events', 'am-events'), 'all_items' => __('All Events', 'am-events'), 'add_new' => __('Add New Event', 'am-events'), 'add_new_item' => __('Event', 'am-events'), 'edit_item' => __('Edit Event', 'am-events'), 'new_item' => __('New Event', 'am-events'), 'view_item' => __('View Events', 'am-events'), 'search_items' => __('Search Events', 'am-events'), 'not_found' => __('No Events Found', 'am-events'), 'not_found_in_trash' => __('No Events Found', 'am-events'), ); $args = array( 'label' => __('Event', 'am-events'), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, //Below Posts 'menu_icon' => 'dashicons-calendar', 'has_archive' => true, 'category_name' => 'etusivu', 'labels' => $labels, 'description' => __('Type for events', 'am-events'), 'supports' => array('title', 'editor', 'thumbnail', 'excerpt'), 'taxonomies' => array('am_event_category', 'am_venue', 'post_tag'), 'rewrite' => array( 'slug' => get_option('am_rewrite_slug', 'am_event') ), ); register_post_type('am_event', $args); } /* * Register custom taxonomy 'am_venues' */ function am_register_taxonomy_venues() { $labels = array( 'name' => __('Venues', 'am-events'), 'singular_name' => __('Venue', 'am-events'), 'search_items' => __('Search Venues', 'am-events'), 'popular_items' => __('Popular Venues', 'am-events'), 'all_items' => __('All Venues', 'am-events'), 'parent_item' => __('Parent Venue', 'am-events'), 'parent_item_colon' => __('Parent Venue:', 'am-events'), 'edit_item' => __('Edit Venue', 'am-events'), 'update_item' => __('Update Venue', 'am-events'), 'add_new_item' => __('Add New Venue', 'am-events'), 'new_item_name' => __('New Venue', 'am-events'), 'separate_items_with_commas' => __('Separate venues with commas', 'am-events'), 'add_or_remove_items' => __('Add or remove venues', 'am-events'), 'choose_from_most_used' => __('Choose from the most used venues', 'am-events'), 'menu_name' => __('Venues', 'am-events'), ); $args = array( 'labels' => $labels, 'public' => true, 'show_in_nav_menus' => true, 'show_ui' => true, 'show_tagcloud' => true, 'show_admin_column' => false, 'hierarchical' => true, 'rewrite' => true, 'query_var' => true ); register_taxonomy('am_venues', array('am_event'), $args); } /** * Registers custom taxonomy 'am_event_categories' */ function am_register_taxonomy_event_categories() { $labels = array( 'name' => __('Event Categories', 'am-events'), 'singular_name' => __('Event Category', 'am-events'), 'search_items' => __('Search Event Categories', 'am-events'), 'popular_items' => __('Popular Event Categories', 'am-events'), 'all_items' => __('All Event Categories', 'am-events'), 'parent_item' => __('Parent Event Category', 'am-events'), 'parent_item_colon' => __('Parent Event Category:', 'am-events'), 'edit_item' => __('Edit Event Category', 'am-events'), 'update_item' => __('Update Event Category', 'am-events'), 'add_new_item' => __('Add New Event Category', 'am-events'), 'new_item_name' => __('New Event Category', 'am-events'), 'separate_items_with_commas' => __('Separate event categories with commas', 'am-events'), 'add_or_remove_items' => __('Add or remove event categories', 'am-events'), 'choose_from_most_used' => __('Choose from the most used event categories', 'am-events'), 'menu_name' => __('Event Categories', 'am-events'), ); $args = array( 'labels' => $labels, 'public' => true, 'show_in_nav_menus' => true, 'show_ui' => true, 'show_tagcloud' => true, 'show_admin_column' => false, 'hierarchical' => true, 'rewrite' => true, 'query_var' => true ); register_taxonomy('am_event_categories', array('am_event'), $args); } function am_event_updated_messages($messages) { global $post, $post_ID; $messages['am_event'] = array( 0 => '', // Unused. Messages start at index 1. 1 => sprintf(__('Event updated. View event', 'am-events'), esc_url(get_permalink($post_ID))), 2 => __('Custom field updated.', 'am-events'), 3 => __('Custom field deleted.', 'am-events'), 4 => __('Event updated.', 'am-events'), /* translators: %s: date and time of the revision */ 5 => isset($_GET['revision']) ? sprintf(__('Event restored to revision from %s', 'am-events'), wp_post_revision_title((int) $_GET['revision'], false)) : false, 6 => sprintf(__('Event published. View event', 'am-events'), esc_url(get_permalink($post_ID))), 7 => __('Event saved.', 'am-events'), 8 => sprintf(__('Event submitted. Preview event', 'am-events'), esc_url(add_query_arg('preview', 'true', get_permalink($post_ID)))), 9 => sprintf(__('Event scheduled for: %1$s. Preview event', 'am-events'), // translators: Publish box date format, see http://php.net/date date_i18n(_x('d.m.Y G:i', 'Publish box, see http://php.net/date', 'am-events'), strtotime($post->post_date)), esc_url(get_permalink($post_ID))), 10 => sprintf(__('Event draft updated. Preview event', 'am-events'), esc_url(add_query_arg('preview', 'true', get_permalink($post_ID)))), ); return $messages; } /** * Init custom post type (CPT) */ function am_cpt_init() { am_register_post_type(); am_register_taxonomy_venues(); am_register_taxonomy_event_categories(); } /** * Flushes rewrite rules to make permalinks work when activating the plugin. * Pay attention to how am_cpt_init is called in the register_activation_hook callback! */ function am_rewrite_flush() { // First, we "add" the custom post type via the above written function. // Note: "add" is written with quotes, as CPTs don't get added to the DB, // They are only referenced in the post_type column with a post entry, // when you add a post of this CPT. am_cpt_init(); // ATTENTION: This is *only* done during plugin activation hook in this example! // You should *NEVER EVER* do this on every page load!! flush_rewrite_rules(); } register_activation_hook(__FILE__, 'am_rewrite_flush'); /* * **************************************************************************** * =TAXONOMY FILTER * *************************************************************************** */ /** * Adds event category filtering to the event listing in administration. */ function am_restrict_events_by_category() { remove_action('save_post', 'my_metabox_save'); global $typenow; $post_type = 'am_event'; $taxonomy = 'am_event_categories'; if ($typenow == $post_type) { $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : ''; $info_taxonomy = get_taxonomy($taxonomy); wp_dropdown_categories(array( 'show_option_all' => __("Show All {$info_taxonomy->label}", 'am-events'), 'taxonomy' => $taxonomy, 'name' => $taxonomy, 'orderby' => 'name', 'selected' => $selected, 'show_count' => true, 'hide_empty' => false, )); }; } function am_convert_id_to_term_in_query($query) { global $pagenow; $post_type = 'am_event'; $taxonomy = 'am_event_categories'; $q_vars = &$query->query_vars; if ($pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0) { $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy); $q_vars[$taxonomy] = $term->slug; } } /* * Add logging function, if not exists. */ if(!function_exists('_log')){ function _log( $message ) { if( WP_DEBUG === true ){ if( is_array( $message ) || is_object( $message ) ){ error_log( print_r( $message, true ) ); } else { error_log( $message ); } } } } /* * Adds start and end dates edit.php quickedit. */ function am_add_quick_edit($column_name, $post_type) { static $printNonce = TRUE; if ( $printNonce ) { $printNonce = FALSE; wp_nonce_field( plugin_basename( __FILE__ ), 'am_quickedit_nonce' ); } ?> 'am_event', 'post_status' => 'any', 'post_count' => 99999, 'posts_per_page' => 99999, 'meta_query' => array( array( 'key' => 'am_recurrence_id', 'value' => $recurrence_id, 'compare' => "=", ), ), ); $the_query = new WP_Query( $args ); return $the_query->post_count; } else { return 0; } } /** * Sets up rewrites to enable date archives for am_event post type. */ function am_event_datearchives_rewrite_rules($wp_rewrite) { $rules = am_generate_date_archives('am_event', $wp_rewrite); $wp_rewrite->rules = $rules + $wp_rewrite->rules; return $wp_rewrite; } function am_generate_date_archives($cpt, $wp_rewrite) { $rules = array(); $post_type = get_post_type_object($cpt); $slug_archive = $post_type->has_archive; if ($slug_archive === false) return $rules; if ($slug_archive === true) { $slug_archive = $post_type->name; } $dates = array( array( 'rule' => "([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})", 'vars' => array('year', 'monthnum', 'day')), array( 'rule' => "([0-9]{4})/([0-9]{1,2})", 'vars' => array('year', 'monthnum')), array( 'rule' => "([0-9]{4})", 'vars' => array('year')) ); foreach ($dates as $data) { $query = 'index.php?post_type='.$cpt; $rule = $slug_archive.'/'.$data['rule']; $i = 1; foreach ($data['vars'] as $var) { $query.= '&'.$var.'='.$wp_rewrite->preg_index($i); $i++; } $rules[$rule."/?$"] = $query; $rules[$rule."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i); $rules[$rule."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i); $rules[$rule."/page/([0-9]{1,})/?$"] = $query."&paged=".$wp_rewrite->preg_index($i); } return $rules; } ?>