array( $this, 'render_block' ) ) ); // Back-compat: render posts saved with a former block name without // showing it in the inserter. New posts use the embedwell/embed block. $legacy_attributes = array( 'id' => array( 'type' => 'number', 'default' => 0, ), 'url' => array( 'type' => 'string', 'default' => '', ), 'height' => array( 'type' => 'number', 'default' => 600, ), 'toolbar' => array( 'type' => 'boolean', 'default' => true, ), 'title' => array( 'type' => 'string', 'default' => '', ), ); foreach ( array( 'paperpane/embed', 'docuforge/embed', 'pdf-embed-forge/embed' ) as $legacy_block ) { if ( WP_Block_Type_Registry::get_instance()->is_registered( $legacy_block ) ) { continue; } register_block_type( $legacy_block, array( 'api_version' => 3, 'render_callback' => array( $this, 'render_block' ), 'supports' => array( 'inserter' => false ), 'attributes' => $legacy_attributes, ) ); } } } /** * Bridge the block attributes to the shared renderer. * * @param array $attributes Block attributes. * @return string */ public function render_block( $attributes ) { // Carry the block's Wide/Full alignment onto the wrapper so the theme can break it out. $extra = ''; if ( ! empty( $attributes['align'] ) ) { $extra = 'align' . sanitize_html_class( $attributes['align'] ); } return $this->render( array( 'id' => isset( $attributes['id'] ) ? $attributes['id'] : 0, 'url' => isset( $attributes['url'] ) ? $attributes['url'] : '', 'height' => isset( $attributes['height'] ) ? $attributes['height'] : 600, 'toolbar' => ( ! isset( $attributes['toolbar'] ) || $attributes['toolbar'] ) ? '1' : '0', 'title' => isset( $attributes['title'] ) ? $attributes['title'] : '', ), $extra ); } /** * Render the PDF viewer. Shared by the block and the shortcodes. * * @param array $atts Attributes. * @return string */ public function render( $atts, $extra_class = '' ) { $a = shortcode_atts( array( 'id' => 0, 'url' => '', 'height' => 600, 'toolbar' => '1', 'title' => '', ), $atts, 'ew-pdf-embed' ); $url = $a['url'] ? $a['url'] : ( $a['id'] ? wp_get_attachment_url( (int) $a['id'] ) : '' ); if ( ! $url ) { return ''; } // Only embed things that are actually PDFs. $is_pdf = ( $a['id'] && 'application/pdf' === get_post_mime_type( (int) $a['id'] ) ) || preg_match( '/\.pdf($|[?#])/i', $url ); if ( ! $is_pdf ) { return ''; } wp_enqueue_style( 'embedwell' ); $height = is_numeric( $a['height'] ) ? (int) $a['height'] . 'px' : preg_replace( '/[^0-9a-z%.]/i', '', (string) $a['height'] ); if ( '' === $height ) { $height = '600px'; } $toolbar = $this->is_on( $a['toolbar'] ); // Open parameters for the built-in viewer: hide the thumbnail/pages sidebar and // fit the page to the viewer width. Chromium honours these; other browsers ignore them. $params = array( 'navpanes=0', 'view=FitH' ); if ( ! $toolbar ) { $params[] = 'toolbar=0'; } $src = $url . '#' . implode( '&', $params ); $title = $a['title']; if ( ! $title && $a['id'] ) { $title = get_the_title( (int) $a['id'] ); } if ( ! $title ) { $title = __( 'Embedded PDF', 'embedwell' ); } $wrap_class = 'embedwell'; if ( $extra_class ) { $wrap_class .= ' ' . $extra_class; } // First-page preview for the mobile card. WordPress generates this on upload // when the host has Imagick + Ghostscript; otherwise we fall back to an icon. $thumb = $a['id'] ? wp_get_attachment_image( (int) $a['id'], 'large', false, array( 'class' => 'embedwell__thumb', 'alt' => '', 'loading' => 'lazy', ) ) : ''; ob_start(); ?>
' . esc_html__( 'Support on Ko-fi', 'embedwell' ) . ''; $links[] = '' . esc_html__( 'Contact developer', 'embedwell' ) . ''; return $links; } /** * Interpret a truthy attribute from a shortcode string or a block boolean. * * @param mixed $value Raw value. * @return bool */ private function is_on( $value ) { if ( is_bool( $value ) ) { return $value; } $value = strtolower( trim( (string) $value ) ); return ! in_array( $value, array( '0', 'false', 'no', 'off', '' ), true ); } }