Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WordPress側の情報をmetaタグに出力する #56

Merged
merged 6 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion assets/js/ga-communicator-setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*!
* Package screen helper.
*
* @deps wp-api-fetch, jquery, wp-i18n, ga-custom-dimensions, ga-sandbox
* @deps wp-api-fetch, jquery, wp-i18n, ga-sandbox
*/

const $ = jQuery;
Expand Down
1 change: 1 addition & 0 deletions assets/js/ga-custom-dimensions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*!
* Custom dimensions.
*
* @deprecated 3.6.0
* @package ga-communicator
* @handle ga-custom-dimensions
* @deps wp-api-fetch, wp-element, wp-components, wp-i18n
Expand Down
2 changes: 1 addition & 1 deletion dist/css/ga-communicator-setting.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/css/map/ga-communicator-setting.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/Kunoichi/GaCommunicator/Api/Accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Rest API.
*
* @package ga-communicator
* @deprecated 3.5.0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The class Accounts is marked as deprecated. Ensure that all references to this class in the codebase are updated or removed to avoid future issues. If this class is part of a public API, make sure to communicate this change properly to the users and provide them with an alternative if possible.

-  * @deprecated 3.5.0
+  * @deprecated 3.5.0 Use NewClass instead.

This comment assumes that NewClass is the new class replacing Accounts. Replace NewClass with the actual class name if it's different.

*/
class Accounts extends RestApiOptions {

Expand Down
1 change: 1 addition & 0 deletions src/Kunoichi/GaCommunicator/Api/Profiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Profiles list.
*
* @package ga-communicator
* @deprecated 3.5.0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The class Profiles has been marked as deprecated. Ensure that all references to this class in the codebase are updated or removed to prevent future issues. If this class is part of a public API, make sure to communicate this change properly to the users and provide them with an alternative if possible.

- * @deprecated 3.5.0
+ * @deprecated 3.5.0 Use the new replacement class instead.

*/
class Profiles extends Properties {

Expand Down
1 change: 1 addition & 0 deletions src/Kunoichi/GaCommunicator/Api/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Accounts api.
*
* @package ga-communicator
* @deprecated 3.5.0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The class Properties is marked as deprecated but there's no alternative suggested. It's a good practice to suggest an alternative when deprecating a class, method or function.

- * @deprecated 3.5.0
+ * @deprecated 3.5.0 Use NewProperties instead.

This assumes that NewProperties is the new class replacing Properties. Please replace NewProperties with the actual class if it's different.

*/
class Properties extends Accounts {

Expand Down
68 changes: 29 additions & 39 deletions src/Kunoichi/GaCommunicator/Screen/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Settings extends Singleton {
'tag',
'extra',
'place',
'meta',
'body-open',
];

Expand Down Expand Up @@ -230,7 +231,7 @@ public function register_setting_fields() {
'description' => __( 'If you choose double tracking, specify which API to use.', 'ga-communicator' ),
'options' => [
'' => __( 'Use Google Analytic 4', 'ga-communicator' ),
'ga' => __( 'Use Universal Analytics(will be deprecated)', 'ga-communicator' ),
'ga' => __( 'Use Universal Analytics(Deprecated)', 'ga-communicator' ),
],
],
] as $key => $setting ) {
Expand Down Expand Up @@ -276,35 +277,6 @@ function( $args ) {
);
}

// Register profiles,
$account_section = $this->slug . '-accounts';
add_settings_section( $account_section, __( 'Universal Analytics Account Setting', 'ga-communicator' ), function() {
printf( '<p class="description">%s</p>', esc_html__( 'If you set credentials, please choose Google Analytics account of your site. Account, property, and profile are required.', 'ga-communicator' ) );
printf(
'<p class="ga-error">%s</p>',
esc_html__( 'Notice: This API will be deprecated on June 2023. Please create new GA4 property.', 'ga-communicator' )
);
}, $this->slug );
foreach ( [
'ga-account' => [
'label' => __( 'Account', 'ga-communicator' ),
'description' => __( 'Google Analytics account.', 'ga-communicator' ),
],
'ga-property' => [
'label' => __( 'Property', 'ga-communicator' ),
'description' => __( 'Google Analytics property like <code>UA-0000000-11</code>.', 'ga-communicator' ),
],
'ga-profile' => [
'label' => __( 'Profile', 'ga-communicator' ),
'description' => __( 'Profile formerly known as "View". e.g. All website data.', 'ga-communicator' ),
],
] as $key => $setting ) {
add_settings_field( $key, $setting['label'], [ $this, 'account_field' ], $this->slug, $account_section, [
'key' => $key,
'description' => $setting['description'],
] );
}

// Render analytics tag.
$tag_section = $this->slug . 'tags';
add_settings_section( $tag_section, __( 'Analytics Tag', 'ga-communicator' ), function() {
fumikito marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -313,7 +285,7 @@ function( $args ) {
$choices = [
'' => __( 'No Output', 'ga-communicator' ),
'gtag' => 'gtag.js',
'universal' => 'Universal Analytics(ga.js)',
'universal' => 'Universal Analytics(Deprecated)',
'manual' => __( 'Manual Code(for GTM)', 'ga-communicator' ),
];
add_settings_field( 'ga-tag', __( 'Tag Type', 'ga-communicator' ), function() use ( $choices ) {
Expand Down Expand Up @@ -344,6 +316,7 @@ function( $args ) {
<?php
endif;
}, $this->slug, $tag_section );

// Additional scrips.
add_settings_field( 'ga-extra', __( 'Additional Scripts', 'ga-communicator' ), function() use ( $choices ) {
$predefined = $this->get_predefined_option( 'extra' );
Expand All @@ -354,11 +327,7 @@ function( $args ) {
<textarea id="ga-extra" readonly class="widefat"><?php echo esc_textarea( $predefined ); ?></textarea>
<p class="description"><?php esc_html_e( 'Extra scripts are defined programmatically.', 'ga-communicator' ); ?></p>
<?php else : ?>
<textarea id="ga-extra" name="ga-extra" rows="5" class="widefat">
<?php
echo esc_textarea( $value );
?>
</textarea>
<textarea id="ga-extra" name="ga-extra" rows="5" class="widefat"><?php echo esc_textarea( $value ); ?></textarea>
<?php endif; ?>
<?php
printf(
Expand All @@ -382,9 +351,6 @@ function( $args ) {
<?php endforeach; ?>
</tbody>
</table>
<div id="ga-dimensions">

</div>
<?php
foreach ( [
'gtag' => "gtagConfig = { custom_map: { dimension1: 'post_id' }, post_id: %post_id% };",
fumikito marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -395,6 +361,30 @@ function( $args ) {
};
}, $this->slug, $tag_section );

// Meta tags.
add_settings_field( 'ga-meta', __( 'Meta tags', 'ga-communicator' ), function() use ( $choices ) {
$predefined = $this->get_predefined_option( 'meta' );
$cur_value = $this->get_option( 'meta', true );
?>
<textarea name="ga-meta" id="ga-meta" class="widefat" rows="5" placeholder="e.g. post_id, post_type, term"><?php echo esc_textarea( $cur_value ); ?></textarea>
<p class="description">
<?php
echo wp_kses_post( sprintf(
// translators: %s is link URL.
__( 'These tags are rendered as meta tags for Google Tag Manager. See <a href="%s" target="_blank" rel="noopener noreferrer">our wiki</a> for more details.', 'ga-communicator' ),
'https://github.com/kuno1/ga-communicator/wiki/meta%E3%82%BF%E3%82%B0%E3%81%B8%E3%81%AE%E5%87%BA%E5%8A%9B'
) );
?>
</p>
<?php if ( $predefined ) : ?>
<p class="description">
<?php esc_html_e( 'Meta tags are defined programmatically.', 'ga-communicator' ); ?>
</p>
<pre><?php esc_html( $predefined ); ?></pre>
<?php
endif;
}, $this->slug, $tag_section );
Comment on lines +364 to +386

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

This new section for Meta tags seems fine, but ensure that user input is properly sanitized before being used or stored to prevent potential security issues such as Cross-Site Scripting (XSS).


// Body open.
add_settings_field( 'ga-body-open', __( 'Tag after <body> open tag', 'ga-communicator' ), function() use ( $choices ) {
$predefined = $this->get_predefined_option( 'body-open' );
Expand Down
2 changes: 1 addition & 1 deletion src/Kunoichi/GaCommunicator/Utility/PlaceHolders.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function replace( $tag ) {
* Sandbox content.
*/
public function sandbox() {
return json_encode( GaCommunicator::get_instance()->default_json(), JSON_PRETTY_PRINT );
return json_encode( GaCommunicator::get_instance()->ga4_default_json(), JSON_PRETTY_PRINT );
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/Kunoichi/GaCommunicator/Utility/ScriptRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ protected function init() {
add_action( 'wp_head', [ $this, 'render' ], $priority );
add_action( 'admin_head', [ $this, 'admin_render' ], $priority );
add_action( 'login_head', [ $this, 'login_render' ], $priority );
// Meta tags.
$meta_priority = (int) apply_filters( 'ga_communicator_meta_priority', 1 );
add_action( 'wp_head', [ $this, 'render_meta' ], $meta_priority );
add_action( 'admin_head', [ $this, 'render_meta' ], $meta_priority );
add_action( 'login_head', [ $this, 'render_meta' ], $meta_priority );
// Body open.
add_action( 'wp_body_open', [ $this, 'body_open' ], 1 );
add_action( 'in_admin_header', [ $this, 'admin_body_open' ], 1 );
Expand Down Expand Up @@ -123,6 +128,32 @@ public function login_body_open() {
}
}

/**
* Render mea tag.
*
* @since 3.6.0
* @return void
*/
public function render_meta() {
$meta_keys = array_filter( array_map( 'trim', explode( ',', $this->setting->get_option( 'meta' ) ) ) );
$meta_tags = [];
$place_holders = $this->setting->placeholder->get();
foreach ( $place_holders as $placeholder ) {
if ( in_array( $placeholder['name'], $meta_keys, true ) ) {
if ( isset( $placeholder['callback'] ) && is_callable( $placeholder['callback'] ) ) {
$meta_tags[ 'gacommunicator:' . $placeholder['name'] ] = $placeholder['callback']();
}
}
}
$meta_tags = apply_filters( 'ga_communicator_meta_tags', $meta_tags );
if ( ! empty( $meta_tags ) ) {
echo "\n<!-- ga-communicaotr meta-tags -->\n";
foreach ( $meta_tags as $key => $value ) {
printf( '<meta name="%s" contnet="%s" />' . "\n", $key, $value );
}
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

There seems to be a typo in the printf function at line 152. The attribute should be content, not contnet. This could lead to incorrect HTML meta tags being generated.

- printf( '<meta name="%s" contnet="%s" />' . "\n", $key, $value );
+ printf( '<meta name="%s" content="%s" />' . "\n", $key, $value );

Also, it's good practice to escape output for security reasons. You can use WordPress's esc_attr() function to ensure that the values of $key and $value are safe for output.

- printf( '<meta name="%s" content="%s" />' . "\n", $key, $value );
+ printf( '<meta name="%s" content="%s" />' . "\n", esc_attr($key), esc_attr($value) );

Lastly, consider checking if $value is not empty before printing the meta tag. This will prevent empty meta tags from being rendered.

foreach ( $meta_tags as $key => $value ) {
+   if (!empty($value)) {
    printf( '<meta name="%s" content="%s" />' . "\n", esc_attr($key), esc_attr($value) );
+   }
}


/**
* Getter.
*
Expand Down