6, 'timeout' => 10, 'batch_size' => 25, 'retention_days' => 30, 'circuit_threshold' => 20, 'alert_email' => '', 'allow_local_urls' => 0, ); } /** * All settings merged with defaults. * * @return array */ public static function all() { return wp_parse_args( (array) get_option( self::OPTION, array() ), self::defaults() ); } /** * Get one setting. * * @param string $key Setting key. * @return mixed */ public static function get( $key ) { $settings = self::all(); return isset( $settings[ $key ] ) ? $settings[ $key ] : null; } /** * Email address for circuit breaker alerts. * * @return string */ public static function alert_email() { $email = (string) self::get( 'alert_email' ); return is_email( $email ) ? $email : (string) get_option( 'admin_email' ); } /** * Whether URLs on localhost, private networks or custom ports are allowed. * * @return bool */ public static function allow_local_urls() { /** * Filter whether endpoints may point at local or private network addresses. * * @param bool $allow Current setting. */ return (bool) apply_filters( 'hookbird_allow_local_urls', (bool) self::get( 'allow_local_urls' ) ); } /** * Sanitize settings on save. * * @param mixed $input Raw input. * @return array */ public static function sanitize( $input ) { $input = is_array( $input ) ? $input : array(); $out = self::defaults(); $ranges = array( 'max_attempts' => array( 1, 10 ), 'timeout' => array( 2, 30 ), 'batch_size' => array( 1, 100 ), 'retention_days' => array( 1, 365 ), 'circuit_threshold' => array( 0, 1000 ), ); foreach ( $ranges as $key => $range ) { if ( isset( $input[ $key ] ) && '' !== $input[ $key ] ) { $out[ $key ] = min( $range[1], max( $range[0], absint( $input[ $key ] ) ) ); } } $email = isset( $input['alert_email'] ) ? sanitize_email( (string) $input['alert_email'] ) : ''; $out['alert_email'] = is_email( $email ) ? $email : ''; $out['allow_local_urls'] = empty( $input['allow_local_urls'] ) ? 0 : 1; return $out; } /** * Retry delays in seconds, indexed by attempts already made minus one. * * @return int[] */ public static function backoff_schedule() { /** * Filter the retry schedule in seconds. * * @param int[] $schedule Delay after the 1st, 2nd, 3rd failed attempt and so on. */ $schedule = (array) apply_filters( 'hookbird_backoff_schedule', array( 30, 120, 600, 1800, 7200, 21600, 43200, 86400, 86400 ) ); return array_values( array_map( 'absint', $schedule ) ); } }