class-ad-list-stats.php
1 week ago
class-cache.php
1 week ago
class-conditional.php
3 months ago
class-content-injection.php
1 year ago
class-data.php
1 week ago
class-sanitize.php
1 year ago
class-testing.php
1 year ago
class-validation.php
1 year ago
class-wordpress.php
1 week ago
index.php
2 years ago
class-data.php
444 lines
| 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 AdvancedAds\Framework\Utilities\HTML; |
| 13 | use AdvancedAds\Framework\Utilities\Str; |
| 14 | use WP_Role; |
| 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 | $utm = ltrim( $utm, '/' ); |
| 366 | |
| 367 | $url = 'https://wpadvancedads.com/support/' . $utm . '-free-user'; |
| 368 | |
| 369 | if ( Conditional::is_any_addon_activated() ) { |
| 370 | $url = 'https://wpadvancedads.com/support/' . $utm . '-with-addons'; |
| 371 | } |
| 372 | |
| 373 | return $url; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Show feed from wpadvancedads.com |
| 378 | * |
| 379 | * @return void |
| 380 | */ |
| 381 | public static function display_rss_feed(): void { |
| 382 | $cache_key = 'advads_feed_posts_v2'; |
| 383 | $cache = get_transient( $cache_key ); |
| 384 | |
| 385 | // Check for cached content. |
| 386 | $cached_content = get_transient( $cache_key ); |
| 387 | if ( false !== $cached_content ) { |
| 388 | echo $cached_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | // Generate feed data. |
| 393 | $content = self::generate_adsense_data(); |
| 394 | |
| 395 | // Cache the content if it's valid. |
| 396 | if ( ! empty( $content ) ) { |
| 397 | set_transient( $cache_key, $content, 2 * HOUR_IN_SECONDS ); |
| 398 | } |
| 399 | |
| 400 | echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Get feed from wpadvancedads.com |
| 405 | * |
| 406 | * @return bool|string HTML content. |
| 407 | */ |
| 408 | private static function generate_adsense_data() { |
| 409 | // Get AdSense data. |
| 410 | $adsense_obj = \AdSense_Report_Data::get_data_from_options( 'domain' ); |
| 411 | $adsense_num = $adsense_obj->get_sums()['28days'] ?? 0; |
| 412 | $currency = $adsense_obj->get_currency() ?? ''; |
| 413 | |
| 414 | // Define thresholds and campaigns. |
| 415 | $feed_id = '1'; |
| 416 | $campaign = 'dashboard'; |
| 417 | $adsense_num = 1500; |
| 418 | $valid_currencies = [ 'EUR', 'USD', 'GBP', 'CHF' ]; |
| 419 | |
| 420 | if ( $adsense_num > 1000 && in_array( $currency, $valid_currencies, true ) ) { |
| 421 | $feed_id = '8361'; |
| 422 | $campaign = 'dashboard-adsense-motors'; |
| 423 | } elseif ( $adsense_num > 100 && in_array( $currency, $valid_currencies, true ) ) { |
| 424 | $feed_id = '8364'; |
| 425 | $campaign = 'dashboard-adsense'; |
| 426 | } |
| 427 | |
| 428 | $url = sprintf( 'https://wpadvancedads.com/wp-json/wp/v2/posts?categories=%s&per_page=3', $feed_id ); |
| 429 | $response = wp_remote_get( $url ); |
| 430 | |
| 431 | if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | $rss_posts = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 436 | |
| 437 | ob_start(); |
| 438 | include ADVADS_ABSPATH . 'views/admin/widgets/rss-posts.php'; |
| 439 | $content = ob_get_clean(); |
| 440 | |
| 441 | return $content; |
| 442 | } |
| 443 | } |
| 444 |