php - 从 Wordpress 插件调用函数
问题描述
我有一个 wordpress 插件,我希望在自定义模板上调用其中一个函数。我要调用的函数是get_ship_now_adjust_date_link
.
是否可以在模板上执行以下操作:
echo get_ship_now_adjust_date_link( $subscription['id'] );
这是插件的完整代码:
<?php
namespace Javorszky\Toolbox;
add_filter( 'wcs_view_subscription_actions', __NAMESPACE__ . '\\add_ship_reschedule_action', 10, 2 );
add_action( 'wp_loaded', __NAMESPACE__ . '\\handle_ship_now_adjust_date_request' );
/**
* Extra actions on the subscription. Only there if the subscription is active.
*
* @param array $actions existing actions on the subscription
* @param \WC_Subscription $subscription the subscription we're adding new actions to
* @return array $actions
*/
function add_ship_reschedule_action( $actions, $subscription ) {
$next_timestamp = $subscription->get_time( 'next_payment' );
if ( 0 != $next_timestamp && 'active' == $subscription->get_status() ) {
$new_actions = array(
'ship_now_recalculate' => array(
'url' => get_ship_now_adjust_date_link( $subscription ),
'name' => Utilities\replace_key_dates( Utilities\get_button_text( 'ship_reschedule_button_text', 'Ship now and recalculate from today' ), $subscription ),
),
);
$actions = array_merge( $actions, $new_actions );
}
return $actions;
}
/**
* URL to be used on the "Ship now and adjust the date" button.
*
* @param \WC_Subscription $subscription Subscription we're getting the link for
* @return string URL to trigger shipping now and keeping the date with
*/
function get_ship_now_adjust_date_link( $subscription ) {
if ( version_compare( \WC_Subscriptions::$version, '2.6.0', '>=' ) ) {
$completed_payments = $subscription->get_payment_count('completed');
} else {
$completed_payments = $subscription->get_completed_payment_count();
}
$action_link = Utilities\strip_custom_query_args();
$action_link = add_query_arg( array( 'subscription_id' => $subscription->get_id(), 'ship_now_adjust_date' => 1 ), $action_link );
$action_link = wp_nonce_url( $action_link, $subscription->get_id() . '_completed_adjust_' . $completed_payments );
return $action_link;
}
/**
* Hooked into `wp_loaded`, this is responsible for charging the subscription now and adjusting the date if certain
* GET variables are present.
*/
function handle_ship_now_adjust_date_request() {
if ( isset( $_GET['ship_now_adjust_date'] ) && isset( $_GET['subscription_id'] ) && isset( $_GET['_wpnonce'] ) && !isset( $_GET['wc-ajax'] ) ) {
$user_id = get_current_user_id();
$subscription = wcs_get_subscription( $_GET['subscription_id'] );
$nonce = $_GET['_wpnonce'];
if ( Utilities\Process\process_ship_now_adjust_date( $user_id, $subscription, $nonce ) ) {
wc_add_notice( _x( 'Your order has been placed!', 'Notice after ship now adjust date request succeeded.', 'jg-toolbox' ) );
wp_safe_redirect( wc_get_endpoint_url( 'view-subscription', $subscription->get_id(), wc_get_page_permalink( 'myaccount' ) ) );
exit;
}
}
}
解决方案
尝试使用\Javorszky\Toolbox\get_ship_now_adjust_date_link
推荐阅读
- python - GitHub GitHub Webhook 秘密验证
- c# - 无法在字符串中搜索换行符
- lua - 如何创建包含需要引用的键的 Luau 类型?
- c# - .NET 6 Preview 7:通用数学的运行时错误
- office-js - Outlook 插件控件元素可以支持多个操作元素吗?
- c++ - 我不断得到用于 interestRate、numberOfPayments 和 loanAmount 的统一变量
- java - 验证电话号码android
- java - 对第三方库的单元测试对象实例化静态调用
- vue.js - Vue:动态创建组件时如何传递props?
- jquery - jQuery - :not 在 css 中工作的选择器不适用于 jquery