/**
 * Add meta boxes.
 *
 * @param type $post_type
 * @param type $post
 * @return boolean
 */
function wpcf_add_meta_boxes($post_type, $post)
{
    $post_types_without_meta_boxes = array('view', 'view-template', 'acf-field-group', 'acf');
    /**
     * Do not add metaboxes
     *
     * Filter allow to add post types which are immune to wpcf_add_meta_boxes()
     * function.
     *
     * @since 1.9.0
     *
     * @param array $post_types array of post types slugs
     */
    $post_types_without_meta_boxes = apply_filters('wpcf_exclude_meta_boxes_on_post_type', $post_types_without_meta_boxes);
    /** This action is documented in embedded/bootstrap.php */
    $post_types_without_meta_boxes = apply_filters('toolset_filter_exclude_own_post_types', $post_types_without_meta_boxes);
    /**
     * check
     */
    if (!empty($post_types_without_meta_boxes) && in_array($post_type, $post_types_without_meta_boxes)) {
        return false;
    }
    //  Fix for empty $post (tabify)
    if (empty($post->ID)) {
        $post = get_default_post_to_edit($post_type, false);
    }
    // Get groups
    $groups = wpcf_admin_post_get_post_groups_fields($post);
    foreach ($groups as $group) {
        $only_preview = '';
        //If Access plugin activated
        if (function_exists('wpcf_access_register_caps')) {
            //If user can't view own profile fields
            if (!current_user_can('view_fields_in_edit_page_' . $group['slug'])) {
                continue;
            }
            //If user can modify current group in own profile
            if (!current_user_can('modify_fields_in_edit_page_' . $group['slug'])) {
                $only_preview = 1;
            }
        }
        // Process fields
        if (!empty($group['fields']) && empty($only_preview)) {
            if (defined('WPTOOLSET_FORMS_VERSION')) {
                $errors = get_post_meta($post->ID, '__wpcf-invalid-fields', true);
                $group['html'] = array();
                foreach ($group['fields'] as $field) {
                    wpcf_admin_post_add_to_editor($field);
                    // Note that the $single argument defaults to false, ergo $meta will be allways an array of metadata,
                    // even for single fields.
                    $meta = get_post_meta($post->ID, $field['meta_key']);
                    $config = wptoolset_form_filter_types_field($field, $post->ID);
                    if ($errors) {
                        $config['validate'] = true;
                    }
                    if ($field['type'] == 'wysiwyg') {
                        $group['html'][] = array('config' => $config, 'meta' => $meta);
                    } else {
                        $group['html'][] = wptoolset_form_field('post', $config, $meta);
                    }
                }
                // OLD
                delete_post_meta($post->ID, 'wpcf-invalid-fields');
                delete_post_meta($post->ID, '__wpcf-invalid-fields');
            } else {
                // Process fields
                $group['fields'] = wpcf_admin_post_process_fields($post, $group['fields'], true);
            }
        }
        // Check if hidden
        if (!isset($group['__show_meta_box']) || $group['__show_meta_box'] != false) {
            $field_group = Types_Field_Group_Post_Factory::load($group['slug']);
            // Skip field groups that can't be loaded
            if (null !== $field_group) {
                $group_wpml = new Types_Wpml_Field_Group($field_group);
                // Add meta boxes
                if (empty($only_preview)) {
                    add_meta_box("wpcf-group-{$group['slug']}", $group_wpml->translate_name(), 'wpcf_admin_post_meta_box', $post_type, $group['meta_box_context'], 'high', $group);
                } else {
                    add_meta_box("wpcf-group-{$group['slug']}", $group_wpml->translate_name(), 'wpcf_admin_post_meta_box_preview', $post_type, $group['meta_box_context'], 'high', $group);
                }
            }
        }
    }
}
function wpcf_admin_render_fields($group, $user_id, $echo = '')
{
    global $wpcf;
    $group_wpml = new Types_Wpml_Field_Group(Types_Field_Group_User_Factory::load($group['slug']));
    $output = '<div class="wpcf-group-area wpcf-group-area_' . $group['slug'] . '">' . "\n\n";
    $output .= '<h3>' . $group_wpml->translate_name() . '</h3>' . "\n\n";
    if (!empty($group['fields'])) {
        // Display description
        if (!empty($group['description'])) {
            $output .= '<span>' . wpautop($group_wpml->translate_description()) . '</span>' . "\n\n";
        }
        $output .= '<div class="wpcf-profile-field-line">' . "\n\n";
        foreach ($group['fields'] as $field_slug => $field) {
            if (empty($field) || !is_array($field)) {
                continue;
            }
            $field = $wpcf->usermeta_field->_parse_cf_form_element($field);
            if (!isset($field['#id'])) {
                $field['#id'] = wpcf_unique_id(serialize($field));
            }
            if (isset($field['wpcf-type'])) {
                // May be ignored
                $field = apply_filters('wpcf_fields_' . $field['wpcf-type'] . '_meta_box_form_value_display', $field);
            }
            // Render form elements
            if (wpcf_compare_wp_version() && $field['#type'] == 'wysiwyg') {
                $field['#editor_settings']['media_buttons'] = '';
                if (!empty($echo)) {
                    $field['#editor_settings']['wpautop'] = true;
                }
                // Especially for WYSIWYG
                $output .= "\n" . '<div class="wpcf-profile-field-line">' . "\n\n";
                $output .= '<div class="wpcf-wysiwyg">' . "\n\n";
                $output .= '<div id="wpcf-textarea-textarea-wrapper" class="form-item form-item-textarea wpcf-form-item wpcf-form-item-textarea">' . "\n\n";
                $output .= isset($field['#before']) ? $field['#before'] : '';
                $output .= '<label class="wpcf-form-label wpcf-form-textarea-label">' . $field['#title'] . '</label>' . "\n\n";
                $output .= '<div class="description wpcf-form-description wpcf-form-description-textarea description-textarea">' . "\n\n" . wpautop($field['#description']) . '</div>' . "\n\n";
                ob_start();
                wp_editor($field['#value'], $field['#id'], $field['#editor_settings']);
                $output .= ob_get_clean() . "\n\n";
                $field['slug'] = str_replace(WPCF_META_PREFIX . 'wysiwyg-', '', $field_slug);
                $field['type'] = 'wysiwyg';
                $output .= '</div>' . "\n\n";
                $output .= isset($field['#after']) ? $field['#after'] : '';
                $output .= '</div>' . "\n\n";
                $output .= '</div>' . "\n\n";
            } else {
                if ($field['#type'] == 'wysiwyg') {
                    $field['#type'] = 'textarea';
                }
                $field['#pattern'] = "\n" . '<div class="wpcf-profile-field-line">
	<div class="wpcf-profile-line-left">
		<LABEL><DESCRIPTION>
	</div>
	<div class="wpcf-profile-line-right"><BEFORE><ERROR><PREFIX><ELEMENT><SUFFIX><AFTER></div>
</div>' . "\n\n";
                if (isset($field['#name']) && (strpos($field['#name'], '[hour]') !== false || strpos($field['#name'], '[minute]') !== false)) {
                    if (isset($field['#attributes']) && $field['#attributes']['class'] == 'wpcf-repetitive') {
                        $field['#pattern'] = strpos($field['#name'], '[hour]') !== false ? __('Hour', 'wpcf') : __('Minute', 'wpcf');
                        $field['#pattern'] .= '<LABEL><DESCRIPTION><ERROR><PREFIX><ELEMENT><SUFFIX><AFTER>' . "\n\n";
                    } else {
                        if (strpos($field['#name'], '[hour]') !== false) {
                            $field['#pattern'] = "\n" . '<div class="wpcf-profile-field-line">
	<div class="wpcf-profile-line-left">&nbsp;&nbsp;&nbsp;&nbsp;' . __('Time', 'wpcf') . '</div>
	<div class="wpcf-profile-line-right">
	<LABEL><DESCRIPTION><ERROR><PREFIX><ELEMENT><SUFFIX><AFTER>' . "\n";
                        } else {
                            $field['#pattern'] = "\n" . '
	<LABEL><DESCRIPTION><ERROR><PREFIX><ELEMENT><SUFFIX><AFTER></div>
</div>' . "\n\n";
                        }
                    }
                }
                if (!empty($echo)) {
                    $field['#validate'] = '';
                }
                $output .= wpcf_form_simple(array($field['#id'] => $field));
            }
        }
        $output .= '</div>';
    }
    /*
     * TODO Move to Conditional code
     *
     * This is already checked. Use hook to add wrapper DIVS and apply CSS.
     */
    if (!empty($group['_conditional_display'])) {
        $output .= '</div>';
    }
    $output .= "\n\n" . '</div>';
    if (!empty($echo)) {
        return $output;
    } else {
        echo $output;
    }
}