Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions inc/plugins/class-dynamic-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function apply_dynamic_content( $content ) {
*/
public static function dynamic_content_regex() {
// Todo: Improve this Regex, it can't go on for like this. Soon it will be longer than the available space in the universe!!!
return '/<o-dynamic(?:\s+(?:data-type=["\'](?P<type>[^"\'<>]+)["\']|data-id=["\'](?P<id>[^"\'<>]+)["\']|data-before=["\'](?P<before>[^"\'<>]+)["\']|data-after=["\'](?P<after>[^"\'<>]+)["\']|data-length=["\'](?P<length>[^"\'<>]+)["\']|data-date-type=["\'](?P<dateType>[^"\'<>]+)["\']|data-date-format=["\'](?P<dateFormat>[^"\'<>]+)["\']|data-date-custom=["\'](?P<dateCustom>[^"\'<>]+)["\']|data-time-type=["\'](?P<timeType>[^"\'<>]+)["\']|data-time-format=["\'](?P<timeFormat>[^"\'<>]+)["\']|data-time-custom=["\'](?P<timeCustom>[^"\'<>]+)["\']|data-term-type=["\'](?P<termType>[^"\'<>]+)["\']|data-term-separator=["\'](?P<termSeparator>[^"\'<>]+)["\']|data-meta-key=["\'](?P<metaKey>[^"\'<>]+)["\']|data-parameter=["\'](?P<parameter>[^"\'<>]+)["\']|data-format=["\'](?P<format>[^"\'<>]+)["\']|data-context=["\'](?P<context>[^"\'<>]+)["\']|data-taxonomy=["\'](?P<taxonomy>[^"\'<>]+)["\']|[a-zA-Z-]+=["\'][^"\'<>]+["\']))*\s*>(?<default>[^ $].*?)<\s*\/\s*o-dynamic>/';
return '/<o-dynamic(?:\s+(?:data-type=["\'](?P<type>[^"\'<>]+)["\']|data-id=["\'](?P<id>[^"\'<>]+)["\']|data-before=["\'](?P<before>[^"\'<>]+)["\']|data-after=["\'](?P<after>[^"\'<>]+)["\']|data-length=["\'](?P<length>[^"\'<>]+)["\']|data-date-type=["\'](?P<dateType>[^"\'<>]+)["\']|data-date-format=["\'](?P<dateFormat>[^"\'<>]+)["\']|data-date-custom=["\'](?P<dateCustom>[^"\'<>]+)["\']|data-time-type=["\'](?P<timeType>[^"\'<>]+)["\']|data-time-format=["\'](?P<timeFormat>[^"\'<>]+)["\']|data-time-custom=["\'](?P<timeCustom>[^"\'<>]+)["\']|data-term-type=["\'](?P<termType>[^"\'<>]+)["\']|data-term-separator=["\'](?P<termSeparator>[^"\'<>]+)["\']|data-meta-key=["\'](?P<metaKey>[^"\'<>]+)["\']|data-parameter=["\'](?P<parameter>[^"\'<>]+)["\']|data-format=["\'](?P<format>[^"\'<>]+)["\']|data-context=["\'](?P<context>[^"\'<>]+)["\']|data-taxonomy=["\'](?P<taxonomy>[^"\'<>]+)["\']|data-hide-prefix=["\'](?P<hidePrefix>[^"\'<>]+)["\']|[a-zA-Z-]+=["\'][^"\'<>]+["\']))*\s*>(?<default>[^ $].*?)<\s*\/\s*o-dynamic>/';
}

/**
Expand Down Expand Up @@ -512,7 +512,7 @@ public function get_data( $data, $magic_tags ) {
}

if ( 'archiveTitle' === $data['type'] ) {
return get_the_archive_title();
return $this->get_archive_title( $data );
}

if ( 'archiveDescription' === $data['type'] ) {
Expand Down Expand Up @@ -644,6 +644,29 @@ public function get_loggedin_email( $data ) {
return esc_html( $email );
}

/**
* Get Archive Title.
*
* @param array<string, mixed> $data Dynamic Data.
*
* @return string
*/
public function get_archive_title( $data ) {
$hide_prefix = isset( $data['hidePrefix'] ) && in_array( $data['hidePrefix'], array( 'true', '1' ), true );

if ( $hide_prefix ) {
add_filter( 'get_the_archive_title_prefix', '__return_empty_string' );
}

$title = get_the_archive_title();

if ( $hide_prefix ) {
remove_filter( 'get_the_archive_title_prefix', '__return_empty_string' );
}

return $title;
}

/**
* Get Archive Description.
*
Expand Down
13 changes: 12 additions & 1 deletion src/blocks/plugins/dynamic-content/value/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
ExternalLink,
SelectControl,
TextControl,
ToggleControl,
PanelBody,
Spinner
} from '@wordpress/components';
Expand All @@ -41,7 +42,8 @@ import { getQueryStringFromObject, setUtm } from '../../../helpers/helper-functi
let hasSettingsPanel = [
'postExcerpt',
'date',
'time'
'time',
'archiveTitle'
];

const dateFormats = {
Expand Down Expand Up @@ -242,6 +244,15 @@ const Fields = ({
</Fragment>
) }

{ 'archiveTitle' === attributes.type && (
<ToggleControl
label={ __( 'Remove Title Prefix', 'otter-blocks' ) }
help={ __( 'Display only the term name, removing prefixes such as "Category:", "Tag:", or "Author:".', 'otter-blocks' ) }
checked={ Boolean( attributes.hidePrefix ) && 'false' !== attributes.hidePrefix }
onChange={ value => changeAttributes({ hidePrefix: value ? '1' : undefined }) }
/>
) }

{ applyFilters( 'otter.dynamicContent.text.controls', '', attributes, changeAttributes ) }
</PanelBody>
) }
Expand Down
5 changes: 3 additions & 2 deletions src/blocks/plugins/dynamic-content/value/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export const format = {
metaKey: 'data-meta-key',
parameter: 'data-parameter',
format: 'data-format',
taxonomy: 'data-taxonomy'
taxonomy: 'data-taxonomy',
hidePrefix: 'data-hide-prefix'
},
edit
};
Expand Down Expand Up @@ -82,7 +83,7 @@ const withDynamicConditions = createHigherOrderComponent( BlockEdit => {

elements.forEach( element => {
const context = select( 'core/editor' ).getCurrentPostId();
const attrs = pick( Object.assign({ context }, element.dataset ), [ 'type', 'context', 'before', 'after', 'length', 'dateType', 'dateFormat', 'dateCustom', 'timeType', 'timeFormat', 'timeCustom', 'termType', 'termSeparator', 'metaKey', 'taxonomy' ]);
const attrs = pick( Object.assign({ context }, element.dataset ), [ 'type', 'context', 'before', 'after', 'length', 'dateType', 'dateFormat', 'dateCustom', 'timeType', 'timeFormat', 'timeCustom', 'termType', 'termSeparator', 'metaKey', 'taxonomy', 'hidePrefix' ]);

if ( 'postContent' === attrs.type ) {
return;
Expand Down
34 changes: 34 additions & 0 deletions tests/test-dynamic-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,40 @@ public function test_archive_title() {
$this->assertEquals( 'archiveTitle', $result['type'] );
}

/**
* Test the Archive Title query with the prefix removed.
*/
public function test_archive_title_hide_prefix() {
$archive_title_query = '<p><o-dynamic data-type="archiveTitle" data-hide-prefix="1">Archive Title</o-dynamic></p>';

$result = array();
$num = Dynamic_Content::parse_dynamic_content_query( $archive_title_query, $result );
$this->assertTrue( boolval( $num ) );
$result = $result[0];

$this->assertEquals( 'archiveTitle', $result['type'] );
$this->assertEquals( '1', $result['hidePrefix'] );
}

/**
* Test the Archive Title evaluation on a category archive.
*/
public function test_archive_title_evaluation() {
$this->go_to( get_term_link( $this->category_id, 'category' ) );

// Default behaviour keeps the prefix.
$with_prefix_query = '<p><o-dynamic data-type="archiveTitle">Archive Title</o-dynamic></p>';
$with_prefix = $this->dynamic_content->apply_dynamic_content( $with_prefix_query );
$this->assertStringContainsString( 'Test Category', $with_prefix );
$this->assertStringContainsString( 'Category:', $with_prefix );

// Enabling the toggle strips the prefix and leaves only the term name.
$no_prefix_query = '<p><o-dynamic data-type="archiveTitle" data-hide-prefix="1">Archive Title</o-dynamic></p>';
$no_prefix = $this->dynamic_content->apply_dynamic_content( $no_prefix_query );
$this->assertEquals( '<p>Test Category</p>', $no_prefix );
$this->assertStringNotContainsString( 'Category:', $no_prefix );
}

/**
* Test the Archive Description query.
*/
Expand Down
Loading