author-recommendation.php
88 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Author-recommendation Block. |
| 4 | * |
| 5 | * @since 12.1 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\AuthorRecommendation; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Jetpack_Gutenberg; |
| 14 | |
| 15 | const FEATURE_NAME = 'author-recommendation'; |
| 16 | const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
| 17 | |
| 18 | /** |
| 19 | * Registers the block for use in Gutenberg |
| 20 | * This is done via an action so that we can disable |
| 21 | * registration if we need to. |
| 22 | */ |
| 23 | function register_block() { |
| 24 | Blocks::jetpack_register_block( |
| 25 | BLOCK_NAME, |
| 26 | array( |
| 27 | 'render_callback' => __NAMESPACE__ . '\load_assets', |
| 28 | 'supports' => array( |
| 29 | 'color' => array( |
| 30 | 'gradients' => true, |
| 31 | 'link' => true, |
| 32 | ), |
| 33 | 'spacing' => array( |
| 34 | 'margin' => true, |
| 35 | 'padding' => true, |
| 36 | ), |
| 37 | 'typography' => array( |
| 38 | 'fontSize' => true, |
| 39 | 'lineHeight' => true, |
| 40 | ), |
| 41 | ), |
| 42 | ) |
| 43 | ); |
| 44 | } |
| 45 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 46 | |
| 47 | /** |
| 48 | * Author-recommendation block registration/dependency declaration. |
| 49 | * |
| 50 | * @param array $attr Array containing the Author-recommendation block attributes. |
| 51 | * @param string $content String containing the Author-recommendation block content. |
| 52 | * |
| 53 | * @return string |
| 54 | */ |
| 55 | function load_assets( $attr, $content ) { |
| 56 | /* |
| 57 | * Enqueue necessary scripts and styles. |
| 58 | */ |
| 59 | Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
| 60 | |
| 61 | $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports(); |
| 62 | $placeholder_icon = '<svg xmlns="http://www.w3.org/2000/svg" height="38px" viewBox="0 0 24 24" width="38px" fill="#646970"><path d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm0 18l2-2 1-1v-2h-2v-1l-1-1H9v3l2 2v1.931C7.06 19.436 4 16.072 4 12l1 1h2v-2h2l3-3V6h-2L9 5v-.411a7.945 7.945 0 016 0V6l-1 1v2l1 1 3.13-3.13A7.983 7.983 0 0119.736 10H18l-2 2v2l1 1h2l.286.286C18.029 18.061 15.239 20 12 20z"/></svg>'; |
| 63 | |
| 64 | foreach ( $attr['recommendations'] as $recommendation ) { |
| 65 | $url = empty( $recommendation['URL'] ) ? '' : esc_url( $recommendation['URL'] ); |
| 66 | $site_icon = empty( $recommendation['site_icon'] ) ? '' : esc_url( $recommendation['site_icon'] ); |
| 67 | $name = empty( $recommendation['name'] ) ? '' : $recommendation['name']; |
| 68 | |
| 69 | $icon_image = $site_icon ? "<img class='site-icon' src='{$site_icon}' alt='" . esc_attr( $name ) . "' />" : $placeholder_icon; |
| 70 | |
| 71 | if ( empty( $name ) || empty( $url ) ) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | $content .= "<div class='recommendation-row'> |
| 76 | {$icon_image} |
| 77 | <a href='{$url}'>" . esc_html( $name ) . '</a> |
| 78 | </div>'; |
| 79 | } |
| 80 | |
| 81 | return sprintf( |
| 82 | '<div class="%s"%s>%s</div>', |
| 83 | ! empty( $wrapper_attributes['class'] ) ? esc_attr( $wrapper_attributes['class'] ) : '', |
| 84 | ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '', |
| 85 | $content |
| 86 | ); |
| 87 | } |
| 88 |