-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathadmin-internal-post-type.php
354 lines (308 loc) · 9.43 KB
/
admin-internal-post-type.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
/**
* ACF Internal Post Type class
*
* Base class to add functionality to ACF internal post types.
*
* @package wordpress/secure-custom-fields
* @subpackage Admin
* @since ACF 6.1
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! class_exists( 'ACF_Admin_Internal_Post_Type' ) ) :
/**
* ACF Internal Post Type class.
*
* Adds logic to the edit page for ACF internal post types.
*/
class ACF_Admin_Internal_Post_Type {
/**
* The slug for the internal post type.
*
* @since ACF 6.1
* @var string
*/
public $post_type = '';
/**
* The admin body class used for the post type.
*
* @since ACF 6.1
* @var string
*/
public $admin_body_class = '';
/**
* Constructs the class.
*/
public function __construct() {
add_action( 'current_screen', array( $this, 'current_screen' ) );
add_action( 'save_post_' . $this->post_type, array( $this, 'save_post' ), 10, 2 );
add_action( 'wp_ajax_acf/link_field_groups', array( $this, 'ajax_link_field_groups' ) );
add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
add_filter( 'use_block_editor_for_post_type', array( $this, 'use_block_editor_for_post_type' ), 10, 2 );
}
/**
* Prevents the block editor from loading when editing an ACF field group.
*
* @since ACF 5.8.0
*
* @param boolean $use_block_editor Whether the post type can be edited or not. Default true.
* @param string $post_type The post type being checked.
* @return boolean
*/
public function use_block_editor_for_post_type( $use_block_editor, $post_type ) {
if ( $post_type === $this->post_type ) {
return false;
}
return $use_block_editor;
}
/**
* This function will customize the message shown when editing a field group
*
* @since ACF 5.0.0
*
* @param array $messages Post type messages.
* @return array
*/
public function post_updated_messages( $messages ) {
return $messages;
}
/**
* This function is fired when loading the admin page before HTML has been rendered.
*
* @since ACF 5.0.0
*/
public function current_screen() {
if ( ! acf_is_screen( $this->post_type ) ) {
return;
}
acf_disable_filters();
acf_enqueue_scripts();
add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
add_action( 'acf/input/admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'acf/input/admin_head', array( $this, 'admin_head' ) );
add_action( 'acf/input/form_data', array( $this, 'form_data' ) );
add_action( 'acf/input/admin_footer', array( $this, 'admin_footer' ) );
add_filter( 'acf/input/admin_l10n', array( $this, 'admin_l10n' ) );
do_action( 'acf/internal_post_type/current_screen', $this->post_type );
}
/**
* Modifies the admin body class.
*
* @since ACF 6.0.0
*
* @param string $classes Space-separated list of CSS classes.
* @return string
*/
public function admin_body_class( $classes ) {
$classes .= ' acf-admin-page acf-internal-post-type ' . esc_attr( $this->admin_body_class );
return apply_filters( 'acf/internal_post_type/admin_body_classes', $classes, $this->post_type );
}
/**
* Enqueues any scripts necessary for internal post type.
*
* @since ACF 5.0.0
*/
public function admin_enqueue_scripts() {
wp_enqueue_script( 'acf-internal-post-type' );
wp_dequeue_script( 'autosave' );
wp_enqueue_style( $this->post_type );
wp_enqueue_script( $this->post_type );
}
/**
* Set up functionality for the field group edit page.
*
* @since ACF 3.1.8
*/
public function admin_head() {
// Override as necessary.
}
/**
* Adds extra HTML to the acf form data element.
*
* @since ACF 5.3.8
*
* @param array $args Arguments array to pass through to action.
* @return void
*/
public function form_data( $args ) {
// Override as necessary.
}
/**
* Admin footer third party hook support
*
* @since ACF 5.3.2
*/
public function admin_footer() {
// Override as necessary.
}
/**
* This function will append extra l10n strings to the acf JS object
*
* @since ACF 5.3.8
*
* @param array $l10n The array of translated strings.
* @return mixed
*/
public function admin_l10n( $l10n ) {
// Override as necessary.
return $l10n;
}
/**
* Ran during the `save_post` hook to verify that the post should be saved.
*
* @since ACF 6.1
*
* @param integer $post_id The ID of the post being saved.
* @param WP_Post $post The post object.
* @return boolean
*/
public function verify_save_post( $post_id, $post ) {
// Do not save if this is an auto save routine.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return false;
}
// Bail early if not an ACF internal post type.
if ( $post->post_type !== $this->post_type ) {
return false;
}
// Only save once! WordPress saves a revision as well.
if ( wp_is_post_revision( $post_id ) ) {
return false;
}
// Verify nonce.
$nonce_name = str_replace(
array( 'acf-', '-' ),
array( '', '_' ),
$this->post_type
);
if ( ! acf_verify_nonce( $nonce_name ) ) {
return false;
}
// Bail early if request came from an unauthorised user.
if ( ! current_user_can( acf_get_setting( 'capability' ) ) ) {
return false;
}
return true;
}
/**
* Powers the modal for linking field groups to newly-created CPTs/taxonomies.
*
* @since ACF 6.1
*/
public function ajax_link_field_groups() {
// Disable filters to ensure ACF loads raw data from DB.
acf_disable_filters();
// phpcs:disable WordPress.Security.NonceVerification.Missing
$args = acf_parse_args(
$_POST,
array(
'nonce' => '',
'post_id' => 0,
'field_groups' => array(),
)
);
// phpcs:enable WordPress.Security.NonceVerification.Missing
// Verify nonce and user capability.
if ( ! wp_verify_nonce( $args['nonce'], 'acf_nonce' ) || ! acf_current_user_can_admin() || ! $args['post_id'] ) {
die();
}
$post_type = get_post_type( $args['post_id'] );
$saved_post = acf_get_internal_post_type( $args['post_id'], $post_type );
// Link the selected field groups.
if ( is_array( $args['field_groups'] ) && ! empty( $args['field_groups'] ) && $saved_post ) {
foreach ( $args['field_groups'] as $field_group_id ) {
$field_group = acf_get_field_group( $field_group_id );
if ( ! is_array( $field_group ) ) {
continue;
}
if ( 'acf-post-type' === $post_type ) {
$param = 'post_type';
$value = $saved_post['post_type'];
} elseif ( 'acf-taxonomy' === $post_type ) {
$param = 'taxonomy';
$value = $saved_post['taxonomy'];
} else {
$param = 'options_page';
$value = $saved_post['menu_slug'];
}
$field_group['location'][] = array(
array(
'param' => $param,
'operator' => '==',
'value' => $value,
),
);
acf_update_field_group( $field_group );
}
ob_start();
?>
<p class="acf-link-successful">
<?php
$link_successful_text = _n(
'Field group linked successfully.',
'Field groups linked successfully.',
count( $args['field_groups'] ),
'secure-custom-fields'
);
echo esc_html( $link_successful_text );
?>
</p>
<div class="acf-actions">
<button type="button" class="acf-btn acf-btn-secondary acf-close-popup"><?php esc_html_e( 'Close Modal', 'secure-custom-fields' ); ?></button>
</div>
<?php
$content = ob_get_clean();
wp_send_json_success( array( 'content' => $content ) );
}
// Render the field group select.
$field_groups = acf_get_field_groups();
$choices = array();
if ( ! empty( $field_groups ) ) {
foreach ( $field_groups as $field_group ) {
if ( ! $field_group['ID'] ) {
continue;
}
$choices[ $field_group['ID'] ] = $field_group['title'];
}
}
$instructions = sprintf(
/* translators: %s - either "post type" or "taxonomy" */
__( 'Add this %s to the location rules of the selected field groups.', 'secure-custom-fields' ),
'acf-post-type' === $post_type ? __( 'post type', 'secure-custom-fields' ) : __( 'taxonomy', 'secure-custom-fields' )
);
$field = acf_get_valid_field(
array(
'type' => 'select',
'name' => 'acf_field_groups',
'choices' => $choices,
'aria-label' => __( 'Please select the field groups to link.', 'secure-custom-fields' ),
'placeholder' => __( 'Select one or many field groups...', 'secure-custom-fields' ),
'label' => __( 'Field Group(s)', 'secure-custom-fields' ),
'instructions' => $instructions,
'ui' => true,
'multiple' => true,
'allow_null' => true,
)
);
ob_start();
?>
<form id="acf-link-field-groups-form">
<?php acf_render_field_wrap( $field, 'div', 'field' ); ?>
<div class="acf-actions">
<button type="button" class="acf-btn acf-btn-secondary acf-close-popup"><?php esc_html_e( 'Cancel', 'secure-custom-fields' ); ?></button>
<button type="submit" class="acf-btn acf-btn-primary"><?php esc_html_e( 'Done', 'secure-custom-fields' ); ?></button>
</div>
</form>
<?php
$content = ob_get_clean();
wp_send_json_success(
array(
'content' => $content,
'title' => esc_html__( 'Link Existing Field Groups', 'secure-custom-fields' ),
)
);
}
}
endif; // Class exists check.