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
661 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 | * Load additional attributes for groups that are not part of the WP terms |
| 164 | * |
| 165 | * @since 1.4.8 |
| 166 | */ |
| 167 | protected function load_additional_attributes() { |
| 168 | // -TODO should abstract (i.e. only call once per request) |
| 169 | $all_groups = get_option( 'advads-ad-groups', [] ); |
| 170 | |
| 171 | if ( ! isset( $all_groups[ $this->id ] ) || ! is_array( $all_groups[ $this->id ] ) ) { return; } |
| 172 | |
| 173 | if ( isset( $this->ad_args['change-group'] ) ) { |
| 174 | // some options was provided by the user |
| 175 | $group_data = Advanced_Ads_Utils::merge_deep_array( [ $all_groups[ $this->id ], $this->ad_args['change-group'] ] ) ; |
| 176 | } else { |
| 177 | $group_data = $all_groups[ $this->id ]; |
| 178 | } |
| 179 | |
| 180 | if ( isset( $group_data['type'] ) ) { |
| 181 | $this->type = $group_data['type']; |
| 182 | } |
| 183 | |
| 184 | // get ad count; default is 1 |
| 185 | if ( isset( $group_data['ad_count'] ) ) { |
| 186 | $this->ad_count = $group_data['ad_count'] === 'all' ? 'all' : (int) $group_data['ad_count']; |
| 187 | } |
| 188 | |
| 189 | if ( isset( $group_data['options'] ) ) { |
| 190 | $this->options = isset( $group_data['options'] ) ? $group_data['options'] : []; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Control the output of the group by type and amount of ads |
| 196 | * |
| 197 | * @param array $ordered_ad_ids Ordered ids of the ads that belong to the group. |
| 198 | * |
| 199 | * @return string $output output of ad(s) by ad |
| 200 | * @since 1.4.8 |
| 201 | */ |
| 202 | public function output( $ordered_ad_ids ) { |
| 203 | if ( empty( $ordered_ad_ids ) ) { |
| 204 | return ''; |
| 205 | } |
| 206 | |
| 207 | // load the ad output |
| 208 | $output = []; |
| 209 | $ads_displayed = 0; |
| 210 | $ad_count = apply_filters( 'advanced-ads-group-ad-count', $this->ad_count, $this ); |
| 211 | |
| 212 | $ad_select = Advanced_Ads_Select::get_instance(); |
| 213 | |
| 214 | // the Advanced_Ads_Ad obj can access this info |
| 215 | $this->ad_args['group_info'] = [ |
| 216 | 'id' => $this->id, |
| 217 | 'name' => $this->name, |
| 218 | 'type' => $this->type, |
| 219 | 'refresh_enabled' => ! empty( $this->options['refresh']['enabled'] ), |
| 220 | ]; |
| 221 | $this->ad_args['ad_label'] = 'disabled'; |
| 222 | |
| 223 | if( is_array( $ordered_ad_ids ) ){ |
| 224 | foreach ( $ordered_ad_ids as $_ad_id ) { |
| 225 | $this->ad_args['group_info']['ads_displayed'] = $ads_displayed; |
| 226 | |
| 227 | // load the ad object |
| 228 | $ad = $ad_select->get_ad_by_method( $_ad_id, Advanced_Ads_Select::AD, $this->ad_args ); |
| 229 | |
| 230 | if ( $ad !== null ) { |
| 231 | $output[] = $ad; |
| 232 | $ads_displayed++; |
| 233 | // break the loop when maximum ads are reached |
| 234 | if( $ads_displayed === $ad_count ) { |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | $global_output = ! isset( $this->ad_args['global_output'] ) || $this->ad_args['global_output']; |
| 242 | if ( $global_output ) { |
| 243 | // add the group to the global output array |
| 244 | $advads = Advanced_Ads::get_instance(); |
| 245 | $advads->current_ads[] = ['type' => 'group', 'id' => $this->id, 'title' => $this->name]; |
| 246 | } |
| 247 | |
| 248 | if ( $output === [] || ! is_array( $output ) ) { |
| 249 | return ''; |
| 250 | } |
| 251 | |
| 252 | // filter grouped ads output |
| 253 | $output_array = apply_filters( 'advanced-ads-group-output-array', $output, $this ); |
| 254 | |
| 255 | // make sure the right format comes through the filter |
| 256 | if ( $output_array === [] || ! is_array( $output_array ) ) { |
| 257 | return ''; |
| 258 | } |
| 259 | |
| 260 | $output_string = implode( '', $output_array ); |
| 261 | |
| 262 | // Adds inline css to the wrapper. |
| 263 | if ( ! empty( $this->ad_args['inline-css'] ) && $this->ad_args['is_top_level'] ) { |
| 264 | $inline_css = new Advanced_Ads_Inline_Css(); |
| 265 | $this->wrapper = $inline_css->add_css( $this->wrapper, $this->ad_args['inline-css'], $global_output ); |
| 266 | } |
| 267 | |
| 268 | if ( ! $this->is_head_placement && $this->wrapper !== [] ) { |
| 269 | $output_string = '<div' . Advanced_Ads_Utils::build_html_attributes( $this->wrapper ) . '>' |
| 270 | . $this->label |
| 271 | . apply_filters( 'advanced-ads-output-wrapper-before-content-group', '', $this ) |
| 272 | . $output_string |
| 273 | . apply_filters( 'advanced-ads-output-wrapper-after-content-group', '', $this ) |
| 274 | . '</div>'; |
| 275 | } |
| 276 | |
| 277 | if ( ! empty( $this->ad_args['is_top_level'] ) && ! empty( $this->ad_args['placement_clearfix'] ) ) { |
| 278 | $output_string .= '<br style="clear: both; display: block; float: none;"/>'; |
| 279 | } |
| 280 | |
| 281 | // filter final group output |
| 282 | return apply_filters( 'advanced-ads-group-output', $output_string, $this ); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Get ordered ids of the ads that belong to the group |
| 287 | * |
| 288 | * @return array |
| 289 | */ |
| 290 | public function get_ordered_ad_ids() { |
| 291 | // load ads |
| 292 | $ads = $this->load_all_ads(); |
| 293 | if ( ! is_array( $ads ) ) { |
| 294 | return []; |
| 295 | } |
| 296 | |
| 297 | // get ad weights serving as an order here |
| 298 | $weights = $this->get_ad_weights( array_keys( $ads ) ); |
| 299 | $ad_ids = wp_list_pluck( $ads, 'ID' ); |
| 300 | |
| 301 | // remove ads with 0 ad weight and unavailable ads (e.g. drafts). |
| 302 | foreach ( $weights as $ad_id => $ad_weight ) { |
| 303 | if ( $ad_weight === 0 || ! in_array( $ad_id, $ad_ids, true ) ) { |
| 304 | unset( $weights[ $ad_id ] ); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // order ads based on group type |
| 309 | if ( $this->type === 'ordered' ) { |
| 310 | $ordered_ad_ids = $this->shuffle_ordered_ads( $weights ); |
| 311 | } else { // default |
| 312 | $ordered_ad_ids = $this->shuffle_ads( $ads, $weights ); |
| 313 | } |
| 314 | |
| 315 | return apply_filters( 'advanced-ads-group-output-ad-ids', $ordered_ad_ids, $this->type, $ads, $weights, $this ); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Return all ads from this group |
| 320 | * |
| 321 | * @since 1.0.0 |
| 322 | */ |
| 323 | public function get_all_ads() { |
| 324 | if ( count( $this->ads ) > 0 ) { |
| 325 | return $this->ads; } |
| 326 | else { |
| 327 | return $this->load_all_ads(); } |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Load all public ads for this group |
| 332 | * |
| 333 | * @since 1.0.0 |
| 334 | * @update 1.1.0 load only public ads |
| 335 | * @update allow to cache groups for few minutes |
| 336 | * |
| 337 | * @return WP_Post[] $ads array with ad (post) objects |
| 338 | */ |
| 339 | private function load_all_ads() { |
| 340 | |
| 341 | if ( ! $this->id ) { |
| 342 | return []; |
| 343 | } |
| 344 | |
| 345 | // reset |
| 346 | $this->ads = []; |
| 347 | |
| 348 | // much more complex than needed: one of the three queries is not needed and the last query gets slow quiet fast |
| 349 | $args = [ |
| 350 | 'post_type' => $this->post_type, |
| 351 | 'post_status' => 'publish', |
| 352 | 'posts_per_page' => -1, |
| 353 | 'taxonomy' => $this->taxonomy, |
| 354 | 'term' => $this->slug, |
| 355 | 'orderby' => 'id' // might want to avoid sorting as not needed for most calls and fast in PHP; slight I/O blocking concern |
| 356 | ]; |
| 357 | |
| 358 | $found = false; |
| 359 | $key = 'ad_group_all_ads_' . $this->post_type . '_' . $this->taxonomy . '_' . $this->slug; |
| 360 | $ads = wp_cache_get( $key, Advanced_Ads_Model::OBJECT_CACHE_GROUP, false, $found ); |
| 361 | if ( $found ) { |
| 362 | $this->ads = $ads; |
| 363 | } else { |
| 364 | $ads = new WP_Query( $args ); |
| 365 | |
| 366 | if ( $ads->have_posts() ) { |
| 367 | $this->ads = $this->add_post_ids( $ads->posts ); |
| 368 | } |
| 369 | wp_cache_set( $key, $this->ads, Advanced_Ads_Model::OBJECT_CACHE_GROUP, Advanced_Ads_Model::OBJECT_CACHE_TTL); |
| 370 | } |
| 371 | |
| 372 | return $this->ads; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Use post ids as keys for ad array |
| 377 | * |
| 378 | * @since 1.0.0 |
| 379 | * @param arr $ads array with post objects |
| 380 | * @return arr $ads array with post objects with post id as their key |
| 381 | * @todo check, if there isn’t a WP function for this already |
| 382 | */ |
| 383 | private function add_post_ids(array $ads){ |
| 384 | |
| 385 | $ads_with_id = []; |
| 386 | foreach ( $ads as $_ad ){ |
| 387 | $ads_with_id[$_ad->ID] = $_ad; |
| 388 | } |
| 389 | |
| 390 | return $ads_with_id; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Shuffle ads based on ad weight. |
| 395 | * |
| 396 | * @param array $ads ad objects. |
| 397 | * @param array $weights ad weights, indexed by ad id. |
| 398 | * |
| 399 | * @return array |
| 400 | * @since 1.0.0 |
| 401 | */ |
| 402 | public function shuffle_ads( $ads, $weights ) { |
| 403 | // get a random ad for every ad there is |
| 404 | $shuffled_ads = []; |
| 405 | // while non-zero weights are set select random next |
| 406 | // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition -- prevents code duplication. |
| 407 | while ( null !== ( $random_ad_id = $this->get_random_ad_by_weight( $weights ) ) ) { |
| 408 | // remove chosen ad from weights array |
| 409 | unset( $weights[ $random_ad_id ] ); |
| 410 | // put random ad into shuffled array |
| 411 | if ( ! empty( $ads[ $random_ad_id ] ) ) { |
| 412 | $shuffled_ads[] = $random_ad_id; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return $shuffled_ads; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Shuffle ads that have the same width. |
| 421 | * |
| 422 | * @since untagged |
| 423 | * @param array $weights Array of $ad_id => weight pairs. |
| 424 | * @return array $ordered_ad_ids Ordered ad ids. |
| 425 | */ |
| 426 | public function shuffle_ordered_ads( array $weights ) { |
| 427 | // order to highest weight first |
| 428 | arsort( $weights ); |
| 429 | $ordered_ad_ids = array_keys( $weights ); |
| 430 | |
| 431 | $weights = array_values( $weights ); |
| 432 | $count = count( $weights ); |
| 433 | $pos = 0; |
| 434 | for ( $i = 1; $i <= $count; $i++ ) { |
| 435 | if ( $i == $count || $weights[ $i ] !== $weights[ $i - 1] ) { |
| 436 | $slice_len = $i - $pos; |
| 437 | if ( $slice_len !== 1 ) { |
| 438 | $shuffled = array_slice( $ordered_ad_ids, $pos, $slice_len ); |
| 439 | shuffle ( $shuffled ); |
| 440 | // Replace the unshuffled chunk with the shuffled one. |
| 441 | array_splice( $ordered_ad_ids, $pos, $slice_len, $shuffled ); |
| 442 | } |
| 443 | $pos = $i; |
| 444 | } |
| 445 | } |
| 446 | return $ordered_ad_ids; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Get random ad by ad weight. |
| 451 | * |
| 452 | * @since 1.0.0 |
| 453 | * @param array $ad_weights Indexed by ad_id [int $ad_id => int $weight]. |
| 454 | * @source applied with fix for order http://stackoverflow.com/a/11872928/904614 |
| 455 | * |
| 456 | * @return null|int |
| 457 | */ |
| 458 | private function get_random_ad_by_weight(array $ad_weights) { |
| 459 | |
| 460 | // use maximum ad weight for ads without this |
| 461 | // ads might have a weight of zero (0); to avoid mt_rand fail assume that at least 1 is set. |
| 462 | $max = array_sum( $ad_weights ); |
| 463 | if ( $max < 1 ) { |
| 464 | return null; |
| 465 | } |
| 466 | |
| 467 | $rand = mt_rand( 1, $max ); |
| 468 | foreach ( $ad_weights as $ad_id => $weight ) { |
| 469 | $rand -= $weight; |
| 470 | if ( $rand <= 0 ) { |
| 471 | return $ad_id; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | return null; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Get weights of ads in this group |
| 480 | * |
| 481 | * @param array $ad_ids Ids of ads assigned to this group. |
| 482 | * |
| 483 | * @return array |
| 484 | */ |
| 485 | public function get_ad_weights( $ad_ids = [] ) { |
| 486 | if ( is_array( $this->ad_weights ) ) { |
| 487 | return $this->ad_weights; |
| 488 | } |
| 489 | |
| 490 | $weights = get_option( 'advads-ad-weights', [] ); |
| 491 | $this->ad_weights = []; |
| 492 | if ( array_key_exists( $this->id, $weights ) ) { |
| 493 | $this->ad_weights = $weights[ $this->id ]; |
| 494 | } |
| 495 | asort( $this->ad_weights ); |
| 496 | |
| 497 | // add ads whose weight has not yet been saved with the default value. |
| 498 | foreach ( $ad_ids as $ad_id ) { |
| 499 | if ( ! array_key_exists( $ad_id, $this->ad_weights ) ) { |
| 500 | $this->ad_weights[ $ad_id ] = self::MAX_AD_GROUP_DEFAULT_WEIGHT; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | return $this->ad_weights; |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Save ad group information that are not included in terms or ad weight |
| 509 | * |
| 510 | * @since 1.4.8 |
| 511 | * @param arr $args group arguments |
| 512 | */ |
| 513 | public function save($args = []) { |
| 514 | |
| 515 | $defaults = [ 'type' => 'default', 'ad_count' => 1, 'options' => [] ]; |
| 516 | $args = wp_parse_args($args, $defaults); |
| 517 | |
| 518 | // get global ad group option |
| 519 | $groups = get_option( 'advads-ad-groups', [] ); |
| 520 | |
| 521 | $groups[$this->id] = $args; |
| 522 | |
| 523 | update_option( 'advads-ad-groups', $groups ); |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Delete all the ad weights for a group by id |
| 528 | * |
| 529 | * @since 1.0.0 |
| 530 | */ |
| 531 | public static function delete_ad_weights($group_id){ |
| 532 | $all_weights = get_option( 'advads-ad-weights', [] ); |
| 533 | if ($all_weights && isset($all_weights[$group_id])){ |
| 534 | unset($all_weights[$group_id]); |
| 535 | update_option( 'advads-ad-weights', $all_weights ); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Create a wrapper to place around the group. |
| 541 | */ |
| 542 | private function create_wrapper() { |
| 543 | $this->wrapper = []; |
| 544 | |
| 545 | if ( $this->ad_args['is_top_level'] ) { |
| 546 | // Add label. |
| 547 | $placement_state = isset( $this->ad_args['ad_label'] ) ? $this->ad_args['ad_label'] : 'default'; |
| 548 | $this->label = Advanced_Ads::get_instance()->get_label( $placement_state ); |
| 549 | |
| 550 | // Add placement class. |
| 551 | if ( ! empty( $this->ad_args['output']['class'] ) && is_array( $this->ad_args['output']['class'] ) ) { |
| 552 | $this->wrapper['class'] = $this->ad_args['output']['class']; |
| 553 | } |
| 554 | |
| 555 | if ( isset( $this->ad_args['output']['wrapper_attrs'] ) && is_array( $this->ad_args['output']['wrapper_attrs'] ) ) { |
| 556 | foreach ( $this->ad_args['output']['wrapper_attrs'] as $key => $value ) { |
| 557 | $this->wrapper[$key] = $value; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | if ( ! empty( $this->ad_args['placement_position'] ) ) { |
| 562 | switch ( $this->ad_args['placement_position'] ) { |
| 563 | case 'left' : |
| 564 | $this->wrapper['style']['float'] = 'left'; |
| 565 | break; |
| 566 | case 'right' : |
| 567 | $this->wrapper['style']['float'] = 'right'; |
| 568 | break; |
| 569 | case 'center' : |
| 570 | // We don't know whether the 'add_wrapper_sizes' option exists and width is set. |
| 571 | $this->wrapper['style']['text-align'] = 'center'; |
| 572 | break; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | $this->wrapper = (array) apply_filters( 'advanced-ads-output-wrapper-options-group', $this->wrapper, $this ); |
| 578 | |
| 579 | if ( ( $this->wrapper !== [] || $this->label ) && ! isset( $this->wrapper['id'] ) ) { |
| 580 | $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix(); |
| 581 | $this->wrapper['id'] = $prefix . mt_rand(); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Calculate the number of available weights for a group depending on |
| 587 | * number of ads and default value. |
| 588 | * |
| 589 | * @param int $num_ads Number of ads in the group. |
| 590 | * @since 1.8.22 |
| 591 | * |
| 592 | * @return max weight used in group settings |
| 593 | */ |
| 594 | public static function get_max_ad_weight( $num_ads = 1 ){ |
| 595 | |
| 596 | // use default if lower than default. |
| 597 | $num_ads = absint( $num_ads ); |
| 598 | |
| 599 | // use number of ads or max ad weight value, whatever is higher |
| 600 | $max_weight = $num_ads < self::MAX_AD_GROUP_DEFAULT_WEIGHT ? self::MAX_AD_GROUP_DEFAULT_WEIGHT : $num_ads; |
| 601 | |
| 602 | // allow users to manipulate max ad weight |
| 603 | return apply_filters( 'advanced-ads-max-ad-weight', $max_weight, $num_ads ); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Get group hints. |
| 608 | * |
| 609 | * @param Advanced_Ads_Group $group The group object. |
| 610 | * |
| 611 | * @return string[] Group hints (escaped strings). |
| 612 | */ |
| 613 | public static function get_hints( Advanced_Ads_Group $group ) { |
| 614 | $hints = []; |
| 615 | |
| 616 | if ( |
| 617 | ! Advanced_Ads_Checks::cache() |
| 618 | || count( $group->get_all_ads() ) < 2 |
| 619 | ) { |
| 620 | return $hints; |
| 621 | } |
| 622 | |
| 623 | if ( ! class_exists( 'Advanced_Ads_Pro' ) ) { |
| 624 | $installed_plugins = get_plugins(); |
| 625 | |
| 626 | if ( isset( $installed_plugins['advanced-ads-pro/advanced-ads-pro.php'] ) ) { |
| 627 | $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' ); |
| 628 | $link_title = __( 'Activate now', 'advanced-ads' ); |
| 629 | } else { |
| 630 | $link = ADVADS_URL . 'add-ons/advanced-ads-pro/?utm_source=advanced-ads&utm_medium=link&utm_campaign=groups-CB'; |
| 631 | $link_title = __( 'Get this add-on', 'advanced-ads' ); |
| 632 | } |
| 633 | |
| 634 | $hints[] = sprintf( |
| 635 | wp_kses( |
| 636 | // translators: %1$s is an URL, %2$s is a URL text |
| 637 | __( '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' ), |
| 638 | [ |
| 639 | 'a' => [ |
| 640 | 'href' => [], |
| 641 | 'target' => [], |
| 642 | ], |
| 643 | ] |
| 644 | ), |
| 645 | $link, |
| 646 | $link_title |
| 647 | ); |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * Allows to add new hints. |
| 652 | * |
| 653 | * @param string[] $hints Existing hints (escaped strings). |
| 654 | * @param Advanced_Ads_Group $group The group object. |
| 655 | */ |
| 656 | return apply_filters( 'advanced-ads-group-hints', $hints, $group ); |
| 657 | } |
| 658 | |
| 659 | |
| 660 | } |
| 661 |