Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/elemento.../modules/componen...
File: components-rest-api.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Elementor\Modules\Components;
[2] Fix | Delete
[3] Fix | Delete
use Elementor\Core\Utils\Api\Error_Builder;
[4] Fix | Delete
use Elementor\Core\Utils\Api\Response_Builder;
[5] Fix | Delete
[6] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[7] Fix | Delete
exit; // Exit if accessed directly.
[8] Fix | Delete
}
[9] Fix | Delete
[10] Fix | Delete
class Components_REST_API {
[11] Fix | Delete
const API_NAMESPACE = 'elementor/v1';
[12] Fix | Delete
const API_BASE = 'components';
[13] Fix | Delete
const STYLES_ROUTE = 'styles';
[14] Fix | Delete
const MAX_COMPONENTS = 50;
[15] Fix | Delete
[16] Fix | Delete
private $repository = null;
[17] Fix | Delete
[18] Fix | Delete
public function register_hooks() {
[19] Fix | Delete
add_action( 'rest_api_init', fn() => $this->register_routes() );
[20] Fix | Delete
}
[21] Fix | Delete
[22] Fix | Delete
private function get_repository() {
[23] Fix | Delete
if ( ! $this->repository ) {
[24] Fix | Delete
$this->repository = new Components_Repository();
[25] Fix | Delete
}
[26] Fix | Delete
[27] Fix | Delete
return $this->repository;
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
private function register_routes() {
[31] Fix | Delete
register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE, [
[32] Fix | Delete
[
[33] Fix | Delete
'methods' => 'GET',
[34] Fix | Delete
'callback' => fn() => $this->route_wrapper( fn() => $this->get_components() ),
[35] Fix | Delete
'permission_callback' => fn() => is_user_logged_in(),
[36] Fix | Delete
],
[37] Fix | Delete
] );
[38] Fix | Delete
[39] Fix | Delete
register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/' . self::STYLES_ROUTE, [
[40] Fix | Delete
[
[41] Fix | Delete
'methods' => 'GET',
[42] Fix | Delete
'callback' => fn() => $this->route_wrapper( fn() => $this->get_styles() ),
[43] Fix | Delete
'permission_callback' => fn() => is_user_logged_in(),
[44] Fix | Delete
],
[45] Fix | Delete
] );
[46] Fix | Delete
[47] Fix | Delete
register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE, [
[48] Fix | Delete
[
[49] Fix | Delete
'methods' => 'POST',
[50] Fix | Delete
'callback' => fn( $request ) => $this->route_wrapper( fn() => $this->create_component( $request ) ),
[51] Fix | Delete
'permission_callback' => fn() => current_user_can( 'manage_options' ),
[52] Fix | Delete
'args' => [
[53] Fix | Delete
'name' => [
[54] Fix | Delete
'type' => 'string',
[55] Fix | Delete
'required' => true,
[56] Fix | Delete
],
[57] Fix | Delete
'content' => [
[58] Fix | Delete
'type' => 'array',
[59] Fix | Delete
'required' => true,
[60] Fix | Delete
'items' => [
[61] Fix | Delete
'type' => 'object',
[62] Fix | Delete
],
[63] Fix | Delete
],
[64] Fix | Delete
],
[65] Fix | Delete
],
[66] Fix | Delete
] );
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
private function get_components() {
[70] Fix | Delete
$components = $this->get_repository()->all();
[71] Fix | Delete
[72] Fix | Delete
$components_list = $components->get_components()->map( fn( $component ) => [
[73] Fix | Delete
'id' => $component['id'],
[74] Fix | Delete
'name' => $component['name'],
[75] Fix | Delete
])->all();
[76] Fix | Delete
[77] Fix | Delete
return Response_Builder::make( $components_list )->build();
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
private function get_styles() {
[81] Fix | Delete
$components = $this->get_repository()->all();
[82] Fix | Delete
[83] Fix | Delete
$styles = [];
[84] Fix | Delete
$components->get_components()->each( function( $component ) use ( &$styles ) {
[85] Fix | Delete
$styles[ $component['id'] ] = $component['styles'];
[86] Fix | Delete
} );
[87] Fix | Delete
[88] Fix | Delete
return Response_Builder::make( $styles )->build();
[89] Fix | Delete
}
[90] Fix | Delete
private function create_component( \WP_REST_Request $request ) {
[91] Fix | Delete
$components = $this->get_repository()->all();
[92] Fix | Delete
$components_count = $components->get_components()->count();
[93] Fix | Delete
[94] Fix | Delete
if ( $components_count >= static::MAX_COMPONENTS ) {
[95] Fix | Delete
return Error_Builder::make( 'components_limit_exceeded' )
[96] Fix | Delete
->set_status( 400 )
[97] Fix | Delete
->set_message( sprintf(
[98] Fix | Delete
/* translators: %d: maximum components limit. */
[99] Fix | Delete
__( 'Components limit exceeded. Maximum allowed: %d', 'elementor' ),
[100] Fix | Delete
static::MAX_COMPONENTS
[101] Fix | Delete
) )
[102] Fix | Delete
->build();
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
$parser = Components_Parser::make();
[106] Fix | Delete
[107] Fix | Delete
$name_result = $parser->parse_name( $request->get_param( 'name' ), $components->get_components()->map( fn( $component ) => $component['name'] )->all() );
[108] Fix | Delete
[109] Fix | Delete
if ( ! $name_result->is_valid() ) {
[110] Fix | Delete
return Error_Builder::make( 'invalid_name' )
[111] Fix | Delete
->set_status( 400 )
[112] Fix | Delete
->set_message( 'Invalid component name: ' . $name_result->errors()->to_string() )
[113] Fix | Delete
->build();
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
$name = $name_result->unwrap();
[117] Fix | Delete
// The content is validated & sanitized in the document save process.
[118] Fix | Delete
$content = $request->get_param( 'content' );
[119] Fix | Delete
[120] Fix | Delete
try {
[121] Fix | Delete
$component_id = $this->get_repository()->create( $name, $content );
[122] Fix | Delete
[123] Fix | Delete
return Response_Builder::make( [ 'component_id' => $component_id ] )->set_status( 201 )->build();
[124] Fix | Delete
} catch ( \Exception $e ) {
[125] Fix | Delete
$error_message = $e->getMessage();
[126] Fix | Delete
[127] Fix | Delete
$invalid_elements_structure_error = str_contains( $error_message, 'Invalid data' );
[128] Fix | Delete
$atomic_styles_validation_error = str_contains( $error_message, 'Styles validation failed' );
[129] Fix | Delete
$atomic_settings_validation_error = str_contains( $error_message, 'Settings validation failed' );
[130] Fix | Delete
[131] Fix | Delete
if ( $invalid_elements_structure_error || $atomic_styles_validation_error || $atomic_settings_validation_error ) {
[132] Fix | Delete
return Error_Builder::make( 'content_validation_failed' )
[133] Fix | Delete
->set_status( 400 )
[134] Fix | Delete
->set_message( $error_message )
[135] Fix | Delete
->build();
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
throw $e;
[139] Fix | Delete
}
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
private function route_wrapper( callable $cb ) {
[143] Fix | Delete
try {
[144] Fix | Delete
$response = $cb();
[145] Fix | Delete
} catch ( \Exception $e ) {
[146] Fix | Delete
return Error_Builder::make( 'unexpected_error' )
[147] Fix | Delete
->set_message( __( 'Something went wrong', 'elementor' ) )
[148] Fix | Delete
->build();
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
return $response;
[152] Fix | Delete
}
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function