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 / WPOrg_Helper.php

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

254 lines 8.0 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( wp_strip_all_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( wp_strip_all_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( wp_strip_all_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( wp_strip_all_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 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context.
148 $conditioned_filter = apply_filters( 'nx_wp_reviews_rating_condition', 3 );
149
150 foreach ( $nodes as $node ) {
151 $raw_review = $node->ownerDocument->saveXML( $node );
152 $review = $this->extract_review_data( $raw_review );
153 if( isset( $review['rating'] ) && intval( $review['rating'] ) < $conditioned_filter ) {
154 continue;
155 }
156 if( $plugin_slug ) {
157 $review['link'] = 'https://wordpress.org/plugins/' . $plugin_slug;
158 }
159 $review['slug'] = $slug;
160 $review['icons'] = $icons;
161 $extracted_reviews[] = $review;
162 }
163
164 unset( $data['reviews'] );
165 if( ! empty( $data ) && ! empty( $extracted_reviews ) ) {
166 return array_merge( $data, array( 'reviews' => $extracted_reviews ) );
167 }
168 return array();
169 }
170
171 public function get_plugin_reviews( $plugin_slug ){
172 if( ! function_exists('plugins_api') ) {
173 require_once ABSPATH . '/wp-admin/includes/plugin-install.php';
174 }
175
176 $this->plugin_information = plugins_api( 'plugin_information', array( 'slug' => $plugin_slug, 'fields' => array( 'reviews' => true, 'icons' => true ) ) );
177
178 $data = array();
179 $data['slug'] = isset( $this->plugin_information->slug ) ? $this->plugin_information->slug : '';
180 $data['icons'] = isset( $this->plugin_information->icons ) ? $this->plugin_information->icons : '';
181 $data['name'] = isset( $this->plugin_information->name ) ? $this->plugin_information->name : '';
182 $data['ratings'] = isset( $this->plugin_information->ratings ) ? $this->plugin_information->ratings : '';
183 $data['rated'] = isset( $this->plugin_information->num_ratings ) ? $this->plugin_information->num_ratings : '';
184 $data['reviews'] = isset( $this->plugin_information->sections ) ? $this->plugin_information->sections['reviews'] : '';
185 return $data;
186 }
187
188 // TODO: Themes Review ( Upcoming )
189 public function get_theme_reviews( $theme_slug ){
190 if( ! function_exists('themes_api') ) {
191 require_once ABSPATH . '/wp-admin/includes/themes.php';
192 }
193 return [];
194 }
195
196 public function get_plugin_stats( $plugin_slug ){
197 if( ! function_exists('plugins_api') ) {
198 require_once ABSPATH . '/wp-admin/includes/plugin-install.php';
199 }
200
201 $this->plugin_information = plugins_api( 'plugin_information', array( 'slug' => $plugin_slug, 'fields' => array( 'downloaded' => true, 'icons' => true, 'historical_summary' => true, 'active_installs' => true ) ) );
202 $new_data = [];
203
204 if( is_wp_error( $this->plugin_information ) ) {
205 return $new_data;
206 }
207
208 $needed_key = array(
209 'name', 'slug', 'num_ratings', 'rating', 'homepage', 'version', 'downloaded', 'icons', 'active_installs', 'author_profile', 'author'
210 );
211
212 foreach( $needed_key as $key => $value ) {
213 if( isset( $this->plugin_information->$value ) ) {
214 $new_data[ $value ] = $this->plugin_information->$value;
215 }
216 }
217
218 if( isset( $new_data['homepage'] ) ) {
219 $new_data['link'] = $new_data['homepage'];
220 unset( $new_data['homepage'] );
221 }
222
223 return $new_data;
224 }
225
226 public function get_theme_stats( $theme_slug ){
227 if( ! function_exists('themes_api') ) {
228 require_once ABSPATH . '/wp-admin/includes/theme.php';
229 }
230
231 $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 ) ) );
232
233 $new_data = array();
234 if( is_wp_error( $this->theme_information ) ) {
235 return $new_data;
236 }
237 $needed_key = array(
238 'name', 'slug', 'num_ratings', 'rating', 'homepage', 'version', 'downloaded', 'screenshot_url', 'active_installs'
239 );
240
241 foreach( $needed_key as $key => $value ) {
242 if( isset( $this->theme_information->$value ) ) {
243 $new_data[ $value ] = $this->theme_information->$value;
244 }
245 }
246
247 if( isset( $new_data['homepage'] ) ) {
248 $new_data['link'] = $new_data['homepage'];
249 unset( $new_data['homepage'] );
250 }
251
252 return $new_data;
253 }
254 }