99 lines
1.8 KiB
PHP
99 lines
1.8 KiB
PHP
<?php
|
|
/**
|
|
* Shared repository helpers.
|
|
*
|
|
* @package KGV\VereinManager
|
|
*/
|
|
|
|
namespace KGV\VereinManager\Repositories;
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
abstract class AbstractRepository {
|
|
|
|
/**
|
|
* WordPress database object.
|
|
*
|
|
* @var \wpdb
|
|
*/
|
|
protected $wpdb;
|
|
|
|
/**
|
|
* Concrete table name.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table = '';
|
|
|
|
/**
|
|
* Build repository instance.
|
|
*/
|
|
public function __construct() {
|
|
global $wpdb;
|
|
|
|
$this->wpdb = $wpdb;
|
|
$this->table = $this->resolve_table();
|
|
}
|
|
|
|
/**
|
|
* Resolve table name in child class.
|
|
*
|
|
* @return string
|
|
*/
|
|
abstract protected function resolve_table();
|
|
|
|
/**
|
|
* Find a single row by ID.
|
|
*
|
|
* @param int $id Record ID.
|
|
* @return object|null
|
|
*/
|
|
public function find( $id ) {
|
|
return $this->wpdb->get_row( $this->wpdb->prepare( "SELECT * FROM {$this->table} WHERE id = %d", absint( $id ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
|
|
}
|
|
|
|
/**
|
|
* Delete a record.
|
|
*
|
|
* @param int $id Record ID.
|
|
* @return bool
|
|
*/
|
|
public function delete( $id ) {
|
|
$result = $this->wpdb->delete( $this->table, array( 'id' => absint( $id ) ), array( '%d' ) );
|
|
return false !== $result;
|
|
}
|
|
|
|
/**
|
|
* Current local datetime.
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function now() {
|
|
return current_time( 'mysql' );
|
|
}
|
|
|
|
/**
|
|
* Restrict sortable columns.
|
|
*
|
|
* @param string $orderby Requested column.
|
|
* @param array $allowed Allowed columns.
|
|
* @param string $default Default column.
|
|
* @return string
|
|
*/
|
|
protected function sanitize_orderby( $orderby, $allowed, $default ) {
|
|
return in_array( $orderby, $allowed, true ) ? $orderby : $default;
|
|
}
|
|
|
|
/**
|
|
* Restrict sort direction.
|
|
*
|
|
* @param string $order Requested order.
|
|
* @return string
|
|
*/
|
|
protected function sanitize_order( $order ) {
|
|
return 'ASC' === strtoupper( (string) $order ) ? 'ASC' : 'DESC';
|
|
}
|
|
}
|