| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
/** |
| 6 |
* A helper to determine the status of ftc and front-end related configuration. |
| 7 |
*/ |
| 8 |
class First_Time_Configuration_Notice_Helper { |
| 9 |
|
| 10 |
/** |
| 11 |
* The options' helper. |
| 12 |
* |
| 13 |
* @var Options_Helper |
| 14 |
*/ |
| 15 |
private $options_helper; |
| 16 |
|
| 17 |
/** |
| 18 |
* The indexing helper. |
| 19 |
* |
| 20 |
* @var Indexing_Helper |
| 21 |
*/ |
| 22 |
private $indexing_helper; |
| 23 |
|
| 24 |
/** |
| 25 |
* Whether we show the alternate mesage. |
| 26 |
* |
| 27 |
* @var bool |
| 28 |
*/ |
| 29 |
private $show_alternate_message; |
| 30 |
|
| 31 |
/** |
| 32 |
* First_Time_Configuration_Notice_Integration constructor. |
| 33 |
* |
| 34 |
* @param Options_Helper $options_helper The options helper. |
| 35 |
* @param Indexing_Helper $indexing_helper The indexing helper. |
| 36 |
*/ |
| 37 |
public function __construct( Options_Helper $options_helper, Indexing_Helper $indexing_helper ) { |
| 38 |
$this->options_helper = $options_helper; |
| 39 |
$this->indexing_helper = $indexing_helper; |
| 40 |
$this->show_alternate_message = false; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Determines whether and where the "First-time SEO Configuration" admin notice should be displayed. |
| 45 |
* |
| 46 |
* @return bool Whether the "First-time SEO Configuration" admin notice should be displayed. |
| 47 |
*/ |
| 48 |
public function should_display_first_time_configuration_notice() { |
| 49 |
if ( ! $this->options_helper->get( 'dismiss_configuration_workout_notice', false ) === false ) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
if ( ! $this->on_wpseo_admin_page_or_dashboard() ) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
return $this->first_time_configuration_not_finished(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Gets the first time configuration title based on the show_alternate_message boolean |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
public function get_first_time_configuration_title() { |
| 64 |
return ( ! $this->show_alternate_message ) ? \__( 'First-time SEO configuration', 'wordpress-seo' ) : \__( 'SEO configuration', 'wordpress-seo' ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Determines if the first time configuration is completely finished. |
| 69 |
* |
| 70 |
* @return bool |
| 71 |
*/ |
| 72 |
public function first_time_configuration_not_finished() { |
| 73 |
if ( ! $this->user_can_do_first_time_configuration() ) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
if ( $this->is_first_time_configuration_finished() ) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
if ( $this->options_helper->get( 'first_time_install', false ) !== false ) { |
| 82 |
return ! $this->are_site_representation_name_and_logo_set() || $this->indexing_helper->get_unindexed_count() > 0; |
| 83 |
} |
| 84 |
|
| 85 |
if ( $this->indexing_helper->is_initial_indexing() === false ) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
if ( $this->indexing_helper->is_finished_indexables_indexing() === true ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
$this->show_alternate_message = true; |
| 94 |
|
| 95 |
return ! $this->are_site_representation_name_and_logo_set(); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Whether the user can do the first-time configuration. |
| 100 |
* |
| 101 |
* @return bool Whether the current user can do the first-time configuration. |
| 102 |
*/ |
| 103 |
private function user_can_do_first_time_configuration() { |
| 104 |
return \current_user_can( 'wpseo_manage_options' ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Whether the user is currently visiting one of our admin pages or the WordPress dashboard. |
| 109 |
* |
| 110 |
* @return bool Whether the current page is a Yoast SEO admin page |
| 111 |
*/ |
| 112 |
private function on_wpseo_admin_page_or_dashboard() { |
| 113 |
$pagenow = $GLOBALS['pagenow']; |
| 114 |
|
| 115 |
// Show on the WP Dashboard. |
| 116 |
if ( $pagenow === 'index.php' ) { |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 121 |
if ( isset( $_GET['page'] ) && \is_string( $_GET['page'] ) ) { |
| 122 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information and only comparing the variable in a condition. |
| 123 |
$page_from_get = \wp_unslash( $_GET['page'] ); |
| 124 |
|
| 125 |
// Show on Yoast SEO pages, with some exceptions. |
| 126 |
if ( $pagenow === 'admin.php' && \strpos( $page_from_get, 'wpseo' ) === 0 ) { |
| 127 |
$exceptions = [ |
| 128 |
'wpseo_installation_successful', |
| 129 |
'wpseo_installation_successful_free', |
| 130 |
]; |
| 131 |
|
| 132 |
if ( ! \in_array( $page_from_get, $exceptions, true ) ) { |
| 133 |
return true; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Whether all steps of the first-time configuration have been finished. |
| 143 |
* |
| 144 |
* @param bool $for_task_list Whether this is called for the task list. |
| 145 |
* |
| 146 |
* @return bool Whether the first-time configuration has been finished. |
| 147 |
*/ |
| 148 |
public function is_first_time_configuration_finished( $for_task_list = false ) { |
| 149 |
$configuration_finished_steps = $this->options_helper->get( 'configuration_finished_steps', [] ); |
| 150 |
$number_of_steps_of_completed_ftc = ( $for_task_list ) ? 4 : 3; |
| 151 |
|
| 152 |
return \count( $configuration_finished_steps ) === $number_of_steps_of_completed_ftc; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Whether the site representation name and logo have been set. |
| 157 |
* |
| 158 |
* @return bool Whether the site representation name and logo have been set. |
| 159 |
*/ |
| 160 |
private function are_site_representation_name_and_logo_set() { |
| 161 |
$company_or_person = $this->options_helper->get( 'company_or_person', '' ); |
| 162 |
|
| 163 |
if ( $company_or_person === '' ) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
if ( $company_or_person === 'company' ) { |
| 168 |
return ! empty( $this->options_helper->get( 'company_name' ) ) && ! empty( $this->options_helper->get( 'company_logo', '' ) ); |
| 169 |
} |
| 170 |
|
| 171 |
return ! empty( $this->options_helper->get( 'company_or_person_user_id' ) ) && ! empty( $this->options_helper->get( 'person_logo', '' ) ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Getter for the show alternate message boolean. |
| 176 |
* |
| 177 |
* @return bool |
| 178 |
*/ |
| 179 |
public function should_show_alternate_message() { |
| 180 |
return $this->show_alternate_message; |
| 181 |
} |
| 182 |
} |
| 183 |
|