/**
// Updates the account type for posts and pages
*/
function ym_account_save($post_id)
{
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || defined('DOING_AJAX') && DOING_AJAX || isset($_REQUEST['bulk_edit'])) {
        return;
    }
    // nonce needed
    /**
    // Use nonce for verification
      wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
      // in widgets 
    	//if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
    */
    $ignores = array('auto-draft', 'inherit', 'trash');
    // allow save on types from http://codex.wordpress.org/Function_Reference/get_post_status
    if (in_array(get_post_status($post_id), $ignores)) {
        return;
    }
    //we add the account types within this if because it should always be set.
    //the only occurrence where it wouldnt be set is in a scheduled post.
    $opt = '';
    if (ym_post('post_account_types')) {
        foreach ((array) ym_post('post_account_types') as $type) {
            $opt .= $type . ';';
        }
        $opt = rtrim($opt, ';');
    }
    update_post_meta($post_id, '_ym_account_type', $opt);
    update_post_meta($post_id, '_ym_post_purchasable', ym_post('post_purchasable'));
    if (ym_post('post_purchasable')) {
        $purchase_expiry = ym_post('post_purchasable_expiry');
        $purchase_limit = ym_post('post_purchasable_limit');
        if ($purchase_expiry != '') {
            $purchase_expiry = strtotime($purchase_expiry);
        }
        $purchase_duration = (int) ym_post('post_purchasable_duration');
        update_post_meta($post_id, '_ym_post_purchasable_featured', ym_post('post_purchasable_featured'));
        update_post_meta($post_id, '_ym_post_purchasable_index', ym_post('ppp_index'));
        update_post_meta($post_id, '_ym_post_purchasable_cost', ym_post('post_purchasable_cost'));
        update_post_meta($post_id, '_ym_post_purchasable_expiry', $purchase_expiry);
        update_post_meta($post_id, '_ym_post_purchasable_limit', $purchase_limit);
        update_post_meta($post_id, '_ym_post_purchasable_duration', $purchase_duration);
        $gateways = ym_spawn_gateways();
        foreach ($gateways as $gateway) {
            if (method_exists($gateway, 'additional_pack_fields')) {
                $fields = $gateway->additional_pack_fields();
                foreach ($fields as $field) {
                    update_post_meta($post_id, '_ym_post_purchasable_' . $field['name'], ym_post($field['name']));
                }
            }
        }
        // bundle
        global $wpdb;
        $data = ym_post('ym_bundle', array());
        $bundles = ym_get_bundles();
        foreach ($bundles as $bundle) {
            $id = $bundle->id;
            if (in_array($id, $data)) {
                // add
                ym_add_post_to_bundle($post_id, $id);
            } else {
                // remove
                ym_remove_post_from_bundle($post_id, $id);
            }
        }
    }
    if (ym_post('available_from')) {
        $string = array();
        foreach (ym_post('available_from') as $key => $type) {
            $string[] = $key . '=' . $type;
        }
        $string = implode(';', $string);
        update_post_meta($post_id, '_ym_account_min_duration', $string);
    }
}
/**
Bundle Content Editor
*/
function ym_bundle_edit_content($bundle)
{
    if (isset($_POST['ym_remove_post_id']) && $_POST['ym_remove_post_id']) {
        ym_remove_post_from_bundle($_POST['ym_remove_post_id'], $bundle->id);
        echo '<div id="message" class="updated"><p>' . __('Removed Post from Bundle', 'ym') . '</p></div>';
        ym_box_bottom();
        ym_box_top();
    }
    if (isset($_POST['add_posts']) && $_POST['add_posts']) {
        foreach ($_POST['post_ids'] as $id) {
            if (ym_add_post_to_bundle($id, $bundle->id)) {
                echo '<p>' . sprintf(__('Added post ID: %s to bundle ID: %s', 'ym'), $id, $bundle->id) . '</p>';
            }
        }
        ym_box_bottom();
        ym_box_top();
    }
    $posts = ym_get_bundle_posts($bundle->id);
    echo '<table style="width: 100%;">';
    $post_ids = array();
    foreach ($posts as $post) {
        $post_ids[] = $post->post_id;
        echo '<tr>';
        echo '<td>(' . $post->post_id . ') ' . get_the_title($post->post_id) . '</td>';
        echo '<td>
		<form action="" method="post">
			<input type="hidden" name="bundle_id" value="' . $bundle->id . '" />
			<input type="hidden" name="posts" value="1" />
			<input type="hidden" name="ym_remove_post_id" value="' . $post->post_id . '" />
			<input type="submit" class="button-secondary deletelink" value="' . __('Remove Post from Bundle', 'ym') . '" />
		</form>
		</td>';
        echo '</tr>';
    }
    echo '</table>';
    ym_box_bottom();
    ym_box_top(__('Add new Item(s) to bundle', 'ym'));
    echo '<form action="" method="post">
		<input type="hidden" name="bundle_id" value="' . $bundle->id . '" />
		<input type="hidden" name="posts" value="1" />
		<input type="hidden" name="add_posts" value="1" />
	<table id="ym_posts_space" style="width: 100%;">
	';
    $max = 15;
    $posts = ym_bundle_get_some_posts(0, $post_ids, $max);
    $total = count($posts);
    foreach ($posts as $post) {
        echo '<tr>';
        echo '<td>(' . $post->ID . ' - ' . $post->post_type . ') ' . $post->post_title . '</td>';
        echo '<td><input type="checkbox" name="post_ids[]" value="' . $post->ID . '" /></td>';
        echo '</tr>';
    }
    echo '
	</table>
	';
    if ($total == $max) {
        echo '<a href="#nowhere" id="ym_next_page" class="button-secondary" style="float: right;">' . __('Load More Posts', 'ym') . '</a>';
    }
    echo '
<input type="hidden" name="offset" id="offset" value="5" />
<input type="hidden" name="current_post_ids" id="current_post_ids" value="' . implode(',', $post_ids) . '" />
	<br />
<input type="submit" class="button-primary" value="' . __('Add Selected Item(s) to Bundle', 'ym') . '" />
	</form>

<script type="text/javascript">' . "\njQuery(document).ready(function() {\n\tjQuery('#ym_next_page').click(function() {\n\t\tvar count = 0;\n\t\tjQuery(this).html('" . __('Loading...', 'ym') . "');\n\t\tjQuery.getJSON('" . YM_ADMIN_URL . "&ym_page=ym-content-bundles&do_munch=1&offset=' + jQuery('#offset').val() + '&post_ids=' + jQuery('#current_post_ids').val() + '&max=" . $max . "', function(data) {\n\t\t\t\tjQuery('#ym_posts_space input').each(function() {\n\t\t\t\t\tif (!jQuery(this).is(':checked')) {\n\t\t\t\t\t\tjQuery(this).parents('tr').remove();\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tjQuery.each(data, function(key, val) {\n\t\t\t\t\tcount++;\n\t\t\t\t\tjQuery(val).appendTo('#ym_posts_space');\n\t\t\t\t});\n\t\t\t\tif (count == " . $max . ") {\n\t\t\t\t\tjQuery('#ym_next_page').html('" . __('Load More Posts', 'ym') . "');\n\t\t\t\t} else {\n\t\t\t\t\tjQuery('#ym_next_page').remove();\n\t\t\t\t}\n\t\t\t\tjQuery('#offset').val(jQuery('#offset').val() + " . $max . ");\n\t\t});\n\t});\n});\n</script>\n";
}