PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / integrations / admin / first-time-configuration-notice-integration.php

first-time-configuration-notice-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 19.0, at src/integrations/admin/first-time-configuration-notice-integration.php

231 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Integrations\Admin;
4
5 use WPSEO_Admin_Asset_Manager;
6 use Yoast\WP\SEO\Conditionals\Admin_Conditional;
7 use Yoast\WP\SEO\Conditionals\No_Conditionals;
8 use Yoast\WP\SEO\Helpers\Indexing_Helper;
9 use Yoast\WP\SEO\Helpers\Options_Helper;
10 use Yoast\WP\SEO\Integrations\Integration_Interface;
11 use Yoast\WP\SEO\Presenters\Admin\Notice_Presenter;
12
13 // phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded -- First time configuration simply has a lot of words.
14 /**
15 * First_Time_Configuration_Notice_Integration class
16 */
17 class First_Time_Configuration_Notice_Integration implements Integration_Interface {
18
19 /**
20 * The options' helper.
21 *
22 * @var Options_Helper
23 */
24 private $options_helper;
25
26 /**
27 * The indexing helper.
28 *
29 * @var Indexing_Helper
30 */
31 private $indexing_helper;
32
33 /**
34 * The admin asset manager.
35 *
36 * @var WPSEO_Admin_Asset_Manager
37 */
38 private $admin_asset_manager;
39
40 /**
41 * {@inheritDoc}
42 */
43 public static function get_conditionals() {
44 return [ Admin_Conditional::class ];
45 }
46
47 /**
48 * First_Time_Configuration_Notice_Integration constructor.
49 *
50 * @param Options_Helper $options_helper The options helper.
51 * @param Indexing_Helper $indexing_helper The indexing helper.
52 * @param WPSEO_Admin_Asset_Manager $admin_asset_manager The admin asset manager.
53 */
54 public function __construct(
55 Options_Helper $options_helper,
56 Indexing_Helper $indexing_helper,
57 WPSEO_Admin_Asset_Manager $admin_asset_manager
58 ) {
59 $this->options_helper = $options_helper;
60 $this->indexing_helper = $indexing_helper;
61 $this->admin_asset_manager = $admin_asset_manager;
62 }
63
64 /**
65 * {@inheritDoc}
66 */
67 public function register_hooks() {
68 \add_action( 'wp_ajax_dismiss_first_time_configuration_notice', [ $this, 'dismiss_first_time_configuration_notice' ] );
69 \add_action( 'admin_notices', [ $this, 'first_time_configuration_notice' ] );
70 }
71
72 /**
73 * Dismisses the First-time configuration notice.
74 *
75 * @return bool
76 */
77 public function dismiss_first_time_configuration_notice() {
78 return $this->options_helper->set( 'dismiss_configuration_workout_notice', true );
79 }
80
81 /**
82 * Determines whether and where the "First-time SEO Configuration" admin notice should be displayed.
83 *
84 * @return bool Whether the "First-time SEO Configuration" admin notice should be displayed.
85 */
86 public function should_display_first_time_configuration_notice() {
87 if ( ! $this->options_helper->get( 'dismiss_configuration_workout_notice', false ) === false ) {
88 return false;
89 }
90
91 if ( ! $this->user_can_do_first_time_configuration() ) {
92 return false;
93 }
94
95 if ( ! $this->on_wpseo_admin_page_or_dashboard() ) {
96 return false;
97 }
98
99 if ( $this->is_first_time_configuration_finished() ) {
100 return false;
101 }
102
103 if ( $this->options_helper->get( 'first_time_install', false ) === false ) {
104 return false;
105 }
106
107 return ! $this->are_site_representation_name_and_logo_set() || $this->indexing_helper->get_unindexed_count() > 0;
108 }
109
110 /**
111 * Displays an admin notice when the first-time configuration has not been finished yet.
112 *
113 * @return void
114 */
115 public function first_time_configuration_notice() {
116 if ( ! $this->should_display_first_time_configuration_notice() ) {
117 return;
118 }
119
120 $this->admin_asset_manager->enqueue_style( 'monorepo' );
121
122 $notice = new Notice_Presenter(
123 \__( 'First-time SEO configuration', 'wordpress-seo' ),
124 \sprintf(
125 /* translators: 1: Link start tag to the first-time configuration, 2: Yoast SEO, 3: Link closing tag. */
126 \__( 'Get started quickly with the %1$s%2$s First-time configuration%3$s and configure Yoast SEO with the optimal SEO settings for your site!', 'wordpress-seo' ),
127 '<a href="' . \esc_url( \self_admin_url( 'admin.php?page=wpseo_dashboard#top#first-time-configuration' ) ) . '">',
128 'Yoast SEO',
129 '</a>'
130 ),
131 'mirrored_fit_bubble_woman_1_optim.svg',
132 null,
133 true,
134 'yoast-first-time-configuration-notice'
135 );
136
137 //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output from present() is considered safe.
138 echo $notice->present();
139
140 // Enable permanently dismissing the notice.
141 echo "<script>
142 function dismiss_first_time_configuration_notice(){
143 var data = {
144 'action': 'dismiss_first_time_configuration_notice',
145 };
146
147 jQuery.post( ajaxurl, data, function( response ) {
148 jQuery( '#yoast-first-time-configuration-notice' ).hide();
149 });
150 }
151
152 jQuery( document ).ready( function() {
153 jQuery( 'body' ).on( 'click', '#yoast-first-time-configuration-notice .notice-dismiss', function() {
154 dismiss_first_time_configuration_notice();
155 } );
156 } );
157 </script>";
158 }
159
160 /**
161 * Whether the user can do the first-time configuration.
162 *
163 * @return bool Whether the current user can do the first-time configuration.
164 */
165 private function user_can_do_first_time_configuration() {
166 return \current_user_can( 'wpseo_manage_options' );
167 }
168
169 /**
170 * Whether the user is currently visiting one of our admin pages or the WordPress dashboard.
171 *
172 * @return bool Whether the current page is a Yoast SEO admin page
173 */
174 private function on_wpseo_admin_page_or_dashboard() {
175 $pagenow = $GLOBALS['pagenow'];
176
177 // Show on the WP Dashboard.
178 if ( $pagenow === 'index.php' ) {
179 return true;
180 }
181
182 $page_from_get = \filter_input( \INPUT_GET, 'page' );
183
184 // Show on Yoast SEO pages, with some exceptions.
185 if ( $pagenow === 'admin.php' && \strpos( $page_from_get, 'wpseo' ) === 0 ) {
186 $exceptions = [
187 'wpseo_installation_successful',
188 'wpseo_installation_successful_free',
189 ];
190
191 if ( ! \in_array( $page_from_get, $exceptions, true ) ) {
192 return true;
193 }
194 }
195
196 return false;
197 }
198
199 /**
200 * Whether all steps of the first-time configuration have been finished.
201 *
202 * @return bool Whether the first-time configuration has been finished.
203 */
204 private function is_first_time_configuration_finished() {
205 $configuration_finished_steps = $this->options_helper->get( 'configuration_finished_steps', [] );
206
207 return \count( $configuration_finished_steps ) === 3;
208 }
209
210 /**
211 * Whether the site representation name and logo have been set.
212 *
213 * @return bool Whether the site representation name and logo have been set.
214 */
215 private function are_site_representation_name_and_logo_set() {
216 $company_or_person = $this->options_helper->get( 'company_or_person', '' );
217
218 if ( $company_or_person === '' ) {
219 return false;
220 }
221
222 if ( $company_or_person === 'company' ) {
223 return ! empty( $this->options_helper->get( 'company_name' ) )
224 && ! empty( $this->options_helper->get( 'company_logo', '' ) );
225 }
226
227 return ! empty( $this->options_helper->get( 'company_or_person_user_id' ) )
228 && ! empty( $this->options_helper->get( 'person_logo', '' ) );
229 }
230 }
231