Changeset 1553768
- Timestamp:
- 12/13/2016 03:02:28 PM (9 years ago)
- Location:
- in-category-order/trunk
- Files:
-
- 2 edited
-
plugin.php (modified) (9 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
in-category-order/trunk/plugin.php
r913261 r1553768 4 4 Plugin URI: http://en.bainternet.info 5 5 Description: This plugin lets you set the order of posts per category 6 Version: 0.0. 16 Version: 0.0.2 7 7 Author: bainternet 8 8 Author Email: admin@bainternet.info … … 27 27 if (!class_exists('in_cat_order')){ 28 28 29 class in_cat_order {29 class in_cat_order { 30 30 /**************** 31 31 * Public Vars * 32 32 ***************/ 33 33 34 /**35 * $dir36 *37 * olds plugin directory38 * @var string39 */34 /** 35 * $dir 36 * 37 * olds plugin directory 38 * @var string 39 */ 40 40 public $dir = ''; 41 41 /** … … 45 45 * @var string 46 46 */ 47 public $url = ''; 48 /** 49 * $txdomain 50 * 51 * holds plugin textDomain 52 * @var string 53 */ 54 public $txdomain = 'in-cat-order'; 55 47 public $url = ''; 48 /** 49 * $txdomain 50 * 51 * holds plugin textDomain 52 * @var string 53 */ 54 public $txdomain = 'in-cat-order'; 55 56 57 /** 58 * $meta_key 59 * 60 * Holds meta key 61 * @var string 62 */ 63 public $meta_key = 'in-cat-order'; 64 56 65 /**************** 57 66 * Methods * 58 67 ***************/ 59 68 60 /**61 * Plugin class Constructor62 */63 function __construct() {69 /** 70 * Plugin class Constructor 71 */ 72 function __construct() { 64 73 $this->setProperties(); 65 $this->dir = plugin_dir_path(__FILE__);66 $this->url = plugins_url('assets/', __FILE__);74 $this->dir = plugin_dir_path(__FILE__); 75 $this->url = plugins_url('assets/', __FILE__); 67 76 $this->hooks(); 68 }69 70 /**71 * hooks72 *73 * function used to add action and filter hooks74 * Used with `adminHooks` and `clientHokks`75 *76 * hooks for both admin and client sides should be added at the buttom77 *78 * @return void79 */77 } 78 79 /** 80 * hooks 81 * 82 * function used to add action and filter hooks 83 * Used with `adminHooks` and `clientHokks` 84 * 85 * hooks for both admin and client sides should be added at the buttom 86 * 87 * @return void 88 */ 80 89 public function hooks(){ 81 90 if(is_admin()) … … 115 124 116 125 /** 117 * setProperties118 *119 * function to set class Properties120 * @param array $args array of arguments121 * @param boolean $properties arguments to set122 */123 public function setProperties($args = array(), $properties = false){126 * setProperties 127 * 128 * function to set class Properties 129 * @param array $args array of arguments 130 * @param boolean $properties arguments to set 131 */ 132 public function setProperties($args = array(), $properties = false){ 124 133 if (!is_array($properties)) 125 134 $properties = array_keys(get_object_vars($this)); 126 135 127 136 foreach ($properties as $key ) { 128 $this->$key = ( isset($args[$key]) ? $args[$key] : $this->$key);137 $this->$key = ( isset( $args[$key] ) ? $args[$key] : $this->$key ); 129 138 } 130 139 } … … 139 148 */ 140 149 public function createNewView($args = array('vars' => array())){ 141 if(!class_exists( 'EasyView'))142 require_once( $this->dir.'/classes/EasyView.php');143 return new EasyView( '',$args);150 if(!class_exists( 'EasyView' )) 151 require_once( $this->dir.'/classes/EasyView.php' ); 152 return new EasyView( '', $args ); 144 153 } 145 154 /** … … 156 165 */ 157 166 public function createViewGet($template = '', $templatedir = '', $args = array('vars' => array())){ 158 $v = $this->createNewView( $templatedir, $args);159 return $v->getRender( $template);167 $v = $this->createNewView( $templatedir, $args ); 168 return $v->getRender( $template ); 160 169 } 161 170 162 171 public function pre_get_posts($q){ 163 if ( $q->is_main_query() && $q->is_category()){164 $order = $this->get_all_category_posts( get_queried_object_id());165 if ( count($order)> 0 ){166 $q->set( 'post__in',$order);167 $q->set( 'orderby','post__in');172 if ( $q->is_main_query() && $q->is_category() ){ 173 $order = $this->get_all_category_posts( get_queried_object_id() ); 174 if ( count($order)> 0 ){ 175 $q->set( 'post__in', $order ); 176 $q->set( 'orderby','post__in'); 168 177 } 169 178 } … … 179 188 function extra_category_fields($tag){ 180 189 wp_enqueue_script('jquery'); 181 wp_enqueue_script('jquery-ui');182 wp_enqueue_script('jquery-ui-sortable');190 wp_enqueue_script('jquery-ui'); 191 wp_enqueue_script('jquery-ui-sortable'); 183 192 $t_id = $tag->term_id; 184 193 $rows = ''; … … 191 200 $table->tthumb = __('Thumbnail',$this->txdomain); 192 201 $table->tsure = __('Are you sure?',$this->txdomain); 193 foreach ($posts as $post) {194 $row= $this->createNewView();202 foreach ($posts as $post) { 203 $row= $this->createNewView(); 195 204 $row->name = get_post_field( 'post_title', $post); 196 205 $row->tremove = __('Remove',$this->txdomain); 197 206 $row->id = $post; 198 $row->thumb = (has_post_thumbnail($post))? get_the_post_thumbnail($post, array(80,80)): 'none';199 $rows .= $row->getRender($this->dir.'/views/in_row_view.php');200 }201 202 $table->rows = $rows;203 $table->render($this->dir.'/views/in_table_view.php');207 $row->thumb = (has_post_thumbnail($post))? get_the_post_thumbnail($post, array(80,80)): 'none'; 208 $rows .= $row->getRender($this->dir.'/views/in_row_view.php'); 209 } 210 211 $table->rows = $rows; 212 $table->render($this->dir.'/views/in_table_view.php'); 204 213 } 205 214 … … 211 220 * @return void 212 221 */ 213 function save_extra_fields_callback( $term_id ) { 214 $t_id = $term_id; 215 $terms_meta = get_option( 'in_category'); 216 $term_meta = isset($terms_meta[$term_id])? $terms_meta[$term_id]: array(); 217 if (isset($_POST['clear_in_cat_order'])){ 218 //save the option array 219 unset($terms_meta[$term_id]); 220 update_option( 'in_category', $terms_meta ); 221 return $y_id; 222 } 223 $keys = array('in_cat_order'); 224 foreach ($keys as $key){ 225 if (isset($_POST[$key])){ 226 $term_meta = $_POST[$key]; 227 }else{ 228 unset($term_meta[$key]); 229 } 230 } 231 //save the option array 232 $terms_meta[$term_id] = $term_meta; 233 update_option( 'in_category', $terms_meta ); 234 } 235 236 /** 237 * get_all_category_posts 238 * 239 * gets a list of all posts from a category 240 * @param int $cat_id 241 * @return array 242 */ 222 function save_extra_fields_callback( $term_id ) { 223 //clear flag checked? 224 if (isset($_POST['clear_in_cat_order'])){ 225 return $this->delete_term_meta( $term_id ); 226 } 227 228 //save order 229 if (isset($_POST['in_cat_order'])){ 230 $this->update_term_meta( $term_id, $_POST['in_cat_order'] ); 231 } 232 } 233 234 /** 235 * get_all_category_posts 236 * 237 * gets a list of all posts from a category 238 * @param int $cat_id 239 * @return array 240 */ 243 241 function get_all_category_posts($cat_id = null){ 244 242 if ($cat_id == null) return array(); 245 $cat_meta = get_option( "in_category"); 246 $order = isset($cat_meta[$cat_id])? $cat_meta[$cat_id]: array(); 247 $posts = get_posts(array( 248 'post_type' => 'post', 249 'posts_per_page' => -1, 250 'post__not_in' => $order, 251 'fields' => 'ids', 252 'cat' => $cat_id 253 ) 254 ); 255 foreach ((array)$posts as $p) { 256 $order[] = $p; 257 } 258 return $order; 259 } 260 261 } // end class 243 $order = $this->get_term_meta( $cat_id ); 244 $posts = get_posts(array( 245 'post_type' => 'post', 246 'posts_per_page' => -1, 247 'post__not_in' => $order, 248 'fields' => 'ids', 249 'cat' => $cat_id 250 ) 251 ); 252 foreach ((array)$posts as $p) { 253 $order[] = $p; 254 } 255 return $order; 256 } 257 258 function update_term_meta( $term_id, $value ){ 259 update_term_meta( $term_id, $this->meta_key, $value ); 260 } 261 262 function delete_term_meta( $term_id ){ 263 delete_term_meta( $term_id, $this->meta_key ); 264 //cleanup options table data 265 $terms_meta = get_option( 'in_category'); 266 if ( isset( $terms_meta[$term_id] ) ){ 267 unset($terms_meta[$term_id]); 268 update_option( 'in_category', $terms_meta ); 269 } 270 } 271 272 function get_term_meta( $term_id ){ 273 $saved = get_term_meta( $term_id, $this->meta_key, true ); 274 if ( !empty( $saved ) ){ 275 return $saved; 276 } 277 //try fallback to saved meta in options table 278 $terms_meta = get_option( 'in_category'); 279 if ( isset( $terms_meta[$term_id] ) ){ 280 $saved = $terms_meta[$term_id]; 281 //store in term meta 282 $this->update_term_meta( $term_id, $saved ); 283 //remove from options table 284 unset( $terms_meta[$term_id] ); 285 update_option( 'in_category', $terms_meta ); 286 return $saved; 287 } 288 return array(); 289 } 290 291 } // end class 262 292 }//end if 263 293 $GLOBALS['in_cat_order'] = new in_cat_order(); -
in-category-order/trunk/readme.txt
r992432 r1553768 3 3 Donate link: http://en.bainternet.info/donations 4 4 Tags: post order, drag n drop order, category order 5 Requires at least: 3.8.06 Tested up to: 4. 0.07 Stable tag: 0.0. 15 Requires at least: 4.3.0 6 Tested up to: 4.7.0 7 Stable tag: 0.0.2 8 8 9 9 This plugin lets you set the order of posts on a category basis with a simple Drag N Drop interface. … … 45 45 46 46 == Changelog == 47 48 0.0.2 updated plugin to save data in term meta instead of options table with automatic fallback nad migration. 47 49 48 50 0.0.1 inital release.
Note: See TracChangeset
for help on using the changeset viewer.