| 1 |
<?php |
| 2 |
namespace ULTP\blocks; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class Post_Comments { |
| 7 |
public function __construct() { |
| 8 |
add_action( 'init', array( $this, 'register' ) ); |
| 9 |
} |
| 10 |
public function get_attributes() { |
| 11 |
|
| 12 |
return array( |
| 13 |
'blockId' => '', |
| 14 |
'layout' => 'layout1', |
| 15 |
/* |
| 16 |
============================ |
| 17 |
Post Comment Settings |
| 18 |
============================*/ |
| 19 |
// Comments Form Heading |
| 20 |
'replyHeading' => true, |
| 21 |
'leaveRepText' => 'Leave a Reply', |
| 22 |
|
| 23 |
/* |
| 24 |
============================ |
| 25 |
Comments Form Input |
| 26 |
============================*/ |
| 27 |
'inputPlaceHolder' => 'Express your thoughts, idea or write a feedback by clicking here & start an awesome comment', |
| 28 |
|
| 29 |
/* |
| 30 |
============================ |
| 31 |
Comments Form label style |
| 32 |
============================*/ |
| 33 |
'inputLabel' => true, |
| 34 |
'cmntInputText' => "Comment's", |
| 35 |
'nameInputText' => 'Name', |
| 36 |
'emailInputText' => 'Email', |
| 37 |
'webInputText' => 'Website Url', |
| 38 |
'cookiesEnable' => true, |
| 39 |
'cookiesText' => 'Save my name, email, and website in this browser for the next time I comment.', |
| 40 |
|
| 41 |
/* |
| 42 |
============================ |
| 43 |
Submit Button Style |
| 44 |
============================*/ |
| 45 |
'subBtnText' => 'Post Comment', |
| 46 |
|
| 47 |
/* |
| 48 |
============================ |
| 49 |
Comment Reply Style |
| 50 |
============================*/ |
| 51 |
// Title and total Comment Count |
| 52 |
'replyText' => 'Comments Text', |
| 53 |
'commentCount' => true, |
| 54 |
|
| 55 |
// -------------------------- |
| 56 |
// Advanced Settings |
| 57 |
// -------------------------- |
| 58 |
'advanceId' => '', |
| 59 |
'advanceZindex' => '', |
| 60 |
'hideExtraLarge' => false, |
| 61 |
'hideDesktop' => false, |
| 62 |
'hideTablet' => false, |
| 63 |
'hideMobile' => false, |
| 64 |
'advanceCss' => '', |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
public function register() { |
| 69 |
register_block_type( |
| 70 |
'ultimate-post/post-comments', |
| 71 |
array( |
| 72 |
'editor_script' => 'ultp-blocks-editor-script', |
| 73 |
'editor_style' => 'ultp-blocks-editor-css', |
| 74 |
'render_callback' => array( $this, 'content' ), |
| 75 |
) |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
public function content( $attr, $noAjax ) { |
| 80 |
$attr = wp_parse_args( $attr, $this->get_attributes() ); |
| 81 |
$block_name = 'post-comments'; |
| 82 |
$wrapper_before = $wrapper_after = $wrapper_content = ''; |
| 83 |
|
| 84 |
if ( is_single() ) { |
| 85 |
|
| 86 |
$attr['className'] = isset( $attr['className'] ) && $attr['className'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['className'] ) : ''; |
| 87 |
$attr['align'] = isset( $attr['align'] ) && $attr['align'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['align'] ) : ''; |
| 88 |
$attr['advanceId'] = isset( $attr['advanceId'] ) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 89 |
$attr['blockId'] = isset( $attr['blockId'] ) ? sanitize_html_class( $attr['blockId'] ) : ''; |
| 90 |
$attr['layout'] = sanitize_html_class( $attr['layout'] ); |
| 91 |
|
| 92 |
$allowed_html_tags = ultimate_post()->ultp_allowed_html_tags(); |
| 93 |
$attr['nameInputText'] = wp_kses( $attr['nameInputText'], $allowed_html_tags ); |
| 94 |
$attr['emailInputText'] = wp_kses( $attr['emailInputText'], $allowed_html_tags ); |
| 95 |
$attr['webInputText'] = wp_kses( $attr['webInputText'], $allowed_html_tags ); |
| 96 |
$attr['cmntInputText'] = wp_kses( $attr['cmntInputText'], $allowed_html_tags ); |
| 97 |
$attr['cookiesText'] = wp_kses( $attr['cookiesText'], $allowed_html_tags ); |
| 98 |
$attr['inputPlaceHolder'] = wp_kses( $attr['inputPlaceHolder'], $allowed_html_tags ); |
| 99 |
$attr['subBtnText'] = wp_kses( $attr['subBtnText'], $allowed_html_tags ); |
| 100 |
$attr['leaveRepText'] = wp_kses( $attr['leaveRepText'], $allowed_html_tags ); |
| 101 |
$attr['replyText'] = wp_kses( $attr['replyText'], $allowed_html_tags ); |
| 102 |
|
| 103 |
$commenter = wp_get_current_commenter(); |
| 104 |
$req = get_option( 'require_name_email' ); |
| 105 |
$aria_req = ( $req ? " aria-required='true'" : '' ); |
| 106 |
|
| 107 |
$auth_label = $attr['inputLabel'] ? '<label for="author">' . $attr['nameInputText'] . ( $req ? '<span class="required">*</span>' : '' ) . '</label>' : ''; |
| 108 |
$email_label = $attr['inputLabel'] ? '<label for="email">' . $attr['emailInputText'] . ( $req ? '<span class="required">*</span>' : '' ) . '</label>' : ''; |
| 109 |
$url_label = $attr['inputLabel'] ? '<label for="url">' . $attr['webInputText'] . '</label>' : ''; |
| 110 |
$comment_label = $attr['inputLabel'] ? '<label for="comment">' . $attr['cmntInputText'] . '</label>' : ''; |
| 111 |
$cookies_label = $attr['cookiesEnable'] ? '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"/><label for="wp-comment-cookies-consent">' . $attr['cookiesText'] . '</label></p>' : ''; |
| 112 |
|
| 113 |
$comments_args = array( |
| 114 |
'comment_field' => '<div class="comment-form-comment ultp-comment-input ultp-field-control">' . wp_kses_post( $comment_label ) . '<textarea class="hi" id="comment" name="comment" placeholder="' . esc_attr( $attr['inputPlaceHolder'] ) . '" cols="45" rows="8" aria-required="true"></textarea></div>', |
| 115 |
'fields' => apply_filters( |
| 116 |
'comment_form_default_fields', |
| 117 |
array( |
| 118 |
'author' => '<div class="ultp-field-control ultp-comment-name">' . wp_kses_post( $auth_label ) . '<input id="author" placeholder="Your name" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></div>', |
| 119 |
'email' => '<div class="ultp-field-control ultp-comment-email">' . wp_kses_post( $email_label ) . '<input id="email" placeholder="your.email@example.com" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></div>', |
| 120 |
'url' => '<div class="ultp-field-control ultp-comment-website">' . wp_kses_post( $url_label ) . '<input id="url" name="url" placeholder="https://yourwebsite.com" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></div>', |
| 121 |
'cookies' => $cookies_label, |
| 122 |
) |
| 123 |
), |
| 124 |
'class_submit' => 'ultp-comment-btn', |
| 125 |
'comment_notes_after' => '', |
| 126 |
'submit_button' => '<input name="%1$s" type="submit" id="%2$s" class="%3$s ultp-comment-btn" value="' . $attr['subBtnText'] . '" />', |
| 127 |
'class_form' => 'ultp-comment-form', |
| 128 |
'title_reply' => ( $attr['replyHeading'] ? '<div class="crunchify-text ultp-comments-title">' . $attr['leaveRepText'] . '</div>' : '' ), |
| 129 |
'class_container' => 'ultp-comment-form-container', |
| 130 |
); |
| 131 |
|
| 132 |
$arg = array( |
| 133 |
'walker' => null, |
| 134 |
'max_depth' => '', |
| 135 |
'style' => 'ul', |
| 136 |
'callback' => null, |
| 137 |
'end-callback' => null, |
| 138 |
'type' => 'comment', |
| 139 |
'page' => '', |
| 140 |
'per_page' => '', |
| 141 |
'avatar_size' => 32, |
| 142 |
'reverse_top_level' => true, |
| 143 |
'reverse_children' => '', |
| 144 |
'format' => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml', |
| 145 |
'short_ping' => false, |
| 146 |
'echo' => true, |
| 147 |
); |
| 148 |
|
| 149 |
$moderation_enable = get_option( 'comment_moderation' ); |
| 150 |
$comments = get_comments( |
| 151 |
array( |
| 152 |
'post_id' => get_the_ID(), |
| 153 |
'status' => $moderation_enable ? 'approve' : 'all', |
| 154 |
) |
| 155 |
); |
| 156 |
|
| 157 |
$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'] : '' ) . '">'; |
| 158 |
$wrapper_before .= '<div class="ultp-block-wrapper ultp-block-comments ultp-comments-' . $attr['layout'] . '">'; |
| 159 |
if ( $attr['commentCount'] && count( $comments ) > 0 ) { |
| 160 |
$wrapper_content .= '<div class="ultp-comment-reply-heading">'; |
| 161 |
$wrapper_content .= count( $comments ) . ' ' . $attr['replyText']; |
| 162 |
$wrapper_content .= '</div>'; |
| 163 |
} |
| 164 |
$wrapper_content .= '<div class="ultp-builder-comment-reply">'; |
| 165 |
ob_start(); |
| 166 |
wp_list_comments( $arg, $comments ); |
| 167 |
$wrapper_content .= ob_get_clean(); |
| 168 |
$wrapper_content .= '</div>'; |
| 169 |
$wrapper_content .= '<div class="ultp-builder-comments">'; |
| 170 |
ob_start(); |
| 171 |
comment_form( $comments_args ); |
| 172 |
$wrapper_content .= ob_get_clean(); |
| 173 |
$wrapper_content .= '</div>'; |
| 174 |
$wrapper_after .= '</div>'; |
| 175 |
$wrapper_after .= '</div>'; |
| 176 |
} |
| 177 |
|
| 178 |
return $wrapper_before . $wrapper_content . $wrapper_after; |
| 179 |
} |
| 180 |
} |
| 181 |
|