Skip to content
This repository has been archived by the owner on Nov 21, 2023. It is now read-only.

Commit

Permalink
Pass wpdtrt-anchorlinks__additions in as a shortcode attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
dotherightthing committed Jan 3, 2021
1 parent d2d096f commit 35b4ef5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 36 deletions.
35 changes: 1 addition & 34 deletions js/_frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const wpdtrtAnchorlinksUi = {
let anchorTextAbbreviated;
let stickyTitle = '';

if ($anchor.length && $anchor.find(highlightController).length) {
if ($anchor.length && $anchor.is(highlightController)) {
anchorTextAbbreviated = $anchor.find('h2').attr('data-abbreviation');

if (anchorTextAbbreviated) {
Expand Down Expand Up @@ -109,37 +109,6 @@ const wpdtrtAnchorlinksUi = {
}
},

/**
* @function injectListAdditions
* @summary Append theme elements after the list, in the order specified.
* @memberof wpdtrtAnchorlinksUi
* @protected
*
* @example
* <div data-wpdtrt-anchorlinks-list-addition="1" data-wpdtrt-anchorlinks-list-addition-clone="false">Added first</div>
* <div data-wpdtrt-anchorlinks-list-addition="2" data-wpdtrt-anchorlinks-list-addition-clone="true">Added second</div>
*/
injectListAdditions: () => {
const $ = wpdtrtAnchorlinksUi.jQuery;
const $elements = $('[data-wpdtrt-anchorlinks-list-addition]');
const $block = $('.wpdtrt-anchorlinks');

$block.append('<div class="wpdtrt-anchorlinks__additions"></div>');
const $additions = $('.wpdtrt-anchorlinks__additions');

$elements.each((i, item) => {
let id = i + 1;
let $orderedAddition = $elements.filter(`[data-wpdtrt-anchorlinks-list-addition="${id}"]`);
let clone = $orderedAddition.attr('data-wpdtrt-anchorlinks-list-addition-clone');

if (clone === 'true') {
$orderedAddition.clone().appendTo($additions);
} else {
$orderedAddition.appendTo($additions);
}
});
},

/**
* @function setListMaxHeight
* @summary Calculate the height available to the anchor list.
Expand Down Expand Up @@ -362,8 +331,6 @@ const wpdtrtAnchorlinksUi = {
pinController: $('[data-wpdtrt-anchorlinks-controls="pinning"]')
};

wpdtrtAnchorlinksUi.injectListAdditions();

wpdtrtAnchorlinksUi.sticky_jump_menu($('.wpdtrt-anchorlinks'));

console.log('wpdtrtAnchorlinksUi.init'); // eslint-disable-line no-console
Expand Down
13 changes: 11 additions & 2 deletions template-parts/wpdtrt-anchorlinks/content-anchorlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
$after_widget = null; // register_sidebar.

// shortcode options.
$post_id = null; // $post->ID stand-in for unit tests
$title_text = null;
$post_id = null; // $post->ID stand-in for unit tests
$title_text = null;
$additional_html = null;

// access to plugin.
$plugin = null;
Expand All @@ -31,6 +32,10 @@
// @link http://kb.network.dan/php/wordpress/extract/.
extract( $options, EXTR_IF_EXISTS );

// if ( null !== $additional_html ) {
// $additional_html = esc_html( $additional_html );
// }.

global $post;

if ( isset( $post ) && is_object( $post ) ) {
Expand Down Expand Up @@ -58,6 +63,10 @@
<?php endif; ?>
<?php
echo $plugin->render_anchor_list_html( (array) $anchors );

if ( null !== $additional_html ) {
echo "<div class='wpdtrt-anchorlinks__additions'>{$additional_html}</div>";
}
?>
</div>

Expand Down
8 changes: 8 additions & 0 deletions wpdtrt-anchorlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ function wpdtrt_anchorlinks_plugin_init() {
'tip' => __( 'e.g. Outline', 'wpdtrt-anchorlinks' ),
'default' => __( 'Outline', 'wpdtrt-anchorlinks' ),
),
'additional_html' => array(
'type' => 'text',
'size' => 30,
'label' => __( 'Additional HTML', 'wpdtrt-anchorlinks' ),
'tip' => __( 'Added in .wpdtrt-anchorlinks__additions', 'wpdtrt-anchorlinks' ),
'default' => __( '', 'wpdtrt-anchorlinks' ),
)
);

/**
Expand Down Expand Up @@ -297,6 +304,7 @@ function wpdtrt_anchorlinks_shortcode_init() {
'selected_instance_options' => array(
'post_id',
'title_text',
'additional_html',
),
)
);
Expand Down

0 comments on commit 35b4ef5

Please sign in to comment.