- Implemented a new Banner model to represent banner data. - Created a BannerRepository for database interactions related to banners. - Developed a BannerService to handle business logic for banners. - Added admin views for listing and adding banners. - Integrated banner hooks for frontend rendering and click tracking. - Created frontend styles and scripts for banner display and interaction. - Updated database migrations to include a new banners table. - Enhanced AdminController to manage banner actions and pages.
82 lines
2.2 KiB
PHP
82 lines
2.2 KiB
PHP
<?php
|
|
namespace Sodino\Repositories;
|
|
|
|
use Sodino\Models\Banner;
|
|
|
|
/**
|
|
* Banner Repository
|
|
*/
|
|
class BannerRepository {
|
|
private $table_name;
|
|
|
|
public function __construct() {
|
|
global $wpdb;
|
|
$this->table_name = $wpdb->prefix . 'sodino_banners';
|
|
}
|
|
|
|
public function getAll() {
|
|
global $wpdb;
|
|
$results = $wpdb->get_results("SELECT * FROM {$this->table_name} ORDER BY priority DESC, id ASC", ARRAY_A);
|
|
$banners = [];
|
|
foreach ($results as $result) {
|
|
$banners[] = new Banner($result);
|
|
}
|
|
return $banners;
|
|
}
|
|
|
|
public function getById($id) {
|
|
global $wpdb;
|
|
$result = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$this->table_name} WHERE id = %d", $id), ARRAY_A);
|
|
return $result ? new Banner($result) : null;
|
|
}
|
|
|
|
public function getEnabled() {
|
|
global $wpdb;
|
|
$results = $wpdb->get_results("SELECT * FROM {$this->table_name} WHERE status = 1 ORDER BY priority DESC, id ASC", ARRAY_A);
|
|
$banners = [];
|
|
foreach ($results as $result) {
|
|
$banners[] = new Banner($result);
|
|
}
|
|
return $banners;
|
|
}
|
|
|
|
public function save(Banner $banner) {
|
|
global $wpdb;
|
|
$data = $banner->toArray();
|
|
unset($data['id'], $data['created_at']);
|
|
|
|
if ($banner->id) {
|
|
$wpdb->update($this->table_name, $data, ['id' => $banner->id]);
|
|
$this->clearCache();
|
|
return $banner->id;
|
|
}
|
|
|
|
$wpdb->insert($this->table_name, $data);
|
|
$this->clearCache();
|
|
return $wpdb->insert_id;
|
|
}
|
|
|
|
public function delete($id) {
|
|
global $wpdb;
|
|
$result = $wpdb->delete($this->table_name, ['id' => $id]);
|
|
$this->clearCache();
|
|
return $result;
|
|
}
|
|
|
|
public function incrementImpression($id) {
|
|
global $wpdb;
|
|
$wpdb->query($wpdb->prepare("UPDATE {$this->table_name} SET impressions = impressions + 1 WHERE id = %d", $id));
|
|
$this->clearCache();
|
|
}
|
|
|
|
public function incrementClick($id) {
|
|
global $wpdb;
|
|
$wpdb->query($wpdb->prepare("UPDATE {$this->table_name} SET clicks = clicks + 1 WHERE id = %d", $id));
|
|
$this->clearCache();
|
|
}
|
|
|
|
public function clearCache() {
|
|
wp_cache_flush();
|
|
}
|
|
}
|