$def ) { if ( empty( $all[ $id ] ) || ! is_array( $all[ $id ] ) || empty( $def['columns'] ) || ! is_array( $def['columns'] ) ) { continue; } $set = $all[ $id ]; foreach ( $def['columns'] as $i => $col ) { $key = isset( $col['key'] ) ? (string) $col['key'] : ''; if ( '' === $key || empty( $set[ $key ] ) || ! is_array( $set[ $key ] ) ) { continue; } $s = $set[ $key ]; if ( array_key_exists( 'show', $s ) ) { $raw[ $id ]['columns'][ $i ]['hidden'] = ! $s['show']; } if ( array_key_exists( 'filter', $s ) ) { if ( $s['filter'] ) { if ( empty( $col['filter'] ) && self::can_filter( $col ) ) { $raw[ $id ]['columns'][ $i ]['filter'] = 'select'; } } else { unset( $raw[ $id ]['columns'][ $i ]['filter'] ); } } } } return $raw; } public static function can_filter( $col ) { $type = isset( $col['type'] ) ? (string) $col['type'] : 'text'; return ! in_array( $type, self::NOT_FILTERABLE, true ); } public static function set( $report, $column, $field, $value ) { $report = preg_replace( '/[^a-z0-9_]/', '', (string) $report ); $column = preg_replace( '/[^a-zA-Z0-9_]/', '', (string) $column ); if ( '' === $report || '' === $column || ! in_array( $field, array( 'show', 'filter' ), true ) ) { return new WP_Error( 'aieo_columns_bad', __( 'That is not a column setting.', 'ai-eshop-optimizer' ) ); } if ( ! class_exists( 'AIEO_Report_Registry' ) ) { return new WP_Error( 'aieo_columns_bad', __( 'The reports are not loaded.', 'ai-eshop-optimizer' ) ); } $def = AIEO_Report_Registry::get( $report ); if ( ! $def ) { return new WP_Error( 'aieo_columns_bad', __( 'No such table.', 'ai-eshop-optimizer' ) ); } $found = null; foreach ( (array) $def->columns() as $col ) { if ( isset( $col['key'] ) && (string) $col['key'] === $column ) { $found = $col; break; } } if ( null === $found ) { return new WP_Error( 'aieo_columns_bad', __( 'No such column.', 'ai-eshop-optimizer' ) ); } if ( 'filter' === $field && $value && ! self::can_filter( $found ) ) { return new WP_Error( 'aieo_columns_bad', __( 'That column presents a value rather than holding one, so it cannot be a filter.', 'ai-eshop-optimizer' ) ); } $before = self::all(); $all = $before; if ( ! isset( $all[ $report ] ) || ! is_array( $all[ $report ] ) ) { $all[ $report ] = array(); } if ( ! isset( $all[ $report ][ $column ] ) || ! is_array( $all[ $report ][ $column ] ) ) { $all[ $report ][ $column ] = array(); } $all[ $report ][ $column ][ $field ] = (bool) $value; update_option( self::OPTION, $all, false ); AIEO_Report_Registry::flush(); $rebuilt = AIEO_Report_Registry::get( $report ); if ( ! $rebuilt || ! $rebuilt->is_valid() ) { update_option( self::OPTION, $before, false ); AIEO_Report_Registry::flush(); $why = $rebuilt ? implode( ' ', (array) $rebuilt->errors() ) : __( 'the definition no longer validates', 'ai-eshop-optimizer' ); return new WP_Error( 'aieo_columns_invalid', sprintf( __( 'Not saved: %s', 'ai-eshop-optimizer' ), $why ) ); } return $all[ $report ]; } public static function matrix( $def ) { $out = array(); foreach ( (array) $def->columns() as $col ) { $key = isset( $col['key'] ) ? (string) $col['key'] : ''; if ( '' === $key ) { continue; } $type = isset( $col['type'] ) ? (string) $col['type'] : 'text'; $out[] = array( 'key' => $key, 'label' => isset( $col['label'] ) && '' !== (string) $col['label'] ? (string) $col['label'] : $key, 'type' => $type, 'show' => empty( $col['hidden'] ), 'filter' => ! empty( $col['filter'] ), 'kind' => ! empty( $col['filter'] ) ? (string) $col['filter'] : '', 'filterable' => self::can_filter( $col ), ); } return $out; } }