Ticket #12731: get_posts_callback_in_query.php.diff
| File get_posts_callback_in_query.php.diff, 11.7 KB (added by , 17 years ago) |
|---|
-
query.php
1092 1092 * @var bool 1093 1093 */ 1094 1094 var $is_posts_page = false; 1095 1096 /** 1097 * Sets a callback for get_posts(). 1098 * 1099 * @access private 1100 * @var bool 1101 */ 1102 var $_get_posts_callback = false; 1103 1095 1104 1096 1105 /** 1097 1106 * Resets query flags to false. … … 1585 1594 } 1586 1595 1587 1596 /** 1597 * Calls do_action_ref_array() normally when no callback or with Retrieve the posts based on query variables. 1598 * 1599 * Handles get_posts_callback when get_posts() needs to call do_action_ref_array(). 1600 * 1601 * @access private 1602 * @uses do_action_ref_array() 1603 * 1604 */ 1605 function _do_action_ref_array_or_callback($tag) { 1606 if ($this->_get_posts_callback) { 1607 do_action_ref_array($this->_get_posts_callback, array(&$this,$tag)); 1608 } else { 1609 do_action_ref_array($tag, array(&$this)); 1610 } 1611 } 1612 1613 /** 1614 * Calls do_action($tag,$value) or if callback then do_action("get_posts_callback_$tag",array($tag,$value,$query)) 1615 * 1616 * Handles get_posts_callback when get_posts() needs to call do_action(). 1617 * 1618 * @access private 1619 * @uses $this->_apply_filters_or_callback() Delegates this function 1620 * 1621 */ 1622 function _do_action_or_callback($tag,$value) { 1623 $this->_apply_filters_ref_array_or_callback($tag,array(&$this,$value)); 1624 } 1625 1626 /** 1627 * Calls apply_filters_ref_array($tag,$args) or if callback then apply_filters_ref_array("get_posts_callback_$tag",$args) 1628 * 1629 * Handles get_posts_callback when get_posts() needs to call apply_filters_ref_array(). 1630 * 1631 * @access private 1632 * @uses apply_filters() 1633 * 1634 * @return filtered value. 1635 */ 1636 function _apply_filters_ref_array_or_callback($tag,$args) { 1637 if ($this->_get_posts_callback) { 1638 $args[] = $tag; 1639 $value = apply_filters_ref_array($this->_get_posts_callback,&$args); 1640 } else { 1641 $value = apply_filters_ref_array($tag,&$args); 1642 } 1643 return $value; 1644 } 1645 1646 1647 /** 1588 1648 * Retrieve the posts based on query variables. 1589 1649 * 1590 1650 * There are a few filters and actions that can be used to modify the post … … 1599 1659 function &get_posts() { 1600 1660 global $wpdb, $user_ID; 1601 1661 1602 do_action_ref_array('pre_get_posts', array(&$this)); 1662 if (!isset($this->query_vars['callback'])) 1663 $this->_get_posts_callback = false; 1664 else if (!has_action($this->_get_posts_callback = 'get_posts_callback_' . $this->query_vars['callback'])) 1665 $this->_get_posts_callback = false; 1603 1666 1667 $this->_do_action_ref_array_or_callback('pre_get_posts', array(&$this)); 1668 1604 1669 // Shorthand. 1605 1670 $q = &$this->query_vars; 1606 1671 … … 1849 1914 $search .= " AND ($wpdb->posts.post_password = '') "; 1850 1915 } 1851 1916 } 1852 $search = apply_filters_ref_array('posts_search', array( $search, &$this ) );1917 $search = $this->_apply_filters_ref_array_or_callback('posts_search', array( $search, &$this ) ); 1853 1918 1854 1919 // Category stuff 1855 1920 … … 2292 2357 // Apply filters on where and join prior to paging so that any 2293 2358 // manipulations to them are reflected in the paging by day queries. 2294 2359 if ( !$q['suppress_filters'] ) { 2295 $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );2296 $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );2360 $where = $this->_apply_filters_ref_array_or_callback('posts_where', array( $where, &$this ) ); 2361 $join = $this->_apply_filters_ref_array_or_callback('posts_join', array( $join, &$this ) ); 2297 2362 } 2298 2363 2299 2364 // Paging … … 2327 2392 } 2328 2393 2329 2394 if ( !$q['suppress_filters'] ) { 2330 $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );2331 $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );2332 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );2333 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );2334 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );2395 $cjoin = $this->_apply_filters_ref_array_or_callback('comment_feed_join', array( $cjoin, &$this ) ); 2396 $cwhere = $this->_apply_filters_ref_array_or_callback('comment_feed_where', array( $cwhere, &$this ) ); 2397 $cgroupby = $this->_apply_filters_ref_array_or_callback('comment_feed_groupby', array( $cgroupby, &$this ) ); 2398 $corderby = $this->_apply_filters_ref_array_or_callback('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); 2399 $climits = $this->_apply_filters_ref_array_or_callback('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) ); 2335 2400 } 2336 2401 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; 2337 2402 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; … … 2357 2422 // Apply post-paging filters on where and join. Only plugins that 2358 2423 // manipulate paging queries should use these hooks. 2359 2424 if ( !$q['suppress_filters'] ) { 2360 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );2361 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );2362 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );2363 $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );2364 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );2365 $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );2366 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );2425 $where = $this->_apply_filters_ref_array_or_callback( 'posts_where_paged', array( $where, &$this ) ); 2426 $groupby = $this->_apply_filters_ref_array_or_callback( 'posts_groupby', array( $groupby, &$this ) ); 2427 $join = $this->_apply_filters_ref_array_or_callback( 'posts_join_paged', array( $join, &$this ) ); 2428 $orderby = $this->_apply_filters_ref_array_or_callback( 'posts_orderby', array( $orderby, &$this ) ); 2429 $distinct = $this->_apply_filters_ref_array_or_callback( 'posts_distinct', array( $distinct, &$this ) ); 2430 $limits = $this->_apply_filters_ref_array_or_callback( 'post_limits', array( $limits, &$this ) ); 2431 $fields = $this->_apply_filters_ref_array_or_callback( 'posts_fields', array( $fields, &$this ) ); 2367 2432 } 2368 2433 2369 2434 // Announce current selection parameters. For use by caching plugins. 2370 do_action('posts_selection', $where . $groupby . $orderby . $limits . $join );2435 $this->_do_action_or_callback('posts_selection', $where . $groupby . $orderby . $limits . $join ); 2371 2436 2372 2437 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above. 2373 2438 if ( !$q['suppress_filters'] ) { 2374 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );2375 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );2376 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );2377 $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );2378 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );2379 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );2380 $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );2439 $where = $this->_apply_filters_ref_array_or_callback( 'posts_where_request', array( $where, &$this ) ); 2440 $groupby = $this->_apply_filters_ref_array_or_callback( 'posts_groupby_request', array( $groupby, &$this ) ); 2441 $join = $this->_apply_filters_ref_array_or_callback( 'posts_join_request', array( $join, &$this ) ); 2442 $orderby = $this->_apply_filters_ref_array_or_callback( 'posts_orderby_request', array( $orderby, &$this ) ); 2443 $distinct = $this->_apply_filters_ref_array_or_callback( 'posts_distinct_request', array( $distinct, &$this ) ); 2444 $fields = $this->_apply_filters_ref_array_or_callback( 'posts_fields_request', array( $fields, &$this ) ); 2445 $limits = $this->_apply_filters_ref_array_or_callback( 'post_limits_request', array( $limits, &$this ) ); 2381 2446 } 2382 2447 2383 2448 if ( ! empty($groupby) ) … … 2390 2455 2391 2456 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits"; 2392 2457 if ( !$q['suppress_filters'] ) 2393 $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );2458 $this->request = $this->_apply_filters_ref_array_or_callback('posts_request', array( $this->request, &$this ) ); 2394 2459 2395 2460 $this->posts = $wpdb->get_results($this->request); 2396 2461 // Raw results filter. Prior to status checks. 2397 2462 if ( !$q['suppress_filters'] ) 2398 $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );2463 $this->posts = $this->_apply_filters_ref_array_or_callback('posts_results', array( $this->posts, &$this ) ); 2399 2464 2400 2465 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) { 2401 $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );2402 $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );2403 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );2466 $cjoin = $this->_apply_filters_ref_array_or_callback('comment_feed_join', array( '', &$this ) ); 2467 $cwhere = $this->_apply_filters_ref_array_or_callback('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) ); 2468 $cgroupby = $this->_apply_filters_ref_array_or_callback('comment_feed_groupby', array( '', &$this ) ); 2404 2469 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; 2405 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );2470 $corderby = $this->_apply_filters_ref_array_or_callback('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); 2406 2471 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; 2407 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );2472 $climits = $this->_apply_filters_ref_array_or_callback('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) ); 2408 2473 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits"; 2409 2474 $this->comments = $wpdb->get_results($comments_request); 2410 2475 $this->comment_count = count($this->comments); 2411 2476 } 2412 2477 2413 2478 if ( !$q['no_found_rows'] && !empty($limits) ) { 2414 $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );2479 $found_posts_query = $this->_apply_filters_ref_array_or_callback( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ); 2415 2480 $this->found_posts = $wpdb->get_var( $found_posts_query ); 2416 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );2481 $this->found_posts = $this->_apply_filters_ref_array_or_callback( 'found_posts', array( $this->found_posts, &$this ) ); 2417 2482 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']); 2418 2483 } 2419 2484 … … 2446 2511 } 2447 2512 2448 2513 if ( $this->is_preview && current_user_can( $edit_cap, $this->posts[0]->ID ) ) 2449 $this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this ));2514 $this->posts[0] = $this->_apply_filters_ref_array_or_callback('the_preview', array( $this->posts[0], &$this )); 2450 2515 } 2451 2516 2452 2517 // Put sticky posts at the top of the posts array … … 2495 2560 } 2496 2561 2497 2562 if ( !$q['suppress_filters'] ) 2498 $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );2563 $this->posts = $this->_apply_filters_ref_array_or_callback('the_posts', array( $this->posts, &$this ) ); 2499 2564 2500 2565 $this->post_count = count($this->posts); 2501 2566
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)