index.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Blocks; |
| 4 | |
| 5 | use Kubio\Core\Blocks\BlockBase; |
| 6 | use Kubio\Core\Registry; |
| 7 | use Kubio_Walker_Comment; |
| 8 | |
| 9 | class PostCommentsBlock extends BlockBase { |
| 10 | |
| 11 | const CONTAINER = 'commentsContainer'; |
| 12 | |
| 13 | |
| 14 | |
| 15 | static function getPostCommentsTemplate() { |
| 16 | return KUBIO_ROOT_DIR . '/lib/blog/comments.php'; |
| 17 | } |
| 18 | |
| 19 | public function serverSideRender() { |
| 20 | global $withcomments; |
| 21 | $withcomments = true; |
| 22 | |
| 23 | return $this->getPostComments( |
| 24 | array( |
| 25 | 'none' => $this->getAttribute( 'noCommentsTitle' ), |
| 26 | 'one' => $this->getAttribute( 'oneCommentTitle' ), |
| 27 | 'multiple' => $this->getAttribute( 'multipleComments' ), |
| 28 | 'disabled' => $this->getAttribute( 'commentsDisabled' ), |
| 29 | 'avatar_size' => $this->getAttribute( 'avatarSize' ), |
| 30 | ) |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | function getPostComments( $attrs = array() ) { |
| 35 | |
| 36 | if ( apply_filters( 'kubio/sandboxed_render', false ) ) { |
| 37 | return ''; |
| 38 | } |
| 39 | |
| 40 | $atts = array_merge( |
| 41 | array( |
| 42 | 'none' => __( 'No responses yet', 'kubio' ), |
| 43 | 'one' => __( 'One response', 'kubio' ), |
| 44 | 'multiple' => __( '{COMMENTS-COUNT} Responses', 'kubio' ), |
| 45 | 'disabled' => __( 'Comments are closed', 'kubio' ), |
| 46 | 'avatar_size' => 32, |
| 47 | ), |
| 48 | $attrs |
| 49 | ); |
| 50 | |
| 51 | if ( kubio_wpml_is_active() ) { |
| 52 | foreach ( $atts as $key => $value ) { |
| 53 | $atts[ $key ] = kubio_wpml_get_translated_string( $value ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | global $kubio_comments_data; |
| 58 | $kubio_comments_data = $atts; |
| 59 | |
| 60 | ob_start(); |
| 61 | add_filter( 'kubio/walker-comment', array( $this, 'getCommentWalker' ) ); |
| 62 | |
| 63 | add_filter( 'comments_template', array( PostCommentsBlock::class, 'getPostCommentsTemplate' ) ); |
| 64 | if ( comments_open( get_the_ID() ) ) { |
| 65 | comments_template(); |
| 66 | } else { |
| 67 | return sprintf( '<p class="comments-disabled">%s</p>', esc_attr( $atts['disabled'] ) ); |
| 68 | } |
| 69 | $content = ob_get_clean(); |
| 70 | |
| 71 | remove_filter( 'comments_template', array( PostCommentsBlock::class, 'getPostCommentsTemplate' ) ); |
| 72 | remove_filter( 'kubio/walker-comment', array( $this, 'getCommentWalker' ) ); |
| 73 | |
| 74 | return $content; |
| 75 | } |
| 76 | |
| 77 | public function getCommentWalker( $walker ) { |
| 78 | $migrations = $this->getAppliedMigrations(); |
| 79 | if ( in_array( 1, $migrations ) || in_array( '1', $migrations ) ) { |
| 80 | require_once KUBIO_ROOT_DIR . '/lib/blog/walker-comment.php'; |
| 81 | return new Kubio_Walker_Comment(); |
| 82 | } |
| 83 | |
| 84 | return $walker; |
| 85 | } |
| 86 | |
| 87 | public function mapPropsToElements() { |
| 88 | return array( |
| 89 | self::CONTAINER => array( |
| 90 | 'innerHTML' => $this->getPostComments( |
| 91 | array( |
| 92 | 'none' => wp_kses_post( $this->getAttribute( 'noCommentsTitle' ) ), |
| 93 | 'one' => wp_kses_post( $this->getAttribute( 'oneCommentTitle' ) ), |
| 94 | 'multiple' => wp_kses_post( $this->getAttribute( 'multipleComments' ) ), |
| 95 | 'disabled' => wp_kses_post( $this->getAttribute( 'commentsDisabled' ) ), |
| 96 | 'avatar_size' => wp_kses_post( $this->getAttribute( 'avatarSize' ) ), |
| 97 | 'html5' => true, |
| 98 | ) |
| 99 | ), |
| 100 | ), |
| 101 | ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | Registry::registerBlock( __DIR__, PostCommentsBlock::class ); |
| 106 |