PluginProbe ʕ •ᴥ•ʔ
پارسی دیت – Parsi Date / 6.2.1
پارسی دیت – Parsi Date v6.2.1
6.2.1 6.2 6.1 5.1.6 5.1.7 5.1.8 5.1.8.2 6.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.0.0-alpha 2.1 2.1.1 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0.1 2.3.0.2 2.3.1 2.3.2 2.3.3 2.3.4 3.0.1 3.0.2 3.0.3 4.0.0 4.0.1 4.0.2 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5
wp-parsidate / inc / Helper / FeedReader.php
wp-parsidate / inc / Helper Last commit date
Assets.php 3 weeks ago Cache.php 3 weeks ago Date.php 3 weeks ago Debug.php 3 weeks ago FeedReader.php 3 weeks ago HTML.php 3 weeks ago Helper.php 3 weeks ago JSON.php 3 weeks ago Nonce.php 3 weeks ago Notice.php 3 weeks ago Number.php 3 weeks ago NumberConverter.php 3 weeks ago Param.php 3 weeks ago Sanitizing.php 3 weeks ago Strip.php 3 weeks ago Templates.php 3 weeks ago User.php 3 weeks ago Validating.php 3 weeks ago WooCommerce.php 3 weeks ago WordPress.php 3 weeks ago
FeedReader.php
294 lines
1 <?php
2
3 namespace WPParsidate\Helper;
4
5 defined( 'ABSPATH' ) || exit;
6
7 class FeedReader {
8 /**
9 * @var array Args
10 */
11 private array $args;
12
13 /**
14 * @var array Default Args
15 */
16 private array $defaultArgs;
17
18 /**
19 * @var array Feed items
20 */
21 private array $feedItems = [];
22
23 /**
24 * @var \WP_Error Error
25 */
26 private \WP_Error $error;
27
28 /**
29 * @var array Replace desc text
30 */
31 private array $replaceDescText = [];
32
33 /**
34 * @param array $args Feed arguments
35 */
36 public function __construct( array $args ) {
37 $this->args = $this->defaultArgs = array(
38 'url' => '',
39 'cache_key' => '',
40 'cache_time' => DAY_IN_SECONDS,
41 'items_number' => 10,
42 'timeout' => 3,
43 'fields' => [ 'link', 'title', 'description', 'author', 'datetime' ],
44 );
45 $this->setArgs( $args );
46 }
47
48 /**
49 * Set feed arguments
50 *
51 * @param array $args Feed Arguments
52 *
53 * @return void
54 */
55 public function setArgs( array $args ): void {
56 $this->args = wp_parse_args( $args, $this->args );
57 $this->args['url'] = Validating::isUrl( $this->args['url'] ) ? $this->args['url'] : '';
58 $this->args['cache_key'] = empty( $this->args['cache_key'] ) ? 'feed_' . Helper::urlToKey( $this->args['url'] ) : $this->args['cache_key'];
59 $this->args['cache_time'] = is_numeric( $this->args['cache_time'] ) ? (int) $this->args['cache_time'] : DAY_IN_SECONDS;
60 $this->args['items_number'] = (int) $this->args['items_number'];
61 $this->args['timeout'] = (int) $this->args['timeout'];
62 $this->args['fields'] = is_array( $this->args['fields'] ) && ! empty( $this->args['fields'] ) ? $this->args['fields'] : $this->defaultArgs['fields'];
63 }
64
65 /**
66 * Get Error
67 *
68 * @return \WP_Error
69 */
70 public function getError(): \WP_Error {
71 return $this->error;
72 }
73
74 /**
75 * Get feed Items
76 *
77 * @return array Feed items
78 */
79 public function getFeedItems(): array {
80 return $this->feedItems;
81 }
82
83 /**
84 * Set replace text value
85 *
86 * @param array $replaceTexts Replace text's
87 *
88 * @return $this
89 */
90 public function replaceDescText( $replaceTexts ): self {
91 $this->replaceDescText = $replaceTexts;
92
93 return $this;
94 }
95
96 /**
97 * Get HTML feed links
98 *
99 * @param array $fields Print fields
100 *
101 * @return array Array of HTML feed links
102 */
103 public function getFeedLinks( $fields = null ): array {
104 $fields = $printFields = is_null( $fields ) ? $this->defaultArgs['fields'] : $fields;
105 if ( empty( $this->feedItems ) ) {
106 return [];
107 }
108
109 if ( empty( $printFields ) ) {
110 return [];
111 }
112
113 $dateFormat = get_option( 'date_format', 'F j, Y' );
114 $timeFormat = get_option( 'time_format', 'g:i a' );
115
116 $links = [];
117 if ( ( $key = array_search( 'link', $printFields, true ) ) !== false ) {
118 unset( $printFields[ $key ] );
119 }
120
121 foreach ( $this->feedItems as $item ) {
122 $title = '';
123 foreach ( $printFields as $field ) {
124 if ( ! empty( $item[ $field ] ) && in_array( $field, $this->defaultArgs['fields'], true ) ) {
125 $value = $item[ $field ];
126 if ( $field === 'datetime' ) {
127 $value = wp_date( $dateFormat . ', ' . $timeFormat, strtotime( $value ) );
128 }
129 $title .= Templates::load( Templates::getPath( 'feed-reader/feed_data_row.php' ), array(
130 'field' => $field,
131 'value' => $value
132 ), false, false );
133 }
134 }
135
136 if ( ! empty( $title ) ) {
137 $links[] = Templates::load( Templates::getPath( 'feed-reader/feed_item.php' ), array(
138 'link' => in_array( 'link', $fields, true ) ? ( $item['link'] ?? '' ) : '',
139 'title' => $title
140 ), false, false );
141 }
142 }
143
144 return $links;
145 }
146
147 /**
148 * Read feed
149 *
150 * @param bool $useCache Use Cache
151 *
152 * @return FeedReader
153 */
154 public function read( bool $useCache = true ): self {
155 if ( empty( $this->args['url'] ) ) {
156 return $this;
157 }
158
159 if ( $useCache ) {
160 $feedItems = Cache::get( $this->args['cache_key'] );
161
162 if ( is_array( $feedItems ) ) {
163 $this->feedItems = $feedItems;
164
165 return $this;
166 }
167 }
168
169 $timeout = $this->args['timeout'];
170 $timeoutFunc = function ( $t ) use ( $timeout ) {
171 return $timeout;
172 };
173 add_filter( 'http_request_timeout', $timeoutFunc );
174
175 $requestUrl = $this->args['url'];
176 add_action( 'wp_feed_cache_transient_lifetime',
177 function ( $cacheTime, $url ) use ( $requestUrl ) {
178 if ( $url === $requestUrl || ! Validating::isUrl( $url ) ) {
179 return 1;
180 }
181
182 return $cacheTime;
183 }
184 , 10, 2 );
185
186 $feed = fetch_feed( $this->args['url'] );
187
188 remove_filter( 'http_request_timeout', $timeoutFunc );
189
190 if ( is_wp_error( $feed ) ) {
191 $this->error = $feed;
192
193 return $this;
194 }
195
196 if ( ! $feed->get_item_quantity() ) {
197 $this->error = new \WP_Error( 'feed_empty', esc_html__( 'Feed is empty.', 'wp-parsidate' ), $this->args );
198 $feed->__destruct();
199 unset( $feed );
200
201 return $this;
202 }
203
204 $itemsNumber = (int) $this->args['items_number'];
205 if ( $itemsNumber < 1 || 20 < $itemsNumber ) {
206 $itemsNumber = 10;
207 }
208
209 $feedItems = [];
210 foreach ( $feed->get_items( 0, $itemsNumber ) as $item ) {
211 $feedItem = [];
212 if ( in_array( 'link', $this->args['fields'], true ) ) {
213 $link = $item->get_link();
214 while ( ! empty( $link ) && stristr( $link, 'http' ) !== $link ) {
215 $link = substr( $link, 1 );
216 }
217 $feedItem['link'] = esc_url_raw( wp_strip_all_tags( $link ) );
218 }
219
220 if ( in_array( 'title', $this->args['fields'], true ) ) {
221 $title = esc_html( trim( wp_strip_all_tags( $item->get_title() ) ) );
222 if ( empty( $title ) ) {
223 $title = esc_html__( 'Untitled', 'wp-parsidate' );
224 }
225
226 $feedItem['title'] = $title;
227 }
228
229 if ( in_array( 'description', $this->args['fields'], true ) ) {
230 $desc = html_entity_decode( $item->get_description(), ENT_QUOTES,
231 get_option( 'blog_charset' ) );
232 $desc = esc_attr( wp_trim_words( $desc, 55, ' [&hellip;]' ) );
233 $feedItem['description'] = $desc;
234 }
235
236 if ( in_array( 'datetime', $this->args['fields'], true ) ) {
237 $feedItem['datetime'] = $item->get_date( 'Y-m-d H:i:s' );
238 $feedItem['timestamp'] = $item->get_date( 'U' );
239 }
240
241 if ( in_array( 'author', $this->args['fields'], true ) ) {
242 $author = $item->get_author();
243 if ( is_object( $author ) ) {
244 $author = $author->get_name();
245 $author = esc_html( wp_strip_all_tags( $author ) );
246 }
247 $feedItem['author'] = $author;
248 }
249
250 if ( ! empty( $feedItem ) ) {
251 if ( ! empty( $this->replaceDescText ) ) {
252 $feedItem = $this->replaceText( $feedItem, $this->replaceDescText );
253 }
254 $feedItems[] = $feedItem;
255 }
256 }
257
258 if ( ! empty( $feedItems ) ) {
259 Cache::set( $this->args['cache_key'], $feedItems,
260 WP_PARSI_DEBUG_MODE ? MINUTE_IN_SECONDS * 10 : $this->args['cache_time'] );
261 $this->feedItems = $feedItems;
262 }
263
264 return $this;
265 }
266
267 public function setEmptyFeedDesc( $desc ) {
268 return Templates::load( Templates::getPath( 'feed-reader/feed_none.php' ), array( 'desc' => $desc ), false, false );
269 }
270
271 /**
272 * Replace text in feed fields
273 *
274 * @param array $feedItem Feed item
275 * @param array $replaceTexts Replace text's
276 * @param string $field Field key
277 *
278 * @return array Feed item
279 */
280 private function replaceText( array $feedItem, array $replaceTexts, string $field = 'description' ): array {
281 if ( isset( $feedItem[ $field ] ) ) {
282 foreach ( $replaceTexts as $replaceText ) {
283 if ( isset( $feedItem['title'] ) && str_contains( $replaceText[0], '%title%' ) ) {
284 $replaceText[0] = str_replace( '%title%', $feedItem['title'], $replaceText[0] );
285 }
286
287 $feedItem[ $field ] = trim( str_replace( $replaceText[0], $replaceText[1], $feedItem[ $field ] ) );
288 }
289 }
290
291 return $feedItem;
292 }
293 }
294