| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Plans\User_Interface; |
| 4 |
|
| 5 |
use WPSEO_Admin_Asset_Manager; |
| 6 |
use Yoast\WP\SEO\Conditionals\Admin_Conditional; |
| 7 |
use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 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\Short_Link_Helper; |
| 11 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 12 |
use Yoast\WP\SEO\Plans\Application\Add_Ons_Collector; |
| 13 |
use Yoast\WP\SEO\Plans\Application\Duplicate_Post_Manager; |
| 14 |
use Yoast\WP\SEO\Promotions\Application\Promotion_Manager; |
| 15 |
|
| 16 |
/** |
| 17 |
* Adds the plans page to the Yoast admin menu. |
| 18 |
*/ |
| 19 |
class Plans_Page_Integration implements Integration_Interface { |
| 20 |
|
| 21 |
use No_Conditionals; |
| 22 |
|
| 23 |
/** |
| 24 |
* The page name. |
| 25 |
*/ |
| 26 |
public const PAGE = 'wpseo_licenses'; |
| 27 |
|
| 28 |
/** |
| 29 |
* The assets name. |
| 30 |
*/ |
| 31 |
public const ASSETS_NAME = 'plans'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Holds the WPSEO_Admin_Asset_Manager. |
| 35 |
* |
| 36 |
* @var WPSEO_Admin_Asset_Manager |
| 37 |
*/ |
| 38 |
private $asset_manager; |
| 39 |
|
| 40 |
/** |
| 41 |
* Holds the Add_Ons_Collector. |
| 42 |
* |
| 43 |
* @var Add_Ons_Collector |
| 44 |
*/ |
| 45 |
private $add_ons_collector; |
| 46 |
|
| 47 |
/** |
| 48 |
* Holds the Current_Page_Helper. |
| 49 |
* |
| 50 |
* @var Current_Page_Helper |
| 51 |
*/ |
| 52 |
private $current_page_helper; |
| 53 |
|
| 54 |
/** |
| 55 |
* Holds the Short_Link_Helper. |
| 56 |
* |
| 57 |
* @var Short_Link_Helper |
| 58 |
*/ |
| 59 |
private $short_link_helper; |
| 60 |
|
| 61 |
/** |
| 62 |
* Holds the Admin_Conditional. |
| 63 |
* |
| 64 |
* @var Admin_Conditional |
| 65 |
*/ |
| 66 |
private $admin_conditional; |
| 67 |
|
| 68 |
/** |
| 69 |
* The promotion manager. |
| 70 |
* |
| 71 |
* @var Promotion_Manager |
| 72 |
*/ |
| 73 |
private $promotion_manager; |
| 74 |
|
| 75 |
/** |
| 76 |
* The Duplicate Post plugin manager. |
| 77 |
* |
| 78 |
* @var Duplicate_Post_Manager |
| 79 |
*/ |
| 80 |
private $duplicate_post_manager; |
| 81 |
|
| 82 |
/** |
| 83 |
* Constructs the instance. |
| 84 |
* |
| 85 |
* @param WPSEO_Admin_Asset_Manager $asset_manager The WPSEO_Admin_Asset_Manager. |
| 86 |
* @param Add_Ons_Collector $add_ons_collector The Add_Ons_Collector. |
| 87 |
* @param Current_Page_Helper $current_page_helper The Current_Page_Helper. |
| 88 |
* @param Short_Link_Helper $short_link_helper The Short_Link_Helper. |
| 89 |
* @param Admin_Conditional $admin_conditional The Admin_Conditional. |
| 90 |
* @param Promotion_Manager $promotion_manager The promotion manager. |
| 91 |
* @param Duplicate_Post_Manager $duplicate_post_manager The Duplicate_Post_Manager. |
| 92 |
*/ |
| 93 |
public function __construct( |
| 94 |
WPSEO_Admin_Asset_Manager $asset_manager, |
| 95 |
Add_Ons_Collector $add_ons_collector, |
| 96 |
Current_Page_Helper $current_page_helper, |
| 97 |
Short_Link_Helper $short_link_helper, |
| 98 |
Admin_Conditional $admin_conditional, |
| 99 |
Promotion_Manager $promotion_manager, |
| 100 |
Duplicate_Post_Manager $duplicate_post_manager |
| 101 |
) { |
| 102 |
$this->asset_manager = $asset_manager; |
| 103 |
$this->add_ons_collector = $add_ons_collector; |
| 104 |
$this->current_page_helper = $current_page_helper; |
| 105 |
$this->short_link_helper = $short_link_helper; |
| 106 |
$this->admin_conditional = $admin_conditional; |
| 107 |
$this->promotion_manager = $promotion_manager; |
| 108 |
$this->duplicate_post_manager = $duplicate_post_manager; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Initializes the integration. |
| 113 |
* |
| 114 |
* This is the place to register hooks and filters. |
| 115 |
* |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public function register_hooks() { |
| 119 |
// Add page with priority 7 to add it above the workouts. |
| 120 |
\add_filter( 'wpseo_submenu_pages', [ $this, 'add_page' ], 7 ); |
| 121 |
\add_filter( 'wpseo_network_submenu_pages', [ $this, 'add_page' ], 7 ); |
| 122 |
|
| 123 |
// Are we on our page? |
| 124 |
if ( $this->admin_conditional->is_met() && $this->current_page_helper->get_current_yoast_seo_page() === self::PAGE ) { |
| 125 |
\add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); |
| 126 |
\add_action( 'in_admin_header', [ $this, 'remove_notices' ], \PHP_INT_MAX ); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Adds the page to the (currently) last position in the array. |
| 132 |
* |
| 133 |
* @param array<string, array<string, array<static|string>>> $pages The pages. |
| 134 |
* |
| 135 |
* @return array<string, array<string, array<static|string>>> The pages. |
| 136 |
*/ |
| 137 |
public function add_page( $pages ) { |
| 138 |
$pages[] = [ |
| 139 |
General_Page_Integration::PAGE, |
| 140 |
'', |
| 141 |
\__( 'Plans', 'wordpress-seo' ), |
| 142 |
'wpseo_manage_options', |
| 143 |
self::PAGE, |
| 144 |
[ $this, 'display_page' ], |
| 145 |
]; |
| 146 |
|
| 147 |
return $pages; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Displays the page. |
| 152 |
* |
| 153 |
* @return void |
| 154 |
*/ |
| 155 |
public function display_page() { |
| 156 |
echo '<div id="yoast-seo-plans"></div>'; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Enqueues the assets. |
| 161 |
* |
| 162 |
* @return void |
| 163 |
*/ |
| 164 |
public function enqueue_assets() { |
| 165 |
// Remove the emoji script as it is incompatible with both React and any contenteditable fields. |
| 166 |
\remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
| 167 |
$this->asset_manager->enqueue_script( self::ASSETS_NAME ); |
| 168 |
$this->asset_manager->enqueue_style( self::ASSETS_NAME ); |
| 169 |
$this->asset_manager->localize_script( self::ASSETS_NAME, 'wpseoScriptData', $this->get_script_data() ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Creates the script data. |
| 174 |
* |
| 175 |
* @return array<string,array<string, string|bool|array<string, string>>> The script data. |
| 176 |
*/ |
| 177 |
private function get_script_data(): array { |
| 178 |
return [ |
| 179 |
'addOns' => $this->add_ons_collector->to_array(), |
| 180 |
'linkParams' => $this->short_link_helper->get_query_params(), |
| 181 |
'preferences' => [ |
| 182 |
'isRtl' => \is_rtl(), |
| 183 |
], |
| 184 |
'currentPromotions' => $this->promotion_manager->get_current_promotions(), |
| 185 |
'duplicatePost' => $this->duplicate_post_manager->get_params(), |
| 186 |
'userCan' => [ |
| 187 |
'installPlugin' => \current_user_can( 'install_plugins' ), |
| 188 |
'activatePlugin' => \current_user_can( 'activate_plugins' ), |
| 189 |
], |
| 190 |
]; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Removes all current WP notices. |
| 195 |
* |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
public function remove_notices() { |
| 199 |
\remove_all_actions( 'admin_notices' ); |
| 200 |
\remove_all_actions( 'user_admin_notices' ); |
| 201 |
\remove_all_actions( 'network_admin_notices' ); |
| 202 |
\remove_all_actions( 'all_admin_notices' ); |
| 203 |
} |
| 204 |
} |
| 205 |
|