Plugin Directory

Changeset 884049


Ignore:
Timestamp:
03/29/2014 08:24:42 AM (12 years ago)
Author:
ssovit
Message:
  • fixed feautred post widget
  • fixed featured toggle that was not working on custom post types
Location:
featured-post
Files:
4 added
2 edited

Legend:

Unmodified
Added
Removed
  • featured-post/trunk/featured-post.php

    r883690 r884049  
    11<?php
     2
    23/*************************************************************************
    34
     
    56Plugin URI: http://ssovit.com/featured-post-wordpress-plugin/
    67Description: Featured Post Plugin For Wordpress.
    7 Version: 3.1
     8Version: 3.2
    89Author: Sovit Tamrakar
    910Author URI: http://wppress.net
     
    2728
    2829**************************************************************************/
    29 class Featured_Post {
    30     var $db = NULL;
    31    
    32     function __construct() {
    33         add_action('init', array(&$this,
    34             'init'
    35         ));
    36         add_action('admin_init', array(&$this,
    37             'admin_init'
    38         ));
    39         add_action('wp_ajax_toggle-featured-post', array(&$this,
    40             'admin_ajax'
    41         ));
    42     }
    43     function init() {
    44         add_filter('query_vars', array(&$this,
    45             'query_vars'
    46         ));
    47         add_action('pre_get_posts', array(&$this,
    48             'pre_get_posts'
    49         ));
    50     }
    51     function admin_init() {
    52         add_filter('current_screen', array(&$this,
    53             'my_current_screen'
    54         ));
    55         add_filter('manage_posts_columns', array(&$this,
    56             'manage_posts_columns'
    57         ));
    58         add_filter('manage_pages_columns', array(&$this,
    59             'manage_posts_columns'
    60         ));
    61         add_filter('manage_posts_custom_column', array(&$this,
    62             'manage_posts_custom_column'
    63         ) , 10, 2);
    64         add_filter('manage_pages_custom_column', array(&$this,
    65             'manage_posts_custom_column'
    66         ) , 10, 2);
    67         add_action('admin_head-edit.php', array(&$this,
    68             'admin_head'
    69         ));
    70         add_filter('pre_get_posts', array(&$this,
    71             'admin_pre_get_posts'
    72         ) , 1);
    73     }
    74     function add_views_link($views) {
    75         $post_type = ((isset($_GET['post_type']) && $_GET['post_type'] != "") ? $_GET['post_type'] : 'post');
    76         $count     = $this->total_featured($post_type);
    77         $class     = $_GET['post_status'] == 'featured' ? "current" : '';
    78         $views['featured']           = "<a class=\"$class\" id=\"featured-post-filter\" href=\"edit.php?&post_status=featured&post_type={$post_type}\">Featured <span class=\"count\">({$count})</span></a>";
    79         return $views;
    80     }
    81     function total_featured($post_type = "post") {
    82         $rowQ      = new WP_Query(array(
    83             'post_type' => $post_type,
    84             'meta_query' => array(
    85                 array(
    86                     'key' => '_is_featured',
    87                     'value' => 'yes'
    88                 )
    89             ) ,
    90             'posts_per_page' => 1
    91         ));
    92         wp_reset_postdata();
    93         wp_reset_query();
    94         $rows = $rowQ->found_posts;
    95         unset($rowQ);
    96         return $rows;
    97     }
    98     function my_current_screen($screen) {
    99         if (defined('DOING_AJAX') && DOING_AJAX) {
    100             return $screen;
    101         }
    102         $post_types = get_post_types(array(
    103             'public' => true,
    104             '_builtin' => false
    105         ));
    106         $post_types[]='post';
    107         foreach ($post_types as $post_type) {
    108             add_filter('views_edit-post', array(&$this,
    109                 'add_views_link'
    110             ));
    111         }
    112         return $screen;
    113     }
    114     function manage_posts_columns($columns) {
    115         global $current_user;
    116         get_currentuserinfo();
    117         if (current_user_can('edit_posts', $user_id)) {
    118             $columns['featured'] = __('Featured');
    119         }
    120         return $columns;
    121     }
    122     function manage_posts_custom_column($column_name, $post_id) {
    123         //echo "here";
    124         if ($column_name == 'featured') {
    125             $is_featured = get_post_meta($post_id, '_is_featured', true);
    126             $class       = "dashicons";
    127             $text        = "";
    128             if ($is_featured == "yes") {
    129                 $class.= " dashicons-star-filled";
    130                 $text = "";
    131             } else {
    132                 $class.= " dashicons-star-empty";
    133             }
    134             echo "<a href=\"#!featured-toggle\" class=\"featured-post-toggle {$class}\" data-post-id=\"{$post_id}\">$text</a>";
    135         }
    136     }
    137     function admin_head() {
    138        
    139         echo '<script type="text/javascript">
     30class Featured_Post
     31{
     32    var $db = NULL;
     33    public $post_types = array();
     34   
     35    function __construct() {
     36       
     37        add_action('init', array(&$this,
     38            'init'
     39        ) , 999);
     40        add_action('admin_init', array(&$this,
     41            'admin_init'
     42        ));
     43        add_action('wp_ajax_toggle-featured-post', array(&$this,
     44            'admin_ajax'
     45        ));
     46    }
     47    function init() {
     48        $this->post_types = get_post_types(array(
     49            '_builtin' => false,
     50        ) , 'names', 'or');
     51        $this->post_types['post'] = 'post';
     52        $this->post_types['page'] = 'page';
     53        ksort($this->post_types);
     54        add_filter('query_vars', array(&$this,
     55            'query_vars'
     56        ));
     57        add_action('pre_get_posts', array(&$this,
     58            'pre_get_posts'
     59        ));
     60    }
     61    function admin_init() {
     62        add_filter('current_screen', array(&$this,
     63            'my_current_screen'
     64        ));
     65       
     66        add_action('admin_head-edit.php', array(&$this,
     67            'admin_head'
     68        ));
     69        add_filter('pre_get_posts', array(&$this,
     70            'admin_pre_get_posts'
     71        ) , 1);
     72        foreach ($this->post_types as $key => $val) {
     73            add_filter('manage_edit-' . $key . '_columns', array(&$this,
     74                'manage_posts_columns'
     75            ));
     76            add_action('manage_' . $key . '_posts_custom_column', array(&$this,
     77                'manage_posts_custom_column'
     78            ) , 10, 2);
     79        }
     80    }
     81    function add_views_link($views) {
     82        $post_type = ((isset($_GET['post_type']) && $_GET['post_type'] != "") ? $_GET['post_type'] : 'post');
     83        $count = $this->total_featured($post_type);
     84        $class = $_GET['post_status'] == 'featured' ? "current" : '';
     85        $views['featured'] = "<a class=\"" . $class . "\" id=\"featured-post-filter\" href=\"edit.php?&post_status=featured&post_type={$post_type}\">Featured <span class=\"count\">({$count})</span></a>";
     86        return $views;
     87    }
     88    function total_featured($post_type = "post") {
     89        $rowQ = new WP_Query(array(
     90            'post_type' => $post_type,
     91            'meta_query' => array(
     92                array(
     93                    'key' => '_is_featured',
     94                    'value' => 'yes'
     95                )
     96            ) ,
     97            'posts_per_page' => 1
     98        ));
     99        wp_reset_postdata();
     100        wp_reset_query();
     101        $rows = $rowQ->found_posts;
     102        unset($rowQ);
     103        return $rows;
     104    }
     105    function my_current_screen($screen) {
     106        if (defined('DOING_AJAX') && DOING_AJAX) {
     107            return $screen;
     108        }
     109       
     110        foreach ($this->post_types as $key => $val) {
     111            add_filter('views_edit-' . $key, array(&$this,
     112                'add_views_link'
     113            ));
     114        }
     115        return $screen;
     116    }
     117    function manage_posts_columns($columns) {
     118        global $current_user;
     119        get_currentuserinfo();
     120        if (current_user_can('edit_posts', $user_id)) {
     121            $columns['featured'] = __('Featured');
     122        }
     123        return $columns;
     124    }
     125    function manage_posts_custom_column($column_name, $post_id) {
     126       
     127        //echo "here";
     128        if ($column_name == 'featured') {
     129            $is_featured = get_post_meta($post_id, '_is_featured', true);
     130            $class = "dashicons";
     131            $text = "";
     132            if ($is_featured == "yes") {
     133                $class.= " dashicons-star-filled";
     134                $text = "";
     135            } else {
     136                $class.= " dashicons-star-empty";
     137            }
     138            echo "<a href=\"#!featured-toggle\" class=\"featured-post-toggle {$class}\" data-post-id=\"{$post_id}\">$text</a>";
     139        }
     140    }
     141    function admin_head() {
     142       
     143        echo '<script type="text/javascript">
    140144        jQuery(document).ready(function($){
    141145            $(\'.featured-post-toggle\').on("click",function(e){
     
    161165        });
    162166        </script>';
    163     }
    164     function admin_ajax() {
    165         header('Content-Type: application/json');
    166         $post_id     = $_POST['post_id'];
    167         $is_featured = get_post_meta($post_id, '_is_featured', true);
    168         $newStatus   = $is_featured == 'yes' ? 'no' : 'yes';
    169         delete_post_meta($post_id, '_is_featured');
    170         add_post_meta($post_id, '_is_featured', $newStatus);
    171         echo json_encode(array('ID'=>$post_id,'new_status'=>$newStatus,'total_featured'=>$this->total_featured(get_post_type($post_id))));
    172         die();
    173     }
    174     function admin_pre_get_posts($query) {
    175         global $wp_query;
    176         if (is_admin() && $_GET['post_status'] == 'featured') {
    177             $query->set('meta_key', 'featured');
    178             $query->set('meta_value', 'yes');
    179         }
    180         return $query;
    181     }
    182     function query_vars($public_query_vars) {
    183         $public_query_vars[] = 'featured';
    184         return $public_query_vars;
    185     }
    186     function pre_get_posts($query) {
    187         if (!is_admin()) {
    188             if ($query->get('featured') == 'yes') {
    189                 $query->set('meta_key', 'featured');
    190                 $query->set('meta_value', 'yes');
    191             }
    192         }
    193         return $query;
    194     }
     167    }
     168    function admin_ajax() {
     169        header('Content-Type: application/json');
     170        $post_id = $_POST['post_id'];
     171        $is_featured = get_post_meta($post_id, '_is_featured', true);
     172        $newStatus = $is_featured == 'yes' ? 'no' : 'yes';
     173        delete_post_meta($post_id, '_is_featured');
     174        add_post_meta($post_id, '_is_featured', $newStatus);
     175        echo json_encode(array(
     176            'ID' => $post_id,
     177            'new_status' => $newStatus,
     178            'total_featured' => $this->total_featured(get_post_type($post_id))
     179        ));
     180        die();
     181    }
     182    function admin_pre_get_posts($query) {
     183        global $wp_query;
     184        if (is_admin() && $_GET['post_status'] == 'featured') {
     185            $query->set('meta_key', 'featured');
     186            $query->set('meta_value', 'yes');
     187        }
     188        return $query;
     189    }
     190    function query_vars($public_query_vars) {
     191        $public_query_vars[] = 'featured';
     192        return $public_query_vars;
     193    }
     194    function pre_get_posts($query) {
     195        if (!is_admin()) {
     196            if ($query->get('featured') == 'yes') {
     197                $query->set('meta_key', 'featured');
     198                $query->set('meta_value', 'yes');
     199            }
     200        }
     201        return $query;
     202    }
    195203}
    196 new Featured_Post();
     204class Featured_Post_Widget extends WP_Widget
     205{
     206    private $post_types = array();
     207    function __construct() {
     208        parent::WP_Widget(false, $name = 'Featured Post');
     209    }
     210   
     211    function form($instance) {
     212        $title = esc_attr($instance['title']);
     213        $type = esc_attr($instance['post_type']);
     214        $num = (int)esc_attr($instance['num']);
     215        $this->post_types = get_post_types(array(
     216            '_builtin' => false,
     217        ) , 'names', 'or');
     218        $this->post_types['post'] = 'post';
     219        $this->post_types['page'] = 'page';
     220        ksort($this->post_types);
     221        echo "<p>";
     222        echo "<label for=\"" . $this->get_field_id('title') . "\">";
     223        echo _e('Title:');
     224        echo "</label>";
     225        echo "<input class=\"widefat\" id=\"" . $this->get_field_id('title') . "\" name=\"" . $this->get_field_name('title') . "\" type=\"text\" value=\"" . $title . "\" />";
     226        echo "</p>";
     227        echo "<p>";
     228        echo "<label for=\"" . $this->get_field_id('post_type') . "\">";
     229        echo _e('Post Type:');
     230        echo "</label>";
     231        echo "<select name = \"" . $this->get_field_name('post_type') . "\" id=\"" . $this->get_field_id('title') . "\" >";
     232        foreach ($this->post_types as $key => $post_type) {
     233            echo '<option value="' . $key . '"' . ($key == $type ? " selected" : "") . '>' . $key . "</option>";
     234        }
     235       
     236        echo "</select>";
     237        echo "</p>";
     238        echo "<p>";
     239        echo "<label for=\"" . $this->get_field_id('num') . "\">";
     240        echo _e('Number To show:');
     241       
     242        echo "</label>";
     243        echo "<input id = \"" . $this->get_field_id('num') . "\" class = \"widefat\" name = \"" . $this->get_field_name('num') . "\" type=\"text\" value =\"" . $num . "\" / >";
     244        echo "</p>";
     245    }
     246    function update($new_instance, $old_instance) {
     247        $instance = $old_instance;
     248        $instance['title'] = strip_tags($new_instance['title']);
     249        $instance['num'] = (int)strip_tags($new_instance['num']);
     250        $instance['post_type'] = strip_tags($new_instance['post_type']);
     251        if ($instance['num'] < 1) {
     252            $instance['num'] = 10;
     253        }
     254        return $instance;
     255    }
     256    function widget($args, $instance) {
     257        extract($args);
     258        $title = apply_filters('widget_title', $instance['title']);
     259        echo $before_widget;
     260        if ($title) {
     261            echo $before_title . $title . $after_title;
     262        }
     263        echo "<ul class=\"widget-list featured-post-widget featured-post\">";
     264        wp_reset_query();
     265        query_posts('post_type=' . $instance['post_type'] . '&showposts=' . $instance['num'] . '&featured=yes');
     266        while (have_posts()) {
     267            the_post();
     268            echo "<li><a href=\"" . the_permalink() . "\">";
     269            the_title();
     270            echo "</a>";
     271            echo "</li>";
     272        }
     273        wp_reset_query();
     274        echo "</ul>";
     275        echo $after_widget;
     276        // outputs the content of the widget
     277    }
     278}
     279$Featured_Post = new Featured_Post();
     280
     281add_action('widgets_init', create_function('', 'return register_widget("Featured_Post_Widget");') , 100);
  • featured-post/trunk/readme.txt

    r883690 r884049  
    3030
    3131== Upgrade Notice ==
     32= 3.2 =
     33Fixed Featured Post Widget and featured toggle not appearing on custom post types
    3234= 3.1 =
    3335Fix some bugs and added dashicon support with 3.8.1 support
Note: See TracChangeset for help on using the changeset viewer.