我正在使用一个自定义XML-RPC方法来捕获我正在构建的插件的一些信息。激活后,用户必须通过输入在我的WP网站上购买插件时提供给他们的用户/密码来验证自己。
插件使用以下代码:
if ( isset( $_POST[\'username\'] ) && isset( $_POST[\'password\'] ) ) {
/* check against remote server */
require_once( ABSPATH . WPINC . \'/class-IXR.php\' );
$this->client = new IXR_Client( trailingslashit( CUSTOMLOGIN_UPDATE_API ) . \'xmlrpc.php\' ); //*/
$url = ( ( is_multisite() ) ? network_site_url() : site_url() );
$client_request_args = array(
\'username\' => $_POST[\'username\'],
\'password\' => $_POST[\'password\'],
\'plugin\' => CUSTOMLOGINPRO_BASENAME,
\'url\' => $url
);
if ( !$this->client->query( \'thefrosty.is_user_authorized\', $client_request_args ) ) {
add_action( \'admin_notices\', array( $this, \'error_notice\' ) );
return false;
}
$this->settings = get_option( CUSTOMLOGINPRO . \'_settings\', array() );
$this->settings[\'api-key\'] = $this->client->getResponse();
update_option( CUSTOMLOGINPRO . \'_settings\', $this->settings );
header( \'Location: \' . admin_url( \'options-general.php?page=\' . CUSTOMLOGINPRO ) );
die();
}
表单提交后看起来很正常,在本地工作时出现200个错误,但使用另一个插件进行测试,该插件发送相同的错误代码,但正在从主机发送响应(现在不用担心)。我一直坚持的是后端代码。我在WP安装的核心插件中创建了一个类。我想这就是它需要的地方。我缺少一些故障代码和错误消息响应,不知道如何执行这些操作。只是想在正确的方向上推动一下。。
正如您所见,我也在针对WooCommerce订单测试用户(WC的代码应该很好),但是尝试更新订单元数据可能需要修复以保存用户URL。
class frosty_core {
function __construct() {
add_filter( \'xmlrpc_methods\', array( $this, \'xmlrpc_methods\' ) );
}
/**
* Create our custom XML-rpc method.
* @ref http://kovshenin.com/2010/04/custom-xml-rpc-methods-in-wordpress-2151/
*/
function xmlrpc_methods( $methods ) {
$methods[\'thefrosty.is_user_authorized\'] = array( $this, \'thefrosty_plugin_callback\' );
return $methods;
}
/**
* XML-prc mothod
*/
function thefrosty_plugin_callback( $args ) {
// Parse the arguments, assuming they\'re in the correct order
$username = $args[0];
$password = $args[1];
$plugin_name = $args[2];
$url = $args[3];
global $wp_xmlrpc_server;
// Let\'s run a check to see if credentials are okay
if ( !$user = $wp_xmlrpc_server->login($username, $password) ) {
return $wp_xmlrpc_server->error;
}
if ( !class_exists( \'woocommerce\' ) ) return \'error, please try again later\';
/* Get the user ID by name */
$current_user_id = get_userdatabylogin( $username );
/* woocommerce/shortcodes/shortcode-my_account.php */
$args = array(
\'numberposts\' => -1,
\'meta_key\' => \'_customer_user\',
\'meta_value\' => $current_user_id,
\'post_type\' => \'shop_order\',
\'post_status\' => \'publish\'
);
$customer_orders = get_posts( $args );
$match = false;
foreach ( $customer_orders as $customer_order ) :
$order = &new woocommerce_order();
$order->populate( $customer_order );
$status = get_term_by( \'slug\', $order->status, \'shop_order_status\' );
if ( \'completed\' !== $status->name ) return; //error, order not completed //*/
if ( $plugin_name !== $order->items->name ) return; // you have not purchased this plugin //*/
$match = true;
$apikey = $order->order_key;
endforeach;
if ( isset( $match ) && $match ) {
/* woocommerce/admin/writepanels/writepanel-order_data.php */
add_filter( \'update_order_item\', create_function( \'$order_items\', \'
$new_meta = &new order_item_meta();
$meta_name = "active_urls";
$meta_value = esc_url( $url );
$new_meta->add( $meta_name, $meta_value );
return $order_items["item_meta"] => $new_meta->meta;\' ) );
return $apikey;
}
else return false;
}
}