-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
* Profiles list. | ||
* | ||
* @package ga-communicator | ||
* @deprecated 3.5.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The class - * @deprecated 3.5.0
+ * @deprecated 3.5.0 Use the new replacement class instead. |
||
*/ | ||
class Profiles extends Properties { | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
* Accounts api. | ||
* | ||
* @package ga-communicator | ||
* @deprecated 3.5.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The class - * @deprecated 3.5.0
+ * @deprecated 3.5.0 Use NewProperties instead. This assumes that |
||
*/ | ||
class Properties extends Accounts { | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ class Settings extends Singleton { | |
'tag', | ||
'extra', | ||
'place', | ||
'meta', | ||
'body-open', | ||
]; | ||
|
||
|
@@ -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 ) { | ||
|
@@ -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
|
||
|
@@ -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 ) { | ||
|
@@ -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' ); | ||
|
@@ -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( | ||
|
@@ -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
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
// 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' ); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ); | ||
|
@@ -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 ); | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There seems to be a typo in the - 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 - 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 foreach ( $meta_tags as $key => $value ) {
+ if (!empty($value)) {
printf( '<meta name="%s" content="%s" />' . "\n", esc_attr($key), esc_attr($value) );
+ }
} |
||
|
||
/** | ||
* Getter. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.This comment assumes that
NewClass
is the new class replacingAccounts
. ReplaceNewClass
with the actual class name if it's different.