Skip to content

Commit

Permalink
:chore: Add database structure :closes: #7242
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Feb 4, 2025
1 parent 5b018e4 commit f8d0c78
Show file tree
Hide file tree
Showing 5 changed files with 424 additions and 0 deletions.
101 changes: 101 additions & 0 deletions inc/Engine/Media/PreloadFonts/Database/Queries/PreloadFonts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

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

use WP_Rocket\Engine\Common\PerformanceHints\Database\Queries\AbstractQueries;
use WP_Rocket\Engine\Common\PerformanceHints\Database\Queries\QueriesInterface;
use WP_Rocket\Engine\Media\PreloadFonts\Database\Schema\PreloadFonts as PreloadFontsSchema;
use WP_Rocket\Engine\Media\PreloadFonts\Database\Rows\PreloadFonts as PreloadFontsRows;
class PreloadFonts extends AbstractQueries implements QueriesInterface {

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

/**
* 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_plf';

/**
* Name of class used to setup the database schema.
*
* @var string
*/
protected $table_schema = PreloadFontsSchema::class;

/**
* 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 = 'preload_fonts';

/**
* 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 = 'preload_fonts';

/**
* 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 = PreloadFontsRows::class;


/**
* Deletes old rows from the database.
*
* This method is used to delete rows from the database that have not been accessed in the last month.
*
* @return bool|int Returns a boolean or integer value. The exact return value depends on the implementation.
*/
public function delete_old_rows() {
// Get the database interface.
$db = $this->get_db();

// Early bailout if no database interface is available.
if ( ! $db ) {
return false;
}

/**
* Filters the interval (in months) to determine when a Preload Fonts(PLF) entry is considered 'old'.
* Old PLF entries are eligible for deletion. By default, a PLF entry is considered old if it hasn't been accessed in the last month.
*
* @param int $delete_interval The interval in months after which an PLF entry is considered old. Default is 1 month.
*/
$delete_interval = wpm_apply_filters_typed( 'integer', 'rocket_plf_cleanup_interval', 1 );

if ( $delete_interval <= 0 ) {
return false;
}

$prefixed_table_name = $db->prefix . $this->table_name;
$query = "DELETE FROM `$prefixed_table_name` WHERE status = 'failed' OR `last_accessed` <= date_sub(now(), interval $delete_interval month)";

return $db->query( $query );
}
}
111 changes: 111 additions & 0 deletions inc/Engine/Media/PreloadFonts/Database/Rows/PreloadFonts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace WP_Rocket\Engine\Media\PreloadFonts\Database\Rows;

use WP_Rocket\Dependencies\BerlinDB\Database\Row;

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

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

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

/**
* Fonts
*
* @var string
*/
public $fonts;

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

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

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

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

/**
* Created time
*
* @var int
*/
public $created_at;

/**
* 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->is_mobile = (bool) $this->is_mobile;
$this->fonts = (string) $this->fonts;
$this->status = (string) $this->status;
$this->error_message = (string) $this->error_message;
$this->modified = empty( $this->modified ) ? 0 : strtotime( (string) $this->modified );
$this->last_accessed = empty( $this->last_accessed ) ? 0 : strtotime( (string) $this->last_accessed );
$this->created_at = empty( $this->created_at ) ? 0 : strtotime( (string) $this->created_at );
}

/**
* Checks if the object has a valid Preload Fonts value.
*
* @return bool Returns true if the object's status is 'completed' and the fonts value is not empty or '[]', false otherwise.
*/
public function has_preload_fonts() {
if ( 'completed' !== $this->status ) {
return false;
}

if ( empty( $this->fonts ) ) {
return false;
}

if ( '[]' === $this->fonts ) {
return false;
}

return true;
}
}
110 changes: 110 additions & 0 deletions inc/Engine/Media/PreloadFonts/Database/Schema/PreloadFonts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\PreloadFonts\Database\Schema;

use WP_Rocket\Dependencies\BerlinDB\Database\Schema;

class PreloadFonts 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,
],

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

// Below the fold column.
[
'name' => 'fonts',
'type' => 'longtext',
'default' => '',
'cache_key' => false,
'searchable' => true,
'sortable' => true,
],

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

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

// 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,
],

// CREATED_AT column.
[
'name' => 'created_at',
'type' => 'timestamp',
'default' => null,
'created' => true,
'date_query' => true,
'sortable' => true,
],
];
}
57 changes: 57 additions & 0 deletions inc/Engine/Media/PreloadFonts/Database/Table/PreloadFonts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\PreloadFonts\Database\Table;

use WP_Rocket\Engine\Common\PerformanceHints\Database\Table\AbstractTable;

class PreloadFonts extends AbstractTable {
/**
* Table name
*
* @var string
*/
protected $name = 'wpr_preload_fonts';

/**
* Database version key (saved in _options or _sitemeta)
*
* @var string
*/
protected $db_version_key = 'wpr_preload_fonts_version';

/**
* Database version
*
* @var int
*/
protected $version = 20250204;

/**
* Key => value array of versions => methods.
*
* @var array
*/
protected $upgrades = [];

/**
* Table schema data.
*
* @var string
*/
protected $schema_data = "
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
url varchar(2000) NOT NULL default '',
is_mobile tinyint(1) NOT NULL default 0,
fonts longtext default '',
error_message longtext default '',
status varchar(255) NOT NULL default '',
modified timestamp NOT NULL default '0000-00-00 00:00:00',
last_accessed timestamp NOT NULL default '0000-00-00 00:00:00',
created_at timestamp NULL,
PRIMARY KEY (id),
KEY url (url(150), is_mobile),
KEY modified (modified),
KEY last_accessed (last_accessed),
INDEX `status_index` (`status`(191))";
}
Loading

0 comments on commit f8d0c78

Please sign in to comment.