bbp_edit_topic_tag_handler( string $action = '' )

Handles the front end tag management (renaming, merging, destroying)

Description

Parameters

$action

(Optional) The requested action to compare this function to

Default value: ''

Source

File: bp-forums/topics/functions.php

1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
function bbp_edit_topic_tag_handler( $action = '' ) {
 
    // Bail if required POST actions aren't passed
    if ( empty( $_POST['tag-id'] ) )
        return;
 
    // Setup possible get actions
    $possible_actions = array(
        'bbp-update-topic-tag',
        'bbp-merge-topic-tag',
        'bbp-delete-topic-tag'
    );
 
    // Bail if actions aren't meant for this function
    if ( !in_array( $action, $possible_actions ) )
        return;
 
    // Setup vars
    $tag_id = (int) $_POST['tag-id'];
    $tag    = get_term( $tag_id, bbp_get_topic_tag_tax_id() );
 
    // Tag does not exist
    if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
        bbp_add_error( 'bbp_manage_topic_invalid_tag', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while getting the tag: %s', 'buddyboss' ), $tag->get_error_message() ) );
        return;
    }
 
    // What action are we trying to perform?
    switch ( $action ) {
 
        // Update tag
        case 'bbp-update-topic-tag' :
 
            // Nonce check
            if ( ! bbp_verify_nonce_request( 'update-tag_' . $tag_id ) ) {
                bbp_add_error( 'bbp_manage_topic_tag_update_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'buddyboss' ) );
                return;
            }
 
            // Can user edit topic tags?
            if ( !current_user_can( 'edit_topic_tags' ) ) {
                bbp_add_error( 'bbp_manage_topic_tag_update_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the discussion tags.', 'buddyboss' ) );
                return;
            }
 
            // No tag name was provided
            if ( empty( $_POST['tag-name'] ) || !$name = $_POST['tag-name'] ) {
                bbp_add_error( 'bbp_manage_topic_tag_update_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'buddyboss' ) );
                return;
            }
 
            // Attempt to update the tag
            $slug = !empty( $_POST['tag-slug'] ) ? $_POST['tag-slug'] : '';
            $tag  = wp_update_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'name' => $name, 'slug' => $slug ) );
 
            // Cannot update tag
            if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
                bbp_add_error( 'bbp_manage_topic_tag_update_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while updating the tag: %s', 'buddyboss' ), $tag->get_error_message() ) );
                return;
            }
 
            // Redirect
            $redirect = get_term_link( $tag_id, bbp_get_topic_tag_tax_id() );
 
            // Update counts, etc...
            do_action( 'bbp_update_topic_tag', $tag_id, $tag, $name, $slug );
 
            break;
 
        // Merge two tags
        case 'bbp-merge-topic-tag'  :
 
            // Nonce check
            if ( ! bbp_verify_nonce_request( 'merge-tag_' . $tag_id ) ) {
                bbp_add_error( 'bbp_manage_topic_tag_merge_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'buddyboss' ) );
                return;
            }
 
            // Can user edit topic tags?
            if ( !current_user_can( 'edit_topic_tags' ) ) {
                bbp_add_error( 'bbp_manage_topic_tag_merge_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the discussion tags.', 'buddyboss' ) );
                return;
            }
 
            // No tag name was provided
            if ( empty( $_POST['tag-existing-name'] ) || !$name = $_POST['tag-existing-name'] ) {
                bbp_add_error( 'bbp_manage_topic_tag_merge_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'buddyboss' ) );
                return;
            }
 
            // If term does not exist, create it
            if ( !$tag = term_exists( $name, bbp_get_topic_tag_tax_id() ) )
                $tag = wp_insert_term( $name, bbp_get_topic_tag_tax_id() );
 
            // Problem inserting the new term
            if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
                bbp_add_error( 'bbp_manage_topic_tag_merge_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while merging the tags: %s', 'buddyboss' ), $tag->get_error_message() ) );
                return;
            }
 
            // Merging in to...
            $to_tag = $tag['term_id'];
 
            // Attempting to merge a tag into itself
            if ( $tag_id === $to_tag ) {
                bbp_add_error( 'bbp_manage_topic_tag_merge_same', __( '<strong>ERROR</strong>: The tags which are being merged can not be the same.', 'buddyboss' ) );
                return;
            }
 
            // Delete the old term
            $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'default' => $to_tag, 'force_default' => true ) );
 
            // Error merging the terms
            if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
                bbp_add_error( 'bbp_manage_topic_tag_merge_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while merging the tags: %s', 'buddyboss' ), $tag->get_error_message() ) );
                return;
            }
 
            // Redirect
            $redirect = get_term_link( (int) $to_tag, bbp_get_topic_tag_tax_id() );
 
            // Update counts, etc...
            do_action( 'bbp_merge_topic_tag', $tag_id, $to_tag, $tag );
 
            break;
 
        // Delete tag
        case 'bbp-delete-topic-tag' :
 
            // Nonce check
            if ( ! bbp_verify_nonce_request( 'delete-tag_' . $tag_id ) ) {
                bbp_add_error( 'bbp_manage_topic_tag_delete_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'buddyboss' ) );
                return;
            }
 
            // Can user delete topic tags?
            if ( !current_user_can( 'delete_topic_tags' ) ) {
                bbp_add_error( 'bbp_manage_topic_tag_delete_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to delete the discussion tags.', 'buddyboss' ) );
                return;
            }
 
            // Attempt to delete term
            $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id() );
 
            // Error deleting term
            if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
                bbp_add_error( 'bbp_manage_topic_tag_delete_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while deleting the tag: %s', 'buddyboss' ), $tag->get_error_message() ) );
                return;
            }
 
            // We don't have any other place to go other than home! Or we may die because of the 404 disease
            $redirect = home_url();
 
            // Update counts, etc...
            do_action( 'bbp_delete_topic_tag', $tag_id, $tag );
 
            break;
    }
 
    /** Successful Moderation *************************************************/
 
    // Redirect back
    $redirect = ( !empty( $redirect ) && !is_wp_error( $redirect ) ) ? $redirect : home_url();
    wp_safe_redirect( $redirect );
 
    // For good measure
    exit();
}

Changelog

Changelog
Version Description
bbPress (r2768) Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.