| 1 |
<?php |
| 2 |
namespace ULTP\blocks; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class Post_Comment_Count { |
| 7 |
public function __construct() { |
| 8 |
add_action( 'init', array( $this, 'register' ) ); |
| 9 |
} |
| 10 |
public function get_attributes() { |
| 11 |
|
| 12 |
return array( |
| 13 |
'blockId' => '', |
| 14 |
|
| 15 |
/* |
| 16 |
============================ |
| 17 |
Post Comment Count Settings |
| 18 |
============================*/ |
| 19 |
'commentLabel' => true, |
| 20 |
'commentIconShow' => true, |
| 21 |
|
| 22 |
/* |
| 23 |
============================ |
| 24 |
Comment Count Label Settings |
| 25 |
============================*/ |
| 26 |
'commentLabelText' => 'comment ', |
| 27 |
'commentLabelAlign' => 'after', |
| 28 |
|
| 29 |
/* |
| 30 |
============================ |
| 31 |
Comment Count Icon Settings |
| 32 |
============================*/ |
| 33 |
'commentIconStyle' => 'commentCount1', |
| 34 |
|
| 35 |
/* |
| 36 |
============================ |
| 37 |
Advance Setting |
| 38 |
============================*/ |
| 39 |
|
| 40 |
// -------------------------- |
| 41 |
// Advanced Settings |
| 42 |
// -------------------------- |
| 43 |
'advanceId' => '', |
| 44 |
'advanceZindex' => '', |
| 45 |
'hideExtraLarge' => false, |
| 46 |
'hideDesktop' => false, |
| 47 |
'hideTablet' => false, |
| 48 |
'hideMobile' => false, |
| 49 |
'advanceCss' => '', |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
public function register() { |
| 54 |
register_block_type( |
| 55 |
'ultimate-post/post-comment-count', |
| 56 |
array( |
| 57 |
'editor_script' => 'ultp-blocks-editor-script', |
| 58 |
'editor_style' => 'ultp-blocks-editor-css', |
| 59 |
'render_callback' => array( $this, 'content' ), |
| 60 |
) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
public function content( $attr, $noAjax ) { |
| 65 |
$attr = wp_parse_args( $attr, $this->get_attributes() ); |
| 66 |
$block_name = 'post-comment-count'; |
| 67 |
$wrapper_before = $wrapper_after = $content = ''; |
| 68 |
|
| 69 |
$attr['className'] = isset( $attr['className'] ) && $attr['className'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['className'] ) : ''; |
| 70 |
$attr['align'] = isset( $attr['align'] ) && $attr['align'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['align'] ) : ''; |
| 71 |
$attr['advanceId'] = isset( $attr['advanceId'] ) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 72 |
$attr['blockId'] = isset( $attr['blockId'] ) ? sanitize_html_class( $attr['blockId'] ) : ''; |
| 73 |
$attr['commentLabelText'] = wp_kses( $attr['commentLabelText'], ultimate_post()->ultp_allowed_html_tags() ); |
| 74 |
|
| 75 |
$comment_count = get_post_field( 'comment_count', '' ); |
| 76 |
$wrapper_before .= '<div ' . ( $attr['advanceId'] ? 'id="' . $attr['advanceId'] . '" ' : '' ) . ' class="wp-block-ultimate-post-' . $block_name . ' ultp-block-' . $attr['blockId'] . ( $attr['className'] ? ' ' . $attr['className'] : '' ) . '' . ( $attr['align'] ? ' align' . $attr['align'] : '' ) . '">'; |
| 77 |
$wrapper_before .= '<div class="ultp-block-wrapper">'; |
| 78 |
$content .= '<span class="ultp-comment-count">'; |
| 79 |
if ( $attr['commentIconShow'] && ( $attr['commentIconStyle'] != '' ) ) { |
| 80 |
$content .= ultimate_post()->get_svg_icon( $attr['commentIconStyle'] ); |
| 81 |
} |
| 82 |
$content .= '<span>' . ( $comment_count ? $comment_count : 0 ) . '</span>'; |
| 83 |
if ( $attr['commentLabel'] ) { |
| 84 |
$content .= '<span class="ultp-comment-label">' . $attr['commentLabelText'] . '</span>'; |
| 85 |
} |
| 86 |
$content .= '</span>'; |
| 87 |
$wrapper_after .= '</div>'; |
| 88 |
$wrapper_after .= '</div>'; |
| 89 |
|
| 90 |
return $wrapper_before . $content . $wrapper_after; |
| 91 |
} |
| 92 |
} |
| 93 |
|