From c528a1003760f2c181e103771801bec8915eb820 Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Mon, 15 Oct 2018 13:20:29 -0500 Subject: [PATCH 1/5] Working on WP-Cron implementation --- classes/LBRY_Admin.php | 42 ++++++++++++++++++++++++++++++++++++++++++ classes/lbrypress.php | 3 +++ 2 files changed, 45 insertions(+) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 8ca99b0..0b910ae 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -17,6 +17,31 @@ class LBRY_Admin add_action('admin_menu', array($this, 'create_options_page')); add_action('admin_init', array($this, 'page_init')); 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')); } /** @@ -204,4 +229,21 @@ class LBRY_Admin wp_safe_redirect($redirect_url); exit(); } + + public static function wallet_balance_cron($lbry) + { + $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); + } + + 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); + } } diff --git a/classes/lbrypress.php b/classes/lbrypress.php index c228699..4c83af1 100644 --- a/classes/lbrypress.php +++ b/classes/lbrypress.php @@ -108,6 +108,7 @@ class LBRYPress 'license2' => 'License 2', 'license3' => 'License 3' )); + $this->define('LBRY_MIN_BALANCE', 20); } /** @@ -191,6 +192,8 @@ class LBRYPress */ public function deactivate() { + // Deactivate Wallet Balance cron job + $this->admin->wallet_balance_deactivate(); error_log('Deactivated'); } From baae43e31892b1c1b155a2e8fbb39bd4c457c15a Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Mon, 15 Oct 2018 13:20:46 -0500 Subject: [PATCH 2/5] Minor tweak --- classes/LBRY_Admin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 0b910ae..eb2a475 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -18,7 +18,7 @@ class LBRY_Admin add_action('admin_init', array($this, 'page_init')); add_action('admin_post_lbry_add_channel', array($this, 'add_channel')); - add_filter('cron_schedules', 'lbry_add_cron_interval'); + // add_filter('cron_schedules', 'lbry_add_cron_interval'); // function lbry_add_cron_interval($schedules) // { @@ -40,7 +40,7 @@ class LBRY_Admin error_log(print_r(_get_cron_array(), true)); - do_action('lbry_wallet_balance_hook', array(LBRY())); + // do_action('lbry_wallet_balance_hook', array(LBRY())); error_log('next: ' . wp_next_scheduled('lbry_wallet_balance_hook')); } From 0bf3fd82a55a18455eaf0ccbfeed521df76e9b3d Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Tue, 23 Oct 2018 18:07:55 -0500 Subject: [PATCH 3/5] 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 */ From 3eb7d43d74175eeba0d540c413796307e56fac4c Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Wed, 24 Oct 2018 11:30:47 -0500 Subject: [PATCH 4/5] Send email warning once a day. Check and post admin warning once an hour --- classes/LBRY_Admin.php | 9 ++++----- classes/LBRY_Admin_Notice.php | 2 +- classes/lbrypress.php | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 41432a5..ae1c040 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -209,8 +209,7 @@ class LBRY_Admin /** * 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 + // COMBAK: Check user permissions possibly public static function wallet_balance_warning() { if (!get_transient('lbry_wallet_warning')) { @@ -219,16 +218,16 @@ class LBRY_Admin 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.'; + $message = "You LBRY Wallet for your wordpress installation at " . site_url() . " is running very low.\r\n\r\nYou currently have " . $balance . ' LBC left in your wallet. 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); + set_transient('lbry_wallet_warning_email', true, DAY_IN_SECONDS); // TODO: Fix outgoing email } 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); + set_transient('lbry_wallet_warning', true, 2 * HOUR_IN_SECONDS); } } } diff --git a/classes/LBRY_Admin_Notice.php b/classes/LBRY_Admin_Notice.php index 44b7a5b..bde0438 100644 --- a/classes/LBRY_Admin_Notice.php +++ b/classes/LBRY_Admin_Notice.php @@ -29,7 +29,7 @@ class LBRY_Admin_Notice /** * Sets transients for admin errors */ - // TODO: Make sure we only set one transient at a time + // TODO: Make sure we only set one transient at a time per error 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 b35751f..6558c7d 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', 2000); + $this->define('LBRY_MIN_BALANCE', 20); } /** From 76f4cfe4ced78533bdb5c4210464bf77bc1849da Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Wed, 24 Oct 2018 11:34:44 -0500 Subject: [PATCH 5/5] Only send email, as per plugin spec --- classes/LBRY_Admin.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index ae1c040..9c14f3f 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -212,22 +212,20 @@ class LBRY_Admin // COMBAK: Check user permissions possibly public static function wallet_balance_warning() { - if (!get_transient('lbry_wallet_warning')) { + // See if we've checked in the past two hours + if (!get_transient('lbry_wallet_check')) { $balance = LBRY()->daemon->wallet_balance(); if ($balance < LBRY_MIN_BALANCE) { + // If LBRY Balance is low, send email, but only once per day 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 " . site_url() . " is running very low.\r\n\r\nYou currently have " . $balance . ' LBC left in your wallet. 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, DAY_IN_SECONDS); - // TODO: Fix outgoing email } - - 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, 2 * HOUR_IN_SECONDS); + set_transient('lbry_wallet_check', true, 2 * HOUR_IN_SECONDS); } } }