| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| function gt_id_label_map() {
|
| return [
|
| 'cct' => [],
|
| 'post_type' => [],
|
| 'taxonomy' => [],
|
| ];
|
| }
|
|
|
| function gt_parse_values( $value ) {
|
| if ( is_string( $value ) ) {
|
| $value = trim( $value );
|
| if ( '' === $value ) return [];
|
| $maybe = maybe_unserialize( $value );
|
| if ( is_array( $maybe ) ) {
|
| $value = $maybe;
|
| } elseif ( in_array( $value[0], [ '[', '{' ], true ) && is_array( $json = json_decode( $value, true ) ) ) {
|
| $value = $json;
|
| }
|
| }
|
| $values = [];
|
| if ( is_array( $value ) ) {
|
| foreach ( $value as $k => $v ) {
|
| if ( 'true' === $v || true === $v ) {
|
| $values[] = $k;
|
| } elseif ( 'false' !== $v && false !== $v && is_scalar( $v ) ) {
|
| $values[] = $v;
|
| }
|
| }
|
| } elseif ( is_scalar( $value ) ) {
|
| $values = preg_split( '/\s*[,|;]\s*/', (string) $value );
|
| }
|
| $values = array_map( fn( $v ) => ltrim( trim( (string) $v ), '#' ), $values );
|
| return array_values( array_unique( array_filter( $values, 'strlen' ) ) );
|
| }
|
|
|
| function gt_parse_ids( $value ) {
|
| return array_values( array_map( 'absint', array_filter( gt_parse_values( $value ), fn( $v ) => is_numeric( $v ) && (int) $v > 0 ) ) );
|
| }
|
|
|
| function gt_cct_manager() {
|
| return class_exists( '\Jet_Engine\Modules\Custom_Content_Types\Module' ) ? \Jet_Engine\Modules\Custom_Content_Types\Module::instance()->manager : null;
|
| }
|
|
|
| function gt_pick_label( $item, $field = '' ) {
|
| foreach ( array_filter( [ $field, 'post_title', 'name', 'title', 'nazov', 'label', 'display_name' ] ) as $f ) {
|
| if ( isset( $item[ $f ] ) && is_scalar( $item[ $f ] ) && '' !== (string) $item[ $f ] ) return (string) $item[ $f ];
|
| }
|
| return '';
|
| }
|
|
|
| function gt_id_to_label( $id, $target ) {
|
| static $cache = [];
|
| $key = "$target|$id";
|
| if ( isset( $cache[ $key ] ) ) return $cache[ $key ];
|
| $p = explode( ':', $target ) + [ '', '', '' ];
|
| $label = '';
|
| if ( 'post' === $p[0] && ( $post = get_post( $id ) ) ) {
|
| $label = '' !== $post->post_title ? $post->post_title : sprintf( '(bez názvu #%d)', $id );
|
| } elseif ( 'term' === $p[0] && ( $term = get_term( $id, $p[1] ) ) && ! is_wp_error( $term ) ) {
|
| $label = $term->name;
|
| } elseif ( 'user' === $p[0] && ( $user = get_userdata( $id ) ) ) {
|
| $label = $user->display_name;
|
| } elseif ( 'cct' === $p[0] && $p[1] && ( $m = gt_cct_manager() ) && ( $type = $m->get_content_types( $p[1] ) ) && ! empty( $type->db ) && ( $item = $type->db->get_item( $id ) ) ) {
|
| $label = gt_pick_label( (array) $item, $p[2] );
|
| }
|
| return $cache[ $key ] = $label;
|
| }
|
|
|
| function gt_field_options_map( $field ) {
|
| static $cache = [];
|
| $key = md5( wp_json_encode( $field ) );
|
| if ( isset( $cache[ $key ] ) ) return $cache[ $key ];
|
|
|
| $map = [];
|
| $source = ( $field['options_source'] ?? '' ) ?: ( ! empty( $field['query_id'] ) ? 'query' : ( ! empty( $field['options_from_glossary'] ) ? 'glossary' : '' ) );
|
|
|
| try {
|
| if ( 'query' === $source && ! empty( $field['query_id'] ) && class_exists( '\Jet_Engine\Query_Builder\Manager' ) && ( $query = \Jet_Engine\Query_Builder\Manager::instance()->get_query_by_id( $field['query_id'] ) ) ) {
|
| foreach ( (array) $query->get_items() as $item ) {
|
| $item = is_object( $item ) && method_exists( $item, 'to_array' ) ? $item->to_array() : (array) $item;
|
| $value = null;
|
| foreach ( array_filter( [ $field['query_value_field'] ?? '', '_ID', 'ID', 'term_id' ] ) as $f ) {
|
| if ( isset( $item[ $f ] ) ) {
|
| $value = $item[ $f ];
|
| break;
|
| }
|
| }
|
| $label = gt_pick_label( $item, $field['query_label_field'] ?? '' );
|
| if ( is_scalar( $value ) && '' !== $label ) $map[ (string) $value ] = $label;
|
| }
|
| }
|
|
|
| if ( 'glossary' === $source && ! empty( $field['glossary_id'] ) && function_exists( 'jet_engine' ) && ( $g = jet_engine()->glossaries->filters ?? null ) && method_exists( $g, 'get_glossary_options' ) ) {
|
| foreach ( (array) $g->get_glossary_options( $field['glossary_id'] ) as $k => $o ) {
|
| if ( is_array( $o ) && isset( $o['value'] ) ) {
|
| $map[ (string) $o['value'] ] = (string) ( $o['label'] ?? $o['value'] );
|
| } elseif ( is_scalar( $o ) ) {
|
| $map[ (string) $k ] = (string) $o;
|
| }
|
| }
|
| }
|
| } catch ( \Throwable $e ) {}
|
|
|
| if ( ! $map && ! empty( $field['options'] ) && is_array( $field['options'] ) ) {
|
| foreach ( $field['options'] as $k => $o ) {
|
| if ( is_array( $o ) && isset( $o['key'] ) ) {
|
| $map[ (string) $o['key'] ] = (string) ( '' !== ( $o['value'] ?? '' ) ? $o['value'] : $o['key'] );
|
| } elseif ( is_array( $o ) && isset( $o['value'] ) ) {
|
| $map[ (string) $o['value'] ] = (string) ( $o['label'] ?? $o['value'] );
|
| } elseif ( is_scalar( $o ) ) {
|
| $map[ (string) $k ] = (string) $o;
|
| }
|
| }
|
| }
|
|
|
| return $cache[ $key ] = $map;
|
| }
|
|
|
| function gt_target_by_name( $name ) {
|
| $name = strtolower( (string) $name );
|
| if ( '_id' === $name ) return '';
|
| foreach ( [ 'post_id' => 'post', 'page_id' => 'post', 'parent_id' => 'post', 'user_id' => 'user', 'term_id' => 'term' ] as $suffix => $target ) {
|
| if ( $name === $suffix || str_ends_with( $name, "_$suffix" ) ) return $target;
|
| }
|
| if ( str_ends_with( $name, '_parent' ) ) return 'post';
|
| if ( strlen( $name ) > 3 && str_ends_with( $name, '_id' ) && ( $slug = gt_find_cct_slug( substr( $name, 0, -3 ) ) ) ) return "cct:$slug";
|
| return '';
|
| }
|
|
|
| function gt_find_cct_slug( $base ) {
|
| static $cache = [];
|
| if ( isset( $cache[ $base ] ) ) return $cache[ $base ];
|
| $cache[ $base ] = '';
|
| if ( $m = gt_cct_manager() ) {
|
| foreach ( [ $base, $base . 's', preg_replace( '/y$/', 'ies', $base ) ] as $v ) {
|
| foreach ( [ $v, str_replace( '_', '-', $v ), str_replace( '-', '_', $v ) ] as $slug ) {
|
| if ( $slug && $m->get_content_types( $slug ) ) return $cache[ $base ] = $slug;
|
| }
|
| }
|
| }
|
| return $cache[ $base ];
|
| }
|
|
|
| function gt_raw_to_html( $value ) {
|
| return esc_html( is_array( $value ) ? implode( ', ', array_filter( $value, 'is_scalar' ) ) : (string) $value );
|
| }
|
|
|
| function gt_ids_to_html( $value, $target ) {
|
| $ids = gt_parse_ids( $value );
|
| if ( ! $ids ) return null;
|
| return implode( ', ', array_map( fn( $id ) => '' !== ( $l = gt_id_to_label( $id, $target ) ) ? esc_html( $l ) : '#' . $id, $ids ) );
|
| }
|
|
|
| function gt_map_to_html( $value, $map ) {
|
| $values = gt_parse_values( $value );
|
| if ( ! array_intersect_key( $map, array_flip( $values ) ) ) return null;
|
| return implode( ', ', array_map( fn( $v ) => esc_html( $map[ $v ] ?? ( is_numeric( $v ) ? "#$v" : $v ) ), $values ) );
|
| }
|
|
|
| function gt_cct_fields( $factory ) {
|
| try {
|
| $raw = ( function () { return $this->fields ?? null; } )->call( $factory );
|
| } catch ( \Throwable $e ) {
|
| $raw = null;
|
| }
|
| if ( ! $raw && method_exists( $factory, 'get_formatted_fields' ) ) $raw = $factory->get_formatted_fields();
|
| $fields = [];
|
| foreach ( (array) $raw as $key => $field ) {
|
| if ( is_array( $field ) ) $fields[ $field['name'] ?? $key ] = $field;
|
| }
|
| return $fields;
|
| }
|
|
|
| add_filter( 'jet-engine/custom-content-types/admin-columns', function ( $columns, $factory ) {
|
| $manual = gt_id_label_map()['cct'][ $factory->get_arg( 'slug' ) ] ?? [];
|
| $fields = gt_cct_fields( $factory );
|
|
|
| foreach ( $columns as $name => $column ) {
|
| if ( '_ID' === $name || ! is_array( $column ) ) continue;
|
|
|
| $field = $fields[ $name ] ?? [];
|
| $target = $manual[ $name ] ?? '';
|
| $fallback = gt_target_by_name( $name ) ?: ( 'posts' === ( $field['type'] ?? '' ) ? 'post' : '' );
|
| $has_opts = ! empty( $field['options_source'] ) || ! empty( $field['query_id'] ) || ! empty( $field['options_from_glossary'] ) || ! empty( $field['options'] );
|
| if ( ! $target && ! $has_opts && ! $fallback ) continue;
|
|
|
| $orig = isset( $column['_cb'] ) && is_callable( $column['_cb'] ) ? $column['_cb'] : null;
|
|
|
| $columns[ $name ]['_cb'] = function ( ...$args ) use ( $target, $field, $has_opts, $fallback, $orig ) {
|
| $value = $args[0] ?? null;
|
| if ( $target && null !== ( $html = gt_ids_to_html( $value, $target ) ) ) return $html;
|
| if ( $has_opts && ( $opts = gt_field_options_map( $field ) ) && null !== ( $html = gt_map_to_html( $value, $opts ) ) ) return $html;
|
| if ( $fallback && null !== ( $html = gt_ids_to_html( $value, $fallback ) ) ) return $html;
|
| return $orig ? $orig( ...$args ) : gt_raw_to_html( $value );
|
| };
|
| }
|
|
|
| return $columns;
|
| }, 20, 2 );
|
|
|
| function gt_add_columns( $cols, $fields ) {
|
| foreach ( $fields as $key => $target ) $cols[ "gt_$key" ] = ucfirst( trim( str_replace( [ '_', '-' ], ' ', $key ) ) );
|
| return $cols;
|
| }
|
|
|
| function gt_column_html( $col, $fields, $get_meta, $id ) {
|
| $key = substr( $col, 3 );
|
| if ( ! str_starts_with( $col, 'gt_' ) || ! isset( $fields[ $key ] ) ) return null;
|
| return gt_ids_to_html( $get_meta( $id, $key, true ), $fields[ $key ] ) ?? '—';
|
| }
|
|
|
| add_action( 'admin_init', function () {
|
| $map = gt_id_label_map();
|
| foreach ( $map['post_type'] as $pt => $fields ) {
|
| add_filter( "manage_{$pt}_posts_columns", fn( $cols ) => gt_add_columns( $cols, $fields ) );
|
| add_action( "manage_{$pt}_posts_custom_column", function ( $col, $id ) use ( $fields ) { echo gt_column_html( $col, $fields, 'get_post_meta', $id ); }, 10, 2 );
|
| }
|
| foreach ( $map['taxonomy'] as $tax => $fields ) {
|
| add_filter( "manage_edit-{$tax}_columns", fn( $cols ) => gt_add_columns( $cols, $fields ) );
|
| add_filter( "manage_{$tax}_custom_column", fn( $content, $col, $id ) => $content . gt_column_html( $col, $fields, 'get_term_meta', $id ), 10, 3 );
|
| }
|
| } );
|
| |
| |
Comments