} return apply_filters('em_events_can_manage', false, $event_ids); } public static function get_post_search($args = array(), $filter = false, $request = array(), $accepted_args = array()){ //supply $accepted_args to parent argument since we can't depend on late static binding until WP requires PHP 5.3 or later $accepted_args = !empty($accepted_args) ? $accepted_args : array_keys(self::get_default_search()); return apply_filters('em_events_get_post_search', parent::get_post_search($args, $filter, $request, $accepted_args)); } /* Overrides EM_Object method to apply a filter to result * @see wp-content/plugins/events-manager/classes/EM_Object#build_sql_conditions() */ public static function build_sql_conditions( $args = array() ){ global $wpdb; //continue with conditions $conditions = parent::build_sql_conditions($args); //specific location query conditions if locations are enabled if( get_option('dbem_locations_enabled') ){ //events with or without locations if( !empty($args['has_location']) ){ $conditions['has_location'] = '('.EM_EVENTS_TABLE.'.location_id IS NOT NULL AND '.EM_EVENTS_TABLE.'.location_id != 0)'; }elseif( !empty($args['no_location']) ){ $conditions['no_location'] = '('.EM_EVENTS_TABLE.'.location_id IS NULL OR '.EM_EVENTS_TABLE.'.location_id = 0)'; }elseif( !empty($conditions['location_status']) ){ $location_specific_args = array('town', 'state', 'country', 'region', 'near', 'geo', 'search'); foreach( $location_specific_args as $location_arg ){ if( !empty($args[$location_arg]) ) $skip_location_null_condition = true; } if( empty($skip_location_null_condition) ){ $conditions['location_status'] = '('.$conditions['location_status'].' OR '.EM_LOCATIONS_TABLE.'.location_id IS NULL)'; } } } //search conditions if( !empty($args['search']) ){ if( get_option('dbem_locations_enabled') ){ $like_search = array('event_name',EM_EVENTS_TABLE.'.post_content','location_name','location_address','location_town','location_postcode','location_state','location_country','location_region'); }else{ $like_search = array('event_name',EM_EVENTS_TABLE.'.post_content'); } $like_search_string = '%'.$wpdb->esc_like($args['search']).'%'; $like_search_strings = array(); foreach( $like_search as $v ) $like_search_strings[] = $like_search_string; $like_search_sql = "(".implode(" LIKE %s OR ", $like_search). " LIKE %s)"; $conditions['search'] = $wpdb->prepare($like_search_sql, $like_search_strings); } //private events if( empty($args['private']) ){ $conditions['private'] = "(`event_private`=0)"; }elseif( !empty($args['private_only']) ){ $conditions['private_only'] = "(`event_private`=1)"; } if( EM_MS_GLOBAL && !empty($args['blog']) ){ if( is_numeric($args['blog']) ){ if( is_main_site($args['blog']) ){ $conditions['blog'] = "(".EM_EVENTS_TABLE.".blog_id={$args['blog']} OR ".EM_EVENTS_TABLE.".blog_id IS NULL)"; }else{ $conditions['blog'] = "(".EM_EVENTS_TABLE.".blog_id={$args['blog']})"; } }else{ if( !is_array($args['blog']) && preg_match('/^([\-0-9],?)+$/', $args['blog']) ){ $conditions['blog'] = "(".EM_EVENTS_TABLE.".blog_id IN ({$args['blog']}) )"; }elseif( is_array($args['blog']) && self::array_is_numeric($args['blog']) ){ $conditions['blog'] = "(".EM_EVENTS_TABLE.".blog_id IN (".implode(',',$args['blog']).") )"; } } } //post search if( !empty($args['post_id'])){ if( is_array($args['post_id']) ){ $conditions['post_id'] = "(".EM_EVENTS_TABLE.".post_id IN (".implode(',',$args['post_id'])."))"; }else{ $conditions['post_id'] = "(".EM_EVENTS_TABLE.".post_id={$args['post_id']})"; } } // event locations if( !empty($args['event_location_type']) ){ $event_location_types = explode(',', $args['event_location_type']); $event_locations_search = array(); // generate array of clean and enabled event location types foreach( $event_location_types as $event_location_type ){ $event_location_type = trim($event_location_type); if( Event_Locations::is_enabled($event_location_type) ){ $event_locations_search[] = $event_location_type; } } // add condition if at least one valid/clean type supplied if( !empty($event_locations_search) ){ if( count($event_locations_search) === 1 ){ $event_location = current($event_locations_search); $conditions['event_location'] = "event_location_type='$event_location'"; }else{ $conditions['event_location'] = "event_location_type IN ('". implode("','", $event_locations_search) ."')"; } } } if( isset($args['has_event_location']) && $args['has_event_location'] !== false ){ if( $args['has_event_location'] ){ $conditions['has_event_location'] = "event_location_type IS NOT NULL"; }else{ $conditions['has_event_location'] = "event_location_type IS NULL"; } } // active status filters if ( get_option('dbem_event_status_enabled') ) { $active_statuses = array( 'include' => array(), 'exclude' => array() ); // get individual status search args $active_status_map = array('active' => 1, 'cancelled' => 0 ); foreach ( $active_status_map as $status_opt => $status ){ if ( $args[$status_opt] == 1 ) { $active_statuses['include'][] = $status; } elseif( $args[$status_opt] !== null ) { $active_statuses['exclude'][] = $status; } } // get general active status if ( !empty($args['active_status']) ){ $active_status_array = is_array($args['active_status']) ? $args['active_status'] : explode(',', str_replace(' ', '', $args['active_status'])); foreach ( $active_status_array as $status ){ if ( $status > 0 ) { $active_statuses['include'][] = $status; } else { $active_statuses['exclude'][] = absint($status); } } } // add conditional if( !empty($active_statuses['include']) ){ $conditions['active_status_include'] = "event_active_status IN (". implode(',', $active_statuses['include']) .")"; } if( !empty($active_statuses['exclude']) ){ $conditions['active_status_exclude'] = "event_active_status NOT IN (". implode(',', $active_statuses['exclude']) .")"; } } return apply_filters( 'em_events_build_sql_conditions', $conditions, $args ); } /** * Overrides EM_Object method to clean ambiguous fields and apply a filter to result. * @see EM_Object::build_sql_orderby() */ public static function build_sql_orderby( $args, $accepted_fields, $default_order = 'ASC' ){ $accepted_fields[] = 'event_date_modified'; $accepted_fields[] = 'event_date_created'; $orderby = parent::build_sql_orderby($args, $accepted_fields, get_option('dbem_events_default_order')); $orderby = self::build_sql_ambiguous_fields_helper($orderby); //fix ambiguous fields return apply_filters( 'em_events_build_sql_orderby', $orderby, $args, $accepted_fields, $default_order ); } /** * Overrides EM_Object method to clean ambiguous fields and apply a filter to result. * @see EM_Object::build_sql_groupby() */ public static function build_sql_groupby( $args, $accepted_fields, $groupby_order = false, $default_order = 'ASC' ){ $accepted_fields[] = 'event_date_modified'; $accepted_fields[] = 'event_date_created'; $groupby = parent::build_sql_groupby($args, $accepted_fields); //fix ambiguous fields and give them scope of events table $groupby = self::build_sql_ambiguous_fields_helper($groupby); return apply_filters( 'em_events_build_sql_groupby', $groupby, $args, $accepted_fields ); } /** * Overrides EM_Object method to clean ambiguous fields and apply a filter to result. * @see EM_Object::build_sql_groupby_orderby() */ public static function build_sql_groupby_orderby($args, $accepted_fields, $default_order = 'ASC' ){ $accepted_fields[] = 'event_date_modified'; $accepted_fields[] = 'event_date_created'; $group_orderby = parent::build_sql_groupby_orderby($args, $accepted_fields, get_option('dbem_events_default_order')); //fix ambiguous fields and give them scope of events table $group_orderby = self::build_sql_ambiguous_fields_helper($group_orderby); return apply_filters( 'em_events_build_sql_groupby_orderby', $group_orderby, $args, $accepted_fields, $default_order ); } /** * Overrides EM_Object method to provide specific reserved fields and events table. * @see EM_Object::build_sql_ambiguous_fields_helper() */ protected static function build_sql_ambiguous_fields_helper( $fields, $reserved_fields = array(), $prefix = 'table_name' ){ //This will likely be removed when PHP 5.3 is the minimum and LSB is a given return parent::build_sql_ambiguous_fields_helper($fields, array('post_id', 'location_id', 'blog_id'), EM_EVENTS_TABLE); } /* * Adds custom Events search defaults * @param array $array_or_defaults may be the array to override defaults * @param array $array * @return array * @uses EM_Object#get_default_search() */ public static function get_default_search( $array_or_defaults = array(), $array = array() ){ $defaults = array( 'recurring' => false, //we don't initially look for recurring events only events and recurrences of recurring events 'orderby' => get_option('dbem_events_default_orderby'), 'order' => get_option('dbem_events_default_order'), 'groupby' => false, 'groupby_orderby' => 'event_start_date, event_start_time', //groups according to event start time, i.e. by default shows earliest event in a scope 'groupby_order' => 'ASC', //groups according to event start time, i.e. by default shows earliest event in a scope 'status' => 1, //approved events only 'town' => false, 'state' => false, 'country' => false, 'region' => false, 'postcode' => false, 'blog' => get_current_blog_id(), 'private' => current_user_can('read_private_events'), 'private_only' => false, 'post_id' => false, //ouput_grouped specific arguments 'mode' => false, 'header_format' => false, 'date_format' => false, //event-specific search attributes 'has_location' => false, //search events with a location 'no_location' => false, //search events without a location 'location_status' => false, //search events with locations of a specific publish status 'event_location_type' => false, 'has_event_location' => false, 'cancelled' => get_option('dbem_events_include_status_cancelled') ? null : false, // include cancelled events 'active' => null, 'active_status' => null, ); //sort out whether defaults were supplied or just the array of search values if( empty($array) ){ $array = $array_or_defaults; }else{ $defaults = array_merge($defaults, $array_or_defaults); } //specific functionality if( EM_MS_GLOBAL && (!is_admin() || defined('DOING_AJAX')) ){ if( empty($array['blog']) && is_main_site() && get_site_option('dbem_ms_global_events') ){ $array['blog'] = false; } } //admin-area specific modifiers if( is_admin() && !defined('DOING_AJAX') ){ //figure out default owning permissions $defaults['owner'] = !current_user_can('edit_others_events') ? get_current_user_id() : false; if( !array_key_exists('status', $array) && current_user_can('edit_others_events') ){ $defaults['status'] = false; //by default, admins see pending and live events } } //check if we're doing any location-specific searching, if so then we (by default) want to match the status of events if( !empty($array['has_location']) ){ //we're looking for events with locations, so we match the status we're searching for events unless there's an argument passed on for something different $defaults['location_status'] = true; }elseif( !empty($array['no_location']) ){ //if no location is being searched for, we should ignore any status searches for location $defaults['location_status'] = $array['location_status'] = false; }else{ $location_specific_args = array('town', 'state', 'country', 'region', 'near', 'geo'); foreach( $location_specific_args as $location_arg ){ if( !empty($array[$location_arg]) ) $defaults['location_status'] = true; } } $args = parent::get_default_search($defaults,$array); //do some post-parnet cleaning up here if locations are enabled or disabled if( !get_option('dbem_locations_enabled') ){ //locations disabled, wipe any args to do with locations so they're ignored $location_args = array('town', 'state', 'country', 'region', 'has_location', 'no_location', 'location_status', 'location', 'geo', 'near', 'location_id'); foreach( $location_args as $arg ) $args[$arg] = false; } // cancelled status $args['cancelled'] === null ? get_option('dbem_events_include_status_cancelled') : $args['cancelled']; return apply_filters('em_events_get_default_search', $args, $array, $defaults); } } ?>