Plugin Directory

Changeset 1553768


Ignore:
Timestamp:
12/13/2016 03:02:28 PM (9 years ago)
Author:
bainternet
Message:

updated plugin to save data in term meta instead of options table with automatic fallback and migration.

Location:
in-category-order/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • in-category-order/trunk/plugin.php

    r913261 r1553768  
    44Plugin URI: http://en.bainternet.info
    55Description: This plugin lets you set the order of posts per category
    6 Version: 0.0.1
     6Version: 0.0.2
    77Author: bainternet
    88Author Email: admin@bainternet.info
     
    2727if (!class_exists('in_cat_order')){
    2828
    29     class in_cat_order {
     29    class in_cat_order {
    3030        /****************
    3131         *  Public Vars *
    3232         ***************/
    3333         
    34         /**
    35         * $dir
    36         *
    37         * olds plugin directory
    38         * @var string
    39         */
     34        /**
     35        * $dir
     36        *
     37        * olds plugin directory
     38        * @var string
     39        */
    4040        public $dir = '';
    4141        /**
     
    4545         * @var string
    4646         */
    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
    5665        /****************
    5766         *    Methods   *
    5867         ***************/
    5968
    60         /**
    61         * Plugin class Constructor
    62         */
    63         function __construct() {
     69        /**
     70        * Plugin class Constructor
     71        */
     72        function __construct() {
    6473            $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__);
    6776            $this->hooks();
    68         }
    69        
    70         /**
    71         * hooks
    72         *
    73         * function used to add action and filter hooks
    74         * Used with `adminHooks` and `clientHokks`
    75         *
    76         * hooks for both admin and client sides should be added at the buttom
    77         *
    78         * @return void
    79         */
     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        */
    8089        public function hooks(){
    8190            if(is_admin())
     
    115124       
    116125        /**
    117          * setProperties
    118          *
    119          * function to set class Properties
    120          * @param array   $args       array of arguments
    121          * @param boolean $properties arguments to set
    122          */
    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){
    124133            if (!is_array($properties))
    125134                $properties = array_keys(get_object_vars($this));
    126135 
    127136            foreach ($properties as $key ) {
    128               $this->$key = (isset($args[$key]) ? $args[$key] : $this->$key);
     137              $this->$key = ( isset( $args[$key] ) ? $args[$key] : $this->$key );
    129138            }
    130139        }
     
    139148         */
    140149        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 );
    144153        }
    145154        /**
     
    156165         */
    157166        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 );
    160169        }
    161170
    162171        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');
    168177                }
    169178            }
     
    179188        function extra_category_fields($tag){
    180189            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');
    183192            $t_id  = $tag->term_id;
    184193            $rows  = '';
     
    191200            $table->tthumb     = __('Thumbnail',$this->txdomain);
    192201            $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();
    195204                $row->name    = get_post_field( 'post_title', $post);
    196205                $row->tremove = __('Remove',$this->txdomain);
    197206                $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');
    204213        }
    205214
     
    211220         * @return void
    212221         */
    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         */
    243241        function get_all_category_posts($cat_id = null){
    244242            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
    262292}//end if
    263293$GLOBALS['in_cat_order'] = new in_cat_order();
  • in-category-order/trunk/readme.txt

    r992432 r1553768  
    33Donate link: http://en.bainternet.info/donations
    44Tags: post order, drag n drop order, category order
    5 Requires at least: 3.8.0
    6 Tested up to: 4.0.0
    7 Stable tag: 0.0.1
     5Requires at least: 4.3.0
     6Tested up to: 4.7.0
     7Stable tag: 0.0.2
    88
    99This plugin lets you set the order of posts on a category basis with a simple Drag N Drop interface.
     
    4545
    4646== Changelog ==
     47
     480.0.2 updated plugin to save data in term meta instead of options table with automatic fallback nad migration.
    4749 
    48500.0.1 inital release.
Note: See TracChangeset for help on using the changeset viewer.