PluginProbe
Advanced Ads – Ad Manager & AdSense / 2.0.21
Advanced Ads – Ad Manager & AdSense v2.0.21
2.0.26 2.0.25 2.0.24 2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 All 348 releases
advanced-ads / includes / utilities / class-data.php

class-data.php in Advanced Ads – Ad Manager & AdSense 2.0.21, at includes/utilities/class-data.php

442 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The class provides utility functions for retrieving and managing plugin data and choices.
4 *
5 * @package AdvancedAds
6 * @author Advanced Ads <info@wpadvancedads.com>
7 * @since 1.47.0
8 */
9
10 namespace AdvancedAds\Utilities;
11
12 use WP_Role;
13 use AdvancedAds\Framework\Utilities\HTML;
14 use AdvancedAds\Framework\Utilities\Str;
15
16 defined( 'ABSPATH' ) || exit;
17
18 /**
19 * Data and Choices.
20 */
21 class Data {
22
23 /**
24 * Get the list of all add-ons.
25 *
26 * @return array
27 */
28 public static function get_addons(): array {
29 static $advads_addons = null;
30
31 if ( null === $advads_addons ) {
32 $advads_addons = [];
33 if ( ! function_exists( 'get_plugins' ) ) {
34 require_once ABSPATH . 'wp-admin/includes/plugin.php';
35 }
36
37 $plugins = \get_plugins();
38 $allowed = [
39 'advanced-ads-pro',
40 'advanced-ads-responsive',
41 'advanced-ads-gam',
42 'advanced-ads-layer',
43 'advanced-ads-selling',
44 'advanced-ads-sticky',
45 'advanced-ads-tracking',
46 'slider-ads',
47 ];
48
49 foreach ( $plugins as $plugin_file => $plugin_data ) {
50 $slug = $plugin_data['TextDomain'];
51 if ( ! in_array( $slug, $allowed, true ) ) {
52 continue;
53 }
54
55 $name = str_replace( [ '', 'Advanced Ads ' ], '', $plugin_data['Name'] );
56
57 $advads_addons[ $slug ] = [
58 'id' => str_replace( 'advanced-ads-', '', $slug ),
59 'name' => $name,
60 'version' => $plugin_data['Version'] ?? '0.0.1',
61 'path' => $plugin_file,
62 'options_slug' => $slug,
63 'uri' => $plugin_data['PluginURI'] ?? 'https://wpadvancedads.com',
64 ];
65 }
66 }
67
68 return $advads_addons;
69 }
70
71 /**
72 * Get the admin screen ids.
73 *
74 * @return array
75 */
76 public static function get_admin_screen_ids(): array {
77 return apply_filters(
78 'advanced-ads-dashboard-screens',
79 [
80 'advanced_ads',
81 'edit-advanced_ads',
82 'edit-advanced_ads_plcmnt',
83 'toplevel_page_advanced-ads',
84 'admin_page_advanced-ads-debug',
85 'admin_page_advanced-ads-import-export',
86 'advanced-ads_page_advanced-ads-groups',
87 'advanced-ads_page_advanced-ads-placements',
88 'advanced-ads_page_advanced-ads-settings',
89 'advanced-ads_page_advanced-ads-tools',
90 ]
91 );
92 }
93
94 /**
95 * Get ad ids
96 *
97 * @return array
98 */
99 public static function get_ads_ids(): array {
100 static $ad_ids = null;
101
102 if ( null !== $ad_ids ) {
103 return $ad_ids;
104 }
105
106 $ad_ids = wp_advads_get_ads_dropdown();
107 $ad_ids = array_keys( $ad_ids );
108
109 return $ad_ids;
110 }
111
112 /**
113 * Get the array of known bots.
114 *
115 * @param bool $filter Whether to apply filters.
116 *
117 * @return array
118 */
119 public static function get_bots( $filter = true ) {
120 // List of bots and crawlers to exclude from ad impressions.
121 $bots = [
122 'bot',
123 'spider',
124 'crawler',
125 'scraper',
126 'parser',
127 '008',
128 'Accoona-AI-Agent',
129 'ADmantX',
130 'alexa',
131 'appie',
132 'Apple-PubSub',
133 'Arachmo',
134 'Ask Jeeves',
135 'avira\.com',
136 'B-l-i-t-z-B-O-T',
137 'boitho\.com-dc',
138 'BUbiNG',
139 'Cerberian Drtrs',
140 'Charlotte',
141 'cosmos',
142 'Covario IDS',
143 'curl',
144 'Datanyze',
145 'DataparkSearch',
146 'Dataprovider\.com',
147 'DDG-Android',
148 'Ecosia',
149 'expo9',
150 'facebookexternalhit',
151 'Feedfetcher-Google',
152 'FindLinks',
153 'Firefly',
154 'froogle',
155 'Genieo',
156 'heritrix',
157 'Holmes',
158 'htdig',
159 'https://developers\.google\.com',
160 'ia_archiver',
161 'ichiro',
162 'igdeSpyder',
163 'InfoSeek',
164 'inktomi',
165 'Kraken',
166 'L\.webis',
167 'Larbin',
168 'Linguee',
169 'LinkWalker',
170 'looksmart',
171 'lwp-trivial',
172 'mabontland',
173 'Mnogosearch',
174 'mogimogi',
175 'Morning Paper',
176 'MVAClient',
177 'NationalDirectory',
178 'NetResearchServer',
179 'NewsGator',
180 'NG-Search',
181 'Nusearch',
182 'NutchCVS',
183 'Nymesis',
184 'oegp',
185 'Orbiter',
186 'Peew',
187 'Pompos',
188 'PostPost',
189 'proximic',
190 'PycURL',
191 'Qseero',
192 'rabaz',
193 'Radian6',
194 'Reeder',
195 'savetheworldheritage',
196 'SBIder',
197 'Scooter',
198 'ScoutJet',
199 'Scrubby',
200 'SearchSight',
201 'semanticdiscovery',
202 'Sensis',
203 'ShopWiki',
204 'silk',
205 'Snappy',
206 'Spade',
207 'Sqworm',
208 'StackRambler',
209 'TechnoratiSnoop',
210 'TECNOSEEK',
211 'Teoma',
212 'Thumbnail\.CZ',
213 'TinEye',
214 'truwoGPS',
215 'updated',
216 'Vagabondo',
217 'voltron',
218 'Vortex',
219 'voyager',
220 'VYU2',
221 'WebBug',
222 'webcollage',
223 'WebIndex',
224 'Websquash\.com',
225 'WeSEE:Ads',
226 'wf84',
227 'Wget',
228 'WomlpeFactory',
229 'WordPress',
230 'yacy',
231 'Yahoo! Slurp',
232 'Yahoo! Slurp China',
233 'YahooSeeker',
234 'YahooSeeker-Testing',
235 'YandexBot',
236 'YandexMedia',
237 'YandexBlogs',
238 'YandexNews',
239 'YandexCalendar',
240 'YandexImages',
241 'Yeti',
242 'yoogliFetchAgent',
243 'Zao',
244 'ZyBorg',
245 'okhttp',
246 'ips-agent',
247 'ltx71',
248 'Optimizer',
249 'Daum',
250 'Qwantify',
251 ];
252
253 return (array) ( $filter ? apply_filters( 'advanced-ads-bots', $bots ) : $bots );
254 }
255
256 /**
257 * Get the roles that are allowed to edit ads.
258 *
259 * @return array
260 */
261 public static function get_filtered_roles_by_cap(): array {
262 return array_filter(
263 wp_roles()->role_objects,
264 static function ( WP_Role $role ) {
265 return $role->has_cap( 'advanced_ads_edit_ads' );
266 }
267 );
268 }
269
270 /**
271 * Render items dropdown html.
272 *
273 * @param array $args Arguments for the dropdown.
274 *
275 * @return void
276 */
277 public static function items_dropdown( $args = [] ): void {
278 $items = self::items_for_select();
279
280 $attrs = [
281 'id' => $args['id'] ?? 'advads-items-select',
282 'name' => $args['name'] ?? 'advads-items-select',
283 'class' => $args['class'] ?? 'advads-items-select',
284 ];
285 ?>
286 <select <?php echo HTML::build_attributes( $attrs ); // phpcs:ignore ?>>
287 <option value=""><?php esc_html_e( '--empty--', 'advanced-ads' ); ?></option>
288 <?php if ( isset( $items['ads'] ) ) : ?>
289 <optgroup label="<?php esc_html_e( 'Ads', 'advanced-ads' ); ?>">
290 <?php foreach ( $items['ads'] as $ad_id => $ad_title ) : ?>
291 <option value="<?php echo esc_attr( $ad_id ); ?>"><?php echo esc_html( $ad_title ); ?></option>
292 <?php endforeach; ?>
293 </optgroup>
294 <?php endif; ?>
295 <?php if ( isset( $items['groups'] ) ) : ?>
296 <optgroup label="<?php esc_html_e( 'Ad Groups', 'advanced-ads' ); ?>">
297 <?php foreach ( $items['groups'] as $group_id => $group_title ) : ?>
298 <option value="<?php echo esc_attr( $group_id ); ?>"><?php echo esc_html( $group_title ); ?></option>
299 <?php endforeach; ?>
300 </optgroup>
301 <?php endif; ?>
302 <?php if ( isset( $items['placements'] ) ) : ?>
303 <optgroup label="<?php esc_html_e( 'Placements', 'advanced-ads' ); ?>">
304 <?php foreach ( $items['placements'] as $placement_id => $placement_title ) : ?>
305 <option value="<?php echo esc_attr( $placement_id ); ?>"><?php echo esc_html( $placement_title ); ?></option>
306 <?php endforeach; ?>
307 </optgroup>
308 <?php endif; ?>
309 </select>
310 <?php
311 }
312
313 /**
314 * Get items for item select field
315 *
316 * @param array $args Arguments for the dropdown.
317 *
318 * @return array
319 */
320 public static function items_for_select( $args = [] ): array {
321 $select = [];
322 $args = wp_parse_args(
323 $args,
324 [
325 'ads' => true,
326 'groups' => true,
327 'placements' => true,
328 ]
329 );
330
331 if ( $args['ads'] ) {
332 $ads = wp_advads_get_ads_dropdown();
333 foreach ( $ads as $ad_id => $ad_title ) {
334 $select['ads'][ 'ad_' . $ad_id ] = $ad_title;
335 }
336 }
337
338 if ( $args['groups'] ) {
339 $groups = wp_advads_get_groups_dropdown();
340 foreach ( $groups as $group_id => $group_title ) {
341 $select['groups'][ 'group_' . $group_id ] = $group_title;
342 }
343 }
344
345 if ( $args['placements'] ) {
346 $placements = wp_advads_get_placements_dropdown();
347 foreach ( $placements as $placement_id => $placement_title ) {
348 $select['placements'][ 'placement_' . $placement_id ] = $placement_title;
349 }
350 }
351
352 return $select;
353 }
354
355 /**
356 * Get the correct support URL: wp.org for free users and website for those with any add-on installed
357 *
358 * @param string $utm add UTM parameter to the link leading to https://wpadvancedads.com, if given.
359 *
360 * @return string URL.
361 */
362 public static function support_url( $utm = '' ) {
363
364 $utm = empty( $utm ) ? '?utm_source=advanced-ads&utm_medium=link&utm_campaign=support' : $utm;
365 $url = 'https://wpadvancedads.com/support/' . $utm . '-free-user';
366
367 if ( Conditional::is_any_addon_activated() ) {
368 $url = 'https://wpadvancedads.com/support/' . $utm . '-with-addons';
369 }
370
371 return $url;
372 }
373
374 /**
375 * Show feed from wpadvancedads.com
376 *
377 * @return void
378 */
379 public static function display_rss_feed(): void {
380 $cache_key = 'advads_feed_posts_v2';
381 $cache = get_transient( $cache_key );
382
383 // Check for cached content.
384 $cached_content = get_transient( $cache_key );
385 if ( false !== $cached_content ) {
386 echo $cached_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
387 return;
388 }
389
390 // Generate feed data.
391 $content = self::generate_adsense_data();
392
393 // Cache the content if it's valid.
394 if ( ! empty( $content ) ) {
395 set_transient( $cache_key, $content, 2 * HOUR_IN_SECONDS );
396 }
397
398 echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
399 }
400
401 /**
402 * Get feed from wpadvancedads.com
403 *
404 * @return bool|string HTML content.
405 */
406 private static function generate_adsense_data() {
407 // Get AdSense data.
408 $adsense_obj = \AdSense_Report_Data::get_data_from_options( 'domain' );
409 $adsense_num = $adsense_obj->get_sums()['28days'] ?? 0;
410 $currency = $adsense_obj->get_currency() ?? '';
411
412 // Define thresholds and campaigns.
413 $feed_id = '1';
414 $campaign = 'dashboard';
415 $adsense_num = 1500;
416 $valid_currencies = [ 'EUR', 'USD', 'GBP', 'CHF' ];
417
418 if ( $adsense_num > 1000 && in_array( $currency, $valid_currencies, true ) ) {
419 $feed_id = '8361';
420 $campaign = 'dashboard-adsense-motors';
421 } elseif ( $adsense_num > 100 && in_array( $currency, $valid_currencies, true ) ) {
422 $feed_id = '8364';
423 $campaign = 'dashboard-adsense';
424 }
425
426 $url = sprintf( 'https://wpadvancedads.com/wp-json/wp/v2/posts?categories=%s&per_page=3', $feed_id );
427 $response = wp_remote_get( $url );
428
429 if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
430 return false;
431 }
432
433 $rss_posts = json_decode( wp_remote_retrieve_body( $response ), true );
434
435 ob_start();
436 include ADVADS_ABSPATH . 'views/admin/widgets/rss-posts.php';
437 $content = ob_get_clean();
438
439 return $content;
440 }
441 }
442