-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings-pages.php
86 lines (76 loc) · 3 KB
/
settings-pages.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
<?php
namespace Sleek\PostTypes;
add_action('init', function () {
if (!function_exists('acf_add_options_page')) {
# trigger_error("sleek/post_types/settings_pages acf_add_options_page() is not defined, unable to create settings pages (have you enabled ACF?)", E_USER_WARNING);
return;
}
# Grab all public post types
$postTypes = get_post_types([], 'objects');
foreach ($postTypes as $postType) {
$hasArchive = ((!(isset($postType->has_archive) and $postType->has_archive === false) or $postType->name === 'post') and ($postType->public === true));
$hasSettings = (isset($postType->has_settings) and $postType->has_settings === true);
$disableSettings = (isset($postType->has_settings) and $postType->has_settings === false);
# Settings pages can be disabled using has_settings => false in CPT config
if (($hasArchive or $hasSettings) and !$disableSettings) {
# Create the options page
acf_add_options_page([
# Translators: For example "[Blog] Settings"
'page_title' => sprintf(__('%s Settings', 'sleek_admin'), $postType->labels->singular_name),
'menu_slug' => $postType->name . '_settings',
'parent_slug' => $postType->name === 'post' ? 'edit.php' : 'edit.php?post_type=' . $postType->name,
'icon_url' => 'dashicons-welcome-write-blog',
'post_id' => $postType->name . '_settings'
]);
# Make options page translatable with WPML
# https://www.advancedcustomfields.com/my-account/view-tickets/?conversation_id=504150372
add_filter('acf/validate_post_id', function ($postId) use ($postType) {
if ($postId === $postType->name . '_settings') {
$defaultLanguage = acf_get_setting('default_language');
$currentLanguage = acf_get_setting('current_language');
if ($currentLanguage and $currentLanguage !== $defaultLanguage) {
$postId .= '_' . $currentLanguage;
}
}
return $postId;
});
# Ignore post-types with no archives (built-in post post type has_archive = false but still has archives)
if ($hasArchive) {
# Add some standard fields (title, description, image)
$groupKey = $postType->name . '_settings';
$fields = \Sleek\Acf\generate_keys(apply_filters('sleek/post_types/archive_fields', [
[
'label' => __('Title', 'sleek_admin'),
'name' => 'title',
'type' => 'text'
],
[
'label' => __('Image', 'sleek_admin'),
'name' => 'image',
'type' => 'image',
'return_format' => 'id'
],
[
'label' => __('Description', 'sleek_admin'),
'name' => 'description',
'type' => 'wysiwyg',
'media_upload' => false,
'toolbar' => 'simple'
]
], $postType), $groupKey);
if ($fields) {
acf_add_local_field_group([
'key' => $groupKey,
'title' => __('Archive Settings', 'sleek_admin'),
'fields' => $fields,
'location' => [[[
'param' => 'options_page',
'operator' => '==',
'value' => $postType->name . '_settings'
]]]
]);
}
}
}
}
}, 99); # NOTE: 99 = make sure all post-types are registered