WP_CLI
11 months ago
abstracts
11 months ago
actions
1 year ago
data-stores
11 months ago
migration
1 year ago
schedules
1 year ago
schema
11 months ago
ActionScheduler_ActionClaim.php
1 year ago
ActionScheduler_ActionFactory.php
1 year ago
ActionScheduler_AdminView.php
1 year ago
ActionScheduler_AsyncRequest_QueueRunner.php
1 year ago
ActionScheduler_Compatibility.php
1 year ago
ActionScheduler_DataController.php
11 months ago
ActionScheduler_DateTime.php
1 year ago
ActionScheduler_Exception.php
3 years ago
ActionScheduler_FatalErrorMonitor.php
1 year ago
ActionScheduler_InvalidActionException.php
1 year ago
ActionScheduler_ListTable.php
1 year ago
ActionScheduler_LogEntry.php
1 year ago
ActionScheduler_NullLogEntry.php
1 year ago
ActionScheduler_OptionLock.php
1 year ago
ActionScheduler_QueueCleaner.php
1 year ago
ActionScheduler_QueueRunner.php
1 year ago
ActionScheduler_RecurringActionScheduler.php
11 months ago
ActionScheduler_SystemInformation.php
1 year ago
ActionScheduler_Versions.php
1 year ago
ActionScheduler_WPCommentCleaner.php
1 year ago
ActionScheduler_wcSystemStatus.php
11 months ago
ActionScheduler_WPCommentCleaner.php
134 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 | $has_logs = 'no'; |
| 70 | |
| 71 | $args = array( |
| 72 | 'type' => ActionScheduler_wpCommentLogger::TYPE, |
| 73 | 'number' => 1, |
| 74 | 'fields' => 'ids', |
| 75 | ); |
| 76 | |
| 77 | if ( (bool) get_comments( $args ) ) { |
| 78 | $has_logs = 'yes'; |
| 79 | |
| 80 | if ( ! as_next_scheduled_action( self::$cleanup_hook ) ) { |
| 81 | as_schedule_single_action( gmdate( 'U' ) + ( 6 * MONTH_IN_SECONDS ), self::$cleanup_hook ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | update_option( self::$has_logs_option_key, $has_logs, true ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Delete all action comments from the WP Comments table. |
| 90 | */ |
| 91 | public static function delete_all_action_comments() { |
| 92 | global $wpdb; |
| 93 | |
| 94 | $wpdb->delete( |
| 95 | $wpdb->comments, |
| 96 | array( |
| 97 | 'comment_type' => ActionScheduler_wpCommentLogger::TYPE, |
| 98 | 'comment_agent' => ActionScheduler_wpCommentLogger::AGENT, |
| 99 | ) |
| 100 | ); |
| 101 | |
| 102 | update_option( self::$has_logs_option_key, 'no', true ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Registers admin notices about the orphaned action logs. |
| 107 | */ |
| 108 | public static function register_admin_notice() { |
| 109 | add_action( 'admin_notices', array( __CLASS__, 'print_admin_notice' ) ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Prints details about the orphaned action logs and includes information on where to learn more. |
| 114 | */ |
| 115 | public static function print_admin_notice() { |
| 116 | $next_cleanup_message = ''; |
| 117 | $next_scheduled_cleanup_hook = as_next_scheduled_action( self::$cleanup_hook ); |
| 118 | |
| 119 | if ( $next_scheduled_cleanup_hook ) { |
| 120 | /* translators: %s: date interval */ |
| 121 | $next_cleanup_message = sprintf( __( 'This data will be deleted in %s.', 'woocommerce' ), human_time_diff( gmdate( 'U' ), $next_scheduled_cleanup_hook ) ); |
| 122 | } |
| 123 | |
| 124 | $notice = sprintf( |
| 125 | /* translators: 1: next cleanup message 2: github issue URL */ |
| 126 | __( '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>', 'woocommerce' ), |
| 127 | $next_cleanup_message, |
| 128 | 'https://github.com/woocommerce/action-scheduler/issues/368' |
| 129 | ); |
| 130 | |
| 131 | echo '<div class="notice notice-warning"><p>' . wp_kses_post( $notice ) . '</p></div>'; |
| 132 | } |
| 133 | } |
| 134 |