self::set_flash($data['msg']); } // Refresh the list of files print '<script type="text/javascript">window.location.replace("?page=cctm_tools&a=import_def");</script>'; return; } elseif (wp_verify_nonce($nonce, 'cctm_delete_defs')) { $defs = CCTM::get_value($_POST, 'defs', array()); if (CCTM_ImportExport::delete_defs($defs)) { $data['msg'] = sprintf('<div class="updated"><p>%s</p></div>', __('Files have been removed from your library.', CCTM_TXTDOMAIN)); CCTM::set_flash($data['msg']); print '<script type="text/javascript">window.location.replace("?page=cctm_tools&a=import_def");</script>'; return; } else { $data['msg'] = CCTM::format_errors(); } } elseif (wp_verify_nonce($nonce, 'cctm_activate_def')) { if (CCTM_ImportExport::activate_def($_POST['def'])) { $data['msg'] = sprintf('<div class="updated"><p>%s</p></div>', __('The definition was imported successfully!', CCTM_TXTDOMAIN)); CCTM::set_flash($data['msg']); print '<script type="text/javascript">window.location.replace("?page=cctm_tools&a=import_def");</script>'; return; } else { $data['msg'] = CCTM::format_errors(); } } else { $data['msg'] = __('Invalid submission.', CCTM_TXTDOMAIN); } } $data['content'] = CCTM::load_view('import.php', $data); print CCTM::load_view('templates/default.php', $data); /*EOF*/
/** * Save the new Custom Fields values. If the content type is not active in the * CCTM plugin or its custom fields are not being standardized, then this function * effectively does nothing. * * WARNING: This function is also called when the wp_insert_post() is called, and * we don't want to step on its toes. We want this to kick in ONLY when a post * is inserted via the WP manager. * see http://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=52 * * @param integer $post_id id of the post these custom fields are associated with * @param object $post the post object */ public static function save_custom_fields($post_id, $post) { // Bail if you're not in the admin editing a post if (!self::_is_existing_post() && !self::_is_new_post()) { return; } // Bail if this post-type is not active in the CCTM if (!isset(CCTM::$data['post_type_defs'][$post->post_type]['is_active']) || CCTM::$data['post_type_defs'][$post->post_type]['is_active'] == 0) { return; } // Bail if there are no custom fields defined in the CCTM if (empty(CCTM::$data['post_type_defs'][$post->post_type]['custom_fields'])) { return; } // See issue http://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=80 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } // Use this to ensure you save custom fields only when saving from the edit/create post page $nonce = CCTM::get_value($_POST, '_cctm_nonce'); if (!wp_verify_nonce($nonce, 'cctm_create_update_post')) { return; } if (!empty($_POST)) { $custom_fields = self::_get_custom_fields($post->post_type); $validation_errors = array(); foreach ($custom_fields as $field_name) { if (!isset(CCTM::$data['custom_field_defs'][$field_name]['type'])) { continue; } $field_type = CCTM::$data['custom_field_defs'][$field_name]['type']; if ($FieldObj = CCTM::load_object($field_type, 'fields')) { $FieldObj->set_props(CCTM::$data['custom_field_defs'][$field_name]); $value = $FieldObj->save_post_filter($_POST, $field_name); CCTM::log("Saving field Type: {$field_type} with value: {$value}", __FILE__, __LINE__); // Custom fields can return a literal null if they don't save data to the db. if ($value !== null) { // Check for empty json arrays, e.g. [""], convert them to empty PHP array() $value_copy = $value; if ($FieldObj->is_repeatable) { $value_copy = json_decode(stripslashes($value), true); if (is_array($value_copy)) { foreach ($value_copy as $k => $v) { if (empty($v)) { unset($value_copy[$k]); } } } } // We do some more work to ensure the database stays lean if (is_array($value_copy) && empty($value_copy) && !CCTM::get_setting('save_empty_fields')) { delete_post_meta($post_id, $field_name); } if (!is_array($value_copy) && !strlen(trim($value_copy)) && !CCTM::get_setting('save_empty_fields')) { // Delete the row from wp_postmeta, or don't write it at all delete_post_meta($post_id, $field_name); } else { update_post_meta($post_id, $field_name, $value); } } } else { // error! Can't include the field class. WTF did you do? } } // Pass validation errors like this: fieldname => validator, e.g. myfield => required if (!empty($validation_errors)) { CCTM::log('Validation errors: ' . json_encode($validation_errors), __FILE__, __LINE__); CCTM::set_flash(json_encode($validation_errors)); } } }
// 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'; } }