Edit File by line
/home/zeestwma/ceyloniy.../wp-conte.../plugins/elemento.../modules/variable.../services
File: variables-service.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Elementor\Modules\Variables\Services;
[2] Fix | Delete
[3] Fix | Delete
use Elementor\Modules\Variables\Services\Batch_Operations\Batch_Error_Formatter;
[4] Fix | Delete
use Elementor\Modules\Variables\Services\Batch_Operations\Batch_Processor;
[5] Fix | Delete
use Elementor\Modules\Variables\Storage\Entities\Variable;
[6] Fix | Delete
use Elementor\Modules\Variables\Storage\Exceptions\BatchOperationFailed;
[7] Fix | Delete
use Elementor\Modules\Variables\Storage\Variables_Repository;
[8] Fix | Delete
use Elementor\Modules\Variables\Storage\Exceptions\FatalError;
[9] Fix | Delete
use Elementor\Modules\Variables\PropTypes\Size_Variable_Prop_Type;
[10] Fix | Delete
use Elementor\Utils as ElementorUtils;
[11] Fix | Delete
[12] Fix | Delete
class Variables_Service {
[13] Fix | Delete
private Variables_Repository $repo;
[14] Fix | Delete
private Batch_Processor $batch_processor;
[15] Fix | Delete
[16] Fix | Delete
public function __construct( Variables_Repository $repository, Batch_Processor $batch_processor ) {
[17] Fix | Delete
$this->repo = $repository;
[18] Fix | Delete
$this->batch_processor = $batch_processor;
[19] Fix | Delete
}
[20] Fix | Delete
[21] Fix | Delete
public function get_variables_list(): array {
[22] Fix | Delete
return $this->load()['data'];
[23] Fix | Delete
}
[24] Fix | Delete
[25] Fix | Delete
public function load() {
[26] Fix | Delete
$collection = $this->repo->load()->serialize( true );
[27] Fix | Delete
foreach ( $collection['data'] as $id => $variable ) {
[28] Fix | Delete
if ( ! ElementorUtils::has_pro() && Size_Variable_Prop_Type::get_key() === $variable['type'] ) {
[29] Fix | Delete
unset( $collection['data'][ $id ] );
[30] Fix | Delete
}
[31] Fix | Delete
}
[32] Fix | Delete
return $collection;
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* @throws BatchOperationFailed Thrown when one of the operations fails.
[37] Fix | Delete
* @throws FatalError Failed to save after batch.
[38] Fix | Delete
*/
[39] Fix | Delete
public function process_batch( array $operations ) {
[40] Fix | Delete
$collection = $this->repo->load();
[41] Fix | Delete
$results = [];
[42] Fix | Delete
$errors = [];
[43] Fix | Delete
[44] Fix | Delete
$error_formatter = new Batch_Error_Formatter();
[45] Fix | Delete
[46] Fix | Delete
foreach ( $operations as $index => $operation ) {
[47] Fix | Delete
try {
[48] Fix | Delete
$results[] = $this->batch_processor->apply_operation( $collection, $operation );
[49] Fix | Delete
[50] Fix | Delete
} catch ( \Exception $e ) {
[51] Fix | Delete
$errors[ $this->batch_processor->operation_id( $operation, $index ) ] = [
[52] Fix | Delete
'status' => $error_formatter->status_for( $e ),
[53] Fix | Delete
'code' => $error_formatter->error_code_for( $e ),
[54] Fix | Delete
'message' => $e->getMessage(),
[55] Fix | Delete
];
[56] Fix | Delete
}
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
if ( ! empty( $errors ) ) {
[60] Fix | Delete
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
[61] Fix | Delete
throw new BatchOperationFailed( 'Batch failed', $errors );
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
$watermark = $this->repo->save( $collection );
[65] Fix | Delete
[66] Fix | Delete
if ( false === $watermark ) {
[67] Fix | Delete
throw new FatalError( 'Failed to save batch operations' );
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
return [
[71] Fix | Delete
'success' => true,
[72] Fix | Delete
'results' => $results,
[73] Fix | Delete
'watermark' => $watermark,
[74] Fix | Delete
];
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
/**
[78] Fix | Delete
* @throws FatalError If variable create fails or validation errors occur.
[79] Fix | Delete
*/
[80] Fix | Delete
public function create( array $data ): array {
[81] Fix | Delete
$collection = $this->repo->load();
[82] Fix | Delete
[83] Fix | Delete
$collection->assert_limit_not_reached();
[84] Fix | Delete
$collection->assert_label_is_unique( $data['label'] );
[85] Fix | Delete
[86] Fix | Delete
$id = $collection->next_id();
[87] Fix | Delete
$data['id'] = $id;
[88] Fix | Delete
[89] Fix | Delete
// TODO: we need to look into this maybe we dont need order to be sent from client. Just implemented as it was
[90] Fix | Delete
if ( ! isset( $data['order'] ) ) {
[91] Fix | Delete
$data['order'] = $collection->get_next_order();
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
$variable = Variable::from_array( $data );
[95] Fix | Delete
[96] Fix | Delete
$collection->add_variable( $variable );
[97] Fix | Delete
[98] Fix | Delete
$watermark = $this->repo->save( $collection );
[99] Fix | Delete
[100] Fix | Delete
if ( false === $watermark ) {
[101] Fix | Delete
throw new FatalError( 'Failed to create variable' );
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
return [
[105] Fix | Delete
'variable' => array_merge( [ 'id' => $id ], $variable->to_array() ),
[106] Fix | Delete
'watermark' => $collection->watermark(),
[107] Fix | Delete
];
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
/**
[111] Fix | Delete
* @throws FatalError If variable update fails.
[112] Fix | Delete
*/
[113] Fix | Delete
public function update( string $id, array $data ): array {
[114] Fix | Delete
$collection = $this->repo->load();
[115] Fix | Delete
$variable = $collection->find_or_fail( $id );
[116] Fix | Delete
[117] Fix | Delete
if ( isset( $data['label'] ) ) {
[118] Fix | Delete
$collection->assert_label_is_unique( $data['label'], $id );
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
$variable->apply_changes( $data );
[122] Fix | Delete
[123] Fix | Delete
$watermark = $this->repo->save( $collection );
[124] Fix | Delete
[125] Fix | Delete
if ( false === $watermark ) {
[126] Fix | Delete
throw new FatalError( 'Failed to update variable' );
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
return [
[130] Fix | Delete
'variable' => array_merge( [ 'id' => $id ], $variable->to_array() ),
[131] Fix | Delete
'watermark' => $watermark,
[132] Fix | Delete
];
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
/**
[136] Fix | Delete
* @throws FatalError If variable delete fails.
[137] Fix | Delete
*/
[138] Fix | Delete
public function delete( string $id ) {
[139] Fix | Delete
$collection = $this->repo->load();
[140] Fix | Delete
$variable = $collection->find_or_fail( $id );
[141] Fix | Delete
[142] Fix | Delete
$variable->soft_delete();
[143] Fix | Delete
[144] Fix | Delete
$watermark = $this->repo->save( $collection );
[145] Fix | Delete
[146] Fix | Delete
if ( false === $watermark ) {
[147] Fix | Delete
throw new FatalError( 'Failed to delete variable' );
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
return [
[151] Fix | Delete
'watermark' => $watermark,
[152] Fix | Delete
'variable' => array_merge( [
[153] Fix | Delete
'id' => $id,
[154] Fix | Delete
'deleted' => true,
[155] Fix | Delete
], $variable->to_array() ),
[156] Fix | Delete
];
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
/**
[160] Fix | Delete
* @throws FatalError If variable restore fails.
[161] Fix | Delete
*/
[162] Fix | Delete
public function restore( string $id, $overrides = [] ) {
[163] Fix | Delete
$collection = $this->repo->load();
[164] Fix | Delete
$variable = $collection->find_or_fail( $id );
[165] Fix | Delete
[166] Fix | Delete
$collection->assert_limit_not_reached();
[167] Fix | Delete
[168] Fix | Delete
if ( isset( $overrides['label'] ) ) {
[169] Fix | Delete
$collection->assert_label_is_unique( $overrides['label'], $variable->id() );
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
$variable->apply_changes( $overrides );
[173] Fix | Delete
[174] Fix | Delete
$variable->restore();
[175] Fix | Delete
[176] Fix | Delete
$watermark = $this->repo->save( $collection );
[177] Fix | Delete
[178] Fix | Delete
if ( false === $watermark ) {
[179] Fix | Delete
throw new FatalError( 'Failed to delete variable' );
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
return [
[183] Fix | Delete
'variable' => array_merge( [ 'id' => $id ], $variable->to_array() ),
[184] Fix | Delete
'watermark' => $watermark,
[185] Fix | Delete
];
[186] Fix | Delete
}
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function