/inc/class-shortcodes.php

https://github.com/NateJacobs/Brickset-API · PHP · 164 lines · 102 code · 20 blank · 42 comment · 6 complexity · c10b32df3aa5ad799112b44e2f3b209f MD5 · raw file

  1. <?php
  2. class BricksetAPIShortcode extends BricksetAPISearch
  3. {
  4. /**
  5. * Start things off right
  6. *
  7. * @author Nate Jacobs
  8. * @date 2/15/13
  9. * @since 1.0
  10. */
  11. public function __construct()
  12. {
  13. add_shortcode( 'bs_set', array( $this, 'get_set' ) );
  14. //add_shortcode( 'bs_my_wanted', array( $this, 'my_wanted' ) );
  15. //add_shortcode( 'bs_my_owned', array( $this, 'my_owned' ) );
  16. }
  17. /**
  18. * Get Set
  19. *
  20. * Displays details for the sets specified.
  21. * More than one set can be specified by seperating set numbers by a comma.
  22. * e.g. 1380,10240
  23. *
  24. * @author Nate Jacobs
  25. * @date 2/15/13
  26. * @since 1.0
  27. *
  28. * @param array
  29. */
  30. public function get_set( $atts )
  31. {
  32. extract( shortcode_atts( array(
  33. 'number' => '',
  34. ), $atts, 'bs_set' ) );
  35. $brickset = parent::get_by_number( $number );
  36. if( is_wp_error( $brickset ) )
  37. {
  38. return $brickset->get_error_message();
  39. }
  40. else
  41. {
  42. global $brickset_api_utilities;
  43. $settings = $brickset_api_utilities->get_settings_rules();
  44. $number = '';
  45. $numberVariant = '';
  46. if( true === $settings['bricklink'] )
  47. {
  48. $bricklink = '<strong>'.__( 'BrickLink', 'bs_api' ).': </strong><a href=http://www.bricklink.com/catalogItem.asp?S='.$number.'-'.$numberVariant.'>BrickLink</a><br><hr>';
  49. }
  50. elseif( false === $settings['bricklink'] )
  51. {
  52. $bricklink = '';
  53. }
  54. $return = '';
  55. foreach( $brickset as $result )
  56. {
  57. $number = sanitize_text_field( $result->number );
  58. $numberVariant = sanitize_text_field( $result->numberVariant );
  59. if( empty( $result->$settings['currency_key'] ) && 'unk' === $settings['currency_unknown'] )
  60. {
  61. $result->$settings['currency_key'] = __( ' Unknown', 'bs_api' );
  62. }
  63. if( empty( $result->$settings['currency_key'] ) && 'us' === $settings['currency_unknown'] )
  64. {
  65. $settings['currency'] = 'US';
  66. $settings['currency_key'] = 'USRetailPrice';
  67. $settings['currency_symbol'] = '&#36;';
  68. }
  69. $return .= '<img src="'.$result->imageURL.'"><br>';
  70. $return .= '<strong>'.__( 'Set Name', 'bs_api' ).': </strong>'.sanitize_text_field( $result->setName ).'<br>';
  71. $return .= '<strong>'.__( 'Set Number', 'bs_api' ).': </strong>'.$number.'-'.$numberVariant.'<br>';
  72. $return .= '<strong>'.__( 'Year', 'bs_api' ).': </strong>'.sanitize_text_field( $result->year ).'<br>';
  73. $return .= '<strong>'.__( 'Theme', 'bs_api' ).': </strong>'.sanitize_text_field( $result->theme ).'<br>';
  74. $return .= '<strong>'.__( 'Subtheme', 'bs_api' ).': </strong>'.sanitize_text_field( $result->subtheme ).'<br>';
  75. $return .= '<strong>'.sprintf( __( '%s Retail Price', 'bs_api' ), $settings['currency'] ).': </strong>'.$settings['currency_symbol'].sanitize_text_field( $result->$settings['currency_key'] ).'<br>';
  76. $return .= '<strong>'.__( 'Pieces', 'bs_api' ).': </strong>'.sanitize_text_field( $result->pieces ).'<br>';
  77. $return .= '<strong>'.__( 'Minifigs', 'bs_api' ).': </strong>'.sanitize_text_field( $result->minifigs ).'<br>';
  78. $return .= '<strong>'.__( 'Set Guide', 'bs_api' ).': </strong><a href='.esc_url( $result->bricksetURL ).'>Brickset</a><br>';
  79. $return .= $bricklink;
  80. }
  81. return $return;
  82. }
  83. }
  84. /**
  85. * My Wanted
  86. *
  87. * Returns a table with the post authors wanted sets
  88. * Not functional yet.
  89. *
  90. * @author Nate Jacobs
  91. * @date 2/16/13
  92. * @since 1.0
  93. */
  94. public function my_wanted()
  95. {
  96. global $post;
  97. $user_id = $post->post_author;
  98. parent::get_wanted( $user_id );
  99. $return = '<h2>My Wanted List</h2>';
  100. $return .= '<table><th>Image</th><th>Set Name</th><th>Set Number</th><th>Pieces</th>';
  101. foreach ( $this->results as $result )
  102. {
  103. $return .= '<tr>';
  104. $return .= '<td><img src="'.$result->thumbnailURL.'"></td>';
  105. $return .= '<td>'.$result->setName.'</td>';
  106. $return .= '<td>'.$result->number.'</td>';
  107. $return .= '<td>'.$result->pieces.'</td>';
  108. $return .= '</tr>';
  109. }
  110. $return .= '</table>';
  111. return $return;
  112. }
  113. /**
  114. * My Owned
  115. *
  116. * Returns a table with the post authors owned sets
  117. * Not functional yet.
  118. *
  119. * @author Nate Jacobs
  120. * @date 2/16/13
  121. * @since 1.0
  122. */
  123. public function my_owned()
  124. {
  125. global $post;
  126. $user_id = $post->post_author;
  127. parent::get_owned( $user_id );
  128. $return = '<h2>My Set List</h2>';
  129. $return .= '<table><th>Image</th><th>Set Name</th><th>Set Number</th><th>Pieces</th><th>Quantity</th>';
  130. foreach ( $this->results as $result )
  131. {
  132. $return .= '<tr>';
  133. $return .= '<td><img src="'.$result->thumbnailURL.'"></td>';
  134. $return .= '<td>'.$result->setName.'</td>';
  135. $return .= '<td>'.$result->number.'</td>';
  136. $return .= '<td>'.$result->pieces.'</td>';
  137. $return .= '<td>'.$result->qtyOwned.'</td>';
  138. $return .= '</tr>';
  139. }
  140. $return .= '</table>';
  141. return $return;
  142. }
  143. }
  144. $brickset_shortcodes = new BricksetAPIShortcode;