getServiceQuery()
);
$oService = new Service($oRequest, new FileCache());
$response = $oService->getData();
echo sprintf(
"%s%s%s%s%s",
$before_widget,
$before_title,
$this->getTitle(),
$after_title,
$this->getContent($response),
$after_widget
);
}
/**
* Return the widget title from the widget settigns
*
* @return string
*/
public function getTitle() {
$aSetting = $this->getSettings();
return $aSetting['title'];
}
/**
* Return the widgets content for given data
*
* @param array $response
* @return string
*/
public function getContent($data) {
if(empty($data)) {
return "There is currently no data to share.
";
}
$content .= '';
$content .= $this->getDataHeaders($data);
$content .= '
';
$content .= $this->getDataContent($data);
$content .= '
';
return $content;
}
/**
* Returns the headers for the given analytics data
*
* @param array $data
* @return string
*/
protected function getDataHeaders($data) {
$oData = current($data);
$aHeader = array();
foreach($oData as $key => $value) {
$aHeader[] = sprintf('%s | ', strtolower($key), ucfirst($key));
}
return join('', $aHeader);
}
/**
* Returns the content for the given analytics data
*
* @param array $data
* @return string
*/
protected function getDataContent($data) {
$content = '';
foreach($data as $oData) {
$content .= '';
foreach($oData as $key => $value) {
$content .= sprintf('| %s | ', strtolower($key), ucfirst($value));
}
$content .= '
';
}
return $content;
}
/**
* Creates parameters to send to the API based on current
* widget settings
*
* @return array
*/
protected function getServiceQuery() {
$aQuery = array();
$aSetting = $this->getSettings();
if(isset($aSetting['service']) && $aSetting['service']) {
$aQuery[] = new QueryParameter('service', $aSetting['service']);
}
if(isset($aSetting['period']) && $aSetting['period']) {
$aQuery[] = new QueryParameter('period', $aSetting['period']);
}
return $aQuery;
}
/**
* Returns settings for the current widget
*
* @return array
*/
protected function getSettings() {
$aSetting = $this->get_settings();
$aSetting = $aSetting[$this->number];
return $aSetting;
}
}
add_action('widgets_init', create_function('', 'return register_widget("AddThisAnalyticsWidget");'));