YaWK  24.1
Yet another WebKit
fbCover.php
Go to the documentation of this file.
1 <?php
3 {
4  /**
5  * @details<b>Use Facebook Graph API to get you latest cover image of your Facebook Page.
6  * Require an App ID and a valid Access Token.</b>
7  *
8  * <p>This widget shows always your current (newest / latest) cover image of your Facebook page.
9  * It is meant to be used as header on top of your template design - or wherever it makes sense.
10  * </p>
11  *
12  *
13  * @author Daniel Retzl <[email protected]>
14  * @copyright 2018 Daniel Retzl
15  * @version 1.0.0
16  * @brief Facebook Cover widget - Embed current cover image from your Facebook page.
17  */
18 class fbCover
19 {
20  /** @param string your app ID (from developers.facebook.com) */
21  public $fbCoverAppId = '';
22  /** @param string your access token (secret word from developers.facebook.com) */
23  public $fbCoverAccessToken = '';
24  /** @param string css class to be set on the cover image */
25  public $fbCoverImageClass = '';
26  /** @param string image width in px or % */
27  public $fbCoverImageWidth = '';
28  /** @param string image height in px or % */
29  public $fbCoverImageHeight = '';
30  /** @param string any title you like (important for SEO) */
31  public $fbCoverImageTitle = '';
32  /** @param string any image alt tag you like (for screenreaders) */
33  public $fbCoverImageAlt = '';
34  /** @param string custom css style attributes */
35  public $fbCoverImageStyle = '';
36  /** @param string true|false was the js SDK loaded? */
37  public $jsSDKLoaded = 'false';
38  /** @param array array: holds the current widget settings */
39  public $settings;
40  /** @param object api result (as object) */
41  public $apiObject;
42 
43  /**
44  * fbCover constructor. Loads all widget settings
45  * @param $db
46  */
47  public function __construct($db)
48  {
49  // create new widget object
50  $widget = new \YAWK\widget();
51  // load this widget settings from db into array
52  $this->settings = $widget->getWidgetSettingsArray($db);
53  // add settings to object property
54  foreach ($this->settings as $property => $value)
55  {
56  $this->$property = $value;
57  }
58  // check if required settings are set
59  $this->checkRequirements();
60  }
61 
62  /**
63  * call check app id and access token
64  *
65  */
66  public function checkRequirements()
67  {
68  $this->checkAppId();
69  $this->checkAccessToken();
70  }
71 
72  /**
73  * check if app id is set
74  * @return bool return true or die with error message
75  */
76  public function checkAppId()
77  { // check of app id is set and not empty
78  if (isset($this->fbCoverAppId) && (!empty($this->fbCoverAppId)))
79  { // app id is set - check if its a number
80  if (is_numeric($this->fbCoverAppId))
81  { // ok, seems good
82  return true;
83  }
84  else
85  { // not a number - abort with error msg
86  die ("app ID is set, but not a numeric value! Please check your app ID - it should contain numbers only.");
87  }
88  }
89  else
90  { // app id not set or empty - abort with error msg
91  die ("app ID is not set. Please add your app ID. You can obtain it from http://developers.facebook.com");
92  }
93  }
94 
95  /**
96  * check if access token is set
97  * @return bool return true or die with error message
98  */
99  public function checkAccessToken()
100  { // check if access token is set and not empty
101  if (isset($this->fbCoverAccessToken) && (!empty($this->fbCoverAccessToken)))
102  { // check if access token is a string
103  if (is_string($this->fbCoverAccessToken))
104  { // ok...
105  return true;
106  }
107  else
108  { // access token is not a string - abort with error msg
109  die ("Access token is set, but not a string value! Please check your access token.");
110  }
111  }
112  else
113  { // access token is not set or empty - abort with error msg
114  die ("Access token is not set. Please add your access token. You can obtain it from http://developers.facebook.com");
115  }
116  }
117 
118  /**
119  * Load Facebook JS Code
120  */
121  public function loadJSSDK()
122  { // check if fb JS SDK was loaded before
123  if ($this->jsSDKLoaded == 'false')
124  { // check if app ID is set
125  if ($this->checkAppId() == true)
126  {
127  /*
128  // include facebook SDK JS
129  echo "<script>
130  window.fbAsyncInit = function() {
131  FB.init({
132  appId : '" . $this->fbCoverAppId . "',
133  xfbml : true,
134  version : 'v3.1'
135  });
136  FB.AppEvents.logPageView();
137  };
138 
139  (function(d, s, id){
140  var js, fjs = d.getElementsByTagName(s)[0];
141  if (d.getElementById(id)) {return;}
142  js = d.createElement(s); js.id = id;
143  js.src = \"https://connect.facebook.net/en_US/sdk.js\";
144  fjs.parentNode.insertBefore(js, fjs);
145  }(document, 'script', 'facebook-jssdk'));
146  </script>";
147  $this->jsSDKLoaded = 'true';
148  */
149  }
150  else
151  {
152  die ("unable to include facebook js SDK - checkAppId failed. Please check your app ID in the widget settings!");
153  }
154  }
155  }
156 
157  /**
158  * Prepare and make the API call and decode the json into the apiObject property
159  * @return mixed Returns an array as obj property, containing the data
160  */
161  public function makeApiCall()
162  {
163  // prepare API call
164  $json_link = "https://graph.facebook.com/v3.3/me?fields=cover&access_token={$this->fbCoverAccessToken}";
165 
166  $curl = curl_init();
167  curl_setopt($curl, CURLOPT_URL, $json_link);
168  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
169  // decode json and create object
170  $this->apiObject = json_decode(curl_exec($curl), true, 512, JSON_BIGINT_AS_STRING);
171  curl_close($curl);
172 
173  return $this->apiObject;
174 
175  // OUTDATED:
176  // get json string
177  // $json = file_get_contents($json_link);
178  // convert json to object
179  // return $this->apiObject = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
180  }
181 
182 
183  /**
184  * Draws the cover image onto screen.
185  *
186  */
187  public function drawCoverImage()
188  {
189  $this->loadJSSDK();
190  $this->makeApiCall();
191 
192  // check if image class is set
193  if (isset($this->fbCoverImageClass) && (empty($this->fbCoverImageClass)))
194  { // if not, use this as default value:
195  $this->fbCoverImageClass = "img-responsive";
196  }
197 
198  // check if alt tag was set
199  if (isset($this->fbCoverImageAlt) && (!empty($this->fbCoverImageAlt)))
200  { // it is, generate markup
201  $altMarkup = " alt=\"$this->fbCoverImageAlt\"";
202  }
203  else
204  { // not set, leave empty
205  $altMarkup = '';
206  }
207 
208  // check if cover image title is set
209  if (isset($this->fbCoverImageTitle) && (!empty($this->fbCoverImageTitle)))
210  { // it is, generate markup
211  $titleMarkup = " title=\"$this->fbCoverImageTitle\"";
212  }
213  else
214  { // not set, leave empty
215  $titleMarkup = '';
216  }
217 
218  // check if image height is set
219  if (isset($this->fbCoverImageHeight) && (!empty($this->fbCoverImageHeight)))
220  { // it is, set markup
221  $heightMarkup = " height=\"$this->fbCoverImageHeight;\"";
222  }
223  else
224  { // leave empty
225  $heightMarkup = '';
226  }
227 
228  // check if image width is set
229  if (isset($this->fbCoverImageWidth) && (!empty($this->fbCoverImageWidth)))
230  { // if height is set
231  if (isset($heightMarkup) && (!empty($heightMarkup)))
232  { // leave a space between tags
233  $widthMarkup = " width=\"$this->fbCoverImageWidth\"";
234  }
235  else
236  { // height is not set - no space needed
237  $widthMarkup = "width=\"$this->fbCoverImageWidth\"";
238  }
239  }
240  else
241  { // width not set - leave markup empty
242  $widthMarkup = '';
243  }
244 
245  // check if css style is set
246  if (isset($this->fbCoverImageStyle) && (!empty($this->fbCoverImageStyle)))
247  { // add style markup
248  $styleMarkup = " style=\"$this->fbCoverImageStyle\"";
249  }
250  else
251  { // no style markup needed
252  $styleMarkup = '';
253  }
254 
255  // finally: output the cover image
256  echo "<img src=\"".$this->apiObject['cover']['source']."\" class=\"$this->fbCoverImageClass\"$styleMarkup"."$heightMarkup"."$widthMarkup"."$altMarkup"."$titleMarkup>";
257  }
258 
259 } // end class events
260 } // end namespace
die
Definition: block-user.php:27
Facebook Cover widget - Embed current cover image from your Facebook page.
Definition: fbCover.php:18
Settings class: get and set YaWK system settings.
Definition: settings.php:9
$value