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

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

253 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace NotificationX\Extensions\WordPress;
4
5 class WPOrg_Helper {
6
7 public $plugin_information;
8 public $theme_information;
9
10 protected function get_links( $html, $strip_tags = false ) {
11
12 $links = array();
13
14 $doc = new \DOMDocument();
15 $doc->loadHTML( $html );
16 $linkTags = $doc->getElementsByTagName( 'a' );
17
18 foreach ( $linkTags as $tag ) {
19 if ( $strip_tags ) {
20 $links[] = trim( strip_tags( $tag->ownerDocument->saveXML( $tag ) ) );
21 } else {
22 $links[] = $tag->ownerDocument->saveXML( $tag );
23 }
24 }
25
26 return $links;
27
28 }
29
30 protected function get_link_href( $html ) {
31
32 $doc = new \DOMDocument();
33 $doc->loadHTML( $html );
34 $linkhrefs = array();
35 $linkTags = $doc->getElementsByTagName( 'a' );
36
37 foreach ( $linkTags as $tag ) {
38 $linkhrefs[] = $tag->getAttribute( 'href' );
39 }
40
41 if ( ! empty( $linkhrefs ) ) {
42 return $linkhrefs[0];
43 } else{
44 return '';
45 }
46
47 }
48
49 protected function get_image_src( $html ) {
50
51 $doc = new \DOMDocument();
52 $doc->loadHTML( $html );
53 $imagepaths = array();
54 $imageTags = $doc->getElementsByTagName( 'img' );
55
56 foreach ( $imageTags as $tag ) {
57 $imagepaths[] = $tag->getAttribute( 'src' );
58 }
59
60 if ( ! empty( $imagepaths ) ) {
61 return $imagepaths[0];
62 } else{
63 return '';
64 }
65
66 }
67
68 protected function get_node_content( $html, $class ) {
69
70 $dom = new \DOMDocument();
71 $dom->loadHTML( $html );
72 $finder = new \DomXPath( $dom );
73 $nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $class ')]");
74 $content = '';
75
76 foreach ( $nodes as $element ) {
77 $content = $element->ownerDocument->saveXML( $element );
78 }
79
80 return trim( strip_tags( $content ) );
81
82 }
83
84 protected function get_tag_content( $html, $search ) {
85
86 $doc = new \DOMDocument();
87 $doc->loadHTML( $html );
88 $titlepaths = array();
89 $titleTags = $doc->getElementsByTagName( $search );
90
91 foreach ( $titleTags as $tag ) {
92 $titlepaths[] = $tag->ownerDocument->saveXML( $tag );
93 }
94
95 if ( ! empty( $titlepaths ) ) {
96 return trim( strip_tags( $titlepaths[0] ) );
97 } else{
98 return '';
99 }
100
101 }
102
103 protected function get_rating_content( $html, $class ) {
104
105 $dom = new \DOMDocument();
106 $dom->loadHTML( $html );
107 $finder = new \DomXPath( $dom );
108 $nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $class ')]");
109
110 $content = '';
111 foreach ( $nodes as $element ) {
112 $content = $element->getAttribute('data-rating');
113 }
114
115 return trim( strip_tags( $content ) );
116
117 }
118
119 protected function extract_review_data( $review ){
120 $data = array();
121 $links = $this->get_links( $review, true );
122
123 $data['username'] = isset( $links[1] ) ? $links[1] : '';
124 // $data['username']['text'] = isset( $links[1] ) ? $links[1] : '';
125 // $data['username']['href'] = $this->get_link_href( $review );
126 $data['avatar']['src'] = $this->get_image_src( $review );
127 $data['content'] = iconv("UTF-8", 'ISO-8859-1', $this->get_node_content( $review, 'review-body' ));
128 $data['plugin_name'] = $this->plugin_information->name;
129 $data['title'] = iconv("UTF-8", 'ISO-8859-1', $this->get_tag_content( $review, 'h4' ));
130 $data['timestamp'] = strtotime( iconv("UTF-8", 'ISO-8859-1', $this->get_node_content( $review, 'review-date' )) );
131 $data['rating'] = $this->get_rating_content( $review, 'wporg-ratings' );
132
133 return $data;
134 }
135
136 public function extract_reviews_from_html( $data, $plugin_slug = '' ){
137 $extracted_reviews = array();
138
139 $dom = new \DOMDocument();
140 $slug = $data['slug'];
141 $icons = $data['icons'];
142 $dom->loadHTML('<?xml encoding="UTF-8">' . $data['reviews']);
143
144 $finder = new \DomXPath( $dom );
145 $nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' review ')]");
146
147 $conditioned_filter = apply_filters( 'nx_wp_reviews_rating_condition', 3 );
148
149 foreach ( $nodes as $node ) {
150 $raw_review = $node->ownerDocument->saveXML( $node );
151 $review = $this->extract_review_data( $raw_review );
152 if( isset( $review['rating'] ) && intval( $review['rating'] ) < $conditioned_filter ) {
153 continue;
154 }
155 if( $plugin_slug ) {
156 $review['link'] = 'https://wordpress.org/plugins/' . $plugin_slug;
157 }
158 $review['slug'] = $slug;
159 $review['icons'] = $icons;
160 $extracted_reviews[] = $review;
161 }
162
163 unset( $data['reviews'] );
164 if( ! empty( $data ) && ! empty( $extracted_reviews ) ) {
165 return array_merge( $data, array( 'reviews' => $extracted_reviews ) );
166 }
167 return array();
168 }
169
170 public function get_plugin_reviews( $plugin_slug ){
171 if( ! function_exists('plugins_api') ) {
172 require_once ABSPATH . '/wp-admin/includes/plugin-install.php';
173 }
174
175 $this->plugin_information = plugins_api( 'plugin_information', array( 'slug' => $plugin_slug, 'fields' => array( 'reviews' => true, 'icons' => true ) ) );
176
177 $data = array();
178 $data['slug'] = isset( $this->plugin_information->slug ) ? $this->plugin_information->slug : '';
179 $data['icons'] = isset( $this->plugin_information->icons ) ? $this->plugin_information->icons : '';
180 $data['name'] = isset( $this->plugin_information->name ) ? $this->plugin_information->name : '';
181 $data['ratings'] = isset( $this->plugin_information->ratings ) ? $this->plugin_information->ratings : '';
182 $data['rated'] = isset( $this->plugin_information->num_ratings ) ? $this->plugin_information->num_ratings : '';
183 $data['reviews'] = isset( $this->plugin_information->sections ) ? $this->plugin_information->sections['reviews'] : '';
184 return $data;
185 }
186
187 // TODO: Themes Review ( Upcoming )
188 public function get_theme_reviews( $theme_slug ){
189 if( ! function_exists('themes_api') ) {
190 require_once ABSPATH . '/wp-admin/includes/themes.php';
191 }
192 return [];
193 }
194
195 public function get_plugin_stats( $plugin_slug ){
196 if( ! function_exists('plugins_api') ) {
197 require_once ABSPATH . '/wp-admin/includes/plugin-install.php';
198 }
199
200 $this->plugin_information = plugins_api( 'plugin_information', array( 'slug' => $plugin_slug, 'fields' => array( 'downloaded' => true, 'icons' => true, 'historical_summary' => true, 'active_installs' => true ) ) );
201 $new_data = [];
202
203 if( is_wp_error( $this->plugin_information ) ) {
204 return $new_data;
205 }
206
207 $needed_key = array(
208 'name', 'slug', 'num_ratings', 'rating', 'homepage', 'version', 'downloaded', 'icons', 'active_installs', 'author_profile', 'author'
209 );
210
211 foreach( $needed_key as $key => $value ) {
212 if( isset( $this->plugin_information->$value ) ) {
213 $new_data[ $value ] = $this->plugin_information->$value;
214 }
215 }
216
217 if( isset( $new_data['homepage'] ) ) {
218 $new_data['link'] = $new_data['homepage'];
219 unset( $new_data['homepage'] );
220 }
221
222 return $new_data;
223 }
224
225 public function get_theme_stats( $theme_slug ){
226 if( ! function_exists('themes_api') ) {
227 require_once ABSPATH . '/wp-admin/includes/theme.php';
228 }
229
230 $this->theme_information = themes_api( 'theme_information', array( 'slug' => $theme_slug, 'fields' => array( 'downloaded' => true, 'sections' => true, 'theme_url' => true, 'photon_screenshots' => true, 'screenshot_url' => true, 'active_installs' => true ) ) );
231
232 $new_data = array();
233 if( is_wp_error( $this->theme_information ) ) {
234 return $new_data;
235 }
236 $needed_key = array(
237 'name', 'slug', 'num_ratings', 'rating', 'homepage', 'version', 'downloaded', 'screenshot_url', 'active_installs'
238 );
239
240 foreach( $needed_key as $key => $value ) {
241 if( isset( $this->theme_information->$value ) ) {
242 $new_data[ $value ] = $this->theme_information->$value;
243 }
244 }
245
246 if( isset( $new_data['homepage'] ) ) {
247 $new_data['link'] = $new_data['homepage'];
248 unset( $new_data['homepage'] );
249 }
250
251 return $new_data;
252 }
253 }