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. if ( 'post_type' == $pod[ 'type' ] && 'meta' == $pod[ 'storage' ] && is_array( $params->orderby ) ) { 
  810.  
  811. foreach ( $params->orderby as $i => $orderby ) { 
  812. if ( strpos( $orderby, '.meta_value_num' ) ) { 
  813. $params->orderby[ $i ] = 'CAST(' . str_replace( '.meta_value_num', '.meta_value', $orderby ) . ' AS DECIMAL)'; 
  814. } elseif ( strpos( $orderby, '.meta_value_date' ) ) { 
  815. $params->orderby[ $i ] = 'CAST(' . str_replace( '.meta_value_date', '.meta_value', $orderby ) . ' AS DATE)'; 
  816.  
  817.  
  818.  
  819. $params->orderby = (array) $params->orderby; 
  820. } else { 
  821. $params->orderby = array(); 
  822.  
  823.  
  824. if ( false === $params->strict && !empty( $this->orderby ) ) 
  825. $params->orderby = array_merge( $params->orderby, (array) $this->orderby ); 
  826.  
  827. if ( !empty( $params->traverse ) ) 
  828. $this->traverse = $params->traverse; 
  829.  
  830. $allowed_search_modes = array( 'int', 'text', 'text_like' ); 
  831.  
  832. if ( !empty( $params->search_mode ) && in_array( $params->search_mode, $allowed_search_modes ) ) 
  833. $this->search_mode = $params->search_mode; 
  834.  
  835. $params->search = (boolean) $params->search; 
  836.  
  837. if ( 1 == pods_v( 'pods_debug_params_all', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) 
  838. pods_debug( $params ); 
  839.  
  840. $params->field_table_alias = 't'; 
  841.  
  842. // Get Aliases for future reference 
  843. $selectsfound = ''; 
  844.  
  845. if ( !empty( $params->select ) ) { 
  846. if ( is_array( $params->select ) ) 
  847. $selectsfound = implode( ', ', $params->select ); 
  848. else 
  849. $selectsfound = $params->select; 
  850.  
  851. // Pull Aliases from SQL query too 
  852. if ( null !== $params->sql ) { 
  853. $temp_sql = ' ' . trim( str_replace( array( "\n", "\r" ), ' ', $params->sql ) ); 
  854. $temp_sql = preg_replace( array( 
  855. '/\sSELECT\sSQL_CALC_FOUND_ROWS\s/i',  
  856. '/\sSELECT\s/i' 
  857. ),  
  858. array( 
  859. ' SELECT ',  
  860. ' SELECT SQL_CALC_FOUND_ROWS ' 
  861. ),  
  862. $temp_sql ); 
  863. preg_match( '/\sSELECT SQL_CALC_FOUND_ROWS\s(.*)\sFROM/i', $temp_sql, $selectmatches ); 
  864. if ( isset( $selectmatches[ 1 ] ) && !empty( $selectmatches[ 1 ] ) && false !== stripos( $selectmatches[ 1 ], ' AS ' ) ) 
  865. $selectsfound .= ( !empty( $selectsfound ) ? ', ' : '' ) . $selectmatches[ 1 ]; 
  866.  
  867. // Build Alias list 
  868. $this->aliases = array(); 
  869.  
  870. if ( !empty( $selectsfound ) && false !== stripos( $selectsfound, ' AS ' ) ) { 
  871. $theselects = array_filter( explode( ', ', $selectsfound ) ); 
  872.  
  873. if ( empty( $theselects ) ) 
  874. $theselects = array_filter( explode( ', ', $selectsfound ) ); 
  875.  
  876. foreach ( $theselects as $selected ) { 
  877. $selected = trim( $selected ); 
  878.  
  879. if ( strlen( $selected ) < 1 ) 
  880. continue; 
  881.  
  882. $selectfield = explode( ' AS ', str_replace( ' as ', ' AS ', $selected ) ); 
  883.  
  884. if ( 2 == count( $selectfield ) ) { 
  885. $field = trim( trim( $selectfield[ 1 ] ), '`' ); 
  886. $real_field = trim( trim( $selectfield[ 0 ] ), '`' ); 
  887. $this->aliases[ $field ] = $real_field; 
  888.  
  889. // Search 
  890. if ( !empty( $params->search ) && !empty( $params->fields ) ) { 
  891. if ( false !== $params->search_query && 0 < strlen( $params->search_query ) ) { 
  892. $where = $having = array(); 
  893.  
  894. if ( false !== $params->search_across ) { 
  895. foreach ( $params->fields as $key => $field ) { 
  896. if ( is_array( $field ) ) { 
  897. $attributes = $field; 
  898. $field = pods_v( 'name', $field, $key, true ); 
  899. else { 
  900. $attributes = array( 
  901. 'type' => '',  
  902. 'options' => array() 
  903. ); 
  904.  
  905. if ( isset( $attributes[ 'options' ][ 'search' ] ) && !$attributes[ 'options' ][ 'search' ] ) 
  906. continue; 
  907.  
  908. if ( in_array( $attributes[ 'type' ], array( 'date', 'time', 'datetime', 'number', 'decimal', 'currency', 'phone', 'password', 'boolean' ) ) ) 
  909. continue; 
  910.  
  911. $fieldfield = '`' . $field . '`'; 
  912.  
  913. if ( 'pick' == $attributes[ 'type' ] && !in_array( pods_v( 'pick_object', $attributes ), $simple_tableless_objects ) ) { 
  914. if ( false === $params->search_across_picks ) 
  915. continue; 
  916. else { 
  917. if ( empty( $attributes[ 'table_info' ] ) ) 
  918. $attributes[ 'table_info' ] = $this->api->get_table_info( pods_v( 'pick_object', $attributes ), pods_v( 'pick_val', $attributes ) ); 
  919.  
  920. if ( empty( $attributes[ 'table_info' ][ 'field_index' ] ) ) 
  921. continue; 
  922.  
  923. $fieldfield = $fieldfield . '.`' . $attributes[ 'table_info' ][ 'field_index' ] . '`'; 
  924. elseif ( in_array( $attributes[ 'type' ], $file_field_types ) ) { 
  925. if ( false === $params->search_across_files ) 
  926. continue; 
  927. else 
  928. $fieldfield = $fieldfield . '.`post_title`'; 
  929. elseif ( isset( $params->fields[ $field ] ) ) { 
  930. if ( $params->meta_fields ) 
  931. $fieldfield = $fieldfield . '.`' . $params->pod_table_prefix . '`'; 
  932. else 
  933. $fieldfield = '`' . $params->pod_table_prefix . '`.' . $fieldfield; 
  934. elseif ( !empty( $params->object_fields ) && !isset( $params->object_fields[ $field ] ) && 'meta' == $pod['storage'] ) 
  935. $fieldfield = $fieldfield . '.`meta_value`'; 
  936. else 
  937. $fieldfield = '`t`.' . $fieldfield; 
  938.  
  939. if ( isset( $this->aliases[ $field ] ) ) 
  940. $fieldfield = '`' . $this->aliases[ $field ] . '`'; 
  941.  
  942. if ( !empty( $attributes[ 'real_name' ] ) ) 
  943. $fieldfield = $attributes[ 'real_name' ]; 
  944.  
  945. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) 
  946. $having[] = "{$fieldfield} LIKE '%" . pods_sanitize_like( $params->search_query ) . "%'"; 
  947. else 
  948. $where[] = "{$fieldfield} LIKE '%" . pods_sanitize_like( $params->search_query ) . "%'"; 
  949. elseif ( !empty( $params->index ) ) { 
  950. $attributes = array(); 
  951.  
  952. $fieldfield = '`t`.`' . $params->index . '`'; 
  953.  
  954. if ( isset( $params->fields[ $params->index ] ) ) { 
  955. if ( $params->meta_fields ) 
  956. $fieldfield = '`' . $params->index . '`.`' . $params->pod_table_prefix . '`'; 
  957. else 
  958. $fieldfield = '`' . $params->pod_table_prefix . '`.`' . $params->index . '`'; 
  959. elseif ( !empty( $params->object_fields ) && !isset( $params->object_fields[ $params->index ] ) && 'meta' == $pod['storage'] ) 
  960. $fieldfield = '`' . $params->index . '`.`meta_value`'; 
  961.  
  962. if ( isset( $attributes[ 'real_name' ] ) && false !== $attributes[ 'real_name' ] && !empty( $attributes[ 'real_name' ] ) ) 
  963. $fieldfield = $attributes[ 'real_name' ]; 
  964.  
  965. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) 
  966. $having[] = "{$fieldfield} LIKE '%" . pods_sanitize_like( $params->search_query ) . "%'"; 
  967. else 
  968. $where[] = "{$fieldfield} LIKE '%" . pods_sanitize_like( $params->search_query ) . "%'"; 
  969.  
  970. if ( !empty( $where ) ) 
  971. $params->where[] = '( ' . implode( ' OR ', $where ) . ' )'; 
  972.  
  973. if ( !empty( $having ) ) 
  974. $params->having[] = '( ' . implode( ' OR ', $having ) . ' )'; 
  975.  
  976. // Filter 
  977. foreach ( $params->filters as $filter ) { 
  978. $where = $having = array(); 
  979.  
  980. if ( !isset( $params->fields[ $filter ] ) ) 
  981. continue; 
  982.  
  983. $attributes = $params->fields[ $filter ]; 
  984. $field = pods_v( 'name', $attributes, $filter, true ); 
  985.  
  986. $filterfield = '`' . $field . '`'; 
  987.  
  988. if ( 'pick' == $attributes[ 'type' ] && !in_array( pods_v( 'pick_object', $attributes ), $simple_tableless_objects ) ) { 
  989. if ( empty( $attributes[ 'table_info' ] ) ) 
  990. $attributes[ 'table_info' ] = $this->api->get_table_info( pods_v( 'pick_object', $attributes ), pods_v( 'pick_val', $attributes ) ); 
  991.  
  992. if ( empty( $attributes[ 'table_info' ][ 'field_index' ] ) ) 
  993. continue; 
  994.  
  995. $filterfield = $filterfield . '.`' . $attributes[ 'table_info' ][ 'field_index' ] . '`'; 
  996. elseif ( in_array( $attributes[ 'type' ], $file_field_types ) ) 
  997. $filterfield = $filterfield . '.`post_title`'; 
  998. elseif ( isset( $params->fields[ $field ] ) ) { 
  999. if ( $params->meta_fields && 'meta' == $pod['storage'] ) 
  1000. $filterfield = $filterfield . '.`meta_value`'; 
  1001. else 
  1002. $filterfield = '`' . $params->pod_table_prefix . '`.' . $filterfield; 
  1003. elseif ( !empty( $params->object_fields ) && !isset( $params->object_fields[ $field ] ) && 'meta' == $pod['storage'] ) 
  1004. $filterfield = $filterfield . '.`meta_value`'; 
  1005. else 
  1006. $filterfield = '`t`.' . $filterfield; 
  1007.  
  1008. if ( isset( $this->aliases[ $field ] ) ) 
  1009. $filterfield = '`' . $this->aliases[ $field ] . '`'; 
  1010.  
  1011. if ( !empty( $attributes[ 'real_name' ] ) ) 
  1012. $filterfield = $attributes[ 'real_name' ]; 
  1013.  
  1014. if ( 'pick' == $attributes[ 'type' ] ) { 
  1015. $filter_value = pods_v( 'filter_' . $field, 'get' ); 
  1016.  
  1017. if ( !is_array( $filter_value ) ) 
  1018. $filter_value = (array) $filter_value; 
  1019.  
  1020. foreach ( $filter_value as $filter_v ) { 
  1021. if ( in_array( pods_v( 'pick_object', $attributes ), $simple_tableless_objects ) ) { 
  1022. if ( strlen( $filter_v ) < 1 ) 
  1023. continue; 
  1024.  
  1025. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) { 
  1026. $having[] = "( {$filterfield} = '" . pods_sanitize( $filter_v ) . "'" 
  1027. . " OR {$filterfield} LIKE '%\"" . pods_sanitize_like( $filter_v ) . "\"%' )"; 
  1028. else { 
  1029. $where[] = "( {$filterfield} = '" . pods_sanitize( $filter_v ) . "'" 
  1030. . " OR {$filterfield} LIKE '%\"" . pods_sanitize_like( $filter_v ) . "\"%' )"; 
  1031. else { 
  1032. $filter_v = (int) $filter_v; 
  1033.  
  1034. if ( empty( $filter_v ) || empty( $attributes[ 'table_info' ] ) || empty( $attributes[ 'table_info' ][ 'field_id' ] ) ) 
  1035. continue; 
  1036.  
  1037. $filterfield = '`' . $field . '`.`' . $attributes[ 'table_info' ][ 'field_id' ] . '`'; 
  1038.  
  1039. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) 
  1040. $having[] = "{$filterfield} = " . $filter_v; 
  1041. else 
  1042. $where[] = "{$filterfield} = " . $filter_v; 
  1043. elseif ( in_array( $attributes[ 'type' ], array( 'date', 'datetime' ) ) ) { 
  1044. $start_value = pods_v( 'filter_' . $field . '_start', 'get', false ); 
  1045. $end_value = pods_v( 'filter_' . $field . '_end', 'get', false ); 
  1046.  
  1047. if ( empty( $start_value ) && empty( $end_value ) ) 
  1048. continue; 
  1049.  
  1050. if ( !empty( $start_value ) ) 
  1051. $start = date_i18n( 'Y-m-d', strtotime( $start_value ) ) . ( 'datetime' == $attributes[ 'type' ] ? ' 00:00:00' : '' ); 
  1052. else 
  1053. $start = date_i18n( 'Y-m-d' ) . ( 'datetime' == $attributes[ 'type' ] ) ? ' 00:00:00' : ''; 
  1054.  
  1055. if ( !empty( $end_value ) ) 
  1056. $end = date_i18n( 'Y-m-d', strtotime( $end_value ) ) . ( 'datetime' == $attributes[ 'type' ] ? ' 23:59:59' : '' ); 
  1057. else 
  1058. $end = date_i18n( 'Y-m-d' ) . ( 'datetime' == $attributes[ 'type' ] ) ? ' 23:59:59' : ''; 
  1059.  
  1060. if ( isset( $attributes[ 'date_ongoing' ] ) && true === $attributes[ 'date_ongoing' ] ) { 
  1061. $date_ongoing = '`' . $attributes[ 'date_ongoing' ] . '`'; 
  1062.  
  1063. if ( isset( $this->aliases[ $date_ongoing ] ) ) 
  1064. $date_ongoing = '`' . $this->aliases[ $date_ongoing ] . '`'; 
  1065.  
  1066. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) 
  1067. $having[] = "(({$filterfield} <= '$start' OR ({$filterfield} >= '$start' AND {$filterfield} <= '$end')) AND ({$date_ongoing} >= '$start' OR ({$date_ongoing} >= '$start' AND {$date_ongoing} <= '$end')))"; 
  1068. else 
  1069. $where[] = "(({$filterfield} <= '$start' OR ({$filterfield} >= '$start' AND {$filterfield} <= '$end')) AND ({$date_ongoing} >= '$start' OR ({$date_ongoing} >= '$start' AND {$date_ongoing} <= '$end')))"; 
  1070. else { 
  1071. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) 
  1072. $having[] = "({$filterfield} BETWEEN '$start' AND '$end')"; 
  1073. else 
  1074. $where[] = "({$filterfield} BETWEEN '$start' AND '$end')"; 
  1075. else { 
  1076. $filter_value = pods_v( 'filter_' . $field, 'get', '' ); 
  1077.  
  1078. if ( strlen( $filter_value ) < 1 ) 
  1079. continue; 
  1080.  
  1081. if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) 
  1082. $having[] = "{$filterfield} LIKE '%" . pods_sanitize_like( $filter_value ) . "%'"; 
  1083. else 
  1084. $where[] = "{$filterfield} LIKE '%" . pods_sanitize_like( $filter_value ) . "%'"; 
  1085.  
  1086. if ( !empty( $where ) ) 
  1087. $params->where[] = implode( ' AND ', $where ); 
  1088.  
  1089. if ( !empty( $having ) ) 
  1090. $params->having[] = implode( ' AND ', $having ); 
  1091.  
  1092. // Traverse the Rabbit Hole 
  1093. if ( !empty( $this->pod ) ) { 
  1094. $haystack = implode( ' ', (array) $params->select ) 
  1095. . ' ' . implode( ' ', (array) $params->where ) 
  1096. . ' ' . implode( ' ', (array) $params->groupby ) 
  1097. . ' ' . implode( ' ', (array) $params->having ) 
  1098. . ' ' . implode( ' ', (array) $params->orderby ); 
  1099. $haystack = preg_replace( '/\s/', ' ', $haystack ); 
  1100. $haystack = preg_replace( '/\w\(/', ' ', $haystack ); 
  1101. $haystack = str_replace( array( '(', ')', ' ', '\\\'', "\\\"" ), ' ', $haystack ); 
  1102.  
  1103. preg_match_all( '/`?[\w\-]+`?(?:\\.`?[\w\-]+`?)+(?=[^"\']*(?:"[^"]*"[^"]*|\'[^\']*\'[^\']*)*$)/', $haystack, $found, PREG_PATTERN_ORDER ); 
  1104.  
  1105. $found = (array) @current( $found ); 
  1106. $find = $replace = $traverse = array(); 
  1107.  
  1108. foreach ( $found as $key => $value ) { 
  1109. $value = str_replace( '`', '', $value ); 
  1110. $value = explode( '.', $value ); 
  1111. $dot = $last_value = array_pop( $value ); 
  1112.  
  1113. if ( 't' == $value[ 0 ] ) 
  1114. continue; 
  1115. elseif ( array_key_exists( $value[ 0 ], $params->join ) ) 
  1116. // Don't traverse for tables we are already going to join 
  1117. continue; 
  1118. elseif ( 1 == count( $value ) && '' == preg_replace( '/[0-9]*/', '', $value[ 0 ] ) && '' == preg_replace( '/[0-9]*/', '', $last_value ) ) 
  1119. continue; 
  1120.  
  1121. $found_value = str_replace( '`', '', $found[ $key ] ); 
  1122. $found_value = '([`]{1}|\b)' . str_replace( '.', '[`]*\.[`]*', $found_value ) . '([`]{1}|\b)'; 
  1123. $found_value = '/' . $found_value . '(?=[^"\']*(?:"[^"]*"[^"]*|\'[^\']*\'[^\']*)*$)/'; 
  1124.  
  1125. if ( in_array( $found_value, $find ) ) { 
  1126. continue; 
  1127.  
  1128. $find[ $key ] = $found_value; 
  1129.  
  1130. if ( '*' != $dot ) 
  1131. $dot = '`' . $dot . '`'; 
  1132.  
  1133. $replace[ $key ] = '`' . implode( '_', $value ) . '`.' . $dot; 
  1134.  
  1135. $value[] = $last_value; 
  1136.  
  1137. if ( !in_array( $value, $traverse ) ) 
  1138. $traverse[ $key ] = $value; 
  1139.  
  1140. if ( !empty( $this->traverse ) ) { 
  1141. foreach ( (array) $this->traverse as $key => $traverse ) { 
  1142. $traverse = str_replace( '`', '', $traverse ); 
  1143. $already_found = false; 
  1144.  
  1145. foreach ( $traverse as $traversal ) { 
  1146. if ( is_array( $traversal ) ) 
  1147. $traversal = implode( '.', $traversal ); 
  1148.  
  1149. if ( $traversal == $traverse ) { 
  1150. $already_found = true; 
  1151. break; 
  1152.  
  1153. if ( !$already_found ) 
  1154. $traverse[ 'traverse_' . $key ] = explode( '.', $traverse ); 
  1155.  
  1156. $joins = array(); 
  1157.  
  1158. if ( !empty( $find ) ) { 
  1159. $params->select = preg_replace( $find, $replace, $params->select ); 
  1160. $params->where = preg_replace( $find, $replace, $params->where ); 
  1161. $params->groupby = preg_replace( $find, $replace, $params->groupby ); 
  1162. $params->having = preg_replace( $find, $replace, $params->having ); 
  1163. $params->orderby = preg_replace( $find, $replace, $params->orderby ); 
  1164.  
  1165. if ( !empty( $traverse ) ) 
  1166. $joins = $this->traverse( $traverse, $params->fields, $params ); 
  1167. elseif ( false !== $params->search ) 
  1168. $joins = $this->traverse( null, $params->fields, $params ); 
  1169.  
  1170. // Traversal Search 
  1171. if ( !empty( $params->search ) && !empty( $this->search_where ) ) 
  1172. $params->where = array_merge( (array) $this->search_where, $params->where ); 
  1173.  
  1174. if ( !empty( $params->join ) && !empty( $joins ) ) 
  1175. $params->join = array_merge( $joins, (array) $params->join ); 
  1176. elseif ( !empty( $joins ) ) 
  1177. $params->join = $joins; 
  1178.  
  1179. // Build 
  1180. if ( null === $params->sql ) { 
  1181. $sql = " 
  1182. SELECT 
  1183. " . ( $params->calc_rows ? 'SQL_CALC_FOUND_ROWS' : '' ) . " 
  1184. " . ( $params->distinct ? 'DISTINCT' : '' ) . " 
  1185. " . ( !empty( $params->select ) ? ( is_array( $params->select ) ? implode( ', ', $params->select ) : $params->select ) : '*' ) . " 
  1186. FROM {$params->table} AS `t` 
  1187. " . ( !empty( $params->join ) ? ( is_array( $params->join ) ? implode( "\n ", $params->join ) : $params->join ) : '' ) . " 
  1188. " . ( !empty( $params->where ) ? 'WHERE ' . ( is_array( $params->where ) ? implode( ' AND ', $params->where ) : $params->where ) : '' ) . " 
  1189. " . ( !empty( $params->groupby ) ? 'GROUP BY ' . ( is_array( $params->groupby ) ? implode( ', ', $params->groupby ) : $params->groupby ) : '' ) . " 
  1190. " . ( !empty( $params->having ) ? 'HAVING ' . ( is_array( $params->having ) ? implode( ' AND ', $params->having ) : $params->having ) : '' ) . " 
  1191. " . ( !empty( $params->orderby ) ? 'ORDER BY ' . ( is_array( $params->orderby ) ? implode( ', ', $params->orderby ) : $params->orderby ) : '' ) . " 
  1192. " . ( ( 0 < $params->page && 0 < $params->limit ) ? 'LIMIT ' . $params->offset . ', ' . ( $params->limit ) : '' ) . " 
  1193. "; 
  1194.  
  1195. if ( !$params->calc_rows ) { 
  1196. // Handle COUNT() SELECT 
  1197. $total_sql_select = "COUNT( " . ( $params->distinct ? 'DISTINCT `t`.`' . $params->id . '`' : '*' ) . " )"; 
  1198.  
  1199. // If 'having' is set, we have to select all so it has access to anything it needs 
  1200. if ( ! empty( $params->having ) ) { 
  1201. $total_sql_select .= ', ' . ( !empty( $params->select ) ? ( is_array( $params->select ) ? implode( ', ', $params->select ) : $params->select ) : '*' ); 
  1202.  
  1203. $this->total_sql = " 
  1204. SELECT {$total_sql_select} 
  1205. FROM {$params->table} AS `t` 
  1206. " . ( !empty( $params->join ) ? ( is_array( $params->join ) ? implode( "\n ", $params->join ) : $params->join ) : '' ) . " 
  1207. " . ( !empty( $params->where ) ? 'WHERE ' . ( is_array( $params->where ) ? implode( ' AND ', $params->where ) : $params->where ) : '' ) . " 
  1208. " . ( !empty( $params->groupby ) ? 'GROUP BY ' . ( is_array( $params->groupby ) ? implode( ', ', $params->groupby ) : $params->groupby ) : '' ) . " 
  1209. " . ( !empty( $params->having ) ? 'HAVING ' . ( is_array( $params->having ) ? implode( ' AND ', $params->having ) : $params->having ) : '' ) . " 
  1210. "; 
  1211. // Rewrite 
  1212. else { 
  1213. $sql = ' ' . trim( str_replace( array( "\n", "\r" ), ' ', $params->sql ) ); 
  1214. $sql = preg_replace( array( 
  1215. '/\sSELECT\sSQL_CALC_FOUND_ROWS\s/i',  
  1216. '/\sSELECT\s/i' 
  1217. ),  
  1218. array( 
  1219. ' SELECT ',  
  1220. ' SELECT SQL_CALC_FOUND_ROWS ' 
  1221. ),  
  1222. $sql ); 
  1223.  
  1224. // Insert variables based on existing statements 
  1225. if ( false === stripos( $sql, '%%SELECT%%' ) ) 
  1226. $sql = preg_replace( '/\sSELECT\sSQL_CALC_FOUND_ROWS\s/i', ' SELECT SQL_CALC_FOUND_ROWS %%SELECT%% ', $sql ); 
  1227. if ( false === stripos( $sql, '%%WHERE%%' ) ) 
  1228. $sql = preg_replace( '/\sWHERE\s(?!.*\sWHERE\s)/i', ' WHERE %%WHERE%% ', $sql ); 
  1229. if ( false === stripos( $sql, '%%GROUPBY%%' ) ) 
  1230. $sql = preg_replace( '/\sGROUP BY\s(?!.*\sGROUP BY\s)/i', ' GROUP BY %%GROUPBY%% ', $sql ); 
  1231. if ( false === stripos( $sql, '%%HAVING%%' ) ) 
  1232. $sql = preg_replace( '/\sHAVING\s(?!.*\sHAVING\s)/i', ' HAVING %%HAVING%% ', $sql ); 
  1233. if ( false === stripos( $sql, '%%ORDERBY%%' ) ) 
  1234. $sql = preg_replace( '/\sORDER BY\s(?!.*\sORDER BY\s)/i', ' ORDER BY %%ORDERBY%% ', $sql ); 
  1235.  
  1236. // Insert variables based on other existing statements 
  1237. if ( false === stripos( $sql, '%%JOIN%%' ) ) { 
  1238. if ( false !== stripos( $sql, ' WHERE ' ) ) 
  1239. $sql = preg_replace( '/\sWHERE\s(?!.*\sWHERE\s)/i', ' %%JOIN%% WHERE ', $sql ); 
  1240. elseif ( false !== stripos( $sql, ' GROUP BY ' ) ) 
  1241. $sql = preg_replace( '/\sGROUP BY\s(?!.*\sGROUP BY\s)/i', ' %%WHERE%% GROUP BY ', $sql ); 
  1242. elseif ( false !== stripos( $sql, ' ORDER BY ' ) ) 
  1243. $sql = preg_replace( '/\ORDER BY\s(?!.*\ORDER BY\s)/i', ' %%WHERE%% ORDER BY ', $sql ); 
  1244. else 
  1245. $sql .= ' %%JOIN%% '; 
  1246. if ( false === stripos( $sql, '%%WHERE%%' ) ) { 
  1247. if ( false !== stripos( $sql, ' GROUP BY ' ) ) 
  1248. $sql = preg_replace( '/\sGROUP BY\s(?!.*\sGROUP BY\s)/i', ' %%WHERE%% GROUP BY ', $sql ); 
  1249. elseif ( false !== stripos( $sql, ' ORDER BY ' ) ) 
  1250. $sql = preg_replace( '/\ORDER BY\s(?!.*\ORDER BY\s)/i', ' %%WHERE%% ORDER BY ', $sql ); 
  1251. else 
  1252. $sql .= ' %%WHERE%% '; 
  1253. if ( false === stripos( $sql, '%%GROUPBY%%' ) ) { 
  1254. if ( false !== stripos( $sql, ' HAVING ' ) ) 
  1255. $sql = preg_replace( '/\sHAVING\s(?!.*\sHAVING\s)/i', ' %%GROUPBY%% HAVING ', $sql ); 
  1256. elseif ( false !== stripos( $sql, ' ORDER BY ' ) ) 
  1257. $sql = preg_replace( '/\ORDER BY\s(?!.*\ORDER BY\s)/i', ' %%GROUPBY%% ORDER BY ', $sql ); 
  1258. else 
  1259. $sql .= ' %%GROUPBY%% '; 
  1260. if ( false === stripos( $sql, '%%HAVING%%' ) ) { 
  1261. if ( false !== stripos( $sql, ' ORDER BY ' ) ) 
  1262. $sql = preg_replace( '/\ORDER BY\s(?!.*\ORDER BY\s)/i', ' %%HAVING%% ORDER BY ', $sql ); 
  1263. else 
  1264. $sql .= ' %%HAVING%% '; 
  1265. if ( false === stripos( $sql, '%%ORDERBY%%' ) ) 
  1266. $sql .= ' %%ORDERBY%% '; 
  1267. if ( false === stripos( $sql, '%%LIMIT%%' ) ) 
  1268. $sql .= ' %%LIMIT%% '; 
  1269.  
  1270. // Replace variables 
  1271. if ( 0 < strlen( $params->select ) ) { 
  1272. if ( false === stripos( $sql, '%%SELECT%% FROM ' ) ) 
  1273. $sql = str_ireplace( '%%SELECT%%', $params->select . ', ', $sql ); 
  1274. else 
  1275. $sql = str_ireplace( '%%SELECT%%', $params->select, $sql ); 
  1276. if ( 0 < strlen( $params->join ) ) 
  1277. $sql = str_ireplace( '%%JOIN%%', $params->join, $sql ); 
  1278. if ( 0 < strlen( $params->where ) ) { 
  1279. if ( false !== stripos( $sql, ' WHERE ' ) ) { 
  1280. if ( false !== stripos( $sql, ' WHERE %%WHERE%% ' ) ) 
  1281. $sql = str_ireplace( '%%WHERE%%', $params->where . ' AND ', $sql ); 
  1282. else 
  1283. $sql = str_ireplace( '%%WHERE%%', ' AND ' . $params->where, $sql ); 
  1284. else 
  1285. $sql = str_ireplace( '%%WHERE%%', ' WHERE ' . $params->where, $sql ); 
  1286. if ( 0 < strlen( $params->groupby ) ) { 
  1287. if ( false !== stripos( $sql, ' GROUP BY ' ) ) { 
  1288. if ( false !== stripos( $sql, ' GROUP BY %%GROUPBY%% ' ) ) 
  1289. $sql = str_ireplace( '%%GROUPBY%%', $params->groupby . ', ', $sql ); 
  1290. else 
  1291. $sql = str_ireplace( '%%GROUPBY%%', ', ' . $params->groupby, $sql ); 
  1292. else 
  1293. $sql = str_ireplace( '%%GROUPBY%%', ' GROUP BY ' . $params->groupby, $sql ); 
  1294. if ( 0 < strlen( $params->having ) && false !== stripos( $sql, ' GROUP BY ' ) ) { 
  1295. if ( false !== stripos( $sql, ' HAVING ' ) ) { 
  1296. if ( false !== stripos( $sql, ' HAVING %%HAVING%% ' ) ) 
  1297. $sql = str_ireplace( '%%HAVING%%', $params->having . ' AND ', $sql ); 
  1298. else 
  1299. $sql = str_ireplace( '%%HAVING%%', ' AND ' . $params->having, $sql ); 
  1300. else 
  1301. $sql = str_ireplace( '%%HAVING%%', ' HAVING ' . $params->having, $sql ); 
  1302. if ( 0 < strlen( $params->orderby ) ) { 
  1303. if ( false !== stripos( $sql, ' ORDER BY ' ) ) { 
  1304. if ( false !== stripos( $sql, ' ORDER BY %%ORDERBY%% ' ) ) 
  1305. $sql = str_ireplace( '%%ORDERBY%%', $params->groupby . ', ', $sql ); 
  1306. else 
  1307. $sql = str_ireplace( '%%ORDERBY%%', ', ' . $params->groupby, $sql ); 
  1308. else 
  1309. $sql = str_ireplace( '%%ORDERBY%%', ' ORDER BY ' . $params->groupby, $sql ); 
  1310. if ( 0 < $params->page && 0 < $params->limit ) { 
  1311. $start = ( $params->page - 1 ) * $params->limit; 
  1312. $end = $start + $params->limit; 
  1313. $sql .= 'LIMIT ' . (int) $start . ', ' . (int) $end; 
  1314.  
  1315. // Clear any unused variables 
  1316. $sql = str_ireplace( array( 
  1317. '%%SELECT%%',  
  1318. '%%JOIN%%',  
  1319. '%%WHERE%%',  
  1320. '%%GROUPBY%%',  
  1321. '%%HAVING%%',  
  1322. '%%ORDERBY%%',  
  1323. '%%LIMIT%%' 
  1324. ), '', $sql ); 
  1325. $sql = str_replace( array( '``', '`' ), array( ' ', ' ' ), $sql ); 
  1326.  
  1327. return $sql; 
  1328.  
  1329. /** 
  1330. * Fetch the total row count returned 
  1331. * @return int Number of rows returned by select() 
  1332. * @since 2.0 
  1333. */ 
  1334. public function total () { 
  1335. return (int) $this->total; 
  1336.  
  1337. /** 
  1338. * Fetch the total row count total 
  1339. * @return int Number of rows found by select() 
  1340. * @since 2.0 
  1341. */ 
  1342. public function total_found () { 
  1343. if(false === $this->total_found_calculated) 
  1344. $this->calculate_totals(); 
  1345.  
  1346. return (int) $this->total_found; 
  1347.  
  1348. /** 
  1349. * Fetch the zebra state 
  1350. * @return bool Zebra state 
  1351. * @since 1.12 
  1352. * @see PodsData::nth 
  1353. */ 
  1354. public function zebra () { 
  1355. return $this->nth( 'odd' ); // Odd numbers 
  1356.  
  1357. /** 
  1358. * Fetch the nth state 
  1359. * @param int|string $nth The $nth to match on the PodsData::row_number 
  1360. * @return bool Whether $nth matches 
  1361. * @since 2.3 
  1362. */ 
  1363. public function nth ( $nth ) { 
  1364. if ( empty( $nth ) ) 
  1365. $nth = 2; 
  1366.  
  1367. $offset = 0; 
  1368. $negative = false; 
  1369.  
  1370. if ( 'even' == $nth ) 
  1371. $nth = 2; 
  1372. elseif ( 'odd' == $nth ) { 
  1373. $negative = true; 
  1374. $nth = 2; 
  1375. elseif ( false !== strpos( $nth, '+' ) ) { 
  1376. $nth = explode( '+', $nth ); 
  1377.  
  1378. if ( isset( $nth[ 1 ] ) ) 
  1379. $offset += (int) trim( $nth[ 1 ] ); 
  1380.  
  1381. $nth = (int) trim( $nth[ 0 ], ' n' ); 
  1382. elseif ( false !== strpos( $nth, '-' ) ) { 
  1383. $nth = explode( '-', $nth ); 
  1384.  
  1385. if ( isset( $nth[ 1 ] ) ) 
  1386. $offset -= (int) trim( $nth[ 1 ] ); 
  1387.  
  1388. $nth = (int) trim( $nth[ 0 ], ' n' ); 
  1389.  
  1390. $nth = (int) $nth; 
  1391. $offset = (int) $offset; 
  1392.  
  1393. if ( 0 == ( ( $this->row_number % $nth ) + $offset ) ) 
  1394. return ( $negative ? false: true ); 
  1395.  
  1396. return ( $negative ? true : false ); 
  1397.  
  1398. /** 
  1399. * Fetch the current position in the loop (starting at 1) 
  1400. * @return int Current row number (+1) 
  1401. * @since 2.3 
  1402. */ 
  1403. public function position () { 
  1404. return $this->row_number + 1; 
  1405.  
  1406. /** 
  1407. * Create a Table 
  1408. * @param string $table Table name 
  1409. * @param string $fields 
  1410. * @param boolean $if_not_exists Check if the table exists. 
  1411. * @return array|bool|mixed|null|void 
  1412. * @uses PodsData::query 
  1413. * @since 2.0 
  1414. */ 
  1415. public static function table_create ( $table, $fields, $if_not_exists = false ) { 
  1416. /** 
  1417. * @var $wpdb wpdb 
  1418. */ 
  1419. global $wpdb; 
  1420.  
  1421. $sql = "CREATE TABLE"; 
  1422.  
  1423. if ( true === $if_not_exists ) 
  1424. $sql .= " IF NOT EXISTS"; 
  1425.  
  1426. $sql .= " `{$wpdb->prefix}" . self::$prefix . "{$table}` ({$fields})"; 
  1427.  
  1428. if ( !empty( $wpdb->charset ) ) 
  1429. $sql .= " DEFAULT CHARACTER SET {$wpdb->charset}"; 
  1430.  
  1431. if ( !empty( $wpdb->collate ) ) 
  1432. $sql .= " COLLATE {$wpdb->collate}"; 
  1433.  
  1434. return self::query( $sql ); 
  1435.  
  1436. /** 
  1437. * Alter a Table 
  1438. * @param string $table Table name 
  1439. * @param string $changes 
  1440. * @return array|bool|mixed|null|void 
  1441. * @uses PodsData::query 
  1442. * @since 2.0 
  1443. */ 
  1444. public static function table_alter ( $table, $changes ) { 
  1445. /** 
  1446. * @var $wpdb wpdb 
  1447. */ 
  1448. global $wpdb; 
  1449.  
  1450. $sql = "ALTER TABLE `{$wpdb->prefix}" . self::$prefix . "{$table}` {$changes}"; 
  1451.  
  1452. return self::query( $sql ); 
  1453.  
  1454. /** 
  1455. * Truncate a Table 
  1456. * @param string $table Table name 
  1457. * @return array|bool|mixed|null|void 
  1458. * @uses PodsData::query 
  1459. * @since 2.0 
  1460. */ 
  1461. public static function table_truncate ( $table ) { 
  1462. /** 
  1463. * @var $wpdb wpdb 
  1464. */ 
  1465. global $wpdb; 
  1466.  
  1467. $sql = "TRUNCATE TABLE `{$wpdb->prefix}" . self::$prefix . "{$table}`"; 
  1468.  
  1469. return self::query( $sql ); 
  1470.  
  1471. /** 
  1472. * Drop a Table 
  1473. * @param string $table Table name 
  1474. * @uses PodsData::query 
  1475. * @return array|bool|mixed|null|void 
  1476. * @uses PodsData::query 
  1477. * @since 2.0 
  1478. */ 
  1479. public static function table_drop ( $table ) { 
  1480. /** 
  1481. * @var $wpdb wpdb 
  1482. */ 
  1483. global $wpdb; 
  1484.  
  1485. $sql = "DROP TABLE `{$wpdb->prefix}" . self::$prefix . "{$table}`"; 
  1486.  
  1487. return self::query( $sql ); 
  1488.  
  1489. /** 
  1490. * Reorder Items 
  1491. * @param string $table Table name 
  1492. * @param string $weight_field 
  1493. * @param string $id_field 
  1494. * @param array $ids 
  1495. * @return bool 
  1496. * @uses PodsData::update 
  1497. * @since 2.0 
  1498. */ 
  1499. public function reorder ( $table, $weight_field, $id_field, $ids ) { 
  1500. $success = false; 
  1501. $ids = (array) $ids; 
  1502.  
  1503. list( $table, $weight_field, $id_field, $ids ) = $this->do_hook( 'reorder', array( 
  1504. $table,  
  1505. $weight_field,  
  1506. $id_field,  
  1507. $ids 
  1508. ) ); 
  1509.  
  1510. if ( !empty( $ids ) ) { 
  1511. $success = true; 
  1512.  
  1513. foreach ( $ids as $weight => $id ) { 
  1514. $updated = $this->update( $table, array( $weight_field => $weight ), array( $id_field => $id ), array( '%d' ), array( '%d' ) ); 
  1515.  
  1516. if ( false === $updated ) 
  1517. $success = false; 
  1518.  
  1519. return $success; 
  1520.  
  1521. /** 
  1522. * Fetch a new row for the current pod_data 
  1523. * @param int $row Row number to fetch 
  1524. * @param bool $explicit_set Whether to set explicitly (use false when in loop) 
  1525. * @return mixed 
  1526. * @since 2.0 
  1527. */ 
  1528. public function fetch ( $row = null, $explicit_set = true ) { 
  1529. global $wpdb; 
  1530.  
  1531. if ( null === $row ) 
  1532. $explicit_set = false; 
  1533.  
  1534. $already_cached = false; 
  1535. $id = $row; 
  1536.  
  1537. $tableless_field_types = PodsForm::tableless_field_types(); 
  1538.  
  1539. if ( null === $row ) { 
  1540. $this->row_number++; 
  1541.  
  1542. $this->row = false; 
  1543.  
  1544. if ( isset( $this->data[ $this->row_number ] ) ) { 
  1545. $this->row = get_object_vars( $this->data[ $this->row_number ] ); 
  1546.  
  1547. $current_row_id = false; 
  1548.  
  1549. if ( in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) ) 
  1550. $current_row_id = pods_var_raw( 'ID', $this->row ); 
  1551. elseif ( 'taxonomy' == $this->pod_data[ 'type' ] ) 
  1552. $current_row_id = pods_var_raw( 'term_id', $this->row ); 
  1553. elseif ( 'user' == $this->pod_data[ 'type' ] ) 
  1554. $current_row_id = pods_var_raw( 'ID', $this->row ); 
  1555. elseif ( 'comment' == $this->pod_data[ 'type' ] ) 
  1556. $current_row_id = pods_var_raw( 'comment_ID', $this->row ); 
  1557. elseif ( 'settings' == $this->pod_data[ 'type' ] ) 
  1558. $current_row_id = $this->pod_data[ 'id' ]; 
  1559.  
  1560. if ( 0 < $current_row_id ) 
  1561. $row = $current_row_id; 
  1562.  
  1563. if ( null !== $row || 'settings' == $this->pod_data[ 'type' ] ) { 
  1564. if ( $explicit_set ) 
  1565. $this->row_number = -1; 
  1566.  
  1567. $mode = 'id'; 
  1568. $id = pods_absint( $row ); 
  1569.  
  1570. if ( !is_numeric( $row ) || 0 === strpos( $row, '0' ) || $row != preg_replace( '/[^0-9]/', '', $row ) ) { 
  1571. $mode = 'slug'; 
  1572. $id = $row; 
  1573.  
  1574. $row = false; 
  1575.  
  1576. if ( !empty( $this->pod ) ) { 
  1577. $row = pods_cache_get( $id, 'pods_items_' . $this->pod ); 
  1578. if ( false !== $row ) { 
  1579. $already_cached = true; 
  1580.  
  1581. $current_row_id = false; 
  1582. $get_table_data = false; 
  1583.  
  1584. $old_row = $this->row; 
  1585.  
  1586. if ( false !== $row && is_array( $row ) ) 
  1587. $this->row = $row; 
  1588. elseif ( in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) ) { 
  1589. if ( 'post_type' == $this->pod_data[ 'type' ] ) { 
  1590. if ( empty( $this->pod_data[ 'object' ] ) ) { 
  1591. $post_type = $this->pod_data[ 'name' ]; 
  1592. else { 
  1593. $post_type = $this->pod_data[ 'object' ]; 
  1594. else 
  1595. $post_type = 'attachment'; 
  1596.  
  1597. if ( 'id' == $mode ) { 
  1598. $this->row = get_post( $id, ARRAY_A ); 
  1599.  
  1600. if ( is_array( $this->row ) && $this->row[ 'post_type' ] != $post_type ) 
  1601. $this->row = false; 
  1602. else { 
  1603. $args = array( 
  1604. 'post_type' => $post_type,  
  1605. 'name' => $id,  
  1606. 'numberposts' => 5 
  1607. ); 
  1608.  
  1609. $find = get_posts( $args ); 
  1610.  
  1611. if ( !empty( $find ) ) 
  1612. $this->row = get_object_vars( $find[ 0 ] ); 
  1613.  
  1614. if ( is_wp_error( $this->row ) || empty( $this->row ) ) 
  1615. $this->row = false; 
  1616. else 
  1617. $current_row_id = $this->row[ 'ID' ]; 
  1618.  
  1619. $get_table_data = true; 
  1620. elseif ( 'taxonomy' == $this->pod_data[ 'type' ] ) { 
  1621. $taxonomy = $this->pod_data[ 'object' ]; 
  1622.  
  1623. if ( empty( $taxonomy ) ) 
  1624. $taxonomy = $this->pod_data[ 'name' ]; 
  1625.  
  1626. // Taxonomies are registered during init, so they aren't available before then 
  1627. if ( !did_action( 'init' ) ) { 
  1628. // hackaround :( 
  1629. if ( 'id' == $mode ) 
  1630. $term_where = 't.term_id = %d'; 
  1631. else 
  1632. $term_where = 't.slug = %s'; 
  1633.  
  1634. $filter = 'raw'; 
  1635. $term = $id; 
  1636.  
  1637. if ( 'id' != $mode || !$_term = wp_cache_get( $term, $taxonomy ) ) { 
  1638. $_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 ) ); 
  1639.  
  1640. if ( $_term ) 
  1641. wp_cache_add( $term, $_term, $taxonomy ); 
  1642.  
  1643. $_term = apply_filters( 'get_term', $_term, $taxonomy ); 
  1644. $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy ); 
  1645. $_term = sanitize_term( $_term, $taxonomy, $filter ); 
  1646.  
  1647. if ( is_object( $_term ) ) 
  1648. $this->row = get_object_vars( $_term ); 
  1649. elseif ( 'id' == $mode ) 
  1650. $this->row = get_term( $id, $taxonomy, ARRAY_A ); 
  1651. else 
  1652. $this->row = get_term_by( 'slug', $id, $taxonomy, ARRAY_A ); 
  1653.  
  1654. if ( is_wp_error( $this->row ) || empty( $this->row ) ) 
  1655. $this->row = false; 
  1656. else 
  1657. $current_row_id = $this->row[ 'term_id' ]; 
  1658.  
  1659. $get_table_data = true; 
  1660. elseif ( 'user' == $this->pod_data[ 'type' ] ) { 
  1661. if ( 'id' == $mode ) 
  1662. $this->row = get_userdata( $id ); 
  1663. else 
  1664. $this->row = get_user_by( 'slug', $id ); 
  1665.  
  1666. if ( is_wp_error( $this->row ) || empty( $this->row ) ) 
  1667. $this->row = false; 
  1668. else { 
  1669. // Get other vars 
  1670. $roles = $this->row->roles; 
  1671. $caps = $this->row->caps; 
  1672. $allcaps = $this->row->allcaps; 
  1673.  
  1674. $this->row = get_object_vars( $this->row->data ); 
  1675.  
  1676. // Set other vars 
  1677. $this->row[ 'roles' ] = $roles; 
  1678. $this->row[ 'caps' ] = $caps; 
  1679. $this->row[ 'allcaps' ] = $allcaps; 
  1680.  
  1681. unset( $this->row[ 'user_pass' ] ); 
  1682.  
  1683. $current_row_id = $this->row[ 'ID' ]; 
  1684.  
  1685. $get_table_data = true; 
  1686. elseif ( 'comment' == $this->pod_data[ 'type' ] ) { 
  1687. $this->row = get_comment( $id, ARRAY_A ); 
  1688.  
  1689. // No slug handling here 
  1690.  
  1691. if ( is_wp_error( $this->row ) || empty( $this->row ) ) 
  1692. $this->row = false; 
  1693. else 
  1694. $current_row_id = $this->row[ 'comment_ID' ]; 
  1695.  
  1696. $get_table_data = true; 
  1697. elseif ( 'settings' == $this->pod_data[ 'type' ] ) { 
  1698. $this->row = array(); 
  1699.  
  1700. if ( empty( $this->fields ) ) 
  1701. $this->row = false; 
  1702. else { 
  1703. foreach ( $this->fields as $field ) { 
  1704. if ( !in_array( $field[ 'type' ], $tableless_field_types ) ) 
  1705. $this->row[ $field[ 'name' ] ] = get_option( $this->pod_data[ 'name' ] . '_' . $field[ 'name' ], null ); 
  1706.  
  1707. // Force ID 
  1708. $this->id = $this->pod_data[ 'id' ]; 
  1709. $this->row[ 'option_id' ] = $this->id; 
  1710. else { 
  1711. $params = array( 
  1712. 'table' => $this->table,  
  1713. 'where' => "`t`.`{$this->field_id}` = " . (int) $id,  
  1714. 'orderby' => "`t`.`{$this->field_id}` DESC",  
  1715. 'page' => 1,  
  1716. 'limit' => 1,  
  1717. 'search' => false 
  1718. ); 
  1719.  
  1720. if ( 'slug' == $mode && !empty( $this->field_slug ) ) { 
  1721. $id = pods_sanitize( $id ); 
  1722. $params[ 'where' ] = "`t`.`{$this->field_slug}` = '{$id}'"; 
  1723.  
  1724. $this->row = pods_data()->select( $params ); 
  1725.  
  1726. if ( empty( $this->row ) ) 
  1727. $this->row = false; 
  1728. else { 
  1729. $current_row = (array) $this->row; 
  1730. $this->row = get_object_vars( (object) @current( $current_row ) ); 
  1731.  
  1732. if ( !$explicit_set && !empty( $this->row ) && is_array( $this->row ) && !empty( $old_row ) ) { 
  1733. $this->row = array_merge( $old_row, $this->row ); 
  1734.  
  1735. if ( 'table' == $this->pod_data[ 'storage' ] && false !== $get_table_data && is_numeric( $current_row_id ) ) { 
  1736. $params = array( 
  1737. 'table' => $wpdb->prefix . "pods_",  
  1738. 'where' => "`t`.`id` = {$current_row_id}",  
  1739. 'orderby' => "`t`.`id` DESC",  
  1740. 'page' => 1,  
  1741. 'limit' => 1,  
  1742. 'search' => false,  
  1743. 'strict' => true 
  1744. ); 
  1745.  
  1746. if ( empty( $this->pod_data[ 'object' ] ) ) 
  1747. $params[ 'table' ] .= $this->pod_data[ 'name' ]; 
  1748. else 
  1749. $params[ 'table' ] .= $this->pod_data[ 'object' ]; 
  1750.  
  1751. $row = pods_data()->select( $params ); 
  1752.  
  1753. if ( !empty( $row ) ) { 
  1754. $current_row = (array) $row; 
  1755. $row = get_object_vars( (object) @current( $current_row ) ); 
  1756.  
  1757. if ( is_array( $this->row ) && !empty( $this->row ) ) 
  1758. $this->row = array_merge( $this->row, $row ); 
  1759. else 
  1760. $this->row = $row; 
  1761.  
  1762. if ( !empty( $this->pod ) && ! $already_cached ) { 
  1763. pods_cache_set( $id, $this->row, 'pods_items_' . $this->pod, 0 ); 
  1764.  
  1765. $this->row = apply_filters( 'pods_data_fetch', $this->row, $id, $this->row_number, $this ); 
  1766.  
  1767. return $this->row; 
  1768.  
  1769. /** 
  1770. * Reset the current data 
  1771. * @param int $row Row number to reset to 
  1772. * @return mixed 
  1773. * @since 2.0 
  1774. */ 
  1775. public function reset ( $row = null ) { 
  1776. $row = pods_absint( $row ); 
  1777.  
  1778. $this->row = false; 
  1779.  
  1780. if ( isset( $this->data[ $row ] ) ) 
  1781. $this->row = get_object_vars( $this->data[ $row ] ); 
  1782.  
  1783. if ( empty( $row ) ) 
  1784. $this->row_number = -1; 
  1785. else 
  1786. $this->row_number = $row - 1; 
  1787.  
  1788. return $this->row; 
  1789.  
  1790. /** 
  1791. * @static 
  1792. * Do a query on the database 
  1793. * @param string|array $sql The SQL to execute 
  1794. * @param string $error Error to throw on problems 
  1795. * @param null $results_error (optional) 
  1796. * @param null $no_results_error (optional) 
  1797. * @return array|bool|mixed|null|void Result of the query 
  1798. * @since 2.0 
  1799. */ 
  1800. public static function query ( $sql, $error = 'Database Error', $results_error = null, $no_results_error = null ) { 
  1801. /** 
  1802. * @var $wpdb wpdb 
  1803. */ 
  1804. global $wpdb; 
  1805.  
  1806. if ( $wpdb->show_errors ) 
  1807. self::$display_errors = true; 
  1808.  
  1809. $display_errors = self::$display_errors; 
  1810.  
  1811. if ( is_object( $error ) ) { 
  1812. if ( isset( $error->display_errors ) && false === $error->display_errors ) 
  1813. $display_errors = false; 
  1814.  
  1815. $error = 'Database Error'; 
  1816. elseif ( is_bool( $error ) ) { 
  1817. $display_errors = $error; 
  1818.  
  1819. if ( false !== $error ) 
  1820. $error = 'Database Error'; 
  1821.  
  1822. $params = (object) array( 
  1823. 'sql' => $sql,  
  1824. 'error' => $error,  
  1825. 'results_error' => $results_error,  
  1826. 'no_results_error' => $no_results_error,  
  1827. 'display_errors' => $display_errors 
  1828. ); 
  1829.  
  1830. // Handle Preparations of Values (sprintf format) 
  1831. if ( is_array( $sql ) ) { 
  1832. if ( isset( $sql[ 0 ] ) && 1 < count( $sql ) ) { 
  1833. if ( 2 == count( $sql ) ) { 
  1834. if ( !is_array( $sql[ 1 ] ) ) 
  1835. $sql[ 1 ] = array( $sql[ 1 ] ); 
  1836.  
  1837. $params->sql = self::prepare( $sql[ 0 ], $sql[ 1 ] ); 
  1838. elseif ( 3 == count( $sql ) ) 
  1839. $params->sql = self::prepare( $sql[ 0 ], array( $sql[ 1 ], $sql[ 2 ] ) ); 
  1840. else 
  1841. $params->sql = self::prepare( $sql[ 0 ], array( $sql[ 1 ], $sql[ 2 ], $sql[ 3 ] ) ); 
  1842. else 
  1843. $params = array_merge( $params, $sql ); 
  1844.  
  1845. if ( 1 == pods_var( 'pods_debug_sql_all', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) 
  1846. echo '<textarea cols="100" rows="24">' . esc_textarea( str_replace( array( '@wp_users', '@wp_' ), array( $wpdb->users, $wpdb->prefix ), $params->sql ) ) . '</textarea>'; 
  1847.  
  1848. $params->sql = trim( $params->sql ); 
  1849.  
  1850. // Run Query 
  1851. $params->sql = apply_filters( 'pods_data_query', $params->sql, $params ); 
  1852.  
  1853. $result = $wpdb->query( $params->sql ); 
  1854.  
  1855. $result = apply_filters( 'pods_data_query_result', $result, $params ); 
  1856.  
  1857. if ( false === $result && !empty( $params->error ) && !empty( $wpdb->last_error ) ) 
  1858. return pods_error( "{$params->error}; SQL: {$params->sql}; Response: {$wpdb->last_error}", $params->display_errors ); 
  1859.  
  1860. if ( 'INSERT' == strtoupper( substr( $params->sql, 0, 6 ) ) || 'REPLACE' == strtoupper( substr( $params->sql, 0, 7 ) ) ) 
  1861. $result = $wpdb->insert_id; 
  1862. elseif ( preg_match( '/^[\s\r\n\(]*SELECT/', strtoupper( $params->sql ) ) ) { 
  1863. $result = (array) $wpdb->last_result; 
  1864.  
  1865. if ( !empty( $result ) && !empty( $params->results_error ) ) 
  1866. return pods_error( $params->results_error, $params->display_errors ); 
  1867. elseif ( empty( $result ) && !empty( $params->no_results_error ) ) 
  1868. return pods_error( $params->no_results_error, $params->display_errors ); 
  1869.  
  1870. return $result; 
  1871.  
  1872. /** 
  1873. * Gets all tables in the WP database, optionally exclude WP core 
  1874. * tables, and/or Pods table by settings the parameters to false. 
  1875. * @param boolean $wp_core 
  1876. * @param boolean $pods_tables restrict Pods 2.x tables 
  1877. * @return array 
  1878. * @since 2.0 
  1879. */ 
  1880. public static function get_tables ( $wp_core = true, $pods_tables = true ) { 
  1881. global $wpdb; 
  1882.  
  1883. $core_wp_tables = array( 
  1884. $wpdb->options,  
  1885. $wpdb->comments,  
  1886. $wpdb->commentmeta,  
  1887. $wpdb->posts,  
  1888. $wpdb->postmeta,  
  1889. $wpdb->users,  
  1890. $wpdb->usermeta,  
  1891. $wpdb->links,  
  1892. $wpdb->terms,  
  1893. $wpdb->term_taxonomy,  
  1894. $wpdb->term_relationships 
  1895. ); 
  1896.  
  1897. $showTables = mysql_list_tables( DB_NAME ); 
  1898.  
  1899. $finalTables = array(); 
  1900.  
  1901. while ( $table = mysql_fetch_row( $showTables ) ) { 
  1902. if ( !$pods_tables && 0 === ( strpos( $table[ 0 ], $wpdb->prefix . rtrim( self::$prefix, '_' ) ) ) ) // don't include pods tables 
  1903. continue; 
  1904. elseif ( !$wp_core && in_array( $table[ 0 ], $core_wp_tables ) ) 
  1905. continue; 
  1906. else 
  1907. $finalTables[] = $table[ 0 ]; 
  1908.  
  1909. return $finalTables; 
  1910.  
  1911. /** 
  1912. * Gets column information from a table 
  1913. * @param string $table Table Name 
  1914. * @return array 
  1915. * @since 2.0 
  1916. */ 
  1917. public static function get_table_columns ( $table ) { 
  1918. global $wpdb; 
  1919.  
  1920. self::query( "SHOW COLUMNS FROM `{$table}` " ); 
  1921.  
  1922. $table_columns = $wpdb->last_result; 
  1923.  
  1924. $table_cols_and_types = array(); 
  1925.  
  1926. foreach ( $table_columns as $table_col ) { 
  1927. // Get only the type, not the attributes 
  1928. if ( false === strpos( $table_col->Type, '(' ) ) 
  1929. $modified_type = $table_col->Type; 
  1930. else 
  1931. $modified_type = substr( $table_col->Type, 0, ( strpos( $table_col->Type, '(' ) ) ); 
  1932.  
  1933. $table_cols_and_types[ $table_col->Field ] = $modified_type; 
  1934.  
  1935. return $table_cols_and_types; 
  1936.  
  1937. /** 
  1938. * Gets column data information from a table 
  1939. * @param string $column_name Column name 
  1940. * @param string $table Table name 
  1941. * @return array 
  1942. * @since 2.0 
  1943. */ 
  1944. public static function get_column_data ( $column_name, $table ) { 
  1945. $describe_data = mysql_query( 'DESCRIBE ' . $table ); 
  1946.  
  1947. $column_data = array(); 
  1948.  
  1949. while ( $column_row = mysql_fetch_assoc( $describe_data ) ) { 
  1950. $column_data[] = $column_row; 
  1951.  
  1952. foreach ( $column_data as $single_column ) { 
  1953. if ( $column_name == $single_column[ 'Field' ] ) 
  1954. return $single_column; 
  1955.  
  1956. return $column_data; 
  1957.  
  1958. /** 
  1959. * Prepare values for the DB 
  1960. * @param string $sql SQL to prepare 
  1961. * @param array $data Data to add to the sql prepare statement 
  1962. * @return bool|null|string 
  1963. * @since 2.0 
  1964. */ 
  1965. public static function prepare ( $sql, $data ) { 
  1966. /** 
  1967. * @var $wpdb wpdb 
  1968. */ 
  1969. global $wpdb; 
  1970. list( $sql, $data ) = apply_filters( 'pods_data_prepare', array( $sql, $data ) ); 
  1971. return $wpdb->prepare( $sql, $data ); 
  1972.  
  1973. /** 
  1974. * Get the string to use in a query for WHERE/HAVING, uses WP_Query meta_query arguments 
  1975. * @param array $fields Array of field matches for querying 
  1976. * @param array $pod Related Pod 
  1977. * @param object $params Parameters passed from select() 
  1978. * @return string|null Query string for WHERE/HAVING 
  1979. * @static 
  1980. * @since 2.3 
  1981. */ 
  1982. public static function query_fields ( $fields, $pod = null, &$params = null ) { 
  1983. $query_fields = array(); 
  1984.  
  1985. if ( !is_object( $params ) ) { 
  1986. $params = new stdClass(); 
  1987.  
  1988. if ( !isset( $params->query_field_level ) || 0 == $params->query_field_level ) { 
  1989. $params->query_fields = array(); 
  1990. $params->query_field_syntax = false; 
  1991. $params->query_field_level = 1; 
  1992.  
  1993. if ( !isset( $params->where_default ) ) { 
  1994. $params->where_default = array(); 
  1995.  
  1996. if ( !isset( $params->where_defaulted ) ) { 
  1997. $params->where_defaulted = false; 
  1998.  
  1999. $current_level = $params->query_field_level; 
  2000.  
  2001. $relation = 'AND'; 
  2002.  
  2003. if ( isset( $fields[ 'relation' ] ) ) { 
  2004. $relation = strtoupper( trim( pods_var( 'relation', $fields, 'AND', null, true ) ) ); 
  2005.  
  2006. if ( 'AND' != $relation ) 
  2007. $relation = 'OR'; 
  2008.  
  2009. unset( $fields[ 'relation' ] ); 
  2010.  
  2011. foreach ( $fields as $field => $match ) { 
  2012. if ( is_array( $match ) && isset( $match[ 'relation' ] ) ) { 
  2013. $params->query_field_level = $current_level + 1; 
  2014.  
  2015. $query_field = self::query_fields( $match, $pod, $params ); 
  2016.  
  2017. $params->query_field_level = $current_level; 
  2018.  
  2019. if ( !empty( $query_field ) ) { 
  2020. $query_fields[] = $query_field; 
  2021. else { 
  2022. $query_field = self::query_field( $field, $match, $pod, $params ); 
  2023.  
  2024. if ( !empty( $query_field ) ) { 
  2025. $query_fields[] = $query_field; 
  2026.  
  2027. if ( !empty( $query_fields ) ) { 
  2028. // If post_status not sent, detect it 
  2029. if ( !empty( $pod ) && 'post_type' == $pod[ 'type' ] && 1 == $current_level && !$params->where_defaulted && !empty( $params->where_default ) ) { 
  2030. $post_status_found = false; 
  2031.  
  2032. if ( !$params->query_field_syntax ) { 
  2033. $haystack = implode( ' ', (array) $query_fields ); 
  2034. $haystack = preg_replace( '/\s/', ' ', $haystack ); 
  2035. $haystack = preg_replace( '/\w\(/', ' ', $haystack ); 
  2036. $haystack = str_replace( array( '(', ')', ' ', '\\\'', "\\\"" ), ' ', $haystack ); 
  2037.  
  2038. preg_match_all( '/`?[\w\-]+`?(?:\\.`?[\w\-]+`?)+(?=[^"\']*(?:"[^"]*"[^"]*|\'[^\']*\'[^\']*)*$)/', $haystack, $found, PREG_PATTERN_ORDER ); 
  2039.  
  2040. $found = (array) @current( $found ); 
  2041.  
  2042. foreach ( $found as $value ) { 
  2043. $value = str_replace( '`', '', $value ); 
  2044. $value = explode( '.', $value ); 
  2045.  
  2046. if ( ( 'post_status' == $value[ 0 ] && 1 == count( $value ) ) || ( 2 == count( $value ) && 't' == $value[ 0 ] && 'post_status' == $value[ 1 ] ) ) { 
  2047. $post_status_found = true; 
  2048.  
  2049. break; 
  2050. elseif ( !empty( $params->query_fields ) && in_array( 'post_status', $params->query_fields ) ) { 
  2051. $post_status_found = true; 
  2052.  
  2053. if ( !$post_status_found ) { 
  2054. $query_fields[] = $params->where_default; 
  2055.  
  2056. if ( 1 < count( $query_fields ) ) 
  2057. $query_fields = '( ( ' . implode( ' ) ' . $relation . ' ( ', $query_fields ) . ' ) )'; 
  2058. else 
  2059. $query_fields = '( ' . implode( ' ' . $relation . ' ', $query_fields ) . ' )'; 
  2060. else 
  2061. $query_fields = null; 
  2062.  
  2063. // query_fields level complete 
  2064. if ( 1 == $params->query_field_level ) { 
  2065. $params->query_field_level = 0; 
  2066.  
  2067. return $query_fields; 
  2068.  
  2069. /** 
  2070. * Get the string to use in a query for matching, uses WP_Query meta_query arguments 
  2071. * @param string|int $field Field name or array index 
  2072. * @param array|string $q Query array (meta_query) or string for matching 
  2073. * @param array $pod Related Pod 
  2074. * @param object $params Parameters passed from select() 
  2075. * @return string|null Query field string 
  2076. * @see PodsData::query_fields 
  2077. * @static 
  2078. * @since 2.3 
  2079. */ 
  2080. public static function query_field ( $field, $q, $pod = null, &$params = null ) { 
  2081. global $wpdb; 
  2082.  
  2083. $simple_tableless_objects = PodsForm::simple_tableless_objects(); 
  2084.  
  2085. $field_query = null; 
  2086.  
  2087. // Plain queries 
  2088. if ( is_numeric( $field ) && !is_array( $q ) ) { 
  2089. return $q; 
  2090. // key => value queries (backwards compatibility) 
  2091. elseif ( !is_numeric( $field ) && ( !is_array( $q ) || ( !isset( $q[ 'key' ] ) && !isset( $q[ 'field' ] ) ) ) ) { 
  2092. $new_q = array( 
  2093. 'field' => $field,  
  2094. 'compare' => pods_var_raw( 'compare', $q, '=', null, true ),  
  2095. 'value' => pods_var_raw( 'value', $q, $q, null, true ),  
  2096. 'sanitize' => pods_var_raw( 'sanitize', $q, true ),  
  2097. 'sanitize_format' => pods_var_raw( 'sanitize_format', $q ),  
  2098. 'cast' => pods_var_raw( 'cast', $q ) 
  2099. ); 
  2100.  
  2101. if ( is_array( $new_q[ 'value' ] ) ) { 
  2102. if ( '=' == $new_q[ 'compare' ] ) 
  2103. $new_q[ 'compare' ] = 'IN'; 
  2104.  
  2105. if ( isset( $new_q[ 'value' ][ 'compare' ] ) ) 
  2106. unset( $new_q[ 'value' ][ 'compare' ] ); 
  2107.  
  2108. $q = $new_q; 
  2109.  
  2110. $field_name = trim( pods_var_raw( 'field', $q, pods_var_raw( 'key', $q, $field, null, true ), null, true ) ); 
  2111. $field_type = strtoupper( trim( pods_var_raw( 'type', $q, 'CHAR', null, true ) ) ); 
  2112. $field_value = pods_var_raw( 'value', $q ); 
  2113. $field_compare = strtoupper( trim( pods_var_raw( 'compare', $q, ( is_array( $field_value ? 'IN' : '=' ) ), null, true ) ) ); 
  2114. $field_sanitize = (boolean) pods_var( 'sanitize', $q, true ); 
  2115. $field_sanitize_format = pods_var_raw( 'sanitize_format', $q, null, null, true ); 
  2116. $field_cast = pods_var_raw( 'cast', $q, null, null, true ); 
  2117.  
  2118. if ( is_object( $params ) ) { 
  2119. $params->meta_query_syntax = true; 
  2120. $params->query_fields[] = $field_name; 
  2121.  
  2122. // Deprecated WP type 
  2123. if ( 'NUMERIC' == $field_type ) 
  2124. $field_type = 'SIGNED'; 
  2125. // Restrict to supported types 
  2126. elseif ( !in_array( $field_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) ) 
  2127. $field_type = 'CHAR'; 
  2128.  
  2129. // Alias / Casting 
  2130. if ( empty( $field_cast ) ) { 
  2131. // Setup field casting from field name 
  2132. if ( false === strpos( $field_name, '`' ) && false === strpos( $field_name, '(' ) && false === strpos( $field_name, ' ' ) ) { 
  2133. // Handle field naming if Pod-based 
  2134. if ( !empty( $pod ) && false === strpos( $field_name, '.' ) ) { 
  2135. $field_cast = ''; 
  2136.  
  2137. $tableless_field_types = PodsForm::tableless_field_types(); 
  2138.  
  2139. if ( isset( $pod[ 'fields' ][ $field_name ] ) && in_array( $pod[ 'fields' ][ $field_name ][ 'type' ], $tableless_field_types ) ) { 
  2140. if ( in_array( $pod[ 'fields' ][ $field_name ][ 'pick_object' ], $simple_tableless_objects ) ) { 
  2141. if ( 'meta' == $pod[ 'storage' ] ) 
  2142. $field_cast = "`{$field_name}`.`meta_value`"; 
  2143. else 
  2144. $field_cast = "`t`.`{$field_name}`"; 
  2145. else { 
  2146. $table = pods_api()->get_table_info( $pod[ 'fields' ][ $field_name ][ 'pick_object' ], $pod[ 'fields' ][ $field_name ][ 'pick_val' ] ); 
  2147.  
  2148. if ( !empty( $table ) ) 
  2149. $field_cast = "`{$field_name}`.`" . $table[ 'field_index' ] . '`'; 
  2150.  
  2151. if ( empty( $field_cast ) ) { 
  2152. if ( !in_array( $pod[ 'type' ], array( 'pod', 'table' ) ) ) { 
  2153. if ( isset( $pod[ 'object_fields' ][ $field_name ] ) ) 
  2154. $field_cast = "`t`.`{$field_name}`"; 
  2155. elseif ( isset( $pod[ 'fields' ][ $field_name ] ) ) { 
  2156. if ( 'meta' == $pod['storage'] ) 
  2157. $field_cast = "`{$field_name}`.`meta_value`"; 
  2158. else 
  2159. $field_cast = "`d`.`{$field_name}`"; 
  2160. else { 
  2161. foreach ( $pod[ 'object_fields' ] as $object_field => $object_field_opt ) { 
  2162. if ( $object_field == $field_name || in_array( $field_name, $object_field_opt[ 'alias' ] ) ) { 
  2163. $field_cast = "`t`.`{$object_field}`"; 
  2164.  
  2165. break; 
  2166. elseif ( isset( $pod[ 'fields' ][ $field_name ] ) ) { 
  2167. if ( 'meta' == $pod['storage'] ) 
  2168. $field_cast = "`{$field_name}`.`meta_value`"; 
  2169. else 
  2170. $field_cast = "`t`.`{$field_name}`"; 
  2171.  
  2172. if ( empty( $field_cast ) ) { 
  2173. if ( 'meta' == $pod['storage'] ) { 
  2174. $field_cast = "`{$field_name}`.`meta_value`"; 
  2175. else 
  2176. $field_cast = "`t`.`{$field_name}`"; 
  2177. else { 
  2178. $field_cast = '`' . str_replace( '.', '`.`', $field_name ) . '`'; 
  2179. else { 
  2180. $field_cast = $field_name; 
  2181.  
  2182. // Cast field if needed 
  2183. if ( 'CHAR' != $field_type ) { 
  2184. $field_cast = 'CAST( ' . $field_cast . ' AS ' . $field_type .' )'; 
  2185.  
  2186. // Setup string sanitizing for $wpdb->prepare() 
  2187. if ( empty( $field_sanitize_format ) ) { 
  2188. // Sanitize as string 
  2189. $field_sanitize_format = '%s'; 
  2190.  
  2191. // Sanitize as integer if needed 
  2192. if ( in_array( $field_type, array( 'UNSIGNED', 'SIGNED' ) ) ) 
  2193. $field_sanitize_format = '%d'; 
  2194.  
  2195. // Restrict to supported comparisons 
  2196. if ( !in_array( $field_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'ALL', 'BETWEEN', 'NOT BETWEEN', 'EXISTS', 'NOT EXISTS', 'REGEXP', 'NOT REGEXP', 'RLIKE' ) ) ) 
  2197. $field_compare = '='; 
  2198.  
  2199. // Restrict to supported array comparisons 
  2200. if ( is_array( $field_value ) && !in_array( $field_compare, array( 'IN', 'NOT IN', 'ALL', 'BETWEEN', 'NOT BETWEEN' ) ) ) { 
  2201. if ( in_array( $field_compare, array( '!=', 'NOT LIKE' ) ) ) 
  2202. $field_compare = 'NOT IN'; 
  2203. else 
  2204. $field_compare = 'IN'; 
  2205. // Restrict to supported string comparisons 
  2206. elseif ( !is_array( $field_value ) && in_array( $field_compare, array( 'IN', 'NOT IN', 'ALL', 'BETWEEN', 'NOT BETWEEN' ) ) ) { 
  2207. $check_value = preg_split( '/[, \s]+/', $field_value ); 
  2208.  
  2209. if ( 1 < count( $check_value ) ) 
  2210. $field_value = $check_value; 
  2211. elseif ( in_array( $field_compare, array( 'NOT IN', 'NOT BETWEEN' ) ) ) 
  2212. $field_compare = '!='; 
  2213. else 
  2214. $field_compare = '='; 
  2215.  
  2216. // Restrict to two values, force = and != if only one value provided 
  2217. if ( in_array( $field_compare, array( 'BETWEEN', 'NOT BETWEEN' ) ) ) { 
  2218. $field_value = array_values( array_slice( $field_value, 0, 2 ) ); 
  2219.  
  2220. if ( 1 == count( $field_value ) ) { 
  2221. if ( 'NOT IN' == $field_compare ) 
  2222. $field_compare = '!='; 
  2223. else 
  2224. $field_compare = '='; 
  2225.  
  2226. // Single array handling 
  2227. if ( 1 == count( $field_value ) && $field_compare == 'ALL' ) { 
  2228. $field_compare = '='; 
  2229. // Empty array handling 
  2230. elseif ( empty( $field_value ) && in_array( $field_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { 
  2231. $field_compare = 'EXISTS'; 
  2232.  
  2233. // Rebuild $q 
  2234. $q = array( 
  2235. 'field' => $field_name,  
  2236. 'type' => $field_type,  
  2237. 'value' => $field_value,  
  2238. 'compare' => $field_compare,  
  2239. 'sanitize' => $field_sanitize,  
  2240. 'sanitize_format' => $field_sanitize_format,  
  2241. 'cast' => $field_cast 
  2242. ); 
  2243.  
  2244. // Make the query 
  2245. if ( in_array( $field_compare, array( '=', '!=', '>', '>=', '<', '<=', 'REGEXP', 'NOT REGEXP', 'RLIKE' ) ) ) { 
  2246. if ( $field_sanitize ) { 
  2247. $field_query = $wpdb->prepare( $field_cast . ' ' . $field_compare . ' ' . $field_sanitize_format, $field_value ); 
  2248. else { 
  2249. $field_query = $field_cast . ' ' . $field_compare . ' "' . $field_value . '"'; 
  2250. elseif ( in_array( $field_compare, array( 'LIKE', 'NOT LIKE' ) ) ) { 
  2251. if ( $field_sanitize ) { 
  2252. $field_query = $field_cast . ' ' . $field_compare . ' "%' . pods_sanitize_like( $field_value ) . '%"'; 
  2253. else { 
  2254. $field_query = $field_cast . ' ' . $field_compare . ' "' . $field_value . '"'; 
  2255. elseif ( in_array( $field_compare, array( 'IN', 'NOT IN', 'ALL' ) ) ) { 
  2256. if ( $field_compare == 'ALL' ) { 
  2257. $field_compare = 'IN'; 
  2258.  
  2259. if ( ! empty( $pod ) ) { 
  2260. $params->having[] = 'COUNT( DISTINCT ' . $field_cast . ' ) = ' . count( $field_value ); 
  2261.  
  2262. if ( empty( $params->groupby ) || ( ! in_array( '`t`.`' . $pod['field_id'] . '`', $params->groupby ) && ! in_array( 't.' . $pod['field_id'] . '', $params->groupby ) ) ) { 
  2263. $params->groupby[] = '`t`.`' . $pod['field_id'] . '`'; 
  2264.  
  2265. if ( $field_sanitize ) { 
  2266. $field_query = $wpdb->prepare( $field_cast . ' ' . $field_compare . ' ( ' . substr( str_repeat( ', ' . $field_sanitize_format, count( $field_value ) ), 1 ) . ' )', $field_value ); 
  2267. else { 
  2268. $field_query = $field_cast . ' ' . $field_compare . ' ( "' . implode( '", "', $field_value ) . '" )'; 
  2269. elseif ( in_array( $field_compare, array( 'BETWEEN', 'NOT BETWEEN' ) ) ) { 
  2270. if ( $field_sanitize ) { 
  2271. $field_query = $wpdb->prepare( $field_cast . ' ' . $field_compare . ' ' . $field_sanitize_format . ' AND ' . $field_sanitize_format, $field_value ); 
  2272. else { 
  2273. $field_query = $field_cast . ' ' . $field_compare . ' "' . $field_value[ 0 ] . '" AND "' . $field_value[ 1 ] . '"'; 
  2274. elseif ( 'EXISTS' == $field_compare ) 
  2275. $field_query = $field_cast . ' IS NOT NULL'; 
  2276. elseif ( 'NOT EXISTS' == $field_compare ) 
  2277. $field_query = $field_cast . ' IS NULL'; 
  2278.  
  2279. $field_query = apply_filters( 'pods_data_field_query', $field_query, $q ); 
  2280.  
  2281. return $field_query; 
  2282.  
  2283. /** 
  2284. * Setup fields for traversal 
  2285. * @param array $fields Associative array of fields data 
  2286. * @return array Traverse feed 
  2287. * @param object $params (optional) Parameters from build() 
  2288. * @since 2.0 
  2289. */ 
  2290. function traverse_build ( $fields = null, $params = null ) { 
  2291. if ( null === $fields ) 
  2292. $fields = $this->fields; 
  2293.  
  2294. $feed = array(); 
  2295.  
  2296. foreach ( $fields as $field => $data ) { 
  2297. if ( !is_array( $data ) ) 
  2298. $field = $data; 
  2299.  
  2300. if ( !isset( $_GET[ 'filter_' . $field ] ) ) 
  2301. continue; 
  2302.  
  2303. $field_value = pods_var( 'filter_' . $field, 'get', false, null, true ); 
  2304.  
  2305. if ( !empty( $field_value ) || 0 < strlen( $field_value ) ) 
  2306. $feed[ 'traverse_' . $field ] = array( $field ); 
  2307.  
  2308. return $feed; 
  2309.  
  2310. /** 
  2311. * Recursively join tables based on fields 
  2312. * @param array $traverse_recurse Array of traversal options 
  2313. * @return array Array of table joins 
  2314. * @since 2.0 
  2315. */ 
  2316. function traverse_recurse ( $traverse_recurse ) { 
  2317. global $wpdb; 
  2318.  
  2319. $defaults = array( 
  2320. 'pod' => null,  
  2321. 'fields' => array(),  
  2322. 'joined' => 't',  
  2323. 'depth' => 0,  
  2324. 'joined_id' => 'id',  
  2325. 'joined_index' => 'id',  
  2326. 'params' => new stdClass(),  
  2327. 'last_table_info' => array() 
  2328. ); 
  2329.  
  2330. $traverse_recurse = array_merge( $defaults, $traverse_recurse ); 
  2331.  
  2332. $joins = array(); 
  2333.  
  2334. if ( 0 == $traverse_recurse[ 'depth' ] && !empty( $traverse_recurse[ 'pod' ] ) && !empty( $traverse_recurse [ 'last_table_info' ] ) && isset( $traverse_recurse [ 'last_table_info' ][ 'id' ] ) ) 
  2335. $pod_data = $traverse_recurse [ 'last_table_info' ]; 
  2336. elseif ( empty( $traverse_recurse[ 'pod' ] ) ) { 
  2337. if ( !empty( $traverse_recurse[ 'params' ] ) && !empty( $traverse_recurse[ 'params' ]->table ) && 0 === strpos( $traverse_recurse[ 'params' ]->table, $wpdb->prefix ) ) { 
  2338. if ( $wpdb->posts == $traverse_recurse[ 'params' ]->table ) 
  2339. $traverse_recurse[ 'pod' ] = 'post_type'; 
  2340. elseif ( $wpdb->terms == $traverse_recurse[ 'params' ]->table ) 
  2341. $traverse_recurse[ 'pod' ] = 'taxonomy'; 
  2342. elseif ( $wpdb->users == $traverse_recurse[ 'params' ]->table ) 
  2343. $traverse_recurse[ 'pod' ] = 'user'; 
  2344. elseif ( $wpdb->comments == $traverse_recurse[ 'params' ]->table ) 
  2345. $traverse_recurse[ 'pod' ] = 'comment'; 
  2346. else 
  2347. return $joins; 
  2348.  
  2349. $pod_data = array(); 
  2350.  
  2351. if ( in_array( $traverse_recurse[ 'pod' ], array( 'user', 'comment' ) ) ) { 
  2352. $pod = $this->api->load_pod( array( 'name' => $traverse_recurse[ 'pod' ], 'table_info' => true ) ); 
  2353.  
  2354. if ( !empty( $pod ) && $pod[ 'type' ] == $pod ) 
  2355. $pod_data = $pod; 
  2356.  
  2357. if ( empty( $pod_data ) ) { 
  2358. $pod_data = array( 
  2359. 'id' => 0,  
  2360. 'name' => '_table_' . $traverse_recurse[ 'pod' ],  
  2361. 'type' => $traverse_recurse[ 'pod' ],  
  2362. 'storage' => ( 'taxonomy' == $traverse_recurse[ 'pod' ] ? 'none' : 'meta' ),  
  2363. 'fields' => array(),  
  2364. 'object_fields' => $this->api->get_wp_object_fields( $traverse_recurse[ 'pod' ] ) 
  2365. ); 
  2366.  
  2367. $pod_data = array_merge( $this->api->get_table_info( $traverse_recurse[ 'pod' ], '' ), $pod_data ); 
  2368.  
  2369. $traverse_recurse[ 'pod' ] = $pod_data[ 'name' ]; 
  2370. else 
  2371. return $joins; 
  2372. else { 
  2373. $pod_data = $this->api->load_pod( array( 'name' => $traverse_recurse[ 'pod' ], 'table_info' => true ), false ); 
  2374.  
  2375. if ( empty( $pod_data ) ) 
  2376. return $joins; 
  2377.  
  2378. if ( isset( $pod_data[ 'object_fields' ] ) ) { 
  2379. $pod_data[ 'fields' ] = array_merge( $pod_data[ 'fields' ], $pod_data[ 'object_fields' ] ); 
  2380.  
  2381. $tableless_field_types = PodsForm::tableless_field_types(); 
  2382. $simple_tableless_objects = PodsForm::simple_tableless_objects(); 
  2383. $file_field_types = PodsForm::file_field_types(); 
  2384.  
  2385. if ( !isset( $this->traversal[ $traverse_recurse[ 'pod' ] ] ) ) 
  2386. $this->traversal[ $traverse_recurse[ 'pod' ] ] = array(); 
  2387.  
  2388. if ( ( empty( $pod_data[ 'meta_table' ] ) || $pod_data[ 'meta_table' ] == $pod_data[ 'table' ] ) && ( empty( $traverse_recurse[ 'fields' ] ) || empty( $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] ] ) ) ) 
  2389. return $joins; 
  2390.  
  2391. $field = $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] ]; 
  2392.  
  2393. $ignore_aliases = array( 
  2394. 'wpml_languages',  
  2395. 'polylang_languages' 
  2396. ); 
  2397.  
  2398. $ignore_aliases = apply_filters( 'pods_data_traverse_recurse_ignore_aliases', $ignore_aliases, $field, $traverse_recurse, $this ); 
  2399.  
  2400. if ( in_array( $field, $ignore_aliases ) ) 
  2401. return $joins; 
  2402.  
  2403. $meta_data_table = false; 
  2404.  
  2405. if ( !isset( $pod_data[ 'fields' ][ $field ] ) && 'd' == $field && isset( $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] - 1 ] ) ) { 
  2406. $field = $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] - 1 ]; 
  2407.  
  2408. $field_type = 'pick'; 
  2409.  
  2410. if ( isset( $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'fields' ][ $field ] ) ) { 
  2411. $field_type = $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'fields' ][ $field ][ 'type' ]; 
  2412. elseif ( isset( $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'object_fields' ][ $field ] ) ) { 
  2413. $field_type = $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'object_fields' ][ $field ][ 'type' ]; 
  2414.  
  2415. $pod_data[ 'fields' ][ $field ] = array( 
  2416. 'id' => 0,  
  2417. 'name' => $field,  
  2418. 'type' => $field_type,  
  2419. 'pick_object' => $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'type' ],  
  2420. 'pick_val' => $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'name' ] 
  2421. ); 
  2422.  
  2423. $meta_data_table = true; 
  2424.  
  2425. // Fallback to meta table if the pod type supports it 
  2426. if ( !isset( $pod_data[ 'fields' ][ $field ] ) ) { 
  2427. $last = end( $traverse_recurse[ 'fields' ] ); 
  2428.  
  2429. if ( 'post_type' == $pod_data[ 'type' ] && !isset( $pod_data[ 'object_fields' ] ) ) 
  2430. $pod_data[ 'object_fields' ] = $this->api->get_wp_object_fields( 'post_type', $pod_data ); 
  2431.  
  2432. if ( 'post_type' == $pod_data[ 'type' ] && isset( $pod_data[ 'object_fields'][ $field ] ) && in_array( $pod_data[ 'object_fields' ][ $field ][ 'type' ], $tableless_field_types ) ) 
  2433. $pod_data[ 'fields' ][ $field ] = $pod_data[ 'object_fields' ][ $field ]; 
  2434. elseif ( 'meta_value' === $last && in_array( $pod_data[ 'type' ], array( 'post_type', 'media', 'user', 'comment' ) ) ) 
  2435. $pod_data[ 'fields' ][ $field ] = PodsForm::field_setup( array( 'name' => $field ) ); 
  2436. else { 
  2437. if ( 'post_type' == $pod_data[ 'type' ] ) { 
  2438. $pod_data[ 'object_fields' ] = $this->api->get_wp_object_fields( 'post_type', $pod_data, true ); 
  2439.  
  2440. if ( 'post_type' == $pod_data[ 'type' ] && isset( $pod_data[ 'object_fields' ][ $field ] ) && in_array( $pod_data[ 'object_fields' ][ $field ][ 'type' ], $tableless_field_types ) ) 
  2441. $pod_data[ 'fields' ][ $field ] = $pod_data[ 'object_fields' ][ $field ]; 
  2442. else 
  2443. return $joins; 
  2444. else 
  2445. return $joins; 
  2446. } elseif ( isset( $pod_data[ 'object_fields' ] ) && isset( $pod_data[ 'object_fields' ][ $field ] ) && ! in_array( $pod_data[ 'object_fields' ][ $field ][ 'type' ], $tableless_field_types ) ) { 
  2447. return $joins; 
  2448.  
  2449. $traverse = $pod_data[ 'fields' ][ $field ]; 
  2450.  
  2451. if ( 'taxonomy' == $traverse[ 'type' ] ) 
  2452. $traverse[ 'table_info' ] = $this->api->get_table_info( $traverse[ 'type' ], $traverse[ 'name' ] ); 
  2453. elseif ( in_array( $traverse[ 'type' ], $file_field_types ) ) 
  2454. $traverse[ 'table_info' ] = $this->api->get_table_info( 'post_type', 'attachment' ); 
  2455. elseif ( !in_array( $traverse[ 'type' ], $tableless_field_types ) ) 
  2456. $traverse[ 'table_info' ] = $this->api->get_table_info( $pod_data[ 'type' ], $pod_data[ 'name' ], $pod_data[ 'name' ], $pod_data ); 
  2457. elseif ( empty( $traverse[ 'table_info' ] ) || ( in_array( $traverse[ 'pick_object' ], $simple_tableless_objects ) && !empty( $traverse_recurse[ 'last_table_info' ] ) ) ) { 
  2458. if ( in_array( $traverse[ 'pick_object' ], $simple_tableless_objects ) && !empty( $traverse_recurse[ 'last_table_info' ] ) ) { 
  2459. $traverse[ 'table_info' ] = $traverse_recurse[ 'last_table_info' ]; 
  2460.  
  2461. if ( !empty( $traverse[ 'table_info' ][ 'meta_table' ] ) ) 
  2462. $meta_data_table = true; 
  2463. elseif ( !in_array( $traverse[ 'type' ], $tableless_field_types ) && !empty( $traverse_recurse[ 'last_table_info' ] ) && 0 == $traverse_recurse[ 'depth' ] ) 
  2464. $traverse[ 'table_info' ] = $traverse_recurse[ 'last_table_info' ]; 
  2465. else { 
  2466. if ( ! isset( $traverse[ 'pod' ] ) ) { 
  2467. $traverse[ 'pod' ] = null; 
  2468.  
  2469. $traverse[ 'table_info' ] = $this->api->get_table_info( $traverse[ 'pick_object' ], $traverse[ 'pick_val' ], null, $traverse[ 'pod' ], $traverse ); 
  2470.  
  2471. if ( isset( $this->traversal[ $traverse_recurse[ 'pod' ] ][ $traverse[ 'name' ] ] ) ) 
  2472. $traverse = array_merge( $traverse, (array) $this->traversal[ $traverse_recurse[ 'pod' ] ][ $traverse[ 'name' ] ] ); 
  2473.  
  2474. $traverse = apply_filters( 'pods_data_traverse', $traverse, compact( 'pod', 'fields', 'joined', 'depth', 'joined_id', 'params' ), $this ); 
  2475.  
  2476. if ( empty( $traverse ) ) 
  2477. return $joins; 
  2478.  
  2479. $traverse[ 'id' ] = (int) $traverse[ 'id' ]; 
  2480.  
  2481. if ( empty( $traverse[ 'id' ] ) ) 
  2482. $traverse[ 'id' ] = $field; 
  2483.  
  2484. $table_info = $traverse[ 'table_info' ]; 
  2485.  
  2486. $this->traversal[ $traverse_recurse[ 'pod' ] ][ $field ] = $traverse; 
  2487.  
  2488. $field_joined = $field; 
  2489.  
  2490. if ( 0 < $traverse_recurse[ 'depth' ] && 't' != $traverse_recurse[ 'joined' ] ) { 
  2491. if ( $meta_data_table && ( 'pick' != $traverse[ 'type' ] || !in_array( pods_var( 'pick_object', $traverse ), $simple_tableless_objects ) ) ) 
  2492. $field_joined = $traverse_recurse[ 'joined' ] . '_d'; 
  2493. else 
  2494. $field_joined = $traverse_recurse[ 'joined' ] . '_' . $field; 
  2495.  
  2496. $rel_alias = 'rel_' . $field_joined; 
  2497.  
  2498. if ( pods_v( 'search', $traverse_recurse[ 'params' ], false ) && empty( $traverse_recurse[ 'params' ]->filters ) ) { 
  2499. if ( 0 < strlen( pods_var( 'filter_' . $field_joined, 'get' ) ) ) { 
  2500. $val = absint( pods_var( 'filter_' . $field_joined, 'get' ) ); 
  2501.  
  2502. $search = "`{$field_joined}`.`{$table_info[ 'field_id' ]}` = {$val}"; 
  2503.  
  2504. if ( 'text' == $this->search_mode ) { 
  2505. $val = pods_var( 'filter_' . $field_joined, 'get' ); 
  2506.  
  2507. $search = "`{$field_joined}`.`{$traverse[ 'name' ]}` = '{$val}'"; 
  2508. elseif ( 'text_like' == $this->search_mode ) { 
  2509. $val = pods_sanitize( pods_sanitize_like( pods_var_raw( 'filter_' . $field_joined ) ) ); 
  2510.  
  2511. $search = "`{$field_joined}`.`{$traverse[ 'name' ]}` LIKE '%{$val}%'"; 
  2512.  
  2513. $this->search_where[] = " {$search} "; 
  2514.  
  2515. $the_join = null; 
  2516.  
  2517. $joined_id = $table_info[ 'field_id' ]; 
  2518. $joined_index = $table_info[ 'field_index' ]; 
  2519.  
  2520. if ( 'taxonomy' == $traverse[ 'type' ] ) { 
  2521. $rel_tt_alias = 'rel_tt_' . $field_joined; 
  2522.  
  2523. if ( $meta_data_table ) { 
  2524. $the_join = " 
  2525. LEFT JOIN `{$table_info[ 'pod_table' ]}` AS `{$field_joined}` ON 
  2526. `{$field_joined}`.`{$table_info[ 'pod_field_id' ]}` = `{$traverse_recurse[ 'rel_alias' ]}`.`{$traverse_recurse[ 'joined_id' ]}` 
  2527. "; 
  2528. else { 
  2529. $the_join = " 
  2530. LEFT JOIN `{$wpdb->term_relationships}` AS `{$rel_alias}` ON 
  2531. `{$rel_alias}`.`object_id` = `{$traverse_recurse[ 'joined' ]}`.`ID` 
  2532.  
  2533. LEFT JOIN `{$wpdb->term_taxonomy}` AS `{$rel_tt_alias}` ON 
  2534. `{$rel_tt_alias}`.`taxonomy` = '{$traverse[ 'name' ]}' 
  2535. AND `{$rel_tt_alias}`.`term_taxonomy_id` = `{$rel_alias}`.`term_taxonomy_id` 
  2536.  
  2537. LEFT JOIN `{$table_info[ 'table' ]}` AS `{$field_joined}` ON 
  2538. `{$field_joined}`.`{$table_info[ 'field_id' ]}` = `{$rel_tt_alias}`.`{$table_info[ 'field_id' ]}` 
  2539. "; 
  2540.  
  2541. // Override $rel_alias 
  2542. $rel_alias = $field_joined; 
  2543.  
  2544. $joined_id = $table_info[ 'field_id' ]; 
  2545. $joined_index = $table_info[ 'field_index' ]; 
  2546. elseif ( in_array( $traverse[ 'type' ], $tableless_field_types ) && ( 'pick' != $traverse[ 'type' ] || !in_array( pods_v( 'pick_object', $traverse ), $simple_tableless_objects ) ) ) { 
  2547. if ( pods_tableless() ) { 
  2548. $the_join = " 
  2549. LEFT JOIN `{$table_info[ 'meta_table' ]}` AS `{$rel_alias}` ON 
  2550. `{$rel_alias}`.`{$table_info[ 'meta_field_index' ]}` = '{$traverse[ 'name' ]}' 
  2551. AND `{$rel_alias}`.`{$table_info[ 'meta_field_id' ]}` = `{$traverse_recurse[ 'joined' ]}`.`{$traverse_recurse[ 'joined_id' ]}` 
  2552.  
  2553. LEFT JOIN `{$table_info[ 'meta_table' ]}` AS `{$field_joined}` ON 
  2554. `{$field_joined}`.`{$table_info[ 'meta_field_index' ]}` = '{$traverse[ 'name' ]}' 
  2555. AND `{$field_joined}`.`{$table_info[ 'meta_field_id' ]}` = CONVERT( `{$rel_alias}`.`{$table_info[ 'meta_field_value' ]}`, SIGNED ) 
  2556. "; 
  2557.  
  2558. $joined_id = $table_info[ 'meta_field_id' ]; 
  2559. $joined_index = $table_info[ 'meta_field_index' ]; 
  2560. elseif ( $meta_data_table ) { 
  2561. if ( $traverse[ 'id' ] !== $traverse[ 'pick_val' ] ) { 
  2562. // This must be a relationship 
  2563. $joined_id = 'related_item_id'; 
  2564. } else { 
  2565. $joined_id = $traverse_recurse[ 'joined_id' ]; 
  2566.  
  2567. $the_join = " 
  2568. LEFT JOIN `{$table_info['pod_table']}` AS `{$field_joined}` ON 
  2569. `{$field_joined}`.`{$table_info['pod_field_id']}` = `{$traverse_recurse['rel_alias']}`.`{$joined_id}` 
  2570. "; 
  2571. else { 
  2572. $the_join = " 
  2573. LEFT JOIN `@wp_podsrel` AS `{$rel_alias}` ON 
  2574. `{$rel_alias}`.`field_id` = {$traverse[ 'id' ]} 
  2575. AND `{$rel_alias}`.`item_id` = `{$traverse_recurse[ 'joined' ]}`.`{$traverse_recurse[ 'joined_id' ]}` 
  2576.  
  2577. LEFT JOIN `{$table_info[ 'table' ]}` AS `{$field_joined}` ON 
  2578. `{$field_joined}`.`{$table_info[ 'field_id' ]}` = `{$rel_alias}`.`related_item_id` 
  2579. "; 
  2580. elseif ( 'meta' == $pod_data[ 'storage' ] ) { 
  2581. if ( 
  2582. ( $traverse_recurse[ 'depth' ] + 2 ) == count( $traverse_recurse[ 'fields' ] ) 
  2583. && ( 'pick' != $traverse[ 'type' ] || !in_array( pods_var( 'pick_object', $traverse ), $simple_tableless_objects ) ) 
  2584. && $table_info[ 'meta_field_value' ] == $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] + 1 ] ) { 
  2585. $the_join = " 
  2586. LEFT JOIN `{$table_info[ 'meta_table' ]}` AS `{$field_joined}` ON 
  2587. `{$field_joined}`.`{$table_info[ 'meta_field_index' ]}` = '{$traverse[ 'name' ]}' 
  2588. AND `{$field_joined}`.`{$table_info[ 'meta_field_id' ]}` = `{$traverse_recurse[ 'joined' ]}`.`{$traverse_recurse[ 'joined_id' ]}` 
  2589. "; 
  2590.  
  2591. $table_info[ 'recurse' ] = false; 
  2592. else { 
  2593. $the_join = " 
  2594. LEFT JOIN `{$table_info[ 'meta_table' ]}` AS `{$field_joined}` ON 
  2595. `{$field_joined}`.`{$table_info[ 'meta_field_index' ]}` = '{$traverse[ 'name' ]}' 
  2596. AND `{$field_joined}`.`{$table_info[ 'meta_field_id' ]}` = `{$traverse_recurse[ 'joined' ]}`.`{$traverse_recurse[ 'joined_id' ]}` 
  2597. "; 
  2598.  
  2599. $joined_id = $table_info[ 'meta_field_id' ]; 
  2600. $joined_index = $table_info[ 'meta_field_index' ]; 
  2601.  
  2602. $traverse_recursive = array( 
  2603. 'pod' => pods_var_raw( 'name', pods_var_raw( 'pod', $table_info ) ),  
  2604. 'fields' => $traverse_recurse[ 'fields' ],  
  2605. 'joined' => $field_joined,  
  2606. 'depth' => ( $traverse_recurse[ 'depth' ] + 1 ),  
  2607. 'joined_id' => $joined_id,  
  2608. 'joined_index' => $joined_index,  
  2609. 'params' => $traverse_recurse[ 'params' ],  
  2610. 'rel_alias' => $rel_alias,  
  2611. 'last_table_info' => $table_info 
  2612. ); 
  2613.  
  2614. $the_join = apply_filters( 'pods_data_traverse_the_join', $the_join, $traverse_recurse, $traverse_recursive, $this ); 
  2615.  
  2616. if ( empty( $the_join ) ) 
  2617. return $joins; 
  2618.  
  2619. $joins[ $traverse_recurse[ 'pod' ] . '_' . $traverse_recurse[ 'depth' ] . '_' . $traverse[ 'id' ] ] = $the_join; 
  2620.  
  2621. if ( ( $traverse_recurse[ 'depth' ] + 1 ) < count( $traverse_recurse[ 'fields' ] ) && !empty( $traverse_recurse[ 'pod' ] ) && false !== $table_info[ 'recurse' ] ) 
  2622. $joins = array_merge( $joins, $this->traverse_recurse( $traverse_recursive ) ); 
  2623.  
  2624. return $joins; 
  2625.  
  2626. /** 
  2627. * Recursively join tables based on fields 
  2628. * @param array $fields Fields to recurse 
  2629. * @param null $all_fields (optional) If $fields is empty then traverse all fields, argument does not need to be passed 
  2630. * @param object $params (optional) Parameters from build() 
  2631. * @return array Array of joins 
  2632. */ 
  2633. function traverse ( $fields = null, $all_fields = null, $params = null ) { 
  2634. $joins = array(); 
  2635.  
  2636. if ( null === $fields ) 
  2637. $fields = $this->traverse_build( $all_fields, $params ); 
  2638.  
  2639. foreach ( (array) $fields as $field_group ) { 
  2640. $traverse_recurse = array( 
  2641. 'pod' => $this->pod,  
  2642. 'fields' => $fields,  
  2643. 'params' => $params,  
  2644. 'last_table_info' => $this->pod_data,  
  2645. 'joined_id' => $this->pod_data[ 'field_id' ],  
  2646. 'joined_index' => $this->pod_data[ 'field_index' ] 
  2647. ); 
  2648.  
  2649. if ( is_array( $field_group ) ) { 
  2650. $traverse_recurse[ 'fields' ] = $field_group; 
  2651.  
  2652. $joins = array_merge( $joins, $this->traverse_recurse( $traverse_recurse ) ); 
  2653. else { 
  2654. $joins = array_merge( $joins, $this->traverse_recurse( $traverse_recurse ) ); 
  2655. $joins = array_filter( $joins ); 
  2656.  
  2657. return $joins; 
  2658.  
  2659. $joins = array_filter( $joins ); 
  2660.  
  2661. return $joins; 
  2662.  
  2663. /** 
  2664. * Handle filters / actions for the class 
  2665. * @since 2.0 
  2666. */ 
  2667. private static function do_hook () { 
  2668. $args = func_get_args(); 
  2669.  
  2670. if ( empty( $args ) ) 
  2671. return false; 
  2672.  
  2673. $name = array_shift( $args ); 
  2674.  
  2675. return pods_do_hook( 'data', $name, $args ); 
  2676.  
  2677. /** 
  2678. * Get the complete sql 
  2679. * @since 2.0.5 
  2680. */ 
  2681. public function get_sql ( $sql ) { 
  2682. global $wpdb; 
  2683.  
  2684. if ( empty( $sql ) ) 
  2685. $sql = $this->sql; 
  2686.  
  2687. $sql = str_replace( array( '@wp_users', '@wp_' ), array( $wpdb->users, $wpdb->prefix ), $sql ); 
  2688.  
  2689. $sql = str_replace( '{prefix}', '@wp_', $sql ); 
  2690. $sql = str_replace( '{/prefix/}', '{prefix}', $sql ); 
  2691.  
  2692. return $sql;