/**
 * Renders meta box content.
 *
 * @param $post
 * @param array $group
 * @param string $echo
 * @param bool $open_style_editor if true use code for open style editor when edit group
 *
 * @return string
 */
function wpcf_admin_post_meta_box($post, $group, $echo = '', $open_style_editor = false)
{
    $field_group = Types_Field_Group_Post_Factory::load($group['args']['slug']);
    // todo Handle null $field_group.
    $group_wpml = new Types_Wpml_Field_Group($field_group);
    if (false === $open_style_editor && defined('WPTOOLSET_FORMS_VERSION')) {
        if (isset($group['args']['html'])) {
            /**
             * show group description
             */
            if (array_key_exists('description', $group['args']) && !empty($group['args']['description'])) {
                echo '<div class="wpcf-meta-box-description">';
                echo wpautop($group_wpml->translate_description());
                echo '</div>';
            }
            foreach ($group['args']['html'] as $field) {
                echo is_array($field) ? wptoolset_form_field('post', $field['config'], $field['meta']) : $field;
            }
        }
        return;
    }
    global $wpcf;
    /**
     * fake post object if need
     */
    $post = wpcf_admin_create_fake_post_if_need($post);
    static $nonce_added = false;
    $group_output = '';
    if (!isset($group['title'])) {
        $temp = $group;
        $group = '';
        $group['args'] = $temp;
        $group['id'] = $temp['slug'];
        $group['title'] = $temp['name'];
        $name = $temp['name'];
    }
    if (!empty($echo)) {
        $group_output = '<h3>This Preview generated for latest post "' . $post->post_title . '"</h3>' . "\n" . '<!-- Previous lines visible only in Admin Style Editor.-->' . "\n\n";
        $group_output .= '<div id="wpcf-group-' . $group['id'] . '" class="postbox " >
            <h3 class=\'hndle\'><span>' . $name . '</span></h3>
            <div class="inside">' . "\n";
    }
    /*
     * TODO Document where this is used
     */
    if (!$nonce_added && empty($echo)) {
        $nonce_action = 'update-' . $post->post_type . '_' . $post->ID;
        wp_nonce_field($nonce_action, '_wpcf_post_wpnonce');
        $nonce_added = true;
    }
    $group_output .= "\n\n" . '<div id="wpcf-group-metabox-id-' . $group['args']['slug'] . '">' . "\n";
    /*
     * TODO Move to Conditional code
     *
     * This is already checked. Use hook to add wrapper DIVS and apply CSS.
     */
    if (!empty($group['args']['_conditional_display'])) {
        if ($group['args']['_conditional_display'] == 'failed') {
            $group_output .= '<div class="wpcf-cd-group wpcf-cd-group-failed" style="display:none;">';
        } else {
            $group_output .= '<div class="wpcf-cd-group wpcf-cd-group-passed">';
        }
    }
    /*
     * TODO Move this into Field code
     * Process fields
     */
    if (!empty($group['args']['fields'])) {
        // Display description
        if (!empty($group['args']['description'])) {
            $group_output .= '<div class="wpcf-meta-box-description">' . wpautop($group_wpml->translate_description()) . '</div>';
        }
        foreach ($group['args']['fields'] as $field_slug => $field) {
            if (empty($field) || !is_array($field)) {
                continue;
            }
            $field = $wpcf->field->_parse_cf_form_element($field);
            if (isset($field['wpcf-type'])) {
                // May be ignored
                $field = apply_filters('wpcf_fields_' . $field['wpcf-type'] . '_meta_box_form_value_display', $field);
            }
            if (!isset($field['#id'])) {
                $field['#id'] = wpcf_unique_id(serialize($field));
            }
            // Render form elements
            if (wpcf_compare_wp_version() && array_key_exists('#type', $field) && 'wysiwyg' == $field['#type'] && !isset($field['#attributes']['disabled'])) {
                //                if ( isset( $field['#attributes']['disabled'] ) ) {
                //                    $field['#editor_settings']['tinymce'] = false;
                //                    $field['#editor_settings']['teeny'] = false;
                //                    $field['#editor_settings']['media_buttons'] = false;
                //                    $field['#editor_settings']['quicktags'] = false;
                //                    $field['#editor_settings']['dfw'] = false;
                //                }
                // Especially for WYSIWYG
                $group_output .= '<div class="wpcf-wysiwyg">';
                $group_output .= '<div id="wpcf-textarea-textarea-wrapper" class="form-item form-item-textarea wpcf-form-item wpcf-form-item-textarea">';
                $group_output .= isset($field['#before']) ? $field['#before'] : '';
                $group_output .= '<label class="wpcf-form-label wpcf-form-textarea-label">' . stripslashes($field['#title']) . '</label>';
                $group_output .= '<div class="description wpcf-form-description wpcf-form-description-textarea description-textarea">
                    ' . wpautop($field['#description']) . '</div>';
                ob_start();
                wp_editor($field['#value'], $field['#id'], $field['#editor_settings']);
                $group_output .= ob_get_clean() . "\n\n";
                $field['slug'] = str_replace(WPCF_META_PREFIX . 'wysiwyg-', '', $field_slug);
                $field['type'] = 'wysiwyg';
                $group_output .= '</div>';
                $group_output .= isset($field['#after']) ? $field['#after'] : '';
                $group_output .= '</div>';
            } else {
                if (array_key_exists('#type', $field) && 'wysiwyg' == $field['#type']) {
                    $field['#type'] = 'textarea';
                }
                if (!empty($echo)) {
                    $field['#validate'] = '';
                }
                $group_output .= wpcf_form_simple(array($field['#id'] => $field));
            }
            do_action('wpcf_fields_' . $field_slug . '_meta_box_form', $field);
            if (isset($field['wpcf-type'])) {
                // May be ignored
                do_action('wpcf_fields_' . $field['wpcf-type'] . '_meta_box_form', $field);
            }
        }
    }
    /*
     * TODO Move to Conditional code
     *
     * This is already checked. Use hook to add wrapper DIVS and apply CSS.
     */
    if (!empty($group['args']['_conditional_display'])) {
        $group_output .= '</div>';
    }
    $group_output .= '</div>';
    if (!empty($echo)) {
        $group_output .= "\n\n</div></div>";
        return $group_output;
    } else {
        echo $group_output;
    }
}
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;
    }
}
Esempio n. 3
0
/**
 * 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;
}
Esempio n. 4
0
/**
 * Bulk translation.
 */
function wpcf_admin_bulk_string_translation()
{
    if (!function_exists('icl_register_string')) {
        return false;
    }
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/custom-types.php';
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/custom-taxonomies.php';
    // Register groups
    $groups = wpcf_admin_fields_get_groups();
    foreach ($groups as $group_id => $group) {
        $group_wpml = new Types_Wpml_Field_Group(Types_Field_Group_Post_Factory::load($group['slug']));
        $group_wpml->register();
    }
    // Register fields
    $fields = wpcf_admin_fields_get_fields();
    foreach ($fields as $field_id => $field) {
        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' name', $field['name']);
        if (isset($field['description'])) {
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' description', $field['description']);
        }
        // For radios or select
        if (!empty($field['data']['options'])) {
            foreach ($field['data']['options'] as $name => $option) {
                if ($name == 'default') {
                    continue;
                }
                if (isset($option['title'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' title', $option['title']);
                }
                if (wpcf_wpml_field_is_translated($field)) {
                    if (isset($option['value'])) {
                        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' value', $option['value']);
                    }
                }
                if (isset($option['display_value'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' display value', $option['display_value']);
                }
            }
        }
        if ($field['type'] == 'checkbox' && (isset($field['set_value']) && $field['set_value'] != '1')) {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value', $field['set_value']);
        }
        if ($field['type'] == 'checkbox' && !empty($field['display_value_selected'])) {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value selected', $field['display_value_selected']);
        }
        if ($field['type'] == 'checkbox' && !empty($field['display_value_not_selected'])) {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value not selected', $field['display_value_not_selected']);
        }
        // Validation message
        if (!empty($field['data']['validate'])) {
            foreach ($field['data']['validate'] as $method => $validation) {
                if (!empty($validation['message'])) {
                    // Skip if it's same as default
                    $default_message = wpcf_admin_validation_messages($method);
                    if ($validation['message'] != $default_message) {
                        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' validation message ' . $method, $validation['message']);
                    }
                }
            }
        }
    }
    // Register types
    $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
    foreach ($custom_types as $post_type => $data) {
        wpcf_custom_types_register_translation($post_type, $data);
    }
    // Register taxonomies
    $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array());
    foreach ($custom_taxonomies as $taxonomy => $data) {
        wpcf_custom_taxonimies_register_translation($taxonomy, $data);
    }
}