wp-customize-utils.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. *
  4. * Assign a value to an object at the given location. Create the nested objects if they aren't already available.
  5. *
  6. * @param object $target The object to assign the value to
  7. * @param array $array The array describing the location of the property to update
  8. * @param object $value The value to assign
  9. * @return object The modified $target object with $value assigned where $array describes
  10. *
  11. */
  12. function set_settings_array( $target, $array, $value ) {
  13. $key = array_shift( $array );
  14. $current =& $target;
  15. while ( 0 < sizeof( $array ) ) {
  16. if ( ! property_exists( $current, $key ) ) {
  17. $current->{ $key } = (object) array();
  18. }
  19. $current =& $current->{ $key };
  20. $key = array_shift( $array );
  21. }
  22. $current->{ $key } = $value;
  23. return $target;
  24. }
  25. /**
  26. *
  27. * Get a value from an object at the given location.
  28. *
  29. * @param array $array The array describing the location of the property to update.
  30. * @param object $object The object.
  31. * @return object The value at the location.
  32. *
  33. */
  34. function get_settings_array( $array, $object ) {
  35. foreach( $array as $property ) {
  36. $object = $object[ $property ];
  37. }
  38. return $object;
  39. }