|
1
|
|
|
<?php if ( !defined( 'ABSPATH' ) ) exit; |
|
2
|
|
|
/** |
|
3
|
|
|
* File defining class for addresses initialisation |
|
4
|
|
|
* |
|
5
|
|
|
* @author Eoxia developpement team <[email protected]> |
|
6
|
|
|
* @version 1.0 |
|
7
|
|
|
* @package Geolocalisation |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** Check if the plugin WPS_LOCALISATION_VERSION is defined. If not defined script will be stopped here */ |
|
11
|
|
|
if ( !defined( 'WPS_LOCALISATION_VERSION' ) ) { |
|
12
|
|
|
die( __("You are not allowed to use this service.", 'wpeo_geoloc') ); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Addresses initialisation class |
|
17
|
|
|
* |
|
18
|
|
|
* @author Eoxia developpement team <[email protected]> |
|
19
|
|
|
* @version 1.0 |
|
20
|
|
|
* @package Geolocalisation |
|
21
|
|
|
*/ |
|
22
|
|
|
class wps_address { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Initialise Addresses component management |
|
26
|
|
|
*/ |
|
27
|
|
|
function __construct() { |
|
|
|
|
|
|
28
|
|
|
/** Create customer entity type on wordpress initilisation*/ |
|
29
|
|
|
add_action( 'init', array( $this, 'create_addresses_entity' ) ); |
|
30
|
|
|
|
|
31
|
|
|
/** Add filters for addresses list */ |
|
32
|
|
|
add_filter( 'bulk_actions-edit-' . WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS, array( $this, 'addresses_list_table_bulk_actions' ) ); |
|
33
|
|
|
add_filter( 'manage_edit-' . WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS . '_columns', array( $this, 'list_table_header' ) ); |
|
34
|
|
|
add_action( 'manage_' . WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS . '_posts_custom_column' , array( $this, 'list_table_column_content' ), 10, 2 ); |
|
35
|
|
|
|
|
36
|
|
|
/** Filter search for customers */ |
|
37
|
|
|
add_filter( 'pre_get_posts', array( $this, 'addresses_custom_query' ) ); |
|
38
|
|
|
|
|
39
|
|
|
/** Load */ |
|
40
|
|
|
add_filter( 'wpshop_custom_template', array( &$this, 'custom_template_load' ) ); |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** Add shortocde listener for addresses */ |
|
44
|
|
|
add_shortcode( 'wps_address_list', array( &$this, 'get_addresses') ); |
|
45
|
|
|
add_shortcode( 'wps_addresses', array( &$this, 'display_addresses_interface') ); |
|
46
|
|
|
add_shortcode( 'wps_addresses_list', array( &$this, 'shortcode_display_addresses_list' ) ); |
|
47
|
|
|
|
|
48
|
|
|
/** Add listener for ajax actions */ |
|
49
|
|
|
add_action( 'wp_ajax_wps_load_address_form', array( &$this, 'wps_load_address_form') ); // DONE |
|
50
|
|
|
add_action( 'wp_ajax_wps_save_address', array( &$this, 'wps_save_address') ); // DONE |
|
51
|
|
|
add_action( 'wp_ajax_wps-address-edition-form-load', array( &$this, 'load_address_edition_form' ) ); // DONE |
|
52
|
|
|
add_action( 'wp_ajax_wps-address-display-an-address', array( &$this, 'display_address' ) ); // DONE |
|
|
|
|
|
|
53
|
|
|
// add_action( 'wap_ajax_wps-address-save-address', array( &$this, 'wps_save_address' ) ); |
|
54
|
|
|
add_action( 'wp_ajax_wps-address-display-list', array( &$this, 'display_addresses_list' ) ); // DONE |
|
55
|
|
|
add_action( 'wp_ajax_wps-address-add-new', array( &$this, 'display_address_adding_form' ) ); // DONE |
|
56
|
|
|
add_action( 'wp_ajax_wps_delete_an_address', array( &$this, 'wps_delete_an_address' ) ); // DONE |
|
57
|
|
|
add_action( 'wp_ajax_wps_reload_address_interface', array( &$this, 'wps_reload_address_interface' ) ); |
|
58
|
|
|
// add_action( 'wap_ajax_display_address_form', array( &$this, '') ); |
|
|
|
|
|
|
59
|
|
|
// add_action( 'wap_ajax_wps-add-an-address-in-admin', array( $this, 'wps_add_an_address_in_admin' ) ); |
|
60
|
|
|
|
|
61
|
|
|
/* Include the different javascript */ |
|
62
|
|
|
add_action( 'wp_enqueue_scripts', array( &$this, 'frontend_js' ) ); |
|
63
|
|
|
add_action( 'admin_enqueue_scripts', array( &$this, 'admin_scripts' ) ); |
|
64
|
|
|
|
|
65
|
|
|
/** Add addresses metaboxes to wordpress element */ |
|
66
|
|
|
add_action('add_meta_boxes', array( &$this, 'addresses_metaboxes'), 1); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
function shortcode_display_addresses_list( $args ) { |
|
|
|
|
|
|
70
|
|
|
$addresses = $this->get_addresses_list( $args[ 'id' ] ); |
|
|
|
|
|
|
71
|
|
|
require_once( wpshop_tools::get_template_part( WPS_LOCALISATION_DIR, WPS_LOCALISATION_TEMPLATES_MAIN_DIR, 'backend', 'addresses') ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Create the addresses entity |
|
76
|
|
|
*/ |
|
77
|
|
|
function create_addresses_entity() { |
|
|
|
|
|
|
78
|
|
|
global $wpdb; |
|
79
|
|
|
$query = $wpdb->prepare( "SELECT P.post_title, PM.meta_value FROM {$wpdb->posts} AS P INNER JOIN {$wpdb->postmeta} AS PM ON (PM.post_id = P.ID) WHERE P.post_name = %s AND PM.meta_key = %s", WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS, '_wpshop_entity_params' ); |
|
80
|
|
|
$entity_definition = $wpdb->get_row( $query ); |
|
81
|
|
|
$entity_params = !empty( $entity_definition ) && !empty( $entity_definition->meta_value ) ? unserialize( $entity_definition->meta_value ) : null; |
|
82
|
|
|
|
|
83
|
|
|
$post_type_params = array( |
|
84
|
|
|
'labels' => array( |
|
85
|
|
|
'name' => __( 'Addresses' , 'wpshop' ), |
|
86
|
|
|
'singular_name' => __( 'Address', 'wpshop' ), |
|
87
|
|
|
'add_new_item' => __( 'New address', 'wpshop' ), |
|
88
|
|
|
'add_new' => __( 'New address', 'wpshop' ), |
|
89
|
|
|
'edit_item' => __( 'Edit address', 'wpshop' ), |
|
90
|
|
|
'new_item' => __( 'New address', 'wpshop' ), |
|
91
|
|
|
'view_item' => __( 'View address', 'wpshop' ), |
|
92
|
|
|
'search_items' => __( 'Search in addresses', 'wpshop' ), |
|
93
|
|
|
'not_found' => __( 'No address found', 'wpshop' ), |
|
94
|
|
|
'not_found_in_trash' => __( 'No address founded in trash', 'wpshop' ), |
|
95
|
|
|
'parent_item_colon' => '', |
|
96
|
|
|
), |
|
97
|
|
|
'description' => '', |
|
98
|
|
|
'supports' => !empty($entity_params['support']) ? $entity_params['support'] : array( 'title' ), |
|
99
|
|
|
'hierarchical' => false, |
|
100
|
|
|
'public' => false, |
|
101
|
|
|
'show_ui' => false, |
|
102
|
|
|
'show_in_menu' => 'edit.php?post_type='.WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS, |
|
103
|
|
|
'show_in_nav_menus' => false, |
|
104
|
|
|
'show_in_admin_bar' => false, |
|
105
|
|
|
'can_export' => false, |
|
106
|
|
|
'has_archive' => false, |
|
107
|
|
|
'exclude_from_search' => true, |
|
108
|
|
|
'publicly_queryable' => false, |
|
109
|
|
|
'rewrite' => false, |
|
110
|
|
|
); |
|
111
|
|
|
register_post_type( WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS, $post_type_params ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Filter bulk actions into customer list table |
|
116
|
|
|
* |
|
117
|
|
|
* @param array $actions Current available actions list |
|
118
|
|
|
* |
|
119
|
|
|
* @return array The new action list to use into customer list table |
|
120
|
|
|
*/ |
|
121
|
|
|
function addresses_list_table_bulk_actions( $actions ){ |
|
|
|
|
|
|
122
|
|
|
unset( $actions[ 'edit' ] ); |
|
123
|
|
|
unset( $actions[ 'trash' ] ); |
|
124
|
|
|
|
|
125
|
|
|
return $actions; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Change the addresses list table header to display custom informations |
|
130
|
|
|
* |
|
131
|
|
|
* @param array $current_header The current header list displayed to filter and modify for new output |
|
132
|
|
|
* |
|
133
|
|
|
* @return array The new header to display |
|
134
|
|
|
*/ |
|
135
|
|
|
function list_table_header( $current_header ) { |
|
|
|
|
|
|
136
|
|
|
unset( $current_header['title'] ); |
|
137
|
|
|
unset( $current_header['date'] ); |
|
138
|
|
|
|
|
139
|
|
|
$current_header['address_identifier'] = __( 'Address ID', 'wpshop' ); |
|
140
|
|
|
$current_header['address_element_name'] = __( 'Element', 'wpshop' ); |
|
141
|
|
|
$current_header['address_type'] = __( 'Address type', 'wpshop' ); |
|
142
|
|
|
$current_header['address_content'] = __( 'Address', 'wpshop' ); |
|
143
|
|
|
|
|
144
|
|
|
return $current_header; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Display the content into list table column |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $column The column identifier to modify output for |
|
151
|
|
|
* @param integer $post_id The current post identifier |
|
152
|
|
|
*/ |
|
153
|
|
|
function list_table_column_content( $column, $post_id ) { |
|
|
|
|
|
|
154
|
|
|
global $wpdb; |
|
155
|
|
|
|
|
156
|
|
|
/** Get wp_users idenfifier from customer id */ |
|
157
|
|
|
$query = $wpdb->prepare( "SELECT PA.post_parent AS post_parent, PA.post_author AS post_author, PC.ID AS parent_id, PC.post_title AS post_title, PC.post_type AS post_type FROM {$wpdb->posts} AS PC INNER JOIN {$wpdb->posts} AS PA ON ( PA.post_parent = PC.ID ) WHERE PA.ID = %d", $post_id); |
|
158
|
|
|
$address_associated_element = $wpdb->get_row( $query ); |
|
159
|
|
|
|
|
160
|
|
|
/** SCOTCH - Define the associated element type - SCOTCH */ |
|
161
|
|
|
$address_associated_element_type = $address_associated_element->post_type; |
|
162
|
|
|
if ( $address_associated_element->post_parent == $address_associated_element->post_author ) { |
|
163
|
|
|
$address_associated_element_type = WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** Get the associated element definition */ |
|
167
|
|
|
$associated_element_definition = get_post_type_object( $address_associated_element_type ); |
|
168
|
|
|
|
|
169
|
|
|
/** Get address informations */ |
|
170
|
|
|
$address_meta = get_post_meta( $post_id ); |
|
171
|
|
|
|
|
172
|
|
|
/** Get user data */ |
|
173
|
|
|
switch ( $address_associated_element_type ) { |
|
174
|
|
|
case WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS: |
|
175
|
|
|
$associated_customer = get_userdata( $address_associated_element->post_author ); |
|
176
|
|
|
$customer_name_to_display = $associated_customer->display_name; |
|
177
|
|
|
if ( !empty( $associated_customer->first_name ) && !empty( $associated_customer->last_name ) ) { |
|
178
|
|
|
$customer_name_to_display = $associated_customer->last_name . ' ' . $associated_customer->first_name ; |
|
179
|
|
|
} |
|
180
|
|
|
$element_main_infos = ( !empty( $associated_element_definition ) && !empty( $associated_element_definition->labels ) && !empty( $associated_element_definition->labels->singular_name ) ? $associated_element_definition->labels->singular_name . ' - ' : "" ) . $address_associated_element->parent_id . ' - ' . $customer_name_to_display; |
|
181
|
|
|
break; |
|
182
|
|
|
|
|
183
|
|
|
default: |
|
184
|
|
|
$element_main_infos = ( !empty( $associated_element_definition ) && !empty( $associated_element_definition->labels ) && !empty( $associated_element_definition->labels->singular_name ) ? $associated_element_definition->labels->singular_name . ' - ' : "" ) . $address_associated_element->parent_id . ' - ' . $address_associated_element->post_title; |
|
185
|
|
|
break; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** Switch current column for custom case */ |
|
189
|
|
|
$use_template = true; |
|
190
|
|
|
switch ( $column ) { } |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
/** Require the template for displaying the current column */ |
|
193
|
|
|
if ( $use_template ) { |
|
194
|
|
|
$template = wpshop_tools::get_template_part( WPS_ADDRESS_DIR, WPS_LOCALISATION_TEMPLATES_MAIN_DIR, 'backend', 'addresses_listtable/' . $column ); |
|
195
|
|
|
if ( is_file( $template ) ) { |
|
196
|
|
|
require( $template ); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* WORDPRESS QUERY HOOK - Hook the query for displaying addresses list |
|
203
|
|
|
* |
|
204
|
|
|
* @param WP_Object $query The current query launched for retrieving addresses |
|
205
|
|
|
*/ |
|
206
|
|
|
function addresses_custom_query( $query ) { |
|
|
|
|
|
|
207
|
|
|
if( is_admin() && $query->query['post_type'] == WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS && $query->is_main_query() ) { |
|
208
|
|
|
add_filter( 'posts_orderby', array( $this, 'addresses_custom_query_order' ) ); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* WORDPRESS QUERY HOOK - QUERY ORDER - Change order for addresses: order list by parent ID |
|
214
|
|
|
* |
|
215
|
|
|
* @return WP_Object The order parameters |
|
216
|
|
|
*/ |
|
217
|
|
|
function addresses_custom_query_order() { |
|
|
|
|
|
|
218
|
|
|
global $wpdb; |
|
219
|
|
|
|
|
220
|
|
|
return "$wpdb->posts.post_parent DESC, ID DESC"; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Check in database if there are addresses associated to current post type |
|
225
|
|
|
* |
|
226
|
|
|
* @param string $post The current post type |
|
227
|
|
|
* |
|
228
|
|
|
* @since 1.0 - WPShop 1.3.7.0 |
|
229
|
|
|
*/ |
|
230
|
|
|
function addresses_metaboxes( $post ) { |
|
|
|
|
|
|
231
|
|
|
global $wpdb; |
|
232
|
|
|
|
|
233
|
|
|
$query = $wpdb->prepare( "SELECT ID FROM " . $wpdb->posts . " WHERE post_name = %s AND post_type = %s", $post, WPSHOP_NEWTYPE_IDENTIFIER_ENTITIES ); |
|
234
|
|
|
$parent = $wpdb->get_var( $query ); |
|
235
|
|
|
if ( !empty( $parent ) ) { |
|
236
|
|
|
$address_meta_box_checking = get_post_meta( $parent, '_wpshop_entity_attached_address', true); |
|
237
|
|
|
if ( !empty($address_meta_box_checking) ) { |
|
238
|
|
|
add_meta_box( 'wps_attached_addresses', __('Attached addresses', 'wpshop'), array( &$this, 'addresses_metaboxes_content' ), $post, 'normal', 'default' ); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Call the different element to display addresses into associated metaboxes into backend part |
|
245
|
|
|
* |
|
246
|
|
|
* @param object $post The current post definition |
|
247
|
|
|
* @param array $args A list of parameters allowing to specify the element to display |
|
248
|
|
|
* |
|
249
|
|
|
* @since 1.0 - WPShop 1.3.7.0 |
|
250
|
|
|
*/ |
|
251
|
|
|
function addresses_metaboxes_content( $post, $args ) { |
|
|
|
|
|
|
252
|
|
|
$addresses = self::get_addresses_list( $post->ID ); |
|
253
|
|
|
require( wpshop_tools::get_template_part( WPS_ADDRESS_DIR, WPS_LOCALISATION_TEMPLATES_MAIN_DIR, "backend", "address", "metabox") ); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Load the different javascript librairies |
|
258
|
|
|
* |
|
259
|
|
|
* @since 1.0 - WPShop 1.3.7.0 |
|
260
|
|
|
*/ |
|
261
|
|
|
function frontend_js() { |
|
|
|
|
|
|
262
|
|
|
wp_register_style( 'wps_address_frontend_css', WPS_ADDRESS_URL . '/assets/css/frontend.css' ); |
|
263
|
|
|
wp_enqueue_style( 'wps_address_frontend_css' ); |
|
264
|
|
|
wp_enqueue_script( 'wps_address_js', WPS_ADDRESS_URL . '/assets/frontend/js/wps_address.js', array( 'jquery', 'jquery-form' ) ); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
View Code Duplication |
function admin_scripts() { |
|
|
|
|
|
|
268
|
|
|
global $current_screen; |
|
269
|
|
|
if ( ! in_array( $current_screen->post_type, array( WPSHOP_NEWTYPE_IDENTIFIER_ORDER, WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS ), true ) ) |
|
270
|
|
|
return; |
|
271
|
|
|
|
|
272
|
|
|
wp_enqueue_script( 'wps_address_js', WPS_ADDRESS_URL . '/assets/backend/js/wps_address.js', array( 'jquery', 'jquery-form' ) ); |
|
273
|
|
|
wp_register_style( 'wps_address_backend_css', WPS_ADDRESS_URL . '/assets/backend/css/backend.css'/*, array( '' )*/ ); |
|
|
|
|
|
|
274
|
|
|
wp_enqueue_style( 'wps_address_backend_css' ); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** Load module/addon automatically to existing template list |
|
278
|
|
|
* |
|
279
|
|
|
* @param array $templates The current template definition |
|
280
|
|
|
* |
|
281
|
|
|
* @return array The template with new elements |
|
282
|
|
|
*/ |
|
283
|
|
View Code Duplication |
function custom_template_load( $templates ) { |
|
|
|
|
|
|
284
|
|
|
include( WPS_LOCALISATION_TEMPLATES_MAIN_DIR . 'wpshop/main_elements.tpl.php'); |
|
285
|
|
|
|
|
286
|
|
|
$wpshop_display = new wpshop_display(); |
|
287
|
|
|
$templates = $wpshop_display->add_modules_template_to_internal( $tpl_element, $templates ); |
|
|
|
|
|
|
288
|
|
|
unset($tpl_element); |
|
289
|
|
|
|
|
290
|
|
|
return $templates; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
|
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Get adress list for an user without hidden attributes |
|
298
|
|
|
* @param Integer $user_id |
|
299
|
|
|
* @return Ambigous <multitype:, mixed, string, boolean, unknown, string> |
|
300
|
|
|
*/ |
|
301
|
|
|
public static function get_addresses_list_no_hidden_attributes( $user_id ) { |
|
302
|
|
|
global $wpdb; |
|
303
|
|
|
$query = $wpdb->prepare( 'SELECT ID FROM '. $wpdb->posts . ' WHERE post_type = %s AND post_parent = %s', WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS, $user_id ); |
|
304
|
|
|
$addresses_ids = $wpdb->get_col( $query ); |
|
305
|
|
|
$address_entity_id = wpshop_entities::get_entity_identifier_from_code( WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS ); |
|
306
|
|
|
$addresses_result = array(); |
|
307
|
|
|
foreach( $addresses_ids as $address_id ) { |
|
308
|
|
|
$address_post_meta = get_post_meta( $address_id, '_wpshop_address_metadata', true ); |
|
309
|
|
|
$address_type_id = get_post_meta( $address_id, '_wpshop_address_attribute_set_id', true ); |
|
310
|
|
|
$query = $wpdb->prepare( 'SELECT attribute_id FROM '.WPSHOP_DBT_ATTRIBUTE_DETAILS . ' WHERE attribute_set_id = %d AND entity_type_id = %d ORDER BY position', $address_type_id, $address_entity_id ); |
|
311
|
|
|
$attributes_ids = $wpdb->get_col( $query ); |
|
312
|
|
|
if( !empty( $attributes_ids ) ) { |
|
313
|
|
|
foreach( $attributes_ids as $attributes_id ) { |
|
314
|
|
|
$attribute_def = wpshop_attributes::getElement( $attributes_id, '"valid"', 'id' ); |
|
315
|
|
|
if( !empty( $attribute_def ) && !empty( $address_post_meta[ $attribute_def->code ] ) && $attribute_def->frontend_input != 'hidden' ) { |
|
316
|
|
|
$addresses_result[ $address_type_id ][ $address_id ][ $attribute_def->code ]['label'] = stripslashes( __( $attribute_def->frontend_label , 'wpshop' ) ); |
|
317
|
|
|
$addresses_result[ $address_type_id ][ $address_id ][ $attribute_def->code ]['value'] = stripslashes( __( $address_post_meta[ $attribute_def->code ], 'wpshop' ) ); |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
return $addresses_result; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Get adress list for an user |
|
327
|
|
|
* @param Integer $user_id |
|
328
|
|
|
* @return Ambigous <multitype:, mixed, string, boolean, unknown, string> |
|
329
|
|
|
*/ |
|
330
|
|
|
public static function get_addresses_list( $user_id ) { |
|
331
|
|
|
global $wpdb; |
|
332
|
|
|
$addresses_list = array(); |
|
333
|
|
|
$query = $wpdb->prepare( 'SELECT ID FROM '. $wpdb->posts. ' WHERE post_type = %s AND post_parent = %s', WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS, $user_id ); |
|
334
|
|
|
$addresses = $wpdb->get_results( $query ); |
|
335
|
|
|
foreach( $addresses as $address ) { |
|
336
|
|
|
$address_post_meta = get_post_meta( $address->ID, '_wpshop_address_metadata', true); |
|
337
|
|
|
$address_type_post_meta = get_post_meta( $address->ID, '_wpshop_address_attribute_set_id', true); |
|
338
|
|
|
|
|
339
|
|
|
if( !empty($address_post_meta) && !empty($address_type_post_meta) ) { |
|
340
|
|
|
$addresses_list[$address_type_post_meta][$address->ID] = $address_post_meta; |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
return $addresses_list; |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** Display Address**/ |
|
347
|
|
|
public static function display_an_address( $address, $address_id = '', $address_type_id = '' ) { |
|
348
|
|
|
global $wpdb; |
|
349
|
|
|
$countries = unserialize(WPSHOP_COUNTRY_LIST); |
|
350
|
|
|
$output = ''; |
|
351
|
|
|
if ( !empty($address) ) { |
|
352
|
|
|
$has_model = false; |
|
|
|
|
|
|
353
|
|
|
|
|
354
|
|
|
/** Check if a model exists**/ |
|
355
|
|
|
if ( !empty($address_id) || !empty( $address_type_id ) ) { |
|
356
|
|
|
$address_type = ( !empty($address_type_id) ) ? $address_type_id : get_post_meta( $address_id, '_wpshop_address_attribute_set_id', true ); |
|
357
|
|
|
if( !empty($address_type) ) { |
|
358
|
|
|
/** Shipping & Billing option **/ |
|
359
|
|
|
$shipping_option = get_option( 'wpshop_shipping_address_choice' ); |
|
360
|
|
|
if ( !empty($shipping_option) && !empty($shipping_option['choice']) && $shipping_option['choice'] == $address_type && !empty($shipping_option['display_model']) ) { |
|
361
|
|
|
$display_model = $shipping_option['display_model']; |
|
362
|
|
|
$has_model = true; |
|
|
|
|
|
|
363
|
|
|
|
|
364
|
|
|
} |
|
365
|
|
|
else { |
|
366
|
|
|
$billing_option = get_option( 'wpshop_billing_address' ); |
|
367
|
|
View Code Duplication |
if ( !empty($billing_option) && !empty($billing_option['choice']) && $billing_option['choice'] == $address_type && !empty($billing_option['display_model']) ) { |
|
|
|
|
|
|
368
|
|
|
$display_model = $billing_option['display_model']; |
|
369
|
|
|
$has_model = true; |
|
|
|
|
|
|
370
|
|
|
} |
|
371
|
|
|
} |
|
372
|
|
|
} |
|
373
|
|
|
} |
|
374
|
|
|
$has_model = false; |
|
375
|
|
|
if ( $has_model ) { |
|
376
|
|
|
foreach( $display_model as $group_id => $group ) { |
|
|
|
|
|
|
377
|
|
|
foreach( $group as $att_id => $att ) { |
|
378
|
|
|
if ( !empty($att) ) { |
|
379
|
|
|
// Get attribute def |
|
380
|
|
|
$attribute_id = str_replace( 'attribute_', '' , $att ); |
|
381
|
|
|
if( !empty($attribute_id) ) { |
|
382
|
|
|
$attribute_def = wpshop_attributes::getElement( $attribute_id, '"valid"', 'id' ); |
|
383
|
|
|
if ( !empty($attribute_def) ) { |
|
384
|
|
|
if ( $attribute_def->frontend_input == 'select' ) { |
|
385
|
|
|
$query = $wpdb->prepare( 'SELECT value FROM '.WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS. ' WHERE id = %d',$address[ $attribute_def->code] ); |
|
386
|
|
|
$output .= '<strong>'.__( $attribute_def->frontend_label, 'wpshop').' :</strong> '.__( $wpdb->get_var( $query ), 'wpshop' ).' '; |
|
387
|
|
|
} |
|
388
|
|
|
elseif( $attribute_def->frontend_verification == 'country' ) { |
|
389
|
|
|
$output .= ( !empty($countries[ $address[ $attribute_def->code] ]) ) ? '<strong>'.__( $attribute_def->frontend_label, 'wpshop').' :</strong> '.__( $countries[ $address[ $attribute_def->code] ], 'wpshop' ).' ' : ''; |
|
390
|
|
|
} |
|
391
|
|
|
else { |
|
392
|
|
|
$output .= ( !empty($address[ $attribute_def->code]) ) ? '<strong>'.__( $attribute_def->frontend_label, 'wpshop').' :</strong> '.$address[ $attribute_def->code].' ' : ' '; |
|
393
|
|
|
} |
|
394
|
|
|
} |
|
395
|
|
|
//End Line |
|
396
|
|
|
$next_element = next( $display_model[$group_id] ); |
|
|
|
|
|
|
397
|
|
|
$end_line = strstr( $next_element, '-end-line-', true ); |
|
398
|
|
|
if ( !empty($end_line) && $end_line == 'wps-attribute' ) { |
|
399
|
|
|
$output .= '<br/>'; |
|
400
|
|
|
} |
|
401
|
|
|
} |
|
402
|
|
|
} |
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
} |
|
406
|
|
|
else { |
|
407
|
|
|
if( !empty($address_type) ) { |
|
408
|
|
|
$tmp_array = array(); |
|
409
|
|
|
$address_entity_id = wpshop_entities::get_entity_identifier_from_code( WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS ); |
|
410
|
|
|
$query = $wpdb->prepare( 'SELECT * FROM '.WPSHOP_DBT_ATTRIBUTE_DETAILS . ' WHERE attribute_set_id = %d AND entity_type_id = %d ORDER BY position', $address_type, $address_entity_id); |
|
411
|
|
|
$attributes_ids = $wpdb->get_results( $query ); |
|
412
|
|
|
if( !empty($attributes_ids) ) { |
|
413
|
|
|
foreach( $attributes_ids as $attributes_id ) { |
|
414
|
|
|
if ( !empty( $attributes_id->attribute_id ) ) { |
|
415
|
|
|
$attribute_def = wpshop_attributes::getElement( $attributes_id->attribute_id, '"valid"', 'id' ); |
|
416
|
|
|
if( $attribute_def->frontend_input != 'hidden') { |
|
417
|
|
|
if( !empty($attribute_def) && !empty($address[ $attribute_def->code ]) && $attribute_def->frontend_input != 'hidden' ) { |
|
418
|
|
|
$tmp_array[ $attribute_def->code]['label'] = stripslashes( __( $attribute_def->frontend_label , 'wpshop' ) ); |
|
419
|
|
|
|
|
420
|
|
|
if( $attribute_def->frontend_verification == 'country' ) { |
|
421
|
|
|
$tmp_array[ $attribute_def->code]['value'] = ( !empty($countries[ $address[ $attribute_def->code] ]) ) ? stripslashes( __( $countries[ $address[ $attribute_def->code] ], 'wpshop' ) ) : stripslashes( $address[ $attribute_def->code ] ); |
|
422
|
|
|
} |
|
423
|
|
|
elseif( in_array( $attribute_def->frontend_input, array('select', 'checkbox') ) ) { |
|
424
|
|
|
$query = $wpdb->prepare( 'SELECT label FROM '. WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS. ' WHERE id = %d', $address[ $attribute_def->code] ); |
|
425
|
|
|
$value = $wpdb->get_var( $query ); |
|
426
|
|
|
$tmp_array[ $attribute_def->code]['value'] = ( !empty($value) ) ? stripslashes( __( $value, 'wpshop' ) ) : ''; |
|
427
|
|
|
} |
|
428
|
|
|
else { |
|
429
|
|
|
$tmp_array[ $attribute_def->code]['value'] = stripslashes( __( $address[ $attribute_def->code ], 'wpshop' ) ); |
|
430
|
|
|
} |
|
431
|
|
|
} |
|
432
|
|
|
} |
|
433
|
|
|
} |
|
434
|
|
|
} |
|
435
|
|
|
$address = $tmp_array; |
|
436
|
|
|
} |
|
437
|
|
|
} |
|
438
|
|
|
foreach( $address as $element_code => $element_value ) { |
|
439
|
|
|
if( is_array($element_value) ) { |
|
440
|
|
|
$output .= '<span class="wps-'.$element_code.'"><strong>' .stripslashes( $element_value['label'] ). ' :</strong> ' .stripslashes( $element_value['value'] ). '</span>'; |
|
441
|
|
|
} |
|
442
|
|
|
else { |
|
443
|
|
|
$output .= '<span class="wps-'.$element_code.'">' .stripslashes( $element_value ). '</span>'; |
|
444
|
|
|
} |
|
445
|
|
|
} |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
} |
|
449
|
|
|
return $output; |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
/** |
|
453
|
|
|
* Get all addresses for current customer for display |
|
454
|
|
|
* |
|
455
|
|
|
* @param integer $address_type_id The current identifier of address type -> attribute_set_id |
|
456
|
|
|
* @param string $address_type A string allowing to display |
|
|
|
|
|
|
457
|
|
|
* |
|
458
|
|
|
* @return string The complete html output for customer addresses |
|
459
|
|
|
*/ |
|
460
|
|
|
function get_addresses_by_type( $address_type_id, $address_type_title, $args = array() ) { |
|
|
|
|
|
|
461
|
|
|
global $wpdb; |
|
462
|
|
|
/** Get current customer addresses list */ |
|
463
|
|
|
if ( is_admin() ) { |
|
464
|
|
|
$post = !empty( $_GET['post'] ) ? (array) $_GET['post'] : array(); |
|
465
|
|
|
$post = get_post( $post ); |
|
466
|
|
|
if ( !empty($post->post_parent) ) { |
|
467
|
|
|
$customer_id = $post->post_parent; |
|
468
|
|
|
} |
|
469
|
|
|
else { |
|
470
|
|
|
$customer_id = $post->post_author; |
|
471
|
|
|
} |
|
472
|
|
|
} |
|
473
|
|
|
else { |
|
474
|
|
|
$customer_id = get_current_user_id(); |
|
475
|
|
|
} |
|
476
|
|
|
|
|
477
|
|
|
$query = $wpdb->prepare(" |
|
478
|
|
|
SELECT ADDRESSES.ID |
|
479
|
|
|
FROM " . $wpdb->posts . " AS ADDRESSES |
|
480
|
|
|
INNER JOIN " . $wpdb->postmeta . " AS ADDRESSES_META ON (ADDRESSES_META.post_id = ADDRESSES.ID) |
|
481
|
|
|
WHERE ADDRESSES.post_type = %s |
|
482
|
|
|
AND ADDRESSES.post_parent = %d |
|
483
|
|
|
AND ADDRESSES_META.meta_key = %s |
|
484
|
|
|
AND ADDRESSES_META.meta_value = %d", |
|
485
|
|
|
WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS, $customer_id, '_'.WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS.'_attribute_set_id', $address_type_id); |
|
486
|
|
|
$addresses = $wpdb->get_results($query); |
|
487
|
|
|
$addresses_list = ''; |
|
488
|
|
|
|
|
489
|
|
|
|
|
490
|
|
|
/** Initialize */ |
|
491
|
|
|
$tpl_component = array(); |
|
492
|
|
|
$tpl_component['CUSTOMER_ADDRESS_TYPE_TITLE'] = ( !empty($args) && !empty($args['first']) && $args['first'] ) ? __('Your address', 'wpshop') : $address_type_title; |
|
493
|
|
|
$tpl_component['LOADING_ICON'] = WPSHOP_LOADING_ICON; |
|
494
|
|
|
$tpl_component['ADDRESS_BUTTONS'] = ''; |
|
495
|
|
|
if( count($addresses) > 0 ) { |
|
496
|
|
|
$tpl_component['ADD_NEW_ADDRESS_LINK'] = get_permalink(wpshop_tools::get_page_id(get_option('wpshop_myaccount_page_id'))) . (strpos(get_permalink(wpshop_tools::get_page_id(get_option('wpshop_myaccount_page_id'))), '?')===false ? '?' : '&'). 'action=add_address&type=' .$address_type_id; |
|
497
|
|
|
} |
|
498
|
|
|
else { |
|
499
|
|
|
$tpl_component['ADD_NEW_ADDRESS_LINK'] = get_permalink(wpshop_tools::get_page_id(get_option('wpshop_myaccount_page_id'))) . (strpos(get_permalink(wpshop_tools::get_page_id(get_option('wpshop_myaccount_page_id'))), '?')===false ? '?' : '&'). 'action=add_address&type=' .$address_type_id .'&first'; |
|
500
|
|
|
} |
|
501
|
|
|
$tpl_component['ADDRESS_TYPE'] = ( !empty($address_type_title) && ($address_type_title == __('Shipping address', 'wpshop'))) ? 'shipping_address' : 'billing_address'; |
|
502
|
|
|
$tpl_component['ADD_NEW_ADDRESS_TITLE'] = sprintf(__('Add a new %s', 'wpshop'), ( ( !empty($args) && !empty($args['first']) && $args['first'] ) ? __('address', 'wpshop') : $address_type_title )); |
|
503
|
|
|
|
|
504
|
|
|
|
|
505
|
|
|
/** Read customer list */ |
|
506
|
|
|
if( count($addresses) > 0 ) { |
|
507
|
|
|
/** Get the fields for addresses */ |
|
508
|
|
|
$address_fields = wps_address::get_addresss_form_fields_by_type($address_type_id); |
|
509
|
|
|
$first = true; |
|
510
|
|
|
$tpl_component['ADDRESS_COMBOBOX_OPTION'] = ''; |
|
511
|
|
|
$nb_of_addresses = 0; |
|
512
|
|
|
foreach ( $addresses as $address ) { |
|
513
|
|
|
// Display the addresses |
|
514
|
|
|
/** If there isn't address in SESSION we display the first address of list by default */ |
|
515
|
|
|
if ( empty($_SESSION[$tpl_component['ADDRESS_TYPE']]) && $first && !is_admin() ) { |
|
516
|
|
|
$address_id = $address->ID; |
|
517
|
|
|
if ( !is_admin() ) { |
|
518
|
|
|
$_SESSION[$tpl_component['ADDRESS_TYPE']] = $address->ID; |
|
519
|
|
|
} |
|
520
|
|
|
} |
|
521
|
|
|
else { |
|
522
|
|
|
$address_id = ( !empty($_SESSION[$tpl_component['ADDRESS_TYPE']]) ) ? $_SESSION[$tpl_component['ADDRESS_TYPE']] : ''; |
|
523
|
|
|
} |
|
524
|
|
|
$address_selected_infos = get_post_meta($address_id, '_'.WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS.'_metadata', true); |
|
525
|
|
|
$address_infos = get_post_meta($address->ID, '_'.WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS.'_metadata', true); |
|
526
|
|
|
|
|
527
|
|
|
|
|
528
|
|
|
if ( !empty($address_infos) ) { |
|
529
|
|
|
|
|
530
|
|
|
$tpl_component['ADDRESS_ID'] = $address->ID; |
|
531
|
|
|
/** If no address was selected, we select the first of the list **/ |
|
532
|
|
|
$tpl_component['CUSTOMER_ADDRESS_CONTENT'] = self::display_an_address($address_fields, $address_selected_infos, $address_id); |
|
533
|
|
|
$tpl_component['ADDRESS_BUTTONS'] = wpshop_display::display_template_element('addresses_box_actions_button_edit', $tpl_component); |
|
534
|
|
|
$tpl_component['choosen_address_LINK_EDIT'] = get_permalink(wpshop_tools::get_page_id(get_option('wpshop_myaccount_page_id'))) . (strpos(get_permalink(wpshop_tools::get_page_id(get_option('wpshop_myaccount_page_id'))), '?')===false ? '?' : '&') . 'action=editAddress&id='.$address_id; |
|
535
|
|
|
$tpl_component['DEFAULT_ADDRESS_ID'] = $address_id; |
|
536
|
|
|
$tpl_component['ADRESS_CONTAINER_CLASS'] = ' wpshop_customer_adress_container_' . $address->ID; |
|
537
|
|
|
$tpl_component['CUSTOMER_CHOOSEN_ADDRESS'] = wpshop_display::display_template_element('display_address_container', $tpl_component); |
|
538
|
|
|
if ( empty($tpl_component['CUSTOMER_ADDRESS_CONTENT']) ) { |
|
539
|
|
|
$tpl_component['CUSTOMER_CHOOSEN_ADDRESS'] = '<span style="color:red;">'.__('No data','wpshop').'</span>'; |
|
540
|
|
|
} |
|
541
|
|
|
|
|
542
|
|
|
$tpl_component['ADDRESS_COMBOBOX_OPTION'] .= '<option value="' .$address->ID. '" ' .( ( !empty($_SESSION[$tpl_component['ADDRESS_TYPE']]) && $_SESSION[$tpl_component['ADDRESS_TYPE']] == $address->ID) ? 'selected="selected"' : null). '>' . (!empty($address_infos['address_title']) ? $address_infos['address_title'] : $address_type_title) . '</option>'; |
|
543
|
|
|
$nb_of_addresses++; |
|
544
|
|
|
} |
|
545
|
|
|
$first = false; |
|
546
|
|
|
} |
|
547
|
|
|
$tpl_component['ADDRESS_COMBOBOX'] = ''; |
|
548
|
|
|
if ( !is_admin() ) { |
|
549
|
|
|
$tpl_component['ADDRESS_COMBOBOX'] = (!empty($tpl_component['ADDRESS_COMBOBOX_OPTION']) && ($nb_of_addresses > 1)) ? wpshop_display::display_template_element('addresses_type_combobox', $tpl_component) : ''; |
|
550
|
|
|
} |
|
551
|
|
|
} |
|
552
|
|
|
else { |
|
553
|
|
|
if ( !empty($args) && !empty($args['first']) && $args['first'] ) { |
|
554
|
|
|
$tpl_component['ADDRESS_TYPE'] = 'first_address'; |
|
555
|
|
|
} |
|
556
|
|
|
$tpl_component['ADDRESS_ID'] = 0; |
|
557
|
|
|
$tpl_component['DEFAULT_ADDRESS_ID'] = 0; |
|
558
|
|
|
$tpl_component['ADDRESS_COMBOBOX'] = ''; |
|
559
|
|
|
$tpl_component['CUSTOMER_CHOOSEN_ADDRESS'] = sprintf( __('You don\'t have any %s, %splease create a new one%s', 'wpshop'), ( (!empty($args) && !empty($args['first']) && $args['first']) ? __('address', 'wpshop') : strtolower($address_type_title) ) , '<a href="' . $tpl_component['ADD_NEW_ADDRESS_LINK'] . '" >', '</a>' ); |
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
|
|
$tpl_component['ADDRESS_BUTTONS'] .= wpshop_display::display_template_element('addresses_box_actions_button_new_address', $tpl_component); |
|
563
|
|
|
if ( !empty($args['only_display']) && ($args['only_display'] == 'yes') ) { |
|
564
|
|
|
$tpl_component['ADDRESS_BUTTONS'] = ''; |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
$addresses_list .= wpshop_display::display_template_element('display_addresses_by_type_container', $tpl_component); |
|
568
|
|
|
|
|
569
|
|
|
|
|
570
|
|
|
|
|
571
|
|
|
return $addresses_list; |
|
572
|
|
|
} |
|
573
|
|
|
|
|
574
|
|
|
|
|
575
|
|
|
|
|
576
|
|
|
/** |
|
577
|
|
|
* Build and return queried address form |
|
578
|
|
|
* @param integer $address_type_id |
|
579
|
|
|
* @param integer $address_id |
|
580
|
|
|
* @param integer $user_id |
|
581
|
|
|
* @return string |
|
582
|
|
|
*/ |
|
583
|
|
|
function loading_address_form( $address_type_id, $address_id = '', $user_id = '', $display_in_front = false ) { |
|
|
|
|
|
|
584
|
|
|
$response = '<div id="wps_address_error_container"></div>'; |
|
585
|
|
|
$response .= '<form id="wps_address_form_save" action="' .admin_url('admin-ajax.php'). '" method="post">'; |
|
586
|
|
|
$response .= '<input type="hidden" name="action" value="wps_save_address" />'; |
|
587
|
|
|
$response .= wp_nonce_field( 'wps_save_address', '_wpnonce', true, false ); |
|
588
|
|
|
$first_address_checking = false; |
|
589
|
|
|
|
|
590
|
|
|
$user_id = ( !empty($user_id) ) ? $user_id : get_current_user_id(); |
|
591
|
|
|
$customer_id = wps_customer_ctr::get_customer_id_by_author_id( $user_id ); |
|
592
|
|
|
if ( !empty($address_id) ) { |
|
593
|
|
|
$response .= self::display_form_fields($address_type_id, $address_id, '', '', array(), array(), array(), $customer_id); |
|
594
|
|
|
$title = __('Edit your address', 'wpshop'); |
|
595
|
|
|
} |
|
596
|
|
|
elseif($address_type_id) { |
|
597
|
|
|
$billing_option = get_option( 'wpshop_billing_address' ); |
|
598
|
|
|
|
|
599
|
|
|
$addresses = self::get_addresses_list( $user_id ); |
|
600
|
|
|
$list_addresses = ( !empty($addresses[ $billing_option['choice'] ]) ) ? $addresses[ $billing_option['choice'] ] : array(); |
|
601
|
|
|
$first_address_checking = ( empty( $list_addresses ) ) ? true : false; |
|
602
|
|
|
|
|
603
|
|
|
$response .= self::display_form_fields($address_type_id, '', '', '', array(), array(), array(), $customer_id ); |
|
604
|
|
|
$title = __('Add a new address', 'wpshop'); |
|
605
|
|
|
} |
|
606
|
|
|
|
|
607
|
|
|
/** Check if a billing address is already save **/ |
|
608
|
|
|
if ( $first_address_checking && $address_type_id != $billing_option['choice'] ) { |
|
|
|
|
|
|
609
|
|
|
$response .= '<div class="wps-form"><input name="wps-shipping-to-billing" id="wps-shipping-to-billing" checked="checked" type="checkbox" /> <label for="wps-shipping-to-billing">' .__( 'Use the same address for billing', 'wpshop' ). '</label></div>'; |
|
610
|
|
|
} |
|
611
|
|
|
|
|
612
|
|
|
$response .= '<button id="wps_submit_address_form" class="wps-bton-first-alignRight-rounded">' .__('Save', 'wpshop'). '</button>'; |
|
613
|
|
|
|
|
614
|
|
|
$response .= '</form>'; |
|
615
|
|
|
return array( $response, $title ); |
|
|
|
|
|
|
616
|
|
|
} |
|
617
|
|
|
|
|
618
|
|
|
/** |
|
619
|
|
|
* Generate an array with all fields for the address form construction. Classified by address type. |
|
620
|
|
|
* @param $typeof |
|
621
|
|
|
* @return array |
|
622
|
|
|
*/ |
|
623
|
|
|
public static function get_addresss_form_fields_by_type ( $typeof, $id ='' ) { |
|
624
|
|
|
$submit_billing_and_shipping_info = !empty( $_POST['submitbillingAndShippingInfo'] ) ? sanitize_key( $_POST['submitbillingAndShippingInfo'] ) : ''; |
|
625
|
|
|
|
|
626
|
|
|
$address = array(); |
|
|
|
|
|
|
627
|
|
|
$all_addresses = ''; |
|
628
|
|
|
$attribute = !empty( $_POST['attribute'] ) ? (array) $_POST['attribute'] : array(); |
|
629
|
|
|
/* Get the attribute set details in order to build the product interface */ |
|
630
|
|
|
|
|
631
|
|
|
$atribute_set_details = wpshop_attributes_set::getAttributeSetDetails($typeof, "'valid'"); |
|
632
|
|
|
if ( !empty($atribute_set_details) ) { |
|
633
|
|
|
foreach ($atribute_set_details as $productAttributeSetDetail) { |
|
|
|
|
|
|
634
|
|
|
$address = array(); |
|
635
|
|
|
$group_name = $productAttributeSetDetail['name']; |
|
636
|
|
|
|
|
637
|
|
|
if(count($productAttributeSetDetail['attribut']) >= 1){ |
|
638
|
|
|
foreach($productAttributeSetDetail['attribut'] as $attribute) { |
|
639
|
|
|
if(!empty($attribute->id)) { |
|
640
|
|
|
if ( !empty( $submit_billing_and_shipping_info ) ) { |
|
641
|
|
|
$value = $attribute[$typeof][$attribute->data_type][$attribute->code]; |
|
642
|
|
|
} |
|
643
|
|
|
else { |
|
644
|
|
|
$value = wpshop_attributes::getAttributeValueForEntityInSet($attribute->data_type, $attribute->id, wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS), (int)$id, array('intrinsic' => $attribute->is_intrinsic, 'backend_input' => $attribute->backend_input)); |
|
645
|
|
|
} |
|
646
|
|
|
$attribute_output_def = wpshop_attributes::get_attribute_field_definition( $attribute, $value, array() ); |
|
647
|
|
|
$attribute_output_def['id'] = 'address_' . $typeof . '_' .$attribute_output_def['id']; |
|
648
|
|
|
$address[str_replace( '-', '_', sanitize_title($group_name) ).'_'.$attribute->code] = $attribute_output_def; |
|
649
|
|
|
} |
|
650
|
|
|
} |
|
651
|
|
|
} |
|
652
|
|
|
|
|
653
|
|
|
$all_addresses[$productAttributeSetDetail['attribute_set_id']][$productAttributeSetDetail['id']]['name'] = $group_name; |
|
654
|
|
|
$all_addresses[$productAttributeSetDetail['attribute_set_id']][$productAttributeSetDetail['id']]['content'] = $address; |
|
655
|
|
|
$all_addresses[$productAttributeSetDetail['attribute_set_id']][$productAttributeSetDetail['id']]['id'] = str_replace('-', '_', sanitize_title($group_name)); |
|
656
|
|
|
$all_addresses[$productAttributeSetDetail['attribute_set_id']][$productAttributeSetDetail['id']]['attribute_set_id'] = $productAttributeSetDetail['attribute_set_id']; |
|
657
|
|
|
} |
|
658
|
|
|
} |
|
659
|
|
|
return $all_addresses; |
|
660
|
|
|
} |
|
661
|
|
|
|
|
662
|
|
|
|
|
663
|
|
|
|
|
664
|
|
|
/** Treat the differents fields of form and classified them by form |
|
665
|
|
|
* @return boolean |
|
666
|
|
|
*/ |
|
667
|
|
|
public static function save_address_infos( $attribute_set_id, $address_id_to_copy = 0, $address_info_to_copy = array(), $customer_id = false, $post_ID = false ) { |
|
668
|
|
|
global $wpdb; |
|
669
|
|
|
|
|
670
|
|
|
if( empty( $address_info_to_copy ) ) { |
|
671
|
|
|
$address_info_to_copy = (array)$_POST; |
|
672
|
|
|
} |
|
673
|
|
|
|
|
674
|
|
|
$adress_save_the_first = !empty( $address_info_to_copy['wps-address-save-the-first'] ) ? sanitize_text_field( $address_info_to_copy['wps-address-save-the-first'] ) : ''; |
|
|
|
|
|
|
675
|
|
|
|
|
676
|
|
|
$attribute = (array)$address_info_to_copy['attribute']; |
|
677
|
|
|
|
|
678
|
|
|
$type_of_form = (int)$address_info_to_copy['type_of_form']; |
|
|
|
|
|
|
679
|
|
|
$current_item_edited = !empty($address_info_to_copy['attribute'][$attribute_set_id]['item_id']) ? (int)wpshop_tools::varSanitizer($address_info_to_copy['attribute'][$attribute_set_id]['item_id']) : null; |
|
680
|
|
|
$current_attribute_set_id = !empty( $address_info_to_copy['current_attribute_set_id'] ) ? (int)$address_info_to_copy['current_attribute_set_id'] : ''; |
|
681
|
|
|
$shipping_to_billing = !empty( $address_info_to_copy['wps-shipping-to-billing'] ) ? sanitize_text_field( $address_info_to_copy['wps-shipping-to-billing'] ) : ''; |
|
682
|
|
|
$shipping_to_billing_id = !empty( $address_info_to_copy['wps-shipping-to-billing-id'] ) ? (int)$address_info_to_copy['wps-shipping-to-billing-id'] : $address_id_to_copy; |
|
683
|
|
|
|
|
684
|
|
|
// Create or update the post address |
|
685
|
|
|
// @TODO : REQUEST |
|
686
|
|
|
$post_parent = ''; |
|
|
|
|
|
|
687
|
|
|
$post_author = get_current_user_id(); |
|
688
|
|
|
$customer_id = !empty( $customer_id ) ? (int) $customer_id : ( !empty( $_REQUEST['user']['customer_id'] ) ? (int) $_REQUEST['user']['customer_id'] : 0 ); |
|
689
|
|
|
$post_ID = !empty( $post_ID ) ? (int) $post_ID : ( !empty( $_REQUEST['post_ID'] ) ? (int) $_REQUEST['post_ID'] : 0 ); |
|
690
|
|
|
|
|
691
|
|
|
if ( !empty($customer_id) ) { |
|
692
|
|
|
$customer = get_post( $customer_id ); |
|
693
|
|
|
$post_parent = $customer->post_author; |
|
694
|
|
|
$post_author = $customer->post_author; |
|
695
|
|
|
} |
|
696
|
|
|
elseif ( !empty( $post_ID ) ) { |
|
697
|
|
|
$post_parent = $post_ID; |
|
698
|
|
|
} |
|
699
|
|
|
else { |
|
700
|
|
|
$post_parent = get_current_user_id(); |
|
701
|
|
|
} |
|
702
|
|
|
$post_address = array( |
|
703
|
|
|
'post_author' => $post_author, |
|
704
|
|
|
'post_title' => !empty( $attribute ) && !empty( $attribute[$attribute_set_id] ) && !empty( $attribute[$attribute_set_id]['varchar'] ) && !empty( $attribute[$attribute_set_id]['varchar']['address_title'] ) ? $attribute[$attribute_set_id]['varchar']['address_title'] : '', |
|
705
|
|
|
'post_status' => 'draft', |
|
706
|
|
|
'post_name' => WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS, |
|
707
|
|
|
'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS, |
|
708
|
|
|
'post_parent'=> $post_parent |
|
709
|
|
|
); |
|
710
|
|
|
|
|
711
|
|
|
$edit_other_thing = true; |
|
|
|
|
|
|
712
|
|
|
|
|
713
|
|
|
if ( empty($current_item_edited) && (empty( $current_attribute_set_id ) || $current_attribute_set_id != $attribute_set_id )) { |
|
714
|
|
|
$current_item_edited = wp_insert_post( $post_address ); |
|
715
|
|
|
if ( is_admin()) { |
|
716
|
|
|
$attribute[$attribute_set_id]['item_id'] = $current_item_edited; |
|
717
|
|
|
} |
|
718
|
|
|
} |
|
719
|
|
|
else { |
|
720
|
|
|
$post_address['ID'] = $current_item_edited; |
|
721
|
|
|
wp_update_post( $post_address ); |
|
722
|
|
|
} |
|
723
|
|
|
|
|
724
|
|
|
/* Shipping to billing save */ |
|
725
|
|
|
if( !empty( $shipping_to_billing ) ) { |
|
726
|
|
|
$wps_shipping_to_billing = array( 'wps-shipping-to-billing' => $shipping_to_billing ); |
|
727
|
|
|
|
|
728
|
|
|
if( !empty( $shipping_to_billing_id ) ) { |
|
729
|
|
|
$wps_shipping_to_billing['wps-shipping-to-billing-id'] = $shipping_to_billing_id; |
|
730
|
|
|
} |
|
731
|
|
|
update_post_meta($current_item_edited, 'wps-shipping-to-billing', $wps_shipping_to_billing ); |
|
732
|
|
|
} |
|
733
|
|
|
|
|
734
|
|
|
//Update the post_meta of address |
|
735
|
|
|
update_post_meta($current_item_edited, WPSHOP_ADDRESS_ATTRIBUTE_SET_ID_META_KEY, $attribute_set_id); |
|
736
|
|
|
|
|
737
|
|
|
|
|
738
|
|
|
foreach ( $attribute[ $attribute_set_id ] as $type => $type_content) { |
|
739
|
|
|
$attribute_not_to_do = array(); |
|
740
|
|
|
if (is_array($type_content) ) { |
|
741
|
|
|
foreach ( $type_content as $code => $value) { |
|
742
|
|
|
$attribute_def = wpshop_attributes::getElement($code, "'valid'", 'code'); |
|
743
|
|
|
if ( !empty($attribute_def->_need_verification) && $attribute_def->_need_verification == 'yes' ) { |
|
744
|
|
|
$code_verif = $code.'2'; |
|
745
|
|
|
$attribute_not_to_do[] = $code_verif; |
|
746
|
|
|
if ( !empty($attributes[$code_verif] )) { |
|
747
|
|
|
unset($attributes[$code_verif]); |
|
748
|
|
|
} |
|
749
|
|
|
} |
|
750
|
|
|
if( !in_array($code, $attribute_not_to_do)) $attributes[$code] = $value; |
|
|
|
|
|
|
751
|
|
|
} |
|
752
|
|
|
} |
|
753
|
|
|
} |
|
754
|
|
|
|
|
755
|
|
|
$attributes = apply_filters( 'wps-address-coordinate-calculation', $attributes ); |
|
756
|
|
|
|
|
757
|
|
|
$result = wpshop_attributes::setAttributesValuesForItem( $current_item_edited, $attributes, false, '' ); |
|
758
|
|
|
$result['current_id'] = $current_item_edited; |
|
759
|
|
|
|
|
760
|
|
|
if( !empty($result['current_id']) ) { |
|
761
|
|
|
// Update $_SESSION[address type] |
|
762
|
|
|
$billing_option = get_option( 'wpshop_billing_address' ); |
|
763
|
|
|
if( !empty($billing_option) && !empty($billing_option['choice']) && $billing_option['choice'] == $attribute_set_id ) { |
|
764
|
|
|
$_SESSION['billing_address'] = $result['current_id']; |
|
765
|
|
|
} |
|
766
|
|
|
else { |
|
767
|
|
|
$_SESSION['shipping_address'] = $result['current_id']; |
|
768
|
|
|
} |
|
769
|
|
|
} |
|
770
|
|
|
|
|
771
|
|
|
return $result; |
|
772
|
|
|
} |
|
773
|
|
|
|
|
774
|
|
|
/** |
|
775
|
|
|
* Display the differents forms fields |
|
776
|
|
|
* @param string $type : Type of address |
|
777
|
|
|
* @param string $first : Customer first address ? |
|
778
|
|
|
* @param string $referer : Referer website page |
|
779
|
|
|
* @param string $admin : Display this form in admin panel |
|
|
|
|
|
|
780
|
|
|
*/ |
|
781
|
|
|
public static function display_form_fields($type, $id = '', $first = '', $referer = '', $special_values = array(), $options = array(), $display_for_admin = array(), $other_customer = '' ) { |
|
|
|
|
|
|
782
|
|
|
global $wpshop, $wpshop_form, $wpdb; |
|
783
|
|
|
|
|
784
|
|
|
$choosen_address = get_option('wpshop_billing_address'); |
|
785
|
|
|
$shipping_address = get_option('wpshop_shipping_address_choice'); |
|
786
|
|
|
$output_form_fields = $form_model = ''; |
|
787
|
|
|
|
|
788
|
|
|
$user_id = ( !empty($other_customer) ) ? $other_customer : get_current_user_id(); |
|
789
|
|
|
|
|
790
|
|
|
if ( empty($type) ) { |
|
791
|
|
|
$type = $choosen_address['choice']; |
|
792
|
|
|
} |
|
793
|
|
|
|
|
794
|
|
|
$result = wps_address::get_addresss_form_fields_by_type($type, $id); |
|
795
|
|
|
|
|
796
|
|
|
/** Check if it's shipping or billing **/ |
|
797
|
|
|
if ( $type == $choosen_address['choice'] ) { |
|
798
|
|
|
$form_model = ( !empty($choosen_address['display_model']) ) ? $choosen_address['display_model'] : null; |
|
799
|
|
|
} |
|
800
|
|
|
elseif( $type == $shipping_address['choice'] ) { |
|
801
|
|
|
$form_model = ( !empty($shipping_address['display_model']) ) ? $shipping_address['display_model'] : null; |
|
802
|
|
|
} |
|
803
|
|
|
|
|
804
|
|
|
|
|
805
|
|
|
$form = $result[$type]; |
|
806
|
|
|
// Take the post id to make the link with the post meta of address |
|
807
|
|
|
$values = array(); |
|
|
|
|
|
|
808
|
|
|
// take the address informations |
|
809
|
|
|
$current_item_edited = !empty($id) ? (int)$id : null; |
|
810
|
|
|
|
|
811
|
|
|
foreach ( $form as $group_id => $group_fields) { |
|
|
|
|
|
|
812
|
|
|
if ( empty($options) || (!empty($options) && ($options['title']))) $output_form_fields .= '<h2>'.__( $group_fields['name'], 'wpshop' ).'</h2>'; |
|
813
|
|
|
$end_line_indicator = 0; $fields_limit_per_line = -1; |
|
814
|
|
|
foreach ( $group_fields['content'] as $key => $field) { |
|
815
|
|
|
$attribute_def = wpshop_attributes::getElement( $field['name'], $element_status = "'valid'", $field_to_search = 'code' ); |
|
816
|
|
|
/** Grid opening **/ |
|
817
|
|
|
if ( !empty($form_model) && !empty($form_model[$group_id]) && in_array('wps-attribute-end-line-'.$end_line_indicator, $form_model[$group_id]) && $fields_limit_per_line == -1 ) { |
|
818
|
|
|
$current_key = array_search( 'wps-attribute-end-line-'.$end_line_indicator, $form_model[$group_id] ); |
|
819
|
|
|
$current_attribute_key = array_search( 'attribute_'.$attribute_def->id, $form_model[$group_id] ); |
|
820
|
|
|
|
|
821
|
|
|
if( $current_attribute_key > $current_key ) { |
|
822
|
|
|
// /** Define limit **/ |
|
|
|
|
|
|
823
|
|
|
// if( in_array('wps-attribute-end-line-' . ($end_line_indicator + 1 ) , $form_model[$group_id]) ) { |
|
824
|
|
|
// $next_key = array_search( 'wps-attribute-end-line-'.( $end_line_indicator + 1 ), $form_model[$group_id] ); |
|
825
|
|
|
// $fields_limit_per_line = $next_key - $current_key - 1; |
|
826
|
|
|
// $fields_limit_per_line = ( $fields_limit_per_line > 6 ) ? 6 : $fields_limit_per_line; |
|
827
|
|
|
// } |
|
828
|
|
|
// else { |
|
829
|
|
|
// $current_key = array_search( 'wps-attribute-end-line-'.$end_line_indicator, $form_model[$group_id] ); |
|
830
|
|
|
// $end_tab = count($form_model[$group_id]) - 1; |
|
831
|
|
|
// $fields_limit_per_line = $end_tab - $current_key - 1; |
|
832
|
|
|
// $fields_limit_per_line = ( $fields_limit_per_line > 6 ) ? 6 : $fields_limit_per_line; |
|
833
|
|
|
// } |
|
834
|
|
|
if ( !empty($fields_limit_per_line) && $fields_limit_per_line != -1 ) { |
|
835
|
|
|
if ( $fields_limit_per_line == 1 ) { |
|
836
|
|
|
$output_form_fields .= '<div class="wps-row">'; |
|
837
|
|
|
} |
|
838
|
|
|
else { |
|
839
|
|
|
$output_form_fields .= '<div class="wps-row wps-gridwrapper' .$fields_limit_per_line. '-padded">'; |
|
840
|
|
|
} |
|
841
|
|
|
} |
|
842
|
|
|
} |
|
843
|
|
|
} |
|
844
|
|
|
|
|
845
|
|
|
if ( empty($options['field_to_hide']) || !is_array($options['field_to_hide']) || !in_array( $key, $options['field_to_hide'] ) ) { |
|
846
|
|
|
$attributeInputDomain = 'attribute[' . $type . '][' . $field['data_type'] . ']'; |
|
847
|
|
|
// Test if there is POST var or if user have already fill his address infos and fill the fields with these infos |
|
848
|
|
|
$referer = !empty($_POST['referer']) ? sanitize_text_field( $_POST['referer'] ) : ''; |
|
849
|
|
|
if( !empty($referer) ) { |
|
850
|
|
|
$value = !empty( $_POST[$form['id']."_".$field['name']] ) ? sanitize_text_field( $_POST[$form['id']."_".$field['name']] ) : ''; |
|
|
|
|
|
|
851
|
|
|
} |
|
852
|
|
|
|
|
853
|
|
|
|
|
854
|
|
|
|
|
855
|
|
|
// Fill Automaticly some fields when it's an address creation |
|
856
|
|
|
switch ( $field['name']) { |
|
857
|
|
|
case 'address_title' : |
|
858
|
|
|
if( empty($field['value']) ) { |
|
859
|
|
|
/** Count Billing and shipping address **/ |
|
860
|
|
|
$billing_address_count = $shipping_address_count = 1; |
|
861
|
|
|
if ( get_current_user_id() != 0 ) { |
|
862
|
|
|
$addresses = get_posts( array('posts_per_page' => -1, 'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS, 'post_parent' => get_current_user_id(), 'post_status' => 'draft') ); |
|
|
|
|
|
|
863
|
|
|
if ( !empty($addresses) ) { |
|
864
|
|
|
foreach( $addresses as $address ) { |
|
865
|
|
|
$address_type = get_post_meta( $address->ID, '_wpshop_address_attribute_set_id', true); |
|
866
|
|
|
if ( !empty($address_type) ){ |
|
867
|
|
|
if ( !empty( $shipping_address_choice['choice'] ) && $address_type == $shipping_address_choice['choice'] ) { |
|
868
|
|
|
$shipping_address_count++; |
|
869
|
|
|
} |
|
870
|
|
|
else{ |
|
871
|
|
|
$billing_address_count++; |
|
872
|
|
|
} |
|
873
|
|
|
} |
|
874
|
|
|
} |
|
875
|
|
|
} |
|
876
|
|
|
} |
|
877
|
|
|
$field['value'] = ( $type == $choosen_address['choice'] ) ? __('Billing address', 'wpshop').( ($billing_address_count > 1) ? ' '.$billing_address_count : '' ) : __('Shipping address', 'wpshop').( ($shipping_address_count > 1) ? ' '.$shipping_address_count : ''); |
|
878
|
|
|
|
|
879
|
|
|
} |
|
880
|
|
|
break; |
|
881
|
|
View Code Duplication |
case 'address_last_name' : |
|
|
|
|
|
|
882
|
|
|
if( empty($field['value']) ) { |
|
883
|
|
|
$usermeta_last_name = get_user_meta( $user_id, 'last_name', true); |
|
884
|
|
|
$field['value'] = ( !empty($usermeta_last_name) ) ? $usermeta_last_name : ''; |
|
885
|
|
|
} |
|
886
|
|
|
break; |
|
887
|
|
View Code Duplication |
case 'address_first_name' : |
|
|
|
|
|
|
888
|
|
|
if( empty($field['value']) ) { |
|
889
|
|
|
$usermeta_first_name = get_user_meta( $user_id, 'first_name', true); |
|
890
|
|
|
$field['value'] = ( !empty($usermeta_first_name) ) ? $usermeta_first_name : ''; |
|
891
|
|
|
} |
|
892
|
|
|
break; |
|
893
|
|
|
case 'address_user_email' : |
|
894
|
|
|
if( empty($field['value']) ) { |
|
895
|
|
|
$user_infos = get_userdata( $user_id ); |
|
896
|
|
|
$field['value'] = ( !empty($user_infos) && !empty($user_infos->user_email) ) ? $user_infos->user_email : ''; |
|
897
|
|
|
} |
|
898
|
|
|
break; |
|
899
|
|
|
default : |
|
900
|
|
|
$field['value'] = ( !empty($field['value']) ) ? $field['value'] : ''; |
|
901
|
|
|
break; |
|
902
|
|
|
} |
|
903
|
|
|
|
|
904
|
|
|
|
|
905
|
|
|
|
|
906
|
|
|
/** Fill fields if $_POST exist **/ |
|
907
|
|
|
if ( !empty( $attribute[$type][$field['data_type']][$field['name']] ) ) { |
|
908
|
|
|
$field['value'] = sanitize_text_field( $_POST['attribute'][$type][$field['data_type']][$field['name']] ); |
|
|
|
|
|
|
909
|
|
|
} |
|
910
|
|
|
|
|
911
|
|
|
|
|
912
|
|
|
if( $field['name'] == 'address_title' && !empty($first) && $type == __('Billing address', 'wpshop') ) { |
|
913
|
|
|
$value = __('Billing address', 'wpshop'); |
|
|
|
|
|
|
914
|
|
|
} |
|
915
|
|
|
elseif( $field['name'] == 'address_title' && !empty($first) && $type == __('Shipping address', 'wpshop') ) { |
|
916
|
|
|
$value = __('Shipping address', 'wpshop'); |
|
|
|
|
|
|
917
|
|
|
} |
|
918
|
|
|
|
|
919
|
|
|
if ( !empty($special_values[$field['name']]) ) { |
|
920
|
|
|
$field['value'] = $special_values[$field['name']]; |
|
921
|
|
|
} |
|
922
|
|
|
|
|
923
|
|
|
$template = 'wpshop_account_form_input'; |
|
|
|
|
|
|
924
|
|
|
if ( $field['type'] == 'hidden' ) { |
|
925
|
|
|
$template = 'wpshop_account_form_hidden_input'; |
|
|
|
|
|
|
926
|
|
|
} |
|
927
|
|
|
|
|
928
|
|
|
if ( $field['frontend_verification'] == 'country' ) { |
|
929
|
|
|
$field['type'] = 'select'; |
|
930
|
|
|
/** display a country list **/ |
|
931
|
|
|
$countries_list = unserialize(WPSHOP_COUNTRY_LIST); |
|
932
|
|
|
$possible_values = array_merge(array('' => __('Choose a country')), $countries_list); |
|
933
|
|
|
|
|
934
|
|
|
$limit_countries_list = get_option( 'wpshop_limit_country_list' ); |
|
935
|
|
|
$default_country_choice = get_option( 'wpshop_country_default_choice' ); |
|
936
|
|
|
if ( !empty($limit_countries_list) ) { |
|
937
|
|
|
$possible_values = array(); |
|
938
|
|
|
if ( count($limit_countries_list) > 1 ) { |
|
939
|
|
|
$possible_values[''] = __('Choose a country'); |
|
940
|
|
|
} |
|
941
|
|
|
foreach( $limit_countries_list as $country_code) { |
|
|
|
|
|
|
942
|
|
|
if ( !empty($countries_list) && !empty($countries_list[$country_code]) ) { |
|
943
|
|
|
$possible_values[$country_code] = $countries_list[$country_code]; |
|
944
|
|
|
} |
|
945
|
|
|
} |
|
946
|
|
|
} |
|
947
|
|
|
|
|
948
|
|
|
$field['value'] = ( !empty($default_country_choice) && array_key_exists($default_country_choice, $possible_values ) ) ? $default_country_choice : ''; |
|
949
|
|
|
$field['possible_value'] = $possible_values; |
|
950
|
|
|
$field['valueToPut'] = 'index'; |
|
951
|
|
|
|
|
952
|
|
|
} |
|
953
|
|
|
|
|
954
|
|
|
|
|
955
|
|
|
|
|
956
|
|
|
$element_simple_class = str_replace('"', '', str_replace('class="', '', str_replace('wpshop_input_datetime', '', $field['option']))); |
|
|
|
|
|
|
957
|
|
|
$input_tpl_component = array(); |
|
958
|
|
|
|
|
959
|
|
|
//$input_tpl_component['CUSTOMER_FORM_INPUT_MAIN_CONTAINER_CLASS'] = ' wsphop_customer_account_form_container wsphop_customer_account_form_container_' . $field['name'] . $element_simple_class; |
|
|
|
|
|
|
960
|
|
|
$input_tpl_component['CUSTOMER_FORM_INPUT_NAME'] = ( !empty($field['name']) ) ? $field['name'] : ''; |
|
961
|
|
|
$input_tpl_component['CUSTOMER_FORM_INPUT_LABEL'] = ( $field['type'] != 'hidden' ) ? stripslashes( __( $field['label'], 'wpshop' ) ) . ( ( $field['required'] == 'yes' ) ? ' <em>*</em>' : '') : ''; |
|
962
|
|
|
$input_tpl_component['CUSTOMER_FORM_INPUT_LABEL_OPTIONS'] = ' for="' . $field['id'] . '"'; |
|
963
|
|
|
$input_tpl_component['CUSTOMER_FORM_INPUT_FIELD'] = wpshop_form::check_input_type($field, $attributeInputDomain); |
|
964
|
|
|
//$output_form_fields .= wpshop_display::display_template_element($template, $input_tpl_component); |
|
|
|
|
|
|
965
|
|
|
|
|
966
|
|
|
|
|
967
|
|
|
$output_form_fields .= wpshop_display::display_template_element('wps_address_field', $input_tpl_component, array(), 'wpshop'); |
|
968
|
|
|
|
|
969
|
|
|
|
|
970
|
|
|
unset($input_tpl_component); |
|
971
|
|
|
|
|
972
|
|
|
if ( $field['_need_verification'] == 'yes' ) { |
|
973
|
|
|
$field['name'] = $field['name'] . '2'; |
|
974
|
|
|
$field['id'] = $field['id'] . '2'; |
|
975
|
|
|
$element_simple_class = str_replace('"', '', str_replace('class="', '', str_replace('wpshop_input_datetime', '', $field['option']))); |
|
976
|
|
|
$input_tpl_component = array(); |
|
977
|
|
|
$input_tpl_component['CUSTOMER_FORM_INPUT_NAME'] = $field['name']; |
|
978
|
|
|
$input_tpl_component['CUSTOMER_FORM_INPUT_LABEL'] = __( $field['label'], 'wpshop' ) . ( ( ($field['required'] == 'yes' && !is_admin()) || ($field['name'] == 'address_user_email' && is_admin()) ) ? ' <span class="required">*</span>' : ''); |
|
979
|
|
|
$input_tpl_component['CUSTOMER_FORM_INPUT_LABEL_OPTIONS'] = ' for="' . $field['id'] . '"'; |
|
980
|
|
|
$input_tpl_component['CUSTOMER_FORM_INPUT_MAIN_CONTAINER_CLASS'] = ' wsphop_customer_account_form_container wsphop_customer_account_form_container_' . $field['name'] . $element_simple_class; |
|
981
|
|
|
$input_tpl_component['CUSTOMER_FORM_INPUT_LABEL'] = sprintf( __('Confirm %s', 'wpshop'), strtolower( __( $field['label'], 'wpshop' ) ) ). ( ($field['required'] == 'yes') && !is_admin() ? ' <span class="required">*</span>' : ''); |
|
982
|
|
|
$input_tpl_component['CUSTOMER_FORM_INPUT_FIELD'] = wpshop_form::check_input_type($field, $attributeInputDomain) . $field['options']; |
|
983
|
|
|
//$output_form_fields .= wpshop_display::display_template_element($template, $input_tpl_component); |
|
|
|
|
|
|
984
|
|
|
$output_form_fields .= wpshop_display::display_template_element('wps_address_field', $input_tpl_component, array(), 'wpshop'); |
|
985
|
|
|
unset($input_tpl_component); |
|
986
|
|
|
} |
|
987
|
|
|
} |
|
988
|
|
|
|
|
989
|
|
|
/** Grid closing **/ |
|
990
|
|
|
if( $fields_limit_per_line != -1 && !empty($fields_limit_per_line) ) { |
|
991
|
|
|
$fields_limit_per_line--; |
|
992
|
|
|
if( $fields_limit_per_line == 0 ) { |
|
993
|
|
|
$output_form_fields .= '</div>'; |
|
994
|
|
|
$fields_limit_per_line = -1; |
|
995
|
|
|
$end_line_indicator++; |
|
996
|
|
|
} |
|
997
|
|
|
} |
|
998
|
|
|
} |
|
999
|
|
|
} |
|
1000
|
|
|
|
|
1001
|
|
|
if ( $type == $choosen_address['choice'] ) { |
|
1002
|
|
|
$output_form_fields .= '<input type="hidden" name="billing_address" value="'.$choosen_address['choice'].'" />'; |
|
1003
|
|
|
} |
|
1004
|
|
|
$shipping_address_options = get_option('wpshop_shipping_address_choice'); |
|
1005
|
|
|
if ( $type == $shipping_address_options['choice'] ) { |
|
1006
|
|
|
$output_form_fields .= '<input type="hidden" name="shipping_address" value="' .$shipping_address_options['choice']. '" />'; |
|
1007
|
|
|
} |
|
1008
|
|
|
$output_form_fields .= '<input type="hidden" name="type_of_form" value="' .$type. '" /><input type="hidden" name="attribute[' .$type. '][item_id]" value="' .$current_item_edited. '" />'; |
|
1009
|
|
|
|
|
1010
|
|
|
$output_form_fields .= ( $user_id != get_current_user_id() ) ? '<input type="hidden" name="user[customer_id]" value="' .$user_id. '" />' : ''; |
|
1011
|
|
|
|
|
1012
|
|
|
if ( empty($first) ) $output_form_fields = wpshop_display::display_template_element('wpshop_customer_addresses_form', array('CUSTOMER_ADDRESSES_FORM_CONTENT' => $output_form_fields, 'CUSTOMER_ADDRESSES_FORM_BUTTONS' => '')); |
|
1013
|
|
|
|
|
1014
|
|
|
return $output_form_fields; |
|
1015
|
|
|
} |
|
1016
|
|
|
|
|
1017
|
|
|
// /** |
|
1018
|
|
|
// * Action when Shipping and billing are same for customer |
|
1019
|
|
|
// * @param integer $billing_address_id |
|
1020
|
|
|
// * @param integer $shipping_address_id |
|
1021
|
|
|
// */ |
|
1022
|
|
|
// function same_shipping_as_billing($billing_address_id, $shipping_address_id) { |
|
|
|
|
|
|
1023
|
|
|
// $tableauGeneral = $_REQUEST; |
|
|
|
|
|
|
1024
|
|
|
// |
|
1025
|
|
|
// // Create an array with the shipping address fields definition |
|
1026
|
|
|
// $shipping_fields = array(); |
|
|
|
|
|
|
1027
|
|
|
// foreach ($tableauGeneral['attribute'][$billing_address_id] as $key=>$attribute_group ) { |
|
|
|
|
|
|
1028
|
|
|
// if ( is_array($attribute_group) ) { |
|
|
|
|
|
|
1029
|
|
|
// foreach( $attribute_group as $field_name=>$value ) { |
|
|
|
|
|
|
1030
|
|
|
// $shipping_fields[] = sanitize_text_field( $field_name ); |
|
|
|
|
|
|
1031
|
|
|
// } |
|
1032
|
|
|
// } |
|
1033
|
|
|
// } |
|
1034
|
|
|
// // Test if the billing address field exist in shipping form |
|
1035
|
|
|
// foreach ($tableauGeneral['attribute'][$shipping_address_id] as $key=>$attribute_group ) { |
|
|
|
|
|
|
1036
|
|
|
// if (is_array($attribute_group) ) { |
|
|
|
|
|
|
1037
|
|
|
// foreach( $attribute_group as $field_name=>$value ) { |
|
|
|
|
|
|
1038
|
|
|
// if ( in_array($field_name, $shipping_fields) ) { |
|
|
|
|
|
|
1039
|
|
|
// if ($field_name == 'address_title') { |
|
|
|
|
|
|
1040
|
|
|
// $tableauGeneral['attribute'][$billing_address_id][$key][$field_name] = __('Billing address', 'wpshop'); |
|
|
|
|
|
|
1041
|
|
|
// } |
|
1042
|
|
|
// else { |
|
1043
|
|
|
// $tableauGeneral['attribute'][$billing_address_id][$key][$field_name] = sanitize_text_field( $tableauGeneral['attribute'][$shipping_address_id][$key][$field_name] ); |
|
|
|
|
|
|
1044
|
|
|
// } |
|
1045
|
|
|
// } |
|
1046
|
|
|
// } |
|
1047
|
|
|
// } |
|
1048
|
|
|
// } |
|
1049
|
|
|
// |
|
1050
|
|
|
// foreach ( $tableauGeneral as $key=>$value ) { |
|
|
|
|
|
|
1051
|
|
|
// if ( !empty($_POST) ) { |
|
|
|
|
|
|
1052
|
|
|
// $_POST[$key] = $value; |
|
|
|
|
|
|
1053
|
|
|
// } |
|
1054
|
|
|
// else { |
|
1055
|
|
|
// $_REQUEST[$key] = $value; |
|
|
|
|
|
|
1056
|
|
|
// } |
|
1057
|
|
|
// } |
|
1058
|
|
|
// |
|
1059
|
|
|
// } |
|
1060
|
|
|
|
|
1061
|
|
|
/** |
|
1062
|
|
|
* Copy Shipping address datas in billing corresponding datas |
|
1063
|
|
|
* @param integer $shipping_address_type_id |
|
1064
|
|
|
* @param integer $billing_address_type_id |
|
1065
|
|
|
*/ |
|
1066
|
|
|
function shipping_to_billing( $shipping_address_type_id, $billing_address_type_id ) { |
|
|
|
|
|
|
1067
|
|
|
global $wpdb; |
|
1068
|
|
|
$tmp_array = array(); |
|
|
|
|
|
|
1069
|
|
|
$tmp_array = !empty( $_REQUEST ) ? (array)$_REQUEST : array(); |
|
1070
|
|
|
|
|
1071
|
|
|
$billing_fields = array(); |
|
|
|
|
|
|
1072
|
|
|
if( !empty($tmp_array) && !empty($tmp_array['attribute']) && !empty($tmp_array['attribute'][$shipping_address_type_id]) ) { |
|
1073
|
|
|
foreach ($tmp_array['attribute'][$shipping_address_type_id] as $key => $attribute_group ) { |
|
1074
|
|
|
if ( is_array($attribute_group) ) { |
|
1075
|
|
|
foreach( $attribute_group as $field_name => $value ) { |
|
1076
|
|
|
$attribute_def = wpshop_attributes::getElement( sanitize_text_field( $field_name ), "'valid'", 'code' ); |
|
1077
|
|
|
if( !empty($attribute_def) ) { |
|
1078
|
|
|
$query = $wpdb->prepare( 'SELECT * FROM '. WPSHOP_DBT_ATTRIBUTE_DETAILS. ' WHERE status = %s AND attribute_id = %d AND attribute_set_id = %s', 'valid', (int)$attribute_def->id, sanitize_text_field( $billing_address_type_id ) ); |
|
1079
|
|
|
$attribute_exist = $wpdb->get_var( $query ); |
|
1080
|
|
|
if ( !empty($attribute_exist) ) { |
|
1081
|
|
|
$tmp_array['attribute'][$billing_address_type_id][$attribute_def->data_type][$field_name] = sanitize_text_field( $value ); |
|
|
|
|
|
|
1082
|
|
|
} |
|
1083
|
|
|
} |
|
1084
|
|
|
} |
|
1085
|
|
|
} |
|
1086
|
|
|
} |
|
1087
|
|
|
} |
|
1088
|
|
|
|
|
1089
|
|
|
return $tmp_array; |
|
1090
|
|
|
} |
|
1091
|
|
|
|
|
1092
|
|
|
|
|
1093
|
|
|
|
|
1094
|
|
|
/** |
|
1095
|
|
|
* Display Address Inteface |
|
1096
|
|
|
* @param integer $customer_id |
|
1097
|
|
|
* @param boolean $admin_display |
|
1098
|
|
|
* @param integer $order_id |
|
1099
|
|
|
* @return string |
|
1100
|
|
|
*/ |
|
1101
|
|
|
function display_addresses_interface( $customer_id = '', $admin_display = false, $order_id = '' ) { |
|
|
|
|
|
|
1102
|
|
|
$output = $extra_class = $billing_address_display = $shipping_address_display = $first_address_checking = ''; |
|
|
|
|
|
|
1103
|
|
|
$is_from_admin = ( !empty($customer_id) ) ? true : false; |
|
1104
|
|
|
$user_id = ( !empty($customer_id) ) ? $customer_id : get_current_user_id(); |
|
1105
|
|
|
|
|
1106
|
|
|
if ( $user_id != 0 ) { |
|
1107
|
|
|
$shipping_option = get_option( 'wpshop_shipping_address_choice' ); |
|
1108
|
|
|
$billing_option = get_option( 'wpshop_billing_address' ); |
|
1109
|
|
|
|
|
1110
|
|
|
/** Shipping address **/ |
|
1111
|
|
|
// Check if is only downloadable else display address |
|
1112
|
|
|
$cart_is_downloadable = false; |
|
1113
|
|
View Code Duplication |
if ( !empty( $_SESSION['cart'] ) && !empty( $_SESSION['cart']['order_items'] ) ) { |
|
|
|
|
|
|
1114
|
|
|
foreach( $_SESSION['cart']['order_items'] as $c ) { |
|
1115
|
|
|
$product = wpshop_products::get_product_data( $c['item_id'] ); |
|
1116
|
|
|
/** Check if it's a variation and check the parent product **/ |
|
1117
|
|
|
if ( get_post_type( $c['item_id'] ) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION ) { |
|
1118
|
|
|
$parent_def = wpshop_products::get_parent_variation( $c['item_id'] ); |
|
1119
|
|
|
if ( !empty($parent_def) && !empty($parent_def['parent_post_meta']) && !empty($parent_def['parent_post_meta']['is_downloadable_']) ) { |
|
1120
|
|
|
$product['is_downloadable_'] = $parent_def['parent_post_meta']['is_downloadable_']; |
|
1121
|
|
|
} |
|
1122
|
|
|
} |
|
1123
|
|
|
if( !empty($product['is_downloadable_']) && ( __( $product['is_downloadable_'], 'wpshop' ) == __('Yes', 'wpshop') || __( $product['is_downloadable_'], 'wpshop') == __('yes', 'wpshop') ) ) { |
|
1124
|
|
|
$cart_is_downloadable = true; |
|
1125
|
|
|
} else { |
|
1126
|
|
|
$cart_is_downloadable = false; |
|
1127
|
|
|
break; |
|
1128
|
|
|
} |
|
1129
|
|
|
} |
|
1130
|
|
|
} |
|
1131
|
|
|
|
|
1132
|
|
|
$checkout_address_type = array( |
|
1133
|
|
|
'shipping' => __( 'Shipping address', 'wpshop' ), |
|
1134
|
|
|
'billing' => __( 'Billing address', 'wpshop' ), |
|
1135
|
|
|
); |
|
1136
|
|
|
$shipping_is_avalaible = true; |
|
1137
|
|
|
foreach ( $checkout_address_type as $address_type => $address_title ) { |
|
1138
|
|
|
$display_address_of_type = true; |
|
1139
|
|
|
switch ( $address_type ) { |
|
1140
|
|
|
case 'shipping': |
|
1141
|
|
|
if ( empty( $shipping_option ) || empty( $shipping_option[ 'activate' ] ) || $cart_is_downloadable ) { |
|
1142
|
|
|
$display_address_of_type = false; |
|
1143
|
|
|
$shipping_is_avalaible = false; |
|
1144
|
|
|
} |
|
1145
|
|
|
else { |
|
1146
|
|
|
$address_type_id = $shipping_option[ 'choice' ]; |
|
1147
|
|
|
} |
|
1148
|
|
|
break; |
|
1149
|
|
|
case 'billing': |
|
1150
|
|
|
$address_type_id = $billing_option[ 'choice' ]; |
|
1151
|
|
|
break; |
|
1152
|
|
|
} |
|
1153
|
|
|
|
|
1154
|
|
|
if ( $display_address_of_type ) { |
|
1155
|
|
|
$box_content = !$admin_display ? self::display_address_interface_content( $address_type_id, $address_title, '', $address_type, $user_id ) : ''; |
|
|
|
|
|
|
1156
|
|
|
$first_address_checking = ( empty( $box_content ) && ( 'shipping' == $address_type ) ) ? true : false; |
|
1157
|
|
|
|
|
1158
|
|
|
ob_start(); |
|
1159
|
|
|
require( wpshop_tools::get_template_part( WPS_ADDRESS_DIR, WPS_LOCALISATION_TEMPLATES_MAIN_DIR, ( $admin_display ? 'backend' : 'frontend' ), 'address', 'container' ) ); |
|
1160
|
|
|
$output .= ob_get_contents(); |
|
1161
|
|
|
ob_end_clean(); |
|
1162
|
|
|
} |
|
1163
|
|
|
} |
|
1164
|
|
|
} |
|
1165
|
|
|
|
|
1166
|
|
|
return $output; |
|
1167
|
|
|
} |
|
1168
|
|
|
|
|
1169
|
|
|
/** Display address interface content **/ |
|
1170
|
|
|
public static function display_address_interface_content( $address_type_id, $address_title, $selected_address = null, $type, $customer_id = '', $admin_display = false, $order_id = '' ) { |
|
1171
|
|
|
$user_id = ( !empty($customer_id) ) ? $customer_id : get_current_user_id(); |
|
1172
|
|
|
$is_from_admin = ( !empty($customer_id) ) ? true : false; |
|
1173
|
|
|
$select_id = ( !empty($type) && $type == 'shipping') ? 'shipping_address_address_list' : 'billing_address_address_list'; |
|
1174
|
|
|
$output = ''; |
|
1175
|
|
|
|
|
1176
|
|
|
if( !empty($address_type_id) ) { |
|
1177
|
|
|
$addresses = self::get_addresses_list( $user_id ); |
|
1178
|
|
|
$list_addresses = ( !empty($addresses[ $address_type_id ]) ) ? $addresses[ $address_type_id ] : array(); |
|
1179
|
|
|
if ( empty($list_addresses) ) { |
|
1180
|
|
|
$form = self::display_form_fields($address_type_id); |
|
1181
|
|
|
} |
|
1182
|
|
|
|
|
1183
|
|
|
ob_start(); |
|
1184
|
|
|
require( wpshop_tools::get_template_part( WPS_ADDRESS_DIR, WPS_LOCALISATION_TEMPLATES_MAIN_DIR, ( $admin_display ? 'backend' : 'frontend' ), "address", "content" ) ); |
|
1185
|
|
|
$output .= ob_get_contents(); |
|
1186
|
|
|
ob_end_clean(); |
|
1187
|
|
|
} |
|
1188
|
|
|
|
|
1189
|
|
|
return $output; |
|
1190
|
|
|
} |
|
1191
|
|
|
|
|
1192
|
|
|
|
|
1193
|
|
|
/** |
|
1194
|
|
|
* AJAX - Display addresses list for a given element |
|
1195
|
|
|
* |
|
1196
|
|
|
* @since 1.0 - WPShop 1.3.7.0 |
|
1197
|
|
|
*/ |
|
1198
|
|
|
function display_addresses_list() { |
|
|
|
|
|
|
1199
|
|
|
$_wpnonce = ( !empty( $_REQUEST['_wpnonce'] ) ) ? sanitize_text_field( $_REQUEST['_wpnonce'] ) : ''; |
|
1200
|
|
|
|
|
1201
|
|
|
if ( !wp_verify_nonce( $_wpnonce, 'display_addresses_list' ) ) |
|
1202
|
|
|
die(); |
|
1203
|
|
|
|
|
1204
|
|
|
$post_id = (int) $_POST['post_id']; |
|
1205
|
|
|
$addresses = $this->get_addresses_list( $post_id ); |
|
|
|
|
|
|
1206
|
|
|
require_once( wpshop_tools::get_template_part( WPS_ADDRESS_DIR, WPS_LOCALISATION_TEMPLATES_MAIN_DIR, "backend", "addresses" ) ); |
|
1207
|
|
|
die(); |
|
1208
|
|
|
} |
|
1209
|
|
|
|
|
1210
|
|
|
/** |
|
1211
|
|
|
* AJAX - Display selected address for a given element |
|
1212
|
|
|
* |
|
1213
|
|
|
* @since 1.0 - WPShop 1.3.7.0 |
|
1214
|
|
|
*/ |
|
1215
|
|
|
function display_address() { |
|
|
|
|
|
|
1216
|
|
|
$_wpnonce = ( !empty( $_REQUEST['_wpnonce'] ) ) ? sanitize_text_field( $_REQUEST['_wpnonce'] ) : ''; |
|
1217
|
|
|
|
|
1218
|
|
|
if ( !wp_verify_nonce( $_wpnonce, 'wps_address_display_an_address' ) ) |
|
1219
|
|
|
die(); |
|
1220
|
|
|
|
|
1221
|
|
|
$adress_id = (int) $_POST['address_id']; |
|
1222
|
|
|
$address_post_meta = get_post_meta( $adress_id, '_wpshop_address_metadata', true); |
|
1223
|
|
|
$address_type_post_meta = get_post_meta( $adress_id, '_wpshop_address_attribute_set_id', true); |
|
1224
|
|
|
if( !empty($address_post_meta) && !empty($address_type_post_meta) ) { |
|
1225
|
|
|
$addresses_list[$address_type_post_meta][ $adress_id ] = $address_post_meta; |
|
|
|
|
|
|
1226
|
|
|
} |
|
1227
|
|
|
$address_open = true; |
|
1228
|
|
|
foreach ( $addresses_list as $address_type => $addresses_list_by_type ) : |
|
|
|
|
|
|
1229
|
|
|
foreach ( $addresses_list_by_type as $address_id => $address ) : |
|
1230
|
|
|
require( wpshop_tools::get_template_part( WPS_ADDRESS_DIR, WPS_LOCALISATION_TEMPLATES_MAIN_DIR, "backend", "address" ) ); |
|
1231
|
|
|
endforeach; |
|
1232
|
|
|
endforeach; |
|
1233
|
|
|
|
|
1234
|
|
|
die(); |
|
1235
|
|
|
} |
|
1236
|
|
|
|
|
1237
|
|
|
/** |
|
1238
|
|
|
* AJAX - Display address form or a given element. CHeck if there are many types of addresses in order to choose address type dropdown or directly address form |
|
1239
|
|
|
* |
|
1240
|
|
|
* @since 1.0 - WPShop 1.3.7.0 |
|
1241
|
|
|
*/ |
|
1242
|
|
|
function display_address_adding_form() { |
|
|
|
|
|
|
1243
|
|
|
global $wpdb; |
|
1244
|
|
|
|
|
1245
|
|
|
$_wpnonce = ( !empty( $_REQUEST['_wpnonce'] ) ) ? sanitize_text_field( $_REQUEST['_wpnonce'] ) : ''; |
|
1246
|
|
|
|
|
1247
|
|
|
if ( !wp_verify_nonce( $_wpnonce, 'display_address_adding_form' ) ) |
|
1248
|
|
|
die(); |
|
1249
|
|
|
|
|
1250
|
|
|
$address_id = 0; |
|
|
|
|
|
|
1251
|
|
|
$post_ID = (int)$_POST[ 'post_id' ]; |
|
1252
|
|
|
$element = get_post( $post_ID ); |
|
1253
|
|
|
$query = $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s ", $element->post_type); |
|
1254
|
|
|
$element_cpt = $wpdb->get_var( $query ); |
|
1255
|
|
|
$attached_addresses = get_post_meta( $element_cpt, '_wpshop_entity_attached_address', true ); |
|
1256
|
|
|
|
|
1257
|
|
|
if ( !empty( $attached_addresses ) ) { |
|
1258
|
|
|
if ( count( $attached_addresses ) == 1 ) { |
|
1259
|
|
|
$address_type_id = $attached_addresses[ 0 ]; |
|
|
|
|
|
|
1260
|
|
|
require_once( wpshop_tools::get_template_part( WPS_ADDRESS_DIR, WPS_LOCALISATION_TEMPLATES_MAIN_DIR, "backend", "address", "form" ) ); |
|
1261
|
|
|
} |
|
1262
|
|
|
else { |
|
1263
|
|
|
require_once( wpshop_tools::get_template_part( WPS_ADDRESS_DIR, WPS_LOCALISATION_TEMPLATES_MAIN_DIR, "backend", "addresses", "types" ) ); |
|
1264
|
|
|
} |
|
1265
|
|
|
} |
|
1266
|
|
|
else { |
|
1267
|
|
|
printf( __( 'No addresses are attached to this element type %s', 'wpeo_geoloc' ), $element->post_type); |
|
1268
|
|
|
} |
|
1269
|
|
|
|
|
1270
|
|
|
die(); |
|
1271
|
|
|
} |
|
1272
|
|
|
|
|
1273
|
|
|
/** |
|
1274
|
|
|
* AJAX - Display address form for address edition |
|
1275
|
|
|
* |
|
1276
|
|
|
* @since 1.0 - WPShop 1.3.7.0 |
|
1277
|
|
|
*/ |
|
1278
|
|
|
function load_address_edition_form() { |
|
|
|
|
|
|
1279
|
|
|
$_wpnonce = ( !empty( $_REQUEST['_wpnonce'] ) ) ? sanitize_text_field( $_REQUEST['_wpnonce'] ) : ''; |
|
1280
|
|
|
|
|
1281
|
|
|
if ( !wp_verify_nonce( $_wpnonce, 'wps_address_edition_form_load' ) ) |
|
1282
|
|
|
die(); |
|
1283
|
|
|
|
|
1284
|
|
|
$address_id = (int)$_POST[ 'element_id' ]; |
|
1285
|
|
|
$post_ID = (int)$_POST[ 'post_id' ]; |
|
1286
|
|
|
$address_type_id = get_post_meta( $address_id, '_wpshop_address_attribute_set_id', true); |
|
1287
|
|
|
$wpeogeo_adress = !empty( $_POST[ 'wpeogeo-address-type-chosen-for-creation' ] ) ? (int) $_POST[ 'wpeogeo-address-type-chosen-for-creation' ] : 0; |
|
1288
|
|
|
if ( empty( $address_id ) && empty( $address_type_id ) && !empty( $wpeogeo_adress ) ) { |
|
1289
|
|
|
$address_type_id = $wpeogeo_adress; |
|
1290
|
|
|
} |
|
1291
|
|
|
|
|
1292
|
|
|
require( wpshop_tools::get_template_part( WPS_ADDRESS_DIR, WPS_LOCALISATION_TEMPLATES_MAIN_DIR, "backend", "address", "form" ) ); |
|
1293
|
|
|
die(); |
|
1294
|
|
|
} |
|
1295
|
|
|
|
|
1296
|
|
|
/** |
|
1297
|
|
|
* AJAX - Delete an address |
|
1298
|
|
|
*/ |
|
1299
|
|
|
function wps_delete_an_address() { |
|
|
|
|
|
|
1300
|
|
|
$_wpnonce = ( !empty( $_REQUEST['_wpnonce'] ) ) ? sanitize_text_field( $_REQUEST['_wpnonce'] ) : ''; |
|
1301
|
|
|
|
|
1302
|
|
|
if ( !wp_verify_nonce( $_wpnonce, 'wps_delete_an_address' ) ) |
|
1303
|
|
|
die(); |
|
1304
|
|
|
|
|
1305
|
|
|
$status = false; $response = ''; |
|
1306
|
|
|
$address_id = ( !empty( $_POST['address_id']) ) ? (int) $_POST['address_id'] : null; |
|
1307
|
|
|
if( !empty($address_id) ) { |
|
1308
|
|
|
/** Check if user is author of address **/ |
|
1309
|
|
|
$address = get_post( $address_id ); |
|
1310
|
|
|
if( !empty($address) && !empty($address->post_type) && $address->post_type == WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS && !empty($address->post_author) && $address->post_author == get_current_user_id() ) { |
|
1311
|
|
|
wp_delete_post( $address_id, true ); |
|
1312
|
|
|
$status = true; |
|
1313
|
|
|
} |
|
1314
|
|
|
} |
|
1315
|
|
|
echo json_encode( array('status' => $status, 'response' => $response) ); |
|
1316
|
|
|
die(); |
|
1317
|
|
|
} |
|
1318
|
|
|
|
|
1319
|
|
|
/** |
|
1320
|
|
|
* AJAX - Relad Address Interface in new checkout tunnel |
|
1321
|
|
|
*/ |
|
1322
|
|
|
function wps_reload_address_interface() { |
|
|
|
|
|
|
1323
|
|
|
check_ajax_referer( 'wps_reload_address_interface' ); |
|
1324
|
|
|
|
|
1325
|
|
|
global $wpdb; |
|
1326
|
|
|
$status = false; $response = ''; |
|
1327
|
|
|
$address_type = !empty($_POST['address_type']) ? (int) $_POST['address_type'] : null; |
|
1328
|
|
|
$selected_address = !empty($_POST['address_id']) ? (int) $_POST['address_id'] : null; |
|
1329
|
|
|
|
|
1330
|
|
|
if ( !empty( $address_type ) ) { |
|
1331
|
|
|
$billing_option = get_option( 'wpshop_billing_address' ); |
|
1332
|
|
|
if ( !empty($billing_option) && !empty($billing_option['choice']) && $billing_option['choice'] == $address_type ) { |
|
1333
|
|
|
$type = 'billing'; |
|
1334
|
|
|
} |
|
1335
|
|
|
else { |
|
1336
|
|
|
$type = 'shipping'; |
|
1337
|
|
|
} |
|
1338
|
|
|
$query = $wpdb->prepare( 'SELECT name FROM '.WPSHOP_DBT_ATTRIBUTE_SET .' WHERE id = %d ', $address_type ); |
|
1339
|
|
|
$address_title = __( $wpdb->get_var( $query ), 'wpshop' ); |
|
1340
|
|
|
$response = self::display_address_interface_content( $address_type, $address_title, $selected_address, $type ); |
|
1341
|
|
|
$status = true; |
|
1342
|
|
|
} |
|
1343
|
|
|
echo json_encode( array( 'status' => $status, 'response' => $response ) ); |
|
1344
|
|
|
die(); |
|
1345
|
|
|
} |
|
1346
|
|
|
|
|
1347
|
|
|
/** |
|
1348
|
|
|
* AJAX - Load address form in Modal Box |
|
1349
|
|
|
*/ |
|
1350
|
|
|
function wps_load_address_form() { |
|
|
|
|
|
|
1351
|
|
|
$address_id = ( !empty( $_POST['address_id'] ) ) ? (int) $_POST['address_id'] : ''; |
|
1352
|
|
|
$address_type_id = ( !empty( $_POST['address_type_id']) ) ? sanitize_text_field( $_POST['address_type_id'] ) : ''; |
|
1353
|
|
|
|
|
1354
|
|
|
check_ajax_referer( 'wps_load_address_form_' . $address_type_id ); |
|
1355
|
|
|
|
|
1356
|
|
|
wp_die( json_encode( self::loading_address_form( $address_type_id, $address_id, get_current_user_id() ) ) ); |
|
1357
|
|
|
} |
|
1358
|
|
|
|
|
1359
|
|
|
/** |
|
1360
|
|
|
* AJAX - Function for save address |
|
1361
|
|
|
*/ |
|
1362
|
|
|
function wps_save_address() { |
|
|
|
|
|
|
1363
|
|
|
check_ajax_referer( 'wps_save_address' ); |
|
1364
|
|
|
global $wpshop, $wpdb; |
|
1365
|
|
|
|
|
1366
|
|
|
$status = false; |
|
1367
|
|
|
$result = $address_type = $same_address_type = ''; |
|
1368
|
|
|
|
|
1369
|
|
|
$adress_save_the_first = !empty( $_POST['wps-address-save-the-first'] ) ? sanitize_text_field( $_POST['wps-address-save-the-first'] ) : ''; |
|
1370
|
|
|
$attribute = !empty( $_POST['attribute'] ) ? (array)$_POST['attribute'] : ''; |
|
1371
|
|
|
|
|
1372
|
|
|
foreach ( $attribute as $id_group => $attribute_group ) { |
|
|
|
|
|
|
1373
|
|
|
$address_type = $id_group; |
|
1374
|
|
|
$group = wps_address::get_addresss_form_fields_by_type ($id_group); |
|
1375
|
|
|
foreach ( $group as $attribute_sets ) { |
|
|
|
|
|
|
1376
|
|
|
foreach ( $attribute_sets as $attribute_set_field ) { |
|
1377
|
|
|
$validate = $wpshop->validateForm($attribute_set_field['content'], $attribute[$id_group], 'address_edition'); |
|
1378
|
|
|
} |
|
1379
|
|
|
|
|
1380
|
|
|
if ( $validate ) { |
|
|
|
|
|
|
1381
|
|
|
$shipping_save = self::save_address_infos( $id_group ); |
|
1382
|
|
|
|
|
1383
|
|
|
$wps_shipping_to_billing = !empty( $_POST['wps-shipping-to-billing'] ) ? sanitize_text_field( $_POST['wps-shipping-to-billing'] ) : ''; |
|
1384
|
|
|
if( !empty( $wps_shipping_to_billing ) ) { |
|
1385
|
|
|
$billing_option = get_option( 'wpshop_billing_address' ); |
|
1386
|
|
|
$shipping_option = get_option( 'wpshop_shipping_address_choice' ); |
|
1387
|
|
|
|
|
1388
|
|
|
$address_info_to_copy = self::shipping_to_billing( $shipping_option['choice'], $billing_option['choice'] ); |
|
1389
|
|
|
self::save_address_infos( $billing_option['choice'], $shipping_save['current_id'], $address_info_to_copy ); |
|
1390
|
|
|
$same_address_type = $billing_option['choice']; |
|
1391
|
|
|
} |
|
1392
|
|
|
|
|
1393
|
|
|
$status = true; |
|
1394
|
|
View Code Duplication |
if ( !empty( $adress_save_the_first ) ) { |
|
|
|
|
|
|
1395
|
|
|
$query = $wpdb->prepare( 'SELECT name FROM ' . WPSHOP_DBT_ATTRIBUTE_SET . ' WHERE id = %d ', $address_type ); |
|
1396
|
|
|
$address_title = __( $wpdb->get_var( $query ), 'wpshop' ); |
|
1397
|
|
|
$result = self::display_address_interface_content( $address_type, $address_title, $shipping_save[ 'current_id' ], $adress_save_the_first ); |
|
1398
|
|
|
} |
|
1399
|
|
|
} |
|
1400
|
|
View Code Duplication |
else { |
|
|
|
|
|
|
1401
|
|
|
if ( !empty($wpshop->errors) ){ |
|
1402
|
|
|
$result = '<div class="wps-alert wps-alert-error">' .__('Some errors have been detected', 'wpshop') . ' : <ul>'; |
|
1403
|
|
|
foreach( $wpshop->errors as $error ){ |
|
1404
|
|
|
$result .= '<li>' .$error. '</li>'; |
|
1405
|
|
|
} |
|
1406
|
|
|
$result .= '</ul></div>'; |
|
1407
|
|
|
} |
|
1408
|
|
|
} |
|
1409
|
|
|
} |
|
1410
|
|
|
} |
|
1411
|
|
|
|
|
1412
|
|
|
wp_die( json_encode( array( $status, $result, $address_type, $same_address_type ) ) ); |
|
1413
|
|
|
} |
|
1414
|
|
|
|
|
1415
|
|
|
} |
|
1416
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.