Skip to content

Commit

Permalink
release: minor
Browse files Browse the repository at this point in the history
#### Features
* Support default Gutenberg editor instead of importing content in the Classic block 
* Adds support for Feedzy widget in the widget block editor 
* Adds default thumbnail image support when no image is available

#### Fixes
* Title Character Limit and the Description Character Limit parameter in the Feedzy Block
* Displaying Default Thumbnail Image does not show in Block or Shortcode approach
* Custom tag is trimmed on save of the import if used inside <iframe> 
* Keyword filters break the import with PHP 8.0
  • Loading branch information
selul authored Aug 27, 2021
2 parents eccd991 + 131c9e5 commit 28b3abb
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 16 deletions.
22 changes: 21 additions & 1 deletion css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,24 @@

.fz-feature-centered {
text-align: center;
}
}

.button.feedzy-open-media {
font-weight: 700;
}

.button.feedzy-remove-media {
visibility: hidden;
}

.button.feedzy-remove-media,
.button.feedzy-remove-media:hover {
color: #f1f1f1;
border-color: #F00;
background: #F00;
font-weight: 700;
}

.button.is-show {
visibility: visible;
}
30 changes: 23 additions & 7 deletions includes/abstract/feedzy-rss-feeds-admin-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ abstract class Feedzy_Rss_Feeds_Admin_Abstract {
* @since 3.0.0
* @access public
*
* @param string $image_src The image source, currently not used.
* @param string $default_img The image source, currently not used.
*
* @return string
*/
public function feedzy_define_default_image( $image_src ) {
$default_img = FEEDZY_ABSURL . '/img/feedzy.svg';
public function feedzy_define_default_image( $default_img ) {
if ( empty( $default_img ) ) {
$default_img = FEEDZY_ABSURL . 'img/feedzy.svg';
}

return apply_filters( 'feedzy_define_default_image_filter', $default_img );
}
Expand Down Expand Up @@ -708,6 +710,7 @@ private function init_feed( $feed_url, $cache, $sc, $allow_https = FEEDZY_ALLOW_
'days' => DAY_IN_SECONDS,
);
$cache_time = 12 * HOUR_IN_SECONDS;
$cache = trim( $cache );
if ( isset( $cache ) && '' !== $cache ) {
list( $value, $unit ) = explode( '_', $cache );
if ( isset( $value ) && is_numeric( $value ) && $value >= 1 && $value <= 100 ) {
Expand Down Expand Up @@ -944,18 +947,30 @@ public function sanitize_attr( $sc, $feed_url ) {
$sc['size'] = '150';
}
if ( ! empty( $sc['keywords_title'] ) ) {
if ( is_array( $sc['keywords_title'] ) ) {
$sc['keywords_title'] = implode( ',', $sc['keywords_title'] );
}
$sc['keywords_title'] = rtrim( $sc['keywords_title'], ',' );
$sc['keywords_title'] = array_map( 'trim', explode( ',', $sc['keywords_title'] ) );
}
if ( ! empty( $sc['keywords_inc'] ) ) {
if ( is_array( $sc['keywords_inc'] ) ) {
$sc['keywords_inc'] = implode( ',', $sc['keywords_inc'] );
}
$sc['keywords_inc'] = rtrim( $sc['keywords_inc'], ',' );
$sc['keywords_inc'] = array_map( 'trim', explode( ',', $sc['keywords_inc'] ) );
}
if ( ! empty( $sc['keywords_ban'] ) ) {
if ( is_array( $sc['keywords_ban'] ) ) {
$sc['keywords_ban'] = implode( ',', $sc['keywords_ban'] );
}
$sc['keywords_ban'] = rtrim( $sc['keywords_ban'], ',' );
$sc['keywords_ban'] = array_map( 'trim', explode( ',', $sc['keywords_ban'] ) );
}
if ( ! empty( $sc['keywords_exc'] ) ) {
if ( is_array( $sc['keywords_exc'] ) ) {
$sc['keywords_exc'] = implode( ',', $sc['keywords_exc'] );
}
$sc['keywords_exc'] = rtrim( $sc['keywords_exc'], ',' );
$sc['keywords_exc'] = array_map( 'trim', explode( ',', $sc['keywords_exc'] ) );
}
Expand Down Expand Up @@ -1266,8 +1281,6 @@ private function get_feed_item_filter( $sc, $sizes, $item, $feed_url, $index, $i
$length = intval( $sc['title'] );
if ( $length > 0 && strlen( $content_title ) > $length ) {
$content_title = preg_replace( '/\s+?(\S+)?$/', '', substr( $content_title, 0, $length ) ) . '...';
} elseif ( 0 === $length ) {
$content_title = '';
}
}
if ( empty( $content_title ) ) {
Expand Down Expand Up @@ -1519,8 +1532,11 @@ public function feedzy_retrieve_image( $item, $sc = null ) {
}

$the_thumbnail = html_entity_decode( $the_thumbnail, ENT_QUOTES, 'UTF-8' );
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
$feed_url = $this->normalize_urls( $sc['feeds'] );
$the_thumbnail = ! empty( $the_thumbnail ) ? $the_thumbnail : apply_filters( 'feedzy_default_image', $sc['default'], $feed_url );
}
$the_thumbnail = apply_filters( 'feedzy_retrieve_image', $the_thumbnail, $item );

return $the_thumbnail;
}

Expand Down Expand Up @@ -1573,7 +1589,7 @@ public function feedzy_return_image( $string ) {
* @return string
*/
public function feedzy_scrape_image( $string, $link = '' ) {
$pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/';
$pattern = '/src=[\'"](.*?:\/\/.*\.(?:jpg|JPG|jpeg|JPEG|jpe|JPE|gif|GIF|png|PNG)+)[\'" >]/';
$match = $link;
preg_match( $pattern, $string, $link );
if ( ! empty( $link ) && isset( $link[1] ) ) {
Expand Down
18 changes: 18 additions & 0 deletions includes/admin/feedzy-rss-feeds-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ public function enqueue_styles_admin() {
);
}

if ( 'feedzy_page_feedzy-settings' === $screen->base ) {
if ( ! did_action( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
wp_enqueue_script( $this->plugin_name . '_setting', FEEDZY_ABSURL . 'js/feedzy-setting.js', array( 'jquery' ), $this->version, true );
wp_localize_script(
$this->plugin_name . '_setting',
'feedzy_setting',
array(
'l10n' => array(
'media_iframe_title' => __( 'Select image', 'feedzy-rss-feeds' ),
'media_iframe_button' => __( 'Set default image', 'feedzy-rss-feeds' ),
),
)
);
}

if ( in_array( $screen->base, array( 'post' ), true ) ) {
wp_enqueue_style( $this->plugin_name . '-admin', FEEDZY_ABSURL . 'css/admin.css', array(), $this->version, 'all' );
}
Expand Down Expand Up @@ -499,6 +516,7 @@ private function save_settings() {
switch ( $post_tab ) {
case 'general':
$settings['general']['rss-feeds'] = isset( $_POST['rss-feeds'] ) ? (int) filter_input( INPUT_POST, 'rss-feeds', FILTER_SANITIZE_NUMBER_INT ) : '';
$settings['general']['default-thumbnail-id'] = isset( $_POST['default-thumbnail-id'] ) ? (int) filter_input( INPUT_POST, 'default-thumbnail-id', FILTER_SANITIZE_NUMBER_INT ) : 0;
break;
case 'headers':
$settings['header']['user-agent'] = isset( $_POST['user-agent'] ) ? filter_input( INPUT_POST, 'user-agent', FILTER_SANITIZE_STRING ) : '';
Expand Down
25 changes: 22 additions & 3 deletions includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,12 @@ public function save_feedzy_import_feed_meta( $post_id, $post ) {
$data_meta[ sanitize_text_field( $key ) ][ sanitize_text_field( $sub_key ) ] = wp_kses( $sub_val, apply_filters( 'feedzy_wp_kses_allowed_html', array() ) );
}
} else {
$data_meta[ sanitize_text_field( $key ) ] = wp_kses( $val, apply_filters( 'feedzy_wp_kses_allowed_html', array() ) );
if ( 'import_post_content' === $key ) {
$val = feedzy_custom_tag_escape( $val );
} else {
$val = wp_kses( $val, apply_filters( 'feedzy_wp_kses_allowed_html', array() ) );
}
$data_meta[ sanitize_text_field( $key ) ] = $val;
}
}
}
Expand Down Expand Up @@ -437,11 +442,14 @@ public function save_feedzy_import_feed_meta( $post_id, $post ) {
}
if ( 'import_post_content' === $key ) {
add_filter( 'wp_kses_allowed_html', array( $this, 'allow_iframe_tag_item_content' ), 10, 2 );
$value = feedzy_custom_tag_escape( $value );
} else {
$value = wp_kses( $value, wp_kses_allowed_html( 'post' ) );
}
if ( get_post_meta( $post_id, $key, false ) ) {
update_post_meta( $post_id, $key, wp_kses( $value, wp_kses_allowed_html( 'post' ) ) );
update_post_meta( $post_id, $key, $value );
} else {
add_post_meta( $post_id, $key, wp_kses( $value, wp_kses_allowed_html( 'post' ) ) );
add_post_meta( $post_id, $key, $value );
}
if ( ! $value ) {
delete_post_meta( $post_id, $key );
Expand Down Expand Up @@ -1360,6 +1368,10 @@ private function run_job( $job, $max ) {

if ( ! defined( 'FEEDZY_ALLOW_UNSAFE_HTML' ) || ! FEEDZY_ALLOW_UNSAFE_HTML ) {
$post_content = wp_kses( $post_content, apply_filters( 'feedzy_wp_kses_allowed_html', array() ) );
if ( use_block_editor_for_post_type( $import_post_type ) ) {
$post_content = ! empty( $post_content ) ? '<!-- wp:html -->' . trim( force_balance_tags( wpautop( $post_content, 'br' ) ) ) . '<!-- /wp:html -->' : $post_content;
$post_content = trim( $post_content );
}
}
$new_post = apply_filters(
'feedzy_insert_post_args',
Expand Down Expand Up @@ -1503,6 +1515,12 @@ private function run_job( $job, $max ) {
}
}

// Set default thumbnail image.
if ( ! $img_success && isset( $this->free_settings['general']['default-thumbnail-id'] ) ) {
$default_thumbnail = $this->free_settings['general']['default-thumbnail-id'];
$img_success = set_post_thumbnail( $new_post_id, $default_thumbnail );
}

if ( ! $img_success ) {
$import_image_errors++;
}
Expand Down Expand Up @@ -2095,6 +2113,7 @@ public function allow_iframe_tag_item_content( $tags, $context ) {
'width' => true,
'frameborder' => true,
'allowfullscreen' => true,
'data-*' => true,
);
}
return $tags;
Expand Down
3 changes: 3 additions & 0 deletions includes/admin/feedzy-wp-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public static function get_instance() {
* @access public
*/
public function registerWidget() {
if ( function_exists( 'wp_use_widgets_block_editor' ) && wp_use_widgets_block_editor() ) {
return;
}
register_widget( 'feedzy_wp_widget' );
}

Expand Down
34 changes: 32 additions & 2 deletions includes/feedzy-rss-feeds-feed-tweaks.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ function display_external_post_image( $html, $post_id, $post_thumbnail_id, $size
$attr['style'] = isset( $attr['style'] ) ? $attr['style'] : '';

// Get image dimensions.
if (is_array($size)) {
if ( is_array( $size ) ) {
$dimensions = wp_sprintf( 'width:%dpx; height:%dpx;', $size[0], $size[1] );
$attr['style'] .= $dimensions;
$attr['style'] .= $dimensions;
} elseif ( function_exists( 'wp_get_registered_image_subsizes' ) ) {
$_wp_additional_image_sizes = wp_get_registered_image_subsizes();
if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
Expand Down Expand Up @@ -162,6 +162,35 @@ function feedzy_is_pro_older_than( $version ) {
return version_compare( FEEDZY_PRO_VERSION, $version, '<' );
}

/**
* Feedzy escape custom tag in html attributes.
*
* @param string $content Content.
* @return string
*/
function feedzy_custom_tag_escape( $content = '' ) {
if ( $content ) {
// Match feedzy custom tags in the src attribute.
preg_match_all( '/(\w+)="([^"]*)"/i', $content, $matches, PREG_SET_ORDER );

// If preg match found custom tags in src attribute.
if ( ! empty( $matches ) ) {
foreach ( $matches as $key => $match ) {
if ( isset( $match[2] ) && false !== stripos( $match[2], '[#item_custom_media:' ) ) {
$replace_with = $match[1];
$replace_to = wp_sprintf( 'data-feedzy_%d_%s', $key, $replace_with );
$content = str_replace( $replace_with, $replace_to, $content );
}
}
$content = wp_kses( $content, wp_kses_allowed_html( 'post' ) );
$content = preg_replace( '/data-feedzy_([0-9_]+)/', '', $content );
} else {
$content = wp_kses( $content, wp_kses_allowed_html( 'post' ) );
}
}
return $content;
}

add_filter(
'feedzy_wp_kses_allowed_html',
function( $allowed_html = array() ) {
Expand Down Expand Up @@ -249,6 +278,7 @@ function( $allowed_html = array() ) {
'width' => array(),
'frameborder' => array(),
'allowfullscreen' => array(),
'data-*' => true,
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion includes/gutenberg/build/block.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions includes/gutenberg/feedzy-rss-feeds-gutenberg-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ public function feedzy_rest_route( $data ) {
'time' => ( ( $item->get_date() ) ? date_i18n( $meta_args['time_format'], $item->get_date( 'U' ) ) : null ),
'description' => isset( $item_attrs['item_description'] ) ? $item_attrs['item_description'] : ( $item->get_description() ? $item->get_description() : null ),
'thumbnail' => $admin->feedzy_retrieve_image( $item ),
'default_img' => apply_filters( 'feedzy_default_image', '', $url ),
'price' => isset( $item_attrs['item_price'] ) ? $item_attrs['item_price'] : null,
'media' => isset( $item_attrs['item_media'] ) ? $item_attrs['item_media'] : null,
'categories' => isset( $item_attrs['item_categories'] ) ? $item_attrs['item_categories'] : null,
Expand Down
3 changes: 3 additions & 0 deletions includes/gutenberg/src/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ class Editor extends Component {
}
}

if ( item['thumbnail'] === '' && this.props.attributes.thumb === 'auto' ) {
item['thumbnail'] = item['default_img'];
}
let meta_values = new Object();
meta_values['author'] = __( 'by' ) + ' ' + author;
meta_values['date'] = __( 'on' ) + ' ' + unescapeHTML( itemDate );
Expand Down
2 changes: 2 additions & 0 deletions includes/gutenberg/src/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class Inspector extends Component {
value={ this.props.attributes.title }
onChange={ this.props.edit.onTitle }
className="feedzy-title-length"
min={0}
/>

<BaseControl>
Expand Down Expand Up @@ -238,6 +239,7 @@ class Inspector extends Component {
value={ this.props.attributes.summarylength }
onChange={ this.props.edit.onSummaryLength }
className="feedzy-summary-length"
min={0}
/>
) }

Expand Down
16 changes: 15 additions & 1 deletion includes/layouts/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class="nav-tab <?php echo $_tab === $active_tab ? esc_attr( 'nav-tab-active' ) :
$disble_featured_image = 'checked';
}

$default_thumbnail_id = isset( $settings['general']['default-thumbnail-id'] ) ? $settings['general']['default-thumbnail-id'] : 0;

switch ( $active_tab ) {
case 'general':
?>
Expand All @@ -58,7 +60,19 @@ class="nav-tab <?php echo $_tab === $active_tab ? esc_attr( 'nav-tab-active' ) :
<input type="checkbox" id="rss-feeds" class="fz-form-control" name="rss-feeds"
value="1" <?php echo esc_html( $disble_featured_image ); ?> />
<label for="rss-feeds"><?php echo esc_html_e( 'Do NOT add the featured image to the website\'s RSS feed.', 'feedzy-rss-feeds' ); ?></label>

</div>
<div class="fz-form-group">
<label><?php esc_html_e( 'Choose default thumbnail image(Feed2Post):', 'feedzy-rss-feeds' ); ?></label>
</div>
<?php if ( $default_thumbnail_id ) : ?>
<div class="fz-form-group feedzy-media-preview">
<?php echo wp_get_attachment_image( $default_thumbnail_id, 'thumbnail' ); ?>
</div>
<?php endif; ?>
<div class="fz-form-group">
<a href="javascript:;" class="feedzy-open-media button action"><?php esc_html_e( 'Choose image', 'feedzy-rss-feeds' ); ?></a>
<a href="javascript:;" class="feedzy-remove-media button action <?php echo $default_thumbnail_id ? esc_attr( 'is-show' ) : ''; ?>"><?php esc_html_e( 'Remove image', 'feedzy-rss-feeds' ); ?></a>
<input type="hidden" name="default-thumbnail-id" id="feed-post-default-thumbnail" value="<?php echo esc_attr( $default_thumbnail_id ); ?>">
</div>
<?php
break;
Expand Down
2 changes: 1 addition & 1 deletion includes/views/import-metabox-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@
</div>
<div class="feedzy-separator dashicons dashicons-leftright"></div>
<div class="form-group input-group form_item">
<textarea name="feedzy_meta_data[import_post_content]" placeholder="<?php esc_html_e( 'Post Content', 'feedzy-rss-feeds' ); ?>" class="form-control"><?php echo wp_kses_post( $import_content ); ?></textarea>
<textarea name="feedzy_meta_data[import_post_content]" placeholder="<?php esc_html_e( 'Post Content', 'feedzy-rss-feeds' ); ?>" class="form-control"><?php echo esc_html( feedzy_custom_tag_escape( $import_content ) ); ?></textarea>
<div class="input-group-btn">
<button type="button" class="btn btn-add-fields dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<?php esc_html_e( 'Insert Tag', 'feedzy-rss-feeds' ); ?> <span class="dashicons dashicons-arrow-down-alt2"></span>
Expand Down
48 changes: 48 additions & 0 deletions js/feedzy-setting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Plugin Name: FEEDZY RSS Feeds
* Plugin URI: http://themeisle.com/plugins/feedzy-rss-feeds/
* Author: Themeisle
*
* @package feedzy-rss-feeds
*/
/* global feedzy_setting */
/* jshint unused:false */
jQuery( function( $ ) {
// on upload button click
$( 'body' ).on( 'click', '.feedzy-open-media', function( e ) {
e.preventDefault();
var button = $( this ),
wp_media_uploader = wp.media( {
title: feedzy_setting.l10n.media_iframe_title,
library : {
type : 'image'
},
button: {
text: feedzy_setting.l10n.media_iframe_button
},
multiple: false
} ).on( 'select', function() { // it also has "open" and "close" events
var attachment = wp_media_uploader.state().get( 'selection' ).first().toJSON();
var attachmentUrl = attachment.url;
if ( attachment.sizes.thumbnail ) {
attachmentUrl = attachment.sizes.thumbnail.url;
}
if ( $( '.feedzy-media-preview' ).length ) {
$( '.feedzy-media-preview' ).find( 'img' ).attr( 'src', attachmentUrl );
} else {
$( '<div class="fz-form-group feedzy-media-preview"><img src="' + attachmentUrl + '"></div>' ).insertBefore( button.parent() );
}
button.parent().find( '.feedzy-remove-media' ).addClass( 'is-show' );
button.parent().find( 'input:hidden' ).val( attachment.id );
} ).open();
});

// on remove button click
$( 'body' ).on( 'click', '.feedzy-remove-media', function( e ) {
e.preventDefault();
var button = $( this );
button.parent().prev( '.feedzy-media-preview' ).remove();
button.removeClass( 'is-show' );
button.parent().find( 'input:hidden' ).val( '' );
});
});

0 comments on commit 28b3abb

Please sign in to comment.