| 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\Helpers\Options_Helper; |
| 8 |
use Yoast\WP\SEO\Helpers\Product_Helper; |
| 9 |
use Yoast\WP\SEO\Helpers\Short_Link_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 |
* The shortlinker. |
| 33 |
* |
| 34 |
* @var Short_Link_Helper |
| 35 |
*/ |
| 36 |
private $shortlinker; |
| 37 |
|
| 38 |
/** |
| 39 |
* {@inheritDoc} |
| 40 |
*/ |
| 41 |
public static function get_conditionals() { |
| 42 |
return [ Admin_Conditional::class ]; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Installation_Success_Integration constructor. |
| 47 |
* |
| 48 |
* @param Options_Helper $options_helper The options helper. |
| 49 |
* @param Product_Helper $product_helper The product helper. |
| 50 |
* @param Short_Link_Helper $shortlinker The shortlinker. |
| 51 |
*/ |
| 52 |
public function __construct( Options_Helper $options_helper, Product_Helper $product_helper, Short_Link_Helper $shortlinker ) { |
| 53 |
$this->options_helper = $options_helper; |
| 54 |
$this->product_helper = $product_helper; |
| 55 |
$this->shortlinker = $shortlinker; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* {@inheritDoc} |
| 60 |
*/ |
| 61 |
public function register_hooks() { |
| 62 |
\add_filter( 'admin_menu', [ $this, 'add_submenu_page' ], 9 ); |
| 63 |
\add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); |
| 64 |
\add_action( 'admin_init', [ $this, 'maybe_redirect' ] ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Redirects to the installation success page if an installation has just occurred. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function maybe_redirect() { |
| 73 |
if ( \wp_doing_ajax() || \wp_doing_cron() || \wp_is_serving_rest_request() || \wp_is_json_request() ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
if ( ! \current_user_can( 'manage_options' ) ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
if ( ! $this->options_helper->get( 'should_redirect_after_install_free', false ) ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
$this->options_helper->set( 'should_redirect_after_install_free', false ); |
| 85 |
|
| 86 |
if ( ! empty( $this->options_helper->get( 'activation_redirect_timestamp_free', 0 ) ) ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
$this->options_helper->set( 'activation_redirect_timestamp_free', \time() ); |
| 90 |
|
| 91 |
// phpcs:ignore WordPress.Security.NonceVerification -- This is not a form. |
| 92 |
if ( isset( $_REQUEST['activate-multi'] ) && $_REQUEST['activate-multi'] === 'true' ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
if ( $this->product_helper->is_premium() ) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
if ( \is_network_admin() || \is_plugin_active_for_network( \WPSEO_BASENAME ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Filter: 'wpseo_should_redirect_after_install' - Allows skipping the redirect to the installation success page. |
| 106 |
* |
| 107 |
* @param bool $should_redirect Whether to redirect. Default true. |
| 108 |
*/ |
| 109 |
if ( ! \apply_filters( 'wpseo_should_redirect_after_install', true ) ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
\wp_safe_redirect( \admin_url( 'admin.php?page=wpseo_installation_successful_free' ), 302, 'Yoast SEO' ); |
| 114 |
$this->terminate_execution(); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Adds the installation success submenu page. |
| 119 |
* |
| 120 |
* @param array $submenu_pages The Yoast SEO submenu pages. |
| 121 |
* |
| 122 |
* @return array the filtered submenu pages. |
| 123 |
*/ |
| 124 |
public function add_submenu_page( $submenu_pages ) { |
| 125 |
\add_submenu_page( |
| 126 |
'options.php', |
| 127 |
\__( 'Installation Successful', 'wordpress-seo' ), |
| 128 |
'', |
| 129 |
'manage_options', |
| 130 |
'wpseo_installation_successful_free', |
| 131 |
[ $this, 'render_page' ], |
| 132 |
); |
| 133 |
|
| 134 |
return $submenu_pages; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Enqueue assets on the Installation success page. |
| 139 |
* |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
public function enqueue_assets() { |
| 143 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Date is not processed or saved. |
| 144 |
if ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'wpseo_installation_successful_free' ) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
$asset_manager = new WPSEO_Admin_Asset_Manager(); |
| 149 |
$asset_manager->enqueue_script( 'installation-success' ); |
| 150 |
$asset_manager->enqueue_style( 'installation-success' ); |
| 151 |
$asset_manager->enqueue_style( 'monorepo' ); |
| 152 |
|
| 153 |
$ftc_url = \esc_url( \admin_url( 'admin.php?page=wpseo_dashboard#/first-time-configuration' ) ); |
| 154 |
|
| 155 |
$asset_manager->localize_script( |
| 156 |
'installation-success', |
| 157 |
'wpseoInstallationSuccess', |
| 158 |
[ |
| 159 |
'pluginUrl' => \esc_url( \plugins_url( '', \WPSEO_FILE ) ), |
| 160 |
'firstTimeConfigurationUrl' => $ftc_url, |
| 161 |
'dashboardUrl' => \esc_url( \admin_url( 'admin.php?page=wpseo_dashboard' ) ), |
| 162 |
'explorePremiumUrl' => $this->shortlinker->build( 'https://yoa.st/ftc-premium-link' ), |
| 163 |
], |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Renders the installation success page. |
| 169 |
* |
| 170 |
* @return void |
| 171 |
*/ |
| 172 |
public function render_page() { |
| 173 |
echo '<div id="wpseo-installation-successful-free" class="yoast"></div>'; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Wrap the `exit` function to make unit testing easier. |
| 178 |
* |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
public function terminate_execution() { |
| 182 |
exit(); |
| 183 |
} |
| 184 |
} |
| 185 |
|