$q ) { $norm = self::validate_question( $code, $q ); if ( $norm ) { $questions[ $norm['code'] ] = $norm; } } $blueprints = array(); foreach ( (array) ( isset( $raw['blueprints'] ) ? $raw['blueprints'] : array() ) as $id => $bp ) { $norm = self::normalise_blueprint( $id, $bp, $questions ); if ( $norm ) { $blueprints[ $norm['id'] ] = $norm; } } return apply_filters( 'aieo_survey_blueprints', array( 'questions' => $questions, 'blueprints' => $blueprints ) ); } public static function flush() { self::$data = null; } public static function validate_question( $code, $q ) { $code = self::code( $code ); if ( '' === $code || ! is_array( $q ) ) { return null; } $out = array( 'code' => $code, 'version' => isset( $q['version'] ) ? (string) $q['version'] : '1', 'label' => isset( $q['label'] ) ? (string) $q['label'] : $code, 'help' => isset( $q['help'] ) ? (string) $q['help'] : '', 'answers' => self::axis( $q, 'answers', self::AXIS_ANSWERS, 'choice' ), 'cardinality' => self::axis( $q, 'cardinality', self::AXIS_CARDINALITY, 'one' ), 'ordered' => self::axis( $q, 'ordered', self::AXIS_ORDERED, 'no' ), 'scored' => self::axis( $q, 'scored', self::AXIS_SCORED, 'none' ), 'length' => self::axis( $q, 'length', self::AXIS_LENGTH, 'short' ), 'widget' => self::axis( $q, 'widget', self::WIDGETS, '' ), 'required' => ! empty( $q['required'] ), 'retired' => ! empty( $q['retired'] ), 'choices' => array(), 'min' => isset( $q['min'] ) ? (int) $q['min'] : 1, 'max' => isset( $q['max'] ) ? (int) $q['max'] : 5, 'low' => isset( $q['low'] ) ? (string) $q['low'] : '', 'high' => isset( $q['high'] ) ? (string) $q['high'] : '', 'answer_key' => isset( $q['answer_key'] ) ? self::code( $q['answer_key'] ) : '', 'depends' => self::depends( isset( $q['depends'] ) ? $q['depends'] : null ), ); foreach ( (array) ( isset( $q['choices'] ) ? $q['choices'] : array() ) as $ck => $cl ) { $ck = self::code( $ck ); if ( '' === $ck ) { continue; } $out['choices'][ $ck ] = (string) $cl; } if ( 'choice' === $out['answers'] && 'scale' !== $out['scored'] && empty( $out['choices'] ) ) { return null; } if ( 'scale' === $out['scored'] && $out['max'] <= $out['min'] ) { return null; } if ( 'correct' === $out['scored'] && ! isset( $out['choices'][ $out['answer_key'] ] ) ) { return null; } return $out; } public static function depends_met( $q, $answers ) { $dep = isset( $q['depends'] ) ? $q['depends'] : null; if ( ! is_array( $dep ) || empty( $dep['code'] ) || empty( $dep['values'] ) ) { return true; } $code = (string) $dep['code']; if ( ! is_array( $answers ) || ! array_key_exists( $code, $answers ) ) { return false; } $given = $answers[ $code ]; foreach ( (array) $given as $v ) { if ( in_array( self::code( $v ), (array) $dep['values'], true ) ) { return true; } } return false; } private static function depends( $d ) { if ( ! is_array( $d ) ) { return null; } $code = isset( $d['code'] ) ? self::code( $d['code'] ) : ''; if ( '' === $code ) { return null; } $raw = isset( $d['values'] ) ? $d['values'] : ( isset( $d['value'] ) ? array( $d['value'] ) : array() ); $values = array(); foreach ( (array) $raw as $v ) { $v = self::code( $v ); if ( '' !== $v ) { $values[ $v ] = true; } } if ( ! $values ) { return null; } return array( 'code' => $code, 'values' => array_keys( $values ) ); } private static function normalise_blueprint( $id, $bp, $questions ) { $id = self::code( $id ); if ( '' === $id || ! is_array( $bp ) ) { return null; } $sections = array(); foreach ( (array) ( isset( $bp['sections'] ) ? $bp['sections'] : array() ) as $s ) { if ( ! is_array( $s ) ) { continue; } $codes = array(); foreach ( (array) ( isset( $s['questions'] ) ? $s['questions'] : array() ) as $c ) { $c = self::code( $c ); if ( '' !== $c && isset( $questions[ $c ] ) && empty( $questions[ $c ]['retired'] ) ) { $codes[] = $c; } } if ( ! $codes ) { continue; } $sections[] = array( 'key' => self::code( isset( $s['key'] ) ? $s['key'] : '' ), 'label' => isset( $s['label'] ) ? (string) $s['label'] : '', 'questions' => $codes, ); } if ( ! $sections ) { return null; } return array( 'id' => $id, 'version' => isset( $bp['version'] ) ? (string) $bp['version'] : '1', 'label' => isset( $bp['label'] ) ? (string) $bp['label'] : $id, 'title' => isset( $bp['title'] ) ? (string) $bp['title'] : '', 'description' => isset( $bp['description'] ) ? (string) $bp['description'] : '', 'intro' => isset( $bp['intro'] ) ? (string) $bp['intro'] : '', 'thanks' => isset( $bp['thanks'] ) ? (string) $bp['thanks'] : '', 'sections' => $sections, ); } private static function axis( $q, $key, $allowed, $default ) { $v = isset( $q[ $key ] ) ? strtolower( trim( (string) $q[ $key ] ) ) : ''; return in_array( $v, $allowed, true ) ? $v : $default; } public static function code( $code ) { return sanitize_key( (string) $code ); } public static function get( $id ) { $all = self::all(); $id = self::code( $id ); if ( ! isset( $all['blueprints'][ $id ] ) ) { return null; } $bp = $all['blueprints'][ $id ]; foreach ( $bp['sections'] as $i => $section ) { $resolved = array(); foreach ( $section['questions'] as $code ) { if ( isset( $all['questions'][ $code ] ) ) { $resolved[] = $all['questions'][ $code ]; } } $bp['sections'][ $i ]['resolved'] = $resolved; } return $bp; } public static function question( $code ) { $all = self::all(); $code = self::code( $code ); return isset( $all['questions'][ $code ] ) ? $all['questions'][ $code ] : null; } public static function question_codes( $id ) { $bp = self::get( $id ); if ( ! $bp ) { return array(); } $out = array(); foreach ( $bp['sections'] as $s ) { foreach ( $s['questions'] as $c ) { $out[] = $c; } } return $out; } public static function options() { $out = array(); foreach ( self::all()['blueprints'] as $id => $bp ) { $out[ $id ] = $bp['label']; } return $out; } public static function control_args( $q, $prefix = 'aieo_answer', $value = '' ) { $name = $prefix . '[' . $q['code'] . ']'; return array( 'answers' => $q['answers'], 'cardinality' => $q['cardinality'], 'ordered' => $q['ordered'], 'scored' => $q['scored'], 'length' => $q['length'], 'widget' => $q['widget'], 'id' => 'aieo-q-' . $q['code'], 'name' => $name, 'label' => $q['label'], 'help' => $q['help'], 'required' => $q['required'], 'options' => $q['choices'], 'min' => $q['min'], 'max' => $q['max'], 'low' => $q['low'], 'high' => $q['high'], 'value' => $value, ); } }