Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

All notable changes to this project will be documented in this file. This project adhere to the [Semantic Versioning](http://semver.org/) standard.

## [0.0.8] 2025-09-30
## [0.0.8] 2025-10-02

* Feature - Update the stellarwp/schema library to v3.
* Fix - Only delete task logs from the Task_Logs table when using DB_Logger in Provider::delete_tasks_on_action_deletion.
* Fix - Schedule cleanup task only when Shepherd tables have been registered successfully.
* Tweak - Update synchronous dispatch filter to default based on delay (true for no delay, false for delayed tasks).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"require": {
"php": ">=7.4",
"stellarwp/db": "^1.1",
"stellarwp/schema": "^2.0",
"stellarwp/schema": "^3.1.2",
"woocommerce/action-scheduler": "3.9.3",
"psr/log": "^1.1"
},
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ parameters:
- %currentWorkingDirectory%/src

ignoreErrors:
- '#Cannot assign new offset to.*Column_Collection#'
- '#StellarWP\\Schema\\Collections\\Column_Collection does not accept StellarWP\\Schema\\Columns\\Contracts\\#'
# Uses func_get_args()
- '#^Function add_query_arg invoked with [123] parameters?, 0 required\.$#'
# Uses func_get_args()
Expand Down
2 changes: 1 addition & 1 deletion src/Abstracts/Model_Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function delete(): void {
*/
public function to_array(): array {
$table_interface = $this->get_table_interface();
$columns = array_keys( $table_interface::get_columns() );
$columns = $table_interface::get_columns()->get_names();

$model = [];
foreach ( $columns as $column ) {
Expand Down
233 changes: 0 additions & 233 deletions src/Abstracts/Table_Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use StellarWP\DB\DB;
use StellarWP\Shepherd\Config;
use StellarWP\Shepherd\Tables\Utility\Safe_Dynamic_Prefix;
use StellarWP\Shepherd\Traits\Custom_Table_Query_Methods;
use DateTimeInterface;

/**
* Class Table_Abstract
Expand All @@ -26,113 +24,6 @@
* @package StellarWP\Shepherd\Abstracts
*/
abstract class Table_Abstract extends Table {
use Custom_Table_Query_Methods;

/**
* The PHP type for an integer.
*
* @since 0.0.1
*
* @var string
*/
public const PHP_TYPE_INT = 'int';

/**
* The PHP type for a string.
*
* @since 0.0.1
*
* @var string
*/
public const PHP_TYPE_STRING = 'string';

/**
* The PHP type for a float.
*
* @since 0.0.1
*
* @var string
*/
public const PHP_TYPE_FLOAT = 'float';

/**
* The PHP type for a boolean.
*
* @since 0.0.1
*
* @var string
*/
public const PHP_TYPE_BOOL = 'bool';

/**
* The PHP type for a datetime.
*
* @since 0.0.1
*
* @var string
*/
public const PHP_TYPE_DATETIME = DateTimeInterface::class;

/**
* The column type for a bigint.
*
* @since 0.0.1
*
* @var string
*/
public const COLUMN_TYPE_BIGINT = 'bigint';

/**
* The column type for a varchar.
*
* @since 0.0.1
*
* @var string
*/
public const COLUMN_TYPE_VARCHAR = 'varchar';

/**
* The column type for a text.
*
* @since 0.0.1
*
* @var string
*/
public const COLUMN_TYPE_TEXT = 'text';

/**
* The column type for a longtext.
*
* @since 0.0.1
*
* @var string
*/
public const COLUMN_TYPE_LONGTEXT = 'longtext';

/**
* The column type for a timestamp.
*
* @since 0.0.1
*
* @var string
*/
public const COLUMN_TYPE_TIMESTAMP = 'timestamp';

public const SQL_RESERVED_DEFAULTS = [
'CURRENT_TIMESTAMP',
'CURRENT_DATE',
'CURRENT_TIME',
];

/**
* The indexes for the table.
*
* @since 0.0.1
*
* @var array<array<string, string>>
*/
public const INDEXES = [];

/**
* Constructor.
*
Expand Down Expand Up @@ -174,130 +65,6 @@ public static function get_schema_slug(): string {
return sprintf( static::$schema_slug, Config::get_hook_prefix() );
}

/**
* An array of all the columns in the table.
*
* @since 0.0.1
*
* @return array<string, array<string, string>>
*/
abstract public static function get_columns(): array;

/**
* An array of all the columns that are searchable.
*
* @since 0.0.1
*
* @return string[]
*/
public static function get_searchable_columns(): array {
return [];
}

/**
* Helper method to check and add an index to a table.
*
* @since 0.0.1
*
* @param array $results The results array to track changes.
* @param string $index_name The name of the index.
* @param string $columns The columns to index.
*
* @return array The updated results array.
*/
protected function check_and_add_index( array $results, string $index_name, string $columns ): array {
$index_name = esc_sql( $index_name );

// Add index only if it does not exist.
if ( $this->has_index( $index_name ) ) {
return $results;
}

$columns = esc_sql( $columns );

DB::query(
DB::prepare( "ALTER TABLE %i ADD INDEX `{$index_name}` ( {$columns} )", esc_sql( static::table_name( true ) ) )
);

return $results;
}

/**
* Returns the table creation SQL in the format supported
* by the `dbDelta` function.
*
* @since 0.0.1
* @since 0.0.3 Updated to remove an empty line after the columns and before the primary key.
*
* @return string The table creation SQL, in the format supported
* by the `dbDelta` function.
*/
public function get_definition() {
global $wpdb;
$table_name = static::table_name( true );
$charset_collate = $wpdb->get_charset_collate();
$uid_column = static::uid_column();

$columns = static::get_columns();

$columns_definitions = [];
foreach ( $columns as $column => $definition ) {
$column_sql = "`{$column}` {$definition['type']}";

if ( ! empty( $definition['length'] ) ) {
$column_sql .= "({$definition['length']})";
}

if ( ! empty( $definition['unsigned'] ) ) {
$column_sql .= ' UNSIGNED';
}

$column_sql .= ! empty( $definition['nullable'] ) ? ' NULL' : ' NOT NULL';

if ( ! empty( $definition['auto_increment'] ) ) {
$column_sql .= ' AUTO_INCREMENT';
}

if ( ! empty( $definition['default'] ) ) {
$column_sql .= ' DEFAULT ' . ( in_array( $definition['default'], self::SQL_RESERVED_DEFAULTS, true ) || in_array( $definition['php_type'], [ self::PHP_TYPE_INT, self::PHP_TYPE_BOOL, self::PHP_TYPE_FLOAT ], true ) ? $definition['default'] : "'{$definition['default']}'" );
}

$columns_definitions[] = $column_sql;
}

$columns_sql = implode( ',' . PHP_EOL, $columns_definitions );

return "
CREATE TABLE `{$table_name}` (
{$columns_sql},
PRIMARY KEY (`{$uid_column}`)
) {$charset_collate};
";
}

/**
* Add indexes after table creation.
*
* @since 0.0.1
*
* @param array<string,string> $results A map of results in the format
* returned by the `dbDelta` function.
*
* @return array<string,string> A map of results in the format returned by
* the `dbDelta` function.
*/
protected function after_update( array $results ) {
if ( empty( static::INDEXES ) || ! is_array( static::INDEXES ) ) {
return $results;
}

foreach ( static::INDEXES as $index ) {
$this->check_and_add_index( $results, $index['name'], $index['columns'] );
}

return $results;
}

/**
* Returns the base table name without the dynamic prefix.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static function get_container(): ContainerInterface {
*/
public static function get_hook_prefix(): string {
if ( ! static::$hook_prefix ) {
$class = __CLASS__;
$class = self::class;
throw new RuntimeException( "You must specify a hook prefix for your project with {$class}::set_hook_prefix()" );
}

Expand Down
7 changes: 4 additions & 3 deletions src/Contracts/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace StellarWP\Shepherd\Contracts;

use StellarWP\Shepherd\Abstracts\Table_Abstract;
use StellarWP\Schema\Tables\Contracts\Table;

/**
* The Shepherd model contract.
Expand Down Expand Up @@ -61,10 +61,11 @@ public function delete(): void;
* Gets the table interface for the model.
*
* @since 0.0.1
* @since 0.0.8 Updated to return Table instead.
*
* @return Table_Abstract The table interface.
* @return Table The table interface.
*/
public function get_table_interface(): Table_Abstract;
public function get_table_interface(): Table;

/**
* Converts the model to an array.
Expand Down
9 changes: 5 additions & 4 deletions src/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use StellarWP\Shepherd\Contracts\Logger;
use StellarWP\Shepherd\Abstracts\Model_Abstract;
use DateTimeInterface;
use StellarWP\Shepherd\Abstracts\Table_Abstract;
use StellarWP\Schema\Tables\Contracts\Table;
use Psr\Log\LogLevel;
use InvalidArgumentException;
use DateTime;
Expand Down Expand Up @@ -279,12 +279,13 @@ public function get_entry(): string {
* Gets the table interface for the log.
*
* @since 0.0.1
* @since 0.0.8 Updated to return Table instead.
*
* @return Table_Abstract The table interface.
* @return Table The table interface.
*
* @throws RuntimeException If the log table interface is invalid.
*/
public function get_table_interface(): Table_Abstract {
public function get_table_interface(): Table {
$logger = Config::get_container()->get( Logger::class );

$table = null;
Expand Down Expand Up @@ -315,7 +316,7 @@ public function get_table_interface(): Table_Abstract {
*/
public function to_array(): array {
$table_interface = Task_Logs_Table::class;
$columns = array_keys( $table_interface::get_columns() );
$columns = $table_interface::get_columns()->get_names();

$model = [];
foreach ( $columns as $column ) {
Expand Down
Loading
Loading