OAuthCommonStorageWordPressMemory

Stores a token in WordPress options database table.

Defined (1)

The class is defined in the following location(s).

/lib/WordPressMemory.php  
  1. class WordPressMemory implements TokenStorageInterface 
  2. /** 
  3. * @var object|TokenInterface 
  4. */ 
  5. protected $tokens; 
  6.  
  7. /** 
  8. * Key for storing token in WordPress options table 
  9. * @var string 
  10. */ 
  11. private $tokenOptionName; 
  12.  
  13. /** 
  14. * Key for storing state in WordPress options table 
  15. * @var string 
  16. */ 
  17. private $stateOptionName; 
  18.  
  19. /** 
  20. * @var array 
  21. */ 
  22. protected $states; 
  23.  
  24. public function __construct() 
  25. $this->tokenOptionName = 'lusitanian_oauth_token'; 
  26. $this->stateOptionName = 'lusitanian_oauth_state'; 
  27.  
  28. $this->tokens = (array)maybe_unserialize( get_option( $this->tokenOptionName ) ); 
  29. $this->states = (array)maybe_unserialize( get_option( $this->stateOptionName ) ); 
  30.  
  31. /** 
  32. * {@inheritDoc} 
  33. */ 
  34. public function retrieveAccessToken($service) 
  35. if ($this->hasAccessToken($service)) { 
  36. return $this->tokens[$service]; 
  37.  
  38. throw new TokenNotFoundException('Token not stored'); 
  39.  
  40. /** 
  41. * {@inheritDoc} 
  42. */ 
  43. public function storeAccessToken($service, TokenInterface $token) 
  44.  
  45. $this->tokens[$service] = $token; 
  46.  
  47. update_option( $this->tokenOptionName, $this->tokens); 
  48.  
  49. // allow chaining 
  50. return $this; 
  51.  
  52. /** 
  53. * {@inheritDoc} 
  54. */ 
  55. public function hasAccessToken($service) 
  56. return is_array($this->tokens) && isset($this->tokens[$service]) && $this->tokens[$service] instanceof TokenInterface; 
  57.  
  58. /** 
  59. * {@inheritDoc} 
  60. */ 
  61. public function clearToken($service) 
  62. if (array_key_exists($service, $this->tokens)) { 
  63. unset($this->tokens[$service]); 
  64.  
  65. update_option( $this->tokenOptionName, $this->tokens); 
  66.  
  67. // allow chaining 
  68. return $this; 
  69.  
  70. /** 
  71. * {@inheritDoc} 
  72. */ 
  73. public function clearAllTokens() 
  74.  
  75. $this->tokens = array(); 
  76.  
  77. update_option( $this->tokenOptionName, $this->tokens); 
  78.  
  79. // allow chaining 
  80. return $this; 
  81.  
  82. /** 
  83. * {@inheritDoc} 
  84. */ 
  85. public function retrieveAuthorizationState($service) 
  86. if ($this->hasAuthorizationState($service)) { 
  87. return $this->states[$service]; 
  88.  
  89. throw new AuthorizationStateNotFoundException('State not stored'); 
  90.  
  91. /** 
  92. * {@inheritDoc} 
  93. */ 
  94. public function storeAuthorizationState($service, $state) 
  95. $this->states[$service] = $state; 
  96.  
  97. update_option( $this->stateOptionName, $this->states); 
  98.  
  99. // allow chaining 
  100. return $this; 
  101.  
  102. /** 
  103. * {@inheritDoc} 
  104. */ 
  105. public function hasAuthorizationState($service) 
  106. return isset($this->states[$service]) && null !== $this->states[$service]; 
  107.  
  108. /** 
  109. * {@inheritDoc} 
  110. */ 
  111. public function clearAuthorizationState($service) 
  112. if (array_key_exists($service, $this->states)) { 
  113. unset($this->states[$service]); 
  114.  
  115. update_option( $this->stateOptionName, $this->states); 
  116.  
  117. // allow chaining 
  118. return $this; 
  119.  
  120. /** 
  121. * {@inheritDoc} 
  122. */ 
  123. public function clearAllAuthorizationStates() 
  124. $this->states = array(); 
  125.  
  126. update_option( $this->stateOptionName, $this->states); 
  127.  
  128. // allow chaining 
  129. return $this;