'boolean',
'default' => false,
);
}
// Rows shown before the "Show more" button; 0 shows all rows.
$args['attributes']['rowsShown'] = array(
'type' => 'integer',
'default' => 0,
);
return $args;
}
add_filter( 'register_block_type_args', 'table_block_sort_add_attribute', 10, 2 );
/**
* Registers the front-end and editor assets. Nothing is printed until a page needs it.
*/
function table_block_sort_register_assets() {
$url = plugin_dir_url( __FILE__ ) . 'assets/';
wp_register_script(
'table-block-sort-view',
$url . 'view.js',
array(),
TABLE_BLOCK_SORT_VERSION,
array(
'in_footer' => true,
'strategy' => 'defer',
)
);
wp_register_style( 'table-block-sort-view', $url . 'view.css', array(), TABLE_BLOCK_SORT_VERSION );
}
add_action( 'init', 'table_block_sort_register_assets' );
/**
* Loads the toolbar buttons and the "Visitor tools" sidebar panel.
*/
function table_block_sort_editor_assets() {
wp_enqueue_script(
'table-block-sort-editor',
plugin_dir_url( __FILE__ ) . 'assets/editor.js',
array( 'wp-hooks', 'wp-compose', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n' ),
TABLE_BLOCK_SORT_VERSION,
true
);
wp_set_script_translations( 'table-block-sort-editor', 'table-block-sort' );
}
add_action( 'enqueue_block_editor_assets', 'table_block_sort_editor_assets' );
/**
* The bar placed above the table: the search field on the left, the download and share icons on the
* right. Everything starts hidden and the script shows what works, so without JavaScript there are no
* controls that do nothing.
*
* The search field is core's Search block markup (without its button, since results show while
* typing), so it gets the theme's own search field styles. Its label ("Search this table") is for
* screen readers only; the placeholder says "Search", short enough for a phone.
*
* @param bool $searchable Whether to include the search field.
* @param bool $downloadable Whether to include the download icon.
* @param bool $share Whether to include the share icon.
* @return string
*/
function table_block_sort_tools( $searchable, $downloadable, $share ) {
$html = '
';
if ( $searchable ) {
$id = wp_unique_id( 'table-block-sort-search-' );
$label = __( 'Search this table', 'table-block-sort' );
$html .= sprintf(
'',
esc_attr( $id ),
esc_html( $label ),
esc_attr__( 'Search', 'table-block-sort' ),
/* translators: %d: number of table rows that match the search. */
esc_attr__( 'Matching rows: %d', 'table-block-sort' ),
esc_attr__( 'No rows match your search.', 'table-block-sort' )
);
wp_enqueue_style( 'wp-block-search' );
}
$html .= '
';
}
/**
* The "Show more" bar placed under the table. Hidden until the script knows there are more rows.
*
* @return string
*/
function table_block_sort_more() {
return sprintf(
'
',
/* translators: 1: number of rows shown, 2: number of rows in the table (or matching the search). */
esc_attr__( 'Showing %1$d of %2$d rows', 'table-block-sort' ),
esc_html__( 'Show more', 'table-block-sort' )
);
}
/**
* Marks a table that uses any visitor tool, adds the bars above and under it and loads the script and
* style on that page only. A striped table gets the style too, for stripes in the theme's colors.
*
* @param string $content Rendered block HTML.
* @param array $block Parsed block.
* @return string
*/
function table_block_sort_render( $content, $block ) {
$attrs = isset( $block['attrs'] ) && is_array( $block['attrs'] ) ? $block['attrs'] : array();
$striped = ! empty( $attrs['className'] ) && preg_match( '/(^|\s)is-style-stripes(\s|$)/', $attrs['className'] );
if ( $striped ) {
// Stripes that follow the theme's text color (see view.css), also for tables without other tools.
wp_enqueue_style( 'table-block-sort-view' );
}
$sortable = ! empty( $attrs['sortable'] );
$searchable = ! empty( $attrs['searchable'] );
$downloadable = ! empty( $attrs['downloadable'] );
$cards = ! empty( $attrs['phoneCards'] );
$sticky = ! empty( $attrs['stickyHeader'] );
$links = ! empty( $attrs['shareLinks'] );
$rows_shown = isset( $attrs['rowsShown'] ) ? max( 0, (int) $attrs['rowsShown'] ) : 0;
if ( ( ! $sortable && ! $searchable && ! $downloadable && ! $cards && ! $sticky && ! $rows_shown ) || ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
return $content;
}
$tags = new WP_HTML_Tag_Processor( $content );
if ( $sticky && $tags->next_tag( 'figure' ) ) {
// Core lets the table scroll sideways inside the figure, which stops a sticky header from sticking.
$tags->add_class( 'table-block-sort-has-sticky' );
}
if ( ! $tags->next_tag( 'table' ) ) {
return $content;
}
$classes = array(
'table-block-sort' => $sortable,
'table-block-sort-search' => $searchable,
'table-block-sort-download' => $downloadable,
'table-block-sort-cards' => $cards,
'table-block-sort-sticky' => $sticky,
'table-block-sort-link' => $links,
);
foreach ( $classes as $class => $on ) {
if ( $on ) {
$tags->add_class( $class );
}
}
if ( $rows_shown ) {
$tags->set_attribute( 'data-table-block-sort-rows', (string) $rows_shown );
}
if ( $cards && $sortable ) {
// Wording for the sort menu that replaces the header buttons on phones.
$tags->set_attribute(
'data-table-block-sort-words',
wp_json_encode(
array(
'sort' => __( 'Sort', 'table-block-sort' ),
/* translators: %s: column name. */
'up' => __( '%s: low to high', 'table-block-sort' ),
/* translators: %s: column name. */
'down' => __( '%s: high to low', 'table-block-sort' ),
)
)
);
}
$content = $tags->get_updated_html();
if ( $rows_shown ) {
$position = strripos( $content, '' );
if ( false !== $position ) {
$content = substr_replace( $content, table_block_sort_more(), $position + strlen( '' ), 0 );
}
}
// The share icon needs something to share: a sort or a search.
$share = $links && ( $sortable || $searchable );
if ( $searchable || $downloadable || $share ) {
$position = stripos( $content, '