1+ <?php
2+ /*
3+ Plugin Name: Woo To App Store Plugin
4+ Plugin URI: https://www.wootoapp.com
5+ Description: Enables various functionality required by Woo To App
6+ Version: 0.0.1
7+ Author: WooToApp
8+ Author URI: https://www.wootoapp.com
9+
10+ Copyright: © 2017 WooToApp
11+ */
12+
13+ add_action ( 'plugins_loaded ' , 'wta_init ' , 0 );
14+
15+
16+ function safe_json_encode ( $ mixed , $ missing = "TRANSLIT " ) {
17+ $ out = json_encode ( $ mixed );
18+ if ( $ err = json_last_error () ) {
19+ iconv_r ( "UTF-8 " , "UTF-8// $ missing " , $ mixed );
20+ $ out = json_encode ( $ mixed );
21+ }
22+
23+ return $ out ;
24+ }
25+ function iconv_r ( $ charset_i , $ charset_o , &$ mixed ) {
26+ if ( is_string ( $ mixed ) ) {
27+ $ mixed = iconv ( $ charset_i , $ charset_o , $ mixed );
28+ } else {
29+ if ( is_object ( $ mixed ) ) {
30+ $ mixed = (array ) $ mixed ;
31+ }
32+ if ( is_array ( $ mixed ) ) {
33+ foreach ( $ mixed as $ key => &$ value ) {
34+ iconv_r ( $ charset_i , $ charset_o , $ value );
35+ }
36+ }
37+ }
38+ }
39+
40+
41+
42+ function wta_init () {
43+ class WooToApp {
44+ private static $ _instance = null ;
45+
46+ protected $ user = null ;
47+ protected $ coupon = null ;
48+
49+ public static function instance () {
50+ if ( is_null ( self ::$ _instance ) ) {
51+ self ::$ _instance = new self ();
52+ }
53+
54+ return self ::$ _instance ;
55+ }
56+
57+ public function __construct () {
58+
59+
60+ /* endpoints */
61+ add_action ( 'wp_ajax_wootoapp_execute ' , array ( $ this , 'wootoapp_execute_callback ' ) );
62+ add_action ( 'wp_ajax_nopriv_wootoapp_execute ' , array ( $ this , 'wootoapp_execute_callback ' ) );
63+
64+
65+ add_filter ( 'woocommerce_settings_tabs_array ' , array ( $ this , 'add_settings_tab ' ), 50 );
66+ add_action ( 'woocommerce_settings_tabs_settings_wootoapp ' , array ( $ this , 'settings_tab ' ) );
67+ add_action ( 'woocommerce_update_options_settings_wootoapp ' , array ( $ this , 'update_settings ' ) );
68+
69+
70+ header ( 'Access-Control-Allow-Credentials:true ' );
71+ header ( 'Access-Control-Allow-Headers:Authorization, Content-Type ' );
72+ header ( 'Access-Control-Allow-Methods:OPTIONS, GET, POST, PUT, PATCH, DELETE ' );
73+ header ( 'Access-Control-Allow-Origin: * ' );
74+ header ( 'Allow: GET ' );
75+
76+ if ( $ _SERVER ['REQUEST_METHOD ' ] == 'OPTIONS ' ) {
77+ header ( 'Access-Control-Allow-Origin: * ' );
78+ header ( 'Access-Control-Allow-Headers: X-Requested-With, Authorization, Content-Type ' );
79+ header ( "HTTP/1.1 200 OK " );
80+ die ();
81+ }
82+ /* END endpoints */
83+
84+ }
85+
86+
87+ public function add_settings_tab ( $ settings_tabs ) {
88+ $ settings_tabs ['settings_wootoapp ' ] = __ ( 'WooToApp ' , 'woocommerce-settings-tab-wootoapp ' );
89+
90+ return $ settings_tabs ;
91+ }
92+
93+ public function settings_tab () {
94+ // woocommerce_admin_fields( self::get_settings() );
95+
96+
97+ include_once ("settings-page.php " );
98+ ?>
99+
100+
101+ <?php
102+
103+ }
104+
105+ public static function update_settings () {
106+ woocommerce_update_options ( self ::get_settings () );
107+ }
108+
109+ public static function get_settings () {
110+
111+ $ settings = array (
112+ 'wc_wootoapp_section_title ' => array (
113+ 'name ' => __ ( 'Settings ' , 'woocommerce-settings-tab-wootoapp ' ),
114+ 'type ' => 'title ' ,
115+ 'desc ' => '' ,
116+ 'id ' => 'WC_settings_wootoapp_section_title '
117+ ),
118+ 'wc_wootoapp_site_id ' => array (
119+ 'name ' => __ ( 'Enter your Site ID ' , 'woocommerce-settings-tab-wootoapp ' ),
120+ 'type ' => 'text ' ,
121+ 'desc ' => __ ( 'This will be on your intro email. ' ,
122+ 'woocommerce-settings-tab-wootoapp ' ),
123+ 'desc_tip ' => true ,
124+ 'id ' => 'WC_settings_wootoapp_site_id '
125+ ),
126+ 'wc_wootoapp_secret_key ' => array (
127+ 'name ' => __ ( 'Enter your Secret Key ' , 'woocommerce-settings-tab-wootoapp ' ),
128+ 'type ' => 'text ' ,
129+ 'css ' => 'min-width:350px; ' ,
130+ 'desc ' => __ ( 'This will be on your intro email. ' ,
131+ 'woocommerce-settings-tab-wootoapp ' ),
132+ 'desc_tip ' => true ,
133+ 'id ' => 'WC_settings_wootoapp_secret_key '
134+ ),
135+ 'wc_wootoapp_logging_enabled ' => array (
136+ 'name ' => __ ( 'Enable Logging? ' , 'woocommerce-settings-tab-wootoapp ' ),
137+ 'type ' => 'checkbox ' ,
138+ 'id ' => 'WC_settings_wootoapp_logging_enabled '
139+ ),
140+ 'wc_wootoapp_livemode_enabled ' => array (
141+ 'name ' => __ ( 'Enable Live Mode? (YES if unsure) ' , 'woocommerce-settings-tab-wootoapp ' ),
142+ 'type ' => 'checkbox ' ,
143+ 'id ' => 'WC_settings_wootoapp_livemode_enabled '
144+ ),
145+ 'wc_wootoapp_section_end ' => array (
146+ 'type ' => 'sectionend ' ,
147+ 'id ' => 'WC_settings_wootoapp_section_end '
148+ )
149+ );
150+
151+ return apply_filters ( 'WC_settings_wootoapp_settings ' , $ settings );
152+ }
153+
154+ public static function log ( $ message ) {
155+ if ( empty ( self ::$ log ) ) {
156+ self ::$ log = new WC_Logger ();
157+ }
158+
159+ if ( get_option ( "WC_settings_wootoapp_livemode_enabled " ) === "yes " ) {
160+ self ::$ log ->add ( 'WooToApp ' , $ message );
161+ }
162+ //
163+ }
164+
165+ public function wootoapp_execute_callback (){
166+ $ user = $ this ->get_authenticated_user ();
167+
168+ if ($ user ){
169+ $ method = $ _GET ['method ' ];
170+
171+ echo json_encode ($ this ->execute_callback_authenticated ($ method , $ user ));
172+ }
173+ else {
174+ echo json_encode ( ['error ' =>'Could not authenticate ' ]);
175+ }
176+ die ();
177+ }
178+
179+ public function execute_callback_authenticated ($ method , $ user ){
180+
181+ global $ wpdb ;
182+
183+ $ request = json_decode ( file_get_contents ( 'php://input ' ), true );;
184+ switch ($ method ){
185+ case "get_shipping_quotation " :
186+ $ line_items = $ request ['line_items ' ];
187+ $ user_id = $ request ['user_id ' ];
188+
189+
190+
191+ wp_set_current_user ( $ user_id );
192+ $ shipping_methods = $ this ->_get_shipping_methods ( $ line_items );
193+
194+ return [ 'shipping_methods ' => $ shipping_methods , 'user_id ' =>$ user_id ];
195+ break ;
196+ }
197+ }
198+
199+ public function get_authenticated_user (){
200+ global $ wpdb ;
201+
202+ $ consumer_key = $ _SERVER ['PHP_AUTH_USER ' ];
203+ $ consumer_secret = $ _SERVER ['PHP_AUTH_PW ' ];
204+
205+ $ user = $ this ->get_user_data_by_consumer_key ( $ consumer_key );
206+
207+ if ( ! hash_equals ( $ user ->consumer_secret , $ consumer_secret ) ) {
208+
209+
210+ return false ;
211+ }
212+
213+ return $ user ;
214+
215+ }
216+
217+ public function _add_items_to_cart ($ line_items , $ c ){
218+ foreach ( $ line_items as $ item ) {
219+ $ c ->add_to_cart ( $ item ['product_id ' ], (int ) $ item ['quantity ' ], 0 , [], [] );
220+ }
221+ }
222+ /**
223+ * @param array $quotation
224+ *
225+ * @return array
226+ */
227+ public function _get_shipping_methods ($ line_items ) {
228+ $ c = WC ()->cart ;
229+ $ c ->empty_cart ();
230+ $ cust = new WC_Customer ( wp_get_current_user ()->ID );
231+
232+ WC ()->customer = $ cust ;
233+
234+ $ this ->_add_items_to_cart ( $ line_items , $ c );
235+
236+ WC ()->cart ->calculate_shipping ();
237+ do_action ( 'woocommerce_cart_totals_before_shipping ' );
238+
239+ $ packages = WC ()->shipping ->get_packages ();
240+ do_action ( 'woocommerce_cart_totals_after_shipping ' );
241+
242+ $ package = $ packages [0 ];
243+ $ rates = $ package ['rates ' ];
244+
245+ $ methods_out = [];
246+
247+
248+ if ( count ( $ rates ) > 0 ) {
249+ foreach ( $ rates as $ shipping_option ) {
250+ $ methods_out [] = array (
251+ 'label ' => $ shipping_option ->label ,
252+ 'amount ' => number_format ( floatval ( $ shipping_option ->cost ), 2 ),
253+ 'detail ' => '' ,
254+ 'identifier ' => $ shipping_option ->id
255+ );
256+ }
257+ }
258+
259+
260+ $ c ->calculate_shipping ();
261+
262+ return $ methods_out ;
263+ }
264+
265+
266+ /** ------------------------------------------------ **/
267+
268+ private function get_user_data_by_consumer_key ( $ consumer_key ) {
269+ global $ wpdb ;
270+
271+ $ consumer_key = wc_api_hash ( sanitize_text_field ( $ consumer_key ) );
272+ $ user = $ wpdb ->get_row ( $ wpdb ->prepare ( "
273+ SELECT key_id, user_id, permissions, consumer_key, consumer_secret, nonces
274+ FROM {$ wpdb ->prefix }woocommerce_api_keys
275+ WHERE consumer_key = %s
276+ " , $ consumer_key ) );
277+
278+ return $ user ;
279+ }
280+
281+
282+ }
283+
284+ $ WooToApp = new WooToApp ();
285+ }
0 commit comments