• Daniel

    (@dannisbet)


    I think I may have found an odd bug. I have a fresh install of WP running Twenty Fifteen. I’m running a script that bulk-adds posts to WordPress via the wp_insert_post() function and assigns categories to each. The user can choose multiple if they wish.

    The issue: some of the categories are set to be excluded from the email digests. When I pass a post through wp_insert_post() with only the excluded category selected, Subscribe2 behaves as expected. However, if I select an excluded category along with other categories that are not, the email is being sent out.

    I discovered in class-s2-core.php that the $check flag in the publish() function was holding as “true” through line 444, as it should in this instance, but after it passed through the // do we have subscribers?, it was being reset and causing the email to be sent.

    I ended up re-writing the line to read this instead:

    // do we have subscribers?
    			if($check) {
    				return $post;
    			}
    			elseif ( empty($public) && empty($registered) ) {
    				// if not, no sense doing anything else
    				return $post;
    			}

    …and it resolves the issue. I wanted to report this, as I think it should be something that gets looked at for the next release.

    Again, this only occurs when I’m using the wp_insert_post() function and assigning categories. From the dashboard and posts panels, it behaves as expected.

    https://www.ads-software.com/plugins/subscribe2/

Viewing 3 replies - 1 through 3 (of 3 total)
  • @dannisbet

    I think I understand al that you’ve said above but this fix for your particular setup may produce unexpected results on other sites.

    I think for example, that your fix would result in no email being sent if the post was marked as private even if the setting was made to send posts for private posts as $check would be true.

    I think the issue is more about expected behaviour. Say you have several categories but assign a post to ones called A and B. B is an excluded category but I as a user am subscribed to category A, I, as the subscriber would perhaps expect the email. I think in your description you are saying you would not. Is that correct?

    @dannisbet

    I’ve thought a little more about this and using some of the hooks in Subscribe2 you can ensure that any post assigned to a category that’s excluded (as well as included ones) won’t generate a notifications with this code (added as a new plugin or in functions.php of your theme).

    It’s untested but I think it will work provided I’ve not put any syntax errors in there:

    add_filter( 's2_send_plain_excerpt_subscribers', 'no_excluded_cats', 10, 3);
    add_filter( 's2_send_plain_fullcontent_subscribers', 'no_excluded_cats', 10, 3);
    add_filter( 's2_send_html_excerpt_subscribers', 'no_excluded_cats', 10, 3);
    add_filter( 's2_send_html_fullcontent_subscribers', 'no_excluded_cats', 10, 3);
    
    function no_excluded_cats( $recipients, $post_id ) {
    	global $mysubscribe2;
    	if ( empty( $mysubscribe2->subscribe2_options['exclude'] ) ) { return $recipients; }
    
    	$s2_taxonomies = apply_filters( 's2_taxonomies', array( 'category' ) );
    	$post_cats = wp_get_object_terms( $post_id, $s2_taxonomies, array( 'fields' => 'ids' ) );
    
    	foreach ( explode( ',', $mysubscribe2->subscribe2_options['exclude'] ) as $cat ) {
    		if ( in_array( $cat, $post_cats ) ) {
    			return array();
    		}
    	}
    	return recipients;
    }
    Thread Starter Daniel

    (@dannisbet)

    @mattyrob

    You have the correct assumption in your first reply: if a category that’s excluded from emails is included in the post’s categories, a message should not send.

    I’ll test the code in your second email and see how that works out.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘$check being reset in class-s2-core.php’ is closed to new replies.