true, 'trigger_status' => 'processing', 'cod_gateways' => array( 'cod' ), 'product_id' => 3, 'package_type' => 'PARCEL', 'default_weight_kg' => 1.0, 'shipment_service_ids' => array(), 'unresolved_district' => 'send', 'delivered_to_completed' => false, 'debug_log' => false, ); } /** * All settings, with defaults for anything not saved yet. * * @return array */ public static function all() { $saved = get_option( self::OPTION, array() ); return array_merge( self::defaults(), is_array( $saved ) ? $saved : array() ); } /** * One setting. * * @param string $key Setting name. * @return mixed */ public static function get( $key ) { $all = self::all(); return isset( $all[ $key ] ) ? $all[ $key ] : null; } /** * Whether the merchant has saved the settings at least once. * * @return bool */ public static function is_saved() { return false !== get_option( self::OPTION, false ); } /** * Saves already-sanitized settings. * * @param array $values Output of sanitize(). * @return void */ public static function save( array $values ) { update_option( self::OPTION, $values, false ); } /** * Validates submitted settings. Unknown or invalid values fall back to defaults. * * @param array $input Submitted values (already unslashed). * @param string[] $statuses Allowed order statuses, without the "wc-" prefix. * @param string[] $gateways Allowed payment gateway IDs. * @return array */ public static function sanitize( array $input, array $statuses, array $gateways ) { $defaults = self::defaults(); $status = isset( $input['trigger_status'] ) ? (string) $input['trigger_status'] : ''; $type = isset( $input['package_type'] ) ? (string) $input['package_type'] : ''; $weight = isset( $input['default_weight_kg'] ) && is_numeric( $input['default_weight_kg'] ) ? round( (float) $input['default_weight_kg'], 3 ) : 0.0; $prod = isset( $input['product_id'] ) && is_numeric( $input['product_id'] ) ? (int) $input['product_id'] : 0; return array( 'auto_create' => ! empty( $input['auto_create'] ), 'trigger_status' => in_array( $status, $statuses, true ) ? $status : $defaults['trigger_status'], 'cod_gateways' => array_values( array_intersect( self::string_list( $input, 'cod_gateways' ), $gateways ) ), 'product_id' => $prod > 0 ? $prod : $defaults['product_id'], 'package_type' => in_array( $type, self::PACKAGE_TYPES, true ) ? $type : $defaults['package_type'], 'default_weight_kg' => $weight > 0 && $weight <= 1000 ? $weight : $defaults['default_weight_kg'], 'shipment_service_ids' => array_values( array_unique( array_filter( array_map( 'intval', self::string_list( $input, 'shipment_service_ids' ) ), array( __CLASS__, 'is_positive' ) ) ) ), 'unresolved_district' => isset( $input['unresolved_district'] ) && 'hold' === $input['unresolved_district'] ? 'hold' : 'send', 'delivered_to_completed' => ! empty( $input['delivered_to_completed'] ), 'debug_log' => ! empty( $input['debug_log'] ), ); } /** * A submitted list as strings, ignoring non-scalar entries. * * @param array $input Submitted values. * @param string $key Field name. * @return string[] */ private static function string_list( array $input, $key ) { if ( ! isset( $input[ $key ] ) || ! is_array( $input[ $key ] ) ) { return array(); } return array_map( 'strval', array_filter( $input[ $key ], 'is_scalar' ) ); } /** * Filter callback: positive integers only. * * @param int $value Value. * @return bool */ private static function is_positive( $value ) { return $value > 0; } }