/**
 * Saves for the selected actions under Profile > Actions.
 *
 * @since 1.0.0
 * @param $data arr. The POST data.
 * @return html. The content and miscellaneous followups.
 */
function bbconnect_save_new_post()
{
    if (!wp_verify_nonce($_POST['bbconnect_admin_nonce'], 'bbconnect-admin-nonce')) {
        die(__('Terribly sorry.', 'bbconnect'));
    }
    if (isset($_POST['data'])) {
        parse_str($_POST['data'], $postarr);
        $sid = $_POST['sid'];
        $type = $_POST['type'];
        if (isset($postarr['post_ID'])) {
            $postarr['ID'] = $postarr['post_ID'];
            //unset( $postarr['post_ID'] );
        }
        $pid = wp_insert_post($postarr);
        //         $postarr['id'] = $pid;
        bbconnect_save_action_meta(array('post_data' => $postarr, 'override' => true));
        $act = get_post($pid);
        $class = apply_filters('bbconnect_save_new_class', array(), $act);
        bbconnect_profile_action_item(array('act' => $act, 'type' => $type, 'new' => true, 'class' => $class, 'ok_edit' => true, 'action' => 'edit', 'bbconnect_cap' => 'admin'));
        ?>
        <script type="text/javascript">
        jQuery(document).ready(function () {
            // IF THE ELEMENT EXISTS, REMOVE IT & THE PARENT
            if ( jQuery('#post-<?php 
        echo $pid;
        ?>
').length != 0 ) {
                jQuery('#post-<?php 
        echo $pid;
        ?>
').parent('li').remove();
            }

            // CLONE THE NEW ELEMENT
            jQuery('.new-post').clone().prependTo('#<?php 
        echo $sid;
        ?>
 .actions-history-list');

            // UPDATE AND REMOVE THE NEW-POST IDENTIFIER
            jQuery('#<?php 
        echo $sid;
        ?>
 .actions-history-list .new-post').each(function(){
                jQuery(this).find('div[title="post-<?php 
        echo $pid;
        ?>
"]').attr('id','post-<?php 
        echo $pid;
        ?>
').attr('title','');
                jQuery(this).removeClass('new-post').fadeIn('fast');
            });

            // UPGRADE THE ORIGINAL
            var newparent = jQuery('.new-post').parent('.bbconnect-viewer').attr('id');
            jQuery('#'+newparent+' .new-post').each(function(){
                jQuery(this).find('div[title="post-<?php 
        echo $pid;
        ?>
"]').attr('id','new-post-<?php 
        echo $pid;
        ?>
').attr('title','');
                jQuery('#new-post-<?php 
        echo $pid;
        ?>
').prependTo('#'+newparent).fadeIn('fast');
                jQuery(this).remove();
            });

        });
        </script>
    <?php 
    } else {
        echo 'error';
    }
    die;
}
function bbconnect_actions_editor($args = null)
{
    global $pagenow, $current_user;
    // SET THE DEFAULTS TO BE OVERRIDDEN AS DESIRED
    $defaults = array('user_id' => false, 'bbconnect_cap' => false, 'action' => false, 'list_only' => false, 'action_only' => false, 'embed' => false, 'embed_id' => false, 'cid' => 'post_content', 'get_actions' => 'bbconnect_get_user_actions', 'type' => 'user', 'ok_edit' => false);
    // PARSE THE INCOMING ARGS
    $args = wp_parse_args($args, $defaults);
    // EXTRACT THE VARIABLES
    extract($args, EXTR_SKIP);
    if (false == $bbconnect_cap || false == $action) {
        return false;
    }
    if (false != $embed && false == $embed_id) {
        return false;
    }
    //if ( isset( $_GET['user_id'] ) ) && $user_id == false ) {
    if ($action_only) {
        if (is_int($action_only)) {
            $one_style = ' style="width: ' . (96 - $action_only) . '%;"';
            $two_style = ' style="width: ' . $action_only . '%;"';
        } else {
            $one_style = ' style="display: none;"';
            $two_style = ' style="width: 96%;"';
        }
    } else {
        if ($list_only) {
            if (is_int($list_only)) {
                $two_style = ' style="width: ' . (96 - $list_only) . '%;"';
                $one_style = ' style="width: ' . $list_only . '%;"';
            } else {
                $two_style = ' style="display: none;"';
                $one_style = ' style="width: 96%;"';
            }
        } else {
            $two_style = '';
            $one_style = '';
        }
    }
    // CHECK TO SEE IF THE CURRENT USER CAN EDIT THE TARGET USER
    if (false == $ok_edit) {
        $ok_edit = bbconnect_user_can('edit_user', array('one' => $current_user->ID, 'two' => $user_id));
    }
    $bbconnect_actions = call_user_func($get_actions);
    if (!empty($bbconnect_actions)) {
        // PULL ALL POSSIBLE ACTIONS AND COMPILE THE LAUNCHER
        $bbconnect_action_types = array();
        $buttons = '';
        $bbconnect_action_launcher = array();
        $args = array('hide_empty' => false);
        $terms = get_terms('bb_note_type', $args);
        $default_terms = array();
        foreach ($terms as $term) {
            if ($term->parent == 0) {
                $bbconnect_action_launcher_left[$term->slug] = $term->name;
                $bbconnect_action_launcher_right[$term->slug] = $term->name;
                if ($term->slug == 'system') {
                    $system_term = $term->term_id;
                }
            } else {
                $child_actions[$term->parent][] = $term;
            }
            // PULL THE ARRAY FOR QUERYING
            $bbconnect_action_types[] = $term->slug;
            $is_default = (bool) get_tax_meta($term->term_id, 'initially_displayed');
            if ($is_default) {
                $default_terms[] = $term->term_id;
            }
        }
        // Now add the children of System to the filter list immediately after System
        $idx = array_search('system', array_keys($bbconnect_action_launcher_left));
        $end_actions = array_splice($bbconnect_action_launcher_left, $idx + 1);
        foreach ($child_actions[$system_term] as $system_child) {
            $bbconnect_action_launcher_left[$system_child->slug] = ' - ' . $system_child->name;
        }
        $bbconnect_action_launcher_left += $end_actions;
        ?>
        <div class="bbconnect-fields">

        <?php 
        if (!empty($bbconnect_action_types)) {
            ?>

        <?php 
            if (false != $embed) {
                echo '<form id="' . $embed_id . '" class="bbconnectpanels-form" method="post" action="">';
            }
            ?>

            <div class="column_holder actions-history-holder"<?php 
            echo $one_style;
            ?>
>
                <ul>
                    <li>
                        <span class="bbconnect-field">
                            <h3><?php 
            _e('Notes', 'bbconnect');
            ?>
</h3>
                            <div style="text-align: right;">
                                <select class="profile-actions-filter">
                                    <option value="default"><?php 
            _e('Default', 'bbconnect');
            ?>
</option>
                                    <option value="all"><?php 
            _e('All', 'bbconnect');
            ?>
</option>
                                    <?php 
            $bbconnect_ae_launcher_left = apply_filters('bbconnect_ae_launcher_left', $bbconnect_action_launcher_left, $embed_id);
            foreach ($bbconnect_ae_launcher_left as $key => $val) {
                echo '<option value="' . $key . '">' . $val . '</option>';
            }
            ?>
                                </select>
                            </div>
                        </span>
                    </li>
                </ul>
                <ul class="bbconnect actions-history-list">
                <?php 
            if (false != $user_id) {
                // MAKE THE QUERY
                $args = array('post_type' => 'bb_note', 'author' => $user_id, 'posts_per_page' => -1, 'post_status' => array('publish', 'private'));
                $action_query = new WP_Query($args);
                // LOOP THE ITEMS
                while ($action_query->have_posts()) {
                    $action_query->the_post();
                    $visible = false;
                    $class = array();
                    $note_types = wp_get_post_terms($action_query->post->ID, 'bb_note_type');
                    foreach ($note_types as $note_type) {
                        $class[] = $note_type->slug;
                        if (!$visible && in_array($note_type->term_id, $default_terms)) {
                            $visible = true;
                            $class[] = 'default';
                        }
                    }
                    bbconnect_profile_action_item(array('act' => $action_query->post, 'type' => $type, 'user' => $user_id, 'class' => $class, 'ok_edit' => $ok_edit, 'action' => $action, 'bbconnect_cap' => $bbconnect_cap, 'visible' => $visible));
                }
            } else {
                do_action('bbconnect_ae_query', $bbconnect_action_types, $embed_id);
            }
            ?>
                </ul>
            </div>

            <div class="column_holder actions-editor"<?php 
            echo $two_style;
            ?>
>
                <ul>
                    <li>
                        <span class="bbconnect-field">
                            <h3>&nbsp;</h3>
                        <?php 
            // IF THE USER CAN EDIT THE TARGET USER, START THE UI
            if (false != $ok_edit || 'bulk-edit' == $action) {
                if (!bbconnect_is_panels() && 'user' == $bbconnect_cap) {
                    // IF THEY ARE A USER ON THE BACKEND, SHOW THEM NOTHING
                } else {
                    $bbconnect_ae_launcher_right = apply_filters('bbconnect_ae_launcher_right', $bbconnect_action_launcher_right, $embed_id, $action, $bbconnect_cap);
                    ?>
                            <div style="text-align: right;">
                                <?php 
                    if ('bulk-edit' == $action) {
                        echo '<a class="rui off" title="actions-bulk-edit">Select a type to edit: </a>';
                        $e_title = __('Edit Actions', 'bbconnect');
                    } else {
                        $e_title = __('Create or Edit Actions', 'bbconnect');
                    }
                    ?>
                                <select class="actions-launcher <?php 
                    echo $action;
                    ?>
 <?php 
                    echo $type;
                    ?>
" title="<?php 
                    echo $user_id;
                    ?>
">
                                    <option value=""><?php 
                    echo $e_title;
                    ?>
</option>
                                    <?php 
                    foreach ($bbconnect_ae_launcher_right as $key => $val) {
                        if ($pagenow == 'admin-ajax.php') {
                            echo '<option value="rel=bbcContent&amp;bbc_type=' . $key . '&amp;uid=' . $user_id . '">' . $val . '</option>';
                        } else {
                            echo '<option value="' . $key . '">' . $val . '</option>';
                        }
                    }
                    ?>
                                </select>
                            </div>
                        <?php 
                }
            }
            ?>
                        </span>
                    </li>
                </ul>
                <ul>
                    <li>
                        <div style="display: block;">
                            <div id="<?php 
            echo $cid;
            ?>
_viewer" class="bbconnect-viewer inside">
                                <div style="display:none;">
                                <?php 
            if ($pagenow != 'admin-ajax.php') {
                if ('3.9' > get_bloginfo('version')) {
                    wp_editor('', $cid, array('tinymce' => true, 'textarea_name' => 'post_content', 'teeny' => false, 'quicktags' => true));
                }
            }
            ?>
                                </div>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>

        <?php 
            if (false != $embed) {
                echo '</form>';
            }
            ?>
        <?php 
        }
        ?>
        </div>
        <?php 
    }
}