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