This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Nigels yafpp.php file

By: a guest on Oct 26th, 2011  |  syntax: PHP  |  size: 21.06 KB  |  views: 83  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Plugin Name: Yet Another Featured Posts Plugin
  4. Plugin URI: http://jonraasch.com/blog/yet-another-featured-posts-plugin
  5. Description: A plugin to manage and display a list of featured posts
  6. Version: 1.4
  7. Author: Jon Raasch
  8. Author URI: http://jonraasch.com/
  9. Documentation: http://dev.jonraasch.com/yafpp/docs
  10. */
  11.  
  12. /*  Copyright 2009-2010 Jon Raasch - Released under the FreeBSD License - See http://dev.jonraasch.com/yafpp/docs#licensing for more info
  13.  
  14. This has been altered by Nigel 28/05/2011
  15. */
  16. // return either an array or HTML formatted list of featured posts
  17. function get_featured_posts($opts = array(
  18.     'before' => '<div>',
  19.     'after'  => '</div>',
  20.     'method' => 'echo',
  21. )) {
  22.     global $post;
  23.     $yafpp_opts = get_option('yafpp_opts');
  24.    
  25.     // posts_id from database
  26.     $featured = $yafpp_opts['featured_posts'];
  27.    
  28.     if ( isset( $opts['excerpt_length'] ) ) $yafpp_opts['excerpt_length'] = $opts['excerpt_length'];
  29.     // if max_posts is 0 set to -1 (0 is deprecated)
  30.     if ( isset( $opts['max_posts'] ) ) $yafpp_opts['max_posts'] = $opts['max_posts'] ? $opts['max_posts'] : -1;
  31.     if ( isset( $opts['no_posts_text'] ) ) $yafpp_opts['no_posts_text'] = $opts['no_posts_text'];
  32.    
  33.     $out = $opts['method'] == 'arr' ? array() : '';
  34.    
  35.     if($featured) {
  36.      
  37.         $featured_arr = explode(',',$featured);
  38.        
  39.         // if method is the_loop, set it up and die
  40.         if ( $opts['method'] == 'the_loop' ) {
  41.             $query_opts = apply_filters('yafpp_get_featured_posts_query', array(
  42.                 'post__in' => $featured_arr,
  43.                 'posts_per_page' => $yafpp_opts['max_posts'],
  44.             ));
  45.             if ( isset( $opts['post_type'] ) ) $query_opts['post_type'] = $opts['post_type'];
  46.             query_posts($query_opts);
  47.            
  48.             return $featured_arr;
  49.         }
  50.        
  51.         function new_excerpt_length($length) {
  52.             $yafpp_opts = get_option('yafpp_opts');
  53.             return $yafpp_opts['excerpt_length'];
  54.         }
  55.        
  56.         $query_opts = apply_filters('yafpp_get_featured_posts_query',array(
  57.             'post__in'          => $featured_arr,
  58.             'posts_per_page'    => $yafpp_opts['max_posts'],
  59.         ));
  60.         if ( isset( $opts['post_type'] ) ) $query_opts['post_type'] = $opts['post_type'];
  61.         $featured_query = new WP_Query($query_opts);
  62.        
  63.         while ($featured_query->have_posts()) {
  64.             $featured_query->the_post();
  65.            
  66.             add_filter('excerpt_length', 'new_excerpt_length');
  67.            
  68.             // define some vars
  69.             $the_title = get_the_title();
  70.             $the_permalink = get_permalink();
  71.             $the_excerpt = get_the_excerpt();
  72.                   $the_image = get_the_post_thumbnail( get_the_ID(), 'thumbnail' ); // Nigel here
  73.          
  74.             if ($opts['method'] == 'arr') {
  75.                 $the_post = array(
  76.                     'id'      => get_the_ID(),
  77.                     'title'   => '<h3>'.$the_title.'</h3>',
  78.                     'excerpt' => $the_excerpt,
  79.                     'url' => $the_permalink,
  80.                     'image' => $the_image,
  81.                     'author' => get_the_author(),
  82.                 );
  83.                 array_push( $out, $the_post);
  84.             }
  85.             else {
  86.                 $out .= $opts['before'];
  87.                
  88.                 $out .= $yafpp_opts['display_title'] ? '<a href="' . $the_permalink . '" rel="bookmark" title="Permanent Link: ' . $the_title . '">' . '<h3>'.$the_title.'</h3>' . '</a><br />' : '';
  89.                
  90.                 $out .= ($the_image) ? '<a href="' . $the_permalink . '" rel="bookmark" title="Permanent Link: ' . $the_title . '" class="yafpp-img">' . $the_image . '</a><br />' : '';
  91.                 $out .= $yafpp_opts['display_excerpt'] ? $the_excerpt : '';
  92.                
  93.                 $out .= $opts['after'];
  94.             }
  95.         } # end while WP Loop
  96.        
  97.     }  
  98.     else if ( $opts['method'] != 'arr' ) $out .= $opts['before'] . $yafpp_opts['no_posts_text'] . $opts['after'];
  99.    
  100.     if ($opts['method'] == 'echo') echo $out;
  101.     else return $out;
  102. }
  103.  
  104. // load the YAFPP Settings stuff
  105. function yafpp_menu_item() {
  106.     $yafpp_opts = get_option('yafpp_opts');
  107.    
  108.     // Add new menu in Setting or Options tab:
  109.     add_options_page('YAFPP - Yet Another Featured Posts Plugin', 'Featured Posts (YAFPP)', $yafpp_opts['admin_level'], 'YAFPP', 'yafpp_admin');
  110. }
  111.  
  112. // YAFPP admin page
  113. function yafpp_admin() {
  114.     global $ol_flash, $current_user;
  115.     get_currentuserinfo();
  116.    
  117.     $yafpp_opts = get_option('yafpp_opts');
  118.  
  119.     if ($_POST['change_featured']) {
  120.         $featuredStr = '';
  121.        
  122.         // parse featured array into string
  123.         if ( $_POST['featured_posts'] ) {
  124.             foreach ( $_POST['featured_posts'] as $post_id=>$val ) $featuredStr .= $post_id . ',';
  125.             $featuredStr = substr($featuredStr, 0, -1);
  126.         }
  127.        
  128.         if ( $yafpp_opts['featured_posts'] != $featuredStr ) {
  129.             $yafpp_opts['featured_posts'] = $featuredStr;
  130.            
  131.             update_option('yafpp_opts', $yafpp_opts);
  132.            
  133.             $ol_flash = "Your featured posts have been saved.";
  134.         }
  135.         else $ol_flash = "Please use the checkboxes to remove any currently featured posts.  To feature new posts <a href=\"edit.php\">go here</a>";
  136.     }
  137.    
  138.     // process posted options
  139.     else if (isset($_POST['excerpt_length']) && $current_user->allcaps['level_10']) {
  140.         // set other options
  141.         $yafpp_opts['display_title'] = $_POST['display_title'] ? 1 : 0;
  142.         $yafpp_opts['display_excerpt'] = $_POST['display_excerpt'] ? 1 : 0;
  143.         $yafpp_opts['feature_pages'] = $_POST['feature_pages'] ? 1 : 0;
  144.         $yafpp_opts['display_image'] = $_POST['display_image'] ? 1 : 0;
  145.        
  146.         $yafpp_opts['photo_height'] = (int) $_POST['photo_height'];
  147.         $yafpp_opts['photo_width'] = (int) $_POST['photo_width'];
  148.        
  149.         $yafpp_opts['excerpt_length'] = (int) $_POST['excerpt_length'];
  150.         $yafpp_opts['max_posts'] = (int) $_POST['max_posts'];
  151.         $yafpp_opts['no_posts_text']  = $_POST['no_posts_text'];
  152.        
  153.         if ( $_POST['admin_level']) $yafpp_opts['admin_level'] = (int) $_POST['admin_level'];
  154.        
  155.         update_option('yafpp_opts',$yafpp_opts);
  156.        
  157.         $ol_flash = "Your YAFPP settings have been saved.";
  158.     }
  159.    
  160.    
  161.     // build the page output
  162.     $out = '';
  163.    
  164.     if ($ol_flash) $out .= '<div id="message"class="updated fade"><p>' . $ol_flash . '</p></div>';
  165.    
  166.     $out .= '<div class="wrap">';
  167.     $out .= '<h2>Yet Another Featured Posts Plugin (YAFPP) Settings</h2>';
  168.    
  169.     $out .= '<p><a href="http://jonraasch.com/blog/yet-another-featured-posts-plugin" target="_blank">Plugin Homepage</a> | <a href="http://dev.jonraasch.com/yafpp/docs" target="_blank">Documentation</a> | <a href="http://dev.jonraasch.com/yafpp/changelog" target="_blank">Changelog</a> | <a href="javascript:document.getElementById(\'donate_form\').submit()">Donate</a></p>';
  170.    
  171.     $out .= '<form action="" method="post"><input type="hidden" name="change_featured" value="1" />';
  172.    
  173.     // featured posts form
  174.     $out .= '<h3>Currently Featured Posts</h3>';
  175.     $out .= '<p><em>Manage featured posts - uncheck to unfeature</em></p>';
  176.    
  177.     $out .= '<table class="widefat">
  178.    <thead><tr><th>Post Title</th><th>Author</th><th>Date</th><th>Featured</th></tr></thead><tbody>';
  179.    
  180.     $featured_arr = explode(',', $yafpp_opts['featured_posts']);
  181.    
  182.     // loop through the posts,
  183.     query_posts(apply_filters('yafpp_get_featured_posts_query_admin',array(
  184.         'post__in' => $featured_arr,
  185.         'posts_per_page' => $yafpp_opts['max_posts']
  186.     )));
  187.    
  188.     $count = 0;
  189.     if ( have_posts() ) : while ( have_posts() ) : the_post();
  190.         $postID = get_the_ID();
  191.        
  192.         if ( in_array($postID, $featured_arr) ) {
  193.             $count++;
  194.            
  195.             $out .= '<tr' . ($count % 2 ? ' class="alternate"' : '') . '><td><strong>' . get_the_title() . '</strong></td><td>By ' . get_the_author() . '</td><td>' . get_the_time('l, F j. Y') . '</td><td><input type="checkbox" name="featured_posts[' . $postID . ']" value="1" checked /></td></tr>';        
  196.         }
  197.     endwhile;
  198.     else : $out .= '<tr><td colspan="4">No posts have been featured - Please add featured posts using the <a href="edit.php">Edit Posts</a> page</td></tr>';
  199.     endif;
  200.    
  201.     $out .= '</tbody><tfoot><tr><th>Post Title</th><th>Author</th><th>Date</th><th>Featured</th></tr></tfoot></table>';
  202.    
  203.     if ($count) $out .= '<div class="submit"><input type="submit" value="Update Featured Posts" /></div></form>';
  204.     else $out .= '<br />';
  205.    
  206.     // only admin may edit YAFPP settings
  207.     if ($current_user->allcaps['level_10']) {
  208.         $out .= '<hr /><form action="" method="post">';
  209.        
  210.         // display settings
  211.         $out .= '<h3>Display Settings</h3>';
  212.        
  213.         $out .= '<p><em>Select which parts of the listing you want to display</em></p>';
  214.        
  215.         $out .= '<table class="optiontable form-table">';
  216.        
  217.         $out .= '<tr><td><input type="checkbox" name="display_title" ' . ($yafpp_opts['display_title'] ? 'checked ' : '') . '/> &nbsp; <strong>Post Title</strong></td></tr>';
  218.        
  219.         $out .= '<tr><td><input type="checkbox" name="display_excerpt" ' . ($yafpp_opts['display_excerpt'] ? 'checked ' : '') . '/> &nbsp; <strong>Post Excerpt</strong></td></tr>';
  220.        
  221.         // photo options
  222.         if ( function_exists('yapb_is_photoblog_post') ) {
  223.             $out .= '<tr><td><input type="checkbox" name="display_image" ' . ($yafpp_opts['display_image'] ? 'checked ' : '') . '/> &nbsp; <strong>Post Image (from YAPB)</strong></td></tr>';
  224.            
  225.             $out .= '<tr><td><h4>Max Photo Dimensions</h4><strong>Width:</strong> <input type="text" name="photo_width" value="' . $yafpp_opts['photo_width'] . '" /> <strong>Height:</strong> <input type="text" name="photo_height" value="' . $yafpp_opts['photo_height'] . '" /></td></tr>';
  226.         }
  227.         $out .= '</table>';
  228.        
  229.         // other settings fields
  230.         $out .= '<h3>Other Settings</h3>
  231.        <table class="optiontable form-table">';
  232.        
  233.         $out .= '<tr><td><input type="checkbox" name="feature_pages" ' . ($yafpp_opts['feature_pages'] ? 'checked ' : '') . '/> &nbsp; <strong>Allow Pages to be featured</strong></td></tr>';
  234.  
  235.         $out .= '<tr><td><strong>Excerpt Length (# of words):</strong></td><td><input type="text" name="excerpt_length" value="' . (int) $yafpp_opts['excerpt_length'] . '" size="50%" /></td></tr>';
  236.        
  237.         $out .= '<tr><td><strong>Max # of featured posts (<code>-1</code> for unlimited):</strong></td><td><input type="text" name="max_posts" value="' . (int) $yafpp_opts['max_posts'] . '" size="50%" /></td></tr>';
  238.        
  239.         $out .= '<tr><td><strong>No Posts Text:</strong></td><td><input type="text" name="no_posts_text" value="' . htmlentities($yafpp_opts['no_posts_text']) . '" size="50%" /></td></tr>';
  240.        
  241.         $out .= '</table>';
  242.    
  243.         // permission settings
  244.         $out .= '<h3>Permission Settings</h3>
  245.        <table class="optiontable form-table">
  246.        <tr><td><strong>Minimum User Level to control Featured Posts</strong></td><td><select name="admin_level">
  247.            <option value="10"' . ($yafpp_opts['admin_level'] == 10 ? ' selected' : '' ) . '>Admin only (10)</option>
  248.            <option value="7"' . ($yafpp_opts['admin_level'] == 7 ? ' selected' : '' ) . '>Editor and Admin (7+)</option>
  249.            <option value="2"' . ($yafpp_opts['admin_level'] == 2 ? ' selected' : '' ) . '>Author, Editor and Admin (2+)</option>
  250.            <option value="1"' . ($yafpp_opts['admin_level'] == 1 ? ' selected' : '' ) . '>Contributor, Author, Editor and Admin (1+)</option>
  251.            <option value="0"' . ($yafpp_opts['admin_level'] == 0 ? ' selected' : '' ) . '>All registered users (not recommended) (0+)</option>
  252.        </select></td></tr></table>
  253.        ';
  254.        
  255.         $out .= '<div class="submit"><input type="submit" value="Save Settings" /></div></form>';
  256.     }
  257.    
  258.     // donations
  259.    
  260.     $out .= '<hr />
  261.    <h2>Do you like this plugin?</h2>
  262.    <p>Please consider making a donation.  I\'m a free-lance web developer, and your donations make these plugins possible.</p>
  263.    <form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="donate_form">
  264.        <input type="hidden" name="cmd" value="_s-xclick">
  265.        <input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----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-----END PKCS7-----
  266.        ">
  267.        <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
  268.        <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
  269.    </form>
  270.    <br />';
  271.  
  272.     $out .= '</div>';
  273.  
  274.     echo $out;
  275. }
  276.  
  277. // add featured column to edit posts listing
  278. function yafpp_add_posts_column($defaults) {
  279.     global $current_user;
  280.     get_currentuserinfo();
  281.    
  282.     $yafpp_opts = get_option('yafpp_opts');
  283.    
  284.     if ($current_user->allcaps['level_' . $yafpp_opts['admin_level']]) {
  285.         $defaults['Featured'] = __('Featured');
  286.     }
  287.    
  288.     return $defaults;
  289. }
  290.  
  291. // build the post listing cta for each row
  292. function yafpp_posts_column($column_name, $id) {
  293.     if ( $column_name == 'Featured' ) {
  294.         $root = get_option('home');
  295.         $yafpp_dir = $root . '/wp-content/plugins/yet-another-featured-posts-plugin';
  296.        
  297.         $yafpp_opts = get_option('yafpp_opts');
  298.        
  299.         $featured_arr = explode(',', $yafpp_opts['featured_posts']);
  300.        
  301.         if (in_array( $id, $featured_arr )) {
  302.             $class = 'class="yafpp_on"';
  303.         }
  304.         else $class = 'class="yafpp_off"';
  305.        
  306.         echo '<div id="yafpp_' .$id . '" onclick="yafpp_ajax_feature(' . $id . ')" ' . $class . '></div><img src="' . $yafpp_dir . '/img/loading.gif" alt="" class="yafpp_loading" />';
  307.     }
  308. }
  309.  
  310. // add css for post listings UI
  311. function yafpp_css() {
  312.     $script_path = substr( __FILE__, 0, -10);
  313.  
  314.     $yafpp_dir = get_option('siteurl') . '/wp-content/plugins' . substr( $script_path, strrpos( $script_path, '/')) . '/';
  315.    
  316.     $out = <<<EOT
  317. <!-- YAFPP ( Yet Another Featured Post Plugin ) -->
  318.  
  319. <style type="text/css">
  320. .yafpp_on, .yafpp_off {
  321.     background: url('$yafpp_dir/img/stars.png') no-repeat 0 0;
  322.     width: 16px;
  323.     height: 16px;
  324.     cursor: pointer;
  325. }
  326.  
  327. .yafpp_off {
  328.     background-position: -16px 0;
  329. }
  330.  
  331. td.loading .yafpp_on, td.loading .yafpp_off {
  332.     display: none;
  333. }
  334.  
  335. .yafpp_loading {
  336.     display: none;
  337. }
  338.  
  339. td.loading .yafpp_loading {
  340.     display: inline;
  341. }
  342. </style>
  343.  
  344. <!-- end YAFPP -->
  345. EOT;
  346.  
  347.     echo $out;
  348. }
  349.  
  350. // append JS for processing edit posts page
  351. function yafpp_ajax_feature_js() {
  352.     // use JavaScript SACK library for Ajax
  353.     wp_print_scripts( array( 'sack' ));
  354.     ?>
  355.     <script type="text/javascript">
  356.     //<![CDATA[
  357.     function yafpp_ajax_feature(postId) {
  358.         document.getElementById('yafpp_' + postId).parentNode.className = 'loading';
  359.        
  360.         var mysack = new sack("<?php bloginfo( 'wpurl' ); ?>/wp-admin/admin-ajax.php");
  361.         var theBox = document.getElementById('yafpp_' + postId);
  362.        
  363.         mysack.execute = 1;
  364.         mysack.method = 'POST';
  365.         mysack.setVar("action", "yafpp_process");
  366.         mysack.setVar("id", postId);
  367.         mysack.setVar("is_on", (theBox.className == 'yafpp_on' ? 1 : 0));
  368.         mysack.encVar("cookie", document.cookie, false);
  369.         mysack.onError = function() { alert('Error featuring, please try again.' )};
  370.         mysack.runAJAX();
  371.    
  372.     return true;
  373.     }
  374.     //]]>
  375.     </script>
  376. <?php
  377. }
  378.  
  379. // backend ajax processing for edit posts page
  380. function yafpp_process_feature() {
  381.     global $current_user;
  382.     get_currentuserinfo();
  383.    
  384.     $yafpp_opts = get_option('yafpp_opts');
  385.    
  386.     // boot if they don't have edit rights
  387.     if (! $current_user->allcaps['level_' . $yafpp_opts['admin_level']]) die( "alert('Sorry, you do not have the correct permissions');");
  388.    
  389.     // read submitted information
  390.     $id = $_POST['id'];
  391.     $is_on = $_POST['is_on'];
  392.    
  393.     $featured_arr = $yafpp_opts['featured_posts'] ? explode(',', $yafpp_opts['featured_posts']) : array();
  394.    
  395.     // add to array if not on and not currently in the array
  396.     if (! $is_on && ! in_array($id, $featured_arr)) array_push($featured_arr, $id);
  397.    
  398.     rsort($featured_arr);
  399.    
  400.     $featured_str = '';
  401.     foreach ( $featured_arr as $post_id ) {
  402.         // if not the same as selected, add to the featured str
  403.         if (! ($is_on && $post_id == $id)) $featured_str .= $post_id . ',';
  404.     }
  405.    
  406.     if ($featured_str) $featured_str = substr($featured_str, 0, -1);
  407.    
  408.     $yafpp_opts['featured_posts'] = $featured_str;
  409.     update_option('yafpp_opts', $yafpp_opts);
  410.    
  411.     // Compose JavaScript for return
  412.     die( "var thisStar = document.getElementById('yafpp_$id'); thisStar.className = '" . ($is_on ? 'yafpp_off' : 'yafpp_on' ) . "'; thisStar.parentNode.className = ''");
  413. }
  414.  
  415. /***** init *****/
  416.  
  417. $yafpp_opts = get_option('yafpp_opts');
  418.  
  419. // set defaults if first time
  420. if (is_null($yafpp_opts['no_posts_text'])) {
  421.     $yafpp_opts['display_title'] = 1;
  422.     $yafpp_opts['display_excerpt'] = 1;
  423.     $yafpp_opts['feature_pages'] = 0;
  424.     $yafpp_opts['photo_width'] = 300;
  425.     $yafpp_opts['photo_height'] = 240;
  426.     $yafpp_opts['excerpt_length'] = 20;
  427.     $yafpp_opts['max_posts'] = 0;
  428.     $yafpp_opts['no_posts_text'] = 'No featured posts';
  429.     $yafpp_opts['admin_level'] = 7;
  430.    
  431.     update_option('yafpp_opts',$yafpp_opts);
  432. }
  433.  
  434. // add admin menu item
  435. add_action('admin_menu', 'yafpp_menu_item');
  436.  
  437. // add column to post listings
  438. add_filter('manage_posts_columns', 'yafpp_add_posts_column');
  439. add_filter('manage_posts_custom_column', 'yafpp_posts_column', 10, 2);
  440.  
  441. // add ajax processing stuff
  442. add_action('wp_ajax_yafpp_process', 'yafpp_process_feature' );
  443. add_action('admin_print_scripts', 'yafpp_ajax_feature_js' );
  444. add_action('admin_head', 'yafpp_css');
  445.  
  446. // add Page support
  447. if($yafpp_opts['feature_pages']) {
  448.     add_filter('manage_pages_columns', 'yafpp_add_posts_column');
  449.     add_filter('manage_pages_custom_column', 'yafpp_posts_column', 10, 2);
  450.     add_filter('yafpp_get_featured_posts_query','yafpp_add_pages_to_query_opts');
  451.     add_filter('yafpp_get_featured_posts_query_admin','yafpp_add_pages_to_query_opts');
  452. }
  453. function yafpp_add_pages_to_query_opts($query_opts) {
  454.     $query_opts['post_type'] = array('page','post');
  455.     return $query_opts;
  456. }
  457.  
  458. ?>
  459.  
clone this paste RAW Paste Data