admin管理员组文章数量:1122846
function test_dashboard_content() {
$args = array( 'posts_per_page' => -1, 'post_type' => array( 'post', 'page' ), 'post_status' => 'publish', 'date_query' => array( 'after' => '1 year ago' ) );
$posts = get_posts( $args );
$allowed = '';
$testlist = '';
foreach ( $posts as $post ) {
$testlist .= '{date: "' . esc_js( get_the_date( 'Y-n-j', $post ) ) . '", value: "' . esc_js( '1' ) . '"},';
}
echo '
<script>
jQuery(document).ready(function($) {
var massive = [
' . wp_kses( $testlist, $allowed ) . '
];
});
</script>';
}
Functionally, everything is as I want it, but security wise, is there anything I should be doing different than the use of esc_js
and wp_kses
here?
function test_dashboard_content() {
$args = array( 'posts_per_page' => -1, 'post_type' => array( 'post', 'page' ), 'post_status' => 'publish', 'date_query' => array( 'after' => '1 year ago' ) );
$posts = get_posts( $args );
$allowed = '';
$testlist = '';
foreach ( $posts as $post ) {
$testlist .= '{date: "' . esc_js( get_the_date( 'Y-n-j', $post ) ) . '", value: "' . esc_js( '1' ) . '"},';
}
echo '
<script>
jQuery(document).ready(function($) {
var massive = [
' . wp_kses( $testlist, $allowed ) . '
];
});
</script>';
}
Functionally, everything is as I want it, but security wise, is there anything I should be doing different than the use of esc_js
and wp_kses
here?
1 Answer
Reset to default 0The wp_kses()
call looks to me unneccessary as you're already escaping the values within the foreach
loop. esc_js()
should be just fine as the strings are mostly hard-coded and the only part that is changing is the date value, so escape that. If value is always 1, then escaping it doesn't add any real value.
If you want to make the code a bit more readable, I would suggest first mapping the posts into a PHP array and then turning it into script friendly format with json_encode()
and wp_add_inline_script()
. This way there is a smaller risk of having typos in the code and data variable.
function test_dashboard_content(): void {
$testlist = json_encode( wpse_425118_get_posts_data() );
wp_add_inline_script(
'my-script-handle',
"var massive = {$testlist};",
'before'
);
}
function wpse_425118_get_posts_data(): array {
$posts = get_posts( array(
'posts_per_page' => -1,
'post_type' => array( 'post', 'page' ),
'post_status' => 'publish',
'date_query' => array( 'after' => '1 year ago' )
) );
return array_map(
function( WP_Post $post ) {
return array(
'date' => esc_attr( get_the_date( 'Y-n-j', $post ) ),
'value' => 1,
);
},
$posts
);
}
本文标签: javascriptEscaping inline JS correctly
版权声明:本文标题:javascript - Escaping inline JS correctly 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307077a1933186.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_enqueue_script()
and pass your data to the script withwp_localize_script()
. – Pat J Commented May 9, 2024 at 3:26wp_localize_script()
was replaced withwp_add_inline_script()
for this context and I can do that, but that still doesn't address escaping the$testlist
variable. – WPdummy Commented May 9, 2024 at 5:52