使用301重定向SEO,将所有404错误自动重定向到任何页面。您可以重定向和记录每404个错误。网站管理员工具中不再出现404错误。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

224 lines
4.9 KiB

  1. <?php
  2. // If this file is called directly, abort.
  3. defined( 'ABSPATH' ) or exit;
  4. /**
  5. * The main 404 email alert class.
  6. *
  7. * This class handles the email alert task for the
  8. * 404 errors found.
  9. *
  10. * @category Core
  11. * @package JJ4T3
  12. * @subpackage EmailAlert
  13. * @author Joel James <mail@cjoel.com>
  14. * @license http://www.gnu.org/licenses/ GNU General Public License
  15. * @link https://duckdev.com/products/404-to-301/
  16. */
  17. class JJ4T3_404_Email {
  18. /**
  19. * Error data class object.
  20. *
  21. * @var object
  22. * @access private
  23. * @since 3.0.0
  24. */
  25. private $error_data;
  26. /**
  27. * Recipient email addresses.
  28. *
  29. * @var string|array
  30. * @access public
  31. */
  32. public $recipient;
  33. /**
  34. * Email subject.
  35. *
  36. * @var string
  37. * @access public
  38. */
  39. public $subject;
  40. /**
  41. * Email headers.
  42. *
  43. * @var array
  44. * @access public
  45. */
  46. public $headers;
  47. /**
  48. * Email content body.
  49. *
  50. * @var string
  51. * @access public
  52. */
  53. public $body;
  54. /**
  55. * Initialize the class and set properties.
  56. *
  57. * @param object $error_data Error logs data class.
  58. *
  59. * @since 3.0.0
  60. * @access public
  61. */
  62. public function __construct( $error_data ) {
  63. $this->error_data = $error_data;
  64. // Set required properties.
  65. $this->set_recipient();
  66. $this->set_subject();
  67. $this->set_headers();
  68. $this->set_body();
  69. }
  70. /**
  71. * Send email alert about the error.
  72. *
  73. * Registering new action hook "jj4t3_before_email".
  74. *
  75. * @since 3.0.0
  76. * @access public
  77. */
  78. public function send_email() {
  79. /**
  80. * Action hook to perform before email alert.
  81. *
  82. * Sending email using wp_mail() function.
  83. *
  84. * @since 3.0.0
  85. *
  86. * @param string $this ->recipient Email recipient.
  87. * @param string $this ->subject Email subject.
  88. * @param string $this ->body Email body.
  89. */
  90. do_action( 'jj4t3_before_email', $this->recipient, $this->subject, $this->body );
  91. //var_dump($this->recipient); exit;
  92. // Send email using wp_mail().
  93. wp_mail( $this->recipient, $this->subject, $this->body, $this->headers );
  94. }
  95. /**
  96. * Set email recipients.
  97. *
  98. * Registering filter - "jj4t3_email_recipient".
  99. *
  100. * @since 3.0.0
  101. * @access private
  102. */
  103. private function set_recipient() {
  104. // Get email recipient if set.
  105. $recipient = jj4t3_get_option( 'email_notify_address', get_option( 'admin_email' ) );
  106. /**
  107. * Filter to alter email recipient.
  108. *
  109. * @since 3.0.0
  110. */
  111. $this->recipient = apply_filters( 'jj4t3_email_recipient', $recipient );
  112. }
  113. /**
  114. * Set subject for the 404 email alert.
  115. *
  116. * Registering filter - "jj4t3_email_subject".
  117. *
  118. * @since 3.0.0
  119. * @access private
  120. */
  121. private function set_subject() {
  122. // Include site title.
  123. $message = __( 'Snap! One more 404 on ', '404-to-301' ) . get_bloginfo( 'name' );
  124. /**
  125. * Filter to alter email subject text.
  126. *
  127. * @since 3.0.0
  128. */
  129. $this->subject = apply_filters( 'jj4t3_email_subject', $message );
  130. }
  131. /**
  132. * Set email headers.
  133. *
  134. * Registering filter - "jj4t3_email_headers".
  135. *
  136. * @since 3.0.0
  137. * @access private
  138. */
  139. private function set_headers() {
  140. /**
  141. * Filter to alter From name of email alert.
  142. *
  143. * @since 3.0.0
  144. */
  145. $from_name = apply_filters( 'jj4t3_email_header_name', get_bloginfo( 'name' ) );
  146. /**
  147. * Filter to alter From email address of email alert.
  148. *
  149. * @since 3.0.0
  150. */
  151. $from_email = apply_filters( 'jj4t3_email_header_email', get_option( 'admin_email' ) );
  152. $this->headers[] = "From: " . $from_name . " <" . $from_email . ">" . "\r\n";
  153. $this->headers[] = "Content-Type: text/html; charset=UTF-8";
  154. }
  155. /**
  156. * Set content for the email alert.
  157. *
  158. * Registering filter - "jj4t3_email_body".
  159. *
  160. * @since 3.0.0
  161. * @access private
  162. */
  163. private function set_body() {
  164. $message = "<p>" . __( 'Bummer! You have one more 404', '404-to-301' ) . "</p>";
  165. $message .= '<table>';
  166. // 404 path.
  167. $message .= '<tr>';
  168. $message .= '<th align="left">' . __( '404 Path', '404-to-301' ) . '</th>';
  169. $message .= '<td align="left">' . $this->error_data->url . '</td>';
  170. $message .= '</tr>';
  171. // IP Address.
  172. $message .= '<tr>';
  173. $message .= '<th align="left">' . __( 'IP Address', '404-to-301' ) . '</th>';
  174. $message .= '<td align="left">' . $this->error_data->ip . '</td>';
  175. $message .= '</tr>';
  176. // Date and time.
  177. $message .= '<tr>';
  178. $message .= '<th align="left">' . __( 'Time', '404-to-301' ) . '</th>';
  179. $message .= '<td align="left">' . $this->error_data->time . '</td>';
  180. $message .= '</tr>';
  181. // Referral url.
  182. $message .= '<tr>';
  183. $message .= '<th align="left">' . __( 'Referral Page', '404-to-301' ) . '</th>';
  184. $message .= '<td align="left">' . $this->error_data->ref . '</td>';
  185. $message .= '</tr>';
  186. $message .= '</table>';
  187. // Who sent me this alert?
  188. $message .= '<p>' . sprintf( __( 'Alert sent by the %s404 to 301%s plugin for WordPress.', '404-to-301' ), '<strong>', '</strong>' ) . '</p>';
  189. /**
  190. * Filter to alter email content.
  191. *
  192. * @since 3.0.0
  193. */
  194. $this->body = apply_filters( 'jj4t3_email_body', $message );
  195. }
  196. }