has_errors() ) {
return;
}
foreach ( $error->get_error_codes() as $error_code ) {
// Extract the "error" type.
$data = $error->get_error_data( $error_code );
$type = empty( $data ) || ! is_string( $data ) ? 'error' : $data;
$message = wp_kses(
implode( '
', $error->get_error_messages( $error_code ) ),
array(
'a' => array( 'href' => true ),
'br' => array(),
'code' => array(),
'em' => array(),
)
);
add_settings_error( 'polylang', (string) $error_code, $message, $type );
}
}
/**
* Tells if the given REST request is in the edit context.
*
* @since 3.5
*
* @param WP_REST_Request $request A REST request.
* @return bool
*
* @phpstan-param WP_REST_Request $request
*/
function pll_is_edit_rest_request( WP_REST_Request $request ): bool {
if ( in_array( $request->get_method(), array( 'PATCH', 'POST', 'PUT' ), true ) ) {
return true;
}
return 'GET' === $request->get_method() && 'edit' === $request->get_param( 'context' );
}
/**
* Sanitizes an ID as positive integer.
* Kind of similar to `absint()`, but rejects negative integers instead of making them positive.
*
* @since 3.9
*
* @param mixed $id A supposedly numeric ID.
* @return int A positive integer. `0` for non numeric values and negative integers.
*
* @phpstan-return int<0, max>
*/
function pll_sanitize_id( $id ): int {
return is_numeric( $id ) && $id >= 1 ? abs( (int) $id ) : 0;
}
/**
* Sanitizes an array of IDs as positive integers.
* `0` values are removed.
*
* @since 3.9
*
* @param mixed $ids A supposedly array of numeric IDs.
* @return int[]
*
* @phpstan-return array
*/
function pll_sanitize_ids( $ids ): array {
if ( empty( $ids ) || ! is_array( $ids ) ) {
return array();
}
return array_filter( array_map( 'pll_sanitize_id', $ids ) );
}
/**
* Enqueues a JS script.
*
* @since 3.9
*
* @param string $name The filename of the script (without the extension).
* @param string[] $dependencies Optional. An array of registered script handles this script depends on.
* @param array $args {
* Optional. An array of additional script loading strategies. Default empty array.
*
* @type string $strategy Optional. If provided, may be either 'defer' or 'async'.
* @type bool $in_footer Optional. Whether to print the script in the footer. Default 'false'.
* @type string $fetchpriority Optional. The fetch priority for the script. Default 'auto'.
* }
* @param string $prefix Optional. Prefix to use for the script handle. Default 'pll-'.
* @return void
*
* @phpstan-param non-empty-string $name
*/
function pll_enqueue_script( string $name, array $dependencies = array(), array $args = array(), string $prefix = 'pll-' ): void {
pll_register_script( $name, $dependencies, $args );
wp_scripts()->enqueue( $prefix . $name );
}
/**
* Registers a JS script.
*
* @since 3.9
*
* @param string $name The filename of the script (without the extension).
* @param string[] $dependencies Optional. An array of registered script handles this script depends on.
* @param array $args {
* Optional. An array of additional script loading strategies. Default empty array.
*
* @type string $strategy Optional. If provided, may be either 'defer' or 'async'.
* @type bool $in_footer Optional. Whether to print the script in the footer. Default 'false'.
* @type string $fetchpriority Optional. The fetch priority for the script. Default 'auto'.
* }
* @param string $prefix Optional. Prefix to use for the script handle. Default 'pll-'.
* @return void
*
* @phpstan-param non-empty-string $name
*/
function pll_register_script( string $name, array $dependencies = array(), array $args = array(), string $prefix = 'pll-' ): void {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
$src = plugins_url( "/js/build/{$name}{$suffix}.js", POLYLANG_ROOT_FILE );
if ( '' === $src ) {
return;
}
$version = WP_DEBUG ? time() : POLYLANG_VERSION;
wp_register_script( $prefix . $name, $src, array_filter( $dependencies ), (string) $version, $args );
}
/**
* Enqueues a CSS stylesheet.
*
* @since 3.9
*
* @param string $name The filename of the stylesheet (without the extension).
* @param string[] $dependencies Optional. An array of registered stylesheet handles this stylesheet depends on.
* @param string $prefix Optional. Prefix to use for the stylesheet handle. Default 'pll-'.
* @return void
*
* @phpstan-param non-empty-string $name
*/
function pll_enqueue_style( string $name, array $dependencies = array(), string $prefix = 'pll-' ): void {
pll_register_style( $name, $dependencies );
wp_styles()->enqueue( $prefix . $name );
}
/**
* Registers a CSS stylesheet.
*
* @since 3.9
*
* @param string $name The filename of the stylesheet (without the extension).
* @param string[] $dependencies Optional. An array of registered stylesheet handles this stylesheet depends on.
* @param string $prefix Optional. Prefix to use for the stylesheet handle. Default 'pll-'.
* @return void
*
* @phpstan-param non-empty-string $name
*/
function pll_register_style( string $name, array $dependencies = array(), string $prefix = 'pll-' ): void {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
$src = plugins_url( "/css/build/{$name}{$suffix}.css", POLYLANG_ROOT_FILE );
if ( '' === $src ) {
return;
}
$version = WP_DEBUG ? time() : POLYLANG_VERSION;
wp_register_style( $prefix . $name, $src, $dependencies, (string) $version );
}