/** * Saves user fields and groups. * * If field name is changed in specific group - new one will be created, * otherwise old one will be updated and will appear in that way in other grups. * * @return type */ function wpcf_admin_save_usermeta_groups_submit($form) { if (!isset($_POST['wpcf']['group']['name'])) { return false; } $_POST['wpcf']['group']['name'] = trim($_POST['wpcf']['group']['name']); $_POST['wpcf']['group'] = apply_filters('wpcf_group_pre_save', $_POST['wpcf']['group']); global $wpdb; $new_group = false; $group_slug = $_POST['wpcf']['group']['slug'] = sanitize_title($_POST['wpcf']['group']['name']); // Basic check if (isset($_REQUEST['group_id'])) { // Check if group exists $post = get_post($_REQUEST['group_id']); // Name changed if (strtolower($_POST['wpcf']['group']['name']) != strtolower($post->post_title)) { // Check if already exists $exists = get_page_by_title($_POST['wpcf']['group']['name'], 'OBJECT', 'wp-types-user-group'); if (!empty($exists)) { $form->triggerError(); wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), $_POST['wpcf']['group']['name']), 'error'); return $form; } } if (empty($post) || $post->post_type != 'wp-types-user-group') { $form->triggerError(); wpcf_admin_message(sprintf(__("Wrong group ID %d", 'wpcf'), intval($_REQUEST['group_id'])), 'error'); return $form; } $group_id = $post->ID; } else { $new_group = true; // Check if already exists $exists = get_page_by_title($_POST['wpcf']['group']['name'], 'OBJECT', 'wp-types-user-group'); if (!empty($exists)) { $form->triggerError(); wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), $_POST['wpcf']['group']['name']), 'error'); return $form; } } // Save fields for future use $fields = array(); if (!empty($_POST['wpcf']['fields'])) { // Before anything - search unallowed characters foreach ($_POST['wpcf']['fields'] as $key => $field) { if (empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['name']) || !empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['slug'])) { $form->triggerError(); wpcf_admin_message(sprintf(__('Field slugs cannot contain non-English characters. Please edit this field name %s and save again.', 'wpcf'), $field['name']), 'error'); return $form; } } foreach ($_POST['wpcf']['fields'] as $key => $field) { $field = apply_filters('wpcf_field_pre_save', $field); if (!empty($field['is_new'])) { // Check name and slug if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']), 'wp-types-user-group', 'wpcf-usermeta')) { $form->triggerError(); wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error'); return $form; } if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']), 'wp-types-user-group', 'wpcf-usermeta')) { $form->triggerError(); wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error'); return $form; } } // Field ID and slug are same thing $field_id = wpcf_admin_fields_save_field($field, 'wp-types-user-group', 'wpcf-usermeta'); if (!empty($field_id)) { $fields[] = $field_id; } } } // Save group $roles = isset($_POST['wpcf']['group']['supports']) ? $_POST['wpcf']['group']['supports'] : array(); $admin_style = $_POST['wpcf']['group']['admin_styles']; // Rename if needed if (isset($_REQUEST['group_id'])) { $_POST['wpcf']['group']['id'] = $_REQUEST['group_id']; } $group_id = wpcf_admin_fields_save_group($_POST['wpcf']['group'], 'wp-types-user-group'); $_POST['wpcf']['group']['id'] = $group_id; // Set open fieldsets if ($new_group && !empty($group_id)) { $open_fieldsets = get_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', true); if (isset($open_fieldsets[-1])) { $open_fieldsets[$group_id] = $open_fieldsets[-1]; unset($open_fieldsets[-1]); update_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', $open_fieldsets); } } // Rest of processes if (!empty($group_id)) { wpcf_admin_fields_save_group_fields($group_id, $fields, false, 'wp-types-user-group'); wpcf_admin_fields_save_group_showfor($group_id, $roles); wpcf_admin_fields_save_group_admin_styles($group_id, $admin_style); $_POST['wpcf']['group']['fields'] = isset($_POST['wpcf']['fields']) ? $_POST['wpcf']['fields'] : array(); do_action('wpcf_save_group', $_POST['wpcf']['group']); wpcf_admin_message_store(apply_filters('types_message_usermeta_saved', __('Group saved', 'wpcf'), $_POST['wpcf']['group']['name'], $new_group ? false : true), 'custom'); wp_redirect(admin_url('admin.php?page=wpcf-edit-usermeta&group_id=' . $group_id)); die; } else { wpcf_admin_message_store(__('Error saving group', 'wpcf'), 'error'); } }
/** * Summary. * * Description. * * @since x.x.x * @access (for functions: only use if private) * * @see Function/method/class relied on * @link URL * @global type $varname Description. * @global type $varname Description. * * @param type $var Description. * @param type $var Optional. Description. * @return type Description. */ public function save() { if (!isset($_POST['wpcf']) || !isset($_POST['wpcf']['group']) || !isset($_POST['wpcf']['group']['name'])) { return false; } $_POST['wpcf']['group'] = apply_filters('wpcf_group_pre_save', $_POST['wpcf']['group']); $group_name = wp_kses_post($_POST['wpcf']['group']['name']); require_once WPCF_EMBEDDED_ABSPATH . '/classes/forms.php'; $form = new Enlimbo_Forms_Wpcf(); if (empty($group_name)) { $form->triggerError(); wpcf_admin_message(__('Group name can not be empty.', 'wpcf'), 'error'); return $form; } $new_group = false; $group_slug = sanitize_title($group_name); // Basic check if (isset($_REQUEST[$this->get_id])) { // Check if group exists $post = get_post(intval($_REQUEST[$this->get_id])); // Name changed if (strtolower($group_name) != strtolower($post->post_title)) { // Check if already exists $exists = get_page_by_title($group_name, 'OBJECT', TYPES_USER_META_FIELD_GROUP_CPT_NAME); if (!empty($exists)) { $form->triggerError(); wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), apply_filters('the_title', $exists->post_title)), 'error'); return $form; } } if (empty($post) || $post->post_type != TYPES_USER_META_FIELD_GROUP_CPT_NAME) { $form->triggerError(); wpcf_admin_message(sprintf(__("Wrong group ID %d", 'wpcf'), intval($_REQUEST[$this->get_id])), 'error'); return $form; } $group_id = $post->ID; } else { $new_group = true; // Check if already exists $exists = get_page_by_title($group_name, 'OBJECT', TYPES_USER_META_FIELD_GROUP_CPT_NAME); if (!empty($exists)) { $form->triggerError(); wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), apply_filters('the_title', $exists->post_title)), 'error'); return $form; } } // Save fields for future use $fields = array(); if (!empty($_POST['wpcf']['fields'])) { foreach ($_POST['wpcf']['fields'] as $key => $field) { $field = wpcf_sanitize_field($field); $field = apply_filters('wpcf_field_pre_save', $field); if (!empty($field['is_new'])) { // Check name and slug if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']), TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta')) { $form->triggerError(); wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error'); return $form; } if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']), TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta')) { $form->triggerError(); wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error'); return $form; } } // Field ID and slug are same thing $field_id = wpcf_admin_fields_save_field($field, TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta'); if (!empty($field_id)) { $fields[] = $field_id; } } } // Save group $roles = isset($_POST['wpcf']['group']['supports']) ? $_POST['wpcf']['group']['supports'] : array(); /** * Admin styles */ if (isset($_POST['wpcf']['group']['admin_styles'])) { $admin_style = esc_html($_POST['wpcf']['group']['admin_styles']); } // Rename if needed if (isset($_REQUEST[$this->get_id])) { $_POST['wpcf']['group']['id'] = intval($_REQUEST[$this->get_id]); } $group_id = wpcf_admin_fields_save_group($_POST['wpcf']['group'], TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'user'); // Set open fieldsets if ($new_group && !empty($group_id)) { $open_fieldsets = get_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', true); if (isset($open_fieldsets[-1])) { $open_fieldsets[$group_id] = $open_fieldsets[-1]; unset($open_fieldsets[-1]); update_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', $open_fieldsets); } } // Rest of processes if (!empty($group_id)) { wpcf_admin_fields_save_group_fields($group_id, $fields, false, TYPES_USER_META_FIELD_GROUP_CPT_NAME); $this->save_group_showfor($group_id, $roles); /** * Admin styles */ if (defined('TYPES_USE_STYLING_EDITOR') && TYPES_USE_STYLING_EDITOR && isset($admin_style)) { wpcf_admin_fields_save_group_admin_styles($group_id, $admin_style); } $_POST['wpcf']['group']['fields'] = isset($_POST['wpcf']['fields']) ? $_POST['wpcf']['fields'] : array(); do_action('wpcf_save_group', $_POST['wpcf']['group']); // admin message if (!wpcf_is_client()) { wpcf_admin_message_store(apply_filters('types_message_usermeta_saved', __('Group saved', 'wpcf'), $group_name, $new_group ? false : true), 'custom'); } wp_safe_redirect(admin_url(sprintf('admin.php?page=wpcf-edit-usermeta&group_id=%d', $group_id))); exit; } else { wpcf_admin_message_store(__('Error saving group', 'wpcf'), 'error'); } }
/** * Update an existing field group or create new one. * * @param array $group * @param string $post_type * @return int Group ID. */ function wpcf_admin_fields_save_group($group, $post_type = TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, $which_fields = 'none') { if (!isset($group['name']) || empty($group['name'])) { return false; } $post = array('post_status' => 'publish', 'post_type' => $post_type, 'post_title' => sanitize_text_field($group['name']), 'post_name' => sanitize_text_field($group['name']), 'post_content' => empty($group['description']) ? '' : $group['description']); $update = false; if (isset($group['id']) && !empty($group['id'])) { $update = true; $post_to_update = get_post($group['id']); if (empty($post_to_update) || $post_to_update->post_type != $post_type) { return false; } $post['ID'] = $post_to_update->ID; $post['post_status'] = $post_to_update->post_status; } if ($update) { $group_id = wp_update_post($post); if (!$group_id) { return false; } update_post_meta($group_id, TOOLSET_EDIT_LAST, time()); } else { $group_id = wp_insert_post($post, true); if (is_wp_error($group_id)) { return false; } } if (isset($group['admin_styles'])) { wpcf_admin_fields_save_group_admin_styles($group_id, $group['admin_styles']); } if (!empty($group['filters_association'])) { update_post_meta($group_id, '_wp_types_group_filters_association', $group['filters_association']); } else { delete_post_meta($group_id, '_wp_types_group_filters_association'); } // WPML register strings if (function_exists('icl_register_string')) { wpcf_translate_register_string('plugin Types', 'group ' . $group_id . ' name', $group['name']); wpcf_translate_register_string('plugin Types', 'group ' . $group_id . ' description', $group['description']); } // admin message wpcf_admin_fields_save_message($update, $which_fields); return $group_id; }
/** * Saves fields and groups. * * If field name is changed in specific group - new one will be created, * otherwise old one will be updated and will appear in that way in other grups. * * @return type */ function wpcf_admin_save_fields_groups_submit($form) { if (!isset($_POST['wpcf']) || !isset($_POST['wpcf']['group']) || !isset($_POST['wpcf']['group']['name'])) { return false; } // @todo maybe sanitize_text_field this too $_POST['wpcf']['group']['name'] = trim(strip_tags($_POST['wpcf']['group']['name'])); $_POST['wpcf']['group'] = apply_filters('wpcf_group_pre_save', $_POST['wpcf']['group']); if (empty($_POST['wpcf']['group']['name'])) { $form->triggerError(); wpcf_admin_message(__('Group name can not be empty.', 'wpcf'), 'error'); return $form; } $new_group = false; $group_slug = $_POST['wpcf']['group']['slug'] = sanitize_title($_POST['wpcf']['group']['name']); // Basic check if (isset($_REQUEST['group_id'])) { // Check if group exists $post = get_post(intval($_REQUEST['group_id'])); // Name changed if (strtolower($_POST['wpcf']['group']['name']) != strtolower($post->post_title)) { // Check if already exists $exists = get_page_by_title($_POST['wpcf']['group']['name'], 'OBJECT', 'wp-types-group'); if (!empty($exists)) { $form->triggerError(); wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), htmlspecialchars($_POST['wpcf']['group']['name'])), 'error'); return $form; } } if (empty($post) || $post->post_type != 'wp-types-group') { $form->triggerError(); wpcf_admin_message(sprintf(__("Wrong group ID %d", 'wpcf'), intval($_REQUEST['group_id'])), 'error'); return $form; } $group_id = $post->ID; } else { $new_group = true; // Check if already exists $exists = get_page_by_title($_POST['wpcf']['group']['name'], 'OBJECT', 'wp-types-group'); if (!empty($exists)) { $form->triggerError(); wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), htmlspecialchars($_POST['wpcf']['group']['name'])), 'error'); return $form; } } // Save fields for future use $fields = array(); if (!empty($_POST['wpcf']['fields'])) { // Before anything - search unallowed characters foreach ($_POST['wpcf']['fields'] as $key => $field) { if (empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['name']) || !empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['slug'])) { $form->triggerError(); wpcf_admin_message(sprintf(__('Field slugs cannot contain non-English characters. Please edit this field name %s and save again.', 'wpcf'), $field['name']), 'error'); return $form; } if (!empty($field['name']) && is_numeric($field['name']) || !empty($field['slug']) && is_numeric($field['slug'])) { $form->triggerError(); wpcf_admin_message(sprintf(__('Field names or slugs cannot contain only numbers.', 'wpcf'), $field['name']), 'error'); return $form; } } // First check all fields foreach ($_POST['wpcf']['fields'] as $key => $field) { $field = apply_filters('wpcf_field_pre_save', $field); if (!empty($field['is_new'])) { // Check name and slug if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']))) { $form->triggerError(); wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error'); return $form; } if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']))) { $form->triggerError(); wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error'); return $form; } } // Field ID and slug are same thing $field_id = wpcf_admin_fields_save_field($field); if (is_wp_error($field_id)) { $form->triggerError(); wpcf_admin_message($field_id->get_error_message(), 'error'); return $form; } if (!empty($field_id)) { $fields[] = $field_id; } // WPML if (function_exists('wpml_cf_translation_preferences_store')) { $wpml_save_cf = wpml_cf_translation_preferences_store($key, wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_id)) . $field_id); } } } // Save group $post_types = isset($_POST['wpcf']['group']['supports']) ? $_POST['wpcf']['group']['supports'] : array(); $taxonomies_post = isset($_POST['wpcf']['group']['taxonomies']) ? $_POST['wpcf']['group']['taxonomies'] : array(); $admin_style = $_POST['wpcf']['group']['admin_styles']; $terms = array(); foreach ($taxonomies_post as $taxonomy) { foreach ($taxonomy as $tax => $term) { $terms[] = $term; } } // Rename if needed if (isset($_REQUEST['group_id'])) { $_POST['wpcf']['group']['id'] = intval($_REQUEST['group_id']); } $group_id = wpcf_admin_fields_save_group($_POST['wpcf']['group']); $_POST['wpcf']['group']['id'] = $group_id; // Set open fieldsets if ($new_group && !empty($group_id)) { $open_fieldsets = get_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', true); if (isset($open_fieldsets[-1])) { $open_fieldsets[$group_id] = $open_fieldsets[-1]; unset($open_fieldsets[-1]); update_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', $open_fieldsets); } } // Rest of processes if (!empty($group_id)) { wpcf_admin_fields_save_group_fields($group_id, $fields); wpcf_admin_fields_save_group_post_types($group_id, $post_types); wpcf_admin_fields_save_group_terms($group_id, $terms); wpcf_admin_fields_save_group_admin_styles($group_id, $admin_style); if (empty($_POST['wpcf']['group']['templates'])) { $_POST['wpcf']['group']['templates'] = array(); } wpcf_admin_fields_save_group_templates($group_id, $_POST['wpcf']['group']['templates']); $_POST['wpcf']['group']['fields'] = isset($_POST['wpcf']['fields']) ? $_POST['wpcf']['fields'] : array(); do_action('wpcf_save_group', $_POST['wpcf']['group']); wpcf_admin_message_store(apply_filters('types_message_custom_fields_saved', __('Group saved', 'wpcf'), $_POST['wpcf']['group']['name'], $new_group ? false : true), 'custom'); wp_redirect(admin_url('admin.php?page=wpcf-edit&group_id=' . $group_id)); die; } else { wpcf_admin_message_store(__('Error saving group', 'wpcf'), 'error'); } }
/** * Update an existing field group or create new one. * * @param array $group * @param string $post_type * @return int Group ID. */ function wpcf_admin_fields_save_group($group, $post_type = TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, $which_fields = 'none') { if (!isset($group['name']) || empty($group['name'])) { return false; } $post = array('post_status' => 'publish', 'post_type' => $post_type, 'post_title' => sanitize_text_field($group['name']), 'post_name' => sanitize_text_field($group['name']), 'post_content' => empty($group['description']) ? '' : $group['description']); if (empty($post['post_title'])) { wpcf_admin_message(__('Please set name', 'wpcf'), 'error'); return false; } $update = false; $slug_pre_save = false; if (isset($group['id']) && !empty($group['id'])) { $update = true; $post_to_update = get_post($group['id']); if (empty($post_to_update) || $post_to_update->post_type != $post_type) { return false; } $post['ID'] = $post_to_update->ID; $slug_pre_save = $post_to_update->post_name; $post['post_status'] = $post_to_update->post_status; } if ($update) { $group_id = wp_update_post($post); if (!$group_id) { return false; } update_post_meta($group_id, TOOLSET_EDIT_LAST, time()); } else { $group_id = wp_insert_post($post, true); if (is_wp_error($group_id)) { return false; } } if (isset($group['admin_styles'])) { wpcf_admin_fields_save_group_admin_styles($group_id, $group['admin_styles']); } if (!empty($group['filters_association'])) { update_post_meta($group_id, '_wp_types_group_filters_association', $group['filters_association']); } else { delete_post_meta($group_id, '_wp_types_group_filters_association'); } // WPML register strings if (function_exists('icl_register_string')) { try { // Legacy function gives us only the underlying post type of the field group. $group_factory = Types_Field_Utils::get_group_factory_by_post_type($post_type); $field_group = $group_factory->load_field_group(sanitize_title($group['name'])); // Skip registering if the group does not exist. if (null != $field_group) { $group_wpml = new Types_Wpml_Field_Group($field_group); $group_wpml->register($slug_pre_save); } } catch (InvalidArgumentException $e) { // Something is seriously wrong - there's no field group factory for given post type, bail. } } // admin message wpcf_admin_fields_save_message($update, $which_fields); return $group_id; }