• Resolved missveronica

    (@missveronicatv)


    UM Documentation Page: “How to add comment’s count in the Comments tab”

    https://docs.ultimatemember.com/article/1757-add-comment-s-count-in-the-comments-tab

    Issue: Published Code Snippet
    Code Snippet Original:

    https://wordpress.org/support/topic/can-you-add-comments-counter-in-profiles-tab/

    Code snippet only works OK for the Administrator User Role when there are no UM comments restrictions.

    Other User Roles where UM comments restrictions hooks must be included in the processing by WP there will be unlimited callbacks by the um_profile_tabs hook to the code snippet until there is no more PHP memory available:

    PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) ....

    UM Support Forum issue: “Errors and slow site?”

    https://wordpress.org/support/topic/errors-and-slow-site/

    New updated code snippet should use another filter hook um_user_profile_tabs and test for a value already being inserted into $tabs['comments']['notifier'] to avoid extra calls to get_comments.

    Working updated code snippet:

    add_filter( "um_user_profile_tabs", "um_072522_add_comment_count", 1000 );
    
    function um_072522_add_comment_count( $tabs ) {
    
        if( isset( $tabs['comments'] ) && ! isset( $tabs['comments']['notifier'] ) ){ 
            $args = array(
                        'user_id' => um_profile_id(),   // Use user_id.
                        'count'   => true               // Return only the count.
                    );
    
            $comments_count = get_comments( $args );
    
            $tabs['comments']['notifier'] = $comments_count;
        }
    
        return $tabs;
    }
Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter missveronica

    (@missveronicatv)

    @gelieys

    I’m getting this header when using your link to the UM Documentation page:

    Preview Changes will not be visible to customers until published.

    Using a direct page link the page has been updated today Last updated on March 22, 2023
    but the updated code snippet will NOT solve anything and it’s still giving the debug.log file this error message after executing a long time period:

    PHP Fatal error: Allowed memory size ....

    Plugin Support andrewshu

    (@andrewshu)

    Hello @missveronicatv

    Did you check as a member or as a guest?

    There was a fatal error when a guest visit a site and updated code fix this. For members there wasn’t any errors on my site.

    Thank you.

    Thread Starter missveronica

    (@missveronicatv)

    @andrewshu

    Read my description of this issue:

    Code snippet only works OK for the Administrator User Role when there are no UM comments restrictions.

    Other User Roles where UM comments restrictions hooks must be included in the processing by WP there will be unlimited callbacks by the um_profile_tabs hook to the code snippet until there is no more PHP memory available:

    PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) ....

    Plugin Support andrewshu

    (@andrewshu)

    @missveronicatv

    I read your description. But I can’t reproduce this issue for other user roles. Maybe you have some special restrictions.

    Thread Starter missveronica

    (@missveronicatv)

    @andrewshu

    You can read this thread how this issue first was found at a production site, which I could later confirm at my UM testing site. I changed filter hook and reduced calls to get_comments which worked OK at both sites. Non-logged in users were not tested at all.

    All details as stack traces and PHP debug logs when the get_comments function is filling up the stack I used for the issue isolation are posted in this thread:

    https://wordpress.org/support/topic/errors-and-slow-site/

    Thread Starter missveronica

    (@missveronicatv)

    @andrewshu

    Non-logged in users were not tested at all.

    The only trace posted in the other thread was with a non logged in user.

    At my test site I have reproduced this bug with the user logged in as well.
    Error message: 503 Service Unavailable.
    WP 6.1.1
    UM 2.5.4
    User role um_member

    First part of my test system stack trace at the action hook posts_selection

    2023-03-26 08:46:29 Memory Usage Trace for User ID:2 Allocated:84MB Used:81MB
    Current SQL: AND ( 
      wpcb_postmeta.meta_key = 'um_content_restriction'
    ) AND wpcb_posts.post_type IN ('post', 'page') AND ((wpcb_posts.post_status <> 'trash' AND wpcb_posts.post_status <> 'auto-draft'))wpcb_posts.IDwpcb_posts.post_date DESC INNER JOIN wpcb_postmeta ON ( wpcb_posts.ID = wpcb_postmeta.post_id )
    Stack trace:
    #0 wp-includes/class-wp-hook.php(308): posts_selection_memory_usage(' AND ( \n  wpcb_...')
    #1 wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array)
    #2 wp-includes/plugin.php(517): WP_Hook->do_action(Array)
    #3 wp-includes/class-wp-query.php(2921): do_action('posts_selection', ' AND ( \n  wpcb_...')
    #4 wp-includes/class-wp-query.php(3749): WP_Query->get_posts()
    #5 wp-includes/post.php(2415): WP_Query->query(Array)
    #6 wp-content/plugins/ultimate-member/includes/core/class-access.php(204): get_posts(Array)
    #7 wp-content/plugins/ultimate-member/includes/core/class-access.php(852): um\core\Access->exclude_posts_array(true, Array)
    #8 wp-includes/class-wp-hook.php(308): um\core\Access->exclude_posts_comments(Object(WP_Comment_Query))
    #9 wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array)
    #10 wp-includes/plugin.php(565): WP_Hook->do_action(Array)
    #11 wp-includes/class-wp-comment-query.php(394): do_action_ref_array('pre_get_comment...', Array)
    #12 wp-includes/class-wp-comment-query.php(366): WP_Comment_Query->get_comments()
    #13 wp-includes/comment.php(244): WP_Comment_Query->query(Array)
    #14 wp-content/themes/twentytwenty/functions.php(866): get_comments(Array)
    #15 wp-includes/class-wp-hook.php(308): um_072522_add_comment_count(Array)
    
    #16 wp-includes/plugin.php(205): WP_Hook->apply_filters(Array, Array)
    #17 wp-content/plugins/ultimate-member/includes/core/class-profile.php(156): apply_filters('um_profile_tabs', Array)
    #18 wp-content/plugins/ultimate-member/includes/class-config.php(592): um\core\Profile->tabs()
    #19 wp-content/plugins/ultimate-member/includes/class-init.php(1000): um\Config->__construct()
    #20 wp-content/plugins/ultimate-member/includes/um-short-functions.php(1285): UM->config()
    #21 wp-content/plugins/ultimate-member/includes/core/class-access.php(1874): um_is_core_post(Object(WP_Post), 'login')
    #22 wp-content/plugins/ultimate-member/includes/core/class-access.php(2089): um\core\Access->get_post_privacy_settings(Object(WP_Post))
    #23 wp-content/plugins/ultimate-member/includes/core/class-access.php(317): um\core\Access->is_restricted(102)
    #24 wp-content/plugins/ultimate-member/includes/core/class-access.php(852): um\core\Access->exclude_posts_array(true, Array)
    #25 wp-includes/class-wp-hook.php(308): um\core\Access->exclude_posts_comments(Object(WP_Comment_Query))
    #26 wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array)
    #27 wp-includes/plugin.php(565): WP_Hook->do_action(Array)
    #28 wp-includes/class-wp-comment-query.php(394): do_action_ref_array('pre_get_comment...', Array)
    #29 wp-includes/class-wp-comment-query.php(366): WP_Comment_Query->get_comments()
    #30 wp-includes/comment.php(244): WP_Comment_Query->query(Array)
    #31 wp-content/themes/twentytwenty/functions.php(866): get_comments(Array)
    #32 wp-includes/class-wp-hook.php(308): um_072522_add_comment_count(Array)
    
    #33 wp-includes/plugin.php(205): WP_Hook->apply_filters(Array, Array)
    #34 wp-content/plugins/ultimate-member/includes/core/class-profile.php(156): apply_filters('um_profile_tabs', Array)
    #35 wp-content/plugins/ultimate-member/includes/class-config.php(592): um\core\Profile->tabs()
    #36 wp-content/plugins/ultimate-member/includes/class-init.php(1000): um\Config->__construct()
    #37 wp-content/plugins/ultimate-member/includes/um-short-functions.php(1285): UM->config()
    #38 wp-content/plugins/ultimate-member/includes/core/class-access.php(1874): um_is_core_post(Object(WP_Post), 'login')
    #39 wp-content/plugins/ultimate-member/includes/core/class-access.php(2089): um\core\Access->get_post_privacy_settings(Object(WP_Post))
    #40 wp-content/plugins/ultimate-member/includes/core/class-access.php(317): um\core\Access->is_restricted(102)
    #41 wp-content/plugins/ultimate-member/includes/core/class-access.php(852): um\core\Access->exclude_posts_array(true, Array)
    #42 wp-includes/class-wp-hook.php(308): um\core\Access->exclude_posts_comments(Object(WP_Comment_Query))
    #43 wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array)
    #44 wp-includes/plugin.php(565): WP_Hook->do_action(Array)
    #45 wp-includes/class-wp-comment-query.php(394): do_action_ref_array('pre_get_comment...', Array)
    #46 wp-includes/class-wp-comment-query.php(366): WP_Comment_Query->get_comments()
    #47 wp-includes/comment.php(244): WP_Comment_Query->query(Array)
    #48 wp-content/themes/twentytwenty/functions.php(866): get_comments(Array)
    #49 wp-includes/class-wp-hook.php(308): um_072522_add_comment_count(Array)

    Code snippet used

    add_filter( 'um_profile_tabs', 'um_072522_add_comment_count', 10 );
    function um_072522_add_comment_count( $tabs ){
    
    	if ( isset( $tabs['comments'] ) && is_user_logged_in() && ! isset( $tabs['comments']['notifier'] )) {
    		$args = array(
    			'user_id' => um_profile_id(),   // Use user_id.
    			'count'   => true // Return only the count.
    		);
    		$comments_count = get_comments( $args );
    		$tabs['comments']['notifier'] = $comments_count;
    	}
    	return $tabs;
    }

    Changing filter hook to um_user_profile_tabs all works OK.

    Plugin Support andrewshu

    (@andrewshu)

    Hello @missveronicatv

    I appreciate your help, but we can’t change the documentation without review. I read all topics and I can’t reproduce this issue for user roles.

    Could you send me screenshot with the access restriction settings? I will check again.

    Regards.

    Thread Starter missveronica

    (@missveronicatv)

    Plugin Support andrewshu

    (@andrewshu)

    Hello @missveronicatv

    I checked several times and I can’t reproduce this – I checked user with comments and without them, with different restrictions.

    Maybe there are some other options to look out for?

    Regards.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘UM Documentation’ is closed to new replies.