Нет описания

wordads.php 1.8KB

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