Exemple #1
0
 function admin_page()
 {
     // Handle delete request. Will default back to "tokens" later
     if (isset($_REQUEST['action']) && 'delete' == $_REQUEST['action']) {
         if (!isset($_REQUEST['nonce']) || !wp_verify_nonce($_REQUEST['nonce'], 'keyring-delete-' . $_REQUEST['service'] . '-' . $_REQUEST['token'])) {
             Keyring::error(__('Invalid/missing delete nonce.', 'keyring'));
             exit;
         }
         if ($this->keyring->get_token_store()->delete(array('id' => (int) $_REQUEST['token'], 'type' => 'access'))) {
             Keyring::message(__('That connection has been deleted.', 'keyring'));
         } else {
             Keyring::message(__('Could not delete that connection!', 'keyring'));
         }
     }
     // Handle test request. Will default back to "tokens" later
     if (isset($_REQUEST['action']) && 'test' == $_REQUEST['action']) {
         if (!isset($_REQUEST['nonce']) || !wp_verify_nonce($_REQUEST['nonce'], 'keyring-test-' . $_REQUEST['service'] . '-' . $_REQUEST['token'])) {
             Keyring::error(__('Invalid/missing testing nonce.', 'keyring'));
             exit;
         }
         // If the test_connection() method exists, call it for this service/connection
         $service = $this->keyring->get_service_by_name($_REQUEST['service']);
         if (method_exists($service, 'test_connection')) {
             $service->set_token($this->keyring->get_token_store()->get_token(array('id' => $_REQUEST['token'], 'type' => 'request')));
             if ($service->test_connection()) {
                 Keyring::message(__('This connection is working correctly.', 'keyring'));
             } else {
                 Keyring::message(__('This connection is <strong>NOT</strong> working correctly.', 'keyring'));
             }
         } else {
             Keyring::message(__('This service does not currently support connection testing.', 'keyring'));
         }
     }
     // Set up our defaults
     $service = '';
     if (!empty($_REQUEST['service'])) {
         $service = $_REQUEST['service'];
     }
     $action = 'tokens';
     if (isset($_REQUEST['action']) && in_array($_REQUEST['action'], array('tokens', 'services', 'request', 'verify', 'manage'))) {
         $action = $_REQUEST['action'];
     }
     // Custom UI optionally hooked in to handle this service/action. Trigger action
     // and assume it handles everything, so bail out after that.
     if (Keyring_Util::has_custom_ui($service, $action)) {
         do_action("keyring_{$service}_{$action}_ui");
         return;
     }
     // Nothing else has bailed, so it must be one of our default/core screens.
     switch ($action) {
         case 'tokens':
             $this->admin_page_header('tokens');
             $list_table = new Keyring_Connections_List_Table();
             $list_table->display();
             $this->admin_page_footer();
             break;
         case 'services':
             $this->admin_page_header('services');
             $services = $this->keyring->get_registered_services();
             if (count($services)) {
                 $configured = $not_configured = array();
                 foreach ($services as $service) {
                     if ($service->is_configured()) {
                         $configured[] = $service;
                     } else {
                         $not_configured[] = $service;
                     }
                 }
                 if (count($configured)) {
                     echo '<p><strong>' . __('Click a service to create a new connection:', 'keyring') . '</strong></p>';
                     echo '<ul>';
                     foreach ($configured as $service) {
                         $request_kr_nonce = wp_create_nonce('keyring-request');
                         $request_nonce = wp_create_nonce('keyring-request-' . $service->get_name());
                         echo '<li><a href="' . esc_url(Keyring_Util::admin_url($service->get_name(), array('action' => 'request', 'kr_nonce' => $request_kr_nonce, 'nonce' => $request_nonce))) . '">' . esc_html($service->get_label()) . '</a>';
                         if (has_action('keyring_' . $service->get_name() . '_manage_ui')) {
                             $manage_kr_nonce = wp_create_nonce('keyring-manage');
                             $manage_nonce = wp_create_nonce('keyring-manage-' . $service->get_name());
                             echo ' (<a href="' . esc_url(Keyring_Util::admin_url($service->get_name(), array('action' => 'manage', 'kr_nonce' => $manage_kr_nonce, 'nonce' => $manage_nonce))) . '">' . esc_html(__('Manage', 'keyring')) . '</a>)';
                         }
                         echo '</li>';
                     }
                     echo '</ul><br /><br />';
                 } else {
                     echo '<p>' . __('There are no fully-configured services available to connect to.', 'keyring') . '</p>';
                 }
                 if (count($not_configured)) {
                     echo '<p>' . __('The following services need to be configured correctly before you can connect to them.', 'keyring') . '</p>';
                     echo '<ul>';
                     foreach ($not_configured as $service) {
                         if (!has_action('keyring_' . $service->get_name() . '_manage_ui')) {
                             continue;
                         }
                         $manage_kr_nonce = wp_create_nonce('keyring-manage');
                         $manage_nonce = wp_create_nonce('keyring-manage-' . $service->get_name());
                         echo '<li><a href="' . esc_url(Keyring_Util::admin_url($service->get_name(), array('action' => 'manage', 'kr_nonce' => $manage_kr_nonce, 'nonce' => $manage_nonce))) . '">' . esc_html($service->get_label()) . '</a></li>';
                     }
                     echo '</ul>';
                 }
             }
             $this->admin_page_footer();
             break;
     }
 }