| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP\StaticSite\Engine\Incremental; |
| 4 |
|
| 5 |
/** |
| 6 |
* Change rows in the table {prefix}flywp_static_changes. |
| 7 |
* |
| 8 |
* @since 1.8.0 |
| 9 |
*/ |
| 10 |
class WpChangeStore implements ChangeStore { |
| 11 |
|
| 12 |
/** |
| 13 |
* Table name without prefix. |
| 14 |
*/ |
| 15 |
const TABLE = 'flywp_static_changes'; |
| 16 |
|
| 17 |
/** |
| 18 |
* @return string Table name with prefix. |
| 19 |
*/ |
| 20 |
public static function table() { |
| 21 |
global $wpdb; |
| 22 |
|
| 23 |
return $wpdb->prefix . self::TABLE; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Create or update the table. |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public static function install() { |
| 32 |
global $wpdb; |
| 33 |
|
| 34 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 35 |
|
| 36 |
$table = self::table(); |
| 37 |
$charset = $wpdb->get_charset_collate(); |
| 38 |
|
| 39 |
dbDelta( |
| 40 |
"CREATE TABLE {$table} ( |
| 41 |
id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 42 |
kind varchar(10) NOT NULL, |
| 43 |
object_type varchar(20) NULL, |
| 44 |
object_id bigint(20) unsigned NULL, |
| 45 |
url text NULL, |
| 46 |
reason varchar(100) NOT NULL, |
| 47 |
created_at datetime NOT NULL, |
| 48 |
PRIMARY KEY (id), |
| 49 |
KEY kind_id (kind,id) |
| 50 |
) {$charset};" |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* {@inheritdoc} |
| 56 |
*/ |
| 57 |
public function insert( $kind, $object_type, $object_id, $url, $reason ) { |
| 58 |
global $wpdb; |
| 59 |
|
| 60 |
$result = $wpdb->insert( |
| 61 |
self::table(), |
| 62 |
[ |
| 63 |
'kind' => $kind, |
| 64 |
'object_type' => $object_type, |
| 65 |
'object_id' => $object_id, |
| 66 |
'url' => $url, |
| 67 |
'reason' => $reason, |
| 68 |
'created_at' => gmdate( 'Y-m-d H:i:s' ), |
| 69 |
] |
| 70 |
); |
| 71 |
|
| 72 |
return false !== $result; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* {@inheritdoc} |
| 77 |
*/ |
| 78 |
public function rows( $after_id, $up_to_id, $limit ) { |
| 79 |
global $wpdb; |
| 80 |
|
| 81 |
$table = self::table(); |
| 82 |
$rows = $wpdb->get_results( |
| 83 |
$wpdb->prepare( |
| 84 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $table is the prefix and a constant. |
| 85 |
"SELECT id, kind, object_type, object_id, url, reason FROM {$table} WHERE id > %d AND id <= %d ORDER BY id ASC LIMIT %d", |
| 86 |
(int) $after_id, |
| 87 |
(int) $up_to_id, |
| 88 |
(int) $limit |
| 89 |
), |
| 90 |
ARRAY_A |
| 91 |
); |
| 92 |
|
| 93 |
return array_map( |
| 94 |
function ( $row ) { |
| 95 |
$row['id'] = (int) $row['id']; |
| 96 |
$row['object_id'] = null === $row['object_id'] ? null : (int) $row['object_id']; |
| 97 |
|
| 98 |
return $row; |
| 99 |
}, |
| 100 |
is_array( $rows ) ? $rows : [] |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* {@inheritdoc} |
| 106 |
*/ |
| 107 |
public function has_global( $after_id, $up_to_id ) { |
| 108 |
global $wpdb; |
| 109 |
|
| 110 |
$table = self::table(); |
| 111 |
|
| 112 |
return (bool) $wpdb->get_var( |
| 113 |
$wpdb->prepare( |
| 114 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $table is the prefix and a constant. |
| 115 |
"SELECT id FROM {$table} WHERE kind = 'global' AND id > %d AND id <= %d LIMIT 1", |
| 116 |
(int) $after_id, |
| 117 |
(int) $up_to_id |
| 118 |
) |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* {@inheritdoc} |
| 124 |
*/ |
| 125 |
public function max_id() { |
| 126 |
global $wpdb; |
| 127 |
|
| 128 |
$table = self::table(); |
| 129 |
|
| 130 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $table is the prefix and a constant. |
| 131 |
return (int) $wpdb->get_var( "SELECT MAX(id) FROM {$table}" ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* {@inheritdoc} |
| 136 |
*/ |
| 137 |
public function clear_up_to( $change_id ) { |
| 138 |
global $wpdb; |
| 139 |
|
| 140 |
$table = self::table(); |
| 141 |
|
| 142 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $table is the prefix and a constant. |
| 143 |
return false !== $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE id <= %d", (int) $change_id ) ); |
| 144 |
} |
| 145 |
} |
| 146 |
|