* * @access public * * @param array $config Full sync configuration for this sync module. * * @return array Number of items yet to be enqueued. */ public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable return 1; } /** * Retrieve the actions that will be sent for this module during a full sync. * * @access public * * @return array Full sync actions of this module. */ public function get_full_sync_actions() { return array( 'jetpack_full_sync_constants' ); } /** * Sync the constants if we're supposed to. * * @access public */ public function maybe_sync_constants() { if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) { return; } set_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME, microtime( true ), Defaults::$default_sync_constants_wait_time ); $constants = $this->get_all_constants(); if ( empty( $constants ) ) { return; } $constants_checksums = (array) get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, array() ); foreach ( $constants as $name => $value ) { $checksum = $this->get_check_sum( $value ); // Explicitly not using Identical comparison as get_option returns a string. if ( ! $this->still_valid_checksum( $constants_checksums, $name, $checksum ) && $value !== null ) { /** * Tells the client to sync a constant to the server * * @param string The name of the constant * @param mixed The value of the constant * * @since 1.6.3 * @since-jetpack 4.2.0 */ do_action( 'jetpack_sync_constant', $name, $value ); $constants_checksums[ $name ] = $checksum; } else { $constants_checksums[ $name ] = $checksum; } } update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums ); } /** * Retrieve all constants as per the current constants whitelist. * Public so that we don't have to store an option for each constant. * * @access public * * @return array All constants. */ public function get_all_constants() { $constants_whitelist = $this->get_constants_whitelist(); return array_combine( $constants_whitelist, array_map( array( $this, 'get_constant' ), $constants_whitelist ) ); } /** * Retrieve the value of a constant. * Used as a wrapper to standartize access to constants. * * @access private * * @param string $constant Constant name. * * @return mixed Return value of the constant. */ private function get_constant( $constant ) { return ( defined( $constant ) ) ? constant( $constant ) : null; } /** * Expand the constants within a hook before they are serialized and sent to the server. * * @access public * * @param array $args The hook parameters. * * @return array $args The hook parameters. */ public function expand_constants( $args ) { if ( $args[0] ) { $constants = $this->get_all_constants(); $constants_checksums = array(); foreach ( $constants as $name => $value ) { $constants_checksums[ $name ] = $this->get_check_sum( $value ); } update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums ); return $constants; } return $args; } /** * Return Total number of objects. * * @param array $config Full Sync config. * * @return int total */ public function total( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable return count( $this->get_constants_whitelist() ); } /** * Retrieve a set of constants by their IDs. * * @access public * * @param string $object_type Object type. * @param array $ids Object IDs. * @return array Array of objects. */ public function get_objects_by_id( $object_type, $ids ) { if ( empty( $ids ) || empty( $object_type ) || 'constant' !== $object_type ) { return array(); } $objects = array(); foreach ( (array) $ids as $id ) { $object = $this->get_object_by_id( $object_type, $id ); if ( 'all' === $id ) { // If all was requested it contains all options and can simply be returned. return $object; } $objects[ $id ] = $object; } return $objects; } /** * Retrieve a constant by its name. * * @access public * * @param string $object_type Type of the sync object. * @param string $id ID of the sync object. * @return mixed Value of Constant. */ public function get_object_by_id( $object_type, $id ) { if ( 'constant' === $object_type ) { // Only whitelisted constants can be returned. if ( in_array( $id, $this->get_constants_whitelist(), true ) ) { return $this->get_constant( $id ); } elseif ( 'all' === $id ) { return $this->get_all_constants(); } } return false; } }