|
| 1 | +<?php |
| 2 | +// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 | +namespace Yoast\WP\SEO\Alerts\Application\Default_SEO_Data; |
| 4 | + |
| 5 | +use Yoast\WP\SEO\Alerts\Infrastructure\Default_SEO_Data\Default_SEO_Data_Collector; |
| 6 | +use Yoast\WP\SEO\Conditionals\Admin_Conditional; |
| 7 | +use Yoast\WP\SEO\Helpers\Indexable_Helper; |
| 8 | +use Yoast\WP\SEO\Helpers\Post_Type_Helper; |
| 9 | +use Yoast\WP\SEO\Helpers\Product_Helper; |
| 10 | +use Yoast\WP\SEO\Helpers\Short_Link_Helper; |
| 11 | +use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 12 | +use Yoast_Notification; |
| 13 | +use Yoast_Notification_Center; |
| 14 | + |
| 15 | +/** |
| 16 | + * Default_SEO_Data_Alert class. |
| 17 | + */ |
| 18 | +class Default_SEO_Data_Alert implements Integration_Interface { |
| 19 | + |
| 20 | + public const NOTIFICATION_ID = 'wpseo-default-seo-data'; |
| 21 | + |
| 22 | + /** |
| 23 | + * The notifications center. |
| 24 | + * |
| 25 | + * @var Yoast_Notification_Center |
| 26 | + */ |
| 27 | + private $notification_center; |
| 28 | + |
| 29 | + /** |
| 30 | + * The default SEO data collector. |
| 31 | + * |
| 32 | + * @var Default_SEO_Data_Collector |
| 33 | + */ |
| 34 | + private $default_seo_data_collector; |
| 35 | + |
| 36 | + /** |
| 37 | + * The short link helper. |
| 38 | + * |
| 39 | + * @var Short_Link_Helper |
| 40 | + */ |
| 41 | + private $short_link_helper; |
| 42 | + |
| 43 | + /** |
| 44 | + * The product helper. |
| 45 | + * |
| 46 | + * @var Product_Helper |
| 47 | + */ |
| 48 | + private $product_helper; |
| 49 | + |
| 50 | + /** |
| 51 | + * The indexable helper. |
| 52 | + * |
| 53 | + * @var Indexable_Helper |
| 54 | + */ |
| 55 | + private $indexable_helper; |
| 56 | + |
| 57 | + /** |
| 58 | + * The post type helper. |
| 59 | + * |
| 60 | + * @var Post_Type_Helper |
| 61 | + */ |
| 62 | + private $post_type_helper; |
| 63 | + |
| 64 | + /** |
| 65 | + * Default_SEO_Data_Alert constructor. |
| 66 | + * |
| 67 | + * @param Yoast_Notification_Center $notification_center The notification center. |
| 68 | + * @param Default_SEO_Data_Collector $default_seo_data_collector The default SEO data collector. |
| 69 | + * @param Short_Link_Helper $short_link_helper The short link helper. |
| 70 | + * @param Product_Helper $product_helper The product helper. |
| 71 | + * @param Indexable_Helper $indexable_helper The indexable helper. |
| 72 | + * @param Post_Type_Helper $post_type_helper The post type helper. |
| 73 | + */ |
| 74 | + public function __construct( |
| 75 | + Yoast_Notification_Center $notification_center, |
| 76 | + Default_SEO_Data_Collector $default_seo_data_collector, |
| 77 | + Short_Link_Helper $short_link_helper, |
| 78 | + Product_Helper $product_helper, |
| 79 | + Indexable_Helper $indexable_helper, |
| 80 | + Post_Type_Helper $post_type_helper |
| 81 | + ) { |
| 82 | + $this->notification_center = $notification_center; |
| 83 | + $this->default_seo_data_collector = $default_seo_data_collector; |
| 84 | + $this->short_link_helper = $short_link_helper; |
| 85 | + $this->product_helper = $product_helper; |
| 86 | + $this->indexable_helper = $indexable_helper; |
| 87 | + $this->post_type_helper = $post_type_helper; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns the conditionals based on which this loadable should be active. |
| 92 | + * |
| 93 | + * @return array<string> |
| 94 | + */ |
| 95 | + public static function get_conditionals() { |
| 96 | + return [ Admin_Conditional::class ]; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Initializes the integration. |
| 101 | + * |
| 102 | + * @return void |
| 103 | + */ |
| 104 | + public function register_hooks() { |
| 105 | + \add_action( 'admin_init', [ $this, 'add_notifications' ] ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Adds notifications (when necessary). |
| 110 | + * |
| 111 | + * We want to show this notification only when there are enough posts that have the default SEO title or meta description, or both. |
| 112 | + * If this is not the case we will not show the notification at all since it does not serve a purpose yet. |
| 113 | + * |
| 114 | + * @return void |
| 115 | + */ |
| 116 | + public function add_notifications() { |
| 117 | + if ( ! $this->indexable_helper->should_index_indexables() ) { |
| 118 | + // Do not show the notification when indexables are disabled. |
| 119 | + $this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID ); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + if ( ! $this->post_type_helper->is_indexable( 'post' ) || ! $this->post_type_helper->has_metabox( 'post' ) ) { |
| 124 | + // Do not show the notification when posts are not indexable or have no metabox. |
| 125 | + $this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID ); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + $posts_with_default_seo_title = $this->default_seo_data_collector->get_posts_with_default_seo_title(); |
| 130 | + $posts_with_default_seo_description = $this->default_seo_data_collector->get_posts_with_default_seo_description(); |
| 131 | + |
| 132 | + $has_enough_posts_with_default_title = \count( $posts_with_default_seo_title ) > 4; |
| 133 | + $has_enough_posts_with_default_desc = \count( $posts_with_default_seo_description ) > 4; |
| 134 | + |
| 135 | + if ( ! $has_enough_posts_with_default_title && ! $has_enough_posts_with_default_desc ) { |
| 136 | + $this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID ); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + $notification = $this->get_default_seo_data_notification( $has_enough_posts_with_default_title, $has_enough_posts_with_default_desc ); |
| 141 | + |
| 142 | + $this->notification_center->add_notification( $notification ); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Build the default SEO data notification. |
| 147 | + * |
| 148 | + * @param bool $has_enough_posts_with_default_title Whether there are content types with default SEO title in their most recent posts. |
| 149 | + * @param bool $has_enough_posts_with_default_desc Whether there are content types with default SEO description in their most recent posts. |
| 150 | + * |
| 151 | + * @return Yoast_Notification The notification containing the suggested plugin. |
| 152 | + */ |
| 153 | + private function get_default_seo_data_notification( $has_enough_posts_with_default_title, $has_enough_posts_with_default_desc ): Yoast_Notification { |
| 154 | + $message = $this->get_default_seo_data_message( $has_enough_posts_with_default_title, $has_enough_posts_with_default_desc ); |
| 155 | + |
| 156 | + return new Yoast_Notification( |
| 157 | + $message, |
| 158 | + [ |
| 159 | + 'id' => self::NOTIFICATION_ID, |
| 160 | + 'type' => Yoast_Notification::WARNING, |
| 161 | + 'capabilities' => [ 'wpseo_manage_options' ], |
| 162 | + ] |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Creates a message to inform users that they are using only default SEO data lately. |
| 168 | + * |
| 169 | + * @param bool $has_enough_posts_with_default_title Whether there are content types with default SEO title in their most recent posts. |
| 170 | + * @param bool $has_enough_posts_with_default_desc Whether there are content types with default SEO description in their most recent posts. |
| 171 | + * |
| 172 | + * @return string The default SEO data message. |
| 173 | + */ |
| 174 | + private function get_default_seo_data_message( $has_enough_posts_with_default_title, $has_enough_posts_with_default_desc ): string { |
| 175 | + $shortlink = ( $this->product_helper->is_premium() ) ? $this->short_link_helper->get( 'https://yoa.st/ai-generate-alert-premium/' ) : $this->short_link_helper->get( 'https://yoa.st/ai-generate-alert-free/' ); |
| 176 | + |
| 177 | + if ( $has_enough_posts_with_default_title && $has_enough_posts_with_default_desc ) { |
| 178 | + $default_seo_data = \esc_html__( 'SEO titles and meta descriptions', 'wordpress-seo' ); |
| 179 | + } |
| 180 | + elseif ( $has_enough_posts_with_default_title ) { |
| 181 | + $default_seo_data = \esc_html__( 'SEO titles', 'wordpress-seo' ); |
| 182 | + } |
| 183 | + elseif ( $has_enough_posts_with_default_desc ) { |
| 184 | + $default_seo_data = \esc_html__( 'meta descriptions', 'wordpress-seo' ); |
| 185 | + } |
| 186 | + else { |
| 187 | + $default_seo_data = \esc_html__( 'SEO data', 'wordpress-seo' ); |
| 188 | + } |
| 189 | + |
| 190 | + /* translators: %1$s expands to "SEO title" or "meta description", %2$s expands to an opening strong tag, %3$s expands to a closing strong tag, %4$s expands to an opening link tag, %5$s expands to a closing link tag. */ |
| 191 | + $message = ( $this->product_helper->is_premium() ) ? \esc_html__( 'Your recent posts are using default %1$s, making them less appealing in search. Create custom titles and descriptions instantly with %2$sYoast AI Generate%3$s. %4$sLearn how to use it%5$s.', 'wordpress-seo' ) : \esc_html__( 'Your recent posts are using default %1$s, which makes them easy to overlook. Catch attention in search with custom titles and descriptions from %2$sYoast AI Generate%3$s. %4$sTry it for free.%5$s', 'wordpress-seo' ); |
| 192 | + |
| 193 | + return \sprintf( |
| 194 | + $message, |
| 195 | + $default_seo_data, |
| 196 | + '<strong>', |
| 197 | + '</strong>', |
| 198 | + '<a href="' . \esc_url( $shortlink ) . '">', |
| 199 | + '</a>' |
| 200 | + ); |
| 201 | + } |
| 202 | +} |
0 commit comments