| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_CDEGN_Rest |
| 4 |
{ |
| 5 |
private $core = null; |
| 6 |
private $namespace = 'code-engine/v1'; |
| 7 |
|
| 8 |
public function __construct( $core ) { |
| 9 |
if ( !current_user_can( 'administrator' ) ) { |
| 10 |
return; |
| 11 |
} |
| 12 |
$this->core = $core; |
| 13 |
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
| 14 |
} |
| 15 |
|
| 16 |
public function rest_api_init() { |
| 17 |
try { |
| 18 |
// POST endpoints |
| 19 |
register_rest_route( $this->namespace, '/register', array( |
| 20 |
'methods' => 'POST', |
| 21 |
'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 22 |
'callback' => array( $this, 'rest_register' ), |
| 23 |
) ); |
| 24 |
register_rest_route( $this->namespace, '/unregister', array( |
| 25 |
'methods' => 'POST', |
| 26 |
'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 27 |
'callback' => array( $this, 'rest_unregister' ), |
| 28 |
) ); |
| 29 |
register_rest_route( $this->namespace, '/refresh', array( |
| 30 |
'methods' => 'POST', |
| 31 |
'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 32 |
'callback' => array( $this, 'rest_refresh' ), |
| 33 |
) ); |
| 34 |
register_rest_route( $this->namespace, '/check-quality', array( |
| 35 |
'methods' => 'POST', |
| 36 |
'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 37 |
'callback' => array( $this, 'rest_check_quality' ), |
| 38 |
) ); |
| 39 |
|
| 40 |
// GET endpoints |
| 41 |
register_rest_route( $this->namespace, '/functions', array( |
| 42 |
'methods' => 'GET', |
| 43 |
'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 44 |
'callback' => array( $this, 'rest_functions' ), |
| 45 |
) ); |
| 46 |
} |
| 47 |
catch ( Exception $e ) { |
| 48 |
var_dump( $e ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
public function rest_register( $request ) { |
| 53 |
try { |
| 54 |
$json = $request->get_json_params(); |
| 55 |
$snippet_id = isset( $json['snippet_id'] ) ? (int)$json['snippet_id'] : null; |
| 56 |
$snippet_src = isset( $json['snippet_src'] ) ? (string)$json['snippet_src'] : null; |
| 57 |
if ( !$snippet_id || !$snippet_src ) { |
| 58 |
throw new Exception( __( 'Snippet ID and Snippet src are required.', 'code-engine') ); |
| 59 |
} |
| 60 |
|
| 61 |
$snippet_information = $this->core->analyze_code( $snippet_id, $snippet_src ); |
| 62 |
$new_option = [ |
| 63 |
'snippet_id' => $snippet_id, |
| 64 |
'snippet_src' => $snippet_src, |
| 65 |
'name' => $snippet_information['function_name'], |
| 66 |
'desc' => '', |
| 67 |
'args' => $snippet_information['args'], |
| 68 |
]; |
| 69 |
$this->core->add_cdegn_functions( $new_option ); |
| 70 |
|
| 71 |
$cdegn_function = new Meow_CDEGN_Models_Cdegn_Function( $new_option ); |
| 72 |
|
| 73 |
return new WP_REST_Response( [ |
| 74 |
'message' => 'Registered successfully.', |
| 75 |
'overview' => $cdegn_function->overview(), |
| 76 |
], 200 ); |
| 77 |
} |
| 78 |
catch ( Exception $e ) { |
| 79 |
return new WP_REST_Response( [ 'message' => $e->getMessage() ], 500 ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public function rest_unregister( $request ) { |
| 84 |
try { |
| 85 |
$json = $request->get_json_params(); |
| 86 |
$snippet_id = isset( $json['snippet_id'] ) ? (int)$json['snippet_id'] : null; |
| 87 |
$snippet_src = isset( $json['snippet_src'] ) ? (string)$json['snippet_src'] : null; |
| 88 |
if ( !$snippet_id || !$snippet_src ) { |
| 89 |
throw new Exception( __( 'Snippet ID and Snippet src are required.', 'code-engine') ); |
| 90 |
} |
| 91 |
|
| 92 |
$this->core->remove_cdegn_functions( $snippet_id, $snippet_src ); |
| 93 |
|
| 94 |
return new WP_REST_Response( [ |
| 95 |
'message' => 'Unregistered successfully.', |
| 96 |
], 200 ); |
| 97 |
} |
| 98 |
catch ( Exception $e ) { |
| 99 |
return new WP_REST_Response( [ 'message' => $e->getMessage() ], 500 ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
public function rest_refresh( $request ) { |
| 104 |
try { |
| 105 |
$json = $request->get_json_params(); |
| 106 |
$snippet_id = isset( $json['snippet_id'] ) ? (int)$json['snippet_id'] : null; |
| 107 |
$snippet_src = isset( $json['snippet_src'] ) ? (string)$json['snippet_src'] : null; |
| 108 |
if ( !$snippet_id || !$snippet_src ) { |
| 109 |
throw new Exception( __( 'Snippet ID and Snippet src are required.', 'code-engine') ); |
| 110 |
} |
| 111 |
|
| 112 |
$snippet_information = $this->core->analyze_code( $snippet_id, $snippet_src ); |
| 113 |
$new_option = [ |
| 114 |
'snippet_id' => $snippet_id, |
| 115 |
'snippet_src' => 'Code Snippets', |
| 116 |
'name' => $snippet_information['function_name'], |
| 117 |
'desc' => '', |
| 118 |
'args' => $snippet_information['args'], |
| 119 |
]; |
| 120 |
$this->core->add_cdegn_functions( $new_option ); |
| 121 |
|
| 122 |
$cdegn_function = new Meow_CDEGN_Models_Cdegn_Function( $new_option ); |
| 123 |
|
| 124 |
return new WP_REST_Response( [ |
| 125 |
'message' => 'Refreshed successfully.', |
| 126 |
'overview' => $cdegn_function->overview(), |
| 127 |
], 200 ); |
| 128 |
} |
| 129 |
catch ( Exception $e ) { |
| 130 |
return new WP_REST_Response( [ 'message' => $e->getMessage() ], 500 ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
public function rest_check_quality( $request ) { |
| 135 |
try { |
| 136 |
$json = $request->get_json_params(); |
| 137 |
$snippet_id = isset( $json['snippet_id'] ) ? (int)$json['snippet_id'] : null; |
| 138 |
$snippet_src = isset( $json['snippet_src'] ) ? (string)$json['snippet_src'] : null; |
| 139 |
if ( !$snippet_id || !$snippet_src ) { |
| 140 |
throw new Exception( __( 'Snippet ID and Snippet src are required.', 'code-engine') ); |
| 141 |
} |
| 142 |
|
| 143 |
$detail = $this->core->get_code_quality_via_ai_engine( $snippet_id, $snippet_src ); |
| 144 |
|
| 145 |
return new WP_REST_Response( [ |
| 146 |
'success' => true, |
| 147 |
'detail' => $detail, |
| 148 |
], 200 ); |
| 149 |
} |
| 150 |
catch ( Exception $e ) { |
| 151 |
return new WP_REST_Response( [ 'message' => $e->getMessage() ], 500 ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
public function rest_functions() { |
| 156 |
try { |
| 157 |
return new WP_REST_Response( [ |
| 158 |
'functions' => array_map( function( $cdegn_function ) { |
| 159 |
return new Meow_CDEGN_Models_Cdegn_Function( $cdegn_function ); |
| 160 |
}, get_option( 'cdegn_functions', [] ) ), |
| 161 |
], 200 ); |
| 162 |
} |
| 163 |
catch ( Exception $e ) { |
| 164 |
return new WP_REST_Response( [ 'message' => $e->getMessage() ], 500 ); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|