'array', 'sanitize_callback' => array( __CLASS__, 'sanitize' ), 'show_in_rest' => false, ) ); } /** * Sanitize settings. An empty secret field keeps the saved key. * * @param array $in Input. * @return array */ public static function sanitize( $in ) { $old = nbots_options(); $in = is_array( $in ) ? $in : array(); $pk = isset( $in['public_key'] ) ? trim( sanitize_text_field( $in['public_key'] ) ) : ''; $sk = isset( $in['secret_key'] ) ? trim( sanitize_text_field( $in['secret_key'] ) ) : ''; if ( '' !== $pk && ! preg_match( '/^pk_[A-Za-z0-9_-]{6,80}$/', $pk ) ) { add_settings_error( 'nbots', 'pk', __( 'The public key must start with pk_ — copy it from the «Connection» page in nbots.', 'nbots' ) ); $pk = $old['public_key']; } if ( '' === $sk ) { $sk = empty( $in['clear_secret'] ) ? $old['secret_key'] : ''; } elseif ( ! preg_match( '/^sk_[A-Za-z0-9_-]{6,120}$/', $sk ) ) { add_settings_error( 'nbots', 'sk', __( 'The secret key must start with sk_ — copy it from «Settings → Developer keys» in nbots.', 'nbots' ) ); $sk = $old['secret_key']; } $ym_id = isset( $in['ym_id'] ) ? substr( preg_replace( '/\D/', '', (string) $in['ym_id'] ), 0, 12 ) : ''; $ym_human = empty( $in['ym_human'] ) ? 0 : 1; if ( $ym_human && '' === $ym_id ) { add_settings_error( 'nbots', 'ym', __( 'To run Metrica only for humans, enter the counter number.', 'nbots' ) ); $ym_human = 0; } delete_transient( 'nbots_ym_scan' ); update_option( 'nbots_ym_saved_at', time(), false ); return array( 'public_key' => $pk, 'secret_key' => $sk, 'skip_admins' => empty( $in['skip_admins'] ) ? 0 : 1, 'cf7' => empty( $in['cf7'] ) ? 0 : 1, 'wpforms' => empty( $in['wpforms'] ) ? 0 : 1, 'elementor' => empty( $in['elementor'] ) ? 0 : 1, 'comments' => empty( $in['comments'] ) ? 0 : 1, 'ym_human' => $ym_human, 'ym_id' => $ym_id, 'ym_wv' => empty( $in['ym_wv'] ) ? 0 : 1, 'ym_ec' => empty( $in['ym_ec'] ) ? 0 : 1, ); } /** * First save. * * @param string $name Option. * @param array $value Value. */ public static function after_add( $name, $value ) { self::after_save( array(), $value ); } /** * Key removed — forget the old status. * * @param array $old Old value. * @param array $value New value. */ public static function after_save( $old, $value ) { if ( empty( $value['secret_key'] ) ) { delete_option( 'nbots_status' ); } } /** * Which form plugins are active. * * @return array */ private static function detected() { return array( 'cf7' => array( 'Contact Form 7', defined( 'WPCF7_VERSION' ) ), 'wpforms' => array( 'WPForms', function_exists( 'wpforms' ) ), 'elementor' => array( 'Elementor Pro', defined( 'ELEMENTOR_PRO_VERSION' ) ), 'comments' => array( __( 'WordPress comments', 'nbots' ), (bool) get_option( 'default_comment_status' ) || (bool) wp_count_comments()->total_comments ), ); } /** * Settings page. */ public static function page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $o = nbots_options(); // After every save check the key right away, so the nbots dashboard shows «Server connected». // Also refresh it every 10 minutes: the Metrica status comes from what visitors' browsers reported to nbots. $status = get_option( 'nbots_status' ); $ym = is_array( $status ) && ! empty( $status['ym'] ) ? $status['ym'] : array(); $wait = $o['ym_human'] && ( empty( $ym['mode'] ) || 'h' !== $ym['mode'] || ! empty( $ym['dup'] ) ); $stale = ! is_array( $status ) || empty( $status['time'] ) || $status['time'] < time() - ( $wait ? 30 : 600 ); if ( $o['secret_key'] && ( isset( $_GET['settings-updated'] ) || $stale ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- flag set by options.php after a nonce-checked save. $status = NBots_Client::ping( $o['secret_key'] ); $status['time'] = time(); update_option( 'nbots_status', $status, false ); } $stats = get_option( 'nbots_stats', array() ); $modes = array( 'observe' => __( 'Observation', 'nbots' ), 'protect' => __( 'Protection', 'nbots' ), 'strict' => __( 'Strict', 'nbots' ), ); ?>

↗

' . esc_html( $status['site'] ) . '', esc_html( isset( $modes[ $status['mode'] ] ) ? $modes[ $status['mode'] ] : $status['mode'] ) ); ?>