| <?php |
| |
| // Query |
| add_action('pre_get_posts', 'hwk_dynamic_pre_get_posts'); |
| function hwk_dynamic_pre_get_posts($query){ |
| if(is_admin() || !$query->is_main_query()) |
| return; |
| |
| // Post Types |
| if(is_home() || $query->is_post_type_archive() || is_singular()){ |
| if(!$post_type = (is_home()) ? 'post' : $query->get('post_type')) |
| return; |
| |
| $post_type_object = get_post_type_object($post_type); |
| |
| $rule = array(); |
| $rule['is_archive'] = is_post_type_archive($post_type_object->name); |
| $rule['has_archive'] = $post_type_object->has_archive; |
| |
| if($post_type == 'post'){ |
| $rule['is_archive'] = is_home(); |
| $rule['has_archive'] = true; |
| } |
| |
| // Single |
| if(is_singular($post_type)) |
| $query = apply_filters('hwk/post_type/' . $post_type . '/query/single', $query); |
| |
| // Archive |
| elseif($rule['has_archive'] && $rule['is_archive']) |
| $query = apply_filters('hwk/post_type/' . $post_type . '/query/archive', $query); |
| } |
| |
| // Taxonomies |
| elseif($query->is_tax() || $query->is_category() || $query->is_tag()){ |
| $queried_object = get_queried_object(); |
| if(!$queried_object || !isset($queried_object->taxonomy)) |
| return; |
| |
| $query = apply_filters('hwk/taxonomy/' . $queried_object->taxonomy . '/query', $query); |
| } |
| } |
| |
| // Template |
| add_filter('template_include', 'hwk_dynamic_template', 999); |
| function hwk_dynamic_template($template){ |
| // Post Types |
| if(is_singular() || is_post_type_archive() || is_home()){ |
| global $wp_query; |
| |
| $post_type = get_post_type(); |
| if(!$post_type && is_home()) |
| $post_type = 'post'; |
| |
| if(!$post_type && isset($wp_query->query['post_type'])) |
| $post_type = $wp_query->query['post_type']; |
| |
| if(!$post_type) |
| return $template; |
| |
| $post_type_object = get_post_type_object($post_type); |
| |
| $rule = array(); |
| $rule['is_archive'] = is_post_type_archive($post_type_object->name); |
| $rule['has_archive'] = $post_type_object->has_archive; |
| |
| if($post_type == 'post'){ |
| $rule['is_archive'] = is_home(); |
| $rule['has_archive'] = true; |
| } |
| |
| // Single |
| if(is_singular($post_type) && ($locate = locate_template(array(apply_filters('hwk/post_type/' . $post_type . '/template/single', $template))))) |
| return $locate; |
| |
| // Archive |
| elseif($rule['has_archive'] && $rule['is_archive'] && ($locate = locate_template(array(apply_filters('hwk/post_type/' . $post_type . '/template/archive', $template))))) |
| return $locate; |
| |
| return $template; |
| } |
| |
| // Taxonomies |
| elseif(is_tax() || is_category() || is_tag()){ |
| $queried_object = get_queried_object(); |
| if(!$queried_object || !isset($queried_object->taxonomy)) |
| return $template; |
| |
| if(($locate = locate_template(array(apply_filters('hwk/taxonomy/' . $queried_object->taxonomy . '/template', $template))))) |
| return $locate; |
| |
| return $template; |
| } |
| |
| return $template; |
| } |
| |
| // Post Type: Permalink |
| add_filter('post_type_link', 'hwk_dynamic_post_type_permalink', 10, 2); |
| add_filter('post_link', 'hwk_dynamic_post_type_permalink', 10, 2); |
| function hwk_dynamic_post_type_permalink($link, $post){ |
| if(!apply_filters('hwk/post_type/' . $post->post_type . '/args/no_single', false)) |
| return $link; |
| |
| if($post_type_archive_link = get_post_type_archive_link($post->post_type)) |
| return $post_type_archive_link; |
| |
| return home_url(); |
| } |
| |
| // Post Type: Rewrite Rules |
| add_action('init', 'hwk_dynamic_post_type_rewrite_rules', 999); |
| function hwk_dynamic_post_type_rewrite_rules(){ |
| $post_types = get_post_types(array('publicly_queryable' => true), 'objects'); |
| if(empty($post_types)) |
| return; |
| |
| foreach($post_types as $post_type){ |
| if(!apply_filters('hwk/post_type/' . $post_type->name . '/args/no_single', false)) |
| continue; |
| |
| add_filter($post_type->name . '_rewrite_rules', '__return_empty_array'); |
| } |
| } |
| |
| // Post Type: Row Actions |
| add_filter('page_row_actions', 'hwk_dynamic_post_type_row_actions', 10, 2); |
| add_filter('post_row_actions', 'hwk_dynamic_post_type_row_actions', 10, 2); |
| function hwk_dynamic_post_type_row_actions($actions, $post){ |
| if(!apply_filters('hwk/post_type/' . $post->post_type . '/args/no_single', false)) |
| return $actions; |
| |
| unset($actions['view']); |
| return $actions; |
| } |
| |
| // Post Type: Args |
| add_filter('register_post_type_args', 'hwk_dynamic_post_type_args', 10, 2); |
| function hwk_dynamic_post_type_args($args, $post_type){ |
| if(!apply_filters('hwk/post_type/' . $post_type . '/args/no_single', false)) |
| return $args; |
| |
| if($args['show_ui'] === null) |
| $args['show_ui'] = true; |
| |
| if($args['show_in_nav_menus'] === null) |
| $args['show_in_nav_menus'] = true; |
| |
| if($args['publicly_queryable'] === null) |
| $args['publicly_queryable'] = true; |
| |
| if($args['exclude_from_search'] === null) |
| $args['exclude_from_search'] = true; |
| |
| $args['public'] = false; |
| |
| return $args; |
| } |
| |
| // WP_Post |
| add_filter('posts_results', 'hwk_dynamic_post_type_wp_post', 10, 2); |
| function hwk_dynamic_post_type_wp_post($posts, $query){ |
| if(empty($posts)) |
| return $posts; |
| |
| foreach($posts as $post){ |
| $post = apply_filters('hwk/post_type/' . get_post_type($post) . '/query/post/all', $post, $query); |
| |
| if($query->is_singlular()) |
| $post = apply_filters('hwk/post_type/' . get_post_type($post) . '/query/post/single', $post, $query); |
| } |
| return $posts; |
| } |