|
<?php |
|
|
|
/* |
|
Plugin Name: Ordered Post Tags – Template Tags & API |
|
Plugin URI: https://gist.github.com/3217544 |
|
Description: Proof of concept plugin, not for production use. Provides template tags and an API for those functions. Won't work without Ordered Post Tags POC plugin. |
|
Version: 0.1 |
|
Author: Simon Wheatley @ Code for the People Ltd |
|
Author URI: http://codeforthepeople.com |
|
*/ |
|
|
|
/** |
|
* Copyright 2012 Code for the People Ltd. All rights reserved |
|
* Released under the GPL license |
|
* http://www.opensource.org/licenses/gpl-license.php |
|
* |
|
* This is an add-on for WordPress |
|
* http://wordpress.org/ |
|
* |
|
* ********************************************************************** |
|
* This program is free software; you can redistribute it and/or modify |
|
* it under the terms of the GNU General Public License as published by |
|
* the Free Software Foundation; either version 2 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* ********************************************************************** |
|
*/ |
|
|
|
// NOW SOME TEMPLATING FUNCTIONALITY FOR DEMO PURPOSES |
|
// =================================================== |
|
|
|
// N.B. Lots of duplicate code follows, were we to come to implement this in core |
|
// it would be sensible to enhance the function equivalents of the functions |
|
// below to accept an additional argument(s) to specify term_order. |
|
|
|
/** |
|
* Retrieve the terms of the taxonomy that are attached to the post |
|
* in term_order. |
|
* |
|
* @param int $id Post ID. |
|
* @param string $taxonomy Taxonomy name. |
|
* @return array|bool False on failure. Array of term objects on success. |
|
*/ |
|
function get_the_terms_ordered( $id, $taxonomy ) { |
|
global $post; |
|
|
|
$id = (int) $id; |
|
|
|
if ( !$id ) { |
|
if ( empty( $post->ID ) ) |
|
return false; |
|
else |
|
$id = (int) $post->ID; |
|
} |
|
|
|
$terms = get_object_term_cache( $id, $taxonomy ); |
|
$terms = wp_cache_get($id, $taxonomy . '_ordered_relationships'); |
|
if ( false === $terms ) { |
|
$terms = wp_get_object_terms( $id, $taxonomy, array( 'orderby' => 'term_order' ) ); |
|
wp_cache_add($id, $terms, $taxonomy . '_ordered_relationships'); |
|
} |
|
|
|
$terms = apply_filters( 'get_the_terms_ordered', $terms, $id, $taxonomy ); |
|
|
|
if ( empty( $terms ) ) |
|
return false; |
|
|
|
return $terms; |
|
} |
|
|
|
/** |
|
* Retrieve a post's terms as a list with specified format ordered by term_order. |
|
* |
|
* @param int $id Post ID. |
|
* @param string $taxonomy Taxonomy name. |
|
* @param string $before Optional. Before list. |
|
* @param string $sep Optional. Separate items using this. |
|
* @param string $after Optional. After list. |
|
* @return string |
|
*/ |
|
function get_the_term_list_ordered( $id, $taxonomy, $before = '', $sep = '', $after = '' ) { |
|
$terms = get_the_terms_ordered( $id, $taxonomy ); |
|
|
|
if ( is_wp_error( $terms ) ) |
|
return $terms; |
|
|
|
if ( empty( $terms ) ) |
|
return false; |
|
|
|
foreach ( $terms as $term ) { |
|
$link = get_term_link( $term, $taxonomy ); |
|
if ( is_wp_error( $link ) ) |
|
return $link; |
|
$term_links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>'; |
|
} |
|
|
|
$term_links = apply_filters( "term_links-$taxonomy", $term_links ); |
|
|
|
return $before . join( $sep, $term_links ) . $after; |
|
} |
|
|
|
/** |
|
* Retrieve the tags for a post formatted as a string ordered by term_order. |
|
* |
|
* @uses apply_filters() Calls 'the_tags' filter on string list of tags. |
|
* |
|
* @param string $before Optional. Before tags. |
|
* @param string $sep Optional. Between tags. |
|
* @param string $after Optional. After tags. |
|
* @param int $id Optional. Post ID. Defaults to the current post. |
|
* @return string |
|
*/ |
|
function get_the_tag_list_ordered( $before = '', $sep = '', $after = '', $id = 0 ) { |
|
return apply_filters( 'the_tags', get_the_term_list_ordered( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id ); |
|
} |
|
|
|
/** |
|
* Print the tags for a post in term_order. |
|
* |
|
* |
|
* @param string $before Optional. Before list. |
|
* @param string $sep Optional. Separate items using this. |
|
* @param string $after Optional. After list. |
|
* @return string |
|
*/ |
|
function the_tags_ordered( $before = null, $sep = ', ', $after = '' ) { |
|
if ( null === $before ) |
|
$before = __('Tags: '); |
|
echo get_the_tag_list_ordered($before, $sep, $after); |
|
} |
|
|
|
|
|
?> |