Skip to content

Commit 397fae5

Browse files
author
Soare Robert-Daniel
committed
dev: add centralized repository for db access
1 parent b9c976c commit 397fae5

11 files changed

Lines changed: 939 additions & 294 deletions

bin/wp-env/mu-plugins/ppom-e2e-bootstrap.php

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -510,22 +510,14 @@ function ppom_e2e_insert_ppom_group( $args ) {
510510

511511
$dt = apply_filters( 'ppom_settings_meta_data_new', $ppom_settings_meta_data );
512512

513-
global $wpdb;
514-
$ppom_table = $wpdb->prefix . PPOM_TABLE_META;
515-
$inserted = $wpdb->insert(
516-
$ppom_table,
517-
$dt,
518-
array_fill( 0, count( $dt ), '%s' )
519-
);
513+
$ppom_id = ppom_meta_repository()->insert_group( $dt, array_fill( 0, count( $dt ), '%s' ) );
520514

521-
if ( false === $inserted ) {
515+
if ( $ppom_id <= 0 ) {
522516
return new WP_Error(
523517
'meta_insert_failed',
524518
__( 'PPOM group could not be saved.', 'woocommerce-product-addon' )
525519
);
526520
}
527-
528-
$ppom_id = (int) $wpdb->insert_id;
529521
$final_fields = ppom_e2e_prepare_form_meta_fields( $ppom_fields, $ppom_id );
530522

531523
if ( is_wp_error( $final_fields ) ) {
@@ -766,16 +758,7 @@ function ppom_e2e_get_ppom_attach_row() {
766758
);
767759
}
768760

769-
global $wpdb;
770-
771-
$table = $wpdb->prefix . PPOM_TABLE_META;
772-
$row = $wpdb->get_row(
773-
$wpdb->prepare(
774-
"SELECT productmeta_categories, productmeta_tags FROM {$table} WHERE productmeta_id = %d",
775-
$ppom_id
776-
),
777-
ARRAY_A
778-
);
761+
$row = ppom_meta_repository()->get_categories_and_tags_columns( $ppom_id );
779762

780763
if ( empty( $row ) || ! is_array( $row ) ) {
781764
wp_send_json_error(
@@ -1164,16 +1147,7 @@ function ppom_e2e_reset_state() {
11641147
$tracked_meta_ids = ppom_e2e_get_tracked_meta_ids();
11651148

11661149
if ( ! empty( $tracked_meta_ids ) && defined( 'PPOM_TABLE_META' ) ) {
1167-
global $wpdb;
1168-
1169-
$ppom_table = $wpdb->prefix . PPOM_TABLE_META;
1170-
$placeholders = implode( ',', array_fill( 0, count( $tracked_meta_ids ), '%d' ) );
1171-
$deleted_result = $wpdb->query(
1172-
$wpdb->prepare(
1173-
"DELETE FROM {$ppom_table} WHERE productmeta_id IN ({$placeholders})",
1174-
$tracked_meta_ids
1175-
)
1176-
);
1150+
$deleted_result = ppom_meta_repository()->delete_by_ids( $tracked_meta_ids );
11771151

11781152
if ( false !== $deleted_result ) {
11791153
$deleted_meta_rows = (int) $deleted_result;

classes/admin.class.php

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -591,17 +591,9 @@ public function get_wc_tags( $current_values ) {
591591
* @return array
592592
*/
593593
public function get_db_field( $ppom_field_id ) {
594-
global $wpdb;
595-
$ppom_table = $wpdb->prefix . PPOM_TABLE_META;
596-
$rows = $wpdb->get_results(
597-
$wpdb->prepare(
598-
"SELECT productmeta_categories, productmeta_tags FROM $ppom_table WHERE productmeta_id = %d",
599-
$ppom_field_id
600-
),
601-
ARRAY_A
602-
);
594+
$rows = ppom_meta_repository()->get_categories_and_tags_columns( (int) $ppom_field_id );
603595

604-
return 0 < count( $rows ) && ! empty( $rows[0] ) ? $rows[0] : array();
596+
return ! empty( $rows ) ? $rows : array();
605597
}
606598

607599
/**
@@ -720,25 +712,7 @@ public function ppom_attach_ppoms() {
720712
* @return void
721713
*/
722714
public static function save_categories_and_tags( $ppom_id, $categories, $tags ) {
723-
global $wpdb;
724-
$ppom_table = $wpdb->prefix . PPOM_TABLE_META;
725-
726-
$data_to_update = array(
727-
'productmeta_categories' => implode( "\r\n", $categories ), // NOTE: Keep the backward compatible format.
728-
);
729-
730-
// false = caller chose not to change tags (e.g. E2E partial update); omit column from UPDATE.
731-
if ( is_array( $tags ) ) {
732-
$data_to_update['productmeta_tags'] = empty( $tags ) ? '' : serialize( $tags );
733-
}
734-
735-
$wpdb->update(
736-
$ppom_table,
737-
$data_to_update,
738-
array( 'productmeta_id' => $ppom_id ), // Where clause
739-
array( '%s' ), // Data format
740-
array( '%d' ) // Where format
741-
);
715+
ppom_meta_repository()->save_categories_and_tags( (int) $ppom_id, $categories, $tags );
742716
}
743717

744718
// Legacy settings bridge and setup.
@@ -955,10 +929,7 @@ public function set_legacy_user() {
955929
return;
956930
}
957931

958-
global $wpdb;
959-
$ppom_meta_table = $wpdb->prefix . PPOM_TABLE_META;
960-
// phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
961-
$res = $wpdb->get_results( "SELECT * FROM `$ppom_meta_table` WHERE `productmeta_js` != '' OR `productmeta_style` != ''" );
932+
$res = ppom_meta_repository()->get_rows_with_non_empty_style_or_js();
962933
update_option( 'ppom_legacy_user', ! empty( $res ) ? 'yes' : 'no' );
963934
}
964935

0 commit comments

Comments
 (0)