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