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