index.php
49 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 | |
| 9 | class PostAuthorBioBlock extends BlockBase { |
| 10 | |
| 11 | const TEXT = 'text'; |
| 12 | private $author_data_desc = null; |
| 13 | |
| 14 | public function computed() { |
| 15 | $this->author_data_desc = $this->getAuthorData( $this->block_context )->description; |
| 16 | return array( |
| 17 | 'showAuthorBio' => $this->author_data_desc ? true : false, |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | public function mapPropsToElements() { |
| 22 | return array( |
| 23 | self::TEXT => array_merge( |
| 24 | array( |
| 25 | 'innerHTML' => wp_kses_post( $this->author_data_desc ), |
| 26 | ) |
| 27 | ), |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | private function getAuthorData( $aBlockContext ) { |
| 32 | if ( ! ( $postId = LodashBasic::get( $aBlockContext, 'postId' ) ) ) { |
| 33 | return null; |
| 34 | } |
| 35 | |
| 36 | $authorId = get_post_field( 'post_author', $postId ); |
| 37 | if ( empty( $authorId ) ) { |
| 38 | return null; |
| 39 | } |
| 40 | |
| 41 | return get_userdata( $authorId ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | Registry::registerBlock( |
| 46 | __DIR__, |
| 47 | PostAuthorBioBlock::class |
| 48 | ); |
| 49 |