使用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.
 
 
 
 
 

377 lines
8.4 KiB

  1. <?php
  2. // If this file is called directly, abort.
  3. defined( 'ABSPATH' ) or exit;
  4. /**
  5. * Get plugin setting value.
  6. *
  7. * Handle the exceptional case properly
  8. * and return false.
  9. *
  10. * @param mixed $option Option name.
  11. * @param mixed $default Default value if not exist.
  12. *
  13. * @since 3.0.0
  14. * @access public
  15. *
  16. * @return string|array
  17. */
  18. function jj4t3_get_option( $option = false, $default = false ) {
  19. if ( ! $option ) {
  20. return $default;
  21. }
  22. // Get our plugin settings value.
  23. $settings = (array) get_option( 'i4t3_gnrl_options', array() );
  24. // Return false, if not exist.
  25. if ( empty( $settings[ $option ] ) ) {
  26. return $default;
  27. }
  28. return $settings[ $option ];
  29. }
  30. /**
  31. * Update a single setting value.
  32. *
  33. * This helper function is used to update a single
  34. * setting value of our plugin settings array.
  35. * Default WordPress update_option() function can
  36. * update the the array only.
  37. *
  38. * @param string $option Option name.
  39. * @param mixed $value Value to update.
  40. *
  41. * @since 3.0.0
  42. * @access public
  43. *
  44. * @return void
  45. */
  46. function jj4t3_update_option( $option, $value = '' ) {
  47. $settings = (array) get_option( 'i4t3_gnrl_options', array() );
  48. $settings[ $option ] = $value;
  49. update_option( 'i4t3_gnrl_options', $settings );
  50. }
  51. /**
  52. * Check if the redirect for 404 enabled.
  53. *
  54. * Check if the user selected "No Redirect" option
  55. * for redirect.
  56. * Registering filter - "jj4t3_redirect_enabled".
  57. *
  58. * @since 3.0.0
  59. * @access public
  60. *
  61. * @return boolean
  62. */
  63. function jj4t3_redirect_enabled() {
  64. // Get redirect to option value.
  65. $enabled = jj4t3_redirect_to();
  66. /**
  67. * Filter hook to alter redirect option.
  68. *
  69. * Return boolean to enable/disable redirect.
  70. *
  71. * @since 2.0.0
  72. */
  73. return (bool) apply_filters( 'jj4t3_redirect_enabled', $enabled );
  74. }
  75. /**
  76. * Check if the email notification is enabled.
  77. *
  78. * Registering filter - "jj4t3_email_notify_enabled".
  79. *
  80. * @since 3.0.0
  81. * @access public
  82. *
  83. * @return boolean
  84. */
  85. function jj4t3_email_notify_enabled() {
  86. // Get email notification option.
  87. $enabled = jj4t3_get_option( 'email_notify' );
  88. /**
  89. * Filter hook to alter email notification option.
  90. *
  91. * Return boolean to enable/disable. For whatever value you return
  92. * it will consider it's boolean only.
  93. *
  94. * @since 2.0.0
  95. */
  96. return (bool) apply_filters( 'jj4t3_email_notify_enabled', $enabled );
  97. }
  98. /**
  99. * Check if the error logging is enabled.
  100. *
  101. * Registering filter - "jj4t3_log_enabled".
  102. *
  103. * @since 3.0.0
  104. * @access public
  105. *
  106. * @return boolean
  107. */
  108. function jj4t3_log_enabled() {
  109. // Get error logging option.
  110. $enabled = jj4t3_get_option( 'redirect_log' );
  111. /**
  112. * Filter hook to alter error logging.
  113. *
  114. * Return boolean to enable/disable logging.For whatever value you return
  115. * it will consider it's boolean only.
  116. *
  117. * @since 2.0.0
  118. */
  119. return (bool) apply_filters( 'jj4t3_log_enabled', $enabled );
  120. }
  121. /**
  122. * Get enabled redirect to.
  123. *
  124. * Retured type of redirect target.
  125. * If redirect is not enabled, return false.
  126. * If unknown value is given return false.
  127. *
  128. * @since 3.0.0
  129. * @access public
  130. *
  131. * @return string|boolean
  132. */
  133. function jj4t3_redirect_to() {
  134. /**
  135. * Filter hook to get redirect to option value.
  136. *
  137. * Accepts only 2 values - page, link.
  138. * If any other value is returned, it will considered
  139. * as the redirect is disabled.
  140. *
  141. * @since 2.0.0
  142. */
  143. $to = apply_filters( 'jj4t3_redirect_to', jj4t3_get_option( 'redirect_to' ) );
  144. // Verify that only allowed values accepted.
  145. if ( in_array( $to, array( 'page', 'link' ) ) ) {
  146. return $to;
  147. }
  148. return false;
  149. }
  150. /**
  151. * Get redirect type.
  152. *
  153. * This function is used to get the redirect
  154. * status code selected by the user.
  155. * Registering filter - jj4t3_redirect_type
  156. * to alter redirect status code.
  157. *
  158. * @since 3.0.0
  159. * @access private
  160. *
  161. * @return int Redirect status code.
  162. */
  163. function jj4t3_redirect_type() {
  164. $type = (int) jj4t3_get_option( 'redirect_type' );
  165. /**
  166. * Filter to modify currently set redirect type.
  167. *
  168. * Return only valid HTTP status codes.
  169. * If you are returning custom status codes other than the default
  170. * values, please make sure that you have added that to "jj4t3_redirect_statuses"
  171. * filter first. Otherwise it will be ignored.
  172. *
  173. * @since 2.0.0
  174. */
  175. $status = apply_filters( 'jj4t3_redirect_type', $type );
  176. // Verify that redirect status is allowed.
  177. if ( in_array( $status, array_keys( jj4t3_redirect_statuses() ) ) ) {
  178. return $status;
  179. }
  180. return 301;
  181. }
  182. /**
  183. * Check if the current user is real human.
  184. *
  185. * This function is used to check the current
  186. * visitor is bot or real human based on the
  187. * browser.
  188. * If it is a bot, browser variables may not
  189. * be there.
  190. * DO NOT relay on this function for serious actions
  191. * as it may be wrong in some cases.
  192. *
  193. * @global bool $is_gecko
  194. * @global bool $is_opera
  195. * @global bool $is_safari
  196. * @global bool $is_chrome
  197. * @global bool $is_IE
  198. * @global bool $is_edge
  199. * @global bool $is_NS4
  200. * @global bool $is_lynx
  201. *
  202. * @since 3.0.0
  203. * @access private
  204. *
  205. * @return boolean
  206. */
  207. function jj4t3_is_human() {
  208. // If mobile OS is found it real user.
  209. if ( wp_is_mobile() ) {
  210. /**
  211. * Filter to modify human vs bot checking.
  212. *
  213. * If you want to add additional conditions to the human checking
  214. * function, use this filter and return a boolean.
  215. * For whatever value you return it will consider it's boolean only.
  216. *
  217. * @param boolean is real human.
  218. * @param string device type.
  219. *
  220. * @since 3.0.0
  221. */
  222. return (bool) apply_filters( 'jj4t3_is_human', true, 'mobile' );
  223. }
  224. // WordPress global variables for browsers.
  225. global $is_gecko, $is_opera, $is_safari, $is_chrome, $is_IE, $is_edge, $is_NS4, $is_lynx;
  226. $human = ( $is_gecko || $is_opera || $is_safari || $is_chrome || $is_IE || $is_edge || $is_NS4 || $is_lynx );
  227. /**
  228. * This filter is documented above.
  229. */
  230. return (bool) apply_filters( 'jj4t3_is_human', $human, 'desktop' );
  231. }
  232. /**
  233. * Set allowed status codes to redirect.
  234. *
  235. * Currently we are using only 3 status codes.
  236. * You can modify this to use more status using the filter.
  237. * Registering filter - "jj4t3_redirect_statuses".
  238. *
  239. * @link https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html HTTP status codes.
  240. * @since 3.0.0
  241. * @access private
  242. *
  243. * @return array Allowed HTTP status codes.
  244. */
  245. function jj4t3_redirect_statuses() {
  246. $statuses = array(
  247. 301 => __( '301 Redirect (SEO)', '404-to-301' ),
  248. 302 => __( '302 Redirect', '404-to-301' ),
  249. 307 => __( '307 Redirect', '404-to-301' ),
  250. );
  251. /**
  252. * Filter for allowed status codes.
  253. *
  254. * If you want to add additional HTTP status codes
  255. * for redirect, please use this filter and add to
  256. * the statuses array.
  257. * DO NOT remove default values (301, 302 and 307) from
  258. * the array.
  259. *
  260. * @since 3.0.0
  261. */
  262. return (array) apply_filters( 'jj4t3_redirect_statuses', $statuses );
  263. }
  264. /**
  265. * Available columns in error logs table.
  266. *
  267. * This columns are being used few times. Use this to avoid
  268. * unwanted names.
  269. * Registering filter - "jj4t3_redirect_statuses".
  270. *
  271. * @since 3.0.0
  272. * @access private
  273. *
  274. * @return array Allowed HTTP status codes.
  275. */
  276. function jj4t3_log_columns() {
  277. $columns = array(
  278. 'date' => __( 'Date', '404-to-301' ),
  279. 'url' => __( '404 Path', '404-to-301' ),
  280. 'ref' => __( 'From', '404-to-301' ),
  281. 'ip' => __( 'IP Address', '404-to-301' ),
  282. 'ua' => __( 'User Agent', '404-to-301' ),
  283. 'redirect' => __( 'Redirect', '404-to-301' ),
  284. );
  285. /**
  286. * Filter for available columns.
  287. *
  288. * These are the availble column names in 404
  289. * error logs.
  290. * Registering filter - "jj4t3_log_columns".
  291. *
  292. * @param array columns name and slug.
  293. *
  294. * @since 3.0.0
  295. */
  296. return (array) apply_filters( 'jj4t3_log_columns', $columns );
  297. }
  298. /**
  299. * Retrive value from $_REQUEST.
  300. *
  301. * Helper function to retrive data from $_REQUEST
  302. * We can use this function to get values from request
  303. * and get a default value if the current key does not exist
  304. * or empty.
  305. * Output will be trimmed.
  306. *
  307. * @param string $key Key to get from request.
  308. * @param mixed $default Default value.
  309. *
  310. * @since 3.0.0
  311. * @access public
  312. *
  313. * @return array|string
  314. */
  315. function jj4t3_from_request( $key = '', $default = '' ) {
  316. // Return default value if key is not given.
  317. if ( empty( $key ) || ! is_string( $key ) ) {
  318. return $default;
  319. }
  320. // Return default value if key not set.
  321. if ( ! isset( $_REQUEST[ $key ] ) ) {
  322. return $default;
  323. }
  324. // Trim output.
  325. if ( is_string( $_REQUEST[ $key ] ) ) {
  326. return trim( $_REQUEST[ $key ] );
  327. } elseif ( is_array( $_REQUEST[ $key ] ) ) {
  328. return array_map( 'trim', $_REQUEST[ $key ] );
  329. }
  330. return $default;
  331. }