PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/www/wp-content/plugins/ithemes-exchange/api/theme/shipping.php

https://github.com/ArzuA/gitwordpress
PHP | 470 lines | 287 code | 59 blank | 124 comment | 11 complexity | 25d5d68ff834b3c345675ac9f857a805 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Shipping class for THEME API
  4. *
  5. * @since 1.4.0
  6. */
  7. class IT_Theme_API_Shipping implements IT_Theme_API {
  8. /**
  9. * API context
  10. * @var string $_context
  11. * @since 1.4.0
  12. */
  13. private $_context = 'shipping';
  14. /**
  15. * Current customer Shipping Address
  16. * @var string $_shipping_address
  17. * @since 1.4.0
  18. */
  19. private $_shipping_address = '';
  20. /**
  21. * Maps api tags to methods
  22. * @var array $_tag_map
  23. * @since 1.4.0
  24. */
  25. public $_tag_map = array(
  26. 'firstname' => 'first_name',
  27. 'lastname' => 'last_name',
  28. 'companyname' => 'company_name',
  29. 'address1' => 'address1',
  30. 'address2' => 'address2',
  31. 'city' => 'city',
  32. 'state' => 'state',
  33. 'zip' => 'zip',
  34. 'country' => 'country',
  35. 'email' => 'email',
  36. 'phone' => 'phone',
  37. 'submit' => 'submit',
  38. 'cancel' => 'cancel',
  39. );
  40. /**
  41. * Constructor
  42. *
  43. * @since 1.4.0
  44. * @return void
  45. */
  46. function IT_Theme_API_Shipping() {
  47. $this->_shipping_address = it_exchange_get_cart_shipping_address();
  48. }
  49. /**
  50. * Returns the context. Also helps to confirm we are an iThemes Exchange theme API class
  51. *
  52. * @since 1.4.0
  53. *
  54. * @return string
  55. */
  56. function get_api_context() {
  57. return $this->_context;
  58. }
  59. /**
  60. * Outputs the shipping address first name data
  61. *
  62. * @since 1.4.0
  63. * @return string
  64. */
  65. function first_name( $options=array() ) {
  66. $defaults = array(
  67. 'format' => 'html',
  68. 'label' => __( 'First Name', 'it-l10n-ithemes-exchange' ),
  69. 'required' => true,
  70. );
  71. $options = ITUtility::merge_defaults( $options, $defaults );
  72. $options['field_id'] = 'it-exchange-shipping-address-first-name';
  73. $options['field_name'] = 'it-exchange-shipping-address-first-name';
  74. $options['value'] = empty( $this->_shipping_address['first-name'] ) ? '' : $this->_shipping_address['first-name'];
  75. return $this->get_fields( $options );
  76. }
  77. /**
  78. * Outputs the shipping address last name data
  79. *
  80. * @since 1.4.0
  81. * @return string
  82. */
  83. function last_name( $options=array() ) {
  84. $defaults = array(
  85. 'format' => 'html',
  86. 'label' => __( 'Last Name', 'it-l10n-ithemes-exchange' ),
  87. 'required' => true,
  88. );
  89. $options = ITUtility::merge_defaults( $options, $defaults );
  90. $options['field_id'] = 'it-exchange-shipping-address-last-name';
  91. $options['field_name'] = 'it-exchange-shipping-address-last-name';
  92. $options['value'] = empty( $this->_shipping_address['last-name'] ) ? '' : $this->_shipping_address['last-name'];
  93. return $this->get_fields( $options );
  94. }
  95. /**
  96. * Outputs the shipping address compnay name data
  97. *
  98. * @since 1.4.0
  99. * @return string
  100. */
  101. function company_name( $options=array() ) {
  102. $defaults = array(
  103. 'format' => 'html',
  104. 'label' => __( 'Company Name', 'it-l10n-ithemes-exchange' ),
  105. 'required' => false,
  106. );
  107. $options = ITUtility::merge_defaults( $options, $defaults );
  108. $options['field_id'] = 'it-exchange-shipping-address-company-name';
  109. $options['field_name'] = 'it-exchange-shipping-address-company-name';
  110. $options['value'] = empty( $this->_shipping_address['company-name'] ) ? '' : $this->_shipping_address['company-name'];
  111. return $this->get_fields( $options );
  112. }
  113. /**
  114. * Outputs the shipping address address 1 data
  115. *
  116. * @since 1.4.0
  117. * @return string
  118. */
  119. function address1( $options=array() ) {
  120. $defaults = array(
  121. 'format' => 'html',
  122. 'label' => __( 'Address', 'it-l10n-ithemes-exchange' ),
  123. 'required' => true,
  124. );
  125. $options = ITUtility::merge_defaults( $options, $defaults );
  126. $options['field_id'] = 'it-exchange-shipping-address-address1';
  127. $options['field_name'] = 'it-exchange-shipping-address-address1';
  128. $options['value'] = empty( $this->_shipping_address['address1'] ) ? '' : $this->_shipping_address['address1'];
  129. return $this->get_fields( $options );
  130. }
  131. /**
  132. * Outputs the shipping address address 2data
  133. *
  134. * @since 1.4.0
  135. * @return string
  136. */
  137. function address2( $options=array() ) {
  138. $defaults = array(
  139. 'format' => 'html',
  140. 'label' => __( 'Address 2', 'it-l10n-ithemes-exchange' ),
  141. 'required' => false,
  142. );
  143. $options = ITUtility::merge_defaults( $options, $defaults );
  144. $options['field_id'] = 'it-exchange-shipping-address-address2';
  145. $options['field_name'] = 'it-exchange-shipping-address-address2';
  146. $options['value'] = empty( $this->_shipping_address['address2'] ) ? '' : $this->_shipping_address['address2'];
  147. return $this->get_fields( $options );
  148. }
  149. /**
  150. * Outputs the shipping address city data
  151. *
  152. * @since 1.4.0
  153. * @return string
  154. */
  155. function city( $options=array() ) {
  156. $defaults = array(
  157. 'format' => 'html',
  158. 'label' => __( 'City', 'it-l10n-ithemes-exchange' ),
  159. 'required' => true,
  160. );
  161. $options = ITUtility::merge_defaults( $options, $defaults );
  162. $options['field_id'] = 'it-exchange-shipping-address-city';
  163. $options['field_name'] = 'it-exchange-shipping-address-city';
  164. $options['value'] = empty( $this->_shipping_address['city'] ) ? '' : $this->_shipping_address['city'];
  165. return $this->get_fields( $options );
  166. }
  167. /**
  168. * Outputs the shipping address zip data
  169. *
  170. * @since 1.4.0
  171. * @return string
  172. */
  173. function zip( $options=array() ) {
  174. $defaults = array(
  175. 'format' => 'html',
  176. 'label' => __( 'Zip Code', 'it-l10n-ithemes-exchange' ),
  177. 'required' => true,
  178. );
  179. $options = ITUtility::merge_defaults( $options, $defaults );
  180. $options['field_id'] = 'it-exchange-shipping-address-zip';
  181. $options['field_name'] = 'it-exchange-shipping-address-zip';
  182. $options['value'] = empty( $this->_shipping_address['zip'] ) ? '' : $this->_shipping_address['zip'];
  183. return $this->get_fields( $options );
  184. }
  185. /**
  186. * Outputs the shipping address country data
  187. *
  188. * @since 1.4.0
  189. * @return string
  190. */
  191. function country( $options=array() ) {
  192. $defaults = array(
  193. 'format' => 'html',
  194. 'label' => __( 'Country', 'it-l10n-ithemes-exchange' ),
  195. 'required' => true,
  196. );
  197. $options = ITUtility::merge_defaults( $options, $defaults );
  198. $options['field_id'] = 'it-exchange-shipping-address-country';
  199. $options['field_name'] = 'it-exchange-shipping-address-country';
  200. $options['value'] = empty( $this->_shipping_address['country'] ) ? '' : $this->_shipping_address['country'];
  201. // Update value if doing ajax
  202. $options['value'] = empty( $_POST['ite_base_country_ajax'] ) ? $options['value'] : $_POST['ite_base_country_ajax'];
  203. $countries = it_exchange_get_data_set( 'countries' );
  204. $current_value = empty( $options['value'] ) ? '' : esc_attr( $options['value'] );
  205. $field = '<select id="' . esc_attr( $options['field_id'] ) . '" name="' . esc_attr( $options['field_name'] ) . '">';
  206. $field .= '<option value=""></option>';
  207. foreach( $countries as $key => $value ) {
  208. $alternatives = esc_attr( $key );
  209. if ( 'US' == $key )
  210. $alternatives .= ' US us usa USA u';
  211. $field .= '<option value="' . esc_attr( $key ) . '" ' . selected( $key, $current_value, false ) . ' data-alternative-spellings="' . $alternatives . '">' . esc_html( $value ) . '</option>';
  212. }
  213. $field .= '</select>';
  214. switch( $options['format'] ) {
  215. case 'field-id' :
  216. $output = $options['field_id'];
  217. break;
  218. case 'field-name':
  219. $output = $options['field_name'];
  220. break;
  221. case 'label':
  222. $output = $options['label'];
  223. break;
  224. case 'field':
  225. $output = $field;
  226. break;
  227. case 'value':
  228. $output = $current_value;
  229. break;
  230. case 'html':
  231. default:
  232. $output = '<label for="' . esc_attr( $options['field_id'] ) . '">' . $options['label'];
  233. if ( $options['required'] )
  234. $output .= '<span class="it-exchange-required-star">&#42;</span>';
  235. $output .= '</label>';
  236. $output .= $field;
  237. }
  238. return $output;
  239. }
  240. /**
  241. * Outputs the shipping address state data
  242. *
  243. * @since 1.4.0
  244. * @return string
  245. */
  246. function state( $options=array() ) {
  247. // Default state value for normal page load
  248. $shipping_value = empty( $this->_shipping_address['state'] ) ? '' : $this->_shipping_address['state'];
  249. $default_value = empty( $_POST['it-exchange-shipping-address-state'] ) ? $shipping_value : $_POST['it-exchange-shipping-address-state'];
  250. $defaults = array(
  251. 'format' => 'html',
  252. 'label' => __( 'State', 'it-l10n-ithemes-exchange' ),
  253. 'value' => $default_value,
  254. 'field-type' => false,
  255. 'required' => true,
  256. );
  257. $options = ITUtility::merge_defaults( $options, $defaults );
  258. // Update value if doing ajax
  259. $options['value'] = empty( $_POST['ite_base_state_ajax'] ) ? $options['value'] : $_POST['ite_base_state_ajax'];
  260. $options['field_id'] = 'it-exchange-shipping-address-state';
  261. $options['field_name'] = 'it-exchange-shipping-address-state';
  262. $options['value'] = empty( $this->_shipping_address['state'] ) ? '' : $this->_shipping_address['state'];
  263. $states = it_exchange_get_data_set( 'states', array( 'country' => it_exchange( 'shipping', 'get-country', array( 'format' => 'value' ) ) ) );
  264. $current_value = empty( $options['value'] ) ? '' : esc_attr( $options['value'] );
  265. $field = '';
  266. if ( ! empty( $states ) && is_array( $states ) && 'text' != $options['field-type'] ) {
  267. $field .= '<select id="' . esc_attr( $options['field_id'] ) . '" name="' . esc_attr( $options['field_name'] ) . '">';
  268. $field .= '<option value=""></option>';
  269. foreach( (array) $states as $key => $value ) {
  270. $alternatives = esc_attr( $key );
  271. $field .= '<option value="' . esc_attr( $key ) . '" ' . selected( $key, $current_value, false ) . ' data-alternative-spellings="' . $alternatives . '">' . esc_html( $value ) . '</option>';
  272. }
  273. $field .= '</select>';
  274. } else {
  275. $text_options = $options;
  276. $text_options['format'] = 'field';
  277. $field .= $this->get_fields( $text_options );
  278. }
  279. switch( $options['format'] ) {
  280. case 'field-id' :
  281. $output = $options['field_id'];
  282. break;
  283. case 'field-name':
  284. $output = $options['field_name'];
  285. break;
  286. case 'label':
  287. $output = $options['label'];
  288. break;
  289. case 'field':
  290. $output = $field;
  291. break;
  292. case 'value':
  293. $output = $current_value;
  294. break;
  295. case 'html':
  296. default:
  297. $output = '<label for="' . esc_attr( $options['field_id'] ) . '">' . $options['label'];
  298. if ( $options['required'] )
  299. $output .= '<span class="it-exchange-required-star">&#42;</span>';
  300. $output .= '</label>';
  301. $output .= $field;
  302. }
  303. return $output;
  304. }
  305. /**
  306. /**
  307. * Outputs the shipping address email data
  308. *
  309. * @since 1.4.0
  310. * @return string
  311. */
  312. function email( $options=array() ) {
  313. $defaults = array(
  314. 'format' => 'html',
  315. 'label' => __( 'Email', 'it-l10n-ithemes-exchange' ),
  316. );
  317. $options = ITUtility::merge_defaults( $options, $defaults );
  318. $options['field_id'] = 'it-exchange-shipping-address-email';
  319. $options['field_name'] = 'it-exchange-shipping-address-email';
  320. $options['value'] = empty( $this->_shipping_address['email'] ) ? '' : $this->_shipping_address['email'];
  321. return $this->get_fields( $options );
  322. }
  323. /**
  324. * Outputs the shipping address phone data
  325. *
  326. * @since 1.4.0
  327. * @return string
  328. */
  329. function phone( $options=array() ) {
  330. $defaults = array(
  331. 'format' => 'html',
  332. 'label' => __( 'Phone', 'it-l10n-ithemes-exchange' ),
  333. );
  334. $options = ITUtility::merge_defaults( $options, $defaults );
  335. $options['field_id'] = 'it-exchange-shipping-address-phone';
  336. $options['field_name'] = 'it-exchange-shipping-address-phone';
  337. $options['value'] = empty( $this->_shipping_address['phone'] ) ? '' : $this->_shipping_address['phone'];
  338. return $this->get_fields( $options );
  339. }
  340. /**
  341. * Outputs the shipping address submit button
  342. *
  343. * @since 1.4.0
  344. * @return string
  345. */
  346. function submit( $options=array() ) {
  347. $defaults = array(
  348. 'format' => 'html',
  349. 'label' => __( 'Submit', 'it-l10n-ithemes-exchange' ),
  350. 'name' => '',
  351. );
  352. $options = ITUtility::merge_defaults( $options, $defaults );
  353. $options['field_id'] = 'it-exchange-shipping-address-submit';
  354. return $output = '<input type="submit" id="' . esc_attr( $options['field_id'] ) . '" name="' . esc_attr( $options['name'] ) . '" value="'. esc_attr( $options['label'] ) .'" />';
  355. }
  356. /**
  357. * Outputs the shipping address phone data
  358. *
  359. * @since 1.4.0
  360. * @return string
  361. */
  362. function cancel( $options=array() ) {
  363. $defaults = array(
  364. 'format' => 'html',
  365. 'label' => __( 'Cancel', 'it-l10n-ithemes-exchange' ),
  366. );
  367. $options = ITUtility::merge_defaults( $options, $defaults );
  368. return '<a class="it-exchange-shipping-address-requirement-cancel" href="' . it_exchange_get_page_url( 'checkout' ) . '">' . $options['label'] . '</a>';
  369. }
  370. /**
  371. * Gets the HTML is the desired format
  372. *
  373. * @since 1.4.0
  374. *
  375. * @param array $options
  376. * @return mixed
  377. */
  378. function get_fields( $options ) {
  379. $value = empty( $options['value'] ) ? '' : esc_attr( $options['value'] );
  380. $class = empty( $options['class'] ) ? '' : esc_attr( $options['class'] );
  381. $options['required'] = ! empty( $options['required'] );
  382. switch( $options['format'] ) {
  383. case 'field-id' :
  384. $output = $options['field_id'];
  385. break;
  386. case 'field-name':
  387. $output = $options['field_name'];
  388. break;
  389. case 'label':
  390. $output = $options['label'];
  391. if ( $options['required'] )
  392. $output .= '<span class="it-exchange-required-star">&#42;</span>';
  393. break;
  394. case 'field':
  395. $output = '<input type="text" class="' . $class . '" id="' . esc_attr( $options['field_id'] ) . '" name="' . esc_attr( $options['field_name'] ) . '" value="' . $value . '" />';
  396. break;
  397. case 'value':
  398. $output = $value;
  399. break;
  400. case 'html':
  401. default:
  402. $output = empty( $options['label'] ) ? '' : '<label for="' . esc_attr( $options['field_id'] ) . '">' . $options['label'];
  403. if ( $options['required'] )
  404. $output .= '&nbsp;<span class="it-exchange-required-star">&#42;</span>';
  405. $output .= '</label>';
  406. $output .= '<input type="text" class="' . $class . '" id="' . esc_attr( $options['field_id'] ) . '" name="' . esc_attr( $options['field_name'] ) . '" value="' . $value . '" />';
  407. }
  408. return $output;
  409. }
  410. }