| Server IP : 89.39.208.18 / Your IP : 216.73.217.74 Web Server : LiteSpeed System : User : ( ) PHP Version : 7.4.33 Disable Function : eval,./,ln,passthru,dl,system,exec,popen,shell_exec,posix_getpwuid,posix_uname,ftp_exec,posix_uname,posix_getpwuid,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,get_current_user,getmyuid,getmygid,listen,chgrp,apache_note,apache_setenv,apache_child_terminate,closelog,debugger_off,debugge_on,ini_restore,netscript,escapeshellarg,cmd,backtick,virtual,show_sourc,show_source,pclose,pcntl_exec,datasec,old_offset,ctrl_dir,ini_alter,leak,listen,chgrp,apache_setenv,define_syslog_variables,root,diskfreespace,disk_free_space,disk_total_space,posix_kill,getmyuid,getmygid,apache_child_terminate,php_ini_scanned_files,ls,ps_aux,chown,getrusage,posixc,posame,chgrp,posix_setuid,posix_setsid,posix_setgid,apache_note,apache_setenv,x_getuid,e_ini_file,nfo,SQL,mysql_li,t_dbs,filegroup,highlight_file,ilegetcontents,get_dir,popen,popens,pfsockopen,dos_conv,apache_get_modules,crack_check,crack_closedict,rar_open,bzopen,bzread,bzwrite,shellcode,posix_isatty,posix_getservbyname,escapeshellarg,hypot,pg_host,pos,posix_access,inurl,posix_times,posix_mknod,pclose,ps_fill,symlink,id,escapeshellcmd,php_uname,phpinfo MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /home3/mahyarcake/www/wp-content/plugins/duplicator-pro/src/Core/Models/ |
Upload File : |
<?php
/**
* @package Duplicator
* @copyright (c) 2022, Snap Creek LLC
*/
namespace Duplicator\Core\Models;
use Error;
use Exception;
use ReflectionClass;
use wpdb;
/**
* Singletion entity class
*/
abstract class AbstractEntitySingleton extends AbstractEntity
{
/** @var static[] */
private static $instances = array();
/**
* Get instance
*
* @return static
*/
public static function getInstance()
{
$class = get_called_class();
if (!isset(self::$instances[$class])) {
$items = static::getItemsFromDatabase();
if (empty($items)) {
self::$instances[$class] = new static();
try {
self::$instances[$class]->firstIstanceInit();
// I save the instance before initializing the values in case they require ajax calls
// that would otherwise re-initialize the singletom object
self::deleteExcessRows(self::$instances[$class]->getId()); // Make sure to delete all duplicate rows
self::$instances[$class]->save();
} catch (Exception $e) {
// Prevent save error on cron events edge cases
self::$instances[$class] = new static();
} catch (Error $e) {
// Prevent save error on cron events edge cases
self::$instances[$class] = new static();
}
} else {
if (count($items) > 1) {
self::deleteExcessRows($items[0]->getId());
}
self::$instances[$class] = $items[0];
}
}
return self::$instances[$class];
}
/**
* Class constructor
*/
abstract protected function __construct();
/**
* This function is called on first istance of singletion object
* Can be extended and used to set dynamic properties values
*
* @return void
*/
protected function firstIstanceInit()
{
}
/**
* Delete all row except id is set
*
* @param int $id Exclude id, if < 0 delete all rows
*
* @return bool True on success, or false on error.
*/
protected static function deleteExcessRows($id)
{
try {
/** @var wpdb $wpdb */
global $wpdb;
$query = $wpdb->prepare(
"DELETE FROM `" . self::getTableName(true) . "` WHERE type = %s AND id != %d",
static::getType(),
$id
);
return $wpdb->query($query) !== false;
} catch (Exception $e) {
// Prevent save error on cron events edge cases
return false;
} catch (Error $e) {
// Prevent save error on cron events edge cases
return false;
}
}
/**
* Delete current entity
*
* @return bool True on success, or false on error.
*/
public function delete()
{
throw new Exception('Isn\'t possibile delete singleton entity, use reset to reset values');
}
/**
* Reset entity values
*
* @param string[] $skipProps the list of props to maintain
* @param ?callable $setCallback set callback function ($propName, $propValue): mixed
* @param ?callable $afterCallback callaback called before save
*
* @return bool True on success, or false on error.
*/
public function reset($skipProps = [], $setCallback = null, $afterCallback = null)
{
// Clean singleton instance
$newIstance = new static();
$reflect = new ReflectionClass($newIstance);
foreach ($reflect->getProperties() as $prop) {
if ($prop->getName() === 'id') {
continue;
}
if (in_array($prop->getName(), $skipProps)) {
continue;
}
$prop->setAccessible(true);
$newVal = (
is_callable($setCallback) ?
call_user_func($setCallback, $prop->getName(), $prop->getValue($newIstance)) :
$prop->getValue($newIstance)
);
$prop->setValue($this, $newVal);
}
if (is_callable($afterCallback)) {
call_user_func($afterCallback);
}
return $this->save();
}
}