Skip to content

Commit

Permalink
Dataviews: All templates: Add: Sorting to template author.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Nov 20, 2023
1 parent 0ba206b commit 42e92e5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
56 changes: 56 additions & 0 deletions lib/compat/wordpress-6.5/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,59 @@ function gutenberg_register_global_styles_revisions_endpoints() {
}

add_action( 'rest_api_init', 'gutenberg_register_global_styles_revisions_endpoints' );

function gutenberg_get_wp_templates_author_text_field( $template_object ) {

Check failure on line 23 in lib/compat/wordpress-6.5/rest-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Missing doc comment for function gutenberg_get_wp_templates_author_text_field()
if ( 'wp_template' === $template_object['type'] || 'wp_template_part' === $template_object['type'] ) {
// Added by theme.
// Template originally provided by a theme, but customized by a user.
// Templates originally didn't have the 'origin' field so identify
// older customized templates by checking for no origin and a 'theme'
// or 'custom' source.
if ( $template_object['has_theme_file'] &&
( 'theme' === $template_object['origin'] || (
empty( $template_object['origin'] ) && in_array( $template_object['source'], array(

Check failure on line 32 in lib/compat/wordpress-6.5/rest-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Opening parenthesis of a multi-line function call must be the last content on the line

Check failure on line 32 in lib/compat/wordpress-6.5/rest-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Only one argument is allowed per line in a multi-line function call
'theme',
'custom',
), true ) )

Check failure on line 35 in lib/compat/wordpress-6.5/rest-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Only one argument is allowed per line in a multi-line function call

Check failure on line 35 in lib/compat/wordpress-6.5/rest-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Closing parenthesis of a multi-line function call must be on a line by itself
)
) {
$theme_name = wp_get_theme( $template_object['theme'] )->get( 'Name' );
return empty( $theme_name ) ? $template_object['theme'] : $theme_name;
}

// Added by plugin.
if ( $template_object['has_theme_file'] && $template_object['origin'] === 'plugin' ) {

Check failure on line 43 in lib/compat/wordpress-6.5/rest-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Use Yoda Condition checks, you must.
$plugins = get_plugins();
$plugin = $plugins[ plugin_basename( sanitize_text_field( $template_object['theme'] . '.php' ) ) ];

Check warning on line 45 in lib/compat/wordpress-6.5/rest-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
return empty( $plugin['Name'] ) ? $template_object['theme'] : $plugin['Name'];
}

// Added by site.
// Template was created from scratch, but has no author. Author support
// was only added to templates in WordPress 5.9. Fallback to showing the
// site logo and title.
if ( empty( $template_object['has_theme_file'] ) && $template_object['source'] === 'custom' && empty( $template_object['author'] ) ) {

Check failure on line 53 in lib/compat/wordpress-6.5/rest-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Use Yoda Condition checks, you must.
return get_bloginfo( 'name' );
}
}

// Added by user.
return get_user_by( 'id', $template_object['author'] );
}

/**
* Registers the Global Styles Revisions REST API routes.
*/
function gutenberg_register_wp_templates_author_text_field() {
register_rest_field(
'wp_template',
'author_text',
array(
'get_callback' => 'gutenberg_get_wp_templates_author_text_field',
'update_callback' => null,
'schema' => null,
)
);
}

add_action( 'rest_api_init', 'gutenberg_register_wp_templates_author_text_field' );
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ export default function DataviewsTemplates() {
: titleB.localeCompare( titleA );
} );
}
if ( view.sort.field === 'author' ) {
filteredTemplates.sort( ( a, b ) => {
const authorA = a.author_text;
const authorB = b.author_text;
return view.sort.direction === 'asc'
? authorA.localeCompare( authorB )
: authorB.localeCompare( authorA );
} );
}
}
// Handle pagination.
const start = ( view.page - 1 ) * view.perPage;
Expand Down Expand Up @@ -180,10 +189,12 @@ export default function DataviewsTemplates() {
{
header: __( 'Author' ),
id: 'author',
getValue: () => {},
render: ( { item } ) => <AuthorField item={ item } />,
getValue: ( { item } ) => item.author_text,
render: ( { item } ) => {
return <AuthorField item={ item } />;
},
enableHiding: false,
enableSorting: false,
enableSorting: true,
},
],
[]
Expand Down

0 comments on commit 42e92e5

Please sign in to comment.