Skip to content

Commit 19c0077

Browse files
authored
Merge pull request #2880 from Codeinwp/feat/categ-title-prefix
feat: add support for hiding archive title prefix
2 parents de89b78 + 1c1f0e3 commit 19c0077

4 files changed

Lines changed: 74 additions & 5 deletions

File tree

inc/plugins/class-dynamic-content.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function apply_dynamic_content( $content ) {
9292
*/
9393
public static function dynamic_content_regex() {
9494
// Todo: Improve this Regex, it can't go on for like this. Soon it will be longer than the available space in the universe!!!
95-
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>/';
95+
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>/';
9696
}
9797

9898
/**
@@ -512,7 +512,7 @@ public function get_data( $data, $magic_tags ) {
512512
}
513513

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

518518
if ( 'archiveDescription' === $data['type'] ) {
@@ -644,6 +644,29 @@ public function get_loggedin_email( $data ) {
644644
return esc_html( $email );
645645
}
646646

647+
/**
648+
* Get Archive Title.
649+
*
650+
* @param array<string, mixed> $data Dynamic Data.
651+
*
652+
* @return string
653+
*/
654+
public function get_archive_title( $data ) {
655+
$hide_prefix = isset( $data['hidePrefix'] ) && in_array( $data['hidePrefix'], array( 'true', '1' ), true );
656+
657+
if ( $hide_prefix ) {
658+
add_filter( 'get_the_archive_title_prefix', '__return_empty_string' );
659+
}
660+
661+
$title = get_the_archive_title();
662+
663+
if ( $hide_prefix ) {
664+
remove_filter( 'get_the_archive_title_prefix', '__return_empty_string' );
665+
}
666+
667+
return $title;
668+
}
669+
647670
/**
648671
* Get Archive Description.
649672
*

src/blocks/plugins/dynamic-content/value/fields.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
ExternalLink,
1818
SelectControl,
1919
TextControl,
20+
ToggleControl,
2021
PanelBody,
2122
Spinner
2223
} from '@wordpress/components';
@@ -41,7 +42,8 @@ import { getQueryStringFromObject, setUtm } from '../../../helpers/helper-functi
4142
let hasSettingsPanel = [
4243
'postExcerpt',
4344
'date',
44-
'time'
45+
'time',
46+
'archiveTitle'
4547
];
4648

4749
const dateFormats = {
@@ -242,6 +244,15 @@ const Fields = ({
242244
</Fragment>
243245
) }
244246

247+
{ 'archiveTitle' === attributes.type && (
248+
<ToggleControl
249+
label={ __( 'Remove Title Prefix', 'otter-blocks' ) }
250+
help={ __( 'Display only the term name, removing prefixes such as "Category:", "Tag:", or "Author:".', 'otter-blocks' ) }
251+
checked={ Boolean( attributes.hidePrefix ) && 'false' !== attributes.hidePrefix }
252+
onChange={ value => changeAttributes({ hidePrefix: value ? '1' : undefined }) }
253+
/>
254+
) }
255+
245256
{ applyFilters( 'otter.dynamicContent.text.controls', '', attributes, changeAttributes ) }
246257
</PanelBody>
247258
) }

src/blocks/plugins/dynamic-content/value/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export const format = {
4848
metaKey: 'data-meta-key',
4949
parameter: 'data-parameter',
5050
format: 'data-format',
51-
taxonomy: 'data-taxonomy'
51+
taxonomy: 'data-taxonomy',
52+
hidePrefix: 'data-hide-prefix'
5253
},
5354
edit
5455
};
@@ -82,7 +83,7 @@ const withDynamicConditions = createHigherOrderComponent( BlockEdit => {
8283

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

8788
if ( 'postContent' === attrs.type ) {
8889
return;

tests/test-dynamic-content.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,40 @@ public function test_archive_title() {
412412
$this->assertEquals( 'archiveTitle', $result['type'] );
413413
}
414414

415+
/**
416+
* Test the Archive Title query with the prefix removed.
417+
*/
418+
public function test_archive_title_hide_prefix() {
419+
$archive_title_query = '<p><o-dynamic data-type="archiveTitle" data-hide-prefix="1">Archive Title</o-dynamic></p>';
420+
421+
$result = array();
422+
$num = Dynamic_Content::parse_dynamic_content_query( $archive_title_query, $result );
423+
$this->assertTrue( boolval( $num ) );
424+
$result = $result[0];
425+
426+
$this->assertEquals( 'archiveTitle', $result['type'] );
427+
$this->assertEquals( '1', $result['hidePrefix'] );
428+
}
429+
430+
/**
431+
* Test the Archive Title evaluation on a category archive.
432+
*/
433+
public function test_archive_title_evaluation() {
434+
$this->go_to( get_term_link( $this->category_id, 'category' ) );
435+
436+
// Default behaviour keeps the prefix.
437+
$with_prefix_query = '<p><o-dynamic data-type="archiveTitle">Archive Title</o-dynamic></p>';
438+
$with_prefix = $this->dynamic_content->apply_dynamic_content( $with_prefix_query );
439+
$this->assertStringContainsString( 'Test Category', $with_prefix );
440+
$this->assertStringContainsString( 'Category:', $with_prefix );
441+
442+
// Enabling the toggle strips the prefix and leaves only the term name.
443+
$no_prefix_query = '<p><o-dynamic data-type="archiveTitle" data-hide-prefix="1">Archive Title</o-dynamic></p>';
444+
$no_prefix = $this->dynamic_content->apply_dynamic_content( $no_prefix_query );
445+
$this->assertEquals( '<p>Test Category</p>', $no_prefix );
446+
$this->assertStringNotContainsString( 'Category:', $no_prefix );
447+
}
448+
415449
/**
416450
* Test the Archive Description query.
417451
*/

0 commit comments

Comments
 (0)