Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f2a8995198
Fetching contributors…

Cannot retrieve contributors at this time

311 lines (249 sloc) 11.052 kb
<?php
/**
* Functions for helping with outputting post formats in a more standardized fashion.
*
* @package Cakifo
* @subpackage Functions
* @since Cakifo 1.5
*/
/* Makes URLs in link posts clickable. */
add_filter( 'the_content', 'cakifo_link_content' );
/* Wraps <blockquote> around quote posts. */
add_filter( 'the_content', 'cakifo_quote_content' );
/* Filter the content of chat posts. */
add_filter( 'the_content', 'cakifo_format_chat_content' );
/* Auto-add paragraphs to the chat text. */
add_filter( 'post_format_chat_text', 'wpautop' );
/**
* Filters the content of the link format posts. Wraps the content in the make_clickable() function
* so that users can enter just a URL into the post content editor.
*
* @since Cakifo 1.5
* @param string $content The post content.
* @return string $content
*/
function cakifo_link_content( $content ) {
if ( has_post_format( 'link' ) )
$content = make_clickable( $content );
return $content;
}
/**
* Wraps the output of the quote post format content in a <blockquote> element if the user hasn't added a
* <blockquote> in the post editor.
*
* @since Cakifo 1.5
* @author Justin Tadlock
* @link http://justintadlock.com
*
* @param string $content The post content.
* @return string $content
*
*/
function cakifo_quote_content( $content ) {
if ( has_post_format( 'quote' ) ) {
preg_match( '/<blockquote.*?>/', $content, $matches );
if ( empty( $matches ) )
$content = "<blockquote>{$content}</blockquote>";
}
return $content;
}
/**
* This function filters the post content when viewing a post with the "chat" post format. It formats the
* content with structured HTML markup to make it easy for theme developers to style chat posts. The
* advantage of this solution is that it allows for more than two speakers (like most solutions). You can
* have 100s of speakers in your chat post, each with their own, cakifo classes for styling.
*
* @since Cakifo 1.5
* @author David Chandra
* @link http://www.turtlepod.org
* @author Justin Tadlock
* @link http://justintadlock.com
* @copyright Copyright (c) 2012
* @link http://themehybrid.com/support/topic/post-format-chat-suggestion
*
* @global array $_post_format_chat_ids An array of IDs for the chat rows based on the author.
* @param string $content The content of the post.
* @return string $chat_output The formatted content of the post.
*/
function cakifo_format_chat_content( $content ) {
global $_post_format_chat_ids;
/* If this is not a 'chat' post, return the content. */
if ( ! has_post_format( 'chat' ) )
return $content;
/* Set the global variable of speaker IDs to a new, empty array for this chat. */
$_post_format_chat_ids = array();
/* Allow the separator (separator for speaker/text) to be filtered. */
$separator = apply_filters( 'post_format_chat_separator', ':' );
/* Open the chat transcript div and give it a cakifo ID based on the post ID. */
$chat_output = "\n\t\t\t" . '<div id="chat-transcript-' . esc_attr( get_the_ID() ) . '" class="chat-transcript">';
/* Split the content to get individual chat rows. */
$chat_rows = preg_split( "/(\r?\n)+|(<br\s*\/?>\s*)+/", $content );
/* Loop through each row and format the output. */
foreach ( $chat_rows as $chat_row ) {
/* If a speaker is found, create a new chat row with speaker and text. */
if ( strpos( $chat_row, $separator ) ) {
/* Split the chat row into author/text. */
$chat_row_split = explode( ':', trim( $chat_row ), 2 );
/* Get the chat author and strip tags. */
$chat_author = strip_tags( trim( $chat_row_split[0] ) );
/* Get the chat text. */
$chat_text = trim( $chat_row_split[1] );
/* Get the chat row ID (based on chat author) to give a specific class to each row for styling. */
$speaker_id = cakifo_format_chat_row_id( $chat_author );
/* Open the chat row. */
$chat_output .= "\n\t\t\t\t" . '<div class="chat-row ' . sanitize_html_class( "chat-speaker-{$speaker_id}" ) . '">';
/* Add the chat row author. */
$chat_output .= "\n\t\t\t\t\t" . '<div class="chat-author ' . sanitize_html_class( strtolower( "chat-author-{$chat_author}" ) ) . ' vcard"><cite class="fn">' . apply_filters( 'post_format_chat_author', $chat_author, $speaker_id ) . '</cite>' . $separator . '</div>';
/* Add the chat row text. */
$chat_output .= "\n\t\t\t\t\t" . '<div class="chat-text">' . str_replace( array( "\r", "\n", "\t" ), '', apply_filters( 'post_format_chat_text', $chat_text, $chat_author, $speaker_id ) ) . '</div>';
/* Close the chat row. */
$chat_output .= "\n\t\t\t\t" . '</div><!-- .chat-row -->';
}
/**
* If no author is found, assume this is a separate paragraph of text that belongs to the
* previous speaker and label it as such, but let's still create a new row.
*/
else {
/* Make sure we have text. */
if ( !empty( $chat_row ) ) {
/* Open the chat row. */
$chat_output .= "\n\t\t\t\t" . '<div class="chat-row ' . sanitize_html_class( "chat-speaker-{$speaker_id}" ) . '">';
/* Don't add a chat row author. The label for the previous row should suffice. */
/* Add the chat row text. */
$chat_output .= "\n\t\t\t\t\t" . '<div class="chat-text">' . str_replace( array( "\r", "\n", "\t" ), '', apply_filters( 'post_format_chat_text', $chat_row, $chat_author, $speaker_id ) ) . '</div>';
/* Close the chat row. */
$chat_output .= "\n\t\t\t</div><!-- .chat-row -->";
}
}
}
/* Close the chat transcript div. */
$chat_output .= "\n\t\t\t</div><!-- .chat-transcript -->\n";
/* Return the chat content and apply filters for developers. */
return apply_filters( 'post_format_chat_content', $chat_output );
}
/**
* This function returns an ID based on the provided chat author name. It keeps these IDs in a global
* array and makes sure we have a cakifo set of IDs. The purpose of this function is to provide an "ID"
* that will be used in an HTML class for individual chat rows so they can be styled. So, speaker "John"
* will always have the same class each time he speaks. And, speaker "Mary" will have a different class
* from "John" but will have the same class each time she speaks.
*
* @since Cakifo 1.5
* @author David Chandra
* @link http://www.turtlepod.org
* @author Justin Tadlock
* @link http://justintadlock.com
* @copyright Copyright (c) 2012
* @link http://themehybrid.com/support/topic/post-format-chat-suggestion
*
* @global array $_post_format_chat_ids An array of IDs for the chat rows based on the author.
* @param string $chat_author Author of the current chat row.
* @return int The ID for the chat row based on the author.
*/
function cakifo_format_chat_row_id( $chat_author ) {
global $_post_format_chat_ids;
/* Let's sanitize the chat author to avoid craziness and differences like "John" and "john". */
$chat_author = strtolower( strip_tags( $chat_author ) );
/* Add the chat author to the array. */
$_post_format_chat_ids[] = $chat_author;
/* Make sure the array only holds cakifo values. */
$_post_format_chat_ids = array_unique( $_post_format_chat_ids );
/* Return the array key for the chat author and add "1" to avoid an ID of "0". */
return absint( array_search( $chat_author, $_post_format_chat_ids ) ) + 1;
}
/**
* Retrieves embedded videos from the post content. This script only searches for embeds used by
* the WordPress embed functionality.
*
* @since Cakifo 1.5
* @author Justin Tadlock
* @link http://justintadlock.com
*
* @global object $wp_embed The global WP_Embed object.
* @return string
*/
function cakifo_get_video_embed() {
global $wp_embed;
/* If this is not a 'video' post, return. */
if ( ! has_post_format( 'video' ) )
return false;
/* Get the post content. */
$content = get_the_content();
/* Set the default $embed variable to false. */
$embed = false;
/* Use WP's built in WP_Embed class methods to handle the dirty work. */
add_filter( 'cakifo_video_shortcode_embed', array( $wp_embed, 'run_shortcode' ) );
add_filter( 'cakifo_video_auto_embed', array( $wp_embed, 'autoembed' ) );
/* We don't want to return a link when an embed doesn't work. Filter this to return false. */
add_filter( 'embed_maybe_make_link', '__return_false' );
/* Check for matches against the [embed] shortcode. */
preg_match_all( '|\[embed.*?](.*?)\[/embed\]|i', $content, $matches, PREG_SET_ORDER );
/* If matches were found, loop through them to see if we can hit the jackpot. */
if ( is_array( $matches ) ) {
foreach ( $matches as $value ) {
/* Apply filters (let WP handle this) to get an embedded video. */
$embed = apply_filters( 'cakifo_video_shortcode_embed', '[embed]' . $value[1]. '[/embed]' );
/* If no embed, continue looping through the array of matches. */
if ( empty( $embed ) )
continue;
}
}
/* If no embed at this point and the user has 'auto embeds' turned on, let's check for URLs in the post. */
if ( empty( $embed ) && get_option( 'embed_autourls' ) ) {
preg_match_all( '|^\s*(https?://[^\s"]+)\s*$|im', $content, $matches, PREG_SET_ORDER );
/* If URL matches are found, loop through them to see if we can get an embed. */
if ( is_array( $matches ) ) {
foreach ( $matches as $value ) {
/* Let WP work its magic with the 'autoembed' method. */
$embed = apply_filters( 'cakifo_video_auto_embed', $value[0] );
/* If no embed, continue looping through the array of matches. */
if ( empty( $embed ) )
continue;
}
}
}
/* Remove the maybe make link filter. */
remove_filter( 'embed_maybe_make_link', '__return_false' );
/* Return the embed. */
return $embed;
}
/**
* Grabs the first URL from the post content of the current post. This is meant to be used with the link post
* format to easily find the link for the post.
*
* @since Cakifo 1.5
* @return string The link if found. Otherwise, the permalink to the post.
*
* @note This is a modified version of the twentyeleven_url_grabber() function in the TwentyEleven theme.
* @author wordpressdotorg
* @copyright Copyright (c) 2012, wordpressdotorg
* @link http://wordpress.org/extend/themes/twentyeleven
* @license http://wordpress.org/about/license
*/
function cakifo_url_grabber() {
if ( ! preg_match( '/<a\s[^>]*?href=[\'"](.+?)[\'"]/is', make_clickable( get_the_content() ), $matches ) )
return get_permalink( get_the_ID() );
return esc_url_raw( $matches[1] );
}
/**
* Returns the number of images attached to the current post in the loop.
*
* @since Cakifo 1.5
* @return int
*/
function cakifo_get_image_attachment_count() {
$images = get_children( array( 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => -1 ) );
return count( $images );
}
/**
* Checks if a post has any content.
*
* @since Cakifo 1.5
* @param int $id The ID of the post.
* @return bool Whether the post has content.
*/
function cakifo_post_has_content( $id = 0 ) {
$post = get_post( $id );
return ( !empty( $post->post_content ) ? true : false );
}
?>
Jump to Line
Something went wrong with that request. Please try again.