Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display metadata in the table #1462

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions classes/class-db-driver-wpdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,17 @@ public function insert_record( $data ) {
$record_id = $wpdb->insert_id;

// Insert record meta.
foreach ( (array) $meta as $key => $vals ) {
foreach ( (array) $vals as $val ) {
foreach ( (array) $meta as $key => $val ) {
if ( is_array( $val ) ) {
$vals = $val;
foreach ( $vals as $k => $val ) {
if ( is_scalar( $val ) && '' !== $val ) {
$this->insert_meta( $record_id, $key . '[' . $k . ']', substr( $val, 0, 200 ) );
}
}
} else {
if ( is_scalar( $val ) && '' !== $val ) {
$this->insert_meta( $record_id, $key, $val );
$this->insert_meta( $record_id, $key, substr( $val, 0, 200 ) );
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions classes/class-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,22 @@ public function column_default( $item, $column_name ) {
esc_attr( $view_all_text )
);
}
if ( $record->meta ) {
$meta = array();
foreach ( $record->meta as $key => $value ) {
if ( false === strpos( $key, '[' ) ) {
$meta[ $key ] = $value;
} else {
$main_key = substr( $key, 0, strpos( $key, '[' ) );
$sub_key = substr( $key, strpos( $key, '[' ) + 1, - 1 );

$meta[ $main_key ][ $sub_key ] = $value;
}
}
$out .= '<details><summary>' . esc_html__( 'Metadata', 'stream' ) . '</summary><pre>';
$out .= esc_html( print_r( $meta, true ) );
$out .= '</pre></details>';
}
$out .= $this->get_action_links( $record );
break;

Expand Down
45 changes: 40 additions & 5 deletions classes/class-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ public function query( $args ) {
$selects[] = "$wpdb->stream.*";
}

$select = implode( ', ', $selects );
$join = "LEFT JOIN $wpdb->streammeta ON $wpdb->stream.ID = $wpdb->streammeta.record_id";
$selects[] = "$wpdb->streammeta.meta_key";
$selects[] = "$wpdb->streammeta.meta_value";
$select = implode( ', ', $selects );

/**
* Filters query WHERE statement as an alternative to filtering
Expand All @@ -231,15 +234,31 @@ public function query( $args ) {
*/
$where = apply_filters( 'wp_stream_db_query_where', $where );

/**
* Build the query but just to get the IDs
*/
$query = "SELECT $wpdb->stream.ID
FROM $wpdb->stream
WHERE 1=1 {$where}
{$orderby}
{$limits}";

$ids = $wpdb->get_col( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

if ( $ids ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ID IN (%d" . str_repeat( ', %d', count( $ids ) - 1 ) . ')', $ids );
} else {
$where .= ' AND 0 = 1';
}

/**
* BUILD THE FINAL QUERY
*/
$query = "SELECT {$select}
FROM $wpdb->stream
{$join}
WHERE 1=1 {$where}
{$orderby}
{$limits}";
{$orderby}";

/**
* Filter allows the final query to be modified before execution
Expand All @@ -254,7 +273,6 @@ public function query( $args ) {
// Build result count query.
$count_query = "SELECT COUNT(*) as found
FROM $wpdb->stream
{$join}
WHERE 1=1 {$where}";

/**
Expand All @@ -267,11 +285,28 @@ public function query( $args ) {
*/
$count_query = apply_filters( 'wp_stream_db_count_query', $count_query, $args );

$items = array();
foreach ( $wpdb->get_results( $query ) as $item ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
if ( ! isset( $items[ $item->ID ] ) ) {
$items[ $item->ID ] = clone $item;
$items[ $item->ID ]->meta = array();
unset( $items[ $item->ID ]->meta_key );
unset( $items[ $item->ID ]->meta_value );
}
if ( isset( $items[ $item->ID ]->meta[ $item->meta_key ] ) ) {
if ( ! is_array( $items[ $item->ID ]->meta[ $item->meta_key ] ) ) {
$items[ $item->ID ]->meta[ $item->meta_key ] = array( $items[ $item->ID ]->meta[ $item->meta_key ] );
}
$items[ $item->ID ]->meta[ $item->meta_key ][] = $item->meta_value;
} else {
$items[ $item->ID ]->meta[ $item->meta_key ] = $item->meta_value;
}
}
/**
* QUERY THE DATABASE FOR RESULTS
*/
$result = array(
'items' => $wpdb->get_results( $query ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
'items' => $items,
'count' => absint( $wpdb->get_var( $count_query ) ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
);

Expand Down