1+ <?php
2+
3+ namespace printapp \functions \front ;
4+
5+ function customize_button () {
6+ global $ post ;
7+ $ printapp_domain_key = get_option ('print_app_domain_key ' );
8+
9+ // load the client-class script...
10+ $ lang_code = substr (get_bloginfo ('language ' ), 0 , 2 );
11+ if (!$ lang_code ) $ lang_code = 'en ' ;
12+ $ run_url = PRINT_APP_RUN_BASE_URL . $ printapp_domain_key . '/ ' . $ post ->ID . '/wp?lang= ' . $ lang_code ;
13+ wp_enqueue_script ('print_app_class ' , $ run_url , '' , '' , true );
14+
15+ // get user data
16+ $ user_data = get_user_data ();
17+
18+ if (!session_id ()) session_start ();
19+
20+ // get project data
21+ if (session_id ()) {
22+ $ session_key = PRINT_APP_SESSION_PREFIX . $ post ->ID ;
23+ if (isset ($ _SESSION [$ session_key ])) $ project_data = $ _SESSION [$ session_key ];
24+ }
25+
26+ // initialize the data to pass to print_app_class
27+ $ pa_project_id = '' ;
28+ $ pa_previews = '' ;
29+ $ pa_mode = 'new-project ' ;
30+ $ pa_user_id = (get_current_user_id () === 0 ) ? 'guest ' : get_current_user_id ();
31+
32+ if (isset ($ project_data )) {
33+ $ pa_project_id = $ project_data ['projectId ' ];
34+ $ pa_mode = isset ($ project_data ['mode ' ]) ? $ project_data ['mode ' ] : 'edit-project ' ;
35+ $ pa_previews = isset ($ project_data ['previews ' ]) ? json_encode ($ project_data ['previews ' ]) : '' ;
36+ }
37+
38+ wp_localize_script ('print_app_class ' , 'printAppParams ' , array (
39+ 'mode ' => $ pa_mode ,
40+ 'langCode ' => $ lang_code ,
41+ 'previews ' => $ pa_previews ,
42+ 'projectId ' => $ pa_project_id ,
43+ 'pluginRoot ' => site_url () . '/print_app ' ,
44+ 'product ' => array (
45+ 'id ' => $ post ->ID ,
46+ 'name ' => $ post ->post_name
47+ ),
48+ 'userId ' => $ pa_user_id ,
49+ 'launchData ' => $ user_data ,
50+ 'wp_ajax_url ' => admin_url ('admin-ajax.php ' ),
51+ ));
52+
53+ echo '<div id="pa-buttons"></div> ' ;
54+ }
55+
56+ function get_user_data () {
57+ if (!is_user_logged_in ()) return 'null ' ;
58+
59+ $ customer = WC ()->customer ;
60+ $ current_user = wp_get_current_user ();
61+
62+ $ fname = esc_js ($ customer ->get_billing_first_name ());
63+ $ lname = esc_js ($ customer ->get_billing_last_name ());
64+
65+ $ address = $ customer ->get_billing_address_1 () . "<br> " ;
66+ if ( !empty ($ customer ->get_billing_address_2 ()) ) {
67+ $ address .= $ customer ->get_billing_address_2 () . "<br> " ;
68+ }
69+ $ address .= $ customer ->get_billing_city () . " " . $ customer ->get_billing_postcode () . "<br> " ;
70+ if ( !empty ($ customer ->get_billing_state ()) ) {
71+ $ address .= $ customer ->get_billing_state () . "<br> " ;
72+ }
73+ $ address .= $ customer ->get_billing_country ();
74+ $ address = esc_js ($ address );
75+
76+ return "{
77+ email: ' " . esc_js ($ current_user ->user_email ) . "',
78+ name: ' {$ fname } {$ lname }',
79+ firstname: ' {$ fname }',
80+ lastname: ' {$ lname }',
81+ phone: ' " . esc_js ($ customer ->get_billing_phone ()) . "',
82+ address: ' {$ address }'.split('<br>').join(' \\n')
83+ } " ;
84+ }
0 commit comments