M_DataMapper

The NextGEN Gallery M DataMapper class.

Defined (1)

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

/products/photocrati_nextgen/modules/datamapper/module.datamapper.php  
  1. class M_DataMapper extends C_Base_Module 
  2. function define($id = 'pope-module',  
  3. $name = 'Pope Module',  
  4. $description = '',  
  5. $version = '',  
  6. $uri = '',  
  7. $author = '',  
  8. $author_uri = '',  
  9. $context = FALSE) 
  10. parent::define( 
  11. 'photocrati-datamapper',  
  12. 'DataMapper',  
  13. 'Provides a database abstraction layer following the DataMapper pattern',  
  14. '0.10',  
  15. 'https://www.imagely.com/wordpress-gallery-plugin/nextgen-gallery/',  
  16. 'Imagely',  
  17. 'https://www.imagely.com' 
  18. ); 
  19.  
  20. C_Photocrati_Installer::add_handler($this->module_id, 'C_Datamapper_Installer'); 
  21.  
  22. function _register_adapters() 
  23. $this->get_registry()->add_adapter('I_Component_Factory', 'A_DataMapper_Factory'); 
  24.  
  25.  
  26. function _register_hooks() 
  27. add_filter('posts_request', array($this, 'set_custom_wp_query'), 50, 2); 
  28. add_filter('posts_fields', array($this, 'set_custom_wp_query_fields'), 50, 2); 
  29. add_filter('posts_where', array($this, 'set_custom_wp_query_where'), 50, 2); 
  30. add_filter('posts_groupby', array($this, 'set_custom_wp_query_groupby'), 50, 2); 
  31.  
  32.  
  33. /** 
  34. * Sets a custom SQL query for the WP_Query class, when the Custom Post 
  35. * DataMapper implementation is used 
  36. * @param string $sql 
  37. * @param WP_Query $wp_query 
  38. * @return string 
  39. */ 
  40. function set_custom_wp_query($sql, $wp_query) 
  41. if ($wp_query->get('datamapper')) { 
  42.  
  43. // Set the custom query 
  44. if (($custom_sql = $wp_query->get('custom_sql'))) { 
  45. $sql = $custom_sql; 
  46.  
  47. // Perhaps we're to initiate a delete query instead? 
  48. elseif ($wp_query->get('is_delete')) { 
  49. $sql = preg_replace("/^SELECT.*FROM/i", "DELETE FROM", $sql); 
  50.  
  51. if ($wp_query->get('debug')) var_dump($sql); 
  52.  
  53. return $sql; 
  54.  
  55. /** 
  56. * Sets custom fields to select from the database 
  57. * @param string $fields 
  58. * @param WP_Query $wp_query 
  59. * @return string 
  60. */ 
  61. function set_custom_wp_query_fields($fields, $wp_query) 
  62. if ($wp_query->get('datamapper')) { 
  63. if (($custom_fields = $wp_query->get('fields')) && $custom_fields != 'ids') { 
  64. $fields = $custom_fields; 
  65.  
  66. return $fields; 
  67.  
  68.  
  69. /** 
  70. * Sets custom where clauses for a query 
  71. * @param string $where 
  72. * @param WP_Query $wp_query 
  73. * @return string 
  74. */ 
  75. function set_custom_wp_query_where($where, $wp_query) 
  76. if ($wp_query->get('datamapper')) { 
  77. $this->add_post_title_where_clauses($where, $wp_query); 
  78. $this->add_post_name_where_clauses($where, $wp_query); 
  79.  
  80. return $where; 
  81.  
  82.  
  83. /** 
  84. * Adds additional group by clauses to the SQL query 
  85. * @param string $groupby 
  86. * @param WP_Query $wp_query 
  87. * @return string 
  88. */ 
  89. function set_custom_wp_query_groupby($groupby, $wp_query) 
  90. $retval = $groupby; 
  91. $group_by_columns = $wp_query->get('group_by_columns'); 
  92. if ($group_by_columns) { 
  93. $retval = str_replace('GROUP BY', '', $retval); 
  94. $columns = explode(', ', $retval); 
  95. foreach (array_reverse($columns) as $column) { 
  96. array_unshift($group_by_columns, trim($column)); 
  97. $retval = "GROUP BY ".implode(', ', $group_by_columns); 
  98. // Not all mysql servers allow access to create temporary tables which are used when doing GROUP BY 
  99. // statements; this can potentially ruin basic queries. If no group_by_columns is set AND the query originates 
  100. // within the datamapper we strip the "GROUP BY" clause entirely in this filter. 
  101. else if ($wp_query->get('datamapper')) { 
  102. $retval = ''; 
  103. return $retval; 
  104.  
  105.  
  106. /** 
  107. * Formats the value of used in a WHERE IN 
  108. * SQL clause for use in the WP_Query where clause 
  109. * @param string|array $values 
  110. * @return string 
  111. */ 
  112. function format_where_in_value($values) 
  113. if (is_string($values) && strpos($values, ', ') !== FALSE) 
  114. $values = explode(", ", $values); 
  115. elseif (!is_array($values)) 
  116. $values = array($values); 
  117.  
  118. // Quote the titles 
  119. foreach ($values as $index => $value) { 
  120. $values[$index] = "'{$value}'"; 
  121.  
  122. return implode(', ', $values); 
  123.  
  124.  
  125. /** 
  126. * Adds post_title to the where clause 
  127. * @param string $where 
  128. * @param WP_Query $wp_query 
  129. * @return string 
  130. */ 
  131. function add_post_title_where_clauses(&$where, &$wp_query) 
  132. global $wpdb; 
  133.  
  134. // Handle post_title query var 
  135. if (($titles = $wp_query->get('post_title'))) { 
  136. $titles = $this->format_where_in_value($titles); 
  137. $where .= " AND {$wpdb->posts}.post_title IN ({$titles})"; 
  138.  
  139. // Handle post_title_like query var 
  140. elseif (($value = $wp_query->get('post_title__like'))) { 
  141. $where .= " AND {$wpdb->posts}.post_title LIKE '{$value}'"; 
  142.  
  143.  
  144. /** 
  145. * Adds post_name to the where clause 
  146. * @param type $where 
  147. * @param type $wp_query 
  148. */ 
  149. function add_post_name_where_clauses(&$where, &$wp_query) 
  150. global $wpdb; 
  151.  
  152. if (($name = $wp_query->get('page_name__like'))) { 
  153. $where .= " AND {$wpdb->posts}.post_name LIKE '{$name}'"; 
  154. elseif (($names = $wp_query->get('page_name__in'))) { 
  155. $names = $this->format_where_in_value($names); 
  156. $where .= " AND {$wpdb->posts}.post_name IN ({$names})"; 
  157.  
  158. /** 
  159. * Unserializes data using our proprietary format 
  160. * @param string $value 
  161. * @return mixed 
  162. */ 
  163. static function unserialize($value) 
  164. $retval = NULL; 
  165. if (is_string($value)) 
  166. $retval = stripcslashes($value); 
  167.  
  168. if (strlen($value) > 1) 
  169. // We can't always rely on base64_decode() or json_decode() to return FALSE as their documentation 
  170. // claims so check if $retval begins with a: as that indicates we have a serialized PHP object. 
  171. if (strpos($retval, 'a:') === 0) 
  172. $er = error_reporting(0); 
  173. $retval = unserialize($value); 
  174. error_reporting($er); 
  175. else { 
  176. // We use json_decode() here because PHP's unserialize() is not Unicode safe. 
  177. $retval = json_decode(base64_decode($retval), TRUE); 
  178.  
  179. return $retval; 
  180.  
  181. /** 
  182. * Serializes the data 
  183. * @param mixed $value 
  184. * @return string 
  185. */ 
  186. static function serialize($value) 
  187. //Using json_encode here because PHP's serialize is not Unicode safe 
  188. return base64_encode(json_encode($value)); 
  189.  
  190. function get_type_list() 
  191. return array( 
  192. 'A_Datamapper_Factory' => 'adapter.datamapper_factory.php',  
  193. 'C_Datamapper_Installer' => 'class.datamapper_installer.php',  
  194. 'C_Datamapper' => 'class.datamapper.php',  
  195. 'C_Custompost_Datamapper_Driver' => 'class.custompost_datamapper_driver.php',  
  196. 'C_Customtable_Datamapper_Driver' => 'class.customtable_datamapper_driver.php',  
  197. 'C_Datamapper_Driver_Base' => 'class.datamapper_driver_base.php',  
  198. 'C_Datamapper_Model' => 'class.datamapper_model.php',  
  199. 'M_Datamapper' => 'module.datamapper.php' 
  200. );