Skip to content

Commit b5d21c8

Browse files
committed
fix: adds src folder to ACF plugin
1 parent e70f616 commit b5d21c8

11 files changed

Lines changed: 1772 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* ACF Block Bindings
4+
*
5+
* @since 6.2.8
6+
* @package ACF
7+
*/
8+
9+
namespace ACF\Blocks;
10+
11+
// Exit if accessed directly.
12+
defined( 'ABSPATH' ) || exit;
13+
14+
/**
15+
* The core ACF Blocks binding class.
16+
*/
17+
class Bindings {
18+
/**
19+
* Block Bindings constructor.
20+
*/
21+
public function __construct() {
22+
// Final check we're on WP 6.5 or newer.
23+
if ( ! function_exists( 'register_block_bindings_source' ) ) {
24+
return;
25+
}
26+
27+
add_action( 'acf/init', array( $this, 'register_binding_sources' ) );
28+
}
29+
30+
/**
31+
* Hooked to acf/init, register our binding sources.
32+
*/
33+
public function register_binding_sources() {
34+
if ( acf_get_setting( 'enable_block_bindings' ) ) {
35+
register_block_bindings_source(
36+
'acf/field',
37+
array(
38+
'label' => _x( 'ACF Fields', 'The core ACF block binding source name for fields on the current page', 'acf' ),
39+
'get_value_callback' => array( $this, 'get_value' ),
40+
)
41+
);
42+
}
43+
}
44+
45+
/**
46+
* Handle returing the block binding value for an ACF meta value.
47+
*
48+
* @since 6.2.8
49+
*
50+
* @param array $source_attrs An array of the source attributes requested.
51+
* @param \WP_Block $block_instance The block instance.
52+
* @param string $attribute_name The block's bound attribute name.
53+
* @return string|null The block binding value or an empty string on failure.
54+
*/
55+
public function get_value( array $source_attrs, \WP_Block $block_instance, string $attribute_name ) {
56+
if ( ! isset( $source_attrs['key'] ) || ! is_string( $source_attrs['key'] ) ) {
57+
$value = '';
58+
} else {
59+
$field = get_field_object( $source_attrs['key'], false, true, true, true );
60+
61+
if ( ! $field ) {
62+
return '';
63+
}
64+
65+
if ( ! acf_field_type_supports( $field['type'], 'bindings', true ) ) {
66+
if ( is_preview() ) {
67+
return apply_filters( 'acf/bindings/field_not_supported_message', '[' . esc_html__( 'The requested ACF field type does not support output in Block Bindings or the ACF shortcode.', 'acf' ) . ']' );
68+
} else {
69+
return '';
70+
}
71+
}
72+
73+
if ( isset( $field['allow_in_bindings'] ) && ! $field['allow_in_bindings'] ) {
74+
if ( is_preview() ) {
75+
return apply_filters( 'acf/bindings/field_not_allowed_message', '[' . esc_html__( 'The requested ACF field is not allowed to be output in bindings or the ACF Shortcode.', 'acf' ) . ']' );
76+
} else {
77+
return '';
78+
}
79+
}
80+
81+
$value = $field['value'];
82+
83+
if ( is_array( $value ) ) {
84+
$value = implode( ', ', $value );
85+
}
86+
87+
// If we're not a scalar we'd throw an error, so return early for safety.
88+
if ( ! is_scalar( $value ) ) {
89+
$value = null;
90+
}
91+
}
92+
93+
return apply_filters( 'acf/blocks/binding_value', $value, $source_attrs, $block_instance, $attribute_name );
94+
}
95+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
// There are many ways to WordPress.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Adds support for saving/retrieving values from comment meta.
4+
*
5+
* @package AdvancedCustomFields
6+
* @subpackage Meta
7+
* @author WP Engine
8+
*/
9+
10+
namespace ACF\Meta;
11+
12+
/**
13+
* A class to add support for saving to comment meta.
14+
*/
15+
class Comment extends MetaLocation {
16+
17+
/**
18+
* The unique slug/name of the meta location.
19+
*
20+
* @var string
21+
*/
22+
public string $location_type = 'comment';
23+
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
/**
3+
* A class that can be extended to add support
4+
* for different meta types/locations in ACF.
5+
*
6+
* @package AdvancedCustomFields
7+
* @subpackage Meta
8+
* @author WP Engine
9+
*/
10+
11+
namespace ACF\Meta;
12+
13+
/**
14+
* The MetaType base class.
15+
*/
16+
class MetaLocation {
17+
18+
/**
19+
* The unique slug/name of the meta location.
20+
*
21+
* @var string
22+
*/
23+
public string $location_type = '';
24+
25+
/**
26+
* The prefix to use for ACF reference keys/hidden meta.
27+
*
28+
* @var string
29+
*/
30+
public string $reference_prefix = '_';
31+
32+
/**
33+
* Constructs the location.
34+
*
35+
* @since 6.4
36+
*/
37+
public function __construct() {
38+
$this->register();
39+
}
40+
41+
/**
42+
* Registers the meta location with ACF, so it can be used by
43+
* various CRUD helper functions.
44+
*
45+
* @since 6.4
46+
*
47+
* @return void
48+
*/
49+
public function register() {
50+
if ( empty( $this->location_type ) ) {
51+
return;
52+
}
53+
54+
$store = acf_get_store( 'acf-meta-locations' );
55+
56+
if ( ! $store ) {
57+
$store = acf_register_store( 'acf-meta-locations' );
58+
}
59+
60+
$store->set( $this->location_type, get_class( $this ) );
61+
}
62+
63+
/**
64+
* Retrieves all ACF meta for the provided object ID.
65+
*
66+
* @since 6.4
67+
*
68+
* @param integer|string $object_id The ID of the object to get meta from.
69+
* @return array
70+
*/
71+
public function get_meta( $object_id = 0 ): array {
72+
$meta = array();
73+
$all_meta = get_metadata( $this->location_type, $object_id );
74+
75+
if ( $all_meta ) {
76+
foreach ( $all_meta as $key => $value ) {
77+
// If a reference exists for this value, add it to the meta array.
78+
if ( isset( $all_meta[ $this->reference_prefix . $key ] ) ) {
79+
$meta[ $key ] = $value[0];
80+
$meta[ $this->reference_prefix . $key ] = $all_meta[ $this->reference_prefix . $key ][0];
81+
}
82+
}
83+
}
84+
85+
// Unserialize results and return.
86+
return array_map( 'acf_maybe_unserialize', $meta );
87+
}
88+
89+
/**
90+
* Retrieves a field value from the database.
91+
*
92+
* @since 6.4
93+
*
94+
* @param integer|string $object_id The ID of the object the metadata is for.
95+
* @param array $field The field array.
96+
* @return mixed
97+
*/
98+
public function get_value( $object_id = 0, array $field = array() ) {
99+
$meta = get_metadata( $this->location_type, $object_id, $field['name'] );
100+
return $meta[0] ?? null;
101+
}
102+
103+
/**
104+
* Gets a reference key for the provided field name.
105+
*
106+
* @since 6.4
107+
*
108+
* @param integer|string $object_id The ID of the object to get the reference key from.
109+
* @param string $field_name The name of the field to get the reference for.
110+
* @return string|null
111+
*/
112+
public function get_reference( $object_id = 0, $field_name = '' ) {
113+
$reference = get_metadata( $this->location_type, $object_id, $this->reference_prefix . $field_name );
114+
return $reference[0] ?? null;
115+
}
116+
117+
/**
118+
* Updates an object ID with the provided meta array.
119+
*
120+
* @since 6.4
121+
*
122+
* @param integer|string $object_id The ID of the object the metadata is for.
123+
* @param array $meta The metadata to save to the object.
124+
* @return void
125+
*/
126+
public function update_meta( $object_id = 0, array $meta = array() ) {
127+
// Slash data. WP expects all data to be slashed and will unslash it (fixes '\' character issues).
128+
$meta = wp_slash( $meta );
129+
130+
foreach ( $meta as $name => $value ) {
131+
update_metadata( $this->location_type, $object_id, $name, $value );
132+
}
133+
}
134+
135+
/**
136+
* Updates a field value in the database.
137+
*
138+
* @since 6.4
139+
*
140+
* @param integer|string $object_id The ID of the object the metadata is for.
141+
* @param array $field The field array.
142+
* @param mixed $value The metadata value.
143+
* @return integer|boolean
144+
*/
145+
public function update_value( $object_id = 0, array $field = array(), $value = '' ) {
146+
return update_metadata( $this->location_type, $object_id, $field['name'], $value );
147+
}
148+
149+
/**
150+
* Updates a reference key in the database.
151+
*
152+
* @since 6.4
153+
*
154+
* @param integer|string $object_id The ID of the object the metadata is for.
155+
* @param string $field_name The name of the field to update the reference for.
156+
* @param string $value The value of the reference key.
157+
* @return integer|boolean
158+
*/
159+
public function update_reference( $object_id = 0, string $field_name = '', string $value = '' ) {
160+
return update_metadata( $this->location_type, $object_id, $this->reference_prefix . $field_name, $value );
161+
}
162+
163+
/**
164+
* Deletes a field value from the database.
165+
*
166+
* @since 6.4
167+
*
168+
* @param integer|string $object_id The ID of the object the metadata is for.
169+
* @param array $field The field array.
170+
* @return boolean
171+
*/
172+
public function delete_value( $object_id = 0, array $field = array() ): bool {
173+
return delete_metadata( $this->location_type, $object_id, $field['name'] );
174+
}
175+
176+
/**
177+
* Deletes a reference key from the database.
178+
*
179+
* @since 6.4
180+
*
181+
* @param integer|string $object_id The ID of the object the metadata is for.
182+
* @param string $field_name The name of the field to delete the reference from.
183+
* @return boolean
184+
*/
185+
public function delete_reference( $object_id = 0, string $field_name = '' ): bool {
186+
return delete_metadata( $this->location_type, $object_id, $this->reference_prefix . $field_name );
187+
}
188+
}

0 commit comments

Comments
 (0)