PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/resources/common.php

http://tastemakers.googlecode.com/
PHP | 450 lines | 271 code | 58 blank | 121 comment | 44 complexity | e278af4a26288c47267e5571a7f5e45a MD5 | raw file
  1. <?php
  2. /* Crops an image to the desired width and height.
  3. */
  4. function make_thumbnail($image, $width, $height) {
  5. // Detect filetype
  6. if (preg_match('/jpg|jpeg/',$image)){
  7. //header('Content-Type: image/jpeg');
  8. $src = imagecreatefromjpeg($image);
  9. }
  10. if (preg_match('/png/',$image)){
  11. //header('Content-Type: image/png');
  12. $src = imagecreatefrompng($image);
  13. }
  14. // Get the size of the original image.
  15. list($src_width, $src_height) = getimagesize($image);
  16. $thumb = ImageCreateTrueColor($width,$height);
  17. imagecopyresampled($thumb, $src, 0, 0, 0, 0, $width, $height, $src_width, $src_height);
  18. return $thumb;
  19. }
  20. /* return the dominant category (useful when article is tagged with more than one category
  21. * $type can be 'text' or 'slug'
  22. */
  23. function dominant_category($type) {
  24. // get all the categories for this page or article
  25. // if category has no parents (ex: post is marked as reviews and show reviews)
  26. // assume that the child category (show reviews) is the article type
  27. $category = get_the_category();
  28. // if tagged with other categories, first look for features category, and remove if found
  29. if(count($category) > 1) {
  30. foreach($category as $key => $value) {
  31. // remove the 'features' category
  32. if ($value->cat_name == 'features')
  33. unset($category[$key]);
  34. }
  35. }
  36. // then, if still more than 1 category, look for parent category, and remove if found
  37. if(count($category) > 1) {
  38. foreach($category as $key => $value) {
  39. // categories with no parent return a 0
  40. if (!($value->category_parent))
  41. unset($category[$key]);
  42. }
  43. }
  44. if($type == 'text') {
  45. if(is_category() || is_home())
  46. return single_cat_title('', false);
  47. else {
  48. $first = reset($category);
  49. return $first->cat_name;
  50. }
  51. }
  52. if($type == 'slug') {
  53. $first = reset($category);
  54. return $first->slug;
  55. }
  56. }
  57. function secs_to_mins($secs) {
  58. $mins = floor($secs / 60);
  59. $secs = $secs % 60;
  60. if ($secs <10)
  61. $secs = '0'.$secs;
  62. return "$mins:$secs";
  63. }
  64. /* removes non alphanumeric characters that can trip up last.fm
  65. * encodes clean string to be put in url (ex: 'test & string' becomes 'test+string')
  66. * regex from http://stackoverflow.com/questions/4973089/cleaning-text-scraped-from-webpage-with-php-regex
  67. */
  68. function prep_search_string($string) {
  69. $string = preg_replace('/[^a-zA-Z0-9-\s\'\!\,\|\(\)\.\*\#\/\:]/', ' ', $string);
  70. $string = explode(' ', $string);
  71. foreach ($string as $key => $word) {
  72. if ($word == '' || $word[0] == '#')
  73. unset($string[$key]);
  74. }
  75. $string = implode(' ', $string);
  76. return urlencode($string);
  77. }
  78. /* case insensitive explode
  79. * from: http://theserverpages.com/php/manual/en/function.explode.php#37563
  80. */
  81. function explodei($separator, $string, $limit = false) {
  82. $len = strlen($separator);
  83. for ($i = 0; ; $i++) {
  84. if (($pos = stripos($string, $separator)) === false || ($limit !== false && $i > $limit - 2)) {
  85. $result[$i] = $string;
  86. break;
  87. }
  88. $result[$i] = substr($string, 0, $pos);
  89. $string = substr($string, $pos + $len);
  90. }
  91. return $result;
  92. }
  93. /* takes a standard show review title (ex: "Pretty Lights @ House of Blues 11.5.10")
  94. * splits title up into venue and artists and returns array of results
  95. * returns "artists" and "venue" keys, $venue is always a string and $artists is an array of the artists
  96. */
  97. function get_show_info($show_review_title) {
  98. // split review title string by the '@' character, artists before, venue after
  99. $show_review_title = explode('@', $show_review_title);
  100. // go through initial artist string and check for multiple artists
  101. // by checking for "with" and "and" keywords
  102. $artists = trim($show_review_title[0]);
  103. // checks if artists string has keyword "with"
  104. // we want to check for this first since less band names can contain the word "and"
  105. // we don't want to take a valid band name (ex: "florence and the machine") and ruin it by splitting it
  106. // it's likely that an author, if reviewing a show of two artists, one of which has an "and", will
  107. // conjoin the artist names with "with" (same goes for "w/")
  108. // (ex: "florence and the machine WITH radiohead" rather than "florence and the machine AND radiohead")
  109. if(stristr($artists, ' with '))
  110. $artists = explodei(' with ', $artists);
  111. else if(stristr($artists, ' w/ '))
  112. $artists = explodei(' w/ ', $artists);
  113. else if(stristr($artists, ' and '))
  114. $artists = explodei(' and ', $artists);
  115. else
  116. $artists = array($artists);
  117. // clean up artists strings
  118. foreach($artists as $key => $artist)
  119. $artists[$key] = trim($artist);
  120. // go through venue string and parse out date and whitespace
  121. $venue = trim($show_review_title[1]);
  122. // remove all non letters from venue name (namely the date)
  123. $venue = preg_replace('/[^a-z-\s]/i', '', $venue);
  124. return array('artists' => $artists, 'venue' => $venue);
  125. }
  126. /* helper to get_interview_artist() - must be performed for each condition. performs the actual
  127. * extraction
  128. */
  129. function extract_artist_name($string, $pre_wrapper) {
  130. $artist = stristr($string, $pre_wrapper);
  131. $artist = substr($artist, strlen($pre_wrapper));
  132. $artist = preg_replace('/[^a-z-\s]/i', '', $artist);
  133. return $artist;
  134. }
  135. /* much like get_show_info - must extract artist name from interview title to pass to artist object
  136. * enables interview articles to display last.fm mini bios of the interviewed artist
  137. *
  138. * have to parse the band name out of the title
  139. *
  140. * common band name wrappers:
  141. * - [name]: A Q&A
  142. * - A Q&A WITH: [name]
  143. * - A Q&A WITH [name]
  144. * - Q&A WITH: [name]
  145. * - Q&A WITH [name]
  146. * - AN INTERVIEW WITH: [name]
  147. * - AN INTERVIEW WITH [name]
  148. * - [name] INTERVIEW
  149. * - [name]: INTERVIEW
  150. * - JUST A TASTE OF... [name]
  151. */
  152. function get_interview_artist($interview_title) {
  153. // check first for broad category of title wrapper, then refine as we figure out in which
  154. // wrapper it's contained
  155. /*
  156. if (stristr($interview_title, 'q&a')) {
  157. // don't check for colon - will be filtered out by regex that removes non-letter characters
  158. if (stristr($interview_title, 'q&a with'))
  159. return extract_artist_name($interview_title, 'q&a with');
  160. // this one returns the part of the string before the search term
  161. // assumes format [name]: A Q&A
  162. // only works in php 5.3.0+
  163. if (stristr($interview_title, 'a q&a', true)) {
  164. $artist = stristr($interview_title, 'a q&a', true);
  165. $artist = preg_replace('/[^a-z-\s]/i', '', $artist);
  166. return $artist;
  167. }
  168. }
  169. */
  170. // needle "q&a" not working because of ampersand, this is an improvisation
  171. // just removes the "q&" in "q&a with"
  172. if (stristr($interview_title, 'a with'))
  173. return extract_artist_name($interview_title, 'a with');
  174. // this one returns the part of the string before the search term
  175. // assumes format [name]: A Q&A - uses "a q" of "a q&a" to avoid ampersand issue
  176. // only works in php 5.3.0+
  177. if (stristr($interview_title, 'a q', true)) {
  178. $artist = stristr($interview_title, 'a q', true);
  179. $artist = preg_replace('/[^a-z-\s]/i', '', $artist);
  180. return $artist;
  181. }
  182. if (stristr($interview_title, 'interview')) {
  183. // don't check for colon - will be filtered out by regex that removes non-letter characters
  184. if (stristr($interview_title, 'interview with'))
  185. return extract_artist_name($interview_title, 'interview with');
  186. // this one returns the part of the string before the search term
  187. // assumes format [name] INTERVIEW
  188. // only works in php 5.3.0+
  189. if (stristr($interview_title, 'interview', true)) {
  190. $artist = stristr($interview_title, 'interview', true);
  191. $artist = preg_replace('/[^a-z-\s]/i', '', $artist);
  192. return $artist;
  193. }
  194. }
  195. // there are only two interviews of this wrapper format - so not checking for variations
  196. // don't check for ellipses - will be filtered out by regex that removes non-letter characters
  197. if (stristr($interview_title, 'just a taste of'))
  198. return extract_artist_name($interview_title, 'just a taste of');
  199. }
  200. /* display a custom preview based on the article's category (photo articles display photoreel)
  201. * replaces the_excerpt as well as echoing the article title. handles everything inside .post
  202. */
  203. function category_preview() {
  204. $category = dominant_category('slug');
  205. switch($category) {
  206. case 'photos':
  207. // pass the photoset object the photoset url
  208. // expects the_content() to be photoset url (ex: http://www.flickr.com/photos/user/sets/999/)
  209. $photoset = new photoset(get_the_content());
  210. echo '<div class="twelvecol last">';
  211. echo '<h1>',
  212. '<img src="'.get_bloginfo('template_directory').'/images/icons/'.$category.'.png" /> ',
  213. '<a href="'.get_permalink().'">', the_title(), '</a>',
  214. '</h1>';
  215. $photoset->render_preview();
  216. echo '</div>';
  217. break;
  218. case 'cd-reviews':
  219. echo '<div class="twocol">';
  220. echo '<a href="'.get_permalink().'">'; the_post_thumbnail('medium'); echo '</a>';
  221. echo '</div>';
  222. echo '<div class="tencol last">';
  223. echo '<h1>',
  224. '<img src="'.get_bloginfo('template_directory').'/images/icons/'.$category.'.png" /> ',
  225. '<a href="'.get_permalink().'">', the_title(), '</a>',
  226. '</h1>';
  227. $custom_fields = get_post_custom();
  228. if ($custom_fields[rating])
  229. display_rating();
  230. the_excerpt();
  231. echo '</div>';
  232. break;
  233. case 'show-reviews':
  234. // parses the title to get the venu / artist(s) show info on last.fm
  235. $show_info = get_show_info(get_the_title());
  236. $venue = new venue($show_info['venue']);
  237. if ($venue->get_image()) {
  238. echo '<div class="twocol">';
  239. echo '<a href="'.get_permalink().'"><img src="'. $venue->get_image() .'" /></a>';
  240. echo '</div>';
  241. echo '<div class="tencol last">';
  242. echo '<h1>',
  243. '<img src="'.get_bloginfo('template_directory').'/images/icons/'.$category.'.png" /> ',
  244. '<a href="'.get_permalink().'">', the_title(), '</a>',
  245. '</h1>';
  246. the_excerpt();
  247. echo '</div>';
  248. } else {
  249. echo '<div class="twocol">';
  250. echo '<a href="'.get_permalink().'">'; the_post_thumbnail('medium'); echo '</a>';
  251. echo '</div>';
  252. echo '<div class="tencol last">';
  253. echo '<h1>',
  254. '<img src="'.get_bloginfo('template_directory').'/images/icons/'.$category.'.png" /> ',
  255. '<a href="'.get_permalink().'">', the_title(), '</a>',
  256. '</h1>';
  257. the_excerpt();
  258. echo '</div>';
  259. }
  260. break;
  261. case 'issues':
  262. echo '<div class="fourcol issue">';
  263. echo '<h1>',
  264. '<img src="'.get_bloginfo('template_directory').'/images/icons/'.$category.'.png" /> ',
  265. '<a href="'.get_permalink().'">', the_title(), '</a>',
  266. '</h1>';
  267. echo '<a href="'.get_permalink().'">'; the_post_thumbnail('issue-thumb'); echo '</a>';
  268. echo '</div>';
  269. break;
  270. case 'blog':
  271. echo '<div class="twocol">';
  272. echo '<p class="date"><span>'.get_the_date('M d').'</span> '.get_the_date('Y').'</p>';
  273. echo '</div>';
  274. echo '<div class="tencol last">';
  275. echo '<h1>',
  276. '<img src="'.get_bloginfo('template_directory').'/images/icons/'.$category.'.png" /> ',
  277. '<a href="'.get_permalink().'">', the_title(), '</a>',
  278. '</h1>';
  279. the_excerpt();
  280. echo '</div>';
  281. break;
  282. case 'features':
  283. echo '<div class="twocol">';
  284. echo '<a href="'.get_permalink().'">'; the_post_thumbnail('medium'); echo '</a>';
  285. echo '</div>';
  286. echo '<div class="tencol last">';
  287. echo '<h1><a href="'.get_permalink().'">', the_title(), '</a></h1>';
  288. the_excerpt();
  289. echo '</div>';
  290. break;
  291. default:
  292. echo '<div class="twocol">';
  293. echo '<a href="'.get_permalink().'">'; the_post_thumbnail('medium'); echo '</a>';
  294. echo '</div>';
  295. echo '<div class="tencol last">';
  296. echo '<h1>',
  297. '<img src="'.get_bloginfo('template_directory').'/images/icons/'.$category.'.png" /> ',
  298. '<a href="'.get_permalink().'">', the_title(), '</a>',
  299. '</h1>';
  300. the_excerpt();
  301. echo '</div>';
  302. }
  303. }
  304. /* reduces copy pasta in all the different single post templates
  305. * simply displays the author / photographer etc.
  306. */
  307. function display_credit() {
  308. $custom_fields = get_post_custom();
  309. $author = $custom_fields[author];
  310. if ($custom_fields[photographer])
  311. $photographer = $custom_fields[photographer];
  312. else
  313. $photographer = $custom_fields[photographers];
  314. if ($author[0])
  315. echo '<p>by '.$author[0].', published '.get_the_date('F jS Y').'</p>';
  316. if ($photographer[0])
  317. echo '<p>photos by '.$photographer[0].'</p>';
  318. }
  319. /* simply displays the rating of a cd review if the rating is
  320. * stored as a custom field
  321. * used on category and single views
  322. * assumes $custom_fields[rating] exists
  323. */
  324. function display_rating() {
  325. $custom_fields = get_post_custom();
  326. $rating = strtolower($custom_fields[rating][0]);
  327. switch($rating) {
  328. case 'tasty':
  329. echo '<p id="rating">Moldy | Stale | Edible | Fresh | <em>Tasty!</em></p>';
  330. break;
  331. case 'fresh':
  332. echo '<p id="rating">Moldy | Stale | Edible | <em>Fresh</em> | Tasty!</p>';
  333. break;
  334. case 'edible':
  335. echo '<p id="rating">Moldy | Stale | <em>Edible</em> | Fresh | Tasty!</p>';
  336. break;
  337. case 'stale':
  338. echo '<p id="rating">Moldy | <em>Stale</em> | Edible | Fresh | Tasty!</p>';
  339. break;
  340. case 'moldy':
  341. echo '<p id="rating"><em>Moldy</em> | Stale | Edible | Fresh | Tasty!</p>';
  342. break;
  343. default:
  344. echo '<p id="rating">No rating yet :(</p>';
  345. }
  346. }
  347. /* backup function that fetches old grooveshark custom field
  348. * incase a cd review has no auto fetched data
  349. */
  350. function display_old_player() {
  351. $custom_fields = get_post_custom();
  352. $player = $custom_fields[player];
  353. if ($player[0])
  354. echo '<div id="old_player">'.$player[0].'</div>';
  355. }
  356. /* accepts array of all author/post_id pairs and goes through them
  357. * and organizes them into a new array where each author shows up
  358. * only once, with a sub array of all their post_ids
  359. *
  360. * function expects wordpress functions to be available
  361. */
  362. function sort_authors($all_author_pairs) {
  363. // init vars used for keeping track of previous authors and
  364. // new author array
  365. $prev_author_name = null;
  366. $prev_author_key = null;
  367. $new_authors = array();
  368. // sort through all authors and add each author name to the new authors
  369. // array only once. then if they show up again, add only the post_id
  370. // to the already existing author in the new array
  371. // this produces a 2D array where each author has a subarray with all
  372. // their post_ids
  373. // the author is the array key, with the post_ids as a sub array
  374. foreach ($all_author_pairs as $key => $author_array) {
  375. $post_id = $author_array['post_id'];
  376. $author_name = $author_array['meta_value'];
  377. // remove anything in parenthesis
  378. $author_name = trim(preg_replace("/\([^\)]+\)/", "", $author_name));
  379. // remove any html tags
  380. $author_name = trim(preg_replace("/\<[^\>]+\>/", "", $author_name));
  381. if ($author_name == $prev_author_name)
  382. $new_authors[$prev_author_name][] = $post_id;
  383. else {
  384. $new_authors[$author_name][] = $post_id;
  385. $prev_author_name = $author_name;
  386. }
  387. }
  388. // resets the array keys
  389. $new_authors = array_merge($new_authors);
  390. return $new_authors;
  391. }
  392. ?>