isset( $raw['questions'] ) && is_array( $raw['questions'] ) ? $raw['questions'] : array(), 'custom' => isset( $raw['custom'] ) && is_array( $raw['custom'] ) ? $raw['custom'] : array(), 'order' => isset( $raw['order'] ) && is_array( $raw['order'] ) ? $raw['order'] : array(), 'added' => isset( $raw['added'] ) && is_array( $raw['added'] ) ? $raw['added'] : array(), ); } private static function put( array $overlay ) { AIEO_Survey_Blueprint::flush(); return update_option( self::OPTION, $overlay, false ); } public static function register() { add_filter( 'aieo_survey_blueprints', array( __CLASS__, 'apply' ), 10, 1 ); } public static function apply( $data ) { $overlay = self::get(); if ( ! $overlay['questions'] && ! $overlay['custom'] && ! $overlay['order'] && ! $overlay['added'] ) { return $data; } $questions = isset( $data['questions'] ) ? (array) $data['questions'] : array(); $blueprints = isset( $data['blueprints'] ) ? (array) $data['blueprints'] : array(); foreach ( $overlay['custom'] as $code => $spec ) { $code = AIEO_Survey_Blueprint::code( $code ); if ( '' === $code || isset( $questions[ $code ] ) ) { continue; } $norm = self::validate_custom( $code, (array) $spec ); if ( $norm ) { $questions[ $code ] = $norm; } } foreach ( $overlay['questions'] as $code => $patch ) { $code = AIEO_Survey_Blueprint::code( $code ); if ( ! isset( $questions[ $code ] ) || ! is_array( $patch ) ) { continue; } if ( isset( $patch['label'] ) && '' !== trim( (string) $patch['label'] ) ) { $questions[ $code ]['label'] = (string) $patch['label']; } if ( isset( $patch['help'] ) ) { $questions[ $code ]['help'] = (string) $patch['help']; } if ( ! empty( $patch['hidden'] ) ) { $questions[ $code ]['retired'] = true; } if ( isset( $patch['version'] ) ) { $questions[ $code ]['version'] = (string) $patch['version']; } foreach ( (array) ( isset( $patch['choices'] ) ? $patch['choices'] : array() ) as $ck => $cl ) { $ck = AIEO_Survey_Blueprint::code( $ck ); if ( '' !== $ck && isset( $questions[ $code ]['choices'][ $ck ] ) ) { $questions[ $code ]['choices'][ $ck ] = (string) $cl; } } foreach ( (array) ( isset( $patch['added_choices'] ) ? $patch['added_choices'] : array() ) as $ck => $cl ) { $ck = AIEO_Survey_Blueprint::code( $ck ); if ( '' !== $ck ) { $questions[ $code ]['choices'][ $ck ] = (string) $cl; } } } foreach ( $blueprints as $bid => $bp ) { foreach ( $bp['sections'] as $i => $section ) { $key = $section['key']; foreach ( (array) ( isset( $overlay['added'][ $bid ][ $key ] ) ? $overlay['added'][ $bid ][ $key ] : array() ) as $code ) { $code = AIEO_Survey_Blueprint::code( $code ); if ( '' !== $code && isset( $questions[ $code ] ) && ! in_array( $code, $section['questions'], true ) ) { $section['questions'][] = $code; } } if ( ! empty( $overlay['order'][ $bid ][ $key ] ) ) { $wanted = array(); foreach ( (array) $overlay['order'][ $bid ][ $key ] as $code ) { $code = AIEO_Survey_Blueprint::code( $code ); if ( in_array( $code, $section['questions'], true ) ) { $wanted[] = $code; } } foreach ( $section['questions'] as $code ) { if ( ! in_array( $code, $wanted, true ) ) { $wanted[] = $code; } } $section['questions'] = $wanted; } $section['questions'] = array_values( array_filter( $section['questions'], static function ( $c ) use ( $questions ) { return isset( $questions[ $c ] ) && empty( $questions[ $c ]['retired'] ); } ) ); $blueprints[ $bid ]['sections'][ $i ] = $section; } $blueprints[ $bid ]['sections'] = array_values( array_filter( $blueprints[ $bid ]['sections'], static function ( $s ) { return ! empty( $s['questions'] ); } ) ); } return array( 'questions' => $questions, 'blueprints' => $blueprints ); } public static function relabel( $code, $label = '', array $choices = array(), $help = null ) { $code = AIEO_Survey_Blueprint::code( $code ); if ( '' === $code ) { return false; } $overlay = self::get(); $patch = isset( $overlay['questions'][ $code ] ) ? (array) $overlay['questions'][ $code ] : array(); if ( '' !== trim( (string) $label ) ) { $patch['label'] = (string) $label; } if ( null !== $help ) { $patch['help'] = (string) $help; } foreach ( $choices as $ck => $cl ) { $ck = AIEO_Survey_Blueprint::code( $ck ); if ( '' !== $ck ) { $patch['choices'][ $ck ] = (string) $cl; } } $overlay['questions'][ $code ] = $patch; return self::put( $overlay ); } public static function set_hidden( $code, $hidden = true ) { $code = AIEO_Survey_Blueprint::code( $code ); if ( '' === $code ) { return false; } $overlay = self::get(); $patch = isset( $overlay['questions'][ $code ] ) ? (array) $overlay['questions'][ $code ] : array(); if ( $hidden ) { $patch['hidden'] = true; } else { unset( $patch['hidden'] ); } $overlay['questions'][ $code ] = $patch; return self::put( $overlay ); } public static function reorder( $blueprint, $section, array $codes ) { $overlay = self::get(); $overlay['order'][ AIEO_Survey_Blueprint::code( $blueprint ) ][ AIEO_Survey_Blueprint::code( $section ) ] = array_values( array_filter( array_map( array( 'AIEO_Survey_Blueprint', 'code' ), $codes ) ) ); return self::put( $overlay ); } public static function add_question( $code, array $spec, $blueprint = '', $section = '' ) { $code = AIEO_Survey_Blueprint::code( $code ); if ( '' === $code ) { return ''; } if ( 0 !== strpos( $code, 'shop_' ) ) { $code = 'shop_' . $code; } $spec['code'] = $code; if ( ! self::validate_custom( $code, $spec ) ) { return ''; } $overlay = self::get(); $overlay['custom'][ $code ] = $spec; $blueprint = AIEO_Survey_Blueprint::code( $blueprint ); $section = AIEO_Survey_Blueprint::code( $section ); if ( '' !== $blueprint && '' !== $section ) { $existing = isset( $overlay['added'][ $blueprint ][ $section ] ) ? (array) $overlay['added'][ $blueprint ][ $section ] : array(); if ( ! in_array( $code, $existing, true ) ) { $existing[] = $code; } $overlay['added'][ $blueprint ][ $section ] = $existing; } return self::put( $overlay ) ? $code : ''; } public static function revise_choices( $code, array $added, $note = '' ) { $code = AIEO_Survey_Blueprint::code( $code ); $q = AIEO_Survey_Blueprint::question( $code ); if ( ! $q ) { return ''; } $clean = array(); foreach ( $added as $ck => $cl ) { $ck = AIEO_Survey_Blueprint::code( $ck ); if ( '' !== $ck ) { $clean[ $ck ] = (string) $cl; } } if ( ! $clean ) { return ''; } $overlay = self::get(); $patch = isset( $overlay['questions'][ $code ] ) ? (array) $overlay['questions'][ $code ] : array(); $version = (int) ( isset( $patch['version'] ) ? $patch['version'] : $q['version'] ) + 1; $patch['added_choices'] = array_merge( isset( $patch['added_choices'] ) ? (array) $patch['added_choices'] : array(), $clean ); $patch['version'] = (string) $version; $patch['revisions'][] = array( 'version' => (string) $version, 'added' => array_keys( $clean ), 'note' => (string) $note, 'at' => current_time( 'mysql', true ), ); $overlay['questions'][ $code ] = $patch; return self::put( $overlay ) ? (string) $version : ''; } private static function validate_custom( $code, array $spec ) { if ( '' === trim( (string) ( isset( $spec['label'] ) ? $spec['label'] : '' ) ) ) { return null; } return AIEO_Survey_Blueprint::validate_question( $code, $spec ); } }