diff --git a/CHANGES.md b/CHANGES.md index d227a84..baaab32 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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. diff --git a/assets/css/mai-toc.css b/assets/css/mai-table-of-contents.css similarity index 100% rename from assets/css/mai-toc.css rename to assets/css/mai-table-of-contents.css diff --git a/assets/css/mai-toc.min.css b/assets/css/mai-table-of-contents.min.css similarity index 100% rename from assets/css/mai-toc.min.css rename to assets/css/mai-table-of-contents.min.css diff --git a/blocks/table-of-contents/block.json b/blocks/table-of-contents/block.json new file mode 100644 index 0000000..2e36afd --- /dev/null +++ b/blocks/table-of-contents/block.json @@ -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 + } +} \ No newline at end of file diff --git a/blocks/table-of-contents/block.php b/blocks/table-of-contents/block.php new file mode 100644 index 0000000..808dd7d --- /dev/null +++ b/blocks/table-of-contents/block.php @@ -0,0 +1,121 @@ + $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', + ], + ], + ], + ] + ); +} diff --git a/classes/class-block.php b/classes/class-block.php deleted file mode 100644 index 0a2fdf5..0000000 --- a/classes/class-block.php +++ /dev/null @@ -1,165 +0,0 @@ -hooks(); - } - - /** - * Runs hooks. - * - * @since 0.1.0 - * - * @return void - */ - function hooks() { - add_action( 'acf/init', [ $this, 'register_block' ] ); - add_action( 'acf/init', [ $this, 'register_field_group' ] ); - } - - /** - * Register block. - * - * @since 0.1.0 - * - * @return void - */ - function register_block() { - if ( ! function_exists( 'acf_register_block_type' ) ) { - return; - } - - acf_register_block_type( - [ - 'name' => 'mai-table-of-contents', - 'title' => __( 'Mai Table of Contents', 'mai-table-of-contents' ), - 'description' => __( 'A table of contents block.', 'mai-table-of-contents' ), - 'icon' => 'list-view', - 'category' => 'formatting', - 'keywords' => [ 'table', 'contents', 'toc' ], - 'mode' => 'preview', - 'multiple' => false, - 'render_callback' => [ $this, 'do_toc' ], - 'enqueue_assets' => function() { - if ( is_admin() ) { - $suffix = maitoc_get_suffix(); - wp_enqueue_style( 'mai-table-of-contents', MAI_TABLE_OF_CONTENTS_PLUGIN_URL . "assets/css/mai-toc{$suffix}.css", [], MAI_TABLE_OF_CONTENTS_VERSION ); - } - }, - 'supports' => [ - 'align' => [ 'wide' ], - 'ancher' => true, - ], - ] - ); - } - - /** - * Renders table of contents. - * - * @since 0.1.0 - * - * @return void - */ - function do_toc( $block, $content = '', $is_preview = false ) { - $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(); - } - - /** - * Registers field groups. - * - * @since 0.1.0 - * - * @return void - */ - function register_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', - ], - ], - ], - ] - ); - } -} diff --git a/classes/class-display.php b/classes/class-display.php index 19fe964..29a1800 100644 --- a/classes/class-display.php +++ b/classes/class-display.php @@ -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. @@ -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; } @@ -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(); } @@ -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', [] ); diff --git a/classes/class-table-of-contents.php b/classes/class-table-of-contents.php index 2891c76..c598108 100644 --- a/classes/class-table-of-contents.php +++ b/classes/class-table-of-contents.php @@ -1,7 +1,7 @@ add_styles(); + $html = '
'; - $html .= $this->get_css(); $html .= sprintf( '
', $this->args['open'] ? ' open': '' ); $html .= ''; $html .= ''; @@ -130,9 +132,8 @@ function get_preview() { * @return string */ function get_toc() { - $post_id = $this->post_id; - static $cache = []; + $post_id = $this->post_id; if ( isset( $cache[ $this->post_id ] ) ) { return $cache[ $this->post_id ]; @@ -153,6 +154,9 @@ function get_toc() { return $cache[ $this->post_id ]; } + // Adds inline CSS. + $this->add_styles(); + // Get classes. $classes = 'mai-toc'; @@ -176,8 +180,7 @@ function get_toc() { } // Build HTML. - $html = sprintf( '
', trim( $classes ) ); - $html .= $this->get_css(); + $html = sprintf( '
', trim( $classes ) ); $html .= sprintf( '
', $this->args['open'] ? ' open' : '' ); $html .= ''; $html .= ''; @@ -220,6 +223,22 @@ function get_toc() { return $cache[ $this->post_id ]; } + /** + * Adds inline CSS. + * + * @since 1.5.3 + * + * @return void + */ + function add_styles() { + $suffix = maitoc_get_suffix(); + $url = MAI_TABLE_OF_CONTENTS_PLUGIN_URL . "assets/css/mai-table-of-contents{$suffix}.css"; + $path = MAI_TABLE_OF_CONTENTS_PLUGIN_DIR . "assets/css/mai-table-of-contents{$suffix}.css"; + + wp_enqueue_style( 'mai-table-of-contents', $url ); + wp_style_add_data( 'mai-table-of-contents', 'path', $path ); + } + /** * Gets content as structured data. * This can't be statically cached because a block or shortcode @@ -417,31 +436,4 @@ function get_labels() { return $labels; } - - /** - * Gets toc css link if it hasn't been loaded yet. - * - * @since 1.3.0 - * - * @return string - */ - function get_css() { - static $loaded = false; - - if ( $loaded ) { - return; - } - - $css = ''; - - // if ( ! is_admin() && did_action( 'wp_print_styles' ) ) { - if ( ! is_admin() ) { - $suffix = maitoc_get_suffix(); - $href = MAI_TABLE_OF_CONTENTS_PLUGIN_URL . "assets/css/mai-toc{$suffix}.css"; - $css = sprintf( '', $href ); - $loaded = true; - } - - return $css; - } } diff --git a/composer.lock b/composer.lock index 08d5783..74e3314 100644 --- a/composer.lock +++ b/composer.lock @@ -8,16 +8,16 @@ "packages": [ { "name": "yahnis-elsts/plugin-update-checker", - "version": "v5.1", + "version": "v5.2", "source": { "type": "git", "url": "https://github.com/YahnisElsts/plugin-update-checker.git", - "reference": "48b03e93c9c2587f9276dce00ce2b6d94c1190d2" + "reference": "5a270988c5f76bfdfbbb42cccc7c9627f7dd64d0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/YahnisElsts/plugin-update-checker/zipball/48b03e93c9c2587f9276dce00ce2b6d94c1190d2", - "reference": "48b03e93c9c2587f9276dce00ce2b6d94c1190d2", + "url": "https://api.github.com/repos/YahnisElsts/plugin-update-checker/zipball/5a270988c5f76bfdfbbb42cccc7c9627f7dd64d0", + "reference": "5a270988c5f76bfdfbbb42cccc7c9627f7dd64d0", "shasum": "" }, "require": { @@ -27,7 +27,7 @@ "type": "library", "autoload": { "files": [ - "load-v5p1.php" + "load-v5p2.php" ] }, "notification-url": "https://packagist.org/downloads/", @@ -52,9 +52,9 @@ ], "support": { "issues": "https://github.com/YahnisElsts/plugin-update-checker/issues", - "source": "https://github.com/YahnisElsts/plugin-update-checker/tree/v5.1" + "source": "https://github.com/YahnisElsts/plugin-update-checker/tree/v5.2" }, - "time": "2023-05-20T11:55:43+00:00" + "time": "2023-08-17T12:44:32+00:00" } ], "packages-dev": [], diff --git a/mai-table-of-contents.php b/mai-table-of-contents.php index 7d83bd6..bb7b8d0 100644 --- a/mai-table-of-contents.php +++ b/mai-table-of-contents.php @@ -140,6 +140,8 @@ private function includes() { foreach ( glob( MAI_TABLE_OF_CONTENTS_INCLUDES_DIR . '*.php' ) as $file ) { include $file; } // Classes. foreach ( glob( MAI_TABLE_OF_CONTENTS_CLASSES_DIR . '*.php' ) as $file ) { include $file; } + // Blocks. + include MAI_TABLE_OF_CONTENTS_PLUGIN_DIR . 'blocks/table-of-contents/block.php'; } /** @@ -198,7 +200,6 @@ public function run() { } new Mai_Table_Of_Contents_Settings; - new Mai_Table_Of_Contents_Block; new Mai_Table_Of_Contents_Display; } } diff --git a/vendor/composer/autoload_files.php b/vendor/composer/autoload_files.php index a9b255d..2874378 100644 --- a/vendor/composer/autoload_files.php +++ b/vendor/composer/autoload_files.php @@ -6,5 +6,5 @@ $baseDir = dirname($vendorDir); return array( - '9b77ddcfb14408a32f5aaf74e0a11694' => $vendorDir . '/yahnis-elsts/plugin-update-checker/load-v5p1.php', + 'efd9d646f43178e7ba3f07758c02ce1d' => $vendorDir . '/yahnis-elsts/plugin-update-checker/load-v5p2.php', ); diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 7e44255..54a788e 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -7,7 +7,7 @@ class ComposerStaticInit1782162e12bcf11213202aa27e2616cd { public static $files = array ( - '9b77ddcfb14408a32f5aaf74e0a11694' => __DIR__ . '/..' . '/yahnis-elsts/plugin-update-checker/load-v5p1.php', + 'efd9d646f43178e7ba3f07758c02ce1d' => __DIR__ . '/..' . '/yahnis-elsts/plugin-update-checker/load-v5p2.php', ); public static $classMap = array ( diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 48d679d..68dc7f2 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -2,29 +2,29 @@ "packages": [ { "name": "yahnis-elsts/plugin-update-checker", - "version": "v5.1", - "version_normalized": "5.1.0.0", + "version": "v5.2", + "version_normalized": "5.2.0.0", "source": { "type": "git", "url": "https://github.com/YahnisElsts/plugin-update-checker.git", - "reference": "48b03e93c9c2587f9276dce00ce2b6d94c1190d2" + "reference": "5a270988c5f76bfdfbbb42cccc7c9627f7dd64d0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/YahnisElsts/plugin-update-checker/zipball/48b03e93c9c2587f9276dce00ce2b6d94c1190d2", - "reference": "48b03e93c9c2587f9276dce00ce2b6d94c1190d2", + "url": "https://api.github.com/repos/YahnisElsts/plugin-update-checker/zipball/5a270988c5f76bfdfbbb42cccc7c9627f7dd64d0", + "reference": "5a270988c5f76bfdfbbb42cccc7c9627f7dd64d0", "shasum": "" }, "require": { "ext-json": "*", "php": ">=5.6.20" }, - "time": "2023-05-20T11:55:43+00:00", + "time": "2023-08-17T12:44:32+00:00", "type": "library", "installation-source": "dist", "autoload": { "files": [ - "load-v5p1.php" + "load-v5p2.php" ] }, "notification-url": "https://packagist.org/downloads/", @@ -49,7 +49,7 @@ ], "support": { "issues": "https://github.com/YahnisElsts/plugin-update-checker/issues", - "source": "https://github.com/YahnisElsts/plugin-update-checker/tree/v5.1" + "source": "https://github.com/YahnisElsts/plugin-update-checker/tree/v5.2" }, "install-path": "../yahnis-elsts/plugin-update-checker" } diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index d43598d..6971c33 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -3,7 +3,7 @@ 'name' => '__root__', 'pretty_version' => 'dev-develop', 'version' => 'dev-develop', - 'reference' => 'd85d6f58ef5e011135e6e304f79b2527c17c2935', + 'reference' => '8630a81cb04dd13343bc8135de13c434933a6006', 'type' => 'library', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), @@ -13,16 +13,16 @@ '__root__' => array( 'pretty_version' => 'dev-develop', 'version' => 'dev-develop', - 'reference' => 'd85d6f58ef5e011135e6e304f79b2527c17c2935', + 'reference' => '8630a81cb04dd13343bc8135de13c434933a6006', 'type' => 'library', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), 'dev_requirement' => false, ), 'yahnis-elsts/plugin-update-checker' => array( - 'pretty_version' => 'v5.1', - 'version' => '5.1.0.0', - 'reference' => '48b03e93c9c2587f9276dce00ce2b6d94c1190d2', + 'pretty_version' => 'v5.2', + 'version' => '5.2.0.0', + 'reference' => '5a270988c5f76bfdfbbb42cccc7c9627f7dd64d0', 'type' => 'library', 'install_path' => __DIR__ . '/../yahnis-elsts/plugin-update-checker', 'aliases' => array(), diff --git a/vendor/yahnis-elsts/plugin-update-checker/Puc/v5/PucFactory.php b/vendor/yahnis-elsts/plugin-update-checker/Puc/v5/PucFactory.php index 3cda059..c2e1243 100644 --- a/vendor/yahnis-elsts/plugin-update-checker/Puc/v5/PucFactory.php +++ b/vendor/yahnis-elsts/plugin-update-checker/Puc/v5/PucFactory.php @@ -4,7 +4,7 @@ if ( !class_exists(PucFactory::class, false) ): - class PucFactory extends \YahnisElsts\PluginUpdateChecker\v5p1\PucFactory { + class PucFactory extends \YahnisElsts\PluginUpdateChecker\v5p2\PucFactory { } endif; diff --git a/vendor/yahnis-elsts/plugin-update-checker/Puc/v5p1/Autoloader.php b/vendor/yahnis-elsts/plugin-update-checker/Puc/v5p2/Autoloader.php similarity index 98% rename from vendor/yahnis-elsts/plugin-update-checker/Puc/v5p1/Autoloader.php rename to vendor/yahnis-elsts/plugin-update-checker/Puc/v5p2/Autoloader.php index ecdede9..87e734d 100644 --- a/vendor/yahnis-elsts/plugin-update-checker/Puc/v5p1/Autoloader.php +++ b/vendor/yahnis-elsts/plugin-update-checker/Puc/v5p2/Autoloader.php @@ -1,6 +1,6 @@ + */ + protected $extraProperties = array(); /** * Create an instance of this class from a JSON document. @@ -135,6 +141,22 @@ protected function getFieldNames() { protected function getPrefixedFilter($tag) { return 'puc_' . $tag; } + + public function __set($name, $value) { + $this->extraProperties[$name] = $value; + } + + public function __get($name) { + return isset($this->extraProperties[$name]) ? $this->extraProperties[$name] : null; + } + + public function __isset($name) { + return isset($this->extraProperties[$name]); + } + + public function __unset($name) { + unset($this->extraProperties[$name]); + } } endif; diff --git a/vendor/yahnis-elsts/plugin-update-checker/Puc/v5p1/OAuthSignature.php b/vendor/yahnis-elsts/plugin-update-checker/Puc/v5p2/OAuthSignature.php similarity index 98% rename from vendor/yahnis-elsts/plugin-update-checker/Puc/v5p1/OAuthSignature.php rename to vendor/yahnis-elsts/plugin-update-checker/Puc/v5p2/OAuthSignature.php index 578bc72..b92eb15 100644 --- a/vendor/yahnis-elsts/plugin-update-checker/Puc/v5p1/OAuthSignature.php +++ b/vendor/yahnis-elsts/plugin-update-checker/Puc/v5p2/OAuthSignature.php @@ -1,5 +1,5 @@ $pucVersionedClass ) { - MajorFactory::addVersion($pucGeneralClass, $pucVersionedClass, '5.1'); + MajorFactory::addVersion($pucGeneralClass, $pucVersionedClass, '5.2'); //Also add it to the minor-version factory in case the major-version factory //was already defined by another, older version of the update checker. - MinorFactory::addVersion($pucGeneralClass, $pucVersionedClass, '5.1'); + MinorFactory::addVersion($pucGeneralClass, $pucVersionedClass, '5.2'); } diff --git a/vendor/yahnis-elsts/plugin-update-checker/plugin-update-checker.php b/vendor/yahnis-elsts/plugin-update-checker/plugin-update-checker.php index ebf10bc..98f3af8 100644 --- a/vendor/yahnis-elsts/plugin-update-checker/plugin-update-checker.php +++ b/vendor/yahnis-elsts/plugin-update-checker/plugin-update-checker.php @@ -1,10 +1,10 @@