is_registered( self::BLOCK ) ) { return false; } $new = wp_register_script( self::EDITOR, AGEVOW_URL . 'admin/js/section-block.js', array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n' ), AGEVOW_VERSION, true ); if ( $new ) { // The site's minimum age, so the box can say what "the site's" means. wp_add_inline_script( self::EDITOR, 'var agevowSection = ' . wp_json_encode( array( 'age' => (int) Settings::get()['min_age'] ) ) . ';', 'before' ); wp_set_script_translations( self::EDITOR, 'agevow-age-verification-age-gate' ); } return false !== register_block_type( self::BLOCK, array( 'api_version' => 3, 'title' => __( 'Age-restricted section', 'agevow-age-verification-age-gate' ), 'description' => __( 'Shows its content only after the visitor\'s age is checked; the rest of the page stays as it is.', 'agevow-age-verification-age-gate' ), 'category' => 'design', 'icon' => 'lock', 'keywords' => array( 'age', 'gate', 'restricted' ), 'attributes' => array( 'age' => array( 'type' => 'integer', 'default' => 0, ), ), 'supports' => array( 'html' => false ), 'editor_script_handles' => array( self::EDITOR ), 'render_callback' => array( self::class, 'render_block' ), ) ); } /** * The block on the page. * * @param array $attributes The block's attributes (age: 0 = the site's minimum age). * @param string $content Its inner blocks, rendered. * @return string */ public static function render_block( $attributes, $content ) { $age = is_array( $attributes ) && isset( $attributes['age'] ) ? $attributes['age'] : 0; return self::markup( self::age( $age ), (string) $content, Settings::get(), get_locale() ); } /** * The shortcode: `[agevow_section age="21"]…[/agevow_section]` (age optional). * * @param array|string $atts Its attributes. * @param string|null $content What it encloses. * @return string */ public static function shortcode( $atts, $content = null ) { $atts = shortcode_atts( array( 'age' => '' ), is_array( $atts ) ? $atts : array(), self::SHORTCODE ); return self::markup( self::age( $atts['age'] ), do_shortcode( (string) $content ), Settings::get(), get_locale() ); } /** * A section's minimum age: its own (18–25), or the site's. * * @param mixed $value The block's or the shortcode's age. * @return int */ public static function age( $value ) { $value = is_string( $value ) ? trim( $value ) : $value; return Settings::is_age( $value ) ? (int) $value : (int) Settings::get()['min_age']; } /** * The page's own section, in its head: the stylesheet and the gate script where the * page shown is a single post or page that holds a section (so the question never * shows unstyled). Elsewhere a section loads them itself, at the end of the page. * * @return void */ public static function enqueue() { $post = is_singular() ? get_queried_object() : null; if ( ! $post instanceof \WP_Post ) { return; } if ( has_block( self::BLOCK, $post ) || has_shortcode( (string) $post->post_content, self::SHORTCODE ) ) { Gate::assets( Settings::get() ); } } /** * The section's HTML: its age, its lock, its question (hidden) and its content. * The same for every visitor; the browser decides. * * @param int $age The minimum age, 18–25. * @param string $content The content behind the gate (HTML). * @param array $settings The settings. * @param string $locale The site's language (the order of the date's fields). * @return string '' when there is nothing to hold back; the content alone in a feed. */ public static function markup( $age, $content, array $settings, $locale ) { if ( '' === trim( (string) $content ) ) { return ''; } // A feed is never gated (§S-GATE): a reader runs no script, so the content // goes as it is, without the question. if ( is_feed() ) { return (string) $content; } Gate::assets( $settings ); ++self::$count; $id = 'agevow-section-' . self::$count; $age = (int) $age; $method = 'dob' === $settings['method'] ? 'dob' : 'confirm'; $attrs = sprintf( ' data-agevow-age="%d" data-agevow-days="%d"', $age, (int) $settings['days'] ); if ( ! empty( $settings['skip_logged_in'] ) ) { $attrs .= ' data-agevow-skip="li"'; } /* translators: %d: the minimum age, 18 to 25. */ $title = sprintf( __( 'This part of the page is for people aged %d or older.', 'agevow-age-verification-age-gate' ), $age ); /* translators: %d: the minimum age, 18 to 25. */ $under = sprintf( __( 'Sorry, this part of the page is for people aged %d or older.', 'agevow-age-verification-age-gate' ), $age ); $html = '
'; $html .= wp_get_inline_script_tag( Gate::own_script( 'section-lock.js' ) ); $html .= ''; $html .= '
' . $content . '
'; $html .= '
'; return $html; } /** * Forget this request's count (the tests print several pages in one process). * * @return void */ public static function reset() { self::$count = 0; } }