PageRenderTime 25ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wysija-newsletters/models/wp_posts.php

https://gitlab.com/meixnertobias/thaimaidaiproductionwp
PHP | 348 lines | 297 code | 32 blank | 19 comment | 56 complexity | dd4c92eaea7797e4d1d8bd0bcaa50c53 MD5 | raw file
  1. <?php
  2. defined('WYSIJA') or die('Restricted access');
  3. class WYSIJA_model_wp_posts extends WYSIJA_model {
  4. var $pk = 'ID';
  5. var $tableWP = true;
  6. var $table_name = 'posts';
  7. var $columns = array(
  8. 'ID' => array(
  9. 'req' => true,
  10. 'type' => 'integer'
  11. ),
  12. 'post_author' => array('type' => 'integer'),
  13. 'post_date' => array(),
  14. 'post_date_gmt' => array(),
  15. 'post_content' => array(),
  16. 'post_title' => array(),
  17. 'post_excerpt' => array(),
  18. 'post_status' => array(),
  19. 'comment_status' => array(),
  20. 'ping_status' => array(),
  21. 'post_password' => array(),
  22. 'post_name' => array(),
  23. 'to_ping' => array(),
  24. 'pinged' => array(),
  25. 'post_modified' => array(),
  26. 'post_modified_gmt' => array(),
  27. 'post_content_filtered' => array(),
  28. 'post_parent' => array('type' => 'integer'),
  29. 'guid' => array(),
  30. 'menu_order' => array('type' => 'integer'),
  31. 'post_type' => array(),
  32. 'post_mime_type' => array(),
  33. 'comment_count' => array('type' => 'integer'),
  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. */
  47. $default_args = array(
  48. 'post_limit' => 10,
  49. 'offset' => 0,
  50. 'category' => null,
  51. 'not_category' => null,
  52. 'orderby' => 'post_date',
  53. 'order' => 'DESC',
  54. 'include' => null,
  55. 'exclude' => null,
  56. 'meta_key' => null,
  57. 'meta_value' => null,
  58. 'post_type' => null,
  59. 'post_mime_type' => null,
  60. 'post_parent' => null,
  61. 'post_status' => 'publish',
  62. 'post_date' => null,
  63. 'is_search_query' => false,
  64. 'search' => null
  65. );
  66. $args = array_merge($default_args, $args);
  67. // set categories
  68. if(isset($args['category_ids']) && strlen(trim($args['category_ids'])) > 0) {
  69. $args['category'] = explode(',', trim($args['category_ids']));
  70. } else {
  71. if(isset($args['post_category']) && (int) $args['post_category'] > 0) {
  72. $args['category'] = (int) $args['post_category'];
  73. }
  74. }
  75. if(isset($args['include_category_ids']) && !empty($args['include_category_ids'])) {
  76. $args['category'] = $args['include_category_ids'];
  77. }
  78. if(isset($args['exclude_category_ids']) && !empty($args['exclude_category_ids'])) {
  79. $args['not_category'] = $args['exclude_category_ids'];
  80. }
  81. // default selected fields
  82. $post_fields = array(
  83. 'A.ID',
  84. 'A.post_title',
  85. 'A.post_content',
  86. 'A.post_excerpt',
  87. 'A.post_author',
  88. 'A.post_type',
  89. 'A.post_status'
  90. );
  91. $additional_post_fields = array(
  92. 'post_date',
  93. 'post_date_gmt',
  94. 'comment_status',
  95. 'ping_status',
  96. 'post_name',
  97. 'to_ping',
  98. 'pinged',
  99. 'post_modified',
  100. 'post_modified_gmt',
  101. 'post_content_filtered',
  102. 'post_parent',
  103. 'guid',
  104. 'menu_order',
  105. 'post_mime_type',
  106. 'comment_count'
  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. );
  116. // merge both fields selection
  117. $post_fields = array_merge(array('A.ID'), $extra_post_fields);
  118. }
  119. $query = sprintf('SELECT DISTINCT %s FROM `[wp]' . $this->table_name . '` A ', join(', ', $post_fields));
  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. $query .= $query_joins;
  128. if($args['is_search_query'] === true) {
  129. $count_query .= $query_joins;
  130. }
  131. }
  132. $conditions = array();
  133. if(isset($args['include']) && $args['include'] !== null) {
  134. $conditions[] = array(
  135. 'col' => 'A.ID',
  136. 'sign' => 'IN',
  137. 'val' => $args['include'],
  138. 'cast' => 'int'
  139. );
  140. } else {
  141. foreach ($args as $type => $value) {
  142. if(!$value) continue;
  143. switch ($type) {
  144. case 'category':
  145. $conditions[] = array(
  146. 'col' => 'C.term_id',
  147. 'sign' => 'IN',
  148. 'val' => $value,
  149. 'cast' => 'int'
  150. );
  151. break;
  152. case 'not_category':
  153. $conditions[] = array(
  154. 'col' => 'C.term_id',
  155. 'sign' => 'NOT IN',
  156. 'val' => $value,
  157. 'cast' => 'int'
  158. );
  159. break;
  160. case 'include':
  161. $conditions[] = array(
  162. 'col' => 'A.ID',
  163. 'sign' => 'IN',
  164. 'val' => $value,
  165. 'cast' => 'int'
  166. );
  167. break;
  168. case 'exclude':
  169. $conditions[] = array(
  170. 'col' => 'A.ID',
  171. 'sign' => 'NOT IN',
  172. 'val' => $value,
  173. 'cast' => 'int'
  174. );
  175. break;
  176. case 'cpt': // this is for backwards compatibility's sake
  177. case 'post_type':
  178. $conditions[] = array(
  179. 'col' => 'A.post_type',
  180. 'sign' => 'IN',
  181. 'val' => $value
  182. );
  183. break;
  184. case 'post_status':
  185. $conditions[] = array(
  186. 'col' => 'A.post_status',
  187. 'sign' => 'IN',
  188. 'val' => $value
  189. );
  190. break;
  191. case 'post_date':
  192. // apply timezone to date value
  193. $helper_toolbox = WYSIJA::get('toolbox', 'helper');
  194. $value = $helper_toolbox->time_tzed($value);
  195. if($value !== '') {
  196. $conditions[] = array(
  197. 'col' => 'A.post_date',
  198. 'sign' => '>',
  199. 'val' => $value
  200. );
  201. }
  202. break;
  203. case 'search':
  204. $conditions[] = array(
  205. 'col' => 'A.post_title',
  206. 'sign' => 'LIKE',
  207. 'val' => '%' . $value . '%'
  208. );
  209. break;
  210. }
  211. }
  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. }
  224. // where conditions
  225. if(!empty($conditions)) {
  226. $query_conditions = $this->build_conditions($conditions);
  227. $query .= $query_conditions;
  228. if($args['is_search_query'] === true) {
  229. $count_query .= $query_conditions;
  230. }
  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. }
  242. }
  243. // set limit (only if we are not requesting posts based on their id)
  244. if(array_key_exists('include', $args) && $args['include'] === null) {
  245. $query_offset = (isset($args['query_offset']) ? (int) $args['query_offset'] : 0);
  246. $query_limit = ((isset($args['post_limit']) && (int) $args['post_limit'] > 0) ? (int) $args['post_limit'] : 10);
  247. $query .= sprintf(' LIMIT %d,%d', $query_offset, $query_limit);
  248. }
  249. if($args['is_search_query'] === true) {
  250. return array(
  251. 'rows' => $this->query('get_res', $query),
  252. 'count' => $this->query('get_row', $count_query)
  253. );
  254. } else {
  255. return $this->query('get_res', $query);
  256. }
  257. }
  258. function build_conditions($conditions) {
  259. $query = '';
  260. $i = 0;
  261. foreach ($conditions as $key => $data) {
  262. if($i > 0) $query .= ' AND ';
  263. $query .= $data['col'] . ' ';
  264. $value = $data['val'];
  265. switch ($data['sign']) {
  266. case 'IN':
  267. case 'NOT IN':
  268. $values = '';
  269. if(is_array($value)) {
  270. if(array_key_exists('cast', $data) && $data['cast'] === 'int') {
  271. $count = count($value);
  272. for ($j = 0; $j < $count; $j++) {
  273. if($value[$j] === null) continue;
  274. $value[$j] = intval($value[$j]);
  275. }
  276. $values = join(', ', $value);
  277. } else {
  278. $values = "'" . join("', '", $value) . "'";
  279. }
  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. }
  289. } else {
  290. // multiple values
  291. $values = "'" . join("','", explode(',', $value)) . "'";
  292. $query .= $data['sign'] . ' (' . $values . ')';
  293. }
  294. }
  295. break;
  296. case 'LIKE':
  297. $query .= ' LIKE "' . $value . '"';
  298. break;
  299. default:
  300. $sign = '=';
  301. if(isset($data['sign'])) $sign = $data['sign'];
  302. if(array_key_exists('cast', $data) && $data['cast'] === 'int') {
  303. $query .= $sign . (int) $value . " ";
  304. } else {
  305. $query .= $sign . "'" . $value . "' ";
  306. }
  307. }
  308. $i++;
  309. }
  310. if($query === '') {
  311. return '';
  312. } else {
  313. return 'WHERE ' . $query;
  314. }
  315. }
  316. }