Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #121 from asedano/master
Browse files Browse the repository at this point in the history
Compatibility with 5.7
  • Loading branch information
funkjedi authored Sep 27, 2018
2 parents ed80267 + 1410ca4 commit e0c11c5
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 42 deletions.
191 changes: 152 additions & 39 deletions assets/acf_5/main.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,164 @@

/**
* Clone functionality from standard Image field type
*/
acf.fields.qtranslate_image = acf.fields.image.extend({
type: 'qtranslate_image',
focus: function() {
this.$el = this.$field.find('.acf-image-uploader.current-language');
this.$input = this.$el.find('input[type="hidden"]');
this.$img = this.$el.find('img');

this.o = acf.get_data(this.$el);
acf.registerFieldType(acf.models.ImageField.extend({
type: 'qtranslate_image',

$control: function(){
return this.$('.acf-image-uploader.current-language');
},

$input: function(){
return this.$('.acf-image-uploader.current-language input[type="hidden"]');
},

render: function( attachment ){
var control = this.$control();

// vars
attachment = this.validateAttachment( attachment );

// update image
control.find('img').attr({
src: attachment.url,
alt: attachment.alt,
title: attachment.title
});

// vars
var val = attachment.id || '';

// update val
this.val( val );

// update class
if( val ) {
control.addClass('has-value');
} else {
control.removeClass('has-value');
}
});
}
}));

/**
* Clone functionality from standard File field type
*/
acf.fields.qtranslate_file = acf.fields.file.extend({
type: 'qtranslate_file',
focus: function() {
this.$el = this.$field.find('.acf-file-uploader.current-language');
this.$input = this.$el.find('input[type="hidden"]');
acf.registerFieldType(acf.models.QtranslateImageField.extend({
type: 'qtranslate_file',
render: function( attachment ){
var control = this.$control();

// vars
attachment = this.validateAttachment( attachment );

this.o = acf.get_data(this.$el);
// update image
this.$control().find(' img').attr({
src: attachment.icon,
alt: attachment.alt,
title: attachment.title
});

// update elements
control.find('[data-name="title"]').text( attachment.title );
control.find('[data-name="filename"]').text( attachment.filename ).attr( 'href', attachment.url );
control.find('[data-name="filesize"]').text( attachment.filesizeHumanReadable );

// vars
var val = attachment.id || '';

// update val
acf.val( this.$input(), val );

// update class
if( val ) {
control.addClass('has-value');
} else {
control.removeClass('has-value');
}
});
},
}));

/**
* Clone functionality from standard WYSIWYG field type
*/
acf.fields.qtranslate_wysiwyg = acf.fields.wysiwyg.extend({
type: 'qtranslate_wysiwyg',
focus: function() {
this.$el = this.$field.find('.wp-editor-wrap.current-language').last();
this.$textarea = this.$el.find('textarea');
this.o = acf.get_data(this.$el);
this.o.id = this.$textarea.attr('id');
},
initialize: function() {
var self = this;
this.$field.find('.wp-editor-wrap').each(function() {
self.$el = jQuery(this);
self.$textarea = self.$el.find('textarea');
self.o = acf.get_data(self.$el);
self.o.id = self.$textarea.attr('id');
acf.fields.wysiwyg.initialize.call(self);
});
this.focus();
acf.registerFieldType(acf.models.WysiwygField.extend({
type: 'qtranslate_wysiwyg',
initializeEditor: function() {
var self = this;
this.$('.acf-editor-wrap').each(function() {
var $wrap = $(this);
var $textarea = $wrap.find('textarea');
var args = {
tinymce: true,
quicktags: true,
toolbar: self.get('toolbar'),
mode: self.getMode(),
field: self
};

// generate new id
var oldId = $textarea.attr('id');
var newId = acf.uniqueId('acf-editor-');

// rename
acf.rename({
target: $wrap,
search: oldId,
replace: newId,
destructive: true
});

// update id
self.set('id', newId, true);

// initialize
acf.tinymce.initialize(newId, args);
});
}
}));

acf.registerFieldType(acf.models.UrlField.extend({
type: 'qtranslate_url',
$control: function(){
return this.$('.acf-input-wrap.current-language');
},

$input: function(){
return this.$('.acf-input-wrap.current-language input[type="url"]');
},
isValid: function(){

// vars
var val = this.val();

// bail early if no val
if( !val ) {
return false;
}

// url
if( val.indexOf('://') !== -1 ) {
return true;
}

// protocol relative url
if( val.indexOf('//') === 0 ) {
return true;
}

// relative url
if( val.indexOf('/') === 0 ) {
return true;
}

// return
return false;
},

render: function(){

// add class
if( this.isValid() ) {
this.$control().addClass('-valid');
} else {
this.$control().removeClass('-valid');
}
});
},
}));
5 changes: 5 additions & 0 deletions assets/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
.multi-language-field > textarea,
.multi-language-field > fieldset,
.multi-language-field .acf_wysiwyg,
.multi-language-field .acf-url,
.multi-language-field .acf-editor-wrap,
.multi-language-field .acf-file-uploader,
.multi-language-field .acf-image-uploader { display: none }
Expand All @@ -79,6 +80,10 @@
margin-top: 0;
}

.acf-input .acf-url input[type="text"] {
padding-left: 25px;
}

.multi-language-field .acf-file-uploader,
.multi-language-field .acf-image-uploader {
padding: 20px 10px 10px;
Expand Down
2 changes: 2 additions & 0 deletions src/acf_5/acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ public function include_fields() {
require_once ACF_QTRANSLATE_PLUGIN_DIR . 'src/acf_5/fields/text.php';
require_once ACF_QTRANSLATE_PLUGIN_DIR . 'src/acf_5/fields/textarea.php';
require_once ACF_QTRANSLATE_PLUGIN_DIR . 'src/acf_5/fields/wysiwyg.php';
require_once ACF_QTRANSLATE_PLUGIN_DIR . 'src/acf_5/fields/url.php';

acf()->fields->register_field_type(new acf_qtranslate_acf_5_text($this->plugin));
acf()->fields->register_field_type(new acf_qtranslate_acf_5_textarea($this->plugin));
acf()->fields->register_field_type(new acf_qtranslate_acf_5_wysiwyg($this->plugin));
acf()->fields->register_field_type(new acf_qtranslate_acf_5_image($this->plugin));
acf()->fields->register_field_type(new acf_qtranslate_acf_5_file($this->plugin));
acf()->fields->register_field_type(new acf_qtranslate_acf_5_url($this->plugin));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/acf_5/fields/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function render_field($field) {

// special atts
foreach( $s as $k ) {
if( $field[ $k ] ) {
if( isset($field[ $k ]) && $field[ $k ] ) {
$atts[ $k ] = $k;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/acf_5/fields/textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function render_field($field) {

// special atts
foreach( $s as $k ) {
if( $field[ $k ] ) {
if( isset($field[ $k ]) && $field[ $k ] ) {
$atts[ $k ] = $k;
}
}
Expand Down
124 changes: 124 additions & 0 deletions src/acf_5/fields/url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

class acf_qtranslate_acf_5_url extends acf_qtranslate_acf_5_text {
function initialize() {

// vars
$this->name = 'qtranslate_url';
$this->label = __("Url",'acf');
$this->category = __("qTranslate",'acf');
$this->defaults = array(
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => ''
);

}

/*
* render_field()
*
* Create the HTML interface for your field
*
* @param $field - an array holding all the field's data
*
* @type action
* @since 3.6
* @date 23/01/13
*/
function render_field($field) {
global $q_config;
$languages = qtrans_getSortedLanguages(true);
$values = qtrans_split($field['value'], $quicktags = true);
$currentLanguage = $this->plugin->get_active_language();

// vars
$o = array( 'type', 'id', 'class', 'name', 'value', 'placeholder' );
$s = array( 'readonly', 'disabled' );
$e = '';

// maxlength
if( $field['maxlength'] !== "" ) {
$o[] = 'maxlength';
}

// populate atts
$atts = array();
foreach( $o as $k ) {
$atts[ $k ] = $field[ $k ];
}

// special atts
foreach( $s as $k ) {
if( isset($field[ $k ]) && $field[ $k ] ) {
$atts[ $k ] = $k;
}
}

// render
$e .= '<div class="acf-url-wrap multi-language-field">';

foreach ($languages as $language) {
$class = ($language === $currentLanguage) ? 'wp-switch-editor current-language' : 'wp-switch-editor';
$e .= '<a class="' . $class . '" data-language="' . $language . '">' . $q_config['language_name'][$language] . '</a>';
}

foreach ($languages as $language) {
$atts['class'] = $field['class'];

$atts['type'] = 'text';
$atts['name'] = $field['name'] . "[$language]";
$atts['value'] = $values[$language];

$container_class = 'acf-input-wrap acf-url';
if ($language === $currentLanguage) {
$container_class .= ' current-language';
}

$e .= '<div class="' . $container_class . '" data-language="' . $language . '">';
$e .= '<i class="acf-icon -globe -small"></i>';
$e .= '<input ' . acf_esc_attr( $atts ) . ' />';
$e .= '</div>';

}

$e .= '</div>';

// return
echo $e;
}

function validate_value( $valid, $value, $field, $input ){
foreach ($value as $valor) {
// bail early if empty
if ( empty( $valor ) ) {

continue;

}

if ( strpos( $valor, '://' ) !== false ) {

// url

} elseif ( strpos( $valor, '//' ) === 0 ) {

// protocol relative url

} elseif ( strpos( $valor, '/' ) === 0 ) {

// relative url

} else {

$valid = __( 'Value must be a valid URL', 'acf' );

}
}

// return
return $valid;
}
}
1 change: 0 additions & 1 deletion src/acf_5/fields/wysiwyg.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ function __construct($plugin) {
}

// actions
add_action('acf/input/admin_footer', array($this, 'input_admin_footer'));

acf_field::__construct();
}
Expand Down

0 comments on commit e0c11c5

Please sign in to comment.