Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coupons & Fees - Relationship Table #3257

Open
wants to merge 17 commits into
base: bucket/order_modifiers_coupon_fees
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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
4 changes: 3 additions & 1 deletion src/Tickets/Order_Modifiers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use TEC\Common\StellarWP\Schema\Register as Schema_Register;
use TEC\Common\StellarWP\Schema\Config as Schema_Config;
use TEC\Common\StellarWP\DB\DB;
use TEC\Tickets\Order_Modifiers\Custom_Tables\Order_Modifier_Relationships;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[phpcs] reported by reviewdog 🐶
SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse
Type TEC\Tickets\Order_Modifiers\Custom_Tables\Order_Modifier_Relationships is not used in this file.

use TEC\Tickets\Order_Modifiers\Custom_Tables\Order_Modifiers;
use TEC\Tickets\Order_Modifiers\Custom_Tables\Order_Modifiers_Meta;
use TEC\Tickets\Order_Modifiers\Modifiers\Coupon;
Expand Down Expand Up @@ -60,7 +61,7 @@ public function do_register(): void {
* @since TBD
*/
protected function hook() {
tribe( Modifier_Settings::class )->register();
tribe( Modifier_Admin_Handler::class )->register();
}

/**
Expand All @@ -84,6 +85,7 @@ public function unregister(): void {
public function register_tables(): void {
$this->container->singleton( Order_Modifiers::class, Schema_Register::table( Order_Modifiers::class ) );
$this->container->singleton( Order_Modifiers_Meta::class, Schema_Register::table( Order_Modifiers_Meta::class ) );
$this->container->singleton( Order_Modifier_Relationships::class, Schema_Register::table( Order_Modifier_Relationships::class ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
/**
* Order Modifiers Relationships custom table logic.
*
* @package TEC\Tickets\Order_Modifiers\Custom_Tables;
*/

namespace TEC\Tickets\Order_Modifiers\Custom_Tables;

use TEC\Common\StellarWP\Schema\Tables\Contracts\Table;
use wpdb;

/**
* Class Orders_Modifiers.
*
* @since TBD
*
* @package TEC\Tickets\Order_Modifiers\Custom_Tables;
*/
class Order_Modifier_Relationships extends Table {
/**
* @since TBD
*
* @var string|null The version number for this schema definition.
*/
public const SCHEMA_VERSION = '1.0.0';

/**
* @since TBD
*
* @var string The base table name.
*/
protected static $base_table_name = 'tec_order_modifier_relationships';

/**
* @since TBD
*
* @var string The organizational group this table belongs to.
*/
protected static $group = 'tec_order_modifiers_group';

/**
* @since TBD
*
* @var string|null The slug used to identify the custom table.
*/
protected static $schema_slug = 'tec-order-modifiers-relationships';

/**
* @since TBD
*
* @var string The field that uniquely identifies a row in the table.
*/
protected static $uid_column = 'object_id';

/**
* Returns the table creation SQL in the format supported
* by the `dbDelta` function.
*
* @since TBD
*
* @return string The table creation SQL, in the format supported
* by the `dbDelta` function.
*/
protected function get_definition() {
global $wpdb;
$table_name = self::table_name( true );
$charset_collate = $wpdb->get_charset_collate();
$parent_table_name = Order_Modifiers::table_name();
$parent_table_uid = Order_Modifiers::uid_column();
$wp_posts_table = $wpdb->posts;

return "
CREATE TABLE `$table_name` (
`object_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`modifier_id` BIGINT UNSIGNED NOT NULL,
`post_id` BIGINT UNSIGNED NOT NULL,
`post_type` VARCHAR(20) NOT NULL,
PRIMARY KEY (`object_id`),
FOREIGN KEY (`modifier_id`) REFERENCES $parent_table_name($parent_table_uid)ON DELETE CASCADE,
CONSTRAINT `fk_post_id` FOREIGN KEY (`post_id`) REFERENCES `$wp_posts_table` (`ID`) ON DELETE CASCADE
) $charset_collate;
";
}

/**
* Allows extending classes that require it to run some methods
* immediately after the table creation or update.
*
* @since TBD
*
* @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 ): array {
// If nothing was changed by dbDelta(), bail.
if ( ! count( $results ) ) {
return $results;
}

global $wpdb;
$table_name = self::table_name( true );

// Check if the table exists first.
if ( ! $this->exists() ) {
return $results;
}

// Helper method to check and add indexes.
$results = $this->check_and_add_index( $wpdb, $results, $table_name, 'tec_order_modifier_relationship_indx_modifier_id', 'modifier_id' );
$results = $this->check_and_add_index( $wpdb, $results, $table_name, 'tec_order_modifier_relationship_indx_post_type', 'post_id,post_type' );
$results = $this->check_and_add_index( $wpdb, $results, $table_name, 'tec_order_modifier_relationship_indx_composite_join', 'modifier_id, post_id, post_type' );

return $results;
}

/**
* Helper method to check and add an index to a table.
*
* @since TBD
*
* @param wpdb $wpdb The WordPress database global.
* @param array $results The results array to track changes.
* @param string $table_name The name of the table.
* @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( wpdb $wpdb, array $results, string $table_name, string $index_name, string $columns ): array {
// Escape table name and columns for safety.
$table_name = esc_sql( $table_name );
$columns = esc_sql( $columns );

// Add index only if it does not exist.
if ( ! $this->has_index( $index_name ) ) {
// Prepare the SQL for adding an index.
$sql = $wpdb->prepare(
"ALTER TABLE `$table_name` ADD INDEX `%s` ( $columns )",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[phpcs] reported by reviewdog 🐶
WordPress.DB.PreparedSQL.InterpolatedNotPrepared
Use placeholders and $wpdb->prepare(); found interpolated variable $table_name at "ALTER TABLE $table_name ADD INDEX %s ( $columns )"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[phpcs] reported by reviewdog 🐶
WordPress.DB.PreparedSQL.InterpolatedNotPrepared
Use placeholders and $wpdb->prepare(); found interpolated variable $columns at "ALTER TABLE $table_name ADD INDEX %s ( $columns )"

$index_name
);

// phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.NotPrepared
$updated = $wpdb->query( $sql );

if ( $updated ) {
$message = sprintf( 'Added index to the %s table on %s.', $table_name, $columns );
} else {
$message = sprintf( 'Failed to add an index on the %s table for %s.', $table_name, $columns );
}

$results[ "{$table_name}.{$columns}" ] = $message;
}

return $results;
}
}
3 changes: 0 additions & 3 deletions src/Tickets/Order_Modifiers/Custom_Tables/Order_Modifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ protected function get_definition() {
return "
CREATE TABLE `$table_name` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`post_id` BIGINT UNSIGNED NOT NULL,
`modifier_type` VARCHAR(255) NOT NULL,
`sub_type` VARCHAR(255) NOT NULL,
`fee_amount_cents` INT NOT NULL,
Expand Down Expand Up @@ -113,9 +112,7 @@ protected function after_update( array $results ): array {
}

// Helper method to check and add indexes.
$results = $this->check_and_add_index( $wpdb, $results, $table_name, 'tec_order_modifier_indx_post_id', 'post_id' );
$results = $this->check_and_add_index( $wpdb, $results, $table_name, 'tec_order_modifier_indx_slug', 'slug' );
$results = $this->check_and_add_index( $wpdb, $results, $table_name, 'tec_order_modifier_indx_post_id_status', 'post_id, status' );
$results = $this->check_and_add_index( $wpdb, $results, $table_name, 'tec_order_modifier_indx_status_modifier_type_slug', 'status, modifier_type, slug' );
$results = $this->check_and_add_index( $wpdb, $results, $table_name, 'tec_order_modifier_indx_type_display_name', 'modifier_type, display_name' );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ class Order_Modifier_DTO extends DataTransferObject {
*/
protected int $id;

/**
* The associated post ID.
*
* @since TBD
*
* @var int
*/
protected int $post_id;

/**
* The modifier type (coupon, fee).
*
Expand Down Expand Up @@ -133,7 +124,6 @@ public static function fromObject( $object ): self {
$self = new self();

$self->id = $object->id;
$self->post_id = $object->post_id;
$self->modifier_type = $object->modifier_type;
$self->sub_type = $object->sub_type;
$self->fee_amount_cents = $object->fee_amount_cents;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* The data transfer object for the Order Modifier Relationship model.
*
* @since TBD
*
* @package TEC\Tickets\Order_Modifiers\Data_Transfer_Objects;
*/

namespace TEC\Tickets\Order_Modifiers\Data_Transfer_Objects;

use TEC\Common\StellarWP\Models\DataTransferObject;
use TEC\Tickets\Order_Modifiers\Models\Order_Modifier_Relationships;

/**
* Class Order_Modifier_Relationships_DTO.
*
* @since TBD
*
* @package TEC\Tickets\Order_Modifiers\Data_Transfer_Objects;
*/
class Order_Modifier_Relationships_DTO extends DataTransferObject {

/**
* The primary key of the relationship.
*
* @since TBD
*
* @var int
*/
protected int $object_id;

/**
* The modifier ID.
*
* @since TBD
*
* @var int
*/
protected int $modifier_id;

/**
* The post ID.
*
* @since TBD
*
* @var int
*/
protected int $post_id;

/**
* The post type.
*
* @since TBD
*
* @var string
*/
protected string $post_type;

/**
* The post title (optional, for clarity when rendering data).
*
* @since TBD
*
* @var string|null
*/
protected ?string $post_title = null;

/**
* Builds a new DTO from an object.
*
* @since TBD
*
* @param object $object The object to build the DTO from.
*
* @return Order_Modifier_Relationships_DTO The DTO instance.
*/
public static function fromObject( $object ): self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[phpcs] reported by reviewdog 🐶
Universal.NamingConventions.NoReservedKeywordParameterNames.objectFound
It is recommended not to use reserved keyword "object" as function parameter name. Found: $object

$self = new self();

$self->object_id = $object->object_id;
$self->modifier_id = $object->modifier_id;
$self->post_id = $object->post_id;
$self->post_type = $object->post_type;
$self->post_title = $object->post_title ?? null;

return $self;
}

/**
* Builds a model instance from the DTO.
*
* @since TBD
*
* @return Order_Modifier_Relationships The model instance.
*/
public function toModel(): Order_Modifier_Relationships {
$attributes = get_object_vars( $this );

return new Order_Modifier_Relationships( $attributes );
}
}
2 changes: 0 additions & 2 deletions src/Tickets/Order_Modifiers/Models/Order_Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* @package TEC\Tickets\Order_Modifiers\Models;
*
* @property int $id The Order Modifier ID.
* @property int $post_id Associated post ID.
* @property string $modifier_type The type of modifier (coupon, fee).
* @property string $sub_type The sub-type of modifier (percentage, flat).
* @property int $fee_amount_cents Amount of fee in cents.
Expand All @@ -42,7 +41,6 @@ class Order_Modifier extends Model implements ModelCrud, ModelFromQueryBuilderOb
*/
protected $properties = [
'id' => 'int',
'post_id' => 'int',
'modifier_type' => 'string',
'sub_type' => 'string',
'fee_amount_cents' => 'int',
Expand Down
Loading
Loading