| 615 | * If we tried to perform plugin updates, check if we should send an email. |
| 616 | * |
| 617 | * @since 5.4.0 |
| 618 | * |
| 619 | * @param object $update_results The result of the plugin updates. |
| 620 | */ |
| 621 | function after_plugin_update( $update_results ) { |
| 622 | if ( empty( $update_results ) ) { |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | $success_items = array(); |
| 627 | $failure_items = array(); |
| 628 | |
| 629 | foreach ( $update_results as $update_result ) { |
| 630 | if ( true === $update_result->result ) { |
| 631 | $success_items[] = $update_result; |
| 632 | } else { |
| 633 | $failure_items[] = $update_result; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | // No updates were logged. |
| 638 | if ( empty( $success_items ) && empty( $failure_items ) ) { |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | if ( empty( $failure_items ) ) { |
| 643 | $this->send_plugin_email( 'success', $success_items, $failure_items ); |
| 644 | } elseif ( empty( $success_items ) ) { |
| 645 | $this->send_plugin_email( 'fail', $success_items, $failure_items ); |
| 646 | } else { |
| 647 | $this->send_plugin_email( 'mixed', $success_items, $failure_items ); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /** |
| 890 | * Sends an email upon the completion or failure of a plugin background update. |
| 891 | * |
| 892 | * @since 5.4.0 |
| 893 | * |
| 894 | * @param string $type The type of email to send. Can be one of 'success', 'failure', 'mixed'. |
| 895 | * @param array $successful_updates A list of plugin updates that succeeded. |
| 896 | * @param array $failed_updates A list of plugin updates that failed. |
| 897 | */ |
| 898 | protected function send_plugin_email( $type, $successful_updates, $failed_updates ) { |
| 899 | /** |
| 900 | * Filters whether to send an email following an automatic background plugin update. |
| 901 | * |
| 902 | * @since 5.4.0 |
| 903 | * |
| 904 | * @param bool $send Whether to send the email. Default true. |
| 905 | * @param string $type The type of email to send. Can be one of |
| 906 | * 'success', 'failure' or 'mixed'. |
| 907 | * @param object $updates The updates offered that were attempted. |
| 908 | */ |
| 909 | if ( ! apply_filters( 'auto_plugin_update_send_email', true, $type, $this->update_results['plugin'] ) ) { |
| 910 | return; |
| 911 | } |
| 912 | |
| 913 | // No updates were attempted. |
| 914 | if ( empty( $successful_updates ) && empty( $failed_updates ) ) { |
| 915 | return; |
| 916 | } |
| 917 | |
| 918 | $body = array(); |
| 919 | |
| 920 | switch ( $type ) { |
| 921 | case 'success': |
| 922 | /* translators: %s: Site title. */ |
| 923 | $subject = __( '[%s] Plugins have automatically updated' ); |
| 924 | |
| 925 | break; |
| 926 | |
| 927 | case 'fail': |
| 928 | /* translators: %s: Site title. */ |
| 929 | $subject = __( '[%s] Plugins have failed to update' ); |
| 930 | $body[] = sprintf( |
| 931 | /* translators: %s: Home URL. */ |
| 932 | __( 'Howdy! Failures occurred when attempting to update plugins on your site at %s.' ), |
| 933 | home_url() |
| 934 | ); |
| 935 | $body[] = "\n"; |
| 936 | $body[] = __( "Please check out your site now. It's possible that everything is working. If it says you need to update, you should do so." ); |
| 937 | |
| 938 | break; |
| 939 | |
| 940 | case 'mixed': |
| 941 | /* translators: %s: Site title. */ |
| 942 | $subject = __( '[%s] Some plugins have automatically updated' ); |
| 943 | |
| 944 | $body[] = sprintf( |
| 945 | /* translators: %s: Home URL. */ |
| 946 | __( 'Howdy! There were some failures while attempting to update plugins on your site at %s.' ), |
| 947 | home_url() |
| 948 | ); |
| 949 | $body[] = "\n"; |
| 950 | $body[] = __( "Please check out your site now. It's possible that everything is working. If it says you need to update, you should do so." ); |
| 951 | $body[] = "\n"; |
| 952 | |
| 953 | break; |
| 954 | } |
| 955 | |
| 956 | if ( in_array( $type, array( 'fail', 'mixed' ), true ) && ! empty( $failed_updates ) ) { |
| 957 | $body[] = __( 'The following plugins failed to update:' ); |
| 958 | // List failed updates. |
| 959 | foreach ( $failed_updates as $item ) { |
| 960 | /* translators: %s: Name of plugin. */ |
| 961 | $body[] = ' ' . sprintf( __( '- %s' ), $item->name ); |
| 962 | } |
| 963 | $body[] = "\n"; |
| 964 | } |
| 965 | |
| 966 | if ( in_array( $type, array( 'success', 'mixed' ), true ) && ! empty( $successful_updates ) ) { |
| 967 | // Successful updates. |
| 968 | $body[] = __( 'The following plugins were successfully updated:' ); |
| 969 | |
| 970 | foreach ( $successful_updates as $plugin ) { |
| 971 | /* translators: %s: Name of plugin. */ |
| 972 | $body[] = ' ' . sprintf( __( '- %s' ), $plugin->name ); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | $body[] = "\n"; |
| 977 | |
| 978 | // Add a note about the support forums. |
| 979 | $body[] = __( 'If you experience any issues or need support, the volunteers in the WordPress.org support forums may be able to help.' ); |
| 980 | $body[] = __( 'https://wordpress.org/support/forums/' ); |
| 981 | |
| 982 | $body[] = "\n" . __( 'The WordPress Team' ); |
| 983 | |
| 984 | $body = implode( "\n", $body ); |
| 985 | $to = get_site_option( 'admin_email' ); |
| 986 | $subject = sprintf( $subject, wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) ); |
| 987 | $headers = ''; |
| 988 | |
| 989 | $email = compact( 'to', 'subject', 'body', 'headers' ); |
| 990 | |
| 991 | /** |
| 992 | * Filters the email sent following an automatic background plugin update. |
| 993 | * |
| 994 | * @since 5.4.0 |
| 995 | * |
| 996 | * @param array $email { |
| 997 | * Array of email arguments that will be passed to wp_mail(). |
| 998 | * |
| 999 | * @type string $to The email recipient. An array of emails |
| 1000 | * can be returned, as handled by wp_mail(). |
| 1001 | * @type string $subject The email's subject. |
| 1002 | * @type string $body The email message body. |
| 1003 | * @type string $headers Any email headers, defaults to no headers. |
| 1004 | * } |
| 1005 | * @param string $type The type of email being sent. Can be one of |
| 1006 | * 'success', 'fail', 'mixed'. |
| 1007 | * @param object $updates The updates that were attempted. |
| 1008 | */ |
| 1009 | $email = apply_filters( 'auto_plugin_update_email', $email, $type, $this->update_results['plugin'] ); |
| 1010 | |
| 1011 | wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] ); |
| 1012 | } |
| 1013 | |
| 1014 | /** |