| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* bbPress Replies |
| 5 |
*/ |
| 6 |
add_filter('bbp_show_lead_topic', function () { |
| 7 |
$show_lead[] = 'true'; |
| 8 |
return $show_lead; |
| 9 |
}); |
| 10 |
|
| 11 |
/** |
| 12 |
* bbPress User Role |
| 13 |
*/ |
| 14 |
if ( ! function_exists( 'forumax_bbp_user_role_icon' ) ) { |
| 15 |
function forumax_bbp_user_role_icon() { |
| 16 |
$author_role = bbp_get_user_display_role( bbp_get_reply_author_id() ); |
| 17 |
switch ( $author_role ) { |
| 18 |
case 'Keymaster': |
| 19 |
get_image_from_settings( 'keymaster_icon' ); |
| 20 |
break; |
| 21 |
case 'Moderator': |
| 22 |
get_image_from_settings( 'moderator_icon' ); |
| 23 |
break; |
| 24 |
case 'Participant': |
| 25 |
get_image_from_settings( 'participant_icon' ); |
| 26 |
break; |
| 27 |
case 'Spectator': |
| 28 |
get_image_from_settings( 'spectator_icon' ); |
| 29 |
break; |
| 30 |
case 'Blocked': |
| 31 |
get_image_from_settings( 'blocked_icon' ); |
| 32 |
break; |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get author role |
| 39 |
*/ |
| 40 |
if (!function_exists('forumax_get_bbp_user_role')) { |
| 41 |
function forumax_get_bbp_user_role() { |
| 42 |
$role = bbp_get_topic_author_role( array( |
| 43 |
'topic_id' => get_the_ID(), |
| 44 |
'before' => '', |
| 45 |
'after' => '', |
| 46 |
)); |
| 47 |
return $role; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get topic reply count |
| 53 |
*/ |
| 54 |
if (!function_exists('forumax_topic_reply_count')) { |
| 55 |
function forumax_topic_reply_count() { |
| 56 |
$get_reply = get_post_meta( get_the_ID(), '_bbp_reply_count', true ); |
| 57 |
$_reply_count = isset( $get_reply ) && !empty( $get_reply ) ? $get_reply : 0; |
| 58 |
echo esc_html( $_reply_count ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Display bbp_reply_admin_links() as a dropdown list. The dropdown will be shown on click on a vertical three dots icon. |
| 64 |
* Create a new function to display the dropdown. |
| 65 |
*/ |
| 66 |
if ( ! function_exists( 'forumax_get_reply_admin_links' ) ) { |
| 67 |
function forumax_get_reply_admin_links( $args = array() ) { |
| 68 |
|
| 69 |
// Parse arguments against default values |
| 70 |
$r = bbp_parse_args( $args, array( |
| 71 |
'id' => 0, |
| 72 |
'before' => '<div class="bbp-admin-links-dropdown">', |
| 73 |
'after' => '</div>', |
| 74 |
'sep' => '', |
| 75 |
'links' => array() |
| 76 |
), 'get_reply_admin_links' ); |
| 77 |
|
| 78 |
$r['id'] = bbp_get_reply_id( $r['id'] ); |
| 79 |
|
| 80 |
// If post is a topic, return the topic admin links instead |
| 81 |
if ( bbp_is_topic( $r['id'] ) ) { |
| 82 |
return forumax_get_topic_admin_links( $args ); |
| 83 |
} |
| 84 |
|
| 85 |
// If post is not a reply, return |
| 86 |
if ( ! bbp_is_reply( $r['id'] ) ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// If topic is trashed, do not show admin links |
| 91 |
if ( bbp_is_topic_trash( bbp_get_reply_topic_id( $r['id'] ) ) ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
// If no links were passed, default to the standard |
| 96 |
if ( empty( $r['links'] ) ) { |
| 97 |
$r['links'] = apply_filters( 'bbp_reply_admin_links', array( |
| 98 |
'edit' => bbp_get_reply_edit_link ( $r ), |
| 99 |
'move' => bbp_get_reply_move_link ( $r ), |
| 100 |
'split' => bbp_get_topic_split_link ( $r ), |
| 101 |
'trash' => bbp_get_reply_trash_link ( $r ), |
| 102 |
'spam' => bbp_get_reply_spam_link ( $r ), |
| 103 |
'approve' => bbp_get_reply_approve_link( $r ), |
| 104 |
'reply' => bbp_get_reply_to_link ( $r ) |
| 105 |
), $r['id'] ); |
| 106 |
} |
| 107 |
|
| 108 |
// See if links need to be unset |
| 109 |
$reply_status = bbp_get_reply_status( $r['id'] ); |
| 110 |
if ( in_array( $reply_status, array( bbp_get_spam_status_id(), bbp_get_trash_status_id(), bbp_get_pending_status_id() ), true ) ) { |
| 111 |
|
| 112 |
// Spam link shouldn't be visible on trashed topics |
| 113 |
if ( bbp_get_trash_status_id() === $reply_status ) { |
| 114 |
unset( $r['links']['spam'] ); |
| 115 |
|
| 116 |
// Trash link shouldn't be visible on spam topics |
| 117 |
} elseif ( bbp_get_spam_status_id() === $reply_status ) { |
| 118 |
unset( $r['links']['trash'] ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// Build our dropdown menu structure |
| 123 |
$menu = '<select name="reply-admin-links" class="nice-select bbp-admin-dropdown-menu" onchange="if(this.value) window.location.href=this.value;">'; |
| 124 |
|
| 125 |
// Process each link as a menu item |
| 126 |
$menu .= '<option value="" disabled selected>' . esc_html__( 'Select an option', 'forumax' ) . '</option>'; |
| 127 |
foreach ( array_filter( $r['links'] ) as $link ) { |
| 128 |
// Extract href attribute from the link |
| 129 |
preg_match('/href=(["\'])([^"\']+)\\1/', $link, $matches); |
| 130 |
$href = !empty($matches[2]) ? $matches[2] : ''; |
| 131 |
|
| 132 |
// Add the option with value attribute containing the URL |
| 133 |
$menu .= '<option value="' . esc_url($href) . '" class="bbp-admin-dropdown-item">' . wp_strip_all_tags($link) . '</option>'; |
| 134 |
} |
| 135 |
|
| 136 |
$menu .= '</select>'; |
| 137 |
|
| 138 |
$retval = $r['before'] . $menu . $r['after']; |
| 139 |
|
| 140 |
// Filter & return |
| 141 |
if ( is_user_logged_in() ) { |
| 142 |
return apply_filters( 'forumax_bbp_get_reply_admin_links', $retval, $r, $args ); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Similar function for topics - to handle the case where a topic is passed to the reply function |
| 149 |
*/ |
| 150 |
if ( ! function_exists( 'forumax_get_topic_admin_links' ) ) { |
| 151 |
function forumax_get_topic_admin_links( $args = array() ) { |
| 152 |
// Parse arguments against default values |
| 153 |
$r = bbp_parse_args( $args, array( |
| 154 |
'id' => 0, |
| 155 |
'before' => '<div class="bbp-admin-links-dropdown">', |
| 156 |
'after' => '</div>', |
| 157 |
'sep' => '', |
| 158 |
'links' => array() |
| 159 |
), 'get_topic_admin_links' ); |
| 160 |
|
| 161 |
$r['id'] = bbp_get_topic_id( $r['id'] ); |
| 162 |
|
| 163 |
// If not a topic, then return |
| 164 |
if ( ! bbp_is_topic( $r['id'] ) ) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
// If no links were passed, get the default ones |
| 169 |
if ( empty( $r['links'] ) ) { |
| 170 |
$r['links'] = apply_filters( 'bbp_topic_admin_links', array( |
| 171 |
'edit' => bbp_get_topic_edit_link ( $r ), |
| 172 |
'close' => bbp_get_topic_close_link ( $r ), |
| 173 |
'stick' => bbp_get_topic_stick_link ( $r ), |
| 174 |
'merge' => bbp_get_topic_merge_link ( $r ), |
| 175 |
'trash' => bbp_get_topic_trash_link ( $r ), |
| 176 |
'spam' => bbp_get_topic_spam_link ( $r ), |
| 177 |
'approve' => bbp_get_topic_approve_link( $r ), |
| 178 |
'reply' => bbp_get_topic_reply_link ( $r ) |
| 179 |
), $r['id'] ); |
| 180 |
} |
| 181 |
|
| 182 |
// See if links need to be unset |
| 183 |
$topic_status = bbp_get_topic_status( $r['id'] ); |
| 184 |
if ( in_array( $topic_status, array( bbp_get_spam_status_id(), bbp_get_trash_status_id(), bbp_get_pending_status_id() ), true ) ) { |
| 185 |
|
| 186 |
// Spam link shouldn't be visible on trashed topics |
| 187 |
if ( bbp_get_trash_status_id() === $topic_status ) { |
| 188 |
unset( $r['links']['spam'] ); |
| 189 |
|
| 190 |
// Trash link shouldn't be visible on spam topics |
| 191 |
} elseif ( bbp_get_spam_status_id() === $topic_status ) { |
| 192 |
unset( $r['links']['trash'] ); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
// Build our dropdown menu structure |
| 197 |
$menu = '<select name="topic-admin-links" class="nice-select bbp-admin-dropdown-menu" onchange="if(this.value) window.location.href=this.value;">'; |
| 198 |
$menu .= '<option value="" disabled selected>Select an option</option>'; |
| 199 |
|
| 200 |
// Process each link as a menu item |
| 201 |
foreach ( array_filter( $r['links'] ) as $link ) { |
| 202 |
// Extract href attribute from the link |
| 203 |
preg_match('/href=(["\'])([^"\']+)\\1/', $link, $matches); |
| 204 |
$href = !empty($matches[2]) ? $matches[2] : ''; |
| 205 |
|
| 206 |
// Add the option with value attribute containing the URL |
| 207 |
$menu .= '<option value="' . esc_url($href) . '" class="bbp-admin-dropdown-item">' . wp_strip_all_tags($link) . '</option>'; |
| 208 |
} |
| 209 |
|
| 210 |
$menu .= '</select>'; |
| 211 |
|
| 212 |
$retval = $r['before'] . $menu . $r['after']; |
| 213 |
|
| 214 |
// Filter & return |
| 215 |
return apply_filters( 'forumax_bbp_get_topic_admin_links', $retval, $r, $args ); |
| 216 |
} |
| 217 |
} |
| 218 |
/** |
| 219 |
* Enqueue the script for sorting bbPress replies |
| 220 |
*/ |
| 221 |
if ( ! function_exists( 'forumax_enqueue_bbp_replies_sorting_script' ) ) { |
| 222 |
function forumax_enqueue_bbp_replies_sorting_script() { |
| 223 |
wp_localize_script('bbp-replies-sorting', 'bbp_reply_ajax_obj', array( |
| 224 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 225 |
'bbp_topic_id' => get_the_ID(), |
| 226 |
)); |
| 227 |
} |
| 228 |
add_action('wp_enqueue_scripts', 'forumax_enqueue_bbp_replies_sorting_script'); |
| 229 |
} |
| 230 |
|
| 231 |
if ( ! function_exists( 'forumax_ajax_sort_bbp_replies' ) ) { |
| 232 |
function forumax_ajax_sort_bbp_replies() { |
| 233 |
if (empty($_POST['topic_id'])) { |
| 234 |
wp_send_json_error(['message' => 'No topic ID provided.']); |
| 235 |
} |
| 236 |
$topic_id = intval($_POST['topic_id']); |
| 237 |
$sort = isset($_POST['sort']) ? sanitize_text_field($_POST['sort']) : 'desc'; |
| 238 |
// Set $_GET['sort'] for the filter to pick up |
| 239 |
$_GET['sort'] = $sort; |
| 240 |
|
| 241 |
// Start output buffering to capture the replies loop HTML |
| 242 |
ob_start(); |
| 243 |
// Set up bbPress replies loop manually for AJAX context |
| 244 |
$replies_args = array( |
| 245 |
'post_parent' => $topic_id, |
| 246 |
'post_type' => bbp_get_reply_post_type(), |
| 247 |
'orderby' => ($sort === 'vote' ? 'meta_value_num' : 'date'), |
| 248 |
'order' => ($sort === 'asc' ? 'ASC' : 'DESC'), |
| 249 |
'meta_key' => ($sort === 'vote' ? 'bbp_voting_score' : ''), |
| 250 |
'posts_per_page' => -1, |
| 251 |
); |
| 252 |
if (bbp_has_replies($replies_args)) { |
| 253 |
while (bbp_replies()) : bbp_the_reply(); |
| 254 |
$template_file = plugin_dir_path( __FILE__ ) . '../templates/loop-single-reply.php'; |
| 255 |
if ( file_exists( $template_file ) ) { |
| 256 |
include $template_file; |
| 257 |
} |
| 258 |
endwhile; |
| 259 |
} else { |
| 260 |
echo '<div>No replies found.</div>'; |
| 261 |
} |
| 262 |
$html = ob_get_clean(); |
| 263 |
|
| 264 |
wp_send_json_success(['html' => $html]); |
| 265 |
} |
| 266 |
add_action('wp_ajax_sort_bbp_replies', 'forumax_ajax_sort_bbp_replies'); |
| 267 |
add_action('wp_ajax_nopriv_sort_bbp_replies', 'forumax_ajax_sort_bbp_replies'); |
| 268 |
} |