Public Post Preview - Version 2.1

Version Description

The update fixes a rare issue which had created 404 errors.

Download this release

Release Info

Developer ocean90
Plugin Icon 128x128 Public Post Preview
Version 2.1
Comparing to
See all releases

Code changes from version 2.0.1 to 2.1

Files changed (2) hide show
  1. public-post-preview.php +61 -23
  2. readme.txt +29 -2
public-post-preview.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
  /**
3
  * Plugin Name: Public Post Preview
4
- * Version: 2.0.1
5
  * Description: Enables you to give a link to anonymous users for public preview of any post type before it is published.
6
  * Author: Dominik Schilling
7
  * Author URI: http://wphelper.de/
@@ -43,14 +43,15 @@ if ( ! class_exists( 'WP' ) ) {
43
  *
44
  * Used hooks:
45
  * - pre_get_posts
 
 
46
  * - add_meta_boxes
47
  * - save_post
48
  * - posts_results
49
  * - wp_ajax_public-post-preview
50
  * - admin_enqueue_scripts
51
- * - init
52
  *
53
- * Inits at 'plugins_loaded' hook.
54
  *
55
  */
56
  class DS_Public_Post_Preview {
@@ -64,18 +65,22 @@ class DS_Public_Post_Preview {
64
  * @since 1.0.0
65
  */
66
  public static function init() {
67
- if ( ! is_admin() )
68
  add_filter( 'pre_get_posts', array( __CLASS__, 'show_public_preview' ) );
69
 
70
- add_action( 'init', array( __CLASS__, 'load_textdomain' ) );
 
 
 
71
 
72
- add_action( 'add_meta_boxes', array( __CLASS__, 'register_meta_boxes' ) );
73
 
74
- add_action( 'save_post', array( __CLASS__, 'register_public_preview' ), 20, 2 );
75
 
76
- add_action( 'wp_ajax_public-post-preview', array( __CLASS__, 'ajax_register_public_preview' ) );
77
 
78
- add_action( 'admin_enqueue_scripts' , array( __CLASS__, 'enqueue_script' ) );
 
79
  }
80
 
81
  /**
@@ -284,10 +289,23 @@ class DS_Public_Post_Preview {
284
  }
285
 
286
  /**
287
- * Show the post if it's a public preview.
 
 
 
 
 
 
 
 
 
 
 
 
 
288
  *
289
- * Only if it's the main query, a preview, a singular page and
290
- * DS_Public_Post_Preview::public_preview_available() return true.
291
  *
292
  * @since 2.0.0
293
  *
@@ -295,12 +313,13 @@ class DS_Public_Post_Preview {
295
  * @return object The WP_Query object, unchanged.
296
  */
297
  public static function show_public_preview( $query ) {
298
- if ( $query->is_main_query() && $query->is_preview() && $query->is_singular() ) {
299
- $post_id = get_query_var( 'page_id' ) ? get_query_var( 'page_id' ) : get_query_var( 'p' );
300
-
301
- if ( self::public_preview_available( $post_id ) )
302
- add_filter( 'posts_results', array( __CLASS__, 'set_post_to_publish' ) );
303
- }
 
304
 
305
  return $query;
306
  }
@@ -315,10 +334,10 @@ class DS_Public_Post_Preview {
315
  * @return bool True if a public preview is allowed, false on a failure.
316
  */
317
  private static function public_preview_available( $post_id ) {
318
- if ( empty( $post_id ) || empty( $_GET['_ppp'] ) )
319
  return false;
320
 
321
- if( ! self::verify_nonce( $_GET['_ppp'], 'public_post_preview_' . $post_id ) )
322
  wp_die( __( 'The link has been expired!', 'ds-public-post-preview' ) );
323
 
324
  if ( ! in_array( $post_id, get_option( 'public_post_preview', array() ) ) )
@@ -336,11 +355,30 @@ class DS_Public_Post_Preview {
336
  * @param array $posts The post to preview.
337
  */
338
  public static function set_post_to_publish( $posts ) {
339
- $posts[0]->post_status = 'publish';
 
 
 
 
340
 
341
  return $posts;
342
  }
343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344
  /**
345
  * Creates a random, one time use token. Without an UID.
346
  *
@@ -352,7 +390,7 @@ class DS_Public_Post_Preview {
352
  * @return string The one use form token
353
  */
354
  private static function create_nonce( $action = -1 ) {
355
- $i = wp_nonce_tick();
356
 
357
  return substr( wp_hash( $i . $action, 'nonce' ), -12, 10 );
358
  }
@@ -369,7 +407,7 @@ class DS_Public_Post_Preview {
369
  * @return bool Whether the nonce check passed or failed.
370
  */
371
  private static function verify_nonce( $nonce, $action = -1 ) {
372
- $i = wp_nonce_tick();
373
 
374
  // Nonce generated 0-12 hours ago
375
  if ( substr( wp_hash( $i . $action, 'nonce' ), -12, 10 ) == $nonce )
1
  <?php
2
  /**
3
  * Plugin Name: Public Post Preview
4
+ * Version: 2.1
5
  * Description: Enables you to give a link to anonymous users for public preview of any post type before it is published.
6
  * Author: Dominik Schilling
7
  * Author URI: http://wphelper.de/
43
  *
44
  * Used hooks:
45
  * - pre_get_posts
46
+ * - query_vars
47
+ * - init
48
  * - add_meta_boxes
49
  * - save_post
50
  * - posts_results
51
  * - wp_ajax_public-post-preview
52
  * - admin_enqueue_scripts
 
53
  *
54
+ * Inits at 'plugins_loaded' hook.
55
  *
56
  */
57
  class DS_Public_Post_Preview {
65
  * @since 1.0.0
66
  */
67
  public static function init() {
68
+ if ( ! is_admin() ) {
69
  add_filter( 'pre_get_posts', array( __CLASS__, 'show_public_preview' ) );
70
 
71
+ add_filter( 'query_vars', array( __CLASS__, 'add_query_var' ) );
72
+ } else {
73
+
74
+ add_action( 'init', array( __CLASS__, 'load_textdomain' ) );
75
 
76
+ add_action( 'add_meta_boxes', array( __CLASS__, 'register_meta_boxes' ) );
77
 
78
+ add_action( 'save_post', array( __CLASS__, 'register_public_preview' ), 20, 2 );
79
 
80
+ add_action( 'wp_ajax_public-post-preview', array( __CLASS__, 'ajax_register_public_preview' ) );
81
 
82
+ add_action( 'admin_enqueue_scripts' , array( __CLASS__, 'enqueue_script' ) );
83
+ }
84
  }
85
 
86
  /**
289
  }
290
 
291
  /**
292
+ * Registers the new query var `_ppp`.
293
+ *
294
+ * @since 2.1
295
+ *
296
+ * @return array List of query variables.
297
+ */
298
+ public static function add_query_var( $qv ) {
299
+ $qv[] = '_ppp';
300
+
301
+ return $qv;
302
+ }
303
+
304
+ /**
305
+ * Registers the filter to handle a public preview.
306
  *
307
+ * Filter will be set if it's the main query, a preview, a singular page
308
+ * and the query var `_ppp` exists.
309
  *
310
  * @since 2.0.0
311
  *
313
  * @return object The WP_Query object, unchanged.
314
  */
315
  public static function show_public_preview( $query ) {
316
+ if (
317
+ $query->is_main_query() &&
318
+ $query->is_preview() &&
319
+ $query->is_singular() &&
320
+ $query->get( '_ppp' )
321
+ )
322
+ add_filter( 'posts_results', array( __CLASS__, 'set_post_to_publish' ), 10, 2 );
323
 
324
  return $query;
325
  }
334
  * @return bool True if a public preview is allowed, false on a failure.
335
  */
336
  private static function public_preview_available( $post_id ) {
337
+ if ( empty( $post_id ) )
338
  return false;
339
 
340
+ if( ! self::verify_nonce( get_query_var( '_ppp' ), 'public_post_preview_' . $post_id ) )
341
  wp_die( __( 'The link has been expired!', 'ds-public-post-preview' ) );
342
 
343
  if ( ! in_array( $post_id, get_option( 'public_post_preview', array() ) ) )
355
  * @param array $posts The post to preview.
356
  */
357
  public static function set_post_to_publish( $posts ) {
358
+ if ( empty( $posts ) )
359
+ return;
360
+
361
+ if ( self::public_preview_available( $posts[0]->ID ) )
362
+ $posts[0]->post_status = 'publish';
363
 
364
  return $posts;
365
  }
366
 
367
+ /**
368
+ * Get the time-dependent variable for nonce creation.
369
+ *
370
+ * @see wp_nonce_tick()
371
+ *
372
+ * @since 2.1
373
+ *
374
+ * @return int The time-dependent variable
375
+ */
376
+ private static function nonce_tick() {
377
+ $nonce_life = apply_filters( 'ppp_nonce_life', 60 * 60 * 48 ); // 48 hours
378
+
379
+ return ceil( time() / ( $nonce_life / 2 ) );
380
+ }
381
+
382
  /**
383
  * Creates a random, one time use token. Without an UID.
384
  *
390
  * @return string The one use form token
391
  */
392
  private static function create_nonce( $action = -1 ) {
393
+ $i = self::nonce_tick();
394
 
395
  return substr( wp_hash( $i . $action, 'nonce' ), -12, 10 );
396
  }
407
  * @return bool Whether the nonce check passed or failed.
408
  */
409
  private static function verify_nonce( $nonce, $action = -1 ) {
410
+ $i = self::nonce_tick();
411
 
412
  // Nonce generated 0-12 hours ago
413
  if ( substr( wp_hash( $i . $action, 'nonce' ), -12, 10 ) == $nonce )
readme.txt CHANGED
@@ -4,7 +4,7 @@ Tags: public, post, preview, posts, custom post types
4
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VR8YU922B7K46
5
  Requires at least: 3.3
6
  Tested up to: 3.5
7
- Stable tag: 2.0.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -14,7 +14,7 @@ Enables you to give a link to anonymous users for public preview of a post befor
14
 
15
  Enables you to give a link to anonymous users for public preview of a post (or any other public post type) before it is published.
16
 
17
- Have you ever been writing a post with the help of someone who does not have access to your blog and needed to give them the ability to preview it before publishing? This plugin takes care of that by generating a URL with an expiring nonce that can be given out for public preview.
18
 
19
  **Sounds pretty good? Install now!**
20
 
@@ -55,6 +55,9 @@ To upload the plugin through WordPress, instead of FTP:
55
 
56
  == Upgrade Notice ==
57
 
 
 
 
58
  = 2.0 =
59
  New plugin maintainer, supports now all public post types, saves preview status via an AJAX request, ready for translation, requires at least WordPress 3.3.
60
 
@@ -63,8 +66,32 @@ New plugin maintainer, supports now all public post types, saves preview status
63
  * The link will be displayed if the checkbox is checked, just copy and share the link with your frieds.
64
  * To disable a preview just uncheck the box.
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  == Change Log ==
 
 
 
 
68
  = 2.0.1 (20012-07-25): =
69
  * Makes the preview link copyable again
70
 
4
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VR8YU922B7K46
5
  Requires at least: 3.3
6
  Tested up to: 3.5
7
+ Stable tag: 2.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
14
 
15
  Enables you to give a link to anonymous users for public preview of a post (or any other public post type) before it is published.
16
 
17
+ Have you ever been writing a post with the help of someone who does not have access to your blog and needed to give them the ability to preview it before publishing? This plugin takes care of that by generating an URL with an expiring nonce that can be given out for public preview.
18
 
19
  **Sounds pretty good? Install now!**
20
 
55
 
56
  == Upgrade Notice ==
57
 
58
+ = 2.1 =
59
+ The update fixes a rare issue which had created 404 errors.
60
+
61
  = 2.0 =
62
  New plugin maintainer, supports now all public post types, saves preview status via an AJAX request, ready for translation, requires at least WordPress 3.3.
63
 
66
  * The link will be displayed if the checkbox is checked, just copy and share the link with your frieds.
67
  * To disable a preview just uncheck the box.
68
 
69
+ == FAQ ==
70
+
71
+ **I have activated the "WordPress SEO by Yoast" plugin and enabled the option "Redirect ugly URL's to clean permalinks.". The public preview doesn't work. What can I do?**
72
+
73
+ Please add <strong>_ppp</strong> to the input field "Other variables not to clean:" on SEO -> Permalinks screen.
74
+
75
+
76
+ **After some time the preview link returns the message "The link has been expired!". Why?**
77
+
78
+ The plugin generates an URL with an expiring nonce. By default a link "lives" 48 hours. After 48 hours the link is expired and you need to copy and share a new link which is automatically generated on the same place under the editor.
79
+
80
+
81
+ **48 hours are not enough to me. Can I extend the nonce time?**
82
+
83
+ Yes, of course. You can use the filter `ppp_nonce_life`. Example for 5 days:
84
+
85
+ `add_filter( 'ppp_nonce_life', 'my_nonce_life' );
86
+ function my_nonce_life() {
87
+ return 60 * 60 * 24 * 5; // 5 days
88
+ }`
89
 
90
  == Change Log ==
91
+ = 2.1 (20012-09-16): =
92
+ * Introduces a filter `ppp_nonce_life`. With the filter you can adjust the expiration of a link. By default a link has a lifetime of 48 hours.
93
+ * In some situations (still not sure when) the preview link is rewritten as a permalink which results in an error. The plugin now works in this situations too.
94
+
95
  = 2.0.1 (20012-07-25): =
96
  * Makes the preview link copyable again
97