From 0bf3fd82a55a18455eaf0ccbfeed521df76e9b3d Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Tue, 23 Oct 2018 18:07:55 -0500 Subject: [PATCH] Moved balance notification to a transient based check. Working on sending email --- classes/LBRY_Admin.php | 61 +++++++++++++---------------------- classes/LBRY_Admin_Notice.php | 1 + classes/lbrypress.php | 20 ++++++------ 3 files changed, 34 insertions(+), 48 deletions(-) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index eb2a475..41432a5 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -16,32 +16,8 @@ class LBRY_Admin { add_action('admin_menu', array($this, 'create_options_page')); add_action('admin_init', array($this, 'page_init')); + add_action('admin_init', array($this, 'wallet_balance_warning')); add_action('admin_post_lbry_add_channel', array($this, 'add_channel')); - - // add_filter('cron_schedules', 'lbry_add_cron_interval'); - - // function lbry_add_cron_interval($schedules) - // { - // $schedules['five_seconds'] = array( - // 'interval' => 5, - // 'display' => esc_html__('Every Five Seconds'), - // ); - // - // return $schedules; - // } - - // if (! wp_next_scheduled('lbry_wallet_balance_hook')) { - // error_log('scheduling'); - // wp_schedule_event(time(), 'five_seconds', 'lbry_wallet_balance_hook'); - // } - - add_action('lbry_wallet_balance_hook', array($this, 'wallet_balance_cron')); - - - - error_log(print_r(_get_cron_array(), true)); - // do_action('lbry_wallet_balance_hook', array(LBRY())); - error_log('next: ' . wp_next_scheduled('lbry_wallet_balance_hook')); } /** @@ -230,20 +206,29 @@ class LBRY_Admin exit(); } - public static function wallet_balance_cron($lbry) + /** + * Checks at most once an hour to see if the wallet balance is too low + */ + // COMBAK: Check user permissions possibly, figure out proper timing Interval, Email warning + // TODO: make sure it doesn't set a bunch of warnings + public static function wallet_balance_warning() { - $balance = 2; // LBRY()->daemon->wallet_balance(); - // - if ($balance < 20000) { - $lbry->notice->set_notice('error', 'Your account balance is low, please add LBC to your account to continue publishing to the LBRY Network', true); - } - error_log('Balance: ' . $balance); - } + if (!get_transient('lbry_wallet_warning')) { + $balance = LBRY()->daemon->wallet_balance(); + if ($balance < LBRY_MIN_BALANCE) { + if (!get_transient('lbry_wallet_warning_email')) { + $email = get_option('admin_email'); + $subject = 'Your LBRYPress Wallet Balance is Low!'; + $message = 'You LBRY Wallet for your wordpress installation at ' . get_option('site_url') . 'is running very low. In order to keep publishing to the LBRY network, please add some LBC to your account.'; + wp_mail($email, $subject, $message); + set_transient('lbry_wallet_warning_email', true, 30); + // TODO: Fix outgoing email + } - public function wallet_balance_deactivate() - { - $timestamp = wp_next_scheduled('lbry_wallet_balance_hook'); - wp_unschedule_event($timestamp, 'lbry_wallet_balance_hook'); - error_log('Disabled: ' . $timestamp); + LBRY()->notice->set_notice('error', 'Your account balance is low, please add LBC to your account to continue publishing to the LBRY Network', true); + } + + set_transient('lbry_wallet_warning', true, 30); + } } } diff --git a/classes/LBRY_Admin_Notice.php b/classes/LBRY_Admin_Notice.php index 594a00b..44b7a5b 100644 --- a/classes/LBRY_Admin_Notice.php +++ b/classes/LBRY_Admin_Notice.php @@ -29,6 +29,7 @@ class LBRY_Admin_Notice /** * Sets transients for admin errors */ + // TODO: Make sure we only set one transient at a time public function set_notice($status = 'error', $message = 'Something went wrong', $is_dismissible = false) { $notice = array( diff --git a/classes/lbrypress.php b/classes/lbrypress.php index 4c83af1..b35751f 100644 --- a/classes/lbrypress.php +++ b/classes/lbrypress.php @@ -108,7 +108,7 @@ class LBRYPress 'license2' => 'License 2', 'license3' => 'License 3' )); - $this->define('LBRY_MIN_BALANCE', 20); + $this->define('LBRY_MIN_BALANCE', 2000); } /** @@ -131,15 +131,6 @@ class LBRYPress { $this->daemon = new LBRY_Daemon(); $this->speech = new LBRY_Speech(); - } - - /** - * Set up all hooks and actions necessary for the plugin to run - */ - private function init_hooks() - { - register_activation_hook(LBRY_PLUGIN_FILE, array($this, 'activate')); - register_deactivation_hook(LBRY_PLUGIN_FILE, array($this, 'deactivate')); // Admin request if (is_admin()) { @@ -151,6 +142,15 @@ class LBRYPress } } + /** + * Set up all hooks and actions necessary for the plugin to run + */ + private function init_hooks() + { + register_activation_hook(LBRY_PLUGIN_FILE, array($this, 'activate')); + register_deactivation_hook(LBRY_PLUGIN_FILE, array($this, 'deactivate')); + } + /** * Run during plugin activation */