Skip to content

Commit

Permalink
Add FFCNR option helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetrakern committed Dec 10, 2024
1 parent 2079464 commit a904185
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions includes/functions/requests/_setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,73 @@ function ffcnr_load_options( $option_names = [], $blog_id_override = null ) {
return $ffcnr_options;
}

/**
* Returns an option value or default.
*
* @since 5.27.0
*
* @param string $option The option to get.
* @param mixed $default Optional default value. Default ''.
* @param string[] $load Array of options to query fro th é database.
*
* @return mixed The option value if found, otherwise the default.
*/

function ffcnr_get_option( $option, $default = '', $load = [] ) {
$options = ffcnr_load_options( $load );

return $options[ $option ] ?? $default;
}

/**
* Add or update an option in the WordPress options table.
*
* @since 5.27.0
* @global wpdb $wpdb WordPress database object.
*
* @param string $name The name of the option.
* @param mixed $value The value of the option.
* @param bool $autoload Whether to autoload the option. Default false.
*
* @return bool True on success, false on failure.
*/

function ffcnr_update_option( $name, $value, $autoload = false ) {
global $wpdb;

$name = trim( $name );
$autoload = $autoload ? 'yes' : 'no';

$existing_option = $wpdb->get_var(
$wpdb->prepare(
"SELECT option_id FROM {$wpdb->options} WHERE option_name = %s LIMIT 1",
$name
)
);

$value = maybe_serialize( $value );

if ( $existing_option ) {
$updated = $wpdb->update(
$wpdb->options,
array( 'option_value' => $value, 'autoload' => $autoload ),
array( 'option_name' => $name ),
['%s', '%s'],
['%s']
);

return $updated !== false;
} else {
$inserted = $wpdb->insert(
$wpdb->options,
array( 'option_name' => $name, 'option_value' => $value, 'autoload' => $autoload ),
['%s', '%s', '%s']
);

return $inserted !== false;
}
}

/**
* Returns hash of a given string.
*
Expand Down

0 comments on commit a904185

Please sign in to comment.