Last active
August 29, 2015 14:23
-
-
Save dtbaker/c06c2b03a67e46969e7e to your computer and use it in GitHub Desktop.
Code snippet to get bbPress talking with Support Hub correctly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this code does two things: | |
// 1) sets the 'support_hub' return parameter to the xmlrpc getUser request. In here we can include additional options to display within Support Hub when replying to a bbPress post. Currently we have 'Mark Thread as Resolved' as an option. This option gets passed back to bbPress via XMLRPC when replying to a post from Support Hub, so we can action this. | |
// 2) a hook for xmlrpc insert post to detect if we're posting a forum thread. This runs the standard bbPress update hooks to ensure all the bbPress meta values are updated correctly after posting a reply. We also look for any custom options (e.g. mark as resolved) and perform actions based on those as well. | |
// UPDATE: instead of adding this code, try using this bbPress Support plugin: https://github.com/dtbaker/bbPress-Support-Forums it does everything (making threads as resolved, feature voting, etc..) | |
// this is for SupportHub to operate correctly, passing the envato_codes as a meta key along with user requests. | |
//apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields ) | |
add_filter('xmlrpc_prepare_user', '_bbps_xmlrpc_prepare_user', 10, 3); | |
function _bbps_xmlrpc_prepare_user($_user, $user, $fields){ | |
$envato_codes = get_user_meta( $_user['user_id'], 'envato_codes', true ); | |
if ( is_array( $envato_codes ) ) { | |
$_user['envato_codes'] = $envato_codes; | |
} | |
$_user['support_hub'] = array( | |
'reply_options' => array( | |
array( | |
'title' => 'Mark Thread as Resolved', | |
'field' => array( | |
'type' => 'check', | |
'value' => 'resolved', | |
'name' => 'thread_resolved', | |
'checked' => true, | |
) | |
) | |
) | |
); | |
return $_user; | |
} | |
//do_action( 'wp_insert_post', $post_ID, $post, $update ); | |
add_action('wp_insert_post','_bbps_wp_insert_post',10,3); | |
function _bbps_wp_insert_post($post_ID, $post, $update){ | |
if($post->post_status == 'publish' && $post->post_type == 'reply'){ | |
$check = get_post_meta($post_ID, 'support_hub', true); | |
$forum_id = bbp_get_topic_forum_id( $post->post_parent ); | |
if($check){ | |
$data = @json_decode($check,true); | |
if(!is_array($data)){ | |
$data = array(); | |
} | |
if(!isset($data['done']) || !$data['done']) { | |
$data['done'] = true; | |
update_post_meta($post_ID, 'support_hub', json_encode($data)); // change the flag so we don't double process during this hook | |
// run the usual bbpress hooks to update thread counts, send email replies and other stuff | |
do_action('bbp_new_reply', $post_ID, $post->post_parent, $forum_id, 0, $post->post_author, false, 0); | |
do_action('bbp_new_reply_post_extras', $post_ID); | |
// todo: only set thread status to resolved if ticked in extra data above. | |
if(isset($data['thread_resolved']) && $data['thread_resolved'] == 'resolved'){ | |
update_post_meta($post->post_parent, '_bbps_topic_status', 2); // update the thread status to resolved | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment