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

Improve log display #49

Merged
merged 2 commits into from
Mar 29, 2022
Merged
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
2 changes: 1 addition & 1 deletion ai-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}

define( 'AI_LOGGER_PATH', __DIR__ );
define( 'AI_LOGGER_URL', plugins_url( '/', __FILE__ ) );
define( 'AI_LOGGER_URL', trailingslashit( plugins_url( '/', __FILE__ ) ) );

// Check if Composer is installed.
if ( ! file_exists( __DIR__ . '/vendor/wordpress-autoload.php' ) ) {
Expand Down
2 changes: 1 addition & 1 deletion inc/class-ai-logger-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct() {
* Enqueue and localize assets.
*/
public function enqueue() {
wp_enqueue_script( 'ai-logger-insert', AI_LOGGER_URL . '/static/js/logger-insert.js', [], '0.1', true );
wp_enqueue_script( 'ai-logger-insert', AI_LOGGER_URL . 'static/js/logger-insert.js', [], '0.1', true );
wp_localize_script(
'ai-logger-insert',
'aiLoggerConfig',
Expand Down
3 changes: 3 additions & 0 deletions inc/class-data-structures.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ public function add_meta_boxes( $post_type ) {
'normal',
'high'
);

// Remove the 'publish' meta box.
remove_meta_box( 'submitdiv', static::POST_TYPE, 'side' );
}

/**
Expand Down
39 changes: 39 additions & 0 deletions inc/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public static function instance() {
protected function __construct() {
add_action( 'admin_init', [ $this, 'on_admin_init' ] );
add_action( 'admin_menu', [ $this, 'on_admin_menu' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'on_admin_enqueue_scripts' ] );

// Enforce a single column layout on ai_log posts.
add_filter( 'screen_layout_columns', [ $this, 'on_screen_layout_columns' ], 10, 2 );
add_filter( 'get_user_option_screen_layout_' . Data_Structures::POST_TYPE, fn () => 1 );
}

/**
Expand Down Expand Up @@ -267,4 +272,38 @@ public function render_checkboxes( $args, $values ) {
);
}
}

/**
* Enqueue admin styles for ai_log posts.
*/
public function on_admin_enqueue_scripts() {
$screen = get_current_screen();

if ( empty( $screen ) || Data_Structures::POST_TYPE !== $screen->post_type ) {
return;
}

wp_enqueue_style(
'ai-logger-admin',
AI_LOGGER_URL . 'static/css/admin.css',
[],
'0.1',
);
}

/**
* Set the screen layout to default to one column.
*
* @param array $columns Screen columns.
* @param string $screen_id Screen ID.
* @return array
*/
public function on_screen_layout_columns( $columns, $screen_id ) {
if ( Data_Structures::POST_TYPE !== $screen_id ) {
return $columns;
}

$columns[ Data_Structures::POST_TYPE ] = 1;
return $columns;
}
}
36 changes: 36 additions & 0 deletions static/css/admin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Logger Admin Styles
*/

/* Reverse the order of the content (left on top, meta boxes below) */
.post-type-ai_log #post-body {
display: flex;
flex-direction: column-reverse;
}

/* Force the post title to the top of the page */
.post-type-ai_log #post-body-content {
order: 1;
}

/* Ensure tables are constrained and don't overflow the page */
.post-type-ai_log .ai-log-display table {
table-layout: fixed;
}
.post-type-ai_log .ai-log-display table tbody td,
.post-type-ai_log .ai-log-display table thead th {
width: 180px;
}

.post-type-ai_log .ai-log-display table tbody td + td,
.post-type-ai_log .ai-log-display table thead th + th {
width: auto;
}

/* Style the <pre> tags */
.post-type-ai_log .ai-log-display pre {
background: rgba(0, 0, 0, 0.07);
max-width: 100%;
overflow-x: scroll;
padding: 3px 5px 2px 5px;
}
5 changes: 2 additions & 3 deletions template-parts/log-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ function ai_logger_render_table( array $data ) {
<?php ai_logger_render_table( (array) $value ); ?>
</table>
<?php elseif ( is_scalar( $value ) ) : ?>
<code>
<?php
<pre><?php // phpcs:ignore Squiz.PHP.EmbeddedPhp.ContentBeforeOpen, Squiz.PHP.EmbeddedPhp.ContentAfterOpen
if ( wp_startswith( $value, '{' ) || wp_startswith( $value, '[' ) ) {
$maybe_json_value = json_decode( $value );
if ( ! empty( $maybe_json_value ) ) {
Expand All @@ -54,7 +53,7 @@ function ai_logger_render_table( array $data ) {

echo esc_html( $value );
?>
</code>
</pre>
<?php elseif ( null === $value ) : ?>
<code>(null)</code>
<?php else : ?>
Expand Down