Advertisement
Guest User

Untitled

a guest
Apr 30th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.69 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Petfinder Listings
  4. Plugin URI: http://www.unboxinteractive.com/wordpress/petfinder-listings-plugin/
  5. Description: The Petfinder Listings plugin takes advantage of the Petfinder API and can be integrated into your site without coding.
  6. Version: 1.0.10
  7. Author: Bridget Wessel
  8. Author URI: http://www.unboxinteractive.com/
  9. License: GPLv2
  10. */
  11.  
  12. /* Copyright 2014 Bridget Wessel (email : bridget@unboxinteractive.com)
  13.  
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License, version 2, as
  16. published by the Free Software Foundation.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27.  
  28. /********** Add default styles ************/
  29.  
  30. ini_set("allow_url_fopen", true);
  31. ini_set("allow_url_include", true);
  32.  
  33.  
  34. function petfinder_listings_styles(){
  35. wp_register_style('petfinder-listings-style', plugins_url( 'petfinder.css', __FILE__ ));
  36. wp_enqueue_style('petfinder-listings-style');
  37. }
  38. add_action('init', 'petfinder_listings_styles');
  39.  
  40.  
  41. /********** Add js to switch out photos ***********/
  42. function petfinder_listings_scripts() {
  43. if (!is_admin()){
  44. wp_register_script( 'petfinder_listings_scripts', plugins_url( '/petfinder.js', __FILE__ ));
  45. wp_enqueue_script( 'petfinder_listings_scripts' );
  46. }
  47. }
  48.  
  49. add_action('wp_enqueue_scripts', 'petfinder_listings_scripts', 10, 1);
  50.  
  51. //add defaults to an array
  52. $petf_options = array(
  53. 'apikey' => 'default',
  54. 'shelter_id' => 'default',
  55. 'thumbnail' => 'pnt',
  56. 'large_image' => 'pn',
  57. 'powered_by' => 'Yes'
  58. );
  59.  
  60. include( dirname(__FILE__) . '/featuredpet-widget.php' );
  61.  
  62. //add settings to database if not already set
  63.  
  64. add_option('Petfinder-Listings', $petf_options);
  65. $petf_options = get_option('Petfinder-Listings');
  66.  
  67. if(!isset($petf_options["powered_by"])){
  68. $petf_options["powered_by"] = "Yes";
  69. }
  70.  
  71. // create custom plugin settings menu
  72.  
  73. add_action('admin_menu', 'petf_admin_page');
  74. add_action( 'widgets_init', create_function('', 'return register_widget("Petfinder_Listings_Featured_Pet");') );
  75.  
  76. function petf_admin_page() {
  77. add_options_page('Petfinder Listings Plugin Settings', 'Petfinder Listings', 'manage_options', 'petf', 'petf_options_page');
  78. }
  79.  
  80. // Add Settings to Plugin Menu
  81. $pluginName = plugin_basename( __FILE__ );
  82.  
  83. add_filter( 'plugin_action_links_' . $pluginName, 'petf_pluginActions' );
  84.  
  85. function petf_pluginActions( $links ) {
  86. $settings_link =
  87. '<a href="' . get_admin_url( null, 'options-general.php' ) . "?page=petf".'">' .
  88. __('Settings') . '</a>';
  89. array_unshift( $links, $settings_link );
  90. return $links;
  91. }
  92.  
  93.  
  94.  
  95. //write settings page
  96. function petf_options_page() {
  97. global $petf_options;
  98. if(isset($_POST['save_changes'])) {
  99. check_admin_referer('petfinder-listings-update_settings');
  100.  
  101. $petf_options['apikey'] = trim($_POST['apikey']);
  102. $petf_options['shelter_id'] = trim($_POST['shelter_id']);
  103. $petf_options['thumbnail'] = trim($_POST['thumbnail']);
  104. $petf_options['large_image'] = trim($_POST['large_image']);
  105. $petf_options['powered_by'] = trim($_POST['powered_by']);
  106.  
  107. update_option('Petfinder-Listings', $petf_options);
  108.  
  109. echo "<div class=\"error\">Your changes have been saved successfully!</div>";
  110. }
  111. ?>
  112. <div class="wrap">
  113.  
  114. <h2>Petfinder Settings</h2>
  115.  
  116. <form name="petfinder-options" action="options-general.php?page=petf" method="post">
  117. <?php
  118. if ( function_exists( 'wp_nonce_field' ) )
  119. wp_nonce_field( 'petfinder-listings-update_settings' ); ?>
  120.  
  121. <table class="form-table">
  122. <tr valign="top">
  123. <th scope="row">Your Petfinder API Key (go <a href="http://www.petfinder.com/developers/api-docs" target="_blank">here</a> to get one)</th>
  124. <td><input type="text" name="apikey" value="<?php echo $petf_options["apikey"] ?>" /></td>
  125. </tr>
  126.  
  127. <tr valign="top">
  128. <th scope="row">Shelter ID</th>
  129. <td><input type="text" name="shelter_id" value="<?php echo $petf_options["shelter_id"] ?>" /></td>
  130. </tr>
  131.  
  132. <tr valign="top">
  133. <th scope="row">Thumbnail Size (select fixed side size, other side varies depending on ratio of original photo)</th>
  134. <td><select name="thumbnail">
  135. <option value="t" <?php echo $petf_options["thumbnail"] == "t" ? "selected='selected'" : ""?>>scaled to 50 pixels tall</option>
  136. <option value="pnt" <?php echo $petf_options["thumbnail"] == "pnt" ? "selected='selected'" : ""?>>scaled to 60 pixels wide</option>
  137. <option value="fpm" <?php echo $petf_options["thumbnail"] == "fpm" ? "selected='selected'" : ""?>>scaled to 95 pixels wide</option>
  138. </select></td>
  139. </tr>
  140.  
  141. <tr valign="top">
  142. <th scope="row">Large Image Size</th>
  143. <td><select name="large_image">
  144. <option value="x" <?php echo $petf_options["large_image"] == "x" ? "selected='selected'" : "" ?>>original, up to 500x500</option>
  145. <option value="pn" <?php echo $petf_options["large_image"] == "pn" ? "selected='selected'" : "" ?>>up to 320x250</option>
  146. </select></td>
  147. </tr>
  148.  
  149. <tr>
  150. <th scope="row">Include Powered by Petfinder at bottom of page. Petfinder provides a great, free service for shelters and it is highly recommended you leave this on your Petfinder pages.</th>
  151. <td><input type="radio" value="Yes" name="powered_by" <?php echo ( $petf_options["powered_by"] == "Yes" )? "checked=\"checked\"" : "" ?>>Yes <input type="radio" value="No" name="powered_by" <?php echo ($petf_options["powered_by"] == "No")? "checked=\"checked\"" : "" ?>>No</td>
  152. </tr>
  153.  
  154. <tr>
  155. <th colspan="2"><p>After saving, create a page with the shortcode [shelter_list] in the content. View this page to see your listings.</p>
  156. <p>You can also add the following options to your shortcode<br />[shelter_list shelter_id="WI185" breed="Italian Greyhound" count=75 animal="dog" include_info="no" css_class="igs" contact="Barb Smith"] </p></th>
  157. </tr>
  158.  
  159.  
  160.  
  161. </table>
  162.  
  163. <p class="submit">
  164. <input type="hidden" name="save_changes" value="1" />
  165. <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
  166. </p>
  167.  
  168. </form>
  169. </div>
  170.  
  171. <?php } // end function petf_options_page
  172.  
  173.  
  174.  
  175.  
  176. /*
  177. For use later - need to convert SimpleXMLElement to array and then change all display data to use array instead of object
  178.  
  179. function sort_by_date($petA, $petB) {
  180. if ( (string)$petA->lastUpdate == (string)$petB->lastUpdate ){
  181. return 0;
  182. }else if ( (string)$petA->lastUpdate < (string)$petB->lastUpdate ){
  183. return -1;
  184. }else{
  185. return 1;
  186. }
  187. }*/
  188.  
  189. add_shortcode('shelter_list','petf_shelter_list');
  190.  
  191. /** Using shortcode shelter_list grab all animals for this shelter.
  192. * Available Options: shelter_id (if want to list animals from shelter not set in Petfinder Listings settings), breed, count, animal, include_info, css_class and contact.
  193. * Breed can be inclusive or exclusive by adding ! before breed name. ***/
  194. function petf_shelter_list( $atts ) {
  195.  
  196. global $petf_options;
  197.  
  198. extract( shortcode_atts( array(
  199. 'shelter_id' => $petf_options['shelter_id'],
  200. 'breed' => '',
  201. 'count' => 75,
  202. 'animal' => '',
  203. 'include_info' => 'yes',
  204. 'css_class' => 'pets',
  205. 'contact' => '',
  206. 'include_mixes' => 'no',
  207. 'status' => 'A',
  208. 'sort_by_date' => false
  209. ), $atts, 'shelter_list' ) );
  210. //get the xml
  211. $response = wp_remote_get( "http://api.petfinder.com/shelter.getPets?key=" . trim($petf_options["apikey"]) . "&count=" . trim(intval($count)) . "&id=" . trim($shelter_id) . "&status=" . trim($status) . "&output=full", array("timeout" => 120) );
  212. //echo "http://api.petfinder.com/shelter.getPets?key=" . trim($petf_options["apikey"]) . "&count=" . trim(intval($count)) . "&id=" . trim($shelter_id) . "&status=" . trim($status) . "&output=full";
  213. $xml = simplexml_load_string(wp_remote_retrieve_body($response));
  214.  
  215. //print_r($xml);
  216. if( $xml->header->status->code == "100"){
  217. $output_buffer = "";
  218. if( count( $xml->pets->pet ) > 0 ){
  219. $output_buffer .= "<div class=\"" . $css_class . "\">";
  220. $pets = $xml->pets->pet;
  221. /*var_dump($pets);
  222. if($sort_by_date){
  223. usort($pets, 'sort_by_date');
  224. }*/
  225.  
  226.  
  227. $numpets = count($pets)-1;
  228. for( $inc = $numpets; $inc >= 0; $inc-- ){
  229.  
  230. $dog = $pets[$inc];
  231. $bigfile = "";
  232.  
  233. $continue = false;
  234. if(($animal == "" || strtolower( $dog->animal ) == strtolower( $animal )) && ( $contact == "" || strtolower( $dog->contact->name ) == strtolower( $contact ) ) ){
  235. if( $breed != "" ){
  236. foreach( $dog->breeds->breed as $this_breed ){
  237. if( strpos( $breed, "!" ) === false ){
  238. if( strtolower($breed) == strtolower( $this_breed ) && ( $dog->mix == "no" || ( $dog->mix == "yes" && $include_mixes == "yes" ) ) ){
  239. $continue = true;
  240. break; //looking for specific breed and it was found
  241. }
  242. }else{
  243. if( strtolower( str_replace( "!", "", $breed ) ) == strtolower( $this_breed ) && $dog->mix == "no" ){
  244. break; //looking for other breeds and this breed was found
  245. }else{
  246. $continue = true;
  247. }
  248. }
  249. }
  250. }else{
  251. $continue = true;
  252. }
  253. if( $continue ){
  254. $output_buffer .= "<div class=\"dog " . $dog->animal . "\"><div class=\"name\"><a name=\"" . $dog->id . "\">". $dog->name . "</a></div>";
  255. $output_buffer .= "<div class=\"images\">";
  256. $firsttime = true;
  257. if(count($dog->media->photos) > 0){
  258. foreach( $dog->media->photos->photo as $photo ){
  259. //$output_buffer .= $photo["size"];
  260. if( $photo['size'] == $petf_options["large_image"] ){
  261. if( $firsttime ){
  262. $output_buffer .= "<img class=\"petfinder-big-img\" id=\"img_". $dog->id . "\" src=\"" . $photo . "\" alt=\" . $dog->name . \">";
  263. }
  264. $bigfile = $photo;
  265. }
  266. if( $photo['size'] == $petf_options["thumbnail"] ){
  267. if( $firsttime ){
  268. $output_buffer .= "<div class=\"petfinder-thumbnails\">";
  269. $firsttime = false;
  270. }
  271. $output_buffer .= "<img class=\"petfinder-thumbmail\" onclick=\"switchbigimg('img_" . $dog->id . "', '" . $bigfile . "');return false;\" src=\"" . $photo . "\" alt=\" . $dog->name . \">";
  272. }
  273. }
  274. }
  275. if( !$firsttime ){
  276. //not first time so there are thumbnails to wrap up in a div. Closing petfinder-thumbnails
  277. $output_buffer .= "</div>";
  278. }
  279. if($include_info == "yes"){
  280.  
  281. $output_buffer .= "<ul class=\"pet-options\">";
  282.  
  283. $firsttime = true;
  284. foreach( $dog->breeds->breed as $this_breed ){
  285. if($firsttime){
  286. $output_buffer .= "<li class=\"breeds\">";
  287. $firsttime = false;
  288. }else{
  289. $output_buffer .= ", ";
  290. }
  291. $output_buffer .= $this_breed;
  292. }
  293. if(!$firsttime){
  294. $output_buffer .= "</li>";
  295. }
  296.  
  297. $icons = "";
  298. foreach( $dog->options->option as $option ){
  299. switch($option){
  300. case "noCats":
  301. $icons .= "<img src=\"http://www.petfinder.com/images/search/no-cat.gif\" width=\"36\" height=\"21\" alt=\"Prefers home without cats\" title=\"Prefers home without cats\" />";
  302. break;
  303. case "noDogs":
  304. $icons .= "<img src=\"http://www.petfinder.com/images/search/no-dogs.gif\" width=\"41\" height=\"21\" alt=\"Prefers home without dogs\" title=\"Prefers home without dogs\" />";
  305. break;
  306. case "noKids":
  307. $icons .= "<img src=\"http://www.petfinder.com/images/search/no-kids.gif\" width=\"34\" height=\"21\" alt=\"Prefers home without small kids\" title=\"Prefers home without small kids\" />";
  308. break;
  309. case "specialNeeds":
  310. $icons .= "<img src=\"http://www.petfinder.com/images/search/spec_needs.gif\" width=\"18\" height=\"20\" alt=\"Special Needs\" title=\"Special Needs\" />";
  311. case "altered":
  312. $output_buffer .= "<li class=\"altered\">Spayed/Neutered</li>";
  313. break;
  314. case "hasShots":
  315. $output_buffer .= "<li class=\"hasShots\">Up-to-date with routine shots</li>";
  316. break;
  317. case "housebroken":
  318. $output_buffer .= "<li class=\"housebroken\">Housebroken</li>";
  319. break;
  320. }
  321. }
  322. if($icons != ""){
  323. $output_buffer .= "<li class=\"icon-options\">" . $icons . "</li>";
  324. }
  325. $output_buffer .= "</ul>";
  326. }
  327. $output_buffer .= "</div>"; //close images
  328. switch ($dog->size){
  329. case "L":
  330. $size = "Large";
  331. break;
  332. case "M":
  333. $size = "Medium";
  334. break;
  335. case "S":
  336. $size = "Small";
  337. break;
  338. default:
  339. $size = "Not known";
  340. break;
  341. }
  342. $output_buffer .= "<div class=\"description\">" . $dog->description . "</div><div class=\"features\">" . $dog->age . ", " . (($dog->sex == "M") ? "Male" : "Female") . ", " . $size . "</div></div>";
  343. $output_buffer .= "<div style=\"clear: both; \"></div>";
  344. }
  345. } //animal does not match
  346. }
  347. if($petf_options['powered_by'] == "Yes"){
  348. $output_buffer .= "<div class=\"powered_by\">Powered by <a href=\"http://www.petfinder.com\" target=\"_blank\">Petfinder.com</a></div>";
  349. }
  350. $output_buffer .= "</div>";
  351. }else{
  352. $output_buffer .= "No pets are listed for this shelter at this time. Please check back soon.";
  353. }
  354. }else if($xml->header->status->code == "201"){
  355. $output_buffer = "No results found for this shelter. Please check back soon!";
  356. }else{
  357. $output_buffer = "Petfinder is down for the moment. Please check back shortly.";
  358. }
  359.  
  360. return $output_buffer;
  361. }
  362.  
  363.  
  364. add_shortcode('get_pet','petf_get_pet');
  365.  
  366. /** Using shortcode get_pet grab one pet from petfinder.
  367. * Available Options: pet_id *required*, include_info, css_class. ***/
  368. function petf_get_pet( $atts ) {
  369.  
  370. global $petf_options;
  371.  
  372. extract( shortcode_atts( array(
  373. 'pet_id' => 1234,
  374. 'include_info' => 'yes',
  375. 'css_class' => 'pets'
  376. ), $atts, 'get_pet' ) );
  377.  
  378. $output_buffer = "";
  379. if(is_numeric($pet_id) && intval($pet_id) > 0 ){
  380. $response = wp_remote_get( "http://api.petfinder.com/pet.get?key=" . $petf_options["apikey"] . "&id=" . $pet_id );
  381. /***** Uncomment line below to view XML from Petfinder above results on page. *****/
  382. //echo "http://api.petfinder.com/pet.get?key=" . $petf_options["apikey"] . "&id=" . $pet_id;
  383. $xml = simplexml_load_string(wp_remote_retrieve_body($response));
  384. //print_r($xml);
  385. if( $xml->header->status->code == "100"){
  386. if( count( $xml->pet ) > 0 ){
  387. $output_buffer .= "<div class=\"" . $css_class . "\">";
  388. foreach( $xml->pet as $dog ){
  389. $output_buffer .= "<div class=\"dog\"><div class=\"name\"><a name=\"" . $dog->id . "\">". $dog->name . "</a></div>";
  390. $output_buffer .= "<div class=\"images\">";
  391. $firsttime = true;
  392. if(count($dog->media->photos) > 0){
  393. foreach( $dog->media->photos->photo as $photo ){
  394. //$output_buffer .= $photo["size"];
  395. if( $photo['size'] == $petf_options["large_image"] ){
  396. if( $firsttime ){
  397. $output_buffer .= "<img class=\"petfinder-big-img\" id=\"img_". $dog->id . "\" src=\"" . $photo . "\" alt=\" . $dog->name \">";
  398. }
  399. $bigfile = $photo;
  400. }
  401. if( $photo['size'] == $petf_options["thumbnail"] ){
  402. if( $firsttime ){
  403. $output_buffer .= "<div class=\"petfinder-thumbnails\">";
  404. $firsttime = false;
  405. }
  406. $output_buffer .= "<img class=\"petfinder-thumbmail\" onclick=\"switchbigimg('img_" . $dog->id . "', '" . $bigfile . "');return false;\" src=\"" . $photo . "\" alt=\" . $dog->name . \">";
  407. }
  408. }
  409. }
  410. if( !$firsttime ){
  411. //not first time so there are thumbnails to wrap up in a div. Closing petfinder-thumbnails
  412. $output_buffer .= "</div>";
  413. }
  414. if($include_info == "yes"){
  415.  
  416. $output_buffer .= "<ul class=\"pet-options\">";
  417.  
  418. $firsttime = true;
  419. foreach( $dog->breeds->breed as $this_breed ){
  420. if($firsttime){
  421. $output_buffer .= "<li class=\"breeds\">";
  422. $firsttime = false;
  423. }else{
  424. $output_buffer .= ", ";
  425. }
  426. $output_buffer .= $this_breed;
  427. }
  428. if(!$firsttime){
  429. $output_buffer .= "</li>";
  430. }
  431.  
  432. $icons = "";
  433. foreach( $dog->options->option as $option ){
  434. switch($option){
  435. case "noCats":
  436. $icons .= "<img src=\"http://www.petfinder.com/images/search/no-cat.gif\" width=\"36\" height=\"21\" alt=\"Prefers home without cats\" title=\"Prefers home without cats\" />";
  437. break;
  438. case "noDogs":
  439. $icons .= "<img src=\"http://www.petfinder.com/images/search/no-dogs.gif\" width=\"41\" height=\"21\" alt=\"Prefers home without dogs\" title=\"Prefers home without dogs\" />";
  440. break;
  441. case "noKids":
  442. $icons .= "<img src=\"http://www.petfinder.com/images/search/no-kids.gif\" width=\"34\" height=\"21\" alt=\"Prefers home without small kids\" title=\"Prefers home without small kids\" />";
  443. break;
  444. case "specialNeeds":
  445. $icons .= "<img src=\"http://www.petfinder.com/images/search/spec_needs.gif\" width=\"18\" height=\"20\" alt=\"Special Needs\" title=\"Special Needs\" />";
  446. case "altered":
  447. $output_buffer .= "<li class=\"altered\">Spayed/Neutered</li>";
  448. break;
  449. case "hasShots":
  450. $output_buffer .= "<li class=\"hasShots\">Up-to-date with routine shots</li>";
  451. break;
  452. case "housebroken":
  453. $output_buffer .= "<li class=\"housebroken\">Housebroken</li>";
  454. break;
  455. }
  456. }
  457. if($icons != ""){
  458. $output_buffer .= "<li class=\"icon-options\">" . $icons . "</li>";
  459. }
  460. $output_buffer .= "</ul>";
  461. }
  462. $output_buffer .= "</div>"; //close images
  463. switch ($dog->size){
  464. case "L":
  465. $size = "Large";
  466. break;
  467. case "M":
  468. $size = "Medium";
  469. break;
  470. case "S":
  471. $size = "Small";
  472. break;
  473. default:
  474. $size = "Not known";
  475. break;
  476. }
  477. $output_buffer .= "<div class=\"description\">" . $dog->description . "</div><div class=\"features\">" . $dog->age . ", " . (($dog->sex == "M") ? "Male" : "Female") . ", " . $size . "</div></div>";
  478. $output_buffer .= "<div style=\"clear: both; \"></div>";
  479. }
  480. if($petf_options['powered_by'] == "Yes"){
  481. $output_buffer .= "<div class=\"powered_by\">Powered by <a href=\"http://www.petfinder.com\" target=\"_blank\">Petfinder.com</a></div>";
  482. }
  483. $output_buffer .= "</div>";
  484. }else{
  485. $output_buffer .= "This pet was not found on Petfinder.";
  486. }
  487. }else if($xml->header->status->code == "201"){
  488. $output_buffer = "No results found for this pet. Please check back soon!";
  489. }else{
  490. $output_buffer = "Petfinder is down for the moment. Please check back shortly.";
  491. }
  492. }else{
  493. $output_buffer .= "Invalid Pet ID supplied.";
  494. }
  495. return $output_buffer;
  496. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement