index.php
71 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Blocks; |
| 4 | |
| 5 | use Kubio\Core\Blocks\BlockBase; |
| 6 | use Kubio\Core\Registry; |
| 7 | |
| 8 | class LanguageSelector extends BlockBase { |
| 9 | const OUTER = 'outer'; |
| 10 | |
| 11 | public function mapPropsToElements() { |
| 12 | //$name = $this->getAttribute( 'name' ); |
| 13 | $content = ''; |
| 14 | if ( $this->getAttribute( 'show', false ) ) { |
| 15 | $content = $this->getContent(); |
| 16 | } |
| 17 | |
| 18 | return array( |
| 19 | self::OUTER => array( |
| 20 | 'innerHTML' => $content, |
| 21 | ), |
| 22 | ); |
| 23 | } |
| 24 | |
| 25 | public function serverSideRender() { |
| 26 | $content = $this->getContent(); |
| 27 | |
| 28 | return $content; |
| 29 | } |
| 30 | |
| 31 | public function getContent() { |
| 32 | |
| 33 | $is_editor = $this->getAttribute( 'isEditor', false ); |
| 34 | $display_as_flags = $this->getAttribute( 'displayAs', false ) === 'flags'; |
| 35 | $show_flags = $this->getAttribute( 'showFlags', false ); |
| 36 | $show_names = $this->getAttribute( 'showNames', false ); |
| 37 | |
| 38 | if ( $is_editor ) { |
| 39 | $post_id = $this->getAttribute( 'postId' ); |
| 40 | } else { |
| 41 | $post_id = get_the_ID(); |
| 42 | } |
| 43 | |
| 44 | $languages = pll_get_post_translations( $post_id ); |
| 45 | |
| 46 | ob_start(); |
| 47 | ?> |
| 48 | <div class="wp-block-kubio-language-selector__wrapper <?php echo $is_editor ? '--is-editor' : ''; ?>"> |
| 49 | <?php |
| 50 | pll_the_languages( |
| 51 | array( |
| 52 | 'dropdown' => ! $display_as_flags, |
| 53 | 'show_flags' => $display_as_flags && $show_flags, |
| 54 | 'show_names' => $display_as_flags && $show_names, |
| 55 | 'hide_current' => true, |
| 56 | ) |
| 57 | ) |
| 58 | ?> |
| 59 | </div> |
| 60 | <?php |
| 61 | $content = ob_get_clean(); |
| 62 | return $content; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | Registry::registerBlock( |
| 68 | __DIR__, |
| 69 | LanguageSelector::class |
| 70 | ); |
| 71 |