| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\MyYoast_Client\User_Interface; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Conditionals\MyYoast_Connection_Conditional; |
| 6 |
use Yoast\WP\SEO\Helpers\Short_Link_Helper; |
| 7 |
use Yoast\WP\SEO\Integrations\Admin\Integrations_Page; |
| 8 |
|
| 9 |
/** |
| 10 |
* Builds the read-only MyYoast connection payload consumed by editor scripts |
| 11 |
* to pick the correct "Yoast AI cannot reach your site" notification variant. |
| 12 |
* |
| 13 |
* Returns `null` when the feature flag is disabled, so consumers treat the |
| 14 |
* connection as unavailable and show the informational-only variant. The |
| 15 |
* payload is deliberately minimal — no store, actions, or tokens reach the |
| 16 |
* editor; the connect call-to-action is just a nonce-protected link that |
| 17 |
* auto-starts the flow on the Integrations page in a new tab. |
| 18 |
*/ |
| 19 |
class Myyoast_Connection_Data_Presenter { |
| 20 |
|
| 21 |
/** |
| 22 |
* The MyYoast connection feature-flag conditional. |
| 23 |
* |
| 24 |
* @var MyYoast_Connection_Conditional |
| 25 |
*/ |
| 26 |
private $myyoast_connection_conditional; |
| 27 |
|
| 28 |
/** |
| 29 |
* The MyYoast connection status presenter. |
| 30 |
* |
| 31 |
* @var Status_Presenter |
| 32 |
*/ |
| 33 |
private $status_presenter; |
| 34 |
|
| 35 |
/** |
| 36 |
* The MyYoast connection-management permission check. |
| 37 |
* |
| 38 |
* @var Connection_Permission |
| 39 |
*/ |
| 40 |
private $connection_permission; |
| 41 |
|
| 42 |
/** |
| 43 |
* The short-link helper. |
| 44 |
* |
| 45 |
* @var Short_Link_Helper |
| 46 |
*/ |
| 47 |
private $short_link_helper; |
| 48 |
|
| 49 |
/** |
| 50 |
* Constructs the instance. |
| 51 |
* |
| 52 |
* @param MyYoast_Connection_Conditional $myyoast_connection_conditional The MyYoast connection feature-flag conditional. |
| 53 |
* @param Status_Presenter $status_presenter The MyYoast connection status presenter. |
| 54 |
* @param Connection_Permission $connection_permission The MyYoast connection-management permission check. |
| 55 |
* @param Short_Link_Helper $short_link_helper The short-link helper. |
| 56 |
*/ |
| 57 |
public function __construct( |
| 58 |
MyYoast_Connection_Conditional $myyoast_connection_conditional, |
| 59 |
Status_Presenter $status_presenter, |
| 60 |
Connection_Permission $connection_permission, |
| 61 |
Short_Link_Helper $short_link_helper |
| 62 |
) { |
| 63 |
$this->myyoast_connection_conditional = $myyoast_connection_conditional; |
| 64 |
$this->status_presenter = $status_presenter; |
| 65 |
$this->connection_permission = $connection_permission; |
| 66 |
$this->short_link_helper = $short_link_helper; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Returns the connection payload, or `null` when the feature flag is disabled. |
| 71 |
* |
| 72 |
* @return array{isProvisioned: bool, canConnect: bool, connectUrl: string|null, learnMoreUrl: string}|null |
| 73 |
*/ |
| 74 |
public function present() { |
| 75 |
if ( ! $this->myyoast_connection_conditional->is_met() ) { |
| 76 |
return null; |
| 77 |
} |
| 78 |
|
| 79 |
$status = $this->status_presenter->present(); |
| 80 |
$can_connect = $this->connection_permission->can_manage(); |
| 81 |
|
| 82 |
return [ |
| 83 |
'isProvisioned' => \is_bool( $status['is_provisioned'] ) && $status['is_provisioned'], |
| 84 |
'canConnect' => $can_connect, |
| 85 |
// Only users who can manage options can start the flow; for everyone |
| 86 |
// else the link is omitted and the editor shows the "ask your admin" variant. |
| 87 |
'connectUrl' => ( $can_connect ) ? $this->get_connect_url() : null, |
| 88 |
'learnMoreUrl' => $this->short_link_helper->get( 'https://yoa.st/ai-myyoast-connection' ), |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Builds the nonce-protected Integrations-page URL that auto-starts the |
| 94 |
* MyYoast connection flow when opened. |
| 95 |
* |
| 96 |
* Built with `add_query_arg()` + `wp_create_nonce()` rather than `wp_nonce_url()`: |
| 97 |
* the latter HTML-encodes the `&` separators for markup output, but this URL is |
| 98 |
* localized and assigned to a React `href`, where it isn't decoded — the browser |
| 99 |
* would then send `amp;start-myyoast-connection` as the query-arg name and the |
| 100 |
* flow would never start. This form keeps the separators as plain `&`. |
| 101 |
* |
| 102 |
* @return string The connect URL. |
| 103 |
*/ |
| 104 |
private function get_connect_url() { |
| 105 |
return \add_query_arg( |
| 106 |
'_wpnonce', |
| 107 |
\wp_create_nonce( 'wpseo-start-myyoast-connection' ), |
| 108 |
\self_admin_url( 'admin.php?page=' . Integrations_Page::PAGE . '&start-myyoast-connection=1' ), |
| 109 |
); |
| 110 |
} |
| 111 |
} |
| 112 |
|