Skip to content
Closed
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
31 changes: 29 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-archive-title-override-prefix=["\'](?P<archiveTitleOverridePrefix>[^"\'<>]+)["\']|data-archive-title-prefix=["\'](?P<archiveTitlePrefix>[^"\'<>]*)["\']|[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 );
}
Comment thread
girishpanchal30 marked this conversation as resolved.

if ( 'archiveDescription' === $data['type'] ) {
Expand Down Expand Up @@ -644,6 +644,33 @@ 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 ) {
$override = isset( $data['archiveTitleOverridePrefix'] ) ? $data['archiveTitleOverridePrefix'] : '';

if ( 'true' !== $override && true !== $override && '1' !== $override ) {
return get_the_archive_title();
}

$custom_prefix = isset( $data['archiveTitlePrefix'] ) ? sanitize_text_field( (string) $data['archiveTitlePrefix'] ) : '';

$filter = function ( $prefix ) use ( $custom_prefix ) {
return $custom_prefix;
};

add_filter( 'get_the_archive_title_prefix', $filter );
$title = get_the_archive_title();
remove_filter( 'get_the_archive_title_prefix', $filter );

return $title;
}

/**
* Get Archive Description.
*
Expand Down
27 changes: 26 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,29 @@ const Fields = ({
</Fragment>
) }

{ 'archiveTitle' === attributes.type && (
<Fragment>
<ToggleControl
label={ __( 'Override Archive Title Prefix', 'otter-blocks' ) }
help={ __( 'Replace the default WordPress archive title prefix (e.g. "Category:") with a custom value, or leave the field empty to remove the prefix entirely.', 'otter-blocks' ) }
checked={ 'true' === attributes.archiveTitleOverridePrefix }
onChange={ () => changeAttributes({
archiveTitleOverridePrefix: 'true' === attributes.archiveTitleOverridePrefix ? undefined : 'true',
archiveTitlePrefix: 'true' === attributes.archiveTitleOverridePrefix ? undefined : attributes.archiveTitlePrefix
}) }
/>

{ 'true' === attributes.archiveTitleOverridePrefix && (
<TextControl
label={ __( 'Archive Title Prefix', 'otter-blocks' ) }
type="text"
value={ attributes.archiveTitlePrefix || '' }
onChange={ archiveTitlePrefix => changeAttributes({ archiveTitlePrefix }) }
/>
) }
</Fragment>
) }

{ applyFilters( 'otter.dynamicContent.text.controls', '', attributes, changeAttributes ) }
</PanelBody>
) }
Expand Down
6 changes: 4 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,9 @@ export const format = {
metaKey: 'data-meta-key',
parameter: 'data-parameter',
format: 'data-format',
taxonomy: 'data-taxonomy'
taxonomy: 'data-taxonomy',
archiveTitleOverridePrefix: 'data-archive-title-override-prefix',
archiveTitlePrefix: 'data-archive-title-prefix'
},
edit
};
Expand Down Expand Up @@ -82,7 +84,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', 'archiveTitleOverridePrefix', 'archiveTitlePrefix' ]);

if ( 'postContent' === attrs.type ) {
return;
Expand Down
122 changes: 122 additions & 0 deletions tests/test-dynamic-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ class TestDynamicContent extends WP_UnitTestCase
*/
protected $post_id;

/**
* The test category ID.
*
* @var int
*/
protected $category_id;

/**
* The test category slug.
*
* @var string
*/
protected $category_slug;

/**
* Set up the test.
*/
Expand Down Expand Up @@ -412,6 +426,38 @@ public function test_archive_title() {
$this->assertEquals( 'archiveTitle', $result['type'] );
}

/**
* Test the Archive Title query with the prefix override attributes.
*/
public function test_archive_title_with_prefix_override() {
$archive_title_query = '<p><o-dynamic data-type="archiveTitle" data-archive-title-override-prefix="true" data-archive-title-prefix="Topic:">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( 'true', $result['archiveTitleOverridePrefix'] );
$this->assertEquals( 'Topic:', $result['archiveTitlePrefix'] );
}

/**
* Test the Archive Title query with the override flag enabled and an empty prefix.
*/
public function test_archive_title_with_empty_prefix_override() {
$archive_title_query = '<p><o-dynamic data-type="archiveTitle" data-archive-title-override-prefix="true" data-archive-title-prefix="">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( 'true', $result['archiveTitleOverridePrefix'] );
$this->assertSame( '', $result['archiveTitlePrefix'] );
}

/**
* Test the Archive Description query.
*/
Expand Down Expand Up @@ -656,6 +702,82 @@ public function test_author_description_evaluation() {

$this->assertEquals( '<p></p>', $result );
}

/**
* Test the Archive Title evaluation with the default WordPress prefix.
*/
public function test_archive_title_evaluation_default_prefix() {
// Navigate to the category archive so get_the_archive_title() resolves.
$this->go_to( get_category_link( $this->category_id ) );

$archive_title_query = '<p><o-dynamic data-type="archiveTitle">Archive Title</o-dynamic></p>';
$result = $this->dynamic_content->apply_dynamic_content( $archive_title_query );

// Default WordPress prefix for category archives must be preserved.
$this->assertStringContainsString( 'Category:', $result );
$this->assertStringContainsString( 'Test Category', $result );
}

/**
* Test the Archive Title evaluation when the override flag is enabled with a custom prefix.
*/
public function test_archive_title_evaluation_with_custom_prefix() {
// Navigate to the category archive so get_the_archive_title() resolves.
$this->go_to( get_category_link( $this->category_id ) );

$archive_title_query = '<p><o-dynamic data-type="archiveTitle" data-archive-title-override-prefix="true" data-archive-title-prefix="Topic:">Archive Title</o-dynamic></p>';
$result = $this->dynamic_content->apply_dynamic_content( $archive_title_query );

// Custom prefix should replace the default "Category:" prefix.
$this->assertStringContainsString( 'Topic:', $result );
$this->assertStringNotContainsString( 'Category:', $result );
$this->assertStringContainsString( 'Test Category', $result );
}

/**
* Test the Archive Title evaluation when the override flag is enabled with an empty prefix.
*/
public function test_archive_title_evaluation_with_empty_prefix() {
// Navigate to the category archive so get_the_archive_title() resolves.
$this->go_to( get_category_link( $this->category_id ) );

$archive_title_query = '<p><o-dynamic data-type="archiveTitle" data-archive-title-override-prefix="true" data-archive-title-prefix="">Archive Title</o-dynamic></p>';
$result = $this->dynamic_content->apply_dynamic_content( $archive_title_query );

// Empty prefix should strip the default WordPress prefix entirely.
$this->assertStringNotContainsString( 'Category:', $result );
$this->assertStringContainsString( 'Test Category', $result );
}

/**
* Test that the Archive Title prefix filter is removed after the dynamic value is rendered.
*/
public function test_archive_title_evaluation_does_not_leak_filter() {
// Navigate to the category archive so get_the_archive_title() resolves.
$this->go_to( get_category_link( $this->category_id ) );

$archive_title_query = '<p><o-dynamic data-type="archiveTitle" data-archive-title-override-prefix="true" data-archive-title-prefix="Topic:">Archive Title</o-dynamic></p>';
$this->dynamic_content->apply_dynamic_content( $archive_title_query );

// After rendering, a subsequent call to get_the_archive_title() must return
// the default WordPress prefix (proving the temporary filter was removed).
$this->assertStringContainsString( 'Category:', get_the_archive_title() );
}

/**
* Test that the Archive Title is unchanged when the override flag is not "true".
*/
public function test_archive_title_evaluation_override_flag_off() {
// Navigate to the category archive so get_the_archive_title() resolves.
$this->go_to( get_category_link( $this->category_id ) );

// The override flag is missing here, so the custom prefix must be ignored.
$archive_title_query = '<p><o-dynamic data-type="archiveTitle" data-archive-title-prefix="Topic:">Archive Title</o-dynamic></p>';
$result = $this->dynamic_content->apply_dynamic_content( $archive_title_query );

$this->assertStringContainsString( 'Category:', $result );
$this->assertStringNotContainsString( 'Topic:', $result );
}

/**
* Test multiple dynamic content queries.
Expand Down
Loading