Plugin Directory

Changeset 1325746


Ignore:
Timestamp:
01/11/2016 10:53:47 AM (10 years ago)
Author:
gorakh.sth
Message:

Added Option page to allow popular post feature for particular post_type.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • my-popular-posts/trunk/index.php

    r1324511 r1325746  
    88*/
    99Class Popular_post{
     10    public $ppt = array();
    1011    function __construct() {
    1112        add_action('save_post', array($this,'check_save_post'), 10, 3 );
    1213        add_action('wp_head',array($this, 'set_in_single'));
    1314        add_action('pre_get_posts', array($this,'change_query'));
     15        $this->set_post_types();
    1416    }
    1517
    16     public function update($post_id){
     18    function update($post_id){
    1719        $meta_key = '_popular_post';
    1820        $views = $this->get_views($post_id);
    19         if($views==''){
     21        if($views=='' && in_array($post->post_type,$this->ppt)){
    2022            delete_post_meta($post_id, $meta_key);
    2123            add_post_meta($post_id, $meta_key, 0);
     
    2628    }
    2729
    28     public function set_in_single(){
    29         if(is_single()){
     30    function set_in_single(){
     31        if(is_single() && in_array(get_post_type(get_the_ID()),$this->ppt)){
    3032            $this->update(get_the_ID());
    3133        }
    3234    }
    3335   
    34     public function get_views($post_id){
     36    function get_views($post_id){
    3537        $count = get_post_meta($post_id, '_popular_post', true);
    3638        return $count;
    3739    }
    3840
    39     public function change_query( $query ) {
     41    function change_query( $query ) {
    4042        if ( $query->get('orderby') == 'popularity' ) {
    4143            $query->set( 'orderby', 'meta_value_num');
     
    5254        }
    5355    }
    54    
     56    function set_post_types(){
     57        $pts = get_post_types(array('public'=>true, 'publicly_queryable'=>true));
     58        foreach($pts as $pt): if($pt == get_option('_mpp_'.$pt)):
     59            $this->ppt[$pt] = get_option('_mpp_'.$pt); 
     60            $this->ppt['post']= 'post';
     61        endif; endforeach;
     62    }   
    5563}
    5664
    57 $var = new Popular_post();
     65new Popular_post;
    5866
    5967function get_view_number(){
     
    92100
    93101
     102/**
     103 * Option page class
     104 **/
     105class options_page {
     106    function __construct() {
     107        add_action( 'admin_menu', array( $this, 'admin_menu' ) );
     108        add_action( 'admin_init', array($this,'register_mpp_theme_option' ));
     109    }
    94110
     111    function admin_menu() {
     112        add_options_page('MPP Option page', 'MPP Setting', 'manage_options', 'popular_posts_options', array($this, 'settings_page') );
     113    }
     114
     115    function  settings_page() {
     116        $this->mpp_theme_option_page();
     117    }
     118
     119    function register_mpp_theme_option() {
     120        $post_types = get_post_types(array('public'=>true, 'publicly_queryable'=>true));
     121        foreach($post_types as $post_type): if($post_type != 'attachment'):
     122            register_setting( 'mpp-option-group', '_mpp_'.$post_type );
     123        endif; endforeach;
     124    }
     125
     126    function mpp_theme_option_page() {
     127        ?>
     128        <div class="wrap">
     129            <h3>Pleaes select post type to allow for <strong>Popular Posts</strong></h3>
     130            <form method="post" action="options.php">
     131                <?php settings_fields( 'mpp-option-group' ); ?>
     132                <?php do_settings_sections( 'mpp-option-group' ); ?>
     133                <?php  $post_types = get_post_types(array('public'=>true, 'publicly_queryable'=>true)); ?>
     134                <table class="form-table">
     135                    <?php foreach($post_types as $post_type): if($post_type != 'attachment'):  ?>
     136                        <tr valign="top">
     137                            <th style="text-transform:capitalize; padding:10px 10px 10px 0; " scope="row"><?php echo $post_type; ?></th>
     138                            <td style="padding:10px 10px;"><input type="checkbox" <?php if($post_type == get_option('_mpp_'.$post_type)): echo 'checked'; endif;  ?> name="<?php echo '_mpp_'.$post_type; ?>" value="<?php echo $post_type; ?>" /></td>
     139                        </tr>
     140                    <?php endif; endforeach;  ?>
     141                </table>
     142                <?php submit_button(); ?>
     143            </form>
     144        </div>
     145        <?php
     146    }
     147
     148}
     149new options_page;
Note: See TracChangeset for help on using the changeset viewer.