I have created a plugin that sends an email with all orders placed that day by X o’clock to email Y. I followed the tutorial here: https://scotch.io/tutorials/how-to-build-a-wordpress-plugin-part-1
On each options update, it removes all scheduled events and add new ones. For some reason that I don’t understand, the email is sending whenever someone logged in as an admin hits the home page. Once I change and save the settings, it appears to stop. Here is what I think is the relevant code:
public function options_update() {
register_setting($this->plugin_name, $this->plugin_name, array($this, 'validate'));
wp_clear_scheduled_hook('send_woocommerce_daily_order_summary_email');
$options = get_option($this->plugin_name);
$timestamp = strtotime($options['send_time'] . ":00:00 " . get_option('timezone_string'));
wp_schedule_event($timestamp, 'daily', 'send_woocommerce_daily_order_summary_email');
}
And where the action is added:`
private function define_admin_hooks() {
$plugin_admin = new Woocommerce_Daily_Order_Summary_Email_Admin( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
// Add menu item
$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_plugin_admin_menu' );
// Add Settings link to the plugin
$plugin_basename = plugin_basename( plugin_dir_path( __DIR__ ) . $this->plugin_name . '.php' );
$this->loader->add_filter( 'plugin_action_links_' . $plugin_basename, $plugin_admin, 'add_action_links' );
// Save/Update our plugin options
$this->loader->add_action('admin_init', $plugin_admin, 'options_update');
}
Can anyone help me understand why this could be happening?