markoheijnen /
tabify-edit-screen
| 1 | <?php |
||
| 2 | |||
| 3 | class Tabify_Edit_Screen_Settings_Posttypes extends Tabify_Edit_Screen_Settings_Base { |
||
| 4 | protected $defaults = array( 'titlediv', 'submitdiv' ); |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Loads the base with the type |
||
| 8 | * |
||
| 9 | * @since 0.4.0 |
||
| 10 | */ |
||
| 11 | public function __construct() { |
||
| 12 | parent::__construct('posttypes'); |
||
| 13 | |||
| 14 | add_action( 'tabify_settings', [ $this, 'display_gutenberg_warning' ] ); |
||
| 15 | add_filter( 'tabify_settings_update', [ $this, 'save_settings' ] ); |
||
| 16 | } |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Show warning if post type uses gutenberg |
||
| 20 | * |
||
| 21 | * @since 0.4.0 |
||
| 22 | */ |
||
| 23 | public function display_gutenberg_warning( $section ) { |
||
| 24 | if (use_block_editor_for_post_type($section)) { |
||
| 25 | echo '<p class="gutenberg-error notice notice-error inline">' . __( 'This post type support Gutenberg which is not supported.', 'tabify-edit-screen' ) . '</p>'; |
||
| 26 | } |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Set the items property when needed. |
||
| 31 | * |
||
| 32 | * @since 1.0.0 |
||
| 33 | */ |
||
| 34 | protected function load_items() { |
||
| 35 | if ($this->items) { |
||
|
0 ignored issues
–
show
|
|||
| 36 | return; |
||
| 37 | } |
||
| 38 | |||
| 39 | $posttypes = $this->get_sections(); |
||
| 40 | $metaboxes = $this->get_metaboxes(); |
||
| 41 | $this->items = apply_filters( 'tabify_metaboxes', $metaboxes, $posttypes ); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Loads all the posttypes as sections |
||
| 46 | * |
||
| 47 | * @return array $posttypes List of post types |
||
| 48 | * |
||
| 49 | * @since 0.4.0 |
||
| 50 | */ |
||
| 51 | protected function load_sections() { |
||
| 52 | $args = array( |
||
| 53 | 'show_ui' => true |
||
| 54 | ); |
||
| 55 | |||
| 56 | $posttypes_objects = get_post_types( $args, 'objects' ); |
||
| 57 | $posttypes_objects = apply_filters( 'tabify_posttypes', $posttypes_objects ); |
||
| 58 | |||
| 59 | $posttypes = array(); |
||
| 60 | foreach ( $posttypes_objects as $posttype_object ) { |
||
| 61 | if ( is_object( $posttype_object ) ) { |
||
| 62 | $posttypes[ $posttype_object->name ] = $posttype_object->label; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | return $posttypes; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Gets all the metaboxes that are registered |
||
| 71 | * |
||
| 72 | * @return array $metaboxes List of metaboxes for all post types |
||
| 73 | * |
||
| 74 | * @since 0.1.0 |
||
| 75 | */ |
||
| 76 | private function get_metaboxes() { |
||
| 77 | global $wp_meta_boxes; |
||
| 78 | |||
| 79 | $metaboxes = array(); |
||
| 80 | $sections = $this->get_sections(); |
||
| 81 | |||
| 82 | foreach ( $sections as $posttype => $label ) { |
||
| 83 | $metaboxes[ $posttype ] = array(); |
||
| 84 | |||
| 85 | if ( post_type_supports( $posttype, 'title' ) ) { |
||
| 86 | $metaboxes[ $posttype ][ 'titlediv'] = __( 'Title' ); |
||
| 87 | } |
||
| 88 | |||
| 89 | if ( post_type_supports( $posttype, 'editor' ) ) { |
||
| 90 | $metaboxes[ $posttype ][ 'postdivrich'] = __( 'Editor' ); |
||
| 91 | } |
||
| 92 | |||
| 93 | $this->load_default_metaboxes( $posttype ); |
||
| 94 | do_action( 'add_meta_boxes', $posttype, null ); |
||
| 95 | do_action( 'add_meta_boxes_' . $posttype, null ); |
||
| 96 | |||
| 97 | do_action( 'tabify_add_meta_boxes', $posttype ); |
||
| 98 | } |
||
| 99 | |||
| 100 | foreach ( $wp_meta_boxes as $posttype => $context ) { |
||
| 101 | foreach ( $context as $priorities ) { |
||
| 102 | foreach ( $priorities as $priority => $_metaboxes ) { |
||
| 103 | foreach ( $_metaboxes as $metabox ) { |
||
| 104 | // Metabox has been removed |
||
| 105 | if ( ! isset( $metabox['id'] ) ) { |
||
| 106 | continue; |
||
| 107 | } |
||
| 108 | |||
| 109 | $metaboxes[ $posttype ][ $metabox['id'] ] = $metabox['title']; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | |||
| 116 | return $metaboxes; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Call escape method when settings are being saved |
||
| 121 | * |
||
| 122 | * @param array $options list of options to be escaped |
||
| 123 | * |
||
| 124 | * @return array $options The escaped options |
||
| 125 | * |
||
| 126 | * @since 0.1.0 |
||
| 127 | */ |
||
| 128 | public function save_settings( $options ) { |
||
| 129 | $options['posttypes'] = $this->escape( $options['posttypes'] ); |
||
| 130 | |||
| 131 | return $options; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Sanitize the options array to be how we expect it to be |
||
| 136 | * |
||
| 137 | * @since 0.2.0 |
||
| 138 | * |
||
| 139 | * @param array $posttypes Raw options array |
||
| 140 | * |
||
| 141 | * @return array filtered options array |
||
| 142 | */ |
||
| 143 | private function escape( $posttypes ) { |
||
| 144 | $posttypes_keys = array_keys( $posttypes ); |
||
| 145 | $amount_posttypes = count( $posttypes ); |
||
| 146 | |||
| 147 | $kses_allowed_html = array( |
||
| 148 | 'b' => array(), |
||
| 149 | 'em' => array(), |
||
| 150 | 'i' => array(), |
||
| 151 | 'span' => array( |
||
| 152 | 'style' => true |
||
| 153 | ), |
||
| 154 | 'strong' => array( |
||
| 155 | 'style' => true |
||
| 156 | ) |
||
| 157 | ); |
||
| 158 | $kses_allowed_html = apply_filters( 'tabify_posttype_escape_kses', $kses_allowed_html ); |
||
| 159 | |||
| 160 | for ( $i = 0; $i < $amount_posttypes; $i++ ) { |
||
| 161 | $key = $posttypes_keys[ $i ]; |
||
| 162 | |||
| 163 | $posttypes[ $key ]['show'] = isset( $posttypes[ $key ]['show'] ); |
||
| 164 | $amount_tabs = count( $posttypes[ $key ]['tabs'] ); |
||
| 165 | |||
| 166 | for ( $j = 0; $j < $amount_tabs; $j++ ) { |
||
| 167 | if ( ! isset( $posttypes[ $key ]['tabs'][ $j ] ) ) { |
||
| 168 | continue; |
||
| 169 | } |
||
| 170 | |||
| 171 | $posttypes[ $key ]['tabs'][ $j ]['title'] = stripslashes( $posttypes[ $key ]['tabs'][ $j ]['title'] ); |
||
| 172 | $posttypes[ $key ]['tabs'][ $j ]['title'] = wp_kses( $posttypes[ $key ]['tabs'][ $j ]['title'], $kses_allowed_html ); |
||
| 173 | |||
| 174 | if ( ! isset( $posttypes[ $key ]['tabs'][ $j ]['items'] ) || count( $posttypes[ $key ]['tabs'][ $j ]['items'] ) == 0 ) { |
||
| 175 | if ( $posttypes[ $key ]['tabs'][ $j ]['title'] == '' ) { |
||
| 176 | unset( $posttypes[ $key ]['tabs'][ $j ] ); |
||
| 177 | } |
||
| 178 | else { |
||
| 179 | $posttypes[ $key ]['tabs'][ $j ]['items'] = array(); |
||
| 180 | } |
||
| 181 | |||
| 182 | continue; |
||
| 183 | } |
||
| 184 | |||
| 185 | $amount_metaboxes = count( $posttypes[ $key ]['tabs'][ $j ]['items'] ); |
||
| 186 | |||
| 187 | for ( $k = 0; $k < $amount_metaboxes; $k++ ) { |
||
| 188 | // Should the metabox be moved. Only applies when browser doesn't support Javascript |
||
| 189 | if ( |
||
| 190 | isset( $posttypes[ $key ]['tabs'][ $j ]['items_tab'][ $k ] ) && |
||
| 191 | $posttypes[ $key ]['tabs'][ $j ]['items_tab'][ $k ] != $j && |
||
| 192 | isset( $posttypes[ $key ]['tabs'][ intval( $posttypes[ $key ]['tabs'][ $j ]['items_tab'][ $k ] ) ] ) |
||
| 193 | ) { |
||
| 194 | $new_tab_key = intval( $posttypes[ $key ]['tabs'][ $j ]['items_tab'][ $k ] ); |
||
| 195 | |||
| 196 | $posttypes[ $key ]['tabs'][ $new_tab_key ]['items'][ $k ] = wp_strip_all_tags( $posttypes[ $key ]['tabs'][ $j ]['items'][ $k ] ); |
||
| 197 | unset( $posttypes[ $key ]['tabs'][ $j ]['items'][ $k ] ); |
||
| 198 | } |
||
| 199 | else { |
||
| 200 | $posttypes[ $key ]['tabs'][ $j ]['items'][ $k ] = wp_strip_all_tags( $posttypes[ $key ]['tabs'][ $j ]['items'][ $k ] ); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | unset( $posttypes[ $key ]['tabs'][ $j ]['items_tab'] ); |
||
| 205 | $posttypes[ $key ]['tabs'][ $j ]['items'] = array_values( $posttypes[ $key ]['tabs'][ $j ]['items'] ); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | return $posttypes; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Gets all the default WordPress metaboxes |
||
| 214 | * Little bit hackish but it works. Hopefully one day there will be a method for this in core. |
||
| 215 | * |
||
| 216 | * Found in wp-admin/edit-form-advanced.php. |
||
| 217 | * |
||
| 218 | * @param string $post_type The post type or which meta boxes need to be loaded |
||
| 219 | * |
||
| 220 | * @since 0.1.0 |
||
| 221 | */ |
||
| 222 | private function load_default_metaboxes( $post_type ) { |
||
| 223 | if ( 'attachment' == $post_type ) { |
||
| 224 | add_meta_box( 'submitdiv', __('Save'), 'attachment_submit_meta_box', $post_type, 'side', 'core' ); |
||
| 225 | add_meta_box( 'attachment-id3', __( 'Metadata' ), 'attachment_id3_data_meta_box', $post_type, 'normal', 'core' ); |
||
| 226 | } |
||
| 227 | else { |
||
| 228 | add_meta_box( 'submitdiv', __('Publish'), 'post_submit_meta_box', $post_type, 'side', 'core' ); |
||
| 229 | } |
||
| 230 | |||
| 231 | if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post_type, 'post-formats' ) ) { |
||
| 232 | add_meta_box( 'formatdiv', _x( 'Format', 'post format' ), 'post_format_meta_box', $post_type, 'side', 'core' ); |
||
| 233 | } |
||
| 234 | |||
| 235 | $this->load_taxonomy_metaboxes( $post_type ); |
||
| 236 | |||
| 237 | if ( post_type_supports( $post_type, 'page-attributes' ) ) { |
||
| 238 | add_meta_box( 'pageparentdiv', 'page' == $post_type ? __( 'Page Attributes' ) : __( 'Attributes' ), 'page_attributes_meta_box', $post_type, 'side', 'core' ); |
||
| 239 | } |
||
| 240 | |||
| 241 | if ( current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) ) { |
||
| 242 | add_meta_box( 'postimagediv', __('Featured Image'), 'post_thumbnail_meta_box', $post_type, 'side', 'low' ); |
||
| 243 | } |
||
| 244 | |||
| 245 | if ( post_type_supports( $post_type, 'excerpt' ) ) { |
||
| 246 | add_meta_box( 'postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', $post_type, 'normal', 'core' ); |
||
| 247 | } |
||
| 248 | |||
| 249 | if ( post_type_supports( $post_type, 'trackbacks' ) ) { |
||
| 250 | add_meta_box( 'trackbacksdiv', __('Send Trackbacks'), 'post_trackback_meta_box', $post_type, 'normal', 'core' ); |
||
| 251 | } |
||
| 252 | |||
| 253 | if ( post_type_supports( $post_type, 'custom-fields' ) ) { |
||
| 254 | add_meta_box( 'postcustom', __('Custom Fields'), 'post_custom_meta_box', $post_type, 'normal', 'core' ); |
||
| 255 | } |
||
| 256 | |||
| 257 | do_action('dbx_post_advanced'); |
||
| 258 | |||
| 259 | if ( post_type_supports( $post_type, 'comments' ) ) { |
||
| 260 | add_meta_box( 'commentstatusdiv', __('Discussion'), 'post_comment_status_meta_box', $post_type, 'normal', 'core' ); |
||
| 261 | } |
||
| 262 | |||
| 263 | if ( post_type_supports( $post_type, 'comments' ) ) { |
||
| 264 | add_meta_box( 'commentsdiv', __('Comments'), 'post_comment_meta_box', $post_type, 'normal', 'core' ); |
||
| 265 | } |
||
| 266 | |||
| 267 | add_meta_box( 'slugdiv', __('Slug'), 'post_slug_meta_box', $post_type, 'normal', 'core' ); |
||
| 268 | |||
| 269 | if ( post_type_supports( $post_type, 'author' ) ) { |
||
| 270 | add_meta_box( 'authordiv', __('Author'), 'post_author_meta_box', $post_type, 'normal', 'core' ); |
||
| 271 | } |
||
| 272 | |||
| 273 | if ( post_type_supports( $post_type, 'revisions' ) ) { |
||
| 274 | add_meta_box( 'revisionsdiv', __('Revisions'), 'post_revisions_meta_box', $post_type, 'normal', 'core' ); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Load taxonomy meta boxes |
||
| 280 | * |
||
| 281 | * @param string $post_type The post type or which meta boxes need to be loaded |
||
| 282 | * |
||
| 283 | * @since 1.0.0 |
||
| 284 | */ |
||
| 285 | private function load_taxonomy_metaboxes( $post_type ) { |
||
| 286 | foreach ( get_object_taxonomies( $post_type ) as $tax_name ) { |
||
| 287 | $taxonomy = get_taxonomy( $tax_name ); |
||
| 288 | |||
| 289 | if ( ! $taxonomy->show_ui ) { |
||
| 290 | continue; |
||
| 291 | } |
||
| 292 | |||
| 293 | $label = $taxonomy->labels->name; |
||
| 294 | |||
| 295 | if ( ! is_taxonomy_hierarchical( $tax_name ) ) { |
||
| 296 | add_meta_box( 'tagsdiv-' . $tax_name, $label, 'post_tags_meta_box', $post_type, 'side', 'core', array( 'taxonomy' => $tax_name ) ); |
||
| 297 | } |
||
| 298 | else { |
||
| 299 | add_meta_box( $tax_name . 'div', $label, 'post_categories_meta_box', $post_type, 'side', 'core', array( 'taxonomy' => $tax_name ) ); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | } |
||
| 304 | |||
| 305 | } |
||
| 306 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.