| @@ -1,419 +1,443 @@ | ||
| 1 | -<?php | |
| 2 | -namespace features; | |
| 3 | - | |
| 4 | -class bbp_solved_topic { | |
| 5 | - | |
| 6 | - /** | |
| 7 | - * The capability required to view private posts. | |
| 8 | - * | |
| 9 | - * @since 1.0 | |
| 10 | - * | |
| 11 | - * @var string $capability | |
| 12 | - */ | |
| 13 | - public $capability = 'moderate'; | |
| 14 | - | |
| 15 | - /* | |
| 16 | - --------------------------------------------* | |
| 17 | - * Constructor | |
| 18 | - *--------------------------------------------*/ | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * Initializes the plugin by setting filters, and administration functions. | |
| 22 | - */ | |
| 23 | - function __construct() { | |
| 24 | - add_action( 'plugins_loaded', [ $this, 'filter_capability' ] ); | |
| 25 | - | |
| 26 | - // show the checkboxes | |
| 27 | - add_action( 'bbp_theme_before_reply_form_submit_wrapper', [ $this, 'reply_checkbox' ] ); | |
| 28 | - add_action( 'bbp_theme_before_topic_form_submit_wrapper', [ $this, 'topic_checkbox' ] ); | |
| 29 | - | |
| 30 | - // save the reply state | |
| 31 | - add_action( 'bbp_new_reply', [ $this, 'update_reply' ], 0, 6 ); | |
| 32 | - add_action( 'bbp_edit_reply', [ $this, 'update_reply' ], 0, 6 ); | |
| 33 | - | |
| 34 | - // save the topic state | |
| 35 | - add_action( 'bbp_new_topic', [ $this, 'update_topic' ], 0, 6 ); | |
| 36 | - add_action( 'bbp_edit_topic', [ $this, 'update_topic' ], 0, 6 ); | |
| 37 | - | |
| 38 | - // reply content filter | |
| 39 | - add_filter( 'bbp_get_reply_excerpt', [ $this, 'reply_content_filter' ], 999, 2 ); | |
| 40 | - add_filter( 'bbp_get_reply_content', [ $this, 'reply_content_filter' ], 999, 2 ); | |
| 41 | - | |
| 42 | - add_filter( 'bbp_theme_before_topic_title', [ $this, 'solved_badge' ], 999, 2 ); | |
| 43 | - add_filter( 'bbp_topic_admin_links', [ $this, 'add_topic_admin_link' ], 10, 2 ); | |
| 44 | - | |
| 45 | - // add a class name indicating the read status | |
| 46 | - add_filter( 'post_class', [ $this, 'reply_post_class' ] ); | |
| 47 | - } | |
| 48 | - | |
| 49 | - /** | |
| 50 | - * Admin links | |
| 51 | - */ | |
| 52 | - public function add_topic_admin_link( $links, $topic_id ) { | |
| 53 | - | |
| 54 | - $topic_author = bbp_get_topic_author_id( bbp_get_topic_id() ); | |
| 55 | - if ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) { | |
| 56 | - if ( isset( $_GET['unsolve_topic'] ) ) { | |
| 57 | - $this->unsolve_topic( bbp_get_topic_id() ); | |
| 58 | - $links['solved'] = '<a href="' . bbp_get_topic_permalink() . '?solve_topic">' . esc_html__( 'Mark as solved', 'ama-core' ) . '</a>'; | |
| 59 | - } elseif ( isset( $_GET['solve_topic'] ) ) { | |
| 60 | - $this->solve_topic( bbp_get_topic_id() ); | |
| 61 | - $links['unsolved'] = '<a href="' . bbp_get_topic_permalink() . '?unsolve_topic">' . esc_html__( 'Mark as unsolved', 'ama-core' ) . '</a>'; | |
| 62 | - } else { | |
| 63 | - if ( $this->is_solved( bbp_get_topic_id() ) ) { | |
| 64 | - $links['unsolved'] = '<a href="' . bbp_get_topic_permalink() . '?unsolve_topic">' . esc_html__( 'Mark as unsolved', 'ama-core' ) . '</a>'; | |
| 65 | - } else { | |
| 66 | - $links['solved'] = '<a href="' . bbp_get_topic_permalink() . '?solve_topic">' . esc_html__( 'Mark as solved', 'ama-core' ) . '</a>'; | |
| 67 | - } | |
| 68 | - } | |
| 69 | - } | |
| 70 | - return $links; | |
| 71 | - } | |
| 72 | - | |
| 73 | - public function solve_topic( $topic_id = 0 ) { | |
| 74 | - $topic_author = bbp_get_topic_author_id( $topic_id ); | |
| 75 | - if ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) { | |
| 76 | - update_post_meta( $topic_id, '_bbp_topic_is_solved', '1' ); | |
| 77 | - $redirect = remove_query_arg( [ 'solve_topic' ] ); | |
| 78 | - wp_safe_redirect( $redirect ); | |
| 79 | - } else { | |
| 80 | - wp_die( esc_html__( 'You do not have the permission to do that!', 'ama-core' ) ); | |
| 81 | - } | |
| 82 | - | |
| 83 | - } | |
| 84 | - | |
| 85 | - public function unsolve_topic( $topic_id = 0 ) { | |
| 86 | - $topic_author = bbp_get_topic_author_id( $topic_id ); | |
| 87 | - if ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) { | |
| 88 | - delete_post_meta( $topic_id, '_bbp_topic_is_solved' ); | |
| 89 | - $redirect = remove_query_arg( [ 'unsolve_topic' ] ); | |
| 90 | - wp_safe_redirect( $redirect ); | |
| 91 | - } else { | |
| 92 | - wp_die( esc_html__( 'You do not have the permission to do that!', 'ama-core' ) ); | |
| 93 | - } | |
| 94 | - } | |
| 95 | - | |
| 96 | - /** | |
| 97 | - * Called during the plugins_loaded action to filter the capability | |
| 98 | - * required to view solved topics. | |
| 99 | - * | |
| 100 | - * @since 1.0 | |
| 101 | - * | |
| 102 | - * @return void | |
| 103 | - */ | |
| 104 | - public function filter_capability() { | |
| 105 | - $this->capability = apply_filters( 'bbp_solved_topic_capability', $this->capability ); | |
| 106 | - } | |
| 107 | - | |
| 108 | - /** | |
| 109 | - * Solved topic badge | |
| 110 | - * | |
| 111 | - * @since 1.0 | |
| 112 | - * | |
| 113 | - * @return void | |
| 114 | - */ | |
| 115 | - public function solved_badge() { | |
| 116 | - $topic_id = bbp_get_topic_id(); | |
| 117 | - if ( $this->is_solved( $topic_id ) ) { | |
| 118 | - echo '<i class="fa fa-check-circle text-success"></i>'; | |
| 119 | - } | |
| 120 | - } | |
| 121 | - | |
| 122 | - /** | |
| 123 | - * Outputs the reply checkbox | |
| 124 | - * | |
| 125 | - * @since 1.0 | |
| 126 | - * | |
| 127 | - * @return void | |
| 128 | - */ | |
| 129 | - public function reply_checkbox() { | |
| 130 | - $has_best_answer = $this->has_best_answer( bbp_get_topic_id() ); | |
| 131 | - $is_best_answer = $this->is_best_answer( bbp_get_reply_id() ); | |
| 132 | - if ( bbp_is_reply_edit() && ( current_user_can( $this->capability ) ) ) { | |
| 133 | - if ( ( ! $has_best_answer ) || ( $is_best_answer ) ) { | |
| 134 | - ?> | |
| 135 | - <p> | |
| 136 | - <input name="bbp_best_answer" id="bbp_best_answer" type="checkbox"<?php checked( '1', $is_best_answer ); ?> value="1" tabindex="<?php bbp_tab_index(); ?>" /> | |
| 137 | - <label for="bbp_best_answer"><?php esc_html_e( 'Set this reply as Best Answer.', 'ama-core' ); ?></label> | |
| 138 | - </p> | |
| 139 | - <?php | |
| 140 | - } | |
| 141 | - } | |
| 142 | - } | |
| 143 | - | |
| 144 | - /** | |
| 145 | - * Outputs the topic checkbox | |
| 146 | - * | |
| 147 | - * @since 1.0 | |
| 148 | - * | |
| 149 | - * @return void | |
| 150 | - */ | |
| 151 | - public function topic_checkbox() { | |
| 152 | - $topic_author = bbp_get_topic_author_id( bbp_get_topic_id() ); | |
| 153 | - if ( bbp_is_topic_edit() && ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) ) { | |
| 154 | - ?> | |
| 155 | - <p> | |
| 156 | - <input name="bbp_solved_topic" id="bbp_solved_topic" type="checkbox"<?php checked( '1', $this->is_solved( bbp_get_topic_id() ) ); ?> value="1" tabindex="<?php bbp_tab_index(); ?>" /> | |
| 157 | - <label for="bbp_solved_topic"><?php esc_html_e( 'Set this post as solved.', 'ama-core' ); ?></label> | |
| 158 | - </p> | |
| 159 | - <?php | |
| 160 | - } | |
| 161 | - | |
| 162 | - } | |
| 163 | - | |
| 164 | - /** | |
| 165 | - * Stores the private state on reply creation and edit | |
| 166 | - * | |
| 167 | - * @since 1.0 | |
| 168 | - * | |
| 169 | - * @param $reply_id int The ID of the reply | |
| 170 | - * @param $topic_id int The ID of the topic the reply belongs to | |
| 171 | - * @param $forum_id int The ID of the forum the topic belongs to | |
| 172 | - * @param $anonymous_data bool Are we posting as an anonymous user? | |
| 173 | - * @param $author_id int The ID of user creating the reply, or the ID of the replie's author during edit | |
| 174 | - * @param $is_edit bool Are we editing a reply? | |
| 175 | - * | |
| 176 | - * @return void | |
| 177 | - */ | |
| 178 | - public function update_reply( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $author_id = 0, $is_edit = false ) { | |
| 179 | - | |
| 180 | - if ( isset( $_POST['bbp_best_answer'] ) ) { | |
| 181 | - $get_topic_meta = get_post_meta( $topic_id, '_bbp_topic_has_best_answer', true ); | |
| 182 | - if ( ! $get_topic_meta ) { | |
| 183 | - update_post_meta( $topic_id, '_bbp_topic_has_best_answer', '1' ); | |
| 184 | - } | |
| 185 | - update_post_meta( $reply_id, '_bbp_reply_is_best_answer', '1' ); | |
| 186 | - } else { | |
| 187 | - delete_post_meta( $reply_id, '_bbp_reply_is_best_answer' ); | |
| 188 | - delete_post_meta( $topic_id, '_bbp_topic_has_best_answer' ); | |
| 189 | - } | |
| 190 | - | |
| 191 | - } | |
| 192 | - | |
| 193 | - /** | |
| 194 | - * Stores the private state on topic creation and edit | |
| 195 | - * | |
| 196 | - * @since 1.0 | |
| 197 | - * | |
| 198 | - * @param $topic_id int The ID of the topic | |
| 199 | - * @param $is_edit bool Are we editing a topic? | |
| 200 | - * | |
| 201 | - * @return void | |
| 202 | - */ | |
| 203 | - public function update_topic( $topic_id = 0, $is_edit = false ) { | |
| 204 | - | |
| 205 | - if ( isset( $_POST['bbp_solved_topic'] ) ) { | |
| 206 | - update_post_meta( $topic_id, '_bbp_topic_is_solved', '1' ); | |
| 207 | - } else { | |
| 208 | - delete_post_meta( $topic_id, '_bbp_topic_is_solved' ); | |
| 209 | - } | |
| 210 | - | |
| 211 | - } | |
| 212 | - | |
| 213 | - | |
| 214 | - /** | |
| 215 | - * Determines if a reply is marked as solved | |
| 216 | - * | |
| 217 | - * @since 1.0 | |
| 218 | - * | |
| 219 | - * @param $reply_id int The ID of the reply | |
| 220 | - * | |
| 221 | - * @return bool | |
| 222 | - */ | |
| 223 | - public function is_best_answer( $reply_id = 0 ) { | |
| 224 | - | |
| 225 | - $retval = false; | |
| 226 | - | |
| 227 | - // Checking a specific reply id | |
| 228 | - if ( ! empty( $reply_id ) ) { | |
| 229 | - $reply = bbp_get_reply( $reply_id ); | |
| 230 | - $reply_id = ! empty( $reply ) ? $reply->ID : 0; | |
| 231 | - | |
| 232 | - // Using the global reply id | |
| 233 | - } elseif ( bbp_get_reply_id() ) { | |
| 234 | - $reply_id = bbp_get_reply_id(); | |
| 235 | - | |
| 236 | - // Use the current post id | |
| 237 | - } elseif ( ! bbp_get_reply_id() ) { | |
| 238 | - $reply_id = get_the_ID(); | |
| 239 | - } | |
| 240 | - | |
| 241 | - if ( ! empty( $reply_id ) ) { | |
| 242 | - $retval = get_post_meta( $reply_id, '_bbp_reply_is_best_answer', true ); | |
| 243 | - } | |
| 244 | - | |
| 245 | - return (bool) apply_filters( 'bbp_reply_is_best_answer', (bool) $retval, $reply_id ); | |
| 246 | - } | |
| 247 | - | |
| 248 | - /** | |
| 249 | - * Determines if a reply is marked as solved | |
| 250 | - * | |
| 251 | - * @since 1.0 | |
| 252 | - * | |
| 253 | - * @param $reply_id int The ID of the reply | |
| 254 | - * | |
| 255 | - * @return bool | |
| 256 | - */ | |
| 257 | - public function is_solved( $topic_id = 0 ) { | |
| 258 | - | |
| 259 | - $retval = false; | |
| 260 | - | |
| 261 | - // Checking a specific reply id. | |
| 262 | - if ( ! empty( $topic_id ) ) { | |
| 263 | - $topic = bbp_get_topic( $topic_id ); | |
| 264 | - $topic_id = ! empty( $topic ) ? $topic->ID : 0; | |
| 265 | - | |
| 266 | - // Using the global reply id. | |
| 267 | - } elseif ( bbp_get_topic_id() ) { | |
| 268 | - $topic_id = bbp_get_topic_id(); | |
| 269 | - | |
| 270 | - // Use the current post id. | |
| 271 | - } elseif ( ! bbp_get_topic_id() ) { | |
| 272 | - $topic_id = get_the_ID(); | |
| 273 | - } | |
| 274 | - | |
| 275 | - if ( ! empty( $topic_id ) ) { | |
| 276 | - $retval = get_post_meta( $topic_id, '_bbp_topic_is_solved', true ); | |
| 277 | - } | |
| 278 | - | |
| 279 | - return (bool) apply_filters( 'bbp_topic_is_solved', (bool) $retval, $topic_id ); | |
| 280 | - } | |
| 281 | - | |
| 282 | - /** | |
| 283 | - * Determines if a reply is marked as solved | |
| 284 | - * | |
| 285 | - * @since 1.0 | |
| 286 | - * | |
| 287 | - * @param $reply_id int The ID of the reply | |
| 288 | - * | |
| 289 | - * @return bool | |
| 290 | - */ | |
| 291 | - public function has_best_answer( $topic_id = 0 ) { | |
| 292 | - | |
| 293 | - // if bbpress is not active, return the classes | |
| 294 | - | |
| 295 | - if ( ! function_exists( 'bbp_get_topic_id' ) ) { | |
| 296 | - return false; | |
| 297 | - } | |
| 298 | - | |
| 299 | - $retval = false; | |
| 300 | - | |
| 301 | - // Checking a specific reply id. | |
| 302 | - if ( ! empty( $topic_id ) ) { | |
| 303 | - $topic = bbp_get_topic( $topic_id ); | |
| 304 | - $topic_id = ! empty( $topic ) ? $topic->ID : 0; | |
| 305 | - | |
| 306 | - // Using the global reply id. | |
| 307 | - } elseif ( bbp_get_topic_id() ) { | |
| 308 | - $topic_id = bbp_get_topic_id(); | |
| 309 | - | |
| 310 | - // Use the current post id. | |
| 311 | - } elseif ( ! bbp_get_topic_id() ) { | |
| 312 | - $topic_id = get_the_ID(); | |
| 313 | - } | |
| 314 | - | |
| 315 | - if ( ! empty( $topic_id ) ) { | |
| 316 | - $retval = get_post_meta( $topic_id, '_bbp_topic_has_best_answer', true ); | |
| 317 | - } | |
| 318 | - | |
| 319 | - return (bool) apply_filters( 'bbp_topic_has_best_answer', (bool) $retval, $topic_id ); | |
| 320 | - } | |
| 321 | - | |
| 322 | - | |
| 323 | - /** | |
| 324 | - * Reply content filter | |
| 325 | - * | |
| 326 | - * @since 1.0 | |
| 327 | - * | |
| 328 | - * @param $content string The content of the reply | |
| 329 | - * @param $reply_id int The ID of the reply | |
| 330 | - * | |
| 331 | - * @return string | |
| 332 | - */ | |
| 333 | - public function reply_content_filter( $content = '', $reply_id = 0 ) { | |
| 334 | - | |
| 335 | - if ( empty( $reply_id ) ) { | |
| 336 | - $reply_id = bbp_get_reply_id( $reply_id ); | |
| 337 | - } | |
| 338 | - | |
| 339 | - $topic_id = bbp_get_topic_id(); | |
| 340 | - | |
| 341 | - if ( $this->is_best_answer( $reply_id ) ) { | |
| 342 | - $content = '<div class="solved-topic-bar" id="best-answer"><span class="badge"><i class="icon_check"></i> ' . esc_html__( 'Best Answer', 'ama-core' ) . '</span></div>' . $content; | |
| 343 | - } | |
| 344 | - | |
| 345 | - return $content; | |
| 346 | - } | |
| 347 | - | |
| 348 | - /** | |
| 349 | - * Topic content filter | |
| 350 | - * | |
| 351 | - * @since 1.0 | |
| 352 | - * | |
| 353 | - * @param $content string The content of the topic | |
| 354 | - * @param $topic_id int The ID of the reply | |
| 355 | - * | |
| 356 | - * @return string | |
| 357 | - */ | |
| 358 | - public function topic_content_filter( $content = '', $topic_id = 0 ) { | |
| 359 | - | |
| 360 | - if ( empty( $topic_id ) ) { | |
| 361 | - $topic_id = bbp_get_topic_id( $topic_id ); | |
| 362 | - } | |
| 363 | - | |
| 364 | - $open = ''; | |
| 365 | - $close = ''; | |
| 366 | - $resolved = ''; | |
| 367 | - $has_best_answer = ''; | |
| 368 | - | |
| 369 | - if ( $this->is_solved( $topic_id ) || $this->has_best_answer( $topic_id ) ) { | |
| 370 | - $open = '<div class="solved-topic-bar">'; | |
| 371 | - $close = '</div>'; | |
| 372 | - } | |
| 373 | - | |
| 374 | - if ( $this->is_solved( $topic_id ) ) { | |
| 375 | - $resolved = '<span class="accepted-ans-mark badge"><i class="icon_check_alt"></i> ' . esc_html__( 'Resolved', 'ama-core' ) . '</span>'; | |
| 376 | - } | |
| 377 | - | |
| 378 | - $content = $content . $open . $resolved . $has_best_answer . $close; | |
| 379 | - | |
| 380 | - return $content; | |
| 381 | - } | |
| 382 | - | |
| 383 | - /** | |
| 384 | - * Adds a new class to replies that are marked as solved | |
| 385 | - * | |
| 386 | - * @since 1.0 | |
| 387 | - * | |
| 388 | - * @param $classes array An array of current class names | |
| 389 | - * | |
| 390 | - * @return bool | |
| 391 | - */ | |
| 392 | - public function reply_post_class( $classes ) { | |
| 393 | - | |
| 394 | - // if bbpress is not active, return the classes | |
| 395 | - if ( ! function_exists( 'bbp_get_reply_id' ) ) { | |
| 396 | - return $classes; | |
| 397 | - } | |
| 398 | - | |
| 399 | - $reply_id = bbp_get_reply_id(); | |
| 400 | - | |
| 401 | - // Only apply the class to replies. | |
| 402 | - if ( bbp_get_reply_post_type() != get_post_type( $reply_id ) ) { | |
| 403 | - return $classes; | |
| 404 | - } | |
| 405 | - | |
| 406 | - if ( $this->is_best_answer( $reply_id ) ) { | |
| 407 | - $classes[] = 'best-answer'; | |
| 408 | - } | |
| 409 | - | |
| 410 | - return $classes; | |
| 411 | - } | |
| 412 | -} | |
| 413 | - | |
| 414 | -// Instantiate the class. | |
| 415 | -$GLOBALS['bbp_solved_topic'] = new bbp_solved_topic(); | |
| 416 | - | |
| 417 | -function bbp_solved_topic(): bbp_solved_topic { | |
| 418 | - return new bbp_solved_topic(); | |
| 1 | +<?php | |
| 2 | +namespace features; | |
| 3 | + | |
| 4 | +class forumax_solve_topic { | |
| 5 | + | |
| 6 | + /** | |
| 7 | + * The capability required to view private posts. | |
| 8 | + * | |
| 9 | + * @since 1.0 | |
| 10 | + * | |
| 11 | + * @var string $capability | |
| 12 | + */ | |
| 13 | + public $capability = 'moderate'; | |
| 14 | + | |
| 15 | + /* | |
| 16 | + --------------------------------------------* | |
| 17 | + * Constructor | |
| 18 | + *--------------------------------------------*/ | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Initializes the plugin by setting filters, and administration functions. | |
| 22 | + */ | |
| 23 | + function __construct() { | |
| 24 | + add_action( 'plugins_loaded', [ $this, 'filter_capability' ] ); | |
| 25 | + | |
| 26 | + // show the checkboxes | |
| 27 | + add_action( 'bbp_theme_before_reply_form_submit_wrapper', [ $this, 'reply_checkbox' ] ); | |
| 28 | + add_action( 'bbp_theme_before_topic_form_submit_wrapper', [ $this, 'topic_checkbox' ] ); | |
| 29 | + | |
| 30 | + // save the reply state | |
| 31 | + add_action( 'bbp_new_reply', [ $this, 'update_reply' ], 0, 6 ); | |
| 32 | + add_action( 'bbp_edit_reply', [ $this, 'update_reply' ], 0, 6 ); | |
| 33 | + | |
| 34 | + // save the topic state | |
| 35 | + add_action( 'bbp_new_topic', [ $this, 'update_topic' ], 0, 6 ); | |
| 36 | + add_action( 'bbp_edit_topic', [ $this, 'update_topic' ], 0, 6 ); | |
| 37 | + | |
| 38 | + // reply content filter | |
| 39 | + add_filter( 'bbp_get_reply_excerpt', [ $this, 'reply_content_filter' ], 999, 2 ); | |
| 40 | + add_filter( 'bbp_get_reply_content', [ $this, 'reply_content_filter' ], 999, 2 ); | |
| 41 | + | |
| 42 | + add_filter( 'bbp_theme_before_topic_title', [ $this, 'solved_badge' ], 999, 2 ); | |
| 43 | + add_filter( 'bbp_topic_admin_links', [ $this, 'add_topic_admin_link' ], 10, 2 ); | |
| 44 | + | |
| 45 | + add_action( 'template_redirect', [ $this, 'handle_solve_action' ] ); | |
| 46 | + | |
| 47 | + // add a class name indicating the read status | |
| 48 | + add_filter( 'post_class', [ $this, 'reply_post_class' ] ); | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * Handle the solve/unsolve action via GET request | |
| 53 | + * | |
| 54 | + * @since 2.2.0 | |
| 55 | + */ | |
| 56 | + public function handle_solve_action() { | |
| 57 | + $topic_id = bbp_get_topic_id(); | |
| 58 | + | |
| 59 | + if ( isset( $_GET['solve_topic'] ) ) { | |
| 60 | + // Verify nonce | |
| 61 | + if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'forumax_solve_topic_' . $topic_id ) ) { | |
| 62 | + wp_die( esc_html__( 'Security check failed.', 'forumax' ) ); | |
| 63 | + } | |
| 64 | + $this->solve_topic( $topic_id ); | |
| 65 | + } elseif ( isset( $_GET['unsolve_topic'] ) ) { | |
| 66 | + if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'forumax_unsolve_topic_' . $topic_id ) ) { | |
| 67 | + wp_die( esc_html__( 'Security check failed.', 'forumax' ) ); | |
| 68 | + } | |
| 69 | + $this->unsolve_topic( $topic_id ); | |
| 70 | + } | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * Admin links | |
| 75 | + */ | |
| 76 | + public function add_topic_admin_link( $links, $topic_id ) { | |
| 77 | + | |
| 78 | + $topic_author = bbp_get_topic_author_id( bbp_get_topic_id() ); | |
| 79 | + if ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) { | |
| 80 | + if ( $this->is_solved( bbp_get_topic_id() ) ) { | |
| 81 | + $links['unsolved'] = '<a href="' . wp_nonce_url( | |
| 82 | + add_query_arg( 'unsolve_topic', '1', bbp_get_topic_permalink() ), | |
| 83 | + 'forumax_unsolve_topic_' . bbp_get_topic_id() | |
| 84 | + ) . '">' . esc_html__( 'Mark as unsolved', 'forumax' ) . '</a>'; | |
| 85 | + } else { | |
| 86 | + $links['solved'] = '<a href="' . wp_nonce_url( | |
| 87 | + add_query_arg( 'solve_topic', '1', bbp_get_topic_permalink() ), | |
| 88 | + 'forumax_solve_topic_' . bbp_get_topic_id() | |
| 89 | + ) . '">' . esc_html__( 'Mark as solved', 'forumax' ) . '</a>'; | |
| 90 | + } | |
| 91 | + } | |
| 92 | + return $links; | |
| 93 | + } | |
| 94 | + | |
| 95 | + public function solve_topic( $topic_id = 0 ) { | |
| 96 | + $topic_author = bbp_get_topic_author_id( $topic_id ); | |
| 97 | + if ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) { | |
| 98 | + update_post_meta( $topic_id, '_bbp_topic_is_solved', '1' ); | |
| 99 | + $redirect = remove_query_arg( [ 'solve_topic', '_wpnonce' ] ); | |
| 100 | + wp_safe_redirect( $redirect ); | |
| 101 | + exit; | |
| 102 | + } else { | |
| 103 | + wp_die( esc_html__( 'You do not have the permission to do that!', 'forumax' ) ); | |
| 104 | + } | |
| 105 | + | |
| 106 | + } | |
| 107 | + | |
| 108 | + public function unsolve_topic( $topic_id = 0 ) { | |
| 109 | + $topic_author = bbp_get_topic_author_id( $topic_id ); | |
| 110 | + if ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) { | |
| 111 | + delete_post_meta( $topic_id, '_bbp_topic_is_solved' ); | |
| 112 | + $redirect = remove_query_arg( [ 'unsolve_topic', '_wpnonce' ] ); | |
| 113 | + wp_safe_redirect( $redirect ); | |
| 114 | + exit; | |
| 115 | + } else { | |
| 116 | + wp_die( esc_html__( 'You do not have the permission to do that!', 'forumax' ) ); | |
| 117 | + } | |
| 118 | + } | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * Called during the plugins_loaded action to filter the capability | |
| 122 | + * required to view solved topics. | |
| 123 | + * | |
| 124 | + * @since 1.0 | |
| 125 | + * | |
| 126 | + * @return void | |
| 127 | + */ | |
| 128 | + public function filter_capability() { | |
| 129 | + $this->capability = apply_filters( 'forumax_solve_topic_capability', $this->capability ); | |
| 130 | + } | |
| 131 | + | |
| 132 | + /** | |
| 133 | + * Solved topic badge | |
| 134 | + * | |
| 135 | + * @since 1.0 | |
| 136 | + * | |
| 137 | + * @return void | |
| 138 | + */ | |
| 139 | + public function solved_badge() { | |
| 140 | + $topic_id = bbp_get_topic_id(); | |
| 141 | + if ( $this->is_solved( $topic_id ) ) { | |
| 142 | + echo '<i class="fa fa-check-circle text-success"></i>'; | |
| 143 | + } | |
| 144 | + } | |
| 145 | + | |
| 146 | + /** | |
| 147 | + * Outputs the reply checkbox | |
| 148 | + * | |
| 149 | + * @since 1.0 | |
| 150 | + * | |
| 151 | + * @return void | |
| 152 | + */ | |
| 153 | + public function reply_checkbox() { | |
| 154 | + $has_best_answer = $this->has_best_answer( bbp_get_topic_id() ); | |
| 155 | + $is_best_answer = $this->is_best_answer( bbp_get_reply_id() ); | |
| 156 | + if ( bbp_is_reply_edit() && ( current_user_can( $this->capability ) ) ) { | |
| 157 | + if ( ( ! $has_best_answer ) || ( $is_best_answer ) ) { | |
| 158 | + ?> | |
| 159 | + <p> | |
| 160 | + <input name="bbp_best_answer" id="bbp_best_answer" type="checkbox"<?php checked( '1', $is_best_answer ); ?> value="1" tabindex="<?php bbp_tab_index(); ?>" /> | |
| 161 | + <label for="bbp_best_answer"><?php esc_html_e( 'Set this reply as Best Answer.', 'forumax' ); ?></label> | |
| 162 | + </p> | |
| 163 | + <?php | |
| 164 | + } | |
| 165 | + } | |
| 166 | + } | |
| 167 | + | |
| 168 | + /** | |
| 169 | + * Outputs the topic checkbox | |
| 170 | + * | |
| 171 | + * @since 1.0 | |
| 172 | + * | |
| 173 | + * @return void | |
| 174 | + */ | |
| 175 | + public function topic_checkbox() { | |
| 176 | + $topic_author = bbp_get_topic_author_id( bbp_get_topic_id() ); | |
| 177 | + if ( bbp_is_topic_edit() && ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) ) { | |
| 178 | + ?> | |
| 179 | + <p> | |
| 180 | + <input name="forumax_solve_topic" id="forumax_solve_topic" type="checkbox"<?php checked( '1', $this->is_solved( bbp_get_topic_id() ) ); ?> value="1" tabindex="<?php bbp_tab_index(); ?>" /> | |
| 181 | + <label for="forumax_solve_topic"><?php esc_html_e( 'Set this post as solved.', 'forumax' ); ?></label> | |
| 182 | + </p> | |
| 183 | + <?php | |
| 184 | + } | |
| 185 | + | |
| 186 | + } | |
| 187 | + | |
| 188 | + /** | |
| 189 | + * Stores the private state on reply creation and edit | |
| 190 | + * | |
| 191 | + * @since 1.0 | |
| 192 | + * | |
| 193 | + * @param $reply_id int The ID of the reply | |
| 194 | + * @param $topic_id int The ID of the topic the reply belongs to | |
| 195 | + * @param $forum_id int The ID of the forum the topic belongs to | |
| 196 | + * @param $anonymous_data bool Are we posting as an anonymous user? | |
| 197 | + * @param $author_id int The ID of user creating the reply, or the ID of the replie's author during edit | |
| 198 | + * @param $is_edit bool Are we editing a reply? | |
| 199 | + * | |
| 200 | + * @return void | |
| 201 | + */ | |
| 202 | + public function update_reply( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $author_id = 0, $is_edit = false ) { | |
| 203 | + | |
| 204 | + if ( isset( $_POST['bbp_best_answer'] ) ) { | |
| 205 | + $get_topic_meta = get_post_meta( $topic_id, '_bbp_topic_has_best_answer', true ); | |
| 206 | + if ( ! $get_topic_meta ) { | |
| 207 | + update_post_meta( $topic_id, '_bbp_topic_has_best_answer', '1' ); | |
| 208 | + } | |
| 209 | + update_post_meta( $reply_id, '_bbp_reply_is_best_answer', '1' ); | |
| 210 | + } else { | |
| 211 | + delete_post_meta( $reply_id, '_bbp_reply_is_best_answer' ); | |
| 212 | + delete_post_meta( $topic_id, '_bbp_topic_has_best_answer' ); | |
| 213 | + } | |
| 214 | + | |
| 215 | + } | |
| 216 | + | |
| 217 | + /** | |
| 218 | + * Stores the private state on topic creation and edit | |
| 219 | + * | |
| 220 | + * @since 1.0 | |
| 221 | + * | |
| 222 | + * @param $topic_id int The ID of the topic | |
| 223 | + * @param $is_edit bool Are we editing a topic? | |
| 224 | + * | |
| 225 | + * @return void | |
| 226 | + */ | |
| 227 | + public function update_topic( $topic_id = 0, $is_edit = false ) { | |
| 228 | + | |
| 229 | + if ( isset( $_POST['forumax_solve_topic'] ) ) { | |
| 230 | + update_post_meta( $topic_id, '_bbp_topic_is_solved', '1' ); | |
| 231 | + } else { | |
| 232 | + delete_post_meta( $topic_id, '_bbp_topic_is_solved' ); | |
| 233 | + } | |
| 234 | + | |
| 235 | + } | |
| 236 | + | |
| 237 | + | |
| 238 | + /** | |
| 239 | + * Determines if a reply is marked as solved | |
| 240 | + * | |
| 241 | + * @since 1.0 | |
| 242 | + * | |
| 243 | + * @param $reply_id int The ID of the reply | |
| 244 | + * | |
| 245 | + * @return bool | |
| 246 | + */ | |
| 247 | + public function is_best_answer( $reply_id = 0 ) { | |
| 248 | + | |
| 249 | + $retval = false; | |
| 250 | + | |
| 251 | + // Checking a specific reply id | |
| 252 | + if ( ! empty( $reply_id ) ) { | |
| 253 | + $reply = bbp_get_reply( $reply_id ); | |
| 254 | + $reply_id = ! empty( $reply ) ? $reply->ID : 0; | |
| 255 | + | |
| 256 | + // Using the global reply id | |
| 257 | + } elseif ( bbp_get_reply_id() ) { | |
| 258 | + $reply_id = bbp_get_reply_id(); | |
| 259 | + | |
| 260 | + // Use the current post id | |
| 261 | + } elseif ( ! bbp_get_reply_id() ) { | |
| 262 | + $reply_id = get_the_ID(); | |
| 263 | + } | |
| 264 | + | |
| 265 | + if ( ! empty( $reply_id ) ) { | |
| 266 | + $retval = get_post_meta( $reply_id, '_bbp_reply_is_best_answer', true ); | |
| 267 | + } | |
| 268 | + | |
| 269 | + return (bool) apply_filters( 'bbp_reply_is_best_answer', (bool) $retval, $reply_id ); | |
| 270 | + } | |
| 271 | + | |
| 272 | + /** | |
| 273 | + * Determines if a reply is marked as solved | |
| 274 | + * | |
| 275 | + * @since 1.0 | |
| 276 | + * | |
| 277 | + * @param $reply_id int The ID of the reply | |
| 278 | + * | |
| 279 | + * @return bool | |
| 280 | + */ | |
| 281 | + public function is_solved( $topic_id = 0 ) { | |
| 282 | + | |
| 283 | + $retval = false; | |
| 284 | + | |
| 285 | + // Checking a specific reply id. | |
| 286 | + if ( ! empty( $topic_id ) ) { | |
| 287 | + $topic = bbp_get_topic( $topic_id ); | |
| 288 | + $topic_id = ! empty( $topic ) ? $topic->ID : 0; | |
| 289 | + | |
| 290 | + // Using the global reply id. | |
| 291 | + } elseif ( bbp_get_topic_id() ) { | |
| 292 | + $topic_id = bbp_get_topic_id(); | |
| 293 | + | |
| 294 | + // Use the current post id. | |
| 295 | + } elseif ( ! bbp_get_topic_id() ) { | |
| 296 | + $topic_id = get_the_ID(); | |
| 297 | + } | |
| 298 | + | |
| 299 | + if ( ! empty( $topic_id ) ) { | |
| 300 | + $retval = get_post_meta( $topic_id, '_bbp_topic_is_solved', true ); | |
| 301 | + } | |
| 302 | + | |
| 303 | + return (bool) apply_filters( 'bbp_topic_is_solved', (bool) $retval, $topic_id ); | |
| 304 | + } | |
| 305 | + | |
| 306 | + /** | |
| 307 | + * Determines if a reply is marked as solved | |
| 308 | + * | |
| 309 | + * @since 1.0 | |
| 310 | + * | |
| 311 | + * @param $reply_id int The ID of the reply | |
| 312 | + * | |
| 313 | + * @return bool | |
| 314 | + */ | |
| 315 | + public function has_best_answer( $topic_id = 0 ) { | |
| 316 | + | |
| 317 | + // if bbpress is not active, return the classes | |
| 318 | + | |
| 319 | + if ( ! function_exists( 'bbp_get_topic_id' ) ) { | |
| 320 | + return false; | |
| 321 | + } | |
| 322 | + | |
| 323 | + $retval = false; | |
| 324 | + | |
| 325 | + // Checking a specific reply id. | |
| 326 | + if ( ! empty( $topic_id ) ) { | |
| 327 | + $topic = bbp_get_topic( $topic_id ); | |
| 328 | + $topic_id = ! empty( $topic ) ? $topic->ID : 0; | |
| 329 | + | |
| 330 | + // Using the global reply id. | |
| 331 | + } elseif ( bbp_get_topic_id() ) { | |
| 332 | + $topic_id = bbp_get_topic_id(); | |
| 333 | + | |
| 334 | + // Use the current post id. | |
| 335 | + } elseif ( ! bbp_get_topic_id() ) { | |
| 336 | + $topic_id = get_the_ID(); | |
| 337 | + } | |
| 338 | + | |
| 339 | + if ( ! empty( $topic_id ) ) { | |
| 340 | + $retval = get_post_meta( $topic_id, '_bbp_topic_has_best_answer', true ); | |
| 341 | + } | |
| 342 | + | |
| 343 | + return (bool) apply_filters( 'bbp_topic_has_best_answer', (bool) $retval, $topic_id ); | |
| 344 | + } | |
| 345 | + | |
| 346 | + | |
| 347 | + /** | |
| 348 | + * Reply content filter | |
| 349 | + * | |
| 350 | + * @since 1.0 | |
| 351 | + * | |
| 352 | + * @param $content string The content of the reply | |
| 353 | + * @param $reply_id int The ID of the reply | |
| 354 | + * | |
| 355 | + * @return string | |
| 356 | + */ | |
| 357 | + public function reply_content_filter( $content = '', $reply_id = 0 ) { | |
| 358 | + | |
| 359 | + if ( empty( $reply_id ) ) { | |
| 360 | + $reply_id = bbp_get_reply_id( $reply_id ); | |
| 361 | + } | |
| 362 | + | |
| 363 | + $topic_id = bbp_get_topic_id(); | |
| 364 | + | |
| 365 | + if ( $this->is_best_answer( $reply_id ) ) { | |
| 366 | + $content = '<div class="solved-topic-bar" id="best-answer"><span class="badge"><i class="icon_check"></i> ' . esc_html__( 'Best Answer', 'forumax' ) . '</span></div>' . $content; | |
| 367 | + } | |
| 368 | + | |
| 369 | + return $content; | |
| 370 | + } | |
| 371 | + | |
| 372 | + /** | |
| 373 | + * Topic content filter | |
| 374 | + * | |
| 375 | + * @since 1.0 | |
| 376 | + * | |
| 377 | + * @param $content string The content of the topic | |
| 378 | + * @param $topic_id int The ID of the reply | |
| 379 | + * | |
| 380 | + * @return string | |
| 381 | + */ | |
| 382 | + public function topic_content_filter( $content = '', $topic_id = 0 ) { | |
| 383 | + | |
| 384 | + if ( empty( $topic_id ) ) { | |
| 385 | + $topic_id = bbp_get_topic_id( $topic_id ); | |
| 386 | + } | |
| 387 | + | |
| 388 | + $open = ''; | |
| 389 | + $close = ''; | |
| 390 | + $resolved = ''; | |
| 391 | + $has_best_answer = ''; | |
| 392 | + | |
| 393 | + if ( $this->is_solved( $topic_id ) || $this->has_best_answer( $topic_id ) ) { | |
| 394 | + $open = '<div class="solved-topic-bar">'; | |
| 395 | + $close = '</div>'; | |
| 396 | + } | |
| 397 | + | |
| 398 | + if ( $this->is_solved( $topic_id ) ) { | |
| 399 | + $resolved = '<span class="accepted-ans-mark badge"><i class="icon_check_alt"></i> ' . esc_html__( 'Resolved', 'forumax' ) . '</span>'; | |
| 400 | + } | |
| 401 | + | |
| 402 | + $content = $content . $open . $resolved . $has_best_answer . $close; | |
| 403 | + | |
| 404 | + return $content; | |
| 405 | + } | |
| 406 | + | |
| 407 | + /** | |
| 408 | + * Adds a new class to replies that are marked as solved | |
| 409 | + * | |
| 410 | + * @since 1.0 | |
| 411 | + * | |
| 412 | + * @param $classes array An array of current class names | |
| 413 | + * | |
| 414 | + * @return bool | |
| 415 | + */ | |
| 416 | + public function reply_post_class( $classes ) { | |
| 417 | + | |
| 418 | + // if bbpress is not active, return the classes | |
| 419 | + if ( ! function_exists( 'bbp_get_reply_id' ) ) { | |
| 420 | + return $classes; | |
| 421 | + } | |
| 422 | + | |
| 423 | + $reply_id = bbp_get_reply_id(); | |
| 424 | + | |
| 425 | + // Only apply the class to replies. | |
| 426 | + if ( bbp_get_reply_post_type() != get_post_type( $reply_id ) ) { | |
| 427 | + return $classes; | |
| 428 | + } | |
| 429 | + | |
| 430 | + if ( $this->is_best_answer( $reply_id ) ) { | |
| 431 | + $classes[] = 'best-answer'; | |
| 432 | + } | |
| 433 | + | |
| 434 | + return $classes; | |
| 435 | + } | |
| 436 | +} | |
| 437 | + | |
| 438 | +// Instantiate the class. | |
| 439 | +$GLOBALS['forumax_solve_topic'] = new forumax_solve_topic(); | |
| 440 | + | |
| 441 | +function forumax_solve_topic(): forumax_solve_topic { | |
| 442 | + return new forumax_solve_topic(); | |
| 419 | 443 | } |