From 662d15c1d2c5bb896985fd551a3649fcd280429e Mon Sep 17 00:00:00 2001 From: "Anthony G. Basile" Date: Tue, 1 Jan 2019 22:20:16 -0500 Subject: Update public-post-preview 2.8.0 Signed-off-by: Anthony G. Basile --- .../js/gutenberg-integration.js | 1 + .../js/public-post-preview.dev.js | 111 ------------- .../public-post-preview/js/public-post-preview.js | 2 +- .../js/public-post-preview.min.js | 2 +- .../js/src/components/preview-toggle/index.js | 171 +++++++++++++++++++++ plugins/public-post-preview/js/src/index.js | 6 + .../languages/public-post-preview.php | 19 +++ .../languages/public-post-preview.pot | 24 +++ .../public-post-preview/public-post-preview.php | 167 +++++++++++++------- plugins/public-post-preview/readme.txt | 17 +- 10 files changed, 346 insertions(+), 174 deletions(-) create mode 100644 plugins/public-post-preview/js/gutenberg-integration.js delete mode 100644 plugins/public-post-preview/js/public-post-preview.dev.js create mode 100644 plugins/public-post-preview/js/src/components/preview-toggle/index.js create mode 100644 plugins/public-post-preview/js/src/index.js create mode 100644 plugins/public-post-preview/languages/public-post-preview.php create mode 100644 plugins/public-post-preview/languages/public-post-preview.pot diff --git a/plugins/public-post-preview/js/gutenberg-integration.js b/plugins/public-post-preview/js/gutenberg-integration.js new file mode 100644 index 00000000..6bf327c1 --- /dev/null +++ b/plugins/public-post-preview/js/gutenberg-integration.js @@ -0,0 +1 @@ +this.publicPostPreview=this.publicPostPreview||{},this.publicPostPreview.main=function(e){var t={};function r(n){if(t[n])return t[n].exports;var i=t[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)r.d(n,i,function(t){return e[t]}.bind(null,i));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=22)}([function(e,t){!function(){e.exports=this.wp.element}()},function(e,t){e.exports=function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}},function(e,t){!function(){e.exports=this.wp.i18n}()},function(e,t,r){"use strict";(function(e){r.d(t,"a",function(){return c});var n=r(17),i=void 0!==e?e:{},a=Object(n.a)(i),c=(a.flush,a.hydrate,a.cx,a.merge,a.getRegisteredStyles,a.injectGlobal,a.keyframes,a.css);a.sheet,a.caches}).call(this,r(21))},function(e,t){!function(){e.exports=this.wp.components}()},function(e,t){!function(){e.exports=this.wp.compose}()},function(e,t){!function(){e.exports=this.wp.editPost}()},function(e,t){!function(){e.exports=this.wp.plugins}()},function(e,t,r){var n=r(18);e.exports=function(e){for(var t=1;t=4;)t=1540483477*(65535&(t=255&e.charCodeAt(i)|(255&e.charCodeAt(++i))<<8|(255&e.charCodeAt(++i))<<16|(255&e.charCodeAt(++i))<<24))+((1540483477*(t>>>16)&65535)<<16),n=1540483477*(65535&n)+((1540483477*(n>>>16)&65535)<<16)^(t=1540483477*(65535&(t^=t>>>24))+((1540483477*(t>>>16)&65535)<<16)),r-=4,++i;switch(r){case 3:n^=(255&e.charCodeAt(i+2))<<16;case 2:n^=(255&e.charCodeAt(i+1))<<8;case 1:n=1540483477*(65535&(n^=255&e.charCodeAt(i)))+((1540483477*(n>>>16)&65535)<<16)}return n=1540483477*(65535&(n^=n>>>13))+((1540483477*(n>>>16)&65535)<<16),((n^=n>>>15)>>>0).toString(36)};var c=function(e){function t(e,t,n){var i=t.trim().split(d);t=i;var a=i.length,c=e.length;switch(c){case 0:case 1:var o=0;for(e=0===c?"":e[0]+" ";on&&(n=(t=t.trim()).charCodeAt(0)),n){case 38:return t.replace(b,"$1"+e.trim());case 58:return e.trim()+t.replace(b,"$1"+e.trim());default:if(0<1*r&&0s.charCodeAt(8))break;case 115:c=c.replace(s,"-webkit-"+s)+";"+c;break;case 207:case 102:c=c.replace(s,"-webkit-"+(102o.charCodeAt(0)&&(o=o.trim()),o=[o],0p)&&(F=(D=D.replace(" ",":")).length),01?i-1:0),o=1;o { + this.setState( { previewEnabled: ! this.state.previewEnabled } ); + } ) + } + + onPreviewUrlInputFocus() { + this.previewUrlInput.current.focus(); + this.previewUrlInput.current.select(); + } + + request( data, callback ) { + jQuery.ajax( { + type: 'POST', + url: ajaxurl, + data: { + action: 'public-post-preview', + _ajax_nonce: DSPublicPostPreviewData.nonce, + ...data + }, + success: callback, + } ); + } + + render() { + const { + previewEnabled, + previewUrl, + hasCopied + } = this.state; + + const ariaCopyLabel = hasCopied ? __( 'Preview URL copied', 'public-post-preview' ) : __( 'Copy the preview URL', 'public-post-preview' ); + + return ( + + + + + { previewEnabled && + +

+ + + this.setState( { hasCopied: true } ) } + onFinishCopy={ () => this.setState( { hasCopied: false } ) } + aria-disabled={ hasCopied } + icon={ } + /> +

+

+ { __( 'Copy and share this preview URL.', 'public-post-preview' ) } +

+
+ } +
+ ); + } +} + +export default compose( [ + withSelect( ( select ) => { + const { + getPostType, + } = select( 'core' ); + const { + getCurrentPostId, + getEditedPostAttribute, + } = select( 'core/editor' ); + const postType = getPostType( getEditedPostAttribute( 'type' ) ); + + return { + postId: getCurrentPostId(), + status: getEditedPostAttribute( 'status' ), + isViewable: get( postType, [ 'viewable' ], false ), + }; + } ), + ifCondition( ( { isViewable } ) => isViewable ), + ifCondition( ( { status } ) => { + return [ + 'auto-draft', + 'publish', + 'private', + ].indexOf( status ) === -1; + } ), +] )( PreviewToggle ); diff --git a/plugins/public-post-preview/js/src/index.js b/plugins/public-post-preview/js/src/index.js new file mode 100644 index 00000000..d56dfc6a --- /dev/null +++ b/plugins/public-post-preview/js/src/index.js @@ -0,0 +1,6 @@ +import { registerPlugin } from '@wordpress/plugins'; +import { default as PreviewToggle } from './components/preview-toggle'; + +registerPlugin( 'public-post-preview', { + render: PreviewToggle, +} ); diff --git a/plugins/public-post-preview/languages/public-post-preview.php b/plugins/public-post-preview/languages/public-post-preview.php new file mode 100644 index 00000000..118fa0f8 --- /dev/null +++ b/plugins/public-post-preview/languages/public-post-preview.php @@ -0,0 +1,19 @@ +is_block_editor() ) || + ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) + ) { + wp_enqueue_script( + 'public-post-preview-gutenberg', + plugins_url( 'js/gutenberg-integration.js', __FILE__ ), + array( + 'lodash', + 'wp-compose', + 'wp-components', + 'wp-data', + 'wp-edit-post', + 'wp-element', + 'wp-i18n', + ), + '20181127', + true + ); - wp_enqueue_script( - 'public-post-preview', - plugins_url( "js/public-post-preview$suffix.js", __FILE__ ), - array( 'jquery' ), - '20160403', - true - ); + $post = get_post(); + wp_localize_script( + 'public-post-preview-gutenberg', + 'DSPublicPostPreviewData', + array( + 'previewEnabled' => self::is_public_preview_enabled( $post ), + 'previewUrl' => self::get_preview_link( $post ), + 'nonce' => wp_create_nonce( 'public-post-preview_' . $post->ID ), + ) + ); - wp_localize_script( - 'public-post-preview', - 'DSPublicPostPreviewL10n', - array( - 'enabled' => __( 'Enabled!', 'public-post-preview' ), - 'disabled' => __( 'Disabled!', 'public-post-preview' ), - ) - ); + if ( function_exists( 'gutenberg_get_jed_locale_data' ) ) { + $locale_data = gutenberg_get_jed_locale_data( 'public-post-preview' ); + wp_add_inline_script( + 'public-post-preview-gutenberg', + 'wp.i18n.setLocaleData( ' . wp_json_encode( $locale_data ) . ', "public-post-preview" );', + 'before' + ); + } + } else { + $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; + + wp_enqueue_script( + 'public-post-preview', + plugins_url( "js/public-post-preview$suffix.js", __FILE__ ), + array( 'jquery' ), + '20180914', + true + ); + + wp_localize_script( + 'public-post-preview', + 'DSPublicPostPreviewL10n', + array( + 'enabled' => __( 'Enabled!', 'public-post-preview' ), + 'disabled' => __( 'Disabled!', 'public-post-preview' ), + ) + ); + } } /** @@ -187,10 +226,9 @@ class DS_Public_Post_Preview { $post = get_post(); } - wp_nonce_field( 'public_post_preview', 'public_post_preview_wpnonce' ); + wp_nonce_field( 'public-post-preview_' . $post->ID, 'public_post_preview_wpnonce' ); - $preview_post_ids = self::get_preview_post_ids(); - $enabled = in_array( $post->ID, $preview_post_ids ); + $enabled = self::is_public_preview_enabled( $post ); ?> @@ -198,12 +236,25 @@ class DS_Public_Post_Preview { ID, $preview_post_ids, true ); + } + /** * Returns the public preview link. * @@ -220,25 +271,25 @@ class DS_Public_Post_Preview { public static function get_preview_link( $post ) { if ( 'page' == $post->post_type ) { $args = array( - 'page_id' => $post->ID, + 'page_id' => $post->ID, ); - } else if ( 'post' == $post->post_type ) { + } elseif ( 'post' == $post->post_type ) { $args = array( - 'p' => $post->ID, + 'p' => $post->ID, ); } else { $args = array( - 'p' => $post->ID, - 'post_type' => $post->post_type, + 'p' => $post->ID, + 'post_type' => $post->post_type, ); } $args['preview'] = true; - $args['_ppp'] = self::create_nonce( 'public_post_preview_' . $post->ID ); + $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 ); + return apply_filters( 'ppp_preview_link', $link, $post->ID, $post ); } /** @@ -298,7 +349,7 @@ class DS_Public_Post_Preview { * @return bool Returns false on a failure, true on a success. */ public static function unregister_public_preview_on_status_change( $new_status, $old_status, $post ) { - $disallowed_status = self::get_published_statuses(); + $disallowed_status = self::get_published_statuses(); $disallowed_status[] = 'trash'; if ( in_array( $new_status, $disallowed_status ) ) { @@ -318,7 +369,7 @@ class DS_Public_Post_Preview { * @return bool Returns false on a failure, true on a success. */ public static function unregister_public_preview_on_edit( $post_id, $post ) { - $disallowed_status = self::get_published_statuses(); + $disallowed_status = self::get_published_statuses(); $disallowed_status[] = 'trash'; if ( in_array( $post->post_status, $disallowed_status ) ) { @@ -356,36 +407,37 @@ class DS_Public_Post_Preview { * @since 2.0.0 */ public static function ajax_register_public_preview() { - check_ajax_referer( 'public_post_preview' ); - $preview_post_id = (int) $_POST['post_ID']; + + check_ajax_referer( 'public-post-preview_' . $preview_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 ) ) { - wp_die( 0 ); + if ( ! current_user_can( 'edit_post', $preview_post_id ) ) { + wp_send_json_error( 'cannot_edit' ); } if ( in_array( $post->post_status, self::get_published_statuses() ) ) { - wp_die( 0 ); + wp_send_json_error( 'invalid_post_status' ); } $preview_post_ids = self::get_preview_post_ids(); - if ( empty( $_POST['checked'] ) && in_array( $preview_post_id, $preview_post_ids ) ) { + if ( 'false' === $_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 ( 'true' === $_POST['checked'] && ! in_array( $preview_post_id, $preview_post_ids ) ) { $preview_post_ids = array_merge( $preview_post_ids, (array) $preview_post_id ); } else { - wp_die( 0 ); + wp_send_json_error( 'unknown_status' ); } $ret = self::set_preview_post_ids( $preview_post_ids ); if ( ! $ret ) { - wp_die( 0 ); + wp_send_json_error( 'not_saved' ); } - wp_die( 1 ); + wp_send_json_success(); } /** @@ -423,6 +475,7 @@ class DS_Public_Post_Preview { if ( ! headers_sent() ) { nocache_headers(); } + add_action( 'wp_head', 'wp_no_robots' ); add_filter( 'posts_results', array( __CLASS__, 'set_post_to_publish' ), 10, 2 ); } @@ -449,7 +502,7 @@ class DS_Public_Post_Preview { } if ( ! in_array( $post_id, self::get_preview_post_ids() ) ) { - wp_die( __( 'No Public Preview available!', 'public-post-preview' ) ); + wp_die( __( 'No public preview available!', 'public-post-preview' ) ); } return true; diff --git a/plugins/public-post-preview/readme.txt b/plugins/public-post-preview/readme.txt index c2867091..ebd6f194 100644 --- a/plugins/public-post-preview/readme.txt +++ b/plugins/public-post-preview/readme.txt @@ -1,9 +1,9 @@ === Public Post Preview === Contributors: ocean90 Tags: public, post, preview, posts, custom post types, draft -Requires at least: 3.5 -Tested up to: 4.7 -Stable tag: 2.6.0 +Requires at least: 4.6 +Tested up to: 5.0 +Stable tag: 2.8.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -40,7 +40,8 @@ To upload the plugin through WordPress, instead of FTP: == Screenshots == -1. Edit Posts Page +1. Setting in block editor +2. Setting in classic editor == Usage == * To enable a public post preview check the box below the edit post box. @@ -71,6 +72,14 @@ function my_nonce_life() { Or use the [Public Post Preview Configurator](https://wordpress.org/plugins/public-post-preview-configurator/). == Change Log == + += 2.8.0 (2018-11-27): = +* Add support for WordPress 5.0 and the new block editor. + += 2.7.0 (2018-09-14): = +* Initial support for Gutenberg. +* Block robots for public post previews. Props [@westonruter](https://github.com/westonruter). + = 2.6.0 (2017-04-27): = * Make `DS_Public_Post_Preview::get_preview_link()` public. Props [@rcstr](https://github.com/rcstr). * Send no-cache headers for public post previews. -- cgit v1.2.3-65-gdbad