PodsData

The Pods - Custom Content Types and Fields PodsData class.

Defined (1)

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

/classes/PodsData.php  
  1. class PodsData { 
  2.  
  3. /** 
  4. * @var PodsData 
  5. */ 
  6. static $instance = null; 
  7.  
  8. /** 
  9. * @var string 
  10. */ 
  11. static protected $prefix = 'pods_'; 
  12.  
  13. /** 
  14. * @var array 
  15. */ 
  16. static protected $field_types = array(); 
  17.  
  18. /** 
  19. * @var bool 
  20. */ 
  21. public static $display_errors = true; 
  22.  
  23. /** 
  24. * @var PodsAPI 
  25. */ 
  26. public $api = null; 
  27.  
  28. /** 
  29. * @var null 
  30. */ 
  31. public $select = null; 
  32.  
  33. /** 
  34. * @var null 
  35. */ 
  36. public $table = null; 
  37.  
  38. /** 
  39. * @var null 
  40. */ 
  41. public $pod = null; 
  42.  
  43. /** 
  44. * @var array|bool|mixed|null|void 
  45. */ 
  46. public $pod_data = null; 
  47.  
  48. /** 
  49. * @var int 
  50. */ 
  51. public $id = 0; 
  52.  
  53. /** 
  54. * @var string 
  55. */ 
  56. public $field_id = 'id'; 
  57.  
  58. /** 
  59. * @var string 
  60. */ 
  61. public $field_index = 'name'; 
  62.  
  63. /** 
  64. * @var string 
  65. */ 
  66. public $field_slug = ''; 
  67.  
  68. /** 
  69. * @var string 
  70. */ 
  71. public $join = ''; 
  72.  
  73. /** 
  74. * @var array 
  75. */ 
  76. public $where = array(); 
  77.  
  78. /** 
  79. * @var array 
  80. */ 
  81. public $where_default = array(); 
  82.  
  83. /** 
  84. * @var string 
  85. */ 
  86. public $orderby = ''; 
  87.  
  88. /** 
  89. * @var array 
  90. */ 
  91. public $fields = array(); 
  92.  
  93. /** 
  94. * @var array 
  95. */ 
  96. public $aliases = array(); 
  97.  
  98. /** 
  99. * @var 
  100. */ 
  101. public $detail_page; 
  102.  
  103. // data 
  104. /** 
  105. * @var int 
  106. */ 
  107. public $row_number = -1; 
  108.  
  109. /** 
  110. * @var 
  111. */ 
  112. public $data; 
  113.  
  114. /** 
  115. * @var 
  116. */ 
  117. public $row; 
  118.  
  119. /** 
  120. * @var 
  121. */ 
  122. public $insert_id; 
  123.  
  124. /** 
  125. * @var 
  126. */ 
  127. public $total; 
  128.  
  129. /** 
  130. * @var 
  131. */ 
  132. public $total_found; 
  133.  
  134. /** 
  135. * @var bool 
  136. */ 
  137. public $total_found_calculated; 
  138.  
  139. // pagination 
  140. /** 
  141. * @var string 
  142. */ 
  143. public $page_var = 'pg'; 
  144.  
  145. /** 
  146. * @var int 
  147. */ 
  148. public $page = 1; 
  149.  
  150. /** 
  151. * @var int 
  152. */ 
  153. public $limit = 15; 
  154.  
  155. /** 
  156. * @var bool 
  157. */ 
  158. public $pagination = true; 
  159.  
  160. // search 
  161. /** 
  162. * @var bool 
  163. */ 
  164. public $search = true; 
  165.  
  166. /** 
  167. * @var string 
  168. */ 
  169. public $search_var = 'search'; 
  170.  
  171. /** 
  172. * @var string 
  173. */ 
  174. public $search_mode = 'int'; // int | text | text_like 
  175. /** 
  176. * @var string 
  177. */ 
  178. public $search_query = ''; 
  179.  
  180. /** 
  181. * @var array 
  182. */ 
  183. public $search_fields = array(); 
  184.  
  185. /** 
  186. * @var string 
  187. */ 
  188. public $search_where = array(); 
  189.  
  190. /** 
  191. * @var array 
  192. */ 
  193. public $filters = array(); 
  194.  
  195. /** 
  196. * Holds Traversal information about Pods 
  197. * @var array 
  198. */ 
  199. public $traversal = array(); 
  200.  
  201. /** 
  202. * Holds custom Traversals to be included 
  203. * @var array 
  204. */ 
  205. public $traverse = array(); 
  206.  
  207. /** 
  208. * Last select() query SQL 
  209. * @var string 
  210. */ 
  211. public $sql = false; 
  212.  
  213. /** 
  214. * Last total sql 
  215. * @var string 
  216. */ 
  217. public $total_sql = false; 
  218.  
  219. /** 
  220. * Singleton handling for a basic pods_data() request 
  221. * @param string $pod Pod name 
  222. * @param integer $id Pod Item ID 
  223. * @param bool $strict If true throws an error if a pod is not found. 
  224. * @return \PodsData 
  225. * @since 2.3.5 
  226. */ 
  227. public static function init ( $pod = null, $id = 0, $strict = true ) { 
  228. if ( ( true !== $pod && null !== $pod ) || 0 != $id ) 
  229. return new PodsData( $pod, $id, $strict ); 
  230. elseif ( !is_object( self::$instance ) ) 
  231. self::$instance = new PodsData(); 
  232. else { 
  233. $vars = get_class_vars( __CLASS__ ); 
  234.  
  235. foreach ( $vars as $var => $default ) { 
  236. if ( 'api' == $var ) 
  237. continue; 
  238.  
  239. self::$instance->{$var} = $default; 
  240.  
  241. return self::$instance; 
  242.  
  243. /** 
  244. * Data Abstraction Class for Pods 
  245. * @param string $pod Pod name 
  246. * @param integer $id Pod Item ID 
  247. * @param bool $strict If true throws an error if a pod is not found. 
  248. * @return \PodsData 
  249. * @license http://www.gnu.org/licenses/gpl-2.0.html 
  250. * @since 2.0 
  251. */ 
  252. public function __construct ( $pod = null, $id = 0, $strict = true ) { 
  253. global $wpdb; 
  254.  
  255. if ( is_object( $pod ) && 'PodsAPI' == get_class( $pod ) ) { 
  256. $this->api = $pod; 
  257. $pod = $this->api->pod; 
  258. else 
  259. $this->api = pods_api( $pod ); 
  260.  
  261. $this->api->display_errors =& self::$display_errors; 
  262.  
  263. if ( !empty( $pod ) ) { 
  264. $this->pod_data =& $this->api->pod_data; 
  265.  
  266. if ( false === $this->pod_data ) { 
  267. if ( true === $strict ) 
  268. return pods_error( 'Pod not found', $this ); 
  269. else 
  270. return $this; 
  271.  
  272. $this->pod_id = $this->pod_data[ 'id' ]; 
  273. $this->pod = $this->pod_data[ 'name' ]; 
  274. $this->fields = $this->pod_data[ 'fields' ]; 
  275.  
  276. if ( isset( $this->pod_data[ 'options' ][ 'detail_url' ] ) ) 
  277. $this->detail_page = $this->pod_data[ 'options' ][ 'detail_url' ]; 
  278.  
  279. if ( isset( $this->pod_data[ 'select' ] ) ) 
  280. $this->select = $this->pod_data[ 'select' ]; 
  281.  
  282. if ( isset( $this->pod_data[ 'table' ] ) ) 
  283. $this->table = $this->pod_data[ 'table' ]; 
  284.  
  285. if ( isset( $this->pod_data[ 'join' ] ) ) 
  286. $this->join = $this->pod_data[ 'join' ]; 
  287.  
  288. if ( isset( $this->pod_data[ 'field_id' ] ) ) 
  289. $this->field_id = $this->pod_data[ 'field_id' ]; 
  290.  
  291. if ( isset( $this->pod_data[ 'field_index' ] ) ) 
  292. $this->field_index = $this->pod_data[ 'field_index' ]; 
  293.  
  294. if ( isset( $this->pod_data[ 'field_slug' ] ) ) 
  295. $this->field_slug = $this->pod_data[ 'field_slug' ]; 
  296.  
  297. if ( isset( $this->pod_data[ 'where' ] ) ) 
  298. $this->where = $this->pod_data[ 'where' ]; 
  299.  
  300. if ( isset( $this->pod_data[ 'where_default' ] ) ) 
  301. $this->where_default = $this->pod_data[ 'where_default' ]; 
  302.  
  303. if ( isset( $this->pod_data[ 'orderby' ] ) ) 
  304. $this->orderby = $this->pod_data[ 'orderby' ]; 
  305.  
  306. if ( 'settings' == $this->pod_data[ 'type' ] ) { 
  307. $this->id = $this->pod_data[ 'id' ]; 
  308.  
  309. $this->fetch( $this->id ); 
  310. elseif ( null !== $id && !is_array( $id ) && !is_object( $id ) ) { 
  311. $this->id = $id; 
  312.  
  313. $this->fetch( $this->id ); 
  314.  
  315. /** 
  316. * Handle tables like they are Pods (for traversal in select/build) 
  317. * @param array|string $table 
  318. * @param string $object 
  319. */ 
  320. public function table ( $table, $object = '' ) { 
  321. global $wpdb; 
  322.  
  323. if ( !is_array( $table ) ) { 
  324. $object_type = ''; 
  325.  
  326. if ( $wpdb->users == $table ) 
  327. $object_type = 'user'; 
  328. elseif ( $wpdb->posts == $table ) 
  329. $object_type = 'post_type'; 
  330. elseif ( $wpdb->terms == $table ) 
  331. $object_type = 'taxonomy'; 
  332. elseif ( $wpdb->options == $table ) 
  333. $object_type = 'settings'; 
  334.  
  335. if ( !empty( $object_type ) ) 
  336. $table = $this->api->get_table_info( $object_type, $object ); 
  337.  
  338. if ( !empty( $table ) && is_array( $table ) ) { 
  339. $table[ 'id' ] = pods_var( 'id', $table[ 'pod' ], 0, null, true ); 
  340. $table[ 'name' ] = pods_var( 'name', $table[ 'pod' ], $table[ 'object_type' ], null, true ); 
  341. $table[ 'type' ] = pods_var_raw( 'type', $table[ 'pod' ], $table[ 'object_type' ], null, true ); 
  342. $table[ 'storage' ] = pods_var_raw( 'storage', $table[ 'pod' ], ( 'taxonomy' == $table[ 'object_type' ] ? 'none' : 'meta' ), null, true ); 
  343. $table[ 'fields' ] = pods_var_raw( 'fields', $table[ 'pod' ], array() ); 
  344. $table[ 'object_fields' ] = pods_var_raw( 'object_fields', $table[ 'pod' ], $this->api->get_wp_object_fields( $table[ 'object_type' ] ), null, true ); 
  345.  
  346. $this->pod_data = $table; 
  347. $this->pod_id = $this->pod_data[ 'id' ]; 
  348. $this->pod = $this->pod_data[ 'name' ]; 
  349. $this->fields = $this->pod_data[ 'fields' ]; 
  350.  
  351. if ( isset( $this->pod_data[ 'select' ] ) ) 
  352. $this->select = $this->pod_data[ 'select' ]; 
  353.  
  354. if ( isset( $this->pod_data[ 'table' ] ) ) 
  355. $this->table = $this->pod_data[ 'table' ]; 
  356.  
  357. if ( isset( $this->pod_data[ 'join' ] ) ) 
  358. $this->join = $this->pod_data[ 'join' ]; 
  359.  
  360. if ( isset( $this->pod_data[ 'field_id' ] ) ) 
  361. $this->field_id = $this->pod_data[ 'field_id' ]; 
  362.  
  363. if ( isset( $this->pod_data[ 'field_index' ] ) ) 
  364. $this->field_index = $this->pod_data[ 'field_index' ]; 
  365.  
  366. if ( isset( $this->pod_data[ 'field_slug' ] ) ) 
  367. $this->field_slug = $this->pod_data[ 'field_slug' ]; 
  368.  
  369. if ( isset( $this->pod_data[ 'where' ] ) ) 
  370. $this->where = $this->pod_data[ 'where' ]; 
  371.  
  372. if ( isset( $this->pod_data[ 'where_default' ] ) ) 
  373. $this->where_default = $this->pod_data[ 'where_default' ]; 
  374.  
  375. if ( isset( $this->pod_data[ 'orderby' ] ) ) 
  376. $this->orderby = $this->pod_data[ 'orderby' ]; 
  377.  
  378. /** 
  379. * Insert an item, eventually mapping to WPDB::insert 
  380. * @param string $table Table name 
  381. * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 
  382. * @param array $format (optional) An array of formats to be mapped to each of the value in $data. 
  383. * @return int|bool The ID of the item 
  384. * @uses wpdb::insert 
  385. * @since 2.0 
  386. */ 
  387. public function insert ( $table, $data, $format = null ) { 
  388. /** 
  389. * @var $wpdb wpdb 
  390. */ 
  391. global $wpdb; 
  392.  
  393. if ( strlen( $table ) < 1 || empty( $data ) || !is_array( $data ) ) 
  394. return false; 
  395.  
  396. if ( empty( $format ) ) { 
  397. $format = array(); 
  398.  
  399. foreach ( $data as $field ) { 
  400. if ( isset( self::$field_types[ $field ] ) ) 
  401. $format[] = self::$field_types[ $field ]; 
  402. elseif ( isset( $wpdb->field_types[ $field ] ) ) 
  403. $format[] = $wpdb->field_types[ $field ]; 
  404. else 
  405. break; 
  406.  
  407. list( $table, $data, $format ) = $this->do_hook( 'insert', array( $table, $data, $format ) ); 
  408.  
  409. $result = $wpdb->insert( $table, $data, $format ); 
  410. $this->insert_id = $wpdb->insert_id; 
  411.  
  412. if ( false !== $result ) 
  413. return $this->insert_id; 
  414.  
  415. return false; 
  416.  
  417. /** 
  418. * @static 
  419. * Insert into a table, if unique key exists just update values. 
  420. * Data must be a key value pair array, keys act as table rows. 
  421. * Returns the prepared query from wpdb or false for errors 
  422. * @param string $table Name of the table to update 
  423. * @param array $data column => value pairs 
  424. * @param array $formats For $wpdb->prepare, uses sprintf formatting 
  425. * @return mixed Sanitized query string 
  426. * @uses wpdb::prepare 
  427. * @since 2.0 
  428. */ 
  429. public static function insert_on_duplicate ( $table, $data, $formats = array() ) { 
  430. /** 
  431. * @var $wpdb wpdb 
  432. */ 
  433. global $wpdb; 
  434.  
  435. $columns = array_keys( $data ); 
  436.  
  437. $update = array(); 
  438. $values = array(); 
  439.  
  440. foreach ( $columns as $column ) { 
  441. $update[] = "`{$column}` = VALUES( `{$column}` )"; 
  442. $values[] = "%s"; 
  443.  
  444. if ( empty( $formats ) ) 
  445. $formats = $values; 
  446.  
  447. $columns_data = implode( '`, `', $columns ); 
  448. $formats = implode( ", ", $formats ); 
  449. $update = implode( ', ', $update ); 
  450.  
  451. $sql = "INSERT INTO `{$table}` ( `{$columns_data}` ) VALUES ( {$formats} ) ON DUPLICATE KEY UPDATE {$update}"; 
  452.  
  453. return $wpdb->prepare( $sql, $data ); 
  454.  
  455. /** 
  456. * Update an item, eventually mapping to WPDB::update 
  457. * @param string $table Table name 
  458. * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 
  459. * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw". 
  460. * @param array $format (optional) An array of formats to be mapped to each of the values in $data. 
  461. * @param array $where_format (optional) An array of formats to be mapped to each of the values in $where. 
  462. * @return bool 
  463. * @since 2.0 
  464. */ 
  465. public function update ( $table, $data, $where, $format = null, $where_format = null ) { 
  466. /** 
  467. * @var $wpdb wpdb 
  468. */ 
  469. global $wpdb; 
  470.  
  471. if ( strlen( $table ) < 1 || empty( $data ) || !is_array( $data ) ) 
  472. return false; 
  473.  
  474. if ( empty( $format ) ) { 
  475. $format = array(); 
  476.  
  477. foreach ( $data as $field ) { 
  478. if ( isset( self::$field_types[ $field ] ) ) 
  479. $form = self::$field_types[ $field ]; 
  480. elseif ( isset( $wpdb->field_types[ $field ] ) ) 
  481. $form = $wpdb->field_types[ $field ]; 
  482. else 
  483. $form = '%s'; 
  484.  
  485. $format[] = $form; 
  486.  
  487. if ( empty( $where_format ) ) { 
  488. $where_format = array(); 
  489.  
  490. foreach ( (array) array_keys( $where ) as $field ) { 
  491. if ( isset( self::$field_types[ $field ] ) ) 
  492. $form = self::$field_types[ $field ]; 
  493. elseif ( isset( $wpdb->field_types[ $field ] ) ) 
  494. $form = $wpdb->field_types[ $field ]; 
  495. else 
  496. $form = '%s'; 
  497.  
  498. $where_format[] = $form; 
  499.  
  500. list( $table, $data, $where, $format, $where_format ) = $this->do_hook( 'update', array( 
  501. $table,  
  502. $data,  
  503. $where,  
  504. $format,  
  505. $where_format 
  506. ) ); 
  507.  
  508. $result = $wpdb->update( $table, $data, $where, $format, $where_format ); 
  509.  
  510. if ( false !== $result ) 
  511. return true; 
  512.  
  513. return false; 
  514.  
  515. /** 
  516. * Delete an item 
  517. * @param string $table Table name 
  518. * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw". 
  519. * @param array $where_format (optional) An array of formats to be mapped to each of the values in $where. 
  520. * @return array|bool|mixed|null|void 
  521. * @uses PodsData::query 
  522. * @uses PodsData::prepare 
  523. * @since 2.0 
  524. */ 
  525. public function delete ( $table, $where, $where_format = null ) { 
  526. /** 
  527. * @var $wpdb wpdb 
  528. */ 
  529. global $wpdb; 
  530.  
  531. if ( strlen( $table ) < 1 || empty( $where ) || !is_array( $where ) ) 
  532. return false; 
  533.  
  534. $wheres = array(); 
  535. $where_formats = $where_format = (array) $where_format; 
  536.  
  537. foreach ( (array) array_keys( $where ) as $field ) { 
  538. if ( !empty( $where_format ) ) 
  539. $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[ 0 ]; 
  540. elseif ( isset( self::$field_types[ $field ] ) ) 
  541. $form = self::$field_types[ $field ]; 
  542. elseif ( isset( $wpdb->field_types[ $field ] ) ) 
  543. $form = $wpdb->field_types[ $field ]; 
  544. else 
  545. $form = '%s'; 
  546.  
  547. $wheres[] = "`{$field}` = {$form}"; 
  548.  
  549. $sql = "DELETE FROM `$table` WHERE " . implode( ' AND ', $wheres ); 
  550.  
  551. list( $sql, $where ) = $this->do_hook( 'delete', array( 
  552. $sql,  
  553. array_values( $where ) 
  554. ), $table, $where, $where_format, $wheres ); 
  555.  
  556. return $this->query( self::prepare( $sql, $where ) ); 
  557.  
  558. /** 
  559. * Select items, eventually building dynamic query 
  560. * @param array $params 
  561. * @return array|bool|mixed 
  562. * @since 2.0 
  563. */ 
  564. public function select ( $params ) { 
  565. global $wpdb; 
  566.  
  567. $cache_key = $results = false; 
  568.  
  569. $params = apply_filters( 'pods_data_pre_select_params', $params ); 
  570.  
  571. // Debug purposes 
  572. if ( 1 == pods_v( 'pods_debug_params', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) 
  573. pods_debug( $params ); 
  574.  
  575. // Get from cache if enabled 
  576. if ( null !== pods_v( 'expires', $params, null, false ) ) { 
  577. $cache_key = md5( (string) $this->pod . serialize( $params ) ); 
  578.  
  579. $results = pods_view_get( $cache_key, pods_v( 'cache_mode', $params, 'cache', true ), 'pods_data_select' ); 
  580.  
  581. if ( empty( $results ) ) 
  582. $results = false; 
  583.  
  584. if ( empty( $results ) ) { 
  585. // Build 
  586. $this->sql = $this->build( $params ); 
  587.  
  588. // Debug purposes 
  589. if ( ( 1 == pods_v( 'pods_debug_sql', 'get', 0 ) || 1 == pods_v( 'pods_debug_sql_all', 'get', 0 ) ) && pods_is_admin( array( 'pods' ) ) ) 
  590. echo '<textarea cols="100" rows="24">' . esc_textarea( str_replace( array( '@wp_users', '@wp_' ), array( $wpdb->users, $wpdb->prefix ), $this->sql ) ) . '</textarea>'; 
  591.  
  592. if ( empty( $this->sql ) ) 
  593. return array(); 
  594.  
  595. // Get Data 
  596. $results = pods_query( $this->sql, $this ); 
  597.  
  598. // Cache if enabled 
  599. if ( false !== $cache_key ) 
  600. pods_view_set( $cache_key, $results, pods_v( 'expires', $params, 0, false ), pods_v( 'cache_mode', $params, 'cache', true ), 'pods_data_select' ); 
  601.  
  602. $results = apply_filters( 'pods_data_select', $results, $params, $this ); 
  603.  
  604. $this->data = $results; 
  605.  
  606. $this->row_number = -1; 
  607. $this->row = null; 
  608.  
  609. // Fill in empty field data (if none provided) 
  610. if ( ( !isset( $this->fields ) || empty( $this->fields ) ) && !empty( $this->data ) ) { 
  611. $this->fields = array(); 
  612. $data = (array) @current( $this->data ); 
  613.  
  614. foreach ( $data as $data_key => $data_value ) { 
  615. $this->fields[ $data_key ] = array( 'label' => ucwords( str_replace( '-', ' ', str_replace( '_', ' ', $data_key ) ) ) ); 
  616. if ( isset( $this->pod_data[ 'object_fields' ][ $data_key ] ) ) { 
  617. $this->fields[ $data_key ] = $this->pod_data[ 'object_fields' ][ $data_key ]; 
  618.  
  619. $this->fields = PodsForm::fields_setup( $this->fields ); 
  620.  
  621. $this->total_found_calculated = false; 
  622.  
  623. $this->total = 0; 
  624.  
  625. if ( ! empty( $this->data ) ) { 
  626. $this->total = count( (array) $this->data ); 
  627.  
  628. return $this->data; 
  629.  
  630. public function calculate_totals () { 
  631. /** 
  632. * @var $wpdb wpdb 
  633. */ 
  634. global $wpdb; 
  635.  
  636. // Set totals 
  637. if ( false !== $this->total_sql ) 
  638. $total = @current( $wpdb->get_col( $this->get_sql( $this->total_sql ) ) ); 
  639. else 
  640. $total = @current( $wpdb->get_col( "SELECT FOUND_ROWS()" ) ); 
  641.  
  642. $total = $this->do_hook( 'select_total', $total ); 
  643. $this->total_found = 0; 
  644. $this->total_found_calculated = true; 
  645.  
  646. if ( is_numeric( $total ) ) 
  647. $this->total_found = $total; 
  648.  
  649. /** 
  650. * Build/Rewrite dynamic SQL and handle search/filter/sort 
  651. * @param array $params 
  652. * @return bool|mixed|string 
  653. * @since 2.0 
  654. */ 
  655. public function build ( $params ) { 
  656. $simple_tableless_objects = PodsForm::simple_tableless_objects(); 
  657. $file_field_types = PodsForm::file_field_types(); 
  658.  
  659. $defaults = array( 
  660. 'select' => '*',  
  661. 'calc_rows' => false,  
  662. 'distinct' => true,  
  663. 'table' => null,  
  664. 'join' => null,  
  665. 'where' => null,  
  666. 'groupby' => null,  
  667. 'having' => null,  
  668. 'orderby' => null,  
  669. 'limit' => -1,  
  670. 'offset' => null,  
  671.  
  672. 'id' => null,  
  673. 'index' => null,  
  674.  
  675. 'page' => 1,  
  676. 'pagination' => $this->pagination,  
  677. 'search' => $this->search,  
  678. 'search_query' => null,  
  679. 'search_mode' => null,  
  680. 'search_across' => false,  
  681. 'search_across_picks' => false,  
  682. 'search_across_files' => false,  
  683. 'filters' => array(),  
  684.  
  685. 'fields' => array(),  
  686. 'object_fields' => array(),  
  687. 'pod_table_prefix' => null,  
  688.  
  689. 'traverse' => array(),  
  690.  
  691. 'sql' => null,  
  692.  
  693. 'strict' => false 
  694. ); 
  695.  
  696. $params = (object) array_merge( $defaults, (array) $params ); 
  697.  
  698. if ( 0 < strlen( $params->sql ) ) 
  699. return $params->sql; 
  700.  
  701. $pod = false; 
  702.  
  703. if ( is_array( $this->pod_data ) ) { 
  704. $pod = $this->pod_data; 
  705.  
  706. // Validate 
  707. $params->page = pods_absint( $params->page ); 
  708.  
  709. $params->pagination = (boolean) $params->pagination; 
  710.  
  711. if ( 0 == $params->page || !$params->pagination ) 
  712. $params->page = 1; 
  713.  
  714. $params->limit = (int) $params->limit; 
  715.  
  716. if ( 0 == $params->limit ) 
  717. $params->limit = -1; 
  718.  
  719. $this->limit = $params->limit; 
  720.  
  721. $offset = ( $params->limit * ( $params->page - 1 ) ); 
  722.  
  723. if ( 0 < (int) $params->offset ) 
  724. $params->offset += $offset; 
  725. else 
  726. $params->offset = $offset; 
  727.  
  728. if ( !$params->pagination || -1 == $params->limit ) { 
  729. $params->page = 1; 
  730. $params->offset = 0; 
  731.  
  732. if ( ( empty( $params->fields ) || !is_array( $params->fields ) ) && !empty( $pod ) && isset( $this->fields ) && !empty( $this->fields ) ) 
  733. $params->fields = $this->fields; 
  734.  
  735. if ( ( empty( $params->object_fields ) || !is_array( $params->object_fields ) ) && !empty( $pod ) && isset( $pod[ 'object_fields' ] ) && !empty( $pod[ 'object_fields' ] ) ) 
  736. $params->object_fields = $pod[ 'object_fields' ]; 
  737.  
  738. if ( empty( $params->filters ) && $params->search ) 
  739. $params->filters = array_keys( $params->fields ); 
  740. elseif ( empty( $params->filters ) ) 
  741. $params->filters = array(); 
  742.  
  743. if ( empty( $params->index ) ) 
  744. $params->index = $this->field_index; 
  745.  
  746. if ( empty( $params->id ) ) 
  747. $params->id = $this->field_id; 
  748.  
  749. if ( empty( $params->table ) && !empty( $pod ) && isset( $this->table ) && !empty( $this->table ) ) 
  750. $params->table = $this->table; 
  751.  
  752. if ( empty( $params->pod_table_prefix ) ) 
  753. $params->pod_table_prefix = 't'; 
  754.  
  755. if ( !empty( $pod ) && !in_array( $pod[ 'type' ], array( 'pod', 'table' ) ) && 'table' == $pod[ 'storage' ] ) 
  756. $params->pod_table_prefix = 'd'; 
  757.  
  758. $params->meta_fields = false; 
  759.  
  760. if ( !empty( $pod ) && !in_array( $pod[ 'type' ], array( 'pod', 'table', 'taxonomy' ) ) && 'meta' == $pod[ 'storage' ] ) 
  761. $params->meta_fields = true; 
  762.  
  763. if ( empty( $params->table ) ) 
  764. return false; 
  765.  
  766. if ( false === strpos( $params->table, '(' ) && false === strpos( $params->table, '`' ) ) 
  767. $params->table = '`' . $params->table . '`'; 
  768.  
  769. if ( !empty( $params->join ) ) 
  770. $params->join = array_merge( (array) $this->join, (array) $params->join ); 
  771. elseif ( false === $params->strict ) 
  772. $params->join = $this->join; 
  773.  
  774. $params->where_defaulted = false; 
  775. $params->where_default = $this->where_default; 
  776.  
  777. if ( false === $params->strict ) { 
  778. // Set default where 
  779. if ( !empty( $this->where_default ) && empty( $params->where ) ) { 
  780. $params->where = array_values( (array) $this->where_default ); 
  781.  
  782. $params->where_defaulted = true; 
  783.  
  784. if ( !empty( $this->where ) ) { 
  785. if ( is_array( $params->where ) && isset( $params->where[ 'relation' ] ) && 'OR' == strtoupper( $params->where[ 'relation' ] ) ) { 
  786. $params->where = array_merge( array( $params->where ), array_values( (array) $this->where ) ); 
  787. else { 
  788. $params->where = array_merge( (array) $params->where, array_values( (array) $this->where ) ); 
  789.  
  790. // Allow where array ( 'field' => 'value' ) and WP_Query meta_query syntax 
  791. if ( ! empty( $params->where ) ) { 
  792. $params->where = $this->query_fields( (array) $params->where, $pod, $params ); 
  793.  
  794. if ( empty( $params->where ) ) { 
  795. $params->where = array(); 
  796. } else { 
  797. $params->where = (array) $params->where; 
  798.  
  799. // Allow having array ( 'field' => 'value' ) and WP_Query meta_query syntax 
  800. if ( ! empty( $params->having ) ) { 
  801. $params->having = $this->query_fields( (array) $params->having, $pod, $params ); 
  802.  
  803. if ( empty( $params->having ) ) { 
  804. $params->having = array(); 
  805. } else { 
  806. $params->having = (array) $params->having; 
  807.  
  808. if ( !empty( $params->orderby ) ) 
  809. $params->orderby = (array) $params->orderby; 
  810. else 
  811. $params->orderby = array(); 
  812.  
  813. if ( false === $params->strict && !empty( $this->orderby ) ) 
  814. $params->orderby = array_merge( $params->orderby, (array) $this->orderby ); 
  815.  
  816. if ( !empty( $params->traverse ) ) 
  817. $this->traverse = $params->traverse; 
  818.  
  819. $allowed_search_modes = array( 'int', 'text', 'text_like' ); 
  820.  
  821. if ( !empty( $params->search_mode ) && in_array( $params->search_mode, $allowed_search_modes ) ) 
  822. $this->search_mode = $params->search_mode; 
  823.  
  824. $params->search = (boolean) $params->search; 
  825.  
  826. if ( 1 == pods_v( 'pods_debug_params_all', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) 
  827. pods_debug( $params ); 
  828.  
  829. $params->field_table_alias = 't'; 
  830.  
  831. // Get Aliases for future reference 
  832. $selectsfound = ''; 
  833.  
  834. if ( !empty( $params->select ) ) { 
  835. if ( is_array( $params->select ) ) 
  836. $selectsfound = implode( ', ', $params->select ); 
  837. else 
  838. $selectsfound = $params->select; 
  839.  
  840. // Pull Aliases from SQL query too 
  841. if ( null !== $params->sql ) { 
  842. $temp_sql = ' ' . trim( str_replace( array( "\n", "\r" ), ' ', $params->sql ) ); 
  843. $temp_sql = preg_replace( array( 
  844. '/\sSELECT\sSQL_CALC_FOUND_ROWS\s/i',  
  845. '/\sSELECT\s/i' 
  846. ),  
  847. array( 
  848. ' SELECT ',  
  849. ' SELECT SQL_CALC_FOUND_ROWS ' 
  850. ),  
  851. $temp_sql ); 
  852. preg_match( '/\sSELECT SQL_CALC_FOUND_ROWS\s(.*)\sFROM/i', $temp_sql, $selectmatches ); 
  853. if ( isset( $selectmatches[ 1 ] ) && !empty( $selectmatches[ 1 ] ) && false !== stripos( $selectmatches[ 1 ], ' AS ' ) ) 
  854. $selectsfound .= ( !empty( $selectsfound ) ? ', ' : '' ) . $selectmatches[ 1 ]; 
  855.  
  856. // Build Alias list 
  857. $this->aliases = array(); 
  858.  
  859. if ( !empty( $selectsfound ) && false !== stripos( $selectsfound, ' AS ' ) ) { 
  860. $theselects = array_filter( explode( ', ', $selectsfound ) ); 
  861.  
  862. if ( empty( $theselects ) ) 
  863. $theselects = array_filter( explode( ', ', $selectsfound ) ); 
  864.  
  865. foreach ( $theselects as $selected ) { 
  866. $selected = trim( $selected ); 
  867.  
  868. if ( strlen( $selected ) < 1 ) 
  869. continue; 
  870.  
  871. $selectfield = explode( ' AS ', str_replace( ' as ', ' AS ', $selected ) ); 
  872.  
  873. if ( 2 == count( $selectfield ) ) { 
  874. $field = trim( trim( $selectfield[ 1 ] ), '`' ); 
  875. $real_field = trim( trim( $selectfield[ 0 ] ), '`' ); 
  876. $this->aliases[ $field ] = $real_field; 
  877.  
  878. // Search 
  879. if ( !empty( $params->search ) && !empty( $params->fields ) ) { 
  880. if ( false !== $params->search_query && 0 < strlen( $params->search_query ) ) { 
  881. $where = $having = array(); 
  882.  
  883. if ( false !== $params->search_across ) { 
  884. foreach ( $params->fields as $key => $field ) { 
  885. if ( is_array( $field ) ) { 
  886. $attributes = $field; 
  887. $field = pods_v( 'name', $field, $key, true ); 
  888. else { 
  889. $attributes = array( 
  890. 'type' => '',  
  891. 'options' => array() 
  892. ); 
  893.  
  894. if ( isset( $attributes[ 'options' ][ 'search' ] ) && !$attributes[ 'options' ][ 'search' ] ) 
  895. continue; 
  896.  
  897. if ( in_array( $attributes[ 'type' ], array( 'date', 'time', 'datetime', 'number', 'decimal', 'currency', 'phone', 'password', 'boolean' ) ) ) 
  898. continue; 
  899.  
  900. $fieldfield = '`' . $field . '`'; 
  901.  
  902. if ( 'pick' == $attributes[ 'type' ] && !in_array( pods_v( 'pick_object', $attributes ), $simple_tableless_objects ) ) { 
  903. if ( false === $params->search_across_picks ) 
  904. continue; 
  905. else { 
  906. if ( empty( $attributes[ 'table_info' ] ) ) 
  907. $attributes[ 'table_info' ] = $this->api->get_table_info( pods_v( 'pick_object', $attributes ), pods_v( 'pick_val', $attributes ) ); 
  908.  
  909. if ( empty( $attributes[ 'table_info' ][ 'field_index' ] ) ) 
  910. continue; 
  911.  
  912. $fieldfield = $fieldfield . '.`' . $attributes[ 'table_info' ][ 'field_index' ] . '`'; 
  913. elseif ( in_array( $attributes[ 'type' ], $file_field_types ) ) { 
  914. if ( false === $params->search_across_files ) 
  915. continue; 
  916. else 
  917. $fieldfield = $fieldfield . '.`post_title`'; 
  918. elseif ( isset( $params->fields[ $field ] ) ) { 
  919. if ( $params->meta_fields ) 
  920. $fieldfield = $fieldfield . '.`' . $params->pod_table_prefix . '`'; 
  921. else 
  922. $fieldfield = '`' . $params->pod_table_prefix . '`.' . $fieldfield; 
  923. elseif ( !empty( $params->object_fields ) && !isset( $params->object_fields[ $field ] ) ) 
  924. $fieldfield = $fieldfield . '.`meta_value`'; 
  925. else 
  926. $fieldfield = '`t`.' . $fieldfield; 
  927.  
  928. if ( isset( $this->aliases[ $field ] ) ) 
  929. $fieldfield = '`' . $this->aliases[ $field ] . '`'; 
  930.  
  931. if ( !empty( $attributes[ 'real_name' ] ) ) 
  932. $fieldfield = $attributes[ 'real_name' ]; 
  933.  
  934. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) 
  935. $having[] = "{$fieldfield} LIKE '%" . pods_sanitize_like( $params->search_query ) . "%'"; 
  936. else 
  937. $where[] = "{$fieldfield} LIKE '%" . pods_sanitize_like( $params->search_query ) . "%'"; 
  938. elseif ( !empty( $params->index ) ) { 
  939. $attributes = array(); 
  940.  
  941. $fieldfield = '`t`.`' . $params->index . '`'; 
  942.  
  943. if ( isset( $params->fields[ $params->index ] ) ) { 
  944. if ( $params->meta_fields ) 
  945. $fieldfield = '`' . $params->index . '`.`' . $params->pod_table_prefix . '`'; 
  946. else 
  947. $fieldfield = '`' . $params->pod_table_prefix . '`.`' . $params->index . '`'; 
  948. elseif ( !empty( $params->object_fields ) && !isset( $params->object_fields[ $params->index ] ) ) 
  949. $fieldfield = '`' . $params->index . '`.`meta_value`'; 
  950.  
  951. if ( isset( $attributes[ 'real_name' ] ) && false !== $attributes[ 'real_name' ] && !empty( $attributes[ 'real_name' ] ) ) 
  952. $fieldfield = $attributes[ 'real_name' ]; 
  953.  
  954. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) 
  955. $having[] = "{$fieldfield} LIKE '%" . pods_sanitize_like( $params->search_query ) . "%'"; 
  956. else 
  957. $where[] = "{$fieldfield} LIKE '%" . pods_sanitize_like( $params->search_query ) . "%'"; 
  958.  
  959. if ( !empty( $where ) ) 
  960. $params->where[] = '( ' . implode( ' OR ', $where ) . ' )'; 
  961.  
  962. if ( !empty( $having ) ) 
  963. $params->having[] = '( ' . implode( ' OR ', $having ) . ' )'; 
  964.  
  965. // Filter 
  966. foreach ( $params->filters as $filter ) { 
  967. $where = $having = array(); 
  968.  
  969. if ( !isset( $params->fields[ $filter ] ) ) 
  970. continue; 
  971.  
  972. $attributes = $params->fields[ $filter ]; 
  973. $field = pods_v( 'name', $attributes, $filter, true ); 
  974.  
  975. $filterfield = '`' . $field . '`'; 
  976.  
  977. if ( 'pick' == $attributes[ 'type' ] && !in_array( pods_v( 'pick_object', $attributes ), $simple_tableless_objects ) ) { 
  978. if ( empty( $attributes[ 'table_info' ] ) ) 
  979. $attributes[ 'table_info' ] = $this->api->get_table_info( pods_v( 'pick_object', $attributes ), pods_v( 'pick_val', $attributes ) ); 
  980.  
  981. if ( empty( $attributes[ 'table_info' ][ 'field_index' ] ) ) 
  982. continue; 
  983.  
  984. $filterfield = $filterfield . '.`' . $attributes[ 'table_info' ][ 'field_index' ] . '`'; 
  985. elseif ( in_array( $attributes[ 'type' ], $file_field_types ) ) 
  986. $filterfield = $filterfield . '.`post_title`'; 
  987. elseif ( isset( $params->fields[ $field ] ) ) { 
  988. if ( $params->meta_fields ) 
  989. $filterfield = $filterfield . '.`meta_value`'; 
  990. else 
  991. $filterfield = '`' . $params->pod_table_prefix . '`.' . $filterfield; 
  992. elseif ( !empty( $params->object_fields ) && !isset( $params->object_fields[ $field ] ) ) 
  993. $filterfield = $filterfield . '.`meta_value`'; 
  994. else 
  995. $filterfield = '`t`.' . $filterfield; 
  996.  
  997. if ( isset( $this->aliases[ $field ] ) ) 
  998. $filterfield = '`' . $this->aliases[ $field ] . '`'; 
  999.  
  1000. if ( !empty( $attributes[ 'real_name' ] ) ) 
  1001. $filterfield = $attributes[ 'real_name' ]; 
  1002.  
  1003. if ( 'pick' == $attributes[ 'type' ] ) { 
  1004. $filter_value = pods_v( 'filter_' . $field, 'get' ); 
  1005.  
  1006. if ( !is_array( $filter_value ) ) 
  1007. $filter_value = (array) $filter_value; 
  1008.  
  1009. foreach ( $filter_value as $filter_v ) { 
  1010. if ( in_array( pods_v( 'pick_object', $attributes ), $simple_tableless_objects ) ) { 
  1011. if ( strlen( $filter_v ) < 1 ) 
  1012. continue; 
  1013.  
  1014. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) { 
  1015. $having[] = "( {$filterfield} = '" . pods_sanitize( $filter_v ) . "'" 
  1016. . " OR {$filterfield} LIKE '%\"" . pods_sanitize_like( $filter_v ) . "\"%' )"; 
  1017. else { 
  1018. $where[] = "( {$filterfield} = '" . pods_sanitize( $filter_v ) . "'" 
  1019. . " OR {$filterfield} LIKE '%\"" . pods_sanitize_like( $filter_v ) . "\"%' )"; 
  1020. else { 
  1021. $filter_v = (int) $filter_v; 
  1022.  
  1023. if ( empty( $filter_v ) || empty( $attributes[ 'table_info' ] ) || empty( $attributes[ 'table_info' ][ 'field_id' ] ) ) 
  1024. continue; 
  1025.  
  1026. $filterfield = '`' . $field . '`.`' . $attributes[ 'table_info' ][ 'field_id' ] . '`'; 
  1027.  
  1028. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) 
  1029. $having[] = "{$filterfield} = " . $filter_v; 
  1030. else 
  1031. $where[] = "{$filterfield} = " . $filter_v; 
  1032. elseif ( in_array( $attributes[ 'type' ], array( 'date', 'datetime' ) ) ) { 
  1033. $start_value = pods_v( 'filter_' . $field . '_start', 'get', false ); 
  1034. $end_value = pods_v( 'filter_' . $field . '_end', 'get', false ); 
  1035.  
  1036. if ( empty( $start_value ) && empty( $end_value ) ) 
  1037. continue; 
  1038.  
  1039. if ( !empty( $start_value ) ) 
  1040. $start = date_i18n( 'Y-m-d', strtotime( $start_value ) ) . ( 'datetime' == $attributes[ 'type' ] ? ' 00:00:00' : '' ); 
  1041. else 
  1042. $start = date_i18n( 'Y-m-d' ) . ( 'datetime' == $attributes[ 'type' ] ) ? ' 00:00:00' : ''; 
  1043.  
  1044. if ( !empty( $end_value ) ) 
  1045. $end = date_i18n( 'Y-m-d', strtotime( $end_value ) ) . ( 'datetime' == $attributes[ 'type' ] ? ' 23:59:59' : '' ); 
  1046. else 
  1047. $end = date_i18n( 'Y-m-d' ) . ( 'datetime' == $attributes[ 'type' ] ) ? ' 23:59:59' : ''; 
  1048.  
  1049. if ( isset( $attributes[ 'date_ongoing' ] ) && true === $attributes[ 'date_ongoing' ] ) { 
  1050. $date_ongoing = '`' . $attributes[ 'date_ongoing' ] . '`'; 
  1051.  
  1052. if ( isset( $this->aliases[ $date_ongoing ] ) ) 
  1053. $date_ongoing = '`' . $this->aliases[ $date_ongoing ] . '`'; 
  1054.  
  1055. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) 
  1056. $having[] = "(({$filterfield} <= '$start' OR ({$filterfield} >= '$start' AND {$filterfield} <= '$end')) AND ({$date_ongoing} >= '$start' OR ({$date_ongoing} >= '$start' AND {$date_ongoing} <= '$end')))"; 
  1057. else 
  1058. $where[] = "(({$filterfield} <= '$start' OR ({$filterfield} >= '$start' AND {$filterfield} <= '$end')) AND ({$date_ongoing} >= '$start' OR ({$date_ongoing} >= '$start' AND {$date_ongoing} <= '$end')))"; 
  1059. else { 
  1060. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) 
  1061. $having[] = "({$filterfield} BETWEEN '$start' AND '$end')"; 
  1062. else 
  1063. $where[] = "({$filterfield} BETWEEN '$start' AND '$end')"; 
  1064. else { 
  1065. $filter_value = pods_v( 'filter_' . $field, 'get', '' ); 
  1066.  
  1067. if ( strlen( $filter_value ) < 1 ) 
  1068. continue; 
  1069.  
  1070. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) 
  1071. $having[] = "{$filterfield} LIKE '%" . pods_sanitize_like( $filter_value ) . "%'"; 
  1072. else 
  1073. $where[] = "{$filterfield} LIKE '%" . pods_sanitize_like( $filter_value ) . "%'"; 
  1074.  
  1075. if ( !empty( $where ) ) 
  1076. $params->where[] = implode( ' AND ', $where ); 
  1077.  
  1078. if ( !empty( $having ) ) 
  1079. $params->having[] = implode( ' AND ', $having ); 
  1080.  
  1081. // Traverse the Rabbit Hole 
  1082. if ( !empty( $this->pod ) ) { 
  1083. $haystack = implode( ' ', (array) $params->select ) 
  1084. . ' ' . implode( ' ', (array) $params->where ) 
  1085. . ' ' . implode( ' ', (array) $params->groupby ) 
  1086. . ' ' . implode( ' ', (array) $params->having ) 
  1087. . ' ' . implode( ' ', (array) $params->orderby ); 
  1088. $haystack = preg_replace( '/\s/', ' ', $haystack ); 
  1089. $haystack = preg_replace( '/\w\(/', ' ', $haystack ); 
  1090. $haystack = str_replace( array( '(', ')', ' ', '\\\'', "\\\"" ), ' ', $haystack ); 
  1091.  
  1092. preg_match_all( '/`?[\w]+`?(?:\\.`?[\w]+`?)+(?=[^"\']*(?:"[^"]*"[^"]*|\'[^\']*\'[^\']*)*$)/', $haystack, $found, PREG_PATTERN_ORDER ); 
  1093.  
  1094. $found = (array) @current( $found ); 
  1095. $find = $replace = $traverse = array(); 
  1096.  
  1097. foreach ( $found as $key => $value ) { 
  1098. $value = str_replace( '`', '', $value ); 
  1099. $value = explode( '.', $value ); 
  1100. $dot = $last_value = array_pop( $value ); 
  1101.  
  1102. if ( 't' == $value[ 0 ] ) 
  1103. continue; 
  1104. elseif ( array_key_exists( $value[ 0 ], $params->join ) ) 
  1105. // Don't traverse for tables we are already going to join 
  1106. continue; 
  1107. elseif ( 1 == count( $value ) && '' == preg_replace( '/[0-9]*/', '', $value[ 0 ] ) && '' == preg_replace( '/[0-9]*/', '', $last_value ) ) 
  1108. continue; 
  1109.  
  1110. $found_value = str_replace( '`', '', $found[ $key ] ); 
  1111. $found_value = '([`]{1}|\b)' . str_replace( '.', '[`]*\.[`]*', $found_value ) . '([`]{1}|\b)'; 
  1112. $found_value = '/' . $found_value . '(?=[^"\']*(?:"[^"]*"[^"]*|\'[^\']*\'[^\']*)*$)/'; 
  1113.  
  1114. if ( in_array( $found_value, $find ) ) { 
  1115. continue; 
  1116.  
  1117. $find[ $key ] = $found_value; 
  1118.  
  1119. if ( '*' != $dot ) 
  1120. $dot = '`' . $dot . '`'; 
  1121.  
  1122. $replace[ $key ] = '`' . implode( '_', $value ) . '`.' . $dot; 
  1123.  
  1124. $value[] = $last_value; 
  1125.  
  1126. if ( !in_array( $value, $traverse ) ) 
  1127. $traverse[ $key ] = $value; 
  1128.  
  1129. if ( !empty( $this->traverse ) ) { 
  1130. foreach ( (array) $this->traverse as $key => $traverse ) { 
  1131. $traverse = str_replace( '`', '', $traverse ); 
  1132. $already_found = false; 
  1133.  
  1134. foreach ( $traverse as $traversal ) { 
  1135. if ( is_array( $traversal ) ) 
  1136. $traversal = implode( '.', $traversal ); 
  1137.  
  1138. if ( $traversal == $traverse ) { 
  1139. $already_found = true; 
  1140. break; 
  1141.  
  1142. if ( !$already_found ) 
  1143. $traverse[ 'traverse_' . $key ] = explode( '.', $traverse ); 
  1144.  
  1145. $joins = array(); 
  1146.  
  1147. if ( !empty( $find ) ) { 
  1148. $params->select = preg_replace( $find, $replace, $params->select ); 
  1149. $params->where = preg_replace( $find, $replace, $params->where ); 
  1150. $params->groupby = preg_replace( $find, $replace, $params->groupby ); 
  1151. $params->having = preg_replace( $find, $replace, $params->having ); 
  1152. $params->orderby = preg_replace( $find, $replace, $params->orderby ); 
  1153.  
  1154. if ( !empty( $traverse ) ) 
  1155. $joins = $this->traverse( $traverse, $params->fields, $params ); 
  1156. elseif ( false !== $params->search ) 
  1157. $joins = $this->traverse( null, $params->fields, $params ); 
  1158.  
  1159. // Traversal Search 
  1160. if ( !empty( $params->search ) && !empty( $this->search_where ) ) 
  1161. $params->where = array_merge( (array) $this->search_where, $params->where ); 
  1162.  
  1163. if ( !empty( $params->join ) && !empty( $joins ) ) 
  1164. $params->join = array_merge( $joins, (array) $params->join ); 
  1165. elseif ( !empty( $joins ) ) 
  1166. $params->join = $joins; 
  1167.  
  1168. // Build 
  1169. if ( null === $params->sql ) { 
  1170. $sql = " 
  1171. SELECT 
  1172. " . ( $params->calc_rows ? 'SQL_CALC_FOUND_ROWS' : '' ) . " 
  1173. " . ( $params->distinct ? 'DISTINCT' : '' ) . " 
  1174. " . ( !empty( $params->select ) ? ( is_array( $params->select ) ? implode( ', ', $params->select ) : $params->select ) : '*' ) . " 
  1175. FROM {$params->table} AS `t` 
  1176. " . ( !empty( $params->join ) ? ( is_array( $params->join ) ? implode( "\n ", $params->join ) : $params->join ) : '' ) . " 
  1177. " . ( !empty( $params->where ) ? 'WHERE ' . ( is_array( $params->where ) ? implode( ' AND ', $params->where ) : $params->where ) : '' ) . " 
  1178. " . ( !empty( $params->groupby ) ? 'GROUP BY ' . ( is_array( $params->groupby ) ? implode( ', ', $params->groupby ) : $params->groupby ) : '' ) . " 
  1179. " . ( !empty( $params->having ) ? 'HAVING ' . ( is_array( $params->having ) ? implode( ' AND ', $params->having ) : $params->having ) : '' ) . " 
  1180. " . ( !empty( $params->orderby ) ? 'ORDER BY ' . ( is_array( $params->orderby ) ? implode( ', ', $params->orderby ) : $params->orderby ) : '' ) . " 
  1181. " . ( ( 0 < $params->page && 0 < $params->limit ) ? 'LIMIT ' . $params->offset . ', ' . ( $params->limit ) : '' ) . " 
  1182. "; 
  1183.  
  1184. if ( !$params->calc_rows ) { 
  1185. $this->total_sql = " 
  1186. SELECT 
  1187. COUNT( " . ( $params->distinct ? 'DISTINCT `t`.`' . $params->id . '`' : '*' ) . " ) 
  1188. FROM {$params->table} AS `t` 
  1189. " . ( !empty( $params->join ) ? ( is_array( $params->join ) ? implode( "\n ", $params->join ) : $params->join ) : '' ) . " 
  1190. " . ( !empty( $params->where ) ? 'WHERE ' . ( is_array( $params->where ) ? implode( ' AND ', $params->where ) : $params->where ) : '' ) . " 
  1191. " . ( !empty( $params->groupby ) ? 'GROUP BY ' . ( is_array( $params->groupby ) ? implode( ', ', $params->groupby ) : $params->groupby ) : '' ) . " 
  1192. " . ( !empty( $params->having ) ? 'HAVING ' . ( is_array( $params->having ) ? implode( ' AND ', $params->having ) : $params->having ) : '' ) . " 
  1193. "; 
  1194. // Rewrite 
  1195. else { 
  1196. $sql = ' ' . trim( str_replace( array( "\n", "\r" ), ' ', $params->sql ) ); 
  1197. $sql = preg_replace( array( 
  1198. '/\sSELECT\sSQL_CALC_FOUND_ROWS\s/i',  
  1199. '/\sSELECT\s/i' 
  1200. ),  
  1201. array( 
  1202. ' SELECT ',  
  1203. ' SELECT SQL_CALC_FOUND_ROWS ' 
  1204. ),  
  1205. $sql ); 
  1206.  
  1207. // Insert variables based on existing statements 
  1208. if ( false === stripos( $sql, '%%SELECT%%' ) ) 
  1209. $sql = preg_replace( '/\sSELECT\sSQL_CALC_FOUND_ROWS\s/i', ' SELECT SQL_CALC_FOUND_ROWS %%SELECT%% ', $sql ); 
  1210. if ( false === stripos( $sql, '%%WHERE%%' ) ) 
  1211. $sql = preg_replace( '/\sWHERE\s(?!.*\sWHERE\s)/i', ' WHERE %%WHERE%% ', $sql ); 
  1212. if ( false === stripos( $sql, '%%GROUPBY%%' ) ) 
  1213. $sql = preg_replace( '/\sGROUP BY\s(?!.*\sGROUP BY\s)/i', ' GROUP BY %%GROUPBY%% ', $sql ); 
  1214. if ( false === stripos( $sql, '%%HAVING%%' ) ) 
  1215. $sql = preg_replace( '/\sHAVING\s(?!.*\sHAVING\s)/i', ' HAVING %%HAVING%% ', $sql ); 
  1216. if ( false === stripos( $sql, '%%ORDERBY%%' ) ) 
  1217. $sql = preg_replace( '/\sORDER BY\s(?!.*\sORDER BY\s)/i', ' ORDER BY %%ORDERBY%% ', $sql ); 
  1218.  
  1219. // Insert variables based on other existing statements 
  1220. if ( false === stripos( $sql, '%%JOIN%%' ) ) { 
  1221. if ( false !== stripos( $sql, ' WHERE ' ) ) 
  1222. $sql = preg_replace( '/\sWHERE\s(?!.*\sWHERE\s)/i', ' %%JOIN%% WHERE ', $sql ); 
  1223. elseif ( false !== stripos( $sql, ' GROUP BY ' ) ) 
  1224. $sql = preg_replace( '/\sGROUP BY\s(?!.*\sGROUP BY\s)/i', ' %%WHERE%% GROUP BY ', $sql ); 
  1225. elseif ( false !== stripos( $sql, ' ORDER BY ' ) ) 
  1226. $sql = preg_replace( '/\ORDER BY\s(?!.*\ORDER BY\s)/i', ' %%WHERE%% ORDER BY ', $sql ); 
  1227. else 
  1228. $sql .= ' %%JOIN%% '; 
  1229. if ( false === stripos( $sql, '%%WHERE%%' ) ) { 
  1230. if ( false !== stripos( $sql, ' GROUP BY ' ) ) 
  1231. $sql = preg_replace( '/\sGROUP BY\s(?!.*\sGROUP BY\s)/i', ' %%WHERE%% GROUP BY ', $sql ); 
  1232. elseif ( false !== stripos( $sql, ' ORDER BY ' ) ) 
  1233. $sql = preg_replace( '/\ORDER BY\s(?!.*\ORDER BY\s)/i', ' %%WHERE%% ORDER BY ', $sql ); 
  1234. else 
  1235. $sql .= ' %%WHERE%% '; 
  1236. if ( false === stripos( $sql, '%%GROUPBY%%' ) ) { 
  1237. if ( false !== stripos( $sql, ' HAVING ' ) ) 
  1238. $sql = preg_replace( '/\sHAVING\s(?!.*\sHAVING\s)/i', ' %%GROUPBY%% HAVING ', $sql ); 
  1239. elseif ( false !== stripos( $sql, ' ORDER BY ' ) ) 
  1240. $sql = preg_replace( '/\ORDER BY\s(?!.*\ORDER BY\s)/i', ' %%GROUPBY%% ORDER BY ', $sql ); 
  1241. else 
  1242. $sql .= ' %%GROUPBY%% '; 
  1243. if ( false === stripos( $sql, '%%HAVING%%' ) ) { 
  1244. if ( false !== stripos( $sql, ' ORDER BY ' ) ) 
  1245. $sql = preg_replace( '/\ORDER BY\s(?!.*\ORDER BY\s)/i', ' %%HAVING%% ORDER BY ', $sql ); 
  1246. else 
  1247. $sql .= ' %%HAVING%% '; 
  1248. if ( false === stripos( $sql, '%%ORDERBY%%' ) ) 
  1249. $sql .= ' %%ORDERBY%% '; 
  1250. if ( false === stripos( $sql, '%%LIMIT%%' ) ) 
  1251. $sql .= ' %%LIMIT%% '; 
  1252.  
  1253. // Replace variables 
  1254. if ( 0 < strlen( $params->select ) ) { 
  1255. if ( false === stripos( $sql, '%%SELECT%% FROM ' ) ) 
  1256. $sql = str_ireplace( '%%SELECT%%', $params->select . ', ', $sql ); 
  1257. else 
  1258. $sql = str_ireplace( '%%SELECT%%', $params->select, $sql ); 
  1259. if ( 0 < strlen( $params->join ) ) 
  1260. $sql = str_ireplace( '%%JOIN%%', $params->join, $sql ); 
  1261. if ( 0 < strlen( $params->where ) ) { 
  1262. if ( false !== stripos( $sql, ' WHERE ' ) ) { 
  1263. if ( false !== stripos( $sql, ' WHERE %%WHERE%% ' ) ) 
  1264. $sql = str_ireplace( '%%WHERE%%', $params->where . ' AND ', $sql ); 
  1265. else 
  1266. $sql = str_ireplace( '%%WHERE%%', ' AND ' . $params->where, $sql ); 
  1267. else 
  1268. $sql = str_ireplace( '%%WHERE%%', ' WHERE ' . $params->where, $sql ); 
  1269. if ( 0 < strlen( $params->groupby ) ) { 
  1270. if ( false !== stripos( $sql, ' GROUP BY ' ) ) { 
  1271. if ( false !== stripos( $sql, ' GROUP BY %%GROUPBY%% ' ) ) 
  1272. $sql = str_ireplace( '%%GROUPBY%%', $params->groupby . ', ', $sql ); 
  1273. else 
  1274. $sql = str_ireplace( '%%GROUPBY%%', ', ' . $params->groupby, $sql ); 
  1275. else 
  1276. $sql = str_ireplace( '%%GROUPBY%%', ' GROUP BY ' . $params->groupby, $sql ); 
  1277. if ( 0 < strlen( $params->having ) && false !== stripos( $sql, ' GROUP BY ' ) ) { 
  1278. if ( false !== stripos( $sql, ' HAVING ' ) ) { 
  1279. if ( false !== stripos( $sql, ' HAVING %%HAVING%% ' ) ) 
  1280. $sql = str_ireplace( '%%HAVING%%', $params->having . ' AND ', $sql ); 
  1281. else 
  1282. $sql = str_ireplace( '%%HAVING%%', ' AND ' . $params->having, $sql ); 
  1283. else 
  1284. $sql = str_ireplace( '%%HAVING%%', ' HAVING ' . $params->having, $sql ); 
  1285. if ( 0 < strlen( $params->orderby ) ) { 
  1286. if ( false !== stripos( $sql, ' ORDER BY ' ) ) { 
  1287. if ( false !== stripos( $sql, ' ORDER BY %%ORDERBY%% ' ) ) 
  1288. $sql = str_ireplace( '%%ORDERBY%%', $params->groupby . ', ', $sql ); 
  1289. else 
  1290. $sql = str_ireplace( '%%ORDERBY%%', ', ' . $params->groupby, $sql ); 
  1291. else 
  1292. $sql = str_ireplace( '%%ORDERBY%%', ' ORDER BY ' . $params->groupby, $sql ); 
  1293. if ( 0 < $params->page && 0 < $params->limit ) { 
  1294. $start = ( $params->page - 1 ) * $params->limit; 
  1295. $end = $start + $params->limit; 
  1296. $sql .= 'LIMIT ' . (int) $start . ', ' . (int) $end; 
  1297.  
  1298. // Clear any unused variables 
  1299. $sql = str_ireplace( array( 
  1300. '%%SELECT%%',  
  1301. '%%JOIN%%',  
  1302. '%%WHERE%%',  
  1303. '%%GROUPBY%%',  
  1304. '%%HAVING%%',  
  1305. '%%ORDERBY%%',  
  1306. '%%LIMIT%%' 
  1307. ), '', $sql ); 
  1308. $sql = str_replace( array( '``', '`' ), array( ' ', ' ' ), $sql ); 
  1309.  
  1310. return $sql; 
  1311.  
  1312. /** 
  1313. * Fetch the total row count returned 
  1314. * @return int Number of rows returned by select() 
  1315. * @since 2.0 
  1316. */ 
  1317. public function total () { 
  1318. return (int) $this->total; 
  1319.  
  1320. /** 
  1321. * Fetch the total row count total 
  1322. * @return int Number of rows found by select() 
  1323. * @since 2.0 
  1324. */ 
  1325. public function total_found () { 
  1326. if(false === $this->total_found_calculated) 
  1327. $this->calculate_totals(); 
  1328.  
  1329. return (int) $this->total_found; 
  1330.  
  1331. /** 
  1332. * Fetch the zebra state 
  1333. * @return bool Zebra state 
  1334. * @since 1.12 
  1335. * @see PodsData::nth 
  1336. */ 
  1337. public function zebra () { 
  1338. return $this->nth( 'odd' ); // Odd numbers 
  1339.  
  1340. /** 
  1341. * Fetch the nth state 
  1342. * @param int|string $nth The $nth to match on the PodsData::row_number 
  1343. * @return bool Whether $nth matches 
  1344. * @since 2.3 
  1345. */ 
  1346. public function nth ( $nth ) { 
  1347. if ( empty( $nth ) ) 
  1348. $nth = 2; 
  1349.  
  1350. $offset = 0; 
  1351. $negative = false; 
  1352.  
  1353. if ( 'even' == $nth ) 
  1354. $nth = 2; 
  1355. elseif ( 'odd' == $nth ) { 
  1356. $negative = true; 
  1357. $nth = 2; 
  1358. elseif ( false !== strpos( $nth, '+' ) ) { 
  1359. $nth = explode( '+', $nth ); 
  1360.  
  1361. if ( isset( $nth[ 1 ] ) ) 
  1362. $offset += (int) trim( $nth[ 1 ] ); 
  1363.  
  1364. $nth = (int) trim( $nth[ 0 ], ' n' ); 
  1365. elseif ( false !== strpos( $nth, '-' ) ) { 
  1366. $nth = explode( '-', $nth ); 
  1367.  
  1368. if ( isset( $nth[ 1 ] ) ) 
  1369. $offset -= (int) trim( $nth[ 1 ] ); 
  1370.  
  1371. $nth = (int) trim( $nth[ 0 ], ' n' ); 
  1372.  
  1373. $nth = (int) $nth; 
  1374. $offset = (int) $offset; 
  1375.  
  1376. if ( 0 == ( ( $this->row_number % $nth ) + $offset ) ) 
  1377. return ( $negative ? false: true ); 
  1378.  
  1379. return ( $negative ? true : false ); 
  1380.  
  1381. /** 
  1382. * Fetch the current position in the loop (starting at 1) 
  1383. * @return int Current row number (+1) 
  1384. * @since 2.3 
  1385. */ 
  1386. public function position () { 
  1387. return $this->row_number + 1; 
  1388.  
  1389. /** 
  1390. * Create a Table 
  1391. * @param string $table Table name 
  1392. * @param string $fields 
  1393. * @param boolean $if_not_exists Check if the table exists. 
  1394. * @return array|bool|mixed|null|void 
  1395. * @uses PodsData::query 
  1396. * @since 2.0 
  1397. */ 
  1398. public static function table_create ( $table, $fields, $if_not_exists = false ) { 
  1399. /** 
  1400. * @var $wpdb wpdb 
  1401. */ 
  1402. global $wpdb; 
  1403.  
  1404. $sql = "CREATE TABLE"; 
  1405.  
  1406. if ( true === $if_not_exists ) 
  1407. $sql .= " IF NOT EXISTS"; 
  1408.  
  1409. $sql .= " `{$wpdb->prefix}" . self::$prefix . "{$table}` ({$fields})"; 
  1410.  
  1411. if ( !empty( $wpdb->charset ) ) 
  1412. $sql .= " DEFAULT CHARACTER SET {$wpdb->charset}"; 
  1413.  
  1414. if ( !empty( $wpdb->collate ) ) 
  1415. $sql .= " COLLATE {$wpdb->collate}"; 
  1416.  
  1417. return self::query( $sql ); 
  1418.  
  1419. /** 
  1420. * Alter a Table 
  1421. * @param string $table Table name 
  1422. * @param string $changes 
  1423. * @return array|bool|mixed|null|void 
  1424. * @uses PodsData::query 
  1425. * @since 2.0 
  1426. */ 
  1427. public static function table_alter ( $table, $changes ) { 
  1428. /** 
  1429. * @var $wpdb wpdb 
  1430. */ 
  1431. global $wpdb; 
  1432.  
  1433. $sql = "ALTER TABLE `{$wpdb->prefix}" . self::$prefix . "{$table}` {$changes}"; 
  1434.  
  1435. return self::query( $sql ); 
  1436.  
  1437. /** 
  1438. * Truncate a Table 
  1439. * @param string $table Table name 
  1440. * @return array|bool|mixed|null|void 
  1441. * @uses PodsData::query 
  1442. * @since 2.0 
  1443. */ 
  1444. public static function table_truncate ( $table ) { 
  1445. /** 
  1446. * @var $wpdb wpdb 
  1447. */ 
  1448. global $wpdb; 
  1449.  
  1450. $sql = "TRUNCATE TABLE `{$wpdb->prefix}" . self::$prefix . "{$table}`"; 
  1451.  
  1452. return self::query( $sql ); 
  1453.  
  1454. /** 
  1455. * Drop a Table 
  1456. * @param string $table Table name 
  1457. * @uses PodsData::query 
  1458. * @return array|bool|mixed|null|void 
  1459. * @uses PodsData::query 
  1460. * @since 2.0 
  1461. */ 
  1462. public static function table_drop ( $table ) { 
  1463. /** 
  1464. * @var $wpdb wpdb 
  1465. */ 
  1466. global $wpdb; 
  1467.  
  1468. $sql = "DROP TABLE `{$wpdb->prefix}" . self::$prefix . "{$table}`"; 
  1469.  
  1470. return self::query( $sql ); 
  1471.  
  1472. /** 
  1473. * Reorder Items 
  1474. * @param string $table Table name 
  1475. * @param string $weight_field 
  1476. * @param string $id_field 
  1477. * @param array $ids 
  1478. * @return bool 
  1479. * @uses PodsData::update 
  1480. * @since 2.0 
  1481. */ 
  1482. public function reorder ( $table, $weight_field, $id_field, $ids ) { 
  1483. $success = false; 
  1484. $ids = (array) $ids; 
  1485.  
  1486. list( $table, $weight_field, $id_field, $ids ) = $this->do_hook( 'reorder', array( 
  1487. $table,  
  1488. $weight_field,  
  1489. $id_field,  
  1490. $ids 
  1491. ) ); 
  1492.  
  1493. if ( !empty( $ids ) ) { 
  1494. $success = true; 
  1495.  
  1496. foreach ( $ids as $weight => $id ) { 
  1497. $updated = $this->update( $table, array( $weight_field => $weight ), array( $id_field => $id ), array( '%d' ), array( '%d' ) ); 
  1498.  
  1499. if ( false === $updated ) 
  1500. $success = false; 
  1501.  
  1502. return $success; 
  1503.  
  1504. /** 
  1505. * Fetch a new row for the current pod_data 
  1506. * @param int $row Row number to fetch 
  1507. * @param bool $explicit_set Whether to set explicitly (use false when in loop) 
  1508. * @return mixed 
  1509. * @since 2.0 
  1510. */ 
  1511. public function fetch ( $row = null, $explicit_set = true ) { 
  1512. global $wpdb; 
  1513.  
  1514. if ( null === $row ) 
  1515. $explicit_set = false; 
  1516.  
  1517. $already_cached = false; 
  1518. $id = $row; 
  1519.  
  1520. $tableless_field_types = PodsForm::tableless_field_types(); 
  1521.  
  1522. if ( null === $row ) { 
  1523. $this->row_number++; 
  1524.  
  1525. $this->row = false; 
  1526.  
  1527. if ( isset( $this->data[ $this->row_number ] ) ) { 
  1528. $this->row = get_object_vars( $this->data[ $this->row_number ] ); 
  1529.  
  1530. $current_row_id = false; 
  1531.  
  1532. if ( in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) ) 
  1533. $current_row_id = pods_var_raw( 'ID', $this->row ); 
  1534. elseif ( 'taxonomy' == $this->pod_data[ 'type' ] ) 
  1535. $current_row_id = pods_var_raw( 'term_id', $this->row ); 
  1536. elseif ( 'user' == $this->pod_data[ 'type' ] ) 
  1537. $current_row_id = pods_var_raw( 'ID', $this->row ); 
  1538. elseif ( 'comment' == $this->pod_data[ 'type' ] ) 
  1539. $current_row_id = pods_var_raw( 'comment_ID', $this->row ); 
  1540. elseif ( 'settings' == $this->pod_data[ 'type' ] ) 
  1541. $current_row_id = $this->pod_data[ 'id' ]; 
  1542.  
  1543. if ( 0 < $current_row_id ) 
  1544. $row = $current_row_id; 
  1545.  
  1546. if ( null !== $row || 'settings' == $this->pod_data[ 'type' ] ) { 
  1547. if ( $explicit_set ) 
  1548. $this->row_number = -1; 
  1549.  
  1550. $mode = 'id'; 
  1551. $id = pods_absint( $row ); 
  1552.  
  1553. if ( !is_numeric( $row ) || 0 === strpos( $row, '0' ) || $row != preg_replace( '/[^0-9]/', '', $row ) ) { 
  1554. $mode = 'slug'; 
  1555. $id = $row; 
  1556.  
  1557. $row = false; 
  1558.  
  1559. if ( !empty( $this->pod ) ) { 
  1560. $row = pods_cache_get( $id, 'pods_items_' . $this->pod ); 
  1561. if ( false !== $row ) { 
  1562. $already_cached = true; 
  1563.  
  1564. $current_row_id = false; 
  1565. $get_table_data = false; 
  1566.  
  1567. $old_row = $this->row; 
  1568.  
  1569. if ( false !== $row && is_array( $row ) ) 
  1570. $this->row = $row; 
  1571. elseif ( in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) ) { 
  1572. if ( 'post_type' == $this->pod_data[ 'type' ] ) { 
  1573. if ( empty( $this->pod_data[ 'object' ] ) ) { 
  1574. $post_type = $this->pod_data[ 'name' ]; 
  1575. else { 
  1576. $post_type = $this->pod_data[ 'object' ]; 
  1577. else 
  1578. $post_type = 'attachment'; 
  1579.  
  1580. if ( 'id' == $mode ) { 
  1581. $this->row = get_post( $id, ARRAY_A ); 
  1582.  
  1583. if ( is_array( $this->row ) && $this->row[ 'post_type' ] != $post_type ) 
  1584. $this->row = false; 
  1585. else { 
  1586. $args = array( 
  1587. 'post_type' => $post_type,  
  1588. 'name' => $id,  
  1589. 'numberposts' => 5 
  1590. ); 
  1591.  
  1592. $find = get_posts( $args ); 
  1593.  
  1594. if ( !empty( $find ) ) 
  1595. $this->row = get_object_vars( $find[ 0 ] ); 
  1596.  
  1597. if ( is_wp_error( $this->row ) || empty( $this->row ) ) 
  1598. $this->row = false; 
  1599. else 
  1600. $current_row_id = $this->row[ 'ID' ]; 
  1601.  
  1602. $get_table_data = true; 
  1603. elseif ( 'taxonomy' == $this->pod_data[ 'type' ] ) { 
  1604. $taxonomy = $this->pod_data[ 'object' ]; 
  1605.  
  1606. if ( empty( $taxonomy ) ) 
  1607. $taxonomy = $this->pod_data[ 'name' ]; 
  1608.  
  1609. // Taxonomies are registered during init, so they aren't available before then 
  1610. if ( !did_action( 'init' ) ) { 
  1611. // hackaround :( 
  1612. if ( 'id' == $mode ) 
  1613. $term_where = 't.term_id = %d'; 
  1614. else 
  1615. $term_where = 't.slug = %s'; 
  1616.  
  1617. $filter = 'raw'; 
  1618. $term = $id; 
  1619.  
  1620. if ( 'id' != $mode || !$_term = wp_cache_get( $term, $taxonomy ) ) { 
  1621. $_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND {$term_where} LIMIT 1", $taxonomy, $term ) ); 
  1622.  
  1623. if ( $_term ) 
  1624. wp_cache_add( $term, $_term, $taxonomy ); 
  1625.  
  1626. $_term = apply_filters( 'get_term', $_term, $taxonomy ); 
  1627. $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy ); 
  1628. $_term = sanitize_term( $_term, $taxonomy, $filter ); 
  1629.  
  1630. if ( is_object( $_term ) ) 
  1631. $this->row = get_object_vars( $_term ); 
  1632. elseif ( 'id' == $mode ) 
  1633. $this->row = get_term( $id, $taxonomy, ARRAY_A ); 
  1634. else 
  1635. $this->row = get_term_by( 'slug', $id, $taxonomy, ARRAY_A ); 
  1636.  
  1637. if ( is_wp_error( $this->row ) || empty( $this->row ) ) 
  1638. $this->row = false; 
  1639. else 
  1640. $current_row_id = $this->row[ 'term_id' ]; 
  1641.  
  1642. $get_table_data = true; 
  1643. elseif ( 'user' == $this->pod_data[ 'type' ] ) { 
  1644. if ( 'id' == $mode ) 
  1645. $this->row = get_userdata( $id ); 
  1646. else 
  1647. $this->row = get_user_by( 'slug', $id ); 
  1648.  
  1649. if ( is_wp_error( $this->row ) || empty( $this->row ) ) 
  1650. $this->row = false; 
  1651. else { 
  1652. // Get other vars 
  1653. $roles = $this->row->roles; 
  1654. $caps = $this->row->caps; 
  1655. $allcaps = $this->row->allcaps; 
  1656.  
  1657. $this->row = get_object_vars( $this->row->data ); 
  1658.  
  1659. // Set other vars 
  1660. $this->row[ 'roles' ] = $roles; 
  1661. $this->row[ 'caps' ] = $caps; 
  1662. $this->row[ 'allcaps' ] = $allcaps; 
  1663.  
  1664. unset( $this->row[ 'user_pass' ] ); 
  1665.  
  1666. $current_row_id = $this->row[ 'ID' ]; 
  1667.  
  1668. $get_table_data = true; 
  1669. elseif ( 'comment' == $this->pod_data[ 'type' ] ) { 
  1670. $this->row = get_comment( $id, ARRAY_A ); 
  1671.  
  1672. // No slug handling here 
  1673.  
  1674. if ( is_wp_error( $this->row ) || empty( $this->row ) ) 
  1675. $this->row = false; 
  1676. else 
  1677. $current_row_id = $this->row[ 'comment_ID' ]; 
  1678.  
  1679. $get_table_data = true; 
  1680. elseif ( 'settings' == $this->pod_data[ 'type' ] ) { 
  1681. $this->row = array(); 
  1682.  
  1683. if ( empty( $this->fields ) ) 
  1684. $this->row = false; 
  1685. else { 
  1686. foreach ( $this->fields as $field ) { 
  1687. if ( !in_array( $field[ 'type' ], $tableless_field_types ) ) 
  1688. $this->row[ $field[ 'name' ] ] = get_option( $this->pod_data[ 'name' ] . '_' . $field[ 'name' ], null ); 
  1689.  
  1690. // Force ID 
  1691. $this->id = $this->pod_data[ 'id' ]; 
  1692. $this->row[ 'option_id' ] = $this->id; 
  1693. else { 
  1694. $params = array( 
  1695. 'table' => $this->table,  
  1696. 'where' => "`t`.`{$this->field_id}` = " . (int) $id,  
  1697. 'orderby' => "`t`.`{$this->field_id}` DESC",  
  1698. 'page' => 1,  
  1699. 'limit' => 1,  
  1700. 'search' => false 
  1701. ); 
  1702.  
  1703. if ( 'slug' == $mode && !empty( $this->field_slug ) ) { 
  1704. $id = pods_sanitize( $id ); 
  1705. $params[ 'where' ] = "`t`.`{$this->field_slug}` = '{$id}'"; 
  1706.  
  1707. $this->row = pods_data()->select( $params ); 
  1708.  
  1709. if ( empty( $this->row ) ) 
  1710. $this->row = false; 
  1711. else { 
  1712. $current_row = (array) $this->row; 
  1713. $this->row = get_object_vars( (object) @current( $current_row ) ); 
  1714.  
  1715. if ( !$explicit_set && !empty( $this->row ) && is_array( $this->row ) && !empty( $old_row ) ) { 
  1716. $this->row = array_merge( $old_row, $this->row ); 
  1717.  
  1718. if ( 'table' == $this->pod_data[ 'storage' ] && false !== $get_table_data && is_numeric( $current_row_id ) ) { 
  1719. $params = array( 
  1720. 'table' => $wpdb->prefix . "pods_",  
  1721. 'where' => "`t`.`id` = {$current_row_id}",  
  1722. 'orderby' => "`t`.`id` DESC",  
  1723. 'page' => 1,  
  1724. 'limit' => 1,  
  1725. 'search' => false,  
  1726. 'strict' => true 
  1727. ); 
  1728.  
  1729. if ( empty( $this->pod_data[ 'object' ] ) ) 
  1730. $params[ 'table' ] .= $this->pod_data[ 'name' ]; 
  1731. else 
  1732. $params[ 'table' ] .= $this->pod_data[ 'object' ]; 
  1733.  
  1734. $row = pods_data()->select( $params ); 
  1735.  
  1736. if ( !empty( $row ) ) { 
  1737. $current_row = (array) $row; 
  1738. $row = get_object_vars( (object) @current( $current_row ) ); 
  1739.  
  1740. if ( is_array( $this->row ) && !empty( $this->row ) ) 
  1741. $this->row = array_merge( $this->row, $row ); 
  1742. else 
  1743. $this->row = $row; 
  1744.  
  1745. if ( !empty( $this->pod ) && ! $already_cached ) { 
  1746. pods_cache_set( $id, $this->row, 'pods_items_' . $this->pod, 0 ); 
  1747.  
  1748. $this->row = apply_filters( 'pods_data_fetch', $this->row, $id, $this->row_number, $this ); 
  1749.  
  1750. return $this->row; 
  1751.  
  1752. /** 
  1753. * Reset the current data 
  1754. * @param int $row Row number to reset to 
  1755. * @return mixed 
  1756. * @since 2.0 
  1757. */ 
  1758. public function reset ( $row = null ) { 
  1759. $row = pods_absint( $row ); 
  1760.  
  1761. $this->row = false; 
  1762.  
  1763. if ( isset( $this->data[ $row ] ) ) 
  1764. $this->row = get_object_vars( $this->data[ $row ] ); 
  1765.  
  1766. if ( empty( $row ) ) 
  1767. $this->row_number = -1; 
  1768. else 
  1769. $this->row_number = $row - 1; 
  1770.  
  1771. return $this->row; 
  1772.  
  1773. /** 
  1774. * @static 
  1775. * Do a query on the database 
  1776. * @param string|array $sql The SQL to execute 
  1777. * @param string $error Error to throw on problems 
  1778. * @param null $results_error (optional) 
  1779. * @param null $no_results_error (optional) 
  1780. * @return array|bool|mixed|null|void Result of the query 
  1781. * @since 2.0 
  1782. */ 
  1783. public static function query ( $sql, $error = 'Database Error', $results_error = null, $no_results_error = null ) { 
  1784. /** 
  1785. * @var $wpdb wpdb 
  1786. */ 
  1787. global $wpdb; 
  1788.  
  1789. if ( $wpdb->show_errors ) 
  1790. self::$display_errors = true; 
  1791.  
  1792. $display_errors = self::$display_errors; 
  1793.  
  1794. if ( is_object( $error ) ) { 
  1795. if ( isset( $error->display_errors ) && false === $error->display_errors ) 
  1796. $display_errors = false; 
  1797.  
  1798. $error = 'Database Error'; 
  1799. elseif ( is_bool( $error ) ) { 
  1800. $display_errors = $error; 
  1801.  
  1802. if ( false !== $error ) 
  1803. $error = 'Database Error'; 
  1804.  
  1805. $params = (object) array( 
  1806. 'sql' => $sql,  
  1807. 'error' => $error,  
  1808. 'results_error' => $results_error,  
  1809. 'no_results_error' => $no_results_error,  
  1810. 'display_errors' => $display_errors 
  1811. ); 
  1812.  
  1813. // Handle Preparations of Values (sprintf format) 
  1814. if ( is_array( $sql ) ) { 
  1815. if ( isset( $sql[ 0 ] ) && 1 < count( $sql ) ) { 
  1816. if ( 2 == count( $sql ) ) { 
  1817. if ( !is_array( $sql[ 1 ] ) ) 
  1818. $sql[ 1 ] = array( $sql[ 1 ] ); 
  1819.  
  1820. $params->sql = self::prepare( $sql[ 0 ], $sql[ 1 ] ); 
  1821. elseif ( 3 == count( $sql ) ) 
  1822. $params->sql = self::prepare( $sql[ 0 ], array( $sql[ 1 ], $sql[ 2 ] ) ); 
  1823. else 
  1824. $params->sql = self::prepare( $sql[ 0 ], array( $sql[ 1 ], $sql[ 2 ], $sql[ 3 ] ) ); 
  1825. else 
  1826. $params = array_merge( $params, $sql ); 
  1827.  
  1828. if ( 1 == pods_var( 'pods_debug_sql_all', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) 
  1829. echo '<textarea cols="100" rows="24">' . esc_textarea( str_replace( array( '@wp_users', '@wp_' ), array( $wpdb->users, $wpdb->prefix ), $params->sql ) ) . '</textarea>'; 
  1830.  
  1831. $params->sql = trim( $params->sql ); 
  1832.  
  1833. // Run Query 
  1834. $params->sql = apply_filters( 'pods_data_query', $params->sql, $params ); 
  1835.  
  1836. $result = $wpdb->query( $params->sql ); 
  1837.  
  1838. $result = apply_filters( 'pods_data_query_result', $result, $params ); 
  1839.  
  1840. if ( false === $result && !empty( $params->error ) && !empty( $wpdb->last_error ) ) 
  1841. return pods_error( "{$params->error}; SQL: {$params->sql}; Response: {$wpdb->last_error}", $params->display_errors ); 
  1842.  
  1843. if ( 'INSERT' == strtoupper( substr( $params->sql, 0, 6 ) ) || 'REPLACE' == strtoupper( substr( $params->sql, 0, 7 ) ) ) 
  1844. $result = $wpdb->insert_id; 
  1845. elseif ( preg_match( '/^[\s\r\n\(]*SELECT/', strtoupper( $params->sql ) ) ) { 
  1846. $result = (array) $wpdb->last_result; 
  1847.  
  1848. if ( !empty( $result ) && !empty( $params->results_error ) ) 
  1849. return pods_error( $params->results_error, $params->display_errors ); 
  1850. elseif ( empty( $result ) && !empty( $params->no_results_error ) ) 
  1851. return pods_error( $params->no_results_error, $params->display_errors ); 
  1852.  
  1853. return $result; 
  1854.  
  1855. /** 
  1856. * Gets all tables in the WP database, optionally exclude WP core 
  1857. * tables, and/or Pods table by settings the parameters to false. 
  1858. * @param boolean $wp_core 
  1859. * @param boolean $pods_tables restrict Pods 2.x tables 
  1860. * @return array 
  1861. * @since 2.0 
  1862. */ 
  1863. public static function get_tables ( $wp_core = true, $pods_tables = true ) { 
  1864. global $wpdb; 
  1865.  
  1866. $core_wp_tables = array( 
  1867. $wpdb->options,  
  1868. $wpdb->comments,  
  1869. $wpdb->commentmeta,  
  1870. $wpdb->posts,  
  1871. $wpdb->postmeta,  
  1872. $wpdb->users,  
  1873. $wpdb->usermeta,  
  1874. $wpdb->links,  
  1875. $wpdb->terms,  
  1876. $wpdb->term_taxonomy,  
  1877. $wpdb->term_relationships 
  1878. ); 
  1879.  
  1880. $showTables = mysql_list_tables( DB_NAME ); 
  1881.  
  1882. $finalTables = array(); 
  1883.  
  1884. while ( $table = mysql_fetch_row( $showTables ) ) { 
  1885. if ( !$pods_tables && 0 === ( strpos( $table[ 0 ], $wpdb->prefix . rtrim( self::$prefix, '_' ) ) ) ) // don't include pods tables 
  1886. continue; 
  1887. elseif ( !$wp_core && in_array( $table[ 0 ], $core_wp_tables ) ) 
  1888. continue; 
  1889. else 
  1890. $finalTables[] = $table[ 0 ]; 
  1891.  
  1892. return $finalTables; 
  1893.  
  1894. /** 
  1895. * Gets column information from a table 
  1896. * @param string $table Table Name 
  1897. * @return array 
  1898. * @since 2.0 
  1899. */ 
  1900. public static function get_table_columns ( $table ) { 
  1901. global $wpdb; 
  1902.  
  1903. self::query( "SHOW COLUMNS FROM `{$table}` " ); 
  1904.  
  1905. $table_columns = $wpdb->last_result; 
  1906.  
  1907. $table_cols_and_types = array(); 
  1908.  
  1909. foreach ( $table_columns as $table_col ) { 
  1910. // Get only the type, not the attributes 
  1911. if ( false === strpos( $table_col->Type, '(' ) ) 
  1912. $modified_type = $table_col->Type; 
  1913. else 
  1914. $modified_type = substr( $table_col->Type, 0, ( strpos( $table_col->Type, '(' ) ) ); 
  1915.  
  1916. $table_cols_and_types[ $table_col->Field ] = $modified_type; 
  1917.  
  1918. return $table_cols_and_types; 
  1919.  
  1920. /** 
  1921. * Gets column data information from a table 
  1922. * @param string $column_name Column name 
  1923. * @param string $table Table name 
  1924. * @return array 
  1925. * @since 2.0 
  1926. */ 
  1927. public static function get_column_data ( $column_name, $table ) { 
  1928. $describe_data = mysql_query( 'DESCRIBE ' . $table ); 
  1929.  
  1930. $column_data = array(); 
  1931.  
  1932. while ( $column_row = mysql_fetch_assoc( $describe_data ) ) { 
  1933. $column_data[] = $column_row; 
  1934.  
  1935. foreach ( $column_data as $single_column ) { 
  1936. if ( $column_name == $single_column[ 'Field' ] ) 
  1937. return $single_column; 
  1938.  
  1939. return $column_data; 
  1940.  
  1941. /** 
  1942. * Prepare values for the DB 
  1943. * @param string $sql SQL to prepare 
  1944. * @param array $data Data to add to the sql prepare statement 
  1945. * @return bool|null|string 
  1946. * @since 2.0 
  1947. */ 
  1948. public static function prepare ( $sql, $data ) { 
  1949. /** 
  1950. * @var $wpdb wpdb 
  1951. */ 
  1952. global $wpdb; 
  1953. list( $sql, $data ) = apply_filters( 'pods_data_prepare', array( $sql, $data ) ); 
  1954. return $wpdb->prepare( $sql, $data ); 
  1955.  
  1956. /** 
  1957. * Get the string to use in a query for WHERE/HAVING, uses WP_Query meta_query arguments 
  1958. * @param array $fields Array of field matches for querying 
  1959. * @param array $pod Related Pod 
  1960. * @param object $params Parameters passed from select() 
  1961. * @return string|null Query string for WHERE/HAVING 
  1962. * @static 
  1963. * @since 2.3 
  1964. */ 
  1965. public static function query_fields ( $fields, $pod = null, &$params = null ) { 
  1966. $query_fields = array(); 
  1967.  
  1968. if ( !is_object( $params ) ) { 
  1969. $params = new stdClass(); 
  1970.  
  1971. if ( !isset( $params->query_field_level ) || 0 == $params->query_field_level ) { 
  1972. $params->query_fields = array(); 
  1973. $params->query_field_syntax = false; 
  1974. $params->query_field_level = 1; 
  1975.  
  1976. if ( !isset( $params->where_default ) ) { 
  1977. $params->where_default = array(); 
  1978.  
  1979. if ( !isset( $params->where_defaulted ) ) { 
  1980. $params->where_defaulted = false; 
  1981.  
  1982. $current_level = $params->query_field_level; 
  1983.  
  1984. $relation = 'AND'; 
  1985.  
  1986. if ( isset( $fields[ 'relation' ] ) ) { 
  1987. $relation = strtoupper( trim( pods_var( 'relation', $fields, 'AND', null, true ) ) ); 
  1988.  
  1989. if ( 'AND' != $relation ) 
  1990. $relation = 'OR'; 
  1991.  
  1992. unset( $fields[ 'relation' ] ); 
  1993.  
  1994. foreach ( $fields as $field => $match ) { 
  1995. if ( is_array( $match ) && isset( $match[ 'relation' ] ) ) { 
  1996. $params->query_field_level = $current_level + 1; 
  1997.  
  1998. $query_field = self::query_fields( $match, $pod, $params ); 
  1999.  
  2000. $params->query_field_level = $current_level; 
  2001.  
  2002. if ( !empty( $query_field ) ) { 
  2003. $query_fields[] = $query_field; 
  2004. else { 
  2005. $query_field = self::query_field( $field, $match, $pod, $params ); 
  2006.  
  2007. if ( !empty( $query_field ) ) { 
  2008. $query_fields[] = $query_field; 
  2009.  
  2010. if ( !empty( $query_fields ) ) { 
  2011. // If post_status not sent, detect it 
  2012. if ( !empty( $pod ) && 'post_type' == $pod[ 'type' ] && 1 == $current_level && !$params->where_defaulted && !empty( $params->where_default ) ) { 
  2013. $post_status_found = false; 
  2014.  
  2015. if ( !$params->query_field_syntax ) { 
  2016. $haystack = implode( ' ', (array) $query_fields ); 
  2017. $haystack = preg_replace( '/\s/', ' ', $haystack ); 
  2018. $haystack = preg_replace( '/\w\(/', ' ', $haystack ); 
  2019. $haystack = str_replace( array( '(', ')', ' ', '\\\'', "\\\"" ), ' ', $haystack ); 
  2020.  
  2021. preg_match_all( '/`?[\w]+`?(?:\\.`?[\w]+`?)+(?=[^"\']*(?:"[^"]*"[^"]*|\'[^\']*\'[^\']*)*$)/', $haystack, $found, PREG_PATTERN_ORDER ); 
  2022.  
  2023. $found = (array) @current( $found ); 
  2024.  
  2025. foreach ( $found as $value ) { 
  2026. $value = str_replace( '`', '', $value ); 
  2027. $value = explode( '.', $value ); 
  2028.  
  2029. if ( ( 'post_status' == $value[ 0 ] && 1 == count( $value ) ) || ( 2 == count( $value ) && 't' == $value[ 0 ] && 'post_status' == $value[ 1 ] ) ) { 
  2030. $post_status_found = true; 
  2031.  
  2032. break; 
  2033. elseif ( !empty( $params->query_fields ) && in_array( 'post_status', $params->query_fields ) ) { 
  2034. $post_status_found = true; 
  2035.  
  2036. if ( !$post_status_found ) { 
  2037. $query_fields[] = $params->where_default; 
  2038.  
  2039. if ( 1 < count( $query_fields ) ) 
  2040. $query_fields = '( ( ' . implode( ' ) ' . $relation . ' ( ', $query_fields ) . ' ) )'; 
  2041. else 
  2042. $query_fields = '( ' . implode( ' ' . $relation . ' ', $query_fields ) . ' )'; 
  2043. else 
  2044. $query_fields = null; 
  2045.  
  2046. // query_fields level complete 
  2047. if ( 1 == $params->query_field_level ) { 
  2048. $params->query_field_level = 0; 
  2049.  
  2050. return $query_fields; 
  2051.  
  2052. /** 
  2053. * Get the string to use in a query for matching, uses WP_Query meta_query arguments 
  2054. * @param string|int $field Field name or array index 
  2055. * @param array|string $q Query array (meta_query) or string for matching 
  2056. * @param array $pod Related Pod 
  2057. * @param object $params Parameters passed from select() 
  2058. * @return string|null Query field string 
  2059. * @see PodsData::query_fields 
  2060. * @static 
  2061. * @since 2.3 
  2062. */ 
  2063. public static function query_field ( $field, $q, $pod = null, &$params = null ) { 
  2064. global $wpdb; 
  2065.  
  2066. $simple_tableless_objects = PodsForm::simple_tableless_objects(); 
  2067.  
  2068. $field_query = null; 
  2069.  
  2070. // Plain queries 
  2071. if ( is_numeric( $field ) && !is_array( $q ) ) { 
  2072. return $q; 
  2073. // key => value queries (backwards compatibility) 
  2074. elseif ( !is_numeric( $field ) && ( !is_array( $q ) || ( !isset( $q[ 'key' ] ) && !isset( $q[ 'field' ] ) ) ) ) { 
  2075. $new_q = array( 
  2076. 'field' => $field,  
  2077. 'compare' => pods_var_raw( 'compare', $q, '=', null, true ),  
  2078. 'value' => pods_var_raw( 'value', $q, $q, null, true ),  
  2079. 'sanitize' => pods_var_raw( 'sanitize', $q, true ),  
  2080. 'sanitize_format' => pods_var_raw( 'sanitize_format', $q ),  
  2081. 'cast' => pods_var_raw( 'cast', $q ) 
  2082. ); 
  2083.  
  2084. if ( is_array( $new_q[ 'value' ] ) ) { 
  2085. if ( '=' == $new_q[ 'compare' ] ) 
  2086. $new_q[ 'compare' ] = 'IN'; 
  2087.  
  2088. if ( isset( $new_q[ 'value' ][ 'compare' ] ) ) 
  2089. unset( $new_q[ 'value' ][ 'compare' ] ); 
  2090.  
  2091. $q = $new_q; 
  2092.  
  2093. $field_name = trim( pods_var_raw( 'field', $q, pods_var_raw( 'key', $q, $field, null, true ), null, true ) ); 
  2094. $field_type = strtoupper( trim( pods_var_raw( 'type', $q, 'CHAR', null, true ) ) ); 
  2095. $field_value = pods_var_raw( 'value', $q ); 
  2096. $field_compare = strtoupper( trim( pods_var_raw( 'compare', $q, ( is_array( $field_value ? 'IN' : '=' ) ), null, true ) ) ); 
  2097. $field_sanitize = (boolean) pods_var( 'sanitize', $q, true ); 
  2098. $field_sanitize_format = pods_var_raw( 'sanitize_format', $q, null, null, true ); 
  2099. $field_cast = pods_var_raw( 'cast', $q, null, null, true ); 
  2100.  
  2101. if ( is_object( $params ) ) { 
  2102. $params->meta_query_syntax = true; 
  2103. $params->query_fields[] = $field_name; 
  2104.  
  2105. // Deprecated WP type 
  2106. if ( 'NUMERIC' == $field_type ) 
  2107. $field_type = 'SIGNED'; 
  2108. // Restrict to supported types 
  2109. elseif ( !in_array( $field_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) ) 
  2110. $field_type = 'CHAR'; 
  2111.  
  2112. // Alias / Casting 
  2113. if ( empty( $field_cast ) ) { 
  2114. // Setup field casting from field name 
  2115. if ( false === strpos( $field_name, '`' ) && false === strpos( $field_name, '(' ) && false === strpos( $field_name, ' ' ) ) { 
  2116. // Handle field naming if Pod-based 
  2117. if ( !empty( $pod ) && false === strpos( $field_name, '.' ) ) { 
  2118. $field_cast = ''; 
  2119.  
  2120. $tableless_field_types = PodsForm::tableless_field_types(); 
  2121.  
  2122. if ( isset( $pod[ 'fields' ][ $field_name ] ) && in_array( $pod[ 'fields' ][ $field_name ][ 'type' ], $tableless_field_types ) ) { 
  2123. if ( in_array( $pod[ 'fields' ][ $field_name ][ 'pick_object' ], $simple_tableless_objects ) ) { 
  2124. if ( 'table' == $pod[ 'storage' ] ) 
  2125. $field_cast = "`t`.`{$field_name}`"; 
  2126. else 
  2127. $field_cast = "`{$field_name}`.`meta_value`"; 
  2128. else { 
  2129. $table = pods_api()->get_table_info( $pod[ 'fields' ][ $field_name ][ 'pick_object' ], $pod[ 'fields' ][ $field_name ][ 'pick_val' ] ); 
  2130.  
  2131. if ( !empty( $table ) ) 
  2132. $field_cast = "`{$field_name}`.`" . $table[ 'field_index' ] . '`'; 
  2133.  
  2134. if ( empty( $field_cast ) ) { 
  2135. if ( !in_array( $pod[ 'type' ], array( 'pod', 'table' ) ) ) { 
  2136. if ( isset( $pod[ 'object_fields' ][ $field_name ] ) ) 
  2137. $field_cast = "`t`.`{$field_name}`"; 
  2138. elseif ( isset( $pod[ 'fields' ][ $field_name ] ) ) { 
  2139. if ( 'table' == $pod[ 'storage' ] ) 
  2140. $field_cast = "`d`.`{$field_name}`"; 
  2141. else 
  2142. $field_cast = "`{$field_name}`.`meta_value`"; 
  2143. else { 
  2144. foreach ( $pod[ 'object_fields' ] as $object_field => $object_field_opt ) { 
  2145. if ( $object_field == $field_name || in_array( $field_name, $object_field_opt[ 'alias' ] ) ) { 
  2146. $field_cast = "`t`.`{$object_field}`"; 
  2147.  
  2148. break; 
  2149. elseif ( isset( $pod[ 'fields' ][ $field_name ] ) ) { 
  2150. if ( 'table' == $pod[ 'storage' ] ) 
  2151. $field_cast = "`t`.`{$field_name}`"; 
  2152. else 
  2153. $field_cast = "`{$field_name}`.`meta_value`"; 
  2154.  
  2155. if ( empty( $field_cast ) ) { 
  2156. if ( 'table' == $pod[ 'storage' ] ) 
  2157. $field_cast = "`{$field_name}`"; 
  2158. else 
  2159. $field_cast = "`{$field_name}`.`meta_value`"; 
  2160. else { 
  2161. $field_cast = '`' . str_replace( '.', '`.`', $field_name ) . '`'; 
  2162. else { 
  2163. $field_cast = $field_name; 
  2164.  
  2165. // Cast field if needed 
  2166. if ( 'CHAR' != $field_type ) { 
  2167. $field_cast = 'CAST( ' . $field_cast . ' AS ' . $field_type .' )'; 
  2168.  
  2169. // Setup string sanitizing for $wpdb->prepare() 
  2170. if ( empty( $field_sanitize_format ) ) { 
  2171. // Sanitize as string 
  2172. $field_sanitize_format = '%s'; 
  2173.  
  2174. // Sanitize as integer if needed 
  2175. if ( in_array( $field_type, array( 'UNSIGNED', 'SIGNED' ) ) ) 
  2176. $field_sanitize_format = '%d'; 
  2177.  
  2178. // Restrict to supported comparisons 
  2179. if ( !in_array( $field_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS', 'NOT EXISTS', 'REGEXP', 'NOT REGEXP', 'RLIKE' ) ) ) 
  2180. $field_compare = '='; 
  2181.  
  2182. // Restrict to supported array comparisons 
  2183. if ( is_array( $field_value ) && !in_array( $field_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { 
  2184. if ( in_array( $field_compare, array( '!=', 'NOT LIKE' ) ) ) 
  2185. $field_compare = 'NOT IN'; 
  2186. else 
  2187. $field_compare = 'IN'; 
  2188. // Restrict to supported string comparisons 
  2189. elseif ( !is_array( $field_value ) && in_array( $field_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { 
  2190. $check_value = preg_split( '/[, \s]+/', $field_value ); 
  2191.  
  2192. if ( 1 < count( $check_value ) ) 
  2193. $field_value = $check_value; 
  2194. elseif ( in_array( $field_compare, array( 'NOT IN', 'NOT BETWEEN' ) ) ) 
  2195. $field_compare = '!='; 
  2196. else 
  2197. $field_compare = '='; 
  2198.  
  2199. // Restrict to two values, force = and != if only one value provided 
  2200. if ( in_array( $field_compare, array( 'BETWEEN', 'NOT BETWEEN' ) ) ) { 
  2201. $field_value = array_values( array_slice( $field_value, 0, 2 ) ); 
  2202.  
  2203. if ( 1 == count( $field_value ) ) { 
  2204. if ( 'NOT IN' == $field_compare ) 
  2205. $field_compare = '!='; 
  2206. else 
  2207. $field_compare = '='; 
  2208.  
  2209. // Empty array handling 
  2210. if ( empty( $field_value ) && in_array( $field_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { 
  2211. $field_compare = 'EXISTS'; 
  2212.  
  2213. // Rebuild $q 
  2214. $q = array( 
  2215. 'field' => $field_name,  
  2216. 'type' => $field_type,  
  2217. 'value' => $field_value,  
  2218. 'compare' => $field_compare,  
  2219. 'sanitize' => $field_sanitize,  
  2220. 'sanitize_format' => $field_sanitize_format,  
  2221. 'cast' => $field_cast 
  2222. ); 
  2223.  
  2224. // Make the query 
  2225. if ( in_array( $field_compare, array( '=', '!=', '>', '>=', '<', '<=', 'REGEXP', 'NOT REGEXP', 'RLIKE' ) ) ) { 
  2226. if ( $field_sanitize ) { 
  2227. $field_query = $wpdb->prepare( $field_cast . ' ' . $field_compare . ' ' . $field_sanitize_format, $field_value ); 
  2228. else { 
  2229. $field_query = $field_cast . ' ' . $field_compare . ' "' . $field_value . '"'; 
  2230. elseif ( in_array( $field_compare, array( 'LIKE', 'NOT LIKE' ) ) ) { 
  2231. if ( $field_sanitize ) { 
  2232. $field_query = $field_cast . ' ' . $field_compare . ' "%' . pods_sanitize_like( $field_value ) . '%"'; 
  2233. else { 
  2234. $field_query = $field_cast . ' ' . $field_compare . ' "' . $field_value . '"'; 
  2235. elseif ( in_array( $field_compare, array( 'IN', 'NOT IN' ) ) ) { 
  2236. if ( $field_sanitize ) { 
  2237. $field_query = $wpdb->prepare( $field_cast . ' ' . $field_compare . ' ( ' . substr( str_repeat( ', ' . $field_sanitize_format, count( $field_value ) ), 1 ) . ' )', $field_value ); 
  2238. else { 
  2239. $field_query = $field_cast . ' ' . $field_compare . ' ( "' . implode( '", "', $field_value ) . '" )'; 
  2240. elseif ( in_array( $field_compare, array( 'BETWEEN', 'NOT BETWEEN' ) ) ) { 
  2241. if ( $field_sanitize ) { 
  2242. $field_query = $wpdb->prepare( $field_cast . ' ' . $field_compare . ' ' . $field_sanitize_format . ' AND ' . $field_sanitize_format, $field_value ); 
  2243. else { 
  2244. $field_query = $field_cast . ' ' . $field_compare . ' "' . $field_value[ 0 ] . '" AND "' . $field_value[ 1 ] . '"'; 
  2245. elseif ( 'EXISTS' == $field_compare ) 
  2246. $field_query = $field_cast . ' IS NOT NULL'; 
  2247. elseif ( 'NOT EXISTS' == $field_compare ) 
  2248. $field_query = $field_cast . ' IS NULL'; 
  2249.  
  2250. $field_query = apply_filters( 'pods_data_field_query', $field_query, $q ); 
  2251.  
  2252. return $field_query; 
  2253.  
  2254. /** 
  2255. * Setup fields for traversal 
  2256. * @param array $fields Associative array of fields data 
  2257. * @return array Traverse feed 
  2258. * @param object $params (optional) Parameters from build() 
  2259. * @since 2.0 
  2260. */ 
  2261. function traverse_build ( $fields = null, $params = null ) { 
  2262. if ( null === $fields ) 
  2263. $fields = $this->fields; 
  2264.  
  2265. $feed = array(); 
  2266.  
  2267. foreach ( $fields as $field => $data ) { 
  2268. if ( !is_array( $data ) ) 
  2269. $field = $data; 
  2270.  
  2271. if ( !isset( $_GET[ 'filter_' . $field ] ) ) 
  2272. continue; 
  2273.  
  2274. $field_value = pods_var( 'filter_' . $field, 'get', false, null, true ); 
  2275.  
  2276. if ( !empty( $field_value ) || 0 < strlen( $field_value ) ) 
  2277. $feed[ 'traverse_' . $field ] = array( $field ); 
  2278.  
  2279. return $feed; 
  2280.  
  2281. /** 
  2282. * Recursively join tables based on fields 
  2283. * @param array $traverse_recurse Array of traversal options 
  2284. * @return array Array of table joins 
  2285. * @since 2.0 
  2286. */ 
  2287. function traverse_recurse ( $traverse_recurse ) { 
  2288. global $wpdb; 
  2289.  
  2290. $defaults = array( 
  2291. 'pod' => null,  
  2292. 'fields' => array(),  
  2293. 'joined' => 't',  
  2294. 'depth' => 0,  
  2295. 'joined_id' => 'id',  
  2296. 'joined_index' => 'id',  
  2297. 'params' => new stdClass(),  
  2298. 'last_table_info' => array() 
  2299. ); 
  2300.  
  2301. $traverse_recurse = array_merge( $defaults, $traverse_recurse ); 
  2302.  
  2303. $joins = array(); 
  2304.  
  2305. if ( 0 == $traverse_recurse[ 'depth' ] && !empty( $traverse_recurse[ 'pod' ] ) && !empty( $traverse_recurse [ 'last_table_info' ] ) && isset( $traverse_recurse [ 'last_table_info' ][ 'id' ] ) ) 
  2306. $pod_data = $traverse_recurse [ 'last_table_info' ]; 
  2307. elseif ( empty( $traverse_recurse[ 'pod' ] ) ) { 
  2308. if ( !empty( $traverse_recurse[ 'params' ] ) && !empty( $traverse_recurse[ 'params' ]->table ) && 0 === strpos( $traverse_recurse[ 'params' ]->table, $wpdb->prefix ) ) { 
  2309. if ( $wpdb->posts == $traverse_recurse[ 'params' ]->table ) 
  2310. $traverse_recurse[ 'pod' ] = 'post_type'; 
  2311. elseif ( $wpdb->terms == $traverse_recurse[ 'params' ]->table ) 
  2312. $traverse_recurse[ 'pod' ] = 'taxonomy'; 
  2313. elseif ( $wpdb->users == $traverse_recurse[ 'params' ]->table ) 
  2314. $traverse_recurse[ 'pod' ] = 'user'; 
  2315. elseif ( $wpdb->comments == $traverse_recurse[ 'params' ]->table ) 
  2316. $traverse_recurse[ 'pod' ] = 'comment'; 
  2317. else 
  2318. return $joins; 
  2319.  
  2320. $pod_data = array(); 
  2321.  
  2322. if ( in_array( $traverse_recurse[ 'pod' ], array( 'user', 'comment' ) ) ) { 
  2323. $pod = $this->api->load_pod( array( 'name' => $traverse_recurse[ 'pod' ], 'table_info' => true ) ); 
  2324.  
  2325. if ( !empty( $pod ) && $pod[ 'type' ] == $pod ) 
  2326. $pod_data = $pod; 
  2327.  
  2328. if ( empty( $pod_data ) ) { 
  2329. $pod_data = array( 
  2330. 'id' => 0,  
  2331. 'name' => '_table_' . $traverse_recurse[ 'pod' ],  
  2332. 'type' => $traverse_recurse[ 'pod' ],  
  2333. 'storage' => ( 'taxonomy' == $traverse_recurse[ 'pod' ] ? 'none' : 'meta' ),  
  2334. 'fields' => array(),  
  2335. 'object_fields' => $this->api->get_wp_object_fields( $traverse_recurse[ 'pod' ] ) 
  2336. ); 
  2337.  
  2338. $pod_data = array_merge( $this->api->get_table_info( $traverse_recurse[ 'pod' ], '' ), $pod_data ); 
  2339.  
  2340. $traverse_recurse[ 'pod' ] = $pod_data[ 'name' ]; 
  2341. else 
  2342. return $joins; 
  2343. else { 
  2344. $pod_data = $this->api->load_pod( array( 'name' => $traverse_recurse[ 'pod' ], 'table_info' => true ), false ); 
  2345.  
  2346. if ( empty( $pod_data ) ) 
  2347. return $joins; 
  2348.  
  2349. if ( isset( $pod_data[ 'object_fields' ] ) ) { 
  2350. $pod_data[ 'fields' ] = array_merge( $pod_data[ 'fields' ], $pod_data[ 'object_fields' ] ); 
  2351.  
  2352. $tableless_field_types = PodsForm::tableless_field_types(); 
  2353. $simple_tableless_objects = PodsForm::simple_tableless_objects(); 
  2354. $file_field_types = PodsForm::file_field_types(); 
  2355.  
  2356. if ( !isset( $this->traversal[ $traverse_recurse[ 'pod' ] ] ) ) 
  2357. $this->traversal[ $traverse_recurse[ 'pod' ] ] = array(); 
  2358.  
  2359. if ( ( empty( $pod_data[ 'meta_table' ] ) || $pod_data[ 'meta_table' ] == $pod_data[ 'table' ] ) && ( empty( $traverse_recurse[ 'fields' ] ) || empty( $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] ] ) ) ) 
  2360. return $joins; 
  2361.  
  2362. $field = $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] ]; 
  2363.  
  2364. $ignore_aliases = array( 
  2365. 'wpml_languages',  
  2366. 'polylang_languages' 
  2367. ); 
  2368.  
  2369. $ignore_aliases = apply_filters( 'pods_data_traverse_recurse_ignore_aliases', $ignore_aliases, $field, $traverse_recurse, $this ); 
  2370.  
  2371. if ( in_array( $field, $ignore_aliases ) ) 
  2372. return $joins; 
  2373.  
  2374. $meta_data_table = false; 
  2375.  
  2376. if ( !isset( $pod_data[ 'fields' ][ $field ] ) && 'd' == $field && isset( $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] - 1 ] ) ) { 
  2377. $field = $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] - 1 ]; 
  2378.  
  2379. $field_type = 'pick'; 
  2380.  
  2381. if ( isset( $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'fields' ][ $field ] ) ) { 
  2382. $field_type = $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'fields' ][ $field ][ 'type' ]; 
  2383. elseif ( isset( $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'object_fields' ][ $field ] ) ) { 
  2384. $field_type = $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'object_fields' ][ $field ][ 'type' ]; 
  2385.  
  2386. $pod_data[ 'fields' ][ $field ] = array( 
  2387. 'id' => 0,  
  2388. 'name' => $field,  
  2389. 'type' => $field_type,  
  2390. 'pick_object' => $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'type' ],  
  2391. 'pick_val' => $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'name' ] 
  2392. ); 
  2393.  
  2394. $meta_data_table = true; 
  2395.  
  2396. // Fallback to meta table if the pod type supports it 
  2397. if ( !isset( $pod_data[ 'fields' ][ $field ] ) ) { 
  2398. $last = end( $traverse_recurse[ 'fields' ] ); 
  2399.  
  2400. if ( 'post_type' == $pod_data[ 'type' ] && !isset( $pod_data[ 'object_fields' ] ) ) 
  2401. $pod_data[ 'object_fields' ] = $this->api->get_wp_object_fields( 'post_type', $pod_data ); 
  2402.  
  2403. if ( 'post_type' == $pod_data[ 'type' ] && isset( $pod_data[ 'object_fields'][ $field ] ) && in_array( $pod_data[ 'object_fields' ][ $field ][ 'type' ], $tableless_field_types ) ) 
  2404. $pod_data[ 'fields' ][ $field ] = $pod_data[ 'object_fields' ][ $field ]; 
  2405. elseif ( 'meta_value' === $last && in_array( $pod_data[ 'type' ], array( 'post_type', 'media', 'user', 'comment' ) ) ) 
  2406. $pod_data[ 'fields' ][ $field ] = PodsForm::field_setup( array( 'name' => $field ) ); 
  2407. else { 
  2408. if ( 'post_type' == $pod_data[ 'type' ] ) { 
  2409. $pod_data[ 'object_fields' ] = $this->api->get_wp_object_fields( 'post_type', $pod_data, true ); 
  2410.  
  2411. if ( 'post_type' == $pod_data[ 'type' ] && isset( $pod_data[ 'object_fields' ][ $field ] ) && in_array( $pod_data[ 'object_fields' ][ $field ][ 'type' ], $tableless_field_types ) ) 
  2412. $pod_data[ 'fields' ][ $field ] = $pod_data[ 'object_fields' ][ $field ]; 
  2413. else 
  2414. return $joins; 
  2415. else 
  2416. return $joins; 
  2417. } elseif ( isset( $pod_data[ 'object_fields' ] ) && isset( $pod_data[ 'object_fields' ][ $field ] ) && ! in_array( $pod_data[ 'object_fields' ][ $field ][ 'type' ], $tableless_field_types ) ) { 
  2418. return $joins; 
  2419.  
  2420. $traverse = $pod_data[ 'fields' ][ $field ]; 
  2421.  
  2422. if ( 'taxonomy' == $traverse[ 'type' ] ) 
  2423. $traverse[ 'table_info' ] = $this->api->get_table_info( $traverse[ 'type' ], $traverse[ 'name' ] ); 
  2424. elseif ( in_array( $traverse[ 'type' ], $file_field_types ) ) 
  2425. $traverse[ 'table_info' ] = $this->api->get_table_info( 'post_type', 'attachment' ); 
  2426. elseif ( !in_array( $traverse[ 'type' ], $tableless_field_types ) ) 
  2427. $traverse[ 'table_info' ] = $this->api->get_table_info( $pod_data[ 'type' ], $pod_data[ 'name' ], $pod_data[ 'name' ], $pod_data ); 
  2428. elseif ( empty( $traverse[ 'table_info' ] ) || ( in_array( $traverse[ 'pick_object' ], $simple_tableless_objects ) && !empty( $traverse_recurse[ 'last_table_info' ] ) ) ) { 
  2429. if ( in_array( $traverse[ 'pick_object' ], $simple_tableless_objects ) && !empty( $traverse_recurse[ 'last_table_info' ] ) ) { 
  2430. $traverse[ 'table_info' ] = $traverse_recurse[ 'last_table_info' ]; 
  2431.  
  2432. if ( !empty( $traverse[ 'table_info' ][ 'meta_table' ] ) ) 
  2433. $meta_data_table = true; 
  2434. elseif ( !in_array( $traverse[ 'type' ], $tableless_field_types ) && !empty( $traverse_recurse[ 'last_table_info' ] ) && 0 == $traverse_recurse[ 'depth' ] ) 
  2435. $traverse[ 'table_info' ] = $traverse_recurse[ 'last_table_info' ]; 
  2436. else { 
  2437. if ( ! isset( $traverse[ 'pod' ] ) ) { 
  2438. $traverse[ 'pod' ] = null; 
  2439.  
  2440. $traverse[ 'table_info' ] = $this->api->get_table_info( $traverse[ 'pick_object' ], $traverse[ 'pick_val' ], null, $traverse[ 'pod' ], $traverse ); 
  2441.  
  2442. if ( isset( $this->traversal[ $traverse_recurse[ 'pod' ] ][ $traverse[ 'name' ] ] ) ) 
  2443. $traverse = array_merge( $traverse, (array) $this->traversal[ $traverse_recurse[ 'pod' ] ][ $traverse[ 'name' ] ] ); 
  2444.  
  2445. $traverse = apply_filters( 'pods_data_traverse', $traverse, compact( 'pod', 'fields', 'joined', 'depth', 'joined_id', 'params' ), $this ); 
  2446.  
  2447. if ( empty( $traverse ) ) 
  2448. return $joins; 
  2449.  
  2450. $traverse[ 'id' ] = (int) $traverse[ 'id' ]; 
  2451.  
  2452. if ( empty( $traverse[ 'id' ] ) ) 
  2453. $traverse[ 'id' ] = $field; 
  2454.  
  2455. $table_info = $traverse[ 'table_info' ]; 
  2456.  
  2457. $this->traversal[ $traverse_recurse[ 'pod' ] ][ $field ] = $traverse; 
  2458.  
  2459. $field_joined = $field; 
  2460.  
  2461. if ( 0 < $traverse_recurse[ 'depth' ] && 't' != $traverse_recurse[ 'joined' ] ) { 
  2462. if ( $meta_data_table && ( 'pick' != $traverse[ 'type' ] || !in_array( pods_var( 'pick_object', $traverse ), $simple_tableless_objects ) ) ) 
  2463. $field_joined = $traverse_recurse[ 'joined' ] . '_d'; 
  2464. else 
  2465. $field_joined = $traverse_recurse[ 'joined' ] . '_' . $field; 
  2466.  
  2467. $rel_alias = 'rel_' . $field_joined; 
  2468.  
  2469. if ( pods_v( 'search', $traverse_recurse[ 'params' ], false ) && empty( $traverse_recurse[ 'params' ]->filters ) ) { 
  2470. if ( 0 < strlen( pods_var( 'filter_' . $field_joined, 'get' ) ) ) { 
  2471. $val = absint( pods_var( 'filter_' . $field_joined, 'get' ) ); 
  2472.  
  2473. $search = "`{$field_joined}`.`{$table_info[ 'field_id' ]}` = {$val}"; 
  2474.  
  2475. if ( 'text' == $this->search_mode ) { 
  2476. $val = pods_var( 'filter_' . $field_joined, 'get' ); 
  2477.  
  2478. $search = "`{$field_joined}`.`{$traverse[ 'name' ]}` = '{$val}'"; 
  2479. elseif ( 'text_like' == $this->search_mode ) { 
  2480. $val = pods_sanitize( pods_sanitize_like( pods_var_raw( 'filter_' . $field_joined ) ) ); 
  2481.  
  2482. $search = "`{$field_joined}`.`{$traverse[ 'name' ]}` LIKE '%{$val}%'"; 
  2483.  
  2484. $this->search_where[] = " {$search} "; 
  2485.  
  2486. $the_join = null; 
  2487.  
  2488. $joined_id = $table_info[ 'field_id' ]; 
  2489. $joined_index = $table_info[ 'field_index' ]; 
  2490.  
  2491. if ( 'taxonomy' == $traverse[ 'type' ] ) { 
  2492. $rel_tt_alias = 'rel_tt_' . $field_joined; 
  2493.  
  2494. if ( $meta_data_table ) { 
  2495. $the_join = " 
  2496. LEFT JOIN `{$table_info[ 'pod_table' ]}` AS `{$field_joined}` ON 
  2497. `{$field_joined}`.`{$table_info[ 'pod_field_id' ]}` = `{$traverse_recurse[ 'rel_alias' ]}`.`{$traverse_recurse[ 'joined_id' ]}` 
  2498. "; 
  2499. else { 
  2500. $the_join = " 
  2501. LEFT JOIN `{$wpdb->term_relationships}` AS `{$rel_alias}` ON 
  2502. `{$rel_alias}`.`object_id` = `{$traverse_recurse[ 'joined' ]}`.`ID` 
  2503.  
  2504. LEFT JOIN `{$wpdb->term_taxonomy}` AS `{$rel_tt_alias}` ON 
  2505. `{$rel_tt_alias}`.`taxonomy` = '{$traverse[ 'name' ]}' 
  2506. AND `{$rel_tt_alias}`.`term_taxonomy_id` = `{$rel_alias}`.`term_taxonomy_id` 
  2507.  
  2508. LEFT JOIN `{$table_info[ 'table' ]}` AS `{$field_joined}` ON 
  2509. `{$field_joined}`.`{$table_info[ 'field_id' ]}` = `{$rel_tt_alias}`.`{$table_info[ 'field_id' ]}` 
  2510. "; 
  2511.  
  2512. // Override $rel_alias 
  2513. $rel_alias = $field_joined; 
  2514.  
  2515. $joined_id = $table_info[ 'field_id' ]; 
  2516. $joined_index = $table_info[ 'field_index' ]; 
  2517. elseif ( in_array( $traverse[ 'type' ], $tableless_field_types ) && ( 'pick' != $traverse[ 'type' ] || !in_array( pods_v( 'pick_object', $traverse ), $simple_tableless_objects ) ) ) { 
  2518. if ( pods_tableless() ) { 
  2519. $the_join = " 
  2520. LEFT JOIN `{$table_info[ 'meta_table' ]}` AS `{$rel_alias}` ON 
  2521. `{$rel_alias}`.`{$table_info[ 'meta_field_index' ]}` = '{$traverse[ 'name' ]}' 
  2522. AND `{$rel_alias}`.`{$table_info[ 'meta_field_id' ]}` = `{$traverse_recurse[ 'joined' ]}`.`{$traverse_recurse[ 'joined_id' ]}` 
  2523.  
  2524. LEFT JOIN `{$table_info[ 'meta_table' ]}` AS `{$field_joined}` ON 
  2525. `{$field_joined}`.`{$table_info[ 'meta_field_index' ]}` = '{$traverse[ 'name' ]}' 
  2526. AND `{$field_joined}`.`{$table_info[ 'meta_field_id' ]}` = CONVERT( `{$rel_alias}`.`{$table_info[ 'meta_field_value' ]}`, SIGNED ) 
  2527. "; 
  2528.  
  2529. $joined_id = $table_info[ 'meta_field_id' ]; 
  2530. $joined_index = $table_info[ 'meta_field_index' ]; 
  2531. elseif ( $meta_data_table ) { 
  2532. if ( $traverse[ 'id' ] !== $traverse[ 'pick_val' ] ) { 
  2533. // This must be a relationship 
  2534. $joined_id = 'related_item_id'; 
  2535. } else { 
  2536. $joined_id = $traverse_recurse[ 'joined_id' ]; 
  2537.  
  2538. $the_join = " 
  2539. LEFT JOIN `{$table_info['pod_table']}` AS `{$field_joined}` ON 
  2540. `{$field_joined}`.`{$table_info['pod_field_id']}` = `{$traverse_recurse['rel_alias']}`.`{$joined_id}` 
  2541. "; 
  2542. else { 
  2543. $the_join = " 
  2544. LEFT JOIN `@wp_podsrel` AS `{$rel_alias}` ON 
  2545. `{$rel_alias}`.`field_id` = {$traverse[ 'id' ]} 
  2546. AND `{$rel_alias}`.`item_id` = `{$traverse_recurse[ 'joined' ]}`.`{$traverse_recurse[ 'joined_id' ]}` 
  2547.  
  2548. LEFT JOIN `{$table_info[ 'table' ]}` AS `{$field_joined}` ON 
  2549. `{$field_joined}`.`{$table_info[ 'field_id' ]}` = `{$rel_alias}`.`related_item_id` 
  2550. "; 
  2551. elseif ( 'meta' == $pod_data[ 'storage' ] ) { 
  2552. if ( 
  2553. ( $traverse_recurse[ 'depth' ] + 2 ) == count( $traverse_recurse[ 'fields' ] ) 
  2554. && ( 'pick' != $traverse[ 'type' ] || !in_array( pods_var( 'pick_object', $traverse ), $simple_tableless_objects ) ) 
  2555. && $table_info[ 'meta_field_value' ] == $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] + 1 ] ) { 
  2556. $the_join = " 
  2557. LEFT JOIN `{$table_info[ 'meta_table' ]}` AS `{$field_joined}` ON 
  2558. `{$field_joined}`.`{$table_info[ 'meta_field_index' ]}` = '{$traverse[ 'name' ]}' 
  2559. AND `{$field_joined}`.`{$table_info[ 'meta_field_id' ]}` = `{$traverse_recurse[ 'joined' ]}`.`{$traverse_recurse[ 'joined_id' ]}` 
  2560. "; 
  2561.  
  2562. $table_info[ 'recurse' ] = false; 
  2563. else { 
  2564. $the_join = " 
  2565. LEFT JOIN `{$table_info[ 'meta_table' ]}` AS `{$field_joined}` ON 
  2566. `{$field_joined}`.`{$table_info[ 'meta_field_index' ]}` = '{$traverse[ 'name' ]}' 
  2567. AND `{$field_joined}`.`{$table_info[ 'meta_field_id' ]}` = `{$traverse_recurse[ 'joined' ]}`.`{$traverse_recurse[ 'joined_id' ]}` 
  2568. "; 
  2569.  
  2570. $joined_id = $table_info[ 'meta_field_id' ]; 
  2571. $joined_index = $table_info[ 'meta_field_index' ]; 
  2572.  
  2573. $traverse_recursive = array( 
  2574. 'pod' => pods_var_raw( 'name', pods_var_raw( 'pod', $table_info ) ),  
  2575. 'fields' => $traverse_recurse[ 'fields' ],  
  2576. 'joined' => $field_joined,  
  2577. 'depth' => ( $traverse_recurse[ 'depth' ] + 1 ),  
  2578. 'joined_id' => $joined_id,  
  2579. 'joined_index' => $joined_index,  
  2580. 'params' => $traverse_recurse[ 'params' ],  
  2581. 'rel_alias' => $rel_alias,  
  2582. 'last_table_info' => $table_info 
  2583. ); 
  2584.  
  2585. $the_join = apply_filters( 'pods_data_traverse_the_join', $the_join, $traverse_recurse, $traverse_recursive, $this ); 
  2586.  
  2587. if ( empty( $the_join ) ) 
  2588. return $joins; 
  2589.  
  2590. $joins[ $traverse_recurse[ 'pod' ] . '_' . $traverse_recurse[ 'depth' ] . '_' . $traverse[ 'id' ] ] = $the_join; 
  2591.  
  2592. if ( ( $traverse_recurse[ 'depth' ] + 1 ) < count( $traverse_recurse[ 'fields' ] ) && !empty( $traverse_recurse[ 'pod' ] ) && false !== $table_info[ 'recurse' ] ) 
  2593. $joins = array_merge( $joins, $this->traverse_recurse( $traverse_recursive ) ); 
  2594.  
  2595. return $joins; 
  2596.  
  2597. /** 
  2598. * Recursively join tables based on fields 
  2599. * @param array $fields Fields to recurse 
  2600. * @param null $all_fields (optional) If $fields is empty then traverse all fields, argument does not need to be passed 
  2601. * @param object $params (optional) Parameters from build() 
  2602. * @return array Array of joins 
  2603. */ 
  2604. function traverse ( $fields = null, $all_fields = null, $params = null ) { 
  2605. $joins = array(); 
  2606.  
  2607. if ( null === $fields ) 
  2608. $fields = $this->traverse_build( $all_fields, $params ); 
  2609.  
  2610. foreach ( (array) $fields as $field_group ) { 
  2611. $traverse_recurse = array( 
  2612. 'pod' => $this->pod,  
  2613. 'fields' => $fields,  
  2614. 'params' => $params,  
  2615. 'last_table_info' => $this->pod_data,  
  2616. 'joined_id' => $this->pod_data[ 'field_id' ],  
  2617. 'joined_index' => $this->pod_data[ 'field_index' ] 
  2618. ); 
  2619.  
  2620. if ( is_array( $field_group ) ) { 
  2621. $traverse_recurse[ 'fields' ] = $field_group; 
  2622.  
  2623. $joins = array_merge( $joins, $this->traverse_recurse( $traverse_recurse ) ); 
  2624. else { 
  2625. $joins = array_merge( $joins, $this->traverse_recurse( $traverse_recurse ) ); 
  2626. $joins = array_filter( $joins ); 
  2627.  
  2628. return $joins; 
  2629.  
  2630. $joins = array_filter( $joins ); 
  2631.  
  2632. return $joins; 
  2633.  
  2634. /** 
  2635. * Handle filters / actions for the class 
  2636. * @since 2.0 
  2637. */ 
  2638. private static function do_hook () { 
  2639. $args = func_get_args(); 
  2640.  
  2641. if ( empty( $args ) ) 
  2642. return false; 
  2643.  
  2644. $name = array_shift( $args ); 
  2645.  
  2646. return pods_do_hook( 'data', $name, $args ); 
  2647.  
  2648. /** 
  2649. * Get the complete sql 
  2650. * @since 2.0.5 
  2651. */ 
  2652. public function get_sql ( $sql ) { 
  2653. global $wpdb; 
  2654.  
  2655. if ( empty( $sql ) ) 
  2656. $sql = $this->sql; 
  2657.  
  2658. $sql = str_replace( array( '@wp_users', '@wp_' ), array( $wpdb->users, $wpdb->prefix ), $sql ); 
  2659.  
  2660. $sql = str_replace( '{prefix}', '@wp_', $sql ); 
  2661. $sql = str_replace( '{/prefix/}', '{prefix}', $sql ); 
  2662.  
  2663. return $sql;