| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional; |
| 9 |
use Yoast\WP\SEO\Promotions\Application\Promotion_Manager; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class WPSEO_Premium_Upsell_Admin_Block |
| 13 |
*/ |
| 14 |
class WPSEO_Premium_Upsell_Admin_Block { |
| 15 |
|
| 16 |
/** |
| 17 |
* Hook to display the block on. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $hook; |
| 22 |
|
| 23 |
/** |
| 24 |
* Identifier to use in the dismissal functionality. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $identifier = 'premium_upsell'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Registers which hook the block will be displayed on. |
| 32 |
* |
| 33 |
* @param string $hook Hook to display the block on. |
| 34 |
*/ |
| 35 |
public function __construct( $hook ) { |
| 36 |
$this->hook = $hook; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Registers WordPress hooks. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function register_hooks() { |
| 45 |
add_action( $this->hook, [ $this, 'render' ] ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Renders the upsell block. |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function render() { |
| 54 |
|
| 55 |
$is_woocommerce_active = ( new WooCommerce_Conditional() )->is_met(); |
| 56 |
$url = ( $is_woocommerce_active ) ? WPSEO_Shortlinker::get( 'https://yoa.st/admin-footer-upsell-woocommerce' ) : WPSEO_Shortlinker::get( 'https://yoa.st/17h' ); |
| 57 |
|
| 58 |
[ $header_text, $header_icon ] = $this->get_header( $is_woocommerce_active ); |
| 59 |
|
| 60 |
$arguments = $this->get_arguments( $is_woocommerce_active ); |
| 61 |
|
| 62 |
$now_including = [ 'Local SEO', 'News SEO', 'Video SEO', __( 'Google Docs add-on (1 seat)', 'wordpress-seo' ) ]; |
| 63 |
if ( $is_woocommerce_active ) { |
| 64 |
array_unshift( $now_including, 'Yoast SEO Premium' ); |
| 65 |
} |
| 66 |
|
| 67 |
$header_class = ( $is_woocommerce_active ) ? 'woo-header' : ''; |
| 68 |
$arguments_html = implode( '', array_map( [ $this, 'get_argument_html' ], $arguments ) ); |
| 69 |
$badge_class = ( $is_woocommerce_active ) ? 'woo-badge' : ''; |
| 70 |
|
| 71 |
$class = $this->get_html_class(); |
| 72 |
|
| 73 |
/* translators: %s expands to Yoast SEO Premium */ |
| 74 |
$button_text = $this->get_button_text( $is_woocommerce_active ); |
| 75 |
/* translators: Hidden accessibility text. */ |
| 76 |
$button_text .= '<span class="screen-reader-text">' . esc_html__( '(Opens in a new browser tab)', 'wordpress-seo' ) . '</span>' |
| 77 |
. '<span aria-hidden="true" class="yoast-button-upsell__caret"></span>'; |
| 78 |
|
| 79 |
$upgrade_button = sprintf( |
| 80 |
'<a id="%1$s" class="yoast-button-upsell" data-action="load-nfd-ctb" data-ctb-id="f6a84663-465f-4cb5-8ba5-f7a6d72224b2" href="%2$s" target="_blank">%3$s</a>', |
| 81 |
esc_attr( 'wpseo-' . $this->identifier . '-popup-button' ), |
| 82 |
esc_url( $url ), |
| 83 |
$button_text, |
| 84 |
); |
| 85 |
|
| 86 |
echo '<div class="' . esc_attr( $class ) . '">'; |
| 87 |
|
| 88 |
if ( YoastSEO()->classes->get( Promotion_Manager::class )->is( 'black-friday-promotion' ) ) { |
| 89 |
$bf_label = esc_html__( 'BLACK FRIDAY', 'wordpress-seo' ); |
| 90 |
$sale_label = esc_html__( '30% OFF', 'wordpress-seo' ); |
| 91 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Already escaped above. |
| 92 |
echo "<div class='black-friday-container'><span>$sale_label</span> <span style='margin-left: auto;'>$bf_label</span> </div>"; |
| 93 |
} |
| 94 |
|
| 95 |
echo '<div class="' . esc_attr( $class . '--container' ) . '">'; |
| 96 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Correctly escaped in get_header() method. |
| 97 |
echo '<h2 class="' . esc_attr( $class . '--header' ) . ' ' . esc_attr( $header_class ) . ' ">' . $header_text . $header_icon . '</h2>'; |
| 98 |
|
| 99 |
echo '<div class="' . esc_attr( $class . '--subheader' ) . '">'; |
| 100 |
echo '<span style="margin-right: 8px">' . esc_html__( 'Now includes:', 'wordpress-seo' ) . '</span>'; |
| 101 |
echo '<div style="display: inline-block;">'; |
| 102 |
foreach ( $now_including as $value ) { |
| 103 |
echo '<span class="yoast-badge ' . esc_attr( $class . '--badge' ) . ' ' . esc_attr( $badge_class ) . '">' . esc_html( $value ) . '</span>'; |
| 104 |
} |
| 105 |
echo '</div>'; |
| 106 |
echo '</div>'; |
| 107 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Correctly escaped in $this->get_argument_html() method. |
| 108 |
echo '<ul class="' . esc_attr( $class . '--motivation' ) . '">' . $arguments_html . '</ul>'; |
| 109 |
|
| 110 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Correctly escaped in $upgrade_button and $button_text above. |
| 111 |
echo '<p style="max-width: inherit; margin-top: 24px; margin-bottom: 0;">' . $upgrade_button . '</p>'; |
| 112 |
echo '</div>'; |
| 113 |
|
| 114 |
echo '</div>'; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Formats the argument to a HTML list item. |
| 119 |
* |
| 120 |
* @param string $argument The argument to format. |
| 121 |
* |
| 122 |
* @return string Formatted argument in HTML. |
| 123 |
*/ |
| 124 |
protected function get_argument_html( $argument ) { |
| 125 |
$assets_uri = trailingslashit( plugin_dir_url( WPSEO_FILE ) ); |
| 126 |
$class = $this->get_html_class(); |
| 127 |
|
| 128 |
return sprintf( |
| 129 |
'<li style="line-height: 19.5px"><img src="%1$s" alt="" width="19.5" height="19.5"><div class="%2$s">%3$s</div></li>', |
| 130 |
esc_url( $assets_uri . 'packages/js/images/icon-check-circle-green.svg' ), |
| 131 |
esc_attr( $class . '--argument' ), |
| 132 |
$argument, |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Returns the HTML base class to use. |
| 138 |
* |
| 139 |
* @return string The HTML base class. |
| 140 |
*/ |
| 141 |
protected function get_html_class() { |
| 142 |
return 'yoast_' . $this->identifier; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Returns the arguments based on whether WooCommerce is active. |
| 147 |
* |
| 148 |
* @param bool $is_woocommerce_active Whether WooCommerce is active. |
| 149 |
* |
| 150 |
* @return array<string> The arguments list. |
| 151 |
*/ |
| 152 |
private function get_arguments( bool $is_woocommerce_active ) { |
| 153 |
$arguments = [ |
| 154 |
esc_html__( 'Generate SEO optimized metadata in seconds with AI', 'wordpress-seo' ), |
| 155 |
esc_html__( 'Make your articles visible, be seen in Google News', 'wordpress-seo' ), |
| 156 |
esc_html__( 'Built to get found by search, AI, and real users', 'wordpress-seo' ), |
| 157 |
esc_html__( 'Easy Local SEO. Show up in Google Maps results', 'wordpress-seo' ), |
| 158 |
esc_html__( 'Internal links and redirect management, easy', 'wordpress-seo' ), |
| 159 |
esc_html__( 'Access to friendly help when you need it, day or night', 'wordpress-seo' ), |
| 160 |
]; |
| 161 |
|
| 162 |
if ( $is_woocommerce_active ) { |
| 163 |
$arguments[1] = esc_html__( 'Boost visibility for your products, from 10 or 10,000+', 'wordpress-seo' ); |
| 164 |
} |
| 165 |
|
| 166 |
return $arguments; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Returns the header text and icon based on whether WooCommerce is active. |
| 171 |
* |
| 172 |
* @param bool $is_woocommerce_active Whether WooCommerce is active. |
| 173 |
* |
| 174 |
* @return array<string, string> The header text and icon. |
| 175 |
*/ |
| 176 |
private function get_header( bool $is_woocommerce_active ) { |
| 177 |
$assets_uri = trailingslashit( plugin_dir_url( WPSEO_FILE ) ); |
| 178 |
if ( $is_woocommerce_active ) { |
| 179 |
$header_text = sprintf( |
| 180 |
/* translators: %s expands to Yoast WooCommerce SEO */ |
| 181 |
esc_html__( 'Upgrade to %s', 'wordpress-seo' ), |
| 182 |
'Yoast WooCommerce SEO', |
| 183 |
); |
| 184 |
$header_icon = sprintf( |
| 185 |
'<img src="%s" alt="" width="14" height="14" style="margin-inline-start: 8px;">', |
| 186 |
esc_url( $assets_uri . 'packages/js/images/icon-trolley.svg' ), |
| 187 |
); |
| 188 |
} |
| 189 |
else { |
| 190 |
$header_text = sprintf( |
| 191 |
/* translators: %s expands to Yoast SEO Premium*/ |
| 192 |
esc_html__( 'Upgrade to %s', 'wordpress-seo' ), |
| 193 |
'Yoast SEO Premium', |
| 194 |
); |
| 195 |
|
| 196 |
$header_icon = sprintf( |
| 197 |
'<img src="%s" alt="" width="14" height="14" style="margin-inline-start: 8px;">', |
| 198 |
esc_url( $assets_uri . 'packages/js/images/icon-crown.svg' ), |
| 199 |
); |
| 200 |
} |
| 201 |
return [ $header_text, $header_icon ]; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Returns the button text based on whether WooCommerce is active. |
| 206 |
* |
| 207 |
* @param bool $is_woocommerce_active Whether WooCommerce is active. |
| 208 |
* |
| 209 |
* @return string The button text. |
| 210 |
*/ |
| 211 |
private function get_button_text( bool $is_woocommerce_active ): string { |
| 212 |
if ( YoastSEO()->classes->get( Promotion_Manager::class )->is( 'black-friday-promotion' ) ) { |
| 213 |
return esc_html__( 'Get 30% off now!', 'wordpress-seo' ); |
| 214 |
} |
| 215 |
else { |
| 216 |
// phpcs:disable Squiz.ControlStructures.InlineIfDeclaration.NotSingleLine -- needed to add translators comments. |
| 217 |
return $is_woocommerce_active |
| 218 |
/* translators: %s expands to Yoast WooCommerce SEO */ |
| 219 |
? sprintf( esc_html__( 'Explore %s now!', 'wordpress-seo' ), 'Yoast WooCommerce SEO' ) |
| 220 |
/* translators: %s expands to Yoast SEO Premium */ |
| 221 |
: sprintf( esc_html__( 'Explore %s now!', 'wordpress-seo' ), 'Yoast SEO Premium' ); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|