index.php
78 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Blocks; |
| 4 | |
| 5 | use Kubio\Core\Blocks\BlockBase; |
| 6 | use Kubio\Core\LodashBasic; |
| 7 | use Kubio\Core\Registry; |
| 8 | use Kubio\Core\Utils as GeneralUtils; |
| 9 | |
| 10 | class PostAuthorAvatarBlock extends BlockBase { |
| 11 | |
| 12 | const OUTER = 'outer'; |
| 13 | const IMAGE = 'image'; |
| 14 | |
| 15 | public function computed() { |
| 16 | return array(); |
| 17 | } |
| 18 | |
| 19 | public function mapPropsToElements() { |
| 20 | if ( ! ( $postId = LodashBasic::get( $this->block_context, 'postId' ) ) ) { |
| 21 | return null; |
| 22 | } |
| 23 | |
| 24 | $authorId = get_post_field( 'post_author', $postId ); |
| 25 | if ( empty( $authorId ) ) { |
| 26 | return ''; |
| 27 | } |
| 28 | |
| 29 | $avatarSize = $this->getAttribute( 'avatarSize' ); |
| 30 | |
| 31 | $defaultImg = GeneralUtils::getDefaultAssetsURL( 'avatar-image-placeholder.png' ); |
| 32 | $src = get_avatar_url( $authorId, array( 'size' => $avatarSize ) ); |
| 33 | $src = $src ? $src : $defaultImg; |
| 34 | $src2x = get_avatar_url( $authorId, array( 'size' => $avatarSize * 2 ) ); |
| 35 | $src2x = ( $src2x ? $src2x : $defaultImg ) . ' 2x'; |
| 36 | |
| 37 | if ( $avatarSize > 0 ) { |
| 38 | $map[ self::IMAGE ] = array( |
| 39 | 'src' => esc_url( $src ), |
| 40 | 'srcset' => esc_attr( $src2x ), |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | return $map; |
| 45 | } |
| 46 | |
| 47 | public function getLinkAttribute() { |
| 48 | if ( ! ( $postId = LodashBasic::get( $this->block_context, 'postId' ) ) ) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | $authorId = get_post_field( 'post_author', $postId ); |
| 53 | if ( empty( $authorId ) ) { |
| 54 | return ''; |
| 55 | } |
| 56 | |
| 57 | return $this->getLinkAttributes( $authorId ); |
| 58 | } |
| 59 | |
| 60 | public function getLinkAttributes( $aAuthorId ) { |
| 61 | if ( ! $this->getAttribute( 'addLink' ) ) { |
| 62 | return array(); |
| 63 | } |
| 64 | |
| 65 | $authorLink = get_author_posts_url( $aAuthorId ); |
| 66 | $link = array( |
| 67 | 'value' => $authorLink, |
| 68 | ); |
| 69 | |
| 70 | return $link; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | Registry::registerBlock( |
| 75 | __DIR__, |
| 76 | PostAuthorAvatarBlock::class |
| 77 | ); |
| 78 |