Skip to content

Commit 7922091

Browse files
committedFeb 4, 2015
Protect Module: refactor ip whitelist
improving data handling so that schema is uniform between the endpoints and the configuration screen.
1 parent cfc34c3 commit 7922091

File tree

4 files changed

+16
-30
lines changed

4 files changed

+16
-30
lines changed
 

‎json-endpoints/jetpack/json-api-jetpack-endpoints.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@
574574
'$site' => '(int|string) The site ID, The site domain'
575575
),
576576
'response_format' => array(
577-
'whitelist' => '(array) An array of formatted whitelists',
577+
'whitelist' => '(array) An array of formatted ip addresses',
578578
),
579579
'example_request_data' => array(
580580
'headers' => array(
@@ -593,11 +593,11 @@
593593
'$site' => '(int|string) The site ID, The site domain'
594594
),
595595
'request_format' => array(
596-
'whitelist' => '(string) A list of IP addresses',
596+
'whitelist' => '(array) A list of IP addresses',
597597
'global' => '(bool) Does this apply to this site only, or all connected Jetpack sites',
598598
),
599599
'response_format' => array(
600-
'whitelist' => '(array) An array of formatted whitelists',
600+
'whitelist' => '(array) An array of formatted ip addresses',
601601
),
602602
'example_request_data' => array(
603603
'headers' => array(

‎modules/protect.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,10 @@ public function check_use_math() {
359359
public function configuration_load() {
360360

361361
if ( isset( $_POST['action'] ) && $_POST['action'] == 'jetpack_protect_save_whitelist' && wp_verify_nonce( $_POST['_wpnonce'], 'jetpack-protect' ) ) {
362-
$this->whitelist_saved = jetpack_protect_save_whitelist( $_POST['whitelist'], $global = false );
363-
$this->whitelist_error = ! $this->whitelist_saved;
362+
$whitelist = str_replace( ' ', '', $_POST['whitelist'] );
363+
$whitelist = explode( PHP_EOL, $whitelist);
364+
$this->whitelist_saved = jetpack_protect_save_whitelist( $whitelist, $global = false );
365+
$this->whitelist_error = ! $this->whitelist_saved;
364366
}
365367

366368
// TODO: REMOVE THIS, IT'S FOR BETA TESTING ONLY

‎modules/protect/config-ui.php

+9-19
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,10 @@
4343

4444
<?php
4545
global $current_user;
46-
$editable_whitelist = jetpack_protect_format_whitelist( $this->whitelist );
47-
$current_user_global_whitelist = wp_list_filter( $this->whitelist, array( 'user_id' => $current_user->ID, 'global'=> true) );
48-
$other_user_whtielist = wp_list_filter( $this->whitelist, array( 'user_id' => $current_user->ID ), 'NOT' );
46+
$whitelist = jetpack_protect_format_whitelist( $this->whitelist );
4947
?>
5048

51-
<?php if ( ! empty( $current_user_global_whitelist ) || ! empty( $other_user_whtielist ) ) : // maybe show user's non-editable whitelists ?>
49+
<?php if ( ! empty( $whitelist['global'] ) || ! empty( $whitelist['other_user'] ) ) : // maybe show user's non-editable whitelists ?>
5250

5351
<table id="non-editable-whitelist" class="whitelist-table" cellpadding="0" cellspacing="0">
5452
<tr>
@@ -63,40 +61,32 @@
6361
</td>
6462
</tr>
6563
<tbody>
66-
<?php if ( ! empty( $current_user_global_whitelist ) ) : // show global whitelist ( only editable via wordpress.com ) ?>
64+
<?php if ( ! empty( $whitelist['global'] ) ) : // show global whitelist ( only editable via wordpress.com ) ?>
6765
<tr>
6866
<th class="heading">
6967
<?php _e( 'IP addresses on your global whitelist', 'jetpack'); ?>
7068
</th>
7169
</tr>
7270

73-
<?php foreach( $current_user_global_whitelist as $item ) : ?>
71+
<?php foreach( $whitelist['global'] as $item ) : ?>
7472
<tr>
7573
<td>
76-
<?php if( $item->range ) : ?>
77-
<?php echo $item->range_low; ?> &ndash; <?php echo $item->range_high; ?>
78-
<?php else: ?>
79-
<?php echo $item->ip_address; ?>
80-
<?php endif; ?>
74+
<?php echo $item; ?>
8175
</td>
8276
</tr>
8377
<?php endforeach; ?>
8478
<?php endif; // end global whitelist ?>
8579

86-
<?php if( ! empty( $other_user_whtielist ) ) : ?>
80+
<?php if( ! empty( $whitelist['other_user'] ) ) : ?>
8781
<tr>
8882
<th class="heading">
8983
<?php _e( 'IP addresses added by other users', 'jetpack' ); ?>
9084
</th>
9185
</tr>
92-
<?php foreach( $other_user_whtielist as $item ) : ?>
86+
<?php foreach( $whitelist['other_user'] as $item ) : ?>
9387
<tr>
9488
<td>
95-
<?php if( $item->range ) : ?>
96-
<?php echo $item->range_low; ?> &ndash; <?php echo $item->range_high; ?>
97-
<?php else: ?>
98-
<?php echo $item->ip_address; ?>
99-
<?php endif; ?>
89+
<?php echo $item; ?>
10090
</td>
10191
</tr>
10292
<?php endforeach; ?>
@@ -123,7 +113,7 @@
123113
</p>
124114
<?php wp_nonce_field( 'jetpack-protect' ); ?>
125115
<input type='hidden' name='action' value='jetpack_protect_save_whitelist' />
126-
<textarea name="whitelist"><?php esc_attr_e($editable_whitelist['local'], 'jetpack'); ?></textarea>
116+
<textarea name="whitelist"><?php echo implode( PHP_EOL, $whitelist['local'] ); ?></textarea>
127117
<p>
128118
<em><?php _e('IPv4 and IPv6 are acceptable. <br />To specify a range, enter the low value and high value separated by a dash. Example: 12.12.12.1-12.12.12.100', 'jetpack' ); ?></em>
129119
</p>

‎modules/protect/shared-functions.php

-6
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ function jetpack_protect_format_whitelist( $whitelist = null ) {
4343
}
4444
}
4545

46-
$formatted['local'] = implode( PHP_EOL, $formatted['local'] );
47-
$formatted['global'] = implode( PHP_EOL, $formatted['global'] );
48-
$formatted['other_user'] = implode( PHP_EOL, $formatted['other_user'] );
49-
5046
return $formatted;
5147
}
5248
}
@@ -55,8 +51,6 @@ function jetpack_protect_format_whitelist( $whitelist = null ) {
5551
function jetpack_protect_save_whitelist( $whitelist, $global ) {
5652
global $current_user;
5753
$whitelist_error = false;
58-
$whitelist = str_replace( ' ', '', $whitelist );
59-
$whitelist = explode( PHP_EOL, $whitelist);
6054
$new_items = array();
6155
$global = (bool) $global;
6256

0 commit comments

Comments
 (0)
Please sign in to comment.