example.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * This Service is provided as an example only. It doesn't do anything useful :)
  4. */
  5. // Always extend Keyring_Service, or something else which extends it (e.g. Keyring_Service_OAuth1)
  6. class Keyring_Service_Example extends Keyring_Service {
  7. const NAME = 'example';
  8. const LABEL = 'Example Service';
  9. function __construct() {
  10. // If you need a custom __construct(), make sure to call the parent explicitly like this
  11. parent::__construct();
  12. // Optionally register methods (of this object) to handle the UI for different actions
  13. // action is in the format "keyring_{$service}_{request/verify}_ui".
  14. // These are optional, and are only required if you need the user to see/do something during
  15. // each step.
  16. add_action( 'keyring_example_request_ui', array( $this, 'request_ui' ) );
  17. add_action( 'keyring_example_verify_ui', array( $this, 'verify_ui' ) );
  18. // Enable "basic" UI for entering key/secret, which a lot of services require
  19. // add_action( 'keyring_example_manage_ui', array( $this, 'basic_ui' ) );
  20. // Optionally make this a service that we can communicate with *without*
  21. // requiring any sort of connection
  22. $this->requires_token( false );
  23. }
  24. /**
  25. * Allows you to do things before any output has been sent to the browser.
  26. * This means you can redirect to a remote site, another page etc if need be.
  27. */
  28. function request_token() {
  29. // Nothing to do in this example
  30. }
  31. /**
  32. * You can define how a token presents itself to the user here. For example for Twitter,
  33. * we might show "@" . $screen_name.
  34. *
  35. * @param Keyring_Access_Token $token
  36. * @return String for use in UIs etc that helps identify this specific token
  37. */
  38. function get_display( Keyring_Access_Token $token ) {
  39. return $token->token;
  40. }
  41. /**
  42. * See __construct() for details on how this is hooked in to handle the UI for
  43. * during the request process.
  44. */
  45. function request_ui() {
  46. Keyring::admin_page_header(); // Generic header which can be used (includes h2 header)
  47. echo '<p>This is just an example of how you could display some sort of custom UI if you needed to.</p>';
  48. echo '<p>Clicking the button below will generate a random token and store it as an example.</p>';
  49. echo '<p class="submitbox">';
  50. echo '<a href="' . esc_url( Keyring_Util::admin_url( 'example', array( 'action' => 'verify' ) ) ) . '" class="button-primary">' . __( 'Continue', 'keyring' ) . '</a>';
  51. echo '<a href="' . esc_attr( $_SERVER['HTTP_REFERER'] ) . '" class="submitdelete" style="margin-left:2em;">Abort</a>';
  52. echo '</p>';
  53. Keyring::admin_page_footer();
  54. }
  55. /**
  56. * Allows you to do things before any output has been sent to the browser.
  57. * This means you can redirect to a remote site, another page etc if need be.
  58. */
  59. function verify_token() {
  60. // Generate a fake token and store it for this example
  61. $token = sha1( time() . mt_rand( 0, 1000 ) . time() );
  62. $meta = array( 'time' => time(), 'user' => get_current_user() );
  63. $this->store_token( $token, $meta );
  64. }
  65. /**
  66. * This method will be used to make requests against this service. This is where
  67. * you should handle injecting tokens/headers/etc required for authentication.
  68. *
  69. * @param string $url
  70. * @param array $params additional parameters/headers for the request. Passed to WP_Http
  71. * @return Response body as a string, or a Keyring_Error with the full WP_Http response object as the "message"
  72. */
  73. function request( $url, array $params = array() ) {
  74. // empty
  75. }
  76. /**
  77. * See __construct() for details on how this is hooked in to handle the UI for
  78. * during the verify process.
  79. */
  80. function verify_ui() {
  81. Keyring::admin_page_header();
  82. echo '<p>As an example, we just randomly generated a token and saved it in the token store. When you go back to your Connections listing, you should see it listed there under "Example Service".</p>';
  83. echo '<p><a href="' . esc_url( Keyring_Util::admin_url() ) . '" class="button-primary">' . __( 'Done', 'keyring' ) . '</a>';
  84. Keyring::admin_page_footer();
  85. }
  86. }
  87. // Always hook into keyring_load_services and use your init method to initiate a Service properly (singleton)
  88. add_action( 'keyring_load_services', array( 'Keyring_Service_Example', 'init' ) );