Skip to content

Instantly share code, notes, and snippets.

@wturnerharris
Created November 13, 2013 04:17
Show Gist options
  • Save wturnerharris/7443614 to your computer and use it in GitHub Desktop.
Save wturnerharris/7443614 to your computer and use it in GitHub Desktop.
Added a new taxonomy for Events Manager, which facilitates adding Event Speakers to events. The taxonomy includes the title, description, and an image.
<?php
if( defined('EM_TAXONOMY_CATEGORY') ) {
define('EM_TAXONOMY_SPEAKER','event-speakers');
define('EM_TAXONOMY_SPEAKER_SLUG', 'event/speakers');
function parse_event_cat_query( ){
global $wp_query, $post;
if( is_tax(EM_TAXONOMY_SPEAKER)||is_tax(EM_TAXONOMY_CATEGORY) ){
unset($wp_query->query_vars['meta_query']);
$wp_query->query_vars['order'] = 'DESC';
}
}
add_filter('parse_query', 'parse_event_cat_query');
// as defined by EventsManager
$supported_array = (EM_MS_GLOBAL && !is_main_site()) ? array():array(EM_POST_TYPE_EVENT,'event-recurring');
register_taxonomy(EM_TAXONOMY_SPEAKER,$supported_array,array(
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => EM_TAXONOMY_SPEAKER_SLUG, 'hierarchical' => true,'with_front'=>false),
//'update_count_callback' => '',
//'show_tagcloud' => true,
//'show_in_nav_menus' => true,
'label' => __('Event Speakers','abana'),
'singular_label' => __('Event Speaker','abana'),
'labels' => array(
'name'=>__('Event Speakers','abana'),
'singular_name'=>__('Event Speaker','abana'),
'search_items'=>__('Search Event Speakers','abana'),
'popular_items'=>__('Popular Event Speakers','abana'),
'all_items'=>__('All Event Speakers','abana'),
'parent_items'=>__('Parent Event Speakers','abana'),
'parent_item_colon'=>__('Parent Event Speaker:','abana'),
'edit_item'=>__('Edit Event Speaker','abana'),
'update_item'=>__('Update Event Speaker','abana'),
'add_new_item'=>__('Add New Event Speaker','abana'),
'new_item_name'=>__('New Event Speaker Name','abana'),
'seperate_items_with_commas'=>__('Seperate event speakers with commas','abana'),
'add_or_remove_items'=>__('Add or remove speakers','abana'),
'choose_from_the_most_used'=>__('Choose from most used event speakers','abana'),
),
));
class EM_Speaker extends EM_Object {
//Taxonomy Fields
var $id = '';
var $term_id;
var $name;
var $slug;
var $term_group;
var $term_taxonomy_id;
var $taxonomy;
var $description = '';
var $parent = 0;
var $count;
//extra attributes imposed by EM_Speaker
var $image_url = '';
var $color;
/**
* Gets data from POST (default), supplied array, or from the database if an ID is supplied
* @param $category_data
* @return null
*/
function EM_Speaker( $category_data = false ) {
global $wpdb;
$this->ms_global_switch();
//Initialize
$category = array();
if( !empty($category_data) ){
//Load category data
if( is_object($category_data) && !empty($category_data->taxonomy) && $category_data->taxonomy == EM_TAXONOMY_SPEAKER ){
$category = $category_data;
}elseif( !is_numeric($category_data) ){
$category = get_term_by('slug', $category_data, EM_TAXONOMY_SPEAKER);
}else{
$category = get_term_by('id', $category_data, EM_TAXONOMY_SPEAKER);
}
}
if( is_object($category) || is_array($category) ){
foreach($category as $key => $value){
$this->$key = $value;
}
}
$this->id = $this->term_id; //backward compatability
$this->ms_global_switch_back();
do_action('em_category',$this, $category_data);
}
function get_image_url(){
if( empty($this->image_url) ){
global $wpdb;
$image_url = $wpdb->get_var('SELECT meta_value FROM '.EM_META_TABLE." WHERE object_id='{$this->term_id}' AND meta_key='speaker-image' LIMIT 1");
$this->image_url = ($image_url != '') ? $image_url:'';
}
return $this->image_url;
}
function get_image_id(){
if( empty($this->image_id) ){
global $wpdb;
$image_id = $wpdb->get_var('SELECT meta_value FROM '.EM_META_TABLE." WHERE object_id='{$this->term_id}' AND meta_key='speaker-image-id' LIMIT 1");
$this->image_id = ($image_id != '') ? $image_id:'';
}
return $this->image_id;
}
function get_url(){
if( empty($this->link) ){
$this->ms_global_switch();
$this->link = get_term_link($this->slug, EM_TAXONOMY_SPEAKER);
$this->ms_global_switch_back();
if ( is_wp_error($this->link) ) $this->link = '';
}
return $this->link;
}
/**
* Depreciated, don't use.
* @return mixed
*/
function has_events(){
global $wpdb;
return false;
}
function output_single($target = 'html'){
$format = get_option ( 'abana_category_page_format' );
return apply_filters('em_category_output_single', $this->output($format, $target), $this, $target);
}
function output($format, $target="html") {
preg_match_all('/\{([a-zA-Z0-9_]+)\}([^{]+)\{\/[a-zA-Z0-9_]+\}/', $format, $conditionals);
if( count($conditionals[0]) > 0 ){
//Check if the language we want exists, if not we take the first language there
foreach($conditionals[1] as $key => $condition){
$format = str_replace($conditionals[0][$key], apply_filters('em_category_output_condition', '', $condition, $conditionals[0][$key], $this), $format);
}
}
$category_string = $format;
preg_match_all("/(#@?_?[A-Za-z0-9]+)({([a-zA-Z0-9,]+)})?/", $format, $placeholders);
foreach($placeholders[1] as $key => $result) {
$replace = '';
$full_result = $placeholders[0][$key];
switch( $result ){
case '#_CATEGORYNAME':
$replace = $this->name;
break;
case '#_CATEGORYID':
$replace = $this->term_id;
break;
case '#_CATEGORYNOTES':
case '#_CATEGORYDESCRIPTION':
$replace = $this->description;
break;
case '#_CATEGORYIMAGE':
case '#_CATEGORYIMAGEURL':
if( $this->get_image_url() != ''){
if($result == '#_CATEGORYIMAGEURL'){
$replace = $this->get_image_url();
}else{
if( empty($placeholders[3][$key]) ){
$replace = "<img src='".esc_url($this->get_image_url())."' alt='".esc_attr($this->name)."'/>";
}else{
$image_size = explode(',', $placeholders[3][$key]);
if( $this->array_is_numeric($image_size) && count($image_size) > 1 ){
if( get_option('abana_disable_timthumb') && $this->get_image_id() ){
//since we previously didn't store image ids along with the url to the image (since taxonomies don't allow normal featured images), sometimes we won't be able to do this, which is why we check there's a valid image id first
$this->ms_global_switch();
$replace = wp_get_attachment_image($this->get_image_id(), $image_size);
$this->ms_global_switch_back();
}else{
$replace = "<img src='".em_get_thumbnail_url($this->get_image_url(), $image_size[0], $image_size[1])."' alt='".esc_attr($this->name)."' width='{$image_size[0]}' height='{$image_size[1]}'/>";
}
}else{
$replace = "<img src='".esc_url($this->get_image_url())."' alt='".esc_attr($this->name)."'/>";
}
}
}
}
break;
case '#_CATEGORYCOLOR':
$replace = $this->get_color();
break;
case '#_CATEGORYLINK':
case '#_CATEGORYURL':
$link = $this->get_url();
$replace = ($result == '#_CATEGORYURL') ? $link : '<a href="'.$link.'">'.esc_html($this->name).'</a>';
break;
case '#_CATEGORYSLUG':
$replace = $this->slug;
break;
case '#_CATEGORYEVENTSPAST': //depreciated, erroneous documentation, left for compatability
case '#_CATEGORYEVENTSNEXT': //depreciated, erroneous documentation, left for compatability
case '#_CATEGORYEVENTSALL': //depreciated, erroneous documentation, left for compatability
case '#_CATEGORYPASTEVENTS':
case '#_CATEGORYNEXTEVENTS':
case '#_CATEGORYALLEVENTS':
//convert depreciated placeholders for compatability
$result = ($result == '#_CATEGORYEVENTSPAST') ? '#_CATEGORYPASTEVENTS':$result;
$result = ($result == '#_CATEGORYEVENTSNEXT') ? '#_CATEGORYNEXTEVENTS':$result;
$result = ($result == '#_CATEGORYEVENTSALL') ? '#_CATEGORYALLEVENTS':$result;
//forget it ever happened? :/
if ($result == '#_CATEGORYPASTEVENTS'){ $scope = 'past'; }
elseif ( $result == '#_CATEGORYNEXTEVENTS' ){ $scope = 'future'; }
else{ $scope = 'all'; }
$events_count = EM_Events::count( array('category'=>$this->term_id, 'scope'=>$scope) );
if ( $events_count > 0 ){
$args = array('category'=>$this->term_id, 'scope'=>$scope, 'pagination'=>1);
$args['format_header'] = get_option('abana_category_event_list_item_header_format');
$args['format_footer'] = get_option('abana_category_event_list_item_footer_format');
$args['format'] = get_option('abana_category_event_list_item_format');
$args['limit'] = get_option('abana_category_event_list_limit');
$args['page'] = (!empty($_REQUEST['pno']) && is_numeric($_REQUEST['pno']) )? $_REQUEST['pno'] : 1;
$replace = EM_Events::output($args);
} else {
$replace = get_option('abana_category_no_events_message','</ul>');
}
break;
default:
$replace = $full_result;
break;
}
$replace = apply_filters('em_category_output_placeholder', $replace, $this, $full_result, $target); //USE WITH CAUTION! THIS MIGHT GET RENAMED
$category_string = str_replace($full_result, $replace , $category_string );
}
$name_filter = ($target == "html") ? 'abana_general':'abana_general_rss'; //TODO remove abana_ filters
$category_string = str_replace('#_CATEGORY', apply_filters($name_filter, $this->name) , $category_string ); //Depreciated
return apply_filters('em_category_output', $category_string, $this, $format, $target);
}
function can_manage( $capability_owner = 'edit_categories', $capability_admin = false ){
global $em_capabilities_array;
//Figure out if this is multisite and require an extra bit of validation
$multisite_check = true;
$can_manage = current_user_can($capability_owner);
//if multisite and supoer admin, just return true
if( is_multisite() && is_super_admin() ){ return true; }
if( EM_MS_GLOBAL && !is_main_site() ){
//User can't admin this bit, as they're on a sub-blog
$can_manage = false;
if(array_key_exists($capability_owner, $em_capabilities_array) ){
$this->add_error( $em_capabilities_array[$capability_owner]);
}
}
return $can_manage;
}
}
class EM_Speakers_Taxonomy{
function init(){
add_action( EM_TAXONOMY_SPEAKER.'_edit_form_fields', array('EM_Speakers_Taxonomy','form'), 10, 1);
add_action( EM_TAXONOMY_SPEAKER.'_add_form_fields', array('EM_Speakers_Taxonomy','form'), 10, 1);
add_action( 'edited_'.EM_TAXONOMY_SPEAKER, array('EM_Speakers_Taxonomy','save'), 10, 2);
add_action( 'create_'.EM_TAXONOMY_SPEAKER, array('EM_Speakers_Taxonomy','save'), 10, 2);
add_action( 'delete_'.EM_TAXONOMY_SPEAKER, array('EM_Speakers_Taxonomy','delete'), 10, 2);
add_filter('manage_edit-'.EM_TAXONOMY_SPEAKER.'_columns' , array('EM_Speakers_Taxonomy','columns_add'));
add_filter('manage_'.EM_TAXONOMY_SPEAKER.'_custom_column' , array('EM_Speakers_Taxonomy','columns_output'),10,3);
self::admin_init();
}
function columns_add($columns) {
//prepend ID after checkbox
$columns['cat-id'] = __('ID','abana');
unset($columns['description']);
return $columns;
}
function columns_output( $val, $column, $term_id ) {
switch ( $column ) {
case 'cat-id':
return $term_id;
break;
}
return $val;
}
function admin_init(){
global $pagenow;
if($pagenow == 'edit-tags.php' && !empty($_GET['taxonomy']) && $_GET['taxonomy'] == EM_TAXONOMY_SPEAKER){
wp_enqueue_style( 'farbtastic' );
wp_enqueue_style( 'thickbox' );
wp_enqueue_script( 'em-speakers-admin', get_stylesheet_directory_uri().'/lib/js/speakers-admin.js', array( 'jquery','media-upload','thickbox','farbtastic' ) );
}
}
function form($tag){
$speaker_image = '';
$is_edit = $tag != EM_TAXONOMY_SPEAKER;
$div = $is_edit ? "tr" : "div" ;
$td = $is_edit ? "td" : "div" ;
$p = $is_edit ? "td" : "p" ;
if( $is_edit ){ //not an add new tag form
$EM_Speaker = new EM_Speaker($tag);
$speaker_image = $EM_Speaker->get_image_url();
$speaker_image_id = $EM_Speaker->get_image_id();
}
$desc_id = is_object($tag) ? "description" : "tag-description";
$desc = is_object($tag) ? $tag->description : "";
?>
<<?=$div?> class="invisible code">
<<?=$p?>></<?=$p?>>
<<?=$td?>>
<script type="text/javascript">var desc = "<?php echo $desc_id; ?>"; jQuery('#'+desc).parents('.form-field').remove();</script>
<style>.form-field input.ed_button{width: auto;}</style>
</<?=$td?>>
</<?=$div ?>>
<<?=$div?> class="form-field">
<<?=$p?> scope="row" valign="top"><label for="<?php echo $desc_id; ?>">Description</label></<?=$p?>>
<<?=$td?>>
<?php wp_editor(htmlspecialchars_decode($desc), $desc_id, array( "teeny" => true, )); ?>
<p class="description"><?php _e('The description is not prominent by default; however, some themes may show it.','abana'); ?></p>
</<?=$td?>>
</<?=$div ?>>
<<?=$div?> class="form-field">
<<?=$p?> scope="row" valign="top"><label for="speaker-image">Image</label></<?=$p?>>
<<?=$td?>>
<?php if( !empty($speaker_image) ): ?>
<p style="width: 300px"><img style="max-width: 100%" src="<?php echo $speaker_image; ?>" /></p>
<?php endif; ?>
<input type="text" name="speaker_image" id="speaker-image" value="<?php echo esc_attr($speaker_image); ?>" style="width:300px;" />
<input type="hidden" name="speaker_image_id" id="speaker-image-id" value="<?php echo esc_attr($speaker_image); ?>" />
<input id="upload_image_button" type="button" value="<?php _e('Choose/Upload Image','abana'); ?>" class="button-secondary" style="width:auto;" /><br />
<p class="description"><?php echo sprintf(__('Choose an image for your speaker, which can be displayed using the %s placeholder.','abana'),'<code>#_SPEAKERIMAGE</code>'); ?></p>
</<?=$td?>>
</<?=$div ?>>
<?php
}
function save($term_id, $tt_id){
global $wpdb;
if (!$term_id) return;
if( !empty($_POST['speaker_image']) ){
//get results and save/update
$prev_settings = $wpdb->get_results('SELECT meta_value FROM '.EM_META_TABLE." WHERE object_id='{$term_id}' AND meta_key='speaker-image'");
if( count($prev_settings) > 0 ){
$wpdb->update(EM_META_TABLE, array('object_id'=>$term_id,'meta_value'=>$_POST['speaker_image']), array('object_id'=>$term_id,'meta_key'=>'speaker-image'));
}else{
$wpdb->insert(EM_META_TABLE, array('object_id'=>$term_id,'meta_key'=>'speaker-image','meta_value'=>$_POST['speaker_image']));
}
if( !empty($_POST['speaker_image_id']) && is_numeric($_POST['speaker_image_id']) ){
//get results and save/update
$prev_settings = $wpdb->get_results('SELECT meta_value FROM '.EM_META_TABLE." WHERE object_id='{$term_id}' AND meta_key='speaker-image-id'");
if( count($prev_settings) > 0 ){
$wpdb->update(EM_META_TABLE, array('object_id'=>$term_id,'meta_value'=>$_POST['speaker_image_id']), array('object_id'=>$term_id,'meta_key'=>'speaker-image-id'));
}else{
$wpdb->insert(EM_META_TABLE, array('object_id'=>$term_id,'meta_key'=>'speaker-image-id','meta_value'=>$_POST['speaker_image_id']));
}
}
}
}
function delete( $term_id ){
global $wpdb;
//delete speaker image and color
$wpdb->query('DELETE FROM '.EM_META_TABLE." WHERE object_id='$term_id' AND meta_key='speaker-image'");
//delete all events speaker relations
$wpdb->query('DELETE FROM '.EM_META_TABLE." WHERE meta_value='{$term_id}' AND meta_key='event-speaker'");
}
}
add_action('admin_init',array('EM_Speakers_Taxonomy','init'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment