> Show Custom Taxonomy Filters functionality.
*/
public $post_type ;
/**
* Show featured images column in list tables for pages and post types that support featured image
*
* @since 1.0.0
*/
public function show_featured_image_column()
{
$post_types = get_post_types( array(
'public' => true,
), 'names' );
foreach ( $post_types as $post_type_key => $post_type_name ) {
if ( post_type_supports( $post_type_key, 'thumbnail' ) ) {
add_filter( "manage_{$post_type_name}_posts_columns", [ $this, 'add_featured_image_column' ], 999 );
add_action(
"manage_{$post_type_name}_posts_custom_column",
[ $this, 'add_featured_image' ],
10,
2
);
}
}
}
/**
* Add a column called Featured Image as the first column
*
* @param mixed $columns
* @return void
* @since 1.0.0
*/
public function add_featured_image_column( $columns )
{
$new_columns = array();
foreach ( $columns as $key => $value ) {
if ( 'title' == $key ) {
// We add featured image column before the 'title' column
$new_columns['asenha-featured-image'] = 'Featured Image';
}
if ( 'thumb' == $key ) {
// For WooCommerce products, we add featured image column before it's native thumbnail column
$new_columns['asenha-featured-image'] = 'Product Image';
}
$new_columns[$key] = $value;
}
// Replace WooCommerce thumbnail column with ASE featured image column
if ( array_key_exists( 'thumb', $new_columns ) ) {
unset( $new_columns['thumb'] );
}
return $new_columns;
}
/**
* Echo featured image's in thumbnail size to a column
*
* @param mixed $column_name
* @param mixed $id
* @since 1.0.0
*/
public function add_featured_image( $column_name, $id )
{
if ( 'asenha-featured-image' === $column_name ) {
if ( has_post_thumbnail( $id ) ) {
$size = 'thumbnail';
echo get_the_post_thumbnail( $id, $size, '' ) ;
} else {
echo '
' ;
}
}
}
/**
* Show excerpt column in list tables for pages and post types that support excerpt.
*
* @since 1.0.0
*/
public function show_excerpt_column()
{
$post_types = get_post_types( array(
'public' => true,
), 'names' );
foreach ( $post_types as $post_type_key => $post_type_name ) {
if ( post_type_supports( $post_type_key, 'excerpt' ) ) {
add_filter( "manage_{$post_type_name}_posts_columns", [ $this, 'add_excerpt_column' ] );
add_action(
"manage_{$post_type_name}_posts_custom_column",
[ $this, 'add_excerpt' ],
10,
2
);
}
}
}
/**
* Add a column called Featured Image as the first column
*
* @param mixed $columns
* @return void
* @since 1.0.0
*/
public function add_excerpt_column( $columns )
{
$new_columns = array();
foreach ( $columns as $key => $value ) {
$new_columns[$key] = $value;
if ( $key == 'title' ) {
$new_columns['asenha-excerpt'] = 'Excerpt';
}
}
return $new_columns;
}
/**
* Echo featured image's in thumbnail size to a column
*
* @param mixed $column_name
* @param mixed $id
* @since 1.0.0
*/
public function add_excerpt( $column_name, $id )
{
if ( 'asenha-excerpt' === $column_name ) {
$excerpt = get_the_excerpt( $id );
// about 310 characters
$excerpt = substr( $excerpt, 0, 160 );
// truncate to 160 characters
$short_excerpt = substr( $excerpt, 0, strrpos( $excerpt, ' ' ) );
echo wp_kses_post( $short_excerpt ) ;
}
}
/**
* Add ID column list table of pages, posts, custom post types, media, taxonomies, custom taxonomies, users amd comments
*
* @since 1.0.0
*/
public function show_id_column()
{
// For pages and hierarchical post types list table
add_filter( 'manage_pages_columns', [ $this, 'add_id_column' ] );
add_action(
'manage_pages_custom_column',
[ $this, 'add_id_echo_value' ],
10,
2
);
// For posts and non-hierarchical custom posts list table
add_filter( 'manage_posts_columns', [ $this, 'add_id_column' ] );
add_action(
'manage_posts_custom_column',
[ $this, 'add_id_echo_value' ],
10,
2
);
// For media list table
add_filter( 'manage_media_columns', [ $this, 'add_id_column' ] );
add_action(
'manage_media_custom_column',
[ $this, 'add_id_echo_value' ],
10,
2
);
// For list table of all taxonomies
$taxonomies = get_taxonomies( [
'public' => true,
], 'names' );
foreach ( $taxonomies as $taxonomy ) {
add_filter( 'manage_edit-' . $taxonomy . '_columns', [ $this, 'add_id_column' ] );
add_action(
'manage_' . $taxonomy . '_custom_column',
[ $this, 'add_id_return_value' ],
10,
3
);
}
// For users list table
add_filter( 'manage_users_columns', [ $this, 'add_id_column' ] );
add_action(
'manage_users_custom_column',
[ $this, 'add_id_return_value' ],
10,
3
);
// For comments list table
add_filter( 'manage_edit-comments_columns', [ $this, 'add_id_column' ] );
add_action(
'manage_comments_custom_column',
[ $this, 'add_id_echo_value' ],
10,
3
);
}
/**
* Add a column called ID
*
* @param mixed $columns
* @return void
* @since 1.0.0
*/
public function add_id_column( $columns )
{
$columns['asenha-id'] = 'ID';
return $columns;
}
/**
* Echo post ID value to a column
*
* @param mixed $column_name
* @param mixed $id
* @since 1.0.0
*/
public function add_id_echo_value( $column_name, $id )
{
if ( 'asenha-id' === $column_name ) {
echo esc_html( $id ) ;
}
}
/**
* Return post ID value to a column
*
* @param mixed $value
* @param mixed $column_name
* @param mixed $id
* @since 1.0.0
*/
public function add_id_return_value( $value, $column_name, $id )
{
if ( 'asenha-id' === $column_name ) {
$value = $id;
}
return $value;
}
/**
* Add ID in the action row of list tables for pages, posts, custom post types, media, taxonomies, custom taxonomies, users amd comments
*
* @since 4.7.4
*/
public function show_id_in_action_row()
{
add_filter(
'page_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
add_filter(
'post_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
add_filter(
'cat_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
add_filter(
'tag_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
add_filter(
'media_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
add_filter(
'comment_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
add_filter(
'user_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
}
/**
* Output the ID in the action row
*
* @since 4.7.4
*/
public function add_id_in_action_row( $actions, $object )
{
if ( current_user_can( 'edit_posts' ) ) {
// For pages, posts, custom post types, media/attachments, users
if ( property_exists( $object, 'ID' ) ) {
$id = $object->ID;
}
// For taxonomies
if ( property_exists( $object, 'term_id' ) ) {
$id = $object->term_id;
}
// For comments
if ( property_exists( $object, 'comment_ID' ) ) {
$id = $object->comment_ID;
}
$actions['asenha-list-table-item-id'] = 'ID: ' . $id . '';
}
return $actions;
}
/**
* Hide comments column in list tables for pages, post types that support comments, and alse media/attachments.
*
* @since 1.0.0
*/
public function hide_comments_column()
{
$post_types = get_post_types( array(
'public' => true,
), 'names' );
foreach ( $post_types as $post_type_key => $post_type_name ) {
if ( post_type_supports( $post_type_key, 'comments' ) ) {
if ( 'attachment' != $post_type_name ) {
// For list tables of pages, posts and other post types
add_filter( "manage_{$post_type_name}_posts_columns", [ $this, 'remove_comment_column' ] );
} else {
// For list table of media/attachment
add_filter( 'manage_media_columns', [ $this, 'remove_comment_column' ] );
}
}
}
}
/**
* Add a column called ID
*
* @param mixed $columns
* @return void
* @since 1.0.0
*/
public function remove_comment_column( $columns )
{
unset( $columns['comments'] );
return $columns;
}
/**
* Hide tags column in list tables for posts.
*
* @since 1.0.0
*/
public function hide_post_tags_column()
{
$post_types = get_post_types( array(
'public' => true,
), 'names' );
foreach ( $post_types as $post_type_key => $post_type_name ) {
if ( $post_type_name == 'post' ) {
add_filter( "manage_posts_columns", [ $this, 'remove_post_tags_column' ] );
}
}
}
/**
* Add a column called ID
*
* @param mixed $columns
* @return void
* @since 1.0.0
*/
public function remove_post_tags_column( $columns )
{
unset( $columns['tags'] );
return $columns;
}
/**
* Show custom (hierarchical) taxonomy filter(s) for all post types.
*
* @since 1.0.0
*/
public function show_custom_taxonomy_filters( $post_type )
{
$post_taxonomies = get_object_taxonomies( $post_type, 'objects' );
// Only show custom taxonomy filters for post types other than 'post'
if ( 'post' != $post_type ) {
array_walk( $post_taxonomies, [ $this, 'output_taxonomy_filter' ] );
}
}
/**
* Output filter on the post type's list table for a taxonomy
*
* @since 1.0.0
*/
public function output_taxonomy_filter( $post_taxonomy )
{
// Only show taxonomy filter when the taxonomy is hierarchical
if ( true === $post_taxonomy->hierarchical ) {
wp_dropdown_categories( array(
'show_option_all' => sprintf( 'All %s', $post_taxonomy->label ),
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'hide_if_empty' => true,
'selected' => filter_input( INPUT_GET, $post_taxonomy->query_var, FILTER_SANITIZE_STRING ),
'hierarchical' => true,
'name' => $post_taxonomy->query_var,
'taxonomy' => $post_taxonomy->name,
'value_field' => 'slug',
) );
}
}
/**
* Enable duplication of pages, posts and custom posts
*
* @since 1.0.0
*/
public function asenha_enable_duplication()
{
$original_post_id = intval( sanitize_text_field( $_REQUEST['post'] ) );
$nonce = sanitize_text_field( $_REQUEST['nonce'] );
if ( wp_verify_nonce( $nonce, 'asenha-duplicate-' . $original_post_id ) && current_user_can( 'edit_posts' ) ) {
$original_post = get_post( $original_post_id );
$post_type = $original_post->post_type;
// Not WooCommerce product
if ( 'product' != $post_type ) {
// Set some attributes for the duplicate post
$new_post_title_suffix = ' (DUPLICATE)';
$new_post_status = 'draft';
$current_user = wp_get_current_user();
$new_post_author_id = $current_user->ID;
// Create the duplicate post and store the ID
$args = array(
'comment_status' => $original_post->comment_status,
'ping_status' => $original_post->ping_status,
'post_author' => $new_post_author_id,
'post_content' => $original_post->post_content,
'post_excerpt' => $original_post->post_excerpt,
'post_parent' => $original_post->post_parent,
'post_password' => $original_post->post_password,
'post_status' => $new_post_status,
'post_title' => $original_post->post_title . $new_post_title_suffix,
'post_type' => $original_post->post_type,
'to_ping' => $original_post->to_ping,
'menu_order' => $original_post->menu_order,
);
$new_post_id = wp_insert_post( $args );
// Copy over the taxonomies
$original_taxonomies = get_object_taxonomies( $original_post->post_type );
if ( !empty($original_taxonomies) && is_array( $original_taxonomies ) ) {
foreach ( $original_taxonomies as $taxonomy ) {
$original_post_terms = wp_get_object_terms( $original_post_id, $taxonomy, array(
'fields' => 'slugs',
) );
wp_set_object_terms(
$new_post_id,
$original_post_terms,
$taxonomy,
false
);
}
}
// Copy over the post meta
$original_post_metas = get_post_meta( $original_post_id );
// all meta keys and the corresponding values
if ( !empty($original_post_metas) ) {
foreach ( $original_post_metas as $meta_key => $meta_values ) {
foreach ( $meta_values as $meta_value ) {
update_post_meta( $new_post_id, $meta_key, wp_slash( maybe_unserialize( $meta_value ) ) );
}
}
}
}
// Redirect to list table of the corresponding post type of original post
// $post_type = get_post_type( $original_post_id );
if ( 'post' == $post_type ) {
wp_redirect( admin_url( 'edit.php' ) );
} else {
wp_redirect( admin_url( 'edit.php?post_type=' . $post_type ) );
}
} else {
wp_die( 'You do not have permission to perform this action.' );
}
}
/**
* Add row action link to perform duplication in page/post list tables
*
* @since 1.0.0
*/
public function add_duplication_action_link( $actions, $post )
{
$post_type = $post->post_type;
if ( current_user_can( 'edit_posts' ) ) {
// Not WooCommerce product
if ( 'product' != $post_type ) {
$actions['asenha-duplicate'] = 'Duplicate';
}
}
return $actions;
}
/**
* Modify the 'Edit' link to be 'Edit or Replace'
*
*/
public function modify_media_list_table_edit_link( $actions, $post )
{
$new_actions = array();
foreach ( $actions as $key => $value ) {
if ( $key == 'edit' ) {
$new_actions['edit'] = 'Edit or Replace';
} else {
$new_actions[$key] = $value;
}
}
return $new_actions;
}
/**
* Add "Custom Order" sub-menu for post types
*
* @since 5.0.0
*/
public function add_content_order_submenu( $context )
{
$options = get_option( ASENHA_SLUG_U, array() );
$content_order_for = $options['content_order_for'];
$content_order_enabled_post_types = array();
foreach ( $options['content_order_for'] as $post_type_slug => $is_custom_order_enabled ) {
if ( $is_custom_order_enabled ) {
$post_type_object = get_post_type_object( $post_type_slug );
if ( is_object( $post_type_object ) && property_exists( $post_type_object, 'labels' ) ) {
$post_type_name_plural = $post_type_object->labels->name;
if ( 'post' == $post_type_slug ) {
$hook_suffix = add_posts_page(
$post_type_name_plural . ' Order',
// Page title
'Order',
// Menu title
'edit_pages',
// Capability required
'custom-order-posts',
// Menu and page slug
[ $this, 'custom_order_page_output' ]
);
} else {
$hook_suffix = add_submenu_page(
'edit.php?post_type=' . $post_type_slug,
// Parent (menu) slug. Ref: https://developer.wordpress.org/reference/functions/add_submenu_page/#comment-1404
$post_type_name_plural . ' Order',
// Page title
'Order',
// Menu title
'edit_pages',
// Capability required
'custom-order-' . $post_type_slug,
// Menu and page slug
[ $this, 'custom_order_page_output' ]
);
}
add_action( 'admin_print_styles-' . $hook_suffix, [ $this, 'enqueue_content_order_styles' ] );
add_action( 'admin_print_scripts-' . $hook_suffix, [ $this, 'enqueue_content_order_scripts' ] );
}
}
}
}
/**
* Output content for the custom order page for each enabled post types
* Not using settings API because all done via AJAX
*
* @since 5.0.0
*/
public function custom_order_page_output()
{
$parent_slug = get_admin_page_parent();
if ( 'edit.php' == $parent_slug ) {
$post_type_slug = 'post';
} else {
$post_type_slug = str_replace( 'edit.php?post_type=', '', $parent_slug );
}
// Object with properties for each post status and the count of posts for each status
// $post_count_object = wp_count_posts( $post_type_slug );
// Number of items with the status 'publish(ed)', 'future' (scheduled), 'draft', 'pending' and 'private'
// $post_count = absint( $post_count_object->publish )
// + absint( $post_count_object->future )
// + absint( $post_count_object->draft )
// + absint( $post_count_object->pending )
// + absint( $post_count_object->private );
?>
The current file will be replaced with the uploaded and/or selected file while retaining the current ID, publish date and file name. Thus, no existing links will break.
Note that you can only replace with the same file type, e.g. JPG can only be replaced by JPG.
ID, '_menu_item_target_blank', true ); if ( $target_blank ) { $atts['target'] = '_blank'; $atts['rel'] = 'noopener noreferrer nofollow'; } return $atts; } /** * Publish posts of any type with missed schedule. * We use the Transients API to reduce straining the site with DB queries on busy sites. * So, this function will only query the DB once every 15 minutes at most. * * @since 3.1.0 */ public function publish_missed_schedule_posts() { if ( is_front_page() || is_home() || is_page() || is_single() || is_singular() || is_archive() || is_admin() || is_blog_admin() || is_robots() || is_ssl() ) { // Get missed schedule posts data from cache $missed_schedule_posts = get_transient( 'asenha_missed_schedule_posts' ); // Nothing found in cache if ( false === $missed_schedule_posts ) { global $wpdb ; $current_gmt_datetime = gmdate( 'Y-m-d H:i:00' ); $args = array( 'public' => true, '_builtin' => false, ); $custom_post_types = get_post_types( $args, 'names' ); // array, e.g. array( 'project', 'book', 'staff' ) if ( count( $custom_post_types ) > 0 ) { $custom_post_types = "'" . implode( "','", $custom_post_types ) . "'"; // string, e.g. 'project','book','staff' $post_types = "'page','post'," . $custom_post_types; // 'page','post','project','book','staff' } else { $post_types = "'page','post'"; } $sql = "SELECT ID FROM {$wpdb->posts} WHERE post_type IN ({$post_types}) AND post_status='future' AND post_date_gmt<'{$current_gmt_datetime}'"; // The following does not work as backslashes are inserted before single quotes in $post_types // $sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type IN (%s) AND post_status='future' AND post_date_gmt<'%s'", array( $post_types, $current_gmt_datetime ) ); $missed_schedule_posts = $wpdb->get_results( $sql, ARRAY_A ); // Save query results as a transient with expiry of 15 minutes set_transient( 'asenha_missed_schedule_posts', $missed_schedule_posts, 15 * MINUTE_IN_SECONDS ); } if ( empty($missed_schedule_posts) || !is_array( $missed_schedule_posts ) ) { return; } foreach ( $missed_schedule_posts as $post ) { wp_publish_post( $post['ID'] ); } } } }