Skip to:
Content
Pages
Categories
Search
Top
Bottom

Checkbox for e-mail subscription + ideas for better backend

  • E-mail subscription is one thing that most of my users miss. In trunk we have this built-in, but problem is that we can only have a link on which users could click to subscribe/unsubscribe and no checkboxes bellow post’s form or automatic subscription.

    I inspected trunk to see how this could be solved and found that there aren’t any hooks that could be used. Only way is to copy existing functions used for subscription and editing them so that they could work as we want.

    So I tried to follow this aproach and make function which would add checkboxes. I took inspiration from _ck_’s Subscribe to Topic plugin.

    First thing, I wanted checkbox to show option depending on if user is already subscribed or not. By hacking function bb_user_subscribe_link I succeed in this. Here is a code:

    function bb_user_subscribe_checkbox() {

    global $topic, $bb_current_user, $bbdb, $bb;

    if ( !bb_is_user_logged_in() )

    return false;

    $there = false;

    if ( $term_id = $bbdb->get_var( “SELECT term_id FROM $bbdb->terms WHERE slug = ‘topic-$topic->topic_id'” ) ) {

    $term_taxonomy_ids = $bbdb->get_col( “SELECT term_taxonomy_id FROM $bbdb->term_taxonomy WHERE term_id = $term_id AND taxonomy = ‘bb_subscribe'” );

    $term_taxonomy_ids = join(‘,’, $term_taxonomy_ids );

    $there = $bbdb->get_var( “SELECT object_id FROM $bbdb->term_relationships WHERE object_id = $bb_current_user->ID AND term_taxonomy_id IN ( $term_taxonomy_ids )” );

    }

    if ( $there )

    echo ‘<label><input name=”subscription_checkbox” type=”checkbox” value=”remove” ‘ . checked_on_front(false, $there) . ‘>Remove my e-mail subscription for this thread</label>’;

    else

    echo ‘<label><input name=”subscription_checkbox” type=”checkbox” value=”add” ‘ . checked_on_front(false, $there) . ‘>Notify me of followup posts via e-mail</label>’;

    }

    function checked_on_front( $checked, $current) {

    if ( $checked == $current)

    return ‘ checked=”checked”‘;

    }

    add_action(‘edit_form’,’bb_user_subscribe_checkbox’,9);

    add_action(‘post_form’,’bb_user_subscribe_checkbox’,9);

    As I said above, this works. But problem is with function that should take value of checkbox and subscribe or unsubscribe user. I used code from file bb-includes/action.subscribe.php but without succes. Here is it:

    function bb_checkbox_subscription_update($post_id=0) {

    global $bbdb, $bb_current_user, $bb_post, $topic, $wp_taxonomy_object;

    $bb_post=bb_get_post($post_id);

    if (!empty($_REQUEST)) {

    $checkbox_status = $_REQUEST;

    if ( ‘add’ == $checkbox_status ) {

    $tt_ids = $wp_taxonomy_object->set_object_terms( $bb_current_user->ID, ‘topic-‘ . $topic->topic_id, ‘bb_subscribe’, array( ‘append’ => true, ‘user_id’ => $bb_current_user->ID ) );

    } elseif ( ‘remove’ == $checkbox_status ) {

    // I hate this with the passion of a thousand suns

    $term_id = $bbdb->get_var( “SELECT term_id FROM $bbdb->terms WHERE slug = ‘topic-$topic->topic_id'” );

    $term_taxonomy_id = $bbdb->get_var( “SELECT term_taxonomy_id FROM $bbdb->term_taxonomy WHERE term_id = $term_id AND taxonomy = ‘bb_subscribe'” );

    $bbdb->query( “DELETE FROM $bbdb->term_relationships WHERE object_id = $bb_current_user->ID AND term_taxonomy_id = $term_taxonomy_id” );

    $bbdb->query( “DELETE FROM $bbdb->term_taxonomy WHERE term_id = $term_id AND taxonomy = ‘bb_subscribe'” );

    }

    }

    }

    add_action( ‘bb_insert_post’, ‘bb_checkbox_subscription_update’);

    It does nothing. State of subscription of user is not changed but I also do not get any errors.

    Is there any way to change this code so that it becomes useful?

    Apart from above, after inspecting this code, I think that this could be solved much better, by making more functions and hooks which could be used by developers.

    First, here is how subscription works. (links are to current revison, 2421).

    In topic.php file of template, we call function bb_user_subscribe_link. This function is placed in bb-includes/funtions.templates.php and what it does is to show link with subscribe/unsubscribe action. Checking for state of subscription is done inside this function. Those links make GET request so in bb-load.php there is check for them which includes file bb-includes/action.subscribe.php. In this file there is function which adds values for subscription in database, based on request from link above, and finally redirects user to topic’s page.

    Sending of notification is done via function bb_notify_subscribers in file bb-includes/functions.bb-posts.php. This function is called by action bb_new_post in bb-includes/default.bb-filters.php.

    As you can see, we can’t use functions for this nor could we add hooks. My idea is following:

    • splitting function bb_user_subscribe_link into one that does checking status of subscription for cuurent user, so that we can use it like in my example with checkboxes, and one which would only return link
    • in bb-includes/action.subscribe.php (or in other functions file) make function that would do subscribing/unsubscribing in database so that it could be called on other places like in my example above; rest of file would be used as it now, with difference that we call function from last sentence
    • in function bb_notify_subscribers add hook before sending e-mail so that we can, for example, have plugin which sends only one meesage to user, untill he visit topic again (as in other forum software, requested here)
    • add bb_user_subscribe_link inside if function which checks if e-mail subscription is turned on globaly by administrator (there are forum owners who don’t want this feature for any of reasons; would be turned on by default)

    What are others thinking about this, especialy those involved with development (commiters, patch-makers)? Could those solutions be made and included? I think that there isn’t much work for it because it is only hacking of existing code.

Viewing 6 replies - 1 through 6 (of 6 total)
  • I have found solution for problem. Since I based form on Subscribe to Topics, I mistakenly used it’s arguments. So working function should be:

    function bb_checkbox_subscription_update($post_id) {

    global $bbdb, $bb_current_user, $bb_post, $topic, $wp_taxonomy_object;

    $bb_post=bb_get_post($post_id);

    if (!empty($_REQUEST)) {

    $checkbox_status = $_REQUEST;

    if ( ‘add’ == $checkbox_status ) {

    $tt_ids = $wp_taxonomy_object->set_object_terms( $bb_current_user->ID, ‘topic-‘ . $bb_post->topic_id, ‘bb_subscribe’, array( ‘append’ => true, ‘user_id’ => $bb_current_user->ID ) );

    } elseif ( ‘remove’ == $checkbox_status ) {

    // I hate this with the passion of a thousand suns

    $term_id = $bbdb->get_var( “SELECT term_id FROM $bbdb->terms WHERE slug = ‘topic-$bb_post->topic_id'” );

    $term_taxonomy_id = $bbdb->get_var( “SELECT term_taxonomy_id FROM $bbdb->term_taxonomy WHERE term_id = $term_id AND taxonomy = ‘bb_subscribe'” );

    $bbdb->query( “DELETE FROM $bbdb->term_relationships WHERE object_id = $bb_current_user->ID AND term_taxonomy_id = $term_taxonomy_id” );

    $bbdb->query( “DELETE FROM $bbdb->term_taxonomy WHERE term_id = $term_id AND taxonomy = ‘bb_subscribe'” );

    }

    }

    }

    Also, I made a code for improvements I suggested in first post. You can see it here. Also check this ticket.


    Milan Dinić
    Participant

    @dimadin

    chrishajer just committed patches that improve subscription feature. Now we have a lot of new features:

    • option to disable/enable subscription in Options>Discussion
    • function for changing subscription status for user on topic (which means it could be used in plugins/themes
    • function for retrieving subscription status of user for topic
    • checkbox below post form for change of subscription status
    • links for subscription are now added to topicmeta action so on most if not all themes it will be shown without need for theme edit
    • a lot of filters/actions so you can remove features, change text etc
    • everything is well documented

    What this means is that now users don’t need to receive emails for every post since there are hooks in function that sends emails. Developers could make new features like sending digest email, one notification until user visits topic, page with list of subscribed topics for user. option to auto subscribe user to particular forum/tag (useful for plugin developers etc) and other.

    What you can also expect is that today or tomorrow you’ll again have option to subscribe to topics on this forum.

    Thanks to Gautam for a lot of improvements to my patch(es) and to Chris who committed final one.


    kevinjohngallagher
    Member

    @kevinjohngallagher

    Great!

    Nice one, and a thanks to everyone involved


    chrishajer
    Participant

    @chrishajer

    // I hate this with the passion of a thousand suns

    I love that comment

    LOL at same comment, which I just ran across in the code.


    Gautam Gupta
    Participant

    @gautamgupta

    LOL x 2 :P

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar