PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / trunk
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar vtrunk
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / includes / Extensions / WordPress / WPOrgReview.php

WPOrgReview.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar trunk, at includes/Extensions/WordPress/WPOrgReview.php

355 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WPOrgReview Extension
5 *
6 * @package NotificationX\Extensions
7 */
8
9 namespace NotificationX\Extensions\WordPress;
10
11 use NotificationX\Admin\Cron;
12 use NotificationX\Core\PostType;
13 use NotificationX\GetInstance;
14 use NotificationX\Extensions\Extension;
15 use NotificationX\Extensions\GlobalFields;
16
17 /**
18 * WPOrgReview Extension
19 * @method static WPOrgReview get_instance($args = null)
20 */
21 class WPOrgReview extends Extension {
22 /**
23 * Instance of WPOrgReview
24 *
25 * @var WPOrgReview
26 */
27 use GetInstance;
28 use WordPress;
29
30 /**
31 * Instance of WPOrg_Helper
32 *
33 * @var [WPOrg_Helper]
34 */
35 public $helper;
36
37 public $priority = 2;
38 public $id = 'wp_reviews';
39 public $img = NOTIFICATIONX_ADMIN_URL . 'images/extensions/sources/wordpress.png';
40 public $doc_link = 'https://notificationx.com/docs-category/configurations/';
41 public $types = 'reviews';
42 public $module = 'modules_wordpress';
43 public $cron_schedule = 'nx_wp_review_interval';
44 public $module_priority = 2;
45
46 /**
47 * Initially Invoked when initialized.
48 */
49 public function __construct() {
50 parent::__construct();
51 }
52
53 public function init_extension()
54 {
55 $this->title = __('WP.Org Reviews', 'notificationx');
56 $this->module_title = __('WordPress', 'notificationx');
57 }
58
59 public function init() {
60 parent::init();
61
62 // add_filter('nx_notification_link', array($this, 'notification_link'), 10, 2);
63
64 add_filter("nx_filtered_entry_{$this->id}", array($this, 'conversion_data'), 10, 2);
65
66 if ($this->helper === null) {
67 $this->helper = new WPOrg_Helper();
68 }
69 }
70
71 public function init_fields() {
72 parent::init_fields();
73 add_filter('nx_content_fields', [$this, 'content_fields']);
74 }
75
76 /**
77 * This functions is hooked
78 *
79 * @return void
80 */
81 public function admin_actions() {
82 parent::admin_actions();
83
84 add_action("nx_cron_update_data_{$this->id}", array($this, 'update_data'), 10, 2);
85 }
86
87 /**
88 * This functions is hooked
89 *
90 * @hooked nx_public_action
91 *
92 * @return void
93 */
94 public function public_actions() {
95 parent::public_actions();
96
97 // Show only single entry if total-rated theme.
98 add_filter( 'nx_frontend_get_entries', [$this, 'total_rated_theme'], 10, 3);
99 // Slice product name
100 add_filter( 'nx_frontend_get_entries', [$this, 'slice_product_name'], 10, 3);
101 }
102
103 /**
104 * Truncates product names in entries based on device-specific length settings.
105 *
106 * @param array $entries Array of product entries to process.
107 * @param array $ids Array of IDs to process (not used here).
108 * @param array $notifications Notification settings keyed by `nx_id`.
109 * @return array Updated entries array with truncated product names.
110 */
111 public function slice_product_name($entries, $ids, $notifications) {
112 foreach ($entries as $key => $entry) {
113 if($entry['source'] == $this->id){
114 $nx_id = $entry['nx_id'];
115 $settings = $notifications[$nx_id];
116 if ( !empty( $settings['wp_reviews_product_name_length'] ) && !empty( $entry['plugin_name'] ) ) {
117 $text = $entry['plugin_name'] ?? '';
118 $maxLength = wp_is_mobile() ? ($settings['wp_reviews_product_name_length']['mobile'] ?? 0) : ($settings['wp_reviews_product_name_length']['desktop'] ?? 0);
119 $entry['plugin_name'] = (strlen($text) > $maxLength && $maxLength > 0) ? substr($text, 0, $maxLength) . '...' : $text;
120 }
121 $entries[$key] = $entry;
122 }
123 }
124
125 return $entries;
126 }
127
128 /**
129 * Get data for WooCommerce Extension.
130 *
131 * @param array $args Settings arguments.
132 * @return mixed
133 */
134 public function content_fields($fields) {
135 $content_fields = &$fields['content']['fields'];
136
137 $content_fields['wp_reviews_product_type'] = [
138 'label' => __('Product Type', 'notificationx'),
139 'name' => 'wp_reviews_product_type',
140 'type' => 'select',
141 'priority' => 79,
142 'default' => 'plugin',
143 'options' => GlobalFields::get_instance()->normalize_fields([
144 'plugin' => __('Plugin', 'notificationx'),
145 ]),
146 'rules' => ['is', 'source', $this->id],
147 ];
148
149 $content_fields['wp_reviews_slug'] = [
150 'label' => __('Slug', 'notificationx'),
151 'name' => 'wp_reviews_slug',
152 'type' => 'text',
153 'priority' => 80,
154 'rules' => ['is', 'source', $this->id]
155 ];
156 $content_fields['wp_reviews_product_name_length'] = [
157 'name' => 'wp_reviews_product_name_length',
158 'type' => "responsive-number",
159 'label' => __('Product Name Length', 'notificationx'),
160 'rules' => ['is', 'source', $this->id],
161 'default' => [
162 "desktop" => 30,
163 "mobile" => 20,
164 ],
165 'priority' => 90,
166 'min' => 10,
167 'controls' => [
168 "desktop" => [
169 "icon" => NOTIFICATIONX_ADMIN_URL . 'images/responsive/desktop.svg',
170 'size' => 18,
171 ],
172 "mobile" => [
173 "icon" => NOTIFICATIONX_ADMIN_URL . 'images/responsive/mobile.svg',
174 'size' => 12,
175 ],
176 ],
177 'help' => __('Set a max content length for product name.', 'notificationx'),
178 ];
179 return $fields;
180 }
181
182 // @todo Something
183 public function fallback_data($data, $saved_data, $settings) {
184 // $data['username'] = __('Someone', 'notificationx');
185 $data['plugin_name_text'] = __('try it out', 'notificationx');
186 $data['anonymous_title'] = __('Anonymous', 'notificationx');
187 $data['rating'] = 5;
188 return $data;
189 }
190
191 public function notification_image($image_data, $data, $settings) {
192 if (!$settings['show_default_image']) {
193 $image_url = '';
194 switch ($settings['show_notification_image']) {
195 case 'featured_image':
196 if (isset($data['icons']['2x'])) {
197 $image_url = $data['icons']['2x'];
198 } else {
199 $image_url = isset($data['icons']['1x']) ? $data['icons']['1x'] : '';
200 }
201 break;
202 case 'gravatar':
203 if (isset($data['avatar'])) {
204 $avatar = $data['avatar']['src'];
205 $image_url = add_query_arg('s', '200', $avatar);
206 }
207 break;
208 }
209 $image_data['url'] = $image_url;
210 }
211
212 $alt_title = isset($data['plugin_name']) ? $data['plugin_name'] : '';
213 $alt_title = empty($alt_title) && isset($data['username']) ? $data['username'] : $alt_title;
214
215 $image_data['alt'] = $alt_title;
216
217 return $image_data;
218 }
219
220 public function saved_post($post, $data, $nx_id) {
221 $this->update_data($nx_id, $data);
222 }
223
224 public function update_data($nx_id, $data = array()) {
225 if (empty($nx_id)) {
226 return;
227 }
228 if (empty($data)) {
229 $data = PostType::get_instance()->get_post($nx_id);
230 }
231
232 $plugin_data = $this->get_plugins_data($nx_id, $data);
233 if(empty($plugin_data)){
234 return;
235 }
236
237 $reviews = $plugin_data['reviews'];
238 unset($plugin_data['reviews']);
239
240 // removing old notifications.
241 $this->delete_notification(null, $nx_id);
242 $entries = [];
243 foreach ($reviews as $review) {
244 $review = array_merge($review, $plugin_data);
245 $entries[] = [
246 'nx_id' => $nx_id,
247 'source' => $this->id,
248 'entry_key' => $review['username'],
249 'data' => $review,
250 ];
251 }
252 $this->update_notifications($entries);
253 return $reviews;
254 }
255
256 /**
257 * This function is responsible for making the notification ready for first time we make the notification.
258 *
259 * @param string $type
260 * @param array $data
261 * @return void
262 */
263 public function get_notification_ready($data = array()) {
264 if (!is_null($data['nx_id'])) {
265 return $this->update_data($data['nx_id'], $data);
266 }
267 return [];
268 }
269
270 public function get_plugins_data($post_id, $data) {
271 if (!$post_id) {
272 return;
273 }
274
275 $product_type = $data['wp_reviews_product_type'];
276 $plugin_slug = $data['wp_reviews_slug'];
277
278 $reviews = [];
279
280 if (!$plugin_slug) {
281 return;
282 }
283
284 if ($product_type == 'plugin') {
285 $reviews_html = $this->helper->get_plugin_reviews($plugin_slug);
286 $reviews = $this->helper->extract_reviews_from_html($reviews_html, $plugin_slug);
287 }
288
289 return $reviews;
290 }
291
292
293 // @todo frontend
294 public function conversion_data($saved_data, $settings) {
295 $saved_data['name'] = isset($saved_data['name']) ? html_entity_decode($saved_data['name']) : '';
296 if(empty($saved_data['rated'])){
297 $saved_data['rated'] = isset($saved_data['ratings']) ? $saved_data['ratings']['5'] : '';
298 }
299 if(empty($saved_data['plugin_name'])){
300 $saved_data['plugin_name'] = $saved_data['name'];
301 }
302
303 $product_type = $settings['wp_reviews_product_type'];
304 if (empty($saved_data['link']) && $product_type == 'plugin' && isset($saved_data['slug'])) {
305 //TODO: Its has to be specific reviews link.
306 $saved_data['link'] = 'https://wordpress.org/plugins/' . $saved_data['slug'];
307 }
308
309 return $saved_data;
310 }
311
312 /**
313 * Show only single entry if total-rated theme.
314 *
315 * @param array $entries
316 * @param array $ids
317 * @return void
318 */
319 public function total_rated_theme($entries, $ids, $notifications) {
320 $nx_ids = [];
321
322 foreach ($entries as $key => $entry) {
323 if($entry['source'] == $this->id){
324 $nx_id = $entry['nx_id'];
325 $settings = $notifications[$nx_id];
326 $theme = $settings['theme'];
327 $product_type = $settings['wp_reviews_product_type'];
328 if($theme === 'reviews_total-rated'){
329 if(!in_array($nx_id, $nx_ids)){
330 $nx_ids[] = $nx_id;
331
332 $entry['rated'] = isset($entry['ratings']) ? $entry['ratings']['5'] : '';
333 $entry['name'] = isset($entry['name']) ? html_entity_decode($entry['name']) : '';
334 $entry['plugin_name'] = $entry['name'];
335 $entry['timestamp'] = time();
336 $product_type = $settings['wp_reviews_product_type'];
337
338 if (empty($entry['link']) && $product_type == 'plugin' && isset($entry['slug'])) {
339 //TODO: Its has to be specific reviews link.
340 $entry['link'] = 'https://wordpress.org/plugins/' . $entry['slug'];
341 }
342 $entries[$key] = $entry;
343 }
344 else{
345 unset($entries[$key]);
346 }
347 }
348
349 }
350 }
351
352 return $entries;
353 }
354 }
355