Skip to content

Commit

Permalink
filter inline editor value
Browse files Browse the repository at this point in the history
  • Loading branch information
jerome-barbato committed Jan 30, 2023
1 parent a71eec1 commit 311a3c5
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 104 deletions.
4 changes: 2 additions & 2 deletions acf-extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
Plugin Name: Advanced Custom Fields Extensions
Description: Advanced Custom Fields add on. Create components, component field, hidden field and latest post field
Version: 1.3.2
Version: 1.3.3
Author: Metabolism
Author URI: https://github.com/wearemetabolism
License: GPLv2 or later
Expand All @@ -11,7 +11,7 @@

defined('ABSPATH') or die('No script kiddies please!');

define('ACF_EXTENSIONS_VERSION', '1.3.2');
define('ACF_EXTENSIONS_VERSION', '1.3.3');

/**
* Load up the translation files
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
---------

##1.3.3
#### Bugfix
- Clean html from inline editor

##1.3.2
#### Bugfix
- replaced use of UPLOAD constant
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name":"metabolism/acf-extensions",
"version": "1.3.2",
"version": "1.3.3",
"homepage": "https://github.com/wearemetabolism/wp-steroids",
"type": "wordpress-plugin",
"description": "ACF Extensions plugin with components, hidden field, dynamic select, inline micro editor, Instagram post, children and latest posts field",
Expand Down
108 changes: 75 additions & 33 deletions fields/class-acf-field-inline-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

if( ! class_exists('acf_field_inline_editor') ) :

class acf_field_inline_editor extends acf_field_text {
class acf_field_inline_editor extends acf_field_text {


/*
/*
* initialize
*
* This function will setup the field type data
Expand All @@ -18,15 +18,57 @@ class acf_field_inline_editor extends acf_field_text {
* @return n/a
*/

function initialize() {
function initialize() {

// vars
$this->name = 'inline_editor';
$this->label = __("Inline editor",'acf');
$this->defaults = array(
'default_value' => ''
);
}
// vars
$this->name = 'inline_editor';
$this->label = __("Inline editor",'acf');
$this->defaults = array(
'default_value' => ''
);
}

function strip_word_html($text, $allowed_tags = '<a><ul><li><b><i><sup><sub><em><strong><u><br><br/><br /><p><h2><h3><h4><h5><h6>')
{
if( !extension_loaded('mbstring') )
return strip_tags($text, $allowed_tags);

mb_regex_encoding('UTF-8');

$search = array('/&lsquo;/u', '/&rsquo;/u', '/&ldquo;/u', '/&rdquo;/u', '/&mdash;/u');
$replace = array('\'', '\'', '"', '"', '-');
$text = preg_replace($search, $replace, $text);

if(mb_stripos($text, '/*') !== FALSE)
$text = mb_eregi_replace('#/\*.*?\*/#s', '', $text, 'm');

$text = preg_replace(array('/<([0-9]+)/'), array('< $1'), $text);
$text = strip_tags($text, $allowed_tags);

$text = preg_replace(array('/^\s\s+/', '/\s\s+$/', '/\s\s+/u'), array('', '', ' '), $text);

$search = array('#<(strong|b)[^>]*>(.*?)</(strong|b)>#isu', '#<(em|i)[^>]*>(.*?)</(em|i)>#isu', '#<u[^>]*>(.*?)</u>#isu');
$replace = array('<b>$2</b>', '<i>$2</i>', '<u>$1</u>');
$text = preg_replace($search, $replace, $text);

$num_matches = preg_match_all("/\<!--/u", $text, $matches);

if($num_matches)
$text = preg_replace('/\<!--(.)*--\>/isu', '', $text);

return preg_replace('/(<[^>]+) style=".*?"/i', '$1', $text);
}

/**
* @param $value
* @param $post_id
* @param $field
* @return string
*/
function update_value($value, $post_id=0, $field=array()){

return $this->strip_word_html($value, '<b><i><strong><sup><sub><a><u><strike>');
}

public function input_admin_enqueue_scripts()
{
Expand Down Expand Up @@ -76,7 +118,7 @@ function render_field( $field ) {
}


/*
/*
* render_field_settings()
*
* Create extra options for your field. This is rendered when editing a field.
Expand All @@ -89,13 +131,13 @@ function render_field( $field ) {
* @param $field - an array holding all the field's data
*/

function render_field_settings( $field ) {
function render_field_settings( $field ) {

// default_value
acf_render_field_setting( $field, array(
'label' => __('Toolbar','acf'),
'instructions' => __('Specify what buttons you want to include on the inline toolbar','acf'),
'type' => 'select',
// default_value
acf_render_field_setting( $field, array(
'label' => __('Toolbar','acf'),
'instructions' => __('Specify what buttons you want to include on the inline toolbar','acf'),
'type' => 'select',
'multiple' => 1,
'ui' => 1,
'choices' => [
Expand All @@ -105,35 +147,35 @@ function render_field_settings( $field ) {
'subscript'=>'subscript',
'superscript'=>'superscript',
'strikeThrough'=>'strikeThrough',
'align'=>'align',
/*'align'=>'align',
'unorderedList'=>'unorderedList',
'nonBreakingSpace'=>'nonBreakingSpace',
'orderedList'=>'orderedList',
'color'=>'color',
'color'=>'color',*/
'nonBreakingSpace'=>'nonBreakingSpace',
'link'=>'link'
],
'name' => 'toolbar',
));
'name' => 'toolbar',
));

// default_value
acf_render_field_setting( $field, array(
// default_value
/*acf_render_field_setting( $field, array(
'label' => __('Colors','acf'),
'instructions' => __('Specify what colors you want to include on the color picker','acf'),
'type' => 'text',
'name' => 'colors'
));
));*/

// default_value
acf_render_field_setting( $field, array(
'label' => __('Rows','acf'),
'type' => 'number',
// default_value
acf_render_field_setting( $field, array(
'label' => __('Rows','acf'),
'type' => 'number',
'name' => 'rows',
'placeholder' => 1
));
));

}
}
}
}

acf_register_field_type( 'acf_field_inline_editor' );
acf_register_field_type( 'acf_field_inline_editor' );

endif; // class_exists check
129 changes: 61 additions & 68 deletions fields/class-acf-field-instagram_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,50 @@

if( ! class_exists('acf_field_instagram_post') ) :

class acf_field_instagram_post extends acf_field_url {
class acf_field_instagram_post extends acf_field_url {

/**
* When saving get title, url and image from Instagram
*
* @param $value
* @param int $post_id
* @param array $field
* @return mixed
*/
public function updateValue($value, $post_id=0, $field=array()){
/**
* When saving get title, url and image from Instagram
*
* @param $value
* @param int $post_id
* @param array $field
* @return mixed
*/
public function update_value($value, $post_id=0, $field=array()){

if( $field['type'] == 'instagram_post' ){
$current_value = acf_get_metadata( $post_id, $field['name'] );

$current_value = acf_get_metadata( $post_id, $field['name'] );
if( ($current_value['url']??'') == $value && !empty($current_value['thumbnail']??'') )
return $current_value;

if( ($current_value['url']??'') == $value )
return $current_value;
if( !empty($value) ){

if( !empty($value) ){
preg_match_all('/\/p\/(.+)\/.*/m', $value, $matches, PREG_SET_ORDER, 0);

preg_match_all('/\/p\/(.+)\/.*/m', $value, $matches, PREG_SET_ORDER, 0);
if( !count($matches) || count($matches[0]) != 2)
return false;

if( !count($matches) || count($matches[0]) != 2)
return false;
$wp_upload_dir = wp_upload_dir();

$wp_upload_dir = wp_upload_dir();
$filepath = $wp_upload_dir['basedir'].'/instagram/'.$matches[0][1].'.jpg';
@file_put_contents($filepath, @file_get_contents('https://www.instagram.com/p/'.$matches[0][1].'/media?size='.($field['size']??'m')));

$filepath = $wp_upload_dir['basedir'].'/instagram/'.$matches[0][1].'.jpg';
@file_put_contents($filepath, @file_get_contents('https://www.instagram.com/p/'.$matches[0][1].'/media?size='.($field['size']??'m')));
if( file_exists($filepath) )
$file_url = str_replace($wp_upload_dir['basedir'], $wp_upload_dir['baseurl'], $filepath);
else
$file_url = false;

if( file_exists($filepath) ){

$file_url = str_replace($wp_upload_dir['basedir'], $wp_upload_dir['baseurl'], $filepath);

$body['thumbnail_url'] = $file_url;
}

$value = [
'url'=>$value,
'title'=>'Instagram post',
'thumbnail'=>$body['thumbnail_url'],
'author_name'=>''
];
}
$value = [
'url'=>$value,
'title'=>'Instagram post',
'thumbnail'=>$file_url,
'author_name'=>''
];
}

return $value;
}
return $value;
}

function render_field_settings( $field ) {

Expand All @@ -66,7 +61,7 @@ function render_field_settings( $field ) {
));
}

/*
/*
* initialize
*
* This function will setup the field type data
Expand All @@ -79,47 +74,45 @@ function render_field_settings( $field ) {
* @return n/a
*/

function initialize() {
function initialize() {

// vars
$this->name = 'instagram_post';
$this->label = __("Instagram post",'acf');
$this->defaults = array(
'default_value' => '',
'size' => 'm',
'placeholder' => 'https://www.instagram.com/p/xxxyyyzzz/'
);
}
// vars
$this->name = 'instagram_post';
$this->label = __("Instagram post",'acf');
$this->defaults = array(
'default_value' => '',
'size' => 'm',
'placeholder' => 'https://www.instagram.com/p/xxxyyyzzz/'
);
}

function render_field( $field ) {
function render_field( $field ) {

$thumbnail = $field['value']['thumbnail']??false;
$field['value'] = $field['value']['url']??'';
$field['type'] = 'url';
$thumbnail = $field['value']['thumbnail']??false;
$field['value'] = $field['value']['url']??'';
$field['type'] = 'url';

echo '<div class="acf-instagram_post">';
echo '<div class="acf-instagram_post">';

parent::render_field($field);
parent::render_field($field);

if( $thumbnail )
echo '<img src="'.$thumbnail.'">';
if( $thumbnail )
echo '<img src="'.$thumbnail.'">';

echo '</div>';
}
echo '</div>';
}

public function __construct()
{
parent::__construct();
public function __construct()
{
parent::__construct();

$wp_upload_dir = wp_upload_dir();

if( !is_dir($wp_upload_dir['basedir'] .'/instagram') )
@mkdir($wp_upload_dir['basedir'].'/instagram', 0777, true);

add_filter('acf/update_value', [$this, 'updateValue'], 10, 3);
}
}
if( !is_dir($wp_upload_dir['basedir'] .'/instagram') )
@mkdir($wp_upload_dir['basedir'].'/instagram', 0777, true);
}
}

acf_register_field_type( 'acf_field_instagram_post' );
acf_register_field_type( 'acf_field_instagram_post' );

endif; // class_exists check

0 comments on commit 311a3c5

Please sign in to comment.