Skip to content

Commit

Permalink
Merge feature/lrc into feature/lrc-functionality-script
Browse files Browse the repository at this point in the history
  • Loading branch information
jeawhanlee committed Aug 21, 2024
2 parents 05868b0 + 9099123 commit 31c145e
Show file tree
Hide file tree
Showing 10 changed files with 735 additions and 36 deletions.
31 changes: 31 additions & 0 deletions inc/Engine/Common/PerformanceHints/AJAX/AJAXControllerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Common\PerformanceHints\AJAX;

trait AJAXControllerTrait {
/**
* Get status code and message to be saved into the database
*
* @param string $status Current status code from $_POST.
* @return array
*/
protected function get_status_code_message( string $status ): array {
$status_code = 'success' !== $status ? 'failed' : 'completed';
$status_message = '';

switch ( $status ) {
case 'script_error':
$status_message = esc_html__( 'Script error', 'rocket' );
break;
case 'timeout':
$status_message = esc_html__( 'Script timeout', 'rocket' );
break;
}

return [
$status_code,
$status_message,
];
}
}
27 changes: 2 additions & 25 deletions inc/Engine/Media/AboveTheFold/AJAX/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace WP_Rocket\Engine\Media\AboveTheFold\AJAX;

use WP_Rocket\Engine\Common\PerformanceHints\AJAX\AJAXControllerTrait;
use WP_Rocket\Engine\Media\AboveTheFold\Database\Queries\AboveTheFold as ATFQuery;
use WP_Rocket\Engine\Common\Context\ContextInterface;
use WP_Rocket\Engine\Optimization\UrlTrait;
Expand All @@ -11,6 +12,7 @@

class Controller implements ControllerInterface {
use UrlTrait;
use AJAXControllerTrait;

/**
* ATFQuery instance
Expand Down Expand Up @@ -141,31 +143,6 @@ public function add_data(): array {
return $payload;
}

/**
* Get status code and message to be saved into the database
*
* @param string $status Current status code from $_POST.
* @return array
*/
private function get_status_code_message( $status ) {
$status_code = 'success' !== $status ? 'failed' : 'completed';
$status_message = '';

switch ( $status ) {
case 'script_error':
$status_message = esc_html__( 'Script error', 'rocket' );
break;
case 'timeout':
$status_message = esc_html__( 'Script timeout', 'rocket' );
break;
}

return [
$status_code,
$status_message,
];
}

/**
* Creates an object with the 'type' property and the first key that exists in the image object.
*
Expand Down
140 changes: 140 additions & 0 deletions inc/Engine/Optimization/LazyRenderContent/AJAX/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Optimization\LazyRenderContent\AJAX;

use WP_Rocket\Engine\Common\Context\ContextInterface;
use WP_Rocket\Engine\Common\PerformanceHints\AJAX\AJAXControllerTrait;
use WP_Rocket\Engine\Optimization\UrlTrait;
use WP_Rocket\Engine\Common\PerformanceHints\AJAX\ControllerInterface;
use WP_Rocket\Engine\Optimization\LazyRenderContent\Database\Queries\LazyRenderContent as LRCQuery;

class Controller implements ControllerInterface {
use UrlTrait;
use AJAXControllerTrait;

/**
* LRCQuery instance
*
* @var LRCQuery
*/
private $query;

/**
* LRC Context.
*
* @var ContextInterface
*/
protected $context;

/**
* Constructor
*
* @param LRCQuery $query LRCQuery instance.
* @param ContextInterface $context Context interface.
*/
public function __construct( LRCQuery $query, ContextInterface $context ) {
$this->query = $query;
$this->context = $context;
}

/**
* Add LRC data to the database
*
* @return array
*/
public function add_data(): array {
$payload = [];
check_ajax_referer( 'rocket_beacon', 'rocket_beacon_nonce' );

if ( ! $this->context->is_allowed() ) {
$payload['lrc'] = 'not allowed';

return $payload;
}

$url = isset( $_POST['url'] ) ? untrailingslashit( esc_url_raw( wp_unslash( $_POST['url'] ) ) ) : '';
$is_mobile = isset( $_POST['is_mobile'] ) ? filter_var( wp_unslash( $_POST['is_mobile'] ), FILTER_VALIDATE_BOOLEAN ) : false;
$results = isset( $_POST['results'] ) ? json_decode( wp_unslash( $_POST['results'] ) ) : (object) [ 'lrc' => [] ]; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$hashes = $results->lrc ?? [];
$below_the_fold = [];

/**
* Filters the maximum number of LRC hashes being saved into the database.
*
* @param int $max_number Maximum number to allow.
* @param string $url Current page url.
* @param string[]|array $hashes Current list of LRC hashes.
*/
$max_lrc_hashes_number = wpm_apply_filters_typed( 'integer', 'rocket_lrc_hashes_number', 20, $url, $hashes );
if ( 0 >= $max_lrc_hashes_number ) {
$max_lrc_hashes_number = 1;
}

foreach ( (array) $hashes as $hash ) {
$below_the_fold[] = sanitize_text_field( wp_unslash( $hash ) );
--$max_lrc_hashes_number;
}

$row = $this->query->get_row( $url, $is_mobile );
if ( ! empty( $row ) ) {
$payload['lrc'] = 'item already in the database';

return $payload;
}

$status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : '';
list( $status_code, $status_message ) = $this->get_status_code_message( $status );

$item = [
'url' => $url,
'is_mobile' => $is_mobile,
'status' => $status_code,
'error_message' => $status_message,
'below_the_fold' => ( is_array( $below_the_fold ) || is_object( $below_the_fold ) ) ? wp_json_encode( $below_the_fold ) : $below_the_fold,
'last_accessed' => current_time( 'mysql', true ),
'created_at' => current_time( 'mysql', true ),
];

$result = $this->query->add_item( $item );
$payload['lrc'] = $item;

if ( ! $result ) {
$payload['lrc'] = 'error when adding the entry to the database';
}

return $payload;
}

/**
* Checks if there is existing data for the current URL and device type from the beacon script.
*
* This method is called via AJAX. It checks if there is existing LRC data for the current URL and device type.
* If the data exists, it returns a JSON success response with true. If the data does not exist, it returns a JSON success response with false.
* If the context is not allowed, it returns a JSON error response with false.
*
* @return array
*/
public function check_data(): array {
$payload = [
'lrc' => false,
];
check_ajax_referer( 'rocket_beacon', 'rocket_beacon_nonce' );

if ( ! $this->context->is_allowed() ) {
$payload['lrc'] = true;
return $payload;
}

$url = isset( $_POST['url'] ) ? untrailingslashit( esc_url_raw( wp_unslash( $_POST['url'] ) ) ) : '';
$is_mobile = isset( $_POST['is_mobile'] ) ? filter_var( wp_unslash( $_POST['is_mobile'] ), FILTER_VALIDATE_BOOLEAN ) : false;

$row = $this->query->get_row( $url, $is_mobile );

if ( ! empty( $row ) ) {
$payload['lrc'] = true;
}

return $payload;
}
}
16 changes: 9 additions & 7 deletions inc/Engine/Optimization/LazyRenderContent/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ class Factory implements FactoryInterface {
/**
* Instantiate the class.
*
* @param ContextInterface $context LRC Context instance.
* @param TableInterface $table LRC Table instance.
* @param QueriesInterface $queries LRC Queries instance.
* @param ContextInterface $context LRC Context instance.
* @param TableInterface $table LRC Table instance.
* @param QueriesInterface $queries LRC Queries instance.
* @param AjaxControllerInterface $ajax_controller LRC AJAX Controller instance.
*/
public function __construct( ContextInterface $context, TableInterface $table, QueriesInterface $queries ) {
$this->context = $context;
$this->table = $table;
$this->queries = $queries;
public function __construct( ContextInterface $context, TableInterface $table, QueriesInterface $queries, AjaxControllerInterface $ajax_controller ) {
$this->context = $context;
$this->table = $table;
$this->queries = $queries;
$this->ajax_controller = $ajax_controller;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function add_hashes( $html ) {
$url = untrailingslashit( home_url( add_query_arg( [], $wp->request ) ) );
$is_mobile = $this->is_mobile();

if ( ! $this->query->get_row( $url, $is_mobile ) ) {
if ( $this->query->get_row( $url, $is_mobile ) ) {
return $html;
}

Expand Down
21 changes: 18 additions & 3 deletions inc/Engine/Optimization/LazyRenderContent/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use WP_Rocket\Engine\Optimization\LazyRenderContent\Context\Context;
use WP_Rocket\Engine\Optimization\LazyRenderContent\Database\Table\LazyRenderContent as LRCTable;
use WP_Rocket\Engine\Optimization\LazyRenderContent\Database\Queries\LazyRenderContent as LRCQuery;
use WP_Rocket\Engine\Optimization\LazyRenderContent\AJAX\Controller as AJAXController;
use WP_Rocket\Engine\Optimization\LazyRenderContent\Frontend\{Controller as FrontController, Subscriber as FrontSubscriber};
use WP_Rocket\Engine\Optimization\LazyRenderContent\Frontend\Processor\Processor;

Expand All @@ -25,6 +26,7 @@ class ServiceProvider extends AbstractServiceProvider {
'lrc_factory',
'lrc_table',
'lrc_query',
'lrc_ajax_controller',
'lrc_frontend_processor',
'lrc_frontend_controller',
'lrc_frontend_subscriber',
Expand Down Expand Up @@ -53,12 +55,15 @@ public function register(): void {

$this->getContainer()->add( 'lrc_query', LRCQuery::class );

$this->getContainer()->addShared( 'lrc_factory', Factory::class )
$this->getContainer()->addShared( 'lrc_table', LRCTable::class );

$this->getContainer()->add( 'lrc_query', LRCQuery::class );

$this->getContainer()->add( 'lrc_ajax_controller', AJAXController::class )
->addArguments(
[
$this->getContainer()->get( 'lrc_context' ),
$this->getContainer()->get( 'lrc_table' ),
$this->getContainer()->get( 'lrc_query' ),
$this->getContainer()->get( 'lrc_context' ),
]
);
$this->getContainer()->add( 'lrc_frontend_processor', Processor::class );
Expand All @@ -77,5 +82,15 @@ public function register(): void {
$this->getContainer()->get( 'lrc_frontend_controller' ),
]
);

$this->getContainer()->addShared( 'lrc_factory', Factory::class )
->addArguments(
[
$this->getContainer()->get( 'lrc_context' ),
$this->getContainer()->get( 'lrc_table' ),
$this->getContainer()->get( 'lrc_query' ),
$this->getContainer()->get( 'lrc_ajax_controller' ),
]
);
}
}
Loading

0 comments on commit 31c145e

Please sign in to comment.