constant-contact.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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( 'email' => $email ),
  242. 'https://api.cc.email/v3/contacts'
  243. );
  244. $request = array(
  245. 'method' => 'GET',
  246. 'headers' => array(
  247. 'Accept' => 'application/json',
  248. 'Content-Type' => 'application/json; charset=utf-8',
  249. ),
  250. );
  251. $response = $this->remote_request( $endpoint, $request );
  252. if ( 400 <= (int) wp_remote_retrieve_response_code( $response ) ) {
  253. if ( WP_DEBUG ) {
  254. $this->log( $endpoint, $request, $response );
  255. }
  256. return false;
  257. }
  258. $response_body = wp_remote_retrieve_body( $response );
  259. if ( empty( $response_body ) ) {
  260. return false;
  261. }
  262. $response_body = json_decode( $response_body, true );
  263. return ! empty( $response_body['contacts'] );
  264. }
  265. public function create_contact( $properties ) {
  266. $endpoint = 'https://api.cc.email/v3/contacts';
  267. $request = array(
  268. 'method' => 'POST',
  269. 'headers' => array(
  270. 'Accept' => 'application/json',
  271. 'Content-Type' => 'application/json; charset=utf-8',
  272. ),
  273. 'body' => json_encode( $properties ),
  274. );
  275. $response = $this->remote_request( $endpoint, $request );
  276. if ( 400 <= (int) wp_remote_retrieve_response_code( $response ) ) {
  277. if ( WP_DEBUG ) {
  278. $this->log( $endpoint, $request, $response );
  279. }
  280. return false;
  281. }
  282. }
  283. public function get_contact_lists() {
  284. $endpoint = 'https://api.cc.email/v3/contact_lists';
  285. $request = array(
  286. 'method' => 'GET',
  287. 'headers' => array(
  288. 'Accept' => 'application/json',
  289. 'Content-Type' => 'application/json; charset=utf-8',
  290. ),
  291. );
  292. $response = $this->remote_request( $endpoint, $request );
  293. if ( 400 <= (int) wp_remote_retrieve_response_code( $response ) ) {
  294. if ( WP_DEBUG ) {
  295. $this->log( $endpoint, $request, $response );
  296. }
  297. return false;
  298. }
  299. $response_body = wp_remote_retrieve_body( $response );
  300. if ( empty( $response_body ) ) {
  301. return false;
  302. }
  303. $response_body = json_decode( $response_body, true );
  304. if ( ! empty( $response_body['lists'] ) ) {
  305. return (array) $response_body['lists'];
  306. } else {
  307. return array();
  308. }
  309. }
  310. public function update_contact_lists( $selection = array() ) {
  311. $contact_lists = array();
  312. $contact_lists_on_api = $this->get_contact_lists();
  313. if ( false !== $contact_lists_on_api ) {
  314. foreach ( (array) $contact_lists_on_api as $list ) {
  315. if ( isset( $list['list_id'] ) ) {
  316. $list_id = trim( $list['list_id'] );
  317. } else {
  318. continue;
  319. }
  320. if ( isset( $this->contact_lists[$list_id]['selected'] ) ) {
  321. $list['selected'] = $this->contact_lists[$list_id]['selected'];
  322. } else {
  323. $list['selected'] = array();
  324. }
  325. $contact_lists[$list_id] = $list;
  326. }
  327. } else {
  328. $contact_lists = $this->contact_lists;
  329. }
  330. foreach ( (array) $selection as $key => $ids_or_names ) {
  331. foreach( $contact_lists as $list_id => $list ) {
  332. if ( in_array( $list['list_id'], (array) $ids_or_names, true )
  333. or in_array( $list['name'], (array) $ids_or_names, true ) ) {
  334. $contact_lists[$list_id]['selected'][$key] = true;
  335. } else {
  336. unset( $contact_lists[$list_id]['selected'][$key] );
  337. }
  338. }
  339. }
  340. $this->contact_lists = $contact_lists;
  341. if ( $selection ) {
  342. $this->save_data();
  343. }
  344. return $this->contact_lists;
  345. }
  346. public function admin_notice( $message = '' ) {
  347. switch ( $message ) {
  348. case 'success':
  349. echo sprintf(
  350. '<div class="updated notice notice-success is-dismissible"><p>%s</p></div>',
  351. esc_html( __( "Connection established.", 'contact-form-7' ) )
  352. );
  353. break;
  354. case 'failed':
  355. echo sprintf(
  356. '<div class="error notice notice-error is-dismissible"><p><strong>%1$s</strong>: %2$s</p></div>',
  357. esc_html( __( "ERROR", 'contact-form-7' ) ),
  358. esc_html( __( "Failed to establish connection. Please double-check your configuration.", 'contact-form-7' ) )
  359. );
  360. break;
  361. case 'updated':
  362. echo sprintf(
  363. '<div class="updated notice notice-success is-dismissible"><p>%s</p></div>',
  364. esc_html( __( "Configuration updated.", 'contact-form-7' ) )
  365. );
  366. break;
  367. }
  368. }
  369. public function display( $action = '' ) {
  370. echo '<p>' . sprintf(
  371. 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' ) ),
  372. wpcf7_link(
  373. __(
  374. 'https://contactform7.com/constant-contact-integration/',
  375. 'contact-form-7'
  376. ),
  377. __( 'Constant Contact Integration', 'contact-form-7' )
  378. )
  379. ) . '</p>';
  380. if ( $this->is_active() ) {
  381. echo sprintf(
  382. '<p class="dashicons-before dashicons-yes">%s</p>',
  383. esc_html( __( "This site is connected to the Constant Contact API.", 'contact-form-7' ) )
  384. );
  385. }
  386. if ( 'setup' == $action ) {
  387. $this->display_setup();
  388. } else {
  389. echo sprintf(
  390. '<p><a href="%1$s" class="button">%2$s</a></p>',
  391. esc_url( $this->menu_page_url( 'action=setup' ) ),
  392. esc_html( __( 'Setup Integration', 'contact-form-7' ) )
  393. );
  394. }
  395. }
  396. private function display_setup() {
  397. ?>
  398. <form method="post" action="<?php echo esc_url( $this->menu_page_url( 'action=setup' ) ); ?>">
  399. <?php wp_nonce_field( 'wpcf7-constant-contact-setup' ); ?>
  400. <table class="form-table">
  401. <tbody>
  402. <tr>
  403. <th scope="row"><label for="client_id"><?php echo esc_html( __( 'API Key', 'contact-form-7' ) ); ?></label></th>
  404. <td><?php
  405. if ( $this->is_active() ) {
  406. echo esc_html( $this->client_id );
  407. echo sprintf(
  408. '<input type="hidden" value="%1$s" id="client_id" name="client_id" />',
  409. esc_attr( $this->client_id )
  410. );
  411. } else {
  412. echo sprintf(
  413. '<input type="text" aria-required="true" value="%1$s" id="client_id" name="client_id" class="regular-text code" />',
  414. esc_attr( $this->client_id )
  415. );
  416. }
  417. ?></td>
  418. </tr>
  419. <tr>
  420. <th scope="row"><label for="client_secret"><?php echo esc_html( __( 'App Secret', 'contact-form-7' ) ); ?></label></th>
  421. <td><?php
  422. if ( $this->is_active() ) {
  423. echo esc_html( wpcf7_mask_password( $this->client_secret ) );
  424. echo sprintf(
  425. '<input type="hidden" value="%1$s" id="client_secret" name="client_secret" />',
  426. esc_attr( $this->client_secret )
  427. );
  428. } else {
  429. echo sprintf(
  430. '<input type="text" aria-required="true" value="%1$s" id="client_secret" name="client_secret" class="regular-text code" />',
  431. esc_attr( $this->client_secret )
  432. );
  433. }
  434. ?></td>
  435. </tr>
  436. <tr>
  437. <th scope="row"><label for="redirect_uri"><?php echo esc_html( __( 'Redirect URI', 'contact-form-7' ) ); ?></label></th>
  438. <td><?php
  439. echo sprintf(
  440. '<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;" />',
  441. $this->get_redirect_uri()
  442. );
  443. ?>
  444. <p class="description"><?php echo esc_html( __( "Set this URL as the redirect URI.", 'contact-form-7' ) ); ?></p>
  445. </td>
  446. </tr>
  447. </tbody>
  448. </table>
  449. <?php
  450. if ( $this->is_active() ) {
  451. submit_button(
  452. _x( 'Reset Keys', 'API keys', 'contact-form-7' ),
  453. 'small', 'reset'
  454. );
  455. } else {
  456. submit_button(
  457. __( 'Connect to the Constant Contact API', 'contact-form-7' )
  458. );
  459. }
  460. ?>
  461. </form>
  462. <?php
  463. if ( $this->is_active() and ! empty( $this->contact_lists ) ) {
  464. ?>
  465. <form method="post" action="<?php echo esc_url( $this->menu_page_url( 'action=edit' ) ); ?>">
  466. <?php wp_nonce_field( 'wpcf7-constant-contact-edit' ); ?>
  467. <table class="form-table">
  468. <tbody>
  469. <tr>
  470. <th scope="row"><?php echo esc_html( _x( 'Contact Lists', 'Constant Contact', 'contact-form-7' ) ); ?></th>
  471. <td>
  472. <fieldset>
  473. <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>
  474. <p class="description"><?php echo esc_html( __( "Select lists to which newly added contacts are to belong.", 'contact-form-7' ) ); ?></p>
  475. <ul class="checkboxes"><?php
  476. foreach ( $this->contact_lists as $list ) {
  477. echo sprintf(
  478. '<li><input %1$s /> <label for="%2$s">%3$s</label></li>',
  479. wpcf7_format_atts( array(
  480. 'type' => 'checkbox',
  481. 'name' => 'contact_lists[]',
  482. 'value' => $list['list_id'],
  483. 'id' => 'contact_list_' . $list['list_id'],
  484. 'checked' => empty( $list['selected']['default'] )
  485. ? ''
  486. : 'checked',
  487. ) ),
  488. esc_attr( 'contact_list_' . $list['list_id'] ),
  489. esc_html( $list['name'] )
  490. );
  491. }
  492. ?></ul>
  493. </fieldset>
  494. </td>
  495. </tr>
  496. </tbody>
  497. </table>
  498. <?php
  499. submit_button();
  500. }
  501. ?>
  502. </form>
  503. <?php
  504. }
  505. }
  506. class WPCF7_ConstantContact_ContactPostRequest {
  507. private $email_address;
  508. private $first_name;
  509. private $last_name;
  510. private $job_title;
  511. private $company_name;
  512. private $create_source;
  513. private $birthday_month;
  514. private $birthday_day;
  515. private $anniversary;
  516. private $custom_fields = array();
  517. private $phone_numbers = array();
  518. private $street_addresses = array();
  519. private $list_memberships = array();
  520. public function __construct() {
  521. }
  522. public function build( WPCF7_Submission $submission ) {
  523. $this->set_create_source( 'Contact' );
  524. $posted_data = (array) $submission->get_posted_data();
  525. if ( isset( $posted_data['your-first-name'] ) ) {
  526. $this->set_first_name( $posted_data['your-first-name'] );
  527. }
  528. if ( isset( $posted_data['your-last-name'] ) ) {
  529. $this->set_last_name( $posted_data['your-last-name'] );
  530. }
  531. if ( ! ( $this->first_name || $this->last_name )
  532. and isset( $posted_data['your-name'] ) ) {
  533. $your_name = preg_split( '/[\s]+/', $posted_data['your-name'], 2 );
  534. $this->set_first_name( array_shift( $your_name ) );
  535. $this->set_last_name( array_shift( $your_name ) );
  536. }
  537. if ( isset( $posted_data['your-email'] ) ) {
  538. $this->set_email_address( $posted_data['your-email'], 'implicit' );
  539. }
  540. if ( isset( $posted_data['your-job-title'] ) ) {
  541. $this->set_job_title( $posted_data['your-job-title'] );
  542. }
  543. if ( isset( $posted_data['your-company-name'] ) ) {
  544. $this->set_company_name( $posted_data['your-company-name'] );
  545. }
  546. if ( isset( $posted_data['your-birthday-month'] )
  547. and isset( $posted_data['your-birthday-day'] ) ) {
  548. $this->set_birthday(
  549. $posted_data['your-birthday-month'],
  550. $posted_data['your-birthday-day']
  551. );
  552. } elseif ( isset( $posted_data['your-birthday'] ) ) {
  553. $date = trim( $posted_data['your-birthday'] );
  554. if ( preg_match( '/^(\d{4})-(\d{2})-(\d{2})$/', $date, $matches ) ) {
  555. $this->set_birthday( $matches[2], $matches[3] );
  556. }
  557. }
  558. if ( isset( $posted_data['your-anniversary'] ) ) {
  559. $this->set_anniversary( $posted_data['your-anniversary'] );
  560. }
  561. if ( isset( $posted_data['your-phone-number'] ) ) {
  562. $this->add_phone_number( $posted_data['your-phone-number'] );
  563. }
  564. $this->add_street_address(
  565. isset( $posted_data['your-address-street'] )
  566. ? $posted_data['your-address-street'] : '',
  567. isset( $posted_data['your-address-city'] )
  568. ? $posted_data['your-address-city'] : '',
  569. isset( $posted_data['your-address-state'] )
  570. ? $posted_data['your-address-state'] : '',
  571. isset( $posted_data['your-address-postal-code'] )
  572. ? $posted_data['your-address-postal-code'] : '',
  573. isset( $posted_data['your-address-country'] )
  574. ? $posted_data['your-address-country'] : ''
  575. );
  576. $service_option = (array) WPCF7::get_option( 'constant_contact' );
  577. $contact_lists = isset( $service_option['contact_lists'] )
  578. ? $service_option['contact_lists'] : array();
  579. $contact_form = $submission->get_contact_form();
  580. if ( $contact_form->additional_setting( 'constant_contact' ) ) {
  581. $key = sprintf( 'wpcf7_contact_form:%d', $contact_form->id() );
  582. } else {
  583. $key = 'default';
  584. }
  585. foreach ( (array) $contact_lists as $list ) {
  586. if ( ! empty( $list['selected'][$key] ) ) {
  587. $this->add_list_membership( $list['list_id'] );
  588. }
  589. }
  590. }
  591. public function is_valid() {
  592. return $this->create_source
  593. && ( $this->email_address || $this->first_name || $this->last_name );
  594. }
  595. public function to_array() {
  596. $output = array(
  597. 'email_address' => $this->email_address,
  598. 'first_name' => $this->first_name,
  599. 'last_name' => $this->last_name,
  600. 'job_title' => $this->job_title,
  601. 'company_name' => $this->company_name,
  602. 'create_source' => $this->create_source,
  603. 'birthday_month' => $this->birthday_month,
  604. 'birthday_day' => $this->birthday_day,
  605. 'anniversary' => $this->anniversary,
  606. 'custom_fields' => $this->custom_fields,
  607. 'phone_numbers' => $this->phone_numbers,
  608. 'street_addresses' => $this->street_addresses,
  609. 'list_memberships' => $this->list_memberships,
  610. );
  611. return array_filter( $output );
  612. }
  613. public function get_email_address() {
  614. if ( isset( $this->email_address['address'] ) ) {
  615. return $this->email_address['address'];
  616. }
  617. return '';
  618. }
  619. public function set_email_address( $address, $permission_to_send = '' ) {
  620. if ( ! wpcf7_is_email( $address )
  621. or 80 < $this->strlen( $address ) ) {
  622. return false;
  623. }
  624. $types_of_permission = array(
  625. 'implicit', 'explicit', 'deprecate', 'pending',
  626. 'unsubscribe', 'temp_hold', 'not_set',
  627. );
  628. if ( ! in_array( $permission_to_send, $types_of_permission ) ) {
  629. $permission_to_send = 'implicit';
  630. }
  631. return $this->email_address = array(
  632. 'address' => $address,
  633. 'permission_to_send' => $permission_to_send,
  634. );
  635. }
  636. public function set_first_name( $first_name ) {
  637. $first_name = trim( $first_name );
  638. if ( empty( $first_name )
  639. or 50 < $this->strlen( $first_name ) ) {
  640. return false;
  641. }
  642. return $this->first_name = $first_name;
  643. }
  644. public function set_last_name( $last_name ) {
  645. $last_name = trim( $last_name );
  646. if ( empty( $last_name )
  647. or 50 < $this->strlen( $last_name ) ) {
  648. return false;
  649. }
  650. return $this->last_name = $last_name;
  651. }
  652. public function set_job_title( $job_title ) {
  653. $job_title = trim( $job_title );
  654. if ( empty( $job_title )
  655. or 50 < $this->strlen( $job_title ) ) {
  656. return false;
  657. }
  658. return $this->job_title = $job_title;
  659. }
  660. public function set_company_name( $company_name ) {
  661. $company_name = trim( $company_name );
  662. if ( empty( $company_name )
  663. or 50 < $this->strlen( $company_name ) ) {
  664. return false;
  665. }
  666. return $this->company_name = $company_name;
  667. }
  668. public function set_create_source( $create_source ) {
  669. if ( ! in_array( $create_source, array( 'Contact', 'Account' ) ) ) {
  670. return false;
  671. }
  672. return $this->create_source = $create_source;
  673. }
  674. public function set_birthday( $month, $day ) {
  675. $month = (int) $month;
  676. $day = (int) $day;
  677. if ( $month < 1 || 12 < $month
  678. or $day < 1 || 31 < $day ) {
  679. return false;
  680. }
  681. $this->birthday_month = $month;
  682. $this->birthday_day = $day;
  683. return array( $this->birthday_month, $this->birthday_day );
  684. }
  685. public function set_anniversary( $anniversary ) {
  686. $pattern = sprintf(
  687. '#^(%s)$#',
  688. implode( '|', array(
  689. '\d{1,2}/\d{1,2}/\d{4}',
  690. '\d{4}/\d{1,2}/\d{1,2}',
  691. '\d{4}-\d{1,2}-\d{1,2}',
  692. '\d{1,2}-\d{1,2}-\d{4}',
  693. ) )
  694. );
  695. if ( ! preg_match( $pattern, $anniversary ) ) {
  696. return false;
  697. }
  698. return $this->anniversary = $anniversary;
  699. }
  700. public function add_custom_field( $custom_field_id, $value ) {
  701. $uuid_pattern = '/^[0-9a-f-]+$/i';
  702. $value = trim( $value );
  703. if ( 25 <= count( $this->custom_fields )
  704. or ! preg_match( $uuid_pattern, $custom_field_id )
  705. or 255 < $this->strlen( $value ) ) {
  706. return false;
  707. }
  708. return $this->custom_fields[] = array(
  709. 'custom_field_id' => $custom_field_id,
  710. 'value' => $value,
  711. );
  712. }
  713. public function add_phone_number( $phone_number, $kind = 'home' ) {
  714. $phone_number = trim( $phone_number );
  715. if ( 2 <= count( $this->phone_numbers )
  716. or ! wpcf7_is_tel( $phone_number )
  717. or 25 < $this->strlen( $phone_number )
  718. or ! in_array( $kind, array( 'home', 'work', 'other' ) ) ) {
  719. return false;
  720. }
  721. return $this->phone_numbers[] = array(
  722. 'phone_number' => $phone_number,
  723. 'kind' => $kind,
  724. );
  725. }
  726. public function add_street_address( $street, $city, $state, $postal_code, $country, $kind = 'home' ) {
  727. $street = trim( $street );
  728. $city = trim( $city );
  729. $state = trim( $state );
  730. $postal_code = trim( $postal_code );
  731. $country = trim( $country );
  732. if ( ! ( $street || $city || $state || $postal_code || $country )
  733. or 1 <= count( $this->street_addresses )
  734. or ! in_array( $kind, array( 'home', 'work', 'other' ) )
  735. or 255 < $this->strlen( $street )
  736. or 50 < $this->strlen( $city )
  737. or 50 < $this->strlen( $state )
  738. or 50 < $this->strlen( $postal_code )
  739. or 50 < $this->strlen( $country ) ) {
  740. return false;
  741. }
  742. return $this->street_addresses[] = array(
  743. 'kind' => $kind,
  744. 'street' => $street,
  745. 'city' => $city,
  746. 'state' => $state,
  747. 'postal_code' => $postal_code,
  748. 'country' => $country,
  749. );
  750. }
  751. public function add_list_membership( $list_id ) {
  752. $uuid_pattern = '/^[0-9a-f-]+$/i';
  753. if ( 50 <= count( $this->list_memberships )
  754. or ! preg_match( $uuid_pattern, $list_id ) ) {
  755. return false;
  756. }
  757. return $this->list_memberships[] = $list_id;
  758. }
  759. protected function strlen( $string ) {
  760. return wpcf7_count_code_units( stripslashes( $string ) );
  761. }
  762. }