File: /home/mmickelson/theflexguys.com/baronconstruct/wp-content/themes/p2/functions.php
<?php
/**
* @package WordPress
* @subpackage P2
*/
require_once( 'inc/utils.php' );
p2_maybe_define( 'P2_INC_PATH', get_template_directory() . '/inc' );
p2_maybe_define( 'P2_INC_URL', get_template_directory_uri() . '/inc' );
p2_maybe_define( 'P2_JS_PATH', get_template_directory() . '/js' );
p2_maybe_define( 'P2_JS_URL', get_template_directory_uri() . '/js' );
class P2 {
/**
* DB version.
*
* @var int
*/
var $db_version = 1;
/**
* Options.
*
* @var array
*/
var $options = array();
/**
* Option name in DB.
*
* @var string
*/
var $option_name = 'p2_manager';
/**
* Components.
*
* @var array
*/
var $components = array();
/**
* Includes and instantiates the various P2 components.
*/
function P2() {
// Fetch options
$this->options = get_option( $this->option_name );
if ( false === $this->options )
$this->options = array();
// Include the P2 components
$includes = array( 'compat', 'terms-in-comments', 'js-locale',
'mentions', 'search', 'js', 'options-page',
'template-tags', 'widgets/recent-tags', 'widgets/recent-comments',
'list-creator' );
if ( defined('DOING_AJAX') && DOING_AJAX )
$includes[] = 'ajax';
foreach ( $includes as $name ) {
require_once( P2_INC_PATH . "/$name.php" );
}
// Add the default P2 components
$this->add( 'mentions', 'P2_Mentions' );
$this->add( 'search', 'P2_Search' );
// Bind actions
add_action( 'init', array( &$this, 'init' ) );
add_action( 'admin_init', array( &$this, 'maybe_upgrade_db' ) );
}
function init() {
// Load language pack
load_theme_textdomain( 'p2', get_template_directory() . '/languages' );
}
/**
* Will upgrade the database if necessary.
*
* When upgrading, triggers actions:
* 'p2_upgrade_db_version'
* 'p2_upgrade_db_version_$number'
*
* Flushes rewrite rules automatically on upgrade.
*/
function maybe_upgrade_db() {
if ( ! isset( $this->options['db_version'] ) || $this->options['db_version'] < $this->db_version ) {
$current_db_version = isset( $this->options['db_version'] ) ? $this->options['db_version'] : 0;
do_action( 'p2_upgrade_db_version', $current_db_version );
for ( ; $current_db_version <= $this->db_version; $current_db_version++ ) {
do_action( "p2_upgrade_db_version_$current_db_version" );
}
// Flush rewrite rules once, so callbacks don't have to.
flush_rewrite_rules();
$this->set_option( 'db_version', $this->db_version );
$this->save_options();
}
}
/**
* COMPONENTS API
*/
function add( $component, $class ) {
$class = apply_filters( "p2_add_component_$component", $class );
if ( class_exists( $class ) )
$this->components[ $component ] = new $class();
}
function get( $component ) {
return $this->components[ $component ];
}
function remove( $component ) {
unset( $this->components[ $component ] );
}
/**
* OPTIONS API
*/
function get_option( $key ) {
return isset( $this->options[ $key ] ) ? $this->options[ $key ] : null;
}
function set_option( $key, $value ) {
return $this->options[ $key ] = $value;
}
function save_options() {
update_option( $this->option_name, $this->options );
}
}
$GLOBALS['p2'] = new P2;
function p2_get( $component = '' ) {
global $p2;
return empty( $component ) ? $p2 : $p2->get( $component );
}
function p2_get_option( $key ) {
return $GLOBALS['p2']->get_option( $key );
}
function p2_set_option( $key, $value ) {
return $GLOBALS['p2']->set_option( $key, $value );
}
function p2_save_options() {
return $GLOBALS['p2']->save_options();
}
/**
* ----------------------------------------------------------------------------
* NOTE: Ideally, the rest of this file should be moved elsewhere.
* ----------------------------------------------------------------------------
*/
$content_width = '632';
register_sidebar( array(
'name' => __( 'Sidebar', 'p2' ),
) );
// Run make_clickable later to avoid shortcode conflicts
add_filter( 'the_content', 'make_clickable', 12 );
// Content Filters
// Filter to be ran on the_content, calls the do_list function from our class
function p2_list_creator( $content ) {
$list_creator = new P2_List_Creator;
return $list_creator->do_list( $content );
}
// Call the filter on normal, non admin calls (this code exists in ajax.php for the special p2 instances)
if ( ! is_admin() )
add_filter( 'pre_kses', 'p2_list_creator', 1 );
add_filter( 'pre_comment_content', 'p2_list_creator', 1 );
function p2_title( $before = '<h2>', $after = '</h2>', $echo = true ) {
if ( is_page() )
return;
if ( is_single() && false === p2_the_title( '', '', false ) ) { ?>
<h2 class="transparent-title"><?php echo the_title(); ?></h2><?php
return true;
} else {
p2_the_title( $before, $after, $echo );
}
}
/**
* Generate a nicely formatted post title
*
* Ignore empty titles, titles that are auto-generated from the
* first part of the post_content
*
* @package WordPress
* @subpackage P2
* @since 1.0.5
*
* @param string $before content to prepend to title
* @param string $after content to append to title
* @param string $echo echo or return
* @return string $out nicely formatted title, will be boolean(false) if no title
*/
function p2_the_title( $before = '<h2>', $after = '</h2>', $echo = true ) {
global $post;
$temp = $post;
$t = apply_filters( 'the_title', $temp->post_title );
$title = $temp->post_title;
$content = $temp->post_content;
$pos = 0;
$out = '';
// Don't show post title if turned off in options or title is default text
if ( 1 != (int) get_option( 'prologue_show_titles' ) || 'Post Title' == $title )
return false;
$content = trim( $content );
$title = trim( $title );
$title = preg_replace( '/\.\.\.$/', '', $title );
$title = str_replace( "\n", ' ', $title );
$title = str_replace( ' ', ' ', $title);
$content = str_replace( "\n", ' ', strip_tags( $content) );
$content = str_replace( ' ', ' ', $content );
$content = trim( $content );
$title = trim( $title );
// Clean up links in the title
if ( false !== strpos( $title, 'http' ) ) {
$split = @str_split( $content, strpos( $content, 'http' ) );
$content = $split[0];
$split2 = @str_split( $title, strpos( $title, 'http' ) );
$title = $split2[0];
}
// Avoid processing an empty title
if ( '' == $title )
return false;
// Avoid processing the title if it's the very first part of the post content
// Which is the case with most "status" posts
$pos = strpos( $content, $title );
if ( false === $pos || 0 < $pos ) {
if ( is_single() )
$out = $before . $t . $after;
else
$out = $before . '<a href="' . get_permalink( $temp->ID ) . '">' . $t . ' </a>' . $after;
if ( $echo )
echo $out;
else
return $out;
}
return false;
}
function p2_comments( $comment, $args ) {
$GLOBALS['comment'] = $comment;
if ( !is_single() && get_comment_type() != 'comment' )
return;
$depth = prologue_get_comment_depth( get_comment_ID() );
$can_edit_post = current_user_can( 'edit_post', $comment->comment_post_ID );
$reply_link = prologue_get_comment_reply_link(
array( 'depth' => $depth, 'max_depth' => $args['max_depth'], 'before' => ' | ', 'reply_text' => __( 'Reply', 'p2' ) ),
$comment->comment_ID, $comment->comment_post_ID );
$content_class = 'commentcontent';
if ( $can_edit_post )
$content_class .= ' comment-edit';
?>
<li id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
<?php do_action( 'p2_comment' ); ?>
<?php echo get_avatar( $comment, 32 ); ?>
<h4>
<?php echo get_comment_author_link(); ?>
<span class="meta">
<?php echo p2_date_time_with_microformat( 'comment' ); ?>
<span class="actions">
<a href="<?php echo esc_url( get_comment_link() ); ?>"><?php _e( 'Permalink', 'p2' ); ?></a>
<?php
echo $reply_link;
if ( $can_edit_post )
edit_comment_link( __( 'Edit', 'p2' ), ' | ' );
?>
</span>
</span>
</h4>
<div id="commentcontent-<?php comment_ID(); ?>" class="<?php echo esc_attr( $content_class ); ?>"><?php
echo apply_filters( 'comment_text', $comment->comment_content );
if ( $comment->comment_approved == '0' ): ?>
<p><em><?php esc_html_e( 'Your comment is awaiting moderation.', 'p2' ); ?></em></p>
<?php endif; ?>
</div>
<?php
}
function get_tags_with_count( $post, $format = 'list', $before = '', $sep = '', $after = '' ) {
$posttags = get_the_tags($post->ID, 'post_tag' );
if ( !$posttags )
return '';
foreach ( $posttags as $tag ) {
if ( $tag->count > 1 && !is_tag($tag->slug) ) {
$tag_link = '<a href="' . get_term_link($tag, 'post_tag' ) . '" rel="tag">' . $tag->name . ' ( ' . number_format_i18n( $tag->count ) . ' )</a>';
} else {
$tag_link = $tag->name;
}
if ( $format == 'list' )
$tag_link = '<li>' . $tag_link . '</li>';
$tag_links[] = $tag_link;
}
return apply_filters( 'tags_with_count', $before . join( $sep, $tag_links ) . $after, $post );
}
function tags_with_count( $format = 'list', $before = '', $sep = '', $after = '' ) {
global $post;
echo get_tags_with_count( $post, $format, $before, $sep, $after );
}
function p2_title_from_content( $content ) {
$title = p2_excerpted_title( $content, 8 ); // limit title to 8 full words
// Try to detect image or video only posts, and set post title accordingly
if ( empty( $title ) ) {
if ( preg_match("/<object|<embed/", $content ) )
$title = __( 'Video Post', 'p2' );
elseif ( preg_match( "/<img/", $content ) )
$title = __( 'Image Post', 'p2' );
}
return $title;
}
if ( is_admin() && ( false === get_option( 'prologue_show_titles' ) ) ) {
add_option( 'prologue_show_titles', 1);
}
function p2_excerpted_title( $content, $word_count ) {
$content = strip_tags( $content );
$words = preg_split( '/([\s_;?!\/\(\)\[\]{}<>\r\n\t"]|\.$|(?<=\D)[:,.\-]|[:,.\-](?=\D))/', $content, $word_count + 1, PREG_SPLIT_NO_EMPTY );
if ( count( $words ) > $word_count ) {
array_pop( $words ); // remove remainder of words
$content = implode( ' ', $words );
$content = $content . '...';
} else {
$content = implode( ' ', $words );
}
$content = trim( strip_tags( $content ) );
return $content;
}
function p2_fix_empty_titles( $post_ID, $post ) {
// Don't call for anything but normal posts (avoid pages, custom taxonomy, nav menu items)
if ( ! is_object( $post ) || 'post' !== $post->post_type )
return;
if ( empty( $post->post_title ) ) {
$post->post_title = p2_title_from_content( $post->post_content );
$post->post_modified = current_time( 'mysql' );
$post->post_modified_gmt = current_time( 'mysql', 1 );
return wp_update_post( $post );
}
}
add_action( 'save_post', 'p2_fix_empty_titles', 10, 2 );
function p2_add_head_content() {
if ( is_home() && is_user_logged_in() ) {
include_once( ABSPATH . '/wp-admin/includes/media.php' );
}
}
add_action( 'wp_head', 'p2_add_head_content' );
function p2_new_post_noajax() {
if ( empty( $_POST['action'] ) || $_POST['action'] != 'post' )
return;
if ( !is_user_logged_in() )
auth_redirect();
if ( !current_user_can( 'publish_posts' ) ) {
wp_redirect( home_url( '/' ) );
exit;
}
$current_user = wp_get_current_user();
check_admin_referer( 'new-post' );
$user_id = $current_user->ID;
$post_content = $_POST['posttext'];
$tags = $_POST['tags'];
$post_title = p2_title_from_content( $post_content );
$post_id = wp_insert_post( array(
'post_author' => $user_id,
'post_title' => $post_title,
'post_content' => $post_content,
'tags_input' => $tags,
'post_status' => 'publish'
) );
wp_redirect( home_url( '/' ) );
exit;
}
add_filter( 'template_redirect', 'p2_new_post_noajax' );
function iphone_css() {
if ( strstr( $_SERVER['HTTP_USER_AGENT'], 'iPhone' ) or isset($_GET['iphone']) && $_GET['iphone'] ) { ?>
<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
<style type="text/css">
/* <![CDATA[ */
/* iPhone CSS */
<?php $iphonecss = dirname( __FILE__ ) . '/style-iphone.css'; if ( is_file( $iphonecss ) ) require $iphonecss; ?>
/* ]]> */
</style>
<?php } }
add_action( 'wp_head', 'iphone_css' );
/*
Modified to replace query string with blog url in output string
*/
function prologue_get_comment_reply_link( $args = array(), $comment = null, $post = null ) {
global $user_ID;
if ( post_password_required() )
return;
$defaults = array( 'add_below' => 'comment', 'respond_id' => 'respond', 'reply_text' => __( 'Reply', 'p2' ),
'login_text' => __( 'Log in to Reply', 'p2' ), 'depth' => 0, 'before' => '', 'after' => '' );
$args = wp_parse_args($args, $defaults);
if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] )
return;
extract($args, EXTR_SKIP);
$comment = get_comment($comment);
$post = get_post($post);
if ( 'open' != $post->comment_status )
return false;
$link = '';
$reply_text = esc_html( $reply_text );
if ( get_option( 'comment_registration' ) && !$user_ID )
$link = '<a rel="nofollow" href="' . site_url( 'wp-login.php?redirect_to=' . urlencode( get_permalink() ) ) . '">' . esc_html( $login_text ) . '</a>';
else
$link = "<a rel='nofollow' class='comment-reply-link' href='". get_permalink($post). "#" . urlencode( $respond_id ) . "' onclick='return addComment.moveForm(\"" . esc_js( "$add_below-$comment->comment_ID" ) . "\", \"$comment->comment_ID\", \"" . esc_js( $respond_id ) . "\", \"$post->ID\")'>$reply_text</a>";
return apply_filters( 'comment_reply_link', $before . $link . $after, $args, $comment, $post);
}
function prologue_comment_depth_loop( $comment_id, $depth ) {
$comment = get_comment( $comment_id );
if ( isset( $comment->comment_parent ) && 0 != $comment->comment_parent ) {
return prologue_comment_depth_loop( $comment->comment_parent, $depth + 1 );
}
return $depth;
}
function prologue_get_comment_depth( $comment_id ) {
return prologue_comment_depth_loop( $comment_id, 1 );
}
function prologue_comment_depth( $comment_id ) {
echo prologue_get_comment_depth( $comment_id );
}
function prologue_poweredby_link() {
return apply_filters( 'prologue_poweredby_link', sprintf( '<a href="%1$s" rel="generator">%2$s</a>', esc_url( __('http://wordpress.org/', 'p2') ), sprintf( __('Proudly powered by %s.', 'p2'), 'WordPress' ) ) );
}
/* Custom Header Code */
define( 'HEADER_TEXTCOLOR', '3478E3' );
define( 'HEADER_IMAGE', '' ); // %s is theme dir uri
define( 'HEADER_IMAGE_WIDTH', 980);
define( 'HEADER_IMAGE_HEIGHT', 120);
function p2_admin_header_style() {
?>
<style type="text/css">
#headimg {
background: url(<?php header_image(); ?>) repeat;
height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
width:<?php echo HEADER_IMAGE_WIDTH; ?>px;
padding:0 0 0 18px;
}
#headimg a {
height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
width:<?php echo HEADER_IMAGE_WIDTH; ?>px;
}
#headimg h1{
padding-top:40px;
margin: 0;
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: 200;
}
#headimg h1 a {
color:#<?php header_textcolor(); ?>;
text-decoration: none;
border-bottom: none;
font-size: 1.4em;
margin: -0.4em 0 0 0;
}
#headimg #desc{
color:#<?php header_textcolor(); ?>;
font-size:1.1em;
margin-top:1em;
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: 200;
}
<?php if ( 'blank' == get_header_textcolor() ) { ?>
#headimg h1, #headimg #desc {
display: none;
}
#headimg h1 a, #headimg #desc {
color:#<?php echo HEADER_TEXTCOLOR ?>;
}
<?php } ?>
</style>
<?php
}
function p2_header_style() {
?>
<style type="text/css">
<?php if ( '' != get_header_image() ) : ?>
#header {
background: url(<?php header_image(); ?>) repeat;
height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
}
#header a.secondary {
height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
width:<?php echo HEADER_IMAGE_WIDTH; ?>px;
display: block;
position: absolute;
top: 0;
}
#header a.secondary:hover {
border: 0;
}
#header .sleeve {
position: relative;
margin-top: 0;
margin-right: 0;
background-color: transparent;
box-shadow: none !important;
-webkit-box-shadow: none !important;
-moz-box-shadow: none !important;
height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
}
#header {
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2) !important;
-webkit-box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2) !important;
-moz-box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2) !important;
}
<?php endif; ?>
<?php if ( 'blank' == get_header_textcolor() ) { ?>
#header h1, #header small {
padding: 0;
text-indent: -1000em;
}
<?php } else { ?>
#header h1 a, #header small {
color: #<?php header_textcolor(); ?>;
}
<?php } ?>
</style>
<?php
}
add_custom_image_header( 'p2_header_style', 'p2_admin_header_style' );
function p2_background_color() {
$background_color = get_option( 'p2_background_color' );
if ( '' != $background_color ) :
?>
<style type="text/css">
body {
background-color: <?php esc_attr_e( $background_color ); ?>;
}
</style>
<?php endif;
}
add_action( 'wp_head', 'p2_background_color' );
function p2_background_image() {
$p2_background_image = get_option( 'p2_background_image' );
if ( 'none' == $p2_background_image || '' == $p2_background_image )
return false;
?>
<style type="text/css">
body {
background-image: url( <?php echo get_template_directory_uri() . '/i/backgrounds/pattern-' . $p2_background_image . '.png' ?> );
}
</style>
<?php
}
add_action( 'wp_head', 'p2_background_image' );
function p2_hidden_sidebar_css() {
$hide_sidebar = get_option( 'p2_hide_sidebar' );
$sleeve_margin = ( is_rtl() ) ? 'margin-left: 0;' : 'margin-right: 0;';
if ( '' != $hide_sidebar ) :
?>
<style type="text/css">
.sleeve_main { <?php echo $sleeve_margin;?> }
#wrapper { background: transparent; }
#header, #footer, #wrapper { width: 760px; }
</style>
<?php endif;
}
add_action( 'wp_head', 'p2_hidden_sidebar_css' );
// Network signup form
function p2_before_signup_form() {
echo '<div class="sleeve_main"><div id="main">';
}
add_action( 'before_signup_form', 'p2_before_signup_form' );
function p2_after_signup_form() {
echo '</div></div>';
}
add_action( 'after_signup_form', 'p2_after_signup_form' );
// Enable background
add_custom_background();
// Feed me
add_theme_support( 'automatic-feed-links' );