| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server |
| 4 |
* |
| 5 |
* @package |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Rest; |
| 8 |
|
| 9 |
class Server |
| 10 |
{ |
| 11 |
|
| 12 |
/** |
| 13 |
* The single instance of the class. |
| 14 |
* |
| 15 |
* @var object |
| 16 |
*/ |
| 17 |
protected static $instance = null; |
| 18 |
|
| 19 |
/** |
| 20 |
* REST API namespaces and endpoints. |
| 21 |
* |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
protected $controllers = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* Get class instance. |
| 28 |
* |
| 29 |
* @return object Instance. |
| 30 |
*/ |
| 31 |
final public static function instance() |
| 32 |
{ |
| 33 |
if (null === static::$instance) { |
| 34 |
static::$instance = new static(); |
| 35 |
} |
| 36 |
return static::$instance; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Hook into WordPress ready to init the REST API as needed. |
| 41 |
*/ |
| 42 |
public function init() |
| 43 |
{ |
| 44 |
add_action('rest_api_init', [ $this, 'register_rest_routes' ], 10); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Register REST API routes. |
| 49 |
*/ |
| 50 |
public function register_rest_routes() |
| 51 |
{ |
| 52 |
|
| 53 |
foreach ($this->get_rest_namespaces() as $namespace => $controllers) { |
| 54 |
foreach ($controllers as $controller_name => $controller_class) { |
| 55 |
$controller_class_name = "WPFunnels\\Rest\\Controllers\\".$controller_class; |
| 56 |
|
| 57 |
if ( ! class_exists( $controller_class_name ) ) { |
| 58 |
continue; |
| 59 |
} |
| 60 |
|
| 61 |
$this->controllers[ $namespace ][ $controller_name ] = new $controller_class_name(); |
| 62 |
$this->controllers[ $namespace ][ $controller_name ]->register_routes(); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
/** |
| 69 |
* Get API namespaces - new namespaces should be registered here. |
| 70 |
* |
| 71 |
* @return array List of Namespaces and Main controller classes. |
| 72 |
*/ |
| 73 |
protected function get_rest_namespaces() |
| 74 |
{ |
| 75 |
return [ |
| 76 |
'wpfunnels/v1' => $this->get_controllers(), |
| 77 |
]; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* List of controllers in the wc/v1 namespace. |
| 82 |
* |
| 83 |
* @return array |
| 84 |
*/ |
| 85 |
protected function get_controllers() |
| 86 |
{ |
| 87 |
return apply_filters( 'wpfunnels/rest_api_controllers', array( |
| 88 |
'mcp_settings' => 'McpSettingsController', |
| 89 |
'settings' => 'SettingsController', |
| 90 |
'templates_library' => 'TemplateLibraryController', |
| 91 |
'order_bump' => 'OrderBumpController', |
| 92 |
'funnel_control' => 'FunnelController', |
| 93 |
'offer' => 'OfferController', |
| 94 |
'step_control' => 'StepController', |
| 95 |
'remote_funnel' => 'RemoteFunnelsController', |
| 96 |
'products' => 'ProductsController', |
| 97 |
'gutenberg' => 'GutenbergCSSController', |
| 98 |
'checkout' => 'CheckoutController', |
| 99 |
'importexport' => 'ImportExportController', |
| 100 |
'report' => 'DashboardController', |
| 101 |
'plugin_installer' => 'PluginInstallerController', |
| 102 |
'setup_wizard' => 'SetupWizardController', |
| 103 |
'store_checkout' => 'StoreCheckoutController', |
| 104 |
'automation' => 'WpfnlAutomationController', |
| 105 |
'ai_chat' => 'AiChatController', |
| 106 |
'ai_generate' => 'SingleShotAiController', |
| 107 |
)); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Return the path to the package. |
| 112 |
* |
| 113 |
* @return string |
| 114 |
*/ |
| 115 |
public static function get_path() |
| 116 |
{ |
| 117 |
return dirname(__DIR__); |
| 118 |
} |
| 119 |
} |
| 120 |
|