Skip to content

Commit

Permalink
Sharing: avoid Fatals when the Sharing_Service class doesn't exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeherve committed Nov 3, 2016
1 parent 5b9a9f5 commit 1043372
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
22 changes: 12 additions & 10 deletions _inc/lib/class.core-rest-api-endpoints.php
Expand Up @@ -852,17 +852,19 @@ public static function jumpstart_activate( $data ) {
$hidden = array();

// Set some sharing settings
$sharing = new Sharing_Service();
$sharing_options['global'] = array(
'button_style' => 'icon',
'sharing_label' => $sharing->default_sharing_label,
'open_links' => 'same',
'show' => array( 'post' ),
'custom' => isset( $sharing_options['global']['custom'] ) ? $sharing_options['global']['custom'] : array()
);
if ( class_exists( 'Sharing_Service' ) ) {
$sharing = new Sharing_Service();
$sharing_options['global'] = array(
'button_style' => 'icon',
'sharing_label' => $sharing->default_sharing_label,
'open_links' => 'same',
'show' => array( 'post' ),
'custom' => isset( $sharing_options['global']['custom'] ) ? $sharing_options['global']['custom'] : array()
);

$result['sharing_options'] = update_option( 'sharing-options', $sharing_options );
$result['sharing_services'] = update_option( 'sharing-services', array( 'visible' => $visible, 'hidden' => $hidden ) );
$result['sharing_options'] = update_option( 'sharing-options', $sharing_options );
$result['sharing_services'] = update_option( 'sharing-services', array( 'visible' => $visible, 'hidden' => $hidden ) );
}
}

// If all Jumpstart modules were activated
Expand Down
30 changes: 23 additions & 7 deletions _inc/lib/core-api/class.jetpack-core-api-module-endpoints.php
Expand Up @@ -580,6 +580,10 @@ public function update_data( $data ) {
break;

case 'sharing_services':
if ( ! class_exists( 'Sharing_Service' ) && ! @include( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
break;
}

$sharer = new Sharing_Service();

// If option value was the same, consider it done.
Expand All @@ -589,22 +593,34 @@ public function update_data( $data ) {
case 'button_style':
case 'sharing_label':
case 'show':
$sharer = new Sharing_Service();
$grouped_options = $sharer->get_global_options();
$grouped_options[$option] = $value;
$updated = $sharer->set_global_options( $grouped_options );
if ( ! class_exists( 'Sharing_Service' ) && ! @include( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
break;
}

$sharer = new Sharing_Service();
$grouped_options = $sharer->get_global_options();
$grouped_options[ $option ] = $value;
$updated = $sharer->set_global_options( $grouped_options );
break;

case 'custom':
$sharer = new Sharing_Service();
if ( ! class_exists( 'Sharing_Service' ) && ! @include( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
break;
}

$sharer = new Sharing_Service();
$updated = $sharer->new_service( stripslashes( $value['sharing_name'] ), stripslashes( $value['sharing_url'] ), stripslashes( $value['sharing_icon'] ) );

// Return new custom service
$response[$option] = $updated;
break;

case 'sharing_delete_service':
$sharer = new Sharing_Service();
if ( ! class_exists( 'Sharing_Service' ) && ! @include( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) {
break;
}

$sharer = new Sharing_Service();
$updated = $sharer->delete_service( $value );
break;

Expand Down Expand Up @@ -1135,4 +1151,4 @@ function jetpack_do_after_gravatar_hovercards_deactivation() {
// When Gravatar Hovercards is deactivated, disable them automatically.
update_option( 'gravatar_disable_hovercards', 'disabled' );
}
add_action( 'jetpack_deactivate_module_gravatar-hovercards', 'jetpack_do_after_gravatar_hovercards_deactivation' );
add_action( 'jetpack_deactivate_module_gravatar-hovercards', 'jetpack_do_after_gravatar_hovercards_deactivation' );
Expand Up @@ -7,7 +7,9 @@ abstract class WPCOM_JSON_API_Sharing_Button_Endpoint extends WPCOM_JSON_API_End
protected $sharing_service;

protected function setup() {
$this->sharing_service = new Sharing_Service();
if ( class_exists( 'Sharing_Service' ) ) {
$this->sharing_service = new Sharing_Service();
}

if ( ! current_user_can( 'manage_options' ) ) {
return new WP_Error( 'forbidden', 'You do not have the capability to manage sharing buttons for this site', 403 );
Expand All @@ -30,7 +32,7 @@ public function format_sharing_button( $button ) {
// Status is either "disabled" or the visibility value
$response['visibility'] = $this->get_button_visibility( $button );
}

if ( ! empty( $button->icon ) ) {
// Only pre-defined sharing buttons include genericon
$response['genericon'] = $button->icon;
Expand Down

0 comments on commit 1043372

Please sign in to comment.