Skip to content

Commit 66be918

Browse files
Fix Multisite fields for compatibility with ACF 6.8.4 (#138)
1 parent 7592060 commit 66be918

7 files changed

Lines changed: 61 additions & 31 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
[Unreleased]
88

9+
## [1.41.8]
10+
11+
### Fixed
12+
- Fix AJAX query compatibility with ACF Pro 6.8.4 for MultisiteRelationship, MultisiteTaxonomy, and Multitaxonomy fields. ACF added field-type nonce validation that rejected requests where the custom field type didn't match the parent's expected type. Shared fix extracted into `AcfAjaxQueryTrait`.
13+
914
## [1.41.7]
1015

1116
### Fixed

plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: ACF Codifier
44
Plugin URI: https://github.com/devgeniem/acf-codifier
55
Description: A helper class to make defining ACF field groups and fields easier in the code.
6-
Version: 1.41.7
6+
Version: 1.41.8
77
Author: Miika Arponen / Geniem Oy
88
Author URI: https://geniem.fi
99
License: GPL-3.0

src/Fields/AcfAjaxQueryTrait.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* ACF Ajax query trait for custom field types.
4+
*/
5+
6+
namespace Geniem\ACF\Fields;
7+
8+
/**
9+
* Trait AcfAjaxQueryTrait
10+
*
11+
* Overrides the parent ajax_query() to pass the correct field type name
12+
* to acf_verify_ajax(). Required since ACF 6.8.4, which added field-type
13+
* validation to AJAX nonce checks. Without this override the parent would
14+
* pass its own type (e.g. 'post_object', 'relationship', 'taxonomy') instead
15+
* of the custom type registered by the subclass, causing verification to fail.
16+
*
17+
* Classes using this trait must set $this->name to the registered field type
18+
* string inside initialize().
19+
*/
20+
trait AcfAjaxQueryTrait {
21+
22+
/**
23+
* Override parent ajax_query to use correct field type for nonce verification.
24+
*
25+
* @return void
26+
*/
27+
public function ajax_query() {
28+
$nonce = \acf_request_arg( 'nonce', '' );
29+
$key = \acf_request_arg( 'field_key', '' );
30+
$conditional_logic = (bool) \acf_request_arg( 'conditional_logic', false );
31+
32+
if ( $conditional_logic ) {
33+
if ( ! \acf_current_user_can_admin() ) {
34+
die();
35+
}
36+
37+
$nonce = '';
38+
$key = '';
39+
}
40+
41+
if ( ! \acf_verify_ajax( $nonce, $key, ! $conditional_logic, $this->name ) ) {
42+
die();
43+
}
44+
45+
\acf_send_ajax_results( $this->get_ajax_query( $_POST ) );
46+
}
47+
}

src/Fields/MultisitePostObject.php

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
class MultisitePostObject extends \acf_field_post_object {
1515

16+
use AcfAjaxQueryTrait;
17+
1618
/**
1719
* Initialize the field.
1820
*
@@ -51,36 +53,6 @@ public function input_admin_enqueue_scripts() {
5153
);
5254
}
5355

54-
/**
55-
* Override parent ajax_query to use correct field type for nonce verification.
56-
*
57-
* ACF 6.8.4 added field-type validation to AJAX nonce checks.
58-
* The parent passes 'post_object' as expected type, but this field
59-
* registers as 'multisite_post_object', causing verification to fail.
60-
*
61-
* @return void
62-
*/
63-
public function ajax_query() {
64-
$nonce = \acf_request_arg( 'nonce', '' );
65-
$key = \acf_request_arg( 'field_key', '' );
66-
$conditional_logic = (bool) \acf_request_arg( 'conditional_logic', false );
67-
68-
if ( $conditional_logic ) {
69-
if ( ! \acf_current_user_can_admin() ) {
70-
die();
71-
}
72-
73-
$nonce = '';
74-
$key = '';
75-
}
76-
77-
if ( ! \acf_verify_ajax( $nonce, $key, ! $conditional_logic, 'multisite_post_object' ) ) {
78-
die();
79-
}
80-
81-
\acf_send_ajax_results( $this->get_ajax_query( $_POST ) );
82-
}
83-
8456
/**
8557
* Switch to the selected blog when using
8658
* the parent method for fetching the AJAX response.

src/Fields/MultisiteRelationship.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
class ACF_Field_Multisite_Relationship extends \acf_field_relationship {
1414

15+
use AcfAjaxQueryTrait;
16+
1517
/**
1618
* Initialize the field
1719
*

src/Fields/MultisiteTaxonomy.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
class ACF_Field_Multisite_Taxonomy extends Multitaxonomy {
1313

14+
use AcfAjaxQueryTrait;
15+
1416
/**
1517
* Initialize the field
1618
*

src/Fields/Multitaxonomy.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
class Multitaxonomy extends \acf_field_taxonomy {
1313

14+
use AcfAjaxQueryTrait;
15+
1416
/**
1517
* Initialize the field
1618
*

0 commit comments

Comments
 (0)