Skip to content

Commit a27ae00

Browse files
feat: experimental Feedzy Tag Block for Feedzy Loop
1 parent 4667c25 commit a27ae00

File tree

10 files changed

+552
-16
lines changed

10 files changed

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

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)