This needed to live in a separate file because I needed to completely control
the entire request: if it were handled by WP, headers() would be sent.
------------------------------------------------------------------------------*/
//@require_once( realpath('../../../../').'/wp-load.php' );
//include_once('../includes/constants.php');
//include_once(CCTM_PATH.'/includes/CCTM.php');
include_once CCTM_PATH . '/includes/CCTM_ImportExport.php';
// Make sure a file was specified
$filename = CCTM::get_value($_REQUEST, 'file');
if (empty($filename)) {
    print CCTM::format_error_msg(__('Definition file not specified.', CCTM_TXTDOMAIN));
    exit;
}
// Make sure the filename is legit
if (!CCTM_ImportExport::is_valid_basename($filename)) {
    print CCTM::format_error_msg(__('Invalid filename: the definition filename should not contain spaces and should use an extension of <code>.cctm.json</code>.', CCTM_TXTDOMAIN));
    exit;
}
// Load up this thing... errors will be thrown
$upload_dir = wp_upload_dir();
$dir = $upload_dir['basedir'] . '/' . CCTM::base_storage_dir . '/' . CCTM::def_dir . '/';
$data = CCTM_ImportExport::load_def_file($dir . $filename);
$data['filename'] = $filename;
// Bail if there were errors
if (!empty(CCTM::$errors)) {
    print CCTM::format_errors();
    exit;
}
// Check encoding, warn if it differs (warn only: it may not be a problem for the importer)
// See http://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=322
$this_charset = get_bloginfo('charset');
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';
        }
    }
    // Repopulate
    foreach ($def as $k => $v) {
        $data[$k] = $v;
    }
}
$data['rows'] = '';
$data['associations'] = '';
// Get the post-types for listing associations.
$displayable_types = self::get_post_types();
//------------------------------------------------------------------------------
$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);
print CCTM::load_view('templates/default.php', $data);
/*EOF*/