class-simple-job-board-admin-add-ons.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. if (!defined('ABSPATH')) {
  3. exit;
  4. } // Exit if accessed directly
  5. /**
  6. * Simple_Job_Board_Add_Ons Class
  7. *
  8. * This is used to display SJB Add-ons listing.
  9. *
  10. * @link https://wordpress.org/plugins/simple-job-board
  11. * @since 2.3.2
  12. *
  13. * @package Simple_Job_Board
  14. * @subpackage Simple_Job_Board/admin
  15. * @author PressTigers <support@presstigers.com>
  16. */
  17. class Simple_Job_Board_Add_Ons {
  18. /**
  19. * Marketplace API URL.
  20. *
  21. * @since 2.3.2
  22. * @access public
  23. * @var string $api_url Marketplace API URL.
  24. */
  25. public $api_url;
  26. /**
  27. * Initialize the class and set its properties.
  28. *
  29. * @since 2.2.3
  30. */
  31. public function __construct() {
  32. // Initialize Marketplace API URL
  33. $this->api_url = 'http://market.presstigers.com/wc-api/v2/products';
  34. // Action - Add Settings Menu
  35. add_action('admin_menu', array($this, 'admin_menu'), 13);
  36. }
  37. /**
  38. * Add Add-ons Page Under Job Board Menu.
  39. *
  40. * @since 2.3.2
  41. */
  42. public function admin_menu() {
  43. add_submenu_page('edit.php?post_type=jobpost', esc_html__('Simple Job Board Add-ons', 'simple-job-board'), esc_html__('Add-ons', 'simple-job-board'), 'manage_options', 'sjb-add-ons', array($this, 'sjb_add_ons'));
  44. }
  45. /**
  46. * Simple Job Board Add-ons
  47. *
  48. * @Since 2.3.2
  49. */
  50. public function sjb_add_ons() {
  51. ?>
  52. <div class="sjb-wrap container-fluid">
  53. <h1><?php echo esc_html__('Simple Job Board Add-ons', 'simple-job-board'); ?></h1>
  54. <?php
  55. $add_on_response = $this->sjb_get_items();
  56. // Print error if error, otherwise print information
  57. if (is_wp_error($add_on_response)) {
  58. echo '<div id="message" class="error notice notice-error">';
  59. echo '<p>The following error occurred when contacting Simple Job Board server: ' . wp_strip_all_tags($add_on_response->get_error_message()) . '</p>';
  60. echo '</div>';
  61. } else {
  62. $add_on_body = json_decode($add_on_response);
  63. $add_on_items = $add_on_body->products;
  64. ?>
  65. <!-- List SJB Add-ons -->
  66. <div class="add-ons">
  67. <div class="row">
  68. <div class="products">
  69. <?php
  70. // Retrive Add-ons Reuired Data
  71. foreach ($add_on_items as $item) {
  72. if ("Simple Plugin Integration" !== $item->title &&
  73. "Plugin Integration With Extended Support" !== $item->title &&
  74. in_array('Simple Job Board', $item->categories)
  75. ) {
  76. $url = explode('/', $item->permalink);
  77. $home_url = $url[0]. '//' . $url[2];
  78. ?>
  79. <div class="col-lg-3 col-md-6 col-sm-6 col-xs-12 product">
  80. <article>
  81. <a href="<?php echo esc_url($item->permalink); ?>" target="_blank">
  82. <img title="<?php echo esc_attr($item->title); ?>" alt="<?php echo esc_attr($item->title); ?>" class="img-responsive" src="<?php echo esc_url($item->images[0]->src); ?>">
  83. <h3><?php echo esc_attr($item->title); ?></h3>
  84. </a>
  85. <?php echo $item->short_description; ?>
  86. <div class="price add-to-cart">
  87. <span><b><?php echo '$' . $item->price; ?></b></span>
  88. <a href="<?php echo esc_url( $home_url . "/cart/?add-to-cart=" . $item->id ); ?>" class="button button-primary cart-btn" target="_blank"><?php echo __( 'Buy Now','simple-job-board' ); ?></a>
  89. </div>
  90. </article>
  91. </div>
  92. <?php
  93. }
  94. }
  95. ?>
  96. <!-- Contact Us Link -->
  97. <div class="col-lg-3 col-md-6 col-sm-6 col-xs-12 product">
  98. <article>
  99. <a href="<?php echo esc_url("https://market.presstigers.com/contact-us/"); ?>" target="_blank">
  100. <img title="SJB customizations" alt="Contact for add-on." class="img-responsive" src="<?php echo esc_url( untrailingslashit(plugins_url(basename(plugin_dir_path(__DIR__)), basename(__DIR__))) . '/admin/images/add-on.jpg');?>" >
  101. <h3><?php echo esc_html__('Unable to locate required add-on?','simple-job-board'); ?></h3>
  102. </a>
  103. <a href="<?php echo esc_url("https://market.presstigers.com/contact-us/"); ?>" class="button button-primary add-on-contact" target="_blank"><?php echo __( 'Contact Us','simple-job-board' ); ?></a>
  104. </article>
  105. </div>
  106. </div>
  107. </div>
  108. </div>
  109. <?php } ?>
  110. </div>
  111. <?php
  112. }
  113. /**
  114. * Get SJB Items.
  115. *
  116. * @return string|WP_Error Returns the contents of the response on success|WP_Error on failure
  117. */
  118. public function sjb_get_items() {
  119. // Collect the args
  120. $params = array(
  121. 'oauth_consumer_key' => 'ck_b65fd5f23cb4c753c5bd095247d2ef1d630d1d3e',
  122. 'oauth_timestamp' => time(),
  123. 'oauth_nonce' => sha1(microtime()),
  124. 'oauth_signature_method' => 'HMAC-SHA256',
  125. );
  126. $params['oauth_signature'] = $this->generate_oauth_signature($params);
  127. // Generate the URL
  128. $url = esc_url_raw($this->api_url) . '?' . http_build_query($params);
  129. // Make API request
  130. $response = wp_remote_post(
  131. $url, array(
  132. 'method' => 'GET',
  133. 'timeout' => 45,
  134. 'redirection' => 5,
  135. 'httpversion' => '1.0',
  136. 'blocking' => true,
  137. 'headers' => array(),
  138. 'body' => $params,
  139. 'cookies' => array(),
  140. )
  141. );
  142. // Check the response code
  143. $response_code = wp_remote_retrieve_response_code($response);
  144. $response_message = wp_remote_retrieve_response_message($response);
  145. if (200 != $response_code && !empty($response_message)) {
  146. return new WP_Error($response_code, $response_message);
  147. } elseif (200 != $response_code) {
  148. return new WP_Error($response_code, esc_html__('Unknown error occurred'));
  149. } else {
  150. return wp_remote_retrieve_body($response);
  151. }
  152. }
  153. /**
  154. * Generate OAuth Signature:
  155. *
  156. * @since 2.3.2
  157. *
  158. * @param array $params Query Parameters (including oauth_*)
  159. * @param string $http_method e.g. GET
  160. * @return string signature
  161. */
  162. public function generate_oauth_signature($params) {
  163. $base_request_uri = rawurlencode($this->api_url);
  164. if (isset($params['filter'])) {
  165. $filters = $params['filter'];
  166. unset($params['filter']);
  167. foreach ($filters as $filter => $filter_value) {
  168. $params['filter[' . $filter . ']'] = $filter_value;
  169. }
  170. }
  171. // Normalize Parameter & Sort them
  172. $params = $this->normalize_parameters($params);
  173. uksort($params, 'strcmp');
  174. // Form Query String
  175. $query_params = array();
  176. foreach ($params as $param_key => $param_value) {
  177. $query_params[] = $param_key . '%3D' . $param_value; // join with equals sign
  178. }
  179. $query_string = implode('%26', $query_params); // join with ampersand
  180. $string_to_sign = 'GET&' . $base_request_uri . '&' . $query_string;
  181. return base64_encode(hash_hmac('SHA256', $string_to_sign, 'cs_7ad8e2e0db0cbd1eeaa2095f2090cbce626a8758', TRUE));
  182. }
  183. /**
  184. * Normalize each parameter by assuming each parameter may have already been
  185. * encoded, so attempt to decode, and then re-encode according to RFC 3986
  186. *
  187. * @since 2.3.2
  188. *
  189. * @param array $parameters Un-normalized pararmeters
  190. * @return array Normalized Parameters
  191. */
  192. private function normalize_parameters($parameters) {
  193. $normalized_parameters = array();
  194. foreach ($parameters as $key => $value) {
  195. // Percent Symbols (%) Must be Double-encoded
  196. $key = str_replace('%', '%25', rawurlencode(rawurldecode($key)));
  197. $value = str_replace('%', '%25', rawurlencode(rawurldecode($value)));
  198. $normalized_parameters[$key] = $value;
  199. }
  200. return $normalized_parameters;
  201. }
  202. }
  203. new Simple_Job_Board_Add_Ons();