Plugin Directory

Changeset 804473


Ignore:
Timestamp:
11/14/2013 06:11:09 PM (12 years ago)
Author:
rolice
Message:

Access policy added. Now only users with authorized roles may use the plug-in. These roles are selected from the new permissions screen. General optimization.

Location:
post-sorter/trunk
Files:
3 added
7 edited

Legend:

Unmodified
Added
Removed
  • post-sorter/trunk

    • Property svn:ignore set to
      ftpsync.settings
  • post-sorter/trunk/css/style.css

    r730333 r804473  
    3939.post_sorter_form textarea[readonly="readonly"]
    4040{ background-color: #eee; }
     41
     42.post_sorter_form .multi-select { margin-bottom: 24px; float: left; }
     43.post_sorter_form .multi-select label { display: block; }
     44.post_sorter_form .multi-select select[multiple] { vertical-align: top; width: 320px; min-height: 160px; }
     45
     46.post_sorter_form .multi-select.toggle { margin: 48px; }
     47.post_sorter_form .multi-select.toggle input[type="button"] { display: block; margin: 12px 0; }
  • post-sorter/trunk/js/common.js

    r730151 r804473  
    11function post_sorter_saveOnKeyUp(e, obj, pid) {
    2     var code = e.which ? e.which : e.keyCode;
    3    
    4     switch(code) {
    5         case 13:
    6             post_sorter_save(obj, pid);
    7             return false;
    8     }
     2    var code = e.which ? e.which : e.keyCode;
     3   
     4    switch(code) {
     5        case 13:
     6            post_sorter_save(obj, pid);
     7            return false;
     8    }
    99}
    1010
    1111function post_sorter_save(obj, pid) {   
    12     jQuery.ajax({
    13         type: "post",
    14         url: ajaxurl,
    15         data: {
    16             action: "save_sort_position",
    17             post_id: pid,
    18             position: jQuery(obj).val()
    19         },
    20         dataType: "json",
    21         success: post_sorter_onSortSave,
    22         error: post_sorter_onError
    23     });
     12    jQuery.ajax({
     13        type: "post",
     14        url: ajaxurl,
     15        data: {
     16            action: "save_sort_position",
     17            post_id: pid,
     18            position: jQuery(obj).val()
     19        },
     20        dataType: "json",
     21        success: post_sorter_onSortSave,
     22        error: post_sorter_onError
     23    });
    2424}
    2525
    2626function post_sorter_onError(jqXhr, textStatus, errorThrown) {
    27     alert(textStatus);
     27    alert(textStatus);
    2828}
    2929
    3030function post_sorter_onSortSave(res) {
    31     window.location.href = window.location;
     31    window.location.href = window.location;
    3232}
    3333
    3434function post_sorter_moveUp(pid) {
    35     post_sorter_move(pid, 'up');
     35    post_sorter_move(pid, 'up');
    3636}
    3737
    3838function post_sorter_moveDown(pid) {
    39     post_sorter_move(pid, 'down');
     39    post_sorter_move(pid, 'down');
    4040}
    4141
    4242function post_sorter_move(pid, direction) {
    43     jQuery.ajax({
    44         type: "post",
    45         url: ajaxurl,
    46         data: {
    47             action: "move_sort_post",
    48             post_id: pid,
    49             direction: direction.toLowerCase() == 'up' ? 'up' : 'down'
    50         },
    51         dataType: "json",
    52         success: post_sorter_onMove,
    53         error: post_sorter_onError
    54     });
     43    jQuery.ajax({
     44        type: "post",
     45        url: ajaxurl,
     46        data: {
     47            action: "move_sort_post",
     48            post_id: pid,
     49            direction: direction.toLowerCase() == 'up' ? 'up' : 'down'
     50        },
     51        dataType: "json",
     52        success: post_sorter_onMove,
     53        error: post_sorter_onError
     54    });
    5555}
    5656
    5757function post_sorter_onMove(res) {
    58     window.location.href = window.location;
     58    window.location.href = window.location;
    5959}
     60
     61function enable_roles() {
     62    console.log(jQuery("#available_roles option:selected"));
     63    return !jQuery("#available_roles option:selected").remove().appendTo("#enabled_roles");
     64}
     65
     66function disable_roles() {
     67    return !jQuery("#enabled_roles option:selected").remove().appendTo("#available_roles"); 
     68}
  • post-sorter/trunk/page/general.php

    r730389 r804473  
    11<?php
    2     global $post_sorter;
     2global $post_sorter;
    33
    4     if( !empty( $_POST ) )
    5         $post_sorter->save_settings();
     4if( !empty( $_POST ) )
     5    $post_sorter->save_settings();
    66?>
    77
     
    99
    1010<div class="wrap">
    11     <h2><?php echo 'Post Sorter' ?></h2>
    12    
    13     <form id="post_sorter_settings" action="" method="post" class="post_sorter_form">
     11    <h2><?php _e( 'Post Sorter', 'post_sorter' ) ?></h2>
     12   
     13    <form id="post_sorter_settings" action="" method="post" class="post_sorter_form">
    1414        <h3><?php _e( 'Basic Settings', 'post_sorter' ) ?></h3>
    1515       
     
    4141            </tbody>
    4242        </table>
    43        
     43       
    4444        <br class="clear" />
    4545        <br class="clear" />
     
    120120            </tbody>
    121121        </table>
    122     </form>
     122    </form>
    123123</div>
    124124
  • post-sorter/trunk/post_sorter.php

    r730389 r804473  
    44  Plugin URI: http://intellisys.org/
    55  Description: Plugin for easy sorting of posts and pages by numeric value, both ascending and descending.
    6   Version: 1.3.1
     6  Version: 1.4
    77  Author: Lyubomir Gardev
    88  Author URI: http://rolice.intellisys.info/
     
    1414
    1515class PostSorter {
     16
    1617    /**
    1718     * Whether custom sorting is enabled
    1819     * @var bool
    1920     */
    20     private $_custom = FALSE;
     21    private $custom = FALSE;
     22
     23    private $plugin_data = NULL;
    2124
    2225    public function __construct() {
     
    2528
    2629    public function init() {
    27         load_plugin_textdomain( 'post_sorter', FALSE, plugin_dir_path(__FILE__) . '/lang/' );
     30        load_plugin_textdomain( 'post_sorter', FALSE, plugin_dir_path(__FILE__) . 'lang/' );
    2831
    2932        add_filter( 'manage_posts_columns', array( $this, 'add_sorter_column' ) );
     
    6669
    6770        add_action( 'admin_menu', array( $this, 'add_menu' ) );
    68         add_action( 'admin_init', array( $this, 'register_settings' ) );
     71        add_action( 'admin_init', array( $this, 'set_up' ) );
    6972    }
    7073
     
    7578        update_option( 'post_sorter_enabled', TRUE );
    7679        update_option( 'post_sorter_direction', 'ASC' );
     80        update_option( 'post_sorter_enabled_roles', array( 'administrator' ) );
    7781
    7882        $args = array(
     
    101105    }
    102106
     107    public function meta( $key ) {
     108        return isset( $this->plugin_data[ $key ] ) ? $this->plugin_data[ $key ] : NULL;
     109    }
     110
    103111    /**
    104112     * Enqueues scripts (JavaScripts) and CSS styles
     
    117125     * @return array          The columns to be rendered with added new column inside
    118126     */
    119     public function add_sorter_column( $columns ) {     
    120         $columns['sort'] = __( 'Sorting', 'post_sorter' );
     127    public function add_sorter_column( $columns ) {
     128        if( $this->can_use() )
     129            $columns['sort'] = __( 'Sorting', 'post_sorter' );
     130
    121131        return $columns;
    122132    }
     
    127137     */
    128138    public function show_sorter_column( $name ) {
    129         //global $post;
    130 
    131139        switch ( $name ) {
    132140            case 'sort':
     
    144152        $val = (int) get_post_meta( $post->ID, POST_SORTER_META_KEY, TRUE );
    145153       
    146         if( $this->_custom ) {
     154        if( $this->custom || !$this->can_use() ) {
    147155            echo '<div class="post_sorter">—</div>';
    148156            return;
     
    173181     */
    174182    public function add_sorter_sort( $columns ) {
    175         $columns['sort'] = 'sort';
     183        if( $this->can_use() )
     184            $columns['sort'] = 'sort';
     185
    176186        return $columns;
    177187    }
     
    211221        // If we have some hooks apply them - other plugins, etc.
    212222        if( 2 <= $this->_count_filter_hooks( 'post_sorter_join' ) ) {
    213             $this->_custom = TRUE;
     223            $this->custom = TRUE;
    214224            $sql = apply_filters( 'post_sorter_join', $sql );
    215225            return $sql;
     
    235245        // If we have some hooks apply them - other plugins, etc.
    236246        if( 2 <= $this->_count_filter_hooks( 'post_sorter_order' ) ) {
    237             $this->_custom = TRUE;
     247            $this->custom = TRUE;
    238248            $sql = apply_filters( 'post_sorter_order', $sql );
    239249            return $sql;
     
    252262        $post_id = (int) $post_id;
    253263
    254         if ( 0 >= $post_id )
    255             return;
    256 
    257         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    258             return;
    259 
    260         if ( !wp_verify_nonce( isset( $_POST['post_sorter'] ) ? $_POST['post_sorter'] : '', plugin_basename(__FILE__) ) )
    261             return;
    262 
    263         if ( !current_user_can( 'edit_post', $post_id ) )
     264        // No actual post to manage
     265        if( 0 >= $post_id )
     266            return;
     267
     268        // Do nothing on autosave
     269        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
     270            return;
     271
     272        // Verify nonce for expected request
     273        if( !wp_verify_nonce( isset( $_POST['post_sorter'] ) ? $_POST['post_sorter'] : '', plugin_basename(__FILE__) ) )
     274            return;
     275
     276        // Check permissions for modifing posts to do that (post edit screen)
     277        if( !current_user_can( 'edit_post', $post_id ) )
     278            return;
     279
     280        // Check if our plugin allows this user to save
     281        if( !$this->can_use() )
    264282            return;
    265283
    266284        $position = $_POST['post_sorter_inline'];
    267285
    268         if ( is_array( $position ) && !empty( $position ) )
     286        if( is_array( $position ) && !empty( $position ) )
    269287            $position = $position[0];
    270288
     
    278296     */
    279297    public function add_menu() {
    280         add_menu_page( 'Post Sorter :: General', 'Post Sorter', 'administrator', 'page-sorter', array( $this, 'render_menu' ) );
    281     }
    282 
    283     /**
    284      * Renders (outputs) admin menu - display the page behind the menu
    285      */
    286     public function render_menu() {
     298        add_menu_page(
     299            __( 'Post Sorter :: General', 'post_sorter' ),
     300            __( 'Post Sorter', 'post_sorter' ),
     301            'administrator',
     302            'post-sorter',
     303            array( $this, 'render_main_menu' )
     304        );
     305
     306        add_submenu_page(
     307            'post-sorter',
     308            __( 'Post Sorter :: Permissions', 'post_sorter' ),
     309            __( 'Permissions', 'post_sorter' ),
     310            'administrator',
     311            'post-sorter-permissions',
     312            array( $this, 'render_permissions_menu' )
     313        );
     314
     315        add_submenu_page(
     316            'post-sorter',
     317            __( 'Post Sorter :: About', 'post_sorter' ),
     318            __( 'About', 'post_sorter' ),
     319            'read',
     320            'post-sorter-about',
     321            array( $this, 'render_about_menu' )
     322        );
     323    }
     324
     325    /**
     326     * Renders (outputs) main admin menu - display the page behind the main menu
     327     */
     328    public function render_main_menu() {
    287329        include( plugin_dir_path(__FILE__) . 'page/general.php' );
    288330    }
    289331
    290     public function register_settings() {
     332    /**
     333     * Renders (outputs) admin menu for permissions - display the page behind the permissions menu
     334     */
     335    public function render_permissions_menu() {
     336        include( plugin_dir_path(__FILE__) . 'page/permissions.php' );
     337    }
     338
     339    /**
     340     * Renders (outputs) about page of the plugin
     341     */
     342    public function render_about_menu() {
     343        include( plugin_dir_path(__FILE__) . 'page/about.php' );
     344    }
     345
     346    // Initial plugin safe-initialization (admin initialized), check for correct state (plugin update), etc.
     347    public function set_up() {
     348        // Keep the proper condition on enabled roles (when wrong or plugin update)
     349        $roles = get_option( 'post_sorter_enabled_roles' );
     350        if(!$roles || !is_array( $roles ) || empty( $roles ) )
     351            update_option( 'post_sorter_enabled_roles', array( 'administrator' ) );
     352
     353        // Load plugin meta-data for runtime routines
     354        $this->plugin_data = get_plugin_data( __FILE__ );
    291355    }
    292356
     
    318382        update_option( 'post_sorter_join_clause', $own_risk ? $this->_sanitize_sql( $_POST['post_sorter_join_clause'] ) : '' );
    319383        update_option( 'post_sorter_order_by_clause', $own_risk ? $this->_sanitize_sql( $_POST['post_sorter_order_by_clause'] ) : '' );
     384    }
     385
     386    /**
     387     * Saves settings for the plugin
     388     */
     389    public function save_permissions() {
     390        $roles = ( isset( $_POST['enabled_roles'] ) && is_array( $_POST['enabled_roles'] ) )  ? $_POST['enabled_roles'] : array();
     391
     392        // Administrator should by put inside always, no matter of selection
     393        if(!in_array('administrator', $roles))
     394            $roles[] = 'administrator';
     395
     396        // To be sure we remove any possible duplicates
     397        $roles = array_unique($roles);
     398
     399        // Save the role selection
     400        update_option( 'post_sorter_enabled_roles', $roles );
    320401    }
    321402
     
    334415     */
    335416    public function add_meta_box() {
    336         add_meta_box( 'post_sorter', __( 'Post Sorter', 'post_sorter' ), array( $this, 'render_meta_box' ), NULL, 'side', 'core' );
     417        if( $this->can_use() )
     418            add_meta_box( 'post_sorter', __( 'Post Sorter', 'post_sorter' ), array( $this, 'render_meta_box' ), NULL, 'side', 'core' );
    337419    }
    338420
     
    346428        $val = (int) get_post_meta( $post->ID, POST_SORTER_META_KEY, TRUE );
    347429       
    348         if( $this->_custom ) {
     430        if( $this->custom ) {
    349431            echo '<div class="post_sorter">' . __( 'Custom sorting is enabled. Factor is ignored.', 'post_sorter' ) . '</div>';
    350432            return;
     
    385467        $post_id = (int) $post_id;
    386468
    387         if ( 0 >= $post_id )
     469        if ( 0 >= $post_id || !$this->can_use() )
    388470            return FALSE;
    389471
     
    464546    }
    465547   
    466    
     548    /**
     549     * Get roles available in the system (WordPress installation)
     550     * @param  bool  $all Whether to return all roles or only those which are not enabled
     551     * @return array      Array with available roles
     552     */
     553    public function get_available_roles( $all = FALSE ) {
     554        global $wp_roles;
     555
     556        $enabled_keys = get_option( 'post_sorter_enabled_roles' );
     557        $all_roles = $wp_roles->roles;
     558        $editable_roles = apply_filters( 'editable_roles', $all_roles );
     559
     560        if($all)
     561            return $editable_roles;
     562
     563        return array_diff_key( $editable_roles, array_flip( $enabled_keys ) );
     564    }
     565
     566    /**
     567     * Returns the roles that are enabled to access plugin functionality
     568     * @return array The roles which are capable of using the plugin
     569     */
     570    public function get_enabled_roles() {
     571        $enabled_keys = get_option( 'post_sorter_enabled_roles' );
     572        $roles = $this->get_available_roles( TRUE );
     573
     574        return array_intersect_key( $roles, array_flip( $enabled_keys ) );
     575    }
     576
     577    private function can_use() {
     578        $user = wp_get_current_user();
     579        $enabled = get_option( 'post_sorter_enabled_roles' );
     580        $enabled = is_array( $enabled ) ? $enabled : array();
     581
     582        error_reporting(E_ALL);
     583        ini_set('display_errors', 'yes');
     584
     585        return 0 < count( array_intersect( $user->roles, $enabled ) );
     586    }
    467587   
    468588    /* == = = = = = = = = = INERNAL FILTERS = = = = = = = = = = == */
     
    531651
    532652        $data = $this->_move( $post_id, $direction );
    533        
    534 
    535653        $result = new stdClass();
    536        
    537         //die( print_r( $data ) );
    538654
    539655        if ( is_array( $data ) )
  • post-sorter/trunk/readme.txt

    r730389 r804473  
    4040With the use of the expert settings and knowledge in SQL you could implement custom sorting. With such sorting the arrows will disappear, since they are no longer applicable.
    4141
     42Access management is now available. You can authorize specific roles to use the plugin from its section - submenu **Permissions**. By default, only administrator would be allowed to operate with the plugin.
     43
    4244== Installation ==
    4345
     
    64663. The screen of post edition - you could see the **metabox added with caption *Post Sorter***.
    65674. A screenshot of updated Post Sorter settings.
     685. Permissions screen where you can authorize roles to operate with Post Sorter.
    6669
    6770== Changelog ==
     71
     72= 1.4 =
     73Access to plugin funcionality is now granted with permissions. These permissions are given to roles from the new settings screen.
    6874
    6975= 1.3.1 =
  • post-sorter/trunk/uninstall.php

    r730333 r804473  
    55delete_option( 'post_sorter_enabled' );
    66delete_option( 'post_sorter_direction' );
     7delete_option( 'post_sorter_enabled_roles' );
    78delete_option( 'post_sorter_metakey' );
    89
Note: See TracChangeset for help on using the changeset viewer.