block-type
3 months ago
dev
3 months ago
form
1 week ago
option
9 months ago
options-page
3 months ago
performance
3 months ago
post-type
3 months ago
taxonomy
3 months ago
ui
3 months ago
author.php
1 week ago
autosync-json.php
3 months ago
autosync-php.php
3 months ago
author.php
209 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | // check setting |
| 8 | if(!acf_get_setting('acfe/modules/author')){ |
| 9 | return; |
| 10 | } |
| 11 | |
| 12 | if(!class_exists('acfe_author')): |
| 13 | |
| 14 | class acfe_author{ |
| 15 | |
| 16 | // vars |
| 17 | public $post_types = array(); |
| 18 | |
| 19 | /** |
| 20 | * construct |
| 21 | */ |
| 22 | function __construct(){ |
| 23 | |
| 24 | acf_add_local_field(array( |
| 25 | 'label' => '', |
| 26 | 'key' => 'field_acfe_author', |
| 27 | 'name' => 'acfe_author', |
| 28 | 'type' => 'user', |
| 29 | 'instructions' => '', |
| 30 | 'required' => 0, |
| 31 | 'conditional_logic' => 0, |
| 32 | 'allow_null' => 0, |
| 33 | 'multiple' => 0, |
| 34 | 'roles' => $this->get_roles(), |
| 35 | 'return_format' => 'array', |
| 36 | )); |
| 37 | |
| 38 | add_action('acfe/add_post_meta_boxes', array($this, 'add_post_meta_boxes'), 10, 2); |
| 39 | add_filter('wp_insert_post_data', array($this, 'wp_insert_post_data'), 10, 2); |
| 40 | add_filter('acf/get_field_group_style', array($this, 'get_field_group_style'), 10, 2); |
| 41 | |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * add_post_meta_boxes |
| 47 | * |
| 48 | * @param $post_type |
| 49 | * @param $post |
| 50 | */ |
| 51 | function add_post_meta_boxes($post_type, $post){ |
| 52 | |
| 53 | // disable on block editor |
| 54 | if(acfe_is_block_editor()){ |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | // validate author supports |
| 59 | if(!post_type_supports($post_type, 'author')){ |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // post type object |
| 64 | $post_type_object = get_post_type_object($post_type); |
| 65 | |
| 66 | // check permission |
| 67 | if(!current_user_can($post_type_object->cap->edit_others_posts)){ |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // remove legacy authordiv |
| 72 | remove_meta_box('authordiv', $post_type, 'normal'); |
| 73 | |
| 74 | // add metabox |
| 75 | add_meta_box('acfe-author', __('Author'), array($this, 'render_meta_box'), $post_type, 'side', 'core', array()); |
| 76 | |
| 77 | // generate postbox |
| 78 | // $postboxes = array(); |
| 79 | // $postboxes[] = array( |
| 80 | // 'id' => 'acfe-author', |
| 81 | // ); |
| 82 | |
| 83 | // get postboxes |
| 84 | // $data = acf_get_instance('ACF_Assets')->data; |
| 85 | // $acf_postboxes = acfe_get($data, 'postboxes', array()); |
| 86 | // $acf_postboxes = array_merge($acf_postboxes, $postboxes); |
| 87 | |
| 88 | // localize postboxes |
| 89 | // acf_localize_data(array( |
| 90 | // 'postboxes' => $acf_postboxes |
| 91 | // )); |
| 92 | |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * render_meta_box |
| 98 | * |
| 99 | * @param $post |
| 100 | * @param $metabox |
| 101 | */ |
| 102 | function render_meta_box($post, $metabox){ |
| 103 | |
| 104 | // retrieve field |
| 105 | $field = acf_get_field('acfe_author'); |
| 106 | |
| 107 | // add value |
| 108 | $field['prefix'] = ''; |
| 109 | $field['value'] = get_post_field('post_author', $post->ID); |
| 110 | |
| 111 | // render field |
| 112 | acf_render_field_wrap($field); |
| 113 | |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /** |
| 118 | * wp_insert_post_data |
| 119 | * |
| 120 | * @param $data |
| 121 | * @param $post_array |
| 122 | * |
| 123 | * @return mixed |
| 124 | */ |
| 125 | function wp_insert_post_data($data, $post_array){ |
| 126 | |
| 127 | // check field exists |
| 128 | if(!acfe_get($post_array, 'field_acfe_author')){ |
| 129 | return $data; |
| 130 | } |
| 131 | |
| 132 | // get post type object |
| 133 | $post_type_object = get_post_type_object($data['post_type']); |
| 134 | |
| 135 | // check permission |
| 136 | if(!current_user_can($post_type_object->cap->edit_others_posts)){ |
| 137 | return $data; |
| 138 | } |
| 139 | |
| 140 | // authors |
| 141 | $post_author = (int) acfe_get($post_array, 'field_acfe_author'); |
| 142 | $_post_author = (int) acfe_get($post_array, 'post_author'); |
| 143 | |
| 144 | // check if author has been changed |
| 145 | if($_post_author === $post_author){ |
| 146 | return $data; |
| 147 | } |
| 148 | |
| 149 | // validate author |
| 150 | if(!get_user_by('ID', $post_author)){ |
| 151 | return $data; |
| 152 | } |
| 153 | |
| 154 | // set new author |
| 155 | $data['post_author'] = $post_author; |
| 156 | |
| 157 | return $data; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * get_field_group_style |
| 164 | * |
| 165 | * @param $style |
| 166 | * @param $field_group |
| 167 | * |
| 168 | * @return array|string|string[] |
| 169 | */ |
| 170 | function get_field_group_style($style, $field_group){ |
| 171 | |
| 172 | $style = str_replace('authordiv', 'acfe-author', $style); |
| 173 | $style = str_replace('display: none;', 'display: none !important;', $style); |
| 174 | |
| 175 | return $style; |
| 176 | |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /** |
| 181 | * get_roles |
| 182 | * |
| 183 | * @return array |
| 184 | */ |
| 185 | function get_roles(){ |
| 186 | |
| 187 | $roles = array(); |
| 188 | |
| 189 | foreach(wp_roles()->roles as $name => $role){ |
| 190 | |
| 191 | // check capability |
| 192 | if(empty($role['capabilities']['level_1'])){ |
| 193 | continue; |
| 194 | } |
| 195 | |
| 196 | $roles[] = $name; |
| 197 | |
| 198 | } |
| 199 | |
| 200 | return $roles; |
| 201 | |
| 202 | } |
| 203 | |
| 204 | } |
| 205 | |
| 206 | // initialize |
| 207 | new acfe_author(); |
| 208 | |
| 209 | endif; |