PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.2
Advanced Ads – Ad Manager & AdSense v2.0.2
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 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 year ago
class-gutenberg.php
281 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 $all_ads = wp_advads_ad_query(
91 [
92 'post_status' => [ 'publish' ],
93 'orderby' => 'title',
94 'order' => 'ASC',
95 ]
96 )->posts;
97 $all_groups = wp_advads_get_all_groups();
98
99 $ads = [];
100 $groups = [];
101 $placements = [];
102
103 foreach ( $all_ads as $ad ) {
104 $ads[] = [
105 'id' => $ad->ID,
106 'title' => $ad->post_title,
107 ];
108 }
109
110 foreach ( $all_groups as $group ) {
111 $groups[] = [
112 'id' => $group->get_id(),
113 'name' => $group->get_name(),
114 ];
115 }
116
117 foreach ( wp_advads_get_all_placements() as $placement ) {
118 if ( $placement->is_type( [ 'sidebar_widget', 'default' ] ) ) {
119 $placements[] = [
120 'id' => $placement->get_id(),
121 'name' => $placement->get_title(),
122 ];
123 }
124 }
125
126 ksort( $placements );
127
128 if ( empty( $placements ) ) {
129 $placements = false;
130 }
131
132 $i18n = [
133 '--empty--' => __( '--empty--', 'advanced-ads' ),
134 'advads' => __( 'Advanced Ads', 'advanced-ads' ),
135 'ads' => __( 'Ads', 'advanced-ads' ),
136 'adGroups' => __( 'Ad Groups', 'advanced-ads' ),
137 'placements' => __( 'Placements', 'advanced-ads' ),
138 'width' => __( 'Width', 'advanced-ads' ),
139 'height' => __( 'Height', 'advanced-ads' ),
140 'size' => __( 'Size', 'advanced-ads' ),
141 'alignment' => __( 'Alignment', 'advanced-ads' ),
142 ];
143
144 $inline_script = wp_json_encode(
145 [
146 'ads' => $ads,
147 'groups' => $groups,
148 'placements' => $placements,
149 'editLinks' => [
150 'group' => admin_url( 'admin.php?page=advanced-ads-groups' ),
151 'placement' => admin_url( 'admin.php?page=advanced-ads-placements' ),
152 'ad' => admin_url( 'post.php?post=%ID%&action=edit' ),
153 ],
154 'imagesUrl' => ADVADS_BASE_URL . 'modules/gutenberg/assets/img/',
155 'i18n' => $i18n,
156 'textFlow' => [
157 'default' => [
158 'label' => __( "Theme's default", 'advanced-ads' ),
159 'description' => __( 'The ad will behave as predefined by the theme.', 'advanced-ads' ),
160 ],
161 'float-left' => [
162 'label' => __( "Float left", 'advanced-ads' ),
163 'description' => __( 'Text will wrap around the ad and its margin.', 'advanced-ads' ),
164 ],
165 'float-right' => [
166 'label' => __( "Float right", 'advanced-ads' ),
167 'description' => __( 'Text will wrap around the ad and its margin.', 'advanced-ads' ),
168 ],
169 'block-left' => [
170 'label' => __( "Block left", 'advanced-ads' ),
171 'description' => __( 'Text will continue after the ad and its margin.', 'advanced-ads' ),
172 ],
173 'block-right' => [
174 'label' => __( "Block right", 'advanced-ads' ),
175 'description' => __( 'Text will continue after the ad and its margin.', 'advanced-ads' ),
176 ],
177 'center' => [
178 'label' => __( "Centered", 'advanced-ads' ),
179 'description' => __( 'Text will continue after the ad and its margin.', 'advanced-ads' ),
180 ],
181 ],
182 ]
183 );
184
185 // put the inline code with the global variable right before the block's JS file.
186 wp_add_inline_script( ADVADS_PLUGIN_BASENAME . '/gutenberg-ad', 'var advadsGutenberg = ' . $inline_script, 'before' );
187 wp_enqueue_script( ADVADS_PLUGIN_BASENAME . '/gutenberg-ad' );
188
189 wp_enqueue_style(
190 ADVADS_PLUGIN_BASENAME . '/gutenberg-ad',
191 ADVADS_BASE_URL . 'modules/gutenberg/assets/block.css',
192 [],
193 ADVADS_VERSION
194 );
195 }
196
197 /**
198 * Server side rendering for single ad block
199 *
200 * @param array $attr Block's attributes.
201 */
202 public static function render_ad_selector( $attr ) {
203 ob_start();
204
205 if ( ! isset( $attr['itemID'] ) ) {
206 ob_end_clean();
207
208 return '';
209 }
210
211 $output = [
212 'output' => [
213 'class' => ! empty( $attr['className'] ) ? array_filter( explode( ' ', $attr['className'] ) ) : [],
214 ],
215 ];
216
217 if ( isset( $attr['fixed_widget'] ) ) {
218 $output['wrapper_attrs']['data-fixed_widget'] = esc_attr( $attr['fixed_widget'] );
219 }
220
221 if ( ! empty( $attr['width'] ) ) {
222 $output['output']['wrapper_attrs']['style']['width'] = absint( $attr['width'] ) . 'px';
223 }
224
225 if ( ! empty( $attr['height'] ) ) {
226 $output['output']['wrapper_attrs']['style']['height'] = absint( $attr['height'] ) . 'px';
227 }
228
229 $align = esc_attr( $attr['align'] ?? 'default' );
230 $after_ad_filter = function( $output ) {
231 return $output . '<br style="clear: both; display: block; float: none;">';
232 };
233
234 if ( 0 === strpos( $align, 'block' ) ) {
235 add_filter( 'advanced-ads-ad-output', $after_ad_filter );
236 }
237
238 switch ( $align ) {
239 case 'float-left':
240 case 'block-left':
241 $output['output']['wrapper_attrs']['style']['float'] = 'left';
242 break;
243 case 'float-right':
244 case 'block-right':
245 $output['output']['wrapper_attrs']['style']['float'] = 'right';
246 break;
247 case 'center':
248 $output['output']['wrapper_attrs']['style']['margin-left'] = 'auto';
249 $output['output']['wrapper_attrs']['style']['margin-right'] = 'auto';
250 $output['output']['wrapper_attrs']['style']['text-align'] = 'center';
251 break;
252 default:
253 }
254
255 // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- we can't escape ad output without potentially breaking ads
256 if ( 0 === strpos( $attr['itemID'], 'ad_' ) ) {
257 echo get_the_ad( absint( substr( $attr['itemID'], 3 ) ), '', $output );
258 } elseif ( 0 === strpos( $attr['itemID'], 'group_' ) ) {
259 echo get_the_group( substr( $attr['itemID'], 6 ), '', $output );
260 } elseif ( 0 === strpos( $attr['itemID'], 'place_' ) ) {
261 $id = substr( $attr['itemID'], 6 );
262 echo get_the_placement( is_numeric( $id ) ? (int) $id : $id, '', $output );
263 }
264
265 // phpcs:enable
266
267 return ob_get_clean();
268 }
269
270 /**
271 * Return the unique instance
272 */
273 public static function get_instance() {
274 if ( null === self::$instance ) {
275 self::$instance = new self();
276 }
277
278 return self::$instance;
279 }
280 }
281