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
<?php
class Papi_Porter_Driver_Core extends Papi_Porter_Driver {
protected $name = 'core';
protected $alias = 'papi';
public function get_value( array $options = [] ) {
if ( ! empty( $options['post_id'] ) ) {
$options['meta_id'] = $options['post_id'];
}
if ( ! isset( $options['meta_id'] ) || ! is_int( $options['meta_id'] ) ) {
throw new InvalidArgumentException(
'Missing `meta_id` option. Should be int.'
);
}
if ( ! isset( $options['property'] ) || $options['property'] instanceof Papi_Core_Property === false ) {
throw new InvalidArgumentException(
'Missing `property` option. Should be instance of `Papi_Core_Property`.'
);
}
if ( ! isset( $options['slug'] ) || empty( $options['slug'] ) ) {
$options['slug'] = $options['property']->get_slug( true );
}
if ( ! isset( $options['value'] ) ) {
throw new InvalidArgumentException( 'Missing `value` option.' );
}
$value = $this->call_value( $options['value'] );
$value = $this->update_value(
$options['property'],
$value,
$options['slug'],
$options['meta_id'],
isset( $options['meta_type'] ) ? $options['meta_type'] : 'post'
);
return papi_maybe_json_decode( maybe_unserialize( $value ) );
}
protected function update_value( $property, $value, $slug, $meta_id, $meta_type = 'post' ) {
if ( ! is_array( $value ) || ! $this->should_update_array( $slug ) ) {
return $property->import_value( $value, $slug, $meta_id );
}
$old = papi_get_field( $meta_id, $slug, [], null, $meta_type );
$value = array_merge( $old, $value );
$value = $property->import_value( $value, $slug, $meta_id, $meta_type );
if ( $property->import_setting( 'property_array_slugs' ) ) {
return papi_from_property_array_slugs( $value, $slug );
}
return $property->update_value( $value, $slug, $meta_id, $meta_type );
}
}