WP_CLI
1 year ago
abstracts
1 year ago
actions
1 year ago
data-stores
1 year ago
migration
1 year ago
schedules
1 year ago
schema
1 year 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
1 year ago
ActionScheduler_DateTime.php
1 year ago
ActionScheduler_Exception.php
4 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
2 years ago
ActionScheduler_QueueCleaner.php
1 year ago
ActionScheduler_QueueRunner.php
1 year ago
ActionScheduler_SystemInformation.php
1 year ago
ActionScheduler_Versions.php
1 year ago
ActionScheduler_WPCommentCleaner.php
1 year ago
ActionScheduler_wcSystemStatus.php
4 years ago
index.php
3 years ago
ActionScheduler_WPCommentCleaner.php
66 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | class ActionScheduler_WPCommentCleaner { |
| 4 | protected static $cleanup_hook = 'action_scheduler/cleanup_wp_comment_logs'; |
| 5 | protected static $wp_comment_logger = null; |
| 6 | protected static $has_logs_option_key = 'as_has_wp_comment_logs'; |
| 7 | public static function init() { |
| 8 | if ( empty( self::$wp_comment_logger ) ) { |
| 9 | self::$wp_comment_logger = new ActionScheduler_wpCommentLogger(); |
| 10 | } |
| 11 | add_action( self::$cleanup_hook, array( __CLASS__, 'delete_all_action_comments' ) ); |
| 12 | // While there are orphaned logs left in the comments table, we need to attach the callbacks which filter comment counts. |
| 13 | add_action( 'pre_get_comments', array( self::$wp_comment_logger, 'filter_comment_queries' ), 10, 1 ); |
| 14 | 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. |
| 15 | add_action( 'comment_feed_where', array( self::$wp_comment_logger, 'filter_comment_feed' ), 10, 2 ); |
| 16 | // Action Scheduler may be displayed as a Tools screen or WooCommerce > Status administration screen. |
| 17 | add_action( 'load-tools_page_action-scheduler', array( __CLASS__, 'register_admin_notice' ) ); |
| 18 | add_action( 'load-woocommerce_page_wc-status', array( __CLASS__, 'register_admin_notice' ) ); |
| 19 | } |
| 20 | public static function has_logs() { |
| 21 | return 'yes' === get_option( self::$has_logs_option_key ); |
| 22 | } |
| 23 | public static function maybe_schedule_cleanup() { |
| 24 | $has_logs = 'no'; |
| 25 | $args = array( |
| 26 | 'type' => ActionScheduler_wpCommentLogger::TYPE, |
| 27 | 'number' => 1, |
| 28 | 'fields' => 'ids', |
| 29 | ); |
| 30 | if ( (bool) get_comments( $args ) ) { |
| 31 | $has_logs = 'yes'; |
| 32 | if ( ! as_next_scheduled_action( self::$cleanup_hook ) ) { |
| 33 | as_schedule_single_action( gmdate( 'U' ) + ( 6 * MONTH_IN_SECONDS ), self::$cleanup_hook ); |
| 34 | } |
| 35 | } |
| 36 | update_option( self::$has_logs_option_key, $has_logs, true ); |
| 37 | } |
| 38 | public static function delete_all_action_comments() { |
| 39 | global $wpdb; |
| 40 | $wpdb->delete( |
| 41 | $wpdb->comments, |
| 42 | array( |
| 43 | 'comment_type' => ActionScheduler_wpCommentLogger::TYPE, |
| 44 | 'comment_agent' => ActionScheduler_wpCommentLogger::AGENT, |
| 45 | ) |
| 46 | ); |
| 47 | update_option( self::$has_logs_option_key, 'no', true ); |
| 48 | } |
| 49 | public static function register_admin_notice() { |
| 50 | add_action( 'admin_notices', array( __CLASS__, 'print_admin_notice' ) ); |
| 51 | } |
| 52 | public static function print_admin_notice() { |
| 53 | $next_cleanup_message = ''; |
| 54 | $next_scheduled_cleanup_hook = as_next_scheduled_action( self::$cleanup_hook ); |
| 55 | if ( $next_scheduled_cleanup_hook ) { |
| 56 | $next_cleanup_message = sprintf( __( 'This data will be deleted in %s.', 'action-scheduler' ), human_time_diff( gmdate( 'U' ), $next_scheduled_cleanup_hook ) ); |
| 57 | } |
| 58 | $notice = sprintf( |
| 59 | __( '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' ), |
| 60 | $next_cleanup_message, |
| 61 | 'https://github.com/woocommerce/action-scheduler/issues/368' |
| 62 | ); |
| 63 | echo '<div class="notice notice-warning"><p>' . wp_kses_post( $notice ) . '</p></div>'; |
| 64 | } |
| 65 | } |
| 66 |