/static/wp-content/plugins/my-calendar/my-calendar-limits.php

https://github.com/elleeott/WPOC-boilerplate · PHP · 269 lines · 260 code · 6 blank · 3 comment · 138 complexity · 6efbdf07710dbad0147b35e61553708a MD5 · raw file

  1. <?php
  2. function mc_select_category($category, $type='event', $group='events' ) {
  3. $category = urldecode($category);
  4. global $wpdb;
  5. $mcdb = $wpdb;
  6. if ( get_option( 'mc_remote' ) == 'true' && function_exists('mc_remote_db') ) { $mcdb = mc_remote_db(); }
  7. $select_category = '';
  8. $data = ($group=='category')?'category_id':'event_category';
  9. if ( isset( $_GET['mcat'] ) ) { $category = $_GET['mcat']; }
  10. if ( preg_match('/^all$|^all,|,all$|,all,/i', $category) > 0 ) {
  11. return '';
  12. } else {
  13. if ( strpos( $category, "|" ) || strpos( $category, "," ) ) {
  14. if ( strpos($category, "|" ) ) {
  15. $categories = explode( "|", $category );
  16. } else {
  17. $categories = explode( ",", $category );
  18. }
  19. $numcat = count($categories);
  20. $i = 1;
  21. foreach ($categories as $key) {
  22. if ( is_numeric($key) ) {
  23. $key = (int) $key;
  24. if ($i == 1) { $select_category .= ($type=='all')?" WHERE (":' ('; }
  25. $select_category .= " $data = $key";
  26. if ($i < $numcat) {
  27. $select_category .= " OR ";
  28. } else if ($i == $numcat) {
  29. $select_category .= ($type=='all')?") ":' ) AND';
  30. }
  31. $i++;
  32. } else {
  33. $key = esc_sql(trim($key));
  34. $cat = $mcdb->get_row("SELECT category_id FROM " . my_calendar_categories_table() . " WHERE category_name = '$key'");
  35. $category_id = $cat->category_id;
  36. if ($i == 1) { $select_category .= ($type=='all')?" WHERE (":' ('; }
  37. $select_category .= " $data = $category_id";
  38. if ($i < $numcat) {
  39. $select_category .= " OR ";
  40. } else if ($i == $numcat) {
  41. $select_category .= ($type=='all')?") ":' ) AND';
  42. }
  43. $i++;
  44. }
  45. }
  46. } else {
  47. if ( is_numeric( $category ) ) {
  48. $select_category = ($type=='all')?" WHERE $data = $category":" event_category = $category AND";
  49. } else {
  50. $cat = $mcdb->get_row("SELECT category_id FROM " . my_calendar_categories_table() . " WHERE category_name = '$category'");
  51. if ( is_object($cat) ) {
  52. $category_id = $cat->category_id;
  53. $select_category = ($type=='all')?" WHERE $data = $category_id":" $data = $category_id AND";
  54. } else {
  55. $select_category = '';
  56. }
  57. }
  58. }
  59. return $select_category;
  60. }
  61. }
  62. function mc_select_author( $author, $type='event' ) {
  63. $author = urldecode($author);
  64. $key = '';
  65. if ( $author == '' || $author == 'all' || $author == 'default' || $author == null ) { return; }
  66. global $wpdb;
  67. $mcdb = $wpdb;
  68. if ( get_option( 'mc_remote' ) == 'true' && function_exists('mc_remote_db') ) { $mcdb = mc_remote_db(); }
  69. $select_author = '';
  70. $data = 'event_author';
  71. if ( isset( $_GET['mc_auth'] ) ) { $author = $_GET['mc_auth']; }
  72. if ( preg_match( '/^all$|^all,|,all$|,all,/i', $author ) > 0 ) {
  73. return '';
  74. } else {
  75. if ( strpos( $author, "|" ) || strpos( $author, "," ) ) {
  76. if ( strpos($author, "|" ) ) {
  77. $authors = explode( "|", $author );
  78. } else {
  79. $authors = explode( ",", $author );
  80. }
  81. $numauth = count($authors);
  82. $i = 1;
  83. foreach ($authors as $key) {
  84. if ( is_numeric($key) ) {
  85. $key = (int) $key;
  86. if ($i == 1) { $select_author .= ($type=='all')?" WHERE (":' ('; }
  87. $select_author .= " $data = $key";
  88. if ($i < $numauth) {
  89. $select_author .= " OR ";
  90. } else if ($i == $numauth) {
  91. $select_author .= ($type=='all')?") ":' ) AND';
  92. }
  93. $i++;
  94. } else {
  95. $key = esc_sql(trim($key));
  96. $author = get_user_by( 'login', $key ); // get author by username
  97. $author_id = $author->ID;
  98. if ($i == 1) { $select_author .= ($type=='all')?" WHERE (":' ('; }
  99. $select_author .= " $data = $author_id";
  100. if ($i < $numauth) {
  101. $select_author .= " OR ";
  102. } else if ($i == $numauth) {
  103. $select_author .= ($type=='all')?") ":' ) AND';
  104. }
  105. $i++;
  106. }
  107. }
  108. } else {
  109. if ( is_numeric( $author ) ) {
  110. $select_author = ($type=='all')?" WHERE $data = $author":" event_author = $author AND";
  111. } else {
  112. $author = esc_sql(trim($author));
  113. $author = get_user_by( 'login', $author ); // get author by username
  114. if ( is_object($author) ) {
  115. $author_id = $author->ID;
  116. $select_author = ($type=='all')?" WHERE $data = $author_id":" $data = $author_id AND";
  117. } else {
  118. $select_author = '';
  119. }
  120. }
  121. }
  122. return $select_author;
  123. }
  124. }
  125. function mc_select_host( $host, $type='event' ) {
  126. $host = urldecode($host);
  127. $key = '';
  128. if ( $host == '' || $host == 'all' || $host == 'default' || $host == null ) { return; }
  129. global $wpdb;
  130. $mcdb = $wpdb;
  131. if ( get_option( 'mc_remote' ) == 'true' && function_exists('mc_remote_db') ) { $mcdb = mc_remote_db(); }
  132. $select_author = '';
  133. $data = 'event_host';
  134. if ( isset( $_GET['mc_auth'] ) ) { $host = $_GET['mc_host']; }
  135. if ( preg_match( '/^all$|^all,|,all$|,all,/i', $host ) > 0 ) {
  136. return '';
  137. } else {
  138. if ( strpos( $host, "|" ) || strpos( $host, "," ) ) {
  139. if ( strpos($host, "|" ) ) {
  140. $hosts = explode( "|", $host );
  141. } else {
  142. $hosts = explode( ",", $host );
  143. }
  144. $numhost = count($hosts);
  145. $i = 1;
  146. foreach ($hosts as $key) {
  147. if ( is_numeric($key) ) {
  148. $key = (int) $key;
  149. if ($i == 1) { $select_host .= ($type=='all')?" WHERE (":' ('; }
  150. $select_host .= " $data = $key";
  151. if ($i < $numhost) {
  152. $select_host .= " OR ";
  153. } else if ($i == $numhost) {
  154. $select_host .= ($type=='all')?") ":' ) AND';
  155. }
  156. $i++;
  157. } else {
  158. $key = esc_sql(trim($key));
  159. $host = get_user_by( 'login', $key ); // get host by username
  160. $host_id = $host->ID;
  161. if ($i == 1) { $select_host .= ($type=='all')?" WHERE (":' ('; }
  162. $select_host .= " $data = $host_id";
  163. if ($i < $numhost) {
  164. $select_host .= " OR ";
  165. } else if ($i == $numhost) {
  166. $select_host .= ($type=='all')?") ":' ) AND';
  167. }
  168. $i++;
  169. }
  170. }
  171. } else {
  172. if ( is_numeric( $host ) ) {
  173. $select_host = ($type=='all')?" WHERE $data = $host":" event_host = $host AND";
  174. } else {
  175. $host = esc_sql(trim($host));
  176. $host = get_user_by( 'login', $host ); // get author by username
  177. if ( is_object($host) ) {
  178. $host_id = $host->ID;
  179. $select_host = ($type=='all')?" WHERE $data = $host_id":" $data = $host_id AND";
  180. } else {
  181. $select_host = '';
  182. }
  183. }
  184. }
  185. return $select_host;
  186. }
  187. }
  188. function mc_limit_string($type='',$ltype='',$lvalue='') {
  189. global $user_ID;
  190. $user_settings = get_option('mc_user_settings');
  191. $limit_string = "";
  192. if ( get_option('mc_user_settings_enabled') == 'true' && $user_settings['my_calendar_location_default']['enabled'] == 'on' || isset($_GET['loc']) && isset($_GET['ltype']) || ( $ltype !='' && $lvalue != '' ) ) {
  193. if ( !isset($_GET['loc']) && !isset($_GET['ltype']) ) {
  194. if ( $ltype == '' && $lvalue == '' ) {
  195. if ( is_user_logged_in() ) {
  196. get_currentuserinfo();
  197. $current_settings = get_user_meta( $user_ID, 'my_calendar_user_settings' );
  198. $current_location = $current_settings['my_calendar_location_default'];
  199. $location = get_option('mc_location_type');
  200. }
  201. } else if ( $ltype !='' && $lvalue != '' ) {
  202. $location = $ltype;
  203. $current_location = esc_sql( $lvalue );
  204. }
  205. } else {
  206. $current_location = urldecode($_GET['loc']);
  207. $location = urldecode($_GET['ltype']);
  208. }
  209. switch ($location) {
  210. case "name":$location_type = "event_label";
  211. break;
  212. case "city":$location_type = "event_city";
  213. break;
  214. case "state":$location_type = "event_state";
  215. break;
  216. case "zip":$location_type = "event_postcode";
  217. break;
  218. case "country":$location_type = "event_country";
  219. break;
  220. case "region":$location_type = "event_region";
  221. break;
  222. default:$location_type = $location;
  223. break;
  224. }
  225. if ($current_location != 'all' && $current_location != '') {
  226. $limit_string = "$location_type='$current_location' AND";
  227. //$limit_string .= ($type=='all')?' AND':"";
  228. }
  229. }
  230. if ( $limit_string != '' ) {
  231. if ( isset($_GET['loc2']) && isset($_GET['ltype2']) ) {
  232. $limit_string .= mc_secondary_limit( $_GET['ltype2'],$_GET['loc2'] );
  233. }
  234. }
  235. return $limit_string;
  236. }
  237. // set up a secondary limit on location
  238. function mc_secondary_limit($ltype='',$lvalue='') {
  239. $limit_string = "";
  240. $current_location = urldecode( $lvalue );
  241. $location = urldecode( $ltype );
  242. switch ($location) {
  243. case "name":$location_type = "event_label";
  244. break;
  245. case "city":$location_type = "event_city";
  246. break;
  247. case "state":$location_type = "event_state";
  248. break;
  249. case "zip":$location_type = "event_postcode";
  250. break;
  251. case "country":$location_type = "event_country";
  252. break;
  253. case "region":$location_type = "event_region";
  254. break;
  255. default:$location_type = "event_label";
  256. break;
  257. }
  258. if ($current_location != 'all' && $current_location != '') {
  259. $limit_string = " $location_type='$current_location' AND ";
  260. //$limit_string .= ($type=='all')?' AND':"";
  261. }
  262. return $limit_string;
  263. }