'', 'widget_key' => '', 'chat_enabled' => false, 'booking_slug' => '', ); } /** * Saved settings merged over the defaults (never missing a key). * * @return array */ public static function settings() { $saved = get_option( self::OPTION, array() ); if ( ! is_array( $saved ) ) { $saved = array(); } return array_merge( self::defaults(), array_intersect_key( $saved, self::defaults() ) ); } /** * One setting. * * @param string $key Setting name. * @return mixed */ public static function get( $key ) { $settings = self::settings(); return isset( $settings[ $key ] ) ? $settings[ $key ] : null; } /** * Result of the last connection check, or an empty array. * * @return array */ public static function connection() { $connection = get_option( self::CONNECTION_OPTION, array() ); return is_array( $connection ) ? $connection : array(); } /** * Base URL of the Medivot app, without a trailing slash. * * @return string */ public static function app_url() { $url = defined( 'MEDIVOT_APP_URL' ) ? (string) MEDIVOT_APP_URL : self::DEFAULT_APP_URL; /** * Filters the base URL of the Medivot app the plugin talks to. * * @param string $url Base URL, for example https://medivot.com. */ $url = untrailingslashit( esc_url_raw( (string) apply_filters( 'medivot_app_url', $url ) ) ); return '' !== $url ? $url : self::DEFAULT_APP_URL; } /** * The workspace handle used in booking links (/book/{handle}). The saved * setting wins; otherwise the handle detected by the API connection check. * * @return string */ public static function booking_handle() { $handle = (string) self::get( 'booking_slug' ); if ( '' !== $handle ) { return $handle; } $connection = self::connection(); if ( ! empty( $connection['ok'] ) && ! empty( $connection['slug'] ) ) { return self::clean_slug( $connection['slug'] ); } return ''; } /** * Keep only the characters Medivot uses in slugs and IDs. * * @param mixed $value Raw value. * @return string */ public static function clean_slug( $value ) { if ( ! is_scalar( $value ) ) { return ''; } return substr( (string) preg_replace( '/[^A-Za-z0-9_-]/', '', (string) $value ), 0, 191 ); } /** * Read slugs from a bare value or a pasted Medivot link. * * "acme/intro-call" and "https://medivot.com/book/acme/intro-call" with the * marker "book" both give array( 'acme', 'intro-call' ). * * @param mixed $value Bare slug, slug path or full link. * @param string $marker Path segment that precedes the slugs in a full link. * @return string[] */ public static function slugs_from( $value, $marker ) { if ( ! is_scalar( $value ) ) { return array(); } $value = trim( (string) $value ); if ( '' === $value ) { return array(); } $path = (string) wp_parse_url( $value, PHP_URL_PATH ); $parts = array_values( array_filter( explode( '/', $path ), 'strlen' ) ); $at = array_search( $marker, $parts, true ); if ( false !== $at ) { $parts = array_slice( $parts, $at + 1 ); } return array_values( array_filter( array_map( array( __CLASS__, 'clean_slug' ), $parts ), 'strlen' ) ); } }