array( 'hook' => 'recovery_mode_email', 'type' => 'email', 'default' => true, ), 'core_update' => array( 'hook' => 'auto_core_update_email', 'type' => 'email', 'default' => true, ), 'plugin_theme_update' => array( 'hook' => 'auto_plugin_theme_update_email', 'type' => 'email', 'default' => true, ), 'update_debug' => array( 'hook' => 'automatic_updates_debug_email', 'type' => 'email', 'default' => true, ), 'new_user' => array( 'hook' => 'wp_new_user_notification_email_admin', 'type' => 'email', 'default' => false, ), 'comment_moderation' => array( 'hook' => 'comment_moderation_headers', 'type' => 'headers', 'default' => false, ), ); $filtered = apply_filters( 'e3p_bccpilot_notifications', $notifications ); return is_array( $filtered ) ? $filtered : $notifications; } /** * Testi mostrati nella schermata per ogni voce del catalogo. * * Tenuti separati dal catalogo perché le traduzioni non si possono chiamare * prima di `init`, mentre i filtri vanno agganciati già in `plugins_loaded` * (la mail di errore critico può partire prima di `init`). * Il filtro `e3p_bccpilot_notification_texts` permette di aggiungere i testi delle voci esterne. * * @return array id => array( label, description ) */ function e3p_bccpilot_get_notification_texts() { $texts = array( 'recovery_mode' => array( 'label' => __( 'Critical error (recovery mode)', 'bccpilot' ), 'description' => __( 'The "Your Site is Experiencing a Technical Issue" email.', 'bccpilot' ), ), 'core_update' => array( 'label' => __( 'WordPress automatic updates', 'bccpilot' ), 'description' => __( 'Result of automatic core updates.', 'bccpilot' ), ), 'plugin_theme_update' => array( 'label' => __( 'Plugin and theme automatic updates', 'bccpilot' ), 'description' => __( 'Result of automatic plugin and theme updates.', 'bccpilot' ), ), 'update_debug' => array( 'label' => __( 'Automatic updates debug', 'bccpilot' ), 'description' => __( 'Debug email about automatic updates.', 'bccpilot' ), ), 'new_user' => array( 'label' => __( 'New user registration', 'bccpilot' ), 'description' => __( 'Notice sent to the administrator when a new user registers.', 'bccpilot' ), ), 'comment_moderation' => array( 'label' => __( 'Comment awaiting moderation', 'bccpilot' ), 'description' => __( 'Notice sent when a comment needs to be approved.', 'bccpilot' ), ), ); $filtered = apply_filters( 'e3p_bccpilot_notification_texts', $texts ); return is_array( $filtered ) ? $filtered : $texts; } /** * Impostazioni salvate, con i valori di default per ciò che manca. * * @return array { email: string, notifications: string[] } */ function e3p_bccpilot_get_settings() { $defaults = array( 'email' => '', 'notifications' => array(), ); foreach ( e3p_bccpilot_get_notifications() as $id => $notification ) { if ( ! empty( $notification['default'] ) ) { $defaults['notifications'][] = $id; } } $saved = get_option( 'e3p_bccpilot_settings', null ); if ( ! is_array( $saved ) ) { return $defaults; } return array( 'email' => isset( $saved['email'] ) ? (string) $saved['email'] : '', 'notifications' => isset( $saved['notifications'] ) && is_array( $saved['notifications'] ) ? $saved['notifications'] : array(), ); } /** * Indirizzo che riceve la copia. Stringa vuota se non c'è un indirizzo valido. * * @return string */ function e3p_bccpilot_get_email() { $settings = e3p_bccpilot_get_settings(); return is_email( $settings['email'] ) ? $settings['email'] : ''; } /** * Aggiunge l'header Bcc a un insieme di header (stringa o array). * * @param string|array $headers Header esistenti. * @return array */ function e3p_bccpilot_append_bcc( $headers ) { $email = e3p_bccpilot_get_email(); if ( ! is_array( $headers ) ) { $headers = array_filter( explode( "\n", str_replace( "\r\n", "\n", (string) $headers ) ) ); } if ( '' !== $email ) { $headers[] = 'Bcc: ' . $email; } return $headers; } /** * Callback per i filtri che ricevono l'array in stile wp_mail. * * @param array $email to, subject, message, headers. * @return array */ function e3p_bccpilot_add_bcc( $email ) { if ( ! is_array( $email ) ) { return $email; } $email['headers'] = e3p_bccpilot_append_bcc( isset( $email['headers'] ) ? $email['headers'] : array() ); return $email; } /** * Callback per i filtri che ricevono solo gli header. * * @param string|array $headers Header esistenti. * @return array */ function e3p_bccpilot_add_bcc_headers( $headers ) { return e3p_bccpilot_append_bcc( $headers ); } /** * Aggancia i filtri delle sole notifiche scelte. Senza un indirizzo valido * non aggancia nulla. */ function e3p_bccpilot_register_filters() { if ( '' === e3p_bccpilot_get_email() ) { return; } $settings = e3p_bccpilot_get_settings(); $notifications = e3p_bccpilot_get_notifications(); foreach ( $settings['notifications'] as $id ) { if ( ! isset( $notifications[ $id ] ) ) { continue; } $callback = 'headers' === $notifications[ $id ]['type'] ? 'e3p_bccpilot_add_bcc_headers' : 'e3p_bccpilot_add_bcc'; add_filter( $notifications[ $id ]['hook'], $callback ); } } add_action( 'plugins_loaded', 'e3p_bccpilot_register_filters' ); /** * Pulisce i dati inviati dal modulo delle impostazioni. * * @param mixed $input Dati grezzi dal modulo. * @return array */ function e3p_bccpilot_sanitize_settings( $input ) { $input = is_array( $input ) ? $input : array(); $clean = array( 'email' => '', 'notifications' => array(), ); $email = isset( $input['email'] ) && is_string( $input['email'] ) ? trim( sanitize_text_field( wp_unslash( $input['email'] ) ) ) : ''; if ( '' !== $email && ! is_email( $email ) ) { // Indirizzo non valido: errore e si mantiene quello già salvato. add_settings_error( 'e3p_bccpilot_settings', 'e3p_bccpilot_invalid_email', __( 'The email address is not valid.', 'bccpilot' ) ); $clean['email'] = e3p_bccpilot_get_settings()['email']; } else { $clean['email'] = $email; } if ( isset( $input['notifications'] ) && is_array( $input['notifications'] ) ) { $known = array_keys( e3p_bccpilot_get_notifications() ); foreach ( $input['notifications'] as $id ) { $id = sanitize_key( $id ); if ( in_array( $id, $known, true ) ) { $clean['notifications'][] = $id; } } } return $clean; } function e3p_bccpilot_register_settings() { register_setting( 'e3p_bccpilot_group', 'e3p_bccpilot_settings', array( 'type' => 'array', 'sanitize_callback' => 'e3p_bccpilot_sanitize_settings', ) ); } add_action( 'admin_init', 'e3p_bccpilot_register_settings' ); function e3p_bccpilot_add_settings_page() { add_options_page( __( 'BccPilot', 'bccpilot' ), __( 'BccPilot', 'bccpilot' ), 'manage_options', 'bccpilot', 'e3p_bccpilot_render_settings_page' ); } add_action( 'admin_menu', 'e3p_bccpilot_add_settings_page' ); function e3p_bccpilot_render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $settings = e3p_bccpilot_get_settings(); $notifications = e3p_bccpilot_get_notifications(); $texts = e3p_bccpilot_get_notification_texts(); ?>