'', 'sort' => 'agents', 'limit' => 0, 'columns' => 'agents,phone,award', 'credit' => 'no', ), $atts, 'agenttally_state_agent_stats' ); wp_enqueue_style( 'agenttally-state-agent-statistics' ); if ( '' !== $a['state'] ) { return usrs_render_single( $a['state'] ); } return usrs_render_table( $a ); } add_shortcode( 'agenttally_state_agent_stats', 'usrs_shortcode' ); /** * Styles are registered always but only ENQUEUED where the table is used, so a * site that never uses it ships no extra CSS. The editor script is plain * JavaScript on the wp.* globals (no build step) and only loads in the editor. */ function usrs_register_assets() { wp_register_style( 'agenttally-state-agent-statistics', USRS_URL . 'assets/style.css', array(), USRS_VERSION ); wp_register_script( 'agenttally-state-agent-statistics-block', USRS_URL . 'assets/block.js', array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-server-side-render', 'wp-i18n' ), USRS_VERSION, true ); // The state list and the dataset date for the block's sidebar, from the bundled data. $states = array(); foreach ( usrs_data()['states'] as $s ) { $states[] = array( 'code' => $s['code'], 'name' => $s['name'], ); } usort( $states, function ( $x, $y ) { return strcmp( $x['name'], $y['name'] ); } ); wp_add_inline_script( 'agenttally-state-agent-statistics-block', 'window.agenttallyBlockData = ' . wp_json_encode( array( 'states' => $states, 'asOf' => usrs_as_of(), ) ) . ';', 'before' ); wp_set_script_translations( 'agenttally-state-agent-statistics-block', 'agenttally-state-agent-statistics' ); } add_action( 'init', 'usrs_register_assets' ); /** * Gutenberg block. The editor shows a live, server-rendered preview and a * sidebar with every option, so nobody needs to know the shortcode; the * front end is rendered by the same code as the shortcode. */ function usrs_register_block() { if ( ! function_exists( 'register_block_type' ) ) { return; } register_block_type( 'agenttally-state-agent-statistics/table', array( 'api_version' => 2, 'title' => __( 'AgentTally State Agent Statistics', 'agenttally-state-agent-statistics' ), 'category' => 'widgets', 'icon' => 'chart-bar', 'description' => __( 'Agent counts by US state: a table, or one state inline.', 'agenttally-state-agent-statistics' ), 'keywords' => array( 'real estate', 'agents', 'statistics' ), 'editor_script' => 'agenttally-state-agent-statistics-block', 'style' => 'agenttally-state-agent-statistics', 'attributes' => array( 'state' => array( 'type' => 'string', 'default' => '', ), 'sort' => array( 'type' => 'string', 'default' => 'agents', ), 'limit' => array( 'type' => 'number', 'default' => 0, ), 'columns' => array( 'type' => 'string', 'default' => 'agents,phone,award', ), 'credit' => array( 'type' => 'boolean', 'default' => false, ), ), 'render_callback' => function ( $attrs ) { return usrs_shortcode( array( 'state' => isset( $attrs['state'] ) ? $attrs['state'] : '', 'sort' => isset( $attrs['sort'] ) ? $attrs['sort'] : 'agents', 'limit' => isset( $attrs['limit'] ) ? $attrs['limit'] : 0, 'columns' => isset( $attrs['columns'] ) ? $attrs['columns'] : 'agents,phone,award', 'credit' => ! empty( $attrs['credit'] ) ? 'yes' : 'no', ) ); }, ) ); } add_action( 'init', 'usrs_register_block' );