Plugin Directory

Changeset 1691979

Timestamp:
07/06/2017 04:12:48 PM (7 years ago)
Author:
nbachiyski
Message:

Long-overdue cleanup

Location:
shareadraft/trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • shareadraft/trunk/readme.txt

    r483173 r1691979  
    11=== Share a Draft ===
    2 Contributors: nbachiyski
     2Contributors: nbachiyski
    33Tags: post, post, draft, posts, drafts, share, sharing
    44Requires at least: 3.2
    5 Tested up to: 3.3
     5Tested up to:
    66Stable tag: 1.4
    77
    8 Let your friends preview one of your drafts, without giving them permissions to edit posts in your blog
     8Share private preview links to your drafts
    99
    1010== Description ==
     
    2727== Screenshots ==
    2828
    29 1. The screen at Manage -> Share a Draft lets you choose a draft and monitor/delete your previous shares.
     291. From Posts → Share a Draft you can share a new draft.
     30
     312. From Posts → Share a Draft you can managed your shared drafts.
     32
    3033
    3134== Changelog ==
     35
     36
     37
     38
     39
     40
    3241
    3342= 1.4 =
  • shareadraft/trunk/shareadraft.php

    r483170 r1691979  
    22/*
    33Plugin Name: Share a Draft
    4 Plugin URI: http://wordpress.org/extend/plugins/shareadraft/
    5 Description: Let your friends preview one of your drafts, without giving them permissions to edit posts in your blog.
     4Plugin URI: http://wordpress.org/plugins/shareadraft/
     5Description:
    66Author: Nikolay Bachiyski
    7 Version: 1.4
    8 Author URI: http://nikolay.bg/
     7Version: 1.
     8Author URI: http/
    99Text Domain: shareadraft
    10 Generated At: www.wp-fun.co.uk;
    11 */
    12 
    13 if (!class_exists('ShareADraft')):
    14 class ShareADraft   {
    15     var $admin_options_name = "ShareADraft_options";
    16 
    17     function __construct(){
    18         add_action('init', array($this, 'init'));
    19     }
    20 
    21     function init() {
    22         global $current_user;
    23         add_action('admin_menu', array($this, 'add_admin_pages'));
    24         add_filter('the_posts', array($this, 'the_posts_intercept'));
    25         add_filter('posts_results', array($this, 'posts_results_intercept'));
    26 
    27         $this->admin_options = $this->get_admin_options();
    28         $this->admin_options = $this->clear_expired($this->admin_options);
    29         $this->user_options = ($current_user->id > 0 && isset($this->admin_options[$current_user->id]))? $this->admin_options[$current_user->id] : array();
    30 
    31         $this->save_admin_options();
    32         load_plugin_textdomain('shareadraft', PLUGINDIR . '/shareadraft/languages');
    33 
    34         if (isset($_GET['page']) && $_GET['page'] == plugin_basename(__FILE__))
    35             $this->admin_page_init();
    36     }
    37 
    38     function admin_page_init() {
    39         wp_enqueue_script('jquery');
    40         add_action('admin_head', array($this, 'print_admin_css'));
    41         add_action('admin_head', array($this, 'print_admin_js'));
    42     }
    43 
    44     function get_admin_options() {
    45         $saved_options = get_option($this->admin_options_name);
    46         return is_array($saved_options)? $saved_options : array();
    47     }
    48 
    49     function save_admin_options(){
    50         global $current_user;
    51         if ($current_user->id > 0) {
    52             $this->admin_options[$current_user->id] = $this->user_options;
    53         }
    54         update_option($this->admin_options_name, $this->admin_options);
    55     }
    56 
    57     function clear_expired($all_options) {
    58         $all = array();
    59         foreach($all_options as $user_id => $options) {
     10Domain Path: /languages
     11*/
     12
     13if ( ! class_exists( 'Share_a_Draft' ) ) :
     14    class Share_a_Draft {
     15        var $admin_options_name = 'ShareADraft_options';
     16        var $shared_post = null;
     17
     18        function __construct() {
     19            add_action( 'init', array( $this, 'init' ) );
     20        }
     21
     22        function init() {
     23            global $current_user;
     24            add_action( 'admin_menu', array( $this, 'add_admin_pages' ) );
     25            add_filter( 'the_posts', array( $this, 'the_posts_intercept' ) );
     26            add_filter( 'posts_results', array( $this, 'posts_results_intercept' ) );
     27
     28            $this->admin_options = $this->get_admin_options();
     29            $this->admin_options = $this->clear_expired( $this->admin_options );
     30            $this->user_options = array();
     31            if ( $current_user->ID > 0 && isset( $this->admin_options[ $current_user->ID ] ) ) {
     32                $this->user_options = $this->admin_options[ $current_user->ID ];
     33            }
     34            $this->save_admin_options();
     35            load_plugin_textdomain( 'shareadraft', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
     36
     37            if ( isset( $_GET['page'] ) && $_GET['page'] === plugin_basename( __FILE__ ) ) {
     38                $this->admin_page_init();
     39            }
     40        }
     41
     42        function admin_page_init() {
     43            wp_enqueue_script( 'jquery' );
     44            add_action( 'admin_head', array( $this, 'print_admin_css' ) );
     45            add_action( 'admin_head', array( $this, 'print_admin_js' ) );
     46        }
     47
     48        function get_admin_options() {
     49            $saved_options = get_option( $this->admin_options_name );
     50            return is_array( $saved_options )? $saved_options : array();
     51        }
     52
     53        function save_admin_options() {
     54            global $current_user;
     55            if ( $current_user->ID > 0 ) {
     56                $this->admin_options[ $current_user->ID ] = $this->user_options;
     57            }
     58            update_option( $this->admin_options_name, $this->admin_options );
     59        }
     60
     61        function clear_expired( $all_options ) {
     62            $all = array();
     63            foreach ( $all_options as $user_id => $options ) {
     64                $shared = array();
     65                if ( ! isset( $options['shared'] ) || ! is_array( $options['shared'] ) ) {
     66                    continue;
     67                }
     68                foreach ( $options['shared'] as $share ) {
     69                    if ( $share['expires'] < time() ) {
     70                        continue;
     71                    }
     72                    $shared[] = $share;
     73                }
     74                $options['shared'] = $shared;
     75                $all[ $user_id ] = $options;
     76            }
     77            return $all;
     78        }
     79
     80        function add_admin_pages() {
     81            add_submenu_page( 'edit.php', __( 'Share a Draft', 'shareadraft' ), __( 'Share a Draft', 'shareadraft' ),
     82            'edit_posts', __FILE__, array( $this, 'output_existing_menu_sub_admin_page' ) );
     83        }
     84
     85        function calculate_seconds( $params ) {
     86            $exp = 60;
     87            $multiply = 60;
     88            if ( isset( $params['expires'] ) && ( $e = intval( $params['expires'] ) ) ) {
     89                $exp = $e;
     90            }
     91            $mults = array(
     92                'm' => MINUTE_IN_SECONDS,
     93                'h' => HOUR_IN_SECONDS,
     94                'd' => DAY_IN_SECONDS,
     95                'w' => WEEK_IN_SECONDS,
     96            );
     97            if ( isset( $params['measure'] ) && isset( $mults[ $params['measure'] ] ) ) {
     98                $multiply = $mults[ $params['measure'] ];
     99            }
     100            return $exp * $multiply;
     101        }
     102
     103        function process_new_share( $params ) {
     104            global $current_user;
     105            if ( isset( $params['post_id'] ) ) {
     106                $p = get_post( $params['post_id'] );
     107                if ( ! $p ) {
     108                    return __( 'There is no such post!', 'shareadraft' );
     109                }
     110                if ( 'publish' === get_post_status( $p ) ) {
     111                    return __( 'The post is published!', 'shareadraft' );
     112                }
     113                if ( ! current_user_can( 'edit_post', $p->ID ) ) {
     114                    return __( 'Sorry, you are not allowed to share posts you can’t edit.', 'shareadraft' );
     115                }
     116                $this->user_options['shared'][] = array(
     117                    'id' => $p->ID,
     118                    'expires' => time() + $this->calculate_seconds( $params ),
     119                    'key' => uniqid( 'baba' . $p->ID . '_' ),
     120                );
     121                $this->save_admin_options();
     122            }
     123        }
     124
     125        function process_delete( $params ) {
     126            if ( ! isset( $params['key'] ) ||
     127            ! isset( $this->user_options['shared'] ) ||
     128            ! is_array( $this->user_options['shared'] ) ) {
     129                return '';
     130            }
    60131            $shared = array();
    61             if (!isset($options['shared']) || !is_array($options['shared'])) {
    62                 continue;
    63             }
    64             foreach($options['shared'] as $share) {
    65                 if ($share['expires'] < time()) {
     132            ) {
     133               
     134           
     135           
     136               
    66137                    continue;
    67138                }
    68139                $shared[] = $share;
    69140            }
    70             $options['shared'] = $shared;
    71             $all[$user_id] = $options;
    72         }
    73         return $all;
    74     }
    75 
    76     function add_admin_pages(){
    77         add_submenu_page("edit.php", __('Share a Draft', 'shareadraft'), __('Share a Draft', 'shareadraft'),
    78             'edit_posts', __FILE__, array($this, 'output_existing_menu_sub_admin_page'));
    79     }
    80 
    81     function calculate_seconds($params) {
    82         $exp = 60;
    83         $multiply = 60;
    84         if (isset($params['expires']) && ($e = intval($params['expires']))) {
    85             $exp = $e;
    86         }
    87         $mults = array('s' => 1, 'm' => 60, 'h' => 3600, 'd' => 24*3600);
    88         if (isset($params['measure']) && isset($mults[$params['measure']])) {
    89             $multiply = $mults[$params['measure']];
    90         }
    91         return $exp * $multiply;
    92     }
    93 
    94     function process_post_options($params) {
    95         global $current_user;
    96         if (isset($params['post_id'])) {
    97             $p = get_post($params['post_id']);
    98             if (!$p) {
    99                 return __('There is no such post!', 'shareadraft');
    100             }
    101             if ('publish' == get_post_status($p)) {
    102                 return __('The post is published!', 'shareadraft');
    103             }
    104             $this->user_options['shared'][] = array('id' => $p->ID,
    105                 'expires' => time() + $this->calculate_seconds($params),
    106                 'key' => uniqid('baba'.$p->ID.'_'));
     141            $this->user_options['shared'] = $shared;
    107142            $this->save_admin_options();
    108         }   
    109     }
    110 
    111     function process_delete($params) {
    112         if (!isset($params['key']) ||
    113             !isset($this->user_options['shared']) ||
    114         !is_array($this->user_options['shared'])) {
    115             return '';
    116         }
    117         $shared = array();
    118         foreach($this->user_options['shared'] as $share) {
    119             if ($share['key'] == $params['key']) {
    120                 continue;
    121             }
    122             $shared[] = $share;
    123         }
    124         $this->user_options['shared'] = $shared;
    125         $this->save_admin_options();
    126     }
    127 
    128     function process_extend($params) {
    129         if (!isset($params['key']) ||
    130             !isset($this->user_options['shared']) ||
    131             !is_array($this->user_options['shared'])) {
    132             return '';
    133         }
    134         $shared = array();
    135         foreach($this->user_options['shared'] as $share) {
    136             if ($share['key'] == $params['key']) {
    137                 $share['expires'] += $this->calculate_seconds($params);
    138             }
    139             $shared[] = $share;
    140         }
    141         $this->user_options['shared'] = $shared;
    142         $this->save_admin_options();
    143     }
    144 
    145     function get_drafts() {
    146         global $current_user;
    147         $my_drafts = get_users_drafts($current_user->id);
    148         $my_scheduled = $this->get_users_future($current_user->id);
    149         $pending = get_others_pending($current_user->id);
    150         $others_drafts = get_others_drafts($current_user->id);
    151         $drafts_struct = array(
     143        }
     144
     145        function process_extend( $params ) {
     146            if ( ! isset( $params['key'] ) ||
     147            ! isset( $this->user_options['shared'] ) ||
     148            ! is_array( $this->user_options['shared'] ) ) {
     149                return '';
     150            }
     151            $shared = array();
     152            foreach ( $this->user_options['shared'] as $share ) {
     153                if ( $share['key'] === $params['key'] ) {
     154                    if ( ! current_user_can( 'edit_post', $share['id'] ) ) {
     155                        return __( 'Sorry, you are not allowed to share posts you can’t edit.', 'shareadraft' );
     156                    }
     157                    $share['expires'] += $this->calculate_seconds( $params );
     158                }
     159                $shared[] = $share;
     160            }
     161            $this->user_options['shared'] = $shared;
     162            $this->save_admin_options();
     163        }
     164
     165        function get_drafts() {
     166            global $current_user;
     167            $unpublished_statuses = array( 'pending', 'draft', 'future', 'private' );
     168            $my_unpublished = get_posts( array(
     169                'post_status' => $unpublished_statuses,
     170                'author' => $current_user->ID,
     171                // some environments, like WordPress.com hook on those filters
     172                // for an extra caching layer
     173                'suppress_filters' => false,
     174            ) );
     175            $others_unpublished = get_posts( array(
     176                'post_status' => $unpublished_statuses,
     177                'author' => -$current_user->ID,
     178                'suppress_filters' => false,
     179                'perm' => 'editable',
     180            ) );
     181            $draft_groups = array(
    152182            array(
    153                 __('Your Drafts:', 'shareadraft'),
    154                 count($my_drafts),
    155                 $my_drafts,
     183                'label' => __( 'My unpublished posts:', 'shareadraft' ),
     184                'posts' => $my_unpublished,
    156185            ),
    157186            array(
    158                 __('Your Scheduled Posts:', 'shareadraft'),
    159                 count($my_scheduled),
    160                 $my_scheduled,
     187                'label' => __( 'Others’ unpubilshed posts:', 'shareadraft' ),
     188                'posts' => $others_unpublished,
    161189            ),
    162             array(
    163                 __('Pending Review:', 'shareadraft'),
    164                 count($pending),
    165                 $pending,
    166             ),
    167             array(
    168                 __('Others&#8217; Drafts:', 'shareadraft'),
    169                 count($others_drafts),
    170                 $others_drafts,
    171             ),
    172         );
    173         return $drafts_struct;
    174     }
    175    
    176     function get_users_future($user_id) {
    177         global $wpdb;
    178         $query = $wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'future' AND post_author = %d ORDER BY post_modified DESC", $user_id);
    179         return $wpdb->get_results( $query );
    180     }
    181 
    182     function get_shared() {
    183         if (!isset($this->user_options['shared']) || !is_array($this->user_options['shared'])) {
    184             return array();
    185         }
    186         return $this->user_options['shared'];
    187     }
    188 
    189     function friendly_delta($s) {
    190         $m = (int)($s/60);
    191         $free_s = $s - $m*60;
    192         $h = (int)($s/3600);
    193         $free_m = (int)(($s - $h*3600)/60);
    194         $d = (int)($s/(24*3600));
    195         $free_h = (int)(($s - $d*(24*3600))/3600);
    196         if ($m < 1) {
    197             $res = array($s);
    198         } elseif ($h < 1) {
    199             $res = array($free_s, $m);
    200         } elseif ($d < 1) {
    201             $res = array($free_s, $free_m, $h);
    202         } else {
    203             $res = array($free_s, $free_m, $free_h, $d);
    204         }
    205         $names = array();
    206         if (isset($res[0]))
    207             $names[] = sprintf(__ngettext('%d second', '%d seconds', $res[0], 'shareadraft'), $res[0]);
    208         if (isset($res[1]))
    209             $names[] = sprintf(__ngettext('%d minute', '%d minutes', $res[1], 'shareadraft'), $res[1]);
    210         if (isset($res[2]))
    211             $names[] = sprintf(__ngettext('%d hour', '%d hours', $res[2], 'shareadraft'), $res[2]);
    212         if (isset($res[3]))
    213             $names[] = sprintf(__ngettext('%d day', '%d days', $res[3], 'shareadraft'), $res[3]);
    214         return implode(', ', array_reverse($names));
    215     }
    216 
    217     function output_existing_menu_sub_admin_page(){
    218         if (isset($_POST['shareadraft_submit'])) {
    219             $msg = $this->process_post_options($_POST);
    220         } elseif (isset($_POST['action']) && $_POST['action'] == 'extend') {
    221             $msg = $this->process_extend($_POST);
    222         } elseif (isset($_GET['action']) && $_GET['action'] == 'delete') {
    223             $msg = $this->process_delete($_GET);
    224         }
    225         $drafts_struct = $this->get_drafts();
    226 ?>
     190            );
     191            return $draft_groups;
     192        }
     193
     194        function get_shared() {
     195            if ( ! isset( $this->user_options['shared'] ) || ! is_array( $this->user_options['shared'] ) ) {
     196                return array();
     197            }
     198            return $this->user_options['shared'];
     199        }
     200
     201        function friendly_delta( $s ) {
     202            $m = (int) ( $s / MINUTE_IN_SECONDS );
     203            $h = (int) ( $s / HOUR_IN_SECONDS );
     204            $free_m = (int) ( ( $s - $h * HOUR_IN_SECONDS ) / MINUTE_IN_SECONDS );
     205            $d = (int) ( $s / DAY_IN_SECONDS );
     206            $free_h = (int) ( ( $s - $d * DAY_IN_SECONDS ) / HOUR_IN_SECONDS );
     207            if ( $m < 1 ) {
     208                $res = array();
     209            } elseif ( $h < 1 ) {
     210                $res = array( $m );
     211            } elseif ( $d < 1 ) {
     212                $res = array( $free_m, $h );
     213            } else {
     214                $res = array( $free_m, $free_h, $d );
     215            }
     216            $names = array();
     217            if ( isset( $res[0] ) ) {
     218                $names[] = sprintf( _n( '%d minute', '%d minutes', $res[0], 'shareadraft' ), $res[0] );
     219            }
     220            if ( isset( $res[1] ) ) {
     221                $names[] = sprintf( _n( '%d hour', '%d hours', $res[1], 'shareadraft' ), $res[1] );
     222            }
     223            if ( isset( $res[2] ) ) {
     224                $names[] = sprintf( _n( '%d day', '%d days', $res[2], 'shareadraft' ), $res[2] );
     225            }
     226            return implode( ', ', array_reverse( $names ) );
     227        }
     228
     229        function output_existing_menu_sub_admin_page() {
     230            $msg = '';
     231            if ( isset( $_POST['shareadraft_submit'] ) ) {
     232                check_admin_referer( 'shareadraft-new-share' );
     233                $msg = $this->process_new_share( $_POST );
     234            } elseif ( isset( $_POST['action'] ) && $_POST['action'] === 'extend' ) {
     235                check_admin_referer( 'shareadraft-extend' );
     236                $msg = $this->process_extend( $_POST );
     237            } elseif ( isset( $_GET['action'] ) && $_GET['action'] === 'delete' ) {
     238                check_admin_referer( 'shareadraft-delete' );
     239                $msg = $this->process_delete( $_GET );
     240            }
     241            $draft_groups = $this->get_drafts();
     242    ?>
    227243    <div class="wrap">
    228         <h2><?php _e('Share a Draft', 'shareadraft'); ?></h2>
    229 <?php   if ($msg):?>
     244        <h2><?php _e(); ?></h2>
     245<?php   if (:?>
    230246        <div id="message" class="updated fade"><?php echo $msg; ?></div>
    231247<?php   endif;?>
    232         <h3><?php _e('Currently shared drafts', 'shareadraft'); ?></h3>
     248        <h3><?php _e(); ?></h3>
    233249        <table class="widefat">
    234250            <thead>
    235251            <tr>
    236                 <th><?php _e('ID', 'shareadraft'); ?></th>
    237                 <th><?php _e('Title', 'shareadraft'); ?></th>
    238                 <th><?php _e('Link', 'shareadraft'); ?></th>
    239                 <th><?php _e('Expires after', 'shareadraft'); ?></th>
    240                 <th colspan="2" class="actions"><?php _e('Actions', 'shareadraft'); ?></th>
     252                <th><?php _e(); ?></th>
     253                <th><?php _e(); ?></th>
     254                <th><?php _e(); ?></th>
     255                <th><?php _e(); ?></th>
     256                <th colspan="2" class="actions"><?php _e(); ?></th>
    241257            </tr>
    242258            </thead>
     
    244260<?php
    245261        $s = $this->get_shared();
    246         foreach($s as $share):
    247             $p = get_post($share['id']);
    248             $url = get_bloginfo('url') . '/?p=' . $p->ID . '&shareadraft='. $share['key'];
    249 ?>
    250             <tr>
    251                 <td><?php echo $p->ID; ?></td>
    252                 <td><?php echo $p->post_title; ?></td>
    253                 <!-- TODO: make the draft link selecatble -->
    254                 <td><a href="<?php echo esc_url( $url ); ?>"><?php echo esc_html( $url ); ?></a></td>
    255                 <td><?php echo $this->friendly_delta($share['expires'] - time()); ?></td>
    256                 <td class="actions">
    257                     <a class="shareadraft-extend edit" id="shareadraft-extend-link-<?php echo $share['key']; ?>"
    258                         href="javascript:shareadraft.toggle_extend('<?php echo $share['key']; ?>');">
    259                             <?php _e('Extend', 'shareadraft'); ?>
    260                     </a>
    261                     <form class="shareadraft-extend" id="shareadraft-extend-form-<?php echo $share['key']; ?>"
    262                         action="" method="post">
    263                         <input type="hidden" name="action" value="extend" />
    264                         <input type="hidden" name="key" value="<?php echo $share['key']; ?>" />
    265                         <input type="submit" class="button" name="shareadraft_extend_submit"
    266                             value="<?php echo attribute_escape(__('Extend', 'shareadraft')); ?>"/>
    267 <?php _e('by', 'shareadraft');?>
     262foreach ( $s as $share ) :
     263    $p = get_post( $share['id'] );
     264    $url = get_bloginfo( 'url' ) . '/?p=' . $p->ID . '&shareadraft=' . $share['key'];
     265    $friendly_delta = $this->friendly_delta( $share['expires'] - time() );
     266    $iso_expires = date_i18n( 'c', $share['expires'] );
     267?>
     268<tr>
     269<td><?php echo $p->ID; ?></td>
     270<td><?php echo $p->post_title; ?></td>
     271<!-- TODO: make the draft link selecatble -->
     272<td><a href="<?php echo esc_url( $url ); ?>"><?php echo esc_html( $url ); ?></a></td>
     273<td><time title="<?php echo $iso_expires; ?>" datetime="<?php echo $iso_expires; ?>"><?php echo $friendly_delta; ?></time></td>
     274<td class="actions">
     275    <a class="shareadraft-extend edit" id="shareadraft-extend-link-<?php echo $share['key']; ?>"
     276        href="javascript:shareadraft.toggle_extend( '<?php echo $share['key']; ?>' );">
     277            <?php _e( 'Extend', 'shareadraft' ); ?>
     278    </a>
     279    <form class="shareadraft-extend" id="shareadraft-extend-form-<?php echo $share['key']; ?>"
     280        action="" method="post">
     281        <input type="hidden" name="action" value="extend" />
     282        <input type="hidden" name="key" value="<?php echo $share['key']; ?>" />
     283        <input type="submit" class="button" name="shareadraft_extend_submit"
     284            value="<?php echo esc_attr__( 'Extend', 'shareadraft' ); ?>"/>
     285<?php _e( 'by', 'shareadraft' );?>
    268286<?php echo $this->tmpl_measure_select(); ?>
    269                         <a class="shareadraft-extend-cancel"
    270                             href="javascript:shareadraft.cancel_extend('<?php echo $share['key']; ?>');">
    271                             <?php _e('Cancel', 'shareadraft'); ?>
    272                         </a>
    273                     </form>
    274                 </td>
    275                 <td class="actions">
    276                     <a class="delete" href="edit.php?page=<?php echo plugin_basename(__FILE__); ?>&amp;action=delete&amp;key=<?php echo $share['key']; ?>"><?php _e('Delete', 'shareadraft'); ?></a>
    277                 </td>
    278             </tr>
     287        <a class="shareadraft-extend-cancel"
     288            href="javascript:shareadraft.cancel_extend( '<?php echo $share['key']; ?>' );">
     289            <?php _e( 'Cancel', 'shareadraft' ); ?>
     290        </a>
     291        <?php wp_nonce_field( 'shareadraft-extend' ); ?>
     292    </form>
     293</td>
     294<td class="actions">
     295<?php
     296    $delete_url = 'edit.php?page=' . plugin_basename( __FILE__ ) . '&action=delete&key=' . $share['key'];
     297    $nonced_delete_url = wp_nonce_url( $delete_url, 'shareadraft-delete' );
     298?>
     299    <a class="delete" href="<?php echo esc_url( $nonced_delete_url ); ?>"><?php _e( 'Delete', 'shareadraft' ); ?></a>
     300</td>
     301</tr>
    279302<?php
    280303        endforeach;
    281         if (empty($s)):
    282 ?>
    283             <tr>
    284                 <td colspan="5"><?php _e('No shared drafts!', 'shareadraft'); ?></td>
    285             </tr>
     304:
     305?>
     306<tr>
     307); ?></td>
     308</tr>
    286309<?php
    287310        endif;
     
    289312            </tbody>
    290313        </table>
    291         <h3><?php _e('Share a Draft', 'shareadraft'); ?></h3>
     314        <h3><?php _e(); ?></h3>
    292315        <form id="shareadraft-share" action="" method="post">
    293316        <p>
    294317            <select id="shareadraft-postid" name="post_id">
    295             <option value=""><?php _e('Choose a draft', 'shareadraft'); ?></option>
    296 <?php
    297         foreach($drafts_struct as $draft_type):
    298             if ($draft_type[1]):
    299 ?>
    300             <option value="" disabled="disabled"></option>
    301             <option value="" disabled="disabled"><?php echo $draft_type[0]; ?></option>
    302 <?php
    303                 foreach($draft_type[2] as $draft):
    304                     if (empty($draft->post_title)) continue;
    305 ?>
    306             <option value="<?php echo $draft->ID?>"><?php echo wp_specialchars($draft->post_title); ?></option>
    307 <?php
    308                 endforeach;
    309             endif;
     318            <option value=""><?php _e( 'Choose a draft', 'shareadraft' ); ?></option>
     319<?php
     320foreach ( $draft_groups as $draft_group ) :
     321    if ( $draft_group['posts'] ) :
     322?>
     323    <option value="" disabled="disabled"></option>
     324    <option value="" disabled="disabled"><?php echo $draft_group['label']; ?></option>
     325<?php
     326foreach ( $draft_group['posts'] as $draft ) :
     327    if ( empty( $draft->post_title ) ) {
     328        continue;
     329    }
     330?>
     331<option value="<?php echo $draft->ID?>"><?php echo esc_html( $draft->post_title ); ?></option>
     332<?php
     333        endforeach;
     334endif;
    310335        endforeach;
    311336?>
     
    314339        <p>
    315340            <input type="submit" class="button" name="shareadraft_submit"
    316                 value="<?php echo attribute_escape(__('Share it', 'shareadraft')); ?>" />
    317             <?php _e('for', 'shareadraft'); ?>
    318             <?php echo $this->tmpl_measure_select(); ?>.
     341                value="<?php echo ); ?>" />
     342            <?php _e(); ?>
     343            <?php echo $this->tmpl_measure_select(); ?>
    319344        </p>
     345
    320346        </form>
    321347        </div>
    322348<?php
    323     }
    324 
    325     function can_view($post_id) {
    326         if (!isset($_GET['shareadraft']) || !is_array($this->admin_options)) {
     349        }
     350
     351        function can_view( $post_id ) {
     352            if ( ! isset( $_GET['shareadraft'] ) || ! is_array( $this->admin_options ) ) {
     353                return false;
     354            }
     355            foreach ( $this->admin_options as $option ) {
     356                if ( ! is_array( $option ) || ! isset( $option['shared'] ) ) {
     357                    continue;
     358                }
     359                $shares = $option['shared'];
     360                foreach ( $shares as $share ) {
     361                    if ( $share['id'] === $post_id && $share['key'] === $_GET['shareadraft'] ) {
     362                        return true;
     363                    }
     364                }
     365            }
    327366            return false;
    328367        }
    329         foreach($this->admin_options as $option) {
    330             if (!is_array($option) || !isset($option['shared'])) continue;
    331             $shares = $option['shared'];
    332             foreach($shares as $share) {
    333                 if ($share['id'] == $post_id && $share['key'] == $_GET['shareadraft']) {
    334                     return true;
    335                 }
    336             }
    337         }
    338         return false;
    339     }
    340 
    341     function posts_results_intercept($posts) {
    342         if (1 != count($posts)) return $posts;
    343         $post = $posts[0];
    344         $status = get_post_status($post);
    345         if ('publish' != $status && $this->can_view($post->ID)) {
    346             $this->shared_post = $post;
    347         }
    348         return $posts;
    349     }
    350 
    351     function the_posts_intercept($posts){
    352         if (empty($posts) && !is_null($this->shared_post)) {
    353             return array($this->shared_post);
    354         } else {
    355             $this->shared_post = null;
     368
     369        function posts_results_intercept( $posts ) {
     370            if ( 1 !== count( $posts ) ) {
     371                return $posts;
     372            }
     373            $post = $posts[0];
     374            $status = get_post_status( $post );
     375            if ( 'publish' !== $status && $this->can_view( $post->ID ) ) {
     376                $this->shared_post = $post;
     377            }
    356378            return $posts;
    357379        }
    358     }
    359 
    360     function tmpl_measure_select() {
    361         $secs = __('seconds', 'shareadraft');
    362         $mins = __('minutes', 'shareadraft');
    363         $hours = __('hours', 'shareadraft');
    364         $days = __('days', 'shareadraft');
    365         return <<<SELECT
     380
     381        function the_posts_intercept( $posts ) {
     382            if ( empty( $posts ) && ! is_null( $this->shared_post ) ) {
     383                return array( $this->shared_post );
     384            } else {
     385                $this->shared_post = null;
     386                return $posts;
     387            }
     388        }
     389
     390        function tmpl_measure_select() {
     391            $mins = __( 'minutes', 'shareadraft' );
     392            $hours = __( 'hours', 'shareadraft' );
     393            $days = __( 'days', 'shareadraft' );
     394            $weeks = __( 'weeks', 'shareadraft' );
     395            return <<<SELECT
    366396            <input name="expires" type="text" value="2" size="4"/>
    367397            <select name="measure">
    368                 <option value="s">$secs</option>
    369398                <option value="m">$mins</option>
    370                 <option value="h" selected="selected">$hours</option>
     399                <option value="h">$hours</option>
    371400                <option value="d">$days</option>
     401
    372402            </select>
    373403SELECT;
    374     }
    375 
    376     function print_admin_css() {
    377 ?>
     404    }
     405
     406    function print_admin_css() {
     407?>
    378408    <style type="text/css">
    379409        a.shareadraft-extend, a.shareadraft-extend-cancel { display: none; }
     
    382412        th.actions, td.actions { text-align: center; }
    383413    </style>
    384 <?php
    385     }
    386 
    387     function print_admin_js() {
    388 ?>
     414<?php
     415    }
     416
     417    function print_admin_js() {
     418?>
    389419    <script type="text/javascript">
    390420    //<![CDATA[
    391     (function($) {
    392         $(function() {
    393             $('form.shareadraft-extend').hide();
    394             $('a.shareadraft-extend').show();
    395             $('a.shareadraft-extend-cancel').show();
    396             $('a.shareadraft-extend-cancel').css('display', 'inline');
    397         });
     421    () {
     422        $(function() {
     423            $().hide();
     424            $().show();
     425            $().show();
     426            $();
     427        });
    398428        window.shareadraft = {
    399             toggle_extend: function(key) {
    400                 $('#shareadraft-extend-form-'+key).show();
    401                 $('#shareadraft-extend-link-'+key).hide();
    402                 $('#shareadraft-extend-form-'+key+' input[name="expires"]').focus();
     429            toggle_extend: function() {
     430                $().show();
     431                $().hide();
     432                $().focus();
    403433            },
    404             cancel_extend: function(key) {
    405                 $('#shareadraft-extend-form-'+key).hide();
    406                 $('#shareadraft-extend-link-'+key).show();
     434            cancel_extend: function() {
     435                $().hide();
     436                $().show();
    407437            }
    408438        };
    409     })(jQuery);
     439    });
    410440    //]]>
    411441    </script>
    412 <?php
     442    <?php
     443        }
    413444    }
     445
     446
     447
     448
    414449}
    415 endif;
    416 
    417 if (class_exists('ShareADraft')) {
    418     $__share_a_draft = new ShareADraft();
    419 }
Note: See TracChangeset for help on using the changeset viewer.