| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Discussed in Tech Council, a better solution is being worked on. |
| 4 |
|
| 5 |
namespace Yoast\WP\SEO\Integrations\Admin\Addon_Installation; |
| 6 |
|
| 7 |
use WPSEO_Addon_Manager; |
| 8 |
use WPSEO_Admin_Asset_Manager; |
| 9 |
use Yoast\WP\SEO\Conditionals\Addon_Installation_Conditional; |
| 10 |
use Yoast\WP\SEO\Conditionals\Admin\Licenses_Page_Conditional; |
| 11 |
use Yoast\WP\SEO\Conditionals\Admin_Conditional; |
| 12 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 13 |
|
| 14 |
/** |
| 15 |
* Represents the Addon installation feature. |
| 16 |
*/ |
| 17 |
class Dialog_Integration implements Integration_Interface { |
| 18 |
|
| 19 |
/** |
| 20 |
* The addon manager. |
| 21 |
* |
| 22 |
* @var WPSEO_Addon_Manager |
| 23 |
*/ |
| 24 |
protected $addon_manager; |
| 25 |
|
| 26 |
/** |
| 27 |
* The addons. |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
protected $owned_addons; |
| 32 |
|
| 33 |
/** |
| 34 |
* {@inheritDoc} |
| 35 |
*/ |
| 36 |
public static function get_conditionals() { |
| 37 |
return [ |
| 38 |
Admin_Conditional::class, |
| 39 |
Licenses_Page_Conditional::class, |
| 40 |
Addon_Installation_Conditional::class, |
| 41 |
]; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Addon_Installation constructor. |
| 46 |
* |
| 47 |
* @param WPSEO_Addon_Manager $addon_manager The addon manager. |
| 48 |
*/ |
| 49 |
public function __construct( WPSEO_Addon_Manager $addon_manager ) { |
| 50 |
$this->addon_manager = $addon_manager; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Registers all hooks to WordPress. |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function register_hooks() { |
| 59 |
\add_action( 'admin_init', [ $this, 'start_addon_installation' ] ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Starts the addon installation flow. |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public function start_addon_installation() { |
| 68 |
// Only show the dialog when we explicitly want to see it. |
| 69 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: This is not a form. |
| 70 |
if ( ! isset( $_GET['install'] ) || $_GET['install'] !== 'true' ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
$this->bust_myyoast_addon_information_cache(); |
| 75 |
$this->owned_addons = $this->get_owned_addons(); |
| 76 |
|
| 77 |
if ( \count( $this->owned_addons ) > 0 ) { |
| 78 |
\add_action( 'admin_enqueue_scripts', [ $this, 'show_modal' ] ); |
| 79 |
} |
| 80 |
else { |
| 81 |
\add_action( 'admin_notices', [ $this, 'throw_no_owned_addons_warning' ] ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Throws a no owned addons warning. |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public function throw_no_owned_addons_warning() { |
| 91 |
echo '<div class="notice notice-warning"><p>' |
| 92 |
. \sprintf( |
| 93 |
/* translators: %1$s expands to Yoast SEO */ |
| 94 |
\esc_html__( |
| 95 |
'No %1$s plugins have been installed. You don\'t seem to own any active subscriptions.', |
| 96 |
'wordpress-seo', |
| 97 |
), |
| 98 |
'Yoast SEO', |
| 99 |
) |
| 100 |
. '</p></div>'; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Shows the modal. |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function show_modal() { |
| 109 |
\wp_localize_script( |
| 110 |
WPSEO_Admin_Asset_Manager::PREFIX . 'addon-installation', |
| 111 |
'wpseoAddonInstallationL10n', |
| 112 |
[ |
| 113 |
'addons' => $this->owned_addons, |
| 114 |
'nonce' => \wp_create_nonce( 'wpseo_addon_installation' ), |
| 115 |
], |
| 116 |
); |
| 117 |
|
| 118 |
$asset_manager = new WPSEO_Admin_Asset_Manager(); |
| 119 |
$asset_manager->enqueue_script( 'addon-installation' ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Retrieves a list of owned addons for the site in MyYoast. |
| 124 |
* |
| 125 |
* @return array List of owned addons with slug as key and name as value. |
| 126 |
*/ |
| 127 |
protected function get_owned_addons() { |
| 128 |
$owned_addons = []; |
| 129 |
|
| 130 |
foreach ( $this->addon_manager->get_myyoast_site_information()->subscriptions as $addon ) { |
| 131 |
$owned_addons[] = $addon->product->name; |
| 132 |
} |
| 133 |
|
| 134 |
return $owned_addons; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Bust the site information transients to have fresh data. |
| 139 |
* |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
protected function bust_myyoast_addon_information_cache() { |
| 143 |
$this->addon_manager->remove_site_information_transients(); |
| 144 |
} |
| 145 |
} |
| 146 |
|