Filter
uncanny-automator
automator_recipe_object_json_encoding_flags
Filters the JSON encoding flags used when saving a recipe object, allowing customization of the serialization process.
add_filter( 'automator_recipe_object_json_encoding_flags', $callback, 10, 1 );
Description
Modify JSON encoding flags for recipe objects. This filter hook allows developers to customize how recipe objects are converted to JSON strings, influencing character encoding handling and other JSON-specific behaviors. It fires before the recipe object is serialized, providing control over the encoding process.
Usage
add_filter( 'automator_recipe_object_json_encoding_flags', 'your_function_name', 10, 1 );
Parameters
-
$this(mixed) - This parameter is a reference to the `Recipe` object itself, allowing the filter to access or modify its properties before JSON encoding.
Return Value
The filtered value.
Examples
/**
* Example of modifying JSON encoding flags for recipe objects.
*
* This filter allows developers to adjust the flags used when encoding
* a recipe object to JSON. For instance, one might want to add
* JSON_PRETTY_PRINT for easier debugging during development.
*
* @param int $flags The current JSON encoding flags.
* @param object $recipe_object The recipe object being encoded.
* @return int The modified JSON encoding flags.
*/
add_filter( 'automator_recipe_object_json_encoding_flags', function( $flags, $recipe_object ) {
// Check if we are in a development environment and enable pretty printing.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) {
// Add JSON_PRETTY_PRINT to make the JSON output more human-readable.
$flags = $flags | JSON_PRETTY_PRINT;
}
return $flags;
}, 10, 2 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/core/services/recipe/structure.php:236
public function toJSON() {
// Pass default option by default.
$flags = apply_filters( 'automator_recipe_object_json_encoding_flags', 0, $this );
// If its a php 7.2 where constant "JSON_INVALID_UTF8_SUBSTITUTE" is available.
if ( defined( 'JSON_INVALID_UTF8_SUBSTITUTE' ) ) {
// Provide a different filter and by default, substitute invalid utf-8 characters.
$flags = apply_filters( 'automator_recipe_object_json_encoding_flags_php72', JSON_INVALID_UTF8_SUBSTITUTE, $this );
}
$decoded = wp_json_encode( $this, $flags );
if ( false === $decoded || ! is_string( $decoded ) ) {
return '';
}
return $decoded;
}