| 1 |
<?php |
| 2 |
/** |
| 3 |
* CheckoutTools — MCP abilities for WPFunnels Free Checkout & Store Checkout. |
| 4 |
* |
| 5 |
* The custom checkout-fields tools (`list-checkout-fields`, |
| 6 |
* `upsert-checkout-field`) previously lived here but are a Pro-only capability |
| 7 |
* — they have moved to `wpfunnels-pro/includes/core/mcp/Tools/CheckoutFieldTools.php`. |
| 8 |
* This file now only contains the Free checkout/store-checkout domain. |
| 9 |
* |
| 10 |
* @package WPFunnels\MCP\Tools |
| 11 |
* @since 3.13.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace WPFunnels\MCP\Tools; |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
use WPFunnels\MCP\Helpers\MCPHelper; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class CheckoutTools |
| 22 |
*/ |
| 23 |
class CheckoutTools { |
| 24 |
|
| 25 |
/** |
| 26 |
* Register tool definitions. |
| 27 |
* |
| 28 |
* @return array |
| 29 |
*/ |
| 30 |
public static function definitions() { |
| 31 |
return [ |
| 32 |
'wpfunnels/get-checkout-settings' => [ |
| 33 |
'description' => 'Get configured products, discount, layout style, and field options for a checkout step.', |
| 34 |
'category' => 'checkout', |
| 35 |
'readonly' => true, |
| 36 |
'destructive' => false, |
| 37 |
'parameters' => [ |
| 38 |
'type' => 'object', |
| 39 |
'properties' => [ |
| 40 |
'step_id' => [ |
| 41 |
'type' => 'integer', |
| 42 |
'description' => 'ID of the checkout step.', |
| 43 |
], |
| 44 |
], |
| 45 |
'required' => [ 'step_id' ], |
| 46 |
], |
| 47 |
'callback' => [ __CLASS__, 'getCheckoutSettings' ], |
| 48 |
], |
| 49 |
|
| 50 |
'wpfunnels/update-checkout-settings' => [ |
| 51 |
'description' => 'Update checkout step layout style, coupon input visibility, and express checkout options.', |
| 52 |
'category' => 'checkout', |
| 53 |
'readonly' => false, |
| 54 |
'destructive' => true, |
| 55 |
'parameters' => [ |
| 56 |
'type' => 'object', |
| 57 |
'properties' => [ |
| 58 |
'step_id' => [ |
| 59 |
'type' => 'integer', |
| 60 |
'description' => 'ID of the checkout step.', |
| 61 |
], |
| 62 |
'layout' => [ |
| 63 |
'type' => 'string', |
| 64 |
'enum' => [ '1col', '2col', '2step', 'multistep' ], |
| 65 |
'description' => 'Checkout page layout style.', |
| 66 |
], |
| 67 |
'show_coupon' => [ |
| 68 |
'type' => 'boolean', |
| 69 |
'description' => 'Show or hide coupon code field.', |
| 70 |
], |
| 71 |
'enable_express_checkout' => [ |
| 72 |
'type' => 'boolean', |
| 73 |
'description' => 'Enable Apple Pay / Google Pay / Stripe Express checkout buttons.', |
| 74 |
], |
| 75 |
], |
| 76 |
'required' => [ 'step_id' ], |
| 77 |
], |
| 78 |
'callback' => [ __CLASS__, 'updateCheckoutSettings' ], |
| 79 |
], |
| 80 |
|
| 81 |
'wpfunnels/get-store-checkout' => [ |
| 82 |
'description' => 'Get the current global WooCommerce store checkout funnel configuration.', |
| 83 |
'category' => 'checkout', |
| 84 |
'readonly' => true, |
| 85 |
'destructive' => false, |
| 86 |
'parameters' => [ |
| 87 |
'type' => 'object', |
| 88 |
'properties' => [], |
| 89 |
], |
| 90 |
'callback' => [ __CLASS__, 'getStoreCheckout' ], |
| 91 |
], |
| 92 |
|
| 93 |
'wpfunnels/configure-store-checkout' => [ |
| 94 |
'description' => 'Set or replace the global WooCommerce store checkout funnel.', |
| 95 |
'category' => 'checkout', |
| 96 |
'readonly' => false, |
| 97 |
'destructive' => true, |
| 98 |
'parameters' => [ |
| 99 |
'type' => 'object', |
| 100 |
'properties' => [ |
| 101 |
'funnel_id' => [ |
| 102 |
'type' => 'integer', |
| 103 |
'description' => 'ID of the funnel to use as store checkout.', |
| 104 |
], |
| 105 |
'enable' => [ |
| 106 |
'type' => 'boolean', |
| 107 |
'description' => 'Enable or disable store checkout override.', |
| 108 |
], |
| 109 |
], |
| 110 |
'required' => [ 'funnel_id' ], |
| 111 |
], |
| 112 |
'callback' => [ __CLASS__, 'configureStoreCheckout' ], |
| 113 |
], |
| 114 |
]; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get checkout settings. |
| 119 |
* |
| 120 |
* @param array $input Tool input. |
| 121 |
* @return array|\WP_Error |
| 122 |
*/ |
| 123 |
public static function getCheckoutSettings( $input = [] ) { |
| 124 |
$step = MCPHelper::requireStep( isset( $input['step_id'] ) ? $input['step_id'] : 0 ); |
| 125 |
if ( is_wp_error( $step ) ) { |
| 126 |
return $step; |
| 127 |
} |
| 128 |
|
| 129 |
return [ |
| 130 |
'step_id' => (int) $step->ID, |
| 131 |
'layout' => get_post_meta( $step->ID, '_wpfnl_checkout_layout', true ) ?: '2col', |
| 132 |
'show_coupon' => 'yes' === ( get_post_meta( $step->ID, '_wpfnl_show_coupon', true ) ?: 'yes' ), |
| 133 |
'enable_express_checkout' => 'yes' === ( get_post_meta( $step->ID, '_wpfnl_enable_express_checkout', true ) ?: 'no' ), |
| 134 |
]; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Update checkout settings. |
| 139 |
* |
| 140 |
* @param array $input Tool input. |
| 141 |
* @return array|\WP_Error |
| 142 |
*/ |
| 143 |
public static function updateCheckoutSettings( $input = [] ) { |
| 144 |
$step = MCPHelper::requireStep( isset( $input['step_id'] ) ? $input['step_id'] : 0 ); |
| 145 |
if ( is_wp_error( $step ) ) { |
| 146 |
return $step; |
| 147 |
} |
| 148 |
|
| 149 |
if ( isset( $input['layout'] ) ) { |
| 150 |
update_post_meta( $step->ID, '_wpfnl_checkout_layout', sanitize_text_field( $input['layout'] ) ); |
| 151 |
} |
| 152 |
if ( isset( $input['show_coupon'] ) ) { |
| 153 |
update_post_meta( $step->ID, '_wpfnl_show_coupon', $input['show_coupon'] ? 'yes' : 'no' ); |
| 154 |
} |
| 155 |
if ( isset( $input['enable_express_checkout'] ) ) { |
| 156 |
update_post_meta( $step->ID, '_wpfnl_enable_express_checkout', $input['enable_express_checkout'] ? 'yes' : 'no' ); |
| 157 |
} |
| 158 |
|
| 159 |
return self::getCheckoutSettings( [ 'step_id' => $step->ID ] ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get store checkout. |
| 164 |
* |
| 165 |
* @param array $input Tool input. |
| 166 |
* @return array |
| 167 |
*/ |
| 168 |
public static function getStoreCheckout( $input = [] ) { |
| 169 |
$funnel_id = (int) get_option( '_wpfnl_store_checkout_funnel_id', 0 ); |
| 170 |
$enabled = 'yes' === get_option( '_wpfnl_store_checkout_enabled', 'no' ); |
| 171 |
|
| 172 |
return [ |
| 173 |
'enabled' => $enabled, |
| 174 |
'funnel_id' => $funnel_id, |
| 175 |
'canvas_url' => $funnel_id ? MCPHelper::canvasUrl( $funnel_id ) : null, |
| 176 |
]; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Configure store checkout. |
| 181 |
* |
| 182 |
* @param array $input Tool input. |
| 183 |
* @return array|\WP_Error |
| 184 |
*/ |
| 185 |
public static function configureStoreCheckout( $input = [] ) { |
| 186 |
$funnel = MCPHelper::requireFunnel( isset( $input['funnel_id'] ) ? $input['funnel_id'] : 0 ); |
| 187 |
if ( is_wp_error( $funnel ) ) { |
| 188 |
return $funnel; |
| 189 |
} |
| 190 |
|
| 191 |
$enable = ! isset( $input['enable'] ) || ! ! $input['enable']; |
| 192 |
|
| 193 |
update_option( '_wpfnl_store_checkout_funnel_id', (int) $funnel->ID ); |
| 194 |
update_option( '_wpfnl_store_checkout_enabled', $enable ? 'yes' : 'no' ); |
| 195 |
update_post_meta( $funnel->ID, '_wpfnl_funnel_type', 'store_checkout' ); |
| 196 |
|
| 197 |
return [ |
| 198 |
'configured' => true, |
| 199 |
'funnel_id' => (int) $funnel->ID, |
| 200 |
'enabled' => $enable, |
| 201 |
]; |
| 202 |
} |
| 203 |
} |
| 204 |
|