/**
 * Loads the correct fields for the selected actions under Profile > Actions.
 *
 * @since 1.0.0
 * @param $data str. The type of action being requested.
 * @return html. The fields.
 */
function bbconnect_get_post_to_edit()
{
    if (!wp_verify_nonce($_POST['bbconnect_admin_nonce'], 'bbconnect-admin-nonce')) {
        die(__('Terribly sorry.', 'bbconnect'));
    }
    if (isset($_POST['data'])) {
        $cid = $_POST['cid'];
        $type = $_POST['type'];
        $post_val = array();
        global $post;
        // WE'RE EDITING AN EXISTING FILE
        if (false !== strpos($_POST['data'], 'edit-')) {
            $post_id = (int) substr($_POST['data'], 5);
            $post = get_post($post_id);
            $post_author = $post->post_author;
            $post_type = $post->post_type;
            $action = 'edit';
            if ($post_type == 'bb_note') {
                $note_types = wp_get_post_terms($post_id, 'bb_note_type');
                foreach ($note_types as $note_type) {
                    if ($note_type->parent == 0) {
                        $parent_term = $note_type;
                    } else {
                        $child_term = $note_type;
                    }
                }
            }
        } else {
            if ('user' == $type) {
                $bbconnect_actions = bbconnect_get_user_actions();
                $post_type = 'bb_note';
                if (empty($_POST['data'])) {
                    echo '';
                    die;
                }
            } else {
                $bbconnect_actions = bbconnect_get_post_actions();
                $post_type = false;
                $post_type = $_POST['data'];
                if (false == $post_type) {
                    echo '';
                    die;
                }
            }
            $post = get_default_post_to_edit($post_type, true);
            $post_author = $_POST['uid'];
            $action = $_POST['actung'];
        }
        // SET THE NONCE
        if ('user' == $type) {
            bbconnect_user_actions_nonce_field();
        } else {
            bbconnect_post_actions_nonce_field();
        }
        $post_fields = array(array('meta' => array('source' => 'wpr', 'meta_key' => 'post_title', 'name' => __('Title', 'bbconnect'), 'help' => '', 'options' => array('field_type' => 'text', 'req' => true, 'public' => false, 'choices' => false))), array('meta' => array('source' => 'wpr', 'meta_key' => 'post_date', 'name' => __('Date', 'bbconnect'), 'help' => '', 'options' => array('field_type' => 'date', 'req' => true, 'public' => false, 'choices' => false))));
        if ('user' == $type) {
            if (!empty($parent_term)) {
                // Editing existing note
                $post_fields[] = array('post_val' => $parent_term->slug, 'meta' => array('source' => 'tax', 'meta_key' => 'bb_note_type_parent', 'name' => __('Type', 'bbconnect'), 'help' => '', 'options' => array('field_type' => 'select', 'req' => true, 'public' => true, 'choices' => array($parent_term->slug => $parent_term->name))));
                $post_fields[] = array('post_val' => $child_term->slug, 'meta' => array('source' => 'tax', 'meta_key' => 'bb_note_type', 'name' => __('', 'bbconnect'), 'help' => '', 'options' => array('field_type' => 'select', 'req' => false, 'public' => true, 'choices' => array($child_term->slug => $child_term->name))));
            } else {
                // New note
                $parent_term = get_term_by('slug', $_POST['data'], 'bb_note_type');
                $terms = get_terms('bb_note_type', array('hide_empty' => false, 'parent' => $parent_term->term_id));
                $choices = array();
                foreach ($terms as $term) {
                    $choices[$term->slug] = $term->name;
                }
                $post_fields[] = array('meta' => array('source' => 'tax', 'meta_key' => 'bb_note_type', 'name' => __('Type', 'bbconnect'), 'help' => '', 'options' => array('field_type' => 'select', 'req' => true, 'public' => false, 'choices' => $choices)));
                $post_fields[] = array('meta' => array('source' => 'tax', 'meta_key' => 'bb_note_type_parent', 'name' => __('', 'bbconnect'), 'help' => '', 'options' => array('field_type' => 'hidden', 'req' => false, 'public' => false, 'choices' => $parent_term->slug)));
            }
        }
        foreach ($post_fields as $field) {
            $meta_key = $field['meta']['meta_key'];
            $field['type'] = 'post';
            $field['action'] = $action;
            $field['id'] = $post->ID;
            $field['swap_name'] = $meta_key;
            if (isset($post->{$meta_key})) {
                $field['post_val'] = $post->{$meta_key};
            }
            echo '<p><ul style="display: block; float: none;">';
            bbconnect_get_field($field);
            echo '</ul></p>';
        }
        if ('bulk-edit' == $action) {
            echo '<ul><li class="meta-item"><span class="bbconnect-label">';
            echo '<a class="rui off" title="' . $cid . 'bulk-edit">Enable Text</a>';
            echo '</span><span class="bbconnect-field">';
        }
        echo '<div style="width: 90%;padding: .3em;margin: .2em 0;">&nbsp;</div>';
        //echo '<p>'. __( 'Title', 'bbconnect' ) .'<br /><input type="text" name="post_title" class="regular-text" value="'.$post->post_title.'" /></p>';
        //echo '<p>'. __( 'Date', 'bbconnect' ) .'<br /><input type="text" class="bbconnect-date" name="post_date" class="regular-text" value="'.$post->post_date.'" /></p>';
        if (preg_match('/Firefox/i', $_SERVER['HTTP_USER_AGENT'])) {
            wp_editor(stripslashes($post->post_content), $cid, array('tinymce' => false, 'textarea_name' => 'post_content', 'teeny' => true, 'quicktags' => true));
        } else {
            wp_editor(stripslashes($post->post_content), $cid, array('tinymce' => true, 'textarea_name' => 'post_content', 'teeny' => false, 'quicktags' => true));
        }
        if ('bulk-edit' == $action) {
            echo '</span></li></ul>';
        }
        // SET THE META
        if ('user' == $type) {
            bbconnect_user_actions_meta_fields(array('post_id' => $post->ID, 'fields' => bbconnect_get_user_actions_meta(), 'action' => $action, 'post_val' => $post_val));
        } else {
            bbconnect_post_actions_meta_fields(array('post_id' => $post->ID, 'fields' => bbconnect_get_post_actions_meta(), 'action' => $action, 'post_val' => $post_val));
        }
        ?>
        <input type="hidden" name="post_ID" value="<?php 
        echo $post->ID;
        ?>
" />
        <input type="hidden" name="post_status" value="publish" />
        <input type="hidden" name="post_author" value="<?php 
        echo $post_author;
        ?>
" />
        <input type="hidden" name="post_type" value="<?php 
        echo $post_type;
        ?>
" />
        <?php 
        $inline_button = apply_filters('bbconnect_inline_do_action_button', array('<input type="submit" class="bbconnect-actions-save button-primary ' . $type . '" name="save" value="' . __('Save', 'bbconnect') . '" />'), $post_type, $type, $action);
        echo '<div class="tright">';
        echo implode(' ', $inline_button);
        echo '</div>';
    } else {
        echo 'error';
    }
    if ('3.9' <= get_bloginfo('version')) {
        _WP_Editors::enqueue_scripts();
        //print_footer_scripts();
        _WP_Editors::editor_js();
        echo '<script src="' . admin_url('js/editor.js') . '" />';
    }
    die;
}
/**
 * Displays a standardized context meta box for Actions.
 *
 * @since 1.0.2
 *
 * @param obj $post. The current post data.
 *
 * @return html output or the custom meta panel.
 */
function bbconnect_user_actions_meta_box($post)
{
    bbconnect_user_actions_nonce_field();
    bbconnect_user_actions_meta_fields(array('post_id' => $post->ID, 'fields' => bbconnect_get_user_actions_meta()));
}