ms_global_switch(); if( is_object($data) && get_class($data) == "EM_Event" && !empty($data->post_id) ){ //Creates a blank categories object if needed $this->event_id = $data->event_id; $this->post_id = $data->post_id; if( EM_MS_GLOBAL && !is_main_site() ){ $cat_ids = $wpdb->get_col('SELECT meta_value FROM '.EM_META_TABLE." WHERE object_id='{$this->event_id}' AND meta_key='event-category'"); foreach($cat_ids as $cat_id){ $this->categories[$cat_id] = new EM_Category($cat_id); } }else{ $results = get_the_terms( $data->post_id, EM_TAXONOMY_CATEGORY ); if( is_array($results) ){ foreach($results as $result){ $this->categories[$result->term_id] = new EM_Category($result); } } } }elseif( is_array($data) && $this->array_is_numeric($data) ){ foreach($data as $category_id){ $this->categories[$category_id] = new EM_Category($category_id); } }elseif( is_array($data) ){ foreach( $data as $EM_Category ){ if( get_class($EM_Category) == 'EM_Category'){ $this->categories[] = $EM_Category; } } } $this->ms_global_switch_back(); do_action('em_categories', $this); } function get_post(){ $this->ms_global_switch(); if(!empty($_POST['event_categories']) && $this->array_is_numeric($_POST['event_categories'])){ foreach( $_POST['event_categories'] as $term ){ $this->categories[$term] = new EM_Category($term); } } $this->ms_global_switch_back(); do_action('em_categories_get_post', $this); } function save(){ $term_slugs = array(); foreach($this->categories as $EM_Category){ /* @var $EM_Category EM_Category */ if( !empty($EM_Category->slug) ) $term_slugs[] = $EM_Category->slug; //save of category will soft-fail if slug is empty } if( count($term_slugs) == 0 && get_option('dbem_default_category') ){ $default_term = get_term_by('id',get_option('dbem_default_category'), EM_TAXONOMY_CATEGORY); if($default_term) $term_slugs[] = $default_term->slug; } if( count($term_slugs) > 0 ){ if( is_multisite() ){ //In MS Global mode, we also save category meta information for global lookups if( EM_MS_GLOBAL && !empty($this->event_id) ){ //delete categories $this->save_index(); } if( !EM_MS_GLOBAL || is_main_site() ){ wp_set_object_terms($this->post_id, $term_slugs, EM_TAXONOMY_CATEGORY); } }else{ wp_set_object_terms($this->post_id, $term_slugs, EM_TAXONOMY_CATEGORY); } } do_action('em_categories_save', $this); } function save_index(){ global $wpdb; $wpdb->query('DELETE FROM '.EM_META_TABLE." WHERE object_id='{$this->event_id}' AND meta_key='event-category'"); foreach($this->categories as $EM_Category){ $wpdb->insert(EM_META_TABLE, array('meta_value'=>$EM_Category->term_id,'object_id'=>$this->event_id,'meta_key'=>'event-category')); } } function get( $args = array() ) { //Quick version, we can accept an array of IDs, which is easy to retrieve self::ms_global_switch(); if( self::array_is_numeric($args) ){ //Array of numbers, assume they are event IDs to retreive $results = get_terms( EM_TAXONOMY_CATEGORY ); $categories = array(); foreach($results as $result){ if( in_array($result->term_id, $args) ){ $categories[$result->term_id] = new EM_Category($result); } } }else{ //We assume it's either an empty array or array of search arguments to merge with defaults $term_args = self::get_default_search($args); $results = get_terms( EM_TAXONOMY_CATEGORY, $term_args); //If we want results directly in an array, why not have a shortcut here? We don't use this in code, so if you're using it and filter the em_categories_get hook, you may want to do this one too. if( !empty($args['array']) ){ return apply_filters('em_categories_get_array', $results, $args); } //Make returned results EM_Category objects $results = (is_array($results)) ? $results:array(); $categories = array(); foreach ( $results as $category ){ $categories[$category->term_id] = new EM_Category($category); } } self::ms_global_switch_back(); return apply_filters('em_categories_get', $categories, $args); } function output( $args ){ global $EM_Category; $EM_Category_old = $EM_Category; //When looping, we can replace EM_Category global with the current event in the loop //Can be either an array for the get search or an array of EM_Category objects if( is_object(current($args)) && get_class((current($args))) == 'EM_Category' ){ $func_args = func_get_args(); $categories = $func_args[0]; $args = (!empty($func_args[1])) ? $func_args[1] : array(); $args = apply_filters('em_categories_output_args', self::get_default_search($args), $categories); $limit = ( !empty($args['limit']) && is_numeric($args['limit']) ) ? $args['limit']:false; $offset = ( !empty($args['offset']) && is_numeric($args['offset']) ) ? $args['offset']:0; $page = ( !empty($args['page']) && is_numeric($args['page']) ) ? $args['page']:1; }else{ $args = apply_filters('em_categories_output_args', self::get_default_search($args) ); $limit = ( !empty($args['limit']) && is_numeric($args['limit']) ) ? $args['limit']:false; $offset = ( !empty($args['offset']) && is_numeric($args['offset']) ) ? $args['offset']:0; $page = ( !empty($args['page']) && is_numeric($args['page']) ) ? $args['page']:1; $args['limit'] = false; $args['offset'] = false; $args['page'] = false; $categories = self::get( $args ); } //What format shall we output this to, or use default $format = ( $args['format'] == '' ) ? get_option( 'dbem_categories_list_item_format' ) : $args['format'] ; $output = ""; $categories_count = count($categories); $categories = apply_filters('em_categories_output_categories', $categories); if ( count($categories) > 0 ) { $category_count = 0; $categories_shown = 0; foreach ( $categories as $EM_Category ) { if( ($categories_shown < $limit || empty($limit)) && ($category_count >= $offset || $offset === 0) ){ $output .= $EM_Category->output($format); $categories_shown++; } $category_count++; } //Add headers and footers to output if( $format == get_option ( 'dbem_categories_list_item_format' ) ){ $single_event_format_header = get_option ( 'dbem_categories_list_item_format_header' ); $single_event_format_header = ( $single_event_format_header != '' ) ? $single_event_format_header : "