Skip to content

Instantly share code, notes, and snippets.

@nitriques
Created March 14, 2014 13:58
Show Gist options
  • Save nitriques/9548145 to your computer and use it in GitHub Desktop.
Save nitriques/9548145 to your computer and use it in GitHub Desktop.
Wordpress Data source for Symphony CMS
<?php
Abstract Class WordpressSqlDatasource extends Datasource {
public $dsParamREDIRECTONEMPTY = 'no';
public function __construct($env=NULL, $process_params=true){
parent::__construct($env, $process_params);
$this->_dependencies = array();
}
public function allowEditorToParse(){
return false;
}
private function renderFilters(array $filters) {
$strFilters = '';
foreach ($filters as $col => $filter) {
$val = MySQL::cleanValue(!is_array($filter) ? $filter : $filter['val']);
if (!is_array($filter) || $filter['not-string'] != true) {
$val = "'" . $val . "'";
}
$eq = !is_array($filter) || !isset($filter['eq']) ? '=' : $filter['eq'];
$tab = !is_array($filter) || !isset($filter['table']) ? 'p' : $filter['table'];
$strFilters .= " AND `$tab`.`$col` $eq $val";
}
return $strFilters;
}
private function renderOrderBy(array $order) {
if (empty($order)) {
return '';
}
$strOrder = 'ORDER BY ';
foreach ($order as $col => $o) {
$strOrder .= " `$col` $o,";
}
return trim($strOrder, ',');
}
private function renderLimit(array $pagination) {
if (empty($pagination) || $pagination['count'] == -1) {
return '';
}
return 'LIMIT ' . $pagination['offset'] . ',' . $pagination['count'];
}
public function getWordpressImage($post_id) {
$query = "
SELECT DISTINCT `p`.`guid` FROM `wp_posts` as `p`
WHERE `p`.`ID` = $post_id
LIMIT 0,1
";
return Symphony::Database()->fetchVar('guid', 0, $query);
}
public function getStickyPosts() {
$query = "
SELECT DISTINCT `option_value`
FROM `wp_options`
WHERE `option_name` = 'sticky_posts'
LIMIT 0,1
";
$obj = Symphony::Database()->fetchVar('option_value', 0, $query);
if ($obj != null) {
return unserialize($obj);
}
return array();
}
public function getWordpressPosts($filters = array(), $order = array(), $pagination = array(), $getMetas = true, $getRelated = true, $getAttachments = false, $select = '`p`.*') {
$lang = implode("','", $this->getLangs());
$default_filters = array(
'post_type' => array('val' => 'post', 'eq' => '=', 'table' => 'p', 'not-string' => false),
'post_status' => array('val' => 'publish', 'eq' => '=', 'table' => 'p', 'not-string' => false),
);
$default_order = array(
'post_date' => 'DESC'
);
$default_pagination = array(
'offset' => 0,
'count' => 10
);
if (Symphony::Engine()->isLoggedIn()) {
unset($default_filters['post_status']);
}
$filters = array_merge($default_filters, $filters);
if ($order == null) {
$order = $default_order;
}
$pagination = array_merge($default_pagination, $pagination);
$strFilters = $this->renderFilters($filters);
$strOrder = $this->renderOrderBy($order);
$limit = $this->renderLimit($pagination);
$query = "
SELECT DISTINCT $select FROM `wp_posts` as `p`
INNER JOIN `wp_postmeta` as `m` ON `p`.`ID` = `m`.`post_id`
WHERE `m`.`meta_key` = 'langue' AND `m`.`meta_value` IN ('$lang')
$strFilters
$strOrder
$limit
";
//var_dump($query);die;
$posts = Symphony::Database()->fetch($query);
//var_dump($posts);
if (is_array($posts)) {
// for each posts
foreach ($posts as $key => $post) {
if (!isset($post['ID'])) {
continue;
}
$post_id = $post['ID'];
// Add new cols, based on existing one
if (isset($post['post_date'])) {
$posts[$key]['date_slug'] = DateTimeObj::format($post['post_date'], 'Y/m/d');
$posts[$key]['date_rss'] = DateTimeObj::format($post['post_date'], DateTime::RSS);
}
// edit existing cols
if (isset($post['post_content'])) {
$posts[$key]['post_content'] = str_replace('<p>&nbsp;</p>', '', $post['post_content']);
}
// Add metas, if we need to.
if ($getMetas) {
$query = "
SELECT DISTINCT * FROM `wp_postmeta`
WHERE `post_id` = $post_id
";
$metas = Symphony::Database()->fetch($query);
//var_dump($metas);
$metasValues = array();
foreach ($metas as $meta) {
if (strpos($meta['meta_key'], '_') !== 0) {
if ($meta['meta_key'] == 'image_entete') {
$img = $this->getWordpressImage(intval($meta['meta_value']));
$metasValues[$meta['meta_key']] = $img;
$metasValues['image_entete_formatted'] = str_replace('http://', '', $img);
} else if ($meta['meta_key'] == 'articles_lies') {
if ($getRelated) {
$obj = unserialize($meta['meta_value']);
if (is_array($obj)) {
$relatedPosts = array();
foreach ($obj as $id) {
$filter = array('ID' => array('val' => intval($id), 'not-string' => true));
$order = array();
$pagination = array('to' => 1);
$relatedPosts = array_merge($relatedPosts, $this->getWordpressPosts($filter, $order, $pagination, true, false, false));
}
$metasValues[$meta['meta_key']] = $relatedPosts;
}
}
} else {
$metasValues[$meta['meta_key']] = $meta['meta_value'];
}
} else if ($meta['meta_key'] == '_wp_attached_file') {
}
}
$posts[$key]['_metas'] = $metasValues;
//var_dump($metasValues);die;
} // end metas
// Add attachments, if we need to.
if ($getAttachments) {
$query = "
SELECT DISTINCT `p`.*, `pm`.`meta_value`
FROM `wp_posts` as `p` LEFT OUTER JOIN `wp_postmeta` as `pm`
ON `p`.`ID` = `pm`.`post_id`
WHERE `post_parent` = $post_id
AND post_type = 'attachment'
";
$attachments = Symphony::Database()->fetch($query);
//var_dump($attachments);die;
$attachmentsValues = array();
foreach ($attachments as $attachment) {
$attachmentsValues[$attachment['ID']] = array(
'title' => $attachment['post_title'],
'file' => $attachment['guid'],
'type' => $attachment['post_mime_type'],
'meta' => strlen($attachment['meta_value']) > 0 ? unserialize($attachment['meta_value']) : null,
);
}
$posts[$key]['_attachments'] = $attachmentsValues;
//var_dump($attachmentsValues);die;
} // end attachments
}
//var_dump($posts);die;
//die;
}
return $posts;
}
private function renderPosts(XMLElement &$result, array $posts) {
//var_dump($posts);die;
foreach ($posts as $post) {
$entry = new XMLElement('entry');
foreach ($post as $key => $value) {
if ($key == '_metas') {
//var_dump($value);die;
foreach ($value as $metaKey => $metaVal) {
$arrayKey = 'meta-'.strtolower(str_replace('_', '-', $metaKey));
if ($metaKey == 'articles_lies') {
$val = new XMLElement($arrayKey);
if (is_array($metaVal)) {
$this->renderPosts($val, $metaVal);
}
$entry->appendChild($val);
} else {
$val = new XMLElement($arrayKey, $metaVal);
$entry->appendChild($val);
}
}
} else if ($key == '_attachments') {
if (!empty($value)) {
$xmlAttachments = new XMLElement('attachments');
foreach ($value as $attachment) {
$xmlAttachment = new XMLElement('attachment');
$xmlAttachment->appendChild(new XMLElement('file', $attachment['file']));
$xmlAttachment->setAttribute('title', $attachment['title']);
$xmlAttachment->setAttribute('type', $attachment['type']);
if (is_array($attachment['meta'])) {
$xmlAttachMeta = new XMLElement('meta');
$xmlAttachMeta->setAttribute('width', $attachment['meta']['width']);
$xmlAttachMeta->setAttribute('height', $attachment['meta']['height']);
$xmlAttachment->appendChild($xmlAttachMeta);
}
$xmlAttachments->appendChild($xmlAttachment);
}
$entry->appendChild($xmlAttachments);
}
} else {
$key = strtolower(str_replace('_', '-', $key));
if ($key == 'post-content') {
$value = General::wrapInCData($value);
}
$val = new XMLElement($key, $value);
$entry->appendChild($val);
}
}
$result->appendChild($entry);
}
}
private function renderPagination(XMLElement &$result, array &$pagination) {
$page = new XMLElement('pagination');
$page->setAttribute('total', $pagination['total']);
$page->setAttribute('per-page', $pagination['per-page']);
$page->setAttribute('current-page', $pagination['current-page']);
$page->setAttribute('last-page', $pagination['last-page']);
$result->appendChild($page);
}
protected function getLangs() {
return array(Frontend::Page()->_param['current-language']);
}
protected function getFilters() {
return array();
}
protected function getOrderBy() {
return array();
}
protected function getPagination() {
return array();
}
protected function getPosts() {
return $this->getWordpressPosts($this->getFilters(), $this->getOrderBy(), $this->getPagination());
}
protected function getPaginationInfo() {
return null;
}
protected function outputParameters(array $posts) {
$params = array();
foreach ($posts as $post) {
$params[] = $post['_metas']['auteur'];
}
return $params;
}
public function execute(array &$param_pool = null) {
$result = new XMLElement($this->dsParamROOTELEMENT);
try {
$posts = $this->getPosts();
//var_dump(empty($posts));die;
if ($this->dsParamREDIRECTONEMPTY == 'yes' && empty($posts)) {
throw new FrontendPageNotFoundException();
}
$pagination = $this->getPaginationInfo();
if ($pagination != null) {
$this->renderPagination($result, $pagination);
}
$this->renderPosts($result, $posts);
$env = Frontend::Page()->Env();
$env['wp-auteurs.names'] = $this->outputParameters($posts);
Frontend::Page()->setEnv($env);
Frontend::Page()->_param['wp-auteurs.names'] = $env['wp-auteurs.names'];
}
catch(FrontendPageNotFoundException $e){
// Work around. This ensures the 404 page is displayed and
// is not picked up by the default catch() statement below
FrontendPageNotFoundExceptionHandler::render($e);
}
catch(Exception $e){
$result->appendChild(new XMLElement('error', $e->getMessage() . ' on ' . $e->getLine() . ' of file ' . $e->getFile()));
return $result;
}
if($this->_force_empty_result) $result = $this->emptyXMLSet();
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment