• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql-acf / 7a271165e30d8148692bf942aab5f990c4e2e766

pending completion
7a271165e30d8148692bf942aab5f990c4e2e766

push

github

GitHub
Merge pull request #345 from markkelnar/cicd-docker-refactor

505 of 1045 relevant lines covered (48.33%)

13.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/class-acfsettings.php
1
<?php
2
/**
3
 * ACF extension for WP-GraphQL
4
 *
5
 * @package wp-graphql-acf
6
 */
7

8
namespace WPGraphQL\ACF;
9

10
/**
11
 * Class ACF_Settings
12
 *
13
 * @package WPGraphQL\ACF
14
 */
15
class ACF_Settings {
16

17
        protected $is_acf6_or_higher = false;
18

19
        /**
20
         * Initialize ACF Settings for the plugin
21
         */
22
        public function init() {
23

24
                $this->is_acf6_or_higher = defined( 'ACF_MAJOR_VERSION' ) && version_compare( ACF_MAJOR_VERSION, 6, '>=' );
×
25

26
                /**
27
                 * Add settings to individual fields to allow each field granular control
28
                 * over how it's shown in the GraphQL Schema
29
                 */
30
                add_action( 'acf/render_field_settings', [ $this, 'add_field_settings' ], 10, 1 );
×
31

32
                /**
33
                 * Enqueue scripts to enhance the UI of the ACF Field Group Settings
34
                 */
35
                add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_graphql_acf_scripts' ], 10, 1 );
×
36

37
                /**
38
                 * Register meta boxes for the ACF Field Group Settings
39
                 */
40
                add_action( 'add_meta_boxes', [ $this, 'register_meta_boxes' ] );
×
41

42
                /**
43
                 * Register an AJAX action and callback for converting ACF Location rules to GraphQL Types
44
                 */
45
                add_action( 'wp_ajax_get_acf_field_group_graphql_types', [ $this, 'ajax_callback' ] );
×
46

47
        }
×
48

49
        /**
50
         * Handle the AJAX callback for converting ACF Location settings to GraphQL Types
51
         *
52
         * @return void
53
         */
54
        public function ajax_callback() {
55

56
                if ( isset( $_POST['data'] ) ) {
×
57

58
                        $form_data = [];
×
59

60
                        parse_str( $_POST['data'], $form_data );
×
61

62
                        if ( empty( $form_data ) || ! isset( $form_data['acf_field_group'] ) ) {
×
63
                                wp_send_json( __( 'No form data.', 'wp-graphql-acf' ) );
×
64
                        }
65

66
                        $field_group = isset( $form_data['acf_field_group'] ) ? $form_data['acf_field_group'] : [];
×
67
                        $rules       = new LocationRules( [ $field_group ] );
×
68
                        $rules->determine_location_rules();
×
69

70
                        $group_name = isset( $field_group['graphql_field_name'] ) ? $field_group['graphql_field_name'] : $field_group['title'];
×
71
                        $group_name = $rules->format_field_name( $group_name );
×
72

73
                        $all_rules = $rules->get_rules();
×
74
                        if ( isset( $all_rules[ $group_name ] ) ) {
×
75
                                wp_send_json( [ 'graphql_types' => array_values( $all_rules[ $group_name ] ) ] );
×
76
                        }
77
                        wp_send_json( [ 'graphql_types' => null ] );
×
78
                }
79

80
                echo __( 'No location rules were found', 'wp-graphql-acf' );
×
81
                wp_die();
×
82
        }
×
83

84
        /**
85
         * Register the GraphQL Settings metabox for the ACF Field Group post type
86
         *
87
         * @return void
88
         */
89
        public function register_meta_boxes() {
90
                add_meta_box( 'wpgraphql-acf-meta-box', __( 'GraphQL', 'wp-graphql-acf' ), [
×
91
                        $this,
×
92
                        'display_metabox'
×
93
                ], [ 'acf-field-group' ] );
×
94
        }
×
95

96
        /**
97
         * Display the GraphQL Settings Metabox on the Field Group admin page
98
         *
99
         * @param $field_group_post_object
100
         */
101
        public function display_metabox( $field_group_post_object ) {
102

103
                global $field_group;
×
104

105
                /**
106
                 * Render a field in the Field Group settings to allow for a Field Group to be shown in GraphQL.
107
                 */
108
                acf_render_field_wrap(
×
109
                        [
110
                                'label'        => __( 'Show in GraphQL', 'acf' ),
×
111
                                'instructions' => __( 'If the field group is active, and this is set to show, the fields in this group will be available in the WPGraphQL Schema based on the respective Location rules.' ),
×
112
                                'type'         => 'true_false',
×
113
                                'name'         => 'show_in_graphql',
×
114
                                'prefix'       => 'acf_field_group',
×
115
                                'value'        => isset( $field_group['show_in_graphql'] ) ? (bool) $field_group['show_in_graphql'] : false,
×
116
                                'ui'           => 1,
×
117
                        ],
118
                        'div',
×
119
                        'label',
×
120
                        true
×
121
                );
122

123
                /**
124
                 * Render a field in the Field Group settings to set the GraphQL field name for the field group.
125
                 */
126
                acf_render_field_wrap(
×
127
                        [
128
                                'label'        => __( 'GraphQL Field Name', 'acf' ),
×
129
                                'instructions' => __( 'The name of the field group in the GraphQL Schema. Names should not include spaces or special characters. Best practice is to use "camelCase".', 'wp-graphql-acf' ),
×
130
                                'type'         => 'text',
×
131
                                'prefix'       => 'acf_field_group',
×
132
                                'name'         => 'graphql_field_name',
×
133
                                'required'     => isset( $field_group['show_in_graphql'] ) ? (bool) $field_group['show_in_graphql'] : false,
×
134
                                'placeholder'  => ! empty( $field_group['graphql_field_name'] ) ? $field_group['graphql_field_name'] : null,
×
135
                                'value'        => ! empty( $field_group['graphql_field_name'] ) ? $field_group['graphql_field_name'] : null,
×
136
                        ],
137
                        'div',
×
138
                        'label',
×
139
                        true
×
140
                );
141

142
                acf_render_field_wrap(
×
143
                        [
144
                                'label'        => __( 'Manually Set GraphQL Types for Field Group', 'acf' ),
×
145
                                'instructions' => __( 'By default, ACF Field groups are added to the GraphQL Schema based on the field group\'s location rules. Checking this box will let you manually control the GraphQL Types the field group should be shown on in the GraphQL Schema using the checkboxes below, and the Location Rules will no longer effect the GraphQL Types.', 'wp-graphql-acf' ),
×
146
                                'type'         => 'true_false',
×
147
                                'name'         => 'map_graphql_types_from_location_rules',
×
148
                                'prefix'       => 'acf_field_group',
×
149
                                'value'        => isset( $field_group['map_graphql_types_from_location_rules'] ) ? (bool) $field_group['map_graphql_types_from_location_rules'] : false,
×
150
                                'ui'           => 1,
×
151
                        ],
152
                        'div',
×
153
                        'label',
×
154
                        true
×
155
                );
156

157
                $choices = Config::get_all_graphql_types();
×
158
                acf_render_field_wrap(
×
159
                        [
160
                                'label'        => __( 'GraphQL Types to Show the Field Group On', 'wp-graphql-acf' ),
×
161
                                'instructions' => __( 'Select the Types in the WPGraphQL Schema to show the fields in this field group on', 'wp-graphql-acf' ),
×
162
                                'type'         => 'checkbox',
×
163
                                'prefix'       => 'acf_field_group',
×
164
                                'name'         => 'graphql_types',
×
165
                                'value'        => ! empty( $field_group['graphql_types'] ) ? $field_group['graphql_types'] : [],
×
166
                                'toggle'       => true,
167
                                'choices'      => $choices,
×
168
                        ],
169
                        'div',
×
170
                        'label',
×
171
                        true
×
172
                );
173

174
                ?>
175
                <div class="acf-hidden">
×
176
                        <input type="hidden" name="acf_field_group[key]"
177
                                   value="<?php echo $field_group['key']; ?>"/>
178
                </div>
179
                <script type="text/javascript">
180
                        if (typeof acf !== 'undefined') {
181
                                acf.newPostbox({
182
                                        'id': 'wpgraphql-acf-meta-box',
183
                                        'label': <?php echo $this->is_acf6_or_higher ? 'top' : "'left'"; ?>
184
                                });
×
185
                        }
186
                </script>
187
                <?php
188

189
        }
×
190

191
        /**
192
         * Add settings to each field to show in GraphQL
193
         *
194
         * @param array $field The field to add the setting to.
195
         */
196
        public function add_field_settings( array $field ) {
197

198
                $supported_fields = Config::get_supported_fields();
×
199

200
                /**
201
                 * If there are no supported fields, or the field is not supported, don't add a setting field.
202
                 */
203
                if ( empty( $supported_fields ) || ! is_array( $supported_fields ) || ! in_array( $field['type'], $supported_fields, true ) ) {
×
204
                        return;
×
205
                }
206

207
                /**
208
                 * Render the "show_in_graphql" setting for the field.
209
                 */
210
                acf_render_field_setting(
×
211
                        $field,
×
212
                        [
213
                                'label'         => __( 'Show in GraphQL', 'wp-graphql-acf' ),
×
214
                                'instructions'  => __( 'Whether the field should be queryable via GraphQL', 'wp-graphql-acf' ),
×
215
                                'name'          => 'show_in_graphql',
×
216
                                'type'          => 'true_false',
×
217
                                'ui'            => 1,
×
218
                                'default_value' => 1,
×
219
                                'value'         => isset( $field['show_in_graphql'] ) ? (bool) $field['show_in_graphql'] : true,
×
220
                        ],
221
                        true
×
222
                );
223

224
        }
×
225

226
        /**
227
         * This enqueues admin script.
228
         *
229
         * @param string $screen The screen that scripts are being enqueued to
230
         *
231
         * @return void
232
         */
233
        public function enqueue_graphql_acf_scripts( string $screen ) {
234
                global $post;
×
235

236
                if ( ( $screen === 'post-new.php' || $screen === 'post.php' ) && ( isset( $post->post_type ) && 'acf-field-group' === $post->post_type ) ) {
×
237
                                wp_enqueue_script( 'graphql-acf', plugins_url( 'src/js/main.js', __DIR__ ), array(
×
238
                                        'jquery',
×
239
                                        'acf-input',
240
                                        'acf-field-group'
241
                                ) );
242
                }
243
        }
×
244

245
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc