Changeset 1325746
- Timestamp:
- 01/11/2016 10:53:47 AM (10 years ago)
- File:
-
- 1 edited
-
my-popular-posts/trunk/index.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
my-popular-posts/trunk/index.php
r1324511 r1325746 8 8 */ 9 9 Class Popular_post{ 10 public $ppt = array(); 10 11 function __construct() { 11 12 add_action('save_post', array($this,'check_save_post'), 10, 3 ); 12 13 add_action('wp_head',array($this, 'set_in_single')); 13 14 add_action('pre_get_posts', array($this,'change_query')); 15 $this->set_post_types(); 14 16 } 15 17 16 publicfunction update($post_id){18 function update($post_id){ 17 19 $meta_key = '_popular_post'; 18 20 $views = $this->get_views($post_id); 19 if($views=='' ){21 if($views=='' && in_array($post->post_type,$this->ppt)){ 20 22 delete_post_meta($post_id, $meta_key); 21 23 add_post_meta($post_id, $meta_key, 0); … … 26 28 } 27 29 28 publicfunction 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)){ 30 32 $this->update(get_the_ID()); 31 33 } 32 34 } 33 35 34 publicfunction get_views($post_id){36 function get_views($post_id){ 35 37 $count = get_post_meta($post_id, '_popular_post', true); 36 38 return $count; 37 39 } 38 40 39 publicfunction change_query( $query ) {41 function change_query( $query ) { 40 42 if ( $query->get('orderby') == 'popularity' ) { 41 43 $query->set( 'orderby', 'meta_value_num'); … … 52 54 } 53 55 } 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 } 55 63 } 56 64 57 $var = new Popular_post();65 new Popular_post; 58 66 59 67 function get_view_number(){ … … 92 100 93 101 102 /** 103 * Option page class 104 **/ 105 class 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 } 94 110 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 } 149 new options_page;
Note: See TracChangeset
for help on using the changeset viewer.