Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Database classes for Above the fold optimization #6208

Merged
merged 8 commits into from
Oct 16, 2023
Merged
74 changes: 74 additions & 0 deletions inc/Engine/Media/AboveTheFold/Database/Queries/AboveTheFold.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\AboveTheFold\Database\Queries;

use WP_Rocket\Dependencies\Database\Query;

class AboveTheFold extends Query {

/**
* Name of the database table to query.
*
* @var string
*/
protected $table_name = 'wpr_above_the_fold';

/**
* String used to alias the database table in MySQL statement.
*
* Keep this short, but descriptive. I.E. "tr" for term relationships.
*
* This is used to avoid collisions with JOINs.
*
* @var string
*/
protected $table_alias = 'wpr_atf';

/**
* Name of class used to setup the database schema.
*
* @var string
*/
protected $table_schema = '\\WP_Rocket\\Engine\\Media\\AboveTheFold\\Database\\Schemas\\UsedCSS';

/** Item ******************************************************************/

/**
* Name for a single item.
*
* Use underscores between words. I.E. "term_relationship"
*
* This is used to automatically generate action hooks.
*
* @var string
*/
protected $item_name = 'above_the_fold';

/**
* Plural version for a group of items.
*
* Use underscores between words. I.E. "term_relationships"
*
* This is used to automatically generate action hooks.
*
* @var string
*/
protected $item_name_plural = 'above_the_fold';

/**
* Name of class used to turn IDs into first-class objects.
*
* This is used when looping through return values to guarantee their shape.
*
* @var mixed
*/
protected $item_shape = '\\WP_Rocket\\Engine\\Media\\AboveTheFold\\Database\\Row\\UsedCSS';

/**
* Table status.
*
* @var boolean
*/
public static $table_exists = false;
}
131 changes: 131 additions & 0 deletions inc/Engine/Media/AboveTheFold/Database/Rows/AboveTheFold.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\AboveTheFold\Database\Row;

use WP_Rocket\Dependencies\Database\Row;

class AboveTheFold extends Row {
/**
* Row ID
*
* @var int
*/
public $id;

/**
* URL
*
* @var string
*/
public $url;

/**
* LCP
*
* @var string
*/
public $lcp;

/**
* Viewport
*
* @var string
*/
public $viewport;

/**
* Is CSS for mobile
*
* @var bool
*/
public $is_mobile;

/**
* Error code
*
* @var string
*/
public $error_code;

/**
* Error message
*
* @var string
*/
public $error_message;

/**
* Number of retries
*
* @var int
*/
public $retries;

/**
* Job ID
*
* @var string
*/
public $job_id;

/**
* Job queue name
*
* @var string
*/
public $queue_name;

/**
* Status
*
* @var string
*/
public $status;

/**
* Submitted time
*
* @var int
*/
public $submitted_at;

/**
* Last modified time
*
* @var int
*/
public $modified;

/**
* Last accessed time
*
* @var int
*/
public $last_accessed;

/**
* Constructor.
*
* @param mixed $item Object Row.
*/
public function __construct( $item ) {
parent::__construct( $item );

// Set the type of each column, and prepare.
$this->id = (int) $this->id;
$this->url = (string) $this->url;
$this->lcp = (string) $this->lcp;
$this->viewport = (string) $this->viewport;
$this->error_code = (string) $this->error_code;
$this->error_message = (string) $this->error_message;
$this->retries = (int) $this->retries;
$this->is_mobile = (bool) $this->is_mobile;
$this->job_id = (string) $this->job_id;
$this->queue_name = (string) $this->queue_name;
$this->status = (string) $this->status;
$this->submitted_at = empty( $this->submitted_at ) ? 0 : strtotime( $this->submitted_at );
$this->modified = empty( $this->modified ) ? 0 : strtotime( $this->modified );
$this->last_accessed = empty( $this->last_accessed ) ? 0 : strtotime( $this->last_accessed );
}
}
165 changes: 165 additions & 0 deletions inc/Engine/Media/AboveTheFold/Database/Schemas/AboveTheFold.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\AboveTheFold\Database\Schemas;

use WP_Rocket\Dependencies\Database\Schema;

class AboveTheFold extends Schema {

/**
* Array of database column objects
*
* @var array
*/
public $columns = [

// ID column.
[
'name' => 'id',
'type' => 'bigint',
'length' => '20',
'unsigned' => true,
'extra' => 'auto_increment',
'primary' => true,
'sortable' => true,
],

// URL column.
[
'name' => 'url',
'type' => 'varchar',
'length' => '2000',
'default' => '',
'cache_key' => true,
'searchable' => true,
'sortable' => true,
],

// LCP column.
[
'name' => 'lcp',
'type' => 'longtext',
'default' => '',
'cache_key' => false,
'searchable' => true,
'sortable' => true,
],

// Viewport column.
[
'name' => 'viewport',
'type' => 'longtext',
'default' => '',
'cache_key' => false,
'searchable' => true,
'sortable' => true,
],

// IS_MOBILE column.
[
'name' => 'is_mobile',
'type' => 'tinyint',
'length' => '1',
'default' => 0,
'cache_key' => true,
'searchable' => true,
'sortable' => true,
],

// error_code column.
[
'name' => 'error_code',
'type' => 'varchar',
'length' => '32',
'default' => null,
'cache_key' => false,
'searchable' => true,
'sortable' => true,
],

// error_message column.
[
'name' => 'error_message',
'type' => 'longtext',
'default' => null,
'cache_key' => false,
'searchable' => true,
'sortable' => true,
],

// RETRIES column.
[
'name' => 'retries',
'type' => 'tinyint',
'length' => '1',
'default' => 1,
'cache_key' => false,
'searchable' => true,
'sortable' => true,
],

// JOB_ID column.
[
'name' => 'job_id',
'type' => 'varchar',
'length' => '255',
'default' => null,
'cache_key' => true,
'searchable' => false,
'sortable' => false,
],

// QUEUE_NAME column.
[
'name' => 'queue_name',
'type' => 'varchar',
'length' => '255',
'default' => null,
'cache_key' => true,
'searchable' => false,
'sortable' => false,
],

// STATUS column.
[
'name' => 'status',
'type' => 'varchar',
'length' => '255',
'default' => null,
'cache_key' => true,
'searchable' => true,
'sortable' => false,
],

// Submitted_at column.
[
'name' => 'submitted_at',
'type' => 'timestamp',
'default' => '0000-00-00 00:00:00',
'created' => true,
'date_query' => true,
'sortable' => true,
],

// MODIFIED column.
[
'name' => 'modified',
'type' => 'timestamp',
'default' => '0000-00-00 00:00:00',
'created' => true,
'date_query' => true,
'sortable' => true,
],

// LAST_ACCESSED column.
[
'name' => 'last_accessed',
'type' => 'timestamp',
'default' => '0000-00-00 00:00:00',
'created' => true,
'date_query' => true,
'sortable' => true,
],
];
}
Loading
Loading