function wpcf_admin_userprofilesave_init($user_id)
{
    if (defined('WPTOOLSET_FORMS_VERSION')) {
        global $wpcf;
        $errors = false;
        /**
         * check checkbox type fields to delete or save empty if needed
         */
        $groups = wpcf_admin_usermeta_get_groups_fields();
        foreach ($groups as $group) {
            if (!array_key_exists('fields', $group) || empty($group['fields'])) {
                continue;
            }
            foreach ($group['fields'] as $field) {
                switch ($field['type']) {
                    case 'checkboxes':
                        if (!array_key_exists('wpcf', $_POST) || !array_key_exists($field['slug'], $_POST['wpcf'])) {
                            delete_user_meta($user_id, $field['meta_key']);
                        }
                        break;
                    case 'checkbox':
                        if (!array_key_exists('wpcf', $_POST) || !array_key_exists($field['slug'], $_POST['wpcf'])) {
                            if ('yes' == $field['data']['save_empty']) {
                                $_POST['wpcf'][$field['slug']] = 0;
                            } else {
                                delete_user_meta($user_id, $field['meta_key']);
                            }
                        }
                        break;
                }
            }
        }
        // Save meta fields
        if (!empty($_POST['wpcf'])) {
            foreach ($_POST['wpcf'] as $field_slug => $field_value) {
                // Get field by slug
                $field = wpcf_fields_get_field_by_slug($field_slug, 'wpcf-usermeta');
                if (empty($field)) {
                    continue;
                }
                // Skip copied fields
                if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                    continue;
                }
                $_field_value = !types_is_repetitive($field) ? array($field_value) : $field_value;
                // Set config
                $config = wptoolset_form_filter_types_field($field, $user_id);
                foreach ($_field_value as $_k => $_val) {
                    // Check if valid
                    $valid = wptoolset_form_validate_field('your-profile', $config, $_val);
                    if (is_wp_error($valid)) {
                        $errors = true;
                        $_errors = $valid->get_error_data();
                        $_msg = sprintf(__('Field "%s" not updated:', 'wpcf'), $field['name']);
                        wpcf_admin_message_store($_msg . ' ' . implode(', ', $_errors), 'error');
                        if (types_is_repetitive($field)) {
                            unset($field_value[$_k]);
                        } else {
                            break;
                        }
                    }
                }
                // Save field
                if (types_is_repetitive($field)) {
                    $wpcf->usermeta_repeater->set($user_id, $field);
                    $wpcf->usermeta_repeater->save($field_value);
                } else {
                    $wpcf->usermeta_field->set($user_id, $field);
                    $wpcf->usermeta_field->usermeta_save($field_value);
                }
                do_action('wpcf_user_field_saved', $user_id, $field);
                // TODO Move to checkboxes
                if ($field['type'] == 'checkboxes') {
                    $field_data = wpcf_admin_fields_get_field($field['id'], false, false, false, 'wpcf-usermeta');
                    if (!empty($field_data['data']['options'])) {
                        $update_data = array();
                        foreach ($field_data['data']['options'] as $option_id => $option_data) {
                            if (!isset($_POST['wpcf'][$field['id']][$option_id])) {
                                if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') {
                                    $update_data[$option_id] = 0;
                                }
                            } else {
                                $update_data[$option_id] = $_POST['wpcf'][$field['id']][$option_id];
                            }
                        }
                        update_user_meta($user_id, $field['meta_key'], $update_data);
                    }
                }
            }
        }
        if ($errors) {
            update_post_meta($user_id, '__wpcf-invalid-fields', true);
        }
        do_action('wpcf_user_saved', $user_id);
        return;
    }
    global $wpcf;
    $all_fields = array();
    $_not_valid = array();
    $_error = false;
    $error = '';
    $groups = $groups = wpcf_admin_usermeta_get_groups_fields();
    if (empty($groups)) {
        return false;
    }
    foreach ($groups as $group) {
        // Process fields
        $fields = wpcf_admin_usermeta_process_fields($user_id, $group['fields'], true, false, 'validation');
        // Validate fields
        $form = wpcf_form_simple_validate($fields);
        $all_fields = $all_fields + $fields;
        // Collect all not valid fields
        if ($form->isError()) {
            $_error = true;
            // Set error only to true
            $_not_valid = array_merge($_not_valid, (array) $form->get_not_valid());
        }
    }
    // Set fields
    foreach ($all_fields as $k => $v) {
        // only Types field
        if (empty($v['wpcf-id'])) {
            continue;
        }
        $_temp = new WPCF_Usermeta_Field();
        $_temp->set($user_id, $v['wpcf-id']);
        $all_fields[$k]['_field'] = $_temp;
    }
    foreach ($_not_valid as $k => $v) {
        // only Types field
        if (empty($v['wpcf-id'])) {
            continue;
        }
        $_temp = new WPCF_Usermeta_Field();
        $_temp->set($user_id, $v['wpcf-id']);
        $_not_valid[$k]['_field'] = $_temp;
    }
    $not_valid = apply_filters('wpcf_post_form_not_valid', $_not_valid, $_error, $all_fields);
    // Notify user about error
    if ($error) {
        wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error');
    }
    /*
     * Save invalid elements so user can be informed after redirect.
     */
    if (!empty($not_valid)) {
        update_user_meta($user_id, 'wpcf-invalid-fields', $not_valid);
    }
    if (!empty($_POST['wpcf'])) {
        foreach ($_POST['wpcf'] as $field_slug => $field_value) {
            $field = wpcf_fields_get_field_by_slug($field_slug, 'wpcf-usermeta');
            if (empty($field)) {
                continue;
            }
            $wpcf->usermeta_field->set($user_id, $field);
            if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                continue;
            }
            if (isset($_POST['__wpcf_repetitive'][$wpcf->usermeta_field->slug])) {
                $wpcf->usermeta_repeater->set($user_id, $field);
                $wpcf->usermeta_repeater->save();
            } else {
                $wpcf->usermeta_field->usermeta_save();
            }
            do_action('wpcf_post_field_saved', '', $field);
        }
        //end foreach
    }
    //end if
    foreach ($all_fields as $field) {
        if (!isset($field['#type'])) {
            continue;
        }
        if ($field['#type'] == 'checkbox') {
            $field_data = wpcf_admin_fields_get_field($field['wpcf-id'], false, false, false, 'wpcf-usermeta');
            if (!isset($_POST['wpcf'][$field['wpcf-slug']])) {
                if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') {
                    update_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], 0);
                } else {
                    delete_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug']);
                }
            }
        }
        if ($field['#type'] == 'checkboxes') {
            $field_data = wpcf_admin_fields_get_field($field['wpcf-id'], false, false, false, 'wpcf-usermeta');
            if (!empty($field_data['data']['options'])) {
                $update_data = array();
                foreach ($field_data['data']['options'] as $option_id => $option_data) {
                    if (!isset($_POST['wpcf'][$field['wpcf-slug']][$option_id])) {
                        if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') {
                            $update_data[$option_id] = 0;
                        }
                    } else {
                        $update_data[$option_id] = $_POST['wpcf'][$field['wpcf-slug']][$option_id];
                    }
                }
                update_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], $update_data);
            }
        }
    }
}
/**
 * Renders JS for editor menu.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @return type
 */
function wpcf_admin_post_add_to_editor_js()
{
    // since 1.3 we do not use global $post
    $post = wpcf_admin_get_edited_post();
    if (empty($post)) {
        $post = (object) array('ID' => -1);
        $post->post_type = false;
    }
    $fields = wpcf_admin_post_add_to_editor('get');
    $groups = wpcf_admin_post_get_post_groups_fields($post);
    if (empty($fields) || empty($groups)) {
        /**
         * check user fields too, but only when CF are empty
         */
        include_once dirname(__FILE__) . '/usermeta-post.php';
        $groups = wpcf_admin_usermeta_get_groups_fields();
        if (empty($groups)) {
            return false;
        }
    }
    $editor_addon = new Editor_addon('types', __('Insert Types Shortcode', 'wpcf'), WPCF_EMBEDDED_RES_RELPATH . '/js/types_editor_plugin.js', '', true, 'icon-types-logo ont-icon-18 ont-color-gray');
    foreach ($groups as $group) {
        if (empty($group['fields'])) {
            continue;
        }
        foreach ($group['fields'] as $group_field_id => $group_field) {
            if (!isset($fields[$group_field_id])) {
                continue;
            }
            $field = $fields[$group_field_id];
            $callback = 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\', \'postmeta\', ' . $post->ID . ')';
            $editor_addon->add_insert_shortcode_menu(stripslashes($field['name']), trim(wpcf_fields_get_shortcode($field), '[]'), $group['name'], $callback);
        }
    }
}
Example #3
0
/**
 * Add styles to userfields groups
 */
function wpcf_admin_fields_userfields_styles()
{
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta-post.php';
    //    $groups = wpcf_admin_fields_get_groups();
    $groups = wpcf_admin_usermeta_get_groups_fields();
    if (!empty($groups)) {
        echo '<style type="text/css">';
        foreach ($groups as $key => $group) {
            echo str_replace("}", "}\n", wpcf_admin_get_groups_admin_styles_by_group($group['id']));
        }
        echo '</style>';
    }
}
Example #4
0
 public function getCustomFields($role = "")
 {
     $fields = array();
     //return get_option('wpcf-usermeta', false);
     $isTypesActive = defined('WPCF_VERSION');
     if ($isTypesActive) {
         //$fields = $this->getCustomFields();
         if (!function_exists("wpcf_admin_usermeta_get_groups_fields")) {
             require_once WPCF_EMBEDDED_ABSPATH . '/includes/usermeta-post.php';
         }
         $_type_fields_groups = wpcf_admin_usermeta_get_groups_fields();
         //Getting all types user meta fields
         foreach ($_type_fields_groups as $n => $f) {
             if ($f['is_active'] == 1) {
                 if (isset($f['_wp_types_group_showfor']) && (is_array($f['_wp_types_group_showfor']) && in_array($role, $f['_wp_types_group_showfor']) || is_string($f['_wp_types_group_showfor']) && 'all' == $f['_wp_types_group_showfor']) || empty($role)) {
                     $fields = array_merge($fields, $f['fields']);
                 }
             }
         }
         $fields = isset($fields) ? $fields : array();
         $plugin = 'types';
         if (!empty($fields)) {
             foreach ($fields as $key => $field) {
                 $fields[$key]['post_labels'] = "{$key}";
                 $fields[$key]['post_type'] = 'user';
                 $fields[$key]['plugin_type'] = $plugin;
                 if (isset($fields[$key]['data']) && isset($fields[$key]['data']['controlled']) && $fields[$key]['data']['controlled']) {
                     // fields simply controlled by types do not have prefix, but original name
                     $fields[$key]['plugin_type_prefix'] = '';
                 } else {
                     // native types fields have prefix
                     $fields[$key]['plugin_type_prefix'] = 'wpcf-';
                 }
             }
         }
     }
     //#########################################################################################
     $sql = 'SELECT distinct(meta_key) as meta_key FROM ' . $this->wpdb->usermeta . '
                 WHERE meta_key not like "%s"';
     $usermetas = $this->wpdb->get_results($this->wpdb->prepare($sql, "wpcf-%"));
     if (!empty($usermetas)) {
         $usermetas = json_decode(json_encode($usermetas), true);
         $to_include = array('first_name', 'last_name', 'description');
         foreach ($usermetas as $n => $metas) {
             if (!in_array($metas['meta_key'], $to_include)) {
                 continue;
             }
             if (!isset($fields[$metas['meta_key']])) {
                 $fields[$metas['meta_key']] = array();
             }
             $fields[$metas['meta_key']]['id'] = $metas['meta_key'];
             $fields[$metas['meta_key']]['slug'] = $metas['meta_key'];
             $fields[$metas['meta_key']]['type'] = $metas['meta_key'] == 'description' ? 'wysiwyg' : 'textfield';
             $fields[$metas['meta_key']]['name'] = $metas['meta_key'] == 'description' ? 'Biographical Info' : ucwords(str_replace("_", " ", $metas['meta_key']));
             $fields[$metas['meta_key']]['data'] = '';
             $fields[$metas['meta_key']]['meta_key'] = $metas['meta_key'];
             $fields[$metas['meta_key']]['post_type'] = 'user';
             $fields[$metas['meta_key']]['meta_type'] = 'usermeta';
             $fields[$metas['meta_key']]['post_labels'] = $fields[$metas['meta_key']]['name'];
             $fields[$metas['meta_key']]['plugin_type'] = "";
             $fields[$metas['meta_key']]['plugin_type_prefix'] = "";
         }
     }
     return $fields;
 }