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