| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong |
| 4 |
namespace Yoast\WP\SEO\Editors\Framework\Site; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Actions\Alert_Dismissal_Action; |
| 7 |
use Yoast\WP\SEO\Alerts\Infrastructure\Default_SEO_Data\Default_SEO_Data_Collector; |
| 8 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 9 |
use Yoast\WP\SEO\Helpers\Product_Helper; |
| 10 |
use Yoast\WP\SEO\Helpers\Short_Link_Helper; |
| 11 |
use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository; |
| 12 |
use Yoast\WP\SEO\Promotions\Application\Promotion_Manager; |
| 13 |
use Yoast\WP\SEO\Surfaces\Meta_Surface; |
| 14 |
|
| 15 |
/** |
| 16 |
* The Post_Site_Information class. |
| 17 |
*/ |
| 18 |
class Post_Site_Information extends Base_Site_Information { |
| 19 |
|
| 20 |
/** |
| 21 |
* The permalink. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
private $permalink; |
| 26 |
|
| 27 |
/** |
| 28 |
* The alert dismissal action. |
| 29 |
* |
| 30 |
* @var Alert_Dismissal_Action |
| 31 |
*/ |
| 32 |
private $alert_dismissal_action; |
| 33 |
|
| 34 |
/** |
| 35 |
* The default SEO data collector. |
| 36 |
* |
| 37 |
* @var Default_SEO_Data_Collector |
| 38 |
*/ |
| 39 |
private $default_seo_data_collector; |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructs the class. |
| 43 |
* |
| 44 |
* @param Short_Link_Helper $short_link_helper The short link helper. |
| 45 |
* @param Wistia_Embed_Permission_Repository $wistia_embed_permission_repository The wistia embed permission |
| 46 |
* repository. |
| 47 |
* @param Meta_Surface $meta The meta surface. |
| 48 |
* @param Product_Helper $product_helper The product helper. |
| 49 |
* @param Alert_Dismissal_Action $alert_dismissal_action The alert dismissal action. |
| 50 |
* @param Options_Helper $options_helper The options helper. |
| 51 |
* @param Promotion_Manager $promotion_manager The promotion manager. |
| 52 |
* @param Default_SEO_Data_Collector $default_seo_data_collector The default SEO data collector. |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function __construct( |
| 57 |
Short_Link_Helper $short_link_helper, |
| 58 |
Wistia_Embed_Permission_Repository $wistia_embed_permission_repository, |
| 59 |
Meta_Surface $meta, |
| 60 |
Product_Helper $product_helper, |
| 61 |
Alert_Dismissal_Action $alert_dismissal_action, |
| 62 |
Options_Helper $options_helper, |
| 63 |
Promotion_Manager $promotion_manager, |
| 64 |
Default_SEO_Data_Collector $default_seo_data_collector |
| 65 |
) { |
| 66 |
parent::__construct( $short_link_helper, $wistia_embed_permission_repository, $meta, $product_helper, $options_helper, $promotion_manager ); |
| 67 |
$this->alert_dismissal_action = $alert_dismissal_action; |
| 68 |
$this->default_seo_data_collector = $default_seo_data_collector; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Sets the permalink. |
| 73 |
* |
| 74 |
* @param string $permalink The permalink. |
| 75 |
* |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
public function set_permalink( string $permalink ): void { |
| 79 |
$this->permalink = $permalink; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns post specific site information together with the generic site information. |
| 84 |
* |
| 85 |
* @return array<string, string|array<string, string>> |
| 86 |
*/ |
| 87 |
public function get_legacy_site_information(): array { |
| 88 |
$dismissed_alerts = $this->alert_dismissal_action->all_dismissed(); |
| 89 |
|
| 90 |
$data = [ |
| 91 |
'dismissedAlerts' => $dismissed_alerts, |
| 92 |
'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), |
| 93 |
'metabox' => [ |
| 94 |
'search_url' => $this->search_url(), |
| 95 |
'post_edit_url' => $this->edit_url(), |
| 96 |
'base_url' => $this->base_url_for_js(), |
| 97 |
], |
| 98 |
'isRecentTitlesDefault' => \count( $this->default_seo_data_collector->get_posts_with_default_seo_title() ) > 4, |
| 99 |
'isRecentDescriptionsDefault' => \count( $this->default_seo_data_collector->get_posts_with_default_seo_description() ) > 4, |
| 100 |
]; |
| 101 |
|
| 102 |
return \array_merge_recursive( $data, parent::get_legacy_site_information() ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Returns post specific site information together with the generic site information. |
| 107 |
* |
| 108 |
* @return array<string, string|string[]> |
| 109 |
*/ |
| 110 |
public function get_site_information(): array { |
| 111 |
$dismissed_alerts = $this->alert_dismissal_action->all_dismissed(); |
| 112 |
|
| 113 |
$data = [ |
| 114 |
'dismissedAlerts' => $dismissed_alerts, |
| 115 |
'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), |
| 116 |
'search_url' => $this->search_url(), |
| 117 |
'post_edit_url' => $this->edit_url(), |
| 118 |
'base_url' => $this->base_url_for_js(), |
| 119 |
'isRecentTitlesDefault' => \count( $this->default_seo_data_collector->get_posts_with_default_seo_title() ) > 4, |
| 120 |
'isRecentDescriptionsDefault' => \count( $this->default_seo_data_collector->get_posts_with_default_seo_description() ) > 4, |
| 121 |
]; |
| 122 |
|
| 123 |
return \array_merge( $data, parent::get_site_information() ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns the url to search for keyword for the post. |
| 128 |
* |
| 129 |
* @return string |
| 130 |
*/ |
| 131 |
private function search_url(): string { |
| 132 |
return \admin_url( 'edit.php?seo_kw_filter={keyword}' ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Returns the url to edit the taxonomy. |
| 137 |
* |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
private function edit_url(): string { |
| 141 |
return \admin_url( 'post.php?post={id}&action=edit' ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns a base URL for use in the JS, takes permalink structure into account. |
| 146 |
* |
| 147 |
* @return string |
| 148 |
*/ |
| 149 |
private function base_url_for_js(): string { |
| 150 |
global $pagenow; |
| 151 |
|
| 152 |
// The default base is the home_url. |
| 153 |
$base_url = \home_url( '/', null ); |
| 154 |
|
| 155 |
if ( $pagenow === 'post-new.php' ) { |
| 156 |
return $base_url; |
| 157 |
} |
| 158 |
|
| 159 |
// If %postname% is the last tag, just strip it and use that as a base. |
| 160 |
if ( \preg_match( '#%postname%/?$#', $this->permalink ) === 1 ) { |
| 161 |
$base_url = \preg_replace( '#%postname%/?$#', '', $this->permalink ); |
| 162 |
} |
| 163 |
|
| 164 |
// If %pagename% is the last tag, just strip it and use that as a base. |
| 165 |
if ( \preg_match( '#%pagename%/?$#', $this->permalink ) === 1 ) { |
| 166 |
$base_url = \preg_replace( '#%pagename%/?$#', '', $this->permalink ); |
| 167 |
} |
| 168 |
|
| 169 |
return $base_url; |
| 170 |
} |
| 171 |
} |
| 172 |
|