Skip to content

Instantly share code, notes, and snippets.

@camtheperson
Created January 18, 2018 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camtheperson/2c8e36b9294b99d5b838d856ea234c31 to your computer and use it in GitHub Desktop.
Save camtheperson/2c8e36b9294b99d5b838d856ea234c31 to your computer and use it in GitHub Desktop.
Blog Feed Issue - extras.php
<?php
namespace Roots\Sage\Extras;
use Roots\Sage\Setup;
/**
* Add <body> classes
*/
function body_class($classes) {
// Add page slug if it doesn't exist
if (is_single() || is_page() && !is_front_page()) {
if (!in_array(basename(get_permalink()), $classes)) {
$classes[] = basename(get_permalink());
}
}
// Add class if sidebar is active
if (Setup\display_sidebar()) {
$classes[] = 'sidebar-primary';
}
return $classes;
}
add_filter('body_class', __NAMESPACE__ . '\\body_class');
/**
* Clean up the_excerpt()
*/
function excerpt_more() {
return ' &hellip; <a href="' . get_permalink() . '">' . __('Continued', 'sage') . '</a>';
}
add_filter('excerpt_more', __NAMESPACE__ . '\\excerpt_more');
// Set up custom post types and taxonomies
function create_taxs() {
register_taxonomy('program-category',
'programs',
array(
'labels' => array(
'name' => __( 'Program Categories' ),
'singular_name' => __( 'Program Category' )
),
'hierarchical' => true,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'program-category' ),
)
);
}
add_action( 'init', __NAMESPACE__ . '\\create_taxs' );
function create_cpts() {
// Programs
register_post_type('programs',
array(
'labels' => array(
'name' => __( 'Programs' ),
'singular_name' => __( 'Program' ),
'edit_item' => __( 'Edit Program' ),
'add_new' => __( 'Add New Program' ),
'add_new_item' => __( 'Add New Program' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array(
'with_front' => false,
'slug' => 'program'
),
'taxonomies' => array('program-category'),
'supports' => array('thumbnail', 'title', 'editor', 'excerpt'),
'menu_position' => 6,
'menu_icon' => 'dashicons-video-alt2'
)
);
// Episodes
register_post_type('episodes',
array(
'labels' => array(
'name' => __( 'Episodes' ),
'singular_name' => __( 'Episode' ),
'edit_item' => __( 'Edit Episode' ),
'add_new' => __( 'Add New Episode' ),
'add_new_item' => __( 'Add New Episode' )
),
'public' => true,
'has_archive' => false,
'rewrite' => array(
'with_front' => false,
'slug' => 'program/%program%/episode'
),
'supports' => array('thumbnail', 'title', 'editor', 'excerpt'),
'menu_position' => 7,
'menu_icon' => 'dashicons-media-video'
)
);
}
add_action( 'init', __NAMESPACE__ . '\\create_cpts' );
/**
* ACF Options page
*/
if (function_exists(acf_add_options_page)) {
acf_add_options_page();
}
/**
* Custom TinyMCE styles
*/
function enable_stylesheet($buttons) {
array_unshift($buttons, 'styleselect');
return $buttons;
}
add_filter('mce_buttons_2', __NAMESPACE__ . '\\enable_stylesheet' );
function custom_mce_styles( $init_array ) {
// Define the style_formats array
$style_formats = array(
array(
'title' => 'White Button',
'block' => 'span',
'classes' => 'button white',
'wrapper' => true,
),
array(
'title' => 'Green Button',
'block' => 'span',
'classes' => 'button green',
'wrapper' => true,
),
array(
'title' => 'Blue Button',
'block' => 'span',
'classes' => 'button blue',
'wrapper' => true,
),
array(
'title' => 'White Arrow',
'block' => 'span',
'classes' => 'arrow right-white',
'wrapper' => true,
),
array(
'title' => 'Blue Arrow',
'block' => 'span',
'classes' => 'arrow right-blue',
'wrapper' => true,
),
array(
'title' => 'IFRAME container',
'block' => 'span',
'classes' => 'iframe-container',
'wrapper' => true,
),
);
$init_array['style_formats'] = json_encode( $style_formats );
return $init_array;
}
add_filter('tiny_mce_before_init', __NAMESPACE__ . '\\custom_mce_styles');
// Custom columns for episodes
function episode_columns( $columns ) {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Episode' ),
'program' => __( 'Program' ),
'date' => __( 'Date' )
);
return $columns;
}
add_filter('manage_edit-episodes_columns', __NAMESPACE__ . '\\episode_columns');
function manage_episodes_columns($column, $post_id) {
if ($column == 'program') {
if ($program = get_field('program', $post_id)) {
$post = $program;
setup_postdata($post);
echo $post->post_title;
wp_reset_postdata();
}
} else {
_e('No Program Assigned');
}
}
add_action('manage_episodes_posts_custom_column', __NAMESPACE__ . '\\manage_episodes_columns', 10, 2);
function episodes_sortable_columns( $columns ) {
$columns['program'] = 'program';
return $columns;
}
add_filter('manage_edit-episodes_sortable_columns', __NAMESPACE__ . '\\episodes_sortable_columns');
// Custom program permalink for episodes
function add_directory_rewrite() {
global $wp_rewrite;
add_rewrite_tag('%program%', '(.+)');
add_rewrite_rule('^program/(.+)/episode/(.+)/', 'index.php?p=$matches[2]&program=$matches[2]', 'top');
add_permastruct('program', '%program%');
}
add_action('init', __NAMESPACE__ . '\\add_directory_rewrite');
function episode_rewrite($permalink, $post, $leavename) {
if ($post->post_type == 'episodes') {
if ($program = get_field('program', $post->ID)) {
$program = $program->post_name;
} else {
$program = 'default';
}
return str_replace('%program%', $program, $permalink);
} else {
return $permalink;
}
}
add_filter('post_type_link', __NAMESPACE__ . '\\episode_rewrite', 11, 3);
/*
* Function creates post duplicate as a draft and redirects then to the edit post screen
*/
function rd_duplicate_post_as_draft() {
global $wpdb;
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
wp_die('No post to duplicate has been supplied!');
}
/*
* Nonce verification
*/
if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) )
return;
/*
* get the original post id
*/
$post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
/*
* and all the original post data then
*/
$post = get_post( $post_id );
/*
* if you don't want current user to be the new post author,
* then change next couple of lines to this: $new_post_author = $post->post_author;
*/
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
/*
* if post data exists, create the post duplicate
*/
if (isset( $post ) && $post != null) {
/*
* new post data array
*/
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
/*
* insert the post by wp_insert_post() function
*/
$new_post_id = wp_insert_post( $args );
/*
* get all current post terms ad set them to the new post draft
*/
$taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}
/*
* duplicate all post meta just in two SQL queries
*/
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if (count($post_meta_infos)!=0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
if( $meta_key == '_wp_old_slug' ) continue;
$meta_value = addslashes($meta_info->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query($sql_query);
}
/*
* finally, redirect to the edit post screen for the new draft
*/
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
} else {
wp_die('Post creation failed, could not find original post: ' . $post_id);
}
}
add_action( 'admin_action_rd_duplicate_post_as_draft', __NAMESPACE__ . '\\rd_duplicate_post_as_draft' );
/*
* Add the duplicate link to action list for post_row_actions
*/
function rd_duplicate_post_link( $actions, $post ) {
if (current_user_can('edit_posts')) {
$actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=rd_duplicate_post_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
}
return $actions;
}
add_filter( 'post_row_actions', __NAMESPACE__ . '\\rd_duplicate_post_link', 10, 2 );
add_filter( 'episodes_row_actions', __NAMESPACE__ . '\\rd_duplicate_post_link', 10, 2 );
add_filter( 'programs_row_actions', __NAMESPACE__ . '\\rd_duplicate_post_link', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment