Advertisement
Guest User

custom.lib.php (ta)

a guest
Jan 31st, 2011
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.15 KB | None | 0 0
  1. <?php
  2. /**
  3. *   Custom function to get current URL
  4. **/
  5. function curPageURL() {
  6.     $pageURL = 'http';
  7.     if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  8.     $pageURL .= "://";
  9.     if ($_SERVER["SERVER_PORT"] != "80") {
  10.      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  11.     } else {
  12.      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  13.     }
  14.     return $pageURL;
  15. }
  16.    
  17. function pp_debug($arr)
  18. {
  19.     echo '<pre>';
  20.     print_r($arr);
  21.     echo '</pre>';
  22. }
  23.  
  24. function gen_pagination($total,$currentPage,$baseLink,$nextPrev=true,$limit=10)
  25. {
  26.     if(!$total OR !$currentPage OR !$baseLink)
  27.     {
  28.         return false;
  29.     }
  30.  
  31.     //Total Number of pages
  32.     $totalPages = ceil($total/$limit);
  33.      
  34.     //Text to use after number of pages
  35.     //$txtPagesAfter = ($totalPages==1)? " page": " pages";
  36.      
  37.     //Start off the list.
  38.     //$txtPageList = '<br />'.$totalPages.$txtPagesAfter.' : <br />';
  39.      
  40.     //Show only 3 pages before current page(so that we don't have too many pages)
  41.     $min = ($page - 3 < $totalPages && $currentPage-3 > 0) ? $currentPage-3 : 1;
  42.      
  43.     //Show only 3 pages after current page(so that we don't have too many pages)
  44.     $max = ($page + 3 > $totalPages) ? $totalPages : $currentPage+3;
  45.      
  46.     //Variable for the actual page links
  47.     $pageLinks = "";
  48.    
  49.     $baseLinkArr = parse_url($baseLink);
  50.     $start = '';
  51.    
  52.     if(isset($baseLinkArr['query']) && !empty($baseLinkArr['query']))
  53.     {
  54.         $start = '&';
  55.     }
  56.     else
  57.     {
  58.         $start = '?';
  59.     }
  60.      
  61.     //Loop to generate the page links
  62.     for($i=$min;$i<=$max;$i++)
  63.     {
  64.         if($currentPage==$i)
  65.         {
  66.             //Current Page
  67.             $pageLinks .= '<a href="#" class="active">'.$i.'</a>';  
  68.         }
  69.         elseif($max <= $totalPages OR $i <= $totalPages)
  70.         {
  71.             $pageLinks .= '<a href="'.$baseLink.$start.'page='.$i.'" class="slide">'.$i.'</a>';
  72.         }
  73.     }
  74.      
  75.     if($nextPrev)
  76.     {
  77.         //Next and previous links
  78.         $next = ($currentPage + 1 > $totalPages) ? false : '<a href="'.$baseLink.$start.'page='.($currentPage + 1).'" class="slide">Next</a>';
  79.          
  80.         $prev = ($currentPage - 1 <= 0 ) ? false : '<a href="'.$baseLink.$start.'page='.($currentPage - 1).'" class="slide">Previous</a>';
  81.     }
  82.      
  83.     if($totalPages > 1)
  84.     {
  85.         return '<br class="clear"/><div class="pagination">'.$txtPageList.$prev.$pageLinks.$next.'</div>';
  86.     }
  87.     else
  88.     {
  89.         return '';
  90.     }
  91.      
  92. }
  93.  
  94. function count_shortcode($content = '')
  95. {
  96.     $return = array();
  97.    
  98.     if(!empty($content))
  99.     {
  100.         $pattern = get_shortcode_regex();
  101.         $count = preg_match_all('/'.$pattern.'/s', $content, $matches);
  102.        
  103.         $return['total'] = $count;
  104.        
  105.         if(isset($matches[0]))
  106.         {
  107.             foreach($matches[0] as $match)
  108.             {
  109.                 $return['content'][] = substr_replace($match ,"",-1);
  110.             }
  111.         }
  112.     }
  113.    
  114.     return $return;
  115. }
  116.  
  117. function pp_breadcrumbs() {
  118.  
  119.   $delimiter = '&raquo;';
  120.   $name = get_bloginfo('name'); //text for the 'Home' link
  121.   $currentBefore = '<span class="current">';
  122.   $currentAfter = '</span>';
  123.  
  124.   if ( !is_home() && !is_front_page() || is_paged() ) {
  125.  
  126.     echo '<div id="crumbs">';
  127.  
  128.     global $post;
  129.     $home = get_bloginfo('url');
  130.     echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . ' ';
  131.  
  132.     if ( is_category() ) {
  133.       global $wp_query;
  134.       $cat_obj = $wp_query->get_queried_object();
  135.       $thisCat = $cat_obj->term_id;
  136.       $thisCat = get_category($thisCat);
  137.       $parentCat = get_category($thisCat->parent);
  138.       if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
  139.       echo $currentBefore . 'Archive by category &#39;';
  140.       single_cat_title();
  141.       echo '&#39;' . $currentAfter;
  142.  
  143.     } elseif ( is_day() ) {
  144.       echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
  145.       echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
  146.       echo $currentBefore . get_the_time('d') . $currentAfter;
  147.  
  148.     } elseif ( is_month() ) {
  149.       echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
  150.       echo $currentBefore . get_the_time('F') . $currentAfter;
  151.  
  152.     } elseif ( is_year() ) {
  153.       echo $currentBefore . get_the_time('Y') . $currentAfter;
  154.  
  155.     } elseif ( is_single() && !is_attachment() ) {
  156.       $cat = get_the_category(); $cat = $cat[0];
  157.       echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
  158.       echo $currentBefore;
  159.       the_title();
  160.       echo $currentAfter;
  161.  
  162.     } elseif ( is_attachment() ) {
  163.       $parent = get_post($post->post_parent);
  164.       $cat = get_the_category($parent->ID); $cat = $cat[0];
  165.       echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
  166.       echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a> ' . $delimiter . ' ';
  167.       echo $currentBefore;
  168.       the_title();
  169.       echo $currentAfter;
  170.  
  171.     } elseif ( is_page() && !$post->post_parent ) {
  172.       echo $currentBefore;
  173.       the_title();
  174.       echo $currentAfter;
  175.  
  176.     } elseif ( is_page() && $post->post_parent ) {
  177.       $parent_id  = $post->post_parent;
  178.       $breadcrumbs = array();
  179.       while ($parent_id) {
  180.         $page = get_page($parent_id);
  181.         $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
  182.         $parent_id  = $page->post_parent;
  183.       }
  184.       $breadcrumbs = array_reverse($breadcrumbs);
  185.       foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
  186.       echo $currentBefore;
  187.       the_title();
  188.       echo $currentAfter;
  189.  
  190.     } elseif ( is_search() ) {
  191.       echo $currentBefore . 'Search results for &#39;' . get_search_query() . '&#39;' . $currentAfter;
  192.  
  193.     } elseif ( is_tag() ) {
  194.       echo $currentBefore . 'Posts tagged &#39;';
  195.       single_tag_title();
  196.       echo '&#39;' . $currentAfter;
  197.  
  198.     } elseif ( is_author() ) {
  199.        global $author;
  200.       $userdata = get_userdata($author);
  201.       echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;
  202.  
  203.     } elseif ( is_404() ) {
  204.       echo $currentBefore . 'Error 404' . $currentAfter;
  205.     }
  206.  
  207.     if ( get_query_var('paged') ) {
  208.       if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
  209.       echo __('Page') . ' ' . get_query_var('paged');
  210.       if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
  211.     }
  212.  
  213.     echo '</div>';
  214.  
  215.   }
  216. }
  217.    
  218. /**
  219. *   Setup blog comment style
  220. **/
  221. function pp_comment($comment, $args, $depth)
  222. {
  223.     $GLOBALS['comment'] = $comment; ?>
  224.    
  225.     <div class="comment" id="comment-<?php comment_ID() ?>">
  226.         <div class="left">
  227.             <?php echo get_avatar($comment,$size='50',$default='<path_to_url>' ); ?>
  228.         </div>
  229.      
  230.  
  231.         <div class="right">
  232.             <?php if ($comment->comment_approved == '0') : ?>
  233.                 <em><?php _e('(Your comment is awaiting moderation.)') ?></em>
  234.                 <br />
  235.             <?php endif; ?>
  236.            
  237.             <strong><i><?php echo $comment->comment_author; ?></i></strong>
  238.             <?php ' '.comment_text() ?>
  239.             <p class="comment-reply-link"><?php comment_reply_link(array_merge( $args, array('depth' => $depth,
  240. 'reply_text' => '
  241. Reply', 'login_text' => 'Log in to reply to this', 'max_depth' => $args['max_depth']))) ?></p>
  242.  
  243.         </div>
  244.     </div>
  245. <?php
  246. }
  247.  
  248. function pp_ago($timestamp){
  249.    $difference = time() - $timestamp;
  250.    $periods = array("second", "minute", "hour", "day", "week", "month", "years", "decade");
  251.    $lengths = array("60","60","24","7","4.35","12","10");
  252.    for($j = 0; $difference >= $lengths[$j]; $j++)
  253.    $difference /= $lengths[$j];
  254.    $difference = round($difference);
  255.    if($difference != 1) $periods[$j].= "s";
  256.    $text = "$difference $periods[$j] ago";
  257.    return $text;
  258. }
  259.  
  260.  
  261. // Substring without losing word meaning and
  262. // tiny words (length 3 by default) are included on the result.
  263. // "..." is added if result do not reach original string length
  264.  
  265. function pp_substr($str, $length, $minword = 3)
  266. {
  267.     $sub = '';
  268.     $len = 0;
  269.    
  270.     foreach (explode(' ', $str) as $word)
  271.     {
  272.         $part = (($sub != '') ? ' ' : '') . $word;
  273.         $sub .= $part;
  274.         $len += strlen($part);
  275.        
  276.         if (strlen($word) > $minword && strlen($sub) >= $length)
  277.         {
  278.             break;
  279.         }
  280.     }
  281.    
  282.     return $sub . (($len < strlen($str)) ? '...' : '');
  283. }
  284.  
  285.  
  286. /**
  287. *   Setup recent posts widget
  288. **/
  289. function pp_posts($sort = 'recent', $items = 3, $echo = TRUE, $bg_color = 'black' , $echo_title = TRUE)
  290. {
  291.     $return_html = '';
  292.    
  293.     if($sort == 'recent')
  294.     {
  295.         $posts = get_posts('numberposts='.$items.'&order=DESC&orderby=date&post_type=post&post_status=publish');
  296.         $title = 'Recent Posts';
  297.     }
  298.     else
  299.     {
  300.         global $wpdb;
  301.        
  302.         $query = "SELECT ID, post_title, post_content FROM {$wpdb->prefix}posts WHERE post_type = 'post' AND post_status= 'publish' ORDER BY comment_count DESC LIMIT 0,".$items;
  303.         $posts = $wpdb->get_results($query);
  304.         $title = 'Popular Posts';
  305.     }
  306.    
  307.     if(!empty($posts))
  308.     {
  309.         if($echo_title)
  310.         {
  311.             $return_html.= '<h2 class="widgettitle">'.$title.'</h2>';
  312.         }
  313.        
  314.         $return_html.= '<ul class="posts blog '.$bg_color.'_wrapper">';
  315.  
  316.             foreach($posts as $post)
  317.             {
  318.                 $image_thumb = '';
  319.                                
  320.                 if(has_post_thumbnail($post->ID, 'large'))
  321.                 {
  322.                     $image_id = get_post_thumbnail_id($post->ID);
  323.                     $image_thumb = wp_get_attachment_image_src($image_id, 'large', true);
  324.                 }
  325.                
  326.                 $return_html.= '<li>';
  327.                
  328.                 if(!empty($image_thumb))
  329.                 {
  330.                     $return_html.= '<a href="'.get_permalink($post->ID).'"><img src="'.get_bloginfo( 'stylesheet_directory' ).'/timthumb.php?src='.$image_thumb[0].'&h=75&w=75&zc=1" alt="" class="frame" /></a>';
  331.                 }
  332.                 $return_html.= '<strong class="header"><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></strong><br/>';
  333.                 $return_html.= pp_substr(strip_tags(strip_shortcodes($post->post_content)), 80).'</li>';
  334.  
  335.             }  
  336.  
  337.         $return_html.= '</ul>';
  338.  
  339.     }
  340.    
  341.     if($echo)
  342.     {
  343.         echo $return_html;
  344.     }
  345.     else
  346.     {
  347.         return $return_html;
  348.     }
  349. }
  350.  
  351. function _substr($str, $length, $minword = 3)
  352. {
  353.     $sub = '';
  354.     $len = 0;
  355.    
  356.     foreach (explode(' ', $str) as $word)
  357.     {
  358.         $part = (($sub != '') ? ' ' : '') . $word;
  359.         $sub .= $part;
  360.         $len += strlen($part);
  361.        
  362.         if (strlen($word) > $minword && strlen($sub) >= $length)
  363.         {
  364.             break;
  365.         }
  366.     }
  367.    
  368.     return $sub . (($len < strlen($str)) ? '...' : '');
  369. }
  370.  
  371. function get_the_content_with_formatting ($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
  372.  
  373.     $pp_blog_read_more_title = get_option('pp_blog_read_more_title');      
  374.     if(empty($pp_blog_read_more_title))
  375.     {
  376.         $pp_blog_read_more_title = 'Read More';
  377.     }
  378.  
  379.     $content = get_the_content('', $stripteaser, $more_file);
  380.     $content = strip_shortcodes($content);
  381.     $content = str_replace(']]>', ']]&gt;', $content);
  382.     $content = _substr(strip_tags(strip_shortcodes($content)), 600);
  383.     $content.= '<br/><br/><br/><a class="button" href="'.get_permalink().'">'.$pp_blog_read_more_title.'</a>';
  384.     return $content;
  385. }
  386.  
  387. function image_from_description($data) {
  388.     preg_match_all('/<img src="([^"]*)"([^>]*)>/i', $data, $matches);
  389.     return $matches[1][0];
  390. }
  391.  
  392.  
  393. function select_image($img, $size) {
  394.     $img = explode('/', $img);
  395.     $filename = array_pop($img);
  396.  
  397.     // The sizes listed here are the ones Flickr provides by default.  Pass the array index in the
  398.  
  399.     // 0 for square, 1 for thumb, 2 for small, etc.
  400.     $s = array(
  401.         '_s.', // square
  402.         '_t.', // thumb
  403.         '_m.', // small
  404.         '.',   // medium
  405.         '_b.'  // large
  406.     );
  407.  
  408.     $img[] = preg_replace('/(_(s|t|m|b))?\./i', $s[$size], $filename);
  409.     return implode('/', $img);
  410. }
  411.  
  412.  
  413. function get_flickr($settings) {
  414.     if (!function_exists('MagpieRSS')) {
  415.         // Check if another plugin is using RSS, may not work
  416.         include_once (ABSPATH . WPINC . '/class-simplepie.php');
  417.         error_reporting(E_ERROR);
  418.     }
  419.    
  420.     if(!isset($settings['items']) || empty($settings['items']))
  421.     {
  422.         $settings['items'] = 9;
  423.     }
  424.    
  425.     // get the feeds
  426.     if ($settings['type'] == "user") { $rss_url = 'http://api.flickr.com/services/feeds/photos_public.gne?id=' . $settings['id'] . '&tags=' . $settings['tags'] . '&per_page='.$settings['items'].'&format=rss_200'; }
  427.     elseif ($settings['type'] == "favorite") { $rss_url = 'http://api.flickr.com/services/feeds/photos_faves.gne?id=' . $settings['id'] . '&format=rss_200'; }
  428.     elseif ($settings['type'] == "set") { $rss_url = 'http://api.flickr.com/services/feeds/photoset.gne?set=' . $settings['set'] . '&nsid=' . $settings['id'] . '&format=rss_200'; }
  429.     elseif ($settings['type'] == "group") { $rss_url = 'http://api.flickr.com/services/feeds/groups_pool.gne?id=' . $settings['id'] . '&format=rss_200'; }
  430.     elseif ($settings['type'] == "public" || $settings['type'] == "community") { $rss_url = 'http://api.flickr.com/services/feeds/photos_public.gne?tags=' . $settings['tags'] . '&format=rss_200'; }
  431.     else {
  432.         print '<strong>No "type" parameter has been setup. Check your settings, or provide the parameter as an argument.</strong>';
  433.         die();
  434.     }
  435.     # get rss file
  436.  
  437.     $feed = new SimplePie($rss_url);
  438.     $photos_arr = array();
  439.    
  440.     foreach ($feed->get_items() as $key => $item)
  441.     {
  442.         $enclosure = $item->get_enclosure();
  443.         $img = image_from_description($item->get_description());
  444.         $thumb_url = select_image($img, 0);
  445.         $large_url = select_image($img, 4);
  446.        
  447.         $photos_arr[] = array(
  448.             'title' => $enclosure->get_title(),
  449.             'thumb_url' => $thumb_url,
  450.             'url' => $large_url,
  451.         );
  452.        
  453.         $current = intval($key+1);
  454.        
  455.         if($current == $settings['items'])
  456.         {
  457.             break;
  458.         }
  459.     }  
  460.  
  461.     return $photos_arr;
  462. }
  463.  
  464. function html2rgb($color)
  465. {
  466.     if ($color[0] == '#')
  467.         $color = substr($color, 1);
  468.  
  469.     if (strlen($color) == 6)
  470.         list($r, $g, $b) = array($color[0].$color[1],
  471.                                  $color[2].$color[3],
  472.                                  $color[4].$color[5]);
  473.     elseif (strlen($color) == 3)
  474.         list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
  475.     else
  476.         return false;
  477.  
  478.     $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
  479.  
  480.     return array($r, $g, $b);
  481. }
  482.  
  483. function hex_lighter($hex,$factor = 30)
  484.     {
  485.     $new_hex = '';
  486.      
  487.     $base['R'] = hexdec($hex{0}.$hex{1});
  488.     $base['G'] = hexdec($hex{2}.$hex{3});
  489.     $base['B'] = hexdec($hex{4}.$hex{5});
  490.      
  491.     foreach ($base as $k => $v)
  492.         {
  493.         $amount = 255 - $v;
  494.         $amount = $amount / 100;
  495.         $amount = round($amount * $factor);
  496.         $new_decimal = $v + $amount;
  497.      
  498.         $new_hex_component = dechex($new_decimal);
  499.         if(strlen($new_hex_component) < 2)
  500.             { $new_hex_component = "0".$new_hex_component; }
  501.         $new_hex .= $new_hex_component;
  502.         }
  503.          
  504.     return $new_hex;    
  505. }
  506.  
  507. function hex_darker($hex,$factor = 30)
  508. {
  509.         $new_hex = '';
  510.        
  511.         $base['R'] = hexdec($hex{0}.$hex{1});
  512.         $base['G'] = hexdec($hex{2}.$hex{3});
  513.         $base['B'] = hexdec($hex{4}.$hex{5});
  514.        
  515.         foreach ($base as $k => $v)
  516.                 {
  517.                 $amount = $v / 100;
  518.                 $amount = round($amount * $factor);
  519.                 $new_decimal = $v - $amount;
  520.        
  521.                 $new_hex_component = dechex($new_decimal);
  522.                 if(strlen($new_hex_component) < 2)
  523.                         { $new_hex_component = "0".$new_hex_component; }
  524.                 $new_hex .= $new_hex_component;
  525.                 }
  526.                
  527.         return $new_hex;        
  528. }
  529.  
  530. function theme_queue_js(){
  531.   if (!is_admin()){
  532.     if (!is_page() AND is_singular() AND comments_open() AND (get_option('thread_comments') == 1)) {
  533.       wp_enqueue_script( 'comment-reply' );
  534.     }
  535.   }
  536. }
  537. add_action('get_header', 'theme_queue_js');
  538.  
  539. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement