diff options
Diffstat (limited to 'plugins/public-post-preview/public-post-preview.php')
-rw-r--r-- | plugins/public-post-preview/public-post-preview.php | 167 |
1 files changed, 110 insertions, 57 deletions
diff --git a/plugins/public-post-preview/public-post-preview.php b/plugins/public-post-preview/public-post-preview.php index 5130a0a1..742a93c3 100644 --- a/plugins/public-post-preview/public-post-preview.php +++ b/plugins/public-post-preview/public-post-preview.php @@ -1,33 +1,31 @@ <?php /** * Plugin Name: Public Post Preview - * Version: 2.6.0 + * Version: 2.8.0 * 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/ + * Author URI: https://wphelper.de/ * Plugin URI: https://dominikschilling.de/wp-plugins/public-post-preview/en/ - * * Text Domain: public-post-preview - * * License: GPLv2 or later * * Previously (2009-2011) maintained by Jonathan Dingman and Matt Martz. * - * Copyright (C) 2012-2017 Dominik Schilling + * Copyright (C) 2012-2018 Dominik Schilling * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** @@ -63,7 +61,7 @@ class DS_Public_Post_Preview { add_action( 'post_submitbox_misc_actions', array( __CLASS__, 'post_submitbox_misc_actions' ) ); add_action( 'save_post', array( __CLASS__, 'register_public_preview' ), 20, 2 ); add_action( 'wp_ajax_public-post-preview', array( __CLASS__, 'ajax_register_public_preview' ) ); - add_action( 'admin_enqueue_scripts' , array( __CLASS__, 'enqueue_script' ) ); + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_script' ) ); add_filter( 'display_post_states', array( __CLASS__, 'display_preview_state' ), 20, 2 ); } } @@ -89,24 +87,65 @@ class DS_Public_Post_Preview { return; } - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; + if ( + ( method_exists( get_current_screen(), 'is_block_editor' ) && get_current_screen()->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 ); ?> <label><input type="checkbox"<?php checked( $enabled ); ?> name="public_post_preview" id="public-post-preview" value="1" /> <?php _e( 'Enable public preview', 'public-post-preview' ); ?> <span id="public-post-preview-ajax"></span></label> @@ -198,13 +236,26 @@ class DS_Public_Post_Preview { <div id="public-post-preview-link" style="margin-top:6px"<?php echo $enabled ? '' : ' class="hidden"'; ?>> <label> <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.)', 'public-post-preview' ); ?></span> + <span class="description"><?php _e( 'Copy and share this preview URL.', 'public-post-preview' ); ?></span> </label> </div> <?php } /** + * Checks if a public preview is enabled for a post. + * + * @since 2.7.0 + * + * @param WP_Post $post The post object. + * @return bool True if a public preview is enabled, false if not. + */ + private static function is_public_preview_enabled( $post ) { + $preview_post_ids = self::get_preview_post_ids(); + return in_array( $post->ID, $preview_post_ids, true ); + } + + /** * Returns the public preview link. * * The link is the home link with these parameters: @@ -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; |