1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Ads Block.
*
* @since 7.1.0
*
* @package Jetpack
*/
class Jetpack_WordAds_Gutenblock {
const BLOCK_NAME = 'jetpack/wordads';
/**
* Check if site is on WP.com Simple.
*
* @return bool
*/
private static function is_wpcom() {
return defined( 'IS_WPCOM' ) && IS_WPCOM;
}
/**
* Check if the WordAds module is active.
*
* @return bool
*/
private static function is_jetpack_module_active() {
return method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'wordads' );
}
/**
* Check if the site is approved for ads for WP.com Simple sites.
*
* @return bool
*/
private static function is_available() {
if ( self::is_wpcom() ) {
return has_any_blog_stickers( array( 'wordads', 'wordads-approved', 'wordads-approved-misfits' ), get_current_blog_id() );
}
return self::is_jetpack_module_active();
}
/**
* Register the WordAds block.
*/
public static function register() {
if ( self::is_available() ) {
jetpack_register_block(
self::BLOCK_NAME,
array(
'render_callback' => array( 'Jetpack_WordAds_Gutenblock', 'gutenblock_render' ),
)
);
}
}
/**
* Set if the WordAds block is available.
*/
public static function set_availability() {
if ( ! self::is_available() ) {
Jetpack_Gutenberg::set_extension_unavailable( self::BLOCK_NAME, 'WordAds unavailable' );
return;
}
// Make the block available. Just in case it wasn't registed before.
Jetpack_Gutenberg::set_extension_available( self::BLOCK_NAME );
}
/**
* Renders the WordAds block.
*
* @param array $attr Block attributes.
*
* @return string Block HTML.
*/
public static function gutenblock_render( $attr ) {
global $wordads;
/** This filter is already documented in modules/wordads/wordads.php `insert_ad()` */
if ( empty( $wordads ) || is_feed() || apply_filters( 'wordads_inpost_disable', false ) ) {
return '';
}
if ( ! empty( $attr['hideMobile'] ) && $wordads->params->is_mobile() ) {
return '';
}
if ( ! self::is_wpcom() && $wordads->option( 'wordads_house' ) ) {
return $wordads->get_ad( 'inline', 'house' );
}
// section_id is mostly depricated at this point, but it helps us (devs) keep track of which ads end up where
// 6 is to keep track of gutenblock ads.
$section_id = $wordads->params->blog_id . '6';
$align = 'center';
if ( isset( $attr['align'] ) && in_array( $attr['align'], array( 'left', 'center', 'right' ), true ) ) {
$align = $attr['align'];
}
$align = 'align' . $align;
$ad_tag_ids = $wordads->get_ad_tags();
$format = 'mrec';
if ( isset( $attr['format'] ) && in_array( $attr['format'], array_keys( $ad_tag_ids ), true ) ) {
$format = $attr['format'];
}
$height = $ad_tag_ids[ $format ]['height'];
$width = $ad_tag_ids[ $format ]['width'];
$snippet = $wordads->get_ad_snippet( $section_id, $height, $width, 'inline', $wordads->get_solo_unit_css() );
return $wordads->get_ad_div( 'inline', $snippet, array( $align ) );
}
}
add_action(
'init',
array( 'Jetpack_WordAds_Gutenblock', 'register' )
);
add_action(
'jetpack_register_gutenberg_extensions',
array( 'Jetpack_WordAds_Gutenblock', 'set_availability' )
);
|