Skip to content

Commit

Permalink
work on php 7.x compat + gutenberg support
Browse files Browse the repository at this point in the history
  • Loading branch information
herewithme committed Apr 26, 2019
1 parent a900ce8 commit f251825
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 201 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
.idea
.phan
*.phar
.idea
82 changes: 62 additions & 20 deletions assets/js/helper-add-tags.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,70 @@
function addTag(tag) {
// Trim tag
tag = tag.replace(/^\s+/, '').replace(/\s+$/, '');
function addTag (tag) {
// Trim tag
tag = tag.replace(/^\s+/, '').replace(/\s+$/, '')

if (document.getElementById("adv-tags-input")) { // Tags input from Simple Tags
if (document.getElementById('adv-tags-input')) { // Tags input from Simple Tags

var tag_entry = document.getElementById("adv-tags-input");
if (tag_entry.value.length > 0 && !tag_entry.value.match(/,\s*$/)) {
tag_entry.value += ", ";
}
var tag_entry = document.getElementById('adv-tags-input')
if (tag_entry.value.length > 0 && !tag_entry.value.match(/,\s*$/)) {
tag_entry.value += ', '
}

var re = new RegExp(tag + ',')
if (!tag_entry.value.match(re)) {
tag_entry.value += tag + ', '
}

var re = new RegExp(tag + ",");
if (!tag_entry.value.match(re)) {
tag_entry.value += tag + ", ";
}
} else if (document.getElementById('new-tag-post_tag')) { // Default tags input from WordPress

} else { // Default tags input from WordPress
tag.replace(/\s+,+\s*/g, ',').replace(/,+/g, ',').replace(/,+\s+,+/g, ',').replace(/,+\s*$/g, '').replace(/^\s*,+/g, '')
if (jQuery('#new-tag-post_tag').val() === '') {
jQuery('#new-tag-post_tag').val(tag)
} else {
jQuery('#new-tag-post_tag').val(jQuery('#new-tag-post_tag').val() + ', ' + tag)
}
//jQuery('.tagadd').WithSelect()

tag.replace(/\s+,+\s*/g, ',').replace(/,+/g, ',').replace(/,+\s+,+/g, ',').replace(/,+\s*$/g, '').replace(/^\s*,+/g, '');
if (jQuery('#new-tag-post_tag').val() === "") {
jQuery('#new-tag-post_tag').val(tag);
} else {
jQuery('#new-tag-post_tag').val(jQuery('#new-tag-post_tag').val() + ", " + tag);
}
jQuery('.tagadd').click();
} else if (typeof wp.data != 'undefined') { // Gutenberg

// Show the tags panel
if (wp.data.select('core/edit-post').isEditorPanelOpened('taxonomy-panel-post_tag') === false) {
wp.data.dispatch('core/edit-post').toggleEditorPanelOpened('taxonomy-panel-post_tag')
}

// Get current post_tags
var tags_taxonomy = wp.data.select('core').getTaxonomy('post_tag')
var tag_rest_base = tags_taxonomy && tags_taxonomy.rest_base
var tags = tag_rest_base && wp.data.select('core/editor').getEditedPostAttribute(tag_rest_base)

var newTags = JSON.parse(JSON.stringify(tags));

jQuery.ajax({
url: ajaxurl + '?action=simpletags&stags_action=maybe_create_tag&tag=' + tag,
cache: false,
//async: false,
dataType: 'json'
}).done(function (result) {
if (result.data.term_id > 0) {
newTags.push(result.data.term_id)

var new_tag = {};
new_tag[tag_rest_base] = newTags;

console.log('core/editor');
console.log(new_tag);
console.log(tag_rest_base);
wp.data.dispatch('core/editor').editPost(new_tag);
//wp.data.dispatch('core/editor').editPost({tags: tags});

// See : https://riad.blog/2018/06/07/efficient-client-data-management-for-wordpress-plugins/
}
}).fail(function () {
console.log('error when trying to create tag')
})

} else {

console.log('no tags input found...')

}
}
80 changes: 40 additions & 40 deletions assets/js/helper-autocomplete.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
function st_split(val) {
return val.split(/,\s*/);
function st_split (val) {
return val.split(/,\s*/)
}

function st_extract_last(term) {
return st_split(term).pop();
function st_extract_last (term) {
return st_split(term).pop()
}

function st_init_autocomplete(p_target, p_url, p_min_chars) {
// Dynamic width ?
p_width = jQuery("" + p_target).width();
if (p_width === 0) {
p_width = 200;
}
function st_init_autocomplete (p_target, p_url, p_min_chars) {
// Dynamic width ?
p_width = jQuery('' + p_target).width()
if (p_width === 0) {
p_width = 200
}

// Init jQuery UI autocomplete
jQuery(p_target).bind("keydown", function(event) {
// don't navigate away from the field on tab when selecting an item
if (event.keyCode === jQuery.ui.keyCode.TAB &&
jQuery(this).data("ui-autocomplete").menu.active) {
event.preventDefault();
}
}).autocomplete({
minLength: p_min_chars,
source: function(request, response) {
jQuery.getJSON(p_url, {
term: st_extract_last(request.term)
}, response);
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function(event, ui) {
var terms = st_split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
});
// Init jQuery UI autocomplete
jQuery(p_target).bind('keydown', function (event) {
// don't navigate away from the field on tab when selecting an item
if (event.keyCode === jQuery.ui.keyCode.TAB &&
jQuery(this).data('ui-autocomplete').menu.active) {
event.preventDefault()
}
}).autocomplete({
minLength: p_min_chars,
source: function (request, response) {
jQuery.getJSON(p_url, {
term: st_extract_last(request.term)
}, response)
},
focus: function () {
// prevent value inserted on focus
return false
},
select: function (event, ui) {
var terms = st_split(this.value)
// remove the current input
terms.pop()
// add the selected item
terms.push(ui.item.value)
// add placeholder to get the comma-and-space at the end
terms.push('')
this.value = terms.join(', ')
return false
}
})
}
166 changes: 101 additions & 65 deletions assets/js/helper-suggested-tags.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,113 @@
jQuery(document).ready(function() {
jQuery("#suggestedtags .hndle span").html(html_entity_decode(stHelperSuggestedTagsL10n.title_bloc));
jQuery("#suggestedtags .inside .container_clicktags").html(stHelperSuggestedTagsL10n.content_bloc);

// Generica all for autocomplet API
jQuery("a.suggest-action-link").click(function(event) {
event.preventDefault();

jQuery('#st_ajax_loading').show();

jQuery("#suggestedtags .container_clicktags").load(ajaxurl + '?action=simpletags&stags_action=' + jQuery(this).data("ajaxaction"), {
content: getContentFromEditor(),
title: jQuery("#title").val(),
tags: jQuery("#tags-input").val()
}, function() {
registerClickTags();
});
return false;
});
});

function getContentFromEditor() {
var data = '';

if ((typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden()) { // Tiny MCE
var ed = tinyMCE.activeEditor;
if ('mce_fullscreen' == ed.id) {
tinyMCE.get('content').setContent(ed.getContent({
format: 'raw'
}), {
format: 'raw'
});
}
tinyMCE.get('content').save();
data = jQuery("#content").val();
} else if (typeof FCKeditorAPI != "undefined") { // FCK Editor
var oEditor = FCKeditorAPI.GetInstance('content');
data = oEditor.GetHTML().stripTags();
} else if (typeof WYM_INSTANCES != "undefined") { // Simple WYMeditor
data = WYM_INSTANCES[0].xhtml();
} else { // No editor, just quick tags
data = jQuery("#content").val();
}
jQuery(document).ready(function () {
jQuery('#suggestedtags .hndle span').html(html_entity_decode(stHelperSuggestedTagsL10n.title_bloc))
jQuery('#suggestedtags .inside .container_clicktags').html(stHelperSuggestedTagsL10n.content_bloc)

// Generi call for autocomplete API
jQuery('a.suggest-action-link').click(function (event) {
event.preventDefault()

jQuery('#st_ajax_loading').show()

jQuery('#suggestedtags .container_clicktags').load(ajaxurl + '?action=simpletags&stags_action=' + jQuery(this).data('ajaxaction'), {
content: getContentFromEditor(),
title: getTitleFromEditor()
}, function () {
registerClickTags()
})
return false
})
})

function getTitleFromEditor () {
var data = ''

if (typeof wp.data != 'undefined') {
data = wp.data.select('core/editor').getEditedPostAttribute('title')
} else { // No editor, just quick tags
data = jQuery('#title').val()
}

// Trim data
data = data.replace(/^\s+/, '').replace(/\s+$/, '')
if (data !== '') {
data = strip_tags(data)
}

// Trim data
data = data.replace(/^\s+/, '').replace(/\s+$/, '');
if (data !== '') {
data = strip_tags(data);
return data
}

function getContentFromEditor () {
var data = ''

if ((typeof tinyMCE != 'undefined') && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden() && typeof wp.data == 'undefined') { // Tiny MCE
var ed = tinyMCE.activeEditor
if ('mce_fullscreen' == ed.id) {
tinyMCE.get('content').setContent(ed.getContent({
format: 'raw'
}), {
format: 'raw'
})
}
tinyMCE.get('content').save()
data = jQuery('#content').val()
} else if (typeof FCKeditorAPI != 'undefined') { // FCK Editor
var oEditor = FCKeditorAPI.GetInstance('content')
data = oEditor.GetHTML().stripTags()
} else if (typeof WYM_INSTANCES != 'undefined') { // Simple WYMeditor
data = WYM_INSTANCES[0].xhtml()
} else if (typeof wp.data != 'undefined' && typeof tinyMCE != 'undefined') {
data = wp.data.select('core/editor').getEditedPostAttribute('content')

return data;
} else { // No editor, just quick tags
data = jQuery('#content').val()
}

// Trim data
data = data.replace(/^\s+/, '').replace(/\s+$/, '')
if (data !== '') {
data = strip_tags(data)
}

return data
}

function registerClickTags() {
jQuery("#suggestedtags .container_clicktags span").click(function(event) {
event.preventDefault();
function registerClickTags () {
jQuery('#suggestedtags .container_clicktags span').click(function (event) {
event.preventDefault()

addTag(this.innerHTML);
});
addTag(this.innerHTML)
})

jQuery('#st_ajax_loading').hide();
if (jQuery('#suggestedtags .inside').css('display') != 'block') {
jQuery('#suggestedtags').toggleClass('closed');
}
jQuery('#st_ajax_loading').hide()
if (jQuery('#suggestedtags .inside').css('display') != 'block') {
jQuery('#suggestedtags').toggleClass('closed')
}
}

function html_entity_decode(str) {
var ta = document.createElement("textarea");
ta.innerHTML = str.replace(/</g, "&lt;").replace(/>/g, "&gt;");
toReturn = ta.value;
ta = null;
return toReturn;
/**
* The html_entity_decode() php function on JS :)
*
* See : https://github.com/hirak/phpjs
*
* @param str
* @returns {string | *}
*/
function html_entity_decode (str) {
var ta = document.createElement('textarea')
ta.innerHTML = str.replace(/</g, '&lt;').replace(/>/g, '&gt;')
toReturn = ta.value
ta = null
return toReturn
}

function strip_tags(str) {
return str.replace(/&lt;\/?[^&gt;]+&gt;/gi, "");
/**
* The strip_tags() php function on JS :)
*
* See : https://github.com/hirak/phpjs
*
* @param str
* @returns {*}
*/
function strip_tags (str) {
return str.replace(/&lt;\/?[^&gt;]+&gt;/gi, '')
}
Loading

0 comments on commit f251825

Please sign in to comment.