$m[1], 'secret' => $m[2], ); } /** * Registers the options and their fields. */ function nodium_register_settings() { register_setting( 'nodium', 'nodium_key', array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'nodium_sanitize_key', 'show_in_rest' => false, ) ); register_setting( 'nodium', 'nodium_proxy', array( 'type' => 'string', 'default' => '1', 'sanitize_callback' => 'nodium_sanitize_proxy', 'show_in_rest' => false, ) ); add_settings_section( 'nodium_main', '', '__return_false', 'nodium' ); add_settings_field( 'nodium_key', __( 'Nodium key', 'nodium' ), 'nodium_key_field', 'nodium', 'nodium_main', array( 'label_for' => 'nodium_key' ) ); add_settings_field( 'nodium_proxy', __( 'Ad blockers', 'nodium' ), 'nodium_proxy_field', 'nodium', 'nodium_main' ); } add_action( 'admin_init', 'nodium_register_settings' ); /** * A key that isn't a Nodium key is refused, and the one saved before stays. The field is never filled in with the saved * key, so a blank one keeps it too, unless "Remove the saved key" is ticked. * * @param mixed $value The submitted value. * @return string */ function nodium_sanitize_key( $value ) { $key = is_string( $value ) ? trim( $value ) : ''; if ( '' === $key ) { // options.php has checked the settings form's nonce before this runs. $clear = isset( $_POST['nodium_key_clear'] ) && '1' === sanitize_text_field( wp_unslash( $_POST['nodium_key_clear'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing return $clear ? '' : nodium_key(); } if ( null !== nodium_parse_key( $key ) ) { return $key; } add_settings_error( 'nodium_key', 'nodium_key_invalid', __( 'That isn’t a Nodium key. Copy it from Settings → Site → Proxy in Nodium: it starts with nk_.', 'nodium' ) ); return nodium_key(); } /** * The proxy setting: "1" on, "0" off. * * @param mixed $value The submitted value. * @return string */ function nodium_sanitize_proxy( $value ) { return '1' === $value ? '1' : '0'; } /** * The saved key, or ''. * * @return string */ function nodium_key() { $key = get_option( 'nodium_key', '' ); return is_string( $key ) ? $key : ''; } /** * Whether the plugin serves the script and passes its events on from this site. * * @return bool */ function nodium_proxy_on() { return '1' === get_option( 'nodium_proxy', '1' ); } /** * The key field. */ function nodium_key_field() { // The saved key's secret never goes back into the page: the field stays empty, and the placeholder shows the site key. $saved = nodium_parse_key( nodium_key() ); printf( '', esc_attr( null === $saved ? '' : 'nk_' . $saved['token'] . '.••••••••' ) ); echo '
' . esc_html__( 'From Settings → Site → Proxy in Nodium. With it, Nodium counts visits on every page of this site, without cookies. Without one, nothing is counted.', 'nodium' ); if ( null !== $saved ) { echo ' ' . esc_html__( 'A key is saved; leave this empty to keep it.', 'nodium' ); } echo '
'; if ( null !== $saved ) { printf( '', esc_html__( 'Remove the saved key', 'nodium' ) ); } } /** * The proxy field. */ function nodium_proxy_field() { printf( '', checked( nodium_proxy_on(), true, false ), esc_html__( 'Count visitors whose ad blockers stop analytics scripts', 'nodium' ) ); echo '' . esc_html__( 'Serves Nodium’s script from this site and passes its events on to Nodium from your server, with each visitor’s address signed by your key. The address is used to tell visitors apart and place them, and Nodium never stores it.', 'nodium' ) . '
'; } /** * Adds Settings → Nodium. */ function nodium_settings_menu() { add_options_page( __( 'Nodium', 'nodium' ), __( 'Nodium', 'nodium' ), 'manage_options', 'nodium', 'nodium_settings_page' ); } add_action( 'admin_menu', 'nodium_settings_menu' ); /** * The settings page. */ function nodium_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } ?>