Skip to content

Commit 9949c75

Browse files
authored
Merge pull request #2772 from woocommerce/tweak/plugin-check-errors-and-warnings
Resolve some of the plugin check errors and warnings
2 parents a80bf4a + b371832 commit 9949c75

37 files changed

+77
-101
lines changed

google-listings-and-ads.php

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
* WC tested up to: 9.6
1717
* Woo:
1818
*
19+
* License: GPLv3
20+
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
21+
*
1922
* @package WooCommerce\Admin
2023
*/
2124

phpcs.xml.dist

+1-10
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,6 @@
8585
</properties>
8686
</rule>
8787

88-
<!-- We'd rather use native functions -->
89-
<rule ref="WordPress.WP.AlternativeFunctions">
90-
<properties>
91-
<property name="exclude" type="array">
92-
<element value="json_encode"/>
93-
<element value="rand"/>
94-
</property>
95-
</properties>
96-
</rule>
97-
9888
<!-- We're judicious in our usage of meta queries -->
9989
<rule ref="WordPress.DB.SlowDBQuery">
10090
<properties>
@@ -118,6 +108,7 @@
118108
<file>./bin</file>
119109
<file>./views</file>
120110
<file>./google-listings-and-ads.php</file>
111+
<file>./uninstall.php</file>
121112

122113
<!-- Show progress and sniff codes in all reports -->
123114
<arg value="ps"/>

src/API/Google/AdsConversionAction.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct( GoogleAdsClient $client ) {
5757
*/
5858
public function create_conversion_action(): array {
5959
try {
60-
$unique = sprintf( '%04x', mt_rand( 0, 0xffff ) );
60+
$unique = sprintf( '%04x', wp_rand( 0, 0xffff ) );
6161

6262
$conversion_action_operation = new ConversionActionOperation();
6363
$conversion_action_operation->setCreate(

src/API/Google/Connection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function connect( string $return_url, string $login_hint = '' ): string {
5252
$result = $client->post(
5353
$this->get_connection_url(),
5454
[
55-
'body' => json_encode( $post_body ),
55+
'body' => wp_json_encode( $post_body ),
5656
]
5757
);
5858

src/API/Google/Middleware.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected function create_merchant_account_request( string $name, string $site_u
131131
$result = $client->post(
132132
$this->get_manager_url( 'create-merchant' ),
133133
[
134-
'body' => json_encode(
134+
'body' => wp_json_encode(
135135
[
136136
'name' => $name,
137137
'websiteUrl' => $site_url,
@@ -198,7 +198,7 @@ public function link_merchant_to_mca(): bool {
198198
$result = $client->post(
199199
$this->get_manager_url( 'link-merchant' ),
200200
[
201-
'body' => json_encode(
201+
'body' => wp_json_encode(
202202
[
203203
'accountId' => $this->options->get_merchant_id(),
204204
]
@@ -240,7 +240,7 @@ public function claim_merchant_website( bool $overwrite = false ): bool {
240240
$result = $client->post(
241241
$this->get_manager_url( 'claim-website' ),
242242
[
243-
'body' => json_encode(
243+
'body' => wp_json_encode(
244244
[
245245
'accountId' => $this->options->get_merchant_id(),
246246
'overwrite' => $overwrite,
@@ -299,7 +299,7 @@ public function create_ads_account(): array {
299299
$result = $client->post(
300300
$this->get_manager_url( $country . '/create-customer' ),
301301
[
302-
'body' => json_encode(
302+
'body' => wp_json_encode(
303303
[
304304
'descriptive_name' => $this->new_account_name(),
305305
'currency_code' => get_woocommerce_currency(),
@@ -358,7 +358,7 @@ public function link_ads_account( int $id ): array {
358358
$result = $client->post(
359359
$this->get_manager_url( 'link-customer' ),
360360
[
361-
'body' => json_encode(
361+
'body' => wp_json_encode(
362362
[
363363
'client_customer' => $id,
364364
]
@@ -429,7 +429,7 @@ public function mark_tos_accepted( string $service, string $email ): TosAccepted
429429
$result = $client->post(
430430
$this->get_tos_url( $service ),
431431
[
432-
'body' => json_encode(
432+
'body' => wp_json_encode(
433433
[
434434
'email' => $email,
435435
]

src/API/WP/NotificationsService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function notify( string $topic, $item_id = null, $data = [] ): bool {
126126

127127
do_action(
128128
'woocommerce_gla_debug_message',
129-
sprintf( 'Notification - Item ID: %s - Topic: %s - Data %s', $item_id, $topic, json_encode( $data ) ),
129+
sprintf( 'Notification - Item ID: %s - Topic: %s - Data %s', $item_id, $topic, wp_json_encode( $data ) ),
130130
__METHOD__
131131
);
132132

src/Assets/ScriptAsset.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected function get_enqueue_callback(): callable {
124124
}
125125

126126
foreach ( $this->inline_scripts as $variable_name => $data_array ) {
127-
$inline_script = "var $variable_name = " . json_encode( $data_array );
127+
$inline_script = "var $variable_name = " . wp_json_encode( $data_array );
128128
wp_add_inline_script( $this->handle, $inline_script, 'before' );
129129
}
130130

src/ConnectionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ protected function render_admin_page() {
770770
<th><label>Errors:</label></th>
771771
<td>
772772
<p>
773-
<code><?php echo isset( $this->integration_status_response['errors'] ) ? wp_kses_post( json_encode( $this->integration_status_response['errors'] ) ) ?? '' : '-'; ?></code>
773+
<code><?php echo isset( $this->integration_status_response['errors'] ) ? wp_kses_post( wp_json_encode( $this->integration_status_response['errors'] ) ) ?? '' : '-'; ?></code>
774774
</p>
775775
</td>
776776
</tr>

src/Coupon/CouponSyncer.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function update( WC_Coupon $coupon ) {
142142
sprintf(
143143
'Skipping coupon (ID: %s) because it does not pass validation: %s',
144144
$coupon->get_id(),
145-
json_encode( $validation_result )
145+
wp_json_encode( $validation_result )
146146
),
147147
__METHOD__
148148
);
@@ -156,7 +156,7 @@ public function update( WC_Coupon $coupon ) {
156156
sprintf(
157157
'Start to upload coupon (ID: %s) as promotion structure: %s',
158158
$coupon->get_id(),
159-
json_encode( $adapted_coupon )
159+
wp_json_encode( $adapted_coupon )
160160
),
161161
__METHOD__
162162
);
@@ -172,7 +172,7 @@ public function update( WC_Coupon $coupon ) {
172172
'woocommerce_gla_debug_message',
173173
sprintf(
174174
"Submitted promotion:\n%s",
175-
json_encode( $adapted_coupon )
175+
wp_json_encode( $adapted_coupon )
176176
),
177177
__METHOD__
178178
);
@@ -195,7 +195,7 @@ public function update( WC_Coupon $coupon ) {
195195
'woocommerce_gla_debug_message',
196196
sprintf(
197197
"Promotion failed to sync with Merchant Center:\n%s",
198-
json_encode( $invalid_promotion )
198+
wp_json_encode( $invalid_promotion )
199199
),
200200
__METHOD__
201201
);
@@ -261,7 +261,7 @@ public function delete( DeleteCouponEntry $coupon ) {
261261
sprintf(
262262
'Start to delete coupon (ID: %s) as promotion structure: %s',
263263
$coupon->get_wc_coupon_id(),
264-
json_encode( $adapted_coupon )
264+
wp_json_encode( $adapted_coupon )
265265
),
266266
__METHOD__
267267
);
@@ -310,7 +310,7 @@ public function delete( DeleteCouponEntry $coupon ) {
310310
sprintf(
311311
"Failed to delete %s promotions from Merchant Center:\n%s",
312312
count( $invalid_promotions ),
313-
json_encode( $invalid_promotions )
313+
wp_json_encode( $invalid_promotions )
314314
),
315315
__METHOD__
316316
);
@@ -329,7 +329,7 @@ public function delete( DeleteCouponEntry $coupon ) {
329329
sprintf(
330330
"Deleted %s promoitons:\n%s",
331331
count( $deleted_promotions ),
332-
json_encode( $deleted_promotions )
332+
wp_json_encode( $deleted_promotions )
333333
),
334334
__METHOD__
335335
);

src/DB/Migration/Migration20211228T1640692399.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function apply(): void {
6565

6666
if ( isset( $mc_settings['offers_free_shipping'] ) && false !== boolval( $mc_settings['offers_free_shipping'] ) && isset( $mc_settings['free_shipping_threshold'] ) ) {
6767
// Move the free shipping threshold from the options to the shipping rate table.
68-
$options_json = json_encode( [ 'free_shipping_threshold' => (float) $mc_settings['free_shipping_threshold'] ] );
68+
$options_json = wp_json_encode( [ 'free_shipping_threshold' => (float) $mc_settings['free_shipping_threshold'] ] );
6969

7070
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
7171
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

src/DB/Query/ShippingRateQuery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function sanitize_value( string $column, $value ) {
4646
throw InvalidQuery::invalid_value( $column );
4747
}
4848

49-
$value = json_encode( $value );
49+
$value = wp_json_encode( $value );
5050
}
5151

5252
return $value;

src/DB/Table/AttributeMappingRulesTable.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AttributeMappingRulesTable extends Table {
2222
* @return string
2323
*/
2424
protected function get_install_query(): string {
25-
return <<< SQL
25+
return "
2626
CREATE TABLE `{$this->get_sql_safe_name()}` (
2727
`id` bigint(20) NOT NULL AUTO_INCREMENT,
2828
`attribute` varchar(255) NOT NULL,
@@ -31,7 +31,7 @@ protected function get_install_query(): string {
3131
`categories` text NOT NULL,
3232
PRIMARY KEY `id` (`id`)
3333
) {$this->get_collation()};
34-
SQL;
34+
";
3535
}
3636

3737
/**

src/DB/Table/BudgetRecommendationTable.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class BudgetRecommendationTable extends Table {
2929
* @return string
3030
*/
3131
protected function get_install_query(): string {
32-
return <<< SQL
32+
return "
3333
CREATE TABLE `{$this->get_sql_safe_name()}` (
3434
id bigint(20) NOT NULL AUTO_INCREMENT,
3535
currency varchar(3) NOT NULL,
@@ -38,7 +38,7 @@ protected function get_install_query(): string {
3838
PRIMARY KEY (id),
3939
UNIQUE KEY country_currency (country, currency)
4040
) {$this->get_collation()};
41-
SQL;
41+
";
4242
}
4343

4444
/**

src/DB/Table/MerchantIssueTable.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class MerchantIssueTable extends Table {
2323
* @return string
2424
*/
2525
protected function get_install_query(): string {
26-
return <<< SQL
26+
return "
2727
CREATE TABLE `{$this->get_sql_safe_name()}` (
2828
`id` bigint(20) NOT NULL AUTO_INCREMENT,
2929
`product_id` bigint(20) NOT NULL,
@@ -39,7 +39,7 @@ protected function get_install_query(): string {
3939
`created_at` datetime NOT NULL,
4040
PRIMARY KEY `id` (`id`)
4141
) {$this->get_collation()};
42-
SQL;
42+
";
4343
}
4444

4545
/**

src/DB/Table/ShippingRateTable.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ShippingRateTable extends Table {
2222
* @return string
2323
*/
2424
protected function get_install_query(): string {
25-
return <<< SQL
25+
return "
2626
CREATE TABLE `{$this->get_sql_safe_name()}` (
2727
id bigint(20) NOT NULL AUTO_INCREMENT,
2828
country varchar(2) NOT NULL,
@@ -33,7 +33,7 @@ protected function get_install_query(): string {
3333
KEY country (country),
3434
KEY currency (currency)
3535
) {$this->get_collation()};
36-
SQL;
36+
";
3737
}
3838

3939
/**

src/DB/Table/ShippingTimeTable.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ShippingTimeTable extends Table {
2222
* @return string
2323
*/
2424
protected function get_install_query(): string {
25-
return <<< SQL
25+
return "
2626
CREATE TABLE `{$this->get_sql_safe_name()}` (
2727
id bigint(20) NOT NULL AUTO_INCREMENT,
2828
country varchar(2) NOT NULL,
@@ -31,7 +31,7 @@ protected function get_install_query(): string {
3131
PRIMARY KEY (id),
3232
KEY country (country)
3333
) {$this->get_collation()};
34-
SQL;
34+
";
3535
}
3636

3737
/**

src/Jobs/ActionSchedulerJobMonitor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ protected function get_failure_timeframe(): int {
206206
* @since 1.7.0
207207
*/
208208
protected static function get_job_hash( string $hook, ?array $args = null ): string {
209-
return hash( 'crc32b', $hook . json_encode( $args ) );
209+
return hash( 'crc32b', $hook . wp_json_encode( $args ) );
210210
}
211211

212212
/**

src/Jobs/Notifications/AbstractItemNotificationJob.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function process_items( array $args ): void {
4444
} catch ( InvalidValue $e ) {
4545
do_action(
4646
'woocommerce_gla_error',
47-
sprintf( 'Error sending Notification for - Item ID: %s - Topic: %s - Data %s. Product was deleted from the database before the notification was sent.', $item, $topic, json_encode( $data ) ),
47+
sprintf( 'Error sending Notification for - Item ID: %s - Topic: %s - Data %s. Product was deleted from the database before the notification was sent.', $item, $topic, wp_json_encode( $data ) ),
4848
__METHOD__
4949
);
5050
}

src/MerchantCenter/MerchantStatuses.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ protected function refresh_account_issues(): void {
514514
$account_issues = array_map(
515515
function ( $issue ) {
516516
sort( $issue['applicable_countries'] );
517-
$issue['applicable_countries'] = json_encode(
517+
$issue['applicable_countries'] = wp_json_encode(
518518
array_unique(
519519
$issue['applicable_countries']
520520
)
@@ -567,7 +567,7 @@ function ( &$countries ) {
567567
ksort( $product_issues );
568568
$product_issues = array_map(
569569
function ( $unique_key, $issue ) {
570-
$issue['applicable_countries'] = json_encode( $this->product_issue_countries[ $unique_key ] );
570+
$issue['applicable_countries'] = wp_json_encode( $this->product_issue_countries[ $unique_key ] );
571571
return $issue;
572572
},
573573
array_keys( $product_issues ),

src/Notes/LeaveReviewActionTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function add_leave_review_note_action( NoteEntry $note ) {
2828
$note->add_action(
2929
'leave-review',
3030
__( 'Leave a review', 'google-listings-and-ads' ),
31-
rand( 0, 1 ) ? $wp_link : $wc_link
31+
wp_rand( 0, 1 ) ? $wp_link : $wc_link
3232
);
3333
}
3434
}

src/Product/BatchProductHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function validate_and_generate_update_request_entries( array $products ):
229229

230230
do_action(
231231
'woocommerce_gla_debug_message',
232-
sprintf( 'Skipping product (ID: %s) because it does not pass validation: %s', $product->get_id(), json_encode( $validation_result ) ),
232+
sprintf( 'Skipping product (ID: %s) because it does not pass validation: %s', $product->get_id(), wp_json_encode( $validation_result ) ),
233233
__METHOD__
234234
);
235235

src/Product/ProductSyncer.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function update_by_batch_requests( array $product_entries ): BatchProduct
149149
sprintf(
150150
"Submitted %s products:\n%s",
151151
count( $updated_products ),
152-
json_encode( $updated_products )
152+
wp_json_encode( $updated_products )
153153
),
154154
__METHOD__
155155
);
@@ -159,7 +159,7 @@ public function update_by_batch_requests( array $product_entries ): BatchProduct
159159
sprintf(
160160
"%s products failed to sync with Merchant Center:\n%s",
161161
count( $invalid_products ),
162-
json_encode( $invalid_products )
162+
wp_json_encode( $invalid_products )
163163
),
164164
__METHOD__
165165
);
@@ -235,7 +235,7 @@ public function delete_by_batch_requests( array $product_entries ): BatchProduct
235235
sprintf(
236236
"Deleted %s products:\n%s",
237237
count( $deleted_products ),
238-
json_encode( $deleted_products ),
238+
wp_json_encode( $deleted_products ),
239239
),
240240
__METHOD__
241241
);
@@ -245,7 +245,7 @@ public function delete_by_batch_requests( array $product_entries ): BatchProduct
245245
sprintf(
246246
"Failed to delete %s products from Merchant Center:\n%s",
247247
count( $invalid_products ),
248-
json_encode( $invalid_products )
248+
wp_json_encode( $invalid_products )
249249
),
250250
__METHOD__
251251
);

0 commit comments

Comments
 (0)