Example #1
0
 /**
  * Gets one or more components.
  *
  * This is a helper function that collaborates with `get_component_results()`.
  *
  * @since 0.5.0
  * @see WPDLib\Components\Manager::get_components_results()
  * @param string $component_path the component path specifying which component(s) you want to retrieve
  * @param WPDLib\Components\Base $curr the current component to iterate through
  * @param string $class_path the class path specifying class restrictions (optional, default is an empty string)
  * @return array returns an array of components
  */
 private static function get_components_recursive($component_path, $curr, $class_path = '')
 {
     $component_path = explode('.', $component_path, 2);
     $class_path = explode('.', $class_path, 2);
     $current_class_path = isset($class_path[0]) ? $class_path[0] : '*';
     $current_children = $curr->get_children($current_class_path);
     $class_path_children = isset($class_path[1]) ? $class_path[1] : '';
     return self::get_components_results($component_path, $current_children, $class_path_children);
 }
 /**
  * This function registers a related objects field if applicable for the field parameters.
  *
  * This is a utility function that should only be called from a field component
  * that can provide the required parameters.
  *
  * A field that is considered a related objects field must have a type of either
  * 'radio', 'multibox', 'select' or 'multiselect' and its 'options' argument must contain
  * only one element that has the key 'posts', 'terms' or 'users'.
  *
  * @since 0.6.0
  * @param WPDLib\FieldTypes\Base $object the field type object
  * @param array $args arguments for the field component
  * @param WPDLib\Components\Base $component the field component
  * @param WPDLib\Components\Base $component_parent the field component's parent component
  */
 public static function maybe_register_related_objects_field($object, $args, $component, $component_parent)
 {
     if (!isset($args['options']) || !is_array($args['options']) || 1 !== count($args['options'])) {
         return;
     }
     if (!is_a($object, 'WPDLib\\FieldTypes\\Radio')) {
         return;
     }
     $available_modes = array('posts', 'terms', 'users');
     $property = '';
     $value = array();
     foreach ($available_modes as $mode) {
         if (isset($args['options'][$mode])) {
             $property = 'related_' . $mode . '_fields';
             if (!$args['options'][$mode] || 'any' === $args['options'][$mode]) {
                 $value[$component->slug] = array();
             } else {
                 $value[$component->slug] = (array) $args['options'][$mode];
             }
             break;
         }
     }
     if (!$property) {
         return;
     }
     $component_grandparent = $component_parent->get_parent();
     if (!$component_grandparent) {
         return;
     }
     $component_grandparent->{$property} = array_merge($component_grandparent->{$property}, $value);
 }