fix: validate CHRNX editorial fallbacks
This commit is contained in:
8 files changed
+232
-17
No files matched your search
+64
-7
@@ -137,6 +137,66 @@ function chrnx_validate_editorial_term_slug( $value, $taxonomy ) {
|
||||
return $term instanceof WP_Term ? $term->slug : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a canonical public Journal page number.
|
||||
*
|
||||
* @param mixed $value Request-shaped page value.
|
||||
* @return int A page from 1 through 10000, or 1 for invalid input.
|
||||
*/
|
||||
function chrnx_parse_journal_page( $value ) {
|
||||
if ( is_int( $value ) ) {
|
||||
$page = $value;
|
||||
} elseif ( is_string( $value ) && preg_match( '/^[1-9][0-9]*$/', $value ) ) {
|
||||
$page = (int) $value;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return $page >= 1 && $page <= 10000 ? $page : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize and bound public Journal search text.
|
||||
*
|
||||
* @param mixed $value Request-shaped search value.
|
||||
* @return string Safe search text or an empty string.
|
||||
*/
|
||||
function chrnx_sanitize_journal_search( $value ) {
|
||||
if ( ! is_scalar( $value ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return mb_substr( trim( sanitize_text_field( wp_unslash( (string) $value ) ) ), 0, 100, 'UTF-8' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a Journal page URL from only validated public filters.
|
||||
*
|
||||
* @param int $page Requested page number.
|
||||
* @param array $filters Request-shaped Journal filters.
|
||||
* @return string Safe Journal URL.
|
||||
*/
|
||||
function chrnx_get_journal_page_url( int $page, array $filters ) {
|
||||
$base_url = strtok( get_pagenum_link( 1, false ), '?' );
|
||||
$args = array();
|
||||
foreach ( array( 'chrnx_topic', 'chrnx_language' ) as $taxonomy ) {
|
||||
$slug = chrnx_validate_editorial_term_slug( $filters[ $taxonomy ] ?? '', $taxonomy );
|
||||
if ( '' !== $slug ) {
|
||||
$args[ $taxonomy ] = $slug;
|
||||
}
|
||||
}
|
||||
$search = chrnx_sanitize_journal_search( $filters['s'] ?? '' );
|
||||
if ( '' !== $search ) {
|
||||
$args['s'] = $search;
|
||||
}
|
||||
$page = chrnx_parse_journal_page( $page );
|
||||
if ( $page > 1 ) {
|
||||
$args['paged'] = $page;
|
||||
}
|
||||
|
||||
return add_query_arg( $args, $base_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Build safe Journal WP_Query arguments from request-shaped data.
|
||||
*
|
||||
@@ -150,7 +210,7 @@ function chrnx_get_journal_query_args( array $request ): array {
|
||||
'post_status' => 'publish',
|
||||
'post__in' => ! empty( $eligible_ids ) ? $eligible_ids : array( 0 ),
|
||||
'posts_per_page' => 10,
|
||||
'paged' => isset( $request['paged'] ) && is_scalar( $request['paged'] ) ? max( 1, intval( $request['paged'] ) ) : 1,
|
||||
'paged' => chrnx_parse_journal_page( $request['paged'] ?? null ),
|
||||
'orderby' => array(
|
||||
'date' => 'DESC',
|
||||
'ID' => 'DESC',
|
||||
@@ -174,12 +234,9 @@ function chrnx_get_journal_query_args( array $request ): array {
|
||||
$args['tax_query'] = $tax_query;
|
||||
}
|
||||
|
||||
if ( isset( $request['s'] ) && is_scalar( $request['s'] ) ) {
|
||||
$search = sanitize_text_field( wp_unslash( (string) $request['s'] ) );
|
||||
$search = mb_substr( trim( $search ), 0, 100, 'UTF-8' );
|
||||
if ( '' !== $search ) {
|
||||
$args['s'] = $search;
|
||||
}
|
||||
$search = chrnx_sanitize_journal_search( $request['s'] ?? '' );
|
||||
if ( '' !== $search ) {
|
||||
$args['s'] = $search;
|
||||
}
|
||||
|
||||
return $args;
|
||||
|
||||
Reference in new issue
Block a user