Beispiel #1
0
/**
 * Processes single field.
 *
 * Since Types 1.2 this function changed. It handles single form element.
 * Form element is already fetched, also meta values using class WPCF_Field.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @todo gradually remove usage of inherited fields
 * @todo Cleanup
 *
 * @staticvar array $repetitive_started
 * @param type $field_object
 * @return mixed boolean|array
 */
function wpcf_admin_post_process_field($field_object)
{
    /*
     * Since Types 1.2
     * All data we need is stored in global $wpcf
     */
    global $wpcf;
    $post = $wpcf->post;
    $field = (array) $field_object->cf;
    $context = $field_object->context;
    $invalid_fields = $wpcf->field->invalid_fields;
    if (!empty($field)) {
        /*
         * Set Unique ID
         */
        $field_id = 'wpcf-' . $field['type'] . '-' . $field['slug'] . '-' . wpcf_unique_id(serialize($field_object->__current_form_element));
        /*
         * Get inherited field
         *
         * TODO Deprecated
         *
         * Since Types 1.2 we encourage developers to completely define fields.
         */
        $inherited_field_data = false;
        if (isset($field_object->config->inherited_field_type)) {
            $_allowed = array('image' => 'file', 'numeric' => 'textfield', 'email' => 'textfield', 'phone' => 'textfield', 'url' => 'textfield');
            if (!array_key_exists($field_object->cf['type'], $_allowed)) {
                //                _deprecated_argument( 'inherited_field_type', '1.2',
                //                        'Since Types 1.2 we encourage developers to completely define fields' );
            }
            $inherited_field_data = wpcf_fields_type_action($field_object->config->inherited_field_type);
        }
        /*
         * CHECKPOINT
         * APPLY FILTERS
         *
         *
         * Moved to WPCF_Field
         * Field value should be already filtered
         *
         * Explanation:
         * When WPCF_Field::set() is called, all these properties are set.
         * WPCF_Field::$cf['value']
         * WPCF_Field::$__meta (single value from DB)
         * WPCF_Field::$meta (single or multiple values if single/repetitive)
         *
         * TODO Make sure value is already filtered and not overwritten
         */
        /*
         * Set generic values
         *
         * FUTURE BREAKPOINT
         * Since Types 1.2 we do not encourage relying on generic field data.
         * Only core fields should use this.
         *
         * TODO Open 3rd party fields dir
         */
        $_element = array('#type' => isset($field_object->config->inherited_field_type) ? $field_object->config->inherited_field_type : $field['type'], '#id' => $field_id, '#title' => $field['name'], '#name' => 'wpcf[' . $field['slug'] . ']', '#value' => isset($field['value']) ? $field['value'] : '', 'wpcf-id' => $field['id'], 'wpcf-slug' => $field['slug'], 'wpcf-type' => $field['type']);
        /*
         * TODO Add explanation about creating duplicated fields
         *
         * NOT USED YET
         *
         * Explain users that fields are added if slug is changed
         */
        wpcf_admin_add_js_settings('wpcfFieldNewInstanceWarning', __('If you change slug, new field will be created', 'wpcf'));
        /*
         * Merge with default element
         *
         * Deprecated from Types 1.2
         * Only core fields use this.
         */
        $element = array_merge($_element, $field_object->__current_form_element);
        /*
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         * TODO From this point code should be simplified.
         */
        if (isset($field['description_extra'])) {
            $element['#description'] .= wpautop($field['description_extra']);
        }
        // Set atributes #1
        if (isset($field['disable'])) {
            $field['#disable'] = $field['disable'];
        }
        if (!empty($field['disable'])) {
            $field['#attributes']['disabled'] = 'disabled';
        }
        if (!empty($field['readonly'])) {
            $field['#attributes']['readonly'] = 'readonly';
        }
        // Format description
        if (!empty($element['#description'])) {
            $element['#description'] = wpautop($element['#description']);
        }
        // Set validation element
        if (isset($field['data']['validate'])) {
            /*
             *
             *
             * TODO First two check are not needed anymore
             */
            // If array has more than one field - see which one is marked
            if ($field_object->__multiple && isset($element['#_validate_this'])) {
                $element['#validate'] = $field['data']['validate'];
            } else {
                if (!$field_object->__multiple) {
                    $element['#validate'] = $field['data']['validate'];
                }
            }
        }
        // Set atributes #2 (override)
        if (isset($field['disable'])) {
            $element['#disable'] = $field['disable'];
        }
        if (!empty($field['disable'])) {
            $element['#attributes']['disabled'] = 'disabled';
        }
        if (!empty($field['readonly'])) {
            $element['#attributes']['readonly'] = 'readonly';
            if (!empty($element['#options'])) {
                foreach ($element['#options'] as $key => $option) {
                    if (!is_array($option)) {
                        $element['#options'][$key] = array('#title' => $key, '#value' => $option);
                    }
                    $element['#options'][$key]['#attributes']['readonly'] = 'readonly';
                    if ($element['#type'] == 'select') {
                        $element['#options'][$key]['#attributes']['disabled'] = 'disabled';
                    }
                }
            }
            if ($element['#type'] == 'select') {
                $element['#attributes']['disabled'] = 'disabled';
            }
        }
        // Check if it was invalid on submit and add error message
        if ($post && !empty($invalid_fields)) {
            if (isset($invalid_fields[$element['#id']]['#error'])) {
                $element['#error'] = $invalid_fields[$element['#id']]['#error'];
            }
        }
        // TODO WPML move Set WPML locked icon
        if (wpcf_wpml_field_is_copied($field)) {
            $element['#title'] .= '<img src="' . WPCF_EMBEDDED_RES_RELPATH . '/images/locked.png" alt="' . __('This field is locked for editing because WPML will copy its value from the original language.', 'wpcf') . '" title="' . __('This field is locked for editing because WPML will copy its value from the original language.', 'wpcf') . '" style="position:relative;left:2px;top:2px;" />';
        }
        // Add repetitive class
        // TODO WPML move
        if (types_is_repetitive($field) && $context != 'post_relationship' && wpcf_wpml_field_is_copied($field)) {
            if (!empty($element['#options']) && $element['#type'] != 'select') {
                foreach ($element['#options'] as $temp_key => $temp_value) {
                    $element['#options'][$temp_key]['#attributes']['class'] = isset($element['#attributes']['class']) ? $element['#attributes']['class'] . ' wpcf-repetitive' : 'wpcf-repetitive';
                }
            } else {
                $element['#attributes']['class'] = isset($element['#attributes']['class']) ? $element['#attributes']['class'] . ' wpcf-repetitive' : 'wpcf-repetitive';
            }
            /*
            *
            *
            * Since Types 1.2 we allow same field values
            *
            * TODO Remove
            *
            * wpcf_admin_add_js_settings('wpcfFormRepetitiveUniqueValuesCheckText',
             '\'' . __('Warning: same values set', 'wpcf') . '\'');
            */
        }
        // Set read-only if copied by WPML
        // TODO WPML Move this to separate WPML code and use only hooks 1.1.5
        if (wpcf_wpml_field_is_copied($field)) {
            if (isset($element['#options'])) {
                foreach ($element['#options'] as $temp_key => $temp_value) {
                    if (isset($temp_value['#attributes'])) {
                        $element['#options'][$temp_key]['#attributes']['readonly'] = 'readonly';
                    } else {
                        $element['#options'][$temp_key]['#attributes'] = array('readonly' => 'readonly');
                    }
                }
            }
            if ($field['type'] == 'select') {
                if (isset($element['#attributes'])) {
                    $element['#attributes']['disabled'] = 'disabled';
                } else {
                    $element['#attributes'] = array('disabled' => 'disabled');
                }
            } else {
                if (isset($element['#attributes'])) {
                    $element['#attributes']['readonly'] = 'readonly';
                } else {
                    $element['#attributes'] = array('readonly' => 'readonly');
                }
            }
        }
        // Final filter for disabled if readonly
        if (isset($element['#attributes']['readonly']) || isset($element['#attributes']['disabled'])) {
            if (types_is_repetitive($field)) {
                $element['#name'] .= '[]';
            }
            if ($field['type'] == 'checkboxes') {
                if (isset($element['#options'])) {
                    foreach ($element['#options'] as $temp_key => $temp_value) {
                        $value = isset($temp_value['#default_value']) ? $temp_value['#default_value'] : $temp_value['#value'];
                        $_after = "<input type=\"hidden\" name=\"{$temp_value['#name']}\" value=\"{$value}\" />";
                        $temp_value['#after'] = isset($temp_value['#after']) ? $temp_value['#after'] . $_after : $_after;
                        $temp_value['#name'] = "wpcf-disabled[{$field['id']}_{$temp_value['#id']}]";
                        $temp_value['#attributes']['disabled'] = 'disabled';
                        $element['#options'][$temp_key] = $temp_value;
                    }
                }
            } else {
                if (in_array($element['#type'], array('checkbox', 'checkboxes', 'radios'))) {
                    if (isset($element['#options'])) {
                        foreach ($element['#options'] as $temp_key => $temp_value) {
                            $element['#options'][$temp_key]['#attributes']['disabled'] = 'disabled';
                        }
                    }
                    $value = isset($element['#default_value']) ? $element['#default_value'] : $element['#value'];
                    $_after = "<input type=\"hidden\" name=\"{$element['#name']}\" value=\"{$value}\" />";
                    $element['#after'] = isset($element['#after']) ? $element['#after'] . $_after : $_after;
                    $element['#attributes']['disabled'] = 'disabled';
                    $element['#name'] = "wpcf-disabled[{$field['id']}_{$element['#id']}]";
                } else {
                    $element['#attributes']['disabled'] = 'disabled';
                    if (is_array($element['#value'])) {
                        //$field['type'] == 'skype' ) {
                        $element['#value'] = array_shift($element['#value']);
                    }
                    $value = htmlentities($element['#value']);
                    $_after = "<input type=\"hidden\" name=\"{$element['#name']}\" value=\"{$value}\" />";
                    $element['#after'] = isset($element['#after']) ? $element['#after'] . $_after : $_after;
                    $element['#name'] = "wpcf-disabled[{$field['id']}_{$element['#id']}]";
                }
            }
        }
        return array('field' => $field, 'element' => $element);
    }
    return false;
}
/**
 * Editor callback form.
 *
 * @global object $wpdb
 *
 */
function wpcf_fields_image_editor_callback($field, $data, $context, $post)
{
    // Get post_ID
    $post_ID = !empty($post->ID) ? $post->ID : false;
    // Get attachment
    $image = false;
    $attachment_id = false;
    if ($post_ID) {
        $image = get_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true);
        if (empty($image)) {
            $user_id = wpcf_usermeta_get_user();
            $image = get_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['slug'], true);
        }
        if (!empty($image)) {
            // Get attachment by guid
            global $wpdb;
            $attachment_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment' AND guid=%s", $image));
        }
    }
    $data['image'] = $image;
    $data['attachment_id'] = $attachment_id;
    // Set post type
    $post_type = !empty($post->post_type) ? $post->post_type : '';
    // Set image_data
    $image_data = wpcf_fields_image_get_data($image);
    if (!in_array($post_type, array('view', 'view-template'))) {
        // We must ignore errors here and treat image as outsider
        if (!empty($image_data['error'])) {
            $image_data['is_outsider'] = 1;
            $image_data['is_attachment'] = 0;
        }
    } else {
        if (!empty($image_data['error'])) {
            $image_data['is_outsider'] = 0;
            $image_data['is_attachment'] = 0;
        }
    }
    $data['preview'] = $attachment_id ? wp_get_attachment_image($attachment_id, 'thumbnail') : '';
    // Title and Alt
    if ($attachment_id) {
        $alt = trim(strip_tags(get_post_meta($attachment_id, '_wp_attachment_image_alt', true)));
        $attachment_post = get_post($attachment_id);
        if (!empty($attachment_post)) {
            $title = trim(strip_tags($attachment_post->post_title));
        } else {
            if (!empty($alt)) {
                $title = $alt;
            }
        }
        if (empty($alt)) {
            $alt = $title;
        }
        if (!isset($data['title'])) {
            $data['title'] = $title;
        }
        if (!isset($data['alt'])) {
            $data['alt'] = $alt;
        }
    }
    // Align options
    $data['alignment_options'] = array('none' => __('None', 'wpcf'), 'left' => __('Left', 'wpcf'), 'center' => __('Center', 'wpcf'), 'right' => __('Right', 'wpcf'));
    // Remote images settings
    $fetch_remote = (bool) wpcf_get_settings('images_remote');
    $data['warning_remote'] = false;
    if (!types_is_repetitive($field) && $image_data['is_outsider'] && !$fetch_remote && !empty($data['image'])) {
        $data['warning_remote'] = true;
    }
    // Size settings
    $data['size_options'] = array('thumbnail' => sprintf(__('Thumbnail - %s', 'wpcf'), get_option('thumbnail_size_w') . 'x' . get_option('thumbnail_size_h')), 'medium' => sprintf(__('Medium - %s', 'wpcf'), get_option('medium_size_w') . 'x' . get_option('medium_size_h')), 'large' => sprintf(__('Large - %s', 'wpcf'), get_option('large_size_w') . 'x' . get_option('large_size_h')), 'full' => __('Original image', 'wpcf'));
    $wp_image_sizes = (array) get_intermediate_image_sizes();
    foreach ($wp_image_sizes as $wp_size) {
        if ($wp_size != 'post-thumbnail' && !array_key_exists($wp_size, $data['size_options'])) {
            $data['size_options'][$wp_size] = $wp_size;
        }
    }
    $data['size_options']['wpcf-custom'] = __('Custom size...', 'wpcf');
    // Get saved settings
    $data = array_merge(wpcf_admin_fields_get_field_last_settings($field['id']), $data);
    return array('supports' => array('styling', 'style'), 'tabs' => array('display' => array('menu_title' => __('Display options', 'wpcf'), 'title' => __('Display options for this field:', 'wpcf'), 'content' => WPCF_Loader::template('editor-modal-image', $data))), 'settings' => $data);
}
Beispiel #3
0
/**
 * Calls view function for specific field type.
 *
 * @param type $field
 * @param type $atts
 * @return type
 */
function types_render_field($field_id = null, $params = array(), $content = null, $code = '')
{
    if (empty($field_id)) {
        return '';
    }
    global $wpcf;
    // HTML var holds actual output
    $html = '';
    // Set post ID to global
    $post_id = get_the_ID();
    // Check if other post required
    if (isset($params['post_id'])) {
        // If numeric value
        if (is_numeric($params['post_id'])) {
            $post_id = intval($params['post_id']);
            // WP parent
        } else {
            if ($params['post_id'] == '$parent') {
                $current_post = get_post($post_id);
                if (empty($current_post->post_parent)) {
                    return '';
                }
                $post_id = $current_post->post_parent;
                // Types parent
            } else {
                if (strpos($params['post_id'], '$') === 0) {
                    $post_id = intval(WPCF_Relationship::get_parent($post_id, trim($params['post_id'], '$')));
                }
            }
        }
    }
    if (empty($post_id)) {
        return '';
    }
    // Set post
    $post = get_post($post_id);
    if (empty($post)) {
        return '';
    }
    // Get field
    $field = types_get_field($field_id);
    // If field not found return empty string
    if (empty($field)) {
        // Log
        if (!function_exists('wplogger')) {
            require_once WPCF_EMBEDDED_TOOLSET_ABSPATH . '/toolset-common/wplogger.php';
        }
        global $wplogger;
        $wplogger->log('types_render_field call for missing field \'' . $field_id . '\'', WPLOG_DEBUG);
        return '';
    }
    // Set field
    $wpcf->field->set($post, $field);
    // See if repetitive
    if (types_is_repetitive($field)) {
        $wpcf->repeater->set($post_id, $field);
        $_meta = $wpcf->repeater->_get_meta();
        $meta = $_meta['custom_order'];
        // Sometimes if meta is empty - array(0 => '') is returned
        if (count($meta) == 1 && reset($meta) == '') {
            return '';
        }
        if (!empty($meta)) {
            $output = '';
            if (isset($params['index'])) {
                $index = $params['index'];
            } else {
                $index = '';
            }
            // Allow wpv-for-each shortcode to set the index
            $index = apply_filters('wpv-for-each-index', $index);
            if ($index === '') {
                $output = array();
                foreach ($meta as $temp_key => $temp_value) {
                    $params['field_value'] = $temp_value;
                    $temp_output = types_render_field_single($field, $params, $content, $code, $temp_key);
                    if (!empty($temp_output)) {
                        $output[] = $temp_output;
                    }
                }
                if (!empty($output) && isset($params['separator']) && $params['separator'] !== '') {
                    $output = implode(html_entity_decode($params['separator']), $output);
                } else {
                    if (!empty($output)) {
                        $output = implode(' ', $output);
                    } else {
                        return '';
                    }
                }
            } else {
                // Make sure indexed right
                $_index = 0;
                foreach ($meta as $temp_key => $temp_value) {
                    if ($_index == $index) {
                        $params['field_value'] = $temp_value;
                        return types_render_field_single($field, $params, $content, $code, $temp_key);
                    }
                    $_index++;
                }
                // If missed index
                return '';
            }
            $html = $output;
        } else {
            return '';
        }
    } else {
        // Non-repetitive field
        $params['field_value'] = wpcf_get_post_meta($post_id, wpcf_types_get_meta_prefix($field) . $field['slug'], true);
        if ($params['field_value'] == '' && $field['type'] != 'checkbox') {
            return '';
        }
        $html = types_render_field_single($field, $params, $content, $code, $wpcf->field->meta_object->meta_id);
    }
    return $wpcf->field->html($html, $params);
}
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);
            }
        }
    }
}
Beispiel #5
0
/**
 * Gets posts that belongs to current post.
 * 
 * @global type $post
 * @param type $post_type
 * @param type $args
 * @return type 
 */
function types_child_posts($post_type, $args = array())
{
    static $cache = array();
    if (isset($args['post_id'])) {
        $post = $args['post_id'] != '0' ? get_post($args['post_id']) : null;
    } else {
        global $post;
    }
    if (empty($post->ID)) {
        return array();
    }
    $cache_key = md5($post->ID . serialize(func_get_args()));
    if (isset($cache[$cache_key])) {
        return $cache[$cache_key];
    }
    global $wp_post_types;
    // WP allows querying inactive post types
    if (!isset($wp_post_types[$post_type]) || !$wp_post_types[$post_type]->publicly_queryable) {
        return array();
    }
    $defaults = array('post_status' => array('publish'));
    $args = wp_parse_args($args, $defaults);
    WPCF_Loader::loadModel('relationship');
    WPCF_Loader::loadInclude('fields-post');
    $child_posts = WPCF_Relationship_Model::getChildrenByPostType($post, $post_type, array(), array(), $args);
    foreach ($child_posts as $child_post_key => $child_post) {
        $child_posts[$child_post_key]->fields = array();
        $groups = wpcf_admin_post_get_post_groups_fields($child_post);
        foreach ($groups as $group) {
            if (!empty($group['fields'])) {
                // Process fields
                foreach ($group['fields'] as $k => $field) {
                    $data = null;
                    if (types_is_repetitive($field)) {
                        $data = wpcf_get_post_meta($child_post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], false);
                        // get all field instances
                    } else {
                        $data = wpcf_get_post_meta($child_post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true);
                        // get single field instance
                        // handle checkboxes which are one value serialized
                        if ($field['type'] == 'checkboxes' && !empty($data)) {
                            $data = maybe_unserialize($data);
                        }
                    }
                    if (!is_null($data)) {
                        $child_posts[$child_post_key]->fields[$k] = $data;
                    }
                }
            }
        }
    }
    $cache[$cache_key] = $child_posts;
    return $child_posts;
}
/**
 * Calls view function for specific usermeta field type.
 *
 * @param $field_id
 * @param array $params (additional attributes: user_id, user_name, user_is_author, user_current)
 * @param null $content
 * @param string $code
 *
 * @return string|void
 * @since unknown
 */
function types_render_usermeta($field_id, $params, $content = null, $code = '')
{
    global $wpcf, $post, $wpdb, $WP_Views;
    $current_user = wp_get_current_user();
    $current_user_id = $current_user->ID;
    // Set post ID
    // user_id, user_name, user_is_author, user_current
    if (is_object($post)) {
        $post_id = $post->ID;
    } else {
        $post_id = 0;
    }
    if (isset($params['post_id']) && !empty($params['post_id'])) {
        $post_id = $params['post_id'];
    }
    // Get User id from views loop
    if (isset($WP_Views->users_data['term']->ID) && !empty($WP_Views->users_data['term']->ID)) {
        $params['user_id'] = $WP_Views->users_data['term']->ID;
    }
    //Get user By ID
    if (isset($params['user_id'])) {
        $user_id = $params['user_id'];
    } else {
        if (isset($params['user_name'])) {
            //Get user by login
            $user_id = $wpdb->get_var($wpdb->prepare("SELECT * FROM " . $wpdb->users . " WHERE user_login = %s", $params['user_name']));
        } else {
            if (isset($params['user_is_author'])) {
                //Get Post author
                $user_id = $post->post_author;
            } else {
                if (isset($params['user_current'])) {
                    //Get current logged user
                    $user_id = $current_user_id;
                } else {
                    //If empty get post author, if no post, return empty
                    if (!empty($post_id)) {
                        $user_id = $post->post_author;
                    } else {
                        return '';
                    }
                }
            }
        }
    }
    if (empty($user_id)) {
        return '';
    }
    // Get field
    $field = types_get_field($field_id, 'usermeta');
    // If field not found return empty string
    if (empty($field)) {
        // Log
        if (!function_exists('wplogger')) {
            require_once WPCF_EMBEDDED_TOOLSET_ABSPATH . '/toolset-common/wplogger.php';
        }
        global $wplogger;
        $wplogger->log('types_render_field call for missing field \'' . $field_id . '\'', WPLOG_DEBUG);
        return '';
    }
    if (types_is_repetitive($field)) {
        $wpcf->usermeta_repeater->set($user_id, $field);
        $_meta = $wpcf->usermeta_repeater->_get_meta();
        $meta = toolset_getarr($_meta, 'custom_order', '');
        // Sometimes if meta is empty - array(0 => '') is returned
        if (count($meta) == 1 && reset($meta) == '') {
            return '';
        }
        if (!empty($meta)) {
            $output = '';
            if (isset($params['index'])) {
                $index = $params['index'];
            } else {
                $index = '';
            }
            // Allow wpv-for-each shortcode to set the index
            $index = apply_filters('wpv-for-each-index', $index);
            if ($index === '') {
                $output = array();
                foreach ($meta as $temp_key => $temp_value) {
                    $params['field_value'] = $temp_value;
                    $temp_output = types_render_field_single($field, $params, $content, $code, $temp_key);
                    if (!Toolset_Utils::is_field_value_truly_empty($temp_output)) {
                        $output[] = $temp_output;
                    }
                }
                if (!empty($output) && isset($params['separator'])) {
                    $output = implode(html_entity_decode($params['separator']), $output);
                } else {
                    if (!empty($output)) {
                        $output = implode('', $output);
                    } else {
                        return '';
                    }
                }
            } else {
                // Make sure indexed right
                $_index = 0;
                foreach ($meta as $temp_key => $temp_value) {
                    if ($_index == $index) {
                        $params['field_value'] = $temp_value;
                        $output = types_render_field_single($field, $params, $content, $code, $temp_key);
                    }
                    $_index++;
                }
            }
            $html = $output;
        } else {
            return '';
        }
    } else {
        $params['field_value'] = get_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['slug'], true);
        if ($params['field_value'] == '' && $field['type'] != 'checkbox') {
            return '';
        }
        $html = types_render_field_single($field, $params, $content, $code);
    }
    // API filter
    $wpcf->usermeta_field->set($user_id, $field);
    return $wpcf->usermeta_field->html($html, $params);
}
Beispiel #7
-1
 /**
  * Renders Thickbox content.
  *
  * Field should provide callback function
  * that will be called automatically.
  *
  * Function should be named like:
  * 'wpcf_fields_' . $field_type . '_editor_callback'
  * e.g. 'wpcf_fields_checkbox__editor_callback'
  *
  * Function should return array with elements:
  * 'supports' - parameters or other feature supported, e.g. 'styling' will
  *     enable 'Styling' options
  *
  * Tabs is array with elements:
  * 'menu_title' - used for menu title
  * 'title' - used for main title
  * 'content' - HTML content of tab
  *
  * @param type $field
  * @param type $meta_type
  * @param type $post_id
  * @param string $shortcode
  */
 function frame($field, $meta_type = 'postmeta', $post_id = -1, $shortcode = null, $callback = false, $views_usermeta = false)
 {
     global $wp_version, $wpcf;
     // Queue rendering JS settings
     add_action('admin_print_footer_scripts', array($this, 'renderTedSettings'), 1);
     wp_enqueue_script('types');
     wp_enqueue_script('types-knockout');
     wp_enqueue_script('types-editor');
     wp_enqueue_script('wp-pointer');
     wp_enqueue_style('types-editor');
     wp_enqueue_style('wp-pointer');
     wp_enqueue_style('toolset-font-awesome');
     // Load cloned WP Media Modal CSS
     if (version_compare($wp_version, '3.5', '<')) {
         wp_enqueue_style('types-editor-cloned');
     }
     $this->field = $field;
     $this->_meta_type = $meta_type;
     $this->_post = get_post($post_id);
     $this->_settings = is_null($shortcode) ? array() : $this->shortcodeToParameters($shortcode);
     $this->callback = $callback;
     $this->_data = array('meta_type' => $meta_type, 'field' => $field, 'field_type_data' => WPCF_Fields::getFieldTypeData($field['type']), 'settings' => array(), 'tabs' => array(), 'supports' => array(), 'post' => $this->_post, 'post_types' => get_post_types(array('show_ui' => true)), 'style' => isset($this->_settings['style']) ? $this->_settings['style'] : '', 'class' => isset($this->_settings['class']) ? $this->_settings['class'] : '', 'output' => 'html', 'user_form' => '');
     // Set title if updated
     if (!is_null($shortcode)) {
         $this->_data['title'] = sprintf(__('Update %s', 'wpcf'), $this->_data['field_type_data']['title']);
         $this->_data['submit_button_title'] = __('Update shortcode', 'wpcf');
     }
     // Exclude post types
     foreach ($wpcf->excluded_post_types as $_post_type) {
         unset($this->_data['post_types'][$_post_type]);
     }
     /*
      * Callback
      */
     $function = 'wpcf_fields_' . $field['type'] . '_editor_callback';
     if (function_exists($function)) {
         // Main callback
         $callback = call_user_func($function, $field, $this->_settings, $this->_meta_type, $this->_post);
         // Add supports
         if (!empty($callback['supports']) && is_array($callback['supports'])) {
             $this->_data['supports'] = $callback['supports'];
         }
         // Add tabs
         if (!empty($callback['tabs']) && is_array($callback['tabs'])) {
             $this->_data['tabs'] = $callback['tabs'];
         }
         // Unify settings
         if (!empty($callback['settings']) && is_array($callback['settings'])) {
             $this->_settings = array_merge($this->_settings, self::sanitizeParams($callback['settings'], 'array'));
         }
     }
     // If no tabs
     if (empty($this->_data['tabs'])) {
         $this->_data['tabs']['display'] = array('menu_title' => __('Display', 'wpcf'), 'title' => __('Display', 'wpcf'), 'content' => sprintf(__('There are no additional display options for the %s field.', 'wpcf'), $this->_data['field_type_data']['title']));
     }
     // Add User ID form
     if ($this->_meta_type == 'usermeta') {
         if (!$views_usermeta) {
             $this->_data['user_form'] = wpcf_form_simple(wpcf_get_usermeta_form_addon($this->_settings));
             $this->_data['supports'][] = 'user_id';
         }
     } else {
         // Add Post ID form
         $this->_data['supports'][] = 'post_id';
     }
     // Get parents
     if (!empty($this->_post->ID)) {
         $this->_data['parents'] = WPCF_Relationship::get_parents($this->_post);
     }
     // Set icons
     $icons = array('audio' => 'icon-music', 'checkbox' => 'icon-check', 'checkboxes' => 'icon-checkboxes', 'colorpicker' => 'icon-tint', 'date' => 'icon-calendar', 'email' => 'icon-envelope-alt', 'embed' => 'icon-youtube-play', 'file' => 'icon-file-alt', 'image' => 'icon-picture', 'map' => 'icon-map-marker', 'numeric' => 'icon-numeric', 'phone' => 'icon-phone', 'radio' => 'icon-radio-button', 'select' => 'icon-select-box', 'skype' => 'icon-skype', 'textarea' => 'icon-text-area', 'textfield' => 'icon-text-field', 'url' => 'icon-link', 'video' => 'icon-film', 'wysiwyg' => 'icon-wysiwyg');
     $this->_data['icon_class'] = isset($icons[$field['type']]) ? $icons[$field['type']] : 'icon-text-field';
     // Is repetitive
     $this->_data['is_repetitive'] = (bool) types_is_repetitive($field);
     if ($this->_data['is_repetitive']) {
         $this->_data['supports'][] = 'separator';
     }
     // Render header
     wpcf_admin_ajax_head();
     // Check if submitted
     $this->_thickbox_check_submit();
     // Render form
     echo '<form method="post" action="" id="types-editor-modal-form">';
     echo WPCF_Loader::view('editor-modal-window', $this->_data);
     wp_nonce_field('types_editor_frame', '__types_editor_nonce');
     echo '</form>';
     // Render footer
     wpcf_admin_ajax_footer();
 }