emails()->add()`. */ final class Module implements ModuleInterface { /** * {@inheritDoc} */ public function get_id(): string { return 'email_settings'; } /** * {@inheritDoc} */ public function register(): void { $settings = aiou()->settings(); // After every feature tab (their emails are also shown next to the // feature itself, e.g. Username Updater's), just before License. $settings->add_tab( 'emails', __( 'Emails', 'all-in-one-utilities' ), __( 'Customize the subject and body of the emails WordPress and this plugin send, and the sender identity they use.', 'all-in-one-utilities' ), 85 ); $this->migrate_legacy_settings(); $this->register_sender_section( $settings ); $this->register_emails( $settings ); ( new SenderIdentity() )->register(); } /** * Sender identity section. * * @param SettingsRegistry $settings The settings registry. * @return void */ private function register_sender_section( SettingsRegistry $settings ): void { $settings->add_section( 'emails', 'sender', __( 'Sender Identity', 'all-in-one-utilities' ), __( 'Override the "from" name and address used for every email this site sends. Leave blank to keep WordPress\'s defaults.', 'all-in-one-utilities' ), 10 ); $settings->add_field( 'emails', 'sender', new Field( id: 'from_name', label: __( 'From name', 'all-in-one-utilities' ), type: 'text', default: '', args: array( 'placeholder' => get_bloginfo( 'name' ), 'label_above' => true, ), priority: 10 ) ); $settings->add_field( 'emails', 'sender', new Field( id: 'from_email', label: __( 'From email address', 'all-in-one-utilities' ), type: 'text', default: '', args: array( 'placeholder' => get_option( 'admin_email' ), 'label_above' => true, ), doc: new Doc( title: __( 'Sender identity', 'all-in-one-utilities' ), short: __( 'Applies to every email WordPress sends, not just the ones below.', 'all-in-one-utilities' ), warnings: array( __( 'Many email providers reject or mark as spam messages "from" a domain they don\'t recognize as yours (SPF/DKIM). Use an address at your own domain.', 'all-in-one-utilities' ) ) ), priority: 20 ) ); } /** * Register the Admin/User Emails sections and their emails. * * @param SettingsRegistry $settings The settings registry. * @return void */ private function register_emails( SettingsRegistry $settings ): void { $settings->add_section( 'emails', 'test_email', __( 'Test Email', 'all-in-one-utilities' ), __( 'Check that your site can send email.', 'all-in-one-utilities' ), 15 ); $settings->add_field( 'emails', 'test_email', TestEmail::field() ); ( new TestEmail() )->register(); $settings->add_section( 'emails', 'admin_emails', __( 'Admin Emails', 'all-in-one-utilities' ), __( 'Emails sent to the site administrator. Switch one off to stop sending it; open it to change its wording.', 'all-in-one-utilities' ), 20 ); $settings->add_section( 'emails', 'user_emails', __( 'User Emails', 'all-in-one-utilities' ), __( 'Emails sent to users, regardless of role. The ones WordPress itself sends are always sent, so new accounts and password and email changes keep working; open one to change its wording.', 'all-in-one-utilities' ), 30 ); $emails = aiou()->emails(); $emails->add( new NewUserAdminEmail(), 'emails', 'admin_emails', 10 ); $emails->add( new NewUserUserEmail(), 'emails', 'user_emails', 10 ); $emails->add( new PasswordResetEmail(), 'emails', 'user_emails', 20 ); $emails->add( new PasswordChangedEmail(), 'emails', 'user_emails', 30 ); $emails->add( new EmailChangedEmail(), 'emails', 'user_emails', 40 ); $emails->add( new UsernameUpdatedEmail(), 'emails', 'user_emails', 50 ); } /** * One-time move from the pre-accordion layout -- separate * `{id}_enabled` / `{id}_subject` / `{id}_body` fields, where the toggle * meant "use custom wording" -- to one `{ enabled, subject, body }` * value per email, where `enabled` means "send it". Every email was * always sent before, so each becomes enabled; custom wording carries * over only where it was switched on. * * @return void */ private function migrate_legacy_settings(): void { $option = get_option( 'aiou_settings_emails' ); if ( ! is_array( $option ) ) { return; } $layout = array( 'admin_emails' => array( 'new_user_admin' ), 'user_emails' => array( 'new_user_user', 'password_reset', 'password_changed', 'email_changed', 'username_updated' ), ); $changed = false; foreach ( $layout as $section => $ids ) { $values = isset( $option[ $section ] ) && is_array( $option[ $section ] ) ? $option[ $section ] : array(); foreach ( $ids as $id ) { if ( ! array_key_exists( $id . '_enabled', $values ) && ! array_key_exists( $id . '_subject', $values ) && ! array_key_exists( $id . '_body', $values ) ) { continue; } $email = array( 'enabled' => 1 ); if ( ! empty( $values[ $id . '_enabled' ] ) ) { foreach ( array( 'subject', 'body' ) as $part ) { if ( '' !== trim( (string) ( $values[ $id . '_' . $part ] ?? '' ) ) ) { $email[ $part ] = (string) $values[ $id . '_' . $part ]; } } } unset( $values[ $id . '_enabled' ], $values[ $id . '_subject' ], $values[ $id . '_body' ] ); if ( ! isset( $values[ $id ] ) ) { $values[ $id ] = $email; } $changed = true; } $option[ $section ] = $values; } if ( $changed ) { update_option( 'aiou_settings_emails', $option ); } } }