| @@ -1,60 +1,155 @@ | ||
| 1 | 1 | <?php |
| 2 | -// Custom post types | |
| 3 | -function portfolio_post_init() { | |
| 4 | - $portfoliolabels = array( | |
| 5 | - 'name' => __('Portfolio', 'virtue'), | |
| 6 | - 'singular_name' => __('Portfolio Item', 'virtue'), | |
| 7 | - 'add_new' => __('Add New', 'virtue'), | |
| 8 | - 'add_new_item' => __('Add New Portfolio Item', 'virtue'), | |
| 9 | - 'edit_item' => __('Edit Portfolio Item', 'virtue'), | |
| 10 | - 'new_item' => __('New Portfolio Item', 'virtue'), | |
| 11 | - 'all_items' => __('All Portfolio', 'virtue'), | |
| 12 | - 'view_item' => __('View Portfolio Item', 'virtue'), | |
| 13 | - 'search_items' => __('Search Portfolio', 'virtue'), | |
| 14 | - 'not_found' => __('No Portfolio Item found', 'virtue'), | |
| 15 | - 'not_found_in_trash' => __('No Portfolio Items found in Trash', 'virtue'), | |
| 16 | - 'parent_item_colon' => '', | |
| 17 | - 'menu_name' => __('Portfolio', 'virtue') | |
| 18 | - ); | |
| 2 | +/** | |
| 3 | + * Init Custom post types | |
| 4 | + * | |
| 5 | + * @package Kadence Toolkit | |
| 6 | + */ | |
| 19 | 7 | |
| 20 | - $portargs = array( | |
| 21 | - 'labels' => $portfoliolabels, | |
| 22 | - 'public' => true, | |
| 23 | - 'publicly_queryable' => true, | |
| 24 | - 'show_ui' => true, | |
| 25 | - 'show_in_menu' => true, | |
| 26 | - 'query_var' => true, | |
| 27 | - 'rewrite' => array( 'slug' => 'portfolio' ), /* you can specify its url slug */ | |
| 28 | - 'has_archive' => false, | |
| 29 | - 'capability_type' => 'post', | |
| 30 | - 'hierarchical' => false, | |
| 31 | - 'menu_position' => 8, | |
| 32 | - 'menu_icon' => VIRTUE_TOOLKIT_URL .'/images/portfolio-icon.png', | |
| 33 | - 'supports' => array( 'title', 'editor', 'author', 'page-attributes', 'thumbnail', 'comments' ) | |
| 34 | - ); | |
| 35 | - // Initialize Taxonomy Labels | |
| 8 | +/** | |
| 9 | + * Init Portfolio posts type. | |
| 10 | + */ | |
| 11 | +function kadence_toolkit_portfolio_post_init() { | |
| 12 | + $portfoliolabels = array( | |
| 13 | + 'name' => __( 'Portfolio', 'virtue-toolkit' ), | |
| 14 | + 'singular_name' => __( 'Portfolio Item', 'virtue-toolkit' ), | |
| 15 | + 'add_new' => __( 'Add New', 'virtue-toolkit' ), | |
| 16 | + 'add_new_item' => __( 'Add New Portfolio Item', 'virtue-toolkit' ), | |
| 17 | + 'edit_item' => __( 'Edit Portfolio Item', 'virtue-toolkit' ), | |
| 18 | + 'new_item' => __( 'New Portfolio Item', 'virtue-toolkit' ), | |
| 19 | + 'all_items' => __( 'All Portfolio', 'virtue-toolkit' ), | |
| 20 | + 'view_item' => __( 'View Portfolio Item', 'virtue-toolkit' ), | |
| 21 | + 'search_items' => __( 'Search Portfolio', 'virtue-toolkit' ), | |
| 22 | + 'not_found' => __( 'No Portfolio Item found', 'virtue-toolkit' ), | |
| 23 | + 'not_found_in_trash' => __( 'No Portfolio Items found in Trash', 'virtue-toolkit' ), | |
| 24 | + 'parent_item_colon' => '', | |
| 25 | + 'menu_name' => __( 'Portfolio', 'virtue-toolkit' ), | |
| 26 | + ); | |
| 27 | + | |
| 28 | + $portargs = array( | |
| 29 | + 'labels' => $portfoliolabels, | |
| 30 | + 'public' => true, | |
| 31 | + 'publicly_queryable' => true, | |
| 32 | + 'show_ui' => true, | |
| 33 | + 'show_in_menu' => true, | |
| 34 | + 'query_var' => true, | |
| 35 | + // 'rewrite' => array( 'slug' => 'portfolio', 'feeds' => true),. | |
| 36 | + 'has_archive' => false, | |
| 37 | + 'capability_type' => 'post', | |
| 38 | + 'hierarchical' => false, | |
| 39 | + 'menu_position' => 8, | |
| 40 | + 'show_in_rest' => true, | |
| 41 | + 'menu_icon' => 'dashicons-format-gallery', | |
| 42 | + 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'page-attributes', 'thumbnail', 'custom-fields', 'comments' ), | |
| 43 | + ); | |
| 44 | + | |
| 45 | + // Initialize Taxonomy Labels. | |
| 36 | 46 | $worklabels = array( |
| 37 | - 'name' => __( 'Portfolio Type', 'virtue' ), | |
| 38 | - 'singular_name' => __( 'Type', 'virtue' ), | |
| 39 | - 'search_items' => __( 'Search Type', 'virtue' ), | |
| 40 | - 'all_items' => __( 'All Type', 'virtue' ), | |
| 41 | - 'parent_item' => __( 'Parent Type', 'virtue' ), | |
| 42 | - 'parent_item_colon' => __( 'Parent Type:', 'virtue' ), | |
| 43 | - 'edit_item' => __( 'Edit Type', 'virtue' ), | |
| 44 | - 'update_item' => __( 'Update Type', 'virtue' ), | |
| 45 | - 'add_new_item' => __( 'Add New Type', 'virtue' ), | |
| 46 | - 'new_item_name' => __( 'New Type Name', 'virtue' ), | |
| 47 | + 'name' => __( 'Portfolio Type', 'virtue-toolkit' ), | |
| 48 | + 'singular_name' => __( 'Type', 'virtue-toolkit' ), | |
| 49 | + 'search_items' => __( 'Search Type', 'virtue-toolkit' ), | |
| 50 | + 'all_items' => __( 'All Type', 'virtue-toolkit' ), | |
| 51 | + 'parent_item' => __( 'Parent Type', 'virtue-toolkit' ), | |
| 52 | + 'parent_item_colon' => __( 'Parent Type:', 'virtue-toolkit' ), | |
| 53 | + 'edit_item' => __( 'Edit Type', 'virtue-toolkit' ), | |
| 54 | + 'update_item' => __( 'Update Type', 'virtue-toolkit' ), | |
| 55 | + 'add_new_item' => __( 'Add New Type', 'virtue-toolkit' ), | |
| 56 | + 'new_item_name' => __( 'New Type Name', 'virtue-toolkit' ), | |
| 47 | 57 | ); |
| 48 | - // Register Custom Taxonomy | |
| 49 | - register_taxonomy('portfolio-type',array('portfolio'), array( | |
| 50 | - 'hierarchical' => true, // define whether to use a system like tags or categories | |
| 51 | - 'labels' => $worklabels, | |
| 52 | - 'show_ui' => true, | |
| 53 | - 'query_var' => true, | |
| 54 | - 'rewrite' => true, | |
| 55 | - )); | |
| 56 | 58 | |
| 57 | - register_post_type( 'portfolio', $portargs ); | |
| 59 | + // Allow slug to be filterd. | |
| 60 | + $portfolio_type_slug = apply_filters( 'kadence_portfolio_type_slug', 'portfolio-type' ); | |
| 61 | + | |
| 62 | + // Register Custom Taxonomy. | |
| 63 | + register_taxonomy( 'portfolio-type', array( 'portfolio' ), array( | |
| 64 | + 'hierarchical' => true, // define whether to use a system like tags or categories. | |
| 65 | + 'labels' => $worklabels, | |
| 66 | + 'show_ui' => true, | |
| 67 | + 'query_var' => true, | |
| 68 | + 'show_in_rest' => true, | |
| 69 | + 'rewrite' => array( 'slug' => $portfolio_type_slug ), | |
| 70 | + ) ); | |
| 71 | + | |
| 72 | + register_post_type( 'portfolio', $portargs ); | |
| 58 | 73 | } |
| 59 | -add_action( 'init', 'portfolio_post_init', 1 ); | |
| 60 | - | |
| 74 | +add_action( 'init', 'kadence_toolkit_portfolio_post_init', 1 ); | |
| 75 | + | |
| 76 | +/** | |
| 77 | + * Init Portfolio slug rewrite | |
| 78 | + */ | |
| 79 | +function kadence_toolkit_portfolio_permalink_init() { | |
| 80 | + global $wp_rewrite; | |
| 81 | + $port_rewrite = apply_filters( 'kadence_portfolio_permalink_slug', 'portfolio' ); | |
| 82 | + $portfolio_structure = '/' . $port_rewrite . '/%portfolio%'; | |
| 83 | + | |
| 84 | + $wp_rewrite->add_rewrite_tag( '%portfolio%', '([^/]+)', 'portfolio=' ); | |
| 85 | + $wp_rewrite->add_permastruct( 'portfolio', $portfolio_structure, false ); | |
| 86 | +} | |
| 87 | +add_action( 'init', 'kadence_toolkit_portfolio_permalink_init', 2 ); | |
| 88 | + | |
| 89 | +/** | |
| 90 | + * Run Portfolio slug rewrite | |
| 91 | + * | |
| 92 | + * @param string $permalink incoming permalink setting. | |
| 93 | + * @param int $post_id incoming post ID. | |
| 94 | + * @param string $leavename incoming name. | |
| 95 | + */ | |
| 96 | +function kadence_toolkit_portfolio_permalink( $permalink, $post_id, $leavename ) { | |
| 97 | + $post = get_post( $post_id ); | |
| 98 | + $rewritecode = array( | |
| 99 | + '%year%', | |
| 100 | + '%monthnum%', | |
| 101 | + '%day%', | |
| 102 | + '%hour%', | |
| 103 | + '%minute%', | |
| 104 | + '%second%', | |
| 105 | + $leavename ? '' : '%postname%', | |
| 106 | + '%post_id%', | |
| 107 | + '%category%', | |
| 108 | + '%author%', | |
| 109 | + $leavename ? '' : '%pagename%', | |
| 110 | + ); | |
| 111 | + | |
| 112 | + if ( ! empty( $permalink ) && ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) { | |
| 113 | + $unixtime = strtotime( $post->post_date ); | |
| 114 | + $category = ''; | |
| 115 | + $author = ''; | |
| 116 | + if ( strpos( $permalink, '%category%' ) !== false ) { | |
| 117 | + $cats = wp_get_post_terms( $post->ID, 'portfolio-type', array( | |
| 118 | + 'orderby' => 'parent', | |
| 119 | + 'order' => 'DESC', | |
| 120 | + ) ); | |
| 121 | + if ( $cats ) { | |
| 122 | + // usort($cats, '_usort_terms_by_ID'); // order by ID. | |
| 123 | + $category = $cats[0]->slug; | |
| 124 | + } | |
| 125 | + // show default category in permalinks, without. | |
| 126 | + // having to assign it explicitly. | |
| 127 | + if ( empty( $category ) ) { | |
| 128 | + $category = 'portfolio-category'; | |
| 129 | + } | |
| 130 | + } | |
| 131 | + | |
| 132 | + if ( strpos( $permalink, '%author%' ) !== false ) { | |
| 133 | + $authordata = get_userdata( $post->post_author ); | |
| 134 | + $author = $authordata->user_nicename; | |
| 135 | + } | |
| 136 | + | |
| 137 | + $date = explode( ' ', date( 'Y m d H i s', $unixtime ) ); | |
| 138 | + $rewritereplace = array( | |
| 139 | + $date[0], | |
| 140 | + $date[1], | |
| 141 | + $date[2], | |
| 142 | + $date[3], | |
| 143 | + $date[4], | |
| 144 | + $date[5], | |
| 145 | + $post->post_name, | |
| 146 | + $post->ID, | |
| 147 | + $category, | |
| 148 | + $author, | |
| 149 | + $post->post_name, | |
| 150 | + ); | |
| 151 | + $permalink = str_replace( $rewritecode, $rewritereplace, $permalink ); | |
| 152 | + } | |
| 153 | + return $permalink; | |
| 154 | +} | |
| 155 | +add_filter( 'post_type_link', 'kadence_toolkit_portfolio_permalink', 10, 3 ); | |