addon_manager = $addon_manager; } /** * Registers all hooks to WordPress. */ public function register_hooks() { add_action( 'admin_init', [ $this, 'start_addon_installation' ] ); } /** * Starts the addon installation flow. * * @returns void */ public function start_addon_installation() { // Only show the dialog when we explicitly want to see it. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: This is not a form. if ( ! isset( $_GET['install'] ) || $_GET['install'] !== 'true' ) { return; } $this->bust_myyoast_addon_information_cache(); $this->owned_addons = $this->get_owned_addons(); if ( count( $this->owned_addons ) > 0 ) { add_action( 'admin_enqueue_scripts', [ $this, 'show_modal' ] ); } else { add_action( 'admin_notices', [ $this, 'throw_no_owned_addons_warning' ] ); } } /** * Throws a no owned addons warning. * * @returns void */ public function throw_no_owned_addons_warning() { echo '

' . sprintf( /* translators: %1$s expands to Yoast SEO */ esc_html__( 'No %1$s plugins have been installed. You don\'t seem to own any active subscriptions.', 'wordpress-seo' ), 'Yoast SEO' ) . '

'; } /** * Shows the modal. * * @returns void */ public function show_modal() { \wp_localize_script( \WPSEO_Admin_Asset_Manager::PREFIX . 'addon-installation', 'wpseoAddonInstallationL10n', [ 'addons' => $this->owned_addons, 'nonce' => \wp_create_nonce( 'wpseo_addon_installation' ), ] ); $asset_manager = new \WPSEO_Admin_Asset_Manager(); $asset_manager->enqueue_script( 'addon-installation' ); } /** * Retrieves a list of owned addons for the site in MyYoast. * * @return array List of owned addons with slug as key and name as value. */ protected function get_owned_addons() { $owned_addons = []; foreach ( $this->addon_manager->get_myyoast_site_information()->subscriptions as $addon ) { $owned_addons[] = $addon->product->name; } return $owned_addons; } /** * Bust the site information transients to have fresh data. * * @return void */ protected function bust_myyoast_addon_information_cache() { $this->addon_manager->remove_site_information_transients(); } }