*Thisprevents"Your settings have been saved."noticesonthetablelist.
*
*@parambool$allowIfallowsavesettings.
*@returnbool
*/
publicfunctionallow_save_settings($allow){
if(!isset($_GET['edit-webhook'])){// WPCS: input var okay, CSRF ok.
returnfalse;
}
return$allow;
}
/**
*Checkifiswebhooksettingspage.
*
*@returnbool
*/
privatefunctionis_webhook_settings_page(){
returnisset($_GET['page'],$_GET['tab'],$_GET['section'])&&'wc-settings'===$_GET['page']&&'advanced'===$_GET['tab']&&'webhooks'===$_GET['section'];// WPCS: input var okay, CSRF ok.
}
/**
*Savemethod.
*/
privatefunctionsave(){
check_admin_referer('woocommerce-settings');
if(!current_user_can('manage_woocommerce')){
wp_die(esc_html__('You do not have permission to update Webhooks','woocommerce'));
}
$errors=array();
$webhook_id=isset($_POST['webhook_id'])?absint($_POST['webhook_id']):0;// WPCS: input var okay, CSRF ok.
$webhook=newWC_Webhook($webhook_id);
// Name.
if(!empty($_POST['webhook_name'])){// WPCS: input var okay, CSRF ok.
$name=sanitize_text_field(wp_unslash($_POST['webhook_name']));// WPCS: input var okay, CSRF ok.
}else{
$name=sprintf(
/* translators: %s: date */
__('Webhook created on %s','woocommerce'),
// @codingStandardsIgnoreStart
strftime(_x('%b %d, %Y @ %I:%M %p','Webhook created on date parsed by strftime','woocommerce'))
// @codingStandardsIgnoreEnd
);
}
$webhook->set_name($name);
if(!$webhook->get_user_id()){
$webhook->set_user_id(get_current_user_id());
}
// Status.
$webhook->set_status(!empty($_POST['webhook_status'])?sanitize_text_field(wp_unslash($_POST['webhook_status'])):'disabled');// WPCS: input var okay, CSRF ok.
// Delivery URL.
$delivery_url=!empty($_POST['webhook_delivery_url'])?esc_url_raw(wp_unslash($_POST['webhook_delivery_url'])):'';// WPCS: input var okay, CSRF ok.
if(wc_is_valid_url($delivery_url)){
$webhook->set_delivery_url($delivery_url);
}
// Secret.
$secret=!empty($_POST['webhook_secret'])?sanitize_text_field(wp_unslash($_POST['webhook_secret'])):wp_generate_password(50,true,true);// WPCS: input var okay, CSRF ok.
$webhook->set_secret($secret);
// Topic.
if(!empty($_POST['webhook_topic'])){// WPCS: input var okay, CSRF ok.
$resource='';
$event='';
switch($_POST['webhook_topic']){// WPCS: input var okay, CSRF ok.
case'action':
$resource='action';
$event=!empty($_POST['webhook_action_event'])?sanitize_text_field(wp_unslash($_POST['webhook_action_event'])):'';// WPCS: input var okay, CSRF ok.
break;
default:
list($resource,$event)=explode('.',sanitize_text_field(wp_unslash($_POST['webhook_topic'])));// WPCS: input var okay, CSRF ok.
break;
}
$topic=$resource.'.'.$event;
if(wc_is_webhook_valid_topic($topic)){
$webhook->set_topic($topic);
}else{
$errors[]=__('Webhook topic unknown. Please select a valid topic.','woocommerce');
}
}
// API version.
$webhook->set_api_version(!empty($_POST['webhook_api_version'])?sanitize_text_field(wp_unslash($_POST['webhook_api_version'])):'wp_api_v2');// WPCS: input var okay, CSRF ok.
<h2class="woocommerce-BlankState-message"><?phpesc_html_e('Webhooks are event notifications sent to URLs of your choice. They can be used to integrate with third-party services which support them.','woocommerce');?></h2>
<aclass="woocommerce-BlankState-cta button-primary button"href="<?php echo esc_url( admin_url( 'admin.php?page=wc-settings&tab=advanced§ion=webhooks&edit-webhook=0' ) ); ?>"><?phpesc_html_e('Create a new webhook','woocommerce');?></a>