bbp_admin_repair_user_favorites()

Clean the users’ favorites

Description

Return

(array) An array of the status code and the message

Source

File: bp-forums/admin/tools.php

841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
function bbp_admin_repair_user_favorites() {
    global $wpdb;
 
    $statement = __( 'Removing trashed discussions from user favorites… %s', 'buddyboss' );
    $result    = __( 'Failed!', 'buddyboss' );
    $key       = $wpdb->prefix . '_bbp_favorites';
    $users     = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `favorites` FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';" );
 
    if ( is_wp_error( $users ) )
        return array( 1, sprintf( $statement, $result ) );
 
    $topics = $wpdb->get_col( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "';" );
 
    if ( is_wp_error( $topics ) )
        return array( 2, sprintf( $statement, $result ) );
 
    $values = array();
    foreach ( $users as $user ) {
        if ( empty( $user->favorites ) || !is_string( $user->favorites ) )
            continue;
 
        $favorites = array_intersect( $topics, explode( ',', $user->favorites ) );
        if ( empty( $favorites ) || !is_array( $favorites ) )
            continue;
 
        $favorites_joined = implode( ',', $favorites );
        $values[]         = "('{$user->user_id}', '{$key}, '{$favorites_joined}')";
 
        // Cleanup
        unset( $favorites, $favorites_joined );
    }
 
    if ( !count( $values ) ) {
        $result = __( 'Nothing to remove!', 'buddyboss' );
        return array( 0, sprintf( $statement, $result ) );
    }
 
    $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';";
    if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
        return array( 4, sprintf( $statement, $result ) );
 
    foreach ( array_chunk( $values, 10000 ) as $chunk ) {
        $chunk = "\n" . implode( ",\n", $chunk );
        $sql_insert = "INSERT INTO `$wpdb->usermeta` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;";
        if ( is_wp_error( $wpdb->query( $sql_insert ) ) ) {
            return array( 5, sprintf( $statement, $result ) );
        }
    }
 
    return array( 0, sprintf( $statement, __( 'Complete!', 'buddyboss' ) ) );
}

Changelog

Changelog
Version Description
bbPress (r2613) 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.