'', 'enabled' => 1, 'audience' => 'everyone', // 'everyone' or 'logged_out'. 'excluded_ids' => '', ); } /** * Current settings merged over the defaults. * * @return array */ public static function get() { $saved = get_option( CLVRLY_OPTION, array() ); return wp_parse_args( is_array( $saved ) ? $saved : array(), self::defaults() ); } /** * Whether the token is set in wp-config.php with the CLVRLY_TOKEN constant. * * @return bool */ public static function token_is_constant() { return defined( 'CLVRLY_TOKEN' ) && self::is_valid_token( CLVRLY_TOKEN ); } /** * The token in use: the CLVRLY_TOKEN constant if set, otherwise the saved one. * * @return string */ public static function token() { if ( self::token_is_constant() ) { return strtolower( CLVRLY_TOKEN ); } $settings = self::get(); return self::is_valid_token( $settings['token'] ) ? $settings['token'] : ''; } /** * Whether a valid token is available. * * @return bool */ public static function has_token() { return '' !== self::token(); } /** * Whether the assistant is switched on (it may still be hidden on some pages). * * @return bool */ public static function is_enabled() { $settings = self::get(); return ! empty( $settings['enabled'] ); } /** * Clvrly tokens are hex strings (32 or 64 characters). * * @param string $token Token to check. * @return bool */ public static function is_valid_token( $token ) { return (bool) preg_match( '/^[a-f0-9]{32,64}$/i', (string) $token ); } /** * Pull a token out of whatever was pasted: the bare token, or the whole install snippet. * * @param string $raw Raw input. * @return string Token (lower case), or the trimmed input if no token was found. */ public static function extract_token( $raw ) { $raw = trim( (string) $raw ); if ( preg_match( '/data-token\s*=\s*["\']?([a-f0-9]{32,64})/i', $raw, $m ) ) { return strtolower( $m[1] ); } $bare = trim( $raw, " \t\n\r\0\x0B\"'" ); return self::is_valid_token( $bare ) ? strtolower( $bare ) : $raw; } /** * Turns "12, 40,abc,40" into array( 12, 40 ). * * @param string $value Comma-separated IDs. * @return int[] */ public static function parse_ids( $value ) { $ids = array_map( 'absint', explode( ',', (string) $value ) ); return array_values( array_unique( array_filter( $ids ) ) ); } /** * Whether the assistant is hidden on a given post, by ID list or by the editor toggle. * * @param int $post_id Post ID. * @return bool */ public static function is_hidden_on( $post_id ) { $post_id = (int) $post_id; if ( ! $post_id ) { return false; } $settings = self::get(); if ( in_array( $post_id, self::parse_ids( $settings['excluded_ids'] ), true ) ) { return true; } return (bool) get_post_meta( $post_id, self::HIDE_META, true ); } /** * Sanitise callback for register_setting(). * * @param mixed $input Raw form input. * @return array */ public static function sanitize( $input ) { $current = self::get(); $input = is_array( $input ) ? $input : array(); $clean = self::defaults(); if ( ! array_key_exists( 'token', $input ) ) { // Token field not submitted (it is read-only when CLVRLY_TOKEN is set). $clean['token'] = $current['token']; } else { $token = self::extract_token( $input['token'] ); $token = strtolower( sanitize_text_field( $token ) ); if ( '' === $token || self::is_valid_token( $token ) ) { $clean['token'] = $token; } else { // Keep the previous token rather than saving a broken one. $clean['token'] = $current['token']; add_settings_error( CLVRLY_OPTION, 'clvrly_invalid_token', __( 'That token doesn\'t look right. Copy it again from "Your Embed Code" in your Clvrly portal. You can paste the token on its own or the whole install snippet.', 'clvrly' ) ); } } $clean['enabled'] = empty( $input['enabled'] ) ? 0 : 1; $clean['audience'] = ( isset( $input['audience'] ) && 'logged_out' === $input['audience'] ) ? 'logged_out' : 'everyone'; $clean['excluded_ids'] = implode( ', ', self::parse_ids( isset( $input['excluded_ids'] ) ? $input['excluded_ids'] : '' ) ); return $clean; } }