CronTasks.php
86 lines
| 1 | <?php // phpcs:ignore |
| 2 | |
| 3 | namespace WP2FA\Cron; |
| 4 | |
| 5 | use WP2FA\Admin\SettingsPage; |
| 6 | use WP2FA\Admin\User; |
| 7 | use WP2FA\Utils\UserUtils; |
| 8 | use \WP2FA\WP2FA as WP2FA; |
| 9 | |
| 10 | /** |
| 11 | * Class for handling our crons. |
| 12 | */ |
| 13 | class CronTasks { |
| 14 | |
| 15 | /** |
| 16 | * Constructor. |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | add_action( 'wp_2fa_check_grace_period_status', array( $this, 'wp_2fa_check_users_grace_period_status' ) ); |
| 20 | add_action( 'init', array( $this, 'register_check_users_grace_period_status_event' ) ); |
| 21 | } |
| 22 | |
| 23 | // This function will run once the 'wp_2fa_check_users_grace_period_status' is called |
| 24 | public function wp_2fa_check_users_grace_period_status() { |
| 25 | // check if the cronjob is enabled in plugin settings |
| 26 | if ( empty( WP2FA::get_wp2fa_setting( 'enable_grace_cron' ) ) ) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | // grab all users |
| 31 | $users_args = array( |
| 32 | 'fields' => array( 'ID' ), |
| 33 | ); |
| 34 | |
| 35 | if ( WP2FA::is_this_multisite() ) { |
| 36 | $users_args['blog_id'] = 0; |
| 37 | } |
| 38 | |
| 39 | $users = UserUtils::get_all_user_ids( 'query', $users_args ); |
| 40 | if ( ! is_array( $users ) ) { |
| 41 | $users = explode( ',', $users ); |
| 42 | } |
| 43 | |
| 44 | if ( empty( $users ) ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | foreach ( $users as $index => $user_id ) { |
| 49 | // creating the user object will update their meta fields to reflect latest plugin settings |
| 50 | $wp2faUser = User::get_instance( $user_id ); |
| 51 | |
| 52 | // run a check to see if user account needs to be locked (this happens only here and during the login) |
| 53 | $wp2faUser->lock_user_account_if_needed(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Function which will register the event |
| 58 | function register_check_users_grace_period_status_event() { |
| 59 | // Make sure this event hasn't been scheduled |
| 60 | if ( ! wp_next_scheduled( 'wp_2fa_check_grace_period_status' ) && ! empty( WP2FA::get_wp2fa_setting( 'enable_grace_cron' ) ) ) { |
| 61 | // Schedule the event |
| 62 | wp_schedule_event( time(), 'hourly', 'wp_2fa_check_grace_period_status' ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Send email to setup authentication |
| 68 | */ |
| 69 | public static function send_expired_grace_email( $user_id ) { |
| 70 | // Bail if the user has not enabled this email. |
| 71 | if ( 'enable_account_locked_email' !== WP2FA::get_wp2fa_email_templates( 'send_account_locked_email' ) ) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | // Grab user data |
| 76 | $user = get_userdata( $user_id ); |
| 77 | // Grab user email |
| 78 | $email = $user->user_email; |
| 79 | |
| 80 | $subject = wp_strip_all_tags( WP2FA::replace_email_strings( WP2FA::get_wp2fa_email_templates( 'user_account_locked_email_subject' ), $user_id ) ); |
| 81 | $message = wpautop( WP2FA::replace_email_strings( WP2FA::get_wp2fa_email_templates( 'user_account_locked_email_body' ), $user_id ) ); |
| 82 | |
| 83 | SettingsPage::send_email( $email, $subject, $message ); |
| 84 | } |
| 85 | } |
| 86 |