Commit dbf7b2b1 by 杨子

增加小程序收获地址插件

parent 96edaca5
<?php
/**
* Plugin Name: WooCommerce 微信小程序高级收货地址
* Plugin URI: https://www.qwqoffice.com/shop.php?mod=product&id=7
* Description: 替换微信收货地址接口,与WooCommerce的国家保持一致,支持保存多个收货地址
* Version: 1.0
* Author: QwqOffice
* Author URI: https://www.qwqoffice.com/
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* W2W requires at least: 1.5
* WC requires at least: 3.0.0
* WC tested up to: 3.4.3
**/
if ( ! class_exists( 'W2W_Advanced_Address' ) ) {
define( 'WP_W2W_Advanced_Address_PATH', plugin_dir_path( __FILE__ ) );
define( 'WP_W2W_Advanced_Address_URL', plugin_dir_url( __FILE__ ) );
class W2W_Advanced_Address {
public $notices = array();
public function __construct() {
// 插件依赖
add_action( 'admin_init', array( $this, 'check_w2w' ) );
// 后台
if ( is_admin() ) {
// 后台通知
add_action( 'admin_notices', array( $this, 'admin_notices' ), 15 );
}
// 添加API - 国家、省份、保存及删除地址
add_action( 'rest_api_init', array( $this, 'add_address_api' ) );
}
// 插件依赖
public function check_w2w() {
if( ! class_exists( 'WooCommerce_To_WeChatApp' ) ) {
$this->add_admin_notice( 'no_w2w', 'error', 'WooCommerce 微信小程序高级地址要求激活 WooCommerce 微信小程序' );
deactivate_plugins( plugin_basename( __FILE__ ) );
if( isset( $_GET['activate'] ) ){
unset( $_GET['activate'] );
}
return;
}
if( class_exists( 'Puc_v4_Factory' ) ) {
Puc_v4_Factory::buildUpdateChecker(
'https://www.qwqoffice.com/wordpress-dev/plugins/w2w-advanced-address/info.json',
__FILE__,
'w2w-advanced-address'
);
}
W2W()->register_extension( 'w2w-advanced-address' );
}
// 添加通知
public function add_admin_notice( $slug, $class, $message ) {
$this->notices[ $slug ] = array(
'class' => $class,
'message' => $message
);
}
// 显示通知
public function admin_notices() {
foreach ( (array) $this->notices as $notice_key => $notice ) {
?>
<div class="<?php echo esc_attr( $notice['class'] ) ?>"><p><?php echo $notice['message'] ?></p></div>
<?php
}
}
// 添加API - 国家、省份、保存及删除地址
public function add_address_api() {
register_rest_route( 'w2w/v1', '/store/countries', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_countries' ),
'args' => array(),
)
) );
register_rest_route( 'w2w/v1', '/store/states', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_states_by_country_code' ),
'args' => array(
'country' => array(
'required' => true
)
),
)
) );
register_rest_route( 'w2w/v1', '/customers/address', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_addresses' ),
'args' => array(),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'save_address' ),
'args' => array(),
)
) );
register_rest_route( 'w2w/v1', '/customers/delete_address', array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'delete_address' ),
'args' => array(
'id' => array(
'required' => true
)
),
)
) );
}
/**
* Get all allowed countries or shipping countries.
*
* @param WP_REST_Request $request
* @return array
*/
public function get_countries( $request ) {
$countries = WC()->countries->get_shipping_countries();
$data = array();
$data['countries'] = array();
foreach( $countries as $code => $label ) {
$data['countries'][] = array(
'id' => $code,
'label' => html_entity_decode( $label )
);
}
$data['states'] = ! empty( $data['countries'] )
? $this->get_states_by_country_code( array( 'country' => isset( $request['country'] ) ? $request['country'] : $data['countries'][0]['id'] ) )
: false;
//$data['address_fields'] = $this->get_address_fields_by_country_code( array( 'country' => $data['countries'][0]['id'] ) );
return $data;
}
/**
* Get states by country code.
*
* @param WP_REST_Request $request
* @return array
*/
public function get_states_by_country_code( $request ) {
$data = array();
if( isset( $request['country'] ) ) {
$states = WC()->countries->get_shipping_country_states();
$data = array();
if( ! empty( $states[ $request['country'] ] ) ) {
foreach( $states[ $request['country'] ] as $code => $label ) {
$data[] = array(
'id' => $code,
'label' => html_entity_decode( $label )
);
}
}
else {
$data = false;
}
}
return $data;
}
/**
* Validates address.
*
* @param WP_REST_Request $request
* @return array
*/
public function validate_address( $request ) {
$errors = new WP_Error();
$data = WC()->checkout->get_posted_data();
foreach ( WC()->checkout->get_checkout_fields() as $fieldset_key => $fieldset ) {
if ( $fieldset_key == 'shipping' || $fieldset_key == 'account' ) {
continue;
}
foreach ( $fieldset as $key => $field ) {
if ( ! isset( $data[ $key ] ) ) {
continue;
}
$required = ! empty( $field['required'] );
$format = array_filter( isset( $field['validate'] ) ? (array) $field['validate'] : array() );
$field_label = isset( $field['label'] ) ? $field['label'] : '';
switch ( $fieldset_key ) {
case 'shipping':
/* translators: %s: field name */
$field_label = sprintf( __( 'Shipping %s', 'woocommerce' ), $field_label );
break;
case 'billing':
/* translators: %s: field name */
$field_label = sprintf( __( 'Billing %s', 'woocommerce' ), $field_label );
break;
}
if ( in_array( 'postcode', $format, true ) ) {
$country = isset( $data[ $fieldset_key . '_country' ] ) ? $data[ $fieldset_key . '_country' ] : WC()->customer->{"get_{$fieldset_key}_country"}();
$data[ $key ] = wc_format_postcode( $data[ $key ], $country );
if ( '' !== $data[ $key ] && ! WC_Validation::is_postcode( $data[ $key ], $country ) ) {
/* translators: %s: field name */
$errors->add( 'validation', sprintf( __( '%s is not a valid postcode / ZIP.', 'woocommerce' ), '<strong>' . esc_html( $field_label ) . '</strong>' ) );
}
}
if ( in_array( 'phone', $format, true ) ) {
$data[ $key ] = wc_format_phone_number( $data[ $key ] );
if ( '' !== $data[ $key ] && ! WC_Validation::is_phone( $data[ $key ] ) ) {
/* translators: %s: phone number */
$errors->add( 'validation', sprintf( __( '%s is not a valid phone number.', 'woocommerce' ), '<strong>' . esc_html( $field_label ) . '</strong>' ) );
}
}
if ( in_array( 'email', $format, true ) && '' !== $data[ $key ] ) {
$data[ $key ] = sanitize_email( $data[ $key ] );
if ( ! is_email( $data[ $key ] ) ) {
/* translators: %s: email address */
$errors->add( 'validation', sprintf( __( '%s is not a valid email address.', 'woocommerce' ), '<strong>' . esc_html( $field_label ) . '</strong>' ) );
continue;
}
}
if ( '' !== $data[ $key ] && in_array( 'state', $format, true ) ) {
$country = isset( $data[ $fieldset_key . '_country' ] ) ? $data[ $fieldset_key . '_country' ] : WC()->customer->{"get_{$fieldset_key}_country"}();
$valid_states = WC()->countries->get_states( $country );
if ( ! empty( $valid_states ) && is_array( $valid_states ) && count( $valid_states ) > 0 ) {
$valid_state_values = array_map( 'wc_strtoupper', array_flip( array_map( 'wc_strtoupper', $valid_states ) ) );
$data[ $key ] = wc_strtoupper( $data[ $key ] );
if ( isset( $valid_state_values[ $data[ $key ] ] ) ) {
// With this part we consider state value to be valid as well, convert it to the state key for the valid_states check below.
$data[ $key ] = $valid_state_values[ $data[ $key ] ];
}
if ( ! in_array( $data[ $key ], $valid_state_values, true ) ) {
/* translators: 1: state field 2: valid states */
$errors->add( 'validation', sprintf( __( '%1$s is not valid. Please enter one of the following: %2$s', 'woocommerce' ), '<strong>' . esc_html( $field_label ) . '</strong>', implode( ', ', $valid_states ) ) );
}
}
}
if ( $required && '' === $data[ $key ] ) {
/* translators: %s: field name */
$errors->add( 'required-field', apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . esc_html( $field_label ) . '</strong>' ), $field_label ) );
}
}
}
$errs = array();
foreach ( $errors->get_error_messages() as $message ) {
$errs[] = W2W_Util::strip_html_tags( array( 'a', 'strong' ), html_entity_decode( $message ) );
}
return $errs;
}
// 获取地址
public function get_addresses( $request ) {
$errors = array();
if( ! is_user_logged_in() ) {
$errors[] = '请登录后继续操作';
return array(
'success' => false,
'errors' => $errors
);
}
$current_user = wp_get_current_user();
$addresses = (array) get_user_meta( $current_user->ID, 'w2w_addresses', true );
$addresses = array_filter( $addresses );
return array(
'success' => true,
'addresses' => $addresses
);
}
// 保存地址
public function save_address( $request ) {
$errors = array();
if( ! is_user_logged_in() ) {
$errors[] = '请登录后继续操作';
return array(
'success' => false,
'errors' => $errors
);
}
$address = array(
'billing_first_name' => $request['billing_first_name'],
'billing_phone' => $request['billing_phone'],
'billing_country' => $request['billing_country'],
'billing_state' => $request['billing_state'],
'billing_city' => $request['billing_city'],
'billing_address_1' => $request['billing_address_1'],
'billing_postcode' => $request['billing_postcode']
);
$check_address = $address;
$_POST['billing_country'] = $_POST['billing_country']['id'];
if( is_array( $_POST['billing_state'] ) ) {
$_POST['billing_state'] = $_POST['billing_state']['id'];
}
$errors = array_merge( $errors, $this->validate_address( $request ) );
if( ! empty( $errors ) ) {
return array(
'success' => false,
'errors' => $errors
);
}
$current_user = wp_get_current_user();
$addresses = (array) get_user_meta( $current_user->ID, 'w2w_addresses', true );
$addresses = array_filter( $addresses );
if( isset( $request['id'] ) ) {
if( isset( $addresses[ $request['id'] ] ) ) {
$addresses[ $request['id'] ] = $address;
}
else {
$errors[] = '无效的ID';
return array(
'success' => false,
'errors' => $errors
);
}
}
else {
array_unshift( $addresses, $address );
}
update_user_meta( $current_user->ID, 'w2w_addresses', $addresses );
return array(
'success' => true,
'addresses' => $addresses
);
}
// 删除地址
public function delete_address( $request ) {
$errors = array();
if( ! is_user_logged_in() ) {
$errors[] = '请登录后继续操作';
return array(
'success' => false,
'errors' => $errors
);
}
$current_user = wp_get_current_user();
$addresses = (array) get_user_meta( $current_user->ID, 'w2w_addresses', true );
$addresses = array_filter( $addresses );
if( isset( $request['id'], $addresses[ $request['id'] ] ) ) {
unset( $addresses[ $request['id'] ] );
$addresses = array_values( $addresses );
}
else {
$errors[] = '无效的ID';
return array(
'success' => false,
'errors' => $errors
);
}
update_user_meta( $current_user->ID, 'w2w_addresses', $addresses );
return array(
'success' => true,
'addresses' => $addresses
);
}
}
$GLOBALS['W2W_Advanced_Address'] = new W2W_Advanced_Address();
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment