array( 'name' => __( 'Grid Layouts', 'us' ), 'singular_name' => __( 'Grid Layout', 'us' ), 'add_new' => __( 'Add Grid Layout', 'us' ), 'add_new_item' => __( 'Add Grid Layout', 'us' ), ), 'public' => TRUE, 'show_in_menu' => 'us-theme-options', 'exclude_from_search' => TRUE, 'show_in_admin_bar' => FALSE, 'publicly_queryable' => FALSE, 'show_in_nav_menus' => FALSE, 'capability_type' => array( 'us_page_block', 'us_page_blocks' ), 'map_meta_cap' => TRUE, 'supports' => FALSE, 'rewrite' => FALSE, 'register_meta_box_cb' => 'usgb_us_grid_type_pages', ) ); // Add "Used in" column into Grid Layouts admin page add_filter( 'manage_us_grid_layout_posts_columns', 'ushb_us_grid_layout_columns_head' ); add_action( 'manage_us_grid_layout_posts_custom_column', 'ushb_us_grid_layout_columns_content', 10, 2 ); function ushb_us_grid_layout_columns_head( $defaults ) { $result = array(); foreach ( $defaults as $key => $title ) { if ( $key == 'date' ) { $result['used_in'] = __( 'Used in', 'us' ); } $result[$key] = $title; } return $result; } function ushb_us_grid_layout_columns_content( $column_name, $post_ID ) { if ( $column_name == 'used_in' ) { echo us_get_used_in_locations( $post_ID ); } } } function usgb_us_grid_type_pages() { global $post; // Dev note: This check is not necessary, but still wanted to make sure this function won't be bound somewhere else if ( ! ( $post instanceof WP_Post ) OR $post->post_type !== 'us_grid_layout' ) { return; } if ( $post->post_status === 'auto-draft' ) { // Page for creating new grid: creating it instantly and proceeding to editing $post_data = array( 'ID' => $post->ID ); // Retrieving occupied names to generate new post title properly $existing_grids = usgb_get_existing_grids(); if ( isset( $_GET['duplicate_from'] ) AND ( $original_post = get_post( (int) $_GET['duplicate_from'] ) ) !== NULL ) { // Handling post duplication $post_data['post_content'] = $original_post->post_content; $title_pattern = $original_post->post_title . ' (%d)'; $cur_index = 2; } else { // Handling creation from scratch $title_pattern = __( 'Layout', 'us' ) . ' %d'; $cur_index = count( $existing_grids ) + 1; } // Generating new post title while ( in_array( $post_data['post_title'] = sprintf( $title_pattern, $cur_index ), $existing_grids ) ) { $cur_index ++; } wp_update_post( $post_data ); wp_publish_post( $post->ID ); // Redirect if ( isset( $_GET['duplicate_from'] ) ) { // When duplicating post, showing posts list next wp_redirect( admin_url( 'edit.php?post_type=us_grid_layout' ) ); } else { // When creating from scratch proceeding to post editing next wp_redirect( admin_url( 'post.php?post=' . $post->ID . '&action=edit' ) ); } } else { // Page for editing a grid add_action( 'admin_enqueue_scripts', 'usgb_enqueue_scripts' ); add_action( 'edit_form_top', 'usgb_edit_form_top' ); } } /** * Get available grids * @return array */ function usgb_get_existing_grids() { $result = array(); $grids = get_posts( array( 'post_type' => 'us_grid_layout', 'posts_per_page' => - 1, 'post_status' => 'any', 'suppress_filters' => 0, ) ); foreach ( $grids as $grid ) { $result[$grid->ID] = $grid->post_title; } return $result; } function usgb_enqueue_scripts() { // Appending dependencies usof_print_styles(); usof_print_scripts(); // Appending required assets global $us_template_directory_uri; wp_enqueue_script( 'us-grid-builder', $us_template_directory_uri . '/framework/admin/js/grid-builder.js', array( 'usof-scripts' ), TRUE ); // Disabling WP auto-save wp_dequeue_script( 'autosave' ); } function usgb_edit_form_top( $post ) { echo '