2525along with Connector for WooToApp Mobile. If not, see https://www.gnu.org/licenses/gpl-2.0.html.
2626*/
2727
28+ if ( ! defined ( 'ABSPATH ' ) ) {
29+ exit ;
30+ } // Exit if accessed directly
31+
2832add_action ( 'plugins_loaded ' , 'wta_init ' , 0 );
2933
3034function wta_init () {
@@ -55,6 +59,7 @@ public function __construct() {
5559 add_action ( 'woocommerce_update_options_settings_wootoapp ' , array ( $ this , 'update_settings ' ) );
5660
5761
62+
5863 if (isset ( $ _REQUEST ['action ' ]) && $ _REQUEST ['action ' ] === "wootoapp_execute " ){
5964 header ( 'Access-Control-Allow-Credentials:true ' );
6065 header ( 'Access-Control-Allow-Headers:Authorization, Content-Type ' );
@@ -74,6 +79,53 @@ public function __construct() {
7479
7580 }
7681
82+ public function wta_css_and_js () {
83+ $ settings = [];
84+ $ settings ['store_id ' ] = get_option ( 'WC_settings_wootoapp_site_id ' );
85+ $ settings ['secret_key ' ] = get_option ( 'WC_settings_wootoapp_secret_key ' );
86+
87+ $ args = array (
88+ 'taxonomy ' => "product_cat " ,
89+ );
90+ $ product_categories = get_terms ( $ args );
91+
92+ $ paypal_email = "" ;
93+
94+ $ paypal_opts = get_option ( "woocommerce_paypal_settings " );
95+ if ( $ paypal_opts ) {
96+ $ paypal_email = $ paypal_opts ['email ' ];
97+ }
98+
99+ $ store_id = str_replace ( "\' " , "" , $ settings ['store_id ' ] );
100+ $ secret_key = str_replace ( "\' " , "" , $ settings ['secret_key ' ] );
101+ $ cats_json = json_encode ( $ product_categories );
102+ $ currency = json_encode ( get_woocommerce_currencies () );
103+ $ pages = json_encode ( get_pages () );
104+
105+ wp_register_script ( 'wta_js ' , "https://app.wootoapp.com/wta-wc-react.js " );
106+ wp_add_inline_script ( 'wta_js ' , <<<EOF
107+ window.WooToApp = {
108+ auth: {
109+ id: ' {$ store_id }',
110+ secret_key: ' {$ secret_key }'
111+ },
112+ environment: "prod",
113+ has_dev_params: false,
114+ categories: $ cats_json,
115+ pages: $ pages,
116+ woo_currencies: $ currency,
117+ currency: "<?php echo get_woocommerce_currency(); ?>",
118+ paypal_email: " $ paypal_email"
119+ }
120+
121+ window.WooToApp.log = window.WooToApp.environment == "prod" ? function(){} : console.log;
122+ EOF
123+ , "before " );
124+
125+
126+ wp_enqueue_style ( 'wta_css ' , "https://app.wootoapp.com/wta-wc-react.css " );
127+ wp_enqueue_script ( 'wta_js ' );
128+ }
77129
78130 public function add_settings_tab ( $ settings_tabs ) {
79131 $ settings_tabs ['settings_wootoapp ' ] = __ ( 'WooToApp ' , 'woocommerce-settings-tab-wootoapp ' );
@@ -82,6 +134,7 @@ public function add_settings_tab( $settings_tabs ) {
82134 }
83135
84136 public function settings_tab () {
137+ $ this ->wta_css_and_js ();
85138 include_once ("settings-page.php " );
86139 }
87140
@@ -159,6 +212,58 @@ public function wootoapp_execute_callback(){
159212 die ();
160213 }
161214
215+ public function execute_callback_authenticated ( $ method , $ user ) {
216+
217+ global $ wpdb ;
218+
219+ $ request = json_decode ( file_get_contents ( 'php://input ' ), true );;
220+ switch ( $ method ) {
221+ case "user_for_email " :
222+ $ u = get_user_by ( "email " , $ request ['email ' ] );
223+ wp_send_json ( [ 'user ' => $ u , 'email_supplied ' => $ request ['email ' ] ] );
224+
225+ break ;
226+ case "authenticate " :
227+ $ creds = array ();
228+ $ creds ['user_login ' ] = $ request ["email " ];
229+ $ creds ['user_password ' ] = $ request ["password " ];
230+
231+ $ user = wp_signon ( $ creds , false );
232+
233+ if ( $ user ) {
234+ if ( $ user ->errors ) {
235+ wp_send_json ( [ 'result ' => false , 'user ' => null , 'errors ' => $ user ->errors ] );
236+
237+ } else {
238+ wp_send_json ( [ 'result ' => true , 'user ' => $ user ] );
239+
240+ }
241+ }
242+ wp_send_json ( [ 'result ' => false , 'user ' => null ] );
243+
244+ break ;
245+
246+ case "get_shipping_quotation " :
247+ $ line_items = $ request ['line_items ' ];
248+ $ user_id = $ request ['user_id ' ];
249+
250+
251+ wp_set_current_user ( $ user_id );
252+ $ shipping_methods = $ this ->_get_shipping_methods ( $ line_items );
253+
254+ return [ 'shipping_methods ' => $ shipping_methods , 'user_id ' => $ user_id ];
255+ break ;
256+ case "send_password_reset_email " :
257+ $ email = $ request ['email ' ];
258+
259+
260+ echo $ this ->reset_email ( $ email ) ? "true " : "false " ;
261+
262+ echo "ok3 " ;
263+ break ;
264+ }
265+ }
266+
162267 public function reset_email ($ email ){
163268
164269
@@ -212,60 +317,6 @@ public function reset_email($email){
212317
213318 }
214319
215- public function execute_callback_authenticated ($ method , $ user ){
216-
217- global $ wpdb ;
218-
219- $ request = json_decode ( file_get_contents ( 'php://input ' ), true );;
220- switch ($ method ){
221- case "user_for_email " :
222- $ u = get_user_by ("email " , $ request ['email ' ]);
223- wp_send_json ( [ 'user ' =>$ u , 'email_supplied ' =>$ request ['email ' ] ] );
224-
225- break ;
226- case "authenticate " :
227- $ creds = array ();
228- $ creds ['user_login ' ] = $ request ["email " ];
229- $ creds ['user_password ' ] = $ request ["password " ];
230-
231- $ user = wp_signon ( $ creds , false );
232-
233- if ($ user ){
234- if ($ user ->errors ){
235- wp_send_json ( [ 'result ' => false , 'user ' => null , 'errors ' =>$ user ->errors ] );
236-
237- }
238- else {
239- wp_send_json ( [ 'result ' => true , 'user ' => $ user ] );
240-
241- }
242- }
243- wp_send_json ( [ 'result ' =>false , 'user ' => null ] );
244-
245- break ;
246-
247- case "get_shipping_quotation " :
248- $ line_items = $ request ['line_items ' ];
249- $ user_id = $ request ['user_id ' ];
250-
251-
252-
253- wp_set_current_user ( $ user_id );
254- $ shipping_methods = $ this ->_get_shipping_methods ( $ line_items );
255-
256- return [ 'shipping_methods ' => $ shipping_methods , 'user_id ' =>$ user_id ];
257- break ;
258- case "send_password_reset_email " :
259- $ email = $ request ['email ' ];
260-
261-
262- echo $ this ->reset_email ($ email ) ? "true " : "false " ;
263-
264- echo "ok3 " ;
265- break ;
266- }
267- }
268-
269320 public function get_authenticated_user (){
270321 global $ wpdb ;
271322
@@ -289,6 +340,7 @@ public function _add_items_to_cart($line_items, $c){
289340 $ c ->add_to_cart ( $ item ['product_id ' ], (int ) $ item ['quantity ' ], 0 , [], [] );
290341 }
291342 }
343+
292344 /**
293345 * @param array $quotation
294346 *
0 commit comments