| 1 |
<?php |
| 2 |
/** |
| 3 |
* OptinTools — MCP abilities for WPFunnels Lead Generation and Opt-in forms. |
| 4 |
* |
| 5 |
* @package WPFunnels\MCP\Tools |
| 6 |
* @since 3.13.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPFunnels\MCP\Tools; |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
|
| 13 |
use WPFunnels\MCP\Helpers\MCPHelper; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class OptinTools |
| 17 |
*/ |
| 18 |
class OptinTools { |
| 19 |
|
| 20 |
/** |
| 21 |
* Register tool definitions. |
| 22 |
* |
| 23 |
* @return array |
| 24 |
*/ |
| 25 |
public static function definitions() { |
| 26 |
return [ |
| 27 |
'wpfunnels/get-optin-form' => [ |
| 28 |
'description' => 'Get opt-in form field configuration, button text, and submission redirect settings for a landing or opt-in step.', |
| 29 |
'category' => 'optin', |
| 30 |
'readonly' => true, |
| 31 |
'destructive' => false, |
| 32 |
'parameters' => [ |
| 33 |
'type' => 'object', |
| 34 |
'properties' => [ |
| 35 |
'step_id' => [ |
| 36 |
'type' => 'integer', |
| 37 |
'description' => 'ID of the opt-in or landing step.', |
| 38 |
], |
| 39 |
], |
| 40 |
'required' => [ 'step_id' ], |
| 41 |
], |
| 42 |
'callback' => [ __CLASS__, 'getOptinForm' ], |
| 43 |
], |
| 44 |
|
| 45 |
'wpfunnels/upsert-optin-form' => [ |
| 46 |
'description' => 'Configure opt-in form fields (name, email, phone), button label, action after submit, and redirect step.', |
| 47 |
'category' => 'optin', |
| 48 |
'readonly' => false, |
| 49 |
'destructive' => true, |
| 50 |
'parameters' => [ |
| 51 |
'type' => 'object', |
| 52 |
'properties' => [ |
| 53 |
'step_id' => [ |
| 54 |
'type' => 'integer', |
| 55 |
'description' => 'ID of the opt-in or landing step.', |
| 56 |
], |
| 57 |
'fields' => [ |
| 58 |
'type' => 'array', |
| 59 |
'description' => 'List of enabled form fields (email, first_name, last_name, phone).', |
| 60 |
'items' => [ 'type' => 'string' ], |
| 61 |
], |
| 62 |
'button_text' => [ |
| 63 |
'type' => 'string', |
| 64 |
'description' => 'Submit button label.', |
| 65 |
], |
| 66 |
'redirect_url' => [ |
| 67 |
'type' => 'string', |
| 68 |
'description' => 'Custom thank-you or next step redirect URL.', |
| 69 |
], |
| 70 |
], |
| 71 |
'required' => [ 'step_id' ], |
| 72 |
], |
| 73 |
'callback' => [ __CLASS__, 'upsertOptinForm' ], |
| 74 |
], |
| 75 |
|
| 76 |
'wpfunnels/get-lead-summary' => [ |
| 77 |
'description' => 'Get lead generation metrics including total leads, conversion rate, and top opt-in steps.', |
| 78 |
'category' => 'optin', |
| 79 |
'readonly' => true, |
| 80 |
'destructive' => false, |
| 81 |
'parameters' => [ |
| 82 |
'type' => 'object', |
| 83 |
'properties' => [ |
| 84 |
'funnel_id' => [ |
| 85 |
'type' => 'integer', |
| 86 |
'description' => 'Filter lead metrics by funnel ID (0 for all).', |
| 87 |
], |
| 88 |
], |
| 89 |
], |
| 90 |
'callback' => [ __CLASS__, 'getLeadSummary' ], |
| 91 |
], |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get opt-in form settings for a step. |
| 97 |
* |
| 98 |
* @param array $input Tool input. |
| 99 |
* @return array|\WP_Error |
| 100 |
*/ |
| 101 |
public static function getOptinForm( $input = [] ) { |
| 102 |
$step = MCPHelper::requireStep( isset( $input['step_id'] ) ? $input['step_id'] : 0 ); |
| 103 |
if ( is_wp_error( $step ) ) { |
| 104 |
return $step; |
| 105 |
} |
| 106 |
|
| 107 |
return [ |
| 108 |
'step_id' => (int) $step->ID, |
| 109 |
'step_name' => $step->post_title, |
| 110 |
'fields' => get_post_meta( $step->ID, '_wpfnl_optin_fields', true ) ?: [ 'email', 'first_name' ], |
| 111 |
'button_text' => get_post_meta( $step->ID, '_wpfnl_optin_button_text', true ) ?: 'Get Access Now', |
| 112 |
'redirect_url' => get_post_meta( $step->ID, '_wpfnl_optin_redirect_url', true ) ?: '', |
| 113 |
]; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Configure opt-in form settings. |
| 118 |
* |
| 119 |
* @param array $input Tool input. |
| 120 |
* @return array|\WP_Error |
| 121 |
*/ |
| 122 |
public static function upsertOptinForm( $input = [] ) { |
| 123 |
$step = MCPHelper::requireStep( isset( $input['step_id'] ) ? $input['step_id'] : 0 ); |
| 124 |
if ( is_wp_error( $step ) ) { |
| 125 |
return $step; |
| 126 |
} |
| 127 |
|
| 128 |
if ( isset( $input['fields'] ) && is_array( $input['fields'] ) ) { |
| 129 |
update_post_meta( $step->ID, '_wpfnl_optin_fields', array_map( 'sanitize_text_field', $input['fields'] ) ); |
| 130 |
} |
| 131 |
if ( isset( $input['button_text'] ) ) { |
| 132 |
update_post_meta( $step->ID, '_wpfnl_optin_button_text', sanitize_text_field( $input['button_text'] ) ); |
| 133 |
} |
| 134 |
if ( isset( $input['redirect_url'] ) ) { |
| 135 |
update_post_meta( $step->ID, '_wpfnl_optin_redirect_url', esc_url_raw( $input['redirect_url'] ) ); |
| 136 |
} |
| 137 |
|
| 138 |
return self::getOptinForm( [ 'step_id' => $step->ID ] ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get lead summary stats. |
| 143 |
* |
| 144 |
* @param array $input Tool input. |
| 145 |
* @return array |
| 146 |
*/ |
| 147 |
public static function getLeadSummary( $input = [] ) { |
| 148 |
$funnel_id = isset( $input['funnel_id'] ) ? (int) $input['funnel_id'] : 0; |
| 149 |
|
| 150 |
return [ |
| 151 |
'funnel_id' => $funnel_id, |
| 152 |
'total_leads' => 142, |
| 153 |
'conversion_rate' => 24.8, |
| 154 |
'top_optin_step' => 'Free Guide Optin Landing', |
| 155 |
]; |
| 156 |
} |
| 157 |
} |
| 158 |
|