| | 1014 | * Takes the query and arguments passed to the calling function, calling wpdb::prepare if needed. |
| | 1015 | * |
| | 1016 | * This will update the passed $query and $output variables based on what's found in $args. |
| | 1017 | * If passed the arguments that don't match one of the output flags, and pass it along to |
| | 1018 | * wpdb::prepare() to modify the $query arg before it gets used in the calling function. |
| | 1019 | * |
| | 1020 | * @since ?.? |
| | 1021 | * |
| | 1022 | * @uses OBJECT For output method detection. |
| | 1023 | * @uses OBJECT_K For output method detection. |
| | 1024 | * @uses ARRAY_A For output method detection. |
| | 1025 | * @uses ARRAY_N For output method detection. |
| | 1026 | * @uses wpdb::prepare() To prepare the query using the found prepare arguments. |
| | 1027 | * |
| | 1028 | * @param array $args The arguments passed to the calling function. |
| | 1029 | * @param string $query The query that was passed to the calling function. |
| | 1030 | * @param string &$output The output format the calling function should use (by reference). |
| | 1031 | * |
| | 1032 | * @return null|false|string The result of wpdb::prepare(), null if no query is passed. |
| | 1033 | */ |
| | 1034 | private function auto_prepare( $args = array(), $query = null, &$output = OBJECT ) { |
| | 1035 | if ( is_null( $query ) ) { |
| | 1036 | return; |
| | 1037 | } |
| | 1038 | |
| | 1039 | if ( count( $args ) > 1 ) { |
| | 1040 | // The first argument is the $query, extract it. |
| | 1041 | $query = array_shift( $args ); |
| | 1042 | |
| | 1043 | // Test if the first or last argument matches one of the output flags. |
| | 1044 | // These flags have been updated to random values, to avoid colisions |
| | 1045 | // with actual prepare arguments. |
| | 1046 | $outputs = array( OBJECT, OBJECT_K, ARRAY_A, ARRAY_N ); |
| | 1047 | if ( in_array( $args[0], $outputs, true ) ) { |
| | 1048 | $output = array_shift( $args ); |
| | 1049 | } elseif ( in_array( end( $args ), $outputs, true ) ) { |
| | 1050 | $output = array_pop( $args ); |
| | 1051 | } |
| | 1052 | |
| | 1053 | // Check if theres is only one argument, and that |
| | 1054 | // it's the array of prepare values (it could happen), |
| | 1055 | // make it the $args array if so. |
| | 1056 | if ( count( $args ) == 1 && is_array( $args[0] ) ) { |
| | 1057 | $args = $args[0]; |
| | 1058 | } |
| | 1059 | |
| | 1060 | // Make sure there are remaining arguments to use. |
| | 1061 | if ( count( $args ) > 0 ) { |
| | 1062 | // Call wpdb::prepare() and pass it's result to $query |
| | 1063 | $query = $this->prepare( $query, $args ); |
| | 1064 | } |
| | 1065 | } |
| | 1066 | } |
| | 1067 | |
| | 1068 | /** |
| | 1521 | * Retrieve the first selected value from the database. |
| | 1522 | * |
| | 1523 | * Auto prepares and calls wpdb::get_var(). |
| | 1524 | * |
| | 1525 | * @since ?.? |
| | 1526 | * |
| | 1527 | * @uses wpdb::auto_prepare() If other arguments are passed to it. |
| | 1528 | * @uses wpdb::get_var() To execute the auto prepared query. |
| | 1529 | * |
| | 1530 | * @param string $query SQL query. |
| | 1531 | * @param mixed $args... The output, and/or the variables to pass through wpdb::auto_prepare(). |
| | 1532 | * |
| | 1533 | * @return string|null Database query result (as string), or null on failure. |
| | 1534 | */ |
| | 1535 | function get_var_prepared( $query ){ |
| | 1536 | $this->func_call = "\$db->get_var_prepared(\"$query\", ".implode(', ', func_get_args()).")"; |
| | 1537 | |
| | 1538 | if( func_num_args() > 1 ) { |
| | 1539 | $query = $this->auto_prepare( func_get_args(), $query ); |
| | 1540 | } |
| | 1541 | |
| | 1542 | return $this->get_var( $query ); |
| | 1543 | } |
| | 1544 | |
| | 1545 | /** |
| | 1580 | * Retrieve the first selected row from the database. |
| | 1581 | * |
| | 1582 | * Auto prepares and calls wpdb::get_row(). |
| | 1583 | * |
| | 1584 | * @since ?.? |
| | 1585 | * |
| | 1586 | * @uses wpdb::auto_prepare() If other arguments are passed to it. |
| | 1587 | * @uses wpdb::get_row() To execute the auto prepared query. |
| | 1588 | * |
| | 1589 | * @param string $query SQL query. |
| | 1590 | * @param mixed $args... The output, and/or the variables to pass through wpdb::auto_prepare(). |
| | 1591 | * |
| | 1592 | * @return mixed Database query result in format specified by $output or null on failure. |
| | 1593 | */ |
| | 1594 | function get_row_prepared( $query ){ |
| | 1595 | $this->func_call = "\$db->get_row_prepared(\"$query\", ".implode(', ', func_get_args()).")"; |
| | 1596 | |
| | 1597 | if( func_num_args() > 1 ) { |
| | 1598 | $query = $this->auto_prepare( func_get_args(), $query, $output ); |
| | 1599 | } |
| | 1600 | |
| | 1601 | return $this->get_row( $query, $output ); |
| | 1602 | } |
| | 1603 | |
| | 1604 | /** |
| | 1630 | * Retrieve the first selected column from the database. |
| | 1631 | * |
| | 1632 | * Auto prepares and calls wpdb::get_col(). |
| | 1633 | * |
| | 1634 | * @since ?.? |
| | 1635 | * |
| | 1636 | * @uses wpdb::auto_prepare() If other arguments are passed to it. |
| | 1637 | * @uses wpdb::get_col() To execute the auto prepared query. |
| | 1638 | * |
| | 1639 | * @param string $query SQL query. |
| | 1640 | * @param mixed $args... The output, and/or the variables to pass through wpdb::auto_prepare(). |
| | 1641 | * |
| | 1642 | * @return mixed Database query results. Array indexed from 0 by SQL result row number. |
| | 1643 | */ |
| | 1644 | function get_col_prepared( $query ){ |
| | 1645 | $this->func_call = "\$db->get_col_prepared(\"$query\", ".implode(', ', func_get_args()).")"; |
| | 1646 | |
| | 1647 | if( func_num_args() > 1 ) { |
| | 1648 | $query = $this->auto_prepare( func_get_args(), $query ); |
| | 1649 | } |
| | 1650 | |
| | 1651 | return $this->get_col( $query ); |
| | 1652 | } |
| | 1653 | |
| | 1654 | /** |
| | 1708 | * Retrieve the results from the database. |
| | 1709 | * |
| | 1710 | * Auto prepares and calls wpdb::get_results(). |
| | 1711 | * |
| | 1712 | * @since ?.? |
| | 1713 | * |
| | 1714 | * @uses wpdb::auto_prepare() If other arguments are passed to it. |
| | 1715 | * @uses wpdb::get_results() To execute the auto prepared query. |
| | 1716 | * |
| | 1717 | * @param string $query SQL query. |
| | 1718 | * @param mixed $args... The output, and/or the variables to pass through wpdb::auto_prepare(). |
| | 1719 | * |
| | 1720 | * @return mixed Database query results. |
| | 1721 | */ |
| | 1722 | function get_results_prepared( $query ){ |
| | 1723 | $this->func_call = "\$db->get_results_prepared(\"$query\", ".implode(', ', func_get_args()).")"; |
| | 1724 | |
| | 1725 | if( func_num_args() > 1 ) { |
| | 1726 | $query = $this->auto_prepare( func_get_args(), $query, $output ); |
| | 1727 | } |
| | 1728 | |
| | 1729 | // Pass to get_results |
| | 1730 | return $this->get_results( $query, $output ); |
| | 1731 | } |
| | 1732 | |
| | 1733 | /** |