Пример #1
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);
}