home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Plugins / articles / articles.php next >
Encoding:
PHP Script  |  2007-09-25  |  7.0 KB  |  257 lines

  1. <?php
  2. /*
  3. Plugin Name: Articles
  4. Plugin URI: http://alexking.org/projects/wordpress
  5. Description: Display posts in an 'Articles' list. To include a post in the list, add a custom field to the post: 'article' = '1'. Set your <a href="options-general.php?page=articles.php">Articles Options here</a>. Questions on configuration, etc.? Make sure to read the README.
  6. Version: 1.2
  7. Author: Alex King
  8. Author URI: http://alexking.org
  9. */ 
  10.  
  11. load_plugin_textdomain('alexking.org');
  12.  
  13. function aka_install() {
  14.     add_option('aka_columns', '1');
  15.     add_option('aka_token', '1');
  16. }
  17.  
  18. if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
  19.     if (get_option('aka_columns') == '') {
  20.         aka_install();
  21.     }
  22. }
  23.  
  24. function aka_request_handler() {
  25.     if (isset($_POST['ak_action'])) {
  26.         switch ($_POST['ak_action']) {
  27.             case 'aka_update_settings':
  28.                 aka_update_settings();
  29.                 break;
  30.         }
  31.     }
  32. }
  33. add_action('init', 'aka_request_handler');
  34.  
  35. function aka_head() {
  36.     if (get_option('aka_columns') == 2) {
  37.         print('
  38. <style type="text/css">
  39. div.aka_half {
  40.     float: left;
  41.     overflow: auto;
  42.     width: 45%;
  43. }
  44. div.aka_clear {
  45.     clear: both;
  46.     float: none;
  47. }
  48. </style>
  49.         ');
  50.     }
  51. }
  52. add_action('wp_head', 'aka_head');
  53.  
  54. function aka_update_settings() {
  55.     if (isset($_POST['aka_columns'])) {
  56.         update_option('aka_columns', intval($_POST['aka_columns']));
  57.     }
  58.     if (isset($_POST['aka_token'])) {
  59.         update_option('aka_token', intval($_POST['aka_token']));
  60.     }
  61.     header('Location: '.get_bloginfo('wpurl').'/wp-admin/options-general.php?page=articles.php&updated=true');
  62.     die();
  63. }
  64.  
  65. function aka_get_articles() {
  66.     global $wpdb, $post;
  67.     
  68.     $original_post = $post;
  69.     
  70.     $columns = get_option('aka_columns');
  71.  
  72.     $now = gmdate("Y-m-d H:i:s",time());
  73.     $posts_data = $wpdb->get_results("
  74.         SELECT $wpdb->posts.*
  75.         FROM $wpdb->posts
  76.         LEFT JOIN $wpdb->postmeta
  77.         ON $wpdb->posts.ID = $wpdb->postmeta.post_id
  78.         WHERE $wpdb->posts.post_status = 'publish'
  79.         AND $wpdb->posts.post_date_gmt < '$now'
  80.         AND $wpdb->postmeta.meta_key = 'article' AND $wpdb->postmeta.meta_value = '1'
  81.         GROUP BY $wpdb->posts.ID
  82.         ORDER BY $wpdb->posts.post_date_gmt DESC
  83.     ");
  84.  
  85.     if (!$posts_data || count($posts_data) == 0) {
  86.         return '';
  87.     }
  88.     
  89.     $posts = array();
  90.     $post_ids = array();
  91.     
  92.     foreach ($posts_data as $post) {
  93.         $posts['post_'.$post->ID] = $post;
  94.         $post_ids[] = $post->ID;
  95.     }
  96.  
  97.     $cats = $wpdb->get_results("
  98.         SELECT $wpdb->term_relationships.object_id, $wpdb->terms.term_id, $wpdb->terms.name, $wpdb->terms.slug
  99.         FROM $wpdb->term_relationships
  100.         LEFT JOIN $wpdb->terms
  101.         ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->terms.term_id
  102.         LEFT JOIN $wpdb->term_taxonomy
  103.         ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id
  104.         WHERE $wpdb->term_relationships.object_id IN (".implode(',', $post_ids).")
  105.         AND $wpdb->term_taxonomy.taxonomy = 'category'
  106.         ORDER BY $wpdb->terms.slug, $wpdb->term_relationships.object_id DESC
  107.     ");
  108.  
  109.     $output = '';
  110.     $current = '';
  111.     $open = false;
  112.     $i = 0;
  113.  
  114.     if ($columns == '2') {
  115.         $half = 0;
  116.         $next = false;
  117.         $output .= '
  118.         <div class="aka_half">
  119.         ';
  120.     }
  121.  
  122.     foreach ($cats as $cat) {
  123.         $slug = $cat->slug;
  124.         if ($current != $slug) {
  125.             if ($next) {
  126.                 $output .= '
  127.             </ul>
  128.         </div>
  129.         <div class="aka_half">
  130.             ';
  131.                 $half++;
  132.                 $open = false;
  133.                 $next = false;
  134.             }
  135.             if ($i > 0 && $open) {
  136.                 $output .= '
  137.                 </ul>
  138.                 ';
  139.             }
  140.             $output .= '
  141.                 <h3 id="cat_'.$slug.'">'.$cat->name.'</h3>
  142.                 <ul>
  143.             ';
  144.             $open = true;
  145.         }
  146.         $current = $slug;
  147.         $post = $posts['post_'.$cat->object_id];
  148.         $output .= '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>'."\n";
  149.         $i++;
  150.     
  151.         if ($columns == '2' && $half == 0 && $i > count($cats) / 2) {
  152.             $next = true;
  153.         }
  154.     }
  155.  
  156.     if ($open) {
  157.         $output .= '
  158.             </ul>
  159.         ';
  160.     }
  161.     
  162.     if ($columns == '2') {
  163.         $output .= '
  164.         </div>
  165.         <div class="aka_clear"></div>
  166.         ';
  167.     }
  168.  
  169.     $post = $original_post;
  170.  
  171.     return $output;
  172. }
  173.  
  174. function aka_show_articles() {
  175.     print(aka_get_articles());
  176. }
  177.  
  178. function aka_the_content($content) {
  179.     if (strstr($content, '###articles###')) {
  180.         $content = str_replace('###articles###', aka_get_articles(), $content);
  181.     }
  182.     return $content;
  183. }
  184. if (get_option('aka_token') == '1') {
  185.     add_action('the_content', 'aka_the_content');
  186. }
  187.  
  188. function aka_the_excerpt($content) {
  189.     return str_replace('###articles###', '', $content);;
  190. }
  191. if (get_option('aka_token') == '1') {
  192.     add_action('the_excerpt', 'aka_the_excerpt');
  193. }
  194.  
  195.  
  196. function aka_options_form() {
  197.     if (get_option('aka_columns') == '2') {
  198.         $column_options = '<option value="1">1</option><option selected="selected" value="2">2</option>';
  199.     }
  200.     else {
  201.         $column_options = '<option value="1" selected="selected">1</option><option value="2">2</option>';
  202.     }
  203.     if (get_option('aka_token') == '0') {
  204.         $token_options = '<option value="1">'.__('Yes', 'alexking.org').'</option><option value="0" selected="selected">'.__('No', 'alexking.org').'</option>';
  205.     }
  206.     else {
  207.         $token_options = '<option value="1" selected="selected">'.__('Yes', 'alexking.org').'</option><option value="0">'.__('No', 'alexking.org').'</option>';
  208.     }
  209.     print('
  210.         <div class="wrap">
  211.             <h2>'.__('Articles Options', 'alexking.org').'</h2>
  212.             <form name="ak_articles" action="'.get_bloginfo('wpurl').'/wp-admin/options-general.php" method="post">
  213.                 <fieldset class="options">
  214.                     <p>
  215.                         <label for="aka_columns">'.__('List articles in how many columns:', 'alexking.org').'</label>
  216.                         <select name="aka_columns" id="aka_columns">'.$column_options.'</select>
  217.                     </p>
  218.                     <p>
  219.                         <label for="aka_token">'.__('Enable <a href="#token">token method</a> for showing the articles list:', 'alexking.org').'</label>
  220.                         <select name="aka_token" id="aka_token">'.$token_options.'</select>
  221.                     </p>
  222.                     <input type="hidden" name="ak_action" value="aka_update_settings" />
  223.                 </fieldset>
  224.                 <p class="submit">
  225.                     <input type="submit" name="submit_buttom" value="'.__('Update Articles Settings', 'alexking.org').'" />
  226.                 </p>
  227.             </form>
  228.             <h2>'.__('Adding Articles to Your List', 'alexking.org').'</h2>
  229.             <p>'.__('To add post to your Articles list, simply add a custom field to the post:', 'alexking.org').'</p>
  230.             <ul>
  231.                 <li>'.__('Key: <strong>article</strong>', 'alexking.org').'</li>
  232.                 <li>'.__('Value: <strong>1</strong>', 'alexking.org').'</li>
  233.             </ul>
  234.             <h2>'.__('Showing the Articles List', 'alexking.org').'</h2>
  235.             <h3 id="token">'.__('Token Method', 'alexking.org').'</h3>
  236.             <p>'.__('If you have enabled the token method above, you can simply add <strong>###articles###</strong> to any post or page and your articles list will be inserted at that place in the post/page.', 'alexking.org').'</p>
  237.             <h3 id="template">'.__('Template Tag Method', 'alexking.org').'</h3>
  238.             <p>'.__('You can always add a template tag to your theme (in a page template perhaps) to show your articles list.', 'alexking.org').'</p>
  239.             <p><strong><code><php aka_show_articles(); ?></code></strong></p>
  240.         </div>
  241.     ');
  242. }
  243.  
  244. function aka_options() {
  245.     if (function_exists('add_options_page')) {
  246.         add_options_page(
  247.             __('Articles Options', 'alexking.org')
  248.             , __('Articles', 'alexking.org')
  249.             , 10
  250.             , basename(__FILE__)
  251.             , 'aka_options_form'
  252.         );
  253.     }
  254. }
  255. add_action('admin_menu', 'aka_options');
  256.  
  257. ?>