$row ) { $row = self::normalise( $id, $row ); if ( isset( $overrides['tables'][ $id ] ) ) { $row = self::apply_override( $row, (array) $overrides['tables'][ $id ] ); } $out[ $id ] = $row; } self::$memo = $out; return $out; } public static function get( $id ) { $all = self::all(); return isset( $all[ (string) $id ] ) ? $all[ (string) $id ] : null; } public static function ids() { return array_keys( self::all() ); } public static function for_page( $page ) { $page = (string) $page; return array_filter( self::all(), static function ( $row ) use ( $page ) { return $row['page'] === $page; } ); } public static function for_anchor( $anchor, $page = '' ) { $anchor = (string) $anchor; $page = (string) $page; if ( '' === $anchor ) { return null; } $found = null; foreach ( self::all() as $row ) { if ( $row['anchor'] !== $anchor ) { continue; } if ( '' !== $page && $row['page'] === $page ) { return $row; } if ( null === $found ) { $found = $row; } } return $found; } public static function declared() { if ( null !== self::$declared ) { return self::$declared; } $rows = array(); $file = AIEO_DIR . 'includes/dmm/data/table-registry.php'; if ( file_exists( $file ) ) { $loaded = include $file; if ( is_array( $loaded ) ) { $rows = $loaded; } } $rows = apply_filters( 'aieo_table_registry', $rows ); if ( ! is_array( $rows ) ) { $rows = array(); } self::$declared = $rows; return $rows; } public static function defect_text() { if ( null !== self::$defects ) { return self::$defects; } $text = array(); $file = AIEO_DIR . 'includes/dmm/data/table-defects.php'; if ( file_exists( $file ) ) { $loaded = include $file; if ( is_array( $loaded ) ) { $text = $loaded; } } $text = apply_filters( 'aieo_table_defect_text', $text ); self::$defects = is_array( $text ) ? $text : array(); return self::$defects; } public static function defect_titles( $ids ) { $all = self::defect_text(); $out = array(); foreach ( (array) $ids as $id ) { $id = (string) $id; if ( isset( $all[ $id ] ) ) { $out[ $id ] = (string) $all[ $id ]; } } return $out; } public static function flush() { self::$memo = null; self::$declared = null; self::$defects = null; } public static function superseded_aliases() { $out = array(); foreach ( self::declared() as $id => $row ) { $row = self::normalise( $id, $row ); if ( '' === $row['superseded_by'] ) { continue; } if ( ! preg_match( '/^insights_t(\d+)\./', (string) $id, $m ) ) { continue; } if ( ! empty( $row['superseded_no_alias'] ) ) { continue; } $out[ 'insights_t' . (int) $m[1] ] = array( 'definition' => $row['superseded_by'], 'table_id' => (int) $m[1], 'label' => $row['superseded_label'], 'preset' => $row['superseded_filter'], ); } return $out; } public static function superseded_vendor_tables() { $out = array(); foreach ( self::declared() as $id => $row ) { $by = isset( $row['superseded_by'] ) ? (string) $row['superseded_by'] : ''; if ( '' === $by ) { continue; } if ( preg_match( '/^insights_t(\d+)\./', (string) $id, $m ) ) { $out[ (int) $m[1] ] = $by; } } return $out; } public static function normalise( $id, $row ) { $row = (array) $row; $out = array( 'id' => (string) $id, 'label' => isset( $row['label'] ) ? (string) $row['label'] : (string) $id, 'page' => isset( $row['page'] ) ? (string) $row['page'] : self::page_from_id( $id ), 'anchor' => isset( $row['anchor'] ) ? (string) $row['anchor'] : '', 'query' => isset( $row['query'] ) && is_array( $row['query'] ) ? $row['query'] : array(), 'kind' => self::enum( isset( $row['kind'] ) ? $row['kind'] : '', self::KINDS, 'grid' ), 'phone' => self::enum( isset( $row['phone'] ) ? $row['phone'] : '', self::PHONE, '' ), 'tier' => self::enum( isset( $row['tier'] ) ? $row['tier'] : '', self::TIERS, 'inherit' ), 'scope' => self::enum( isset( $row['scope'] ) ? $row['scope'] : '', self::SCOPES, 'local' ), 'source' => self::enum( isset( $row['source'] ) ? $row['source'] : '', self::SOURCES, 'rows' ), 'rows_est' => isset( $row['rows_est'] ) ? (int) $row['rows_est'] : null, 'defects' => isset( $row['defects'] ) ? array_map( 'strval', (array) $row['defects'] ) : array(), 'note' => isset( $row['note'] ) ? (string) $row['note'] : '', 'caps' => self::normalise_caps( isset( $row['caps'] ) ? $row['caps'] : array() ), 'per_page' => isset( $row['per_page'] ) ? self::clamp_per_page( $row['per_page'] ) : null, 'adopted' => ! empty( $row['adopted'] ), 'superseded_by' => isset( $row['superseded_by'] ) ? (string) $row['superseded_by'] : '', 'superseded_filter' => isset( $row['superseded_filter'] ) && is_array( $row['superseded_filter'] ) ? $row['superseded_filter'] : array(), 'superseded_label' => isset( $row['superseded_label'] ) ? (string) $row['superseded_label'] : '', 'superseded_no_alias' => ! empty( $row['superseded_no_alias'] ), ); if ( 'bespoke' === $out['kind'] ) { $out['caps'] = self::normalise_caps( array( 'sort' => 'none' ) ); } return $out; } private static function normalise_caps( $caps ) { $caps = (array) $caps; $out = array( 'sort' => self::enum( isset( $caps['sort'] ) ? $caps['sort'] : '', self::SORT_MODES, 'client' ) ); foreach ( self::FLAGS as $flag ) { $out[ $flag ] = ! empty( $caps[ $flag ] ); } return $out; } public static function problems() { $problems = array(); $declared = self::declared(); $modules = self::known_pages(); foreach ( $declared as $id => $row ) { $id = (string) $id; $bad = array(); $row = (array) $row; if ( ! preg_match( '/^[a-z0-9_-]+\.[a-z0-9_]+$/', $id ) ) { $bad[] = __( 'The id must be lowercase page.table: the page may carry hyphens, the table part is letters, digits and underscores.', 'ai-eshop-optimizer' ); } if ( empty( $row['label'] ) ) { $bad[] = __( 'No label.', 'ai-eshop-optimizer' ); } foreach ( array( 'kind' => self::KINDS, 'phone' => self::PHONE, 'tier' => self::TIERS, 'scope' => self::SCOPES, 'source' => self::SOURCES, ) as $key => $legal ) { if ( isset( $row[ $key ] ) && ! in_array( (string) $row[ $key ], $legal, true ) ) { $bad[] = sprintf( __( '%1$s is not a legal value: %2$s', 'ai-eshop-optimizer' ), $key, (string) $row[ $key ] ); } } $page = isset( $row['page'] ) ? (string) $row['page'] : self::page_from_id( $id ); if ( '' === $page ) { $bad[] = __( 'No owning page.', 'ai-eshop-optimizer' ); } elseif ( $modules && ! isset( $modules[ $page ] ) ) { $bad[] = sprintf( __( 'No module registered the page %s on this install.', 'ai-eshop-optimizer' ), $page ); } if ( $bad ) { $problems[ $id ] = $bad; } } return $problems; } private static function known_pages() { if ( ! class_exists( 'AIEO_DMM' ) || ! method_exists( 'AIEO_DMM', 'instance' ) ) { return array(); } $dmm = AIEO_DMM::instance(); if ( ! method_exists( $dmm, 'get_modules' ) ) { return array(); } $out = array(); foreach ( (array) $dmm->get_modules() as $key => $module ) { $id = is_object( $module ) && method_exists( $module, 'get_id' ) ? $module->get_id() : $key; if ( '' !== (string) $id ) { $out[ (string) $id ] = true; } } return $out; } private static function page_from_id( $id ) { $parts = explode( '.', (string) $id, 2 ); return isset( $parts[0] ) ? $parts[0] : ''; } private static function enum( $value, $legal, $fallback ) { $value = (string) $value; return in_array( $value, $legal, true ) ? $value : $fallback; } public static function tier( $id ) { $row = self::get( $id ); if ( ! $row ) { return 'free'; } if ( 'inherit' !== $row['tier'] ) { return $row['tier']; } if ( ! class_exists( 'AIEO_Capability_Registry' ) ) { return 'free'; } $resolved = AIEO_Capability_Registry::tier_for( $row['page'], self::group_of( $row['page'] ) ); return isset( $resolved['tier'] ) ? (string) $resolved['tier'] : 'free'; } private static function group_of( $page ) { if ( ! class_exists( 'AIEO_DMM' ) || ! method_exists( 'AIEO_DMM', 'instance' ) ) { return ''; } $module = AIEO_DMM::instance()->get_module( (string) $page ); return ( $module && method_exists( $module, 'get_group' ) ) ? (string) $module->get_group() : ''; } public static function is_portable( $id ) { $row = self::get( $id ); return $row && in_array( $row['scope'], array( 'saas', 'both' ), true ); } public static function overrides() { $stored = get_option( self::OPTION, array() ); if ( ! is_array( $stored ) ) { $stored = array(); } return array( 'defaults' => isset( $stored['defaults'] ) && is_array( $stored['defaults'] ) ? $stored['defaults'] : array(), 'tables' => isset( $stored['tables'] ) && is_array( $stored['tables'] ) ? $stored['tables'] : array(), ); } private static function apply_override( $row, $override ) { if ( isset( $override['caps'] ) && is_array( $override['caps'] ) ) { $caps = $row['caps']; foreach ( $override['caps'] as $key => $value ) { if ( 'sort' === $key ) { $caps['sort'] = self::enum( $value, self::SORT_MODES, $caps['sort'] ); } elseif ( in_array( (string) $key, self::FLAGS, true ) ) { $caps[ $key ] = (bool) $value; } } if ( 'bespoke' !== $row['kind'] ) { $row['caps'] = $caps; } } if ( isset( $override['per_page'] ) ) { $row['per_page'] = self::clamp_per_page( $override['per_page'] ); } $row['overridden'] = true; return $row; } public static function set_override( $id, $settings ) { $id = (string) $id; if ( ! self::get( $id ) ) { return false; } $clean = array(); if ( isset( $settings['caps'] ) && is_array( $settings['caps'] ) ) { $caps = array(); foreach ( $settings['caps'] as $key => $value ) { if ( 'sort' === $key ) { $caps['sort'] = self::enum( $value, self::SORT_MODES, 'client' ); } elseif ( in_array( (string) $key, self::FLAGS, true ) ) { $caps[ (string) $key ] = (bool) $value; } } if ( $caps ) { $clean['caps'] = $caps; } } if ( isset( $settings['per_page'] ) && '' !== $settings['per_page'] ) { $clean['per_page'] = self::clamp_per_page( $settings['per_page'] ); } $stored = self::overrides(); if ( $clean ) { $stored['tables'][ $id ] = $clean; } else { unset( $stored['tables'][ $id ] ); } update_option( self::OPTION, $stored, false ); self::flush(); return true; } public static function clear_override( $id ) { $stored = self::overrides(); if ( ! isset( $stored['tables'][ (string) $id ] ) ) { return false; } unset( $stored['tables'][ (string) $id ] ); update_option( self::OPTION, $stored, false ); self::flush(); return true; } public static function site_defaults() { $stored = self::overrides(); return array( 'per_page' => isset( $stored['defaults']['per_page'] ) ? self::clamp_per_page( $stored['defaults']['per_page'] ) : self::PER_PAGE_DEFAULT, ); } public static function set_site_defaults( $defaults ) { $stored = self::overrides(); if ( isset( $defaults['per_page'] ) ) { $stored['defaults']['per_page'] = self::clamp_per_page( $defaults['per_page'] ); } update_option( self::OPTION, $stored, false ); self::flush(); } public static function per_page( $id ) { $row = self::get( $id ); $defaults = self::site_defaults(); $value = ( $row && null !== $row['per_page'] ) ? $row['per_page'] : $defaults['per_page']; $mine = self::user_per_page( $id ); if ( $mine > 0 ) { $value = $mine; } return self::clamp_per_page( apply_filters( 'aieo_table_per_page', $value, (string) $id ) ); } public static function user_per_page( $id ) { $uid = get_current_user_id(); if ( ! $uid ) { return 0; } $all = get_user_meta( $uid, self::USER_META, true ); if ( ! is_array( $all ) || ! isset( $all[ (string) $id ] ) ) { return 0; } return self::clamp_per_page( $all[ (string) $id ] ); } public static function set_user_per_page( $id, $value ) { $uid = get_current_user_id(); if ( ! $uid || '' === (string) $id ) { return false; } $all = get_user_meta( $uid, self::USER_META, true ); $all = is_array( $all ) ? $all : array(); if ( (int) $value <= 0 ) { unset( $all[ (string) $id ] ); } else { unset( $all[ (string) $id ] ); $all[ (string) $id ] = self::clamp_per_page( $value ); } if ( count( $all ) > self::USER_META_MAX ) { $all = array_slice( $all, -self::USER_META_MAX, null, true ); } update_user_meta( $uid, self::USER_META, $all ); return true; } public static function clamp_per_page( $value ) { $n = (int) $value; if ( $n <= 0 ) { return self::PER_PAGE_DEFAULT; } return max( self::PER_PAGE_MIN, min( self::PER_PAGE_MAX, $n ) ); } public static function counts() { $out = array( 'total' => 0, 'grid' => 0, 'form' => 0, 'bespoke' => 0, 'free' => 0, 'pro' => 0, 'local' => 0, 'saas' => 0, 'both' => 0, 'adopted' => 0, 'defects' => 0, 'problems' => count( self::problems() ), ); foreach ( self::all() as $id => $row ) { $out['total']++; $out[ $row['kind'] ]++; $out[ $row['scope'] ]++; $out[ self::tier( $id ) ]++; if ( $row['adopted'] ) { $out['adopted']++; } $out['defects'] += count( $row['defects'] ); } return $out; } public static function capability_rows() { $rows = array(); foreach ( self::all() as $id => $row ) { $rows[ 'table:' . $id ] = array( 'id' => 'table:' . $id, 'label' => $row['label'], 'kind' => 'table', 'page' => $row['page'], 'tier' => self::tier( $id ), 'description' => $row['note'], ); } return $rows; } }