/src/Twitter/Widgets/Buttons/Follow.php

https://github.com/twitter/wordpress · PHP · 288 lines · 101 code · 29 blank · 158 comment · 25 complexity · 312d514db0369a9b520708571e395f46 MD5 · raw file

  1. <?php
  2. /*
  3. The MIT License (MIT)
  4. Copyright (c) 2015 Twitter Inc.
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21. namespace Twitter\Widgets\Buttons;
  22. /**
  23. * Follow button markup to be interpreted by Twitter's widget JavaScript
  24. *
  25. * @since 1.0.0
  26. *
  27. * @link https://dev.twitter.com/web/follow-button Follow button documentation
  28. */
  29. class Follow extends \Twitter\Widgets\Base
  30. {
  31. /**
  32. * HTML class expected by the Twitter widget JS
  33. *
  34. * @since 1.0.0
  35. *
  36. * @type string
  37. */
  38. const HTML_CLASS = 'twitter-follow-button';
  39. /**
  40. * Allowed values for the size property
  41. *
  42. * @since 1.0.0
  43. *
  44. * @type array allowed sizes {
  45. * @type string size
  46. * @type bool exists
  47. * }
  48. */
  49. public static $ALLOWED_SIZES = array( 'medium' => true, 'large' => true );
  50. /**
  51. * Show the current number of followers alongside the button
  52. *
  53. * @since 1.0.0
  54. *
  55. * @type bool
  56. */
  57. protected $show_count = true;
  58. /**
  59. * Follow Web Intent
  60. *
  61. * @since 1.0.0
  62. *
  63. * @type \Twitter\Intents\Follow
  64. */
  65. protected $intent;
  66. /**
  67. * Show or hide the screen name of the account
  68. *
  69. * @since 1.0.0
  70. *
  71. * @type bool
  72. */
  73. protected $show_screen_name = true;
  74. /**
  75. * Size of the button
  76. *
  77. * @since 1.0.0
  78. *
  79. * @type string
  80. */
  81. protected $size;
  82. /**
  83. * Require screen name. Initialize Follow Web Intent
  84. *
  85. * @since 1.0.0
  86. *
  87. * @param string $screen_name Twitter account name
  88. * @param bool $validate validate screen name matches Twitter username allowed characters and length before saving
  89. */
  90. public function __construct($screen_name, $validate = true)
  91. {
  92. $this->intent = new \Twitter\Intents\Follow($screen_name, $validate);
  93. }
  94. /**
  95. * Show the number of followers alongside the Follow button
  96. *
  97. * @since 1.0.0
  98. *
  99. * @return self support chaining
  100. */
  101. public function showCount()
  102. {
  103. $this->show_count = true;
  104. return $this;
  105. }
  106. /**
  107. * Show only the Follow button, without a follower count
  108. *
  109. * @since 1.0.0
  110. *
  111. * @return self support chaining
  112. */
  113. public function hideCount()
  114. {
  115. $this->show_count = false;
  116. return $this;
  117. }
  118. /**
  119. * Return the screen name of the Twitter user
  120. *
  121. * @since 1.0.0
  122. *
  123. * @return string Twitter screen name, or blank string if no screen name stored
  124. */
  125. public function getScreenName()
  126. {
  127. return $this->intent->getScreenName();
  128. }
  129. /**
  130. * Show the screen name to follow inside the Follow button
  131. *
  132. * @since 1.0.0
  133. *
  134. * @return self support chaining
  135. */
  136. public function showScreenName()
  137. {
  138. $this->show_screen_name = true;
  139. return $this;
  140. }
  141. /**
  142. * Hide the screen name from display inside the Follow button
  143. *
  144. * @since 1.0.0
  145. *
  146. * @return self support chaining
  147. */
  148. public function hideScreenName()
  149. {
  150. $this->show_screen_name = false;
  151. return $this;
  152. }
  153. /**
  154. * Set the desired size of the Follow button
  155. *
  156. * @since 1.0.0
  157. *
  158. * @param string $size button size
  159. *
  160. * @return self support chaining
  161. */
  162. public function setSize($size)
  163. {
  164. if ($size && isset(static::$ALLOWED_SIZES[$size])) {
  165. $this->size = $size;
  166. }
  167. return $this;
  168. }
  169. /**
  170. * Build a Follow button object from an associative array
  171. *
  172. * @since 1.0.0
  173. *
  174. * @param array $options associative array of options {
  175. * @type string option name
  176. * @type string|int|bool option value
  177. * }
  178. *
  179. * @return static|null Follow button object or null if minimum requirements not met
  180. */
  181. public static function fromArray($options)
  182. {
  183. if (! isset($options['screen_name']) && $options['screen_name']) {
  184. return null;
  185. }
  186. $class = get_called_class();
  187. $follow = new $class( $options['screen_name'] );
  188. unset($class);
  189. $follow->setBaseOptions($options);
  190. if (isset($options['show_count']) && ( false === $options['show_count'] || 'false' === $options['show_count'] || 0 == $options['show_count'] )) {
  191. $follow->hideCount();
  192. }
  193. if (isset($options['show_screen_name']) && ( false === $options['show_screen_name'] || 'false' === $options['show_screen_name'] || 0 == $options['show_screen_name'] )) {
  194. $follow->hideScreenName();
  195. }
  196. if (isset($options['size']) && 'medium' !== $options['size']) {
  197. $follow->setSize($options['size']);
  198. }
  199. return $follow;
  200. }
  201. /**
  202. * Convert the class object into an array, removing default field values
  203. *
  204. * @since 1.0.0
  205. *
  206. * @return array properties as associative array
  207. */
  208. public function toArray()
  209. {
  210. $data = parent::toArray();
  211. if (false === $this->show_screen_name) {
  212. $data['show-screen-name'] = 'false';
  213. }
  214. if (false === $this->show_count) {
  215. $data['show-count'] = 'false';
  216. }
  217. if ($this->size && 'medium' !== $this->size) {
  218. $data['size'] = $this->size;
  219. }
  220. return $data;
  221. }
  222. /**
  223. * Generate HTML to encourage follow behavior and expose data to the Twitter for Websites JavaScript
  224. *
  225. * @since 1.0.0
  226. *
  227. * @param string $anchor_text inner text of the generated anchor element. Supports a single '%s' screen name passed through sprintf. Default: Follow %s
  228. * @param string $html_builder_class callable HTML builder with a static anchorElement class
  229. *
  230. * @return string HTML markup or empty string if minimum requirements not met
  231. */
  232. public function toHTML($anchor_text = 'Follow %s', $html_builder_class = '\Twitter\Helpers\HTMLBuilder')
  233. {
  234. if (! ( is_string($anchor_text) && $anchor_text )) {
  235. return '';
  236. }
  237. // test for invalid passed class
  238. if (! ( class_exists($html_builder_class) && method_exists($html_builder_class, 'anchorElement') )) {
  239. return '';
  240. }
  241. $intent_url = $this->intent->getIntentURL();
  242. // no screen name stored
  243. if (! $intent_url) {
  244. return '';
  245. }
  246. return $html_builder_class::anchorElement(
  247. $intent_url,
  248. sprintf($anchor_text, '@' . $this->getScreenName()),
  249. array(
  250. 'class' => static::HTML_CLASS,
  251. ),
  252. $this->toArray()
  253. );
  254. }
  255. }