__NAMESPACE__ . '\render_block', // Since Gutenberg #31873. 'style' => 'wp-mediaelement', ) ); } add_action( 'init', __NAMESPACE__ . '\register_block' ); /** * Returns the error message wrapped in HTML if current user * has the capability to edit the post. Public visitors will * never see errors. * * @param string $message The error message to display. * @return string */ function render_error( $message ) { // Suppress errors for users unable to address them. if ( ! current_user_can( 'edit_posts' ) ) { return ''; } return '
' . esc_html( $message ) . '
'; } /** * Podcast Player block registration/dependency declaration. * * @param array $attributes Array containing the Podcast Player block attributes. * @param string $content Fallback content - a direct link to RSS, as rendered by save.js. * @return string */ function render_block( $attributes, $content ) { // Don't render an interactive version of the block outside the frontend context. if ( ! jetpack_is_frontend() ) { return $content; } // Test for empty URLS. if ( empty( $attributes['url'] ) ) { return render_error( __( 'No Podcast URL provided. Please enter a valid Podcast RSS feed URL.', 'jetpack' ) ); } // Test for invalid URLs. if ( ! wp_http_validate_url( $attributes['url'] ) ) { return render_error( __( 'Your podcast URL is invalid and couldn\'t be embedded. Please double check your URL.', 'jetpack' ) ); } if ( ! empty( $attributes['selectedEpisodes'] ) ) { $guids = array_map( function ( $episode ) { return $episode['guid']; }, $attributes['selectedEpisodes'] ); $player_args = array( 'guids' => $guids ); } else { $player_args = array(); } // Sanitize the URL. $attributes['url'] = esc_url_raw( $attributes['url'] ); $player_data = ( new Jetpack_Podcast_Helper( $attributes['url'] ) )->get_player_data( $player_args ); if ( is_wp_error( $player_data ) ) { return render_error( $player_data->get_error_message() ); } return render_player( $player_data, $attributes ); } /** * Renders the HTML for the Podcast player and tracklist. * * @param array $player_data The player data details. * @param array $attributes Array containing the Podcast Player block attributes. * @return string The HTML for the podcast player. */ function render_player( $player_data, $attributes ) { // If there are no tracks (it is possible) then display appropriate user facing error message. if ( empty( $player_data['tracks'] ) ) { return render_error( __( 'No tracks available to play.', 'jetpack' ) ); } if ( is_wp_error( $player_data['tracks'] ) ) { return render_error( $player_data['tracks']->get_error_message() ); } // Only use the amount of tracks requested. $player_data['tracks'] = array_slice( $player_data['tracks'], 0, absint( $attributes['itemsToShow'] ) ); // Generate a unique id for the block instance. $instance_id = wp_unique_id( 'jetpack-podcast-player-block-' . get_the_ID() . '-' ); $player_data['playerId'] = $instance_id; // Generate object to be used as props for PodcastPlayer. $player_props = array_merge( // Add all attributes. array( 'attributes' => $attributes ), // Add all player data. $player_data ); $primary_colors = get_colors( 'primary', $attributes, 'color' ); $secondary_colors = get_colors( 'secondary', $attributes, 'color' ); $background_colors = get_colors( 'background', $attributes, 'background-color' ); $player_classes_name = trim( "{$secondary_colors['class']} {$background_colors['class']}" ); $player_inline_style = trim( "{$secondary_colors['style']} {$background_colors['style']}" ); $player_inline_style .= get_css_vars( $attributes ); $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports(); $block_classname = Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attributes, array( 'is-default' ) ); $is_amp = Blocks::is_amp_request(); ob_start(); ?>