checkbox_input( 'multisite-configuration-enabled', __( 'Configure the plugin for a WordPress multisite setup.', 'wp-discourse' ), __( "Enabling this setting will create a wpdc_topic_blog database table so that Discourse topics can be associated with the blog they were posted from. This makes it possible to sync topic data between Discourse and WordPress with a single webhook. When Multisite Configuration is enabled, the settings on this page will be used to configure their related settings on your network's subsites.", 'wp-discourse' ) ); $this->next_setting_heading( __( 'Connection Settings', 'wp-discourse' ) ); } /** * *************************** * * Connection Settings Fields. *****************************/ /** * Outputs markup for the url input. */ public function url_input() { $this->input( 'url', __( 'The base URL of your forum, for example http://discourse.example.com', 'wp-discourse' ), 'url' ); } /** * Outputs markup for the api-key input. */ public function api_key_input() { $url = $this->get_site_option( 'url' ); if ( $url ) { $this->input( 'api-key', __( 'Found on your forum at ', 'wp-discourse' ) . '' . esc_url( $url ) . '/admin/api/keys. ' . "If you haven't yet created an API key, Click 'New API Key', set User Level to 'Single User', set 'User' to an admin account, select 'Global Key' and click 'Save'. Copy and paste the API key here.", 'wp-discourse' ); } else { $this->input( 'api-key', __( "Found on your forum at /admin/api/keys. If you haven't yet created an API key, Click 'New API Key', set User Level to 'Single User', set 'User' to an admin account, select 'Global Key' and click 'Save'. Copy and paste the API key here.", 'wp-discourse' ) ); } } /** * Outputs markup for the publish-username input. */ public function publish_username_input() { $this->input( 'publish-username', __( 'The default Discourse username under which WordPress posts will be published on your forum. The Publishing Username is also used for making API calls to Discourse. It must be set to a Discourse admin username.', 'wp-discourse' ), null, null, null, 'system' ); } /** * Outputs markup for the discourse_connection_logs checkbox. */ public function connection_logs() { $this->checkbox_input( 'connection-logs', __( 'Enable connection logs.', 'wp-discourse' ), __( 'Log attempts to check the connection with Discourse.', 'wp-discourse' ) ); $this->next_setting_heading( __( 'Webhook Settings', 'wp-discourse' ) ); } /** * ************************ * * Webhook Settings Fields. **************************/ /** * Outputs markup for use-discourse-webhook checkbox. */ public function use_discourse_webhook_checkbox() { $webhook_payload_url = network_site_url( '/wp-json/wp-discourse/v1/update-topic-content' ); $discourse_url = $this->get_site_option( 'url' ); if ( ! empty( $discourse_url ) ) { $discourse_webhooks_url = '' . esc_url( $discourse_url ) . '/admin/api/web_hooks'; } else { $discourse_webhooks_url = 'http://forum.example.com/admin/api/web_hooks'; } $description = sprintf( // translators: Discourse webhook description. Placeholder: discourse_webhook_url, webhook_payload_url. __( 'URL:%2$s Events: "Post is created", "Post is updated".', 'wp-discourse' ), $discourse_webhooks_url, $webhook_payload_url ); $this->checkbox_input( 'use-discourse-webhook', __( 'Enable the Sync Comment Data webhook endpoint.', 'wp-discourse' ), $description ); } /** * Outputs markup for use-discourse-user-webhook checkbox. */ public function use_discourse_user_webhook_checkbox() { $webhook_payload_url = network_site_url( '/wp-json/wp-discourse/v1/update-user' ); $url = $this->get_site_option( 'url' ); if ( ! empty( $url ) ) { $discourse_webhooks_url = '' . esc_url( $url ) . '/admin/api/web_hooks'; } else { $discourse_webhooks_url = 'http://forum.example.com/admin/api/web_hooks'; } $description = sprintf( // translators: Discourse webhook description. Placeholder: discourse_webhook_url, webhook_payload_url. __( 'URL:%2$s Events: "User is created", "User is Updated".', 'wp-discourse' ), $discourse_webhooks_url, $webhook_payload_url ); $this->checkbox_input( 'use-discourse-user-webhook', __( 'Enable the Update Userdata webhook endpoint.', 'wp-discourse' ), $description ); } /** * Outputs markup for the webhook-match-user-email-checkbox. */ public function webhook_match_user_email_checkbox() { $this->checkbox_input( 'webhook-match-user-email', __( 'Match WordPress users with Discourse users by email.', 'wp-discourse' ), __( 'Update Userdata will attempt to match users by email if other methods of matching have not worked.', 'wp-discourse' ) ); } /** * Outputs markup for webhook-secret input. */ public function webhook_secret_input() { $description = __( 'String of text at least 12 characters long.', 'wp-discourse' ); $this->input( 'webhook-secret', $description ); } /** * Outputs markup for the verbose-webhook-logs checkbox. */ public function verbose_webhook_logs() { $this->checkbox_input( 'verbose-webhook-logs', __( 'Enable verbose logs for webhooks.', 'wp-discourse' ), __( 'Will log successful syncs as well as errors.', 'wp-discourse' ) ); $this->next_setting_heading( __( 'Publishing Settings', 'wp-discourse' ) ); } /** * *********************** * * Publish Settings Fields. **************************/ /** * Outputs markup for hide-discourse-name-field checkbox. */ public function hide_discourse_name_field_checkbox() { $this->checkbox_input( 'hide-discourse-name-field', __( 'Removes the Discourse Username field from the WordPress user profile page.', 'wp-discourse' ), __( 'If you enable this setting and also enable the Update Userdata webhook, new users created on Discourse will have the their Discourse Username automatically filled in and be uneditable on WordPress.', 'wp-discourse' ) ); } /** * Outputs markup for the verbose-publication-logs checkbox. */ public function verbose_publication_logs() { $this->checkbox_input( 'verbose-publication-logs', __( 'Enable verbose logs for publication.', 'wp-discourse' ), __( 'Will log successful publications as well as errors.', 'wp-discourse' ) ); $this->next_setting_heading( __( 'Comment Settings', 'wp-discourse' ) ); } /** * *********************** * * Comment Settings Fields. **************************/ /** * Outputs markup for the verbose-comment-logs checkbox. */ public function verbose_comment_logs() { $this->checkbox_input( 'verbose-comment-logs', __( 'Enable verbose logs for comments.', 'wp-discourse' ), __( 'Will log successful operations as well as errors.', 'wp-discourse' ) ); $this->next_setting_heading( __( 'DiscourseConnect Settings', 'wp-discourse' ) ); } /** * ******************* * * DiscourseConnect Settings Fields. **********************/ /** * Outputs markup for the enable-sso checkbox. */ public function enable_sso_provider_checkbox() { $sso_documentation_url = get_admin_url( BLOG_ID_CURRENT_SITE, '/admin.php?page=wp_discourse_options&tab=sso_provider&parent_tab=sso_options' ); $sso_documentation_link = '' . __( 'DiscourseConnect Provider tab', 'wp-discourse' ) . ''; $description = __( 'Use this WordPress instance as the DiscourseConnect provider for your Discourse forum.', 'wp-discourse' ); $details = sprintf( // translators: enable_sso_provider input. Placeholder: sso_documentation_link. __( 'For details about using WordPress as the DiscourseConnect Provider, please visit the %1s of the main site in your network.', 'wp-discourse' ), $sso_documentation_link ); $this->checkbox_input( 'enable-sso', $description, $details ); } /** * Outputs markup for sso-client-enabled checkbox. */ public function enable_sso_client_checkbox() { $sso_documentation_url = get_admin_url( BLOG_ID_CURRENT_SITE, '/admin.php?page=wp_discourse_options&tab=sso_client&parent_tab=sso_options' ); $sso_documentation_link = '' . __( 'DiscourseConnect Client tab', 'wp-discourse' ) . ''; $description = __( 'Allow your WordPress site to function as a DiscourseConnect client to Discourse.', 'wp-discourse' ); $details = sprintf( // translators: enable_sso_client checkbox. Placeholder: sso_documentation_link. __( 'For details about using WordPress as a DiscourseConnect Client for Discourse, please visit the %1s of the main site in your network.', 'wp-discourse' ), $sso_documentation_link ); $this->checkbox_input( 'sso-client-enabled', $description, $details ); } /** * Outputs markup for the sso-secret input. */ public function sso_secret_input() { $url = $this->get_site_option( 'url' ); if ( ! empty( $url ) ) { $discourse_sso_url = '' . esc_url( $url ) . '/admin/site_settings/category/all_results?filter=sso'; } else { $discourse_sso_url = 'http://forum.example.com/admin/site_settings/category/all_results?filter=sso'; } $description = sprintf( // translators: SSO secret input. Placeholder: discourse_sso_url. __( 'The secret key used to verify Discourse DiscourseConnect requests. Set it to a string of text, at least 10 characters long. It needs to match the key set at %1$s.', 'wp-discourse' ), $discourse_sso_url ); $this->input( 'sso-secret', $description ); } /** * Outputs markup for the verbose-sso-logs checkbox. */ public function verbose_sso_logs() { $this->checkbox_input( 'verbose-sso-logs', __( 'Enable verbose logs for DiscourseConnect.', 'wp-discourse' ), __( 'Will log successful operations as well as errors.', 'wp-discourse' ) ); } /** * Creates the network options page. */ public function network_options_page() { if ( ! current_user_can( 'manage_network_options' ) ) { exit; } $action_url = add_query_arg( 'action', 'discourse_network_options', network_admin_url( 'edit.php' ) ) ?>

validate_site_options( $site_options ); } wp_redirect( add_query_arg( array( 'page' => 'discourse_network_options', 'updated' => 'true', ), network_admin_url( 'admin.php' ) ) ); exit(); } /** * A callback function used for sanitizing array values. * * @param string $item The item to be sanitized. * * @return array|string */ protected function sanitize_item( $item ) { return esc_attr( $item ); } /** * Adds documentation to the top of the settings page. */ public function network_settings_details() { ?>


$value ) { $filter = 'wpdc_validate_site_' . str_replace( '-', '_', $key ); $value = apply_filters( $filter, $value ); $updated_options[ $key ] = $value; if ( 'multisite-configuration-enabled' === $key ) { update_site_option( 'wpdc_multisite_configuration', $value ); } } update_site_option( 'wpdc_site_options', $updated_options ); } /** * Sets success and error notices. * * This is an awkward way to do it, but I'm not finding a better option. */ public function network_config_notices() { $screen = get_current_screen(); $discourse_screen = ! empty( $screen->parent_base ) && 'discourse_network_options' === $screen->parent_base; $multisite_configuration_enabled = $this->get_site_option( 'multisite-configuration-enabled' ); if ( $discourse_screen && $multisite_configuration_enabled ) { $notices = ''; $url = $this->get_site_option( 'url' ); $api_key = $this->get_site_option( 'api-key' ); $publish_username = $this->get_site_option( 'publish-username' ); $use_discourse_webhook = $this->get_site_option( 'use-discourse-webhook' ); $use_discourse_user_webhook = $this->get_site_option( 'use-discourse-user-webhook' ); $webhook_secret = $this->get_site_option( 'webhook-secret' ); $sso_secret = $this->get_site_option( 'sso-secret' ); $enable_sso = $this->get_site_option( 'enable-sso' ); $sso_client_enabled = $this->get_site_option( 'sso-client-enabled' ); if ( ! ( $url && $api_key && $publish_username ) ) { $notices .= '

' . __( 'To connect with Discourse, you need to supply the Discourse URL, API Key, and Publishing Username.', 'wp-discourse' ) . '

'; } elseif ( empty( $this->check_connection_status() ) ) { $notices .= '

' . __( 'You are not connected to Discourse. Check that your connection settings are correct. If the issue persists, enable connection logs and check Logs.', 'wp-discourse' ) . '

'; } else { $notices .= '

' . __( 'You are connected to Discourse!', 'wp-discourse' ) . '

'; } if ( ( ! empty( $use_discourse_webhook ) && empty( $webhook_secret ) ) || ( ! empty( $use_discourse_user_webhook ) && empty( $webhook_secret ) ) ) { $notices .= '

' . __( 'To use Discourse webhooks, you need to supply a webhook secret key.', 'wp-discourse' ) . '

'; } if ( ! empty( $webhook_secret ) && strlen( $webhook_secret ) < 12 ) { $notices .= '

' . __( 'The Webhook Secret Key must be at least 12 characters long.', 'wp-discourse' ) . '

'; } if ( ! empty( $enable_sso ) && empty( $sso_secret ) ) { $notices .= '

' . __( 'To use WordPress as the DiscourseConnect Provider, you need to supply a DiscourseConnect Secret Key.', 'wp-discourse' ) . '

'; } if ( ! empty( $sso_client_enabled ) && empty( $sso_secret ) ) { $notices .= '

' . __( 'To use WordPress as the DiscourseConnect Client, you need to supply a DiscourseConnect Secret Key.', 'wp-discourse' ) . '

'; } if ( ! empty( $enable_sso ) && ! empty( $sso_client_enabled ) ) { $notices .= '

' . __( "You can't enable both the DiscourseConnect Client and DiscourseConnect Provider functionality.", 'wp-discourse' ) . '

'; } if ( ! empty( $sso_secret ) && strlen( $sso_secret ) < 10 ) { $notices .= '

' . __( 'The DiscourseConnect Secret Key must be at least 10 characters long.', 'wp-discourse' ) . '

'; } }// End if(). if ( ! empty( $notices ) ) { echo wp_kses_post( $notices ); } } /** * Returns a single option from the wpdc_site_options array. * * @param string $key The option to find. * * @return bool|mixed */ protected function get_site_option( $key ) { static $site_options = array(); if ( empty( $site_options ) ) { $site_options = get_site_option( 'wpdc_site_options' ); } $option = ! empty( $site_options[ $key ] ) ? $site_options[ $key ] : false; return $option; } /** * Outputs the markup for an input box, defaults to outputting a text input, but * can be used for other types. * * @param string $option The name of the option. * @param string $description The description of the settings field. * @param null|string $type The type of input ('number', 'url', etc). * @param null|int $min The min value (applied to number inputs). * @param null|int $max The max value (applies to number inputs). * @param null|int|string $default An optional default value. */ protected function input( $option, $description, $type = null, $min = null, $max = null, $default = null ) { $value = $this->get_site_option( $option ); $value = empty( $value ) && $default ? $default : $value; $allowed = array( 'a' => array( 'href' => array(), 'target' => array(), ), ); ?> ' type="" value='' class="regular-text ltr"/>

get_site_option( $option ); $allowed = array( 'a' => array( 'href' => array(), 'target' => array(), ), 'strong' => array(), 'code' => array(), ); $checked = ! empty( $value ) ? 'checked="checked"' : ''; ?>