diff --git a/inc/plugins/class-dynamic-content.php b/inc/plugins/class-dynamic-content.php index 2bf17a902..1924d6474 100644 --- a/inc/plugins/class-dynamic-content.php +++ b/inc/plugins/class-dynamic-content.php @@ -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 '/[^"\'<>]+)["\']|data-id=["\'](?P[^"\'<>]+)["\']|data-before=["\'](?P[^"\'<>]+)["\']|data-after=["\'](?P[^"\'<>]+)["\']|data-length=["\'](?P[^"\'<>]+)["\']|data-date-type=["\'](?P[^"\'<>]+)["\']|data-date-format=["\'](?P[^"\'<>]+)["\']|data-date-custom=["\'](?P[^"\'<>]+)["\']|data-time-type=["\'](?P[^"\'<>]+)["\']|data-time-format=["\'](?P[^"\'<>]+)["\']|data-time-custom=["\'](?P[^"\'<>]+)["\']|data-term-type=["\'](?P[^"\'<>]+)["\']|data-term-separator=["\'](?P[^"\'<>]+)["\']|data-meta-key=["\'](?P[^"\'<>]+)["\']|data-parameter=["\'](?P[^"\'<>]+)["\']|data-format=["\'](?P[^"\'<>]+)["\']|data-context=["\'](?P[^"\'<>]+)["\']|data-taxonomy=["\'](?P[^"\'<>]+)["\']|[a-zA-Z-]+=["\'][^"\'<>]+["\']))*\s*>(?[^ $].*?)<\s*\/\s*o-dynamic>/'; + return '/[^"\'<>]+)["\']|data-id=["\'](?P[^"\'<>]+)["\']|data-before=["\'](?P[^"\'<>]+)["\']|data-after=["\'](?P[^"\'<>]+)["\']|data-length=["\'](?P[^"\'<>]+)["\']|data-date-type=["\'](?P[^"\'<>]+)["\']|data-date-format=["\'](?P[^"\'<>]+)["\']|data-date-custom=["\'](?P[^"\'<>]+)["\']|data-time-type=["\'](?P[^"\'<>]+)["\']|data-time-format=["\'](?P[^"\'<>]+)["\']|data-time-custom=["\'](?P[^"\'<>]+)["\']|data-term-type=["\'](?P[^"\'<>]+)["\']|data-term-separator=["\'](?P[^"\'<>]+)["\']|data-meta-key=["\'](?P[^"\'<>]+)["\']|data-parameter=["\'](?P[^"\'<>]+)["\']|data-format=["\'](?P[^"\'<>]+)["\']|data-context=["\'](?P[^"\'<>]+)["\']|data-taxonomy=["\'](?P[^"\'<>]+)["\']|data-hide-prefix=["\'](?P[^"\'<>]+)["\']|[a-zA-Z-]+=["\'][^"\'<>]+["\']))*\s*>(?[^ $].*?)<\s*\/\s*o-dynamic>/'; } /** @@ -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'] ) { @@ -644,6 +644,29 @@ public function get_loggedin_email( $data ) { return esc_html( $email ); } + /** + * Get Archive Title. + * + * @param array $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. * diff --git a/src/blocks/plugins/dynamic-content/value/fields.js b/src/blocks/plugins/dynamic-content/value/fields.js index 8c2110e99..506ed14f9 100644 --- a/src/blocks/plugins/dynamic-content/value/fields.js +++ b/src/blocks/plugins/dynamic-content/value/fields.js @@ -17,6 +17,7 @@ import { ExternalLink, SelectControl, TextControl, + ToggleControl, PanelBody, Spinner } from '@wordpress/components'; @@ -41,7 +42,8 @@ import { getQueryStringFromObject, setUtm } from '../../../helpers/helper-functi let hasSettingsPanel = [ 'postExcerpt', 'date', - 'time' + 'time', + 'archiveTitle' ]; const dateFormats = { @@ -242,6 +244,15 @@ const Fields = ({ ) } + { 'archiveTitle' === attributes.type && ( + changeAttributes({ hidePrefix: value ? '1' : undefined }) } + /> + ) } + { applyFilters( 'otter.dynamicContent.text.controls', '', attributes, changeAttributes ) } ) } diff --git a/src/blocks/plugins/dynamic-content/value/index.js b/src/blocks/plugins/dynamic-content/value/index.js index 70aa99b1b..bbd5e4732 100644 --- a/src/blocks/plugins/dynamic-content/value/index.js +++ b/src/blocks/plugins/dynamic-content/value/index.js @@ -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 }; @@ -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; diff --git a/tests/test-dynamic-content.php b/tests/test-dynamic-content.php index 8b008c72a..7e24ee340 100644 --- a/tests/test-dynamic-content.php +++ b/tests/test-dynamic-content.php @@ -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 = '

Archive Title

'; + + $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 = '

Archive Title

'; + $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 = '

Archive Title

'; + $no_prefix = $this->dynamic_content->apply_dynamic_content( $no_prefix_query ); + $this->assertEquals( '

Test Category

', $no_prefix ); + $this->assertStringNotContainsString( 'Category:', $no_prefix ); + } + /** * Test the Archive Description query. */