bj;
}
/**
* Save entity
*
* @return bool True on success, or false on error.
*/
public function save()
{
$saved = false;
if ($this->id < 0) {
$saved = ($this->insert() !== false);
} else {
$saved = $this->update();
}
return $saved;
}
/**
* Insert entity
*
* @return int|false The number of rows inserted, or false on error.
*/
protected function insert()
{
/** @var wpdb $wpdb */
global $wpdb;
if ($this->id > -1) {
throw new Exception('Entity already exists');
}
$this->updated = $this->created = gmdate("Y-m-d H:i:s");
$this->version = DUPLICATOR_PRO_VERSION;
$result = $wpdb->insert(
self::getTableName(),
[
'type' => $this->getType(),
'data' => '', // First I create a row without an object to generate the id, and then I update the row create
'version' => $this->version,
'created_at' => $this->created,
'updated_at' => $this->updated,
],
[
'%s',
'%s',
'%s',
'%s',
'%s',
]
);
if ($result === false) {
return false;
}
$this->id = $wpdb->insert_id;
if ($this->update() === false) {
$this->delete();
return false;
}
return $this->id;
}
/**
* Update entity
*
* @return bool True on success, or false on error.
*/
protected function update()
{
/** @var wpdb $wpdb */
global $wpdb;
if ($this->id < 0) {
throw new Exception('Entity don\'t exists in database');
}
$this->updated = gmdate("Y-m-d H:i:s");
$this->version = DUPLICATOR_PRO_VERSION;
return ($wpdb->update(
self::getTableName(),
[
'type' => $this->getType(),
'value_1' => $this->value1,
'value_2' => $this->value2,
'value_3' => $this->value3,
'value_4' => $this->value4,
'value_5' => $this->value5,
'data' => JsonSerialize::serialize($this, JsonSerialize::JSON_SKIP_CLASS_NAME | JSON_PRETTY_PRINT),
'version' => $this->version,
'created_at' => $this->created,
'updated_at' => $this->updated,
],
['id' => $this->id],
[
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
],
['%d']
) !== false);
}
/**
* Delete current entity
*
* @return bool True on success, or false on error.
*/
public function delete()
{
/** @var wpdb $wpdb */
global $wpdb;
if ($this->id < 0) {
return true;
}
if (
$wpdb->delete(
self::getTableName(),
['id' => $this->id],
['%d']
) === false
) {
return false;
}
$this->id = -1;
return true;
}
/**
* Entity table name
*
* @param bool $escape If true apply esc_sql to table name
*
* @return string
*/
public static function getTableName($escape = false)
{
/** @var wpdb $wpdb */
global $wpdb;
$res = $wpdb->base_prefix . 'duplicator_entities';
return ($escape ? esc_sql($res) : $res);
}
/**
* Get entities of current type
*
* @param int<0, max> $page current page, if $pageSize is 0 o 1 $pase is the offset
* @param int<0, max> $pageSize page size, 0 return all entities
* @param ?callable $sortCallback sort function on items result
* @param ?callable $filterCallback filter on items result
* @param array{'col': string, 'mode': string} $orderby query ordder by
*
* @return static[]|false return entities list of false on failure
*/
protected static function getItemsFromDatabase(
$page = 0,
$pageSize = 0,
$sortCallback = null,
$filterCallback = null,
$orderby = [
'col' => 'id',
'mode' => 'ASC',
]
) {
try {
/** @var wpdb $wpdb */
global $wpdb;
$offset = $page * max(1, $pageSize);
$pageSize = ($pageSize ? $pageSize : PHP_INT_MAX);
$orderCol = isset($orderby['col']) ? $orderby['col'] : 'id';
$order = isset($orderby['mode']) ? $orderby['mode'] : 'ASC';
$query = $wpdb->prepare(
"SELECT * FROM `" . self::getTableName(true) . "` WHERE type = %s ORDER BY {$orderCol} {$order} LIMIT %d OFFSET %d",
static::getType(),
$pageSize,
$offset
);
if (($rows = $wpdb->get_results($query, ARRAY_A)) === null) {
throw new Exception('Get item query fail');
}
$instances = [];
foreach ($rows as $row) {
$instances[] = static::getEntityFromJson($row['data'], $row);
}
if (is_callable($filterCallback)) {
$instances = array_filter($instances, $filterCallback);
}
if (is_callable($sortCallback)) {
usort($instances, $sortCallback);
} else {
$instances = array_values($instances);
}
} catch (Exception $e) {
DUP_PRO_Log::traceError(SnapLog::getTextException($e));
return false;
} catch (Error $e) {
DUP_PRO_Log::traceError(SnapLog::getTextException($e));
return false;
}
return $instances;
}
/**
* Get ids of current type
*
* @param int<0, max> $page current page, if $pageSize is 0 o 1 $pase is the offset
* @param int<0, max> $pageSize page size, 0 return all entities
* @param ?callable $sortCallback sort function on items result
* @param ?callable $filterCallback filter on items result
* @param array{'col': string, 'mode': string} $orderby query ordder by
*
* @return int[]|false return entities list of false on failure
*/
protected static function getIdsFromDatabase(
$page = 0,
$pageSize = 0,
$sortCallback = null,
$filterCallback = null,
$orderby = [
'col' => 'id',
'mode' => 'ASC',
]
) {
try {
/** @var wpdb $wpdb */
global $wpdb;
$offset = $page * max(1, $pageSize);
$pageSize = ($pageSize ? $pageSize : PHP_INT_MAX);
$orderCol = isset($orderby['col']) ? $orderby['col'] : 'id';
$order = isset($orderby['mode']) ? $orderby['mode'] : 'ASC';
$query = $wpdb->prepare(
"SELECT id FROM `" . self::getTableName(true) . "` WHERE type = %s ORDER BY {$orderCol} {$order} LIMIT %d OFFSET %d",
static::getType(),
$pageSize,
$offset
);
if (($rows = $wpdb->get_results($query, ARRAY_A)) === null) {
throw new Exception('Get item query fail');
}
$ids = array();
foreach ($rows as $row) {
$ids[] = (int) $row['id'];
}
if (is_callable($filterCallback)) {
$ids = array_filter($ids, $filterCallback);
}
if (is_callable($sortCallback)) {
usort($ids, $sortCallback);
} else {
$ids = array_values($ids);
}
} catch (Exception $e) {
DUP_PRO_Log::traceError(SnapLog::getTextException($e));
return false;
} catch (Error $e) {
DUP_PRO_Log::traceError(SnapLog::getTextException($e));
return false;
}
return $ids;
}
/**
* Count entity items
*
* @return int|false
*/
protected static function countItemsFromDatabase()
{
try {
/** @var wpdb $wpdb */
global $wpdb;
$query = $wpdb->prepare(
"SELECT COUNT(*) FROM `" . self::getTableName(true) . "` WHERE type = %s",
static::getType()
);
if (($count = $wpdb->get_var($query)) === null) {
throw new Exception('Get item query fail');
}
} catch (Exception $e) {
DUP_PRO_Log::traceError(SnapLog::getTextException($e));
return false;
} catch (Error $e) {
DUP_PRO_Log::traceError(SnapLog::getTextException($e));
return false;
}
return (int) $count;
}
/**
* Init entity table
*
* @return string[] Strings containing the results of the various update queries.
*/
final public static function initTable()
{
/** @var wpdb $wpdb */
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = static::getTableName(true);
// PRIMARY KEY must have 2 spaces before for dbDelta to work
// Mysql 5.5 can't have more than 1 DEFAULT CURRENT_TIMESTAMP
$sql = <<