'async', 'in_footer' => true, ) ); add_shortcode( 'webtospeech', 'webtospeech_shortcode' ); register_block_type( WEBTOSPEECH_DIR . 'blocks/player' ); foreach ( array_keys( webtospeech_available_post_types() ) as $post_type ) { register_post_meta( $post_type, WEBTOSPEECH_DISABLED_META, array( 'type' => 'boolean', 'single' => true, 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', 'auth_callback' => static function ( $allowed, $meta_key, $post_id ) { return current_user_can( 'edit_post', $post_id ); }, ) ); } } /** * Whether the plugin has a site key to load players with. * * @return bool */ function webtospeech_is_configured() { return '' !== webtospeech_setting( 'site_key' ); } /** * Whether the player was turned off for a post. * * @param int|WP_Post|null $post The post, or the current one. * @return bool */ function webtospeech_is_disabled_for( $post = null ) { $post = get_post( $post ); return $post && (bool) get_post_meta( $post->ID, WEBTOSPEECH_DISABLED_META, true ); } /** * Whether a post places its player by hand, with the shortcode or the block, so it shouldn't get one automatically too. * * @param WP_Post $post The post. * @return bool */ function webtospeech_has_manual_player( $post ) { return has_shortcode( $post->post_content, 'webtospeech' ) || has_block( 'webtospeech/player', $post ); } /** * Whether the page being viewed is a single post of a type that gets the player automatically. * * @return bool */ function webtospeech_is_automatic_page() { if ( ! webtospeech_is_configured() || is_admin() || is_feed() || is_embed() ) { return false; } $post_types = (array) webtospeech_setting( 'post_types' ); // A static front page is the site's home, not an article, even when pages get players. if ( ! $post_types || ! is_singular( $post_types ) || is_front_page() ) { return false; } $post = get_queried_object(); return $post instanceof WP_Post && ! post_password_required( $post ) && ! webtospeech_is_disabled_for( $post ) && ! webtospeech_has_manual_player( $post ); } /** * Load the embed script on pages that will get a player automatically. */ function webtospeech_enqueue_for_automatic_player() { if ( webtospeech_is_automatic_page() ) { wp_enqueue_script( WEBTOSPEECH_SCRIPT_HANDLE ); } } /** * Name the site on the embed script's tag, as embed.js expects. * * @param array $attributes The script tag's attributes. * @return array */ function webtospeech_script_attributes( $attributes ) { if ( isset( $attributes['id'] ) && WEBTOSPEECH_SCRIPT_HANDLE . '-js' === $attributes['id'] ) { $attributes['data-site'] = webtospeech_setting( 'site_key' ); } return $attributes; } /** * The element embed.js turns into a player, for a post. * * @param WP_Post|null $post The post to read, or null to let embed.js work out the page. * @param string $theme The player's theme: auto, light or dark. * @return string */ function webtospeech_player_html( $post = null, $theme = '' ) { $theme = in_array( $theme, array( 'auto', 'light', 'dark' ), true ) ? $theme : webtospeech_setting( 'theme' ); $html = '
'; /** * Filters the element the player is drawn in. * * @param string $html The element. * @param WP_Post|null $post The post it reads. */ return (string) apply_filters( 'webtospeech_player_html', $html, $post ); } /** * Add the player above or below a post's content, on single views of the chosen post types. * * Only the main post on the page gets one, not posts shown in widgets, related-post lists, or excerpts. * * @param string $content The post's content. * @return string */ function webtospeech_add_player_to_content( $content ) { if ( ! in_the_loop() || ! is_main_query() || ! webtospeech_is_automatic_page() ) { return $content; } $post = get_post(); if ( ! $post || get_queried_object_id() !== $post->ID ) { return $content; } wp_enqueue_script( WEBTOSPEECH_SCRIPT_HANDLE ); $player = webtospeech_player_html( $post ); return 'below' === webtospeech_setting( 'position' ) ? $content . $player : $player . $content; } /** * The player where it's placed by hand, for the shortcode and the block. * * Like the automatic player, it appears only on a post's own page, not in archives, search results or the blog index. * * @param string $theme The player's theme, or an empty string for the one in the settings. * @return string */ function webtospeech_manual_player( $theme = '' ) { if ( ! webtospeech_is_configured() || ! is_singular() || is_feed() || is_embed() ) { return ''; } $post = get_post(); if ( $post && ( webtospeech_is_disabled_for( $post ) || post_password_required( $post ) ) ) { return ''; } wp_enqueue_script( WEBTOSPEECH_SCRIPT_HANDLE ); return webtospeech_player_html( $post, $theme ); } /** * The [webtospeech] shortcode. Takes an optional theme="auto|light|dark". * * @param array|string $atts The shortcode's attributes. * @return string */ function webtospeech_shortcode( $atts ) { $atts = shortcode_atts( array( 'theme' => '' ), $atts, 'webtospeech' ); return webtospeech_manual_player( sanitize_key( $atts['theme'] ) ); } /** * Print the verification meta tag on the front page, so webtospeech.app can check the site is yours. */ function webtospeech_print_verification_meta() { $token = webtospeech_setting( 'verification_token' ); if ( '' === $token || ! is_front_page() ) { return; } printf( '' . "\n", esc_attr( $token ) ); }