- Implemented the main module class with essential properties and methods. - Added translation support for various user interface strings. - Created XML configuration file for module versioning. - Ensured compatibility with different PHP versions and PrestaShop versions.
137 lines
4.2 KiB
PHP
137 lines
4.2 KiB
PHP
<?php
|
|
|
|
use X13Webp\Helpers\X13Db;
|
|
|
|
include(dirname(__FILE__) . '/../../config/config.inc.php');
|
|
|
|
/* Check security token */
|
|
if (!Tools::isPHPCLI()) {
|
|
include(dirname(__FILE__) . '/../../init.php');
|
|
}
|
|
|
|
class x13webpCron {
|
|
|
|
private $module;
|
|
private $done_images = 0;
|
|
|
|
public function __construct()
|
|
{
|
|
|
|
$this->module = Module::getInstanceByName('x13webp');
|
|
|
|
if (Tools::getValue('token') == $this->module->x13helper->getCronToken()) {
|
|
|
|
$cron_activity = Configuration::get($this->module->options_prefix . 'CRON_ACTIVITY');
|
|
if ($cron_activity && $cron_activity > time()) {
|
|
die('Cron has already been started');
|
|
} else {
|
|
Configuration::updateValue($this->module->options_prefix . 'CRON_ACTIVITY', strtotime('+ 1 minutes', time()));
|
|
}
|
|
|
|
$this->start_time = time();
|
|
$this->max_execution_time = (int) ini_get('max_execution_time');
|
|
|
|
if (Configuration::get($this->module->options_prefix . 'CRON_OPERATION_MODE') == 2) {
|
|
|
|
$this->deleteAllWebp();
|
|
}
|
|
|
|
$this->generateMissingWebp();
|
|
|
|
Configuration::updateValue($this->module->options_prefix . 'CRON_DATE', date('Y-m-d H:i:s'));
|
|
|
|
die('Done: ' . $this->getCronLog());
|
|
} else {
|
|
die('Bad token');
|
|
}
|
|
}
|
|
|
|
public function getCronLog()
|
|
{
|
|
return $this->done_images . ' images generated';
|
|
}
|
|
|
|
public function checkMaxExecutionTime()
|
|
{
|
|
if (time() - $this->start_time > $this->max_execution_time - 4) die('Timeout: ' . $this->getCronLog());
|
|
}
|
|
|
|
public function generateMissingWebp()
|
|
{
|
|
|
|
$allowed = json_decode(Configuration::get($this->module->options_prefix . 'ALLOWED_GENERATED_IMAGES'), true);
|
|
|
|
$types = $this->module->x13helper->getImageTypes();
|
|
|
|
if (!empty($types)) foreach ($types as $type) {
|
|
|
|
if (!in_array($type['val'], $allowed)) continue;
|
|
|
|
$total = $this->module->x13helper->getTotalImages($type['val']);
|
|
|
|
if ($type['has_types']) {
|
|
|
|
$image_types = ImageType::getImagesTypes($type['val']);
|
|
|
|
array_unshift($image_types, [
|
|
'name' => 'main'
|
|
]);
|
|
|
|
if (!empty($image_types)) foreach ($image_types as $image_type) {
|
|
|
|
$done = X13Db::getTotalDoneImages($type['val'], $image_type['name']);
|
|
|
|
$output = $this->module->x13converter->convertImages($done, $total, $type['val'], $image_type['name'], 'webp');
|
|
|
|
while ($output && !$output['complete'] && !$output['error']) {
|
|
|
|
$this->checkMaxExecutionTime();
|
|
|
|
$output = $this->module->x13converter->convertImages($output['done'], $total, $type['val'], $image_type['name'], 'webp');
|
|
$this->done_images += 1;
|
|
}
|
|
}
|
|
} else {
|
|
|
|
if ($type['val'] == 'theme') {
|
|
$done = X13Db::getTotalDoneImages($type['val'] . '_' . _THEME_NAME_, '');
|
|
if (defined('_PARENT_THEME_NAME_') && !empty(_PARENT_THEME_NAME_)) {
|
|
$done += X13Db::getTotalDoneImages($type['val'] . '_' . _PARENT_THEME_NAME_, '');
|
|
}
|
|
} else {
|
|
$done = X13Db::getTotalDoneImages($type['val']);
|
|
}
|
|
|
|
$output = $this->module->x13converter->convertImages($done, $total, $type['val'], 'all', 'webp');
|
|
|
|
while ($output && !$output['complete'] && !$output['error']) {
|
|
|
|
$this->checkMaxExecutionTime();
|
|
|
|
$output = $this->module->x13converter->convertImages($output['done'], $total, $type['val'], '', 'webp');
|
|
$this->done_images += 1;
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
public function deleteAllWebp()
|
|
{
|
|
|
|
$type = X13Db::getGeneratedWebpType();
|
|
|
|
if (!$type) return true;
|
|
|
|
$image_type = X13Db::getGeneratedImageType($type);
|
|
|
|
if ($image_type !== false) {
|
|
|
|
$this->module->x13cleaner->forceDeleteWebpImages($type, $image_type);
|
|
}
|
|
|
|
$this->deleteAllWebp();
|
|
}
|
|
|
|
}
|
|
|
|
new x13webpCron(); |