|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Yoast\WP\SEO\Plans\User_Interface; |
| 4 | + |
| 5 | +use WPSEO_Shortlinker; |
| 6 | +use Yoast\WP\SEO\Conditionals\Traits\Admin_Conditional_Trait; |
| 7 | +use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional; |
| 8 | +use Yoast\WP\SEO\General\User_Interface\General_Page_Integration; |
| 9 | +use Yoast\WP\SEO\Helpers\Current_Page_Helper; |
| 10 | +use Yoast\WP\SEO\Helpers\Product_Helper; |
| 11 | +use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 12 | + |
| 13 | +/** |
| 14 | + * Adds the plans page to the Yoast admin menu. |
| 15 | + */ |
| 16 | +class Upgrade_Sidebar_Menu_Integration implements Integration_Interface { |
| 17 | + |
| 18 | + use Admin_Conditional_Trait; |
| 19 | + |
| 20 | + /** |
| 21 | + * The page name. |
| 22 | + */ |
| 23 | + public const PAGE = 'wpseo_upgrade_sidebar'; |
| 24 | + |
| 25 | + /** |
| 26 | + * The WooCommerce conditional. |
| 27 | + * |
| 28 | + * @var WooCommerce_Conditional |
| 29 | + */ |
| 30 | + private $woocommerce_conditional; |
| 31 | + |
| 32 | + /** |
| 33 | + * The shortlinker. |
| 34 | + * |
| 35 | + * @var WPSEO_Shortlinker |
| 36 | + */ |
| 37 | + private $shortlinker; |
| 38 | + |
| 39 | + /** |
| 40 | + * The product helper. |
| 41 | + * |
| 42 | + * @var Product_Helper |
| 43 | + */ |
| 44 | + private $product_helper; |
| 45 | + |
| 46 | + /** |
| 47 | + * The current page helper. |
| 48 | + * |
| 49 | + * @var Current_Page_Helper |
| 50 | + */ |
| 51 | + private $current_page_helper; |
| 52 | + |
| 53 | + /** |
| 54 | + * Constructor. |
| 55 | + * |
| 56 | + * @param WooCommerce_Conditional $woocommerce_conditional The WooCommerce conditional. |
| 57 | + * @param WPSEO_Shortlinker $shortlinker The shortlinker. |
| 58 | + * @param Product_Helper $product_helper The product helper. |
| 59 | + * @param Current_Page_Helper $current_page_helper The current page helper. |
| 60 | + */ |
| 61 | + public function __construct( |
| 62 | + WooCommerce_Conditional $woocommerce_conditional, |
| 63 | + WPSEO_Shortlinker $shortlinker, |
| 64 | + Product_Helper $product_helper, |
| 65 | + Current_Page_Helper $current_page_helper |
| 66 | + ) { |
| 67 | + $this->woocommerce_conditional = $woocommerce_conditional; |
| 68 | + $this->shortlinker = $shortlinker; |
| 69 | + $this->product_helper = $product_helper; |
| 70 | + $this->current_page_helper = $current_page_helper; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Initializes the integration. |
| 75 | + * |
| 76 | + * This is the place to register hooks and filters. |
| 77 | + * |
| 78 | + * @return void |
| 79 | + */ |
| 80 | + public function register_hooks() { |
| 81 | + // Add page with PHP_INT_MAX so its always the last item. |
| 82 | + \add_filter( 'wpseo_submenu_pages', [ $this, 'add_page' ], \PHP_INT_MAX ); |
| 83 | + \add_filter( 'wpseo_network_submenu_pages', [ $this, 'add_page' ], \PHP_INT_MAX ); |
| 84 | + \add_action( 'admin_init', [ $this, 'do_redirect' ], 1 ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Adds the page to the (currently) last position in the array. |
| 89 | + * |
| 90 | + * @param array<string, array<string, array<static|string>>> $pages The pages. |
| 91 | + * |
| 92 | + * @return array<string, array<string, array<static|string>>> The pages. |
| 93 | + */ |
| 94 | + public function add_page( $pages ) { |
| 95 | + |
| 96 | + if ( ! $this->product_helper->is_premium() ) { |
| 97 | + $pages[] = [ |
| 98 | + General_Page_Integration::PAGE, |
| 99 | + '', |
| 100 | + '<span class="yst-root"><span class="yst-button yst-w-full yst-button--upsell yst-button--small">' . \__( 'Upgrade', 'wordpress-seo' ) . ' </span></span>', |
| 101 | + 'wpseo_manage_options', |
| 102 | + self::PAGE, |
| 103 | + static function () { |
| 104 | + echo 'redirecting...'; |
| 105 | + }, |
| 106 | + ]; |
| 107 | + } |
| 108 | + |
| 109 | + return $pages; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Redirects to the yoast.com. |
| 114 | + * |
| 115 | + * @return void |
| 116 | + */ |
| 117 | + public function do_redirect(): void { |
| 118 | + |
| 119 | + if ( $this->current_page_helper->get_current_yoast_seo_page() !== self::PAGE ) { |
| 120 | + return; |
| 121 | + } |
| 122 | + $link = $this->shortlinker->build_shortlink( 'https://yoa.st/wordpress-menu-upgrade-premium' ); |
| 123 | + if ( $this->woocommerce_conditional->is_met() ) { |
| 124 | + $link = $this->shortlinker->build_shortlink( 'https://yoa.st/wordpress-menu-upgrade-woocommerce' ); |
| 125 | + } |
| 126 | + |
| 127 | + \wp_redirect( $link );//phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- Safe redirect is used here. |
| 128 | + exit; |
| 129 | + } |
| 130 | +} |
0 commit comments