solve_topic( $topic_id ); } elseif ( isset( $_GET['unsolve_topic'] ) ) { if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'forumax_unsolve_topic_' . $topic_id ) ) { wp_die( esc_html__( 'Security check failed.', 'forumax' ) ); } $this->unsolve_topic( $topic_id ); } } /** * Admin links */ public function add_topic_admin_link( $links, $topic_id ) { $topic_author = bbp_get_topic_author_id( bbp_get_topic_id() ); if ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) { if ( $this->is_solved( bbp_get_topic_id() ) ) { $links['unsolved'] = '' . esc_html__( 'Mark as unsolved', 'forumax' ) . ''; } else { $links['solved'] = '' . esc_html__( 'Mark as solved', 'forumax' ) . ''; } } return $links; } public function solve_topic( $topic_id = 0 ) { $topic_author = bbp_get_topic_author_id( $topic_id ); if ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) { update_post_meta( $topic_id, '_bbp_topic_is_solved', '1' ); $redirect = remove_query_arg( [ 'solve_topic', '_wpnonce' ] ); wp_safe_redirect( $redirect ); exit; } else { wp_die( esc_html__( 'You do not have the permission to do that!', 'forumax' ) ); } } public function unsolve_topic( $topic_id = 0 ) { $topic_author = bbp_get_topic_author_id( $topic_id ); if ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) { delete_post_meta( $topic_id, '_bbp_topic_is_solved' ); $redirect = remove_query_arg( [ 'unsolve_topic', '_wpnonce' ] ); wp_safe_redirect( $redirect ); exit; } else { wp_die( esc_html__( 'You do not have the permission to do that!', 'forumax' ) ); } } /** * Called during the plugins_loaded action to filter the capability * required to view solved topics. * * @since 1.0 * * @return void */ public function filter_capability() { $this->capability = apply_filters( 'forumax_solve_topic_capability', $this->capability ); } /** * Solved topic badge * * @since 1.0 * * @return void */ public function solved_badge() { $topic_id = bbp_get_topic_id(); if ( $this->is_solved( $topic_id ) ) { echo ''; } } /** * Outputs the reply checkbox * * @since 1.0 * * @return void */ public function reply_checkbox() { $has_best_answer = $this->has_best_answer( bbp_get_topic_id() ); $is_best_answer = $this->is_best_answer( bbp_get_reply_id() ); if ( bbp_is_reply_edit() && ( current_user_can( $this->capability ) ) ) { if ( ( ! $has_best_answer ) || ( $is_best_answer ) ) { ?>

value="1" tabindex="" />

capability ) ) || ( $topic_author == get_current_user_id() ) ) ) { ?>

is_solved( bbp_get_topic_id() ) ); ?> value="1" tabindex="" />

ID : 0; // Using the global reply id } elseif ( bbp_get_reply_id() ) { $reply_id = bbp_get_reply_id(); // Use the current post id } elseif ( ! bbp_get_reply_id() ) { $reply_id = get_the_ID(); } if ( ! empty( $reply_id ) ) { $retval = get_post_meta( $reply_id, '_bbp_reply_is_best_answer', true ); } return (bool) apply_filters( 'bbp_reply_is_best_answer', (bool) $retval, $reply_id ); } /** * Determines if a reply is marked as solved * * @since 1.0 * * @param $reply_id int The ID of the reply * * @return bool */ public function is_solved( $topic_id = 0 ) { $retval = false; // Checking a specific reply id. if ( ! empty( $topic_id ) ) { $topic = bbp_get_topic( $topic_id ); $topic_id = ! empty( $topic ) ? $topic->ID : 0; // Using the global reply id. } elseif ( bbp_get_topic_id() ) { $topic_id = bbp_get_topic_id(); // Use the current post id. } elseif ( ! bbp_get_topic_id() ) { $topic_id = get_the_ID(); } if ( ! empty( $topic_id ) ) { $retval = get_post_meta( $topic_id, '_bbp_topic_is_solved', true ); } return (bool) apply_filters( 'bbp_topic_is_solved', (bool) $retval, $topic_id ); } /** * Determines if a reply is marked as solved * * @since 1.0 * * @param $reply_id int The ID of the reply * * @return bool */ public function has_best_answer( $topic_id = 0 ) { // if bbpress is not active, return the classes if ( ! function_exists( 'bbp_get_topic_id' ) ) { return false; } $retval = false; // Checking a specific reply id. if ( ! empty( $topic_id ) ) { $topic = bbp_get_topic( $topic_id ); $topic_id = ! empty( $topic ) ? $topic->ID : 0; // Using the global reply id. } elseif ( bbp_get_topic_id() ) { $topic_id = bbp_get_topic_id(); // Use the current post id. } elseif ( ! bbp_get_topic_id() ) { $topic_id = get_the_ID(); } if ( ! empty( $topic_id ) ) { $retval = get_post_meta( $topic_id, '_bbp_topic_has_best_answer', true ); } return (bool) apply_filters( 'bbp_topic_has_best_answer', (bool) $retval, $topic_id ); } /** * Reply content filter * * @since 1.0 * * @param $content string The content of the reply * @param $reply_id int The ID of the reply * * @return string */ public function reply_content_filter( $content = '', $reply_id = 0 ) { if ( empty( $reply_id ) ) { $reply_id = bbp_get_reply_id( $reply_id ); } $topic_id = bbp_get_topic_id(); if ( $this->is_best_answer( $reply_id ) ) { $content = '
' . esc_html__( 'Best Answer', 'forumax' ) . '
' . $content; } return $content; } /** * Topic content filter * * @since 1.0 * * @param $content string The content of the topic * @param $topic_id int The ID of the reply * * @return string */ public function topic_content_filter( $content = '', $topic_id = 0 ) { if ( empty( $topic_id ) ) { $topic_id = bbp_get_topic_id( $topic_id ); } $open = ''; $close = ''; $resolved = ''; $has_best_answer = ''; if ( $this->is_solved( $topic_id ) || $this->has_best_answer( $topic_id ) ) { $open = '
'; $close = '
'; } if ( $this->is_solved( $topic_id ) ) { $resolved = ' ' . esc_html__( 'Resolved', 'forumax' ) . ''; } $content = $content . $open . $resolved . $has_best_answer . $close; return $content; } /** * Adds a new class to replies that are marked as solved * * @since 1.0 * * @param $classes array An array of current class names * * @return bool */ public function reply_post_class( $classes ) { // if bbpress is not active, return the classes if ( ! function_exists( 'bbp_get_reply_id' ) ) { return $classes; } $reply_id = bbp_get_reply_id(); // Only apply the class to replies. if ( bbp_get_reply_post_type() != get_post_type( $reply_id ) ) { return $classes; } if ( $this->is_best_answer( $reply_id ) ) { $classes[] = 'best-answer'; } return $classes; } } // Instantiate the class. $GLOBALS['forumax_solve_topic'] = new forumax_solve_topic(); function forumax_solve_topic(): forumax_solve_topic { return new forumax_solve_topic(); }