__( 'Language', 'accessibit' ), 'languageHelp' => __( 'The visitor\'s own language is the default. Pick one to always show that language.', 'accessibit' ), 'languages' => self::language_options(), ), JSON_HEX_TAG | JSON_HEX_AMP ) . ';', 'before' ); register_block_type( ACCESSIBIT_PLUGIN_DIR . 'blocks/statement', array( 'render_callback' => array( __CLASS__, 'render_block' ) ) ); } /** * The language choices, labelled. * * @return array */ public static function language_options(): array { return array( array( 'value' => '', 'label' => __( 'The visitor\'s language', 'accessibit' ), ), array( 'value' => 'it', 'label' => __( 'Italian', 'accessibit' ), ), array( 'value' => 'en', 'label' => __( 'English', 'accessibit' ), ), array( 'value' => 'es', 'label' => __( 'Spanish', 'accessibit' ), ), ); } /** * `[accessibit_statement lang="it"]`. * * @param array|string $atts Shortcode attributes ('' when there are none). * @return string */ public static function shortcode( $atts ): string { $atts = shortcode_atts( array( 'lang' => '' ), is_array( $atts ) ? $atts : array(), self::SHORTCODE ); return self::render( (string) $atts['lang'] ); } /** * The block's server-side render. * * @param array $attributes The block's attributes. * @return string */ public static function render_block( $attributes ): string { $lang = is_array( $attributes ) && isset( $attributes['lang'] ) ? (string) $attributes['lang'] : ''; $html = self::render( $lang ); if ( '' === $html ) { return ''; } return '
' . $html . '
'; } /** * The embed, or what an editor sees in its place. * * Visitors get nothing until there is something published: the public * address answers a plain-text "not found" before that, which would print * an English error line inside the page. Someone who can edit the page is * told why it is empty instead, so a placed block is not a mystery. * * @param string $lang `it`, `en`, `es`, or '' for the visitor's own. * @return string */ public static function render( string $lang ): string { self::maybe_look_up(); $id = Accessibit_Settings::get_statement_id(); // A cloned site — the connected database copied to staging or to // another domain — carries the original's statement id and its // published flag. Its binding is stale, as the widget already knows // (it is held back for the same reason), so the original's statement // is not embedded here either (PR #1162 review). $stale = Accessibit_Settings::is_binding_stale(); if ( ! $stale && Accessibit_Settings::is_site_linked() && '' !== $id && Accessibit_Settings::is_statement_published() ) { return self::embed_html( Accessibit_Links::statement_public_for( ACCESSIBIT_API_BASE_URL, $id, self::language( $lang ) ), __( 'Accessibility statement', 'accessibit' ) ); } if ( ! current_user_can( 'edit_posts' ) ) { return ''; } if ( $stale ) { return '

' . esc_html__( 'This site\'s address changed since it was connected to AccessiBit, so its accessibility statement is not shown here. Reconnect it from AccessiBit → Setup. Only people who can edit this site see this note.', 'accessibit' ) . '

'; } return '

' . esc_html__( 'Your AccessiBit accessibility statement appears here once it is published. Publish it from AccessiBit → Statement. Only people who can edit this site see this note.', 'accessibit' ) . '

'; } /** * Have AccessiBit asked for the site's statement, if none published is * known yet — through WP-Cron, never inside this request. * * It used to ask right here, synchronously: on a page holding the block or * the shortcode, one visitor every fifteen minutes waited on the API, up to * `LOOKUP_TIMEOUT` seconds when it was slow or a host firewall dropped the * call (PR #1162 review). Now this only schedules a single cron event, * which WordPress runs from a non-blocking loopback request. * * The lock is taken when scheduling, so a page view per interval asks * rather than every one. Once a published statement is known there is * nothing left to learn: later publications keep the same id and address. */ private static function maybe_look_up(): void { if ( Accessibit_Settings::is_statement_published() || ! Accessibit_Settings::is_site_linked() || Accessibit_Settings::is_binding_stale() ) { return; } if ( false !== get_transient( self::LOOKUP_LOCK ) ) { return; } set_transient( self::LOOKUP_LOCK, 1, self::LOOKUP_EVERY ); if ( false === wp_next_scheduled( self::LOOKUP_HOOK ) ) { wp_schedule_single_event( time(), self::LOOKUP_HOOK ); } } /** * Ask AccessiBit for the site's statement and remember what it says. * * Run by the cron event `maybe_look_up()` schedules, and, unthrottled, by * the Statement screen (`Accessibit_Admin`). Anything but a statement — a * 403 for a plan without one, a 404, no answer — leaves the settings as * they were, and sets how long until the next scheduled try * (`lookup_backoff()`). */ public static function look_up(): void { // Checked again here: the binding may have gone stale, or the site been // disconnected, between the page view that scheduled this and the cron // request that runs it. if ( ! Accessibit_Settings::is_site_linked() || Accessibit_Settings::is_binding_stale() ) { return; } $result = Accessibit_Api_Client::from_settings()->forward( 'GET', '/sites/' . rawurlencode( Accessibit_Settings::get_site_id() ) . '/statement', null, self::LOOKUP_TIMEOUT ); $answer = Accessibit_Rest::answer( $result ); if ( null !== $answer['remember'] ) { Accessibit_Rest::remember_statement( $answer['remember'] ); } set_transient( self::LOOKUP_LOCK, 1, self::lookup_backoff( $result ) ); } /** * Seconds until the next scheduled look-up, after one that answered this. * * A plan without the statement, or a key AccessiBit no longer accepts, * answers the same every time, so it is asked once a day rather than every * fifteen minutes. Opening the Statement screen still asks at once. * * Pure, for the unit suite. * * @param array $result The API client's result. * @return int */ public static function lookup_backoff( array $result ): int { $code = $result['code'] ?? ''; if ( empty( $result['ok'] ) && in_array( $code, array( 'statement_license_required', 'invalid_api_key' ), true ) ) { return DAY_IN_SECONDS; } return self::LOOKUP_EVERY; } /** * A language the statement is published in, or '' for the visitor's own. * * @param string $lang Whatever the shortcode or block was given. * @return string */ public static function language( string $lang ): string { $lang = strtolower( trim( $lang ) ); return in_array( $lang, self::LANGUAGES, true ) ? $lang : ''; } /** * The dashboard's embed snippet (`StatementEmbedPanel`), as markup. * * Pure, for the unit suite. * * @param string $url The public statement address. * @param string $title The frame's accessible name. * @return string */ public static function embed_html( string $url, string $title ): string { return '
' . '' . '
'; } }