-
Notifications
You must be signed in to change notification settings - Fork 43
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
base: bucket/order_modifiers_coupon_fees
Are you sure you want to change the base?
Changes from 14 commits
f723ae2
ca89056
2262d43
8099b6b
530d094
62807de
b0edc94
959a1e2
b3b6d2e
32d15c4
eed4a58
2841c0b
0f7ed87
ce33fb8
3c5739a
5dd2891
0fa6f99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 )", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
$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; | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
$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 ); | ||
} | ||
} |
There was a problem hiding this comment.
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.