| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Share a Draft |
| 4 |
Plugin URI: http://wordpress.org/extends/plugins/shareadraft/ |
| 5 |
Description: Let your friends preview one of your drafts, without giving them permissions to edit posts in your blog |
| 6 |
Author: Nikolay Bachiyski |
| 7 |
Version: 0.3 |
| 8 |
Author URI: http://nb.niichavo.org/ |
| 9 |
Generated At: www.wp-fun.co.uk; |
| 10 |
*/ |
| 11 |
|
| 12 |
if (!class_exists('ShareADraft')) { |
| 13 |
class ShareADraft { |
| 14 |
var $adminOptionsName = "ShareADraft_options"; |
| 15 |
|
| 16 |
function ShareADraft(){ |
| 17 |
add_action('init', array(&$this, 'init')); |
| 18 |
} |
| 19 |
|
| 20 |
function init() { |
| 21 |
global $current_user; |
| 22 |
add_action("admin_menu", array(&$this,"add_admin_pages")); |
| 23 |
add_filter('the_posts', array(&$this, 'the_posts_intercept')); |
| 24 |
add_filter('posts_results', array(&$this, 'posts_results_intercept')); |
| 25 |
|
| 26 |
$this->adminOptions = $this->getAdminOptions(); |
| 27 |
$this->adminOptions = $this->clearExpired($this->adminOptions); |
| 28 |
$this->userOptions = ($current_user->id > 0 && isset($this->adminOptions[$current_user->id]))? $this->adminOptions[$current_user->id] : array(); |
| 29 |
|
| 30 |
$this->saveAdminOptions(); |
| 31 |
load_plugin_textdomain('shareadraft'); |
| 32 |
} |
| 33 |
|
| 34 |
function getAdminOptions() { |
| 35 |
$savedOptions = get_option($this->adminOptionsName); |
| 36 |
return is_array($savedOptions)? $savedOptions : array(); |
| 37 |
} |
| 38 |
|
| 39 |
function saveAdminOptions(){ |
| 40 |
global $current_user; |
| 41 |
if ($current_user->id > 0) { |
| 42 |
$this->adminOptions[$current_user->id] = $this->userOptions; |
| 43 |
} |
| 44 |
update_option($this->adminOptionsName, $this->adminOptions); |
| 45 |
} |
| 46 |
|
| 47 |
function clearExpired($all_options) { |
| 48 |
$all = array(); |
| 49 |
foreach($all_options as $user_id => $options) { |
| 50 |
$shared = array(); |
| 51 |
if (!isset($options['shared']) || !is_array($options['shared'])) { |
| 52 |
continue; |
| 53 |
} |
| 54 |
foreach($options['shared'] as $share) { |
| 55 |
if ($share['expires'] < time()) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
$shared[] = $share; |
| 59 |
} |
| 60 |
$options['shared'] = $shared; |
| 61 |
$all[$user_id] = $options; |
| 62 |
} |
| 63 |
return $all; |
| 64 |
} |
| 65 |
|
| 66 |
function add_admin_pages(){ |
| 67 |
add_submenu_page("edit.php", __('Share a Draft', 'shareadraft'), __('Share a Draft', 'shareadraft'), 'edit_posts', __FILE__, array(&$this,"output_existing_menu_sub_admin_page")); |
| 68 |
} |
| 69 |
|
| 70 |
function process_post_options($params) { |
| 71 |
global $current_user; |
| 72 |
if (isset($params['post_id'])) { |
| 73 |
$p = get_post($params['post_id']); |
| 74 |
if (!$p) { |
| 75 |
return __('There is no such post!', 'shareadraft'); |
| 76 |
} |
| 77 |
if ('publish' == get_post_status($p)) { |
| 78 |
return __('The post is published!', 'shareadraft'); |
| 79 |
} |
| 80 |
$exp = 60; |
| 81 |
$multiply = 60; |
| 82 |
if (isset($params['expires']) && ($e = intval($params['expires']))) { |
| 83 |
$exp = $e; |
| 84 |
} |
| 85 |
$mults = array('s' => 1, 'm' => 60, 'h' => 3600, 'd' => 24*3600); |
| 86 |
if (isset($params['measure']) && isset($mults[$params['measure']])) { |
| 87 |
$multiply = $mults[$params['measure']]; |
| 88 |
} |
| 89 |
$this->userOptions['shared'][] = array('id' => $p->ID, 'expires' => time() + $exp*$multiply, 'key' => uniqid('baba'.$p->ID.'_')); |
| 90 |
$this->saveAdminOptions(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
function process_delete($params) { |
| 95 |
if (!isset($params['key']) || !isset($this->userOptions['shared']) || !is_array($this->userOptions['shared'])) { |
| 96 |
return ''; |
| 97 |
} |
| 98 |
$shared = array(); |
| 99 |
foreach($this->userOptions['shared'] as $share) { |
| 100 |
if ($share['key'] == $params['key']) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
$shared[] = $share; |
| 104 |
} |
| 105 |
$this->userOptions['shared'] = $shared; |
| 106 |
$this->saveAdminOptions(); |
| 107 |
} |
| 108 |
|
| 109 |
function get_drafts() { |
| 110 |
global $current_user; |
| 111 |
$my_drafts = get_users_drafts($current_user->id); |
| 112 |
$pending = get_others_pending($current_user->id); |
| 113 |
$others_drafts = get_others_drafts($current_user->id); |
| 114 |
$drafts_struct = array( |
| 115 |
array( |
| 116 |
__('Your Drafts:', 'shareadraft'), |
| 117 |
count($my_drafts), |
| 118 |
&$my_drafts, |
| 119 |
), |
| 120 |
array( |
| 121 |
__('Pending Review:', 'shareadraft'), |
| 122 |
count($pending), |
| 123 |
&$pending, |
| 124 |
), |
| 125 |
array( |
| 126 |
__('Others’ Drafts:', 'shareadraft'), |
| 127 |
count($others_drafts), |
| 128 |
&$others_drafts, |
| 129 |
), |
| 130 |
); |
| 131 |
return $drafts_struct; |
| 132 |
} |
| 133 |
|
| 134 |
function get_shared() { |
| 135 |
if (!isset($this->userOptions['shared']) || !is_array($this->userOptions['shared'])) { |
| 136 |
return array(); |
| 137 |
} |
| 138 |
return $this->userOptions['shared']; |
| 139 |
} |
| 140 |
|
| 141 |
function friendly_delta($s) { |
| 142 |
$m = (int)($s/60); |
| 143 |
$free_s = $s - $m*60; |
| 144 |
$h = (int)($s/3600); |
| 145 |
$free_m = (int)(($s - $h*3600)/60); |
| 146 |
$d = (int)($s/(24*3600)); |
| 147 |
$free_h = (int)(($s - $d*(24*3600))/3600); |
| 148 |
if ($m < 1) { |
| 149 |
$res = array($s); |
| 150 |
} elseif ($h < 1) { |
| 151 |
$res = array($free_s, $m); |
| 152 |
} elseif ($d < 1) { |
| 153 |
$res = array($free_s, $free_m, $h); |
| 154 |
} else { |
| 155 |
$res = array($free_s, $free_m, $free_h, $d); |
| 156 |
} |
| 157 |
$names = array(); |
| 158 |
if (isset($res[0])) $names[] = sprintf(__ngettext('%d second', '%d seconds', $res[0], 'shareadraft'), $res[0]); |
| 159 |
if (isset($res[1])) $names[] = sprintf(__ngettext('%d minute', '%d minutes', $res[1], 'shareadraft'), $res[1]); |
| 160 |
if (isset($res[2])) $names[] = sprintf(__ngettext('%d hour', '%d hours', $res[2], 'shareadraft'), $res[2]); |
| 161 |
if (isset($res[3])) $names[] = sprintf(__ngettext('%d day', '%d days', $res[3], 'shareadraft'), $res[3]); |
| 162 |
return implode(', ', array_reverse($names)); |
| 163 |
} |
| 164 |
|
| 165 |
function output_existing_menu_sub_admin_page(){ |
| 166 |
if (isset($_POST['shareadraft_submit'])) { |
| 167 |
$msg = $this->process_post_options($_POST); |
| 168 |
} elseif (isset($_GET['action']) && $_GET['action'] == 'delete') { |
| 169 |
$msg = $this->process_delete($_GET); |
| 170 |
} |
| 171 |
?> |
| 172 |
<div class="wrap"> |
| 173 |
<h2><?php _e('Share a Draft', 'shareadraft'); ?></h2> |
| 174 |
<?php if ($msg):?> |
| 175 |
<div id="message" class="updated fade"><?php echo $msg; ?></div> |
| 176 |
<?php endif;?> |
| 177 |
<h3><?php _e('Currently shared drafts', 'shareadraft'); ?></h3> |
| 178 |
<table class="widefat"> |
| 179 |
<thead> |
| 180 |
<tr> |
| 181 |
<th><?php _e('ID', 'shareadraft'); ?></th> |
| 182 |
<th><?php _e('Title', 'shareadraft'); ?></th> |
| 183 |
<th><?php _e('Link', 'shareadraft'); ?></th> |
| 184 |
<th><?php _e('Expires after', 'shareadraft'); ?></th> |
| 185 |
<th><?php _e('Actions', 'shareadraft'); ?></th> |
| 186 |
</tr> |
| 187 |
</thead> |
| 188 |
<tbody> |
| 189 |
<?php |
| 190 |
$s = $this->get_shared(); |
| 191 |
foreach($s as $share): |
| 192 |
$p = get_post($share['id']); |
| 193 |
?> |
| 194 |
<tr> |
| 195 |
<td><?php echo $p->ID; ?></td> |
| 196 |
<td><?php echo $p->post_title; ?></td> |
| 197 |
<!-- TODO: make the draft link selecatble --> |
| 198 |
<td><?php echo get_option('siteurl'); ?>?p=<?php echo $p->ID?>&shareadraft=<?php echo $share['key']; ?></td> |
| 199 |
<td><?php echo $this->friendly_delta($share['expires'] - time()); ?></td> |
| 200 |
<td><a class="delete" href="edit.php?page=<?php echo plugin_basename(__FILE__); ?>&action=delete&key=<?php echo $share['key']; ?>">Delete</a></td> |
| 201 |
<?php |
| 202 |
endforeach; |
| 203 |
if (empty($s)): |
| 204 |
?> |
| 205 |
<tr> |
| 206 |
<td colspan="5"><?php _e('No shared drafts!', 'shareadraft'); ?></td> |
| 207 |
</tr> |
| 208 |
<?php |
| 209 |
endif; |
| 210 |
?> |
| 211 |
</tbody> |
| 212 |
</table> |
| 213 |
<h3>Share a draft</h3> |
| 214 |
<form id="shareadraft-share" action="" method="post"> |
| 215 |
<p> |
| 216 |
<select id="shareadraft-postid" name="post_id"> |
| 217 |
<option value=""><?php _e('Choose a draft', 'shareadraft'); ?></option> |
| 218 |
<?php |
| 219 |
$drafts_struct = $this->get_drafts(); |
| 220 |
foreach($drafts_struct as $draft_type): |
| 221 |
if ($draft_type[1]): |
| 222 |
?> |
| 223 |
<option value="" disabled="disabled"></option> |
| 224 |
<option value="" disabled="disabled"><?php echo $draft_type[0]; ?></option> |
| 225 |
<?php |
| 226 |
foreach($draft_type[2] as $draft): |
| 227 |
?> |
| 228 |
<option value="<?php echo $draft->ID?>"><?php echo wp_specialchars($draft->post_title); ?></option> |
| 229 |
<?php |
| 230 |
endforeach; |
| 231 |
endif; |
| 232 |
endforeach; |
| 233 |
?> |
| 234 |
</select> |
| 235 |
</p> |
| 236 |
<p> |
| 237 |
<input type="submit" name="shareadraft_submit" value="<?php echo attribute_escape(__('Share it', 'shareadraft')); ?>" /> |
| 238 |
for |
| 239 |
<input name="expires" type="text" value="2" size="4"/> |
| 240 |
<select name="measure"> |
| 241 |
<option value="s">seconds</option> |
| 242 |
<option value="m">minutes</option> |
| 243 |
<option value="h" selected="selected">hours</option> |
| 244 |
<option value="d">days</option> |
| 245 |
</select>. |
| 246 |
</p> |
| 247 |
</form> |
| 248 |
</div> |
| 249 |
<?php |
| 250 |
} |
| 251 |
|
| 252 |
function can_view($post_id) { |
| 253 |
if (!isset($_GET['shareadraft']) || !is_array($this->adminOptions)) { |
| 254 |
return false; |
| 255 |
} |
| 256 |
foreach($this->adminOptions as $option) { |
| 257 |
if (!is_array($option) || !isset($option['shared'])) continue; |
| 258 |
$shares = $option['shared']; |
| 259 |
foreach($shares as $share) { |
| 260 |
if ($share['id'] == $post_id && $share['key'] == $_GET['shareadraft']) { |
| 261 |
return true; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
function posts_results_intercept($posts) { |
| 269 |
if (1 != count($posts)) return $posts; |
| 270 |
$post = &$posts[0]; |
| 271 |
$status = get_post_status($post); |
| 272 |
if ('publish' != $status && $this->can_view($post->ID)) { |
| 273 |
$this->shared_post = & $post; |
| 274 |
} |
| 275 |
return $posts; |
| 276 |
} |
| 277 |
|
| 278 |
function the_posts_intercept($posts){ |
| 279 |
if (empty($posts) && !is_null($this->shared_post)) { |
| 280 |
return array(&$this->shared_post); |
| 281 |
} else { |
| 282 |
$this->shared_post = null; |
| 283 |
return $posts; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
if (class_exists('ShareADraft')) { |
| 290 |
$ShareADraft = new ShareADraft(); |
| 291 |
} |
| 292 |
|
| 293 |
?> |
| 294 |
|