Advanced_Ads_Modal.php
2 years ago
EDD_SL_Plugin_Updater.php
4 years ago
ad-ajax.php
2 years ago
ad-debug.php
2 years ago
ad-expiration.php
3 years ago
ad-health-notices.php
2 years ago
ad-model.php
2 years ago
ad-select.php
3 years ago
ad.php
2 years ago
ad_ajax_callbacks.php
2 years ago
ad_group.php
2 years ago
ad_placements.php
2 years ago
ad_type_abstract.php
2 years ago
ad_type_content.php
2 years ago
ad_type_dummy.php
3 years ago
ad_type_group.php
2 years ago
ad_type_image.php
2 years ago
ad_type_plain.php
2 years ago
checks.php
2 years ago
class-translation-promo.php
3 years ago
compatibility.php
2 years ago
display-conditions.php
2 years ago
filesystem.php
3 years ago
frontend_checks.php
2 years ago
in-content-injector.php
2 years ago
inline-css.php
2 years ago
plugin.php
2 years ago
upgrades.php
2 years ago
utils.php
3 years ago
visitor-conditions.php
2 years ago
widget.php
2 years ago
ad_group.php
683 lines
| 1 | <?php |
| 2 | |
| 3 | use AdvancedAds\Entities; |
| 4 | use AdvancedAds\Utilities\WordPress; |
| 5 | |
| 6 | /** |
| 7 | * An ad group object |
| 8 | * |
| 9 | * @package Advanced_Ads_Group |
| 10 | * @author Thomas Maier <support@wpadvancedads.com> |
| 11 | * @license GPL-2.0+ |
| 12 | * @link https://wpadvancedads.com |
| 13 | * @copyright 2014 Thomas Maier, Advanced Ads GmbH |
| 14 | */ |
| 15 | class Advanced_Ads_Group { |
| 16 | |
| 17 | /** |
| 18 | * Default ad group weight |
| 19 | * previously called MAX_AD_GROUP_WEIGHT |
| 20 | */ |
| 21 | const MAX_AD_GROUP_DEFAULT_WEIGHT = 10; |
| 22 | |
| 23 | /** |
| 24 | * Term id of this ad group |
| 25 | */ |
| 26 | public $id = 0; |
| 27 | |
| 28 | /** |
| 29 | * Group type |
| 30 | * |
| 31 | * @since 1.4.8 |
| 32 | */ |
| 33 | public $type = 'default'; |
| 34 | |
| 35 | /** |
| 36 | * Name of the taxonomy |
| 37 | */ |
| 38 | public $taxonomy = ''; |
| 39 | |
| 40 | /** |
| 41 | * Post type of the ads |
| 42 | */ |
| 43 | protected $post_type = ''; |
| 44 | |
| 45 | /** |
| 46 | * The current loaded ad |
| 47 | */ |
| 48 | protected $current_ad = ''; |
| 49 | |
| 50 | /** |
| 51 | * The name of the term |
| 52 | */ |
| 53 | public $name = ''; |
| 54 | |
| 55 | /** |
| 56 | * The slug of the term |
| 57 | */ |
| 58 | public $slug = ''; |
| 59 | |
| 60 | /** |
| 61 | * The description of the term |
| 62 | */ |
| 63 | public $description = ''; |
| 64 | |
| 65 | /** |
| 66 | * Number of ads to display in the group block |
| 67 | */ |
| 68 | public $ad_count = 1; |
| 69 | |
| 70 | /**$slug |
| 71 | * contains other options |
| 72 | * |
| 73 | * @since 1.5.5 |
| 74 | */ |
| 75 | public $options = []; |
| 76 | |
| 77 | /** |
| 78 | * Optional arguments passed to ads. |
| 79 | * |
| 80 | * @var array |
| 81 | */ |
| 82 | public $ad_args = []; |
| 83 | |
| 84 | /** |
| 85 | * Containing ad weights |
| 86 | */ |
| 87 | private $ad_weights; |
| 88 | |
| 89 | /** |
| 90 | * Array with post type objects (ads) |
| 91 | */ |
| 92 | private $ads = false; |
| 93 | |
| 94 | /** |
| 95 | * Multidimensional array contains information about the wrapper |
| 96 | * each possible html attribute is an array with possible multiple elements. |
| 97 | * |
| 98 | * @since untagged |
| 99 | */ |
| 100 | public $wrapper = []; |
| 101 | |
| 102 | /** |
| 103 | * Displayed above the ad. |
| 104 | */ |
| 105 | public $label = ''; |
| 106 | |
| 107 | /** |
| 108 | * Whether this group is in a head placement. |
| 109 | * |
| 110 | * @var bool |
| 111 | */ |
| 112 | private $is_head_placement; |
| 113 | |
| 114 | /** |
| 115 | * True, if this is an Advanced Ads Ad Group |
| 116 | * |
| 117 | * @var bool |
| 118 | */ |
| 119 | public $is_group = false; |
| 120 | |
| 121 | /** |
| 122 | * The decorated WP_Term object. |
| 123 | * |
| 124 | * @var \WP_Term |
| 125 | */ |
| 126 | private $group; |
| 127 | |
| 128 | /** |
| 129 | * Init ad group object |
| 130 | * |
| 131 | * @param int|WP_Term $group Either id of the ad group or term object. |
| 132 | * @param iterable $ad_args Optional arguments passed to ads. |
| 133 | * |
| 134 | * @since 1.0.0 |
| 135 | */ |
| 136 | public function __construct( $group, iterable $ad_args = [] ) { |
| 137 | $this->taxonomy = Entities::TAXONOMY_AD_GROUP; |
| 138 | |
| 139 | $this->group = get_term( $group, $this->taxonomy ); |
| 140 | if ( $this->group === null || is_wp_error( $this->group ) ) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | $this->is_group = true; |
| 145 | $this->id = $this->group->term_id; |
| 146 | $this->name = $this->group->name; |
| 147 | $this->slug = $this->group->slug; |
| 148 | $this->description = $this->group->description; |
| 149 | $this->post_type = Entities::POST_TYPE_AD; |
| 150 | $this->ad_args = $ad_args; |
| 151 | $this->is_head_placement = isset( $this->ad_args['placement_type'] ) && $this->ad_args['placement_type'] === 'header'; |
| 152 | $this->ad_args['is_top_level'] = ! isset( $this->ad_args['is_top_level'] ); |
| 153 | |
| 154 | $this->load_additional_attributes(); |
| 155 | |
| 156 | if ( ! $this->is_head_placement ) { |
| 157 | $this->create_wrapper(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * If a property on the original WP_Term is requested, return it, otherwise null. |
| 163 | * |
| 164 | * @param string $name The requested property name. |
| 165 | * |
| 166 | * @return mixed|null |
| 167 | * @noinspection MagicMethodsValidityInspection -- We don't want to allow setting of properties. |
| 168 | */ |
| 169 | public function __get( string $name ) { |
| 170 | return $this->group->$name ?? null; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Whether a property is set. |
| 175 | * |
| 176 | * @param string $name The requested property name. |
| 177 | * |
| 178 | * @return bool |
| 179 | * @noinspection MagicMethodsValidityInspection -- We don't want to allow setting of properties. |
| 180 | */ |
| 181 | public function __isset( string $name ): bool { |
| 182 | return isset( $this->group->$name ); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Load additional attributes for groups that are not part of the WP terms |
| 187 | * |
| 188 | * @since 1.4.8 |
| 189 | */ |
| 190 | protected function load_additional_attributes() { |
| 191 | // -TODO should abstract (i.e. only call once per request) |
| 192 | $all_groups = get_option( 'advads-ad-groups', [] ); |
| 193 | |
| 194 | if ( ! isset( $all_groups[ $this->id ] ) || ! is_array( $all_groups[ $this->id ] ) ) { return; } |
| 195 | |
| 196 | if ( isset( $this->ad_args['change-group'] ) ) { |
| 197 | // some options was provided by the user |
| 198 | $group_data = Advanced_Ads_Utils::merge_deep_array( [ $all_groups[ $this->id ], $this->ad_args['change-group'] ] ) ; |
| 199 | } else { |
| 200 | $group_data = $all_groups[ $this->id ]; |
| 201 | } |
| 202 | |
| 203 | if ( isset( $group_data['type'] ) ) { |
| 204 | $this->type = $group_data['type']; |
| 205 | } |
| 206 | |
| 207 | // get ad count; default is 1 |
| 208 | if ( isset( $group_data['ad_count'] ) ) { |
| 209 | $this->ad_count = $group_data['ad_count'] === 'all' ? 'all' : (int) $group_data['ad_count']; |
| 210 | } |
| 211 | |
| 212 | if ( isset( $group_data['options'] ) ) { |
| 213 | $this->options = isset( $group_data['options'] ) ? $group_data['options'] : []; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Control the output of the group by type and amount of ads |
| 219 | * |
| 220 | * @param array $ordered_ad_ids Ordered ids of the ads that belong to the group. |
| 221 | * |
| 222 | * @return string $output output of ad(s) by ad |
| 223 | * @since 1.4.8 |
| 224 | */ |
| 225 | public function output( $ordered_ad_ids ) { |
| 226 | if ( empty( $ordered_ad_ids ) ) { |
| 227 | return ''; |
| 228 | } |
| 229 | |
| 230 | // load the ad output |
| 231 | $output = []; |
| 232 | $ads_displayed = 0; |
| 233 | $ad_count = apply_filters( 'advanced-ads-group-ad-count', $this->ad_count, $this ); |
| 234 | |
| 235 | $ad_select = Advanced_Ads_Select::get_instance(); |
| 236 | |
| 237 | // the Advanced_Ads_Ad obj can access this info |
| 238 | $this->ad_args['group_info'] = [ |
| 239 | 'id' => $this->id, |
| 240 | 'name' => $this->name, |
| 241 | 'type' => $this->type, |
| 242 | 'refresh_enabled' => ! empty( $this->options['refresh']['enabled'] ), |
| 243 | ]; |
| 244 | $this->ad_args['ad_label'] = 'disabled'; |
| 245 | |
| 246 | if( is_array( $ordered_ad_ids ) ){ |
| 247 | foreach ( $ordered_ad_ids as $_ad_id ) { |
| 248 | $this->ad_args['group_info']['ads_displayed'] = $ads_displayed; |
| 249 | |
| 250 | // load the ad output |
| 251 | $ad = $ad_select->get_ad_by_method( $_ad_id, Advanced_Ads_Select::AD, $this->ad_args ); |
| 252 | |
| 253 | if ( ! empty( $ad ) ) { |
| 254 | $output[] = $ad; |
| 255 | $ads_displayed++; |
| 256 | // break the loop when maximum ads are reached |
| 257 | if( $ads_displayed === $ad_count ) { |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | $global_output = ! isset( $this->ad_args['global_output'] ) || $this->ad_args['global_output']; |
| 265 | if ( $global_output ) { |
| 266 | // add the group to the global output array |
| 267 | $advads = Advanced_Ads::get_instance(); |
| 268 | $advads->current_ads[] = ['type' => 'group', 'id' => $this->id, 'title' => $this->name]; |
| 269 | } |
| 270 | |
| 271 | if ( $output === [] || ! is_array( $output ) ) { |
| 272 | return ''; |
| 273 | } |
| 274 | |
| 275 | // filter grouped ads output |
| 276 | $output_array = apply_filters( 'advanced-ads-group-output-array', $output, $this ); |
| 277 | |
| 278 | // make sure the right format comes through the filter |
| 279 | if ( $output_array === [] || ! is_array( $output_array ) ) { |
| 280 | return ''; |
| 281 | } |
| 282 | |
| 283 | $output_string = implode( '', $output_array ); |
| 284 | |
| 285 | // Adds inline css to the wrapper. |
| 286 | if ( ! empty( $this->ad_args['inline-css'] ) && $this->ad_args['is_top_level'] ) { |
| 287 | $inline_css = new Advanced_Ads_Inline_Css(); |
| 288 | $this->wrapper = $inline_css->add_css( $this->wrapper, $this->ad_args['inline-css'], $global_output ); |
| 289 | } |
| 290 | |
| 291 | if ( ! $this->is_head_placement && $this->wrapper !== [] ) { |
| 292 | $output_string = '<div' . Advanced_Ads_Utils::build_html_attributes( $this->wrapper ) . '>' |
| 293 | . $this->label |
| 294 | . apply_filters( 'advanced-ads-output-wrapper-before-content-group', '', $this ) |
| 295 | . $output_string |
| 296 | . apply_filters( 'advanced-ads-output-wrapper-after-content-group', '', $this ) |
| 297 | . '</div>'; |
| 298 | } |
| 299 | |
| 300 | if ( ! empty( $this->ad_args['is_top_level'] ) && ! empty( $this->ad_args['placement_clearfix'] ) ) { |
| 301 | $output_string .= '<br style="clear: both; display: block; float: none;"/>'; |
| 302 | } |
| 303 | |
| 304 | // filter final group output |
| 305 | return apply_filters( 'advanced-ads-group-output', $output_string, $this ); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Get ordered ids of the ads that belong to the group |
| 310 | * |
| 311 | * @return array |
| 312 | */ |
| 313 | public function get_ordered_ad_ids() { |
| 314 | // load ads |
| 315 | $ads = $this->load_all_ads(); |
| 316 | if ( ! is_array( $ads ) ) { |
| 317 | return []; |
| 318 | } |
| 319 | |
| 320 | // get ad weights serving as an order here |
| 321 | $weights = $this->get_ad_weights( array_keys( $ads ) ); |
| 322 | $ad_ids = wp_list_pluck( $ads, 'ID' ); |
| 323 | |
| 324 | // remove ads with 0 ad weight and unavailable ads (e.g. drafts). |
| 325 | foreach ( $weights as $ad_id => $ad_weight ) { |
| 326 | if ( $ad_weight === 0 || ! in_array( $ad_id, $ad_ids, true ) ) { |
| 327 | unset( $weights[ $ad_id ] ); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | // order ads based on group type |
| 332 | if ( $this->type === 'ordered' ) { |
| 333 | $ordered_ad_ids = $this->shuffle_ordered_ads( $weights ); |
| 334 | } else { // default |
| 335 | $ordered_ad_ids = $this->shuffle_ads( $ads, $weights ); |
| 336 | } |
| 337 | |
| 338 | return apply_filters( 'advanced-ads-group-output-ad-ids', $ordered_ad_ids, $this->type, $ads, $weights, $this ); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Return all ads from this group |
| 343 | * |
| 344 | * @since 1.0.0 |
| 345 | */ |
| 346 | public function get_all_ads() { |
| 347 | return $this->load_all_ads(); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Load all public ads for this group |
| 352 | * |
| 353 | * @since 1.0.0 |
| 354 | * @update 1.1.0 load only public ads |
| 355 | * @update allow to cache groups for few minutes |
| 356 | * |
| 357 | * @return bool|WP_Post[] $ads array with ad (post) objects |
| 358 | */ |
| 359 | private function load_all_ads() { |
| 360 | |
| 361 | if ( ! $this->id ) { |
| 362 | return []; |
| 363 | } |
| 364 | |
| 365 | if ( false !== $this->ads ) { |
| 366 | return $this->ads; |
| 367 | } |
| 368 | |
| 369 | // Much more complex than needed: one of the three queries is not needed and the last query gets slow quiet fast. |
| 370 | $args = [ |
| 371 | 'post_type' => $this->post_type, |
| 372 | 'post_status' => 'publish', |
| 373 | 'posts_per_page' => -1, |
| 374 | 'taxonomy' => $this->taxonomy, |
| 375 | 'term' => $this->slug, |
| 376 | 'orderby' => 'id', // Might want to avoid sorting as not needed for most calls and fast in PHP; slight I/O blocking concern. |
| 377 | ]; |
| 378 | |
| 379 | $ads = new WP_Query( $args ); |
| 380 | |
| 381 | if ( $ads->have_posts() ) { |
| 382 | $this->ads = $this->add_post_ids( $ads->posts ); |
| 383 | } else { |
| 384 | $this->ads = []; |
| 385 | } |
| 386 | |
| 387 | return $this->ads; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Use post ids as keys for ad array |
| 392 | * |
| 393 | * @since 1.0.0 |
| 394 | * @param array $ads array with post objects. |
| 395 | * @return array $ads array with post objects with post id as their key. |
| 396 | */ |
| 397 | private function add_post_ids( array $ads ) { |
| 398 | return array_reduce( |
| 399 | $ads, |
| 400 | function( $ads, $ad ) { |
| 401 | $ads[ $ad->ID ] = $ad; |
| 402 | |
| 403 | return $ads; |
| 404 | }, |
| 405 | [] |
| 406 | ); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Shuffle ads based on ad weight. |
| 411 | * |
| 412 | * @param array $ads ad objects. |
| 413 | * @param array $weights ad weights, indexed by ad id. |
| 414 | * |
| 415 | * @return array |
| 416 | * @since 1.0.0 |
| 417 | */ |
| 418 | public function shuffle_ads( $ads, $weights ) { |
| 419 | // get a random ad for every ad there is |
| 420 | $shuffled_ads = []; |
| 421 | // while non-zero weights are set select random next |
| 422 | // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition -- prevents code duplication. |
| 423 | while ( null !== ( $random_ad_id = $this->get_random_ad_by_weight( $weights ) ) ) { |
| 424 | // remove chosen ad from weights array |
| 425 | unset( $weights[ $random_ad_id ] ); |
| 426 | // put random ad into shuffled array |
| 427 | if ( ! empty( $ads[ $random_ad_id ] ) ) { |
| 428 | $shuffled_ads[] = $random_ad_id; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | return $shuffled_ads; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Shuffle ads that have the same width. |
| 437 | * |
| 438 | * @since untagged |
| 439 | * @param array $weights Array of $ad_id => weight pairs. |
| 440 | * @return array $ordered_ad_ids Ordered ad ids. |
| 441 | */ |
| 442 | public function shuffle_ordered_ads( array $weights ) { |
| 443 | // order to highest weight first |
| 444 | arsort( $weights ); |
| 445 | $ordered_ad_ids = array_keys( $weights ); |
| 446 | |
| 447 | $weights = array_values( $weights ); |
| 448 | $count = count( $weights ); |
| 449 | $pos = 0; |
| 450 | for ( $i = 1; $i <= $count; $i++ ) { |
| 451 | if ( $i == $count || $weights[ $i ] !== $weights[ $i - 1] ) { |
| 452 | $slice_len = $i - $pos; |
| 453 | if ( $slice_len !== 1 ) { |
| 454 | $shuffled = array_slice( $ordered_ad_ids, $pos, $slice_len ); |
| 455 | shuffle ( $shuffled ); |
| 456 | // Replace the unshuffled chunk with the shuffled one. |
| 457 | array_splice( $ordered_ad_ids, $pos, $slice_len, $shuffled ); |
| 458 | } |
| 459 | $pos = $i; |
| 460 | } |
| 461 | } |
| 462 | return $ordered_ad_ids; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Get random ad by ad weight. |
| 467 | * |
| 468 | * @since 1.0.0 |
| 469 | * @param array $ad_weights Indexed by ad_id [int $ad_id => int $weight]. |
| 470 | * @source applied with fix for order http://stackoverflow.com/a/11872928/904614 |
| 471 | * |
| 472 | * @return null|int |
| 473 | */ |
| 474 | private function get_random_ad_by_weight(array $ad_weights) { |
| 475 | |
| 476 | // use maximum ad weight for ads without this |
| 477 | // ads might have a weight of zero (0); to avoid mt_rand fail assume that at least 1 is set. |
| 478 | $max = array_sum( $ad_weights ); |
| 479 | if ( $max < 1 ) { |
| 480 | return null; |
| 481 | } |
| 482 | |
| 483 | $rand = mt_rand( 1, $max ); |
| 484 | foreach ( $ad_weights as $ad_id => $weight ) { |
| 485 | $rand -= $weight; |
| 486 | if ( $rand <= 0 ) { |
| 487 | return $ad_id; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | return null; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Get weights of ads in this group |
| 496 | * |
| 497 | * @param array $ad_ids Ids of ads assigned to this group. |
| 498 | * |
| 499 | * @return array |
| 500 | */ |
| 501 | public function get_ad_weights( $ad_ids = [] ) { |
| 502 | if ( is_array( $this->ad_weights ) ) { |
| 503 | return $this->ad_weights; |
| 504 | } |
| 505 | |
| 506 | $weights = get_option( 'advads-ad-weights', [] ); |
| 507 | $this->ad_weights = []; |
| 508 | if ( array_key_exists( $this->id, $weights ) ) { |
| 509 | $this->ad_weights = $weights[ $this->id ]; |
| 510 | } |
| 511 | asort( $this->ad_weights ); |
| 512 | |
| 513 | // add ads whose weight has not yet been saved with the default value. |
| 514 | foreach ( $ad_ids as $ad_id ) { |
| 515 | if ( ! array_key_exists( $ad_id, $this->ad_weights ) ) { |
| 516 | $this->ad_weights[ $ad_id ] = self::MAX_AD_GROUP_DEFAULT_WEIGHT; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | return $this->ad_weights; |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Save ad group information that are not included in terms or ad weight |
| 525 | * |
| 526 | * @since 1.4.8 |
| 527 | * @param arr $args group arguments |
| 528 | */ |
| 529 | public function save($args = []) { |
| 530 | |
| 531 | $defaults = [ 'type' => 'default', 'ad_count' => 1, 'options' => [] ]; |
| 532 | $args = wp_parse_args($args, $defaults); |
| 533 | |
| 534 | // get global ad group option |
| 535 | $groups = get_option( 'advads-ad-groups', [] ); |
| 536 | |
| 537 | $groups[$this->id] = $args; |
| 538 | |
| 539 | update_option( 'advads-ad-groups', $groups ); |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Delete all the ad weights for a group by id |
| 544 | * |
| 545 | * @since 1.0.0 |
| 546 | */ |
| 547 | public static function delete_ad_weights($group_id){ |
| 548 | $all_weights = get_option( 'advads-ad-weights', [] ); |
| 549 | if ($all_weights && isset($all_weights[$group_id])){ |
| 550 | unset($all_weights[$group_id]); |
| 551 | update_option( 'advads-ad-weights', $all_weights ); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Create a wrapper to place around the group. |
| 557 | */ |
| 558 | private function create_wrapper() { |
| 559 | $this->wrapper = []; |
| 560 | |
| 561 | if ( $this->ad_args['is_top_level'] ) { |
| 562 | // Add label. |
| 563 | $placement_state = isset( $this->ad_args['ad_label'] ) ? $this->ad_args['ad_label'] : 'default'; |
| 564 | $this->label = Advanced_Ads::get_instance()->get_label( $placement_state ); |
| 565 | |
| 566 | // Add placement class. |
| 567 | if ( ! empty( $this->ad_args['output']['class'] ) && is_array( $this->ad_args['output']['class'] ) ) { |
| 568 | $this->wrapper['class'] = $this->ad_args['output']['class']; |
| 569 | } |
| 570 | |
| 571 | // ad Health Tool add class wrapper |
| 572 | if ( WordPress::user_can('advanced_ads_edit_ads') ) { |
| 573 | $frontend_prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix(); |
| 574 | $this->wrapper['class'][] = $frontend_prefix . 'highlighted-wrappers'; |
| 575 | } |
| 576 | |
| 577 | if ( isset( $this->ad_args['output']['wrapper_attrs'] ) && is_array( $this->ad_args['output']['wrapper_attrs'] ) ) { |
| 578 | foreach ( $this->ad_args['output']['wrapper_attrs'] as $key => $value ) { |
| 579 | $this->wrapper[$key] = $value; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | if ( ! empty( $this->ad_args['placement_position'] ) ) { |
| 584 | switch ( $this->ad_args['placement_position'] ) { |
| 585 | case 'left' : |
| 586 | $this->wrapper['style']['float'] = 'left'; |
| 587 | break; |
| 588 | case 'right' : |
| 589 | $this->wrapper['style']['float'] = 'right'; |
| 590 | break; |
| 591 | case 'center' : |
| 592 | // We don't know whether the 'add_wrapper_sizes' option exists and width is set. |
| 593 | $this->wrapper['style']['text-align'] = 'center'; |
| 594 | break; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | $this->wrapper = (array) apply_filters( 'advanced-ads-output-wrapper-options-group', $this->wrapper, $this ); |
| 600 | |
| 601 | if ( ( $this->wrapper !== [] || $this->label ) && ! isset( $this->wrapper['id'] ) ) { |
| 602 | $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix(); |
| 603 | $this->wrapper['id'] = $prefix . mt_rand(); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Calculate the number of available weights for a group depending on |
| 609 | * number of ads and default value. |
| 610 | * |
| 611 | * @param int $num_ads Number of ads in the group. |
| 612 | * @since 1.8.22 |
| 613 | * |
| 614 | * @return max weight used in group settings |
| 615 | */ |
| 616 | public static function get_max_ad_weight( $num_ads = 1 ){ |
| 617 | |
| 618 | // use default if lower than default. |
| 619 | $num_ads = absint( $num_ads ); |
| 620 | |
| 621 | // use number of ads or max ad weight value, whatever is higher |
| 622 | $max_weight = $num_ads < self::MAX_AD_GROUP_DEFAULT_WEIGHT ? self::MAX_AD_GROUP_DEFAULT_WEIGHT : $num_ads; |
| 623 | |
| 624 | // allow users to manipulate max ad weight |
| 625 | return apply_filters( 'advanced-ads-max-ad-weight', $max_weight, $num_ads ); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Get group hints. |
| 630 | * |
| 631 | * @param Advanced_Ads_Group $group The group object. |
| 632 | * |
| 633 | * @return string[] Group hints (escaped strings). |
| 634 | */ |
| 635 | public static function get_hints( Advanced_Ads_Group $group ) { |
| 636 | $hints = []; |
| 637 | |
| 638 | if ( |
| 639 | ! Advanced_Ads_Checks::cache() |
| 640 | || count( $group->get_all_ads() ) < 2 |
| 641 | ) { |
| 642 | return $hints; |
| 643 | } |
| 644 | |
| 645 | if ( ! class_exists( 'Advanced_Ads_Pro' ) ) { |
| 646 | $installed_plugins = get_plugins(); |
| 647 | |
| 648 | if ( isset( $installed_plugins['advanced-ads-pro/advanced-ads-pro.php'] ) ) { |
| 649 | $link = wp_nonce_url( 'plugins.php?action=activate&plugin=advanced-ads-pro/advanced-ads-pro.php', 'activate-plugin_advanced-ads-pro/advanced-ads-pro.php' ); |
| 650 | $link_title = __( 'Activate now', 'advanced-ads' ); |
| 651 | } else { |
| 652 | $link = 'https://wpadvancedads.com/add-ons/advanced-ads-pro/?utm_source=advanced-ads&utm_medium=link&utm_campaign=groups-CB'; |
| 653 | $link_title = __( 'Get this add-on', 'advanced-ads' ); |
| 654 | } |
| 655 | |
| 656 | $hints[] = sprintf( |
| 657 | wp_kses( |
| 658 | // translators: %1$s is an URL, %2$s is a URL text |
| 659 | __( 'It seems that a caching plugin is activated. Your ads might not rotate properly. The cache busting in Advanced Ads Pro will solve that. <a href="%1$s" target="_blank">%2$s.</a>', 'advanced-ads' ), |
| 660 | [ |
| 661 | 'a' => [ |
| 662 | 'href' => [], |
| 663 | 'target' => [], |
| 664 | ], |
| 665 | ] |
| 666 | ), |
| 667 | $link, |
| 668 | $link_title |
| 669 | ); |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * Allows to add new hints. |
| 674 | * |
| 675 | * @param string[] $hints Existing hints (escaped strings). |
| 676 | * @param Advanced_Ads_Group $group The group object. |
| 677 | */ |
| 678 | return apply_filters( 'advanced-ads-group-hints', $hints, $group ); |
| 679 | } |
| 680 | |
| 681 | |
| 682 | } |
| 683 |