class-premium-upsell-admin-block.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Class WPSEO_Premium_Upsell_Admin_Block
  9. */
  10. class WPSEO_Premium_Upsell_Admin_Block {
  11. /** @var string Hook to display the block on. */
  12. protected $hook;
  13. /** @var string Identifier to use in the dismissal functionality. */
  14. protected $identifier = 'premium_upsell_admin_block';
  15. /**
  16. * Registers which hook the block will be displayed on.
  17. *
  18. * @param string $hook Hook to display the block on.
  19. */
  20. public function __construct( $hook ) {
  21. $this->hook = $hook;
  22. }
  23. /**
  24. * Registers WordPress hooks.
  25. *
  26. * @return void
  27. */
  28. public function register_hooks() {
  29. if ( ! $this->is_hidden() ) {
  30. add_action( $this->hook, array( $this, 'render' ) );
  31. }
  32. }
  33. /**
  34. * Renders the upsell block.
  35. *
  36. * @return void
  37. */
  38. public function render() {
  39. $url = WPSEO_Shortlinker::get( 'https://yoa.st/17h' );
  40. $arguments = array(
  41. '<strong>' . esc_html__( 'Multiple keywords', 'wordpress-seo' ) . '</strong>: ' . esc_html__( 'Increase your SEO reach', 'wordpress-seo' ),
  42. '<strong>' . esc_html__( 'No more dead links', 'wordpress-seo' ) . '</strong>: ' . esc_html__( 'Easy redirect manager', 'wordpress-seo' ),
  43. '<strong>' . esc_html__( 'Superfast internal linking suggestions', 'wordpress-seo' ) . '</strong>',
  44. '<strong>' . esc_html__( 'Social media preview', 'wordpress-seo' ) . '</strong>: ' . esc_html__( 'Facebook & Twitter', 'wordpress-seo' ),
  45. '<strong>' . esc_html__( '24/7 support', 'wordpress-seo' ) . '</strong>',
  46. '<strong>' . esc_html__( 'No ads!', 'wordpress-seo' ) . '</strong>',
  47. );
  48. $arguments_html = implode( '', array_map( array( $this, 'get_argument_html' ), $arguments ) );
  49. $class = $this->get_html_class();
  50. /* translators: %s expands to "Yoast SEO Premium". */
  51. $dismiss_msg = sprintf( __( 'Dismiss %s upgrade motivation', 'wordpress-seo' ), 'Yoast SEO Premium' );
  52. /* translators: %s expands to "Yoast SEO Premium". */
  53. $upgrade_msg = sprintf( __( 'Find out why you should upgrade to %s &raquo;', 'wordpress-seo' ), 'Yoast SEO Premium' );
  54. echo '<div class="' . esc_attr( $class ) . '">';
  55. printf(
  56. '<a href="%1$s" style="" class="alignright %2$s" aria-label="%3$s">X</a>',
  57. esc_url( add_query_arg( array( $this->get_query_variable_name() => 1 ) ) ),
  58. esc_attr( $class . '--close' ),
  59. esc_attr( $dismiss_msg )
  60. );
  61. echo '<div>';
  62. echo '<h2 class="' . esc_attr( $class . '--header' ) . '">' . esc_html__( 'Go premium!', 'wordpress-seo' ) . '</h2>';
  63. echo '<ul class="' . esc_attr( $class . '--motivation' ) . '">' . $arguments_html . '</ul>';
  64. echo '<p><a href="' . esc_url( $url ) . '" target="_blank">' . esc_html( $upgrade_msg ) . '</a><br />';
  65. echo '</div>';
  66. echo '</div>';
  67. }
  68. /**
  69. * Formats the argument to a HTML list item.
  70. *
  71. * @param string $argument The argument to format.
  72. *
  73. * @return string Formatted argument in HTML.
  74. */
  75. protected function get_argument_html( $argument ) {
  76. $class = $this->get_html_class();
  77. return sprintf(
  78. '<li><div class="%1$s">%2$s</div></li>',
  79. esc_attr( $class . '--argument' ),
  80. $argument
  81. );
  82. }
  83. /**
  84. * Checks if the block is hidden by the user.
  85. *
  86. * @return bool False when it should be shown, True if it should be hidden.
  87. */
  88. protected function is_hidden() {
  89. $transient_name = $this->get_option_name();
  90. $hide = (bool) get_user_option( $transient_name );
  91. if ( ! $hide ) {
  92. $query_variable_name = $this->get_query_variable_name();
  93. if ( filter_input( INPUT_GET, $query_variable_name, FILTER_VALIDATE_INT ) === 1 ) {
  94. // No expiration time, so this would normally not expire, but it wouldn't be copied to other sites etc.
  95. update_user_option( get_current_user_id(), $transient_name, true );
  96. $hide = true;
  97. }
  98. }
  99. return $hide;
  100. }
  101. /**
  102. * Retrieves the option name to use.
  103. *
  104. * @return string The name of the option to save the data in.
  105. */
  106. protected function get_option_name() {
  107. return 'yoast_promo_hide_' . $this->identifier;
  108. }
  109. /**
  110. * Retrieves the query variable to use for dismissing the block.
  111. *
  112. * @return string The name of the query variable to use.
  113. */
  114. protected function get_query_variable_name() {
  115. return 'yoast_promo_hide_' . $this->identifier;
  116. }
  117. /**
  118. * Returns the HTML base class to use.
  119. *
  120. * @return string The HTML base class.
  121. */
  122. protected function get_html_class() {
  123. return 'yoast_' . $this->identifier;
  124. }
  125. }