File "class-acf-field-link.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/advanced-custom-fields/includes/fields/class-acf-field-link.php
File size: 3.92 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

if (!class_exists('acf_field_link')) {
    class acf_field_link extends acf_field
    {
        function initialize()
        {
            $this->name = 'link';
            $this->label = __('Link', 'acf');
            $this->category = 'relational';
            $this->defaults = array('return_format' => 'array');
        }
        function get_link($value = '')
        {
            $link = array('title' => '', 'url' => '', 'target' => '');
            if (is_array($value)) {
                $link = array_merge($link, $value);
            } elseif (is_numeric($value)) {
                $link['title'] = get_the_title($value);
                $link['url'] = get_permalink($value);
            } elseif (is_string($value)) {
                $link['url'] = $value;
            }
            return $link;
        }
        function render_field($field)
        {
            $div = array('id' => $field['id'], 'class' => $field['class'] . ' acf-link');
            acf_enqueue_uploader();
            $link = $this->get_link($field['value']);
            if ($link['url']) {
                $div['class'] .= ' -value';
            }
            if ($link['target'] === '_blank') {
                $div['class'] .= ' -external';
            }
            ?>
<div <?php 
            acf_esc_attr_e($div);
            ?>>
	
	<div class="acf-hidden">
		<a class="link-node" href="<?php 
            echo esc_url($link['url']);
            ?>" target="<?php 
            echo esc_attr($link['target']);
            ?>"><?php 
            echo esc_html($link['title']);
            ?></a>
			<?php 
            foreach ($link as $k => $v) {
                ?>
				<?php 
                acf_hidden_input(array('class' => "input-{$k}", 'name' => $field['name'] . "[{$k}]", 'value' => $v));
                ?>
		<?php 
            }
            ?>
	</div>
	
	<a href="#" class="button" data-name="add" target=""><?php 
            _e('Select Link', 'acf');
            ?></a>
	
	<div class="link-wrap">
		<span class="link-title"><?php 
            echo esc_html($link['title']);
            ?></span>
		<a class="link-url" href="<?php 
            echo esc_url($link['url']);
            ?>" target="_blank"><?php 
            echo esc_html($link['url']);
            ?></a>
		<i class="acf-icon -link-ext acf-js-tooltip" title="<?php 
            _e('Opens in a new window/tab', 'acf');
            ?>"></i><a class="acf-icon -pencil -clear acf-js-tooltip" data-name="edit" href="#" title="<?php 
            _e('Edit', 'acf');
            ?>"></a><a class="acf-icon -cancel -clear acf-js-tooltip" data-name="remove" href="#" title="<?php 
            _e('Remove', 'acf');
            ?>"></a>
	</div>
	
</div>
			<?php 
        }
        function render_field_settings($field)
        {
            acf_render_field_setting($field, array('label' => __('Return Value', 'acf'), 'instructions' => __('Specify the returned value on front end', 'acf'), 'type' => 'radio', 'name' => 'return_format', 'layout' => 'horizontal', 'choices' => array('array' => __('Link Array', 'acf'), 'url' => __('Link URL', 'acf'))));
        }
        function format_value($value, $post_id, $field)
        {
            if (empty($value)) {
                return $value;
            }
            $link = $this->get_link($value);
            if ($field['return_format'] == 'url') {
                return $link['url'];
            }
            return $link;
        }
        function validate_value($valid, $value, $field, $input)
        {
            if (!$field['required']) {
                return $valid;
            }
            if (empty($value) || empty($value['url'])) {
                return false;
            }
            return $valid;
        }
        function update_value($value, $post_id, $field)
        {
            if (empty($value) || empty($value['url'])) {
                $value = '';
            }
            return $value;
        }
    }
    acf_register_field_type('acf_field_link');
}