PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.3.1
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.3.1
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 / WPOrgStats.php

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

297 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WPOrgStats Extension
5 *
6 * @package NotificationX\Extensions
7 */
8
9 namespace NotificationX\Extensions\WordPress;
10
11 use NotificationX\Admin\Cron;
12 use NotificationX\Core\Helper;
13 use NotificationX\Core\PostType;
14 use NotificationX\GetInstance;
15 use NotificationX\Extensions\Extension;
16 use NotificationX\Extensions\GlobalFields;
17
18 /**
19 * WPOrgStats Extension
20 * @method static WPOrgStats get_instance($args = null)
21 */
22 class WPOrgStats extends Extension {
23 /**
24 * Instance of WPOrgStats
25 *
26 * @var WPOrgStats
27 */
28 use GetInstance;
29 use WordPress;
30
31 /**
32 * Undocumented variable
33 *
34 * @var [WPOrg_Helper]
35 */
36 public $helper;
37
38 public $priority = 5;
39 public $id = 'wp_stats';
40 public $img = NOTIFICATIONX_ADMIN_URL . 'images/extensions/sources/wordpress.png';
41 public $doc_link = 'https://notificationx.com/docs-category/configurations/';
42 public $types = 'download_stats';
43 public $module = 'modules_wordpress';
44 public $cron_schedule = 'nx_wp_stats_interval';
45 public $module_priority = 2;
46
47 /**
48 * Initially Invoked when initialized.
49 */
50 public function __construct() {
51 parent::__construct();
52 }
53
54 public function init_extension()
55 {
56 $this->title = __('WP.Org Stats', 'notificationx');
57 $this->module_title = __('WordPress', 'notificationx');
58 }
59
60
61 public function init() {
62 parent::init();
63 add_filter("nx_filtered_entry_{$this->id}", array($this, 'conversion_data'), 10, 2);
64 }
65
66 public function init_fields() {
67 parent::init_fields();
68 add_filter('nx_content_fields', [$this, 'content_fields']);
69 }
70
71 /**
72 * This functions is hooked
73 *
74 * @return void
75 */
76 public function admin_actions() {
77 parent::admin_actions();
78 add_action("nx_cron_update_data_{$this->id}", array($this, 'update_data'), 10, 2);
79 }
80
81 /**
82 * This functions is hooked
83 *
84 * @hooked nx_public_action
85 *
86 * @return void
87 */
88 public function public_actions() {
89 parent::public_actions();
90
91 add_filter( 'nx_frontend_get_entries', [$this, 'slice_product_name'], 10, 3);
92 }
93
94 /**
95 * Truncates product names in entries based on device-specific length settings.
96 *
97 * @param array $entries Array of product entries to process.
98 * @param array $ids Array of IDs to process (not used here).
99 * @param array $notifications Notification settings keyed by `nx_id`.
100 * @return array Updated entries array with truncated product names.
101 */
102 public function slice_product_name($entries, $ids, $notifications) {
103 foreach ($entries as $key => $entry) {
104 if($entry['source'] == $this->id){
105 $nx_id = $entry['nx_id'];
106 $settings = $notifications[$nx_id];
107 if ( !empty( $settings['wp_stats_product_name_length'] ) && !empty( $entry['plugin_theme_name'] ) ) {
108 $text = $entry['plugin_theme_name'] ?? '';
109 $maxLength = wp_is_mobile() ? ($settings['wp_stats_product_name_length']['mobile'] ?? 0) : ($settings['wp_stats_product_name_length']['desktop'] ?? 0);
110 $entry['plugin_theme_name'] = (strlen($text) > $maxLength && $maxLength > 0) ? substr($text, 0, $maxLength) . '...' : $text;
111 }
112 $entries[$key] = $entry;
113 }
114 }
115
116 return $entries;
117 }
118
119 /**
120 * Get data for WooCommerce Extension.
121 *
122 * @param array $args Settings arguments.
123 * @return mixed
124 */
125 public function content_fields($fields) {
126 $content_fields = &$fields['content']['fields'];
127
128 $content_fields['wp_stats_product_type'] = [
129 'name' => 'wp_stats_product_type',
130 'type' => 'select',
131 'label' => __('Product Type', 'notificationx'),
132 'priority' => 79,
133 'default' => 'plugin',
134 'options' => GlobalFields::get_instance()->normalize_fields([
135 'plugin' => __('Plugin', 'notificationx'),
136 'theme' => __('Theme', 'notificationx'),
137 ]),
138 'rules' => ['is', 'source', $this->id]
139 ];
140
141 $content_fields['wp_stats_slug'] = [
142 'name' => 'wp_stats_slug',
143 'type' => 'text',
144 'label' => __('Slug', 'notificationx'),
145 'priority' => 80,
146 'rules' => ['is', 'source', $this->id]
147 ];
148 $content_fields['wp_stats_product_name_length'] = [
149 'name' => 'wp_stats_product_name_length',
150 'type' => "responsive-number",
151 'label' => __('Product Name Length', 'notificationx'),
152 'rules' => ['is', 'source', $this->id],
153 'default' => [
154 "desktop" => 30,
155 "mobile" => 20,
156 ],
157 'priority' => 90,
158 'min' => 10,
159 'controls' => [
160 "desktop" => [
161 "icon" => NOTIFICATIONX_ADMIN_URL . 'images/responsive/desktop.svg',
162 'size' => 18,
163 ],
164 "mobile" => [
165 "icon" => NOTIFICATIONX_ADMIN_URL . 'images/responsive/mobile.svg',
166 'size' => 12,
167 ],
168 ],
169 'help' => __('Set a max content length for product name.', 'notificationx'),
170 ];
171 return $fields;
172 }
173
174 // @todo
175 public function fallback_data($data, $saved_data, $settings) {
176 $data['today'] = 0;
177 $data['yesterday'] = 0;
178 $data['last_week'] = 0;
179 $data['all_time'] = 0;
180 $data['active_installs'] = 0;
181 $data['today_text'] = __('Try It Out', 'notificationx');
182 $data['last_week_text'] = __('Get Started for Free.', 'notificationx');
183 $data['all_time_text'] = __('Why Don\'t You?', 'notificationx');
184 $data['active_installs_text'] = __( 'Try It Out', 'notificationx' );
185
186 if(!empty($saved_data['name'])){
187 $data['plugin_theme_name'] = $saved_data['name'];
188 }
189
190 return $data;
191 }
192
193 // @todo Frontend
194 public function conversion_data($saved_data, $settings) {
195
196 // translators: %s: number of downloads today.
197 $saved_data['today'] = sprintf(__( '%s times today', 'notificationx' ), Helper::nice_number( $saved_data['today'] ));
198 // translators: %s: number of downloads yesterday.
199 $saved_data['yesterday'] = sprintf(__( '%s times', 'notificationx' ), Helper::nice_number( $saved_data['yesterday'] ));
200 // translators: %s: number of downloads in last 7 days.
201 $saved_data['last_week'] = sprintf(__( '%s times in last 7 days', 'notificationx' ), Helper::nice_number( $saved_data['last_week'] ));
202 // translators: %s: number of downloads of all time.
203 $saved_data['all_time'] = sprintf(__( '%s times', 'notificationx' ), Helper::nice_number( $saved_data['all_time'] ));
204 $saved_data['active_installs'] = Helper::nice_number( $saved_data['active_installs'] );
205 // wp_send_json($saved_data);
206 return $saved_data;
207 }
208
209 // @todo
210 public function notification_image($image_data, $data, $settings) {
211 if (!$settings['show_default_image']) {
212 $image_url = '';
213 if (isset($data['icons']) && $settings['wp_stats_product_type'] === 'plugin') {
214 if (isset($data['icons']['2x'])) {
215 $image_url = $data['icons']['2x'];
216 } else {
217 $image_url = isset($data['icons']['1x']) ? $data['icons']['1x'] : '';
218 }
219 }
220 if (isset($data['screenshot_url']) && $settings['wp_stats_product_type'] === 'theme') {
221 $image_url = $data['screenshot_url'];
222 }
223 $image_data['url'] = $image_url;
224 }
225
226 return $image_data;
227 }
228
229 public function saved_post($post, $data, $nx_id) {
230 $this->update_data($nx_id, $data);
231 return $post;
232 }
233
234 public function update_data($post_id, $data = array()) {
235 if (empty($post_id)) {
236 return;
237 }
238 if (empty($data)) {
239 $data = PostType::get_instance()->get_post($post_id);
240 }
241
242 $plugins_data = $this->get_plugins_data($post_id, $data);
243 $this->delete_notification(null, $post_id);
244 $this->update_notification([
245 'nx_id' => $post_id,
246 'source' => $this->id,
247 'entry_key' => !empty( $plugins_data['slug'] ) ? $plugins_data['slug'] : '',
248 'data' => $plugins_data,
249 ]);
250 }
251
252 public function get_plugins_data($post_id, $_nx_meta) {
253 if (!$post_id) {
254 return;
255 }
256 $this->helper = new WPOrg_Helper();
257 $product_type = $_nx_meta['wp_stats_product_type'];
258 $plugin_slug = $_nx_meta['wp_stats_slug'];
259
260 if (!$plugin_slug) {
261 return;
262 }
263
264 $total_stats = array();
265
266 if ($product_type == 'plugin') {
267 $raw_stats = $this->helper->get_plugin_stats($plugin_slug);
268 $raw_historical_summary = $this->remote_get('https://api.wordpress.org/stats/plugin/1.0/downloads.php?slug=' . $plugin_slug . '&historical_summary=1');
269 $historical_summary = json_decode(json_encode($raw_historical_summary), true);
270 $total_stats = array_merge($raw_stats, $historical_summary);
271 $total_stats['link'] = "https://wordpress.org/plugins/" . $total_stats['slug'];
272 }
273
274 if ($product_type == 'theme') {
275 $stats = $this->helper->get_theme_stats($plugin_slug);
276 $raw_historical_summary = $this->remote_get('https://api.wordpress.org/stats/themes/1.0/downloads.php?slug=' . $plugin_slug . '&historical_summary=1');
277 $historical_summary = json_decode(json_encode($raw_historical_summary), true);
278 $total_stats = array_merge($stats, $historical_summary);
279 }
280 $total_stats['plugin_theme_name'] = !empty($total_stats['name']) ? html_entity_decode($total_stats['name']) : '';
281 return $total_stats;
282 }
283
284 /**
285 * This function is responsible for making the notification ready for first time we make the notification.
286 *
287 * @param string $type
288 * @param array $data
289 * @return void
290 */
291 public function get_notification_ready($data = array()) {
292 if (!is_null($data['nx_id'])) {
293 $this->update_data($data['nx_id'], $data);
294 }
295 }
296 }
297