Skip to content

Commit

Permalink
Fixing #17: Add classes to content blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
RadoslavGeorgiev committed Feb 24, 2017
1 parent bec830b commit e60ff8c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion classes/abstract-class-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function offsetGet( $offset ) {
$value = $this->data[ $offset ];
if( method_exists( $this, 'map' ) ) {
$map = rila_dot_to_array( $this->map() );
return Meta::map( $value, $offset, $map );
return Meta::map( $value, $offset, $map );
} else {
return $value;
}
Expand Down
48 changes: 45 additions & 3 deletions classes/class-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,57 @@ public function count() {
*/
public function render() {
$this->init();
$out = '';
$out = '';
$of_type = array();

foreach( $this->blocks as $block ) {
foreach( $this->blocks as $index => $block ) {
$simplified = rila_cleanup_class( get_class( $block ), 'Block' );
$simplified = strtolower( $simplified );

# Remove misc. characters
$simplified = preg_replace( '~[_\\\\\s]~', '-', $simplified );

# Generate the appropriate index for the type
if( isset( $of_type[ $simplified ] ) ) {
$type_index = ++$of_type[ $simplified ];
} else {
$type_index = $of_type[ $simplified ] = 0;
}

if( method_exists( $block, 'get_css_class' ) ) {
/**
* Allows the CSS class of the block to be generated by the block itself.
*
* @since 0.11
*
* @param int $index The index of the block (starting with 0).
* @param int $type_index The index of the specific blocks' type.
* @return string A full CSS class.
*/
$css_class = $block->get_css_class( $index, $of_type[ $simplified ] );
} else {
$classes = array(
'block',
'block-' . $simplified,
'block-' . ( $index + 1 )
);

$css_class = implode( ' ', $classes );
}

/**
* Allows the CSS class of a block to be modified.
*
* @since 0.11
*
* @param string $css_class The already-generated CSS class.
* @param Rila\Block $block The block whose class is being generated.
* @param int $index The zero-based index of the block.
* @param int $of_type The zero-based occurence of the blocks' type.
* @return string
*/
$css_class = apply_filters( 'rila.builder.block_class', $css_class, $block, $index, $of_type[ $simplified ] );

/**
* Allows the opening element for a block to be modified.
*
Expand All @@ -189,7 +231,7 @@ public function render() {
* @param Block $block The block and it's data.
* @return string
*/
$before = apply_filters( 'rila.builder.before_block', '<div class="block block-' . $simplified . '">', $block );
$before = apply_filters( 'rila.builder.before_block', '<div class="' . $css_class . '">', $block );

/**
* Allows the closing element for a block to be modified.
Expand Down

0 comments on commit e60ff8c

Please sign in to comment.