PluginProbe
Advanced Ads – Ad Manager & AdSense / 1.39.0
Advanced Ads – Ad Manager & AdSense v1.39.0
2.0.26 2.0.25 2.0.24 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 All 348 releases
advanced-ads / classes / inline-css.php
inline-css.php
114 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Handles Advanced Ads Inline CSS settings.
5 */
6 class Advanced_Ads_Inline_Css {
7 /**
8 * Holds the state if inline css should be output or not.
9 *
10 * @var bool
11 */
12 protected $add_inline_css;
13
14 /**
15 * Initialize the module.
16 */
17 public function __construct() {
18
19 /**
20 * Filters the state if inline css should be output or not.
21 * Ajax CB container could have added inline css already.
22 *
23 * Set to false if an addon output inline css before the main plugin.
24 *
25 * @param bool Contains the state.
26 */
27 $this->add_inline_css = apply_filters( 'advanced-ads-output-inline-css', true );
28 if ( ! $this->add_inline_css ) {
29 return;
30 }
31
32 // Add inline css to the tcf container.
33 $this->check_tcf_option();
34 }
35
36 /**
37 * Adds inline css.
38 *
39 * @param array $wrapper Add wrapper array.
40 * @param string $css Custom inline css.
41 * @param bool|null $global_output Whether this ad is using cache-busting.
42 *
43 * @return array
44 */
45 public function add_css( $wrapper, $css, $global_output ) {
46 $this->add_inline_css = $this->add_inline_css && $global_output !== false;
47 if ( ! $this->add_inline_css ) {
48 return $wrapper;
49 }
50
51 $styles = $this->get_styles_by_string( $css );
52 $wrapper['style'] = empty( $wrapper['style'] ) ? $styles : array_merge( $wrapper['style'], $styles );
53 $this->add_inline_css = false;
54
55 return $wrapper;
56 }
57
58 /**
59 * Extend TCF output with a container containing inline css.
60 *
61 * @param string $output The output string.
62 * @param Advanced_Ads_Ad $ad The ad object.
63 *
64 * @return string
65 */
66 public function add_tcf_container( $output, Advanced_Ads_Ad $ad ) {
67 $inline_css = $ad->options( 'inline-css' );
68
69 if (
70 ! $ad->global_output
71 || empty( $inline_css )
72 || strpos( $output, '<div class="tcf-container"' ) === 0
73 ) {
74 return $output;
75 }
76
77 return sprintf(
78 '<div class="tcf-container" style="' . $inline_css . '">%s</div>',
79 $output
80 );
81 }
82
83 /**
84 * Reformat css styles string to array.
85 *
86 * @param string $string CSS-Style.
87 *
88 * @return array
89 */
90 private function get_styles_by_string( $string ) {
91 $chunks = array_chunk( preg_split( '/[:;]/', $string ), 2 );
92 array_walk_recursive( $chunks, function( &$value ) {
93 $value = trim( $value );
94 } );
95
96 return array_combine( array_filter( array_column( $chunks, 0 ) ), array_filter( array_column( $chunks, 1 ) ) );
97 }
98
99 /**
100 * If TCF is active, i.e. there is a TCF container, add the options to this container.
101 */
102 private function check_tcf_option() {
103 static $privacy_options;
104 if ( $privacy_options === null ) {
105 $privacy_options = Advanced_Ads_Privacy::get_instance()->options();
106 }
107
108 if ( ! empty( $privacy_options['enabled'] ) && $privacy_options['enabled'] === 'on' && $privacy_options['consent-method'] === 'iab_tcf_20' ) {
109 add_filter( 'advanced-ads-output-final', [ $this, 'add_tcf_container' ], 20, 2 );
110 $this->add_inline_css = false;
111 }
112 }
113 }
114