diff --git a/automatically-paginate-posts.php b/automatically-paginate-posts.php index c2ea7dea956f7e203b5a76fe69c53382a33265cc..bf960c8cb78aeff07e58fc9e1e14b70014309146 100644 --- a/automatically-paginate-posts.php +++ b/automatically-paginate-posts.php @@ -27,28 +27,141 @@ class Automatically_Paginate_Posts { * Class variables */ 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'; /** * Register actions and filters * - * @uses add_action, add_filter + * @uses add_action, register_uninstall_hook, add_filter * @return null */ public function __construct() { + //Filters 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( 'save_post', array( $this, 'action_save_post' ) ); 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() { //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 { * @action add_meta_box * @return null */ - function action_add_meta_boxes() { + public function action_add_meta_boxes() { foreach ( $this->post_types as $post_type ) { add_meta_box( 'autopaging', 'Post Autopaging', array( $this, 'meta_box_autopaging' ), $post_type, 'side' ); } @@ -71,7 +184,7 @@ class Automatically_Paginate_Posts { * @uses esc_attr, checked, wp_nonce_field * @return string */ - function meta_box_autopaging( $post ) { + public function meta_box_autopaging( $post ) { ?> <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> @@ -90,7 +203,7 @@ class Automatically_Paginate_Posts { * @action save_post * @return null */ - function action_save_post( $post_id ) { + public function action_save_post( $post_id ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; @@ -113,7 +226,7 @@ class Automatically_Paginate_Posts { * @filter the_posts * @return array */ - function filter_the_posts( $posts ) { + public function filter_the_posts( $posts ) { if ( ! is_admin() ) { 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 ) ) {