1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
<?php
class Papi_Property_Group extends Papi_Property_Repeater {
public function format_value( $value, $slug, $post_id ) {
if ( ! is_array( $value ) ) {
return [];
}
$value = parent::format_value( $value, $slug, $post_id );
return array_shift( $value );
}
public function get_default_settings() {
return [
'items' => []
];
}
protected function get_settings_properties() {
$settings = $this->get_settings();
if ( is_null( $settings ) ) {
return [];
}
return array_filter( papi_to_array( $settings->items ), 'papi_is_property' );
}
public function html() {
$properties = $this->get_settings_properties();
$properties = $this->prepare_properties( $properties );
if ( $this->get_option( 'layout' ) === 'vertical' ) {
echo '<br />';
}
echo '<div class="papi-property-group">';
papi_render_properties( $properties );
echo '</div>';
}
protected function prepare_properties( $properties ) {
$result = [];
$value = $this->get_value();
$value = is_array( $value ) ? $value : [];
foreach ( $properties as $property ) {
$render_property = clone $property->get_options();
$value_slug = $property->get_slug( true );
if ( array_key_exists( $value_slug, $value ) ) {
$render_property->value = $value[$value_slug];
} else {
$render_property->value = null;
}
$render_property->slug = $this->html_name( $property, $this->counter );
$result[] = $render_property;
}
return $result;
}
}