Brak opisu

ActionScheduler_WPCommentCleaner.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Class ActionScheduler_WPCommentCleaner
  4. *
  5. * @since 3.0.0
  6. */
  7. class ActionScheduler_WPCommentCleaner {
  8. /**
  9. * Post migration hook used to cleanup the WP comment table.
  10. *
  11. * @var string
  12. */
  13. protected static $cleanup_hook = 'action_scheduler/cleanup_wp_comment_logs';
  14. /**
  15. * An instance of the ActionScheduler_wpCommentLogger class to interact with the comments table.
  16. *
  17. * This instance should only be used as an interface. It should not be initialized.
  18. *
  19. * @var ActionScheduler_wpCommentLogger
  20. */
  21. protected static $wp_comment_logger = null;
  22. /**
  23. * The key used to store the cached value of whether there are logs in the WP comment table.
  24. *
  25. * @var string
  26. */
  27. protected static $has_logs_option_key = 'as_has_wp_comment_logs';
  28. /**
  29. * Initialize the class and attach callbacks.
  30. */
  31. public static function init() {
  32. if ( empty( self::$wp_comment_logger ) ) {
  33. self::$wp_comment_logger = new ActionScheduler_wpCommentLogger();
  34. }
  35. add_action( self::$cleanup_hook, array( __CLASS__, 'delete_all_action_comments' ) );
  36. // While there are orphaned logs left in the comments table, we need to attach the callbacks which filter comment counts.
  37. add_action( 'pre_get_comments', array( self::$wp_comment_logger, 'filter_comment_queries' ), 10, 1 );
  38. add_action( 'wp_count_comments', array( self::$wp_comment_logger, 'filter_comment_count' ), 20, 2 ); // run after WC_Comments::wp_count_comments() to make sure we exclude order notes and action logs
  39. add_action( 'comment_feed_where', array( self::$wp_comment_logger, 'filter_comment_feed' ), 10, 2 );
  40. // Action Scheduler may be displayed as a Tools screen or WooCommerce > Status administration screen
  41. add_action( 'load-tools_page_action-scheduler', array( __CLASS__, 'register_admin_notice' ) );
  42. add_action( 'load-woocommerce_page_wc-status', array( __CLASS__, 'register_admin_notice' ) );
  43. }
  44. /**
  45. * Determines if there are log entries in the wp comments table.
  46. *
  47. * Uses the flag set on migration completion set by @see self::maybe_schedule_cleanup().
  48. *
  49. * @return boolean Whether there are scheduled action comments in the comments table.
  50. */
  51. public static function has_logs() {
  52. return 'yes' === get_option( self::$has_logs_option_key );
  53. }
  54. /**
  55. * Schedules the WP Post comment table cleanup to run in 6 months if it's not already scheduled.
  56. * Attached to the migration complete hook 'action_scheduler/migration_complete'.
  57. */
  58. public static function maybe_schedule_cleanup() {
  59. if ( (bool) get_comments( array( 'type' => ActionScheduler_wpCommentLogger::TYPE, 'number' => 1, 'fields' => 'ids' ) ) ) {
  60. update_option( self::$has_logs_option_key, 'yes' );
  61. if ( ! as_next_scheduled_action( self::$cleanup_hook ) ) {
  62. as_schedule_single_action( gmdate( 'U' ) + ( 6 * MONTH_IN_SECONDS ), self::$cleanup_hook );
  63. }
  64. }
  65. }
  66. /**
  67. * Delete all action comments from the WP Comments table.
  68. */
  69. public static function delete_all_action_comments() {
  70. global $wpdb;
  71. $wpdb->delete( $wpdb->comments, array( 'comment_type' => ActionScheduler_wpCommentLogger::TYPE, 'comment_agent' => ActionScheduler_wpCommentLogger::AGENT ) );
  72. delete_option( self::$has_logs_option_key );
  73. }
  74. /**
  75. * Registers admin notices about the orphaned action logs.
  76. */
  77. public static function register_admin_notice() {
  78. add_action( 'admin_notices', array( __CLASS__, 'print_admin_notice' ) );
  79. }
  80. /**
  81. * Prints details about the orphaned action logs and includes information on where to learn more.
  82. */
  83. public static function print_admin_notice() {
  84. $next_cleanup_message = '';
  85. $next_scheduled_cleanup_hook = as_next_scheduled_action( self::$cleanup_hook );
  86. if ( $next_scheduled_cleanup_hook ) {
  87. /* translators: %s: date interval */
  88. $next_cleanup_message = sprintf( __( 'This data will be deleted in %s.', 'woocommerce' ), human_time_diff( gmdate( 'U' ), $next_scheduled_cleanup_hook ) );
  89. }
  90. $notice = sprintf(
  91. /* translators: 1: next cleanup message 2: github issue URL */
  92. __( 'Action Scheduler has migrated data to custom tables; however, orphaned log entries exist in the WordPress Comments table. %1$s <a href="%2$s">Learn more &raquo;</a>', 'woocommerce' ),
  93. $next_cleanup_message,
  94. 'https://github.com/woocommerce/action-scheduler/issues/368'
  95. );
  96. echo '<div class="notice notice-warning"><p>' . wp_kses_post( $notice ) . '</p></div>';
  97. }
  98. }