PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.17.4
Advanced Ads – Ad Manager & AdSense v1.17.4
2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 1.41.0 1.42.0 1.42.1 1.42.2 1.43.0 1.44.0 1.44.1 1.45.0 1.45.1 1.46.0 1.47.0 1.47.1 1.47.2 1.47.3 1.47.4 1.47.5 1.48.0 1.48.1 1.49.0 1.5.0 1.5.0.1 1.5.1 1.5.2 1.5.2.1 1.5.4 1.5.4.1 1.5.5 1.50.0 1.51.0 1.51.1 1.51.2 1.51.3 1.52.0 1.52.1 1.52.2 1.52.3 1.52.4 1.53.0 1.53.1 1.53.2 1.54.0 1.54.1 1.55.0 1.56.0 1.56.1 1.56.2 1.56.3 1.56.4 1.6 1.6.1 1.6.10 1.6.10.1 1.6.10.2 1.6.11 1.6.11.1 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.17.1 1.6.17.2 1.6.2 1.6.2.1 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.6.1 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.6.8.3 1.6.9 1.6.9.1 1.6.9.2 1.6.9.3 1.6.9.4 1.7 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.1.1 1.7.1.2 1.7.1.3 1.7.1.4 1.7.1.5 1.7.10 trunk 1.7.11 1.0.1 1.7.12 1.0.2 1.7.13 1.0.3 1.7.14 1.1.0 1.7.15 1.1.1 1.7.16 1.1.2 1.7.17 1.1.3 1.7.18 1.10 1.7.19 1.10.1 1.7.2 1.10.10 1.7.2.1 1.10.11 1.7.20 1.10.12 1.7.21 1.10.2 1.7.22 1.10.3 1.7.23 1.10.4 1.7.24 1.10.5 1.7.25 1.10.6 1.7.3 1.10.7 1.7.4 1.10.8 1.7.4.1 1.10.9 1.7.4.2 1.11 1.7.4.3 1.11.1 1.7.4.4 1.11.2 1.7.4.5 1.12 1.7.5 1.13 1.7.5.1 1.13.1 1.7.6 1.13.2 1.7.7 1.13.3 1.7.8 1.13.4 1.7.9 1.13.5 1.7.9.1 1.13.6 1.7.9.2 1.13.7 1.7.9.3 1.13.8 1.8 1.14 1.8.1 1.14.1 1.8.10 1.14.10 1.8.11 1.14.11 1.8.12 1.14.2 1.8.13 1.14.3 1.8.14 1.14.4 1.8.15 1.14.5 1.8.16 1.14.6 1.8.17 1.14.7 1.8.18 1.14.8 1.8.19 1.14.9 1.8.2 1.15 1.8.20 1.16 1.8.21 1.16.1 1.8.22 1.17 1.8.23 1.17.1 1.8.24 1.17.10 1.8.25 1.17.10-rc.1 1.8.26 1.17.11 1.8.27 1.17.12 1.8.28 1.17.12-rc.1 1.8.29 1.17.2 1.8.3 1.17.3 1.8.30 1.17.4 1.8.4 1.17.5 1.8.5 1.17.6 1.8.6 1.17.7 1.8.7 1.17.8 1.8.8 1.17.9 1.8.9 1.17.9-beta.1 1.9 1.18.0 2.0.0 1.19.0 2.0.1 1.19.1 2.0.10 1.2 2.0.11 1.2.1 2.0.12 1.2.2 2.0.13 1.2.3 2.0.14 1.2.4 2.0.15 1.2.5 2.0.16 1.2.6 2.0.17 1.2.7 2.0.18 1.20.0 2.0.19 1.20.0-rc.1 2.0.2 1.20.0-rc.2 2.0.20 1.20.1 2.0.3 1.20.2 2.0.4 1.20.3 2.0.5 1.21.0 2.0.6 1.21.1 2.0.7 1.22.0 2.0.8 1.22.1 2.0.9 1.22.2 1.23.0 1.23.1 1.23.2 1.24.0 1.24.1 1.24.2 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.29.0 1.29.1 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.30.0 1.30.1 1.30.2 1.30.2-rc.1 1.30.3 1.30.4 1.30.4-rc.1 1.30.5 1.31.0 1.31.1 1.32.0 1.32.0-rc.1 1.33.0 1.33.1 1.33.2 1.34.0 1.35.0 1.35.1 1.36.0 1.36.1 1.36.2 1.36.3 1.37.0 1.37.1 1.37.2
advanced-ads / classes / ad.php
advanced-ads / classes Last commit date
EDD_SL_Plugin_Updater.php 6 years ago ad-ajax.php 6 years ago ad-debug.php 8 years ago ad-health-notices.php 6 years ago ad-model.php 6 years ago ad-select.php 9 years ago ad.php 6 years ago ad_ajax_callbacks.php 6 years ago ad_group.php 6 years ago ad_placements.php 6 years ago ad_type_abstract.php 6 years ago ad_type_content.php 6 years ago ad_type_dummy.php 6 years ago ad_type_group.php 6 years ago ad_type_image.php 6 years ago ad_type_plain.php 6 years ago checks.php 6 years ago compatibility.php 6 years ago display-conditions.php 6 years ago filesystem.php 8 years ago frontend-notices.php 6 years ago frontend_checks.php 6 years ago plugin.php 6 years ago upgrades.php 7 years ago utils.php 6 years ago visitor-conditions.php 6 years ago widget.php 6 years ago
ad.php
987 lines
1 <?php
2 /**
3 * Advanced Ads Ad.
4 *
5 * @package Advanced_Ads_Ad
6 * @author Thomas Maier <support@wpadvancedads.com>
7 * @license GPL-2.0+
8 * @link https://wpadvancedads.com
9 * @copyright 2013-2020 Thomas Maier, Advanced Ads GmbH
10 */
11
12 /**
13 * An ad object
14 *
15 * @package Advanced_Ads_Ad
16 * @author Thomas Maier <support@wpadvancedads.com>
17 * @deprecated since version 1.5.3 (May 6th 2015)
18 * might still be needed if some old add-ons are running somewhere
19 */
20 if ( ! class_exists( 'Advads_Ad', false ) ) {
21 class Advads_Ad extends Advanced_Ads_Ad {
22
23 }
24 }
25
26 /**
27 * An ad object
28 *
29 * @package Advanced_Ads_Ad
30 * @author Thomas Maier <support@wpadvancedads.com>
31 */
32 class Advanced_Ads_Ad {
33
34 /**
35 * Id of the post type for this ad
36 *
37 * @var int $id
38 */
39 public $id = 0;
40
41 /**
42 * True, if this is an Advanced Ads Ad post type
43 *
44 * @var bool $is_ad
45 */
46 public $is_ad = false;
47
48 /**
49 * Ad type
50 *
51 * @var string $type ad type.
52 */
53 public $type = 'content';
54
55 /**
56 * Ad width
57 *
58 * @var int $width width of the ad.
59 */
60 public $width = 0;
61
62 /**
63 * Target url
64 *
65 * @var string $url ad URL parameter.
66 */
67 public $url = '';
68
69 /**
70 * Ad height
71 *
72 * @var int $height height of the ad.
73 */
74 public $height = 0;
75
76 /**
77 * Object of current ad type
78 *
79 * @var object $type_obj object of the current ad type.
80 */
81 protected $type_obj;
82
83 /**
84 * Content of the ad
85 *
86 * Only needed for ad types using the post content field
87 *
88 * @var string $content content of the ad.
89 */
90 public $content = '';
91
92 /**
93 * Conditions of the ad display
94 *
95 * @var array $conditions display and visitor conditions.
96 */
97 public $conditions = array();
98
99 /**
100 * Status of the ad (e.g. publish, pending)
101 *
102 * @var string $status status of the ad.
103 */
104 public $status = '';
105
106 /**
107 * Array with meta field options aka parameters
108 *
109 * @var array $options ad options.
110 */
111 protected $options = array();
112
113 /**
114 * Name of the meta field to save options to
115 *
116 * @var string $options_meta_field under which post meta key the ad options are stored.
117 */
118 public static $options_meta_field = 'advanced_ads_ad_options';
119
120 /**
121 * Additional arguments set when ad is loaded, overwrites or extends options
122 *
123 * @var array $args
124 */
125 public $args = array();
126
127 /**
128 * Multidimensional array contains information about the wrapper
129 * Each possible html attribute is an array with possible multiple elements
130 *
131 * @var array $wrapper options of the ad wrapper.
132 */
133 public $wrapper = array();
134
135 /**
136 * Will the ad be tracked?
137 *
138 * @var mixed $global_output
139 */
140 public $global_output;
141
142 /**
143 * Title of the ad
144 *
145 * @var string $title
146 */
147 public $title = '';
148
149 /**
150 * Displayed above the ad.
151 *
152 * @var string $label ad label.
153 */
154 protected $label = '';
155
156 /**
157 * Init ad object
158 *
159 * @param int $id id of the ad.
160 * @param array $args additional arguments.
161 */
162 public function __construct( $id, $args = array() ) {
163 $id = absint( $id );
164 $this->id = $id;
165 $this->args = is_array( $args ) ? $args : array();
166
167 // whether the ad will be tracked.
168 $this->global_output = isset( $this->args['global_output'] ) ? (bool) $this->args['global_output'] : true;
169
170 if ( ! empty( $id ) ) {
171 $this->load( $id );
172 }
173
174 // dynamically add sanitize filters for condition types.
175 $_types = array();
176 // -TODO use model
177 $advanced_ads_ad_conditions = Advanced_Ads::get_ad_conditions();
178 foreach ( $advanced_ads_ad_conditions as $_condition ) {
179 // add unique.
180 $_types[ $_condition['type'] ] = false;
181 }
182 // iterate types.
183 foreach ( array_keys( $_types ) as $_type ) {
184 // -TODO might be faster to use __call() method or isset()-test class method array
185 $method_name = 'sanitize_condition_' . $_type;
186 if ( method_exists( $this, $method_name ) ) {
187 add_filter( 'advanced-ads-sanitize-condition-' . $_type, array( $this, $method_name ), 10, 1 );
188 } elseif ( function_exists( 'advads_sanitize_condition_' . $_type ) ) {
189 // check for public function to sanitize this.
190 add_filter( 'advanced-ads-sanitize-condition-' . $_type, 'advads_sanitize_condition_' . $_type, 10, 1 );
191
192 }
193 }
194 }
195
196 /**
197 * Load an ad object by id based on its ad type
198 *
199 * @param int $id ad id.
200 *
201 * @return bool false if ad could not be loaded.
202 */
203 private function load( $id = 0 ) {
204
205 $_data = get_post( $id );
206 if ( null === $_data ) {
207 return false;
208 }
209
210 // return, if not an ad.
211 if ( Advanced_Ads::POST_TYPE_SLUG !== $_data->post_type ) {
212 return false;
213 } else {
214 $this->is_ad = true;
215 }
216
217 $this->type = $this->options( 'type' );
218 $this->title = $_data->post_title;
219 /* load ad type object */
220 $types = Advanced_Ads::get_instance()->ad_types;
221 if ( isset( $types[ $this->type ] ) ) {
222 $this->type_obj = $types[ $this->type ];
223 } else {
224 $this->type_obj = new Advanced_Ads_Ad_Type_Abstract();
225 }
226 $this->url = $this->get_url();
227 $this->width = absint( $this->options( 'width' ) );
228 $this->height = absint( $this->options( 'height' ) );
229 $this->conditions = $this->options( 'conditions' );
230 $this->description = $this->options( 'description' );
231 $this->output = $this->options( 'output' );
232 $this->status = $_data->post_status;
233 $this->expiry_date = $this->options( 'expiry_date' );
234 $this->is_head_placement = isset( $this->args['placement_type'] ) && 'header' === $this->args['placement_type'];
235 $this->args['is_top_level'] = ! isset( $this->args['is_top_level'] );
236
237 // load content based on ad type.
238 $this->content = $this->type_obj->load_content( $_data );
239
240 if ( ! $this->is_head_placement ) {
241 $this->maybe_create_label();
242 $this->wrapper = $this->load_wrapper_options();
243
244 // set wrapper conditions.
245 $this->wrapper = apply_filters( 'advanced-ads-set-wrapper', $this->wrapper, $this );
246 // add unique wrapper id.
247 if ( is_array( $this->wrapper )
248 && array() !== $this->wrapper
249 && ! isset( $this->wrapper['id'] ) ) {
250 // create unique id if not yet given.
251 $this->wrapper['id'] = $this->create_wrapper_id();
252 }
253 }
254 }
255
256 /**
257 * Get options from meta field and return specific field
258 *
259 * @param string $field post meta key to be returned.
260 * @param array $default default options.
261 *
262 * @return mixed meta field content
263 * @todo check against default values
264 */
265 public function options( $field = '', $default = null ) {
266 // retrieve options, if not given yet
267 // -TODO may execute multiple times (if empty); bad design and risk to access unintialised data with direct access to $this->options property.
268 if ( array() === $this->options ) {
269 // may return false.
270 $meta = get_post_meta( $this->id, self::$options_meta_field, true );
271 if ( $meta ) {
272 // merge meta with arguments given on ad load.
273 $this->options = Advanced_Ads_Utils::merge_deep_array( array( $meta, $this->args ) );
274 } else {
275 // load arguments given on ad load.
276 $this->options = $this->args;
277 }
278
279 if ( isset( $this->options['change-ad'] ) ) {
280 // some options was provided by the user.
281 $this->options = Advanced_Ads_Utils::merge_deep_array(
282 array(
283 $this->options,
284 $this->options['change-ad'],
285 )
286 );
287 }
288 }
289
290 // return specific option.
291 if ( '' !== $field ) {
292 if ( isset( $this->options[ $field ] ) ) {
293 return $this->options[ $field ];
294 }
295 } else { // return all options.
296 if ( ! empty( $this->options ) ) {
297 return $this->options;
298 }
299 }
300
301 return $default;
302 }
303
304 /**
305 * Set an option of the ad
306 *
307 * @param string $option name of the option.
308 * @param mixed $value value of the option.
309 *
310 * @since 1.1.0
311 */
312 public function set_option( $option = '', $value = '' ) {
313 if ( '' === $option ) {
314 return;
315 }
316
317 // get current options.
318 $options = $this->options();
319
320 // set options.
321 $options[ $option ] = $value;
322
323 // save options.
324 $this->options = $options;
325
326 }
327
328
329 /**
330 * Return ad content for frontend output
331 *
332 * @param array $output_options output options.
333 *
334 * @return string $output ad output
335 * @since 1.0.0
336 */
337 public function output( $output_options = array() ) {
338 if ( ! $this->is_ad ) {
339 return '';
340 }
341
342 $this->global_output = isset( $output_options['global_output'] ) ? $output_options['global_output'] : $this->global_output;
343 $output_options['global_output'] = $this->global_output;
344
345 // switch between normal and debug mode.
346 // check if debug output should only be displayed to admins.
347 $user_can_manage_ads = current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options' ) );
348 if ( isset( $this->output['debugmode'] )
349 && ( $user_can_manage_ads || ( ! $user_can_manage_ads && ! defined( 'ADVANCED_ADS_AD_DEBUG_FOR_ADMIN_ONLY' ) ) ) ) {
350 $debug = new Advanced_Ads_Ad_Debug();
351
352 return $debug->prepare_debug_output( $this );
353 } else {
354 $output = $this->prepare_frontend_output();
355 }
356
357 // add the ad to the global output array.
358 $advads = Advanced_Ads::get_instance();
359 if ( $output_options['global_output'] ) {
360 $new_ad = array(
361 'type' => 'ad',
362 'id' => $this->id,
363 'title' => $this->title,
364 'output' => $output,
365 );
366 // if ( method_exists( 'Advanced_Ads_Tracking_Plugin' , 'check_ad_tracking_enabled' ) ) {
367 // if ( class_exists( 'Advanced_Ads_Tracking_Plugin', false ) ) {
368 if ( defined( 'AAT_VERSION' ) && - 1 < version_compare( AAT_VERSION, '1.4.2' ) ) {
369
370 $new_ad['tracking_enabled'] = Advanced_Ads_Tracking_Plugin::get_instance()->check_ad_tracking_enabled( $this );
371
372 $tracking_options = Advanced_Ads_Tracking_Plugin::get_instance()->options();
373 if ( isset( $tracking_options['method'] ) && 'frontend' === $tracking_options['method'] && isset( $this->output['placement_id'] ) ) {
374 $new_ad['placement_id'] = $this->output['placement_id'];
375 }
376 }
377
378 $advads->current_ads[] = $new_ad;
379 }
380
381 // action when output is created.
382 do_action( 'advanced-ads-output', $this, $output, $output_options );
383
384 return apply_filters( 'advanced-ads-output-final', $output, $this, $output_options );
385 }
386
387 /**
388 * Check if the ad can be displayed in frontend due to its own conditions
389 *
390 * @param array $check_options check options.
391 *
392 * @return bool $can_display true if can be displayed in frontend
393 * @since 1.0.0
394 */
395 public function can_display( $check_options = array() ) {
396 $check_options = wp_parse_args(
397 $check_options,
398 array(
399 'passive_cache_busting' => false,
400 'ignore_debugmode' => false,
401 )
402 );
403
404 // prevent ad to show up through wp_head, if this is not a header placement.
405 if ( doing_action( 'wp_head' ) && isset( $this->options['placement_type'] ) && 'header' !== $this->options['placement_type'] ) {
406 return false;
407 }
408
409 // Check If the current ad is requested using a shortcode placed in the content of the current ad.
410 if ( isset( $this->options['shortcode_ad_id'] ) && (int) $this->options['shortcode_ad_id'] === $this->id ) {
411 return false;
412 }
413
414 // force ad display if debug mode is enabled.
415 if ( isset( $this->output['debugmode'] ) && ! $check_options['ignore_debugmode'] ) {
416 return true;
417 }
418
419 if ( ! $check_options['passive_cache_busting'] ) {
420 // don’t display ads that are not published or private for users not logged in.
421 if ( 'publish' !== $this->status && ! ( 'private' === $this->status && is_user_logged_in() ) ) {
422 return false;
423 }
424
425 if ( ! $this->can_display_by_visitor() || ! $this->can_display_by_expiry_date() ) {
426 return false;
427 }
428 } else {
429 if ( 'publish' !== $this->status || ! $this->can_display_by_expiry_date() ) {
430 return false;
431 }
432 }
433
434 // add own conditions to flag output as possible or not.
435 $can_display = apply_filters( 'advanced-ads-can-display', true, $this, $check_options );
436
437 return $can_display;
438 }
439
440 /**
441 * Check visitor conditions
442 *
443 * @return bool $can_display true if can be displayed in frontend based on visitor settings
444 * @since 1.1.0
445 */
446 public function can_display_by_visitor() {
447 if ( ! empty( $this->options['wp_the_query']['is_feed'] ) ) {
448 return true;
449 }
450
451 // check old "visitor" and new "visitors" conditions.
452 if ( ( empty( $this->options['visitors'] ) ||
453 ! is_array( $this->options['visitors'] ) )
454 && ( empty( $this->options['visitor'] ) ||
455 ! is_array( $this->options['visitor'] )
456 ) ) {
457 return true;
458 }
459
460 if ( isset( $this->options['visitors'] ) && is_array( $this->options['visitors'] ) ) {
461
462 $visitor_conditions = $this->options['visitors'];
463
464 $last_result = false;
465 $length = count( $visitor_conditions );
466
467 for ( $i = 0; $i < $length; ++ $i ) {
468 $_condition = current( $visitor_conditions );
469 // ignore OR if last result was true.
470 if ( $last_result && isset( $_condition['connector'] ) && 'or' === $_condition['connector'] ) {
471 next( $visitor_conditions );
472 continue;
473 }
474 $result = Advanced_Ads_Visitor_Conditions::frontend_check( $_condition, $this );
475 $last_result = $result;
476 if ( ! $result ) {
477 // return false only, if the next condition doesn’t have an OR operator.
478 $next = next( $visitor_conditions );
479 if ( ! isset( $next['connector'] ) || 'or' !== $next['connector'] ) {
480 return false;
481 }
482 } else {
483 next( $visitor_conditions );
484 }
485 }
486 }
487
488 /**
489 * "old" visitor conditions
490 *
491 * @deprecated since version 1.5.4
492 */
493
494 if ( empty( $this->options['visitor'] ) ||
495 ! is_array( $this->options['visitor'] ) ) {
496 return true;
497 }
498 $visitor_conditions = $this->options( 'visitor' );
499
500 // check mobile condition.
501 if ( isset( $visitor_conditions['mobile'] ) ) {
502 switch ( $visitor_conditions['mobile'] ) {
503 case 'only':
504 if ( ! wp_is_mobile() ) {
505 return false;
506 }
507 break;
508 case 'no':
509 if ( wp_is_mobile() ) {
510 return false;
511 }
512 break;
513 }
514 }
515
516 return true;
517 }
518
519 /**
520 * Check expiry date
521 *
522 * @return bool $can_display true if can be displayed in frontend based on expiry date
523 * @since 1.3.15
524 */
525 public function can_display_by_expiry_date() {
526
527 // if expiry_date is not set, null is returned.
528 $ad_expiry_date = (int) $this->options( 'expiry_date' );
529
530 if ( $ad_expiry_date <= 0 || $ad_expiry_date > time() ) {
531 return true;
532 }
533
534 // set status to 'draft' if the ad is expired.
535 if ( 'draft' !== $this->status ) {
536 // removing the kses filters here so that expiring ads don’t lose HTML or other code.
537 kses_remove_filters();
538 wp_update_post(
539 array(
540 'ID' => $this->id,
541 'post_status' => 'draft',
542 )
543 );
544 kses_init_filters();
545 /**
546 * Run when an ad expires
547 */
548 do_action( 'advanced-ads-ad-expired', $this->id, $this );
549 }
550
551 return false;
552 }
553
554 /**
555 * Save an ad to the database
556 * takes values from the current state
557 */
558 public function save() {
559 global $wpdb;
560
561 // remove slashes from content.
562 $this->content = $this->prepare_content_to_save();
563
564 $where = array( 'ID' => $this->id );
565 $wpdb->update( $wpdb->posts, array( 'post_content' => $this->content ), $where );
566
567 // clean post from object cache.
568 clean_post_cache( $this->id );
569
570 // sanitize conditions
571 // see sanitize_conditions function for example on using this filter.
572 $conditions = self::sanitize_conditions_on_save( $this->conditions );
573
574 // save other options to post meta field.
575 $options = $this->options();
576
577 $options['type'] = $this->type;
578 $options['url'] = $this->url;
579 // Inform the tracking add-on about the new url.
580 unset( $options['tracking']['link'] );
581 $options['width'] = $this->width;
582 $options['height'] = $this->height;
583 $options['conditions'] = $conditions;
584 $options['expiry_date'] = $this->expiry_date;
585 $options['description'] = $this->description;
586
587 // sanitize container ID option.
588 $options['output']['wrapper-id'] = isset( $options['output']['wrapper-id'] ) ? sanitize_key( $options['output']['wrapper-id'] ) : '';
589
590 // filter to manipulate options or add more to be saved.
591 $options = apply_filters( 'advanced-ads-save-options', $options, $this );
592
593 update_post_meta( $this->id, self::$options_meta_field, $options );
594 }
595
596 /**
597 * Save ad options.
598 * Meant to be used from the outside of an ad.
599 *
600 * @param int $ad_id post ID of the ad.
601 * @param array $options ad options.
602 */
603 public static function save_ad_options( $ad_id, array $options ) {
604
605 // don’t allow to clear options by accident.
606 if ( array() === $options ) {
607 return;
608 }
609
610 update_post_meta( $ad_id, self::$options_meta_field, $options );
611 }
612
613 /**
614 * Native filter for content field before being saved
615 *
616 * @return string $content ad content
617 * @since 1.0.0
618 */
619 public function prepare_content_to_save() {
620
621 $content = $this->content;
622
623 // load ad type specific parameter filter
624 // @todo this is just a hotfix for type_obj not set, yet the cause is still unknown.
625 if ( is_object( $this->type_obj ) ) {
626 $content = $this->type_obj->sanitize_content( $content );
627 }
628 // apply a custom filter by ad type.
629 $content = apply_filters( 'advanced-ads-pre-ad-save-' . $this->type, $content );
630
631 return $content;
632 }
633
634 /**
635 * Native filter for ad parameters before being saved
636 *
637 * @return array $parameters sanitized parameters.
638 */
639 public function prepare_parameters_to_save() {
640
641 $parameters = $this->parameters;
642 // load ad type specific parameter filter.
643 $parameters = $this->type_obj->sanitize_parameters( $parameters );
644
645 // apply native WP filter for content fields.
646 return $parameters;
647 }
648
649 /**
650 * Prepare ads output
651 *
652 * @return string.
653 */
654 public function prepare_frontend_output() {
655 $options = $this->options();
656
657 if ( isset( $options['change-ad']['content'] ) ) {
658 // output was provided by the user.
659 $output = $options['change-ad']['content'];
660 } else {
661 // load ad type specific content filter.
662 $output = $this->type_obj->prepare_output( $this );
663 }
664
665 // don’t deliver anything, if main ad content is empty.
666 if ( empty( $output ) ) {
667 return;
668 }
669
670 if ( ! $this->is_head_placement ) {
671 // filter to manipulate the output before the wrapper is added
672 $output = apply_filters( 'advanced-ads-output-inside-wrapper', $output, $this );
673
674 if ( $this->label ) {
675 $output = $this->label . $output;
676 }
677
678 // build wrapper around the ad.
679 $output = $this->add_wrapper( $output );
680
681 // add a clearfix, if set.
682 if ( ( isset( $this->output['clearfix'] ) && $this->output['clearfix'] )
683 || ( ! empty( $this->args['is_top_level'] ) && ! empty( $this->args['placement_clearfix'] ) ) ) {
684 $output .= '<br style="clear: both; display: block; float: none;"/>';
685 }
686 }
687
688 // apply a custom filter by ad type.
689 $output = apply_filters( 'advanced-ads-ad-output', $output, $this );
690
691 return $output;
692 }
693
694 /**
695 * Sanitize ad display conditions when saving the ad
696 *
697 * @param array $conditions conditions array send via the dashboard form for an ad.
698 *
699 * @return array with sanitized conditions
700 * @since 1.0.0
701 */
702 public function sanitize_conditions_on_save( $conditions = array() ) {
703
704 global $advanced_ads_ad_conditions;
705
706 if ( ! is_array( $conditions ) || array() === $conditions ) {
707 return array();
708 }
709
710 foreach ( $conditions as $_key => $_condition ) {
711 if ( 'postids' === $_key ) {
712 // sanitize single post conditions
713 if ( empty( $_condition['ids'] ) ) { // remove, if empty.
714 $_condition['include'] = array();
715 $_condition['exclude'] = array();
716 } elseif ( isset( $_condition['method'] ) ) {
717 switch ( $_condition['method'] ) {
718 case 'include':
719 $_condition['include'] = $_condition['ids'];
720 $_condition['exclude'] = array();
721 break;
722 case 'exclude':
723 $_condition['include'] = array();
724 $_condition['exclude'] = $_condition['ids'];
725 break;
726 }
727 }
728 } else {
729 if ( ! is_array( $_condition ) ) {
730 $_condition = trim( $_condition );
731 }
732 if ( $_condition == '' ) {
733 $conditions[ $_key ] = $_condition;
734 continue;
735 }
736 }
737 $type = ! empty( $advanced_ads_ad_conditions[ $_key ]['type'] ) ? $advanced_ads_ad_conditions[ $_key ]['type'] : 0;
738 if ( empty( $type ) ) {
739 continue;
740 }
741
742 // dynamically apply filters for each condition used.
743 $conditions[ $_key ] = apply_filters( 'advanced-ads-sanitize-condition-' . $type, $_condition );
744 }
745
746 return $conditions;
747 }
748
749 /**
750 * Sanitize id input field(s) for pattern /1,2,3,4/
751 *
752 * @param mixed $cond input string/array.
753 *
754 * @return array/string $cond sanitized string/array
755 */
756 public static function sanitize_condition_idfield( $cond = '' ) {
757 // strip anything that is not comma or number.
758
759 if ( is_array( $cond ) ) {
760 foreach ( $cond as $_key => $_cond ) {
761 $cond[ $_key ] = preg_replace( '#[^0-9,]#', '', $_cond );
762 }
763 } else {
764 $cond = preg_replace( '#[^0-9,]#', '', $cond );
765 }
766
767 return $cond;
768 }
769
770 /**
771 * Sanitize radio input field
772 *
773 * @param string $string input string.
774 *
775 * @return string $string sanitized string.
776 */
777 public static function sanitize_condition_radio( $string = '' ) {
778 // only allow 0, 1 and empty.
779 return preg_replace( '#[^01]#', '', $string );
780 }
781
782 /**
783 * Sanitize comma seperated text input field
784 *
785 * @param mixed $cond input string/array.
786 *
787 * @return array/string $cond sanitized string/array.
788 */
789 public static function sanitize_condition_textvalues( $cond = '' ) {
790 // strip anything that is not comma, alphanumeric, minus and underscore.
791 if ( is_array( $cond ) ) {
792 foreach ( $cond as $_key => $_cond ) {
793 $cond[ $_key ] = preg_replace( '#[^0-9,A-Za-z-_]#', '', $_cond );
794 }
795 } else {
796 $cond = preg_replace( '#[^0-9,A-Za-z-_]#', '', $cond );
797 }
798
799 return $cond;
800 }
801
802 /**
803 * Load wrapper options set with the ad
804 *
805 * @return array $wrapper options array ready to be use in add_wrapper() function.
806 * @since 1.3
807 */
808 protected function load_wrapper_options() {
809 $wrapper = array();
810
811 $position = ! empty( $this->output['position'] ) ? $this->output['position'] : '';
812 $use_placement_pos = false;
813
814 if ( $this->args['is_top_level'] ) {
815 if ( isset( $this->output['class'] ) && is_array( $this->output['class'] ) ) {
816 $wrapper['class'] = $this->output['class'];
817 }
818 if ( ! empty( $this->args['placement_position'] ) ) {
819 // If not group, Set placement position instead of ad position.
820 $use_placement_pos = true;
821 $position = $this->args['placement_position'];
822 }
823 }
824
825 switch ( $position ) {
826 case 'left':
827 $wrapper['style']['float'] = 'left';
828 break;
829 case 'right':
830 $wrapper['style']['float'] = 'right';
831 break;
832 case 'center':
833 if ( ! empty( $this->output['add_wrapper_sizes'] ) ) {
834 $wrapper['style']['margin-left'] = 'auto';
835 $wrapper['style']['margin-right'] = 'auto';
836
837 if ( $use_placement_pos ) {
838 $wrapper['style']['text-align'] = 'center';
839 }
840 } else {
841 $wrapper['style']['text-align'] = 'center';
842 }
843
844 // add css rule after wrapper to center the ad.
845 break;
846 case 'clearfix':
847 $wrapper['style']['clear'] = 'both';
848 break;
849 }
850
851 // add manual classes.
852 if ( isset( $this->output['wrapper-class'] ) && '' !== $this->output['wrapper-class'] ) {
853 $classes = explode( ' ', $this->output['wrapper-class'] );
854
855 foreach ( $classes as $_class ) {
856 $wrapper['class'][] = sanitize_text_field( $_class );
857 }
858 }
859
860 if ( ! empty( $this->output['margin']['top'] ) ) {
861 $wrapper['style']['margin-top'] = intval( $this->output['margin']['top'] ) . 'px';
862 }
863 if ( ! empty( $this->output['margin']['right'] ) ) {
864 $wrapper['style']['margin-right'] = intval( $this->output['margin']['right'] ) . 'px';
865 }
866 if ( ! empty( $this->output['margin']['bottom'] ) ) {
867 $wrapper['style']['margin-bottom'] = intval( $this->output['margin']['bottom'] ) . 'px';
868 }
869 if ( ! empty( $this->output['margin']['left'] ) ) {
870 $wrapper['style']['margin-left'] = intval( $this->output['margin']['left'] ) . 'px';
871 }
872
873 if ( ! empty( $this->output['add_wrapper_sizes'] ) ) {
874 $wrapper['style']['width'] = intval( $this->width ) . 'px';
875 $wrapper['style']['height'] = intval( $this->height ) . 'px';
876 }
877
878 if ( ! empty( $this->output['clearfix_before'] ) ) {
879 $wrapper['style']['clear'] = 'both';
880 }
881
882 return $wrapper;
883 }
884
885 /**
886 * Add a wrapper arount the ad content if wrapper information are given
887 *
888 * @param string $ad_content content of the ad.
889 *
890 * @return string $wrapper ad within the wrapper
891 * @since 1.1.4
892 */
893 protected function add_wrapper( $ad_content = '' ) {
894 $wrapper_options = apply_filters( 'advanced-ads-output-wrapper-options', $this->wrapper, $this );
895
896 if ( ( ! isset( $this->output['wrapper-id'] ) || '' === $this->output['wrapper-id'] )
897 && array() === $wrapper_options || ! is_array( $wrapper_options ) ) {
898 return $ad_content;
899 }
900
901 // create unique id if not yet given.
902 if ( empty( $wrapper_options['id'] ) ) {
903 $wrapper_options['id'] = $this->create_wrapper_id();
904 $this->wrapper['id'] = $wrapper_options['id'];
905 }
906
907 // add edit button for users with the appropriate rights.
908 if ( ! defined( 'ADVANCED_ADS_DISABLE_EDIT_BAR' ) && current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ) ) ) {
909 ob_start();
910 include ADVADS_BASE_PATH . 'public/views/ad-edit-bar.php';
911 $ad_content = ob_get_clean() . $ad_content;
912 }
913
914 // build the box
915 $wrapper = '<div' . Advanced_Ads_Utils::build_html_attributes( $wrapper_options ) . '>';
916 $wrapper .= apply_filters( 'advanced-ads-output-wrapper-before-content', '', $this );
917 $wrapper .= $ad_content;
918 $wrapper .= apply_filters( 'advanced-ads-output-wrapper-after-content', '', $this );
919 $wrapper .= '</div>';
920
921 return $wrapper;
922 }
923
924 /**
925 * Create a random wrapper id
926 *
927 * @return string $id random id string
928 * @since 1.1.4
929 */
930 private function create_wrapper_id() {
931
932 if ( isset( $this->output['wrapper-id'] ) ) {
933 $id = sanitize_key( $this->output['wrapper-id'] );
934 if ( '' !== $id ) {
935 return $id;
936 }
937 }
938
939 $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix();
940
941 return $prefix . mt_rand();
942 }
943
944 /**
945 * Create an "Advertisement" label if conditions are met.
946 */
947 public function maybe_create_label() {
948 $placement_state = isset( $this->args['ad_label'] ) ? $this->args['ad_label'] : 'default';
949
950 $label = Advanced_Ads::get_instance()->get_label( $placement_state );
951 if ( 'group' !== $this->type && $label ) {
952 $this->label = $label;
953 }
954 }
955
956 /**
957 * Get the ad url.
958 *
959 * @return string
960 */
961 private function get_url() {
962 $this->url = $this->options( 'url' );
963
964 // If the tracking add-on is not active.
965 if ( ! defined( 'AAT_VERSION' ) ) {
966 global $pagenow;
967 // If this is not the ad edit page.
968 if ( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow ) {
969 // Remove placeholders.
970 $this->url = str_replace(
971 array(
972 '[POST_ID]',
973 '[POST_SLUG]',
974 '[CAT_SLUG]',
975 '[AD_ID]',
976 ),
977 '',
978 $this->url
979 );
980 }
981 }
982
983 return $this->url;
984 }
985
986 }
987