2
0
Fork 0
mirror of https://github.com/discourse/wp-discourse.git synced 2025-10-03 08:59:21 +08:00
wp-discourse/lib/discourse.php

315 lines
8.9 KiB
PHP
Raw Normal View History

2015-01-05 10:37:43 -06:00
<?php
/**
2016-06-11 19:01:56 -07:00
* Sets up the plugin.
2016-06-12 11:12:56 -07:00
*
2016-06-11 19:01:56 -07:00
* @package WPDiscourse
2015-01-05 10:37:43 -06:00
*/
2016-06-11 19:01:56 -07:00
namespace WPDiscourse\Discourse;
2016-06-06 10:55:37 -07:00
2018-03-01 15:02:24 -08:00
use WPDiscourse\Shared\PluginUtilities;
2016-06-11 19:01:56 -07:00
/**
* Class Discourse
*/
class Discourse {
2018-03-01 15:02:24 -08:00
use PluginUtilities;
2016-06-06 10:55:37 -07:00
/**
* Gives access to the plugin options.
*
* @access protected
* @var mixed|void
*/
protected $options;
2016-09-20 18:05:40 -07:00
/**
* The connection options array.
2016-09-20 21:12:46 -07:00
*
2017-03-22 18:27:16 -07:00
* @access protected
2016-09-20 18:05:40 -07:00
* @var array
*/
protected $discourse_connect = array(
2016-09-23 17:21:08 -07:00
'url' => '',
'api-key' => '',
2016-09-09 00:44:22 -07:00
'publish-username' => 'system',
);
2016-09-20 18:05:40 -07:00
/**
* The publishing options array.
*
2017-03-22 18:27:16 -07:00
* @access protected
2016-09-20 18:05:40 -07:00
* @var array
*/
protected $discourse_publish = array(
'display-subcategories' => 0,
'publish-category' => '',
'publish-category-update' => 0,
'full-post-content' => 0,
'allow-tags' => 0,
'max-tags' => 5,
'publish-as-unlisted' => 0,
'custom-excerpt-length' => 55,
'add-featured-link' => 0,
'auto-publish' => 0,
'force-publish' => 0,
'force-publish-max-age' => 0,
'publish-failure-notice' => 0,
'publish-failure-email' => '',
'auto-track' => 1,
'allowed_post_types' => array( 'post' ),
'hide-discourse-name-field' => 0,
'discourse-username-editable' => 0,
2016-09-09 00:44:22 -07:00
);
2016-09-20 18:05:40 -07:00
/**
* The commenting options array.
*
2017-03-22 18:27:16 -07:00
* @access protected
2016-09-20 18:05:40 -07:00
* @var array
*/
protected $discourse_comment = array(
2018-10-12 14:33:56 -07:00
'enable-discourse-comments' => 0,
'comment-type' => 'display-comments',
2017-11-28 13:38:43 -08:00
'ajax-load' => 0,
'cache-html' => 0,
2018-07-24 17:10:05 -07:00
'clear-cached-comment-html' => 0,
2017-10-13 14:36:53 -07:00
'discourse-new-tab' => 0,
'comment-sync-period' => 10,
'hide-wordpress-comments' => 0,
2016-09-09 00:44:22 -07:00
'show-existing-comments' => 0,
2016-09-25 18:51:06 -07:00
'existing-comments-heading' => '',
'max-comments' => 5,
2016-09-09 00:44:22 -07:00
'min-replies' => 1,
2016-09-25 18:51:06 -07:00
'min-score' => 0,
2016-09-09 00:44:22 -07:00
'min-trust-level' => 1,
'bypass-trust-level-score' => 50,
'custom-datetime-format' => '',
2016-09-09 00:44:22 -07:00
'only-show-moderator-liked' => 0,
'load-comment-css' => 0,
2016-09-09 00:44:22 -07:00
);
2016-09-20 18:05:40 -07:00
/**
* The configurable text options array.
*
2017-03-22 18:27:16 -07:00
* @access protected
2016-09-20 18:05:40 -07:00
* @var array
*/
2016-09-19 20:19:36 -07:00
protected $discourse_configurable_text = array(
'discourse-link-text' => '',
2016-09-23 17:21:08 -07:00
'start-discussion-text' => 'Start the discussion at',
2018-04-18 12:57:38 -07:00
'continue-discussion-text' => 'Continue the discussion at',
'join-discussion-text' => 'Join the discussion at',
2018-04-18 17:09:10 -07:00
'comments-singular-text' => 'Comment',
'comments-plural-text' => 'Comments',
'no-comments-text' => 'Join the Discussion',
2016-09-25 18:51:06 -07:00
'notable-replies-text' => 'Notable Replies',
2016-09-20 11:33:53 -07:00
'comments-not-available-text' => 'Comments are not currently available for this post.',
2016-09-23 17:21:08 -07:00
'participants-text' => 'Participants',
'published-at-text' => 'Originally published at:',
'single-reply-text' => 'Reply',
'many-replies-text' => 'Replies',
'more-replies-more-text' => 'more',
'external-login-text' => 'Log in with Discourse',
'link-to-discourse-text' => 'Link your account to Discourse',
'linked-to-discourse-text' => 'Your account is linked with Discourse!',
2016-09-19 20:19:36 -07:00
);
2017-06-26 20:57:39 -07:00
/**
* The webhook options array.
*
* @access protected
* @var array
*/
protected $discourse_webhook = array(
'use-discourse-webhook' => 0,
2017-07-09 16:02:22 -07:00
'webhook-secret' => '',
'webhook-match-old-topics' => 0,
2017-07-05 11:39:10 -07:00
'use-discourse-user-webhook' => 0,
2017-07-09 16:02:22 -07:00
'webhook-match-user-email' => 0,
2017-06-26 20:57:39 -07:00
);
2017-03-22 18:27:16 -07:00
/**
* The sso_common options array.
*
* @access protected
* @var array
*/
2017-03-18 00:52:14 -07:00
protected $discourse_sso_common = array(
2017-03-22 00:25:00 -07:00
'sso-secret' => '',
2017-03-18 00:52:14 -07:00
);
2017-03-22 18:27:16 -07:00
/**
* The sso_provider options.
*
* @access protected
* @var array
*/
2017-03-18 00:52:14 -07:00
protected $discourse_sso_provider = array(
2017-11-19 14:35:58 -08:00
'enable-sso' => 0,
'auto-create-sso-user' => 0,
'login-path' => '',
'real-name-as-discourse-name' => 0,
'force-avatar-update' => 0,
'redirect-without-login' => 0,
2017-03-18 00:52:14 -07:00
);
/**
2017-03-22 18:27:16 -07:00
* The sso_client options.
2017-03-18 00:52:14 -07:00
*
* @var array
*/
protected $discourse_sso_client = array(
'sso-client-enabled' => 0,
'sso-client-login-form-change' => 0,
'sso-client-login-form-redirect' => '',
'sso-client-sync-by-email' => 0,
'sso-client-disable-create-user' => 0,
'sso-client-sync-logout' => 0,
2016-09-09 00:44:22 -07:00
);
/**
* The discourse_logs options.
*
* @access protected
* @var array
*/
protected $discourse_logs = array(
'logs-enabled' => 1,
);
2016-09-21 13:55:20 -07:00
/**
* The array of option groups, used for assembling the options into a single array.
*
* @var array
*/
2016-09-21 12:20:53 -07:00
protected $discourse_option_groups = array(
'discourse_connect',
'discourse_publish',
'discourse_comment',
'discourse_configurable_text',
2017-06-26 20:57:39 -07:00
'discourse_webhook',
2017-03-18 00:52:14 -07:00
'discourse_sso_common',
'discourse_sso_provider',
'discourse_sso_client',
'discourse_logs',
2016-09-21 12:20:53 -07:00
);
2016-06-06 10:55:37 -07:00
/**
* Discourse constructor.
*/
2016-06-11 18:23:34 -07:00
public function __construct() {
add_action( 'init', array( $this, 'initialize_plugin' ) );
add_filter( 'allowed_redirect_hosts', array( $this, 'allow_discourse_redirect' ) );
add_filter( 'wp_kses_allowed_html', array( $this, 'allow_time_tag' ) );
add_action( 'admin_head', array( $this, 'wpdc_admin_head' ) );
2016-06-06 10:55:37 -07:00
}
2016-06-12 11:12:56 -07:00
2016-09-19 17:03:28 -07:00
/**
* Initializes the plugin configuration, loads the text domain etc.
*/
public function initialize_plugin() {
2016-09-19 17:03:28 -07:00
load_plugin_textdomain( 'wp-discourse', false, basename( dirname( __FILE__ ) ) . '/languages' );
2018-02-23 14:42:57 -08:00
$this->options = $this->get_options();
2016-09-19 17:03:28 -07:00
// Set the Discourse domain name option.
2017-07-09 16:02:22 -07:00
$discourse_url = ! empty( $this->options['url'] ) ? $this->options['url'] : null;
$domain_name = wp_parse_url( $discourse_url, PHP_URL_HOST );
update_option( 'wpdc_discourse_domain', $domain_name );
update_option( 'discourse_option_groups', $this->discourse_option_groups );
2017-03-25 12:09:18 -07:00
foreach ( $this->discourse_option_groups as $group_name ) {
if ( 'discourse_configurable_text' === $group_name && get_option( 'discourse_configurable_text' ) ) {
$saved_values = get_option( 'discourse_configurable_text' );
2017-03-25 12:09:18 -07:00
$default_values = $this->discourse_configurable_text;
$merged_values = array_merge( $default_values, $saved_values );
array_walk( $merged_values, self::class . '::register_text_translations' );
2017-03-25 12:16:34 -07:00
update_option( $group_name, $merged_values );
2017-03-25 12:09:18 -07:00
} else {
add_option( $group_name, $this->$group_name );
}
}
2018-10-12 14:33:56 -07:00
// Transfer 'use-discourse-comments' and 'add-join-link' options. Plugin version 1.7.7.
$commenting_options = get_option( 'discourse_comment' );
if ( ! empty( $commenting_options['use-discourse-comments'] ) || ! empty( $commenting_options['add-join-link'] ) ) {
$commenting_options['enable-discourse-comments'] = 1;
$commenting_options['comment-type'] = ! empty( $this->options['use-discourse-comments'] ) ? 'display-comments' : 'display-comments-link';
}
unset( $commenting_options['use-discourse-comments'] );
unset( $commenting_options['add-join-link'] );
update_option( 'discourse_comment', $commenting_options );
2017-02-18 15:24:28 -08:00
// Create a backup for the discourse_configurable_text option.
update_option( 'discourse_configurable_text_backup', $this->discourse_configurable_text );
2017-03-25 09:34:39 -07:00
update_option( 'discourse_version', WPDISCOURSE_VERSION );
$this->register_assets();
}
2017-03-16 18:15:54 -07:00
/**
* Adds the Discourse forum domain name to the allowed hosts for wp_safe_redirect().
*
* @param array $hosts The array of allowed hosts.
*
* @return array
*/
public function allow_discourse_redirect( $hosts ) {
2017-04-11 19:49:49 -07:00
$discourse_domain = get_option( 'wpdc_discourse_domain' );
if ( $discourse_domain ) {
$hosts[] = $discourse_domain;
}
return $hosts;
2016-09-23 17:21:08 -07:00
}
2016-09-12 18:31:57 -07:00
/**
* Allow the time tag - used in Discourse comments.
*
* @param array $allowedposttags The array of allowed html tags.
*
* @return array
*/
public function allow_time_tag( $allowedposttags ) {
$allowedposttags['time'] = array(
'datetime' => array(),
);
return $allowedposttags;
}
/**
* Adds WP Discourse tags to the <head> in the admin panel.
*/
public function wpdc_admin_head() {
$this->wpdc_icon();
$this->wpdc_url();
}
/**
* Adds the WP Discourse icon to the <head> for use in the client.
*/
public function wpdc_icon() {
echo '<meta name="wpdc-icon" content="' . esc_html( WPDISCOURSE_LOGO ) . '" />';
}
/**
* Adds the WP Discourse URL to the <head> for use in the client.
*/
public function wpdc_url() {
echo '<meta name="wpdc-url" content="' . esc_html( $this->options['url'] ) . '" />';
}
/**
* Registers assets on initialization
*/
public function register_assets() {
$style_path = '../css/comments.css';
$script_path = '../js/load-comments.js';
wp_register_script( 'load_comments_js', plugins_url( $script_path, __FILE__ ), array( 'jquery' ), filemtime( plugin_dir_path( __FILE__ ) . $script_path ), true );
wp_register_style( 'comment_styles', plugins_url( $style_path, __FILE__ ), array(), filemtime( plugin_dir_path( __FILE__ ) . $style_path ) );
}
2015-01-05 10:37:43 -06:00
}