Skip to content

Commit d9bdd16

Browse files
feat: experimental Feedzy Tag Block for Feedzy Loop
1 parent 621af35 commit d9bdd16

File tree

9 files changed

+548
-7
lines changed

9 files changed

+548
-7
lines changed

includes/feedzy-rss-feeds.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ function () {
285285
function () {
286286
if ( function_exists( 'register_block_type' ) ) {
287287
Feedzy_Rss_Feeds_Gutenberg_Block::get_instance();
288+
Feedzy_Rss_Feeds_Tag_Block::get_instance();
288289
Feedzy_Rss_Feeds_Loop_Block::get_instance();
289290
}
290291
}

includes/gutenberg/feedzy-rss-feeds-loop-block.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private function __construct() {
4949
$this->version = Feedzy_Rss_Feeds::get_version();
5050
$this->admin = Feedzy_Rss_Feeds::instance()->get_admin();
5151
add_action( 'init', array( $this, 'register_block' ) );
52-
add_filter( 'feedzy_loop_item', array( $this, 'apply_magic_tags' ), 10, 2 );
52+
add_filter( 'feedzy_loop_item', array( $this, 'apply_magic_tags' ), 10, 3 );
5353
}
5454

5555
/**
@@ -95,9 +95,10 @@ public function register_block() {
9595
*
9696
* @param array $attributes The block attributes.
9797
* @param string $content The block content.
98+
* @param WP_Block $block Block instance.
9899
* @return string The block content.
99100
*/
100-
public function render_callback( $attributes, $content ) {
101+
public function render_callback( $attributes, $content, $block ) {
101102
$content = empty( $content ) ? ( $attributes['innerBlocksContent'] ?? '' ) : $content;
102103
$is_preview = isset( $attributes['innerBlocksContent'] ) && ! empty( $attributes['innerBlocksContent'] );
103104
$feed_urls = array();
@@ -166,12 +167,20 @@ public function render_callback( $attributes, $content ) {
166167
$loop = '';
167168

168169
foreach ($feed_items as $key => $item) {
169-
$loop .= apply_filters( 'feedzy_loop_item', $content, $item );
170+
// Reference https://github.com/WordPress/gutenberg/blob/b3cda428abe895a0a97c0a6df0e0cf5c925d9208/packages/block-library/src/post-template/index.php#L113-L125
171+
$filter_block_context = static function ( $context ) use ( $item ) {
172+
$context['feedzy-rss-feeds/feedItem'] = $item;
173+
return $context;
174+
};
175+
176+
add_filter( 'render_block_context', $filter_block_context, 1 );
177+
$loop .= apply_filters( 'feedzy_loop_item', $content, $item, $block );
178+
remove_filter( 'render_block_context', $filter_block_context, 1 );
170179
}
171180

172181
return sprintf(
173182
'<div %1$s>%2$s</div>',
174-
$wrapper_attributes = get_block_wrapper_attributes( array(
183+
get_block_wrapper_attributes( array(
175184
'class' => 'feedzy-loop-columns-' . $column_count,
176185
) ),
177186
$loop
@@ -182,11 +191,22 @@ public function render_callback( $attributes, $content ) {
182191
* Magic Tags Replacement.
183192
*
184193
* @param string $content The content.
194+
* @param array<string, mixed> $item The feed item.
195+
* @param WP_Block $block Block instance.
185196
*
186-
* @return string The content.
197+
* @return string The feed content.
187198
*/
188-
public function apply_magic_tags( $content, $item ) {
199+
public function apply_magic_tags( $content, $item, $block ) {
189200
$pattern = '/\{\{feedzy_([^}]+)\}\}/';
201+
202+
$block_content = (new WP_Block( $block->parsed_block ) )->render( array( 'dynamic' => false ) );
203+
204+
if ( empty( $block_content ) ) {
205+
$content = do_blocks( $block->attributes['innerBlocksContent'] );
206+
} else {
207+
$content = $block_content;
208+
}
209+
190210
$content = str_replace(
191211
array(
192212
FEEDZY_ABSURL . 'img/feedzy.svg',
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<?php
2+
/**
3+
* Class for functionalities related to Loop block.
4+
*
5+
* Defines the functions that need to be used for Loop block,
6+
* and REST router.
7+
*
8+
* @package feedzy-rss-feeds
9+
* @subpackage feedzy-rss-feeds/includes/guteneberg
10+
* @author Themeisle <[email protected]>
11+
*
12+
* @since 5.1.0
13+
*/
14+
class Feedzy_Rss_Feeds_Tag_Block {
15+
16+
/**
17+
* A reference to an instance of this class.
18+
*
19+
* @var Feedzy_Rss_Feeds_Tag_Block The one Feedzy_Rss_Feeds_Loop_Block instance.
20+
*/
21+
private static $instance;
22+
23+
/**
24+
* Instance of Feedzy_Rss_Feeds_Admin class.
25+
*
26+
* @var Feedzy_Rss_Feeds_Admin $admin The Feedzy_Rss_Feeds_Admin instance.
27+
*/
28+
private $admin;
29+
30+
/**
31+
* Feedzy RSS Feeds plugin version.
32+
*
33+
* @var string $version The current version of the plugin.
34+
*/
35+
protected $version;
36+
37+
/**
38+
* Returns an instance of this class.
39+
*/
40+
public static function get_instance() {
41+
if ( null === self::$instance ) {
42+
self::$instance = new Feedzy_Rss_Feeds_Tag_Block();
43+
}
44+
return self::$instance;
45+
}
46+
47+
/**
48+
* Initializes the plugin by setting filters and administration functions.
49+
*/
50+
private function __construct() {
51+
$this->version = Feedzy_Rss_Feeds::get_version();
52+
$this->admin = Feedzy_Rss_Feeds::instance()->get_admin();
53+
add_action( 'init', array( $this, 'register_block' ) );
54+
}
55+
56+
/**
57+
* Register Block
58+
*/
59+
public function register_block() {
60+
$metadata_file = trailingslashit( FEEDZY_ABSPATH ) . '/build/tag/block.json';
61+
register_block_type_from_metadata(
62+
$metadata_file,
63+
array(
64+
'render_callback' => array( $this, 'render_callback' ),
65+
)
66+
);
67+
}
68+
69+
/**
70+
* Render Callback
71+
*
72+
* @param array $attributes The block attributes.
73+
* @param string $content The block content.
74+
* @param WP_Block $block The block instance.
75+
* @return string The block content.
76+
*/
77+
public function render_callback( $attributes, $content, $block ) {
78+
if (
79+
! isset( $attributes['tag'] ) || empty( $attributes['tag'] ) ||
80+
! isset( $block->context ) || empty( $block->context ) ||
81+
! isset( $block->context['feedzy-rss-feeds/feedItem'] ) || empty( $block->context['feedzy-rss-feeds/feedItem'] )
82+
) {
83+
return '';
84+
}
85+
86+
$feed_item = $block->context['feedzy-rss-feeds/feedItem'];
87+
88+
if ( ! isset( $feed_item[ $attributes['tag'] ] ) ) {
89+
return '';
90+
}
91+
92+
return wp_kses_post( $feed_item[ $attributes['tag'] ] );
93+
}
94+
95+
/**
96+
* Magic Tags Replacement.
97+
*
98+
* @param string $content The content.
99+
*
100+
* @return string The content.
101+
*/
102+
public function apply_magic_tags( $content, $item ) {
103+
$pattern = '/\{\{feedzy_([^}]+)\}\}/';
104+
$content = str_replace(
105+
array(
106+
FEEDZY_ABSURL . 'img/feedzy.svg',
107+
'http://{{feedzy_url}}'
108+
),
109+
array(
110+
'{{feedzy_image}}',
111+
'{{feedzy_url}}'
112+
),
113+
$content
114+
);
115+
116+
return preg_replace_callback( $pattern, function( $matches ) use ( $item ) {
117+
return isset( $matches[1] ) ? $this->get_value( $matches[1], $item ) : '';
118+
}, $content );
119+
}
120+
121+
/**
122+
* Get Dynamic Value.
123+
*
124+
* @param string $key The key.
125+
* @param array $item Feed item.
126+
*
127+
* @return string The value.
128+
*/
129+
public function get_value( $key, $item ) {
130+
switch ( $key ) {
131+
case 'title':
132+
return isset( $item['item_title'] ) ? $item['item_title'] : '';
133+
case 'url':
134+
return isset( $item['item_url'] ) ? $item['item_url'] : '';
135+
case 'date':
136+
$item_date = isset( $item['item_date'] ) ? wp_date( get_option( 'date_format' ), $item['item_date'] ) : '';
137+
return $item_date;
138+
case 'time':
139+
$item_date = isset( $item['item_date'] ) ? wp_date( get_option( 'time_format' ), $item['item_date'] ) : '';
140+
return $item_date;
141+
case 'datetime':
142+
$item_date = isset( $item['item_date'] ) ? wp_date( get_option( 'date_format' ), $item['item_date'] ) : '';
143+
$item_time = isset( $item['item_date'] ) ? wp_date( get_option( 'time_format' ), $item['item_date'] ) : '';
144+
/* translators: 1: date, 2: time */
145+
$datetime = sprintf( __( '%1$s at %2$s', 'feedzy-rss-feeds' ), $item_date, $item_time );
146+
return $datetime;
147+
case 'author':
148+
if ( isset( $item['item_author'] ) && is_string( $item['item_author'] ) ) {
149+
return $item['item_author'];
150+
} elseif ( isset( $item['item_author'] ) && is_object( $item['item_author'] ) ) {
151+
return $item['item_author']->get_name();
152+
}
153+
return '';
154+
case 'description':
155+
return isset( $item['item_description'] ) ? $item['item_description'] : '';
156+
case 'content':
157+
return isset( $item['item_content'] ) ? $item['item_content'] : '';
158+
case 'meta':
159+
return isset( $item['item_meta'] ) ? $item['item_meta'] : '';
160+
case 'categories':
161+
return isset( $item['item_categories'] ) ? $item['item_categories'] : '';
162+
case 'image':
163+
$settings = apply_filters( 'feedzy_get_settings', array() );
164+
if ( $settings && ! empty( $settings['general']['default-thumbnail-id'] ) ) {
165+
$default_img = wp_get_attachment_image_src( $settings['general']['default-thumbnail-id'], 'full' );
166+
$default_img = ! empty( $default_img ) ? reset( $default_img ) : '';
167+
} else {
168+
$default_img = FEEDZY_ABSURL . 'img/feedzy.svg';
169+
}
170+
171+
return isset( $item['item_img_path'] ) ? $item['item_img_path'] : $default_img;
172+
case 'media':
173+
return isset( $item['item_media']['src'] ) ? $item['item_media']['src'] : '';
174+
case 'price':
175+
return isset( $item['item_price'] ) ? $item['item_price'] : '';
176+
case 'source':
177+
return isset( $item['item_source'] ) ? $item['item_source'] : '';
178+
default:
179+
return '';
180+
}
181+
}
182+
}

js/FeedzyLoop/edit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const Edit = ({ attributes, setAttributes, clientId }) => {
121121
);
122122
}
123123

124-
if ((!isSelected || isPreviewing) && innerBlocksContent) {
124+
if (isPreviewing && innerBlocksContent) {
125125
return (
126126
<>
127127
<Controls

0 commit comments

Comments
 (0)