set_service() public method

public set_service ( $id, Sharing_Source $service )
$service Sharing_Source
Ejemplo n.º 1
0
 public function ajax_save_options()
 {
     if (isset($_POST['_wpnonce']) && isset($_POST['service']) && wp_verify_nonce($_POST['_wpnonce'], 'sharing-options_' . $_POST['service'])) {
         $sharer = new Sharing_Service();
         $service = $sharer->get_service($_POST['service']);
         if ($service && $service instanceof Sharing_Advanced_Source) {
             $service->update_options($_POST);
             $sharer->set_service($_POST['service'], $service);
         }
         $this->output_service($service->get_id(), $service, true);
         echo '<!--->';
         $service->button_style = 'icon-text';
         $this->output_preview($service);
         die;
     }
 }
 public function callback($path = '', $blog_id = 0, $button_id = 0)
 {
     $new = $this->api->ends_with($path, '/new');
     $input = $this->input();
     // Validate request
     $blog_id = $this->api->switch_to_blog_and_validate_user($this->api->get_blog_id($blog_id));
     if (is_wp_error($blog_id)) {
         return $blog_id;
     }
     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);
     } else {
         if (!class_exists('Sharing_Service') || !class_exists('Sharing_Source') || method_exists('Jetpack', 'is_module_active') && !Jetpack::is_module_active('sharedaddy')) {
             return new WP_Error('missing_jetpack_module', 'The Sharing module must be activated in order to use this endpoint', 400);
         } else {
             if (!empty($input['visibility']) && !in_array($input['visibility'], WPCOM_JSON_API_Get_Sharing_Buttons_Endpoint::$all_visibilities)) {
                 return new WP_Error('invalid_visibility', sprintf('The visibility field must be one of the following values: %s', implode(', ', WPCOM_JSON_API_Get_Sharing_Buttons_Endpoint::$all_visibilities)), 400);
             } else {
                 if ($new && empty($input['URL'])) {
                     return new WP_Error('invalid_request', 'The URL field is required', 400);
                 } else {
                     if ($new && empty($input['icon'])) {
                         return new WP_Error('invalid_request', 'The icon field is required', 400);
                     }
                 }
             }
         }
     }
     // Assign default values
     $visibility = $input['visibility'];
     if (empty($visibility) || !isset($input['visibility']) && true === $input['enabled']) {
         $visibility = 'visible';
     }
     // Update or create button
     $ss = new Sharing_Service();
     $blog_services = $ss->get_blog_services();
     if ($new) {
         // Attempt to create new button
         $updated_service = $ss->new_service($input['name'], $input['URL'], $input['icon']);
         if (false !== $updated_service && (isset($input['enabled']) && true === $input['enabled'] || isset($input['visibility']))) {
             $blog_services[$visibility][(string) $updated_service->get_id()] = $updated_service;
             $ss->set_blog_services(array_keys($blog_services['visible']), array_keys($blog_services['hidden']));
         }
     } else {
         // Find existing button
         $all_buttons = $ss->get_all_services_blog();
         if (!array_key_exists($button_id, $all_buttons)) {
             // Button doesn't exist
             return new WP_Error('not_found', 'The specified sharing button was not found', 404);
         }
         $updated_service = $all_buttons[$button_id];
         $service_id = $updated_service->get_id();
         if (is_a($all_buttons[$button_id], 'Share_Custom')) {
             // Replace options for existing custom button
             $options = $updated_service->get_options();
             $name = isset($input['name']) ? $input['name'] : $options['name'];
             $url = isset($input['URL']) ? $input['URL'] : $options['url'];
             $icon = isset($input['icon']) ? $input['icon'] : $options['icon'];
             $updated_service = new Share_Custom($service_id, array('name' => $name, 'url' => $url, 'icon' => $icon));
             $ss->set_service($button_id, $updated_service);
         }
         // Update button visibility
         $visibility_changed = (isset($input['visibility']) || true === $input['enabled']) && !array_key_exists($service_id, $blog_services[$visibility]);
         $is_disabling = false === $input['enabled'];
         if ($visibility_changed || $is_disabling) {
             // Remove from all other visibilities
             foreach ($blog_services as $service_visibility => $services) {
                 if ($service_visibility !== $visibility || $is_disabling) {
                     unset($blog_services[$service_visibility][$service_id]);
                 }
             }
             if ($visibility_changed) {
                 $blog_services[$visibility][$service_id] = $updated_service;
             }
             $ss->set_blog_services(array_keys($blog_services['visible']), array_keys($blog_services['hidden']));
         }
     }
     if (false === $updated_service) {
         return new WP_Error('invalid_request', sprintf('The sharing button was not %s', $new ? 'created' : 'updated'), 400);
     } else {
         return WPCOM_JSON_API_Get_Sharing_Button_Endpoint::format_sharing_button($ss, $updated_service);
     }
 }