// Init data points
$data['id'] = '';
$data['title'] = '';
$data['context'] = 'advanced';
$data['priority'] = 'default';
$data['callback'] = '';
$data['callback_args'] = '';
$data['visibility_control'] = '';
$data['style'] = file_get_contents(CCTM_PATH . '/css/validation.css');
// Save if submitted...
if (!empty($_POST) && check_admin_referer($data['action_name'], $data['nonce_name'])) {
    $def = CCTM_Metabox::sanitize($_POST);
    unset($def['old_id']);
    if (CCTM_Metabox::is_valid_def($_POST)) {
        CCTM::$data['metabox_defs'][$def['id']] = $def;
        CCTM::set_flash(CCTM::format_msg(__('Metabox created.', CCTM_TXTDOMAIN)));
        $continue_editing = CCTM::get_value($_POST, 'continue_editing');
        unset($_POST);
        if ($continue_editing) {
            CCTM::redirect('?page=cctm&a=edit_metabox&id=' . $def['id']);
        } else {
            CCTM::redirect('?page=cctm');
        }
        return;
    } else {
        $data['msg'] = CCTM::format_error_msg(CCTM_Metabox::$errors, __('Please correct the following problems.', CCTM_TXTDOMAIN));
        foreach (CCTM_Metabox::$errors as $field => $error) {
            $data[$field . '.error'] = sprintf('<span class="cctm_validation_error">%s</span>', $error);
            $data[$field . '.error_class'] = 'cctm_validation_error';
        }
    }
if ($data['inactive_cnt'] < 0) {
    $data['inactive_cnt'] = 0;
}
// Assemble the inactive post-types
$inactive = array();
foreach ($data['results'] as &$r) {
    if (!in_array($r->post_type, $pts)) {
        $inactive[] = $r->post_type;
        $r->post_type = $r->post_type . ' (disabled)';
    }
}
// If properly submitted, Proceed with purging the database
if (!empty($_POST) && check_admin_referer($data['action_name'], $data['nonce_name'])) {
    if (!$data['inactive_cnt']) {
        // Nothing to do
        $msg = CCTM::format_msg(__('Your database is clean: there are no inactive post-types that need to be purged.', CCTM_TXTDOMAIN));
        self::set_flash($msg);
        include CCTM_PATH . '/controllers/tools.php';
        return;
    }
    // Purge the database
    $post_type_str = '';
    foreach ($inactive as &$in) {
        $in = $wpdb->prepare('%s', $in);
    }
    $post_type_str = implode(',', $inactive);
    global $wpdb;
    // Delete the custom fields
    $query = "DELETE FROM {$wpdb->postmeta} WHERE post_id IN ( SELECT ID FROM {$wpdb->posts} WHERE post_type IN ({$post_type_str}) )";
    $wpdb->query($query);
    // Delete taxonomy refs
// Data object we will save
// Init Error bits
$data['id.error'] = '';
$data['title.error'] = '';
$data['callback.error'] = '';
$data['id.error_class'] = '';
$data['title.error_class'] = '';
$data['callback.error_class'] = '';
$data['style'] = file_get_contents(CCTM_PATH . '/css/validation.css');
// Save if submitted...
if (!empty($_POST) && check_admin_referer($data['action_name'], $data['nonce_name'])) {
    //print_r($_POST); exit;
    $def = CCTM_Metabox::sanitize($_POST);
    //print_r($def); exit;
    if (CCTM_Metabox::is_valid_def($_POST, true)) {
        $data['msg'] = CCTM::format_msg(__('Metabox updated.', CCTM_TXTDOMAIN));
        // Find any refs to the old id and update them
        if ($def['id'] != $def['old_id']) {
            if (isset(CCTM::$data['post_type_defs']) && is_array(CCTM::$data['post_type_defs'])) {
                foreach (CCTM::$data['post_type_defs'] as $pt => $ptd) {
                    if (isset($ptd['map_field_metabox']) && is_array($ptd['map_field_metabox'])) {
                        foreach ($ptd['map_field_metabox'] as $cf => $mb) {
                            if ($mb == $def['old_id']) {
                                CCTM::$data['post_type_defs'][$pt]['map_field_metabox'][$cf] = $def['id'];
                            }
                        }
                    }
                }
            }
        }
        unset(CCTM::$data['metabox_defs'][$def['old_id']]);
$data['post_type'] = '';
// as default
$data['def'] = self::$default_post_type_def;
//		$def = self::$post_type_form_definition;
// Save data if it was properly submitted
if (!empty($_POST) && check_admin_referer($data['action_name'], $data['nonce_name'])) {
    $sanitized_vals = CCTM_PostTypeDef::sanitize_post_type_def($_POST);
    $error_msg = CCTM_PostTypeDef::post_type_name_has_errors($sanitized_vals, true);
    if (empty($error_msg)) {
        // Clean slate.  This nukes any instance of 'is_foreign' (and potentially other issues)
        // that may arise if the post-type name was used by another plugin and the CCTM tracked
        // custom fields for that plugin, and then later the other plugin was deactivated and
        // the CCTM wants to use the same post-type name.
        unset(CCTM::$data['post_type_defs'][$sanitized_vals['post_type']]);
        CCTM_PostTypeDef::save_post_type_settings($sanitized_vals);
        $data['msg'] = CCTM::format_msg(sprintf(__('The content type %s has been created', CCTM_TXTDOMAIN), '<em>' . $sanitized_vals['post_type'] . '</em>'));
        self::set_flash($data['msg']);
        include CCTM_PATH . '/controllers/list_post_types.php';
        return;
    } else {
        // clean up... menu labels in particular can get gunked up. :(
        $data['def'] = $sanitized_vals;
        $data['def']['labels']['singular_name'] = '';
        $data['def']['label'] = '';
        $data['msg'] = CCTM::format_error_msg($error_msg);
    }
}
$data['icons'] = CCTM_PostTypeDef::get_post_type_icons();
$data['columns'] = CCTM_PostTypeDef::get_columns($post_type);
$data['orderby_options'] = CCTM_PostTypeDef::get_orderby_options($post_type);
$data['content'] = CCTM::load_view('post_type.php', $data);