PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / libraries / adapter / rss / feed.php
vikappointments / libraries / adapter / rss Last commit date
feed.php 1 month ago optinexception.php 1 month ago reader.php 1 month ago
feed.php
289 lines
1 <?php
2 /**
3 * @package VikWP - Libraries
4 * @subpackage adapter.rss
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 JLoader::import('adapter.rss.optinexception');
15
16 /**
17 * Build a feed starting from the given SimplePie XML element.
18 *
19 * @since 10.1.31
20 */
21 class JRssFeed
22 {
23 /**
24 * A list of cached feeds.
25 *
26 * @var array
27 */
28 protected static $userFeeds = array();
29
30 /**
31 * A unique ID of the feed.
32 *
33 * @var string
34 */
35 protected $id;
36
37 /**
38 * The title of the feed.
39 *
40 * @var string
41 */
42 protected $title;
43
44 /**
45 * A summary of the feed.
46 * The full content in case the summary was not specified.
47 *
48 * @var string
49 */
50 protected $description;
51
52 /**
53 * The full content of the feed.
54 *
55 * @var string
56 */
57 protected $content;
58
59 /**
60 * The category to which the feed belong.
61 *
62 * @var string
63 */
64 protected $category;
65
66 /**
67 * The GMT publication date.
68 *
69 * @var string
70 */
71 protected $date;
72
73 /**
74 * The RSS permalink.
75 *
76 * @var string
77 */
78 protected $permalink;
79
80 /**
81 * Stores the current user ID.
82 *
83 * @var integer
84 */
85 protected $userId;
86
87 /**
88 * Stores the plugin callee.
89 *
90 * @var string
91 */
92 protected $plugin;
93
94 /**
95 * Class constructor.
96 *
97 * @param mixed $element The XML feed element.
98 * @param string $plugin The plugin that reads the feed.
99 */
100 public function __construct($element, $plugin)
101 {
102 if ($element instanceof SimplePie_Item || $element instanceof SimplePie\Item)
103 {
104 // extract data from SimplePie Item
105 $this->id = md5($element->get_id());
106 $this->title = $element->get_title();
107 $this->description = $element->get_description();
108 $this->content = $element->get_content();
109 $this->category = $element->get_category();
110 $this->date = JFactory::getDate($element->get_gmdate('Y-m-d H:i:s'));
111 $this->permalink = $element->get_permalink();
112 }
113 else
114 {
115 // extract data from array/object
116 foreach ((array) $element as $k => $v)
117 {
118 if (property_exists($this, $k))
119 {
120 $this->{$k} = $v;
121 }
122 }
123 }
124
125 $this->userId = JFactory::getUser()->id;
126
127 $this->plugin = (string) $plugin;
128 }
129
130 /**
131 * Magic method used to access internal properties.
132 *
133 * @param string $name The property name.
134 *
135 * @return mixed The value of the property.
136 */
137 public function __get($name)
138 {
139 if ($name === 'category')
140 {
141 return $this->category ? $this->category->get_label() : '';
142 }
143 else if (isset($this->{$name}))
144 {
145 return $this->{$name};
146 }
147
148 return null;
149 }
150
151 /**
152 * Magic method used to check whether a property exists.
153 *
154 * @param string $name The property name.
155 *
156 * @return boolean True in case the property is set.
157 */
158 public function __isset($name)
159 {
160 return isset($this->{$name});
161 }
162
163 /**
164 * Marks the feed as already seen.
165 *
166 * @return self This object to support chaining.
167 */
168 public function dismiss()
169 {
170 if ($this->userId)
171 {
172 // dismiss the feed
173 static::setUserFeed($this->userId, $this->plugin, $this->id, 0);
174 }
175
176 return $this;
177 }
178
179 /**
180 * Display feed again in a second time.
181 *
182 * @param boolean $status True if seen, false otherwise.
183 *
184 * @return self This object to support chaining.
185 */
186 public function delay($minutes = 60)
187 {
188 if ($this->userId)
189 {
190 // delay the feed
191 static::setUserFeed($this->userId, $this->plugin, $this->id, $minutes);
192 }
193
194 return $this;
195 }
196
197 /**
198 * Checks whether the feed should be displayed or not.
199 *
200 * @return boolean True to display the feed, false otherwise.
201 */
202 public function isVisible()
203 {
204 if (!$this->userId)
205 {
206 // never visible for users
207 return false;
208 }
209
210 // get user feeds
211 $feeds = static::getUserFeeds($this->userId, $this->plugin);
212
213 // check if the feed was registered
214 if (!isset($feeds[$this->id]))
215 {
216 // feed not registered, can display it
217 return true;
218 }
219
220 // check whether the feed was dismissed
221 if (!$feeds[$this->id])
222 {
223 // already dismissed, do not display
224 return false;
225 }
226
227 // then check whether we reached the delay threshold
228 return JFactory::getDate('now') >= JFactory::getDate($feeds[$this->id]);
229 }
230
231 /**
232 * Returns a list of user feeds.
233 *
234 * @param integer $userId The user ID.
235 * @param string $plugin The callee.
236 *
237 * @return &array A lookup containing all the viewed feeds.
238 */
239 protected static function &getUserFeeds($userId, $plugin)
240 {
241 if (!isset(static::$userFeeds[$userId]))
242 {
243 // recover user lookup
244 $feed = get_user_meta($userId, $plugin . '_rss_feeds', true);
245
246 // cache result
247 static::$userFeeds[$userId] = $feed ? (array) $feed : array();
248 }
249
250 return static::$userFeeds[$userId];
251 }
252
253 /**
254 * Flags the status of the specified feed.
255 *
256 * @param integer $userId The user ID.
257 * @param string $plugin The callee.
258 * @param string $feedId The feed ID.
259 * @param integer $value The flag value. Specify 0 to dismiss the feed.
260 * Any other positive value will delay the feed
261 * by the specified minutes.
262 *
263 * @return void
264 */
265 protected static function setUserFeed($userId, $plugin, $feedId, $value)
266 {
267 // get user feeds
268 $feeds = static::getUserFeeds($userId, $plugin);
269
270 // take only positive values
271 $value = abs($value);
272
273 if ($value)
274 {
275 // create limit date
276 $date = JFactory::getDate('+' . $value . ' minutes');
277
278 // register SQL date
279 $value = $date->toSql();
280 }
281
282 // register value
283 $feeds[$feedId] = $value;
284
285 // permanently update
286 update_user_meta($userId, $plugin . '_rss_feeds', $feeds);
287 }
288 }
289