PluginProbe
Share a Draft / 1.0
Share a Draft v1.0
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.0, at shareadraft.php

384 lines 12.2 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.0
8 Author URI: http://nikolay.bg/
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', PLUGINDIR . '/shareadraft/languages');
32
33 if (isset($_GET['page']) && $_GET['page'] == plugin_basename(__FILE__))
34 $this->admin_page_init();
35 }
36
37 function admin_page_init() {
38 wp_enqueue_script('jquery');
39 add_action('admin_head', array(&$this, 'print_admin_css'));
40 add_action('admin_head', array(&$this, 'print_admin_js'));
41 }
42
43 function getAdminOptions() {
44 $savedOptions = get_option($this->adminOptionsName);
45 return is_array($savedOptions)? $savedOptions : array();
46 }
47
48 function saveAdminOptions(){
49 global $current_user;
50 if ($current_user->id > 0) {
51 $this->adminOptions[$current_user->id] = $this->userOptions;
52 }
53 update_option($this->adminOptionsName, $this->adminOptions);
54 }
55
56 function clearExpired($all_options) {
57 $all = array();
58 foreach($all_options as $user_id => $options) {
59 $shared = array();
60 if (!isset($options['shared']) || !is_array($options['shared'])) {
61 continue;
62 }
63 foreach($options['shared'] as $share) {
64 if ($share['expires'] < time()) {
65 continue;
66 }
67 $shared[] = $share;
68 }
69 $options['shared'] = $shared;
70 $all[$user_id] = $options;
71 }
72 return $all;
73 }
74
75 function add_admin_pages(){
76 add_submenu_page("edit.php", __('Share a Draft', 'shareadraft'), __('Share a Draft', 'shareadraft'), 'edit_posts', __FILE__, array(&$this,"output_existing_menu_sub_admin_page"));
77 }
78
79 function calculate_seconds($params) {
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 return $exp * $multiply;
90 }
91
92 function process_post_options($params) {
93 global $current_user;
94 if (isset($params['post_id'])) {
95 $p = get_post($params['post_id']);
96 if (!$p) {
97 return __('There is no such post!', 'shareadraft');
98 }
99 if ('publish' == get_post_status($p)) {
100 return __('The post is published!', 'shareadraft');
101 }
102 $this->userOptions['shared'][] = array('id' => $p->ID, 'expires' => time() + $this->calculate_seconds($params), 'key' => uniqid('baba'.$p->ID.'_'));
103 $this->saveAdminOptions();
104 }
105 }
106
107 function process_delete($params) {
108 if (!isset($params['key']) || !isset($this->userOptions['shared']) || !is_array($this->userOptions['shared'])) {
109 return '';
110 }
111 $shared = array();
112 foreach($this->userOptions['shared'] as $share) {
113 if ($share['key'] == $params['key']) {
114 continue;
115 }
116 $shared[] = $share;
117 }
118 $this->userOptions['shared'] = $shared;
119 $this->saveAdminOptions();
120 }
121
122 function process_extend($params) {
123 if (!isset($params['key']) || !isset($this->userOptions['shared']) || !is_array($this->userOptions['shared'])) {
124 return '';
125 }
126 $shared = array();
127 foreach($this->userOptions['shared'] as $share) {
128 if ($share['key'] == $params['key']) {
129 $share['expires'] += $this->calculate_seconds($params);
130 }
131 $shared[] = $share;
132 }
133 $this->userOptions['shared'] = $shared;
134 $this->saveAdminOptions();
135 }
136
137 function get_drafts() {
138 global $current_user;
139 $my_drafts = get_users_drafts($current_user->id);
140 $pending = get_others_pending($current_user->id);
141 $others_drafts = get_others_drafts($current_user->id);
142 $drafts_struct = array(
143 array(
144 __('Your Drafts:', 'shareadraft'),
145 count($my_drafts),
146 &$my_drafts,
147 ),
148 array(
149 __('Pending Review:', 'shareadraft'),
150 count($pending),
151 &$pending,
152 ),
153 array(
154 __('Others&#8217; Drafts:', 'shareadraft'),
155 count($others_drafts),
156 &$others_drafts,
157 ),
158 );
159 return $drafts_struct;
160 }
161
162 function get_shared() {
163 if (!isset($this->userOptions['shared']) || !is_array($this->userOptions['shared'])) {
164 return array();
165 }
166 return $this->userOptions['shared'];
167 }
168
169 function friendly_delta($s) {
170 $m = (int)($s/60);
171 $free_s = $s - $m*60;
172 $h = (int)($s/3600);
173 $free_m = (int)(($s - $h*3600)/60);
174 $d = (int)($s/(24*3600));
175 $free_h = (int)(($s - $d*(24*3600))/3600);
176 if ($m < 1) {
177 $res = array($s);
178 } elseif ($h < 1) {
179 $res = array($free_s, $m);
180 } elseif ($d < 1) {
181 $res = array($free_s, $free_m, $h);
182 } else {
183 $res = array($free_s, $free_m, $free_h, $d);
184 }
185 $names = array();
186 if (isset($res[0])) $names[] = sprintf(__ngettext('%d second', '%d seconds', $res[0], 'shareadraft'), $res[0]);
187 if (isset($res[1])) $names[] = sprintf(__ngettext('%d minute', '%d minutes', $res[1], 'shareadraft'), $res[1]);
188 if (isset($res[2])) $names[] = sprintf(__ngettext('%d hour', '%d hours', $res[2], 'shareadraft'), $res[2]);
189 if (isset($res[3])) $names[] = sprintf(__ngettext('%d day', '%d days', $res[3], 'shareadraft'), $res[3]);
190 return implode(', ', array_reverse($names));
191 }
192
193 function output_existing_menu_sub_admin_page(){
194 if (isset($_POST['shareadraft_submit'])) {
195 $msg = $this->process_post_options($_POST);
196 } elseif (isset($_POST['action']) && $_POST['action'] == 'extend') {
197 $msg = $this->process_extend($_POST);
198 } elseif (isset($_GET['action']) && $_GET['action'] == 'delete') {
199 $msg = $this->process_delete($_GET);
200 }
201 ?>
202 <div class="wrap">
203 <h2><?php _e('Share a Draft', 'shareadraft'); ?></h2>
204 <?php if ($msg):?>
205 <div id="message" class="updated fade"><?php echo $msg; ?></div>
206 <?php endif;?>
207 <h3><?php _e('Currently shared drafts', 'shareadraft'); ?></h3>
208 <table class="widefat">
209 <thead>
210 <tr>
211 <th><?php _e('ID', 'shareadraft'); ?></th>
212 <th><?php _e('Title', 'shareadraft'); ?></th>
213 <th><?php _e('Link', 'shareadraft'); ?></th>
214 <th><?php _e('Expires after', 'shareadraft'); ?></th>
215 <th colspan="2" class="actions"><?php _e('Actions', 'shareadraft'); ?></th>
216 </tr>
217 </thead>
218 <tbody>
219 <?php
220 $s = $this->get_shared();
221 foreach($s as $share):
222 $p = get_post($share['id']);
223 ?>
224 <tr>
225 <td><?php echo $p->ID; ?></td>
226 <td><?php echo $p->post_title; ?></td>
227 <!-- TODO: make the draft link selecatble -->
228 <td><?php echo get_option('siteurl'); ?>?p=<?php echo $p->ID?>&amp;shareadraft=<?php echo $share['key']; ?></td>
229 <td><?php echo $this->friendly_delta($share['expires'] - time()); ?></td>
230 <td class="actions">
231 <a class="shareadraft-extend edit" id="shareadraft-extend-link-<?php echo $share['key']; ?>" href="javascript:shareadraft.toggle_extend('<?php echo $share['key']; ?>');"><?php _e('Extend', 'shareadraft'); ?></a>
232 <form class="shareadraft-extend" id="shareadraft-extend-form-<?php echo $share['key']; ?>" action="" method="post">
233 <input type="hidden" name="action" value="extend" />
234 <input type="hidden" name="key" value="<?php echo $share['key']; ?>" />
235 <input type="submit" name="shareadraft_extend_submit" value="<?php echo attribute_escape(__('Extend', 'shareadraft')); ?>"/>
236 <?php _e('by', 'shareadraft');?>
237 <?php echo $this->tmpl_measure_select(); ?>
238 <a class="shareadraft-extend-cancel" href="javascript:shareadraft.cancel_extend('<?php echo $share['key']; ?>');"><?php _e('Cancel', 'shareadraft'); ?></a>
239 </form>
240 </td>
241 <td class="actions">
242 <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>
243 </td>
244 <?php
245 endforeach;
246 if (empty($s)):
247 ?>
248 <tr>
249 <td colspan="5"><?php _e('No shared drafts!', 'shareadraft'); ?></td>
250 </tr>
251 <?php
252 endif;
253 ?>
254 </tbody>
255 </table>
256 <h3><?php _e('Share a Draft', 'shareadraft'); ?></h3>
257 <form id="shareadraft-share" action="" method="post">
258 <p>
259 <select id="shareadraft-postid" name="post_id">
260 <option value=""><?php _e('Choose a draft', 'shareadraft'); ?></option>
261 <?php
262 $drafts_struct = $this->get_drafts();
263 foreach($drafts_struct as $draft_type):
264 if ($draft_type[1]):
265 ?>
266 <option value="" disabled="disabled"></option>
267 <option value="" disabled="disabled"><?php echo $draft_type[0]; ?></option>
268 <?php
269 foreach($draft_type[2] as $draft):
270 if (empty($draft->post_title)) continue;
271 ?>
272 <option value="<?php echo $draft->ID?>"><?php echo wp_specialchars($draft->post_title); ?></option>
273 <?php
274 endforeach;
275 endif;
276 endforeach;
277 ?>
278 </select>
279 </p>
280 <p>
281 <input type="submit" name="shareadraft_submit" value="<?php echo attribute_escape(__('Share it', 'shareadraft')); ?>" />
282 <?php _e('for', 'shareadraft'); ?>
283 <?php echo $this->tmpl_measure_select(); ?>.
284 </p>
285 </form>
286 </div>
287 <?php
288 }
289
290 function can_view($post_id) {
291 if (!isset($_GET['shareadraft']) || !is_array($this->adminOptions)) {
292 return false;
293 }
294 foreach($this->adminOptions as $option) {
295 if (!is_array($option) || !isset($option['shared'])) continue;
296 $shares = $option['shared'];
297 foreach($shares as $share) {
298 if ($share['id'] == $post_id && $share['key'] == $_GET['shareadraft']) {
299 return true;
300 }
301 }
302 }
303 return false;
304 }
305
306 function posts_results_intercept($posts) {
307 if (1 != count($posts)) return $posts;
308 $post = &$posts[0];
309 $status = get_post_status($post);
310 if ('publish' != $status && $this->can_view($post->ID)) {
311 $this->shared_post = & $post;
312 }
313 return $posts;
314 }
315
316 function the_posts_intercept($posts){
317 if (empty($posts) && !is_null($this->shared_post)) {
318 return array(&$this->shared_post);
319 } else {
320 $this->shared_post = null;
321 return $posts;
322 }
323 }
324
325 function tmpl_measure_select() {
326 $secs = __('seconds', 'shareadraft');
327 $mins = __('minutes', 'shareadraft');
328 $hours = __('hours', 'shareadraft');
329 $days = __('days', 'shareadraft');
330 return <<<SELECT
331 <input name="expires" type="text" value="2" size="4"/>
332 <select name="measure">
333 <option value="s">$secs</option>
334 <option value="m">$mins</option>
335 <option value="h" selected="selected">$hours</option>
336 <option value="d">$days</option>
337 </select>
338 SELECT;
339 }
340
341 function print_admin_css() {
342 ?>
343 <style type="text/css">
344 a.shareadraft-extend, a.shareadraft-extend-cancel { display: none; }
345 form.shareadraft-extend {white-space: nowrap;}
346 form.shareadraft-extend, form.shareadraft-extend input, form.shareadraft-extend select { font-size: 11px;}
347 th.actions, td.actions {text-align: center;}
348 </style>
349 <?php
350 }
351
352 function print_admin_js() {
353 ?>
354 <script type="text/javascript">
355 //<![CDATA[
356 jQuery(function($) {
357 $('form.shareadraft-extend').hide();
358 $('a.shareadraft-extend').show();
359 $('a.shareadraft-extend-cancel').show();
360 $('a.shareadraft-extend-cancel').css('display', 'inline');
361 });
362 var shareadraft = {
363 toggle_extend: function(key) {
364 jQuery('#shareadraft-extend-form-'+key).show();
365 jQuery('#shareadraft-extend-link-'+key).hide();
366 },
367 cancel_extend: function(key) {
368 jQuery('#shareadraft-extend-form-'+key).hide();
369 jQuery('#shareadraft-extend-link-'+key).show();
370 }
371 };
372 //]]>
373 </script>
374 <?php
375 }
376 }
377 endif;
378
379 if (class_exists('ShareADraft')) {
380 $ShareADraft = new ShareADraft();
381 }
382
383 ?>
384