PageRenderTime 88ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/plugins/contact-form-7/modules/constant-contact.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 947 lines | 794 code | 153 blank | 0 comment | 87 complexity | a306835a697e9986dff8edf737587bc4 MD5 | raw file
  1. <?php
  2. add_action( 'wpcf7_init', 'wpcf7_constant_contact_register_service', 10, 0 );
  3. function wpcf7_constant_contact_register_service() {
  4. $integration = WPCF7_Integration::get_instance();
  5. $integration->add_category( 'email_marketing',
  6. __( 'Email Marketing', 'contact-form-7' ) );
  7. $service = WPCF7_ConstantContact::get_instance();
  8. $integration->add_service( 'constant_contact', $service );
  9. }
  10. add_action( 'wpcf7_save_contact_form',
  11. 'wpcf7_constant_contact_save_contact_form', 10, 1 );
  12. function wpcf7_constant_contact_save_contact_form( $contact_form ) {
  13. $service = WPCF7_ConstantContact::get_instance();
  14. if ( ! $service->is_active() ) {
  15. return;
  16. }
  17. $additional_settings = $contact_form->additional_setting(
  18. 'constant_contact',
  19. false
  20. );
  21. $list_names = array();
  22. $pattern = '/[\t ]*('
  23. . "'[^']*'"
  24. . '|'
  25. . '"[^"]*"'
  26. . '|'
  27. . '[^,]*?'
  28. . ')[\t ]*(?:[,]+|$)/';
  29. foreach ( $additional_settings as $setting ) {
  30. if ( preg_match_all( $pattern, $setting, $matches ) ) {
  31. foreach ( $matches[1] as $match ) {
  32. $name = trim( wpcf7_strip_quote( $match ) );
  33. if ( '' !== $name ) {
  34. $list_names[] = $name;
  35. }
  36. }
  37. }
  38. }
  39. $list_names = array_unique( $list_names );
  40. $key = sprintf( 'wpcf7_contact_form:%d', $contact_form->id() );
  41. $service->update_contact_lists( array( $key => $list_names ) );
  42. }
  43. add_action( 'wpcf7_submit', 'wpcf7_constant_contact_submit', 10, 2 );
  44. function wpcf7_constant_contact_submit( $contact_form, $result ) {
  45. $service = WPCF7_ConstantContact::get_instance();
  46. if ( ! $service->is_active() ) {
  47. return;
  48. }
  49. if ( $contact_form->in_demo_mode() ) {
  50. return;
  51. }
  52. $do_submit = true;
  53. if ( empty( $result['status'] )
  54. or ! in_array( $result['status'], array( 'mail_sent' ) ) ) {
  55. $do_submit = false;
  56. }
  57. $additional_settings = $contact_form->additional_setting(
  58. 'constant_contact',
  59. false
  60. );
  61. foreach ( $additional_settings as $setting ) {
  62. if ( in_array( $setting, array( 'off', 'false', '0' ), true ) ) {
  63. $do_submit = false;
  64. break;
  65. }
  66. }
  67. $do_submit = apply_filters( 'wpcf7_constant_contact_submit',
  68. $do_submit, $contact_form, $result );
  69. if ( ! $do_submit ) {
  70. return;
  71. }
  72. $submission = WPCF7_Submission::get_instance();
  73. $consented = true;
  74. foreach ( $contact_form->scan_form_tags( 'feature=name-attr' ) as $tag ) {
  75. if ( $tag->has_option( 'consent_for:constant_contact' )
  76. and null == $submission->get_posted_data( $tag->name ) ) {
  77. $consented = false;
  78. break;
  79. }
  80. }
  81. if ( ! $consented ) {
  82. return;
  83. }
  84. $request_builder_class_name = apply_filters(
  85. 'wpcf7_constant_contact_contact_post_request_builder',
  86. 'WPCF7_ConstantContact_ContactPostRequest'
  87. );
  88. if ( ! class_exists( $request_builder_class_name ) ) {
  89. return;
  90. }
  91. $request_builder = new $request_builder_class_name;
  92. $request_builder->build( $submission );
  93. if ( ! $request_builder->is_valid() ) {
  94. return;
  95. }
  96. if ( $email = $request_builder->get_email_address()
  97. and $service->email_exists( $email ) ) {
  98. return;
  99. }
  100. $service->create_contact( $request_builder->to_array() );
  101. }
  102. if ( ! class_exists( 'WPCF7_Service_OAuth2' ) ) {
  103. return;
  104. }
  105. class WPCF7_ConstantContact extends WPCF7_Service_OAuth2 {
  106. const service_name = 'constant_contact';
  107. const authorization_endpoint = 'https://api.cc.email/v3/idfed';
  108. const token_endpoint = 'https://idfed.constantcontact.com/as/token.oauth2';
  109. private static $instance;
  110. protected $contact_lists = array();
  111. public static function get_instance() {
  112. if ( empty( self::$instance ) ) {
  113. self::$instance = new self;
  114. }
  115. return self::$instance;
  116. }
  117. private function __construct() {
  118. $this->authorization_endpoint = self::authorization_endpoint;
  119. $this->token_endpoint = self::token_endpoint;
  120. $option = (array) WPCF7::get_option( self::service_name );
  121. if ( isset( $option['client_id'] ) ) {
  122. $this->client_id = $option['client_id'];
  123. }
  124. if ( isset( $option['client_secret'] ) ) {
  125. $this->client_secret = $option['client_secret'];
  126. }
  127. if ( isset( $option['access_token'] ) ) {
  128. $this->access_token = $option['access_token'];
  129. }
  130. if ( isset( $option['refresh_token'] ) ) {
  131. $this->refresh_token = $option['refresh_token'];
  132. }
  133. if ( $this->is_active() ) {
  134. if ( isset( $option['contact_lists'] ) ) {
  135. $this->contact_lists = $option['contact_lists'];
  136. }
  137. }
  138. add_action( 'wpcf7_admin_init', array( $this, 'auth_redirect' ) );
  139. }
  140. public function auth_redirect() {
  141. $auth = isset( $_GET['auth'] ) ? trim( $_GET['auth'] ) : '';
  142. $code = isset( $_GET['code'] ) ? trim( $_GET['code'] ) : '';
  143. if ( self::service_name === $auth and $code
  144. and current_user_can( 'wpcf7_manage_integration' ) ) {
  145. $redirect_to = add_query_arg(
  146. array(
  147. 'service' => self::service_name,
  148. 'action' => 'auth_redirect',
  149. 'code' => $code,
  150. ),
  151. menu_page_url( 'wpcf7-integration', false )
  152. );
  153. wp_safe_redirect( $redirect_to );
  154. exit();
  155. }
  156. }
  157. protected function save_data() {
  158. $option = array_merge(
  159. (array) WPCF7::get_option( self::service_name ),
  160. array(
  161. 'client_id' => $this->client_id,
  162. 'client_secret' => $this->client_secret,
  163. 'access_token' => $this->access_token,
  164. 'refresh_token' => $this->refresh_token,
  165. 'contact_lists' => $this->contact_lists,
  166. )
  167. );
  168. WPCF7::update_option( self::service_name, $option );
  169. }
  170. protected function reset_data() {
  171. $this->client_id = '';
  172. $this->client_secret = '';
  173. $this->access_token = '';
  174. $this->refresh_token = '';
  175. $this->contact_lists = array();
  176. $this->save_data();
  177. }
  178. public function get_title() {
  179. return __( 'Constant Contact', 'contact-form-7' );
  180. }
  181. public function get_categories() {
  182. return array( 'email_marketing' );
  183. }
  184. public function icon() {
  185. }
  186. public function link() {
  187. echo sprintf( '<a href="%1$s">%2$s</a>',
  188. 'https://constant-contact.evyy.net/c/1293104/205991/3411',
  189. 'constantcontact.com'
  190. );
  191. }
  192. protected function get_redirect_uri() {
  193. return admin_url( '/?auth=' . self::service_name );
  194. }
  195. protected function menu_page_url( $args = '' ) {
  196. $args = wp_parse_args( $args, array() );
  197. $url = menu_page_url( 'wpcf7-integration', false );
  198. $url = add_query_arg( array( 'service' => self::service_name ), $url );
  199. if ( ! empty( $args) ) {
  200. $url = add_query_arg( $args, $url );
  201. }
  202. return $url;
  203. }
  204. public function load( $action = '' ) {
  205. parent::load( $action );
  206. if ( 'setup' == $action and 'POST' == $_SERVER['REQUEST_METHOD'] ) {
  207. check_admin_referer( 'wpcf7-constant-contact-setup' );
  208. if ( ! empty( $_POST['reset'] ) ) {
  209. $this->reset_data();
  210. } else {
  211. $this->client_id = isset( $_POST['client_id'] )
  212. ? trim( $_POST['client_id'] ) : '';
  213. $this->client_secret = isset( $_POST['client_secret'] )
  214. ? trim( $_POST['client_secret'] ) : '';
  215. $this->save_data();
  216. $this->authorize( 'contact_data' );
  217. }
  218. wp_safe_redirect( $this->menu_page_url( 'action=setup' ) );
  219. exit();
  220. }
  221. if ( 'edit' == $action and 'POST' == $_SERVER['REQUEST_METHOD'] ) {
  222. check_admin_referer( 'wpcf7-constant-contact-edit' );
  223. $list_ids = isset( $_POST['contact_lists'] )
  224. ? (array) $_POST['contact_lists']
  225. : array();
  226. $this->update_contact_lists( array( 'default' => $list_ids ) );
  227. wp_safe_redirect( $this->menu_page_url(
  228. array(
  229. 'action' => 'setup',
  230. 'message' => 'updated',
  231. )
  232. ) );
  233. exit();
  234. }
  235. if ( $this->is_active() ) {
  236. $this->update_contact_lists();
  237. }
  238. }
  239. public function email_exists( $email ) {
  240. $endpoint = add_query_arg(
  241. array(
  242. 'email' => $email,
  243. 'status' => 'all',
  244. ),
  245. 'https://api.cc.email/v3/contacts'
  246. );
  247. $request = array(
  248. 'method' => 'GET',
  249. 'headers' => array(
  250. 'Accept' => 'application/json',
  251. 'Content-Type' => 'application/json; charset=utf-8',
  252. ),
  253. );
  254. $response = $this->remote_request( $endpoint, $request );
  255. if ( 400 <= (int) wp_remote_retrieve_response_code( $response ) ) {
  256. if ( WP_DEBUG ) {
  257. $this->log( $endpoint, $request, $response );
  258. }
  259. return false;
  260. }
  261. $response_body = wp_remote_retrieve_body( $response );
  262. if ( empty( $response_body ) ) {
  263. return false;
  264. }
  265. $response_body = json_decode( $response_body, true );
  266. return ! empty( $response_body['contacts'] );
  267. }
  268. public function create_contact( $properties ) {
  269. $endpoint = 'https://api.cc.email/v3/contacts';
  270. $request = array(
  271. 'method' => 'POST',
  272. 'headers' => array(
  273. 'Accept' => 'application/json',
  274. 'Content-Type' => 'application/json; charset=utf-8',
  275. ),
  276. 'body' => json_encode( $properties ),
  277. );
  278. $response = $this->remote_request( $endpoint, $request );
  279. if ( 400 <= (int) wp_remote_retrieve_response_code( $response ) ) {
  280. if ( WP_DEBUG ) {
  281. $this->log( $endpoint, $request, $response );
  282. }
  283. return false;
  284. }
  285. }
  286. public function get_contact_lists() {
  287. $endpoint = 'https://api.cc.email/v3/contact_lists';
  288. $request = array(
  289. 'method' => 'GET',
  290. 'headers' => array(
  291. 'Accept' => 'application/json',
  292. 'Content-Type' => 'application/json; charset=utf-8',
  293. ),
  294. );
  295. $response = $this->remote_request( $endpoint, $request );
  296. if ( 400 <= (int) wp_remote_retrieve_response_code( $response ) ) {
  297. if ( WP_DEBUG ) {
  298. $this->log( $endpoint, $request, $response );
  299. }
  300. return false;
  301. }
  302. $response_body = wp_remote_retrieve_body( $response );
  303. if ( empty( $response_body ) ) {
  304. return false;
  305. }
  306. $response_body = json_decode( $response_body, true );
  307. if ( ! empty( $response_body['lists'] ) ) {
  308. return (array) $response_body['lists'];
  309. } else {
  310. return array();
  311. }
  312. }
  313. public function update_contact_lists( $selection = array() ) {
  314. $contact_lists = array();
  315. $contact_lists_on_api = $this->get_contact_lists();
  316. if ( false !== $contact_lists_on_api ) {
  317. foreach ( (array) $contact_lists_on_api as $list ) {
  318. if ( isset( $list['list_id'] ) ) {
  319. $list_id = trim( $list['list_id'] );
  320. } else {
  321. continue;
  322. }
  323. if ( isset( $this->contact_lists[$list_id]['selected'] ) ) {
  324. $list['selected'] = $this->contact_lists[$list_id]['selected'];
  325. } else {
  326. $list['selected'] = array();
  327. }
  328. $contact_lists[$list_id] = $list;
  329. }
  330. } else {
  331. $contact_lists = $this->contact_lists;
  332. }
  333. foreach ( (array) $selection as $key => $ids_or_names ) {
  334. foreach( $contact_lists as $list_id => $list ) {
  335. if ( in_array( $list['list_id'], (array) $ids_or_names, true )
  336. or in_array( $list['name'], (array) $ids_or_names, true ) ) {
  337. $contact_lists[$list_id]['selected'][$key] = true;
  338. } else {
  339. unset( $contact_lists[$list_id]['selected'][$key] );
  340. }
  341. }
  342. }
  343. $this->contact_lists = $contact_lists;
  344. if ( $selection ) {
  345. $this->save_data();
  346. }
  347. return $this->contact_lists;
  348. }
  349. public function admin_notice( $message = '' ) {
  350. switch ( $message ) {
  351. case 'success':
  352. echo sprintf(
  353. '<div class="updated notice notice-success is-dismissible"><p>%s</p></div>',
  354. esc_html( __( "Connection established.", 'contact-form-7' ) )
  355. );
  356. break;
  357. case 'failed':
  358. echo sprintf(
  359. '<div class="error notice notice-error is-dismissible"><p><strong>%1$s</strong>: %2$s</p></div>',
  360. esc_html( __( "Error", 'contact-form-7' ) ),
  361. esc_html( __( "Failed to establish connection. Please double-check your configuration.", 'contact-form-7' ) )
  362. );
  363. break;
  364. case 'updated':
  365. echo sprintf(
  366. '<div class="updated notice notice-success is-dismissible"><p>%s</p></div>',
  367. esc_html( __( "Configuration updated.", 'contact-form-7' ) )
  368. );
  369. break;
  370. }
  371. }
  372. public function display( $action = '' ) {
  373. echo '<p>' . sprintf(
  374. esc_html( __( 'The Constant Contact integration module allows you to send contact data collected through your contact forms to the Constant Contact API. You can create reliable email subscription services in a few easy steps. For details, see %s.', 'contact-form-7' ) ),
  375. wpcf7_link(
  376. __(
  377. 'https://contactform7.com/constant-contact-integration/',
  378. 'contact-form-7'
  379. ),
  380. __( 'Constant Contact Integration', 'contact-form-7' )
  381. )
  382. ) . '</p>';
  383. if ( $this->is_active() ) {
  384. echo sprintf(
  385. '<p class="dashicons-before dashicons-yes">%s</p>',
  386. esc_html( __( "This site is connected to the Constant Contact API.", 'contact-form-7' ) )
  387. );
  388. }
  389. if ( 'setup' == $action ) {
  390. $this->display_setup();
  391. } else {
  392. echo sprintf(
  393. '<p><a href="%1$s" class="button">%2$s</a></p>',
  394. esc_url( $this->menu_page_url( 'action=setup' ) ),
  395. esc_html( __( 'Setup Integration', 'contact-form-7' ) )
  396. );
  397. }
  398. }
  399. private function display_setup() {
  400. ?>
  401. <form method="post" action="<?php echo esc_url( $this->menu_page_url( 'action=setup' ) ); ?>">
  402. <?php wp_nonce_field( 'wpcf7-constant-contact-setup' ); ?>
  403. <table class="form-table">
  404. <tbody>
  405. <tr>
  406. <th scope="row"><label for="client_id"><?php echo esc_html( __( 'API Key', 'contact-form-7' ) ); ?></label></th>
  407. <td><?php
  408. if ( $this->is_active() ) {
  409. echo esc_html( $this->client_id );
  410. echo sprintf(
  411. '<input type="hidden" value="%1$s" id="client_id" name="client_id" />',
  412. esc_attr( $this->client_id )
  413. );
  414. } else {
  415. echo sprintf(
  416. '<input type="text" aria-required="true" value="%1$s" id="client_id" name="client_id" class="regular-text code" />',
  417. esc_attr( $this->client_id )
  418. );
  419. }
  420. ?></td>
  421. </tr>
  422. <tr>
  423. <th scope="row"><label for="client_secret"><?php echo esc_html( __( 'App Secret', 'contact-form-7' ) ); ?></label></th>
  424. <td><?php
  425. if ( $this->is_active() ) {
  426. echo esc_html( wpcf7_mask_password( $this->client_secret ) );
  427. echo sprintf(
  428. '<input type="hidden" value="%1$s" id="client_secret" name="client_secret" />',
  429. esc_attr( $this->client_secret )
  430. );
  431. } else {
  432. echo sprintf(
  433. '<input type="text" aria-required="true" value="%1$s" id="client_secret" name="client_secret" class="regular-text code" />',
  434. esc_attr( $this->client_secret )
  435. );
  436. }
  437. ?></td>
  438. </tr>
  439. <tr>
  440. <th scope="row"><label for="redirect_uri"><?php echo esc_html( __( 'Redirect URI', 'contact-form-7' ) ); ?></label></th>
  441. <td><?php
  442. echo sprintf(
  443. '<input type="text" value="%1$s" id="redirect_uri" name="redirect_uri" class="large-text code" readonly="readonly" onfocus="this.select();" style="font-size: 11px;" />',
  444. $this->get_redirect_uri()
  445. );
  446. ?>
  447. <p class="description"><?php echo esc_html( __( "Set this URL as the redirect URI.", 'contact-form-7' ) ); ?></p>
  448. </td>
  449. </tr>
  450. </tbody>
  451. </table>
  452. <?php
  453. if ( $this->is_active() ) {
  454. submit_button(
  455. _x( 'Reset Keys', 'API keys', 'contact-form-7' ),
  456. 'small', 'reset'
  457. );
  458. } else {
  459. submit_button(
  460. __( 'Connect to the Constant Contact API', 'contact-form-7' )
  461. );
  462. }
  463. ?>
  464. </form>
  465. <?php
  466. if ( $this->is_active() and ! empty( $this->contact_lists ) ) {
  467. ?>
  468. <form method="post" action="<?php echo esc_url( $this->menu_page_url( 'action=edit' ) ); ?>">
  469. <?php wp_nonce_field( 'wpcf7-constant-contact-edit' ); ?>
  470. <table class="form-table">
  471. <tbody>
  472. <tr>
  473. <th scope="row"><?php echo esc_html( _x( 'Contact Lists', 'Constant Contact', 'contact-form-7' ) ); ?></th>
  474. <td>
  475. <fieldset>
  476. <legend class="screen-reader-text"><span><?php echo esc_html( _x( "Contact Lists: Select lists to which newly added contacts are to belong.", 'Constant Contact', 'contact-form-7' ) ); ?></span></legend>
  477. <p class="description"><?php echo esc_html( __( "Select lists to which newly added contacts are to belong.", 'contact-form-7' ) ); ?></p>
  478. <ul class="checkboxes"><?php
  479. foreach ( $this->contact_lists as $list ) {
  480. echo sprintf(
  481. '<li><input %1$s /> <label for="%2$s">%3$s</label></li>',
  482. wpcf7_format_atts( array(
  483. 'type' => 'checkbox',
  484. 'name' => 'contact_lists[]',
  485. 'value' => $list['list_id'],
  486. 'id' => 'contact_list_' . $list['list_id'],
  487. 'checked' => empty( $list['selected']['default'] )
  488. ? ''
  489. : 'checked',
  490. ) ),
  491. esc_attr( 'contact_list_' . $list['list_id'] ),
  492. esc_html( $list['name'] )
  493. );
  494. }
  495. ?></ul>
  496. </fieldset>
  497. </td>
  498. </tr>
  499. </tbody>
  500. </table>
  501. <?php
  502. submit_button();
  503. }
  504. ?>
  505. </form>
  506. <?php
  507. }
  508. }
  509. class WPCF7_ConstantContact_ContactPostRequest {
  510. private $email_address;
  511. private $first_name;
  512. private $last_name;
  513. private $job_title;
  514. private $company_name;
  515. private $create_source;
  516. private $birthday_month;
  517. private $birthday_day;
  518. private $anniversary;
  519. private $custom_fields = array();
  520. private $phone_numbers = array();
  521. private $street_addresses = array();
  522. private $list_memberships = array();
  523. public function __construct() {
  524. }
  525. public function build( WPCF7_Submission $submission ) {
  526. $this->set_create_source( 'Contact' );
  527. $posted_data = (array) $submission->get_posted_data();
  528. if ( isset( $posted_data['your-first-name'] ) ) {
  529. $this->set_first_name( $posted_data['your-first-name'] );
  530. }
  531. if ( isset( $posted_data['your-last-name'] ) ) {
  532. $this->set_last_name( $posted_data['your-last-name'] );
  533. }
  534. if ( ! ( $this->first_name || $this->last_name )
  535. and isset( $posted_data['your-name'] ) ) {
  536. $your_name = preg_split( '/[\s]+/', $posted_data['your-name'], 2 );
  537. $this->set_first_name( array_shift( $your_name ) );
  538. $this->set_last_name( array_shift( $your_name ) );
  539. }
  540. if ( isset( $posted_data['your-email'] ) ) {
  541. $this->set_email_address( $posted_data['your-email'], 'implicit' );
  542. }
  543. if ( isset( $posted_data['your-job-title'] ) ) {
  544. $this->set_job_title( $posted_data['your-job-title'] );
  545. }
  546. if ( isset( $posted_data['your-company-name'] ) ) {
  547. $this->set_company_name( $posted_data['your-company-name'] );
  548. }
  549. if ( isset( $posted_data['your-birthday-month'] )
  550. and isset( $posted_data['your-birthday-day'] ) ) {
  551. $this->set_birthday(
  552. $posted_data['your-birthday-month'],
  553. $posted_data['your-birthday-day']
  554. );
  555. } elseif ( isset( $posted_data['your-birthday'] ) ) {
  556. $date = trim( $posted_data['your-birthday'] );
  557. if ( preg_match( '/^(\d{4})-(\d{2})-(\d{2})$/', $date, $matches ) ) {
  558. $this->set_birthday( $matches[2], $matches[3] );
  559. }
  560. }
  561. if ( isset( $posted_data['your-anniversary'] ) ) {
  562. $this->set_anniversary( $posted_data['your-anniversary'] );
  563. }
  564. if ( isset( $posted_data['your-phone-number'] ) ) {
  565. $this->add_phone_number( $posted_data['your-phone-number'] );
  566. }
  567. $this->add_street_address(
  568. isset( $posted_data['your-address-street'] )
  569. ? $posted_data['your-address-street'] : '',
  570. isset( $posted_data['your-address-city'] )
  571. ? $posted_data['your-address-city'] : '',
  572. isset( $posted_data['your-address-state'] )
  573. ? $posted_data['your-address-state'] : '',
  574. isset( $posted_data['your-address-postal-code'] )
  575. ? $posted_data['your-address-postal-code'] : '',
  576. isset( $posted_data['your-address-country'] )
  577. ? $posted_data['your-address-country'] : ''
  578. );
  579. $service_option = (array) WPCF7::get_option( 'constant_contact' );
  580. $contact_lists = isset( $service_option['contact_lists'] )
  581. ? $service_option['contact_lists'] : array();
  582. $contact_form = $submission->get_contact_form();
  583. if ( $contact_form->additional_setting( 'constant_contact' ) ) {
  584. $key = sprintf( 'wpcf7_contact_form:%d', $contact_form->id() );
  585. } else {
  586. $key = 'default';
  587. }
  588. foreach ( (array) $contact_lists as $list ) {
  589. if ( ! empty( $list['selected'][$key] ) ) {
  590. $this->add_list_membership( $list['list_id'] );
  591. }
  592. }
  593. }
  594. public function is_valid() {
  595. return $this->create_source
  596. && ( $this->email_address || $this->first_name || $this->last_name );
  597. }
  598. public function to_array() {
  599. $output = array(
  600. 'email_address' => $this->email_address,
  601. 'first_name' => $this->first_name,
  602. 'last_name' => $this->last_name,
  603. 'job_title' => $this->job_title,
  604. 'company_name' => $this->company_name,
  605. 'create_source' => $this->create_source,
  606. 'birthday_month' => $this->birthday_month,
  607. 'birthday_day' => $this->birthday_day,
  608. 'anniversary' => $this->anniversary,
  609. 'custom_fields' => $this->custom_fields,
  610. 'phone_numbers' => $this->phone_numbers,
  611. 'street_addresses' => $this->street_addresses,
  612. 'list_memberships' => $this->list_memberships,
  613. );
  614. return array_filter( $output );
  615. }
  616. public function get_email_address() {
  617. if ( isset( $this->email_address['address'] ) ) {
  618. return $this->email_address['address'];
  619. }
  620. return '';
  621. }
  622. public function set_email_address( $address, $permission_to_send = '' ) {
  623. if ( ! wpcf7_is_email( $address )
  624. or 80 < $this->strlen( $address ) ) {
  625. return false;
  626. }
  627. $types_of_permission = array(
  628. 'implicit', 'explicit', 'deprecate', 'pending',
  629. 'unsubscribe', 'temp_hold', 'not_set',
  630. );
  631. if ( ! in_array( $permission_to_send, $types_of_permission ) ) {
  632. $permission_to_send = 'implicit';
  633. }
  634. return $this->email_address = array(
  635. 'address' => $address,
  636. 'permission_to_send' => $permission_to_send,
  637. );
  638. }
  639. public function set_first_name( $first_name ) {
  640. $first_name = trim( $first_name );
  641. if ( empty( $first_name )
  642. or 50 < $this->strlen( $first_name ) ) {
  643. return false;
  644. }
  645. return $this->first_name = $first_name;
  646. }
  647. public function set_last_name( $last_name ) {
  648. $last_name = trim( $last_name );
  649. if ( empty( $last_name )
  650. or 50 < $this->strlen( $last_name ) ) {
  651. return false;
  652. }
  653. return $this->last_name = $last_name;
  654. }
  655. public function set_job_title( $job_title ) {
  656. $job_title = trim( $job_title );
  657. if ( empty( $job_title )
  658. or 50 < $this->strlen( $job_title ) ) {
  659. return false;
  660. }
  661. return $this->job_title = $job_title;
  662. }
  663. public function set_company_name( $company_name ) {
  664. $company_name = trim( $company_name );
  665. if ( empty( $company_name )
  666. or 50 < $this->strlen( $company_name ) ) {
  667. return false;
  668. }
  669. return $this->company_name = $company_name;
  670. }
  671. public function set_create_source( $create_source ) {
  672. if ( ! in_array( $create_source, array( 'Contact', 'Account' ) ) ) {
  673. return false;
  674. }
  675. return $this->create_source = $create_source;
  676. }
  677. public function set_birthday( $month, $day ) {
  678. $month = (int) $month;
  679. $day = (int) $day;
  680. if ( $month < 1 || 12 < $month
  681. or $day < 1 || 31 < $day ) {
  682. return false;
  683. }
  684. $this->birthday_month = $month;
  685. $this->birthday_day = $day;
  686. return array( $this->birthday_month, $this->birthday_day );
  687. }
  688. public function set_anniversary( $anniversary ) {
  689. $pattern = sprintf(
  690. '#^(%s)$#',
  691. implode( '|', array(
  692. '\d{1,2}/\d{1,2}/\d{4}',
  693. '\d{4}/\d{1,2}/\d{1,2}',
  694. '\d{4}-\d{1,2}-\d{1,2}',
  695. '\d{1,2}-\d{1,2}-\d{4}',
  696. ) )
  697. );
  698. if ( ! preg_match( $pattern, $anniversary ) ) {
  699. return false;
  700. }
  701. return $this->anniversary = $anniversary;
  702. }
  703. public function add_custom_field( $custom_field_id, $value ) {
  704. $uuid_pattern = '/^[0-9a-f-]+$/i';
  705. $value = trim( $value );
  706. if ( 25 <= count( $this->custom_fields )
  707. or ! preg_match( $uuid_pattern, $custom_field_id )
  708. or 255 < $this->strlen( $value ) ) {
  709. return false;
  710. }
  711. return $this->custom_fields[] = array(
  712. 'custom_field_id' => $custom_field_id,
  713. 'value' => $value,
  714. );
  715. }
  716. public function add_phone_number( $phone_number, $kind = 'home' ) {
  717. $phone_number = trim( $phone_number );
  718. if ( empty( $phone_number )
  719. or ! wpcf7_is_tel( $phone_number )
  720. or 25 < $this->strlen( $phone_number )
  721. or 2 <= count( $this->phone_numbers )
  722. or ! in_array( $kind, array( 'home', 'work', 'other' ) ) ) {
  723. return false;
  724. }
  725. return $this->phone_numbers[] = array(
  726. 'phone_number' => $phone_number,
  727. 'kind' => $kind,
  728. );
  729. }
  730. public function add_street_address( $street, $city, $state, $postal_code, $country, $kind = 'home' ) {
  731. $street = trim( $street );
  732. $city = trim( $city );
  733. $state = trim( $state );
  734. $postal_code = trim( $postal_code );
  735. $country = trim( $country );
  736. if ( ! ( $street || $city || $state || $postal_code || $country )
  737. or 1 <= count( $this->street_addresses )
  738. or ! in_array( $kind, array( 'home', 'work', 'other' ) )
  739. or 255 < $this->strlen( $street )
  740. or 50 < $this->strlen( $city )
  741. or 50 < $this->strlen( $state )
  742. or 50 < $this->strlen( $postal_code )
  743. or 50 < $this->strlen( $country ) ) {
  744. return false;
  745. }
  746. return $this->street_addresses[] = array(
  747. 'kind' => $kind,
  748. 'street' => $street,
  749. 'city' => $city,
  750. 'state' => $state,
  751. 'postal_code' => $postal_code,
  752. 'country' => $country,
  753. );
  754. }
  755. public function add_list_membership( $list_id ) {
  756. $uuid_pattern = '/^[0-9a-f-]+$/i';
  757. if ( 50 <= count( $this->list_memberships )
  758. or ! preg_match( $uuid_pattern, $list_id ) ) {
  759. return false;
  760. }
  761. return $this->list_memberships[] = $list_id;
  762. }
  763. protected function strlen( $string ) {
  764. return wpcf7_count_code_units( stripslashes( $string ) );
  765. }
  766. }