Skip to content

Commit

Permalink
Merge branch 'release/1.5.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
JiveDig committed Oct 17, 2023
2 parents f95be39 + 8c1da3e commit e9274a6
Show file tree
Hide file tree
Showing 56 changed files with 319 additions and 321 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 1.5.3 (10/17/23)
* Added: [Developers] New `mai_table_of_contents_has_custom` filter to declare when you're rendering a TOC via custom display methods.
* Changed: Register TOC block with block.json.
* Changed: Update the updater.

## 1.5.2 (6/28/23)
* Changed: Update the updater.
* Fixed: TOC block and shortcode not respecting the minimum headings count.
Expand Down
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions blocks/table-of-contents/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "acf/mai-table-of-contents",
"title": "Mai Table of Contents",
"description": "A table of contents block.",
"category": "formatting",
"keywords": [ "talbe", "contents", "toc" ],
"icon": "list-view",
"textdomain": "mai-table-of-contents",
"editorStyle": "file:../../assets/css/mai-toc.min.css",
"acf": {
"mode": "preview",
"renderCallback": "mai_do_toc_block"
},
"supports": {
"align": [ "wide" ],
"anchor": true
}
}
121 changes: 121 additions & 0 deletions blocks/table-of-contents/block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

// Prevent direct file access.
defined( 'ABSPATH' ) || die;

add_action( 'acf/init', 'mai_register_toc_block' );
/**
* Register block.
*
* @since 0.1.0
*
* @return void
*/
function mai_register_toc_block() {
register_block_type( __DIR__ . '/block.json' );
}

/**
* Callback function to render the block.
*
* @since 0.1.0
*
* @param array $block The block settings and attributes.
* @param string $content The block inner HTML (empty).
* @param bool $is_preview True during AJAX preview.
* @param int $post_id The post ID this block is saved to.
*
* @return void
*/
function mai_do_toc_block( $block, $content = '', $is_preview = false, $post_id = 0 ) {
$custom = get_field( 'maitoc_custom' );
$args = [
'preview' => $is_preview,
'align' => $block['align'],
'class' => isset( $block['className'] ) && $block['className'] ? $block['className'] : '',
];

if ( $custom ) {
$args['open'] = get_field( 'maitoc_open' );
$args['headings'] = get_field( 'maitoc_headings' );
}

$toc = new Mai_Table_Of_Contents( $args );

echo $toc->get();
}

add_action( 'acf/init', 'mai_register_toc_field_group' );
/**
* Registers field groups.
*
* @since 0.1.0
*
* @return void
*/
function mai_register_toc_field_group() {
if ( ! function_exists( 'acf_add_local_field_group' ) ) {
return;
}

acf_add_local_field_group(
[
'key' => 'maitoc_table_of_contents_block',
'title' => __( 'Table of Contents', 'mai-table-of-contents' ),
'fields' => [
[
'key' => 'field_5dd59fad35b30',
'label' => __( 'Override default settings', 'mai-table-of-contents' ),
'name' => 'maitoc_custom',
'type' => 'true_false',
'message' => __( 'Use custom settings', 'mai-table-of-contents' ),
],
[
'key' => 'field_5dd5a09a56ef9',
'label' => __( 'Load Open/Closed', 'mai-table-of-contents' ),
'name' => 'maitoc_open',
'type' => 'true_false',
'default_value' => 1,
'ui' => 1,
'ui_off_text' => __( 'Closed', 'mai-table-of-contents' ),
'ui_on_text' => __( 'Open', 'mai-table-of-contents' ),
'conditional_logic' => [
[
[
'field' => 'field_5dd59fad35b30',
'operator' => '==',
'value' => '1',
],
],
],
],
[
'key' => 'field_5dd5a0d956efa',
'label' => __( 'Minimum Headings', 'mai-table-of-contents' ),
'name' => 'maitoc_headings',
'type' => 'number',
'default_value' => 2,
'step' => 1,
'conditional_logic' => [
[
[
'field' => 'field_5dd59fad35b30',
'operator' => '==',
'value' => '1',
],
],
],
],
],
'location' => [
[
[
'param' => 'block',
'operator' => '==',
'value' => 'acf/mai-table-of-contents',
],
],
],
]
);
}
165 changes: 0 additions & 165 deletions classes/class-block.php

This file was deleted.

16 changes: 10 additions & 6 deletions classes/class-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

class Mai_Table_Of_Contents_Display {
protected $post_id;
protected $has_toc;
protected $has_default;
protected $has_block;
protected $has_shortcode;
protected $has_native;
protected $has_custom;

/**
* Gets it started.
Expand Down Expand Up @@ -78,13 +80,15 @@ function filter_content( $content ) {
return $content;
}

// Check if we have a block or shortcode.
$this->has_toc = $this->has_toc();
// Check if we have a default, block, or shortcode.
$this->has_default = $this->has_default();
$this->has_block = $this->has_block();
$this->has_shortcode = $this->has_shortcode( $content );
$this->has_native = $this->has_default || $this->has_block || $this->has_shortcode;
$this->has_custom = apply_filters( 'mai_table_of_contents_has_custom', false, $this->post_id );

// Bail if no TOC.
if ( ! ( $this->has_toc || $this->has_block || $this->has_shortcode ) ) {
if ( ! ( $this->has_native || $this->has_custom ) ) {
return $content;
}

Expand All @@ -93,7 +97,7 @@ function filter_content( $content ) {
$toc = new Mai_Table_Of_Contents( [], $this->post_id, $content );

// If no block or shortcode in the content, add TOC.
if ( ! ( $this->has_block || $this->has_shortcode ) ) {
if ( ! ( $this->has_block || $this->has_shortcode || $this->has_custom ) ) {
$html .= $toc->get();
}

Expand All @@ -110,7 +114,7 @@ function filter_content( $content ) {
*
* @return bool
*/
function has_toc() {
function has_default() {
// Get post_types (with ACF strange key).
$post_types = (array) get_option( 'options_maitoc_post_types', [] );

Expand Down
Loading

0 comments on commit e9274a6

Please sign in to comment.