FacebookPageTabHelper.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright 2017 Facebook, Inc.
  4. *
  5. * You are hereby granted a non-exclusive, worldwide, royalty-free license to
  6. * use, copy, modify, and distribute this software in source code or binary
  7. * form for use in connection with the web services and APIs provided by
  8. * Facebook.
  9. *
  10. * As with any software that integrates with the Facebook platform, your use
  11. * of this software is subject to the Facebook Developer Principles and
  12. * Policies [http://developers.facebook.com/policy/]. This copyright notice
  13. * shall be included in all copies or substantial portions of the software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. namespace Facebook\Helpers;
  25. use Facebook\FacebookApp;
  26. use Facebook\FacebookClient;
  27. /**
  28. * Class FacebookPageTabHelper
  29. *
  30. * @package Facebook
  31. */
  32. class FacebookPageTabHelper extends FacebookCanvasHelper
  33. {
  34. /**
  35. * @var array|null
  36. */
  37. protected $pageData;
  38. /**
  39. * Initialize the helper and process available signed request data.
  40. *
  41. * @param FacebookApp $app The FacebookApp entity.
  42. * @param FacebookClient $client The client to make HTTP requests.
  43. * @param string|null $graphVersion The version of Graph to use.
  44. */
  45. public function __construct(FacebookApp $app, FacebookClient $client, $graphVersion = null)
  46. {
  47. parent::__construct($app, $client, $graphVersion);
  48. if (!$this->signedRequest) {
  49. return;
  50. }
  51. $this->pageData = $this->signedRequest->get('page');
  52. }
  53. /**
  54. * Returns a value from the page data.
  55. *
  56. * @param string $key
  57. * @param mixed|null $default
  58. *
  59. * @return mixed|null
  60. */
  61. public function getPageData($key, $default = null)
  62. {
  63. if (isset($this->pageData[$key])) {
  64. return $this->pageData[$key];
  65. }
  66. return $default;
  67. }
  68. /**
  69. * Returns true if the user is an admin.
  70. *
  71. * @return boolean
  72. */
  73. public function isAdmin()
  74. {
  75. return $this->getPageData('admin') === true;
  76. }
  77. /**
  78. * Returns the page id if available.
  79. *
  80. * @return string|null
  81. */
  82. public function getPageId()
  83. {
  84. return $this->getPageData('id');
  85. }
  86. }