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

/wp-includes/class-wp-query.php

https://bitbucket.org/stephenharris/stephenharris
PHP | 4107 lines | 2043 code | 473 blank | 1591 comment | 615 complexity | c6fdacc253ba3c6aeec26c780af71ad8 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Query API: WP_Query class
  4. *
  5. * @package WordPress
  6. * @subpackage Query
  7. * @since 4.7.0
  8. */
  9. /**
  10. * The WordPress Query class.
  11. *
  12. * @link https://codex.wordpress.org/Function_Reference/WP_Query Codex page.
  13. *
  14. * @since 1.5.0
  15. * @since 4.5.0 Removed the `$comments_popup` property.
  16. */
  17. class WP_Query {
  18. /**
  19. * Query vars set by the user
  20. *
  21. * @since 1.5.0
  22. * @access public
  23. * @var array
  24. */
  25. public $query;
  26. /**
  27. * Query vars, after parsing
  28. *
  29. * @since 1.5.0
  30. * @access public
  31. * @var array
  32. */
  33. public $query_vars = array();
  34. /**
  35. * Taxonomy query, as passed to get_tax_sql()
  36. *
  37. * @since 3.1.0
  38. * @access public
  39. * @var object WP_Tax_Query
  40. */
  41. public $tax_query;
  42. /**
  43. * Metadata query container
  44. *
  45. * @since 3.2.0
  46. * @access public
  47. * @var object WP_Meta_Query
  48. */
  49. public $meta_query = false;
  50. /**
  51. * Date query container
  52. *
  53. * @since 3.7.0
  54. * @access public
  55. * @var object WP_Date_Query
  56. */
  57. public $date_query = false;
  58. /**
  59. * Holds the data for a single object that is queried.
  60. *
  61. * Holds the contents of a post, page, category, attachment.
  62. *
  63. * @since 1.5.0
  64. * @access public
  65. * @var object|array
  66. */
  67. public $queried_object;
  68. /**
  69. * The ID of the queried object.
  70. *
  71. * @since 1.5.0
  72. * @access public
  73. * @var int
  74. */
  75. public $queried_object_id;
  76. /**
  77. * Get post database query.
  78. *
  79. * @since 2.0.1
  80. * @access public
  81. * @var string
  82. */
  83. public $request;
  84. /**
  85. * List of posts.
  86. *
  87. * @since 1.5.0
  88. * @access public
  89. * @var array
  90. */
  91. public $posts;
  92. /**
  93. * The amount of posts for the current query.
  94. *
  95. * @since 1.5.0
  96. * @access public
  97. * @var int
  98. */
  99. public $post_count = 0;
  100. /**
  101. * Index of the current item in the loop.
  102. *
  103. * @since 1.5.0
  104. * @access public
  105. * @var int
  106. */
  107. public $current_post = -1;
  108. /**
  109. * Whether the loop has started and the caller is in the loop.
  110. *
  111. * @since 2.0.0
  112. * @access public
  113. * @var bool
  114. */
  115. public $in_the_loop = false;
  116. /**
  117. * The current post.
  118. *
  119. * @since 1.5.0
  120. * @access public
  121. * @var WP_Post
  122. */
  123. public $post;
  124. /**
  125. * The list of comments for current post.
  126. *
  127. * @since 2.2.0
  128. * @access public
  129. * @var array
  130. */
  131. public $comments;
  132. /**
  133. * The amount of comments for the posts.
  134. *
  135. * @since 2.2.0
  136. * @access public
  137. * @var int
  138. */
  139. public $comment_count = 0;
  140. /**
  141. * The index of the comment in the comment loop.
  142. *
  143. * @since 2.2.0
  144. * @access public
  145. * @var int
  146. */
  147. public $current_comment = -1;
  148. /**
  149. * Current comment ID.
  150. *
  151. * @since 2.2.0
  152. * @access public
  153. * @var int
  154. */
  155. public $comment;
  156. /**
  157. * The amount of found posts for the current query.
  158. *
  159. * If limit clause was not used, equals $post_count.
  160. *
  161. * @since 2.1.0
  162. * @access public
  163. * @var int
  164. */
  165. public $found_posts = 0;
  166. /**
  167. * The amount of pages.
  168. *
  169. * @since 2.1.0
  170. * @access public
  171. * @var int
  172. */
  173. public $max_num_pages = 0;
  174. /**
  175. * The amount of comment pages.
  176. *
  177. * @since 2.7.0
  178. * @access public
  179. * @var int
  180. */
  181. public $max_num_comment_pages = 0;
  182. /**
  183. * Set if query is single post.
  184. *
  185. * @since 1.5.0
  186. * @access public
  187. * @var bool
  188. */
  189. public $is_single = false;
  190. /**
  191. * Set if query is preview of blog.
  192. *
  193. * @since 2.0.0
  194. * @access public
  195. * @var bool
  196. */
  197. public $is_preview = false;
  198. /**
  199. * Set if query returns a page.
  200. *
  201. * @since 1.5.0
  202. * @access public
  203. * @var bool
  204. */
  205. public $is_page = false;
  206. /**
  207. * Set if query is an archive list.
  208. *
  209. * @since 1.5.0
  210. * @access public
  211. * @var bool
  212. */
  213. public $is_archive = false;
  214. /**
  215. * Set if query is part of a date.
  216. *
  217. * @since 1.5.0
  218. * @access public
  219. * @var bool
  220. */
  221. public $is_date = false;
  222. /**
  223. * Set if query contains a year.
  224. *
  225. * @since 1.5.0
  226. * @access public
  227. * @var bool
  228. */
  229. public $is_year = false;
  230. /**
  231. * Set if query contains a month.
  232. *
  233. * @since 1.5.0
  234. * @access public
  235. * @var bool
  236. */
  237. public $is_month = false;
  238. /**
  239. * Set if query contains a day.
  240. *
  241. * @since 1.5.0
  242. * @access public
  243. * @var bool
  244. */
  245. public $is_day = false;
  246. /**
  247. * Set if query contains time.
  248. *
  249. * @since 1.5.0
  250. * @access public
  251. * @var bool
  252. */
  253. public $is_time = false;
  254. /**
  255. * Set if query contains an author.
  256. *
  257. * @since 1.5.0
  258. * @access public
  259. * @var bool
  260. */
  261. public $is_author = false;
  262. /**
  263. * Set if query contains category.
  264. *
  265. * @since 1.5.0
  266. * @access public
  267. * @var bool
  268. */
  269. public $is_category = false;
  270. /**
  271. * Set if query contains tag.
  272. *
  273. * @since 2.3.0
  274. * @access public
  275. * @var bool
  276. */
  277. public $is_tag = false;
  278. /**
  279. * Set if query contains taxonomy.
  280. *
  281. * @since 2.5.0
  282. * @access public
  283. * @var bool
  284. */
  285. public $is_tax = false;
  286. /**
  287. * Set if query was part of a search result.
  288. *
  289. * @since 1.5.0
  290. * @access public
  291. * @var bool
  292. */
  293. public $is_search = false;
  294. /**
  295. * Set if query is feed display.
  296. *
  297. * @since 1.5.0
  298. * @access public
  299. * @var bool
  300. */
  301. public $is_feed = false;
  302. /**
  303. * Set if query is comment feed display.
  304. *
  305. * @since 2.2.0
  306. * @access public
  307. * @var bool
  308. */
  309. public $is_comment_feed = false;
  310. /**
  311. * Set if query is trackback.
  312. *
  313. * @since 1.5.0
  314. * @access public
  315. * @var bool
  316. */
  317. public $is_trackback = false;
  318. /**
  319. * Set if query is blog homepage.
  320. *
  321. * @since 1.5.0
  322. * @access public
  323. * @var bool
  324. */
  325. public $is_home = false;
  326. /**
  327. * Set if query couldn't found anything.
  328. *
  329. * @since 1.5.0
  330. * @access public
  331. * @var bool
  332. */
  333. public $is_404 = false;
  334. /**
  335. * Set if query is embed.
  336. *
  337. * @since 4.4.0
  338. * @access public
  339. * @var bool
  340. */
  341. public $is_embed = false;
  342. /**
  343. * Set if query is paged
  344. *
  345. * @since 1.5.0
  346. * @access public
  347. * @var bool
  348. */
  349. public $is_paged = false;
  350. /**
  351. * Set if query is part of administration page.
  352. *
  353. * @since 1.5.0
  354. * @access public
  355. * @var bool
  356. */
  357. public $is_admin = false;
  358. /**
  359. * Set if query is an attachment.
  360. *
  361. * @since 2.0.0
  362. * @access public
  363. * @var bool
  364. */
  365. public $is_attachment = false;
  366. /**
  367. * Set if is single, is a page, or is an attachment.
  368. *
  369. * @since 2.1.0
  370. * @access public
  371. * @var bool
  372. */
  373. public $is_singular = false;
  374. /**
  375. * Set if query is for robots.
  376. *
  377. * @since 2.1.0
  378. * @access public
  379. * @var bool
  380. */
  381. public $is_robots = false;
  382. /**
  383. * Set if query contains posts.
  384. *
  385. * Basically, the homepage if the option isn't set for the static homepage.
  386. *
  387. * @since 2.1.0
  388. * @access public
  389. * @var bool
  390. */
  391. public $is_posts_page = false;
  392. /**
  393. * Set if query is for a post type archive.
  394. *
  395. * @since 3.1.0
  396. * @access public
  397. * @var bool
  398. */
  399. public $is_post_type_archive = false;
  400. /**
  401. * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
  402. * whether we have to re-parse because something has changed
  403. *
  404. * @since 3.1.0
  405. * @access private
  406. * @var bool|string
  407. */
  408. private $query_vars_hash = false;
  409. /**
  410. * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
  411. * via pre_get_posts hooks.
  412. *
  413. * @since 3.1.1
  414. * @access private
  415. */
  416. private $query_vars_changed = true;
  417. /**
  418. * Set if post thumbnails are cached
  419. *
  420. * @since 3.2.0
  421. * @access public
  422. * @var bool
  423. */
  424. public $thumbnails_cached = false;
  425. /**
  426. * Cached list of search stopwords.
  427. *
  428. * @since 3.7.0
  429. * @var array
  430. */
  431. private $stopwords;
  432. private $compat_fields = array( 'query_vars_hash', 'query_vars_changed' );
  433. private $compat_methods = array( 'init_query_flags', 'parse_tax_query' );
  434. /**
  435. * Resets query flags to false.
  436. *
  437. * The query flags are what page info WordPress was able to figure out.
  438. *
  439. * @since 2.0.0
  440. * @access private
  441. */
  442. private function init_query_flags() {
  443. $this->is_single = false;
  444. $this->is_preview = false;
  445. $this->is_page = false;
  446. $this->is_archive = false;
  447. $this->is_date = false;
  448. $this->is_year = false;
  449. $this->is_month = false;
  450. $this->is_day = false;
  451. $this->is_time = false;
  452. $this->is_author = false;
  453. $this->is_category = false;
  454. $this->is_tag = false;
  455. $this->is_tax = false;
  456. $this->is_search = false;
  457. $this->is_feed = false;
  458. $this->is_comment_feed = false;
  459. $this->is_trackback = false;
  460. $this->is_home = false;
  461. $this->is_404 = false;
  462. $this->is_paged = false;
  463. $this->is_admin = false;
  464. $this->is_attachment = false;
  465. $this->is_singular = false;
  466. $this->is_robots = false;
  467. $this->is_posts_page = false;
  468. $this->is_post_type_archive = false;
  469. }
  470. /**
  471. * Initiates object properties and sets default values.
  472. *
  473. * @since 1.5.0
  474. * @access public
  475. */
  476. public function init() {
  477. unset($this->posts);
  478. unset($this->query);
  479. $this->query_vars = array();
  480. unset($this->queried_object);
  481. unset($this->queried_object_id);
  482. $this->post_count = 0;
  483. $this->current_post = -1;
  484. $this->in_the_loop = false;
  485. unset( $this->request );
  486. unset( $this->post );
  487. unset( $this->comments );
  488. unset( $this->comment );
  489. $this->comment_count = 0;
  490. $this->current_comment = -1;
  491. $this->found_posts = 0;
  492. $this->max_num_pages = 0;
  493. $this->max_num_comment_pages = 0;
  494. $this->init_query_flags();
  495. }
  496. /**
  497. * Reparse the query vars.
  498. *
  499. * @since 1.5.0
  500. * @access public
  501. */
  502. public function parse_query_vars() {
  503. $this->parse_query();
  504. }
  505. /**
  506. * Fills in the query variables, which do not exist within the parameter.
  507. *
  508. * @since 2.1.0
  509. * @since 4.4.0 Removed the `comments_popup` public query variable.
  510. * @access public
  511. *
  512. * @param array $array Defined query variables.
  513. * @return array Complete query variables with undefined ones filled in empty.
  514. */
  515. public function fill_query_vars($array) {
  516. $keys = array(
  517. 'error'
  518. , 'm'
  519. , 'p'
  520. , 'post_parent'
  521. , 'subpost'
  522. , 'subpost_id'
  523. , 'attachment'
  524. , 'attachment_id'
  525. , 'name'
  526. , 'static'
  527. , 'pagename'
  528. , 'page_id'
  529. , 'second'
  530. , 'minute'
  531. , 'hour'
  532. , 'day'
  533. , 'monthnum'
  534. , 'year'
  535. , 'w'
  536. , 'category_name'
  537. , 'tag'
  538. , 'cat'
  539. , 'tag_id'
  540. , 'author'
  541. , 'author_name'
  542. , 'feed'
  543. , 'tb'
  544. , 'paged'
  545. , 'meta_key'
  546. , 'meta_value'
  547. , 'preview'
  548. , 's'
  549. , 'sentence'
  550. , 'title'
  551. , 'fields'
  552. , 'menu_order'
  553. , 'embed'
  554. );
  555. foreach ( $keys as $key ) {
  556. if ( !isset($array[$key]) )
  557. $array[$key] = '';
  558. }
  559. $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in', 'post_name__in',
  560. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
  561. 'author__in', 'author__not_in' );
  562. foreach ( $array_keys as $key ) {
  563. if ( !isset($array[$key]) )
  564. $array[$key] = array();
  565. }
  566. return $array;
  567. }
  568. /**
  569. * Parse a query string and set query type booleans.
  570. *
  571. * @since 1.5.0
  572. * @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's
  573. * array key to `$orderby`.
  574. * @since 4.4.0 Introduced `$post_name__in` and `$title` parameters. `$s` was updated to support excluded
  575. * search terms, by prepending a hyphen.
  576. * @since 4.5.0 Removed the `$comments_popup` parameter.
  577. * Introduced the `$comment_status` and `$ping_status` parameters.
  578. * Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts.
  579. * @since 4.6.0 Added 'post_name__in' support for `$orderby`. Introduced the `$lazy_load_term_meta` argument.
  580. * @access public
  581. *
  582. * @param string|array $query {
  583. * Optional. Array or string of Query parameters.
  584. *
  585. * @type int $attachment_id Attachment post ID. Used for 'attachment' post_type.
  586. * @type int|string $author Author ID, or comma-separated list of IDs.
  587. * @type string $author_name User 'user_nicename'.
  588. * @type array $author__in An array of author IDs to query from.
  589. * @type array $author__not_in An array of author IDs not to query from.
  590. * @type bool $cache_results Whether to cache post information. Default true.
  591. * @type int|string $cat Category ID or comma-separated list of IDs (this or any children).
  592. * @type array $category__and An array of category IDs (AND in).
  593. * @type array $category__in An array of category IDs (OR in, no children).
  594. * @type array $category__not_in An array of category IDs (NOT in).
  595. * @type string $category_name Use category slug (not name, this or any children).
  596. * @type string $comment_status Comment status.
  597. * @type int $comments_per_page The number of comments to return per page.
  598. * Default 'comments_per_page' option.
  599. * @type array $date_query An associative array of WP_Date_Query arguments.
  600. * See WP_Date_Query::__construct().
  601. * @type int $day Day of the month. Default empty. Accepts numbers 1-31.
  602. * @type bool $exact Whether to search by exact keyword. Default false.
  603. * @type string|array $fields Which fields to return. Single field or all fields (string),
  604. * or array of fields. 'id=>parent' uses 'id' and 'post_parent'.
  605. * Default all fields. Accepts 'ids', 'id=>parent'.
  606. * @type int $hour Hour of the day. Default empty. Accepts numbers 0-23.
  607. * @type int|bool $ignore_sticky_posts Whether to ignore sticky posts or not. Setting this to false
  608. * excludes stickies from 'post__in'. Accepts 1|true, 0|false.
  609. * Default 0|false.
  610. * @type int $m Combination YearMonth. Accepts any four-digit year and month
  611. * numbers 1-12. Default empty.
  612. * @type string $meta_compare Comparison operator to test the 'meta_value'.
  613. * @type string $meta_key Custom field key.
  614. * @type array $meta_query An associative array of WP_Meta_Query arguments. See WP_Meta_Query.
  615. * @type string $meta_value Custom field value.
  616. * @type int $meta_value_num Custom field value number.
  617. * @type int $menu_order The menu order of the posts.
  618. * @type int $monthnum The two-digit month. Default empty. Accepts numbers 1-12.
  619. * @type string $name Post slug.
  620. * @type bool $nopaging Show all posts (true) or paginate (false). Default false.
  621. * @type bool $no_found_rows Whether to skip counting the total rows found. Enabling can improve
  622. * performance. Default false.
  623. * @type int $offset The number of posts to offset before retrieval.
  624. * @type string $order Designates ascending or descending order of posts. Default 'DESC'.
  625. * Accepts 'ASC', 'DESC'.
  626. * @type string|array $orderby Sort retrieved posts by parameter. One or more options may be
  627. * passed. To use 'meta_value', or 'meta_value_num',
  628. * 'meta_key=keyname' must be also be defined. To sort by a
  629. * specific `$meta_query` clause, use that clause's array key.
  630. * Default 'date'. Accepts 'none', 'name', 'author', 'date',
  631. * 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand',
  632. * 'RAND(x)' (where 'x' is an integer seed value),
  633. * 'comment_count', 'meta_value', 'meta_value_num', 'post__in',
  634. * 'post_name__in', 'post_parent__in', and the array keys
  635. * of `$meta_query`.
  636. * @type int $p Post ID.
  637. * @type int $page Show the number of posts that would show up on page X of a
  638. * static front page.
  639. * @type int $paged The number of the current page.
  640. * @type int $page_id Page ID.
  641. * @type string $pagename Page slug.
  642. * @type string $perm Show posts if user has the appropriate capability.
  643. * @type string $ping_status Ping status.
  644. * @type array $post__in An array of post IDs to retrieve, sticky posts will be included
  645. * @type string $post_mime_type The mime type of the post. Used for 'attachment' post_type.
  646. * @type array $post__not_in An array of post IDs not to retrieve. Note: a string of comma-
  647. * separated IDs will NOT work.
  648. * @type int $post_parent Page ID to retrieve child pages for. Use 0 to only retrieve
  649. * top-level pages.
  650. * @type array $post_parent__in An array containing parent page IDs to query child pages from.
  651. * @type array $post_parent__not_in An array containing parent page IDs not to query child pages from.
  652. * @type string|array $post_type A post type slug (string) or array of post type slugs.
  653. * Default 'any' if using 'tax_query'.
  654. * @type string|array $post_status A post status (string) or array of post statuses.
  655. * @type int $posts_per_page The number of posts to query for. Use -1 to request all posts.
  656. * @type int $posts_per_archive_page The number of posts to query for by archive page. Overrides
  657. * 'posts_per_page' when is_archive(), or is_search() are true.
  658. * @type array $post_name__in An array of post slugs that results must match.
  659. * @type string $s Search keyword(s). Prepending a term with a hyphen will
  660. * exclude posts matching that term. Eg, 'pillow -sofa' will
  661. * return posts containing 'pillow' but not 'sofa'. The
  662. * character used for exclusion can be modified using the
  663. * the 'wp_query_search_exclusion_prefix' filter.
  664. * @type int $second Second of the minute. Default empty. Accepts numbers 0-60.
  665. * @type bool $sentence Whether to search by phrase. Default false.
  666. * @type bool $suppress_filters Whether to suppress filters. Default false.
  667. * @type string $tag Tag slug. Comma-separated (either), Plus-separated (all).
  668. * @type array $tag__and An array of tag ids (AND in).
  669. * @type array $tag__in An array of tag ids (OR in).
  670. * @type array $tag__not_in An array of tag ids (NOT in).
  671. * @type int $tag_id Tag id or comma-separated list of IDs.
  672. * @type array $tag_slug__and An array of tag slugs (AND in).
  673. * @type array $tag_slug__in An array of tag slugs (OR in). unless 'ignore_sticky_posts' is
  674. * true. Note: a string of comma-separated IDs will NOT work.
  675. * @type array $tax_query An associative array of WP_Tax_Query arguments.
  676. * See WP_Tax_Query->queries.
  677. * @type string $title Post title.
  678. * @type bool $update_post_meta_cache Whether to update the post meta cache. Default true.
  679. * @type bool $update_post_term_cache Whether to update the post term cache. Default true.
  680. * @type bool $lazy_load_term_meta Whether to lazy-load term meta. Setting to false will
  681. * disable cache priming for term meta, so that each
  682. * get_term_meta() call will hit the database.
  683. * Defaults to the value of `$update_post_term_cache`.
  684. * @type int $w The week number of the year. Default empty. Accepts numbers 0-53.
  685. * @type int $year The four-digit year. Default empty. Accepts any four-digit year.
  686. * }
  687. */
  688. public function parse_query( $query = '' ) {
  689. if ( ! empty( $query ) ) {
  690. $this->init();
  691. $this->query = $this->query_vars = wp_parse_args( $query );
  692. } elseif ( ! isset( $this->query ) ) {
  693. $this->query = $this->query_vars;
  694. }
  695. $this->query_vars = $this->fill_query_vars($this->query_vars);
  696. $qv = &$this->query_vars;
  697. $this->query_vars_changed = true;
  698. if ( ! empty($qv['robots']) )
  699. $this->is_robots = true;
  700. if ( ! is_scalar( $qv['p'] ) || $qv['p'] < 0 ) {
  701. $qv['p'] = 0;
  702. $qv['error'] = '404';
  703. } else {
  704. $qv['p'] = intval( $qv['p'] );
  705. }
  706. $qv['page_id'] = absint($qv['page_id']);
  707. $qv['year'] = absint($qv['year']);
  708. $qv['monthnum'] = absint($qv['monthnum']);
  709. $qv['day'] = absint($qv['day']);
  710. $qv['w'] = absint($qv['w']);
  711. $qv['m'] = is_scalar( $qv['m'] ) ? preg_replace( '|[^0-9]|', '', $qv['m'] ) : '';
  712. $qv['paged'] = absint($qv['paged']);
  713. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  714. $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
  715. $qv['pagename'] = trim( $qv['pagename'] );
  716. $qv['name'] = trim( $qv['name'] );
  717. $qv['title'] = trim( $qv['title'] );
  718. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  719. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  720. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  721. if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
  722. // Fairly insane upper bound for search string lengths.
  723. if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) {
  724. $qv['s'] = '';
  725. }
  726. // Compat. Map subpost to attachment.
  727. if ( '' != $qv['subpost'] )
  728. $qv['attachment'] = $qv['subpost'];
  729. if ( '' != $qv['subpost_id'] )
  730. $qv['attachment_id'] = $qv['subpost_id'];
  731. $qv['attachment_id'] = absint($qv['attachment_id']);
  732. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  733. $this->is_single = true;
  734. $this->is_attachment = true;
  735. } elseif ( '' != $qv['name'] ) {
  736. $this->is_single = true;
  737. } elseif ( $qv['p'] ) {
  738. $this->is_single = true;
  739. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  740. // If year, month, day, hour, minute, and second are set, a single
  741. // post is being queried.
  742. $this->is_single = true;
  743. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  744. $this->is_page = true;
  745. $this->is_single = false;
  746. } else {
  747. // Look for archive queries. Dates, categories, authors, search, post type archives.
  748. if ( isset( $this->query['s'] ) ) {
  749. $this->is_search = true;
  750. }
  751. if ( '' !== $qv['second'] ) {
  752. $this->is_time = true;
  753. $this->is_date = true;
  754. }
  755. if ( '' !== $qv['minute'] ) {
  756. $this->is_time = true;
  757. $this->is_date = true;
  758. }
  759. if ( '' !== $qv['hour'] ) {
  760. $this->is_time = true;
  761. $this->is_date = true;
  762. }
  763. if ( $qv['day'] ) {
  764. if ( ! $this->is_date ) {
  765. $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] );
  766. if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) {
  767. $qv['error'] = '404';
  768. } else {
  769. $this->is_day = true;
  770. $this->is_date = true;
  771. }
  772. }
  773. }
  774. if ( $qv['monthnum'] ) {
  775. if ( ! $this->is_date ) {
  776. if ( 12 < $qv['monthnum'] ) {
  777. $qv['error'] = '404';
  778. } else {
  779. $this->is_month = true;
  780. $this->is_date = true;
  781. }
  782. }
  783. }
  784. if ( $qv['year'] ) {
  785. if ( ! $this->is_date ) {
  786. $this->is_year = true;
  787. $this->is_date = true;
  788. }
  789. }
  790. if ( $qv['m'] ) {
  791. $this->is_date = true;
  792. if ( strlen($qv['m']) > 9 ) {
  793. $this->is_time = true;
  794. } elseif ( strlen( $qv['m'] ) > 7 ) {
  795. $this->is_day = true;
  796. } elseif ( strlen( $qv['m'] ) > 5 ) {
  797. $this->is_month = true;
  798. } else {
  799. $this->is_year = true;
  800. }
  801. }
  802. if ( '' != $qv['w'] ) {
  803. $this->is_date = true;
  804. }
  805. $this->query_vars_hash = false;
  806. $this->parse_tax_query( $qv );
  807. foreach ( $this->tax_query->queries as $tax_query ) {
  808. if ( ! is_array( $tax_query ) ) {
  809. continue;
  810. }
  811. if ( isset( $tax_query['operator'] ) && 'NOT IN' != $tax_query['operator'] ) {
  812. switch ( $tax_query['taxonomy'] ) {
  813. case 'category':
  814. $this->is_category = true;
  815. break;
  816. case 'post_tag':
  817. $this->is_tag = true;
  818. break;
  819. default:
  820. $this->is_tax = true;
  821. }
  822. }
  823. }
  824. unset( $tax_query );
  825. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  826. $this->is_author = false;
  827. } else {
  828. $this->is_author = true;
  829. }
  830. if ( '' != $qv['author_name'] )
  831. $this->is_author = true;
  832. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  833. $post_type_obj = get_post_type_object( $qv['post_type'] );
  834. if ( ! empty( $post_type_obj->has_archive ) )
  835. $this->is_post_type_archive = true;
  836. }
  837. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  838. $this->is_archive = true;
  839. }
  840. if ( '' != $qv['feed'] )
  841. $this->is_feed = true;
  842. if ( '' != $qv['embed'] ) {
  843. $this->is_embed = true;
  844. }
  845. if ( '' != $qv['tb'] )
  846. $this->is_trackback = true;
  847. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  848. $this->is_paged = true;
  849. // if we're previewing inside the write screen
  850. if ( '' != $qv['preview'] )
  851. $this->is_preview = true;
  852. if ( is_admin() )
  853. $this->is_admin = true;
  854. if ( false !== strpos($qv['feed'], 'comments-') ) {
  855. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  856. $qv['withcomments'] = 1;
  857. }
  858. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  859. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  860. $this->is_comment_feed = true;
  861. if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots ) )
  862. $this->is_home = true;
  863. // Correct is_* for page_on_front and page_for_posts
  864. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  865. $_query = wp_parse_args($this->query);
  866. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  867. if ( isset($_query['pagename']) && '' == $_query['pagename'] )
  868. unset($_query['pagename']);
  869. unset( $_query['embed'] );
  870. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  871. $this->is_page = true;
  872. $this->is_home = false;
  873. $qv['page_id'] = get_option('page_on_front');
  874. // Correct <!--nextpage--> for page_on_front
  875. if ( !empty($qv['paged']) ) {
  876. $qv['page'] = $qv['paged'];
  877. unset($qv['paged']);
  878. }
  879. }
  880. }
  881. if ( '' != $qv['pagename'] ) {
  882. $this->queried_object = get_page_by_path( $qv['pagename'] );
  883. if ( $this->queried_object && 'attachment' == $this->queried_object->post_type ) {
  884. if ( preg_match( "/^[^%]*%(?:postname)%/", get_option( 'permalink_structure' ) ) ) {
  885. // See if we also have a post with the same slug
  886. $post = get_page_by_path( $qv['pagename'], OBJECT, 'post' );
  887. if ( $post ) {
  888. $this->queried_object = $post;
  889. $this->is_page = false;
  890. $this->is_single = true;
  891. }
  892. }
  893. }
  894. if ( ! empty( $this->queried_object ) ) {
  895. $this->queried_object_id = (int) $this->queried_object->ID;
  896. } else {
  897. unset( $this->queried_object );
  898. }
  899. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  900. $this->is_page = false;
  901. $this->is_home = true;
  902. $this->is_posts_page = true;
  903. }
  904. }
  905. if ( $qv['page_id'] ) {
  906. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  907. $this->is_page = false;
  908. $this->is_home = true;
  909. $this->is_posts_page = true;
  910. }
  911. }
  912. if ( !empty($qv['post_type']) ) {
  913. if ( is_array($qv['post_type']) )
  914. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  915. else
  916. $qv['post_type'] = sanitize_key($qv['post_type']);
  917. }
  918. if ( ! empty( $qv['post_status'] ) ) {
  919. if ( is_array( $qv['post_status'] ) )
  920. $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
  921. else
  922. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  923. }
  924. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  925. $this->is_comment_feed = false;
  926. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  927. // Done correcting is_* for page_on_front and page_for_posts
  928. if ( '404' == $qv['error'] )
  929. $this->set_404();
  930. $this->is_embed = $this->is_embed && ( $this->is_singular || $this->is_404 );
  931. $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  932. $this->query_vars_changed = false;
  933. /**
  934. * Fires after the main query vars have been parsed.
  935. *
  936. * @since 1.5.0
  937. *
  938. * @param WP_Query &$this The WP_Query instance (passed by reference).
  939. */
  940. do_action_ref_array( 'parse_query', array( &$this ) );
  941. }
  942. /**
  943. * Parses various taxonomy related query vars.
  944. *
  945. * For BC, this method is not marked as protected. See [28987].
  946. *
  947. * @access protected
  948. * @since 3.1.0
  949. *
  950. * @param array $q The query variables. Passed by reference.
  951. */
  952. public function parse_tax_query( &$q ) {
  953. if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
  954. $tax_query = $q['tax_query'];
  955. } else {
  956. $tax_query = array();
  957. }
  958. if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
  959. $tax_query[] = array(
  960. 'taxonomy' => $q['taxonomy'],
  961. 'terms' => array( $q['term'] ),
  962. 'field' => 'slug',
  963. );
  964. }
  965. foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) {
  966. if ( 'post_tag' == $taxonomy )
  967. continue; // Handled further down in the $q['tag'] block
  968. if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
  969. $tax_query_defaults = array(
  970. 'taxonomy' => $taxonomy,
  971. 'field' => 'slug',
  972. );
  973. if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
  974. $q[$t->query_var] = wp_basename( $q[$t->query_var] );
  975. }
  976. $term = $q[$t->query_var];
  977. if ( is_array( $term ) ) {
  978. $term = implode( ',', $term );
  979. }
  980. if ( strpos($term, '+') !== false ) {
  981. $terms = preg_split( '/[+]+/', $term );
  982. foreach ( $terms as $term ) {
  983. $tax_query[] = array_merge( $tax_query_defaults, array(
  984. 'terms' => array( $term )
  985. ) );
  986. }
  987. } else {
  988. $tax_query[] = array_merge( $tax_query_defaults, array(
  989. 'terms' => preg_split( '/[,]+/', $term )
  990. ) );
  991. }
  992. }
  993. }
  994. // If querystring 'cat' is an array, implode it.
  995. if ( is_array( $q['cat'] ) ) {
  996. $q['cat'] = implode( ',', $q['cat'] );
  997. }
  998. // Category stuff
  999. if ( ! empty( $q['cat'] ) && ! $this->is_singular ) {
  1000. $cat_in = $cat_not_in = array();
  1001. $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) );
  1002. $cat_array = array_map( 'intval', $cat_array );
  1003. $q['cat'] = implode( ',', $cat_array );
  1004. foreach ( $cat_array as $cat ) {
  1005. if ( $cat > 0 )
  1006. $cat_in[] = $cat;
  1007. elseif ( $cat < 0 )
  1008. $cat_not_in[] = abs( $cat );
  1009. }
  1010. if ( ! empty( $cat_in ) ) {
  1011. $tax_query[] = array(
  1012. 'taxonomy' => 'category',
  1013. 'terms' => $cat_in,
  1014. 'field' => 'term_id',
  1015. 'include_children' => true
  1016. );
  1017. }
  1018. if ( ! empty( $cat_not_in ) ) {
  1019. $tax_query[] = array(
  1020. 'taxonomy' => 'category',
  1021. 'terms' => $cat_not_in,
  1022. 'field' => 'term_id',
  1023. 'operator' => 'NOT IN',
  1024. 'include_children' => true
  1025. );
  1026. }
  1027. unset( $cat_array, $cat_in, $cat_not_in );
  1028. }
  1029. if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
  1030. $q['category__and'] = (array) $q['category__and'];
  1031. if ( ! isset( $q['category__in'] ) )
  1032. $q['category__in'] = array();
  1033. $q['category__in'][] = absint( reset( $q['category__and'] ) );
  1034. unset( $q['category__and'] );
  1035. }
  1036. if ( ! empty( $q['category__in'] ) ) {
  1037. $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
  1038. $tax_query[] = array(
  1039. 'taxonomy' => 'category',
  1040. 'terms' => $q['category__in'],
  1041. 'field' => 'term_id',
  1042. 'include_children' => false
  1043. );
  1044. }
  1045. if ( ! empty($q['category__not_in']) ) {
  1046. $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
  1047. $tax_query[] = array(
  1048. 'taxonomy' => 'category',
  1049. 'terms' => $q['category__not_in'],
  1050. 'operator' => 'NOT IN',
  1051. 'include_children' => false
  1052. );
  1053. }
  1054. if ( ! empty($q['category__and']) ) {
  1055. $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
  1056. $tax_query[] = array(
  1057. 'taxonomy' => 'category',
  1058. 'terms' => $q['category__and'],
  1059. 'field' => 'term_id',
  1060. 'operator' => 'AND',
  1061. 'include_children' => false
  1062. );
  1063. }
  1064. // If querystring 'tag' is array, implode it.
  1065. if ( is_array( $q['tag'] ) ) {
  1066. $q['tag'] = implode( ',', $q['tag'] );
  1067. }
  1068. // Tag stuff
  1069. if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
  1070. if ( strpos($q['tag'], ',') !== false ) {
  1071. $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
  1072. foreach ( (array) $tags as $tag ) {
  1073. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1074. $q['tag_slug__in'][] = $tag;
  1075. }
  1076. } elseif ( preg_match('/[+\r\n\t ]+/', $q['tag'] ) || ! empty( $q['cat'] ) ) {
  1077. $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
  1078. foreach ( (array) $tags as $tag ) {
  1079. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1080. $q['tag_slug__and'][] = $tag;
  1081. }
  1082. } else {
  1083. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1084. $q['tag_slug__in'][] = $q['tag'];
  1085. }
  1086. }
  1087. if ( !empty($q['tag_id']) ) {
  1088. $q['tag_id'] = absint( $q['tag_id'] );
  1089. $tax_query[] = array(
  1090. 'taxonomy' => 'post_tag',
  1091. 'terms' => $q['tag_id']
  1092. );
  1093. }
  1094. if ( !empty($q['tag__in']) ) {
  1095. $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
  1096. $tax_query[] = array(
  1097. 'taxonomy' => 'post_tag',
  1098. 'terms' => $q['tag__in']
  1099. );
  1100. }
  1101. if ( !empty($q['tag__not_in']) ) {
  1102. $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
  1103. $tax_query[] = array(
  1104. 'taxonomy' => 'post_tag',
  1105. 'terms' => $q['tag__not_in'],
  1106. 'operator' => 'NOT IN'
  1107. );
  1108. }
  1109. if ( !empty($q['tag__and']) ) {
  1110. $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
  1111. $tax_query[] = array(
  1112. 'taxonomy' => 'post_tag',
  1113. 'terms' => $q['tag__and'],
  1114. 'operator' => 'AND'
  1115. );
  1116. }
  1117. if ( !empty($q['tag_slug__in']) ) {
  1118. $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
  1119. $tax_query[] = array(
  1120. 'taxonomy' => 'post_tag',
  1121. 'terms' => $q['tag_slug__in'],
  1122. 'field' => 'slug'
  1123. );
  1124. }
  1125. if ( !empty($q['tag_slug__and']) ) {
  1126. $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
  1127. $tax_query[] = array(
  1128. 'taxonomy' => 'post_tag',
  1129. 'terms' => $q['tag_slug__and'],
  1130. 'field' => 'slug',
  1131. 'operator' => 'AND'
  1132. );
  1133. }
  1134. $this->tax_query = new WP_Tax_Query( $tax_query );
  1135. /**
  1136. * Fires after taxonomy-related query vars have been parsed.
  1137. *
  1138. * @since 3.7.0
  1139. *
  1140. * @param WP_Query $this The WP_Query instance.
  1141. */
  1142. do_action( 'parse_tax_query', $this );
  1143. }
  1144. /**
  1145. * Generate SQL for the WHERE clause based on passed search terms.
  1146. *
  1147. * @since 3.7.0
  1148. *
  1149. * @param array $q Query variables.
  1150. * @return string WHERE clause.
  1151. */
  1152. protected function parse_search( &$q ) {
  1153. global $wpdb;
  1154. $search = '';
  1155. // added slashes screw with quote grouping when done early, so done later
  1156. $q['s'] = stripslashes( $q['s'] );
  1157. if ( empty( $_GET['s'] ) && $this->is_main_query() )
  1158. $q['s'] = urldecode( $q['s'] );
  1159. // there are no line breaks in <input /> fields
  1160. $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
  1161. $q['search_terms_count'] = 1;
  1162. if ( ! empty( $q['sentence'] ) ) {
  1163. $q['search_terms'] = array( $q['s'] );
  1164. } else {
  1165. if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
  1166. $q['search_terms_count'] = count( $matches[0] );
  1167. $q['search_terms'] = $this->parse_search_terms( $matches[0] );
  1168. // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
  1169. if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
  1170. $q['search_terms'] = array( $q['s'] );
  1171. } else {
  1172. $q['search_terms'] = array( $q['s'] );
  1173. }
  1174. }
  1175. $n = ! empty( $q['exact'] ) ? '' : '%';
  1176. $searchand = '';
  1177. $q['search_orderby_title'] = array();
  1178. /**
  1179. * Filters the prefix that indicates that a search term should be excluded from results.
  1180. *
  1181. * @since 4.7.0
  1182. *
  1183. * @param string $exclusion_prefix The prefix. Default '-'. Returning
  1184. * an empty value disables exclusions.
  1185. */
  1186. $exclusion_prefix = apply_filters( 'wp_query_search_exclusion_prefix', '-' );
  1187. foreach ( $q['search_terms'] as $term ) {
  1188. // If there is an $exclusion_prefix, terms prefixed with it should be excluded.
  1189. $exclude = $exclusion_prefix && ( $exclusion_prefix === substr( $term, 0, 1 ) );
  1190. if ( $exclude ) {
  1191. $like_op = 'NOT LIKE';
  1192. $andor_op = 'AND';
  1193. $term = substr( $term, 1 );
  1194. } else {
  1195. $like_op = 'LIKE';
  1196. $andor_op = 'OR';
  1197. }
  1198. if ( $n && ! $exclude ) {
  1199. $like = '%' . $wpdb->esc_like( $term ) . '%';
  1200. $q['search_orderby_title'][] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like );
  1201. }
  1202. $like = $n . $wpdb->esc_like( $term ) . $n;
  1203. $search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like );
  1204. $searchand = ' AND ';
  1205. }
  1206. if ( ! empty( $search ) ) {
  1207. $search = " AND ({$search}) ";
  1208. if ( ! is_user_logged_in() ) {
  1209. $search .= " AND ({$wpdb->posts}.post_password = '') ";
  1210. }
  1211. }
  1212. return $search;
  1213. }
  1214. /**
  1215. * Check if the terms are suitable for searching.
  1216. *
  1217. * Uses an array of stopwords (terms) that are excluded from the separate
  1218. * term matching when searching for posts. The list of English stopwords is
  1219. * the approximate search engines list, and is translatable.
  1220. *
  1221. * @since 3.7.0
  1222. *
  1223. * @param array $terms Terms to check.
  1224. * @return array Terms that are not stopwords.
  1225. */
  1226. protected function parse_search_terms( $terms ) {
  1227. $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
  1228. $checked = array();
  1229. $stopwords = $this->get_search_stopwords();
  1230. foreach ( $terms as $term ) {
  1231. // keep before/after spaces when term is for exact match
  1232. if ( preg_match( '/^".+"$/', $term ) )
  1233. $term = trim( $term, "\"'" );
  1234. else
  1235. $term = trim( $term, "\"' " );
  1236. // Avoid single A-Z and single dashes.
  1237. if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z\-]$/i', $term ) ) )
  1238. continue;
  1239. if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
  1240. continue;
  1241. $checked[] = $term;
  1242. }
  1243. return $checked;
  1244. }
  1245. /**
  1246. * Retrieve stopwords used when parsing search terms.
  1247. *
  1248. * @since 3.7.0
  1249. *
  1250. * @return array Stopwords.
  1251. */
  1252. protected function get_search_stopwords() {
  1253. if ( isset( $this->stopwords ) )
  1254. return $this->stopwords;
  1255. /* translators: This is a comma-separated list of very common words that should be excluded from a search,
  1256. * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
  1257. * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
  1258. */
  1259. $words = explode( ',', _x( 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www',
  1260. 'Comma-separated list of search stopwords in your language' ) );
  1261. $stopwords = array();
  1262. foreach ( $words as $word ) {
  1263. $word = trim( $word, "\r\n\t " );
  1264. if ( $word )
  1265. $stopwords[] = $word;
  1266. }
  1267. /**
  1268. * Filters stopwords used when parsing search terms.
  1269. *
  1270. * @since 3.7.0
  1271. *
  1272. * @param array $stopwords Stopwords.
  1273. */
  1274. $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
  1275. return $this->stopwords;
  1276. }
  1277. /**
  1278. * Generate SQL for the ORDER BY condition based on passed search terms.
  1279. *
  1280. * @param array $q Query variables.
  1281. * @return string ORDER BY clause.
  1282. */
  1283. protected function parse_search_order( &$q ) {
  1284. global $wpdb;
  1285. if ( $q['search_terms_count'] > 1 ) {
  1286. $num_terms = count( $q['search_orderby_title'] );
  1287. // If the search terms contain negative queries, don't bother ordering by sentence matches.
  1288. $like = '';
  1289. if ( ! preg_match( '/(?:\s|^)\-/', $q['s'] ) ) {
  1290. $like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
  1291. }
  1292. $search_orderby = '';
  1293. // sentence match in 'post_title'
  1294. if ( $like ) {
  1295. $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_title LIKE %s THEN 1 ", $like );
  1296. }
  1297. // sanity limit, sort as sentence when more than 6 terms
  1298. // (few searches are longer than 6 terms and most titles are not)
  1299. if ( $num_terms < 7 ) {
  1300. // all words in title
  1301. $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
  1302. // any word in title, not needed when $num_terms == 1
  1303. if ( $num_terms > 1 )
  1304. $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
  1305. }
  1306. // Sentence match in 'post_content' and 'post_excerpt'.
  1307. if ( $like ) {
  1308. $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_excerpt LIKE %s THEN 4 ", $like );
  1309. $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_content LIKE %s THEN 5 ", $like );
  1310. }
  1311. if ( $search_orderby ) {
  1312. $search_orderby = '(CASE ' . $search_orderby . 'ELSE 6 END)';
  1313. }
  1314. } else {
  1315. // single word or sentence search
  1316. $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
  1317. }
  1318. return $search_orderby;
  1319. }
  1320. /**
  1321. * If the passed orderby value is allowed, convert the alias to a
  1322. * properly-prefixed orderby value.
  1323. *
  1324. * @since 4.0.0
  1325. * @access protected
  1326. *
  1327. * @param string $orderby Alias for the field to order by.
  1328. * @return string|false Table-prefixed value to used in the ORDER clause. False otherwise.
  1329. */
  1330. protected function parse_orderby( $orderby ) {
  1331. global $wpdb;
  1332. // Used to filter values.
  1333. $allowed_keys = array(
  1334. 'post_name', 'post_author', 'post_date', 'post_title', 'post_modified',
  1335. 'post_parent', 'post_type', 'name', 'author', 'date', 'title', 'modified',
  1336. 'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand',
  1337. );
  1338. $primary_meta_key = '';
  1339. $primary_meta_query = false;
  1340. $meta_clauses = $this->meta_query->get_clauses();
  1341. if ( ! empty( $meta_clauses ) ) {
  1342. $primary_meta_query = reset( $meta_clauses );
  1343. if ( ! empty( $primary_meta_query['key'] ) ) {
  1344. $primary_meta_key = $primary_meta_query['key'];
  1345. $allowed_keys[] = $primary_meta_key;
  1346. }
  1347. $allowed_keys[] = 'meta_value';
  1348. $allowed_keys[] = 'meta_value_num';
  1349. $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) );
  1350. }
  1351. // If RAND() contains a seed value, sanitize and add to allowed keys.
  1352. $rand_with_seed = false;
  1353. if ( preg_match( '/RAND\(([0-9]+)\)/i', $orderby, $matches ) ) {
  1354. $orderby = sprintf( 'RAND(%s)', intval( $matches[1] ) );
  1355. $allowed_keys[] = $orderby;
  1356. $rand_with_seed = true;
  1357. }
  1358. if ( ! in_array( $orderby, $allowed_keys, true ) ) {
  1359. return false;
  1360. }
  1361. switch ( $orderby ) {
  1362. case 'post_name':
  1363. case 'post_author':
  1364. case 'post_date':
  1365. case 'post_title':
  1366. case 'post_modified':
  1367. case 'post_parent':
  1368. case 'post_type':
  1369. case 'ID':
  1370. case 'menu_order':
  1371. case 'comment_count':
  1372. $orderby_clause = "{$wpdb->posts}.{$orderby}";
  1373. break;
  1374. case 'rand':
  1375. $orderby_clause = 'RAND()';
  1376. break;
  1377. case $primary_meta_key:
  1378. case 'meta_value':
  1379. if ( ! empty( $primary_meta_query['type'] ) ) {
  1380. $orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
  1381. } else {
  1382. $orderby_clause = "{$primary_meta_query['alias']}.meta_value";
  1383. }
  1384. break;
  1385. case 'meta_value_num':
  1386. $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0";
  1387. break;
  1388. default:
  1389. if ( array_key_exists( $orderby, $meta_clauses ) ) {
  1390. // $orderby corresponds to a meta_query clause.
  1391. $meta_clause = $meta_clauses[ $orderby ];
  1392. $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
  1393. } elseif ( $rand_with_seed ) {
  1394. $orderby_clause = $orderby;
  1395. } else {
  1396. // Default: order by post field.
  1397. $orderby_clause = "{$wpdb->posts}.post_" . sanitize_key( $orderby );
  1398. }
  1399. break;
  1400. }
  1401. return $orderby_clause;
  1402. }
  1403. /**
  1404. * Parse an 'order' query variable and cast it to ASC or DESC as necessary.
  1405. *
  1406. * @since 4.0.0
  1407. * @access protected
  1408. *
  1409. * @param string $order The 'order' query variable.
  1410. * @return string The sanitized 'order' query variable.
  1411. */
  1412. protected function parse_order( $order ) {
  1413. if ( ! is_string( $order ) || empty( $order ) ) {
  1414. return 'DESC';
  1415. }
  1416. if ( 'ASC' === strtoupper( $order ) ) {
  1417. return 'ASC';
  1418. } else {
  1419. return 'DESC';
  1420. }
  1421. }
  1422. /**
  1423. * Sets the 404 property and saves whether query is feed.
  1424. *
  1425. * @since 2.0.0
  1426. * @access public
  1427. */
  1428. public function set_404() {
  1429. $is_feed = $this->is_feed;
  1430. $this->init_query_flags();
  1431. $this->is_404 = true;
  1432. $this->is_feed = $is_feed;
  1433. }
  1434. /**
  1435. * Retrieve query variable.
  1436. *
  1437. * @since 1.5.0
  1438. * @since 3.9.0 The `$default` argument was introduced.
  1439. *
  1440. * @access public
  1441. *
  1442. * @param string $query_var Query variable key.
  1443. * @param mixed $default Optional. Value to return if the query variable is not set. Default empty.
  1444. * @return mixed Contents of the query variable.
  1445. */
  1446. public function get( $query_var, $default = '' ) {
  1447. if ( isset( $this->query_vars[ $query_var ] ) ) {
  1448. return $this->query_vars[ $query_var ];
  1449. }
  1450. return $default;
  1451. }
  1452. /**
  1453. * Set query variable.
  1454. *
  1455. * @since 1.5.0
  1456. * @access public
  1457. *
  1458. * @param string $query_var Query variable key.
  1459. * @param mixed $value Query variable value.
  1460. */
  1461. public function set($query_var, $value) {
  1462. $this->query_vars[$query_var] = $value;
  1463. }
  1464. /**
  1465. * Retrieve the posts based on query variables.
  1466. *
  1467. * There are a few filters and actions that can be used to modify the post
  1468. * database query.
  1469. *
  1470. * @since 1.5.0
  1471. * @access public
  1472. *
  1473. * @return array List of posts.
  1474. */
  1475. public function get_posts() {
  1476. global $wpdb;
  1477. $this->parse_query();
  1478. /**
  1479. * Fires after the query variable object is created, but before the actual query is run.
  1480. *
  1481. * Note: If using conditional tags, use the method versions within the passed instance
  1482. * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions
  1483. * like is_main_query() test against the global $wp_query instance, not the passed one.
  1484. *
  1485. * @since 2.0.0
  1486. *
  1487. * @param WP_Query &$this The WP_Query instance (passed by reference).
  1488. */
  1489. do_action_ref_array( 'pre_get_posts', array( &$this ) );
  1490. // Shorthand.
  1491. $q = &$this->query_vars;
  1492. // Fill again in case pre_get_posts unset some vars.
  1493. $q = $this->fill_query_vars($q);
  1494. // Parse meta query
  1495. $this->meta_query = new WP_Meta_Query();
  1496. $this->meta_query->parse_query_vars( $q );
  1497. // Set a flag if a pre_get_posts hook changed the query vars.
  1498. $hash = md5( serialize( $this->query_vars ) );
  1499. if ( $hash != $this->query_vars_hash ) {
  1500. $this->query_vars_changed = true;
  1501. $this->query_vars_hash = $hash;
  1502. }
  1503. unset($hash);
  1504. // First let's clear some variables
  1505. $distinct = '';
  1506. $whichauthor = '';
  1507. $whichmimetype = '';
  1508. $where = '';
  1509. $limits = '';
  1510. $join = '';
  1511. $search = '';
  1512. $groupby = '';
  1513. $post_status_join = false;
  1514. $page = 1;
  1515. if ( isset( $q['caller_get_posts'] ) ) {
  1516. _deprecated_argument( 'WP_Query', '3.1.0', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
  1517. if ( !isset( $q['ignore_sticky_posts'] ) )
  1518. $q['i…

Large files files are truncated, but you can click here to view the full file