Skip to content
Open
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
4 changes: 4 additions & 0 deletions changelog/fix-pluginscore-ratings
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: update

update: usage of WP best practices
6 changes: 3 additions & 3 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ public function process_payment( $order_id ) {
// This allows WC to check if WP_DEBUG mode is enabled before returning previous Exception and expose Exception class name to frontend.
add_filter( 'woocommerce_return_previous_exceptions', '__return_true' );
wc_add_notice( wp_strip_all_tags( WC_Payments_Utils::get_filtered_error_message( $e, $blocked_by_fraud_rules ) ), 'error' );
do_action( 'update_payment_result_on_error', $e, $order );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only usage of update_payment_result_on_error is in our codebase. I verified it with this:

So I think it's safe to rename this hook to have a wcpay_ prefix.

do_action( 'wcpay_update_payment_result_on_error', $e, $order );

return [
'result' => 'fail',
Expand Down Expand Up @@ -1340,15 +1340,15 @@ public function update_customer_with_order_data( $order, $customer_id, $is_test_

/**
* Sets up a handler to add error details to the payment result.
* Registers an action to handle 'update_payment_result_on_error',
* Registers an action to handle 'wcpay_update_payment_result_on_error',
* using the payment result object from 'woocommerce_rest_checkout_process_payment_with_context'.
*
* @param PaymentContext $context The payment context.
* @param PaymentResult $result The payment result, passed by reference.
*/
public function setup_payment_error_handler( PaymentContext $context, PaymentResult &$result ) {
add_action(
'update_payment_result_on_error',
'wcpay_update_payment_result_on_error',
function ( $error ) use ( &$result ) {
$result->set_payment_details(
array_merge(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @param string $currency The currency to display.
* @return string
*/
function format_price_helper( array $product, string $currency ): string {
function wcpay_format_price_helper( array $product, string $currency ): string {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Despite this function being inside of a template, it looks like we still need to prefix it with a wcpay_ prefix, otherwise WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound fails

$active_price = $product['price'];
$regular_price = $product['regular_price'];
$has_discount = $active_price !== $regular_price;
Expand Down Expand Up @@ -139,7 +139,7 @@ function format_price_helper( array $product, string $currency ): string {
<tr>
<td class="align-left">
<div><?php echo esc_html( $item['name'] ); ?></div>
<div><?php echo esc_html( $item['quantity'] ); ?> @ <?php echo wp_kses( format_price_helper( $item['product'], $order['currency'] ), 'post' ); ?></div>
<div><?php echo esc_html( $item['quantity'] ); ?> @ <?php echo wp_kses( wcpay_format_price_helper( $item['product'], $order['currency'] ), 'post' ); ?></div>
<div><?php printf( '%s: %s', esc_html__( 'SKU', 'woocommerce-payments' ), esc_html( $item['product']['id'] ) ); ?></div> <!-- TODO SKU or ID? -->
</td>
<td class="align-right align-top"><?php echo wp_kses( wc_price( $item['subtotal'], [ 'currency' => $order['currency'] ] ), 'post' ); ?></td>
Expand Down
9 changes: 4 additions & 5 deletions includes/notes/class-wc-payments-remote-note-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ private function create_note( array $note_data ) {
*/
public function delete_notes() {
global $wpdb;
$prefix = self::NOTE_NAME_PREFIX;
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "DELETE FROM {$wpdb->prefix}wc_admin_note_actions WHERE name LIKE '{$prefix}%'" );
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "DELETE FROM {$wpdb->prefix}wc_admin_notes WHERE name LIKE '{$prefix}%'" );
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}wc_admin_note_actions WHERE name LIKE %s", $wpdb->esc_like( self::NOTE_NAME_PREFIX ) . '%' ) );
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}wc_admin_notes WHERE name LIKE %s", $wpdb->esc_like( self::NOTE_NAME_PREFIX ) . '%' ) );
}

/**
Expand Down
3 changes: 2 additions & 1 deletion includes/woopay/class-woopay-store-api-session-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public function save_data() {

$wpdb->query(
$wpdb->prepare(
"INSERT INTO $this->table (`session_key`, `session_value`, `session_expiry`) VALUES (%s, %s, %d) ON DUPLICATE KEY UPDATE `session_value` = VALUES(`session_value`), `session_expiry` = VALUES(`session_expiry`)", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here I just moved the phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared comment before the query. It looks like some linters might fail it.

// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"INSERT INTO $this->table (`session_key`, `session_value`, `session_expiry`) VALUES (%s, %s, %d) ON DUPLICATE KEY UPDATE `session_value` = VALUES(`session_value`), `session_expiry` = VALUES(`session_expiry`)",
$this->_customer_id,
maybe_serialize( $this->_data ),
$this->session_expiration
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/notes/test-class-wc-payments-remote-note-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,36 @@ public function test_throws_on_malformed_data() {
$this->expectException( Rest_Request_Exception::class );
$this->note_service->put_note( $note_data );
}

public function test_delete_notes() {
global $wpdb;

$real_data_store = WC_Data_Store::load( 'admin-note' );
$note_service = new WC_Payments_Remote_Note_Service( $real_data_store );

$note_data = [
'title' => 'test',
'content' => 'hello_world',
];

$note_service->put_note( $note_data );

$notes_before = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->prefix}wc_admin_notes WHERE name LIKE %s",
'wc-payments-remote-notes-%'
)
);
$this->assertEquals( 1, $notes_before );

$note_service->delete_notes();

$notes_after = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->prefix}wc_admin_notes WHERE name LIKE %s",
'wc-payments-remote-notes-%'
)
);
$this->assertEquals( 0, $notes_after );
}
}
Loading