array( __CLASS__, 'sanitize' ) ) );
} );
add_filter( 'option_page_capability_acpw_group', static function () { return 'manage_options'; } );
add_action( 'admin_post_acpw_test', array( __CLASS__, 'test' ) );
add_action( 'admin_post_acpw_retry', array( __CLASS__, 'retry' ) );
add_action( 'admin_post_acpw_clear_history', array( __CLASS__, 'clear_history' ) );
add_action( 'update_option_acpw_settings', array( 'ACPW_Store', 'trim_history' ), 10, 0 );
add_action( 'admin_notices', array( __CLASS__, 'notices' ) );
add_filter( 'plugin_action_links_' . plugin_basename( ACPW_FILE ), static function ( $links ) {
array_unshift( $links, '' . esc_html__( 'Settings', 'abandoned-cart-webhooks-for-woocommerce' ) . '' );
return $links;
} );
}
public static function sanitize( $input ) {
$old = ACPW_Store::settings();
if ( ACPW_Compat::previous_active() || get_option( 'acpw_db_version' ) !== ACPW_VERSION ) {
add_settings_error( 'acpw_settings', 'upgrade', __( 'Deactivate the previous build and complete storage migration before saving settings.', 'abandoned-cart-webhooks-for-woocommerce' ) );
return $old;
}
$input = is_array( $input ) ? $input : array();
$value = $old;
// Each checkbox has an explicit zero-value form field. A partial or unrelated
// options update must never enable an opt-in feature by accident.
foreach ( array( 'enabled', 'recovery_enabled', 'delete_data', 'require_email' ) as $flag ) {
if ( array_key_exists( $flag, $input ) ) {
$value[ $flag ] = ( '1' === (string) $input[ $flag ] ) ? 1 : 0;
}
}
// Recovery is a separate opt-in, not a consequence of enabling capture.
// A previously disabled installation requires positive confirmation of
// this sensitive feature before it can transition to enabled.
if ( empty( $old['recovery_enabled'] ) && ! empty( $value['recovery_enabled'] ) ) {
if ( ! isset( $input['recovery_confirm'] ) || '1' !== (string) $input['recovery_confirm'] ) {
$value['recovery_enabled'] = 0;
add_settings_error( 'acpw_settings', 'recovery_confirmation', __( 'Cart recovery was kept off. To enable it, check both the recovery option and its separate confirmation, then save again.', 'abandoned-cart-webhooks-for-woocommerce' ), 'error' );
}
}
$url = isset( $input['webhook_url'] ) && is_string( $input['webhook_url'] ) ? esc_url_raw( trim( $input['webhook_url'] ) ) : '';
if ( $url && ( 'https' !== wp_parse_url( $url, PHP_URL_SCHEME ) || ! wp_http_validate_url( $url ) ) ) {
add_settings_error( 'acpw_settings', 'url', __( 'Enter a public HTTPS webhook URL. The previous URL has been retained.', 'abandoned-cart-webhooks-for-woocommerce' ) );
} elseif ( ! empty( $input['clear_url'] ) ) {
$value['webhook_url'] = '';
} elseif ( $url ) {
$value['webhook_url'] = $url;
}
foreach ( array( 'cutoff_minutes' => array( 1, 43200 ), 'repeat_hours' => array( 0, 8760 ), 'link_days' => array( 1, 90 ), 'retention_days' => array( 2, 365 ), 'history_limit' => array( 0, 100000 ), 'email_cooldown_hours' => array( 1, 8760 ), 'hourly_event_limit' => array( 0, 1000000 ) ) as $key => list( $min, $max ) ) {
$number = isset( $input[ $key ] ) && is_scalar( $input[ $key ] ) ? (int) $input[ $key ] : (int) $old[ $key ];
$value[ $key ] = max( $min, min( $max, $number ) );
}
$value['item_metadata'] = isset( $input['item_metadata'] ) && in_array( $input['item_metadata'], array( 'full', 'minimal' ), true ) ? $input['item_metadata'] : $old['item_metadata'];
$value['retention_days'] = max( $value['retention_days'], $value['link_days'] + 1 );
if ( ! empty( $input['clear_secret'] ) ) {
$value['signing_secret'] = '';
} elseif ( ! empty( $input['signing_secret'] ) && is_string( $input['signing_secret'] ) ) {
$value['signing_secret'] = substr( trim( $input['signing_secret'] ), 0, 254 );
}
if ( $value['enabled'] && ! $value['webhook_url'] ) {
$value['enabled'] = 0;
add_settings_error( 'acpw_settings', 'missing_url', 'Set a webhook URL before enabling delivery.' );
}
if ( $value['enabled'] !== $old['enabled'] ) {
add_settings_error( 'acpw_settings', 'tracking_state', $value['enabled'] ? __( 'Tracking and eligible webhook delivery enabled.', 'abandoned-cart-webhooks-for-woocommerce' ) : __( 'Tracking and eligible webhook delivery disabled.', 'abandoned-cart-webhooks-for-woocommerce' ), 'updated' );
}
if ( $value['recovery_enabled'] !== $old['recovery_enabled'] ) {
add_settings_error( 'acpw_settings', 'recovery_state', $value['recovery_enabled'] ? __( 'Saved-cart recovery explicitly enabled.', 'abandoned-cart-webhooks-for-woocommerce' ) : __( 'Saved-cart recovery disabled.', 'abandoned-cart-webhooks-for-woocommerce' ), 'updated' );
}
if ( ! empty( $old['require_email'] ) && empty( $value['require_email'] ) ) {
// Carts excluded under the former email requirement can now join the normal queue.
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned cart state; requalification is intentionally performed only when this setting changes.
$wpdb->update( ACPW_Store::carts(), array( 'status' => 'active' ), array( 'status' => 'no_email' ) );
}
delete_option( 'acpw_upgrade_notice' );
return $value;
}
public static function notices() {
if ( ! current_user_can( 'manage_options' ) && ! current_user_can( 'activate_plugins' ) ) {
return;
}
if ( get_option( 'acpw_install_error' ) ) {
echo '
' . esc_html__( 'Cart Webhooks storage initialization did not complete. Confirm database table permissions and retry activation or reload the plugin. Tracking and deliveries remain paused until installation succeeds.', 'abandoned-cart-webhooks-for-woocommerce' ) . '
' . esc_html__( 'Previous settings and stored carts were migrated. Tracking is paused and cart recovery is off. Review settings and explicitly enable the features you want.', 'abandoned-cart-webhooks-for-woocommerce' ) . '
' . esc_html__( 'Tracking and delivery are paused while the previous build is active. Deactivate the previous build before using this plugin.', 'abandoned-cart-webhooks-for-woocommerce' ) . '
';
}
}
public static function page() {
global $wpdb;
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( ACPW_Compat::previous_active() || get_option( 'acpw_db_version' ) !== ACPW_VERSION ) {
echo '
' . esc_html__( 'Deactivate the previous build and complete storage migration before using these controls.', 'abandoned-cart-webhooks-for-woocommerce' ) . '
' . esc_html__( 'Complete cart data to your webhook. Customer role labels come from this store’s registered WordPress roles; shoppers without an account are identified as guests.', 'abandoned-cart-webhooks-for-woocommerce' ) . '
' . esc_html__( 'WP-Cron is disabled. A working hosting/server cron must run WordPress scheduled tasks.', 'abandoned-cart-webhooks-for-woocommerce' ) . '
' . esc_html__( 'Read-only view of stored carts. Active carts have not yet been queued; queued and failed carts have delivery events. Eligibility times describe inactivity, not email qualification. Scheduled tasks, email validity and delivery limits may affect actual sending.', 'abandoned-cart-webhooks-for-woocommerce' ) . '
' . esc_html__( 'Uses up to two current catalogue products. Recovery links are included only when cart recovery is enabled. Every recipient email field uses', 'abandoned-cart-webhooks-for-woocommerce' ) . ' ' . '' . esc_html( $email ) . '' . esc_html__( '. No customer cart is copied and your current cart is not changed.', 'abandoned-cart-webhooks-for-woocommerce' ) . '
' . esc_html__( 'Works while tracking is paused. Save your webhook URL first. Configure your receiver to accept a sample request, then send this test.', 'abandoned-cart-webhooks-for-woocommerce' ) . '