assets = $assets; $this->context = $context; } /** * Initializes the Web Stories embed block. * * @since 1.1.0 */ public function register(): void { if ( wp_style_is( self::SCRIPT_HANDLE, 'registered' ) ) { return; } $this->assets->register_style_asset( self::SCRIPT_HANDLE ); if ( \defined( 'AMPFORWP_VERSION' ) ) { add_action( 'amp_post_template_css', [ $this, 'add_amp_post_template_css' ] ); } add_filter( 'wp_kses_allowed_html', [ $this, 'filter_kses_allowed_html' ] ); } /** * Get the action priority to use for registering the service. * * @since 1.6.0 * * @return int Registration action priority to use. */ public static function get_registration_action_priority(): int { return 11; } /** * Prints required inline CSS when using the AMP for WP plugin. * * @since 1.13.0 */ public function add_amp_post_template_css(): void { $path = $this->assets->get_base_path( sprintf( 'assets/css/%s%s.css', self::SCRIPT_HANDLE, is_rtl() ? '-rtl' : '' ) ); if ( is_readable( $path ) ) { $css = file_get_contents( $path ); // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown echo $css; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } } /** * Filter the allowed tags for KSES to allow for amp-story children. * * @since 1.0.0 * * @param array>|mixed $allowed_tags Allowed tags. * @return array>|mixed Allowed tags. * * @template T * * @phpstan-return ($allowed_tags is array ? array : mixed) */ public function filter_kses_allowed_html( $allowed_tags ) { if ( ! \is_array( $allowed_tags ) ) { return $allowed_tags; } $story_player_components = [ 'amp-story-player' => [], ]; $allowed_tags = array_merge( $allowed_tags, $story_player_components ); return $allowed_tags; } /** * Renders a story with given attributes. * * @since 1.30.0 * * @param Story $story Story instance. * @param array $attributes Embed render attributes. * @return string Rendered embed output. */ public function render_story( Story $story, array $attributes ): string { if ( is_feed() ) { $renderer = new Image( $story ); } elseif ( ! empty( $attributes['previewOnly'] ) ) { $renderer = new Singleton( $story, $this->assets ); } else { $renderer = new Embed( $story, $this->assets, $this->context ); } return $renderer->render( $attributes ); } /** * Renders an embed with given attributes. * * @since 1.1.0 * * @param array $attributes Embed render attributes. * @return string Rendered embed output. */ public function render( array $attributes ): string { // The only mandatory attribute. if ( empty( $attributes['url'] ) && empty( $attributes['previewOnly'] ) ) { return ''; } if ( empty( $attributes['title'] ) ) { $attributes['title'] = __( 'Web Story', 'web-stories' ); } $data = [ 'title' => $attributes['title'], 'url' => $attributes['url'], 'poster_portrait' => $attributes['poster'], ]; $story = new Story( $data ); return $this->render_story( $story, $attributes ); } /** * Return an array of default attributes. * * @since 1.1.0 * * @return array Default attributes. */ protected function default_attrs(): array { $attrs = [ 'align' => 'none', 'height' => 600, 'poster' => '', 'url' => '', 'title' => '', 'width' => 360, 'previewOnly' => false, ]; /** * Filters settings passed to the web stories embed. * * @since 1.1.0 * * @param array $attrs Array of settings passed to web stories embed. */ return apply_filters( 'web_stories_embed_default_attributes', $attrs ); } }