Skip to content

Commit 67fc03c

Browse files
committed
Fixed phpstan and logic errors
1 parent bc317d8 commit 67fc03c

4 files changed

Lines changed: 27 additions & 12 deletions

File tree

includes/admin/class-tools-page.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,23 @@ public function render_page() {
167167
/* Sync funnel (run aggregation now) */
168168
if ( isset( $_POST['tptn_sync_funnel'] ) && check_admin_referer( 'tptn-tools-settings' ) ) {
169169
$result = Database::aggregate_visit_log();
170-
if ( $result ) {
170+
if ( true === $result ) {
171171
add_settings_error( 'tptn-notices', '', esc_html__( 'Funnel has been synced. Buffered visits have been aggregated.', 'top-10' ), 'updated' );
172-
} else {
173-
add_settings_error( 'tptn-notices', '', esc_html__( 'Nothing to sync. The funnel is empty or another sync is in progress.', 'top-10' ), 'updated' );
172+
} elseif ( 0 === $result ) {
173+
add_settings_error( 'tptn-notices', '', esc_html__( 'Nothing to sync. The funnel is empty.', 'top-10' ), 'updated' );
174+
} elseif ( false === $result ) {
175+
add_settings_error( 'tptn-notices', '', esc_html__( 'Sync skipped: another aggregation is already in progress. Please try again in a moment.', 'top-10' ), 'updated' );
176+
} elseif ( is_wp_error( $result ) ) {
177+
add_settings_error(
178+
'tptn-notices',
179+
'',
180+
sprintf(
181+
/* translators: %s: error message from the database */
182+
esc_html__( 'Sync failed: %s', 'top-10' ),
183+
esc_html( $result->get_error_message() )
184+
),
185+
'error'
186+
);
174187
}
175188
}
176189

@@ -563,6 +576,7 @@ public static function get_db_status_report() {
563576
printf(
564577
/* translators: %s: human-readable time difference */
565578
esc_html__( 'Next run: %s', 'top-10' ),
579+
/* translators: %s: human-readable time difference */
566580
esc_html( sprintf( __( 'in %s', 'top-10' ), human_time_diff( time(), $next_run ) ) )
567581
);
568582
?>

includes/admin/settings/class-settings-sanitize.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public function sanitize_sensitive_field( $value, $key ) {
283283
}
284284

285285
// If input is masked, return existing encrypted key.
286-
if ( is_string( $value ) && strpos( $value, '**' ) !== false ) {
286+
if ( strpos( $value, '**' ) !== false ) {
287287
return $stored_encrypted_key;
288288
}
289289

includes/class-database.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ public static function append_to_funnel( $post_id, $blog_id, $activate_counter =
716716
* @since 4.3.0
717717
*
718718
* @param int $batch_size Maximum funnel rows to process per run.
719-
* @return bool True if rows were processed, false if none found or lock not acquired.
719+
* @return true|false|int|\WP_Error True if rows processed, false if lock not acquired, 0 if funnel empty, WP_Error on DB failure.
720720
*/
721721
public static function aggregate_visit_log( $batch_size = 10000 ) {
722722
global $wpdb;
@@ -740,7 +740,7 @@ public static function aggregate_visit_log( $batch_size = 10000 ) {
740740
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
741741
if ( false === $wpdb->query( 'START TRANSACTION' ) ) {
742742
$wpdb->query( "SELECT RELEASE_LOCK('tptn_aggregation')" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
743-
return false;
743+
return new \WP_Error( 'tptn_transaction_failed', $wpdb->last_error ? $wpdb->last_error : __( 'Could not start transaction.', 'top-10' ) );
744744
}
745745
}
746746

@@ -751,7 +751,7 @@ public static function aggregate_visit_log( $batch_size = 10000 ) {
751751
if ( ! $is_sqlite ) {
752752
$wpdb->query( 'ROLLBACK' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
753753
}
754-
return false;
754+
return 0;
755755
}
756756

757757
$cap_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$funnel_table} ORDER BY id ASC LIMIT %d, 1", $batch_size ) );
@@ -777,7 +777,7 @@ public static function aggregate_visit_log( $batch_size = 10000 ) {
777777
if ( ! $is_sqlite ) {
778778
$wpdb->query( 'ROLLBACK' );
779779
}
780-
return false;
780+
return new \WP_Error( 'tptn_log_insert_failed', $wpdb->last_error ? $wpdb->last_error : __( 'Failed to copy visits to log table.', 'top-10' ) );
781781
}
782782

783783
$r = $wpdb->query(
@@ -798,7 +798,7 @@ public static function aggregate_visit_log( $batch_size = 10000 ) {
798798
if ( ! $is_sqlite ) {
799799
$wpdb->query( 'ROLLBACK' );
800800
}
801-
return false;
801+
return new \WP_Error( 'tptn_daily_insert_failed', $wpdb->last_error ? $wpdb->last_error : __( 'Failed to aggregate visits into daily table.', 'top-10' ) );
802802
}
803803

804804
$r = $wpdb->query(
@@ -818,22 +818,22 @@ public static function aggregate_visit_log( $batch_size = 10000 ) {
818818
if ( ! $is_sqlite ) {
819819
$wpdb->query( 'ROLLBACK' );
820820
}
821-
return false;
821+
return new \WP_Error( 'tptn_overall_insert_failed', $wpdb->last_error ? $wpdb->last_error : __( 'Failed to aggregate visits into overall table.', 'top-10' ) );
822822
}
823823

824824
$r = $wpdb->query( $wpdb->prepare( "DELETE FROM {$funnel_table} WHERE id <= %d", $max_id ) );
825825
if ( false === $r ) {
826826
if ( ! $is_sqlite ) {
827827
$wpdb->query( 'ROLLBACK' );
828828
}
829-
return false;
829+
return new \WP_Error( 'tptn_funnel_delete_failed', $wpdb->last_error ? $wpdb->last_error : __( 'Failed to drain funnel table.', 'top-10' ) );
830830
}
831831
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
832832

833833
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
834834
if ( ! $is_sqlite && false === $wpdb->query( 'COMMIT' ) ) {
835835
$wpdb->query( 'ROLLBACK' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
836-
return false;
836+
return new \WP_Error( 'tptn_commit_failed', $wpdb->last_error ? $wpdb->last_error : __( 'Transaction commit failed.', 'top-10' ) );
837837
}
838838

839839
do_action( 'tptn_count_updated', 0, 0, false );

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<exclude-pattern>*/index.asset.php</exclude-pattern>
1212
<exclude-pattern>*/freemius/*</exclude-pattern>
1313
<exclude-pattern>*/.playground/*</exclude-pattern>
14+
<exclude-pattern>phpstan-bootstrap.php</exclude-pattern>
1415

1516
<!-- Only check PHP files. -->
1617
<arg name="extensions" value="php"/>

0 commit comments

Comments
 (0)