| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Admin; |
| 4 |
|
| 5 |
use WPSEO_Admin_Asset_Manager; |
| 6 |
use Yoast\WP\SEO\Conditionals\Admin_Conditional; |
| 7 |
use Yoast\WP\SEO\Conditionals\Installation_Success_Conditional; |
| 8 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 9 |
use Yoast\WP\SEO\Helpers\Product_Helper; |
| 10 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 11 |
|
| 12 |
/** |
| 13 |
* Installation_Success_Integration class |
| 14 |
*/ |
| 15 |
class Installation_Success_Integration implements Integration_Interface { |
| 16 |
|
| 17 |
/** |
| 18 |
* The options helper. |
| 19 |
* |
| 20 |
* @var Options_Helper |
| 21 |
*/ |
| 22 |
protected $options_helper; |
| 23 |
|
| 24 |
/** |
| 25 |
* The product helper. |
| 26 |
* |
| 27 |
* @var Product_Helper |
| 28 |
*/ |
| 29 |
protected $product_helper; |
| 30 |
|
| 31 |
/** |
| 32 |
* {@inheritDoc} |
| 33 |
*/ |
| 34 |
public static function get_conditionals() { |
| 35 |
return [ Admin_Conditional::class, Installation_Success_Conditional::class ]; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Installation_Success_Integration constructor. |
| 40 |
* |
| 41 |
* @param Options_Helper $options_helper The options helper. |
| 42 |
* @param Product_Helper $product_helper The product helper. |
| 43 |
*/ |
| 44 |
public function __construct( Options_Helper $options_helper, Product_Helper $product_helper ) { |
| 45 |
$this->options_helper = $options_helper; |
| 46 |
$this->product_helper = $product_helper; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* {@inheritDoc} |
| 51 |
*/ |
| 52 |
public function register_hooks() { |
| 53 |
\add_filter( 'admin_menu', [ $this, 'add_submenu_page' ], 9 ); |
| 54 |
\add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); |
| 55 |
\add_action( 'admin_init', [ $this, 'maybe_redirect' ] ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Redirects to the installation success page if an installation has just occurred. |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public function maybe_redirect() { |
| 64 |
if ( ! $this->options_helper->get( 'should_redirect_after_install_free', false ) ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
$this->options_helper->set( 'should_redirect_after_install_free', false ); |
| 68 |
|
| 69 |
if ( ! empty( $this->options_helper->get( 'activation_redirect_timestamp_free', 0 ) ) ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
$this->options_helper->set( 'activation_redirect_timestamp_free', \time() ); |
| 73 |
|
| 74 |
// phpcs:ignore WordPress.Security.NonceVerification -- This is not a form. |
| 75 |
if ( isset( $_REQUEST['activate-multi'] ) && $_REQUEST['activate-multi'] === 'true' ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
if ( $this->product_helper->is_premium() ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
if ( \is_network_admin() || \is_plugin_active_for_network( WPSEO_BASENAME ) ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
\wp_safe_redirect( \admin_url( 'admin.php?page=wpseo_installation_successful_free' ), 302, 'Yoast SEO' ); |
| 88 |
$this->terminate_execution(); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Adds the installation success submenu page. |
| 93 |
* |
| 94 |
* @param array $submenu_pages The Yoast SEO submenu pages. |
| 95 |
* |
| 96 |
* @return array the filtered submenu pages. |
| 97 |
*/ |
| 98 |
public function add_submenu_page( $submenu_pages ) { |
| 99 |
\add_submenu_page( |
| 100 |
null, |
| 101 |
\__( 'Installation Successful', 'wordpress-seo' ), |
| 102 |
null, |
| 103 |
'manage_options', |
| 104 |
'wpseo_installation_successful_free', |
| 105 |
[ $this, 'render_page' ] |
| 106 |
); |
| 107 |
|
| 108 |
return $submenu_pages; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Enqueue assets on the Installation success page. |
| 113 |
*/ |
| 114 |
public function enqueue_assets() { |
| 115 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Date is not processed or saved. |
| 116 |
if ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'wpseo_installation_successful_free' ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
$asset_manager = new WPSEO_Admin_Asset_Manager(); |
| 121 |
$asset_manager->enqueue_script( 'installation-success' ); |
| 122 |
$asset_manager->enqueue_style( 'installation-success' ); |
| 123 |
$asset_manager->enqueue_style( 'monorepo' ); |
| 124 |
|
| 125 |
$asset_manager->localize_script( |
| 126 |
'installation-success', |
| 127 |
'wpseoInstallationSuccess', |
| 128 |
[ |
| 129 |
'pluginUrl' => \esc_url( \plugins_url( '', WPSEO_FILE ) ), |
| 130 |
'configurationWorkoutUrl' => \esc_url( \admin_url( 'admin.php?page=wpseo_workouts#configuration' ) ), |
| 131 |
'canDoConfigurationWorkout' => \current_user_can( 'wpseo_manage_options' ), |
| 132 |
'canEditWordPressOptions' => \current_user_can( 'manage_options' ), |
| 133 |
] |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Renders the installation success page. |
| 139 |
*/ |
| 140 |
public function render_page() { |
| 141 |
echo '<div id="wpseo-installation-successful-free" class="yoast"></div>'; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Wrap the `exit` function to make unit testing easier. |
| 146 |
*/ |
| 147 |
public function terminate_execution() { |
| 148 |
exit; |
| 149 |
} |
| 150 |
} |
| 151 |
|