'string', 'sanitize_callback' => array( 'Ascue_Settings', 'sanitize_site_key' ), 'default' => '', 'show_in_rest' => false, ) ); register_setting( self::OPTION_GROUP, Ascue_Settings::OPTION_API_URL, array( 'type' => 'string', 'sanitize_callback' => array( 'Ascue_Settings', 'sanitize_api_url' ), 'default' => Ascue_Settings::DEFAULT_API_URL, 'show_in_rest' => false, ) ); register_setting( self::OPTION_GROUP, Ascue_Settings::OPTION_INTEGRATIONS, array( 'type' => 'array', 'sanitize_callback' => array( 'Ascue_Settings', 'sanitize_integrations' ), 'show_in_rest' => false, ) ); } /** * Adds Settings → Ascue. * * @return void */ public static function add_page() { add_options_page( __( 'Ascue', 'ascue' ), __( 'Ascue', 'ascue' ), 'manage_options', self::PAGE_SLUG, array( __CLASS__, 'render_page' ) ); } /** * Adds a "Settings" link on the Plugins screen. * * @param array $links Existing links. * @return array */ public static function add_action_link( $links ) { $url = admin_url( 'options-general.php?page=' . self::PAGE_SLUG ); array_unshift( $links, '' . esc_html__( 'Settings', 'ascue' ) . '' ); return $links; } /** * Loads the script for the settings page only. * * @param string $hook_suffix Current admin page. * @return void */ public static function enqueue_assets( $hook_suffix ) { if ( 'settings_page_' . self::PAGE_SLUG !== $hook_suffix ) { return; } wp_enqueue_script( 'ascue-admin', ASCUE_PLUGIN_URL . 'assets/admin.js', array(), ASCUE_VERSION, true ); wp_localize_script( 'ascue-admin', 'ascueAdmin', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'action' => self::AJAX_ACTION, 'nonce' => wp_create_nonce( self::AJAX_ACTION ), 'testing' => __( 'Testing…', 'ascue' ), 'failed' => __( 'The test could not run. Reload the page and try again.', 'ascue' ), ) ); } /** * Renders the settings page. * * @return void */ public static function render_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $site_key = Ascue_Settings::get_site_key(); $api_url = Ascue_Settings::get_api_url(); ?>

<?php echo esc_attr( get_admin_page_title() ); ?>

__( 'You are not allowed to do this.', 'ascue' ) ), 403 ); } $site_key = isset( $_POST['siteKey'] ) ? Ascue_Settings::clean_site_key( sanitize_text_field( wp_unslash( $_POST['siteKey'] ) ) ) : ''; if ( '' === $site_key ) { $site_key = Ascue_Settings::get_site_key(); } $api_url = isset( $_POST['apiUrl'] ) ? Ascue_Settings::clean_api_url( sanitize_text_field( wp_unslash( $_POST['apiUrl'] ) ) ) : ''; if ( '' === $api_url ) { $api_url = Ascue_Settings::get_api_url(); } if ( '' === $site_key ) { wp_send_json_error( array( 'message' => __( 'Enter your site key first.', 'ascue' ) ) ); } $result = Ascue_Client::post_message( $api_url, $site_key, array( 'body' => sprintf( /* translators: %s: site name. */ __( 'Test message from the Ascue WordPress plugin on %s. You can ignore it.', 'ascue' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ), 'subject' => __( 'Ascue connection test', 'ascue' ), 'source' => 'plugin', 'deliver' => false, ) ); if ( $result['accepted'] ) { wp_send_json_success( array( 'message' => __( 'Connected. Ascue accepted the test message. Your forms are protected.', 'ascue' ) ) ); } if ( 0 === $result['status'] ) { $message = sprintf( /* translators: 1: API URL, 2: error message. */ __( 'Could not reach Ascue at %1$s (%2$s). Until this works, your forms send their normal email.', 'ascue' ), $api_url, $result['detail'] ); } elseif ( 401 === $result['status'] ) { $message = __( 'Ascue did not recognize this site key. Copy it again from your Ascue dashboard.', 'ascue' ); } else { $message = sprintf( /* translators: %d: HTTP status code. */ __( 'Ascue answered with HTTP %d instead of accepting the message. Until this works, your forms send their normal email.', 'ascue' ), $result['status'] ); if ( '' !== $result['error_message'] ) { $message .= ' ' . $result['error_message']; } } wp_send_json_error( array( 'message' => $message ) ); } }