| 1 |
<?php |
| 2 |
/** |
| 3 |
* Webhook Settings |
| 4 |
* |
| 5 |
* @package WPDiscourse |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPDiscourse\Admin; |
| 9 |
|
| 10 |
use WPDiscourse\Shared\PluginUtilities; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class WebhookSettings |
| 14 |
*/ |
| 15 |
class WebhookSettings { |
| 16 |
use PluginUtilities; |
| 17 |
|
| 18 |
/** |
| 19 |
* An instance of the FormHelper class. |
| 20 |
* |
| 21 |
* @access protected |
| 22 |
* @var \WPDiscourse\Admin\FormHelper |
| 23 |
*/ |
| 24 |
protected $form_helper; |
| 25 |
|
| 26 |
/** |
| 27 |
* Gives access to the plugin options. |
| 28 |
* |
| 29 |
* @access protected |
| 30 |
* @var mixed|void |
| 31 |
*/ |
| 32 |
protected $options; |
| 33 |
|
| 34 |
/** |
| 35 |
* Whether or not to display the webhook_options fields. |
| 36 |
* |
| 37 |
* @access protected |
| 38 |
* @var bool |
| 39 |
*/ |
| 40 |
protected $display_webhook_options; |
| 41 |
|
| 42 |
/** |
| 43 |
* CommentSettings constructor. |
| 44 |
* |
| 45 |
* @param \WPDiscourse\Admin\FormHelper $form_helper An instance of the FormHelper class. |
| 46 |
*/ |
| 47 |
public function __construct( $form_helper ) { |
| 48 |
$this->form_helper = $form_helper; |
| 49 |
|
| 50 |
add_action( 'admin_init', array( $this, 'register_webhook_settings' ) ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Add settings section, settings fields, and register the setting. |
| 55 |
*/ |
| 56 |
public function register_webhook_settings() { |
| 57 |
$this->options = $this->get_options(); |
| 58 |
$this->display_webhook_options = ! is_multisite() || empty( $this->options['multisite-configuration-enabled'] ); |
| 59 |
|
| 60 |
add_settings_section( |
| 61 |
'discourse_webhook_settings_section', |
| 62 |
__( 'Webhook Settings', 'wp-discourse' ), |
| 63 |
array( |
| 64 |
$this, |
| 65 |
'webhook_settings_tab_details', |
| 66 |
), |
| 67 |
'discourse_webhook' |
| 68 |
); |
| 69 |
|
| 70 |
if ( $this->display_webhook_options ) { |
| 71 |
add_settings_field( |
| 72 |
'discourse_use_discourse_webhook', |
| 73 |
__( 'Sync Comment Data', 'wp-discourse' ), |
| 74 |
array( |
| 75 |
$this, |
| 76 |
'use_discourse_webhook_checkbox', |
| 77 |
), |
| 78 |
'discourse_webhook', |
| 79 |
'discourse_webhook_settings_section' |
| 80 |
); |
| 81 |
|
| 82 |
add_settings_field( |
| 83 |
'discourse_use_discourse_user_webhook', |
| 84 |
__( 'Update Userdata', 'wp-discourse' ), |
| 85 |
array( |
| 86 |
$this, |
| 87 |
'use_discourse_user_webhook_checkbox', |
| 88 |
), |
| 89 |
'discourse_webhook', |
| 90 |
'discourse_webhook_settings_section' |
| 91 |
); |
| 92 |
|
| 93 |
add_settings_field( |
| 94 |
'discourse_webhook_match_user_email', |
| 95 |
__( 'Match Users by Email', 'wp-discourse' ), |
| 96 |
array( |
| 97 |
$this, |
| 98 |
'webhook_match_user_email_checkbox', |
| 99 |
), |
| 100 |
'discourse_webhook', |
| 101 |
'discourse_webhook_settings_section' |
| 102 |
); |
| 103 |
|
| 104 |
add_settings_field( |
| 105 |
'discourse_webhook_secret', |
| 106 |
__( 'Webhook Secret Key', 'wp-discourse' ), |
| 107 |
array( |
| 108 |
$this, |
| 109 |
'webhook_secret_input', |
| 110 |
), |
| 111 |
'discourse_webhook', |
| 112 |
'discourse_webhook_settings_section' |
| 113 |
); |
| 114 |
|
| 115 |
add_settings_field( |
| 116 |
'discourse_verbose_webhook_logs', |
| 117 |
__( 'Verbose Webhook Logs', 'wp-discourse' ), |
| 118 |
array( |
| 119 |
$this, |
| 120 |
'verbose_webhook_logs', |
| 121 |
), |
| 122 |
'discourse_webhook', |
| 123 |
'discourse_webhook_settings_section' |
| 124 |
); |
| 125 |
|
| 126 |
}// End if(). |
| 127 |
|
| 128 |
register_setting( |
| 129 |
'discourse_webhook', |
| 130 |
'discourse_webhook', |
| 131 |
array( |
| 132 |
$this->form_helper, |
| 133 |
'validate_options', |
| 134 |
) |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Outputs markup for use-discourse-webhook checkbox. |
| 140 |
*/ |
| 141 |
public function use_discourse_webhook_checkbox() { |
| 142 |
$blog_id = is_multisite() ? get_current_blog_id() : null; |
| 143 |
$webhook_payload_url = get_rest_url( $blog_id, '/wp-discourse/v1/update-topic-content' ); |
| 144 |
if ( ! empty( $this->options['url'] ) ) { |
| 145 |
|
| 146 |
$discourse_webhooks_url = '<a href="' . esc_url( $this->options['url'] ) . '/admin/api/web_hooks" target="_blank" rel="noreferrer noopener">' . |
| 147 |
esc_url( $this->options['url'] ) . '/admin/api/web_hooks</a>'; |
| 148 |
} else { |
| 149 |
$discourse_webhooks_url = 'http://forum.example.com/admin/api/web_hooks'; |
| 150 |
} |
| 151 |
|
| 152 |
$description = sprintf( |
| 153 |
// translators: Discourse webhook description. Placeholder: discourse_webhook_url, webhook_payload_url. |
| 154 |
__( |
| 155 |
'<strong>URL:</strong><code>%2$s</code> |
| 156 |
<strong>Events:</strong> "Post is created", "Post is updated".', |
| 157 |
'wp-discourse' |
| 158 |
), |
| 159 |
$discourse_webhooks_url, |
| 160 |
$webhook_payload_url |
| 161 |
); |
| 162 |
|
| 163 |
$this->form_helper->checkbox_input( |
| 164 |
'use-discourse-webhook', |
| 165 |
'discourse_webhook', |
| 166 |
__( |
| 167 |
'Enable the Sync Comment Data webhook endpoint.', |
| 168 |
'wp-discourse' |
| 169 |
), |
| 170 |
$description |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Outputs markup for use-discourse-user-webhook checkbox. |
| 176 |
*/ |
| 177 |
public function use_discourse_user_webhook_checkbox() { |
| 178 |
$webhook_payload_url = home_url( '/wp-json/wp-discourse/v1/update-user' ); |
| 179 |
if ( ! empty( $this->options['url'] ) ) { |
| 180 |
$discourse_webhooks_url = '<a href="' . esc_url( $this->options['url'] ) . '/admin/api/web_hooks" target="_blank" rel="noreferrer noopener">' . |
| 181 |
esc_url( $this->options['url'] ) . '/admin/api/web_hooks</a>'; |
| 182 |
} else { |
| 183 |
$discourse_webhooks_url = 'http://forum.example.com/admin/api/web_hooks'; |
| 184 |
} |
| 185 |
|
| 186 |
$description = sprintf( |
| 187 |
// translators: Discourse webhook description. Placeholder: discourse_webhook_url, webhook_payload_url. |
| 188 |
__( |
| 189 |
'<strong>URL:</strong><code>%2$s</code> |
| 190 |
<strong>Events:</strong> "User is created", "User is Updated".', |
| 191 |
'wp-discourse' |
| 192 |
), |
| 193 |
$discourse_webhooks_url, |
| 194 |
$webhook_payload_url |
| 195 |
); |
| 196 |
|
| 197 |
$this->form_helper->checkbox_input( |
| 198 |
'use-discourse-user-webhook', |
| 199 |
'discourse_webhook', |
| 200 |
__( |
| 201 |
'Enable the Update Userdata webhook endpoint.', |
| 202 |
'wp-discourse' |
| 203 |
), |
| 204 |
$description |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Outputs markup for webhook-match-user-email checkbox. |
| 210 |
*/ |
| 211 |
public function webhook_match_user_email_checkbox() { |
| 212 |
$this->form_helper->checkbox_input( |
| 213 |
'webhook-match-user-email', |
| 214 |
'discourse_webhook', |
| 215 |
__( |
| 216 |
'Match WordPress users with Discourse users by email.', |
| 217 |
'wp-discourse' |
| 218 |
), |
| 219 |
__( |
| 220 |
'Update Userdata will attempt to match users by email if other methods of matching have not worked.', |
| 221 |
'wp-discourse' |
| 222 |
) |
| 223 |
); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Outputs markup for webhook-secret input. |
| 228 |
*/ |
| 229 |
public function webhook_secret_input() { |
| 230 |
$description = __( |
| 231 |
'String of text at least 12 characters long.', |
| 232 |
'wp-discourse' |
| 233 |
); |
| 234 |
$this->form_helper->input( 'webhook-secret', 'discourse_webhook', $description ); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Outputs markup for the discourse_verbose_webhook_logs checkbox. |
| 239 |
*/ |
| 240 |
public function verbose_webhook_logs() { |
| 241 |
$this->form_helper->checkbox_input( |
| 242 |
'verbose-webhook-logs', |
| 243 |
'discourse_webhook', |
| 244 |
__( |
| 245 |
'Enable verbose logs for webhooks.', |
| 246 |
'wp-discourse' |
| 247 |
), |
| 248 |
__( 'Will log successful syncs as well as errors.', 'wp-discourse' ) . ' View logs in the <a href="?page=wp_discourse_options&tab=log_viewer">' . __( 'Log Viewer', 'wp-discourse' ) . '</a>.' |
| 249 |
); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Details for the 'webhook_options' tab. |
| 254 |
*/ |
| 255 |
public function webhook_settings_tab_details() { |
| 256 |
$setup_howto_url = 'https://meta.discourse.org/t/311552'; |
| 257 |
?> |
| 258 |
<p class="wpdc-options-documentation"> |
| 259 |
<em> |
| 260 |
<?php |
| 261 |
esc_html_e( |
| 262 |
'This section is for configuring Discourse webhooks.', |
| 263 |
'wp-discourse' |
| 264 |
); |
| 265 |
?> |
| 266 |
</em> |
| 267 |
</p> |
| 268 |
<?php if ( $this->display_webhook_options ) : ?> |
| 269 |
<p class="wpdc-options-documentation"> |
| 270 |
<em> |
| 271 |
<?php esc_html_e( 'For instructions on setting up webhooks see ', 'wp-discourse' ); ?> |
| 272 |
<a href="<?php echo esc_url( $setup_howto_url ); ?>" |
| 273 |
target="_blank" rel="noreferrer noopener"><?php esc_html_e( 'Configure WP Discourse webhooks.', 'wp-discourse' ); ?></a> |
| 274 |
</em> |
| 275 |
</p> |
| 276 |
<?php else : ?> |
| 277 |
<p class="wpdc-options-documentation wpdc-subsite-documentation"> |
| 278 |
<em> |
| 279 |
<strong> |
| 280 |
<?php |
| 281 |
esc_html_e( |
| 282 |
"You are using the WP Discourse plugin in a subsite of a multisite installation. |
| 283 |
The plugin's webhook configuration is being managed through the installation's main site.", |
| 284 |
'wp-discourse' |
| 285 |
); |
| 286 |
?> |
| 287 |
</strong> |
| 288 |
</em> |
| 289 |
</p> |
| 290 |
<?php endif; ?> |
| 291 |
<?php |
| 292 |
} |
| 293 |
} |
| 294 |
|