jetpack_protect_save_whitelist

Jetpack Protect Save Whitelist.

Description

jetpack_protect_save_whitelist( $whitelist, (bool) $global = false ); 

Parameters (2)

0. $whitelist
The whitelist.
1. $global — Optional. (bool) => false
The global.

Usage

  1. if ( !function_exists( 'jetpack_protect_save_whitelist' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/modules/protect/shared-functions.php'; 
  3.  
  4. // The whitelist. 
  5. $whitelist = null; 
  6.  
  7. // The global. 
  8. $global = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = jetpack_protect_save_whitelist($whitelist, $global); 
  12.  

Defined (1)

The function is defined in the following location(s).

/modules/protect/shared-functions.php  
  1. function jetpack_protect_save_whitelist( $whitelist, $global = false ) { 
  2. $whitelist_error = false; 
  3. $new_items = array (); 
  4.  
  5. if ( ! is_array( $whitelist ) ) { 
  6. return new WP_Error( 'invalid_parameters', __( 'Expecting an array', jetpack ) ); 
  7.  
  8. if ( $global && ! is_multisite() ) { 
  9. return new WP_Error( 'invalid_parameters', __( 'Cannot use global flag on non-multisites', jetpack ) ); 
  10.  
  11. if ( $global && ! current_user_can( 'manage_network' ) ) { 
  12. return new WP_Error( 'permission_denied', __( 'Only super admins can edit the global whitelist', jetpack ) ); 
  13.  
  14. // validate each item 
  15. foreach ( $whitelist as $item ) { 
  16.  
  17. $item = trim( $item ); 
  18.  
  19. if ( empty( $item ) ) { 
  20. continue; 
  21.  
  22. $range = false; 
  23. if ( strpos( $item, '-' ) ) { 
  24. $item = explode( '-', $item ); 
  25. $range = true; 
  26. $new_item = new stdClass(); 
  27. $new_item->range = $range; 
  28.  
  29. if ( ! empty( $range ) ) { 
  30.  
  31. $low = trim( $item[0] ); 
  32. $high = trim( $item[1] ); 
  33.  
  34. if ( ! filter_var( $low, FILTER_VALIDATE_IP ) || ! filter_var( $high, FILTER_VALIDATE_IP ) ) { 
  35. $whitelist_error = true; 
  36. break; 
  37.  
  38. if ( ! jetpack_convert_ip_address( $low ) || ! jetpack_convert_ip_address( $high ) ) { 
  39. $whitelist_error = true; 
  40. break; 
  41.  
  42. $new_item->range_low = $low; 
  43. $new_item->range_high = $high; 
  44.  
  45. } else { 
  46.  
  47. if ( ! filter_var( $item, FILTER_VALIDATE_IP ) ) { 
  48. $whitelist_error = true; 
  49. break; 
  50.  
  51. if ( ! jetpack_convert_ip_address( $item ) ) { 
  52. $whitelist_error = true; 
  53. break; 
  54. $new_item->ip_address = $item; 
  55.  
  56. $new_items[] = $new_item; 
  57.  
  58. } // end item loop 
  59.  
  60. if ( ! empty( $whitelist_error ) ) { 
  61. return new WP_Error( 'invalid_ip', __( 'One of your IP addresses was not valid.', jetpack ) ); 
  62.  
  63. if ( $global ) { 
  64. update_site_option( 'jetpack_protect_global_whitelist', $new_items ); 
  65. // once a user has saved their global whitelist, we can permanently remove the legacy option 
  66. delete_site_option( 'jetpack_protect_whitelist' ); 
  67. } else { 
  68. Jetpack_Options::update_option( 'protect_whitelist', $new_items ); 
  69.  
  70. return true;