balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

categories.php (2656B)


      1 <?php
      2 /**
      3  * Server-side rendering of the `core/categories` block.
      4  *
      5  * @package WordPress
      6  */
      7 
      8 /**
      9  * Renders the `core/categories` block on server.
     10  *
     11  * @param array $attributes The block attributes.
     12  *
     13  * @return string Returns the categories list/dropdown markup.
     14  */
     15 function render_block_core_categories( $attributes ) {
     16 	static $block_id = 0;
     17 	$block_id++;
     18 
     19 	$args = array(
     20 		'echo'         => false,
     21 		'hierarchical' => ! empty( $attributes['showHierarchy'] ),
     22 		'orderby'      => 'name',
     23 		'show_count'   => ! empty( $attributes['showPostCounts'] ),
     24 		'title_li'     => '',
     25 	);
     26 
     27 	if ( ! empty( $attributes['displayAsDropdown'] ) ) {
     28 		$id                       = 'wp-block-categories-' . $block_id;
     29 		$args['id']               = $id;
     30 		$args['show_option_none'] = __( 'Select Category' );
     31 		$wrapper_markup           = '<div %1$s><label class="screen-reader-text" for="' . $id . '">' . __( 'Categories' ) . '</label>%2$s</div>';
     32 		$items_markup             = wp_dropdown_categories( $args );
     33 		$type                     = 'dropdown';
     34 
     35 		if ( ! is_admin() ) {
     36 			// Inject the dropdown script immediately after the select dropdown.
     37 			$items_markup = preg_replace(
     38 				'#(?<=</select>)#',
     39 				build_dropdown_script_block_core_categories( $id ),
     40 				$items_markup,
     41 				1
     42 			);
     43 		}
     44 	} else {
     45 		$wrapper_markup = '<ul %1$s>%2$s</ul>';
     46 		$items_markup   = wp_list_categories( $args );
     47 		$type           = 'list';
     48 	}
     49 
     50 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) );
     51 
     52 	return sprintf(
     53 		$wrapper_markup,
     54 		$wrapper_attributes,
     55 		$items_markup
     56 	);
     57 }
     58 
     59 /**
     60  * Generates the inline script for a categories dropdown field.
     61  *
     62  * @param string $dropdown_id ID of the dropdown field.
     63  *
     64  * @return string Returns the dropdown onChange redirection script.
     65  */
     66 function build_dropdown_script_block_core_categories( $dropdown_id ) {
     67 	ob_start();
     68 	?>
     69 	<script type='text/javascript'>
     70 	/* <![CDATA[ */
     71 	( function() {
     72 		var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' );
     73 		function onCatChange() {
     74 			if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
     75 				location.href = "<?php echo home_url(); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value;
     76 			}
     77 		}
     78 		dropdown.onchange = onCatChange;
     79 	})();
     80 	/* ]]> */
     81 	</script>
     82 	<?php
     83 	return ob_get_clean();
     84 }
     85 
     86 /**
     87  * Registers the `core/categories` block on server.
     88  */
     89 function register_block_core_categories() {
     90 	register_block_type_from_metadata(
     91 		__DIR__ . '/categories',
     92 		array(
     93 			'render_callback' => 'render_block_core_categories',
     94 		)
     95 	);
     96 }
     97 add_action( 'init', 'register_block_core_categories' );