WYSIJA_model_wp_posts

The MailPoet Newsletters WYSIJA model wp posts class.

Defined (1)

The class is defined in the following location(s).

/models/wp_posts.php  
  1. class WYSIJA_model_wp_posts extends WYSIJA_model { 
  2.  
  3. var $pk = 'ID'; 
  4. var $tableWP = true; 
  5. var $table_name = 'posts'; 
  6. var $columns = array( 
  7. 'ID' => array( 
  8. 'req' => true,  
  9. 'type' => 'integer' 
  10. ),  
  11. 'post_author' => array('type' => 'integer'),  
  12. 'post_date' => array(),  
  13. 'post_date_gmt' => array(),  
  14. 'post_content' => array(),  
  15. 'post_title' => array(),  
  16. 'post_excerpt' => array(),  
  17. 'post_status' => array(),  
  18. 'comment_status' => array(),  
  19. 'ping_status' => array(),  
  20. 'post_password' => array(),  
  21. 'post_name' => array(),  
  22. 'to_ping' => array(),  
  23. 'pinged' => array(),  
  24. 'post_modified' => array(),  
  25. 'post_modified_gmt' => array(),  
  26. 'post_content_filtered' => array(),  
  27. 'post_parent' => array('type' => 'integer'),  
  28. 'guid' => array(),  
  29. 'menu_order' => array('type' => 'integer'),  
  30. 'post_type' => array(),  
  31. 'post_mime_type' => array(),  
  32. 'comment_count' => array('type' => 'integer'),  
  33. ); 
  34.  
  35. function __construct() { 
  36. parent::__construct(); 
  37. $this->table_prefix = ''; 
  38.  
  39. function get_posts($args = array()) { 
  40. /** 
  41. * SELECT A.ID, A.post_title, A.post_content, A.post_date FROM `wp_posts` A 
  42. * LEFT JOIN `wp_term_relationships` B ON (A.ID = B.object_id) 
  43. * LEFT JOIN `wp_term_taxonomy` C ON (C.term_taxonomy_id = B.term_taxonomy_id) 
  44. * WHERE C.term_id IN (326) AND A.post_type IN ('post') AND A.post_status IN ('publish') ORDER BY post_date DESC LIMIT 0, 10; 
  45. */ 
  46. $default_args = array( 
  47. 'post_limit' => 10,  
  48. 'offset' => 0,  
  49. 'category' => null,  
  50. 'not_category' => null,  
  51. 'orderby' => 'post_date',  
  52. 'order' => 'DESC',  
  53. 'include' => null,  
  54. 'exclude' => null,  
  55. 'meta_key' => null,  
  56. 'meta_value' => null,  
  57. 'post_type' => null,  
  58. 'post_mime_type' => null,  
  59. 'post_parent' => null,  
  60. 'post_status' => 'publish',  
  61. 'post_date' => null,  
  62. 'is_search_query' => false,  
  63. 'search' => null 
  64. ); 
  65.  
  66. $args = array_merge($default_args, $args); 
  67.  
  68. // set categories 
  69. if(isset($args['category_ids']) && strlen(trim($args['category_ids'])) > 0) { 
  70. $args['category'] = explode(', ', trim($args['category_ids'])); 
  71. } else { 
  72. if(isset($args['post_category']) && (int) $args['post_category'] > 0) { 
  73. $args['category'] = (int) $args['post_category']; 
  74. if(isset($args['include_category_ids']) && !empty($args['include_category_ids'])) { 
  75. $args['category'] = $args['include_category_ids']; 
  76. if(isset($args['exclude_category_ids']) && !empty($args['exclude_category_ids'])) { 
  77. $args['not_category'] = $args['exclude_category_ids']; 
  78.  
  79. // default selected fields 
  80. $post_fields = array( 
  81. 'A.ID',  
  82. 'A.post_title',  
  83. 'A.post_content',  
  84. 'A.post_excerpt',  
  85. 'A.post_author',  
  86. 'A.post_type',  
  87. 'A.post_status' 
  88. ); 
  89.  
  90. $additional_post_fields = array( 
  91. 'post_date',  
  92. 'post_date_gmt',  
  93. 'comment_status',  
  94. 'ping_status',  
  95. 'post_name',  
  96. 'to_ping',  
  97. 'pinged',  
  98. 'post_modified',  
  99. 'post_modified_gmt',  
  100. 'post_content_filtered',  
  101. 'post_parent',  
  102. 'guid',  
  103. 'menu_order',  
  104. 'post_mime_type',  
  105. 'comment_count' 
  106. ); 
  107.  
  108. // look for manual fields to select 
  109. if(isset($args['post_fields']) && is_array($args['post_fields']) && !empty($args['post_fields'])) { 
  110. $extra_post_fields = array_values( 
  111. array_intersect( 
  112. $additional_post_fields,  
  113. array_map('esc_sql', $args['post_fields']) 
  114. ); 
  115. // merge both fields selection 
  116. $post_fields = array_merge(array('A.ID'), $extra_post_fields); 
  117.  
  118. $query = sprintf('SELECT DISTINCT %s FROM `[wp]' . $this->table_name . '` A ', join(', ', $post_fields)); 
  119.  
  120. if($args['is_search_query'] === true) { 
  121. $count_query = 'SELECT COUNT(DISTINCT A.ID) as total FROM `[wp]' . $this->table_name . '` A '; 
  122.  
  123. // search by category 
  124. if((isset($args['category']) && !empty($args['category'])) || (isset($args['not_category']) && !empty($args['not_category']))) { 
  125. $query_joins = 'JOIN `[wp]term_relationships` B ON (A.ID = B.object_id) '; 
  126. $query_joins .= 'JOIN `[wp]term_taxonomy` C ON (C.term_taxonomy_id = B.term_taxonomy_id) '; 
  127.  
  128. $query .= $query_joins; 
  129.  
  130. if($args['is_search_query'] === true) { 
  131. $count_query .= $query_joins; 
  132.  
  133. $conditions = array(); 
  134.  
  135. if(isset($args['include']) && $args['include'] !== null) { 
  136. $conditions[] = array( 
  137. 'col' => 'A.ID',  
  138. 'sign' => 'IN',  
  139. 'val' => $args['include'],  
  140. 'cast' => 'int' 
  141. ); 
  142. } else { 
  143. foreach ($args as $type => $value) { 
  144. if(!$value) continue; 
  145. switch ($type) { 
  146. case 'category': 
  147. $conditions[] = array( 
  148. 'col' => 'C.term_id',  
  149. 'sign' => 'IN',  
  150. 'val' => $value,  
  151. 'cast' => 'int' 
  152. ); 
  153. break; 
  154. case 'not_category': 
  155. $conditions[] = array( 
  156. 'col' => 'C.term_id',  
  157. 'sign' => 'NOT IN',  
  158. 'val' => $value,  
  159. 'cast' => 'int' 
  160. ); 
  161. break; 
  162. case 'include': 
  163. $conditions[] = array( 
  164. 'col' => 'A.ID',  
  165. 'sign' => 'IN',  
  166. 'val' => $value,  
  167. 'cast' => 'int' 
  168. ); 
  169. break; 
  170. case 'exclude': 
  171. $conditions[] = array( 
  172. 'col' => 'A.ID',  
  173. 'sign' => 'NOT IN',  
  174. 'val' => $value,  
  175. 'cast' => 'int' 
  176. ); 
  177. break; 
  178. case 'cpt': // this is for backwards compatibility's sake 
  179. case 'post_type': 
  180. $conditions[] = array( 
  181. 'col' => 'A.post_type',  
  182. 'sign' => 'IN',  
  183. 'val' => $value 
  184. ); 
  185. break; 
  186. case 'post_status': 
  187. $conditions[] = array( 
  188. 'col' => 'A.post_status',  
  189. 'sign' => 'IN',  
  190. 'val' => $value 
  191. ); 
  192. break; 
  193. case 'post_date': 
  194. // apply timezone to date value 
  195. $helper_toolbox = WYSIJA::get('toolbox', 'helper'); 
  196. $value = $helper_toolbox->time_tzed($value); 
  197.  
  198. if($value !== '') { 
  199. $conditions[] = array( 
  200. 'col' => 'A.post_date',  
  201. 'sign' => '>',  
  202. 'val' => $value 
  203. ); 
  204. break; 
  205. case 'search': 
  206. $conditions[] = array( 
  207. 'col' => 'A.post_title',  
  208. 'sign' => 'LIKE',  
  209. 'val' => '%' . $value . '%' 
  210. ); 
  211. break; 
  212.  
  213. // set static conditions for post statuses (we don't want drafts and such to appear in search results) 
  214. if($args['include'] === null) { 
  215. $conditions[] = array( 
  216. 'col' => 'A.post_status',  
  217. 'sign' => 'NOT IN',  
  218. 'val' => array( 
  219. 'auto-draft',  
  220. 'inherit' 
  221. ); 
  222.  
  223. // where conditions 
  224. if(!empty($conditions)) { 
  225. $query_conditions = $this->build_conditions($conditions); 
  226.  
  227. $query .= $query_conditions; 
  228.  
  229. if($args['is_search_query'] === true) { 
  230. $count_query .= $query_conditions; 
  231.  
  232. // order by 
  233. if(isset($args['orderby'])) { 
  234. $query .= ' ORDER BY ' . $args['orderby']; 
  235. if(isset($args['sort_by'])) { 
  236. $query .= ' ' . (($args['sort_by'] === 'newest') ? 'DESC' : 'ASC'); 
  237. } else { 
  238. if(isset($args['order'])) { 
  239. $query .= ' ' . $args['order']; 
  240.  
  241. // set limit (only if we are not requesting posts based on their id) 
  242. if(array_key_exists('include', $args) && $args['include'] === null) { 
  243. $query_offset = (isset($args['query_offset']) ? (int) $args['query_offset'] : 0); 
  244. $query_limit = ((isset($args['post_limit']) && (int) $args['post_limit'] > 0) ? (int) $args['post_limit'] : 10); 
  245. $query .= sprintf(' LIMIT %d, %d', $query_offset, $query_limit); 
  246.  
  247. if($args['is_search_query'] === true) { 
  248. return array( 
  249. 'rows' => $this->query('get_res', $query),  
  250. 'count' => $this->query('get_row', $count_query) 
  251. ); 
  252. } else { 
  253. return $this->query('get_res', $query); 
  254.  
  255. function build_conditions($conditions) { 
  256. $query = ''; 
  257. $i = 0; 
  258.  
  259. foreach ($conditions as $key => $data) { 
  260.  
  261. if($i > 0) $query .= ' AND '; 
  262.  
  263. $query .= $data['col'] . ' '; 
  264.  
  265. $value = $data['val']; 
  266.  
  267. switch ($data['sign']) { 
  268. case 'IN': 
  269. case 'NOT IN': 
  270. $values = ''; 
  271. if(is_array($value)) { 
  272. if(array_key_exists('cast', $data) && $data['cast'] === 'int') { 
  273. $count = count($value); 
  274. for ($j = 0; $j < $count; $j++) { 
  275. if($value[$j] === null) continue; 
  276. $value[$j] = intval($value[$j]); 
  277. $values = join(', ', $value); 
  278. } else { 
  279. $values = "'" . join("', '", $value) . "'"; 
  280. $query .= $data['sign'] . ' (' . $values . ')'; 
  281. } else { 
  282. if(strpos($value, ', ') === false) { 
  283. // single value 
  284. if(array_key_exists('cast', $data) && $data['cast'] === 'int') { 
  285. $query .= '= ' . (int) $value; 
  286. } else { 
  287. $query .= '= "' . $value . '"'; 
  288. } else { 
  289. // multiple values 
  290. $values = "'" . join("', '", explode(', ', $value)) . "'"; 
  291. $query .= $data['sign'] . ' (' . $values . ')'; 
  292. break; 
  293. case 'LIKE': 
  294. $query .= ' LIKE "' . $value . '"'; 
  295. break; 
  296. default: 
  297. $sign = '='; 
  298. if(isset($data['sign'])) $sign = $data['sign']; 
  299.  
  300. if(array_key_exists('cast', $data) && $data['cast'] === 'int') { 
  301. $query .= $sign . (int) $value . " "; 
  302. } else { 
  303. $query .= $sign . "'" . $value . "' "; 
  304. $i++; 
  305.  
  306. if($query === '') { 
  307. return ''; 
  308. } else { 
  309. return 'WHERE ' . $query;