* @package ${1:ScreenOptionsClass} * @license https://www.gnu.org/licenses/gpl-3.0.en.html */ /* Copyright (C) 2018 Chris Reynolds This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Main Screen Options Framework class. */ class ${1:ScreenOptionsClass} { /** * The class instance. * * @var null */ private static \$instance = null; /** * Creates or returns an instance of this class. * * @since 1.0.0 * @return ${1:ScreenOptionsClass} A single instance of this class. */ public static function get_instance() { if ( null === self::\$instance ) { self::\$instance = new self(); } return self::\$instance; } /** * The class constructor. */ private function __construct() { $admin_page = ${2:\$admin_page}; add_action( "load-\$admin_page", [ \$this, 'get_screen_options' ] ); add_filter( 'screen_settings', [ \$this, 'show_screen_options' ], 10, 2 ); add_filter( 'set-screen-option', [ \$this, 'set_option' ], 11, 3 ); } /** * Array of screen options to display. * * @return array The screen option function names. */ private function screen_options() { \$screen_options = []; foreach ( ${3:function_to_get_options()} as \$option_name ) { \$screen_options[] = [ 'option' => \$option_name, 'title' => ucwords( \$option_name ), ]; } return \$screen_options; } /** * Register the screen options. */ public function get_screen_options() { \$screen = get_current_screen(); if ( ! is_object( \$screen ) || ${2:\$admin_page} !== \$screen->id ) { return; } // Loop through all the options and add a screen option for each. foreach ( ${3:function_to_get_options()} as \$option_name ) { add_screen_option( "${4:option_identifier}_\$option_name", [ 'option' => \$option_name, 'value' => true, ] ); } } /** * The HTML markup to wrap around each option. */ public function before() { ?>

get_option( \$id, 'value' ) ? 'checked="checked"' : ''; } ?> base ) { return \$status; } ob_start(); \$this->before(); foreach ( \$this->screen_options() as \$screen_option ) { \$this->show_option( \$screen_option['title'], \$screen_option['option'] ); } \$this->after(); return ob_get_clean(); } /** * Save the screen option setting. * * @param string \$status No idea. Not used. No one can tell me what it does. * @param string \$option The option name. * @param array \$value Whatever option you're setting. */ public function set_option( \$status, \$option, \$value ) { if ( isset( \$_POST['${5:nonce_name}'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( \$_POST['${5:nonce_name}'] ) ), '${5:nonce_name}' ) ) { if ( '${4:option_identifier}_options' === \$option ) { \$value = isset( \$_POST['${4:option_identifier}'] ) && is_array( \$_POST['${4:option_identifier}'] ) ? \$_POST['${4:option_identifier}'] : []; // WPCS: Sanitization ok. } } return \$value; } } // Fire it up! ${1:ScreenOptionsClass}::get_instance(); ]]>
wp_screen_options source.php