/src/api/classes/api.php

https://github.com/72lions/v1.72lions.com · PHP · 341 lines · 192 code · 85 blank · 64 comment · 24 complexity · 779280ac5e4c54687426a54eb8dc71de MD5 · raw file

  1. <?php
  2. /**
  3. * Responsible for retrieving all the necessary data from the wp database
  4. *
  5. * @module 72lionsPHP
  6. * @class API
  7. * @author Thodoris Tsiridis
  8. * @version 1.0
  9. */
  10. class API {
  11. /**
  12. * Constants
  13. */
  14. protected static $XML = '';
  15. protected static $DB_USERNAME = 'root';
  16. protected static $DB_PASSWORD = '';
  17. protected static $DB_HOST = 'localhost';
  18. protected static $DB_NAME = '72lionswp';
  19. /**
  20. * Protected variables
  21. */
  22. protected $posts = array();
  23. protected $pages = array();
  24. /**
  25. * Returns an array with all the categories
  26. *
  27. * @param {Number} $start The beginning of the result set
  28. * @param {Number} $total The total items to laod
  29. * @param {String} $sort The sorting
  30. * @return {Array}
  31. * @author Thodoris Tsiridis
  32. */
  33. public function getCategories($start = 0, $total = 10, $sort = 'name ASC') {
  34. $query = "SELECT WT.* FROM wp_terms WT, wp_term_taxonomy WTT
  35. WHERE WT.term_id = WTT.term_id
  36. AND taxonomy='category'
  37. ORDER BY WT.".$sort."
  38. LIMIT ".$start.",".$total;
  39. if(MC::get($query) == null){
  40. $db = new DB();
  41. $db->connect(self::$DB_USERNAME, self::$DB_PASSWORD, self::$DB_HOST, self::$DB_NAME);
  42. $result = mysql_query($query) or die('Class '.__CLASS__.' -> '.__FUNCTION__.' : ' . mysql_error());
  43. while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  44. $category = new Category($row);
  45. Category::addCategory($category);
  46. MC::set('category'.$category->id, $category);
  47. }
  48. $db->disconnect();
  49. unset($db);
  50. MC::set($query, Category::$categories);
  51. unset($result);
  52. }
  53. return MC::get($query);
  54. }
  55. /**
  56. * Returns an array with all the posts
  57. *
  58. * @param {Number} $categoryId The id of the parent category
  59. * @param {Number} $start The beginning of the result set
  60. * @param {Number} $total The total items to laod
  61. * @param {String} $sort The sorting
  62. * @return {Array}
  63. * @author Thodoris Tsiridis
  64. */
  65. public function getPosts($categoryId = null, $tagId = null, $start = 0, $total = 10, $sort = 'post_date DESC') {
  66. $db = new DB();
  67. $db->connect(self::$DB_USERNAME, self::$DB_PASSWORD, self::$DB_HOST, self::$DB_NAME);
  68. if ($tagId !== null) {
  69. $query = "SELECT * FROM wp_posts WPP,
  70. wp_term_taxonomy WPTT,
  71. wp_term_relationships WPTR
  72. WHERE WPP.post_status='publish'
  73. AND WPP.post_type='post'
  74. AND WPTT.term_id=".mysql_real_escape_string($tagId)."
  75. AND WPTT.taxonomy='post_tag'
  76. AND WPTR.term_taxonomy_id = WPTT.term_taxonomy_id
  77. AND WPTR.object_id = WPP.ID
  78. ORDER BY ".$sort."
  79. LIMIT ".$start.",".$total;
  80. } else if ($categoryId !== null){
  81. $query = "SELECT * FROM wp_posts WPP,
  82. wp_term_taxonomy WPTT,
  83. wp_term_relationships WPTR
  84. WHERE WPP.post_status='publish'
  85. AND WPP.post_type='post'
  86. AND WPTT.term_id=".mysql_real_escape_string($categoryId)."
  87. AND WPTT.taxonomy='category'
  88. AND WPTR.term_taxonomy_id = WPTT.term_taxonomy_id
  89. AND WPTR.object_id = WPP.ID
  90. ORDER BY ".$sort."
  91. LIMIT ".$start.",".$total;
  92. } else {
  93. $query = "SELECT * FROM wp_posts
  94. WHERE post_status='publish'
  95. AND post_type='post'
  96. ORDER BY ".$sort."
  97. LIMIT ".$start.",".$total;
  98. }
  99. if(MC::get($query) == null){
  100. $result = mysql_query($query) or die('Class '.__CLASS__.' -> '.__FUNCTION__.' : ' . mysql_error());
  101. while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  102. // Create a post
  103. $post = $this->createPostObject($row);
  104. $this->posts[] = $post;
  105. MC::set('post'.$post->id, $post);
  106. MC::set($post->slug, $post);
  107. }
  108. unset($result);
  109. MC::set($query, $this->posts);
  110. }
  111. $db->disconnect();
  112. unset($db);
  113. return MC::get($query);
  114. }
  115. /**
  116. * Returns an array with all the pages
  117. *
  118. * @param {Number} $categoryId The id of the parent category
  119. * @param {Number} $start The beginning of the result set
  120. * @param {Number} $total The total items to laod
  121. *
  122. * @return {Array}
  123. * @author Thodoris Tsiridis
  124. */
  125. public function getPages($categoryId = null, $start = 0, $total = 10, $sort = 'post_date DESC') {
  126. if($categoryId !== null){
  127. $query = "SELECT * FROM wp_posts
  128. WHERE post_status='publish'
  129. AND post_type='page'
  130. ORDER BY ".$sort."
  131. LIMIT ".$start.",".$total;
  132. } else {
  133. $query = "SELECT * FROM wp_posts
  134. WHERE post_status='page'
  135. AND post_type='post'
  136. ORDER BY ".$sort."
  137. LIMIT ".$start.",".$total;
  138. }
  139. if(MC::get($query) == null){
  140. $db = new DB();
  141. $db->connect(self::$DB_USERNAME, self::$DB_PASSWORD, self::$DB_HOST, self::$DB_NAME);
  142. $result = mysql_query($query) or die('Class '.__CLASS__.' -> '.__FUNCTION__.' : ' . mysql_error());
  143. while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  144. $post = new Post($row);
  145. $this->pages[] = $post;
  146. MC::set('page'.$post->id, $post);
  147. MC::set($post->slug, $post);
  148. }
  149. $db->disconnect();
  150. unset($db);
  151. MC::set($query, $this->pages);
  152. }
  153. return MC::get($query);
  154. }
  155. /**
  156. * Gets the details of a post
  157. *
  158. * @param {Number} $postId The id of the post that we want to get the details
  159. * @return {Object}
  160. * @author Thodoris Tsiridis
  161. */
  162. public function getPostDetails($postId) {
  163. if(MC::get($postId) == null) {
  164. $db = new DB();
  165. $db->connect(self::$DB_USERNAME, self::$DB_PASSWORD, self::$DB_HOST, self::$DB_NAME);
  166. $query = "SELECT * FROM wp_posts
  167. WHERE post_status='publish'
  168. AND (post_type='post' || post_type='page')
  169. AND post_name='".mysql_real_escape_string($postId)."'";
  170. $result = mysql_query($query) or die('Class '.__CLASS__.' -> '.__FUNCTION__.' : ' . mysql_error());
  171. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  172. $total = mysql_num_rows($result);
  173. if($total > 0) {
  174. $post = $this->createPostObject($row);
  175. if($row['post_type'] === 'post'){
  176. MC::set('post'.$post->id, $post);
  177. } else {
  178. MC::set('page'.$post->id, $post);
  179. }
  180. MC::set($post->slug, $post);
  181. }
  182. $db->disconnect();
  183. unset($db);
  184. }
  185. return MC::get($postId);
  186. }
  187. /**
  188. * Gets the details of a post
  189. *
  190. * @param {Number} $postId The id of the post that we want to get the details
  191. * @return {Object}
  192. * @author Thodoris Tsiridis
  193. */
  194. public function getComments($postId) {
  195. //TODO: First check memcache
  196. }
  197. protected function createPostObject($row) {
  198. $post = new Post($row);
  199. // Get the attachments
  200. $queryAt = "SELECT * FROM wp_postmeta
  201. WHERE post_id=".$row['ID'];
  202. $resultAt = mysql_query($queryAt) or die('Class '.__CLASS__.' -> '.__FUNCTION__.' : ' . mysql_error());
  203. while($rowAt = mysql_fetch_array($resultAt, MYSQL_ASSOC)){
  204. //Get the thumbnail
  205. if($rowAt['meta_key'] === '_thumbnail_id') {
  206. $queryThumbnail = "SELECT * FROM wp_postmeta
  207. WHERE post_id=" . $rowAt['meta_value'];
  208. $resultTh = mysql_query($queryThumbnail) or die('Class '.__CLASS__.' -> '.__FUNCTION__.' : ' . mysql_error());
  209. while($rowTh = mysql_fetch_array($resultTh, MYSQL_ASSOC)){
  210. if($rowTh['meta_key'] === '_wp_attached_file') {
  211. $post->thumbnail['File'] = $rowTh['meta_value'];
  212. }
  213. if($rowTh['meta_key'] === '_wp_attachment_metadata') {
  214. $post->thumbnail['Data'] = unserialize($rowTh['meta_value']);
  215. }
  216. }
  217. unset($resultTh);
  218. } else {
  219. // Get the rest of the data
  220. $post->addMeta($rowAt['meta_key'], $rowAt['meta_value']);
  221. }
  222. }
  223. // Get the categories
  224. $queryCats = "SELECT WT.* FROM wp_terms WT, wp_term_taxonomy WTT, wp_term_relationships WPTR
  225. WHERE WT.term_id = WTT.term_id
  226. AND WPTR.term_taxonomy_id = WTT.term_taxonomy_id
  227. AND WPTR.object_id = ".$post->id."
  228. AND WTT.taxonomy='category'
  229. ORDER BY WT.name ASC";
  230. $resultCats = mysql_query($queryCats) or die('Class '.__CLASS__.' -> '.__FUNCTION__.' : ' . mysql_error());
  231. while($rowCat = mysql_fetch_array($resultCats, MYSQL_ASSOC)){
  232. if(MC::get('category'.$rowCat['term_id']) == null){
  233. $category = new Category($rowCat);
  234. Category::addCategory($category);
  235. MC::set('category'.$category->id, $category);
  236. }
  237. $post->addToCategory(MC::get('category'.$rowCat['term_id']));
  238. }
  239. unset($resultCats);
  240. // Get the tags
  241. $queryTags = "SELECT WT.* FROM wp_terms WT, wp_term_taxonomy WTT, wp_term_relationships WPTR
  242. WHERE WT.term_id = WTT.term_id
  243. AND WPTR.term_taxonomy_id = WTT.term_taxonomy_id
  244. AND WPTR.object_id = ".$post->id."
  245. AND WTT.taxonomy='post_tag'
  246. ORDER BY WT.name ASC";
  247. $resultTags= mysql_query($queryTags) or die('Class '.__CLASS__.' -> '.__FUNCTION__.' : ' . mysql_error());
  248. while($rowTags = mysql_fetch_array($resultTags, MYSQL_ASSOC)){
  249. $post->addTag($rowTags['name'], $rowTags['slug'], $rowTags['term_id']);
  250. }
  251. unset($resultTags);
  252. unset($resultAt);
  253. return $post;
  254. }
  255. }
  256. ?>