PluginProbe
Share a Draft / 1.3
Share a Draft v1.3
1.7 trunk 0.2 0.3 0.4 0.5 0.6 0.7 1.0 1.1 1.2 1.3 1.4 1.5 1.6
shareadraft / shareadraft.php

shareadraft.php in Share a Draft 1.3, at shareadraft.php

405 lines 12.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin 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.
6 Author: Nikolay Bachiyski
7 Version: 1.3
8 Author URI: http://nikolay.bg/
9 Text 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 ShareADraft(){
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) {
60 $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()) {
66 continue;
67 }
68 $shared[] = $share;
69 }
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.'_'));
107 $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 $pending = get_others_pending($current_user->id);
149 $others_drafts = get_others_drafts($current_user->id);
150 $drafts_struct = array(
151 array(
152 __('Your Drafts:', 'shareadraft'),
153 count($my_drafts),
154 &$my_drafts,
155 ),
156 array(
157 __('Pending Review:', 'shareadraft'),
158 count($pending),
159 &$pending,
160 ),
161 array(
162 __('Others&#8217; Drafts:', 'shareadraft'),
163 count($others_drafts),
164 &$others_drafts,
165 ),
166 );
167 return $drafts_struct;
168 }
169
170 function get_shared() {
171 if (!isset($this->user_options['shared']) || !is_array($this->user_options['shared'])) {
172 return array();
173 }
174 return $this->user_options['shared'];
175 }
176
177 function friendly_delta($s) {
178 $m = (int)($s/60);
179 $free_s = $s - $m*60;
180 $h = (int)($s/3600);
181 $free_m = (int)(($s - $h*3600)/60);
182 $d = (int)($s/(24*3600));
183 $free_h = (int)(($s - $d*(24*3600))/3600);
184 if ($m < 1) {
185 $res = array($s);
186 } elseif ($h < 1) {
187 $res = array($free_s, $m);
188 } elseif ($d < 1) {
189 $res = array($free_s, $free_m, $h);
190 } else {
191 $res = array($free_s, $free_m, $free_h, $d);
192 }
193 $names = array();
194 if (isset($res[0]))
195 $names[] = sprintf(__ngettext('%d second', '%d seconds', $res[0], 'shareadraft'), $res[0]);
196 if (isset($res[1]))
197 $names[] = sprintf(__ngettext('%d minute', '%d minutes', $res[1], 'shareadraft'), $res[1]);
198 if (isset($res[2]))
199 $names[] = sprintf(__ngettext('%d hour', '%d hours', $res[2], 'shareadraft'), $res[2]);
200 if (isset($res[3]))
201 $names[] = sprintf(__ngettext('%d day', '%d days', $res[3], 'shareadraft'), $res[3]);
202 return implode(', ', array_reverse($names));
203 }
204
205 function output_existing_menu_sub_admin_page(){
206 if (isset($_POST['shareadraft_submit'])) {
207 $msg = $this->process_post_options($_POST);
208 } elseif (isset($_POST['action']) && $_POST['action'] == 'extend') {
209 $msg = $this->process_extend($_POST);
210 } elseif (isset($_GET['action']) && $_GET['action'] == 'delete') {
211 $msg = $this->process_delete($_GET);
212 }
213 $drafts_struct = $this->get_drafts();
214 ?>
215 <div class="wrap">
216 <h2><?php _e('Share a Draft', 'shareadraft'); ?></h2>
217 <?php if ($msg):?>
218 <div id="message" class="updated fade"><?php echo $msg; ?></div>
219 <?php endif;?>
220 <h3><?php _e('Currently shared drafts', 'shareadraft'); ?></h3>
221 <table class="widefat">
222 <thead>
223 <tr>
224 <th><?php _e('ID', 'shareadraft'); ?></th>
225 <th><?php _e('Title', 'shareadraft'); ?></th>
226 <th><?php _e('Link', 'shareadraft'); ?></th>
227 <th><?php _e('Expires after', 'shareadraft'); ?></th>
228 <th colspan="2" class="actions"><?php _e('Actions', 'shareadraft'); ?></th>
229 </tr>
230 </thead>
231 <tbody>
232 <?php
233 $s = $this->get_shared();
234 foreach($s as $share):
235 $p = get_post($share['id']);
236 ?>
237 <tr>
238 <td><?php echo $p->ID; ?></td>
239 <td><?php echo $p->post_title; ?></td>
240 <!-- TODO: make the draft link selecatble -->
241 <td><?php echo get_bloginfo('url'); ?>/?p=<?php echo $p->ID?>&amp;shareadraft=<?php echo $share['key']; ?></td>
242 <td><?php echo $this->friendly_delta($share['expires'] - time()); ?></td>
243 <td class="actions">
244 <a class="shareadraft-extend edit" id="shareadraft-extend-link-<?php echo $share['key']; ?>"
245 href="javascript:shareadraft.toggle_extend('<?php echo $share['key']; ?>');">
246 <?php _e('Extend', 'shareadraft'); ?>
247 </a>
248 <form class="shareadraft-extend" id="shareadraft-extend-form-<?php echo $share['key']; ?>"
249 action="" method="post">
250 <input type="hidden" name="action" value="extend" />
251 <input type="hidden" name="key" value="<?php echo $share['key']; ?>" />
252 <input type="submit" class="button" name="shareadraft_extend_submit"
253 value="<?php echo attribute_escape(__('Extend', 'shareadraft')); ?>"/>
254 <?php _e('by', 'shareadraft');?>
255 <?php echo $this->tmpl_measure_select(); ?>
256 <a class="shareadraft-extend-cancel"
257 href="javascript:shareadraft.cancel_extend('<?php echo $share['key']; ?>');">
258 <?php _e('Cancel', 'shareadraft'); ?>
259 </a>
260 </form>
261 </td>
262 <td class="actions">
263 <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>
264 </td>
265 </tr>
266 <?php
267 endforeach;
268 if (empty($s)):
269 ?>
270 <tr>
271 <td colspan="5"><?php _e('No shared drafts!', 'shareadraft'); ?></td>
272 </tr>
273 <?php
274 endif;
275 ?>
276 </tbody>
277 </table>
278 <h3><?php _e('Share a Draft', 'shareadraft'); ?></h3>
279 <form id="shareadraft-share" action="" method="post">
280 <p>
281 <select id="shareadraft-postid" name="post_id">
282 <option value=""><?php _e('Choose a draft', 'shareadraft'); ?></option>
283 <?php
284 foreach($drafts_struct as $draft_type):
285 if ($draft_type[1]):
286 ?>
287 <option value="" disabled="disabled"></option>
288 <option value="" disabled="disabled"><?php echo $draft_type[0]; ?></option>
289 <?php
290 foreach($draft_type[2] as $draft):
291 if (empty($draft->post_title)) continue;
292 ?>
293 <option value="<?php echo $draft->ID?>"><?php echo wp_specialchars($draft->post_title); ?></option>
294 <?php
295 endforeach;
296 endif;
297 endforeach;
298 ?>
299 </select>
300 </p>
301 <p>
302 <input type="submit" class="button" name="shareadraft_submit"
303 value="<?php echo attribute_escape(__('Share it', 'shareadraft')); ?>" />
304 <?php _e('for', 'shareadraft'); ?>
305 <?php echo $this->tmpl_measure_select(); ?>.
306 </p>
307 </form>
308 </div>
309 <?php
310 }
311
312 function can_view($post_id) {
313 if (!isset($_GET['shareadraft']) || !is_array($this->admin_options)) {
314 return false;
315 }
316 foreach($this->admin_options as $option) {
317 if (!is_array($option) || !isset($option['shared'])) continue;
318 $shares = $option['shared'];
319 foreach($shares as $share) {
320 if ($share['id'] == $post_id && $share['key'] == $_GET['shareadraft']) {
321 return true;
322 }
323 }
324 }
325 return false;
326 }
327
328 function posts_results_intercept($posts) {
329 if (1 != count($posts)) return $posts;
330 $post = &$posts[0];
331 $status = get_post_status($post);
332 if ('publish' != $status && $this->can_view($post->ID)) {
333 $this->shared_post = & $post;
334 }
335 return $posts;
336 }
337
338 function the_posts_intercept($posts){
339 if (empty($posts) && !is_null($this->shared_post)) {
340 return array(&$this->shared_post);
341 } else {
342 $this->shared_post = null;
343 return $posts;
344 }
345 }
346
347 function tmpl_measure_select() {
348 $secs = __('seconds', 'shareadraft');
349 $mins = __('minutes', 'shareadraft');
350 $hours = __('hours', 'shareadraft');
351 $days = __('days', 'shareadraft');
352 return <<<SELECT
353 <input name="expires" type="text" value="2" size="4"/>
354 <select name="measure">
355 <option value="s">$secs</option>
356 <option value="m">$mins</option>
357 <option value="h" selected="selected">$hours</option>
358 <option value="d">$days</option>
359 </select>
360 SELECT;
361 }
362
363 function print_admin_css() {
364 ?>
365 <style type="text/css">
366 a.shareadraft-extend, a.shareadraft-extend-cancel { display: none; }
367 form.shareadraft-extend { white-space: nowrap; }
368 form.shareadraft-extend, form.shareadraft-extend input, form.shareadraft-extend select { font-size: 11px; }
369 th.actions, td.actions { text-align: center; }
370 </style>
371 <?php
372 }
373
374 function print_admin_js() {
375 ?>
376 <script type="text/javascript">
377 //<![CDATA[
378 jQuery(function($) {
379 $('form.shareadraft-extend').hide();
380 $('a.shareadraft-extend').show();
381 $('a.shareadraft-extend-cancel').show();
382 $('a.shareadraft-extend-cancel').css('display', 'inline');
383 });
384 var shareadraft = {
385 toggle_extend: function(key) {
386 jQuery('#shareadraft-extend-form-'+key).show();
387 jQuery('#shareadraft-extend-link-'+key).hide();
388 jQuery('#shareadraft-extend-form-'+key+' input[name="expires"]').focus();
389 },
390 cancel_extend: function(key) {
391 jQuery('#shareadraft-extend-form-'+key).hide();
392 jQuery('#shareadraft-extend-link-'+key).show();
393 }
394 };
395 //]]>
396 </script>
397 <?php
398 }
399 }
400 endif;
401
402 if (class_exists('ShareADraft')) {
403 $__share_a_draft = new ShareADraft();
404 }
405