Jetpack_Sync_Module_Constants

The WordPress Core Jetpack Sync Module Constants class.

Defined (1)

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

/sync/class.jetpack-sync-module-constants.php  
  1. class Jetpack_Sync_Module_Constants extends Jetpack_Sync_Module { 
  2. const CONSTANTS_CHECKSUM_OPTION_NAME = 'jetpack_constants_sync_checksum'; 
  3. const CONSTANTS_AWAIT_TRANSIENT_NAME = 'jetpack_sync_constants_await'; 
  4.  
  5. public function name() { 
  6. return 'constants'; 
  7.  
  8. public function init_listeners( $callable ) { 
  9. add_action( 'jetpack_sync_constant', $callable, 10, 2 ); 
  10.  
  11. public function init_full_sync_listeners( $callable ) { 
  12. add_action( 'jetpack_full_sync_constants', $callable ); 
  13.  
  14. public function init_before_send() { 
  15. add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_constants' ) ); 
  16.  
  17. // full sync 
  18. add_filter( 'jetpack_sync_before_send_jetpack_full_sync_constants', array( $this, 'expand_constants' ) ); 
  19.  
  20. public function reset_data() { 
  21. delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME ); 
  22. delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ); 
  23.  
  24. function set_constants_whitelist( $constants ) { 
  25. $this->constants_whitelist = $constants; 
  26.  
  27. function get_constants_whitelist() { 
  28. return Jetpack_Sync_Defaults::get_constants_whitelist(); 
  29.  
  30. function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { 
  31. /** 
  32. * Tells the client to sync all constants to the server 
  33. * @since 4.2.0 
  34. * @param boolean Whether to expand constants (should always be true) 
  35. */ 
  36. do_action( 'jetpack_full_sync_constants', true ); 
  37.  
  38. // The number of actions enqueued, and next module state (true == done) 
  39. return array( 1, true ); 
  40.  
  41. function estimate_full_sync_actions( $config ) { 
  42. return 1; 
  43.  
  44. function get_full_sync_actions() { 
  45. return array( 'jetpack_full_sync_constants' ); 
  46.  
  47. function maybe_sync_constants() { 
  48. if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) { 
  49. return; 
  50.  
  51. set_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_constants_wait_time ); 
  52.  
  53. $constants = $this->get_all_constants(); 
  54. if ( empty( $constants ) ) { 
  55. return; 
  56.  
  57. $constants_checksums = (array) get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, array() ); 
  58.  
  59. foreach ( $constants as $name => $value ) { 
  60. $checksum = $this->get_check_sum( $value ); 
  61. // explicitly not using Identical comparison as get_option returns a string 
  62. if ( ! $this->still_valid_checksum( $constants_checksums, $name, $checksum ) && ! is_null( $value ) ) { 
  63. /** 
  64. * Tells the client to sync a constant to the server 
  65. * @since 4.2.0 
  66. * @param string The name of the constant 
  67. * @param mixed The value of the constant 
  68. */ 
  69. do_action( 'jetpack_sync_constant', $name, $value ); 
  70. $constants_checksums[ $name ] = $checksum; 
  71. } else { 
  72. $constants_checksums[ $name ] = $checksum; 
  73. update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums ); 
  74.  
  75. // public so that we don't have to store an option for each constant 
  76. function get_all_constants() { 
  77. $constants_whitelist = $this->get_constants_whitelist(); 
  78. return array_combine( 
  79. $constants_whitelist,  
  80. array_map( array( $this, 'get_constant' ), $constants_whitelist ) 
  81. ); 
  82.  
  83. private function get_constant( $constant ) { 
  84. return ( defined( $constant ) ) ? 
  85. constant( $constant ) 
  86. : null; 
  87.  
  88. public function expand_constants( $args ) { 
  89. if ( $args[0] ) { 
  90. return $this->get_all_constants(); 
  91.  
  92. return $args;