class-quick-edit-meta.php
<?php
/**
* Firefly
*
* @package Firefly
* @since 1.0.0
* @copyright Copyright (c) 2016 Liftable Media
* @license GPL-2.0+
*/
/**
* Quick Edit Metadata class
*
*/
class Firefly_Quick_Edit_Meta {
/**
* Metadata for Quick Edit
* Only supports checkboxes currently
*/
private $meta_fields = array(
array(
'key' => 'can_weekend_story',
'label' => 'CAN Weekend Story',
'type' => 'checkbox',
),
);
/**
* Primary class constructor
*
* @since 1.0.0
*/
function __construct() {
add_filter( 'manage_post_posts_columns', array( $this, 'posts_column' ) );
add_filter( 'manage_post_posts_custom_column', array( $this, 'posts_column_content' ), 10, 2 );
add_action( 'bulk_edit_custom_box', array( $this, 'bulk_quick_edit' ), 10, 2 );
add_action( 'quick_edit_custom_box', array( $this, 'bulk_quick_edit' ), 10, 2 );
add_action( 'admin_print_scripts-edit.php', array( $this, 'quick_edit_js' ) );
add_action( 'save_post', array( $this, 'save_quick_edit' ), 10, 2 );
add_action( 'wp_ajax_firefly_quick_edit_save', array( $this, 'save_bulk_edit' ) );
}
/**
* Add a Posts Column
*
*/
function posts_column( $columns ) {
$columns['firefly_meta'] = 'Post Settings';
return $columns;
}
/**
* Post Column Content
*
*/
function posts_column_content( $column_name, $post_id ) {
switch( $column_name ) {
case 'firefly_meta':
foreach( $this->meta_fields as $meta_field ) {
$meta_value = get_post_meta( $post_id, $meta_field['key'], true );
if( 'on' == $meta_value )
echo '<div id="' . $meta_field['key'] . '-' . $post_id . '">' . $meta_field['label'] . '</div>';
}
break;
}
}
/**
* Bulk and Quick Edit
*
*/
function bulk_quick_edit( $column_name, $post_type ) {
if( 'post' !== $post_type )
return;
switch( $column_name ) {
case 'firefly_meta':
echo '<fieldset class="inline-edit-col-right"><div class="inline-edit-col"><div class="inline-edit-group wp-clearfix">';
foreach( $this->meta_fields as $meta_field ) {
echo '<label class="alignleft"><input type="checkbox" name="' . $meta_field['key'] . '" value=""><span class="checkbox-title">' . $meta_field['label'] .'</span></label>';
}
echo '</div></div></fieldset>';
break;
}
}
/**
* Quick Edit JS
*
*/
function quick_edit_js() {
wp_enqueue_script( 'firefly-quick-edit', get_stylesheet_directory_uri() . '/js/quick-edit.js', array( 'jquery', 'inline-edit-post' ), '', true );
wp_localize_script( 'firefly-quick-edit', 'firefly_meta', $this->meta_fields );
}
/**
* Save Quick Edit
*
*/
function save_quick_edit( $post_id, $post ) {
// pointless if $_POST is empty (this happens on bulk edit)
if ( empty( $_POST ) )
return;
// verify quick edit nonce
if ( isset( $_POST[ '_inline_edit' ] ) && ! wp_verify_nonce( $_POST[ '_inline_edit' ], 'inlineeditnonce' ) )
return;
// don't save for autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// dont save for revisions
if ( isset( $post->post_type ) && $post->post_type == 'revision' )
return;
// Only run for 'post' post type
if( 'post' !== $post->post_type )
return;
// Update our post meta
foreach( $this->meta_fields as $meta_field ) {
if( array_key_exists( $meta_field['key'], $_POST ) )
update_post_meta( $post_id, $meta_field['key'], 'on' );
else
delete_post_meta( $post_id, $meta_field['key'] );
}
}
/**
* Save Bulk Edits
*
*/
function save_bulk_edit() {
$post_ids = ( isset( $_POST[ 'post_ids' ] ) && !empty( $_POST[ 'post_ids' ] ) ) ? $_POST[ 'post_ids' ] : NULL;
if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {
foreach( $post_ids as $post_id ) {
// Update our post meta
foreach( $this->meta_fields as $meta_field ) {
if( array_key_exists( $meta_field['key'], $_POST ) )
update_post_meta( $post_id, $meta_field['key'], 'on' );
else
delete_post_meta( $post_id, $meta_field['key'] );
}
}
}
}
}
new Firefly_Quick_Edit_Meta;
quick-edit.js
(function($) {
// we create a copy of the WP inline edit post function
var $wp_inline_edit = inlineEditPost.edit;
// and then we overwrite the function with our own code
inlineEditPost.edit = function( id ) {
// "call" the original WP edit function
// we don't want to leave WordPress hanging
$wp_inline_edit.apply( this, arguments );
// now we take care of our business
// get the post ID
var $post_id = 0;
if ( typeof( id ) == 'object' )
$post_id = parseInt( this.getId( id ) );
if ( $post_id > 0 ) {
// Set metadata
firefly_meta.forEach(function(element,index,array){
var $meta = $('#' + element.key + '-' + $post_id );
if( $meta.length )
$('#edit-' + $post_id).find('input[name="' + element.key + '"]').prop('checked', true);
});
}
};
$( '#bulk_edit' ).on( 'click', function() {
// define the bulk edit row
var $bulk_row = $( '#bulk-edit' );
// get the selected post ids that are being edited
var $post_ids = new Array();
$bulk_row.find( '#bulk-titles' ).children().each( function() {
$post_ids.push( $( this ).attr( 'id' ).replace( /^(ttle)/i, '' ) );
});
// basic data for AJAX query
var data = {
action: 'firefly_quick_edit_save',
post_ids: $post_ids,
};
// Add metadata
firefly_meta.forEach(function(element,index,array){
key = element.key;
value = 'value';
data[key] = value;
});
// save the data
$.ajax({
url: ajaxurl, // this is a variable that WordPress has already defined for us
type: 'POST',
async: false,
cache: false,
data: data,
});
});
})(jQuery);