diff options
Diffstat (limited to 'plugins/public-post-preview')
-rw-r--r-- | plugins/public-post-preview/js/public-post-preview.js | 3 | ||||
-rw-r--r-- | plugins/public-post-preview/js/public-post-preview.min.js | 7 | ||||
-rw-r--r-- | plugins/public-post-preview/public-post-preview.php | 217 | ||||
-rw-r--r-- | plugins/public-post-preview/readme.txt | 57 |
4 files changed, 189 insertions, 95 deletions
diff --git a/plugins/public-post-preview/js/public-post-preview.js b/plugins/public-post-preview/js/public-post-preview.js index 1ba65d81..8b7d55f3 100644 --- a/plugins/public-post-preview/js/public-post-preview.js +++ b/plugins/public-post-preview/js/public-post-preview.js @@ -16,9 +16,6 @@ t.status.css( 'opacity', 0 ); - if ( ! t.checkbox.prop( 'checked' ) ) - t.link.hide(); - t.checkbox.bind( 'change', function() { t.change(); } ); diff --git a/plugins/public-post-preview/js/public-post-preview.min.js b/plugins/public-post-preview/js/public-post-preview.min.js index 0ff89e8e..b9462266 100644 --- a/plugins/public-post-preview/js/public-post-preview.min.js +++ b/plugins/public-post-preview/js/public-post-preview.min.js @@ -1,6 +1 @@ -(function(a){DSPublicPostPreview={initialize:function(){var b=this;b.checkbox=a("#public-post-preview");b.link=a("#public-post-preview-link");b.nonce=a("#public_post_preview_wpnonce"); -b.status=a("#public-post-preview-ajax");b.status.css("opacity",0);if(!b.checkbox.prop("checked")){b.link.hide();}b.checkbox.bind("change",function(){b.change(); -});},change:function(){var b=this,c=b.checkbox.prop("checked")?1:0;b.link.toggle();b.checkbox.prop("disabled","disabled");b.request({_ajax_nonce:b.nonce.val(),checked:c,post_ID:a("#post_ID").val()},function(d){if(d){if(c){b.status.text(DSPublicPostPreviewL10n.enabled); -b._pulsate(b.status,"green");}else{b.status.text(DSPublicPostPreviewL10n.disabled);b._pulsate(b.status,"red");}}b.checkbox.prop("disabled","");});},request:function(b,c){a.ajax({type:"POST",url:ajaxurl,data:a.extend(b,{action:"public-post-preview"}),success:c}); -},_pulsate:function(c,b){c.css("color",b).animate({opacity:1},600,"linear").animate({opacity:0},600,"linear",function(){c.empty();});}};a(DSPublicPostPreview.initialize()); -})(jQuery);
\ No newline at end of file +(function(a){DSPublicPostPreview={initialize:function(){var b=this;b.checkbox=a("#public-post-preview");b.link=a("#public-post-preview-link");b.nonce=a("#public_post_preview_wpnonce");b.status=a("#public-post-preview-ajax");b.status.css("opacity",0);b.checkbox.bind("change",function(){b.change();});},change:function(){var b=this,c=b.checkbox.prop("checked")?1:0;b.link.toggle();b.checkbox.prop("disabled","disabled");b.request({_ajax_nonce:b.nonce.val(),checked:c,post_ID:a("#post_ID").val()},function(d){if(d){if(c){b.status.text(DSPublicPostPreviewL10n.enabled);b._pulsate(b.status,"green");}else{b.status.text(DSPublicPostPreviewL10n.disabled);b._pulsate(b.status,"red");}}b.checkbox.prop("disabled","");});},request:function(b,c){a.ajax({type:"POST",url:ajaxurl,data:a.extend(b,{action:"public-post-preview"}),success:c});},_pulsate:function(c,b){c.css("color",b).animate({opacity:1},600,"linear").animate({opacity:0},600,"linear",function(){c.empty();});}};a(DSPublicPostPreview.initialize());})(jQuery);
\ No newline at end of file diff --git a/plugins/public-post-preview/public-post-preview.php b/plugins/public-post-preview/public-post-preview.php index ce2744be..7b5b1221 100644 --- a/plugins/public-post-preview/public-post-preview.php +++ b/plugins/public-post-preview/public-post-preview.php @@ -1,7 +1,7 @@ <?php /** * Plugin Name: Public Post Preview - * Version: 2.2 + * Version: 2.4 * Description: Enables you to give a link to anonymous users for public preview of any post type before it is published. * Author: Dominik Schilling * Author URI: http://wphelper.de/ @@ -14,7 +14,7 @@ * * Previously (2009-2011) maintained by Jonathan Dingman and Matt Martz. * - * Copyright (C) 2012-2013 Dominik Schilling + * Copyright (C) 2012-2014 Dominik Schilling * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -50,6 +50,8 @@ if ( ! class_exists( 'WP' ) ) { * - posts_results * - wp_ajax_public-post-preview * - admin_enqueue_scripts + * - comments_open + * - pings_open * * Inits at 'plugins_loaded' hook. * @@ -71,6 +73,9 @@ class DS_Public_Post_Preview { add_filter( 'pre_get_posts', array( __CLASS__, 'show_public_preview' ) ); add_filter( 'query_vars', array( __CLASS__, 'add_query_var' ) ); + + // Add the query var to WordPress SEO by Yoast whitelist. + add_filter( 'wpseo_whitelist_permalink_vars', array( __CLASS__, 'add_query_var' ) ); } else { add_action( 'post_submitbox_misc_actions', array( __CLASS__, 'post_submitbox_misc_actions' ) ); @@ -79,6 +84,8 @@ class DS_Public_Post_Preview { add_action( 'wp_ajax_public-post-preview', array( __CLASS__, 'ajax_register_public_preview' ) ); add_action( 'admin_enqueue_scripts' , array( __CLASS__, 'enqueue_script' ) ); + + add_filter( 'display_post_states', array( __CLASS__, 'display_preview_state' ), 20, 2 ); } } @@ -101,8 +108,9 @@ class DS_Public_Post_Preview { * @since 2.0.0 */ public static function enqueue_script( $hook_suffix ) { - if ( ! in_array( $hook_suffix, array( 'post.php', 'post-new.php' ) ) ) + if ( ! in_array( $hook_suffix, array( 'post.php', 'post-new.php' ) ) ) { return; + } $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; @@ -125,7 +133,20 @@ class DS_Public_Post_Preview { } /** - * Adds the checkbox to the submit metabox + * Adds "Public Preview" to the list of display states used in the Posts list table. + * + * @since 2.4.0 + */ + public static function display_preview_state( $post_states, $post ) { + if ( in_array( $post->ID, self::get_preview_post_ids() ) ) { + $post_states['ppp_enabled'] = __( 'Public Preview', 'ds-public-post-preview' ); + } + + return $post_states; + } + + /** + * Adds the checkbox to the submit metabox. * * @since 2.2.0 */ @@ -138,11 +159,19 @@ class DS_Public_Post_Preview { $post = get_post(); - if ( ! in_array( $post->post_type, $post_types ) ) + if ( ! in_array( $post->post_type, $post_types ) ) { return false; + } - if ( ! in_array( $post->post_status, array( 'draft', 'pending', 'future' ) ) ) + // Do nothing for auto drafts + if ( $post->post_status == 'auto-draft' ) { return false; + } + + // Post is already published + if ( in_array( $post->post_status, self::get_published_statuses() ) ) { + return false; + } ?> <div class="misc-pub-section public-post-preview"> @@ -153,23 +182,41 @@ class DS_Public_Post_Preview { } /** - * Prints the checkbox if the is draft, pending or future. + * Returns post statuses which represent a published post. + * + * @since 2.4.0 + * + * @return array List with post statuses. + */ + private static function get_published_statuses() { + $published_statuses = array( 'publish', 'private' ); + + return apply_filters( 'ppp_published_statuses', $published_statuses ); + } + + /** + * Prints the checkbox with the input field for the preview link. * + * @since 2.0.0 + * + * @param WP_Post $post The post object. */ private static function get_checkbox_html( $post ) { - if ( empty( $post ) ) + if ( empty( $post ) ) { $post = get_post(); + } wp_nonce_field( 'public_post_preview', 'public_post_preview_wpnonce' ); $preview_post_ids = self::get_preview_post_ids(); + $enabeld = in_array( $post->ID, $preview_post_ids ); ?> - <label><input type="checkbox"<?php checked( in_array( $post->ID, $preview_post_ids ) ); ?> name="public_post_preview" id="public-post-preview" value="1" /> + <label><input type="checkbox"<?php checked( $enabeld ); ?> name="public_post_preview" id="public-post-preview" value="1" /> <?php _e( 'Enable public preview', 'ds-public-post-preview' ); ?> <span id="public-post-preview-ajax"></span></label> - <div id="public-post-preview-link" style="margin-top:6px"> + <div id="public-post-preview-link" style="margin-top:6px"<?php echo $enabeld ? '' : ' class="hidden"'; ?>> <label> - <input type="text" name="public_post_preview_link" class="regular-text" value="<?php echo esc_attr( self::get_preview_link( $post->ID ) ); ?>" style="width:99%" readonly /> + <input type="text" name="public_post_preview_link" class="regular-text" value="<?php echo esc_attr( self::get_preview_link( $post ) ); ?>" style="width:99%" readonly /> <span class="description"><?php _e( '(Copy and share this link.)', 'ds-public-post-preview' ); ?></span> </label> </div> @@ -177,25 +224,40 @@ class DS_Public_Post_Preview { } /** - * Returns the public preview link. - * - * The link is the permalink with these parameters: - * - preview, always true (query var for core) - * - _ppp, a custom nonce, see DS_Public_Post_Preview::create_nonce() - * - * @since 2.0.0 - * - * @param int $post_id The post id. - * @return string The generated public preview link. - */ - private static function get_preview_link( $post_id ) { - return add_query_arg( - array( - 'preview' => true, - '_ppp' => self::create_nonce( 'public_post_preview_' . $post_id ), - ), - get_permalink( $post_id ) - ); + * Returns the public preview link. + * + * The link is the home link with these parameters: + * - preview, always true (query var for core) + * - _ppp, a custom nonce, see DS_Public_Post_Preview::create_nonce() + * - page_id or p or p and post_type to specify the post. + * + * @since 2.0.0 + * + * @param WP_Post $post The post object. + * @return string The generated public preview link. + */ + private static function get_preview_link( $post ) { + if ( 'page' == $post->post_type ) { + $args = array( + 'page_id' => $post->ID, + ); + } else if ( 'post' == $post->post_type ) { + $args = array( + 'p' => $post->ID, + ); + } else { + $args = array( + 'p' => $post->ID, + 'post_type' => $post->post_type, + ); + } + + $args['preview'] = true; + $args['_ppp'] = self::create_nonce( 'public_post_preview_' . $post->ID ); + + $link = add_query_arg( $args, home_url( '/' ) ); + + return apply_filters( 'ppp_preview_link', $link, $post->ID, $post ); } /** @@ -210,32 +272,36 @@ class DS_Public_Post_Preview { * @return bool Returns false on a failure, true on a success. */ public static function register_public_preview( $post_id, $post ) { - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return false; + } - if ( wp_is_post_revision( $post_id ) ) + if ( wp_is_post_revision( $post_id ) ) { return false; + } - if ( empty( $_POST['public_post_preview_wpnonce'] ) || ! wp_verify_nonce( $_POST['public_post_preview_wpnonce'], 'public_post_preview' ) ) + if ( empty( $_POST['public_post_preview_wpnonce'] ) || ! wp_verify_nonce( $_POST['public_post_preview_wpnonce'], 'public_post_preview' ) ) { return false; + } $preview_post_ids = self::get_preview_post_ids(); $preview_post_id = $post->ID; - if ( empty( $_POST['public_post_preview'] ) && in_array( $preview_post_id, $preview_post_ids ) ) + if ( empty( $_POST['public_post_preview'] ) && in_array( $preview_post_id, $preview_post_ids ) ) { $preview_post_ids = array_diff( $preview_post_ids, (array) $preview_post_id ); - elseif ( + } elseif ( ! empty( $_POST['public_post_preview'] ) && ! empty( $_POST['original_post_status'] ) && 'publish' != $_POST['original_post_status'] && 'publish' == $post->post_status && in_array( $preview_post_id, $preview_post_ids ) - ) + ) { $preview_post_ids = array_diff( $preview_post_ids, (array) $preview_post_id ); - elseif ( ! empty( $_POST['public_post_preview'] ) && ! in_array( $preview_post_id, $preview_post_ids ) ) + } elseif ( ! empty( $_POST['public_post_preview'] ) && ! in_array( $preview_post_id, $preview_post_ids ) ) { $preview_post_ids = array_merge( $preview_post_ids, (array) $preview_post_id ); - else - return false; // Nothing changed. + } else { + return false; // Nothing has changed. + } return self::set_preview_post_ids( $preview_post_ids ); } @@ -253,25 +319,29 @@ class DS_Public_Post_Preview { $preview_post_id = (int) $_POST['post_ID']; $post = get_post( $preview_post_id ); - if ( ( 'page' == $post->post_type && ! current_user_can( 'edit_page', $preview_post_id ) ) || ! current_user_can( 'edit_post', $preview_post_id ) ) + if ( ( 'page' == $post->post_type && ! current_user_can( 'edit_page', $preview_post_id ) ) || ! current_user_can( 'edit_post', $preview_post_id ) ) { wp_die( 0 ); + } - if ( ! in_array( $post->post_status, array( 'draft', 'pending', 'future' ) ) ) + if ( in_array( $post->post_status, self::get_published_statuses() ) ) { wp_die( 0 ); + } $preview_post_ids = self::get_preview_post_ids(); - if ( empty( $_POST['checked'] ) && in_array( $preview_post_id, $preview_post_ids ) ) + if ( empty( $_POST['checked'] ) && in_array( $preview_post_id, $preview_post_ids ) ) { $preview_post_ids = array_diff( $preview_post_ids, (array) $preview_post_id ); - elseif ( ! empty( $_POST['checked'] ) && ! in_array( $preview_post_id, $preview_post_ids ) ) + } elseif ( ! empty( $_POST['checked'] ) && ! in_array( $preview_post_id, $preview_post_ids ) ) { $preview_post_ids = array_merge( $preview_post_ids, (array) $preview_post_id ); - else + } else { wp_die( 0 ); + } $ret = self::set_preview_post_ids( $preview_post_ids ); - if ( ! $ret ) + if ( ! $ret ) { wp_die( 0 ); + } wp_die( 1 ); } @@ -279,7 +349,7 @@ class DS_Public_Post_Preview { /** * Registers the new query var `_ppp`. * - * @since 2.1 + * @since 2.1.0 * * @return array List of query variables. */ @@ -306,8 +376,9 @@ class DS_Public_Post_Preview { $query->is_preview() && $query->is_singular() && $query->get( '_ppp' ) - ) + ) { add_filter( 'posts_results', array( __CLASS__, 'set_post_to_publish' ), 10, 2 ); + } return $query; } @@ -321,15 +392,18 @@ class DS_Public_Post_Preview { * @param int $post_id The post id. * @return bool True if a public preview is allowed, false on a failure. */ - private static function public_preview_available( $post_id ) { - if ( empty( $post_id ) ) + private static function is_public_preview_available( $post_id ) { + if ( empty( $post_id ) ) { return false; + } - if( ! self::verify_nonce( get_query_var( '_ppp' ), 'public_post_preview_' . $post_id ) ) + if ( ! self::verify_nonce( get_query_var( '_ppp' ), 'public_post_preview_' . $post_id ) ) { wp_die( __( 'The link has been expired!', 'ds-public-post-preview' ) ); + } - if ( ! in_array( $post_id, get_option( 'public_post_preview', array() ) ) ) + if ( ! in_array( $post_id, self::get_preview_post_ids() ) ) { wp_die( __( 'No Public Preview available!', 'ds-public-post-preview' ) ); + } return true; } @@ -346,16 +420,44 @@ class DS_Public_Post_Preview { // Remove the filter again, otherwise it will be applied to other queries too. remove_filter( 'posts_results', array( __CLASS__, 'set_post_to_publish' ), 10, 2 ); - if ( empty( $posts ) ) + if ( empty( $posts ) ) { return; + } - if ( self::public_preview_available( $posts[0]->ID ) ) + $post_id = $posts[0]->ID; + + // If the post has gone live, redirect to it's proper permalink + self::maybe_redirect_to_published_post( $post_id ); + + if ( self::is_public_preview_available( $post_id ) ) { + // Set post status to publish so that it's visible $posts[0]->post_status = 'publish'; + // Disable comments and pings for this post + add_filter( 'comments_open', '__return_false' ); + add_filter( 'pings_open', '__return_false' ); + } + return $posts; } /** + * Redirects to post's proper permalink, if it has gone live. + * + * @since 2.0.0 + * + * @param int $post_id The post id. + */ + private static function maybe_redirect_to_published_post( $post_id ) { + if ( ! in_array( get_post_status( $post_id ), self::get_published_statuses() ) ) { + return false; + } + + wp_redirect( get_permalink( $post_id ), 301 ); + exit; + } + + /** * Get the time-dependent variable for nonce creation. * * @see wp_nonce_tick() @@ -401,12 +503,14 @@ class DS_Public_Post_Preview { $i = self::nonce_tick(); // Nonce generated 0-12 hours ago - if ( substr( wp_hash( $i . $action, 'nonce' ), -12, 10 ) == $nonce ) + if ( substr( wp_hash( $i . $action, 'nonce' ), -12, 10 ) == $nonce ) { return 1; + } // Nonce generated 12-24 hours ago - if ( substr( wp_hash( ( $i - 1 ) . $action, 'nonce' ), -12, 10 ) == $nonce ) + if ( substr( wp_hash( ( $i - 1 ) . $action, 'nonce' ), -12, 10 ) == $nonce ) { return 2; + } // Invalid nonce return false; @@ -444,8 +548,9 @@ class DS_Public_Post_Preview { */ private static function get_plugin_info( $key = null ) { $plugin_data = get_plugin_data( __FILE__); - if ( array_key_exists( $key, $plugin_data ) ) + if ( array_key_exists( $key, $plugin_data ) ) { return $plugin_data[ $key ]; + } return false; } diff --git a/plugins/public-post-preview/readme.txt b/plugins/public-post-preview/readme.txt index 9c144b84..8aa49458 100644 --- a/plugins/public-post-preview/readme.txt +++ b/plugins/public-post-preview/readme.txt @@ -1,10 +1,10 @@ === Public Post Preview === Contributors: ocean90 -Tags: public, post, preview, posts, custom post types +Tags: public, post, preview, posts, custom post types, draft Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VR8YU922B7K46 Requires at least: 3.5 -Tested up to: 3.6 -Stable tag: 2.2 +Tested up to: 4.0 +Stable tag: 2.4 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -21,7 +21,7 @@ Have you ever been writing a post with the help of someone who does not have acc *Previously this plugin was maintained by [Matt Martz](http://profiles.wordpress.org/sivel/) and was an idea of [Jonathan Dingman](http://profiles.wordpress.org/jdingman/).* = Feedback = -If you want, you can drop me a line @[ocean90](http://twitter.com/ocean90) on Twitter or @[Dominik Schilling](https://plus.google.com/101675293278434581718/) on Google+. +If you want, you can drop me a line @[ocean90](http://twitter.com/ocean90) on Twitter or @[Dominik Schilling](https://plus.google.com/+DominikSchilling/) on Google+. = More = Try also some of my [other plugins](http://profiles.wordpress.org/users/ocean90) or visit my site [wpGrafie.de](http://wpgrafie.de/). @@ -53,29 +53,13 @@ To upload the plugin through WordPress, instead of FTP: 1. Edit Posts Page -== Upgrade Notice == - -= 2.2 = -Based on feedback: Removed the extra metabox and added preview link to the main Publish metabox. Requires now WordPress 3.5 - -= 2.1 = -The update fixes a rare issue which had created 404 errors. - -= 2.0 = -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. - == Usage == * To enable a public post preview check the box below the edit post box. -* The link will be displayed if the checkbox is checked, just copy and share the link with your frieds. +* The link will be displayed if the checkbox is checked, just copy and share the link with your friends. * To disable a preview just uncheck the box. == Frequently Asked Questions == -**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?** - -Please add <strong>_ppp</strong> to the input field "Other variables not to clean:" on SEO -> Permalinks screen. - - **After some time the preview link returns the message "The link has been expired!". Why?** 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. @@ -90,23 +74,36 @@ function my_nonce_life() { return 60 * 60 * 24 * 5; // 5 days }` -== Change Log == -= 2.2 (20013-03-15): = -* Based on feedback I have removed the extra metabox and added the preview link to the main Publish metabox -* Only shows the checkbox if the post status/post type is good -* Requires now WordPress 3.5 +Or use the [Public Post Preview Configurator](https://wordpress.org/plugins/public-post-preview-configurator/). -= 2.1.1 (20012-09-19): = +== Change Log == += 2.4 (2014-08-21): +* Supports EditFlow and custom statuses +* Disables comments and pings during public post preview +* Adds **Public Preview** to the list of display states used in the Posts list table +* Prevents flickering of link box on Post edit while loading + += 2.3 (2013-11-18): = +* Introduces a filter `ppp_preview_link`. With the filter you can adjust the preview link. +* If a post has gone live, redirect to it's proper permalink. +* Adds the query var `_ppp` to WordPress SEO by Yoast whitelist. + += 2.2 (2013-03-15): = +* Based on feedback I have removed the extra metabox and added the preview link to the main Publish metabox. +* Only show the checkbox if the post status/post type is good. +* Requires WordPress 3.5. + += 2.1.1 (2012-09-19): = * Sorry for the new update. Through a change in 2.1 a filter was applied to each query. The misplaced "The link has been expired!" message is now gone. Props Aki Björklund and Jonathan Channon. -= 2.1 (20012-09-16): = += 2.1 (2012-09-16): = * 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. * 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. -= 2.0.1 (20012-07-25): = += 2.0.1 (2012-07-25): = * Makes the preview link copyable again -= 2.0 (20012-07-23): = += 2.0 (2012-07-23): = * Support for all public post types * Saves public preview status via an AJAX request * I18n |