": "" }, * "identity": "email", * "identity_type": "email" * } * } * * `identity` and `identity_type` are the LaunchList form's identity field when the route was saved, * used only when the forms list is not cached. */ class Routes { /** The option. */ const OPTION = 'launchlist_routes'; /** * The key for one source form. * * @param string $source Adapter slug. * @param string $form_id The form's ID in its plugin. * @return string */ public static function key( $source, $form_id ) { return $source . ':' . $form_id; } /** * Every route, each in the shape above. * * @return array, identity: string, identity_type: string}> */ public static function all() { $stored = get_option( self::OPTION, array() ); $routes = array(); if ( ! is_array( $stored ) ) { return $routes; } foreach ( $stored as $key => $route ) { if ( ! is_string( $key ) || ! is_array( $route ) ) { continue; } $route = self::normalize( $route ); if ( null !== $route ) { $routes[ $key ] = $route; } } return $routes; } /** * One route. * * @param string $key `"{source}:{form_id}"`. * @return array{form: string, enabled: bool, map: array, identity: string, identity_type: string}|null */ public static function get( $key ) { $routes = self::all(); return isset( $routes[ $key ] ) ? $routes[ $key ] : null; } /** * Replace every route. * * @param array $routes Routes keyed `"{source}:{form_id}"`. * @return void */ public static function save( array $routes ) { $clean = array(); foreach ( $routes as $key => $route ) { $route = is_array( $route ) ? self::normalize( $route ) : null; if ( is_string( $key ) && null !== $route ) { $clean[ $key ] = $route; } } update_option( self::OPTION, $clean, false ); } /** * Check one route's shape. * * @param array $route The stored route. * @return array{form: string, enabled: bool, map: array, identity: string, identity_type: string}|null */ private static function normalize( array $route ) { $form = isset( $route['form'] ) && is_string( $route['form'] ) ? $route['form'] : ''; if ( ! preg_match( Forms::FORM_ID, $form ) ) { return null; } $map = array(); if ( isset( $route['map'] ) && is_array( $route['map'] ) ) { foreach ( $route['map'] as $field_key => $source_name ) { // PHP turns a numeric string key into an int; both are field keys. if ( ( is_string( $field_key ) || is_int( $field_key ) ) && '' !== (string) $field_key && ( is_string( $source_name ) || is_int( $source_name ) ) ) { $map[ (string) $field_key ] = (string) $source_name; } } } return array( 'form' => $form, 'enabled' => ! empty( $route['enabled'] ), 'map' => $map, 'identity' => isset( $route['identity'] ) && is_string( $route['identity'] ) ? $route['identity'] : '', 'identity_type' => isset( $route['identity_type'] ) && is_string( $route['identity_type'] ) ? $route['identity_type'] : '', ); } }