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