initial
This commit is contained in:
+31
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Data_Sync_Actions;
|
||||
|
||||
use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Data_Sync_Action;
|
||||
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Regenerate;
|
||||
|
||||
/**
|
||||
* Critical CSS Action: request regeneration.
|
||||
*/
|
||||
class Regenerate_CSS implements Data_Sync_Action {
|
||||
|
||||
/**
|
||||
* Handles the action logic.
|
||||
*
|
||||
* @param mixed $_data JSON Data passed to the action.
|
||||
* @param \WP_REST_Request $_request The request object.
|
||||
*/
|
||||
public function handle( $_data, $_request ) {
|
||||
$regenerate = new Regenerate();
|
||||
$regenerate->start();
|
||||
|
||||
$state = $regenerate->get_state();
|
||||
|
||||
return array(
|
||||
'success' => ! $state->has_errors(),
|
||||
'state' => $state->get(),
|
||||
'errors' => $state->get_error_message(),
|
||||
);
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Data_Sync_Actions;
|
||||
|
||||
use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Data_Sync_Action;
|
||||
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_State;
|
||||
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_Storage;
|
||||
|
||||
/**
|
||||
* Critical CSS Action: Set CSS for a provider.
|
||||
*/
|
||||
class Set_Provider_CSS implements Data_Sync_Action {
|
||||
|
||||
/**
|
||||
* Handles the action logic.
|
||||
*
|
||||
* @param mixed $data JSON Data passed to the action.
|
||||
* @param \WP_REST_Request $_request The request object.
|
||||
*/
|
||||
public function handle( $data, $_request ) {
|
||||
$state = new Critical_CSS_State();
|
||||
|
||||
if ( empty( $data['key'] ) || empty( $data['css'] ) ) {
|
||||
return array(
|
||||
'success' => false,
|
||||
'state' => $state->get(),
|
||||
'error' => 'Invalid data',
|
||||
);
|
||||
}
|
||||
|
||||
$provider_key = sanitize_key( $data['key'] );
|
||||
$css = $this->unmask_content( $data['css'] );
|
||||
|
||||
$storage = new Critical_CSS_Storage();
|
||||
$storage->store_css( $provider_key, $css );
|
||||
|
||||
$state->set_provider_success( $provider_key );
|
||||
$state->save();
|
||||
|
||||
return array(
|
||||
'success' => true,
|
||||
'state' => $state->get(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmasks the content.
|
||||
*
|
||||
* @param string $content The content to unmask.
|
||||
* @return string The unmasked content.
|
||||
*/
|
||||
private function unmask_content( $content ) {
|
||||
return str_replace( '__JB_XMLNS__', 'xmlns', $content );
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Data_Sync_Actions;
|
||||
|
||||
use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Data_Sync_Action;
|
||||
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_State;
|
||||
|
||||
/**
|
||||
* Critical CSS Action: Update whether or not to show a provider which is in an error state.
|
||||
*/
|
||||
class Set_Provider_Error_Dismissed implements Data_Sync_Action {
|
||||
/**
|
||||
* Handles the action logic.
|
||||
*
|
||||
* @param mixed $data JSON Data passed to the action.
|
||||
* @param \WP_REST_Request $_request The request object.
|
||||
*/
|
||||
public function handle( $data, $_request ) {
|
||||
$state = new Critical_CSS_State();
|
||||
|
||||
foreach ( $data as $item ) {
|
||||
if ( empty( $item['provider'] ) ) {
|
||||
return array(
|
||||
'success' => false,
|
||||
'state' => $state->get(),
|
||||
'error' => 'Invalid data',
|
||||
);
|
||||
}
|
||||
|
||||
$provider_key = sanitize_key( $item['provider'] );
|
||||
$dismissed = ! empty( $item['dismissed'] );
|
||||
|
||||
$state->set_provider_error_dismissed( $provider_key, $item['error_type'], $dismissed );
|
||||
}
|
||||
|
||||
$state->save();
|
||||
|
||||
return array(
|
||||
'success' => true,
|
||||
'state' => $state->get(),
|
||||
);
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Automattic\Jetpack_Boost\Lib\Critical_CSS\Data_Sync_Actions;
|
||||
|
||||
use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Data_Sync_Action;
|
||||
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_State;
|
||||
|
||||
/**
|
||||
* Critical CSS Action: Store errors for a provider.
|
||||
*/
|
||||
class Set_Provider_Errors implements Data_Sync_Action {
|
||||
/**
|
||||
* Handles the action logic.
|
||||
*
|
||||
* @param mixed $data JSON Data passed to the action.
|
||||
* @param \WP_REST_Request $_request The request object.
|
||||
*/
|
||||
public function handle( $data, $_request ) {
|
||||
$state = new Critical_CSS_State();
|
||||
|
||||
if ( empty( $data['key'] ) || empty( $data['errors'] ) ) {
|
||||
return array(
|
||||
'success' => false,
|
||||
'state' => $state->get(),
|
||||
'error' => 'Invalid data',
|
||||
);
|
||||
}
|
||||
|
||||
$provider_key = sanitize_key( $data['key'] );
|
||||
$errors = $data['errors'];
|
||||
|
||||
$state->set_provider_errors( $provider_key, $errors );
|
||||
$state->save();
|
||||
|
||||
return array(
|
||||
'success' => true,
|
||||
'state' => $state->get(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user