PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.24
Advanced Ads – Ad Manager & AdSense v2.0.24
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 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 / modules / gutenberg / includes / class-gutenberg.php
advanced-ads / modules / gutenberg / includes Last commit date
class-gutenberg.php 1 week ago
class-gutenberg.php
279 lines
1 <?php
2 // phpcs:ignoreFile
3 /**
4 * Gutenberg block registration
5 *
6 * @package Advanced_Ads
7 */
8
9 /**
10 * Class Advanced_Ads_Gutenberg
11 */
12 class Advanced_Ads_Gutenberg {
13
14 /**
15 * The singleton
16 *
17 * @var Advanced_Ads_Gutenberg
18 */
19 private static $instance;
20
21 /**
22 * CSS classes to use on the frontend
23 *
24 * @var string
25 */
26 private static $css_class;
27
28 /**
29 * Constructor
30 */
31 private function __construct() {
32 add_action( 'init', [ $this, 'init' ] );
33 add_action( 'enqueue_block_editor_assets', [ $this, 'register_scripts' ] );
34 }
35
36 /**
37 * Register blocks
38 */
39 public function init() {
40 if ( ! function_exists( 'register_block_type' ) ) {
41 // no Gutenberg, Abort.
42 return;
43 }
44
45 register_block_type(
46 'advads/gblock',
47 [
48 'editor_script_handles' => [ ADVADS_PLUGIN_BASENAME . '/gutenberg-ad' ],
49 'editor_style_handles' => [ ADVADS_PLUGIN_BASENAME . '/gutenberg-ad' ],
50 'render_callback' => [ $this, 'render_ad_selector' ],
51 ]
52 );
53
54 /**
55 * Removes legacy widget from legacy widget block.
56 *
57 * @param string[] $widget_types An array of excluded widget-type IDs.
58 *
59 * @return array
60 */
61 add_filter(
62 'widget_types_to_hide_from_legacy_widget_block',
63 function( $widget_types ) {
64 $widget_types[] = 'advads_ad_widget';
65
66 return $widget_types;
67 }
68 );
69 }
70
71 /**
72 * Register back end scripts
73 *
74 * @return void
75 */
76 public function register_scripts() {
77 if ( ! function_exists( 'register_block_type' ) ) {
78 // no Gutenberg, Abort.
79 return;
80 }
81
82 wp_register_script(
83 ADVADS_PLUGIN_BASENAME . '/gutenberg-ad',
84 ADVADS_BASE_URL . 'modules/gutenberg/assets/advanced-ads.block.js',
85 [ 'wp-dom-ready', 'wp-blocks', 'wp-element' ],
86 ADVADS_VERSION,
87 false
88 );
89
90 $ads = [];
91 $groups = [];
92 $placements = [];
93
94 foreach ( wp_advads_get_ad_summaries() as $id => $summary ) {
95 if ( ! in_array( $summary['status'], [ 'publish', 'future' ], true ) ) {
96 continue;
97 }
98
99 $ads[] = [
100 'id' => $id,
101 'title' => $summary['title'],
102 ];
103 }
104
105 foreach ( wp_advads_get_groups_dropdown() as $id => $title ) {
106 $groups[] = [
107 'id' => $id,
108 'name' => $title,
109 ];
110 }
111
112 foreach ( wp_advads_get_placement_summaries() as $id => $summary ) {
113 if ( ! in_array( $summary['type'], [ 'sidebar_widget', 'default' ], true ) ) {
114 continue;
115 }
116
117 $placements[] = [
118 'id' => $id,
119 'name' => $summary['title'],
120 ];
121 }
122
123 ksort( $placements );
124
125 if ( empty( $placements ) ) {
126 $placements = false;
127 }
128
129 $i18n = [
130 '--empty--' => __( '--empty--', 'advanced-ads' ),
131 'advads' => __( 'Advanced Ads', 'advanced-ads' ),
132 'ads' => __( 'Ads', 'advanced-ads' ),
133 'adGroups' => __( 'Ad Groups', 'advanced-ads' ),
134 'placements' => __( 'Placements', 'advanced-ads' ),
135 'width' => __( 'Width', 'advanced-ads' ),
136 'height' => __( 'Height', 'advanced-ads' ),
137 'size' => __( 'Size', 'advanced-ads' ),
138 'alignment' => __( 'Alignment', 'advanced-ads' ),
139 ];
140
141 $inline_script = wp_json_encode(
142 [
143 'wpVersion' => get_bloginfo( 'version' ),
144 'ads' => $ads,
145 'groups' => $groups,
146 'placements' => $placements,
147 'editLinks' => [
148 'group' => admin_url( 'admin.php?page=advanced-ads-groups' ),
149 'placement' => admin_url( 'admin.php?page=advanced-ads-placements' ),
150 'ad' => admin_url( 'post.php?post=%ID%&action=edit' ),
151 ],
152 'imagesUrl' => ADVADS_BASE_URL . 'modules/gutenberg/assets/img/',
153 'i18n' => $i18n,
154 'textFlow' => [
155 'default' => [
156 'label' => __( "Theme's default", 'advanced-ads' ),
157 'description' => __( 'The ad will behave as predefined by the theme.', 'advanced-ads' ),
158 ],
159 'float-left' => [
160 'label' => __( "Float left", 'advanced-ads' ),
161 'description' => __( 'Text will wrap around the ad and its margin.', 'advanced-ads' ),
162 ],
163 'float-right' => [
164 'label' => __( "Float right", 'advanced-ads' ),
165 'description' => __( 'Text will wrap around the ad and its margin.', 'advanced-ads' ),
166 ],
167 'block-left' => [
168 'label' => __( "Block left", 'advanced-ads' ),
169 'description' => __( 'Text will continue after the ad and its margin.', 'advanced-ads' ),
170 ],
171 'block-right' => [
172 'label' => __( "Block right", 'advanced-ads' ),
173 'description' => __( 'Text will continue after the ad and its margin.', 'advanced-ads' ),
174 ],
175 'center' => [
176 'label' => __( "Centered", 'advanced-ads' ),
177 'description' => __( 'Text will continue after the ad and its margin.', 'advanced-ads' ),
178 ],
179 ],
180 ]
181 );
182
183 // put the inline code with the global variable right before the block's JS file.
184 wp_add_inline_script( ADVADS_PLUGIN_BASENAME . '/gutenberg-ad', 'var advadsGutenberg = ' . $inline_script, 'before' );
185 wp_enqueue_script( ADVADS_PLUGIN_BASENAME . '/gutenberg-ad' );
186
187 wp_enqueue_style(
188 ADVADS_PLUGIN_BASENAME . '/gutenberg-ad',
189 ADVADS_BASE_URL . 'modules/gutenberg/assets/block.css',
190 [],
191 ADVADS_VERSION
192 );
193 }
194
195 /**
196 * Server side rendering for single ad block
197 *
198 * @param array $attr Block's attributes.
199 */
200 public static function render_ad_selector( $attr ) {
201 ob_start();
202
203 if ( ! isset( $attr['itemID'] ) ) {
204 ob_end_clean();
205
206 return '';
207 }
208
209 $output = [
210 'output' => [
211 'class' => ! empty( $attr['className'] ) ? array_filter( explode( ' ', $attr['className'] ) ) : [],
212 ],
213 ];
214
215 if ( isset( $attr['fixed_widget'] ) ) {
216 $output['wrapper_attrs']['data-fixed_widget'] = esc_attr( $attr['fixed_widget'] );
217 }
218
219 if ( ! empty( $attr['width'] ) ) {
220 $output['output']['wrapper_attrs']['style']['width'] = absint( $attr['width'] ) . 'px';
221 }
222
223 if ( ! empty( $attr['height'] ) ) {
224 $output['output']['wrapper_attrs']['style']['height'] = absint( $attr['height'] ) . 'px';
225 }
226
227 $align = esc_attr( $attr['align'] ?? 'default' );
228 $after_ad_filter = function( $output ) {
229 return $output . '<br style="clear: both; display: block; float: none;">';
230 };
231
232 if ( 0 === strpos( $align, 'block' ) ) {
233 add_filter( 'advanced-ads-ad-output', $after_ad_filter );
234 }
235
236 switch ( $align ) {
237 case 'float-left':
238 case 'block-left':
239 $output['output']['wrapper_attrs']['style']['float'] = 'left';
240 break;
241 case 'float-right':
242 case 'block-right':
243 $output['output']['wrapper_attrs']['style']['float'] = 'right';
244 break;
245 case 'center':
246 $output['output']['wrapper_attrs']['style']['margin-left'] = 'auto';
247 $output['output']['wrapper_attrs']['style']['margin-right'] = 'auto';
248 $output['output']['wrapper_attrs']['style']['text-align'] = 'center';
249 break;
250 default:
251 }
252
253 // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- we can't escape ad output without potentially breaking ads
254 if ( 0 === strpos( $attr['itemID'], 'ad_' ) ) {
255 echo get_the_ad( absint( substr( $attr['itemID'], 3 ) ), '', $output );
256 } elseif ( 0 === strpos( $attr['itemID'], 'group_' ) ) {
257 echo get_the_group( substr( $attr['itemID'], 6 ), '', $output );
258 } elseif ( 0 === strpos( $attr['itemID'], 'place_' ) ) {
259 $id = substr( $attr['itemID'], 6 );
260 echo get_the_placement( is_numeric( $id ) ? (int) $id : $id, '', $output );
261 }
262
263 // phpcs:enable
264
265 return ob_get_clean();
266 }
267
268 /**
269 * Return the unique instance
270 */
271 public static function get_instance() {
272 if ( null === self::$instance ) {
273 self::$instance = new self();
274 }
275
276 return self::$instance;
277 }
278 }
279