| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations; |
| 4 |
|
| 5 |
use WPSEO_Addon_Manager; |
| 6 |
use WPSEO_Admin_Asset_Manager; |
| 7 |
use Yoast\WP\SEO\Conditionals\Admin_Conditional; |
| 8 |
use Yoast\WP\SEO\Conditionals\User_Can_Manage_Wpseo_Options_Conditional; |
| 9 |
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional; |
| 10 |
use Yoast\WP\SEO\Helpers\Current_Page_Helper; |
| 11 |
use Yoast\WP\SEO\Helpers\Product_Helper; |
| 12 |
use Yoast\WP\SEO\Helpers\Short_Link_Helper; |
| 13 |
use Yoast\WP\SEO\Promotions\Application\Promotion_Manager; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Support_Integration. |
| 17 |
*/ |
| 18 |
class Support_Integration implements Integration_Interface { |
| 19 |
|
| 20 |
public const PAGE = 'wpseo_page_support'; |
| 21 |
|
| 22 |
public const CAPABILITY = 'wpseo_manage_options'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Holds the WPSEO_Admin_Asset_Manager. |
| 26 |
* |
| 27 |
* @var WPSEO_Admin_Asset_Manager |
| 28 |
*/ |
| 29 |
private $asset_manager; |
| 30 |
|
| 31 |
/** |
| 32 |
* Holds the Current_Page_Helper. |
| 33 |
* |
| 34 |
* @var Current_Page_Helper |
| 35 |
*/ |
| 36 |
private $current_page_helper; |
| 37 |
|
| 38 |
/** |
| 39 |
* Holds the Product_Helper. |
| 40 |
* |
| 41 |
* @var Product_Helper |
| 42 |
*/ |
| 43 |
private $product_helper; |
| 44 |
|
| 45 |
/** |
| 46 |
* Holds the Short_Link_Helper. |
| 47 |
* |
| 48 |
* @var Short_Link_Helper |
| 49 |
*/ |
| 50 |
private $shortlink_helper; |
| 51 |
|
| 52 |
/** |
| 53 |
* Holds the WooCommerce_Conditional. |
| 54 |
* |
| 55 |
* @var WooCommerce_Conditional |
| 56 |
*/ |
| 57 |
private $woocommerce_conditional; |
| 58 |
|
| 59 |
/** |
| 60 |
* Holds the WPSEO_Addon_Manager. |
| 61 |
* |
| 62 |
* @var WPSEO_Addon_Manager |
| 63 |
*/ |
| 64 |
private $addon_manager; |
| 65 |
|
| 66 |
/** |
| 67 |
* Constructs Support_Integration. |
| 68 |
* |
| 69 |
* @param WPSEO_Admin_Asset_Manager $asset_manager The WPSEO_Admin_Asset_Manager. |
| 70 |
* @param Current_Page_Helper $current_page_helper The Current_Page_Helper. |
| 71 |
* @param Product_Helper $product_helper The Product_Helper. |
| 72 |
* @param Short_Link_Helper $shortlink_helper The Short_Link_Helper. |
| 73 |
* @param WooCommerce_Conditional $woocommerce_conditional The WooCommerce_Conditional. |
| 74 |
* @param WPSEO_Addon_Manager $addon_manager The WPSEO_Addon_Manager. |
| 75 |
*/ |
| 76 |
public function __construct( |
| 77 |
WPSEO_Admin_Asset_Manager $asset_manager, |
| 78 |
Current_Page_Helper $current_page_helper, |
| 79 |
Product_Helper $product_helper, |
| 80 |
Short_Link_Helper $shortlink_helper, |
| 81 |
WooCommerce_Conditional $woocommerce_conditional, |
| 82 |
WPSEO_Addon_Manager $addon_manager |
| 83 |
) { |
| 84 |
$this->asset_manager = $asset_manager; |
| 85 |
$this->current_page_helper = $current_page_helper; |
| 86 |
$this->product_helper = $product_helper; |
| 87 |
$this->shortlink_helper = $shortlink_helper; |
| 88 |
$this->woocommerce_conditional = $woocommerce_conditional; |
| 89 |
$this->addon_manager = $addon_manager; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns the conditionals based on which this loadable should be active. |
| 94 |
* |
| 95 |
* @return array<string> |
| 96 |
*/ |
| 97 |
public static function get_conditionals() { |
| 98 |
return [ Admin_Conditional::class, User_Can_Manage_Wpseo_Options_Conditional::class ]; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Initializes the integration. |
| 103 |
* |
| 104 |
* This is the place to register hooks and filters. |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function register_hooks() { |
| 109 |
// Add page using PHP_INT_MAX - 1 to allow other items (like Brand Insights) to be positioned after. |
| 110 |
\add_filter( 'wpseo_submenu_pages', [ $this, 'add_page' ], ( \PHP_INT_MAX - 1 ) ); |
| 111 |
|
| 112 |
// Are we on the settings page? |
| 113 |
if ( $this->current_page_helper->get_current_yoast_seo_page() === self::PAGE ) { |
| 114 |
\add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); |
| 115 |
\add_action( 'in_admin_header', [ $this, 'remove_notices' ], \PHP_INT_MAX ); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Adds the page. |
| 121 |
* |
| 122 |
* @param array<array<string|callable>> $pages The pages. |
| 123 |
* |
| 124 |
* @return array<array<string|callable>> The pages. |
| 125 |
*/ |
| 126 |
public function add_page( array $pages ) { |
| 127 |
$pages[] = [ |
| 128 |
'wpseo_dashboard', |
| 129 |
'', |
| 130 |
\__( 'Support', 'wordpress-seo' ), |
| 131 |
self::CAPABILITY, |
| 132 |
self::PAGE, |
| 133 |
[ $this, 'display_page' ], |
| 134 |
]; |
| 135 |
|
| 136 |
return $pages; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Displays the page. |
| 141 |
* |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
public function display_page() { |
| 145 |
echo '<div id="yoast-seo-support"></div>'; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Enqueues the assets. |
| 150 |
* |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
public function enqueue_assets() { |
| 154 |
// Remove the emoji script as it is incompatible with both React and any contenteditable fields. |
| 155 |
\remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
| 156 |
$this->asset_manager->enqueue_script( 'support' ); |
| 157 |
$this->asset_manager->enqueue_style( 'support' ); |
| 158 |
if ( \YoastSEO()->classes->get( Promotion_Manager::class )->is( 'black-friday-promotion' ) ) { |
| 159 |
$this->asset_manager->enqueue_style( 'black-friday-banner' ); |
| 160 |
} |
| 161 |
$this->asset_manager->localize_script( 'support', 'wpseoScriptData', $this->get_script_data() ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Removes all current WP notices. |
| 166 |
* |
| 167 |
* @return void |
| 168 |
*/ |
| 169 |
public function remove_notices() { |
| 170 |
\remove_all_actions( 'admin_notices' ); |
| 171 |
\remove_all_actions( 'user_admin_notices' ); |
| 172 |
\remove_all_actions( 'network_admin_notices' ); |
| 173 |
\remove_all_actions( 'all_admin_notices' ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Creates the script data. |
| 178 |
* |
| 179 |
* @return array<string, array<array<string, bool|string|array<string>>, bool, string>> The script data. |
| 180 |
*/ |
| 181 |
public function get_script_data() { |
| 182 |
return [ |
| 183 |
'preferences' => [ |
| 184 |
'hasPremiumSubscription' => $this->addon_manager->has_active_addons() && $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ), |
| 185 |
'hasWooSeoSubscription' => $this->addon_manager->has_active_addons() && $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::WOOCOMMERCE_SLUG ), |
| 186 |
'isRtl' => \is_rtl(), |
| 187 |
'pluginUrl' => \plugins_url( '', \WPSEO_FILE ), |
| 188 |
'upsellSettings' => [ |
| 189 |
'actionId' => 'load-nfd-ctb', |
| 190 |
'premiumCtbId' => 'f6a84663-465f-4cb5-8ba5-f7a6d72224b2', |
| 191 |
], |
| 192 |
'isWooCommerceActive' => $this->woocommerce_conditional->is_met(), |
| 193 |
], |
| 194 |
'linkParams' => $this->shortlink_helper->get_query_params(), |
| 195 |
'currentPromotions' => \YoastSEO()->classes->get( Promotion_Manager::class )->get_current_promotions(), |
| 196 |
]; |
| 197 |
} |
| 198 |
} |
| 199 |
|