Skip to content

Commit e1d1bef

Browse files
committed
code annotations, formatting, better testing, some fixes
1 parent d7d46c9 commit e1d1bef

File tree

7 files changed

+121
-47
lines changed

7 files changed

+121
-47
lines changed

include/ACFCustomizer/Compat/ACF/ACF.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,26 @@ class ACF extends Core\Singleton implements Core\ComponentInterface {
1717
*/
1818
protected function __construct() {
1919

20-
// need to figure out which version ist lowest...
21-
22-
2320
global $wp_customize;
2421

2522
$core = Core\Core::instance();
2623

27-
if ( ! is_null( $wp_customize ) ) {
28-
// instantinate at 11
29-
// add_action( 'plugins_loaded', array('ACFCustomizer\Compat\ACF\Customize','instance'), 11 );
30-
31-
}
32-
3324
require_once $core->get_plugin_dir() . '/include/api/acf-functions.php';
3425

3526
add_action( 'acf/include_location_rules', array( $this, 'load_location_rule' ) );
27+
add_filter( 'acf/decode_post_id', [ $this, 'decode_post_id' ], 10, 2 );
28+
29+
}
3630

31+
/**
32+
* @filter acf/decode_post_id
33+
*/
34+
public function decode_post_id( $decoded_post_id, $post_id ) {
35+
if ( 'option' === $decoded_post_id['type'] && ( $storage_type = Customize::instance()->get_storage_type_by_post_id( $post_id ) ) ) {
36+
// storage type by post id
37+
$decoded_post_id['type'] = $storage_type;
38+
}
39+
return $decoded_post_id;
3740
}
3841

3942
/**
@@ -51,6 +54,7 @@ public function ensure_field_key( $field, $post_id ) {
5154
$local_fields = acf_get_local_store( 'fields' )->query(array(
5255
'name' => $field['name']
5356
));
57+
5458
$candidates = array_keys( $local_fields );
5559
$candidates = array_merge( $candidates, $wpdb->get_col( $wpdb->prepare(
5660
"SELECT post_name FROM $wpdb->posts WHERE post_excerpt = %s",
@@ -78,14 +82,15 @@ public function ensure_field_key( $field, $post_id ) {
7882

7983
/**
8084
* returns whether an ACF field is handling data for specified post_id
85+
*
8186
* @param array $acf_field
8287
* @param int|string $post_id
8388
* @return boolean
8489
*/
8590
public function acf_field_is_handling_post_id( $acf_field, $post_id ) {
8691

8792
$decoded_post_id = acf_decode_post_id( $post_id );
88-
if ( ! in_array( $decoded_post_id['type'], [ 'post', 'term', 'option' ] ) ) {
93+
if ( ! in_array( $decoded_post_id['type'], [ 'post', 'term', 'option', 'theme_mod' ] ) ) {
8994
return false;
9095
}
9196

include/ACFCustomizer/Compat/ACF/Customize.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ public function get_section_id_by_fieldgroup_post_id( $field_group_key, $post_id
101101
return false;
102102
}
103103

104+
/**
105+
* Used by customize preview shortcuts
106+
*
107+
* @param string $post_id
108+
* @return boolean|string false on failure storage type otherwise
109+
*/
110+
public function get_storage_type_by_post_id( $post_id ) {
111+
foreach ( $this->sections as $section_id => $section ) {
112+
if ( $post_id === $section[ 'post_id' ] ) {
113+
return $section['storage_type'];
114+
}
115+
}
116+
return false;
117+
}
118+
104119
/**
105120
* @action customize_controls_enqueue_scripts
106121
*/

include/ACFCustomizer/Compat/ACF/CustomizePreview.php

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,64 @@ protected function __construct() {
3838
* @action acf_customizer_field
3939
*/
4040
public function partial_field( $selector, $post_id = null ) {
41+
echo $this->get_partial_field( $selector, $post_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
42+
}
43+
44+
/**
45+
* @action acf_customizer_row
46+
*/
47+
public function partial_row( ) {
48+
echo $this->partial_row(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
49+
}
50+
51+
/**
52+
* @action acf_customizer_sub_field
53+
*/
54+
public function partial_subfield( $selector ) {
55+
echo $this->get_partial_subfield( $selector ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
56+
}
57+
58+
/**
59+
* @action acf_customizer_field
60+
*/
61+
public function get_partial_field( $selector, $post_id = null ) {
4162
if ( $path = $this->build_path( $selector, $post_id ) ) {
42-
echo $this->get_partial_button( $path ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
63+
return $this->get_partial_button( $path );
4364
}
65+
return '';
4466
}
4567

4668
/**
4769
* @action acf_customizer_row
4870
*/
49-
public function partial_row( ) {
71+
public function get_partial_row() {
5072
if ( $path = $this->build_path( ) ) {
51-
echo $this->get_partial_button( $path ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
73+
return $this->get_partial_button( $path );
5274
}
75+
return '';
5376
}
5477

5578

5679
/**
5780
* @action acf_customizer_sub_field
5881
*/
59-
public function partial_subfield( $selector ) {
82+
public function get_partial_subfield( $selector ) {
6083
if ( $path = $this->build_path( $selector ) ) {
61-
echo $this->get_partial_button( $path ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
84+
return $this->get_partial_button( $path );
6285
}
86+
return '';
6387
}
6488

6589
/**
6690
* Build path from
6791
*
6892
* @param string $selector If null will build path based on ACF loop hierarchy. If not get path from field hierarchy.
6993
* @param string|int $post_id If null will guess current post id.
70-
* @return array
94+
* @return array Array of field keys
7195
*/
7296
private function build_path( $selector = null, $post_id = null ) {
7397

74-
$path = array();
98+
$path = [];
7599
$section_id = false;
76100
$post_id_info = false;
77101

@@ -80,21 +104,18 @@ private function build_path( $selector = null, $post_id = null ) {
80104

81105
$customize = Customize::instance();
82106
$acf = ACF::instance();
83-
107+
var_export($is_loop);
84108
if ( is_null( $post_id ) ) {
85109
if ( $is_loop ) {
86110
$post_id = acf_get_loop( 'active', 'post_id' );
87111
} else {
88112
$post_id = acf_get_valid_post_id();
89113
}
90-
91114
}
92115

93116
$post_id_info = acf_get_post_id_info( $post_id );
94117

95-
96118
if ( ! is_null( $selector ) ) {
97-
98119
if ( $is_loop ) {
99120
$field = get_sub_field_object( $selector, $post_id );
100121
} else {
@@ -104,7 +125,9 @@ private function build_path( $selector = null, $post_id = null ) {
104125
$path[] = $field['key'];
105126
} else {
106127
$field = get_field_object( $acf->ensure_field_key( [ 'name' => $selector ], $post_id ), $post_id );
107-
$path[] = $field['key'];
128+
if ( is_array( $field ) ) {
129+
$path[] = $field['key'];
130+
}
108131
}
109132
}
110133

@@ -121,11 +144,7 @@ private function build_path( $selector = null, $post_id = null ) {
121144
// } else {
122145
// $field = acf_get_field( $acf->ensure_field_key( [ 'name' => $selector ], $post_id ), $post_id );
123146
}
124-
if ( ! $field ) {
125-
var_dump($loops);
126-
var_dump($path);
127-
var_dump($acf->ensure_field_key( [ 'name' => $selector ], $post_id ));
128-
}
147+
129148
$field_group_key = $field['parent'];
130149

131150
$path[] = $field_group_key; // field group

include/ACFCustomizer/Compat/ACF/Storage/Storage.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ protected function __construct() {
4949
/**
5050
* register a post id
5151
*
52+
* @param string setting_id
5253
* @param string $post_id
5354
*/
54-
public function register_setting_id( $setting_id ) {
55-
$this->setting_ids[ $setting_id ] = true;
55+
public function register_setting_id( $setting_id, $post_id = true ) {
56+
$this->setting_ids[ $setting_id ] = $post_id;
5657
}
5758

5859

@@ -64,7 +65,7 @@ public function register_setting_id( $setting_id ) {
6465
*/
6566
public function has_setting_id( $setting_id ) {
6667

67-
return isset( $this->setting_ids[ $setting_id ] ) && $this->setting_ids[ $setting_id ];
68+
return isset( $this->setting_ids[ $setting_id ] ) && false !== $this->setting_ids[ $setting_id ];
6869

6970
}
7071

include/ACFCustomizer/Core/Core.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,28 @@
1515

1616
class Core extends Plugin implements CoreInterface {
1717

18-
18+
/** @var Asset\Asset */
1919
public $control_css = null;
2020

21+
/** @var Asset\Asset */
2122
public $control_js = null;
2223

24+
/** @var Asset\Asset */
2325
public $preview_css = null;
2426

27+
/** @var Asset\Asset */
2528
public $preview_js = null;
2629

27-
private $is_loaded = false;
30+
/** @var boolean */
31+
private $is_loaded = false;
2832

2933
/**
3034
* @inheritdoc
3135
*/
3236
protected function __construct() {
33-
add_action( 'plugins_loaded' , array( $this , 'init_compat' ), 0 ); // must run before hook acf/include_location_rules (acf as regular plugin)
34-
add_action( 'init' , array( $this , 'init_compat' ), 0 ); // must run before hook acf/include_location_rules (acf bundled in theme)
35-
add_action( 'init' , array( $this , 'init_assets' ) );
37+
add_action( 'plugins_loaded', [ $this , 'init_compat' ], 0 ); // must run before hook acf/include_location_rules (acf as regular plugin)
38+
add_action( 'init', [ $this , 'init_compat' ], 0 ); // must run before hook acf/include_location_rules (acf bundled in theme)
39+
add_action( 'init', [ $this , 'init_assets' ] );
3640

3741
$args = func_get_args();
3842
parent::__construct( ...$args );

index.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
Description: Use ACF Fields in customizer.
88
Author: Jörn Lund
99
Version: 0.3.1
10-
Tested up to: 5.5
11-
Requires PHP: 5.6
10+
Tested up to: 6.2
11+
Requires PHP: 7.4
1212
Author URI: https://github.com/mcguffin
1313
License: GPL3
1414
Text Domain: acf-customizer
1515
Domain Path: /languages/
16-
Update URI: https://github.com/mcguffin/acf-customizer/raw/main/.wp-release-info.json
16+
Update URI: https://github.com/mcguffin/acf-customizer/raw/master/.wp-release-info.json
1717
*/
1818

1919
/* Copyright 2018 Jörn Lund

test/test.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class PluginTest {
88

99
private $current_json_save_path = null;
10+
private $customizePreview = null;
1011

1112
public function __construct() {
1213
add_filter( 'acf/settings/load_json', [ $this, 'load_json' ] );
@@ -34,6 +35,8 @@ public function __construct() {
3435
}
3536

3637
public function the_content( $content ) {
38+
$this->customizePreview = Compat\ACF\CustomizePreview::instance();
39+
3740
$content = '';
3841

3942
$show = [
@@ -72,31 +75,58 @@ public function the_content( $content ) {
7275
$content .= "\n";
7376
}
7477

75-
get_template_part('template-parts/acf/repeat-basic');
78+
// get_template_part('template-parts/acf/repeat-basic');
7679

7780
return $content;
7881
}
7982

80-
private function render_field_groups( $field_groups, $post_id = null ) {
81-
$customizePreview = Compat\ACF\CustomizePreview::instance();
83+
private function render_field_groups( $field_groups, $post_id = null, $get_field_cb = 'get_field' ) {
8284
$content = '';
8385
foreach ( $field_groups as $group ) {
8486
$fields = acf_get_fields( $group );
8587

8688
$content .= "# Group: ".$group['title']."\n";
8789
foreach ( $fields as $field ) {
88-
$content .= $customizePreview->get_partial_field( $field['name'], $post_id );
89-
$content .= '<pre>';
90-
$content .= "Label: ".$field['label']."\n";
91-
$content .= "Name: ".$field['name']."\n";
92-
$content .= "Value: ".var_export(get_field($field['name'],$post_id),true)."\n"."\n";
93-
$content .= '</pre>';
90+
if ( in_array( $field['type'], ['flexible_content', 'repeater'] ) ) {
91+
$content .= '<pre>';
92+
$content .= "Label: ".$field['label']."\n";
93+
$content .= "Name: ".$field['name']."\n";
94+
$content .= '</pre>';
95+
while ( have_rows( $field['name'] ) ) {
96+
the_row();
97+
$content .= $this->customizePreview->get_partial_row();
98+
99+
$sub_fields = acf_get_fields( $field );
100+
foreach ( $sub_fields as $sub_field ) {
101+
$content .= $this->render_field( $sub_field, $post_id, 'get_sub_field' );
102+
}
103+
}
104+
} else {
105+
$content .= $this->render_field( $field, $post_id );
106+
}
94107
}
95108
$content .= "\n";
96109
}
97110
return $content;
98111
}
99112

113+
private function render_field( $field, $post_id, $get_field_cb = 'get_field' ) {
114+
$content = '';
115+
if ( $get_field_cb === 'get_field' ) {
116+
$content .= $this->customizePreview->get_partial_field( $field['name'], $post_id );
117+
} else {
118+
// $content .= $this->customizePreview->get_partial_subfield( $field['name'] );
119+
}
120+
$content .= '<pre>';
121+
$content .= "Label: ".$field['label']."\n";
122+
$content .= "Name: ".$field['name']."\n";
123+
$content .= "Value: ".var_export($get_field_cb($field['name'],$post_id),true)."\n"."\n";
124+
$content .= '</pre>';
125+
126+
return $content;
127+
128+
}
129+
100130
// public function template_include( $template ) {
101131
// $file = str_replace( get_stylesheet_directory(), '', $template);
102132
// $test_template = dirname(__FILE__) . '/templates' . $file;

0 commit comments

Comments
 (0)