class.jetpack-sync-module-constants.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. require_once dirname( __FILE__ ) . '/class.jetpack-sync-defaults.php';
  3. class Jetpack_Sync_Module_Constants extends Jetpack_Sync_Module {
  4. const CONSTANTS_CHECKSUM_OPTION_NAME = 'jetpack_constants_sync_checksum';
  5. const CONSTANTS_AWAIT_TRANSIENT_NAME = 'jetpack_sync_constants_await';
  6. public function name() {
  7. return 'constants';
  8. }
  9. public function init_listeners( $callable ) {
  10. add_action( 'jetpack_sync_constant', $callable, 10, 2 );
  11. }
  12. public function init_full_sync_listeners( $callable ) {
  13. add_action( 'jetpack_full_sync_constants', $callable );
  14. }
  15. public function init_before_send() {
  16. add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_constants' ) );
  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. *
  34. * @since 4.2.0
  35. *
  36. * @param boolean Whether to expand constants (should always be true)
  37. */
  38. do_action( 'jetpack_full_sync_constants', true );
  39. // The number of actions enqueued, and next module state (true == done)
  40. return array( 1, true );
  41. }
  42. function estimate_full_sync_actions( $config ) {
  43. return 1;
  44. }
  45. function get_full_sync_actions() {
  46. return array( 'jetpack_full_sync_constants' );
  47. }
  48. function maybe_sync_constants() {
  49. if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) {
  50. return;
  51. }
  52. set_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_constants_wait_time );
  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. foreach ( $constants as $name => $value ) {
  59. $checksum = $this->get_check_sum( $value );
  60. // explicitly not using Identical comparison as get_option returns a string
  61. if ( ! $this->still_valid_checksum( $constants_checksums, $name, $checksum ) && ! is_null( $value ) ) {
  62. /**
  63. * Tells the client to sync a constant to the server
  64. *
  65. * @since 4.2.0
  66. *
  67. * @param string The name of the constant
  68. * @param mixed The value of the constant
  69. */
  70. do_action( 'jetpack_sync_constant', $name, $value );
  71. $constants_checksums[ $name ] = $checksum;
  72. } else {
  73. $constants_checksums[ $name ] = $checksum;
  74. }
  75. }
  76. update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums );
  77. }
  78. // public so that we don't have to store an option for each constant
  79. function get_all_constants() {
  80. $constants_whitelist = $this->get_constants_whitelist();
  81. return array_combine(
  82. $constants_whitelist,
  83. array_map( array( $this, 'get_constant' ), $constants_whitelist )
  84. );
  85. }
  86. private function get_constant( $constant ) {
  87. return ( defined( $constant ) ) ?
  88. constant( $constant )
  89. : null;
  90. }
  91. public function expand_constants( $args ) {
  92. if ( $args[0] ) {
  93. $constants = $this->get_all_constants();
  94. $constants_checksums = array();
  95. foreach ( $constants as $name => $value ) {
  96. $constants_checksums[ $name ] = $this->get_check_sum( $value );
  97. }
  98. update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums );
  99. return $constants;
  100. }
  101. return $args;
  102. }
  103. }