From c04048a1bd593a82e552d9df8c2bf41f6b19206d Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Wed, 25 Mar 2020 03:03:22 +1100 Subject: [PATCH] Add bulk actions to postpone and cancel events --- .../post-types/class-sp-admin-cpt-event.php | 59 +++++--- modules/sportspress-bulk-actions.php | 142 ++++++++++++------ 2 files changed, 140 insertions(+), 61 deletions(-) diff --git a/includes/admin/post-types/class-sp-admin-cpt-event.php b/includes/admin/post-types/class-sp-admin-cpt-event.php index e145645c..dcdedbb4 100644 --- a/includes/admin/post-types/class-sp-admin-cpt-event.php +++ b/includes/admin/post-types/class-sp-admin-cpt-event.php @@ -44,6 +44,9 @@ class SP_Admin_CPT_Event extends SP_Admin_CPT { // Filtering add_action( 'restrict_manage_posts', array( $this, 'filters' ) ); add_filter( 'parse_query', array( $this, 'filters_query' ) ); + + // Post states + add_filter( 'display_post_states', array( $this, 'post_states' ), 10, 2 ); // Call SP_Admin_CPT constructor parent::__construct(); @@ -294,27 +297,47 @@ class SP_Admin_CPT_Event extends SP_Admin_CPT { wp_nonce_field( 'sp-save-inline-results', 'sp-inline-nonce', false ); } - /** - * Filter in admin based on options - * - * @param mixed $query - */ - public function filters_query( $query ) { - global $typenow, $wp_query; + /** + * Filter in admin based on options + * + * @param mixed $query + */ + public function filters_query( $query ) { + global $typenow, $wp_query; - if ( $typenow == 'sp_event' ) { + if ( $typenow == 'sp_event' ) { - if ( ! empty( $_GET['team'] ) ) { - $query->query_vars['meta_value'] = $_GET['team']; - $query->query_vars['meta_key'] = 'sp_team'; - } + if ( ! empty( $_GET['team'] ) ) { + $query->query_vars['meta_value'] = $_GET['team']; + $query->query_vars['meta_key'] = 'sp_team'; + } - if ( ! empty( $_GET['match_day'] ) ) { - $query->query_vars['meta_value'] = $_GET['match_day']; - $query->query_vars['meta_key'] = 'sp_day'; - } - } - } + if ( ! empty( $_GET['match_day'] ) ) { + $query->query_vars['meta_value'] = $_GET['match_day']; + $query->query_vars['meta_key'] = 'sp_day'; + } + } + } + + /** + * Replace displayed post state for events + * + * @param array $post_states + * @param object $post + */ + public function post_states( $post_states, $post ) { + $status = get_post_meta( $post->ID, 'sp_status', true ); + + if ( 'postponed' == $status ) { + $post_states = array( __( 'Postponed', 'sportspress' ) ); + } elseif ( 'cancelled' == $status ) { + $post_states = array( __( 'Canceled', 'sportspress' ) ); + } elseif ( 'tbd' == $status ) { + $post_states = array( __( 'TBD', 'sportspress' ) ); + } + + return $post_states; + } } endif; diff --git a/modules/sportspress-bulk-actions.php b/modules/sportspress-bulk-actions.php index c95b19b0..b8811fd3 100644 --- a/modules/sportspress-bulk-actions.php +++ b/modules/sportspress-bulk-actions.php @@ -28,10 +28,16 @@ class SportsPress_Bulk_Actions { // Define constants $this->define_constants(); - // Teams - add_filter( 'bulk_actions-edit-sp_team', array( $this, 'team_actions' ) ); - add_filter( 'handle_bulk_actions-edit-sp_team', array( $this, 'team_actions_handler' ), 10, 3 ); - add_action( 'admin_notices', array( $this, 'admin_notices' ) ); + // Teams + add_filter( 'bulk_actions-edit-sp_team', array( $this, 'team_actions' ) ); + add_filter( 'handle_bulk_actions-edit-sp_team', array( $this, 'team_actions_handler' ), 10, 3 ); + + // Events + add_filter( 'bulk_actions-edit-sp_event', array( $this, 'event_actions' ) ); + add_filter( 'handle_bulk_actions-edit-sp_event', array( $this, 'event_actions_handler' ), 10, 3 ); + + // Notices + add_action( 'admin_notices', array( $this, 'admin_notices' ) ); } /** @@ -48,57 +54,107 @@ class SportsPress_Bulk_Actions { define( 'SP_BULK_ACTIONS_DIR', plugin_dir_path( __FILE__ ) ); } - /** - * Add option to the team bulk actions dropdown. - */ - public function team_actions( $bulk_actions ) { - $bulk_actions['sp_calendar'] = __( 'Generate Calendars', 'sportspress' ); - return $bulk_actions; - } + /** + * Add option to the team bulk actions dropdown. + */ + public function team_actions( $bulk_actions ) { + $bulk_actions['sp_calendar'] = __( 'Generate Calendars', 'sportspress' ); + return $bulk_actions; + } - /** - * Handle form submission for team bulk actions. - */ - public function team_actions_handler( $redirect_to, $doaction, $post_ids ) { - if ( $doaction !== 'sp_calendar' ) { - return $redirect_to; - } + /** + * Handle form submission for team bulk actions. + */ + public function team_actions_handler( $redirect_to, $doaction, $post_ids ) { + if ( $doaction !== 'sp_calendar' ) { + return $redirect_to; + } - foreach ( $post_ids as $post_id ) { - $post = array(); - $post['post_title'] = get_the_title( $post_id ) . ' ' . __( 'Calendar', 'sportspress' ); - $post['post_type'] = 'sp_calendar'; - $post['post_status'] = 'publish'; + foreach ( $post_ids as $post_id ) { + $post = array(); + $post['post_title'] = get_the_title( $post_id ) . ' ' . __( 'Calendar', 'sportspress' ); + $post['post_type'] = 'sp_calendar'; + $post['post_status'] = 'publish'; - // Insert post - $id = wp_insert_post( $post ); + // Insert post + $id = wp_insert_post( $post ); - // Flag as bulk - update_post_meta( $id, '_sp_bulk', 1 ); + // Flag as bulk + update_post_meta( $id, '_sp_bulk', 1 ); - // Update meta - update_post_meta( $id, 'sp_team', $post_id ); - update_post_meta( $id, 'sp_format', 'calendar' ); - } + // Update meta + update_post_meta( $id, 'sp_team', $post_id ); + update_post_meta( $id, 'sp_format', 'calendar' ); + } - $redirect_to = add_query_arg( 'sp_bulk_generated_calendars', count( $post_ids ), $redirect_to ); - return $redirect_to; - } + $redirect_to = add_query_arg( 'sp_bulk_generated_calendars', count( $post_ids ), $redirect_to ); + return $redirect_to; + } + + /** + * Add option to the event bulk actions dropdown. + */ + public function event_actions( $bulk_actions ) { + $bulk_actions['sp_postpone'] = __( 'Postpone events', 'sportspress' ); + $bulk_actions['sp_cancel'] = __( 'Cancel events', 'sportspress' ); + return $bulk_actions; + } + + /** + * Handle form submission for event bulk actions. + */ + public function event_actions_handler( $redirect_to, $doaction, $post_ids ) { + if ( ! in_array( $doaction, array( 'sp_postpone', 'sp_cancel' ) ) ) { + return $redirect_to; + } + + if ( 'sp_postpone' == $doaction ) { + foreach ( $post_ids as $post_id ) { + update_post_meta( $post_id, 'sp_status', 'postponed' ); + } + $redirect_to = add_query_arg( 'sp_bulk_postponed_events', count( $post_ids ), $redirect_to ); + } elseif ( 'sp_cancel' == $doaction ) { + foreach ( $post_ids as $post_id ) { + update_post_meta( $post_id, 'sp_status', 'cancelled' ); + } + $redirect_to = add_query_arg( 'sp_bulk_cancelled_events', count( $post_ids ), $redirect_to ); + } + + return $redirect_to; + } /** * Display notices after form submission. */ public function admin_notices() { - if ( ! empty( $_REQUEST['sp_bulk_generated_calendars'] ) ) { - $count = intval( $_REQUEST['sp_bulk_generated_calendars'] ); + if ( ! empty( $_REQUEST['sp_bulk_generated_calendars'] ) ) { + $count = intval( $_REQUEST['sp_bulk_generated_calendars'] ); - printf( '

' . - _n( 'Generated %s calendar.', - 'Generated %s calendars.', - $count, - 'sportspress' - ) . ' ' . __( 'View', 'sportspress' ) . '

', $count ); - } + printf( '

' . + _n( 'Generated %s calendar.', + 'Generated %s calendars.', + $count, + 'sportspress' + ) . ' ' . __( 'View', 'sportspress' ) . '

', $count ); + } elseif ( ! empty( $_REQUEST['sp_bulk_postponed_events'] ) ) { + $count = intval( $_REQUEST['sp_bulk_postponed_events'] ); + + printf( '

' . + _n( 'Postponed %s event.', + 'Postponed %s events.', + $count, + 'sportspress' + ) . '

', $count ); + } elseif ( ! empty( $_REQUEST['sp_bulk_cancelled_events'] ) ) { + $count = intval( $_REQUEST['sp_bulk_cancelled_events'] ); + + printf( '

' . + _n( 'Canceled %s event.', + 'Canceled %s events.', + $count, + 'sportspress' + ) . '

', $count ); + } } }