1: <?php
2:
3: 4: 5:
6: class Papi_Porter_Driver_Core extends Papi_Porter_Driver {
7:
8: 9: 10: 11: 12:
13: protected $name = 'core';
14:
15: 16: 17: 18: 19:
20: protected $alias = 'papi';
21:
22: 23: 24: 25: 26: 27: 28: 29: 30:
31: public function get_value( array $options = [] ) {
32: if ( ! isset( $options['post_id'] ) || ! is_int( $options['post_id'] ) ) {
33: throw new InvalidArgumentException(
34: 'Missing `post_id` option. Should be int.'
35: );
36: }
37:
38: if ( ! isset( $options['property'] ) || $options['property'] instanceof Papi_Core_Property === false ) {
39: throw new InvalidArgumentException(
40: 'Missing `property` option. Should be instance of `Papi_Core_Property`.'
41: );
42: }
43:
44: if ( ! isset( $options['slug'] ) || empty( $options['slug'] ) ) {
45: $options['slug'] = $options['property']->get_slug( true );
46: }
47:
48: if ( ! isset( $options['value'] ) ) {
49: throw new InvalidArgumentException( 'Missing `value` option.' );
50: }
51:
52: $value = $this->call_value( $options['value'] );
53: $value = $this->update_value(
54: $options['property'],
55: $value,
56: $options['slug'],
57: $options['post_id']
58: );
59:
60: return papi_maybe_json_decode( maybe_unserialize( $value ) );
61: }
62:
63: 64: 65: 66: 67: 68: 69: 70: 71: 72:
73: protected function update_value( $property, $value, $slug, $post_id ) {
74: if ( ! is_array( $value ) || ! $this->should_update_array( $slug ) ) {
75: return $property->import_value( $value, $slug, $post_id );
76: }
77:
78: $old = papi_get_field( $post_id, $slug, [] );
79: $value = array_merge( $old, $value );
80: $value = $property->import_value( $value, $slug, $post_id );
81:
82: if ( $property->import_setting( 'property_array_slugs' ) ) {
83: return papi_from_property_array_slugs( $value, $slug );
84: }
85:
86: return $property->update_value( $value, $slug, $post_id );
87: }
88: }
89: