index.php
205 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Blocks; |
| 4 | |
| 5 | use Kubio\Core\Blocks\BlockBase; |
| 6 | use Kubio\Core\GlobalElements\Icon; |
| 7 | use Kubio\Core\LodashBasic; |
| 8 | use Kubio\Core\Registry; |
| 9 | |
| 10 | class PostMetaBlock extends BlockBase { |
| 11 | |
| 12 | |
| 13 | const CONTAINER = 'metaDataContainer'; |
| 14 | |
| 15 | private $postId; |
| 16 | private $metaFunctionsMap = array(); |
| 17 | |
| 18 | public function __construct( $block, $autoload = true, $context = array() ) { |
| 19 | parent::__construct( $block, $autoload, $context ); |
| 20 | $this->metaFunctionsMap = array( |
| 21 | 'author' => array( |
| 22 | 'content' => 'getAuthorContent', |
| 23 | 'url' => 'getAuthorUrl', |
| 24 | ), |
| 25 | 'date' => array( |
| 26 | 'content' => 'getDateContent', |
| 27 | 'url' => 'getDateUrl', |
| 28 | ), |
| 29 | 'time' => array( |
| 30 | 'content' => 'getTimeContent', |
| 31 | 'url' => 'getTimeUrl', |
| 32 | ), |
| 33 | 'comments' => array( |
| 34 | 'content' => 'getCommentsContent', |
| 35 | 'url' => 'getCommentsUrl', |
| 36 | ), |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | public function mapPropsToElements() { |
| 41 | $this->postId = LodashBasic::get( $this->block_context, 'postId' ); |
| 42 | $map = array(); |
| 43 | $map[ self::CONTAINER ] = array( |
| 44 | 'innerHTML' => $this->getMetaItems(), |
| 45 | ); |
| 46 | |
| 47 | return $map; |
| 48 | } |
| 49 | |
| 50 | private function getMetaItems() { |
| 51 | $meta = $this->getAttribute( 'metadata' ); |
| 52 | $metadata = $this->parseItems( $meta ); |
| 53 | $show_icons = $this->getAttribute( 'showIcons' ); |
| 54 | $separator = $this->getAttribute( 'separator' ); |
| 55 | $is_last_index = count( $metadata ) - 1; |
| 56 | ob_start(); |
| 57 | |
| 58 | foreach ( $metadata as $index => $item ) { |
| 59 | |
| 60 | $item_value = LodashBasic::get( $item, 'value' ); |
| 61 | $icon = new Icon( 'span', array( 'name' => $item['icon'] ) ); |
| 62 | |
| 63 | ?> |
| 64 | <span class="metadata-item"> |
| 65 | <?php if ( $item['prefix'] !== '' ) : ?> |
| 66 | <span class="metadata-prefix"> <?php echo esc_html( $item['prefix'] ); ?> </span> |
| 67 | <?php endif; ?> |
| 68 | |
| 69 | <a href="<?php echo esc_url( $this->getHref( $item_value ) ); ?>"> |
| 70 | <?php if ( $show_icons ) : ?> |
| 71 | <?php echo wp_kses_post( $icon ); ?> |
| 72 | <?php endif; ?> |
| 73 | <?php echo wp_kses_post( $this->getItemContent( $item_value ) ); ?> |
| 74 | </a> |
| 75 | |
| 76 | <?php if ( $item['suffix'] != '' ) : ?> |
| 77 | <span class="metadata-suffix"> <?php echo esc_html( $item['suffix'] ); ?> </span> |
| 78 | <?php endif; ?> |
| 79 | </span> |
| 80 | <?php if ( $is_last_index > $index && $separator != '' ) : ?> |
| 81 | <span class="metadata-separator"> <?php echo esc_html( $separator ); ?> </span> |
| 82 | <?php endif; ?> |
| 83 | |
| 84 | <?php |
| 85 | } |
| 86 | |
| 87 | return ob_get_clean(); |
| 88 | } |
| 89 | |
| 90 | private function parseItems( $items ) { |
| 91 | $sorted_items = array(); |
| 92 | foreach ( $items as $item ) { |
| 93 | $check_value = LodashBasic::get( $item, 'check' ); |
| 94 | if ( $check_value === 'true' || $check_value === true ) { |
| 95 | $sorted_items[] = $item; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return $sorted_items; |
| 100 | } |
| 101 | |
| 102 | private function getHref( $item_name ) { |
| 103 | |
| 104 | return $this->getUrl( $item_name ); |
| 105 | } |
| 106 | |
| 107 | public function getUrl( $type, $arg = array() ) { |
| 108 | $function_name = LodashBasic::get( $this->metaFunctionsMap, array( $type, 'url' ) ); |
| 109 | if ( $function_name ) { |
| 110 | return call_user_func( array( $this, $function_name ), $arg ); |
| 111 | } |
| 112 | |
| 113 | return ''; |
| 114 | } |
| 115 | |
| 116 | private function getItemContent( $item_name ) { |
| 117 | |
| 118 | $atts = array(); |
| 119 | if ( $item_name === 'date' ) { |
| 120 | $atts['dateformat'] = $this->getAttribute( 'dateFormat' ); |
| 121 | } |
| 122 | |
| 123 | return $this->getContent( $item_name, $atts ); |
| 124 | } |
| 125 | |
| 126 | public function getContent( $type, $arg ) { |
| 127 | $function_name = LodashBasic::get( $this->metaFunctionsMap, array( $type, 'content' ) ); |
| 128 | if ( $function_name ) { |
| 129 | return call_user_func( array( $this, $function_name ), $arg ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | public function serverSideRender() { |
| 134 | $this->postId = $this->getAttribute( 'editorContext.postId' ); |
| 135 | |
| 136 | return $this->getMetaItems(); |
| 137 | } |
| 138 | |
| 139 | public function getAuthorContent() { |
| 140 | $author_id = get_post_field( 'post_author', $this->postId ); |
| 141 | $content = get_the_author_meta( 'display_name', $author_id ); |
| 142 | |
| 143 | return apply_filters( 'kubio_post_meta_author_content', $content ); |
| 144 | } |
| 145 | |
| 146 | public function getAuthorUrl() { |
| 147 | $author_id = get_post_field( 'post_author', $this->postId ); |
| 148 | $url = get_author_posts_url( $author_id ); |
| 149 | |
| 150 | return apply_filters( 'kubio_post_meta_author_url', $url ); |
| 151 | } |
| 152 | |
| 153 | public function getDateContent( $atts ) { |
| 154 | $atts = shortcode_atts( |
| 155 | array( |
| 156 | 'dateformat' => '', |
| 157 | ), |
| 158 | $atts |
| 159 | ); |
| 160 | $format = apply_filters( 'kubio_post_meta_date_format', $atts['dateformat'] ); |
| 161 | $date = get_the_date( $format, $this->postId ); |
| 162 | $content = apply_filters( 'kubio_post_meta_date_content', $date, $format ); |
| 163 | |
| 164 | return $content; |
| 165 | } |
| 166 | |
| 167 | public function getDateUrl() { |
| 168 | $id = $this->postId; |
| 169 | $link = get_day_link( |
| 170 | get_post_time( 'Y', false, $id, true ), |
| 171 | get_post_time( 'm', false, $id, true ), |
| 172 | get_post_time( 'j', false, $id, true ) |
| 173 | ); |
| 174 | |
| 175 | return apply_filters( 'kubio_post_meta_date_url', $link ); |
| 176 | } |
| 177 | |
| 178 | public function getTimeContent() { |
| 179 | $time = get_the_time( '', $this->postId ); |
| 180 | |
| 181 | return apply_filters( 'kubio_post_meta_time_content', $time ); |
| 182 | } |
| 183 | |
| 184 | public function getTimeUrl() { |
| 185 | $time = ''; |
| 186 | |
| 187 | return apply_filters( 'kubio_post_meta_time_url', $time ); |
| 188 | } |
| 189 | |
| 190 | public function getCommentsUrl() { |
| 191 | $url = get_comments_link( $this->postId ); |
| 192 | |
| 193 | return apply_filters( 'kubio_post_meta_comments_url', $url ); |
| 194 | } |
| 195 | |
| 196 | public function getCommentsContent() { |
| 197 | $content = get_comments_number( $this->postId ); |
| 198 | |
| 199 | return apply_filters( 'kubio_post_meta_comments_content', $content ); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | |
| 204 | Registry::registerBlock( __DIR__, PostMetaBlock::class ); |
| 205 |