Edit Settings. Version: 2.0 Author: April Hodge Silver Author URI: http://springthistle.com/ License: GPL2 Copyright 2011 April Hodge Silver (email : april@springthistle.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. 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. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ /* * ahscp_callposts_handler() * gets the post_content & title of the most recent published post in given category * to be used via shortcode only [ahscp_callposts] * @param string shortcode attributes * @returns string with post title and content */ $ahscpsett = array(); function ahscp_callposts_handler($atts) { $out = ''; global $post, $ahscpsett; extract(shortcode_atts(array( 'type' => null, 'category' => null, 'custom_field'=>null, 'title'=>null, 'numberposts'=>1, 'class'=>'post', 'sidebox'=>null, 'sideboxsize'=>'small', 'content_style'=>null, 'separator'=>', ', 'cols'=>1, 'col_item_height'=>260, 'col_item_width'=>320, 'showthumb'=>false, 'thumbsize'=>'medium', 'order'=>'ASC', 'dateformat'=>null, 'orderby'=>'post_date', 'continue_text'=>'Read more »', 'pre_continue_text'=>'', 'linktitle'=>'true', 'template'=>0, 'post_type'=>'post', ), $atts)); // for backwards compatibility if (empty($type)) $type = $category; if ($separator=='
  • ') $separator = 'list'; // order by default or by custom field? // let shortcode override options default if (empty($custom_field)) $custom_field = get_option('ahscp_customfield'); if (empty($title)) $title = get_option('ahscp_titletype'); if (empty($orderby)) $orderby = get_option('ahscp_orderby'); if (empty($content_style)) $content_style = get_option('ahscp_contentstyle'); if (!$dateformat) $dateformat = get_option('ahscp_dateformat'); if (empty($title)) $title = 'h3'; if (preg_match('/[0-9]*/',$thumbsize)) { $thumbsize = preg_split('/,/',$thumbsize); } // globalize options $ahscpsett['continue_text'] = $continue_text; $ahscpsett['pre_continue_text'] = $pre_continue_text; $ahscpsett['dateformat'] = $dateformat; $ahscpsett['title'] = $title; $ahscpsett['class'] = $class; $ahscpsett['content_style'] = $content_style; $ahscpsett['showthumb'] = $showthumb; $ahscpsett['thumbsize'] = $thumbsize; $ahscpsett['separator'] = $separator; $ahscpsett['linktitle'] = $linktitle; $ahscpsett['col_item_height'] = $col_item_height; $ahscpsett['template'] = $template; // if $category/$type has commas, turn it into a list of IDs $catids = ahscp_get_cats_array(); $category_ids = ""; if (!empty($type)) { if (preg_match('/,/',$type)) { $cnames = preg_split("/,[ ]*/",$type); $ahscpsett['single_cat_name'] = $cnames[0]; foreach ($cnames as $n) $category_ids .= $catids[$n].','; } else { $category_ids = $catids[$type]; $ahscpsett['single_cat_name'] = $type; } } // get two lists of posts. the first ordered by the custom field, the second those without the custom field if (!empty($custom_field)) $posts1 = get_posts('numberposts='.$numberposts.'&category='.$category_ids.'&meta_key='.$custom_field.'&orderby=meta_value&order='.$order.'&post_type='.$post_type); $posts2 = get_posts('numberposts='.$numberposts.'&category='.$category_ids.'&orderby='.$orderby.'&order='.$order.'&post_type='.$post_type); if (!empty($custom_field)) { list($out1,$used_ids) = ahscp_spit_posts($posts1); list($out2,$discard) = ahscp_spit_posts($posts2,$used_ids); $out = $out1.$out2; } else { list($out,$discard) = ahscp_spit_posts($posts2); } if (!$ahscpsett['template']) { if ($ahscpsett['content_style']=='title' && strstr('list',$ahscpsett['separator'])) { $out = ''; } if ($sidebox!=null) { $sizes = array('small'=>'210','medium'=>'300','large'=>'350','xlarge'=>'425'); $out = '
    '.$out.'
    '; } if ($cols > 1) { $style = ''; $out = $style.'
    '.$out.'
     
    '; } } else { if (get_option('ahscp__tmpl_'.$ahscpsett['template'].'_group')==1) $out = ''; } return '
    '.$out.'
    '; } /* * creates a string with post title and content, in a div * used by ahscp_callposts_handler * @param array of wordpress posts * @param HTML object to use for the title * @param name of class to apply to div * @param optional array of post IDs to exclude * @returns string with post title and content for all posts in the array */ function ahscp_spit_posts($posts,$exclude=array()) { $used_ids = array(); $out = ''; global $more, $ahscpsett; $urlpath = WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__)); foreach($posts as $post) : setup_postdata($post); $more = 0; if (!in_array($post->ID,$exclude)) { // Using a template? if ($ahscpsett['template']) { if (!empty($post->post_excerpt)) $excerpt = $post->post_excerpt; else $excerpt = ahscp_excerpt($post->post_content); $content = get_the_content($ahscpsett['continue_text']); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); $content = preg_replace('/Read more »<\/a>/','Read more »',$content); if ($url = get_edit_post_link($post->ID)) $editlink = ' Edit'; $tmpl = stripslashes(get_option('ahscp_tmpl_'.$ahscpsett['template'].'_text')); $tmpl = preg_replace('/%%TITLE%%/',$post->post_title,$tmpl); $tmpl = preg_replace('/%%URL%%/',get_permalink($post->ID),$tmpl); $tmpl = preg_replace('/%%IMAGE%%/',get_the_post_thumbnail($post->ID, $ahscpsett['thumbsize']),$tmpl); $tmpl = preg_replace('/%%DATE%%/',get_the_time($ahscpsett['dateformat'],$post->ID),$tmpl); $tmpl = preg_replace('/%%CONTENT%%/',$content,$tmpl); $tmpl = preg_replace('/%%EXCERPT%%/',$excerpt,$tmpl); $tmpl = preg_replace('/%%EDITLINK%%/',$editlink,$tmpl); $tmpl = preg_replace('/%%CATEGORY%%/',$ahscpsett['single_cat_name'],$tmpl); $out .= $tmpl; } // Not using a template else if ($ahscpsett['content_style'] != 'title') { $out .= '
    '; $out .= '<'.$ahscpsett['title'].'>'; if (!empty($ahscpsett['dateformat'])) $out .= get_the_time($ahscpsett['dateformat'],$post->ID).' - '; // post title if ($ahscpsett['linktitle']!='false') $out .= ''; $out .= $post->post_title; if ($ahscpsett['linktitle']!='false') $out .= ''; // edit link if ($url = get_edit_post_link($post->ID)) $out .= ' Edit'; $out .= ''; if ($ahscpsett['showthumb']==true) { $out .= get_the_post_thumbnail($post->ID, $ahscpsett['thumbsize']); } if ($ahscpsett['content_style']=='excerpt') { if (!empty($post->post_excerpt)) $excerpt = $post->post_excerpt; // else $excerpt = get_the_excerpt(); else $excerpt = ahscp_excerpt($post->post_content); $out .= '

    '.$excerpt.' '.$ahscpsett['pre_continue_text'].''.$ahscpsett['continue_text'].'

    '; } else { // default content_style=full $content = get_the_content($ahscpsett['continue_text']); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); $content = preg_replace('/Read more »<\/a>/','Read more »',$content); if ($ahscpsett['showthumb']==true) $content = strip_tags($content,'



    '); $out .= $content; } $out .= '

    '; // end
    (or whatever) } else { if (strstr('list',$ahscpsett['separator'])) { $out .= '
  • '.''.$post->post_title.''.'
  • '."\n"; } else { $out .= ''.$post->post_title.''.$ahscpsett['separator']; } } $used_ids[]=$post->ID; } endforeach; $toreturn = array($out,$used_ids); return $toreturn; } add_shortcode('ahs_callposts', 'ahscp_callposts_handler'); /* * ahscp_get_cats_array() * gets an array of categories ID=>NAME * @returns the array of categories */ function ahscp_get_cats_array() { global $wpdb; $sql = "SELECT tt.term_id, t.slug FROM ".$wpdb->prefix."term_taxonomy tt, ".$wpdb->prefix."terms t WHERE tt.taxonomy LIKE 'category' AND tt.term_id=t.term_id ORDER BY t.slug"; $result = $wpdb->get_results($sql); $catids=array(); foreach ($result as $i) { $catids[$i->slug]=$i->term_id; } return $catids; } /* * ahscp_excerpt() * returns a string that is an excerpt of the post content. replaces get_the_excerpt() which was causing problems * @param string post_content * @returns string excerpt */ function ahscp_excerpt($text) { $text = strip_tags($text); $text = preg_replace('/\[([^[])+\]/','',$text); $text = preg_replace("/\n|\r/"," ",$text); $text = ahscp_substrws($text,450); return $text; } /* Thanks, Benny. http://www.php.net/manual/en/function.substr.php#90724 */ /** * word-sensitive substring function with html tags awareness * @param text The text to cut * @param len The maximum length of the cut string * @returns string **/ function ahscp_substrws( $text, $len=180 ) { if ( (strlen($text) > $len) ) { $whitespaceposition = strpos($text," ",$len)-1; if ($whitespaceposition > 0) $text = substr($text, 0, ($whitespaceposition+1)); // close unclosed html tags if (preg_match_all("|<([a-zA-Z]+)>|",$text,$aBuffer) ) { if( !empty($aBuffer[1]) ) { preg_match_all("||",$text,$aBuffer2); if( count($aBuffer[1]) != count($aBuffer2[1]) ) { foreach( $aBuffer[1] as $index => $tag ) { if( empty($aBuffer2[1][$index]) || $aBuffer2[1][$index] != $tag) $text .= ''; } } } } } return $text; } // this functions adds the CSS to the head function ahscp_callposts_styles() { echo "\n\n"; echo '"; echo "\n\n\n"; } add_action('wp_head', 'ahscp_callposts_styles'); require('ahs_callposts_admin.php'); ?>