'', 'lang' => '', 'height' => '', 'color' => '', ), $atts, 'pdfkind' ); return self::render( (string) $atts['tool'], (string) $atts['lang'], (int) $atts['height'], (string) $atts['color'] ); } /** * The tool's container; embed.js puts the tool in it. * * @param string $tool Tool ID, for example merge-pdf. * @param string $lang Language code, or empty for the plugin setting. * @param int $height Height in pixels, or 0 for the default. * @param string $color Colour like #0057b8, or empty for the plugin setting. * @return string */ public static function render( $tool, $lang = '', $height = 0, $color = '' ) { $tool = sanitize_key( $tool ); $tools = pdfkind_tools(); if ( '' === $tool ) { return ''; } if ( ! isset( $tools[ $tool ] ) ) { if ( ! current_user_can( 'edit_posts' ) ) { return ''; } return '
' . esc_html( sprintf( /* translators: %s: the tool ID that was typed. */ __( 'PDFkind: unknown tool "%s". Check the tool ID. Only editors see this message.', 'pdfkind' ), $tool ) ) . '
'; } $lang = sanitize_key( $lang ); if ( ! isset( Pdfkind_Settings::LANGUAGES[ $lang ] ) ) { $lang = Pdfkind_Settings::lang(); } // Editor tools have a fixed height; the others grow to fit their content. $is_editor = $tools[ $tool ][2]; $height = $height > 0 ? min( max( $height, 320 ), 4000 ) : ( $is_editor ? 900 : 640 ); wp_enqueue_script( Pdfkind_Frontend::HANDLE ); $color = sanitize_hex_color( $color ); if ( ! $color ) { $color = Pdfkind_Settings::get()['color']; } return sprintf( '', esc_attr( $tool ), esc_attr( $lang ), $height, esc_url( PDFKIND_ORIGIN . '/' . $tool ), esc_html( $tools[ $tool ][0] ), $color ? ' data-color="' . esc_attr( $color ) . '"' : '' ); } /** * The block. Its editor script ships in blocks/tool/ with no build step. */ public static function register_block() { register_block_type( PDFKIND_DIR . 'blocks/tool', array( 'render_callback' => array( __CLASS__, 'render_block' ) ) ); $handle = generate_block_asset_handle( 'pdfkind/tool', 'editorScript' ); $tools = array(); foreach ( pdfkind_tools() as $id => $t ) { $tools[] = array( 'id' => $id, 'name' => $t[0], 'cat' => $t[1], 'editor' => $t[2], 'short' => $t[3], ); } $langs = array(); foreach ( Pdfkind_Settings::LANGUAGES as $code => $label ) { $langs[] = array( 'value' => $code, 'label' => $label, ); } wp_add_inline_script( $handle, 'window.pdfkindBlock = ' . wp_json_encode( array( 'tools' => $tools, 'langs' => $langs, 'lang' => Pdfkind_Settings::lang(), 'color' => Pdfkind_Settings::get()['color'], 'origin' => PDFKIND_ORIGIN, ) ) . ';', 'before' ); wp_set_script_translations( $handle, 'pdfkind' ); } /** * Front-end output of the block. * * @param array $attributes Block attributes. * @return string */ public static function render_block( $attributes ) { $tool = isset( $attributes['tool'] ) ? (string) $attributes['tool'] : ''; $lang = isset( $attributes['lang'] ) ? (string) $attributes['lang'] : ''; $height = isset( $attributes['height'] ) ? (int) $attributes['height'] : 0; $color = isset( $attributes['color'] ) ? (string) $attributes['color'] : ''; $html = self::render( $tool, $lang, $height, $color ); return '' === $html ? '' : '