Skip to content

Commit 2854c73

Browse files
jazzsequencepwtyler
authored andcommitted
Add pantheon-wp-coding-standards (#247)
* use pantheon coding standards exclude vip minimum which flags a lot of session-related code * add phpcbf script and run it * remove empty return statement per phpcs * set the linting box to php8.0
1 parent 776840b commit 2854c73

11 files changed

Lines changed: 505 additions & 178 deletions

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
lint:
2222
working_directory: ~/pantheon-systems/wp-native-php-sessions
2323
docker:
24-
- image: quay.io/pantheon-public/build-tools-ci:8.x-php8.2
24+
- image: quay.io/pantheon-public/build-tools-ci:8.x-php8.0
2525
steps:
2626
- checkout
2727
- restore_cache:

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
],
1616
"require-dev": {
1717
"pantheon-systems/pantheon-wordpress-upstream-tests": "dev-master",
18-
"wp-coding-standards/wpcs": "dev-develop as 2.3.1",
19-
"dealerdirect/phpcodesniffer-composer-installer": "^1.0.0",
18+
"pantheon-systems/pantheon-wp-coding-standards": "^1.0",
2019
"phpunit/phpunit": "^9",
21-
"phpcompatibility/php-compatibility": "^9.3",
2220
"yoast/phpunit-polyfills": "^1.0"
2321
},
2422
"scripts": {
2523
"lint": "@phpcs",
2624
"phpcs": "vendor/bin/phpcs",
25+
"phpcbf": "vendor/bin/phpcbf",
2726
"phpunit": "vendor/bin/phpunit",
2827
"test": "@phpunit"
2928
},

composer.lock

Lines changed: 455 additions & 121 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inc/class-admin.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ public static function get_instance() {
4444
*/
4545
private function setup_actions() {
4646

47-
add_action( 'admin_menu', array( $this, 'action_admin_menu' ) );
48-
add_action( 'wp_ajax_pantheon_clear_session', array( $this, 'handle_clear_session' ) );
47+
add_action( 'admin_menu', [ $this, 'action_admin_menu' ] );
48+
add_action( 'wp_ajax_pantheon_clear_session', [ $this, 'handle_clear_session' ] );
4949
}
5050

5151
/**
5252
* Register the admin menu
5353
*/
5454
public function action_admin_menu() {
5555

56-
add_management_page( __( 'Pantheon Sessions', 'wp-native-php-sessions' ), __( 'Sessions', 'wp-native-php-sessions' ), self::$capability, 'pantheon-sessions', array( $this, 'handle_page' ) );
56+
add_management_page( __( 'Pantheon Sessions', 'wp-native-php-sessions' ), __( 'Sessions', 'wp-native-php-sessions' ), self::$capability, 'pantheon-sessions', [ $this, 'handle_page' ] );
5757
}
5858

5959
/**
@@ -68,16 +68,16 @@ public function handle_page() {
6868
echo '<div class="wrap">';
6969

7070
echo '<div>';
71-
$query_args = array(
71+
$query_args = [
7272
'action' => 'pantheon_clear_session',
7373
'nonce' => wp_create_nonce( 'pantheon_clear_session' ),
7474
'session' => 'all',
75-
);
75+
];
7676
if ( $wpdb->get_var( "SELECT COUNT(session_id) FROM $wpdb->pantheon_sessions" ) ) {
7777
echo '<a class="button pantheon-clear-all-sessions" style="float:right; margin-top: 9px;" href="' . esc_url( add_query_arg( $query_args, admin_url( 'admin-ajax.php' ) ) ) . '">' . esc_html__( 'Clear All', 'wp-native-php-sessions' ) . '</a>';
7878
}
7979
echo '<h2>' . esc_html__( 'Pantheon Sessions', 'wp-native-php-sessions' ) . '</h2>';
80-
if ( isset( $_GET['message'] ) && in_array( $_GET['message'], array( 'delete-all-session', 'delete-session' ), true ) ) {
80+
if ( isset( $_GET['message'] ) && in_array( $_GET['message'], [ 'delete-all-session', 'delete-session' ], true ) ) {
8181
if ( 'delete-all-session' === $_GET['message'] ) {
8282
$message = __( 'Cleared all sessions.', 'wp-native-php-sessions' );
8383
} elseif ( 'delete-session' === $_GET['message'] ) {
@@ -93,7 +93,7 @@ public function handle_page() {
9393

9494
echo '</div>';
9595

96-
add_action( 'admin_footer', array( $this, 'action_admin_footer' ) );
96+
add_action( 'admin_footer', [ $this, 'action_admin_footer' ] );
9797
}
9898

9999
/**

inc/class-cli-command.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ public function list_( $args, $assoc_args ) {
2929
WP_CLI::error( 'Pantheon Sessions is currently disabled.' );
3030
}
3131

32-
$defaults = array(
32+
$defaults = [
3333
'format' => 'table',
3434
'fields' => 'session_id,user_id,datetime,ip_address,data',
35-
);
35+
];
3636
$assoc_args = array_merge( $defaults, $assoc_args );
3737

38-
$sessions = array();
38+
$sessions = [];
3939
foreach ( new \WP_CLI\Iterators\Query( "SELECT * FROM {$wpdb->pantheon_sessions} ORDER BY datetime DESC" ) as $row ) {
4040
$sessions[] = $row;
4141
}

inc/class-list-table.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public function prepare_items() {
1919
global $wpdb;
2020

2121
$columns = $this->get_columns();
22-
$hidden = array();
23-
$sortable = array();
24-
$this->_column_headers = array( $columns, $hidden, $sortable );
22+
$hidden = [];
23+
$sortable = [];
24+
$this->_column_headers = [ $columns, $hidden, $sortable ];
2525

2626
$per_page = 20;
2727
$paged = ( isset( $_GET['paged'] ) ) ? (int) $_GET['paged'] : 1;
@@ -31,10 +31,10 @@ public function prepare_items() {
3131
$total_items = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->pantheon_sessions" );
3232

3333
$this->set_pagination_args(
34-
array(
34+
[
3535
'total_items' => $total_items,
3636
'per_page' => $per_page,
37-
)
37+
]
3838
);
3939
}
4040

@@ -49,13 +49,13 @@ public function no_items() {
4949
* Get the columns in the list table
5050
*/
5151
public function get_columns() {
52-
return array(
52+
return [
5353
'session_id' => __( 'Session ID', 'wp-native-php-sessions' ),
5454
'user_id' => __( 'User ID', 'wp-native-php-sessions' ),
5555
'ip_address' => __( 'IP Address', 'wp-native-php-sessions' ),
5656
'datetime' => __( 'Last Active', 'wp-native-php-sessions' ),
5757
'data' => __( 'Data', 'wp-native-php-sessions' ),
58-
);
58+
];
5959
}
6060

6161
/**
@@ -68,14 +68,14 @@ public function column_default( $item, $column_name ) {
6868
if ( 'data' === $column_name ) {
6969
return '<code>' . esc_html( $item->data ) . '</code>';
7070
} elseif ( 'session_id' === $column_name ) {
71-
$query_args = array(
71+
$query_args = [
7272
'action' => 'pantheon_clear_session',
7373
'nonce' => wp_create_nonce( 'pantheon_clear_session' ),
7474
'session' => $item->session_id,
75-
);
76-
$actions = array(
75+
];
76+
$actions = [
7777
'clear' => '<a href="' . esc_url( add_query_arg( $query_args, admin_url( 'admin-ajax.php' ) ) ) . '">' . esc_html__( 'Clear', 'wp-native-php-sessions' ) . '</a>',
78-
);
78+
];
7979
return esc_html( $item->session_id ) . $this->row_actions( $actions );
8080
} elseif ( 'datetime' === $column_name ) {
8181
// translators: Time ago.

inc/class-session.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ class Session {
1717
*
1818
* @var array
1919
*/
20-
private static $sessions = array();
20+
private static $sessions = [];
2121

2222
/**
2323
* Any secure sessions stored statically.
2424
*
2525
* @var array
2626
*/
27-
private static $secure_sessions = array();
27+
private static $secure_sessions = [];
2828

2929
/**
3030
* Session id.
@@ -84,10 +84,10 @@ public static function create_for_sid( $sid ) {
8484

8585
$wpdb = self::restore_wpdb_if_null( $wpdb );
8686

87-
$insert_data = array(
87+
$insert_data = [
8888
'session_id' => $sid,
8989
'user_id' => (int) get_current_user_id(),
90-
);
90+
];
9191
if ( function_exists( 'is_ssl' ) && is_ssl() ) {
9292
$insert_data['secure_session_id'] = $sid;
9393
}
@@ -148,10 +148,10 @@ public function set_user_id( $user_id ) {
148148
$this->user_id = (int) $user_id;
149149
$wpdb->update(
150150
$wpdb->pantheon_sessions,
151-
array(
151+
[
152152
'user_id' => $this->user_id,
153-
),
154-
array( self::get_session_id_column() => $this->get_id() )
153+
],
154+
[ self::get_session_id_column() => $this->get_id() ]
155155
);
156156
}
157157

@@ -175,13 +175,13 @@ public function set_data( $data ) {
175175

176176
$wpdb->update(
177177
$wpdb->pantheon_sessions,
178-
array(
178+
[
179179
'user_id' => (int) get_current_user_id(),
180180
'datetime' => gmdate( 'Y-m-d H:i:s' ),
181181
'ip_address' => self::get_client_ip_server(),
182182
'data' => maybe_serialize( $data ),
183-
),
184-
array( self::get_session_id_column() => $this->get_id() )
183+
],
184+
[ self::get_session_id_column() => $this->get_id() ]
185185
);
186186

187187
$this->data = maybe_serialize( $data );
@@ -197,14 +197,14 @@ public static function get_client_ip_server() {
197197
$ip_address = apply_filters( 'pantheon_sessions_client_ip_default', '127.0.0.1' );
198198
$ip_source = null;
199199

200-
$keys = array(
200+
$keys = [
201201
'HTTP_CLIENT_IP',
202202
'HTTP_X_FORWARDED_FOR',
203203
'HTTP_X_FORWARDED',
204204
'HTTP_FORWARDED_FOR',
205205
'HTTP_FORWARDED',
206206
'REMOTE_ADDR',
207-
);
207+
];
208208

209209
$ip_filter_flags = apply_filters( 'pantheon_sessions_client_ip_filter_flags', FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE );
210210

@@ -243,10 +243,10 @@ public function destroy() {
243243

244244
$wpdb = self::restore_wpdb_if_null( $wpdb );
245245

246-
$wpdb->delete( $wpdb->pantheon_sessions, array( self::get_session_id_column() => $this->get_id() ) );
246+
$wpdb->delete( $wpdb->pantheon_sessions, [ self::get_session_id_column() => $this->get_id() ] );
247247

248248
// Reset $_SESSION to prevent a new session from being started.
249-
$_SESSION = array();
249+
$_SESSION = [];
250250

251251
$this->delete_cookies();
252252
}
@@ -280,11 +280,11 @@ private function delete_cookies() {
280280
}
281281

282282
$session_name = session_name();
283-
$cookies = array(
283+
$cookies = [
284284
$session_name,
285285
substr( $session_name, 1 ),
286286
'S' . $session_name,
287-
);
287+
];
288288

289289
foreach ( $cookies as $cookie_name ) {
290290

pantheon-sessions.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ private function load() {
6262
$this->setup_database();
6363
$this->initialize_session_override();
6464
$this->set_ini_values();
65-
add_action( 'set_logged_in_cookie', array( __CLASS__, 'action_set_logged_in_cookie' ), 10, 4 );
66-
add_action( 'clear_auth_cookie', array( __CLASS__, 'action_clear_auth_cookie' ) );
65+
add_action( 'set_logged_in_cookie', [ __CLASS__, 'action_set_logged_in_cookie' ], 10, 4 );
66+
add_action( 'clear_auth_cookie', [ __CLASS__, 'action_clear_auth_cookie' ] );
6767
}
6868
}
6969

@@ -239,10 +239,7 @@ public static function force_first_load() {
239239
update_option( 'active_plugins', $plugins );
240240
}
241241
}
242-
243-
return;
244242
}
245-
246243
}
247244

248245
/**

phpcs.xml.dist

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
<!-- Show progress and sniff codes in all reports -->
1010
<arg value="ps"/>
1111

12-
<rule ref="WordPress-Core" />
12+
<rule ref="Pantheon-WP">
13+
<!-- Ignore all of the VIP rules. These mostly deal with sessions, and that's explicitly what this plugin does. -->
14+
<exclude name="WordPressVIPMinimum" />
15+
</rule>
1316
<rule ref="WordPress-Docs" />
1417
<rule ref="PHPCompatibility"/>
1518

@@ -33,10 +36,4 @@
3336
<rule ref="Squiz.Commenting.FunctionComment.MissingParamTag">
3437
<exclude-pattern>*/inc/class-cli-command.php</exclude-pattern>
3538
</rule>
36-
37-
<!-- Excluded sniffs -->
38-
<rule ref="Universal.Operators.DisallowShortTernary.Found">
39-
<exclude-pattern>*/inc/*</exclude-pattern>
40-
<exclude-pattern>pantheon-sessions.php</exclude-pattern>
41-
</rule>
4239
</ruleset>

tests/phpunit/test-init-plugin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ public function test_database_created() {
3030
$column_data = $wpdb->get_results( "SHOW COLUMNS FROM {$table_name}" );
3131
$columns = wp_list_pluck( $column_data, 'Field' );
3232
$this->assertEquals(
33-
array(
33+
[
3434
'id',
3535
'user_id',
3636
'session_id',
3737
'secure_session_id',
3838
'ip_address',
3939
'datetime',
4040
'data',
41-
),
41+
],
4242
$columns
4343
);
4444
}

0 commit comments

Comments
 (0)