isset( $c['key'] ) ? (string) $c['key'] : '', 'agent' => isset( $c['agent'] ) ? (string) $c['agent'] : '', 'workspace' => isset( $c['workspace'] ) ? (string) $c['workspace'] : '', ); } public static function connect( $key, $agent = '', $workspace = '' ) { update_option( self::CONNECTION, array( 'key' => $key, 'agent' => sanitize_text_field( $agent ), 'workspace' => sanitize_text_field( $workspace ), ) ); } public static function disconnect() { delete_option( self::CONNECTION ); } /** The agent's public key as ReplyBash makes them (acme_7Hq2k), or '' when it isn't one. */ public static function clean_key( $key ) { $key = trim( (string) $key ); return preg_match( '/^[A-Za-z0-9_-]{3,64}$/', $key ) ? $key : ''; } public static function defaults() { return array( 'enabled' => 1, 'pages_mode' => 'all', 'pages' => '', 'hide_staff' => 0, 'identify' => 0, 'identity_secret' => '', ); } public static function settings() { return wp_parse_args( get_option( self::SETTINGS, array() ), self::defaults() ); } /** register_setting's sanitize_callback: the form posts every field but the secret, which is kept when left empty. */ public static function sanitize( $input ) { $old = self::settings(); $input = is_array( $input ) ? $input : array(); $mode = isset( $input['pages_mode'] ) ? $input['pages_mode'] : 'all'; $secret = isset( $input['identity_secret'] ) ? trim( (string) $input['identity_secret'] ) : ''; $pages = array(); foreach ( preg_split( '/\r\n|\r|\n/', isset( $input['pages'] ) ? (string) $input['pages'] : '' ) as $line ) { $line = self::clean_path( $line ); if ( '' !== $line ) { $pages[] = $line; } } return array( 'enabled' => empty( $input['enabled'] ) ? 0 : 1, 'pages_mode' => in_array( $mode, array( 'all', 'except', 'only' ), true ) ? $mode : 'all', 'pages' => implode( "\n", array_unique( $pages ) ), 'hide_staff' => empty( $input['hide_staff'] ) ? 0 : 1, 'identify' => empty( $input['identify'] ) ? 0 : 1, 'identity_secret' => '' === $secret ? $old['identity_secret'] : sanitize_text_field( $secret ), ); } /** "https://acme.com/Pricing/?x=1" → "/pricing", "blog/*" → "/blog/*". '' for blank lines. */ public static function clean_path( $line ) { $line = trim( (string) $line ); if ( '' === $line ) { return ''; } $star = '*' === substr( $line, -1 ); $path = wp_parse_url( $star ? substr( $line, 0, -1 ) : $line, PHP_URL_PATH ); $path = '/' . trim( strtolower( (string) $path ), '/' ); if ( $star ) { return '/' === $path ? '/*' : $path . '/*'; } return $path; } }