Skip to content

Commit

Permalink
Merge pull request #5616 from google/enhancement/5450-tag-placement-r…
Browse files Browse the repository at this point in the history
…efactoring

Enhancement/5450 tag placement refactoring
  • Loading branch information
felixarntz authored Jul 27, 2022
2 parents a831f8c + 4fde6b9 commit cdbed1a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
1 change: 0 additions & 1 deletion includes/Modules/Thank_With_Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ private function register_tag() {
$tag->use_guard( new Tag_Environment_Type_Guard() );

if ( $tag->can_register() ) {
$tag->set_publication_id( $settings['publicationID'] );
$tag->set_button_placement( $settings['buttonPlacement'] );
$tag->set_button_post_types( $settings['buttonPostTypes'] );

Expand Down
12 changes: 6 additions & 6 deletions includes/Modules/Thank_With_Google/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ private function sanitize_button_placement( $button_placement ) {
if ( in_array(
$button_placement,
array(
'dynamic_low',
'dynamic_high',
'static_auto',
'static_above-content',
'static_below-content',
'static_below-first-paragraph',
Web_Tag::PLACEMENT_DYNAMIC_LOW,
Web_Tag::PLACEMENT_DYNAMIC_HIGH,
Web_Tag::PLACEMENT_STATIC_AUTO,
Web_Tag::PLACEMENT_STATIC_ABOVE_CONTENT,
Web_Tag::PLACEMENT_STATIC_BELOW_CONTENT,
Web_Tag::PLACEMENT_STATIC_AFTER_1ST_P,
),
true
) ) {
Expand Down
26 changes: 11 additions & 15 deletions includes/Modules/Thank_With_Google/Web_Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class Web_Tag extends Module_Web_Tag {

use Method_Proxy_Trait, Tag_With_DNS_Prefetch_Trait;

const PLACEMENT_DYNAMIC_LOW = 'dynamic_low';
const PLACEMENT_DYNAMIC_HIGH = 'dynamic_high';
const PLACEMENT_STATIC_AUTO = 'static_auto';
const PLACEMENT_STATIC_ABOVE_CONTENT = 'static_above-content';
const PLACEMENT_STATIC_BELOW_CONTENT = 'static_below-content';
const PLACEMENT_STATIC_AFTER_1ST_P = 'static_below-first-paragraph';

/**
* Publication ID.
*
Expand All @@ -49,17 +56,6 @@ class Web_Tag extends Module_Web_Tag {
*/
private $button_post_types;

/**
* Sets the current publication ID.
*
* @since n.e.x.t
*
* @param string $publication_id Publication ID.
*/
public function set_publication_id( $publication_id ) {
$this->publication_id = $publication_id;
}

/**
* Sets the current button placement.
*
Expand Down Expand Up @@ -133,7 +129,7 @@ protected function enqueue_twg_script() {
});
});
",
esc_js( $this->publication_id ),
esc_js( $this->tag_id ),
esc_js( $this->has_static_button_placement() ? 'inline' : 'floating' ),
esc_js( $is_singular_button_post_type_entity ? get_permalink() : '' ),
esc_js( GOOGLESITEKIT_VERSION ),
Expand Down Expand Up @@ -179,11 +175,11 @@ protected function update_the_content( $content ) {
return $button_placeholder;
}

if ( in_array( $this->button_placement, array( 'static_auto', 'static_below-content' ), true ) ) {
if ( in_array( $this->button_placement, array( self::PLACEMENT_STATIC_AUTO, self::PLACEMENT_STATIC_BELOW_CONTENT ), true ) ) {
$content = $content . $button_placeholder;
} elseif ( 'static_above-content' === $this->button_placement ) {
} elseif ( self::PLACEMENT_STATIC_ABOVE_CONTENT === $this->button_placement ) {
$content = $button_placeholder . $content;
} elseif ( 'static_below-first-paragraph' === $this->button_placement ) {
} elseif ( self::PLACEMENT_STATIC_AFTER_1ST_P === $this->button_placement ) {
$content = substr_replace( $content, $button_placeholder, strpos( $content, '</p>' ) + 4, 0 ); // strlen( '</p>' ) is 4.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ public function set_up() {
parent::set_up();

$this->web_tag = new Web_Tag( '12345', Thank_With_Google::MODULE_SLUG );

$this->web_tag->set_publication_id( '12345' );
$this->web_tag->set_button_post_types( array( 'post' ) );

remove_all_filters( 'the_content' );
$this->web_tag->register();
}

public function test_content_placeholder_not_inserted_on_unselected_button_post_types() {
$this->web_tag->set_button_placement( 'static_auto' );
$this->web_tag->set_button_placement( Web_Tag::PLACEMENT_STATIC_AUTO );

$post_ID = $this->factory()->post->create( array( 'post_type' => 'page' ) );
$this->go_to( get_permalink( $post_ID ) );
Expand All @@ -53,7 +51,7 @@ public function test_content_placeholder_not_inserted_on_unselected_button_post_
}

public function test_content_placeholder_inserted_on_button_post_types() {
$this->web_tag->set_button_placement( 'static_auto' );
$this->web_tag->set_button_placement( Web_Tag::PLACEMENT_STATIC_AUTO );

$this->create_post_and_go_to_it();

Expand All @@ -64,7 +62,7 @@ public function test_content_placeholder_inserted_on_button_post_types() {
}

public function test_content_placeholder_not_inserted_on_dynamic_button_placement() {
$this->web_tag->set_button_placement( 'dynamic_low' );
$this->web_tag->set_button_placement( Web_Tag::PLACEMENT_DYNAMIC_LOW );

$this->create_post_and_go_to_it();

Expand All @@ -75,7 +73,7 @@ public function test_content_placeholder_not_inserted_on_dynamic_button_placemen
}

public function test_content_placeholder_inserted_static_below_content() {
$this->web_tag->set_button_placement( 'static_below-content' );
$this->web_tag->set_button_placement( Web_Tag::PLACEMENT_STATIC_BELOW_CONTENT );

$this->create_post_and_go_to_it();

Expand All @@ -87,7 +85,7 @@ public function test_content_placeholder_inserted_static_below_content() {
}

public function test_content_placeholder_inserted_static_above_content() {
$this->web_tag->set_button_placement( 'static_above-content' );
$this->web_tag->set_button_placement( Web_Tag::PLACEMENT_STATIC_ABOVE_CONTENT );

$this->create_post_and_go_to_it();

Expand All @@ -99,7 +97,7 @@ public function test_content_placeholder_inserted_static_above_content() {
}

public function test_content_placeholder_inserted_static_below_first_paragraph() {
$this->web_tag->set_button_placement( 'static_below-first-paragraph' );
$this->web_tag->set_button_placement( Web_Tag::PLACEMENT_STATIC_AFTER_1ST_P );

$this->create_post_and_go_to_it();

Expand Down

0 comments on commit cdbed1a

Please sign in to comment.