progress = $progress; } public function register(): void { \add_filter( 'admin_footer_text', [ $this, 'footerText' ] ); \add_action( 'admin_notices', [ $this, 'renderNotice' ] ); \add_action( 'admin_post_' . self::ACTION, [ $this, 'handleAction' ] ); } /** * @param mixed $text * @return mixed */ public function footerText( $text ) { if ( ! $this->isPluginScreen() ) { return $text; } return sprintf( \esc_html__( 'Enjoying Agent001? Please rate us %s on WordPress.org. Thank you!', 'agent001-support-assistant-for-woocommerce' ), '★★★★★' ); } public function renderNotice(): void { if ( ! $this->isPluginScreen() || ! \current_user_can( SettingsPage::CAPABILITY ) || ! $this->isDue() ) { return; } echo '

'; \esc_html_e( 'Agent001 is up and running in your store. If the assistant helps your shoppers, would you mind leaving a quick review on WordPress.org? It really helps us grow.', 'agent001-support-assistant-for-woocommerce' ); echo '

'; echo '' . \esc_html__( 'Rate Agent001', 'agent001-support-assistant-for-woocommerce' ) . ' '; echo '' . \esc_html__( 'Maybe later', 'agent001-support-assistant-for-woocommerce' ) . ' '; echo '' . \esc_html__( "Don't ask again", 'agent001-support-assistant-for-woocommerce' ) . ''; echo '

'; } public function handleAction(): void { if ( ! \current_user_can( SettingsPage::CAPABILITY ) ) { \wp_die( \esc_html__( 'Permission denied.', 'agent001-support-assistant-for-woocommerce' ), '', [ 'response' => 403 ] ); } \check_admin_referer( self::ACTION ); $choice = isset( $_GET['choice'] ) && is_string( $_GET['choice'] ) ? \sanitize_key( \wp_unslash( $_GET['choice'] ) ) : ''; \update_user_meta( \get_current_user_id(), self::META, $choice === 'later' ? time() + self::LATER_DELAY_SECONDS : 'done' ); if ( $choice === 'rate' ) { \add_filter( 'allowed_redirect_hosts', static function ( array $hosts ): array { $hosts[] = 'wordpress.org'; return $hosts; } ); \wp_safe_redirect( self::REVIEW_URL ); exit; } \wp_safe_redirect( \add_query_arg( [ 'page' => SettingsPage::PAGE_SLUG ], \admin_url( 'admin.php' ) ) ); exit; } public function isDue(): bool { if ( \get_option( Onboarding::OPTION, '' ) !== 'complete' ) { return false; } if ( $this->progress->current()['phase'] !== SyncProgress::PHASE_DONE ) { return false; } $userId = \get_current_user_id(); $state = \get_user_meta( $userId, self::META, true ); if ( $state === '' || $state === false ) { \update_user_meta( $userId, self::META, time() + self::FIRST_DELAY_SECONDS ); return false; } return is_numeric( $state ) && time() >= (int) $state; } private function actionUrl( string $choice ): string { return \wp_nonce_url( \admin_url( 'admin-post.php?action=' . self::ACTION . '&choice=' . $choice ), self::ACTION ); } private function isPluginScreen(): bool { $screen = \function_exists( 'get_current_screen' ) ? \get_current_screen() : null; return $screen !== null && $screen->id === 'toplevel_page_' . SettingsPage::PAGE_SLUG; } }