*/ class TICA_Events_Admin { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; $type = TICA_Events::POST_TYPE; $this->when_box_id = "{$type}_form_when"; $this->from_date_id = "{$type}_from_date"; $this->to_date_id = "{$type}_to_date"; $this->from_time_id = "{$type}_from_time"; $this->to_time_id = "{$type}_to_time"; $this->when_fields = [ $this->from_date_id, $this->to_date_id, $this->from_time_id, $this->to_time_id, ]; $this->where_box_id = "{$type}_form_where"; $this->location_name_id = "{$type}_location_name"; $this->location_address_id = "{$type}_location_address"; $this->location_city_id = "{$type}_location_city"; $this->location_state_id = "{$type}_location_state"; $this->location_zip_id = "{$type}_location_zip"; $this->location_country_id = "{$type}_location_country"; $this->where_fields = [ $this->location_name_id, $this->location_address_id, $this->location_city_id, $this->location_state_id, $this->location_zip_id, $this->location_country_id, ]; } /** * Register the stylesheets for the admin area. * * @since 1.0.0 */ public function enqueue_styles() { wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/tica-events-admin.css', $this->version, 'all' ); } /** * Register the JavaScript for the admin area. * @param $hook * * @since 1.0.0 */ public function enqueue_scripts( $hook ) { global $post; // add scripts to add/edit form if ( TICA_Events::POST_TYPE == $post->post_type && in_array( $hook, [ 'post-new.php', 'post.php' ] ) ) { wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/tica-events-edit.js', ['jquery'], $this->version, false ); $options = [ 'from_date_id' => $this->from_date_id, 'to_date_id' => $this->to_date_id, 'from_time_id' => $this->from_time_id, 'to_time_id' => $this->to_time_id, ]; $options = json_encode($options); wp_add_inline_script($this->plugin_name, "jQuery(document).ready(function() { jQuery('#{$this->when_box_id}').initWhenBox({$options}); });"); } } public function register_events_cpt() { // register events post type $labels = array( 'name' => _x( 'Events', 'Post Type General Name', 'e' ), 'singular_name' => _x( 'Event', 'Post Type Singular Name', 'tica-events' ), 'menu_name' => __( 'TICA Events', 'tica-events' ), 'name_admin_bar' => __( 'Event', 'tica-events' ), 'archives' => __( 'Events', 'tica-events' ), 'attributes' => __( 'Event Attributes', 'tica-events' ), 'parent_item_colon' => __( 'Parent Item:', 'tica-events' ), 'all_items' => __( 'All Events', 'tica-events' ), 'add_new_item' => __( 'Add New Event', 'tica-events' ), 'add_new' => __( 'Add New', 'tica-events' ), 'new_item' => __( 'New Event', 'tica-events' ), 'edit_item' => __( 'Edit Event', 'tica-events' ), 'update_item' => __( 'Update Event', 'tica-events' ), 'view_item' => __( 'View Event', 'tica-events' ), 'view_items' => __( 'View Events', 'tica-events' ), 'search_items' => __( 'Search Events', 'tica-events' ), 'not_found' => __( 'Not found', 'tica-events' ), 'not_found_in_trash' => __( 'Not found in Trash', 'tica-events' ), 'featured_image' => __( 'Featured Image', 'tica-events' ), 'set_featured_image' => __( 'Set featured image', 'tica-events' ), 'remove_featured_image' => __( 'Remove featured image', 'tica-events' ), 'use_featured_image' => __( 'Use as featured image', 'tica-events' ), 'insert_into_item' => __( /** @lang text */ 'Insert into event', 'tica-events' ), 'uploaded_to_this_item' => __( 'Uploaded to this events item', 'tica-events' ), 'items_list' => __( 'Events list', 'tica-events' ), 'items_list_navigation' => __( 'Events list navigation', 'tica-events' ), 'filter_items_list' => __( 'Filter Events list', 'tica-events' ), ); $args = array( 'label' => __( 'Events', 'tica-events' ), 'description' => __( 'Events Description', 'tica-events' ), 'labels' => $labels, 'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 30, 'menu_icon' => plugin_dir_url( __FILE__ ) . 'img/icon.png', 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => false, 'exclude_from_search' => true, 'publicly_queryable' => true, 'capability_type' => 'post', 'rewrite' => [ 'slug' => 'event' ] ); register_post_type( TICA_Events::POST_TYPE, $args ); } public function add_meta_boxes() { add_meta_box( TICA_Events::POST_TYPE . '_meta_when', __( 'When (EDT)', 'tica-events' ), [ $this, 'meta_when_callback' ], null, 'side', 'high' ); add_meta_box( TICA_Events::POST_TYPE . '_meta_where', __( 'Location', 'tica-events' ), [ $this, 'meta_where_callback' ] ); } public function meta_when_callback( $post ) { if ( $post->post_type != TICA_Events::POST_TYPE ) { return; } $meta = get_post_meta( $post->ID ); $from_date = ( ! empty( $meta[$this->from_date_id][0] ) ) ? $meta[$this->from_date_id][0] : ''; $to_date = ( ! empty( $meta[$this->to_date_id][0] ) ) ? $meta[$this->to_date_id][0] : ''; $from_time = ( ! empty( $meta[$this->from_time_id][0] ) ) ? $meta[$this->from_time_id][0] : ''; $to_time = ( ! empty( $meta[$this->to_time_id][0] ) ) ? $meta[$this->to_time_id][0] : ''; ?>

post_type != TICA_Events::POST_TYPE ) { return; } $meta = get_post_meta( $post->ID ); $location_name = ( ! empty( $meta[$this->location_name_id][0] ) ) ? $meta[$this->location_name_id][0] : ''; $location_address = ( ! empty( $meta[$this->location_address_id][0] ) ) ? $meta[$this->location_address_id][0] : ''; $location_city = ( ! empty( $meta[$this->location_city_id][0] ) ) ? $meta[$this->location_city_id][0] : ''; $location_state = ( ! empty( $meta[$this->location_state_id][0] ) ) ? $meta[$this->location_state_id][0] : ''; $location_zip = ( ! empty( $meta[$this->location_zip_id][0] ) ) ? $meta[$this->location_zip_id][0] : ''; $location_country = ( ! empty( $meta[$this->location_country_id][0] ) ) ? $meta[$this->location_country_id][0] : ''; ?>

_save_when( $post_id ); $this->_save_where( $post_id ); } protected function _save_when( $post_id ) { foreach ( $this->when_fields as $name ) { $value = @$_POST[$name]; if ( $name == $this->to_date_id && empty( $value ) ) { $value = @$_POST[$this->from_date_id]; } if ( ! empty( $value ) ) { update_post_meta( $post_id, $name, $value ); } else { update_post_meta( $post_id, $name, '' ); } } } protected function _save_where( $post_id ) { foreach ( $this->where_fields as $name ) { if ( isset( $_POST[$name] ) ) { update_post_meta( $post_id, $name, $_POST[$name] ); } else { update_post_meta( $post_id, $name, '' ); } } } public function posts_columns( $columns ) { $first_val = reset( $columns ); $first_key = key( $columns ); if ( 'cb' == $first_key ) { array_shift( $columns ); $columns = [ $first_key => $first_val, TICA_Events::DEFAULT_ORDERBY => 'When' ] + $columns; } else { $columns = [ TICA_Events::DEFAULT_ORDERBY => 'When' ] + $columns; } return $columns; } public function sortable_columns( $columns ) { $columns[TICA_Events::DEFAULT_ORDERBY] = [ TICA_Events::DEFAULT_ORDERBY, ( 'DESC' == strtoupper( TICA_Events::DEFAULT_ORDER ) ) ]; return $columns; } public function posts_custom_column( $column, $post_id ) { $date = get_post_meta( $post_id, $column, true ); $date_format = get_option( 'date_format' ); if ( $date ) { $date = date( $date_format, strtotime( $date ) ); } else { $date = '-' ; } esc_html_e( $date ); } public function sort_custom_column( $clauses, $wp_query ) { global $wpdb; $order_by = TICA_Events::DEFAULT_ORDERBY; if ( isset( $wp_query->query['orderby'] ) && $wp_query->query['orderby'] == $order_by ) { $clauses['join'] .= /** @lang text */ <<postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '{$order_by}' SQL; $clauses['orderby'] = "{$wpdb->postmeta}.meta_value "; if ( strtoupper( $wp_query->get( 'order' ) ) == 'ASC' ){ $clauses['orderby'] .= 'ASC'; } else{ $clauses['orderby'] .= 'DESC'; } } return $clauses; } public function default_order() { if ( TICA_Events::POST_TYPE == @$_REQUEST['post_type'] && empty( $_REQUEST['orderby'] ) ) { $url = get_admin_url( null, '/edit.php?' ); $url .= http_build_query( [ 'post_type' => TICA_Events::POST_TYPE, 'orderby' => TICA_Events::DEFAULT_ORDERBY, 'order' => TICA_Events::DEFAULT_ORDER, ] ); wp_redirect( $url ); } } public function print_admin_styles() { if ( ! is_admin() ) { return; } global $pagenow; $column = TICA_Events::DEFAULT_ORDERBY; if ( $pagenow == 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] == TICA_Events::POST_TYPE ) { echo << STYLE; } } }