| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add admin notices. |
| 4 |
* |
| 5 |
* @package WPDiscourse; |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPDiscourse\Admin; |
| 9 |
|
| 10 |
use WPDiscourse\Shared\PluginUtilities; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class AdminNotice |
| 14 |
*/ |
| 15 |
class AdminNotice { |
| 16 |
use PluginUtilities; |
| 17 |
|
| 18 |
/** |
| 19 |
* Gives access to the plugin options. |
| 20 |
* |
| 21 |
* @access protected |
| 22 |
* @var mixed|void |
| 23 |
*/ |
| 24 |
protected $options; |
| 25 |
|
| 26 |
/** |
| 27 |
* AdminNotice constructor. |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
add_action( 'admin_notices', array( $this, 'set_admin_notices' ) ); |
| 31 |
add_action( 'admin_init', array( $this, 'setup_options' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Sets the plugin options. |
| 36 |
*/ |
| 37 |
public function setup_options() { |
| 38 |
$this->options = $this->get_options(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Set admin notices. |
| 43 |
* |
| 44 |
* @return null |
| 45 |
*/ |
| 46 |
public function set_admin_notices() { |
| 47 |
global $pagenow, $post; |
| 48 |
$current_screen = get_current_screen(); |
| 49 |
|
| 50 |
// Admin notices aren't supported by the block editor. For now, disable admin notices for versions >= 5.0. |
| 51 |
if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) ) { |
| 52 |
|
| 53 |
return null; |
| 54 |
} |
| 55 |
|
| 56 |
// Post edit screen notices. |
| 57 |
if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) { |
| 58 |
$allowed_post_types = ! empty( $this->options['allowed_post_types'] ) ? $this->options['allowed_post_types'] : array(); |
| 59 |
|
| 60 |
if ( in_array( $post->post_type, $allowed_post_types, true ) ) { |
| 61 |
$post_id = $post->ID; |
| 62 |
|
| 63 |
$discourse_publishing_response = get_post_meta( $post_id, 'wpdc_publishing_response', true ); |
| 64 |
if ( ! empty( $discourse_publishing_response ) ) { |
| 65 |
|
| 66 |
if ( 'error' === $discourse_publishing_response ) { |
| 67 |
$error_message = __( '<div class="notice notice-error is-dismissible"><p>There has been an error publishing this post to Discourse.</p></div>', 'wp-discourse' ); |
| 68 |
|
| 69 |
echo wp_kses_post( $error_message ); |
| 70 |
|
| 71 |
delete_post_meta( $post_id, 'wpdc_publishing_response' ); |
| 72 |
} |
| 73 |
|
| 74 |
if ( 'success' === $discourse_publishing_response ) { |
| 75 |
$discourse_permalink = get_post_meta( $post_id, 'discourse_permalink', true ); |
| 76 |
$discourse_link = '<a href="' . esc_url( $discourse_permalink ) . '" target="_blank" rel="noreferrer noopener">' . __( 'View post', 'wp-discourse' ) . '</a>'; |
| 77 |
|
| 78 |
$success_message = sprintf( |
| 79 |
// translators: Discourse post-published success message. Placeholder: discourse_permalink. |
| 80 |
__( '<div class="notice notice-success is-dismissible"><p>Your post has been published to Discourse. %1$s on Discourse.</p></div>', 'wp-discourse' ), |
| 81 |
$discourse_link |
| 82 |
); |
| 83 |
|
| 84 |
delete_post_meta( $post_id, 'wpdc_publishing_response' ); |
| 85 |
|
| 86 |
echo wp_kses_post( $success_message ); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
$discourse_linking_response = get_post_meta( $post_id, 'wpdc_linking_response', true ); |
| 91 |
if ( 'error' === $discourse_linking_response ) { |
| 92 |
$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' ); |
| 93 |
|
| 94 |
delete_post_meta( $post_id, 'wpdc_linking_response' ); |
| 95 |
echo wp_kses_post( $error_message ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( 'invalid_url' === $discourse_linking_response ) { |
| 99 |
$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' ); |
| 100 |
|
| 101 |
delete_post_meta( $post_id, 'wpdc_linking_response' ); |
| 102 |
echo wp_kses_post( $error_message ); |
| 103 |
} |
| 104 |
|
| 105 |
$discourse_username = get_user_meta( get_current_user_id(), 'discourse_username', true ); |
| 106 |
$current_username = wp_get_current_user()->user_login; |
| 107 |
$publish_username = ! empty( $this->options['publish-username'] ) ? $this->options['publish-username'] : ''; |
| 108 |
$use_discourse_name_field = empty( $this->options['hide-discourse-name-field'] ); |
| 109 |
|
| 110 |
$profile_page_link = '<a href="' . esc_url( admin_url( '/profile.php' ) ) . '">' . __( 'profile page', 'wp-discourse' ) . '</a>'; |
| 111 |
|
| 112 |
if ( empty( $discourse_username ) |
| 113 |
&& $use_discourse_name_field |
| 114 |
&& $current_username !== $publish_username |
| 115 |
) { |
| 116 |
$username_not_set_notice = sprintf( |
| 117 |
// translators: Discourse username_not_set notice. Placeholder: discourse_username. |
| 118 |
__( '<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' ), |
| 119 |
$publish_username, |
| 120 |
$profile_page_link |
| 121 |
); |
| 122 |
|
| 123 |
echo wp_kses_post( $username_not_set_notice ); |
| 124 |
} |
| 125 |
}// End if(). |
| 126 |
}// End if(). |
| 127 |
|
| 128 |
return null; |
| 129 |
} |
| 130 |
} |
| 131 |
|