index.php
62 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Blocks; |
| 4 | |
| 5 | use Kubio\Core\Blocks\BlockBase; |
| 6 | use Kubio\Core\Registry; |
| 7 | |
| 8 | class PostCommentsFormBlock extends BlockBase { |
| 9 | |
| 10 | const CONTAINER = 'container'; |
| 11 | |
| 12 | function disableCurrentUser() { |
| 13 | return false; |
| 14 | } |
| 15 | |
| 16 | public function serverSideRender() { |
| 17 | $user = wp_get_current_user(); |
| 18 | wp_set_current_user( 0 ); |
| 19 | add_filter( 'determine_current_user', array( $this, 'disableCurrentUser' ), PHP_INT_MAX ); |
| 20 | |
| 21 | $content = $this->renderForm(); |
| 22 | |
| 23 | if ( empty( $content ) ) { |
| 24 | $content = sprintf( |
| 25 | '<p class="comments-disabled"><p>%s</p></p>', |
| 26 | __( 'Comments are closed. This Post Comments Form block is still present here but is not visible on front-end.', 'kubio' ) |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | if ( $user ) { |
| 31 | wp_set_current_user( $user->ID ); |
| 32 | } |
| 33 | remove_filter( 'determine_current_user', array( $this, 'disableCurrentUser' ), PHP_INT_MAX ); |
| 34 | |
| 35 | return $content; |
| 36 | } |
| 37 | |
| 38 | function renderForm() { |
| 39 | |
| 40 | if ( $this->isSandboxRender() ) { |
| 41 | return ''; |
| 42 | } |
| 43 | |
| 44 | ob_start(); |
| 45 | if ( comments_open( get_the_ID() ) ) { |
| 46 | comment_form( get_the_ID() ); |
| 47 | } |
| 48 | |
| 49 | return ob_get_clean(); |
| 50 | } |
| 51 | |
| 52 | public function mapPropsToElements() { |
| 53 | return array( |
| 54 | self::CONTAINER => array( |
| 55 | 'innerHTML' => $this->renderForm(), |
| 56 | ), |
| 57 | ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | Registry::registerBlock( __DIR__, PostCommentsFormBlock::class ); |
| 62 |