'array', 'sanitize_callback' => 'aidanto_sanitize_settings', 'show_in_rest' => false, ) ); add_settings_section( 'aidanto_chat', '', '__return_false', AIDANTO_SETTINGS_PAGE ); add_settings_field( 'aidanto_site_id', __( 'Website code or site ID', 'aidanto' ), 'aidanto_render_site_id_field', AIDANTO_SETTINGS_PAGE, 'aidanto_chat', array( 'label_for' => 'aidanto_site_id' ) ); add_settings_field( 'aidanto_enabled', __( 'Chat on this site', 'aidanto' ), 'aidanto_render_enabled_field', AIDANTO_SETTINGS_PAGE, 'aidanto_chat' ); } add_action( 'admin_init', 'aidanto_register_settings' ); function aidanto_add_settings_page() { add_options_page( __( 'Aidanto', 'aidanto' ), __( 'Aidanto', 'aidanto' ), 'manage_options', AIDANTO_SETTINGS_PAGE, 'aidanto_render_settings_page' ); } add_action( 'admin_menu', 'aidanto_add_settings_page' ); function aidanto_action_links( $links ) { $url = admin_url( 'options-general.php?page=' . AIDANTO_SETTINGS_PAGE ); array_unshift( $links, '' . esc_html__( 'Settings', 'aidanto' ) . '' ); return $links; } add_filter( 'plugin_action_links_' . plugin_basename( AIDANTO_PLUGIN_FILE ), 'aidanto_action_links' ); // Accepts a bare site ID or the whole code from Aidanto; only the production chat host is trusted. function aidanto_extract_site_id( $value ) { if ( preg_match( AIDANTO_SITE_ID_PATTERN, $value ) ) { return $value; } $tags = new WP_HTML_Tag_Processor( $value ); if ( $tags->next_tag( 'script' ) ) { if ( AIDANTO_SCRIPT_URL !== $tags->get_attribute( 'src' ) ) { return new WP_Error( 'aidanto_other_address', __( 'This code does not load the Aidanto chat. Copy the code from the “Widget on your website” card in Aidanto and paste it here.', 'aidanto' ) ); } $site_id = $tags->get_attribute( 'data-site-id' ); if ( is_string( $site_id ) && preg_match( AIDANTO_SITE_ID_PATTERN, $site_id ) ) { return $site_id; } } return new WP_Error( 'aidanto_invalid_site_id', __( 'This is not an Aidanto website code or site ID. Copy the code from the “Widget on your website” card in Aidanto and paste it whole, or paste only the site ID that starts with “site_”.', 'aidanto' ) ); } // WordPress runs this twice when the option is first created, so its result must pass through unchanged. function aidanto_sanitize_settings( $input ) { $saved = aidanto_get_settings(); $input = is_array( $input ) ? $input : array(); $value = isset( $input['site_id'] ) && is_string( $input['site_id'] ) ? trim( $input['site_id'] ) : ''; $site_id = '' === $value ? '' : aidanto_extract_site_id( $value ); if ( is_wp_error( $site_id ) ) { add_settings_error( AIDANTO_OPTION, $site_id->get_error_code(), $site_id->get_error_message() ); $site_id = $saved['site_id']; } return array( 'site_id' => $site_id, 'enabled' => ! empty( $input['enabled'] ), ); } function aidanto_site_address() { $parts = wp_parse_url( home_url() ); if ( empty( $parts['scheme'] ) || empty( $parts['host'] ) ) { return ''; } return $parts['scheme'] . '://' . $parts['host'] . ( empty( $parts['port'] ) ? '' : ':' . $parts['port'] ); } function aidanto_render_site_id_field() { $settings = aidanto_get_settings(); ?>

' . esc_html__( 'Start a free 14-day trial', 'aidanto' ) . '' ); ?>

  1. ' . esc_html( $address ) . '' ); ?>