'yes', 'min_seconds' => 3, 'block_disposable' => 'yes', 'custom_domains' => '', 'block_message' => '', // Empty means the translated default; resolved at display time so no text is translated before init. 'logging' => 'yes', 'cleanup_days' => 0, ); $settings = get_option( 'ordersieve_settings', array() ); if ( ! is_array( $settings ) ) { $settings = array(); } $settings = wp_parse_args( $settings, $defaults ); // WooCommerce stores number fields unvalidated; keep them within the ranges the settings screen offers. $settings['min_seconds'] = min( 60, max( 0, (int) $settings['min_seconds'] ) ); $settings['cleanup_days'] = min( 365, max( 0, (int) $settings['cleanup_days'] ) ); return $settings; } /** * Boot the plugin once all plugins are loaded. * * @return void */ function ordersieve_init() { if ( ! class_exists( 'WooCommerce' ) ) { add_action( 'admin_notices', 'ordersieve_wc_missing_notice' ); return; } if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, ORDERSIEVE_MIN_WC_VERSION, '<' ) ) { add_action( 'admin_notices', 'ordersieve_wc_outdated_notice' ); return; } require_once ORDERSIEVE_DIR . 'includes/class-ordersieve-guard.php'; require_once ORDERSIEVE_DIR . 'includes/class-ordersieve-settings.php'; require_once ORDERSIEVE_DIR . 'includes/class-ordersieve-cleanup.php'; require_once ORDERSIEVE_DIR . 'includes/class-ordersieve-logs-page.php'; OrderSieve_Guard::init(); OrderSieve_Settings::init(); OrderSieve_Cleanup::init(); OrderSieve_Logs_Page::init(); add_action( 'admin_init', 'ordersieve_maybe_schedule_cleanup' ); add_action( 'admin_init', 'ordersieve_add_privacy_policy_content' ); } add_action( 'plugins_loaded', 'ordersieve_init' ); /** * Whether WooCommerce's Action Scheduler is loaded and ready to accept jobs. * * @return bool */ function ordersieve_action_scheduler_ready() { return function_exists( 'as_schedule_recurring_action' ) && function_exists( 'as_has_scheduled_action' ) && class_exists( 'ActionScheduler' ) && ActionScheduler::is_initialized(); } /** * Keep the daily cleanup job in sync with the setting: schedule it when * cleanup is enabled and missing, unschedule it when cleanup is disabled. * * Uses WooCommerce's Action Scheduler (visible under WooCommerce > Status > * Scheduled Actions) rather than WP-Cron. Runs on admin_init, so it also * covers plugins updated in place without an activation hook. * * @return void */ function ordersieve_maybe_schedule_cleanup() { if ( ! ordersieve_action_scheduler_ready() ) { return; } $settings = ordersieve_get_settings(); $enabled = (int) $settings['cleanup_days'] > 0; $scheduled = as_has_scheduled_action( ORDERSIEVE_CLEANUP_HOOK, array(), ORDERSIEVE_AS_GROUP ); if ( $enabled && ! $scheduled ) { as_schedule_recurring_action( time() + HOUR_IN_SECONDS, DAY_IN_SECONDS, ORDERSIEVE_CLEANUP_HOOK, array(), ORDERSIEVE_AS_GROUP, true ); } elseif ( ! $enabled && $scheduled ) { as_unschedule_all_actions( ORDERSIEVE_CLEANUP_HOOK, array(), ORDERSIEVE_AS_GROUP ); } } /** * Remove every scheduled cleanup job. * * @return void */ function ordersieve_unschedule_cleanup() { if ( function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( ORDERSIEVE_CLEANUP_HOOK, array(), ORDERSIEVE_AS_GROUP ); } } /** * Suggest privacy policy text, since blocked attempts are logged with email and IP. * * @return void */ function ordersieve_add_privacy_policy_content() { if ( ! function_exists( 'wp_add_privacy_policy_content' ) ) { return; } $content = '

' . esc_html__( 'OrderSieve blocks suspected automated checkout attempts. Suggested text for your privacy policy:', 'ordersieve' ) . '

' . '

' . esc_html__( 'When a checkout attempt is blocked as suspected automated or abusive, we record the reason, the email address entered and the IP address in our store logs. This information is used only to protect the store from abuse, is kept according to our log retention settings, and is not shared with third parties.', 'ordersieve' ) . '

'; wp_add_privacy_policy_content( __( 'OrderSieve', 'ordersieve' ), $content ); } /** * Whether a dependency notice should be shown on the current screen. * * Notices are limited to the Plugins screen and to users who can manage * plugins, so the plugin never adds noise to the rest of the dashboard. * * @return bool */ function ordersieve_should_show_dependency_notice() { if ( ! current_user_can( 'activate_plugins' ) ) { return false; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; return $screen && in_array( $screen->id, array( 'plugins', 'plugins-network' ), true ); } /** * Admin notice shown on the Plugins screen when WooCommerce is inactive. * * @return void */ function ordersieve_wc_missing_notice() { if ( ! ordersieve_should_show_dependency_notice() ) { return; } echo '

'; esc_html_e( 'OrderSieve requires WooCommerce to be installed and active.', 'ordersieve' ); echo '

'; } /** * Admin notice shown on the Plugins screen when WooCommerce is too old. * * @return void */ function ordersieve_wc_outdated_notice() { if ( ! ordersieve_should_show_dependency_notice() ) { return; } echo '

'; printf( /* translators: %s: minimum WooCommerce version */ esc_html__( 'OrderSieve requires WooCommerce %s or newer. Please update WooCommerce.', 'ordersieve' ), esc_html( ORDERSIEVE_MIN_WC_VERSION ) ); echo '

'; } /** * Schedule the daily cleanup job on activation when cleanup is enabled. * If Action Scheduler is not ready yet, the admin_init sync schedules it. * * @return void */ function ordersieve_activate() { ordersieve_maybe_schedule_cleanup(); } register_activation_hook( __FILE__, 'ordersieve_activate' ); /** * Remove the scheduled cleanup job on deactivation. * * @return void */ function ordersieve_deactivate() { ordersieve_unschedule_cleanup(); } register_deactivation_hook( __FILE__, 'ordersieve_deactivate' );