d = $this->validate( (array) $raw ); } public function is_valid() { return ! $this->errors; } public function errors() { return $this->errors; } public function to_array() { return $this->d; } public function get( $key, $default = null ) { return array_key_exists( $key, $this->d ) ? $this->d[ $key ] : $default; } public function row_key() { return (string) $this->get( 'row_key', '' ); } public function actions() { return (array) $this->get( 'actions', array() ); } public function bulk_actions() { $out = (array) $this->get( 'bulk_actions', array() ); foreach ( $this->actions() as $action ) { if ( empty( $action['bulk'] ) ) { continue; } $out[] = array( 'id' => (string) $action['id'], 'label' => (string) $action['label'], 'capability' => (string) $action['capability'], 'destructive' => false, 'confirm' => (string) $action['bulk_confirm'], 'scope' => (string) $action['bulk'], 'param' => array(), ); } foreach ( $this->columns() as $col ) { if ( empty( $col['settable'] ) || empty( $col['editable'] ) ) { continue; } $key = (string) $col['key']; $filter = isset( $col['filter'] ) ? (string) $col['filter'] : ''; if ( in_array( $filter, array( 'select', 'multiselect' ), true ) ) { $param = array( 'type' => 'options', 'column' => $key, 'required' => true ); } elseif ( in_array( (string) $col['type'], array( 'integer', 'money', 'decimal', 'percent' ), true ) ) { $param = array( 'type' => 'number', 'column' => $key, 'required' => true ); } else { $param = array( 'type' => 'text', 'column' => $key, 'required' => true ); } $out[] = array( 'id' => 'set_' . strtolower( $key ), 'label' => '', 'capability' => '', 'destructive' => false, 'confirm' => '', 'scope' => 'selection', 'param' => $param, 'set_column' => $key, ); } return $out; } public function bulk_action( $id ) { foreach ( $this->bulk_actions() as $action ) { if ( (string) $action['id'] === (string) $id ) { return $action; } } return null; } public function id() { return (string) $this->get( 'id', '' ); } public function version() { return (string) $this->get( 'version', '1' ); } public function tier() { return (string) $this->get( 'tier', 'inherit' ); } public function scope() { return (string) $this->get( 'scope', 'local' ); } public function source() { return (string) $this->get( 'source', 'table' ); } public function columns() { return (array) $this->get( 'columns', array() ); } public function aggregate_source( $key ) { foreach ( $this->columns() as $col ) { if ( (string) $col['key'] === (string) $key && ! empty( $col['aggregate_of'] ) ) { return (string) $col['aggregate_of']; } } return (string) $key; } public function sortable_keys() { $out = array(); foreach ( $this->columns() as $col ) { if ( ! empty( $col['sortable'] ) ) { $out[] = (string) ( isset( $col['sort_key'] ) && '' !== $col['sort_key'] ? $col['sort_key'] : $col['key'] ); } } return array_values( array_unique( $out ) ); } public function sort_for_column( $key ) { foreach ( $this->columns() as $col ) { if ( (string) $col['key'] === (string) $key && ! empty( $col['sortable'] ) ) { return (string) ( isset( $col['sort_key'] ) && '' !== $col['sort_key'] ? $col['sort_key'] : $col['key'] ); } } return ''; } public function column_for_sort( $sort ) { $found = ''; foreach ( $this->columns() as $col ) { if ( empty( $col['sortable'] ) || ! empty( $col['hidden'] ) || $this->sort_for_column( (string) $col['key'] ) !== (string) $sort ) { continue; } if ( (string) $col['key'] === (string) $sort ) { return (string) $sort; } $found = '' !== $found ? $found : (string) $col['key']; } return '' !== $found ? $found : (string) $sort; } public function searchable_keys() { $out = array(); foreach ( $this->columns() as $col ) { if ( ! empty( $col['searchable'] ) ) { $out[] = (string) $col['key']; } } return $out; } public function filterable() { $out = array(); foreach ( $this->columns() as $col ) { if ( ! empty( $col['filter'] ) ) { $out[ (string) $col['key'] ] = (string) $col['filter']; } } return $out; } public function default_sort() { $s = $this->get( 'default_sort' ); if ( ! is_array( $s ) || ! isset( $s[0] ) ) { return null; } $dir = isset( $s[1] ) && 'desc' === strtolower( (string) $s[1] ) ? 'DESC' : 'ASC'; return array( (string) $s[0], $dir ); } private function validate( $raw ) { foreach ( array_keys( $raw ) as $key ) { if ( ! in_array( (string) $key, self::KEYS, true ) ) { $this->errors[] = sprintf( 'unknown key "%s"', (string) $key ); } } $d = array_merge( array( 'id' => '', 'label' => '', 'description' => '', 'group' => '', 'section' => '', 'icon' => '', 'tier' => 'inherit', 'scope' => 'local', 'source' => 'table', 'table' => '', 'sql' => '', 'rows' => null, 'base_where' => '', 'columns' => array(), 'params' => array(), 'aggregates' => array(), 'default_sort' => null, 'per_page' => 0, 'row_key' => '', 'row_type' => '', 'actions' => array(), 'bulk_actions' => array(), 'export' => array(), 'capabilities' => array(), 'empty_state' => '', 'version' => '1', 'registry_id' => '', 'cap' => 0, 'chart' => array(), 'menu_priority' => 0, 'figures' => array(), 'guidance' => array(), 'views' => array(), 'columns_shown' => array(), 'filter_bar' => array(), ), $raw ); if ( ! preg_match( '/^[a-z0-9_]+$/', (string) $d['id'] ) ) { $this->errors[] = 'id must be lowercase letters, digits and underscores'; } if ( '' === trim( (string) $d['label'] ) ) { $this->errors[] = 'no label'; } if ( '' === trim( (string) $d['empty_state'] ) ) { $this->errors[] = 'no empty_state; empty is a normal state and the definition must say what to show'; } $d['tier'] = in_array( (string) $d['tier'], array( 'free', 'pro', 'inherit' ), true ) ? (string) $d['tier'] : 'inherit'; $d['scope'] = in_array( (string) $d['scope'], array( 'local', 'saas', 'both' ), true ) ? (string) $d['scope'] : 'local'; $d['source'] = in_array( (string) $d['source'], array( 'table', 'sql', 'rows' ), true ) ? (string) $d['source'] : 'table'; switch ( $d['source'] ) { case 'table': if ( '' === trim( (string) $d['table'] ) ) { $this->errors[] = 'source is "table" but no table was named'; } break; case 'sql': if ( self::is_sql_builder( $d['sql'] ) ) { break; } if ( '' === trim( (string) $d['sql'] ) ) { $this->errors[] = 'source is "sql" but no SELECT was given'; } elseif ( ! self::looks_like_a_select( (string) $d['sql'] ) ) { $this->errors[] = 'sql must be a single SELECT (or WITH ... SELECT) with no trailing statement'; } break; case 'rows': if ( ! is_callable( $d['rows'] ) ) { $this->errors[] = 'source is "rows" but no callable was given'; } break; } $d['columns'] = $this->validate_columns( (array) $d['columns'] ); if ( ! $d['columns'] ) { $this->errors[] = 'no columns'; } $d['params'] = $this->validate_params( (array) $d['params'], $d['columns'] ); $d['aggregates'] = $this->validate_aggregates( (array) $d['aggregates'], $d['columns'] ); $this->validate_default_sort( $d ); $d['actions'] = $this->validate_actions( (array) $d['actions'] ); $d['bulk_actions'] = $this->validate_bulk_actions( (array) $d['bulk_actions'], $d ); $d['capabilities'] = $this->validate_capabilities( (array) $d['capabilities'] ); $this->validate_row_key( $d ); $this->validate_screen( $d ); $d['per_page'] = (int) $d['per_page']; $d['cap'] = max( 0, (int) $d['cap'] ); $d['version'] = (string) $d['version']; return $d; } private function validate_columns( $columns ) { $out = array(); $seen = array(); foreach ( $columns as $i => $col ) { if ( ! is_array( $col ) ) { $this->errors[] = sprintf( 'column %d is not an array', (int) $i ); continue; } foreach ( array_keys( $col ) as $key ) { if ( ! in_array( (string) $key, self::COLUMN_KEYS, true ) ) { $this->errors[] = sprintf( 'column %d: unknown key "%s"', (int) $i, (string) $key ); } } $key = isset( $col['key'] ) ? (string) $col['key'] : ''; if ( ! preg_match( '/^[a-zA-Z0-9_]+$/', $key ) ) { $this->errors[] = sprintf( 'column %d: key "%s" must be letters, digits and underscores', (int) $i, $key ); continue; } if ( isset( $seen[ $key ] ) ) { $this->errors[] = sprintf( 'duplicate column key "%s"', $key ); continue; } $seen[ $key ] = true; if ( isset( $col['filter'] ) && ! in_array( (string) $col['filter'], self::FILTERS, true ) ) { $this->errors[] = sprintf( 'column "%s": unknown filter kind "%s"', $key, (string) $col['filter'] ); } if ( isset( $col['aggregate'] ) && ! in_array( strtoupper( (string) $col['aggregate'] ), self::AGGREGATES, true ) ) { $this->errors[] = sprintf( 'column "%s": unknown aggregate "%s"', $key, (string) $col['aggregate'] ); } if ( isset( $col['select'] ) && ! self::is_safe_select_expression( (string) $col['select'] ) ) { $this->errors[] = sprintf( 'column "%s": select expression contains characters that cannot appear in one', $key ); } if ( ! empty( $col['settable'] ) ) { if ( empty( $col['editable'] ) ) { $this->errors[] = sprintf( 'column "%s": settable requires editable — the mass write rides the same save path as the cell', $key ); } if ( '' !== (string) ( isset( $col['select'] ) ? $col['select'] : '' ) ) { $this->errors[] = sprintf( 'column "%s": a computed column cannot be settable; there is no stored column behind the expression', $key ); } } $cell = class_exists( 'AIEO_Table_Columns' ) ? AIEO_Table_Columns::normalise( $col ) : $col; foreach ( array( 'select', 'searchable', 'filter', 'filter_match', 'hidden', 'aggregate', 'counts' ) as $extra ) { if ( isset( $col[ $extra ] ) ) { $cell[ $extra ] = $col[ $extra ]; } } $out[] = $cell; } return $out; } private function validate_params( $params, $columns ) { $out = array(); $keys = wp_list_pluck( (array) $columns, 'key' ); foreach ( $params as $key => $spec ) { $key = (string) $key; if ( ! preg_match( '/^[a-z0-9_]+$/', $key ) ) { $this->errors[] = sprintf( 'param "%s": name must be lowercase letters, digits and underscores', $key ); continue; } if ( in_array( $key, $keys, true ) ) { $this->errors[] = sprintf( 'param "%s" shares its name with a column; a scope parameter and a filter cannot be the same key', $key ); continue; } if ( ! is_array( $spec ) ) { $spec = array( 'default' => $spec ); } foreach ( array_keys( $spec ) as $k ) { if ( ! in_array( (string) $k, self::PARAM_KEYS, true ) ) { $this->errors[] = sprintf( 'param "%s": unknown key "%s"', $key, (string) $k ); } } $type = isset( $spec['type'] ) ? (string) $spec['type'] : 'text'; if ( ! in_array( $type, self::PARAM_TYPES, true ) ) { $this->errors[] = sprintf( 'param "%s": unknown type "%s"', $key, $type ); $type = 'text'; } $options = isset( $spec['options'] ) ? array_values( (array) $spec['options'] ) : array(); foreach ( $options as $opt ) { if ( ! is_scalar( $opt ) ) { $this->errors[] = sprintf( 'param "%s": every option must be a scalar', $key ); break; } } $default = isset( $spec['default'] ) ? $spec['default'] : ''; if ( $options && ! in_array( (string) $default, array_map( 'strval', $options ), true ) ) { $this->errors[] = sprintf( 'param "%s": the default is not one of its own options', $key ); } $out[ $key ] = array( 'default' => $default, 'type' => $type, 'options' => $options, 'label' => isset( $spec['label'] ) ? (string) $spec['label'] : '', ); } return $out; } public function apply_param_defaults( $params ) { $params = is_array( $params ) ? $params : array(); foreach ( (array) $this->get( 'params', array() ) as $key => $spec ) { $given = array_key_exists( $key, $params ) ? $params[ $key ] : null; if ( null === $given || is_array( $given ) || '' === (string) $given ) { $given = array_key_exists( $key, $params ) && ! is_array( $params[ $key ] ) ? $params[ $key ] : $spec['default']; } if ( $spec['options'] && ! in_array( (string) $given, array_map( 'strval', $spec['options'] ), true ) ) { $given = $spec['default']; } switch ( $spec['type'] ) { case 'int': $given = (int) $given; break; case 'number': $given = (float) $given; break; case 'bool': $given = ( '1' === (string) $given || 'true' === strtolower( (string) $given ) ) ? 1 : 0; break; default: $given = (string) $given; } $params[ $key ] = $given; } return $params; } private function validate_aggregates( $aggregates, $columns ) { $out = array(); $keys = wp_list_pluck( $columns, 'key' ); foreach ( $columns as $col ) { if ( ! empty( $col['aggregate'] ) ) { $out[ (string) $col['key'] ] = strtoupper( (string) $col['aggregate'] ); } if ( ! empty( $col['aggregate_of'] ) ) { if ( empty( $col['aggregate'] ) ) { $this->errors[] = sprintf( 'aggregate_of on "%s" without an aggregate', (string) $col['key'] ); } elseif ( ! in_array( (string) $col['aggregate_of'], $keys, true ) ) { $this->errors[] = sprintf( 'aggregate_of on "%s" names "%s", which is not a column', (string) $col['key'], (string) $col['aggregate_of'] ); } } } foreach ( $aggregates as $key => $func ) { $key = (string) $key; $func = strtoupper( (string) $func ); if ( ! in_array( $key, $keys, true ) ) { $this->errors[] = sprintf( 'aggregate on "%s", which is not a column', $key ); continue; } if ( ! in_array( $func, self::AGGREGATES, true ) ) { $this->errors[] = sprintf( 'aggregate "%s" on "%s" is not one of %s', $func, $key, implode( '/', self::AGGREGATES ) ); continue; } $out[ $key ] = $func; } return $out; } private function validate_actions( $actions ) { $out = array(); $seen = array(); foreach ( $actions as $i => $action ) { if ( ! is_array( $action ) ) { $this->errors[] = sprintf( 'action %d is not an array', (int) $i ); continue; } foreach ( array_keys( $action ) as $key ) { if ( ! in_array( (string) $key, self::ACTION_KEYS, true ) ) { $this->errors[] = sprintf( 'action %d: unknown key "%s"', (int) $i, (string) $key ); } } foreach ( array( 'label', 'confirm', 'confirm_name', 'confirm_noun', 'confirm_body', 'href' ) as $no_callable ) { if ( isset( $action[ $no_callable ] ) && ! is_scalar( $action[ $no_callable ] ) ) { $this->errors[] = sprintf( 'action %d: "%s" must be a plain string; a definition is serialisable data and cannot carry a callable', (int) $i, $no_callable ); } } $id = isset( $action['id'] ) ? (string) $action['id'] : ''; if ( ! preg_match( '/^[a-z0-9_]+$/', $id ) ) { $this->errors[] = sprintf( 'action %d: id must be lowercase letters, digits and underscores', (int) $i ); continue; } if ( isset( $seen[ $id ] ) ) { $this->errors[] = sprintf( 'duplicate action id "%s"', $id ); continue; } $seen[ $id ] = true; if ( empty( $action['label'] ) ) { $this->errors[] = sprintf( 'action "%s": no label', $id ); } if ( ! empty( $action['destructive'] ) && empty( $action['confirm'] ) ) { $this->errors[] = sprintf( 'action "%s" is destructive and must declare a confirm string', $id ); } if ( isset( $action['variant'] ) && ! AIEO_Variants::is_action_variant( $action['variant'] ) ) { $this->errors[] = sprintf( 'action "%s": unknown variant "%s" (one of: %s)', $id, (string) $action['variant'], implode( ', ', AIEO_Variants::ACTION_VARIANTS ) ); } if ( isset( $action['confirm_level'] ) && ! in_array( (string) $action['confirm_level'], self::ACTION_CONFIRM_LEVELS, true ) ) { $this->errors[] = sprintf( 'action "%s": unknown confirm_level "%s" (one of: %s)', $id, (string) $action['confirm_level'], implode( ', ', self::ACTION_CONFIRM_LEVELS ) ); } if ( isset( $action['confirm_name'] ) && ! preg_match( '/^[a-zA-Z0-9_]+$/', (string) $action['confirm_name'] ) ) { $this->errors[] = sprintf( 'action "%s": `confirm_name` must name a column', $id ); } if ( isset( $action['intent'] ) ) { $intent = is_array( $action['intent'] ) ? $action['intent'] : array( 'name' => (string) $action['intent'] ); foreach ( array_keys( $intent ) as $k ) { if ( ! in_array( (string) $k, AIEO_Action_Intents::INTENT_KEYS, true ) ) { $this->errors[] = sprintf( 'action "%s": unknown `intent` key "%s"', $id, (string) $k ); } } $iname = isset( $intent['name'] ) ? (string) $intent['name'] : ''; if ( ! AIEO_Action_Intents::is_intent( $iname ) ) { $this->errors[] = sprintf( 'action "%s": unknown intent "%s" (one of: %s)', $id, $iname, implode( ', ', AIEO_Action_Intents::INTENTS ) ); } if ( empty( $intent['key'] ) || ! preg_match( '/^[a-zA-Z0-9_]+$/', (string) $intent['key'] ) ) { $this->errors[] = sprintf( 'action "%s": `intent` must name the column holding the subject id', $id ); } if ( ! empty( $action['href'] ) ) { $this->errors[] = sprintf( 'action "%s": declares both `intent` and `href`; pick one', $id ); } $action['intent'] = $intent; } if ( isset( $action['when'] ) ) { $when = (array) $action['when']; foreach ( array_keys( $when ) as $k ) { if ( ! in_array( (string) $k, self::ACTION_WHEN_KEYS, true ) ) { $this->errors[] = sprintf( 'action "%s": unknown `when` key "%s"', $id, (string) $k ); } } if ( empty( $when['key'] ) || ! preg_match( '/^[a-zA-Z0-9_]+$/', (string) $when['key'] ) ) { $this->errors[] = sprintf( 'action "%s": `when` must name a column', $id ); } elseif ( ! array_key_exists( 'is', $when ) && ! array_key_exists( 'not', $when ) ) { $this->errors[] = sprintf( 'action "%s": `when` needs `is` or `not`', $id ); } } $bulk = isset( $action['bulk'] ) ? $action['bulk'] : false; if ( false !== $bulk && '' !== $bulk ) { if ( true === $bulk || 'selection' === $bulk ) { $action['bulk'] = 'selection'; } elseif ( 'filter' === $bulk ) { $action['bulk'] = 'filter'; } else { $this->errors[] = sprintf( 'action "%s": `bulk` must be true, "selection" or "filter"', $id ); $action['bulk'] = false; } if ( ! empty( $action['destructive'] ) ) { $this->errors[] = sprintf( 'action "%s" is destructive and cannot be promoted with `bulk`; declare an explicit bulk action instead', $id ); } if ( $action['bulk'] && ( empty( $action['bulk_confirm'] ) || ! is_string( $action['bulk_confirm'] ) ) ) { $this->errors[] = sprintf( 'action "%s": `bulk` needs a `bulk_confirm` string that counts the rows (%%1$s)', $id ); } } else { $action['bulk'] = false; } $out[] = array_merge( array( 'id' => $id, 'label' => '', 'capability' => '', 'destructive' => false, 'confirm' => '', 'variant' => 'secondary', 'intent' => array(), 'hook' => '', 'href' => '', 'when' => array(), 'bulk' => false, 'bulk_confirm' => '', ), $action ); } return $out; } private function validate_bulk_actions( $actions, $d ) { $out = array(); $seen = array(); foreach ( $actions as $i => $action ) { if ( ! is_array( $action ) ) { $this->errors[] = sprintf( 'bulk action %d is not an array', (int) $i ); continue; } foreach ( array_keys( $action ) as $key ) { if ( ! in_array( (string) $key, self::BULK_ACTION_KEYS, true ) ) { $this->errors[] = sprintf( 'bulk action %d: unknown key "%s"', (int) $i, (string) $key ); } } foreach ( array( 'label', 'confirm' ) as $no_callable ) { if ( isset( $action[ $no_callable ] ) && ! is_scalar( $action[ $no_callable ] ) ) { $this->errors[] = sprintf( 'bulk action %d: "%s" must be a plain string; a definition is serialisable data and cannot carry a callable', (int) $i, $no_callable ); } } $id = isset( $action['id'] ) ? (string) $action['id'] : ''; if ( ! preg_match( '/^[a-z0-9_]+$/', $id ) ) { $this->errors[] = sprintf( 'bulk action %d: id must be lowercase letters, digits and underscores', (int) $i ); continue; } if ( isset( $seen[ $id ] ) ) { $this->errors[] = sprintf( 'duplicate bulk action id "%s"', $id ); continue; } $seen[ $id ] = true; if ( empty( $action['label'] ) ) { $this->errors[] = sprintf( 'bulk action "%s": no label', $id ); } if ( empty( $action['confirm'] ) || ! is_string( $action['confirm'] ) ) { $this->errors[] = sprintf( 'bulk action "%s": no confirm string; a bulk action changes many rows at once and must say how many before it does', $id ); } $scope = isset( $action['scope'] ) ? (string) $action['scope'] : 'selection'; if ( ! in_array( $scope, self::BULK_SCOPES, true ) ) { $this->errors[] = sprintf( 'bulk action "%s": scope must be one of %s', $id, implode( '/', self::BULK_SCOPES ) ); $scope = 'selection'; } $param = isset( $action['param'] ) ? $action['param'] : array(); if ( $param ) { $param = $this->validate_bulk_param( $id, $param ); } else { $param = array(); } $out[] = array_merge( array( 'id' => $id, 'label' => '', 'capability' => '', 'destructive' => false, 'confirm' => '', ), $action, array( 'scope' => $scope, 'param' => $param, ) ); } $derived = array(); foreach ( (array) $d['actions'] as $action ) { if ( ! empty( $action['bulk'] ) ) { $derived[] = (string) $action['id']; } } foreach ( (array) $d['columns'] as $col ) { if ( ! empty( $col['settable'] ) ) { $derived[] = 'set_' . strtolower( (string) $col['key'] ); } } if ( $out || $derived ) { if ( '' === (string) $d['row_key'] ) { $this->errors[] = 'bulk actions are declared but no row_key, so a selection could not name its rows'; } $has_selector = false; foreach ( (array) $d['columns'] as $col ) { if ( isset( $col['type'] ) && 'selector' === (string) $col['type'] ) { $has_selector = true; break; } } if ( ! $has_selector ) { $this->errors[] = 'bulk actions are declared but no selector column, so there is nothing to tick'; } } foreach ( $derived as $did ) { if ( isset( $seen[ $did ] ) ) { $this->errors[] = sprintf( 'bulk action id "%s" collides with a promoted row action or settable column', $did ); } $seen[ $did ] = true; } return $out; } private function validate_bulk_param( $id, $param ) { if ( ! is_array( $param ) ) { $this->errors[] = sprintf( 'bulk action "%s": `param` must be an array', $id ); return array(); } foreach ( array_keys( $param ) as $k ) { if ( ! in_array( (string) $k, self::BULK_PARAM_KEYS, true ) ) { $this->errors[] = sprintf( 'bulk action "%s": unknown param key "%s"', $id, (string) $k ); } } $type = isset( $param['type'] ) ? (string) $param['type'] : ''; if ( ! in_array( $type, self::BULK_PARAM_TYPES, true ) ) { $this->errors[] = sprintf( 'bulk action "%s": param type must be one of %s', $id, implode( '/', self::BULK_PARAM_TYPES ) ); return array(); } if ( 'term' === $type && ( empty( $param['taxonomy'] ) || ! preg_match( '/^[a-z0-9_\-]+$/', (string) $param['taxonomy'] ) ) ) { $this->errors[] = sprintf( 'bulk action "%s": a term param must name its taxonomy', $id ); } if ( 'select' === $type ) { $options = isset( $param['options'] ) ? $param['options'] : null; if ( ! is_array( $options ) || ! $options ) { $this->errors[] = sprintf( 'bulk action "%s": a select param needs a non-empty options map', $id ); } else { foreach ( $options as $ov ) { if ( ! is_scalar( $ov ) ) { $this->errors[] = sprintf( 'bulk action "%s": every param option must be a scalar', $id ); break; } } } } if ( isset( $param['label'] ) && ! is_scalar( $param['label'] ) ) { $this->errors[] = sprintf( 'bulk action "%s": the param label must be a plain string', $id ); } return array_merge( array( 'type' => $type, 'taxonomy' => '', 'options' => array(), 'required' => true, 'label' => '', ), $param ); } private function validate_capabilities( $caps ) { $out = array(); foreach ( $caps as $key => $capability ) { $key = (string) $key; if ( ! in_array( $key, self::CAPABILITY_KEYS, true ) ) { $this->errors[] = sprintf( 'unknown capability "%s"; expected one of %s', $key, implode( '/', self::CAPABILITY_KEYS ) ); continue; } if ( ! is_string( $capability ) || ! preg_match( '/^[a-zA-Z0-9_\-]+$/', $capability ) ) { $this->errors[] = sprintf( 'capability "%s" must be a capability NAME, not %s', $key, gettype( $capability ) ); continue; } $out[ $key ] = $capability; } return $out; } public function capability_for( $what ) { $caps = (array) $this->get( 'capabilities', array() ); $chain = array( 'view' => array( 'view' ), 'edit' => array( 'edit', 'view' ), 'execute' => array( 'execute', 'edit', 'view' ), 'export' => array( 'export', 'view' ), ); foreach ( isset( $chain[ $what ] ) ? $chain[ $what ] : array( 'view' ) as $key ) { if ( ! empty( $caps[ $key ] ) ) { return (string) $caps[ $key ]; } } return ''; } public function user_can( $what ) { $required = $this->capability_for( $what ); if ( '' === $required ) { $required = class_exists( 'AIEO_Report_Roles' ) ? AIEO_Report_Roles::capability_for( $what ) : AIEO_CAPABILITY; } return current_user_can( $required ); } private function validate_screen( $d ) { $keys = array_column( (array) $d['columns'], 'key' ); if ( array() !== $d['figures'] ) { $select = is_array( $d['figures'] ) ? (string) ( $d['figures']['select'] ?? '' ) : ''; if ( '' === trim( $select ) || false !== strpos( $select, ';' ) || ! is_array( $d['figures']['items'] ?? null ) ) { $this->errors[] = 'figures must be {select, items}, the select one list of expressions'; } } foreach ( (array) $d['guidance'] as $i => $text ) { if ( ! is_array( $text ) || '' === trim( (string) ( $text['text'] ?? '' ) ) ) { $this->errors[] = sprintf( 'guidance %d has no text', (int) $i ); } } foreach ( (array) $d['views'] as $key => $view ) { if ( ! is_array( $view ) || 1 !== preg_match( '/^[a-z0-9]+(?:_[a-z0-9]+)*$/', (string) $key ) ) { $this->errors[] = sprintf( 'view "%s" must be keyed in lowercase words joined by single underscores', (string) $key ); } } $filter = isset( $d['filter_bar']['first'] ) ? (array) $d['filter_bar']['first'] : array(); foreach ( array_merge( (array) $d['columns_shown'], $filter ) as $key ) { if ( '__actions' !== $key && ! in_array( (string) $key, $keys, true ) ) { $this->errors[] = sprintf( 'the screen names an undeclared column "%s"', (string) $key ); } } } private function validate_row_key( $d ) { $key = (string) $d['row_key']; if ( '' === $key ) { if ( ! empty( $d['actions'] ) ) { $this->errors[] = 'actions are declared but no row_key, so an action could not identify its row'; } return; } $keys = wp_list_pluck( (array) $d['columns'], 'key' ); if ( ! in_array( $key, $keys, true ) ) { $this->errors[] = sprintf( 'row_key "%s" is not a declared column', $key ); } } private function validate_default_sort( $d ) { if ( null === $d['default_sort'] ) { return; } if ( ! is_array( $d['default_sort'] ) || ! isset( $d['default_sort'][0] ) ) { $this->errors[] = 'default_sort must be array( column, "asc"|"desc" )'; return; } $want = (string) $d['default_sort'][0]; $sortable = array(); foreach ( (array) $d['columns'] as $col ) { if ( ! empty( $col['sortable'] ) ) { $sortable[] = (string) ( ! empty( $col['sort_key'] ) ? $col['sort_key'] : $col['key'] ); } } if ( ! in_array( $want, $sortable, true ) ) { $this->errors[] = sprintf( 'default_sort names "%s", which is not a sortable column', $want ); } } public static function is_sql_builder( $sql ) { if ( is_string( $sql ) ) { if ( preg_match( '/^\s*(SELECT|WITH)\b/i', $sql ) ) { return false; } return function_exists( $sql ); } return is_callable( $sql ); } public static function looks_like_a_select( $sql ) { $sql = trim( $sql ); if ( '' === $sql ) { return false; } $stripped = preg_replace( "/'(?:[^'\\\\]|\\\\.)*'/", "''", $sql ); $stripped = preg_replace( '/"(?:[^"\\\\]|\\\\.)*"/', '""', (string) $stripped ); $stripped = preg_replace( '/--[^\n]*|#[^\n]*/', '', (string) $stripped ); $stripped = preg_replace( '#/\*.*?\*/#s', '', (string) $stripped ); $stripped = trim( (string) $stripped ); if ( ! preg_match( '/^\s*(SELECT|WITH)\b/i', $stripped ) ) { return false; } $stripped = rtrim( $stripped, "; \t\n\r" ); return false === strpos( $stripped, ';' ); } public static function is_safe_select_expression( $expr ) { $expr = trim( $expr ); if ( '' === $expr || strlen( $expr ) > 200 ) { return false; } if ( preg_match( '/\b(SELECT|UNION|INSERT|UPDATE|DELETE|DROP|INTO|SLEEP|BENCHMARK|INFORMATION_SCHEMA)\b/i', $expr ) ) { return false; } if ( false !== strpos( $expr, ';' ) || false !== strpos( $expr, '--' ) || false !== strpos( $expr, '/*' ) ) { return false; } return (bool) preg_match( '/^[A-Za-z0-9_ ,.`()*\/+\-\'"=<>!:]+$/', $expr ); } }