PageRenderTime 30ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

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