Commit e358e9c5 authored by Erick Hitter's avatar Erick Hitter

Add settings field for post types to Reading page and update filter used to...

Add settings field for post types to Reading page and update filter used to populate post types class variable to use same. Fixes #1.
parent 525498c3
...@@ -27,28 +27,141 @@ class Automatically_Paginate_Posts { ...@@ -27,28 +27,141 @@ class Automatically_Paginate_Posts {
* Class variables * Class variables
*/ */
private $post_types; private $post_types;
private $post_types_default = array( 'post' );
//Ensure option names match values in this::uninstall
private $option_name_post_types = 'autopaging_post_types';
private $meta_key_disable_autopaging = '_disable_autopaging'; private $meta_key_disable_autopaging = '_disable_autopaging';
/** /**
* Register actions and filters * Register actions and filters
* *
* @uses add_action, add_filter * @uses add_action, register_uninstall_hook, add_filter
* @return null * @return null
*/ */
public function __construct() { public function __construct() {
//Filters
add_action( 'init', array( $this, 'action_init' ) ); add_action( 'init', array( $this, 'action_init' ) );
//Admin settings
register_uninstall_hook( __FILE__, array( 'Automatically_Paginate_Posts', 'uninstall' ) );
add_filter( 'plugin_action_links', array( $this, 'filter_plugin_action_links' ), 10, 2 );
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
//Post-type settings
add_action( 'add_meta_boxes', array( $this, 'action_add_meta_boxes' ) ); add_action( 'add_meta_boxes', array( $this, 'action_add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'action_save_post' ) ); add_action( 'save_post', array( $this, 'action_save_post' ) );
add_filter( 'the_posts', array( $this, 'filter_the_posts' ) ); add_filter( 'the_posts', array( $this, 'filter_the_posts' ) );
} }
/** /**
* Set post types this plugin can act on, either from Reading page or via filter
* *
* @uses apply_filters, get_option
* @action init
* @return null
*/ */
public function action_init() { public function action_init() {
//Post types //Post types
$this->post_types = apply_filters( 'autopaging_post_types', array( 'post', 'page' ) ); $this->post_types = apply_filters( 'autopaging_post_types', get_option( $this->option_name_post_types, $this->post_types_default ) );
}
/**
* Delete plugin settings when uninstalled.
* Options names here must match those defined in Class Variables section above.
*
* @uses delete_option
* @action uninstall
* @return null
*/
public function uninstall() {
delete_option( 'autopaging_post_types' );
}
/**
* Add settings link to plugin's row actions
*
* @param array $actions
* @param string $file
* @filter plugin_action_links,
*/
public function filter_plugin_action_links( $actions, $file ) {
if ( false !== strpos( $file, basename( __FILE__ ) ) )
$actions[ 'settings' ] = '<a href="' . admin_url( 'options-reading.php' ) . '">Settings</a>';
return $actions;
}
/**
* Register settings and settings sections
* Settings appear on the Reading page
*
* @uses register_setting, add_settings_section, add_settings_field
* @action admin_init
* @return null
*/
public function action_admin_init() {
register_setting( 'reading', $this->option_name_post_types, array( $this, 'sanitize_supported_post_types' ) );
add_settings_section( 'autopaging', 'Automatically Paginate Posts', '__return_false', 'reading' );
add_settings_field( 'autopaging-post-types', __( 'Supported post types', 'autopaging' ), array( $this, 'settings_field_post_types' ), 'reading', 'autopaging' );
}
/**
* Render post types options
*
* @uses get_post_types, get_option, esc_attr, checked, esc_html
* @return string
*/
public function settings_field_post_types() {
//Get all public post types
$post_types = get_post_types( array(
'public' => true
), 'objects' );
//Remove attachments
unset( $post_types[ 'attachment' ] );
//Current settings
$current_types = get_option( $this->option_name_post_types, $this->post_types_default );
//Output checkboxes
foreach ( $post_types as $post_type => $atts ) :
?>
<input type="checkbox" name="<?php echo esc_attr( $this->option_name_post_types ); ?>[]" id="post-type-<?php echo esc_attr( $post_type ); ?>" value="<?php echo esc_attr( $post_type ); ?>"<?php checked( in_array( $post_type, $current_types ) ); ?> /> <label for="post-type-<?php echo esc_attr( $post_type ); ?>"><?php echo esc_html( $atts->label ); ?><br />
<?php
endforeach;
}
/**
* Sanitize post type inputs
*
* @param array $post_types_checked
* @uses get_post_types
* @return array
*/
public function sanitize_supported_post_types( $post_types_checked ) {
$post_types_sanitized = array();
//Ensure that only existing, public post types are submitted as valid options
if ( is_array( $post_types_checked ) && ! empty( $post_types_checked ) ) {
//Get all public post types
$post_types = get_post_types( array(
'public' => true
) );
//Remove attachments
unset( $post_types[ 'attachment' ] );
//Check input post types against those registered with WordPress and made available to this plugin
foreach ( $post_types_checked as $post_type ) {
if ( array_key_exists( $post_type, $post_types ) )
$post_types_sanitized[] = $post_type;
}
}
return $post_types_sanitized;
} }
/** /**
...@@ -58,7 +171,7 @@ class Automatically_Paginate_Posts { ...@@ -58,7 +171,7 @@ class Automatically_Paginate_Posts {
* @action add_meta_box * @action add_meta_box
* @return null * @return null
*/ */
function action_add_meta_boxes() { public function action_add_meta_boxes() {
foreach ( $this->post_types as $post_type ) { foreach ( $this->post_types as $post_type ) {
add_meta_box( 'autopaging', 'Post Autopaging', array( $this, 'meta_box_autopaging' ), $post_type, 'side' ); add_meta_box( 'autopaging', 'Post Autopaging', array( $this, 'meta_box_autopaging' ), $post_type, 'side' );
} }
...@@ -71,7 +184,7 @@ class Automatically_Paginate_Posts { ...@@ -71,7 +184,7 @@ class Automatically_Paginate_Posts {
* @uses esc_attr, checked, wp_nonce_field * @uses esc_attr, checked, wp_nonce_field
* @return string * @return string
*/ */
function meta_box_autopaging( $post ) { public function meta_box_autopaging( $post ) {
?> ?>
<p> <p>
<input type="checkbox" name="<?php echo esc_attr( $this->meta_key_disable_autopaging ); ?>" id="<?php echo esc_attr( $this->meta_key_disable_autopaging ); ?>_checkbox" value="1"<?php checked( (bool) get_post_meta( $post->ID, $this->meta_key_disable_autopaging, true ) ); ?> /> <label for="<?php echo esc_attr( $this->meta_key_disable_autopaging ); ?>_checkbox">Disable autopaging for this post?</label> <input type="checkbox" name="<?php echo esc_attr( $this->meta_key_disable_autopaging ); ?>" id="<?php echo esc_attr( $this->meta_key_disable_autopaging ); ?>_checkbox" value="1"<?php checked( (bool) get_post_meta( $post->ID, $this->meta_key_disable_autopaging, true ) ); ?> /> <label for="<?php echo esc_attr( $this->meta_key_disable_autopaging ); ?>_checkbox">Disable autopaging for this post?</label>
...@@ -90,7 +203,7 @@ class Automatically_Paginate_Posts { ...@@ -90,7 +203,7 @@ class Automatically_Paginate_Posts {
* @action save_post * @action save_post
* @return null * @return null
*/ */
function action_save_post( $post_id ) { public function action_save_post( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return; return;
...@@ -113,7 +226,7 @@ class Automatically_Paginate_Posts { ...@@ -113,7 +226,7 @@ class Automatically_Paginate_Posts {
* @filter the_posts * @filter the_posts
* @return array * @return array
*/ */
function filter_the_posts( $posts ) { public function filter_the_posts( $posts ) {
if ( ! is_admin() ) { if ( ! is_admin() ) {
foreach( $posts as $the_post ) { foreach( $posts as $the_post ) {
if ( in_array( $the_post->post_type, $this->post_types ) && ! preg_match( '#<!--nextpage-->#i', $the_post->post_content ) && ! (bool) get_post_meta( $the_post->ID, $this->meta_key_disable_autopaging, true ) ) { if ( in_array( $the_post->post_type, $this->post_types ) && ! preg_match( '#<!--nextpage-->#i', $the_post->post_content ) && ! (bool) get_post_meta( $the_post->ID, $this->meta_key_disable_autopaging, true ) ) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment