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