Advertisement
Guest User

Untitled

a guest
May 2nd, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.06 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Post Tiles
  4. Plugin URI: http://www.tincrate.com/plugins
  5. Description: This plugin displays recent posts as tiles. Posts can choose categories by id and numbeer of posts to display. Example shortcode: <strong>[post-tiles]</strong> or <strong>[post-tiles categories="1,2,4,10" posts="8" excerpt="18"]</strong>.
  6. Author: Ethan Hackett - TinCrate
  7. Version: 1.2.5
  8. Author URI: http://www.tincrate.com/plugins
  9.  
  10. Copyright (C) 2012 Ethan Hackett
  11.  
  12.     This program is free software: you can redistribute it and/or modify
  13.     it under the terms of the GNU General Public License as published by
  14.     the Free Software Foundation, either version 3 of the License, or
  15.     (at your option) any later version.
  16.  
  17.     This program is distributed in the hope that it will be useful,
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.     GNU General Public License for more details.
  21.  
  22.     You should have received a copy of the GNU General Public License
  23.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24.  
  25. */
  26. // Checks to See Wordpress Version
  27. global $wp_version;
  28.  
  29. // If it's not compatable with wordpress version 3.0 die and display message.
  30. if ( !version_compare($wp_version,"3.0",">=") )
  31. {
  32.     die("<h2>You need at lease version 3.0 of Wordpress to use the TinCrate plugin.</h2><a href='http:www/wordpress.org' target='_blank'>Visit Wordpress.org to upgrade your current version of Wordpress.");
  33. }
  34.  
  35. // Adds the function for the admin menu
  36. add_action('admin_menu', 'tc_post_tiles_menu');
  37.  
  38. // Creates the admin page and title
  39. function tc_post_tiles_menu() {
  40.     add_options_page('Post Tiles', 'Post Tiles', 'manage_options', 'post-tiles-identifier', 'tc_post_tiles_options');
  41. }
  42.  
  43. // Adds function for declaring options
  44. add_action( 'admin_init', 'register_post_tiles_options' );
  45.  
  46. function register_post_tiles_options(){
  47.    
  48.     // Registers Non Category Related Settings (it's outside the categories loop)
  49.     register_setting( 'tc-plugin-settings', 'category-key');
  50.     register_setting( 'tc-plugin-settings', 'category-key-jquery');
  51.     register_setting( 'tc-plugin-settings', 'featured-images');
  52.    
  53.     // Get all Post Categories
  54.     $categories = get_categories();    
  55.    
  56.     //Loop Through Each Category
  57.     foreach($categories as $category) {
  58.         $cat_var = "cat_".$category->name;
  59.         // Make lowercase
  60.         $cat_var = strtolower($cat_var);
  61.         $cat_var = str_replace(" ", "_", $cat_var);
  62.         $cat = $category->name;
  63.        
  64.         // Creates a registered setting for each category.
  65.         register_setting('tc-plugin-settings', $cat_var);
  66.     }
  67. }
  68.  
  69. // Adds the color picker
  70. add_action('init', 'load_theme_scripts');
  71. function load_theme_scripts() {
  72.     wp_enqueue_style( 'farbtastic' );
  73.     wp_enqueue_script( 'farbtastic' );
  74. }
  75.  
  76. function tc_post_tiles_options() {
  77.    
  78.     // Checks to make sure the viewer is the admin
  79.     if (!current_user_can('manage_options'))  {
  80.         wp_die( __('You do not have sufficient permissions to access this page.') );
  81.     }
  82.     // Start the page output
  83.     // Get the plugin Url to attatch CSS and images etc.
  84.     $plugin_url = plugins_url()."/post-tiles";
  85.     wp_enqueue_style( 'post-tiles', $plugin_url.'/post-tiles.css' );
  86.     wp_enqueue_script('post-tiles', $plugin_url.'/post-tiles.js' );
  87.    
  88.     echo "<div class='wrap'>";
  89.     echo "<div id='icon-options-general' class='icon32'></div>
  90.     <h2>Post Tiles:</h2>
  91.     <h3>How to use Post Tiles:</h3>
  92.         <ul>
  93.             <li>1.) To add posts tiles to a page or post copy and paste the following shortcode. Example: <strong>[post-tiles]</strong></li>
  94.             <li>2.) By default 8 posts are displayed. To change the amount of posts to display, use the <strong>posts=' '</strong> attribute in the shortcode. Example: <strong>[post-tiles posts='10']</strong></li>
  95.             <li>3.) By default all post categories are called for the tiles. To specify the categories use the <strong>categories=' '</strong> attribute separating them by commas. Example: <strong>[post-tiles categories='1,2,4']</strong></li>
  96.             <li><div id='tc-note'><em><strong>NOTE:</strong> The category id numbers are listed below, next to the category names. You can use both the categories and posts attributes Example: <strong>[post-tiles categories='1,2,4' posts='8']</strong></em></div></li>
  97.             </ul>
  98.     <div id='color-container'>     
  99.     <div id='color-picker-example'>
  100.         <div id='picker'></div>
  101.         <div id='example-post'><div id='example-tile'><big>Example Post Title Here</big></div>Example post excerpt displayed below the title.</div>
  102.     </div>
  103.     <form method='POST' action='options.php'><div id='category-list'><h3>Pick Your Cutom Tile Colors:</h3><em>1.) Click inside each of the category inputs. <br/>2.) Next use the color picker on the left to select the color for that categories tiles.<br/>3.) Save Your Changes</em><ul>";
  104.    
  105.    
  106.     // Define the settings for recording
  107.     settings_fields('tc-plugin-settings');
  108.    
  109.     // Retrieve additional options before the categories loop
  110.     $show_key = get_option('category-key');
  111.     if(empty($show_key)){
  112.         $show_key = "false";
  113.     }
  114.     $show_key_jquery = get_option('category-key-jquery');
  115.     if(empty($show_key_jquery)){
  116.         $show_key_jquery = "true";
  117.     }
  118.     $featured_images_key = get_option('featured-images');
  119.     if(empty($featured_images_key)){
  120.         $featured_images_key = "false";
  121.     }
  122.        
  123.     // Get all Post Categories
  124.     $categories = get_categories();  
  125.  
  126.        
  127.     //Loops through each category and displays color inputs.
  128.     foreach($categories as $category) {
  129.         $cat_var = "cat_".$category->name;
  130.         // Make lowercase
  131.         $cat_var = strtolower($cat_var);
  132.         $cat_var = str_replace(" ", "_", $cat_var);
  133.         $cat = $category->name;
  134.         $id = $category->cat_ID;
  135.         // Retrieves option value
  136.         $cat_var_value = get_option($cat_var);
  137.         // Checks the value to see if it's empty. If it is use default.
  138.         if (empty($cat_var_value)){
  139.             $cat_var_value = "#afe2f8";
  140.         }
  141.        
  142.         // Echo out each list Category Name > Id > Input
  143.         echo "<li><strong>".$cat." </strong> <em>(".$id.")</em>
  144.         <input type='text' class='colorwell' name='".$cat_var."' value='".$cat_var_value."' />
  145.         </li>";
  146.     }
  147.    
  148.     ?>
  149.     <li class='category-key-list-item'>
  150.       <div id='category-key-radio'>
  151.           <input type="radio" name="category-key-jquery" <?php if($show_key_jquery == 'true') echo 'checked="checked"'; ?> value="true" /> On &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  152.           <input type="radio" name="category-key-jquery" <?php if($show_key_jquery == 'false') echo 'checked="checked"'; ?> value="false" /> Off&nbsp;&nbsp;&nbsp;
  153.       </div>
  154.       <div id='category-key-admin'>
  155.           <strong>jQuery Category Sorting & Animation</strong>
  156.           <br/>
  157.           <small><em>Turns the jQuery features on or off</em></small>
  158.       </div>
  159.     </li>
  160.    
  161.     <li class='category-key-list-item'>
  162.       <div id='category-key-radio'>
  163.           <input type="radio" name="category-key" <?php if($show_key == 'true') echo 'checked="checked"'; ?> value="true" /> Show &nbsp;&nbsp;
  164.           <input type="radio" name="category-key" <?php if($show_key == 'false') echo 'checked="checked"'; ?> value="false" /> Hide
  165.       </div>
  166.       <div id='category-key-admin'>
  167.           <strong>Display the Category Key</strong>
  168.           <br/>
  169.           <small><em>Show category names and colors</em></small>
  170.           <!-- An alert that this feature requires jQuery be turned on -->
  171.           <?php if($show_key_jquery == 'false') echo '<br/><small class="attention"><em><strong>ATTENTION:</strong> This featured requires jQuery be turned on</em></small>'; ?>
  172.       </div>
  173.     </li>
  174.    
  175.     <li class='category-key-list-item'>
  176.       <div id='category-key-radio'>
  177.           <input type="radio" name="featured-images" <?php if($featured_images_key == 'true') echo 'checked="checked"'; ?> value="true" /> On &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  178.           <input type="radio" name="featured-images" <?php if($featured_images_key == 'false') echo 'checked="checked"'; ?> value="false" /> Off&nbsp;&nbsp;&nbsp;
  179.       </div>
  180.       <div id='category-key-admin'>
  181.           <strong>Use Featured Images For Tiles</strong>
  182.           <br/>
  183.           <small><em>Uses the posts assigned featured image.</em></small>
  184.           <!-- An alert that this feature requires jQuery be turned on -->
  185.           <?php if($show_key_jquery == 'false') echo '<br/><small class="attention"><em><strong>ATTENTION:</strong> This featured requires jQuery be turned on</em></small>'; ?>
  186.       </div>
  187.     </li>
  188.    
  189.    
  190.          
  191.     <li id="submit-button"><input type='submit' class='button-primary' value='<?php _e('Save Changes') ?>'/></li></ul>
  192.     </div></form></div>
  193.     <div id='donate-box'>
  194.         <form action='https://www.paypal.com/cgi-bin/webscr' method='post'>
  195.         <input type='hidden' name='cmd' value='_s-xclick'>
  196.         <input type='hidden' name='hosted_button_id' value='HH7L4BWHALHLA'>
  197.         <input type='image' src='https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif' border='0' name='submit' alt='PayPal - The safer, easier way to pay online!'>
  198.         <img alt='' border='0' src='https://www.paypalobjects.com/en_US/i/scr/pixel.gif' width='1' height='1'>
  199.         </form>
  200.         <h3>Consider Donating</h3>
  201.         <em>Consider buying me a cup of coffee for the development of this plugin.</em>
  202.     </div>
  203.     <div id='tc-footer'>The Post Tiles Wordpress plugin was created by <a href="http://www.ethanhackett.com" target="_blank" title="Designed and Developed by Ethan Hackett www.ethanhackett.com">Ethan Hackett</a> as part of <a href="http://www.tincrate.com/plugins" target="_blank">TinCrate</a>.</div></div>
  204.     <?php
  205. }
  206. // End Admin Page
  207.  
  208.  
  209.  
  210.  
  211. // Start Frontend Code
  212. // Sets the "tc_post_tiles" function as a shortcode [tc_post_tiles]
  213. add_shortcode('post-tiles','tc_post_tiles');
  214.  
  215. // Runs Output
  216. function tc_post_tiles($atts,$content=null)
  217. {
  218.     shortcode_atts( array('categories'=>'', 'posts'=>'', 'excerpt'=>''), $atts);
  219.     // EXAMPLE USAGE: [post-tiles categories="1,2,3,4" posts="8"]
  220.        
  221.        // Defaults
  222.        extract(shortcode_atts(array(
  223.           "categories" => '',
  224.           "posts" => '',   
  225.           "excerpt" => ''
  226.        ), $atts));
  227.        
  228.        // Configure Both Categories and Number of Posts For Query
  229.        $excerpt_length = $atts['excerpt'];
  230.        
  231.        // Configure Categories For Query
  232.        $cat_num = $atts['categories'];     
  233.        if (isset($cat_num)) {
  234.            $cat_query = '&cat='.$atts['categories'];
  235.        }
  236.      
  237.        // Configure Number of Posts For Query if empty use 8.
  238.        $posts = $atts['posts'];    
  239.        if (isset($posts)) {
  240.            $posts_query = 'posts_per_page='.$atts['posts'];
  241.        }
  242.        else {
  243.            $posts_query = 'posts_per_page=50';
  244.        }
  245.        
  246.        // Configure Both Categories and Number of Posts For Query
  247.        $the_query = $posts_query.$cat_query;
  248.        
  249.        // Run the query              
  250.        query_posts($the_query);
  251.        
  252.        // Reset and setup variables
  253.        $output = '';
  254.        $temp_title = '';
  255.        $temp_link = '';
  256.        
  257.        // Retrives The On/Off for jQuery
  258.        $show_key_jquery = get_option('category-key-jquery');
  259.        
  260.        // If jquery is on(true) then output the following
  261.        if ($show_key_jquery == "true"){
  262.            // Get Plugin Url
  263.            $plugin_url = plugins_url()."/post-tiles";
  264.            // Attatch Javascript
  265.            echo "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>";
  266.            echo "<script type='text/javascript' src='".$plugin_url."/post-tiles-frontend.js'></script>";  
  267.            // Add CSS
  268.            ?>
  269.            <style media="screen" type="text/css">
  270.                     ul#post-tiles li, #category-key {
  271.                         opacity: 0;
  272.                     }      
  273.            </style><?php
  274.        }
  275.        
  276.                
  277.        // the loop
  278.        if (have_posts()) : while (have_posts()) : the_post();
  279.        
  280.           // Get Post Elements
  281.           $temp_title = get_the_title($post->ID);
  282.           $temp_link = get_permalink($post->ID);
  283.           $temp_excerpt = get_the_excerpt($post->ID);
  284.          
  285.           // Check to see if the excerpt was not defined thus empty
  286.           if ($excerpt_length == "") {
  287.                // Set excerpt length to 19 as default.
  288.               $excerpt_length =  '19';
  289.           }
  290.           // Truncates the excerpt length (Not using default wordpress for plugin conflicts)
  291.           $words = explode(' ', $temp_excerpt);
  292.           if (count($words) > $excerpt_length){
  293.             array_splice($words, $excerpt_length);
  294.             $temp_excerpt = implode(' ', $words);
  295.             $temp_excerpt .= '&hellip;';
  296.           }
  297.              
  298.            
  299.        
  300.          
  301.           // Grabs the categories then assigns the first category in the string to the class.
  302.           $category = get_the_category();
  303.           $category_name = $category[0]->cat_name;
  304.          
  305.           // Recovering Saved Color Values
  306.           // Define the Settings for recording
  307.           $cat_var = "cat_".$category_name;
  308.           // Make lowercase
  309.           $cat_var_value = strtolower($cat_var);
  310.           $cat_var = str_replace(" ", "_", $cat_var);
  311.           // Retrieve the option "hexadecimal value" for this category
  312.           $cat_var_value = get_option($cat_var);
  313.           // Checks the value to see if it's empty. If it is use default.
  314.           if (empty($cat_var_value)){
  315.             $cat_var_value = "#afe2f8";
  316.           }
  317.          
  318.           // Retrieve the option feature image.
  319.           $featured_images_key = get_option('featured-images');
  320.           // See if Featured image is true.
  321.           // Clear features_style variable.
  322.           $featured_style = "";
  323.           // Get the post ID.
  324.           $theID = get_the_ID();
  325.           // If there is a featured image.
  326.           if ($featured_images_key == "true") {
  327.               if ( has_post_thumbnail()) {
  328.                   // Retrieve the featured image.
  329.                   $thumb = wp_get_attachment_image_src( get_post_thumbnail_id(), 'medium');
  330.                   // Strip featured image down to the url.
  331.                   $url = $thumb['0'];
  332.                   $featured_style = "style='background: url(".$url.") 0 0;' class='featured-image'";
  333.               }
  334.          }
  335.           // Each output creates the li > link > title > exerpt
  336.           $output .= "<li ".$featured_style." id='".$theID."'><a href='$temp_link' class='".$cat_var."'style='background-color: $cat_var_value;'><h3>$temp_title</h3>$temp_excerpt</a></li>\n";
  337.           // Each output_key creates a li > category color block > category name
  338.              
  339.        endwhile; else:
  340.        
  341.           $output .= "No posts available. Double check your the post-tiles shortcode, selected categories, and number of posts.";
  342.          
  343.        endif;
  344.        
  345.        // Reset the query so it doesn't interupt the normal query
  346.        wp_reset_query();
  347.        
  348.        
  349.        // Display Key
  350.        $show_key = get_option('category-key');
  351.        $show_key_jquery = get_option('category-key-jquery');
  352.        $featured_images_key= get_option('featured-images');
  353.        
  354.        // If it's empty then the default value is false (default)
  355.        if(empty($show_key)){
  356.         $show_key == "false";
  357.        }
  358.        // If it's true then build the key.
  359.        if ($show_key == "true"){
  360.             $ca_args = array(
  361.                // $cat_num pulls from shortcode
  362.               'include' => $cat_num
  363.               );
  364.             //List categories  
  365.             $categories = get_categories($ca_args);  
  366.             //Loops through each category and displays key and color.
  367.             foreach($categories as $category) {
  368.                     // Set's category names
  369.                     $cat_var = $category->name;
  370.                     // Cleans up category names that have spaces
  371.                     $cat_var = str_replace(" ", "_", $cat_var);
  372.                     // Joins the cat_ with the category name to retrieve option
  373.                     $cat_var_key = "cat_".$cat_var;
  374.                     // Get's the category options which are the hexadecimal colors
  375.                     $cat_var_key_val = get_option($cat_var_key);
  376.                     // loops through the each category and prints them
  377.                     $key_items .= "<a href='#' class='key' id='".$cat_var_key."' style='background-color:".$cat_var_key_val.";'>".$cat_var."</a>\n";
  378.             }
  379.            
  380.             // Creates the finished key
  381.             $key_finished = "<div id='category-key'>\n".$key_items."<a href='#' class='key' id='category-all'>All</a></div>\n\n";
  382.        } else {
  383.             $key_finished = "<!-- Category Key is turned off. See the admin settings for Post Tiles -->";
  384.        }
  385.      
  386.      
  387.        return $key_finished.'<ul id="post-tiles">'.$output.'</ul>';
  388.      
  389.        $plugin_url = plugins_url()."/post-tiles";
  390. }
  391.  
  392. // Add the Scripts
  393. add_action('wp_enqueue_scripts', 'post_tiles_stylesheet');
  394.  
  395. // Adds CSS
  396. function post_tiles_stylesheet() {
  397.     $plugin_url = plugins_url()."/post-tiles";
  398.     wp_enqueue_style( 'post-tiles', $plugin_url.'/post-tiles.css' );
  399. //  wp_enqueue_script('post-tiles', $plugin_url.'/post-tiles-frontend.js' );
  400. }
  401. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement