form
6 years ago
author.php
6 years ago
autosync.php
6 years ago
dev.php
6 years ago
dynamic-block-type.php
6 years ago
dynamic-form.php
6 years ago
dynamic-options-page.php
6 years ago
dynamic-post-type.php
6 years ago
dynamic-taxonomy.php
6 years ago
taxonomy.php
6 years ago
author.php
230 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | // Check setting |
| 7 | if(!acf_get_setting('acfe/modules/author')) |
| 8 | return; |
| 9 | |
| 10 | /** |
| 11 | * Register Field Group |
| 12 | */ |
| 13 | add_filter('acf/get_field_groups', 'acfe_author_field_group_permissions', 999); |
| 14 | function acfe_author_field_group_permissions($field_groups){ |
| 15 | |
| 16 | if(!is_admin()) |
| 17 | return $field_groups; |
| 18 | |
| 19 | $check_current_screen = acf_is_screen(array( |
| 20 | 'edit-acf-field-group', |
| 21 | 'acf-field-group', |
| 22 | 'acf_page_acf-tools' |
| 23 | )); |
| 24 | |
| 25 | if($check_current_screen) |
| 26 | return $field_groups; |
| 27 | |
| 28 | global $post; |
| 29 | |
| 30 | // Get Post ID |
| 31 | $post_id = get_the_ID(); |
| 32 | |
| 33 | if(empty($post_id) && isset($_REQUEST['post'])) |
| 34 | $post_id = (int) $_REQUEST['post']; |
| 35 | |
| 36 | if(empty($post_id) && isset($post->ID)) |
| 37 | $post_id = $post->ID; |
| 38 | |
| 39 | if(empty($post_id)) |
| 40 | return $field_groups; |
| 41 | |
| 42 | // Get Post Type Object |
| 43 | $post_type_object = get_post_type_object(get_post_type($post_id)); |
| 44 | if(empty($post_type_object)) |
| 45 | return $field_groups; |
| 46 | |
| 47 | foreach($field_groups as $key => $field_group){ |
| 48 | |
| 49 | if($field_group['key'] != 'group_acfe_author') |
| 50 | continue; |
| 51 | |
| 52 | if(!current_user_can($post_type_object->cap->edit_others_posts)) |
| 53 | unset($field_groups[$key]); |
| 54 | |
| 55 | } |
| 56 | |
| 57 | return $field_groups; |
| 58 | |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Remove Native WP Metabox |
| 63 | */ |
| 64 | add_action('admin_menu','acfe_author_remove_default_metabox'); |
| 65 | function acfe_author_remove_default_metabox(){ |
| 66 | |
| 67 | $get_post_types = get_post_types_by_support('author'); |
| 68 | if(empty($get_post_types)) |
| 69 | return; |
| 70 | |
| 71 | foreach($get_post_types as $post_type){ |
| 72 | |
| 73 | if(in_array($post_type, array('attachment', 'revision', 'customize_changeset'))) |
| 74 | continue; |
| 75 | |
| 76 | // Remove Metabox |
| 77 | remove_meta_box('authordiv', $post_type, 'normal'); |
| 78 | |
| 79 | } |
| 80 | |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Prepare Default Value |
| 85 | */ |
| 86 | add_filter('acf/prepare_field/name=acfe_author', 'acfe_author_prepare'); |
| 87 | function acfe_author_prepare($field){ |
| 88 | |
| 89 | // Get Post ID |
| 90 | $post_id = get_the_ID(); |
| 91 | if(empty($post_id)) |
| 92 | return false; |
| 93 | |
| 94 | // Check Post Type & Permissions |
| 95 | $post_type_object = get_post_type_object(get_post_type($post_id)); |
| 96 | if(empty($post_type_object) || !current_user_can($post_type_object->cap->edit_others_posts)) |
| 97 | return false; |
| 98 | |
| 99 | // Set Default |
| 100 | $author_id = get_post_field('post_author', $post_id); |
| 101 | $field['value'] = $author_id; |
| 102 | |
| 103 | return $field; |
| 104 | |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Save Post Action |
| 109 | */ |
| 110 | add_action('acf/save_post', 'acfe_author_post_save', 0); |
| 111 | function acfe_author_post_save($post_id){ |
| 112 | |
| 113 | // Check Field Exists |
| 114 | if(!isset($_POST['acf']['acfe_author'])) |
| 115 | return; |
| 116 | |
| 117 | // Check Post Type & Permissions |
| 118 | $post_type_object = get_post_type_object(get_post_type($post_id)); |
| 119 | if(empty($post_type_object) || !current_user_can($post_type_object->cap->edit_others_posts)) |
| 120 | return; |
| 121 | |
| 122 | // Set & Validate Author |
| 123 | $author_id = (int) $_POST['acf']['acfe_author']; |
| 124 | if(!get_user_by('ID', $author_id)) |
| 125 | return; |
| 126 | |
| 127 | // Update Post Author |
| 128 | wp_update_post(array( |
| 129 | 'ID' => $post_id, |
| 130 | 'post_author' => $author_id |
| 131 | )); |
| 132 | |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Bypass Metadata Update |
| 137 | */ |
| 138 | add_filter('acf/pre_update_value', 'acfe_author_meta_update', 10, 4); |
| 139 | function acfe_author_meta_update($return, $value, $post_id, $field){ |
| 140 | |
| 141 | if($field['name'] === 'acfe_author') |
| 142 | return false; |
| 143 | |
| 144 | return $return; |
| 145 | |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Field Group Hide on Screen |
| 150 | */ |
| 151 | add_filter('acf/get_field_group_style', 'acfe_author_meta_hide_on_screen', 10, 2); |
| 152 | function acfe_author_meta_hide_on_screen($style, $field_group){ |
| 153 | |
| 154 | $style = str_replace('authordiv', 'acf-group_acfe_author', $style); |
| 155 | $style = str_replace('display: none;', 'display: none !important;', $style); |
| 156 | |
| 157 | return $style; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | // Get Post Types Locations |
| 162 | $get_post_types = get_post_types_by_support('author'); |
| 163 | if(!empty($get_post_types)){ |
| 164 | |
| 165 | // Set Locations |
| 166 | $locations = array(); |
| 167 | |
| 168 | foreach($get_post_types as $post_type){ |
| 169 | |
| 170 | $locations[] = array( |
| 171 | array( |
| 172 | 'param' => 'post_type', |
| 173 | 'operator' => '==', |
| 174 | 'value' => $post_type, |
| 175 | ) |
| 176 | ); |
| 177 | |
| 178 | } |
| 179 | |
| 180 | // Roles |
| 181 | global $wp_roles; |
| 182 | |
| 183 | $authors_roles = array(); |
| 184 | foreach($wp_roles->roles as $role_name => $role){ |
| 185 | |
| 186 | if(!isset($role['capabilities']['level_1']) || empty($role['capabilities']['level_1'])) |
| 187 | continue; |
| 188 | |
| 189 | $authors_roles[] = $role_name; |
| 190 | |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Add Local Field Group |
| 195 | */ |
| 196 | acf_add_local_field_group(array( |
| 197 | 'title' => __('Author'), |
| 198 | 'key' => 'group_acfe_author', |
| 199 | 'menu_order' => 99999, |
| 200 | 'position' => 'side', |
| 201 | 'style' => 'default', |
| 202 | 'label_placement' => 'top', |
| 203 | 'instruction_placement' => 'label', |
| 204 | 'hide_on_screen' => '', |
| 205 | 'active' => 1, |
| 206 | 'description' => '', |
| 207 | 'location' => $locations, |
| 208 | 'fields' => array( |
| 209 | array( |
| 210 | 'label' => '', |
| 211 | 'key' => 'acfe_author', |
| 212 | 'name' => 'acfe_author', |
| 213 | 'type' => 'user', |
| 214 | 'instructions' => '', |
| 215 | 'required' => 0, |
| 216 | 'conditional_logic' => 0, |
| 217 | 'allow_null' => 0, |
| 218 | 'multiple' => 0, |
| 219 | 'return_format' => 'array', |
| 220 | 'role' => $authors_roles, |
| 221 | 'wrapper' => array( |
| 222 | 'width' => '', |
| 223 | 'class' => '', |
| 224 | 'id' => '', |
| 225 | ) |
| 226 | ), |
| 227 | ) |
| 228 | )); |
| 229 | |
| 230 | } |