wordads.php
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Wordads shortcode. |
| 4 | * |
| 5 | * Examples: |
| 6 | * [wordads] |
| 7 | * |
| 8 | * @package Jetpack |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * Embed WordAds 'ad' in post |
| 13 | */ |
| 14 | class Jetpack_WordAds_Shortcode { |
| 15 | |
| 16 | /** |
| 17 | * Used to determine whether scripts and styles have been enqueued already. |
| 18 | * |
| 19 | * @var bool false Should we enqueue scripts and styles. |
| 20 | */ |
| 21 | private $scripts_and_style_included = false; |
| 22 | |
| 23 | /** |
| 24 | * Initialize. |
| 25 | */ |
| 26 | public function __construct() { |
| 27 | add_action( 'init', array( $this, 'action_init' ) ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Register our shortcode and enqueue necessary files. |
| 32 | */ |
| 33 | public function action_init() { |
| 34 | global $wordads; |
| 35 | |
| 36 | if ( empty( $wordads ) ) { |
| 37 | return null; |
| 38 | } |
| 39 | |
| 40 | add_shortcode( 'wordads', array( $this, 'wordads_shortcode' ) ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Our [wordads] shortcode. |
| 45 | * Prints a WordAds Ad. |
| 46 | * |
| 47 | * @param array $atts Array of shortcode attributes. |
| 48 | * @param string $content Post content. |
| 49 | * |
| 50 | * @return string HTML for WordAds shortcode. |
| 51 | */ |
| 52 | public static function wordads_shortcode( $atts, $content = '' ) { |
| 53 | $atts = shortcode_atts( array(), $atts, 'wordads' ); |
| 54 | |
| 55 | return self::wordads_shortcode_html( $atts, $content ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * The shortcode output |
| 60 | * |
| 61 | * @param array $atts Array of shortcode attributes. |
| 62 | * @param string $content Post content. |
| 63 | * |
| 64 | * @return string HTML output |
| 65 | */ |
| 66 | private static function wordads_shortcode_html( $atts, $content = '' ) { |
| 67 | global $wordads; |
| 68 | |
| 69 | if ( empty( $wordads ) ) { |
| 70 | return '<div>' . __( 'The WordAds module is not active', 'jetpack' ) . '</div>'; |
| 71 | } |
| 72 | |
| 73 | $html = '<div class="jetpack-wordad" itemscope itemtype="https://schema.org/WPAdBlock"></div>'; |
| 74 | $html = $wordads->insert_inline_ad( $html ); |
| 75 | |
| 76 | return $html; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | new Jetpack_WordAds_Shortcode(); |
| 81 |