Add filters for post types

This commit is contained in:
Jay Wood
2015-08-29 13:13:02 -04:00
parent f8c5a911f2
commit 3f92ad967c

View File

@ -4,7 +4,7 @@ class CWV3Admin {
function hooks() { function hooks() {
// Post Meta Box for this. // Post Meta Box for this.
add_action( 'add_meta_boxes', array( $this, 'cw_meta' ) ); add_action( 'add_meta_boxes', array( $this, 'setup_metabox' ) );
add_action( 'save_post', array( $this, 'cwv3_meta_save' ) ); add_action( 'save_post', array( $this, 'cwv3_meta_save' ) );
add_action( 'admin_head', array( $this, 'render_lazy_mans_css' ) ); add_action( 'admin_head', array( $this, 'render_lazy_mans_css' ) );
@ -45,28 +45,42 @@ class CWV3Admin {
} }
public function cw_meta() { /**
$scr = array( 'post', 'page' ); * Add metabox to post types
foreach ( $scr as $screen ) { * @return void
add_meta_box( 'cwv3_meta_section', */
__( 'CWV3 Security', 'cwv3' ), public function setup_metabox() {
array( $this, 'render_metabox' ), $post_type = $this->get_cwv3_post_types();
$screen, if ( is_array( $post_type ) ) {
'side', foreach ( $post_type as $screen ) {
'high' add_meta_box( 'cwv3_meta_section',
); __( 'CWV3 Security', 'cwv3' ),
array( $this, 'render_metabox' ),
$screen,
'side',
'high'
);
}
} }
} }
public function cwv3_meta_save( $post_id ) { /**
* Gets the post types that can be used with CWv2
* @return array
*/
public function get_cwv3_post_types() {
$types = apply_filters( 'cwv3_post_types', array( 'post', 'page' ) );
$types = empty( $types ) ? array() : $types;
return ! is_array( $types ) ? array( $types ) : $types;
}
public function cwv3_meta_save( $post_id ) {
$post_types = $this->get_cwv3_post_types();
// check isset before access (edit by @jgraup) // check isset before access (edit by @jgraup)
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) { if ( isset( $_POST['post_type'] ) && in_array( $_POST['post_type'], $post_types ) ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) { if ( ! current_user_can( 'edit_page', $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
return; return;
} else { } else {
if ( ! current_user_can( 'edit_post', $post_id ) ) { return; }
if ( ! isset( $_POST['cwv3_meta'] ) || ! wp_verify_nonce( $_POST['cwv3_meta'], plugin_basename( __FILE__ ) ) ) { return; } if ( ! isset( $_POST['cwv3_meta'] ) || ! wp_verify_nonce( $_POST['cwv3_meta'], plugin_basename( __FILE__ ) ) ) { return; }
} }
} }