|
| 1 | +<?php |
| 2 | +/* |
| 3 | +Plugin Name: Send message |
| 4 | +Version: 0.1.0 |
| 5 | +Description: Send an email (subject and text message) to WordPress users. |
| 6 | +Plugin URI: https://github.com/szepeviktor/wordpress-plugin-construction |
| 7 | +License: GPLv2 (or later) |
| 8 | +Author: Viktor Szépe |
| 9 | +GitHub Plugin URI: https://github.com/szepeviktor/wordpress-plugin-construction |
| 10 | +*/ |
| 11 | + |
| 12 | +if ( ! function_exists( 'add_filter' ) ) { |
| 13 | + error_log( 'Break-in attempt detected: send_message_direct_access ' |
| 14 | + . addslashes( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' ) |
| 15 | + ); |
| 16 | + ob_get_level() && ob_end_clean(); |
| 17 | + if ( ! headers_sent() ) { |
| 18 | + header( 'Status: 403 Forbidden' ); |
| 19 | + header( 'HTTP/1.1 403 Forbidden', true, 403 ); |
| 20 | + header( 'Connection: Close' ); |
| 21 | + } |
| 22 | + exit; |
| 23 | +} |
| 24 | + |
| 25 | +final class O1_Send_Message { |
| 26 | + |
| 27 | + private $capability = 'list_users'; |
| 28 | + private $from_name = ''; |
| 29 | + |
| 30 | + public function __construct() { |
| 31 | + |
| 32 | + if ( ! is_admin() ) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + add_action( 'init', array( $this, 'init' ) ); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + public function init() { |
| 41 | + |
| 42 | + if ( ! current_user_can( $this->capability ) ) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + // @TODO Bulk action, multiple recipients |
| 47 | + add_filter( 'user_row_actions', array( $this, 'user_action' ), 0, 2 ); |
| 48 | + |
| 49 | + if ( is_network_admin() ) { |
| 50 | + add_action( 'network_admin_menu', array( $this, 'register_submenu' ) ); |
| 51 | + } else { |
| 52 | + add_action( 'admin_menu', array( $this, 'register_submenu' ) ); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public function user_action( $actions, $user ) { |
| 57 | + |
| 58 | + $actions['message'] = sprintf( '<a class="sendmessage" href="%s">%s</a>', |
| 59 | + wp_nonce_url( admin_url( 'users.php?page=sendmessage&user_id=' . $user->ID ), 'sendmessage' ), |
| 60 | + __( 'Send message', 'send-message' ) |
| 61 | + ); |
| 62 | + |
| 63 | + return $actions; |
| 64 | + } |
| 65 | + |
| 66 | + public function register_submenu() { |
| 67 | + |
| 68 | + add_submenu_page( |
| 69 | + 'users.php', |
| 70 | + __( 'Page Title', 'send-message' ), |
| 71 | + __( 'Menu item', 'send-message' ), |
| 72 | + $this->capability, |
| 73 | + 'sendmessage', |
| 74 | + array( $this, 'submenu_callback' ) |
| 75 | + ); |
| 76 | + // Hide menu |
| 77 | + remove_submenu_page( 'users.php', 'sendmessage' ); |
| 78 | + } |
| 79 | + |
| 80 | + public function from_name( $old_name ) { |
| 81 | + |
| 82 | + return $this->from_name; |
| 83 | + } |
| 84 | + |
| 85 | + private function process_post( $user ) { |
| 86 | + |
| 87 | + if ( ! isset( $_POST['submit'] ) ) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + // From |
| 92 | + $current_user = wp_get_current_user(); |
| 93 | + $this->from_name = $current_user->display_name; |
| 94 | + add_filter( 'wp_mail_from_name', array( $this, 'from_name' ), 9999 ); |
| 95 | + $from = get_option( 'admin_email' ); |
| 96 | + |
| 97 | + // To |
| 98 | + $to = $user->user_email; |
| 99 | + |
| 100 | + // Subject |
| 101 | + if ( empty( $_POST['message_subject'] ) || empty( trim( $_POST['message_subject'] ) ) ) { |
| 102 | + $subject = __( 'Message from ', 'send-message' ) . get_bloginfo( 'name' ); |
| 103 | + } else { |
| 104 | + $subject = $_POST['message_subject']; |
| 105 | + } |
| 106 | + |
| 107 | + // Message |
| 108 | + if ( empty( $_POST['message_text'] ) || empty( trim( $_POST['message_text'] ) ) ) { |
| 109 | + return array( |
| 110 | + 'notice_class' => 'notice-warning', |
| 111 | + 'notice_text' => 'Empty message. Please enter some text.', |
| 112 | + ); |
| 113 | + } |
| 114 | + $message = $_POST['message_text'] ; |
| 115 | + |
| 116 | + if ( wp_mail( $to, $subject, $message ) ) { |
| 117 | + return array( |
| 118 | + 'notice_class' => 'notice-success', |
| 119 | + 'notice_text' => 'E-mail sent.', |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + public function submenu_callback() { |
| 126 | + |
| 127 | + /** |
| 128 | + * 1. Show empty form |
| 129 | + * 2. Send + Show sent form |
| 130 | + */ |
| 131 | + |
| 132 | + // Protect form |
| 133 | + check_admin_referer( 'sendmessage' ); |
| 134 | + |
| 135 | + // To |
| 136 | + $user = get_user_by( 'ID', intval( $_GET['user_id'] ) ); |
| 137 | + if ( false === $user ) { |
| 138 | + wp_die( |
| 139 | + '<h1>' . __( 'Cheatin’ uh?' ) . '</h1>' . |
| 140 | + '<p>' . __( 'There are no valid users selected for recipients.', 'send-message' ) . '</p>', |
| 141 | + 403 |
| 142 | + ); |
| 143 | + } |
| 144 | + $to_name = $user->display_name; |
| 145 | + |
| 146 | + // Send email on submit |
| 147 | + $result = $this->process_post( $user ); |
| 148 | + |
| 149 | + ?> |
| 150 | + <div class="wrap" id="send-message-page"> |
| 151 | + <h1>Send Message</h1> |
| 152 | + |
| 153 | + <?php if ( is_array( $result ) ) : ?> |
| 154 | + <div class="notice <?php echo esc_attr( $result['notice_class'] ); ?> is-dismissible"> |
| 155 | + <p><?php echo esc_html( $result['notice_text'] ); ?></p> |
| 156 | + </div> |
| 157 | + <?php endif; ?> |
| 158 | + |
| 159 | + <form id="send-message" method="post" novalidate="novalidate" |
| 160 | + action="<?php echo esc_url( self_admin_url( 'users.php?page=sendmessage&user_id=' . $user->ID ) ); ?>"> |
| 161 | + <?php wp_nonce_field( 'sendmessage' ); ?> |
| 162 | + <table class="form-table"> |
| 163 | + <tbody> |
| 164 | + <tr class="message-to-wrap"> |
| 165 | + <th><label for="message_to_name"><?php esc_html_e( 'To', 'send-message' ); ?></label></th> |
| 166 | + <td><input name="to_name" id="to_name" value="<?php echo esc_attr( $to_name ); ?>" |
| 167 | + class="regular-text" type="text" disabled></td> |
| 168 | + </tr> |
| 169 | + <tr class="message-subject-wrap"> |
| 170 | + <th><label for="message_subject"><?php esc_html_e( 'Subject', 'send-message' ); ?></label></th> |
| 171 | + <td><input name="message_subject" id="message_subject" value="" |
| 172 | + class="regular-text" type="text" autofocus></td> |
| 173 | + </tr> |
| 174 | + <tr class="message-text-wrap"> |
| 175 | + <th><label for="message_text"><?php esc_html_e( 'Message', 'send-message' ); ?></label></th> |
| 176 | + <td><textarea name="message_text" id="message_text" value="" |
| 177 | + rows="5" class="regular-text"></textarea></td> |
| 178 | + </tr> |
| 179 | + </tbody> |
| 180 | + </table> |
| 181 | + <p class="submit"><input name="submit" id="submit" |
| 182 | + class="button button-primary" value="<?php esc_attr_e( 'Send', 'send-message' ); ?>" type="submit"></p> |
| 183 | + </form> |
| 184 | + </div> |
| 185 | + <?php |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +new O1_Send_Message(); |
0 commit comments