Plugin Directory

Changeset 1497254


Ignore:
Timestamp:
09/16/2016 11:44:07 PM (9 years ago)
Author:
marclarr
Message:

updated to version 1.4.0

Location:
fast-page-switch/trunk
Files:
5 added
2 edited

Legend:

Unmodified
Added
Removed
  • fast-page-switch/trunk/fast-page-switch.php

    r1190938 r1497254  
    11<?php
    2 
    32/*
    4 Plugin Name: Fast Page Switch
    5 Plugin URI: http://gravitysupport.com
    6 Description: Lets you quickly switch pages in admin edit view.
    7 Version: 1.3.1
    8 Author: Marc Wiest
    9 Author URI: http://gravitysupport.com
     3Plugin Name:    Fast Page Switch
     4Plugin URI:     https://marcwiest.com
     5Description:    Save time switching between posts of any post-type in wp-admin.
     6Version:        1.4
     7Author:         Marc Wiest
     8Author URI:     https://marcwiest.com
     9License:        GPL-2.0+
     10License URI:    http://www.gnu.org/licenses/gpl-2.0.txt
     11Text Domain:    fast-page-switch
     12Domain Path:    /languages
    1013*/
    1114
    12 if ( ! defined( 'WPINC' ) )
    13     wp_die( 'Please don\'t load this file directly.' );
     15// Abort if accessed directly.
     16if ( ! defined( 'WPINC' ) ) die;
    1417
    15 define( 'FPS_I18N', 'fast-page-switch' );
    1618define( 'FPS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
    1719define( 'FPS_PLUGIN_URL', plugins_url( '/', __FILE__ ) );
    1820
     21global $fps_default_types;
     22$fps_default_types = array(
     23    'post' => __('Posts'), // translated by wp core
     24    'page' => __('Pages'), // translated by wp core
     25);
     26
    1927/**
    20  * Admin Scripts & Styles
     28 * Load Plugin Text Domain
     29 */
     30load_plugin_textdomain( 'fast-page-switch', false, FPS_PLUGIN_DIR.'languages/' );
     31
     32/**
     33 * Add Plugin Action Links
     34 */
     35add_filter( 'plugin_action_links_'.plugin_basename(__FILE__), 'fps_add_action_links' );
     36function fps_add_action_links( $links )
     37{
     38    $links[] = '<a href="'.admin_url('options-general.php?page=Fast+Page+Switch').'">'.__('Settings','fast-page-switch').'</a>';
     39    return $links;
     40}
     41
     42/**
     43 * Add Admin Settings Page
     44 */
     45include_once FPS_PLUGIN_DIR.'fps-admin.php';
     46
     47/**
     48 * Enqueue Admin Scripts & Styles
    2149 */
    2250add_action( 'admin_enqueue_scripts', 'fps_admin_scripts' );
    23 function fps_admin_scripts() 
     51function fps_admin_scripts()
    2452{
     53    global $fps_default_types;
     54    $user_types = get_option( 'fps_post_types', $fps_default_types );
     55
    2556    $screen = get_current_screen();
    26 
    2757    $post_type = $screen->post_type;
    2858    $action = $screen->action;
    2959    $action = empty($action) && isset($_GET['action']) ? $_GET['action'] : $action;
    3060
    31     if ( ( 'page' == $post_type || 'post' == $post_type ) && ('add' == $action || 'edit' == $action) ) :
     61    if ( in_array( $post_type, array_keys($user_types) ) && ('add' == $action || 'edit' == $action) ) {
    3262        wp_enqueue_style( 'select2', FPS_PLUGIN_URL.'assets/css/select2.css', array(), '4.0.0' );
    3363        wp_enqueue_script( 'select2', FPS_PLUGIN_URL.'assets/js/select2.min.js', array('jquery'), '4.0.0' );
    34     endif;
    35 }
    36 
    37 /**
    38  * Metabox Content
    39  */
    40 function fps_metabox_markup()
    41 {
    42     $pages = get_pages( array(
    43         'post_type' => 'page',
    44         'post_status' => apply_filters( 'fps_get_pages_by_post_status', 'publish,private,draft,auto-draft,future,pending' ),
    45     ) );
    46 
    47     $posts = get_posts( array(
    48         'post_type' => 'post',
    49         'post_status' => apply_filters( 'fps_get_posts_by_post_status', array('private','draft','auto-draft','future','pending','publish') ),
    50         'posts_per_page' => -1,
    51     ) );
    52 
    53     $selected = '';
    54     $page_selected = 0;
    55     $post_selected = 0;
    56     foreach( $pages as $page ) :
    57 
    58         if ( isset($_GET['post']) ) {
    59 
    60             $selected = $_GET['post'];
    61        
    62             if ( $page->ID == $selected ) {
    63                 $page_selected = $selected;
    64             } else {
    65                 $post_selected = $selected;
    66             }
    67 
    68         }
    69 
    70     endforeach;
    71 
    72     $args = array(
    73         'depth' => 0,
    74         'selected' => $page_selected, // isset($_GET['post']) ? $_GET['post'] : 0,
    75     );
    76 
    77     if ( !empty($pages) || !empty($posts) ) :
    78 
    79         $jquery = "<script>
    80            
    81             jQuery(document).ready(function($) {
    82                 'use strict';
    83 
    84                 var fps = $('#fast-page-switch');
    85    
    86                 if ( typeof fps.select2() == 'object' ) {
    87                     fps.select2({
    88                         theme: 'classic',
    89                         placeholder: 'Switch'
    90                     });
    91                 }
    92 
    93                 fps.on( 'change', function (event) {
    94                     event.preventDefault;
    95 
    96                     if ( fps.val() !== '".$selected."' ) {
    97 
    98                         location_change( $(this).val() );
    99 
    100                         // - Select2 was getting stuck on the new value when a
    101                         //   page change was prevented due to unsaved changes.
    102                         // - To be backwards compatible with Select2 version 3,
    103                         //   the old method for reseting a value is also used.
    104                         fps.select2('val','".$selected."'); // Select2 v3
    105                         fps.val('".$selected."').trigger('change'); // Select2 v4
    106                     }
    107                 });
    108 
    109                 function location_change( post_id ) {
    110                     var admin_url = '".trailingslashit(admin_url())."';
    111                     window.location.href = admin_url + 'post.php?post=' + post_id + '&action=edit';
    112                 }
    113 
    114             });
    115 
    116         </script>";
    117 
    118         $html = '<select id="fast-page-switch" style="width:100%;">';
    119         $html .= '<option></option>';
    120         $html .= '<optgroup label="'.__('Pages',FPS_I18N).'">';
    121         $html .= walk_page_dropdown_tree( $pages, $args['depth'], $args );
    122         $html .= '</optgroup>';
    123         $html .= '<optgroup label="'.__('Posts',FPS_I18N).'">';
    124         foreach( $posts as $post ) :
    125             $s = $post_selected == $post->ID ? ' selected="selected"' : '';
    126             $html .= '<option'.$s.' value="'.$post->ID.'">'.$post->post_title.'</option>';
    127         endforeach;
    128         $html .= '</optgroup>';
    129         $html .= '</select>';
    130 
    131     endif;
    132 
    133     echo $jquery.$html;
     64    }
    13465}
    13566
     
    13869 */
    13970add_action( 'add_meta_boxes', 'fps_add_metabox' );
    140 function fps_add_metabox() 
     71function fps_add_metabox()
    14172{
    142     if ( ! current_user_can('moderate_comments') )
    143         return; // exit if not admin or editor
     73    global $fps_default_types;
     74    $user_types = get_option( 'fps_post_types', $fps_default_types );
    14475
    145     add_meta_box(
    146         'fps-metabox-page',
    147         __( 'Fast Page Switch', FPS_I18N ),
    148         'fps_metabox_markup',
    149         'page',
    150         'side',
    151         'high',
    152         null
    153     );
     76    $screen = get_current_screen();
     77    $type_name = $screen->post_type;
    15478
    155     add_meta_box(
    156         'fps-metabox-post',
    157         __( 'Fast Page Switch', FPS_I18N ),
    158         'fps_metabox_markup',
    159         'post',
    160         'side',
    161         'high',
    162         null
    163     );
     79    if ( ! in_array( $type_name, array_keys($user_types) ) )
     80        return;
     81
     82    // test if the current user can edit at least one of the post-types
     83    $user_may_see_metabox = false;
     84    foreach( $user_types as $type => $label ) {
     85        $type_obj = get_post_type_object( $type );
     86        if ( current_user_can( $type_obj->cap->edit_posts ) ) {
     87            $user_may_see_metabox = true;
     88            break;
     89        }
     90    }
     91
     92    if ( $user_may_see_metabox ) {
     93        add_meta_box(
     94            'fps-metabox-'.$type_name,
     95            esc_html__( 'Fast Page Switch', 'fast-page-switch' ),
     96            '_fps_metabox_cb',
     97            $type_name,
     98            'side',
     99            'high',
     100            null
     101        );
     102    }
    164103}
    165104
     105function _fps_metabox_cb( $post )
     106{
     107    global $fps_default_types;
     108    $user_types = get_option( 'fps_post_types', $fps_default_types );
    166109
     110    $options = array();
     111    foreach( $user_types as $type => $label ) {
    167112
     113        $all_posts = get_posts( array(
     114            'post_type' => $type,
     115            'post_status' => get_option( 'fps_post_statuses', array('private','draft','future','pending','publish') ),
     116            'posts_per_page' => -1,
     117        ) );
     118
     119        // only include posts that the current user can edit
     120        $user_can_edit_post = get_post_type_object($type)->cap->edit_post;
     121        $user_posts = array();
     122        foreach( $all_posts as $ap ) {
     123            if ( ! current_user_can( $user_can_edit_post, $ap->ID ) )
     124                continue;
     125            $user_posts[] = $ap;
     126        }
     127
     128        if ( ! empty($user_posts) ) {
     129            $options[ $type ]['label'] = $label;
     130            $options[ $type ]['posts'] = $user_posts;
     131        }
     132    }
     133
     134    if ( ! empty($options) ) {
     135        ?>
     136        <script>
     137        jQuery(document).ready(function($) {
     138            'use strict';
     139
     140            var fps = $('#fast-page-switch'),
     141                s2Exists = $.isFunction( $.fn.select2 );
     142
     143            if ( s2Exists ) {
     144                fps.select2({
     145                    theme: 'classic',
     146                    placeholder: 'Switch'
     147                });
     148            }
     149
     150            fps.on( 'change', function(e) {
     151                e.preventDefault;
     152
     153                if ( fps.val() !== '<?php echo $post->ID; ?>' ) {
     154
     155                    location_change( $(this).val() );
     156
     157                    // - Select2 was getting stuck on the new value when a
     158                    //   page change was prevented due to unsaved changes.
     159                    // - To be backwards compatible with Select2 version 3,
     160                    //   the old method for reseting a value is also used.
     161                    if ( s2Exists ) {
     162                        fps.select2('val','<?php echo $post->ID; ?>'); // Select2 v3
     163                    }
     164                    fps.val('<?php echo $post->ID; ?>').trigger('change'); // Select2 v4
     165                }
     166            });
     167
     168            function location_change( post_id ) {
     169                var admin_url = '<?php trailingslashit(admin_url()); ?>';
     170                window.location.href = admin_url + 'post.php?post=' + post_id + '&action=edit';
     171            }
     172
     173        });
     174        </script>
     175        <?php
     176
     177        $html = '<select id="fast-page-switch" style="width:100%;">';
     178        $html .= '<option></option>';
     179        foreach( $options as $opt ) {
     180            $html .= '<optgroup label="'.$opt['label'].'">';
     181            foreach( $opt['posts'] as $p ) {
     182                $selected = $post->ID == $p->ID ? ' selected="selected"' : '';
     183                $html .= "<option value='{$p->ID}' $selected>{$p->post_title}</option>";
     184            }
     185        }
     186        $html .= '</optgroup>';
     187        echo $html . '</select>';
     188
     189    } else {
     190
     191        // this metabox hides itself if there are no posts to show
     192        echo "<style>#fps-metabox-{$post->post_type}{display:none;}</style>";
     193
     194    }
     195}
  • fast-page-switch/trunk/readme.txt

    r1196723 r1497254  
    11=== Plugin Name ===
    22Contributors: marclarr
    3 Tags: fast, page, pages, post, posts, switch, admin, edit, easy, quick
     3Tags: page, pages, post, posts, switch, admin, ui, edit, easy, quick, fast
    44Requires at least: 3.1
    5 Tested up to: 4.2.2
     5Tested up to: 4.7
    66Stable tag: trunk
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
    99
    10 Save time switching between posts & pages in admin.
     10Save time switching between posts of any post-type in wp-admin.
    1111
    1212== Description ==
    1313
    14 This plugin adds a metabox to the edit screen for posts and pages. The metabox lets you quickly switch between all available posts and pages using the Select2 jQuery plugin. No need to visit "All Pages/Posts“ first.
     14This plugin adds a metabox to the edit screen for any post type. The metabox lets you quickly switch between all available posts using the Select2 jQuery plugin. No need to visit "All Posts“ first. You can use the settings page to determine for which post types (e.g. Pages, Posts, etc.) the metabox should be available.
    1515
    16 Please do not use the rating system for your support requests. Simply leave a message in the actual [support forum](https://wordpress.org/support/plugin/fast-page-switch) and I will get to you ASAP.
     16Please do not use the rating system for your support requests. Simply leave a message in the actual [support forum](https://wordpress.org/support/plugin/fast-page-switch) and I will get back to you <abbr title="as soon as possible">ASAP</abbr>.
    1717
    1818<em>If this plugin saves you time, please consider supporting it with a good rating. Thanks.</em>
     
    2020== Screenshots ==
    2121
    22 1. A quick preview of the fast page switch metabox and it's functionality.
     221. The fast page switch metabox and it's functionality.
     232. Preview of the admin settings page (since v1.4).
    2324
    2425== Changelog ==
     26
     27= 1.4.0 =
     28* Added support for custom post types.
     29* Created settings page for managing post types shown.
     30* To keep the database clean, settings are delete via uninstall.php when plugin is deleted.
     31* Contrary to version 1.3.1, the metabox now only shows if the current user can edit at least one of the available post-types which also has at least one post the user is allowed to edit.
     32* Removed filter "fps_get_pages_by_post_status" in favor of admin setting "Post Statuses."
     33* Removed filter "fps_get_posts_by_post_status" in favor of admin setting "Post Statuses."
     34* The plugin is now translatable.
    2535
    2636= 1.3.1 =
Note: See TracChangeset for help on using the changeset viewer.