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