Skip to content

Commit 0e2c9fa

Browse files
committed
Remove the inlined scripts and css and queue up properly.
1 parent e515fd1 commit 0e2c9fa

File tree

2 files changed

+109
-121
lines changed

2 files changed

+109
-121
lines changed

settings-page.php

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,11 @@
11
<?php
2-
3-
$settings = [];
4-
$settings['store_id'] = get_option( 'WC_settings_wootoapp_site_id' );
5-
$settings['secret_key'] = get_option( 'WC_settings_wootoapp_secret_key' );
6-
7-
$force_use_local = true;
8-
$force_use_local_db = false;
9-
$force_use_prod_db = true;
10-
$force_use_prod = true;
11-
$dev_url = "www.wooc.local";
12-
$use_local_react = $_SERVER['HTTP_HOST'] === $dev_url || $force_use_local;//true;
13-
$use_prod_db = ($force_use_local_db || ( $_SERVER['HTTP_HOST'] === $dev_url)) ? false : true;
14-
15-
if($force_use_prod_db){
16-
$use_prod_db = true;
17-
}
18-
if ( $force_use_prod ) {
19-
$use_local_react = false;
20-
}
21-
22-
$using_some_dev_params = !!($force_use_local || $force_use_local_db);
23-
$args = array(
24-
'taxonomy' => "product_cat",
25-
);
26-
$product_categories = get_terms( $args );
27-
28-
29-
$paypal_email = "";
30-
31-
$paypal_opts = get_option("woocommerce_paypal_settings");
32-
if($paypal_opts){
33-
$paypal_email = $paypal_opts['email'];
34-
}
2+
if ( ! defined( 'ABSPATH' ) ) {
3+
exit;
4+
} // Exit if accessed directly
355

366
?>
377

38-
<?php if( $using_some_dev_params): ?>
39-
<h1>WARNING - DEV MODE</h1>
40-
<?php endif; ?>
41-
<link rel="stylesheet" href="https://app.wootoapp.com/wta-wc-react.css">
428
<div id="wta-root">
439

4410
</div>
45-
<script type="text/javascript">
46-
window.WooToApp = {
47-
auth: {
48-
id: '<?php echo str_replace("\'", "", $settings['store_id']); ?>',
49-
secret_key: '<?php echo str_replace( "\'", "", $settings['secret_key']); ?>'
50-
},
51-
environment: "<?php echo $use_prod_db ? "prod" : "dev"; ?>",
52-
has_dev_params: <?php echo $using_some_dev_params ? "true" : "false";?>,
53-
categories: <?php echo json_encode($product_categories); ?>,
54-
pages: <?php echo json_encode(get_pages()); ?>,
55-
woo_currencies: <?php echo json_encode(get_woocommerce_currencies()); ?>,
56-
currency: "<?php echo get_woocommerce_currency(); ?>",
57-
paypal_email: "<?php echo $paypal_email; ?>"
58-
}
59-
60-
window.WooToApp.log = window.WooToApp.environment == "prod" ? function(){} : console.log;
61-
62-
console.log(window.WooToApp);
63-
64-
</script>
65-
66-
67-
68-
<?php if( $use_local_react): ?>
69-
<script type="text/javascript" src="http://localhost:3000/static/js/bundle.js"></script>
70-
71-
<?php else: ?>
72-
<script type="text/javascript" src="https://app.wootoapp.com/wta-wc-react.js"></script>
73-
74-
<?php endif; ?>
7511

woo-to-app.php

Lines changed: 106 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
along 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+
2832
add_action( 'plugins_loaded', 'wta_init', 0 );
2933

3034
function 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

Comments
 (0)