Skip to content

Commit

Permalink
JSON API: Include site_icon option in site settings endpoint (#5282)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth authored and samhotchkiss committed Nov 8, 2016
1 parent 3662dd7 commit 7c24905
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions json-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -2494,6 +2494,7 @@
'jetpack_testimonial_posts_per_page' => '(int) Number of testimonials to show per page',
'jetpack_portfolio' => '(bool) Whether portfolio custom post type is enabled for the site',
'jetpack_portfolio_posts_per_page' => '(int) Number of portfolio projects to show per page',
'site_icon' => '(int) Media attachment ID to use as site icon. Set to zero or an otherwise empty value to clear',
),

'response_format' => array(
Expand Down
42 changes: 35 additions & 7 deletions json-endpoints/class.wpcom-json-api-site-settings-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ public function get_category_details( $category ) {
);
}

/**
* Returns an option value as the result of the callable being applied to
* it if a value is set, otherwise null.
*
* @param (string) $option_name Option name
* @param (callable) $cast_callable Callable to invoke on option value
* @return (int|null) Numeric option value or null
*/
protected function get_cast_option_value_or_null( $option_name, $cast_callable ) {
$option_value = get_option( $option_name, null );
if ( is_null( $option_value ) ) {
return $option_value;
}

return call_user_func( $cast_callable, $option_value );
}

/**
* Collects the necessary information to return for a get settings response.
*
Expand Down Expand Up @@ -136,11 +153,6 @@ public function get_settings_response() {
)
);

$eventbrite_api_token = (int) get_option( 'eventbrite_api_token' );
if ( 0 === $eventbrite_api_token ) {
$eventbrite_api_token = null;
}

$holiday_snow = false;
if ( function_exists( 'jetpack_holiday_snow_option_name' ) ) {
$holiday_snow = (bool) get_option( jetpack_holiday_snow_option_name() );
Expand Down Expand Up @@ -191,14 +203,15 @@ public function get_settings_response() {
'jetpack_comment_likes_enabled' => (bool) get_option( 'jetpack_comment_likes_enabled', false ),
'twitter_via' => (string) get_option( 'twitter_via' ),
'jetpack-twitter-cards-site-tag' => (string) get_option( 'jetpack-twitter-cards-site-tag' ),
'eventbrite_api_token' => $eventbrite_api_token,
'eventbrite_api_token' => $this->get_cast_option_value_or_null( 'eventbrite_api_token', 'intval' ),
'holidaysnow' => $holiday_snow,
'gmt_offset' => get_option( 'gmt_offset' ),
'timezone_string' => get_option( 'timezone_string' ),
'jetpack_testimonial' => (bool) get_option( 'jetpack_testimonial', '0' ),
'jetpack_testimonial_posts_per_page' => (int) get_option( 'jetpack_testimonial_posts_per_page', '10' ),
'jetpack_portfolio' => (bool) get_option( 'jetpack_portfolio', '0' ),
'jetpack_portfolio_posts_per_page' => (int) get_option( 'jetpack_portfolio_posts_per_page', '10' ),
'site_icon' => $this->get_cast_option_value_or_null( 'site_icon', 'intval' ),
);

//allow future versions of this endpoint to support additional settings keys
Expand Down Expand Up @@ -417,13 +430,28 @@ public function update_settings() {
$value = '';
}

// Always set timezone_string either with the given value or with an
// Always set timezone_string either with the given value or with an
// empty string
if ( update_option( $key, $value ) ) {
$updated[ $key ] = $value;
}
break;

case 'site_icon':
// settings are stored as deletable numeric (all empty
// values as delete intent), validated as media image
if ( empty( $value ) || WPCOM_JSON_API::is_falsy( $value ) ) {
if ( delete_option( $key ) ) {
$updated[ $key ] = null;
}
} else if ( is_numeric( $value ) ) {
$coerce_value = (int) $value;
if ( wp_attachment_is_image( $coerce_value ) && update_option( $key, $coerce_value ) ) {
$updated[ $key ] = $coerce_value;
}
}
break;

default:
//allow future versions of this endpoint to support additional settings keys
if ( has_filter( 'site_settings_endpoint_update_' . $key ) ) {
Expand Down

0 comments on commit 7c24905

Please sign in to comment.