2
0
Fork 0
mirror of https://github.com/discourse/wp-discourse.git synced 2025-08-17 18:11:19 +08:00

Revert "Remove files for WordPress VIP plugin"

This reverts commit 7d1a9b32ca.
This commit is contained in:
scossar 2019-08-26 14:13:18 -07:00
parent e7a64bc08e
commit 079d41f78b
5 changed files with 243 additions and 0 deletions

130
admin/admin-notice.php Normal file
View file

@ -0,0 +1,130 @@
<?php
/**
* Add admin notices.
*
* @package WPDiscourse;
*/
namespace WPDiscourse\Admin;
use WPDiscourse\Shared\PluginUtilities;
/**
* Class AdminNotice
*/
class AdminNotice {
use PluginUtilities;
/**
* Gives access to the plugin options.
*
* @access protected
* @var mixed|void
*/
protected $options;
/**
* AdminNotice constructor.
*/
public function __construct() {
add_action( 'admin_notices', array( $this, 'set_admin_notices' ) );
add_action( 'admin_init', array( $this, 'setup_options' ) );
}
/**
* Sets the plugin options.
*/
public function setup_options() {
$this->options = $this->get_options();
}
/**
* Set admin notices.
*
* @return null
*/
public function set_admin_notices() {
global $pagenow, $post;
$current_screen = get_current_screen();
// Admin notices aren't supported by the block editor. For now, disable admin notices for versions >= 5.0.
if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) ) {
return null;
}
// Post edit screen notices.
if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
$allowed_post_types = ! empty( $this->options['allowed_post_types'] ) ? $this->options['allowed_post_types'] : array();
if ( in_array( $post->post_type, $allowed_post_types, true ) ) {
$post_id = $post->ID;
$discourse_publishing_response = get_post_meta( $post_id, 'wpdc_publishing_response', true );
if ( ! empty( $discourse_publishing_response ) ) {
if ( 'error' === $discourse_publishing_response ) {
$error_message = __( '<div class="notice notice-error is-dismissible"><p>There has been an error publishing this post to Discourse.</p></div>', 'wp-discourse' );
echo wp_kses_post( $error_message );
delete_post_meta( $post_id, 'wpdc_publishing_response' );
}
if ( 'success' === $discourse_publishing_response ) {
$discourse_permalink = get_post_meta( $post_id, 'discourse_permalink', true );
$discourse_link = '<a href="' . esc_url( $discourse_permalink ) . '" target="_blank">' . __( 'View post', 'wp-discourse' ) . '</a>';
$success_message = sprintf(
// translators: Discourse post-published success message. Placeholder: discourse_permalink.
__( '<div class="notice notice-success is-dismissible"><p>Your post has been published to Discourse. %1$s on Discourse.</p></div>', 'wp-discourse' ),
$discourse_link
);
delete_post_meta( $post_id, 'wpdc_publishing_response' );
echo wp_kses_post( $success_message );
}
}
$discourse_linking_response = get_post_meta( $post_id, 'wpdc_linking_response', true );
if ( 'error' === $discourse_linking_response ) {
$error_message = __( '<div class="notice notice-error is-dismissible"><p>There has been an error linking this post with Discourse. Make sure you are supplying the URL of an existing topic on your forum.</p></div>', 'wp-discourse' );
delete_post_meta( $post_id, 'wpdc_linking_response' );
echo wp_kses_post( $error_message );
}
if ( 'invalid_url' === $discourse_linking_response ) {
$error_message = __( '<div class="notice notice-error is-dismissible"><p>There has been an error linking this post with Discourse. The supplied URL does not match your forum\'s domain.</p></div>', 'wp-discourse' );
delete_post_meta( $post_id, 'wpdc_linking_response' );
echo wp_kses_post( $error_message );
}
$discourse_username = get_user_meta( get_current_user_id(), 'discourse_username', true );
$current_username = wp_get_current_user()->user_login;
$publish_username = ! empty( $this->options['publish-username'] ) ? $this->options['publish-username'] : '';
$use_discourse_name_field = empty( $this->options['hide-discourse-name-field'] );
$profile_page_link = '<a href="' . esc_url( admin_url( '/profile.php' ) ) . '">' . __( 'profile page', 'wp-discourse' ) . '</a>';
if ( empty( $discourse_username )
&& $use_discourse_name_field
&& $current_username !== $publish_username
) {
$username_not_set_notice = sprintf(
// translators: Discourse username_not_set notice. Placeholder: discourse_username.
__( '<div class="notice notice-error is-dismissible"><p>You have not set your Discourse username. Any posts you publish to Discourse will be published under the system default username \'%1$s\'. To stop seeing this notice, please visit your %2$s and set your Discourse username.</p></div>', 'wp-discourse' ),
$publish_username,
$profile_page_link
);
echo wp_kses_post( $username_not_set_notice );
}
}// End if().
}// End if().
return null;
}
}

View file

@ -17,7 +17,9 @@ if ( is_admin() ) {
require_once __DIR__ . '/options-page.php';
require_once __DIR__ . '/publish-settings.php';
require_once __DIR__ . '/settings-validator.php';
require_once __DIR__ . '/sso-settings.php';
require_once __DIR__ . '/webhook-settings.php';
require_once __DIR__ . '/admin-notice.php';
$form_helper = FormHelper::get_instance();
$options_page = OptionsPage::get_instance();
@ -26,8 +28,10 @@ if ( is_admin() ) {
new PublishSettings( $form_helper );
new CommentSettings( $form_helper );
new ConfigurableTextSettings( $form_helper );
new SSOSettings( $form_helper );
new WebhookSettings( $form_helper );
new SettingsValidator();
new AdminNotice();
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\enqueue_admin_scripts' );
if ( is_multisite() ) {
@ -35,6 +39,7 @@ if ( is_admin() ) {
}
} // End if().
/**
* Enqueue admin styles and scripts.
*/

View file

@ -68,6 +68,17 @@ class CommentSettings {
'discourse_commenting_settings_section'
);
add_settings_field(
'discourse_ajax_load',
__( 'Load Comments With Ajax', 'wp-discourse' ),
array(
$this,
'ajax_load_checkbox',
),
'discourse_comment',
'discourse_commenting_settings_section'
);
add_settings_field(
'discourse_load_comment_css',
__( 'Load Comment CSS', 'wp-discourse' ),
@ -287,6 +298,22 @@ class CommentSettings {
);
}
/**
* Outputs markup for the discourse-new-tab checkbox.
*/
public function ajax_load_checkbox() {
$this->form_helper->checkbox_input(
'ajax-load',
'discourse_comment',
__( 'Load comments with Ajax.', 'wp-discourse' ),
__(
'This is useful if page caching is preventing Discourse comments from updating on WordPress. When this setting is enabled, old WordPress comments
cannot be displayed beneath the Discourse comments.',
'wp-discourse'
)
);
}
/**
* Outputs markup for the load-comment-css checkbox.
*/

27
js/load-comments.js Normal file
View file

@ -0,0 +1,27 @@
/* globals wpdc */
/**
* Loads Discourse comments into the .wpdc-comments div.
*
* @package WPDiscourse
*/
(function( $ ) {
$( document ).ready(
function() {
var commentsURL = wpdc.commentsURL,
$commentArea = $( '#wpdc-comments' ),
postId = $commentArea.data( 'post-id' );
$.ajax(
{
url: commentsURL + '?post_id=' + postId,
success: function( response ) {
$commentArea.removeClass( 'wpdc-comments-loading' );
$commentArea.addClass( 'wpdc-comments-loaded' );
$commentArea.html( response );
}
}
);
}
);
})( jQuery );

View file

@ -45,6 +45,7 @@ class DiscourseComment {
add_filter( 'comments_template', array( $this, 'comments_template' ), 20, 1 );
add_filter( 'wp_kses_allowed_html', array( $this, 'extend_allowed_html' ), 10, 2 );
add_action( 'wp_enqueue_scripts', array( $this, 'discourse_comments_js' ) );
add_action( 'rest_api_init', array( $this, 'initialize_comment_route' ) );
}
/**
@ -105,12 +106,65 @@ class DiscourseComment {
}
}
if ( ! empty( $this->options['ajax-load'] ) ) {
wp_register_script( 'load_comments_js', plugins_url( '../js/load-comments.js', __FILE__ ), array( 'jquery' ), WPDISCOURSE_VERSION, true );
$data = array(
'commentsURL' => home_url( '/wp-json/wp-discourse/v1/discourse-comments' ),
);
wp_enqueue_script( 'load_comments_js' );
wp_localize_script( 'load_comments_js', 'wpdc', $data );
}
if ( ! empty( $this->options['load-comment-css'] ) ) {
wp_register_style( 'comment_styles', WPDISCOURSE_URL . '/css/comments.css', array(), WPDISCOURSE_VERSION );
wp_enqueue_style( 'comment_styles' );
}
}
/**
* Registers a Rest API route for returning comments at /wp-json/wp-discourse/v1/discourse-comments.
*/
public function initialize_comment_route() {
if ( ! empty( $this->options['ajax-load'] ) ) {
register_rest_route(
'wp-discourse/v1',
'discourse-comments',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_discourse_comments' ),
),
)
);
}
}
/**
* Handles the REST request for Discourse comments.
*
* @param \WP_REST_Request Object $request The WP_REST_Request for Discourse comments.
*
* @return string
*/
public function get_discourse_comments( $request ) {
$post_id = isset( $request['post_id'] ) ? intval( ( $request['post_id'] ) ) : 0;
if ( empty( $post_id ) ) {
return '';
}
$status = get_post_status( $post_id );
$post = get_post( $post_id );
if ( 'publish' !== $status || ! empty( $post->post_password ) ) {
return '';
}
return wp_kses_post( $this->comment_formatter->format( $post_id ) );
}
/**
* Checks if a post is using Discourse comments.
*