bbp_favorites_handler (字符串美元的行动='')
处理前端添加和删除喜爱的主题
描述
参数
- 美元的行动
-
(字符串)(可选)请求将此函数与之进行比较的操作
默认值:“
源
函数bbp_favorites_handler($action = ") {if (!bbp_is_favorites_active())返回false;if (empty($_GET['topic_id'])) return;$possible_actions = array('bbp_favorite_add', 'bbp_favorite_remove',);if (!in_array($action, $possible_actions)) return;//发生了什么动作?$_GET['topic_id'] = intval($_GET['topic_id']);$user_id = bbp_get_user_id(0, true, true);//检查空主题if (empty($topic_id)) {bbp_add_error('bbp_favorite_topic_id', __('ERROR: No topic was found!你最喜欢的主题是什么?', 'buddyboss'));//检查nonce} elseif (! bbp_verify_nonce_request( 'toggle-favorite_' . $topic_id ) ) { bbp_add_error( 'bbp_favorite_nonce', __( 'ERROR: Are you sure you wanted to do that?', 'buddyboss' ) ); // Check current user's ability to edit the user } elseif ( !current_user_can( 'edit_user', $user_id ) ) { bbp_add_error( 'bbp_favorite_permissions', __( 'ERROR: You don\'t have the permission to edit favorites of that user!', 'buddyboss' ) ); } // Bail if errors if ( bbp_has_errors() ) return; /** No errors *************************************************************/ $is_favorite = bbp_is_user_favorite( $user_id, $topic_id ); $success = false; if ( true === $is_favorite && 'bbp_favorite_remove' === $action ) $success = bbp_remove_user_favorite( $user_id, $topic_id ); elseif ( false === $is_favorite && 'bbp_favorite_add' === $action ) $success = bbp_add_user_favorite( $user_id, $topic_id ); // Do additional favorites actions do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action ); // Success! if ( true === $success ) { // Redirect back from whence we came if ( bbp_is_favorites() ) { $redirect = bbp_get_favorites_permalink( $user_id ); } elseif ( bbp_is_single_user() ) { $redirect = bbp_get_user_profile_url(); } elseif ( is_singular( bbp_get_topic_post_type() ) ) { $redirect = bbp_get_topic_permalink( $topic_id ); } elseif ( is_single() || is_page() ) { $redirect = get_permalink(); } else { $redirect = get_permalink( $topic_id ); } wp_safe_redirect( $redirect ); // For good measure exit(); // Fail! Handle errors } elseif ( true === $is_favorite && 'bbp_favorite_remove' === $action ) { bbp_add_error( 'bbp_favorite_remove', __( 'ERROR: There was a problem removing that discussion from favorites!', 'buddyboss' ) ); } elseif ( false === $is_favorite && 'bbp_favorite_add' === $action ) { bbp_add_error( 'bbp_favorite_add', __( 'ERROR: There was a problem favoriting that discussion!', 'buddyboss' ) ); } }