/** * Handles output for a list of objects, can be overridden for descendants. * If you want to override how a list of objects are outputted, override * this, if you just want to override how a single object is outputted, see * the toHTML method. **/ public function objectsToHTML($objects, $css_classes) { if (count($objects) < 1) { return ''; } $class_name = get_custom_post_type($objects[0]->post_type); $class = new $class_name(); ob_start(); ?> <ul class="nobullet <?php if ($css_classes) { echo $css_classes; } else { echo $class->options('name'); ?> -list<?php } ?> "> <?php foreach ($objects as $o) { ?> <li class="document <?php echo $class_name::get_document_application($o); ?> "> <?php echo $class->toHTML($o); ?> </li> <?php } ?> </ul> <?php $html = ob_get_clean(); return $html; }
/** * Post search * * @return string * @author Chris Conover **/ function sc_post_type_search($params = array(), $content = '') { $defaults = array('post_type_name' => 'post', 'taxonomy' => 'category', 'show_empty_sections' => false, 'non_alpha_section_name' => 'Other', 'column_width' => 'span4', 'column_count' => '3', 'order_by' => 'title', 'order' => 'ASC', 'show_sorting' => True, 'default_sorting' => 'term', 'show_sorting' => True); $params = $params === '' ? $defaults : array_merge($defaults, $params); $params['show_empty_sections'] = (bool) $params['show_empty_sections']; $params['column_count'] = is_numeric($params['column_count']) ? (int) $params['column_count'] : $defaults['column_count']; $params['show_sorting'] = (bool) $params['show_sorting']; if (!in_array($params['default_sorting'], array('term', 'alpha'))) { $params['default_sorting'] = $default['default_sorting']; } // Resolve the post type class if (is_null($post_type_class = get_custom_post_type($params['post_type_name']))) { return '<p>Invalid post type.</p>'; } $post_type = new $post_type_class(); // Set default search text if the user didn't if (!isset($params['default_search_text'])) { $params['default_search_text'] = 'Find a ' . $post_type->singular_name; } // Register if the search data with the JS PostTypeSearchDataManager // Format is array(post->ID=>terms) where terms include the post title // as well as all associated tag names $search_data = array(); foreach (get_posts(array('numberposts' => -1, 'post_type' => $params['post_type_name'])) as $post) { $search_data[$post->ID] = array($post->post_title); foreach (wp_get_object_terms($post->ID, 'post_tag') as $term) { $search_data[$post->ID][] = $term->name; } } ?> <script type="text/javascript"> if(typeof PostTypeSearchDataManager != 'undefined') { PostTypeSearchDataManager.register(new PostTypeSearchData( <?php echo json_encode($params['column_count']); ?> , <?php echo json_encode($params['column_width']); ?> , <?php echo json_encode($search_data); ?> )); } </script> <?php // Split up this post type's posts by term $by_term = array(); foreach (get_terms($params['taxonomy']) as $term) { $posts = get_posts(array('numberposts' => -1, 'post_type' => $params['post_type_name'], 'tax_query' => array(array('taxonomy' => $params['taxonomy'], 'field' => 'id', 'terms' => $term->term_id)), 'orderby' => $params['order_by'], 'order' => $params['order'])); if (count($posts) == 0 && $params['show_empty_sections']) { $by_term[$term->name] = array(); } else { $by_term[$term->name] = $posts; } } // Split up this post type's posts by the first alpha character $by_alpha = array(); $by_alpha_posts = get_posts(array('numberposts' => -1, 'post_type' => $params['post_type_name'], 'orderby' => 'title', 'order' => 'alpha')); foreach ($by_alpha_posts as $post) { if (preg_match('/([a-zA-Z])/', $post->post_title, $matches) == 1) { $by_alpha[strtoupper($matches[1])][] = $post; } else { $by_alpha[$params['non_alpha_section_name']][] = $post; } } ksort($by_alpha); if ($params['show_empty_sections']) { foreach (range('a', 'z') as $letter) { if (!isset($by_alpha[strtoupper($letter)])) { $by_alpha[strtoupper($letter)] = array(); } } } $sections = array('post-type-search-term' => $by_term, 'post-type-search-alpha' => $by_alpha); ob_start(); ?> <div class="post-type-search"> <div class="post-type-search-header"> <form class="post-type-search-form" action="." method="get"> <label style="display:none;">Search</label> <input type="text" class="span3" placeholder="<?php echo $params['default_search_text']; ?> "> </form> </div> <div class="post-type-search-results "></div> <?php if ($params['show_sorting']) { ?> <div class="btn-group post-type-search-sorting"> <button class="btn<?php if ($params['default_sorting'] == 'term') { echo ' active'; } ?> "><i class="icon-list-alt"></i></button> <button class="btn<?php if ($params['default_sorting'] == 'alpha') { echo ' active'; } ?> "><i class="icon-font"></i></button> </div> <?php } ?> <?php foreach ($sections as $id => $section) { $hide = false; switch ($id) { case 'post-type-search-alpha': if ($params['default_sorting'] == 'term') { $hide = True; } break; case 'post-type-search-term': if ($params['default_sorting'] == 'alpha') { $hide = True; } break; } ?> <div class="<?php echo $id; ?> "<?php if ($hide) { echo ' style="display:none;"'; } ?> > <?php foreach ($section as $section_title => $section_posts) { ?> <?php if (count($section_posts) > 0 || $params['show_empty_sections']) { ?> <div> <h3><?php echo esc_html($section_title); ?> </h3> <div class="row"> <?php if (count($section_posts) > 0) { ?> <?php $posts_per_column = ceil(count($section_posts) / $params['column_count']); ?> <?php foreach (range(0, $params['column_count'] - 1) as $column_index) { ?> <?php $start = $column_index * $posts_per_column; ?> <?php $end = $start + $posts_per_column; ?> <?php if (count($section_posts) > $start) { ?> <div class="<?php echo $params['column_width']; ?> "> <ul> <?php foreach (array_slice($section_posts, $start, $end) as $post) { ?> <li data-post-id="<?php echo $post->ID; ?> "><?php echo $post_type->toHTML($post); ?> </li> <?php } ?> </ul> </div> <?php } ?> <?php } ?> <?php } ?> </div> </div> <?php } ?> <?php } ?> </div> <?php } ?> </div> <?php return ob_get_clean(); }
/** * Fetches objects defined by arguments passed, outputs the objects according * to the objectsToHTML method located on the object. Used by the auto * generated shortcodes enabled on custom post types. See also: * * CustomPostType::objectsToHTML * CustomPostType::toHTML * * @return string * @author Jared Lang **/ function sc_object_list($attrs, $options = array()) { if (!is_array($attrs)) { return ''; } $default_options = array('default_content' => null, 'sort_func' => null, 'objects_only' => False); extract(array_merge($default_options, $options)); # set defaults and combine with passed arguments $default_attrs = array('type' => null, 'limit' => -1, 'join' => 'or', 'class' => '', 'orderby' => 'menu_order title', 'order' => 'ASC', 'offset' => 0); $params = array_merge($default_attrs, $attrs); # verify options if ($params['type'] == null) { return '<p class="error">No type defined for object list.</p>'; } if (!is_numeric($params['limit'])) { return '<p class="error">Invalid limit argument, must be a number.</p>'; } if (!in_array(strtoupper($params['join']), array('AND', 'OR'))) { return '<p class="error">Invalid join type, must be one of "and" or "or".</p>'; } if (null == ($class = get_custom_post_type($params['type']))) { return '<p class="error">Invalid post type.</p>'; } $class = new $class(); # Use post type specified ordering? if (!isset($attrs['orderby']) && !is_null($class->default_orderby)) { $params['orderby'] = $class->orderby; } if (!isset($attrs['order']) && !is_null($class->default_order)) { $params['order'] = $class->default_order; } # get taxonomies and translation $translate = array('tags' => 'post_tag', 'categories' => 'category', 'org_groups' => 'org_groups'); $taxonomies = array_diff(array_keys($attrs), array_keys($default_attrs)); # assemble taxonomy query $tax_queries = array(); $tax_queries['relation'] = strtoupper($params['join']); foreach ($taxonomies as $tax) { $terms = $params[$tax]; $terms = trim(preg_replace('/\\s+/', ' ', $terms)); $terms = explode(' ', $terms); if (array_key_exists($tax, $translate)) { $tax = $translate[$tax]; } $tax_queries[] = array('taxonomy' => $tax, 'field' => 'slug', 'terms' => $terms); } # perform query $query_array = array('tax_query' => $tax_queries, 'post_status' => 'publish', 'post_type' => $params['type'], 'posts_per_page' => $params['limit'], 'orderby' => $params['orderby'], 'order' => $params['order'], 'offset' => $params['offset']); $query = new WP_Query($query_array); global $post; $objects = array(); while ($query->have_posts()) { $query->the_post(); $objects[] = $post; } # Custom sort if applicable if ($sort_func !== null) { usort($objects, $sort_func); } wp_reset_postdata(); if ($objects_only) { return $objects; } if (count($objects)) { $html = $class->objectsToHTML($objects, $params['class']); } else { $html = $default_content; } return $html; }