Skip to content

Instantly share code, notes, and snippets.

@JayWood
Created November 28, 2016 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayWood/87f193dea264681fba202a36dadf47c3 to your computer and use it in GitHub Desktop.
Save JayWood/87f193dea264681fba202a36dadf47c3 to your computer and use it in GitHub Desktop.
My Custom Settings API Implementation for those of us who don't need bloated options panel libraries.
<?php
/**
* WooCommerce Order Generator Admin Options Page
*
* @since NEXT
* @package WooCommerce Order Generator
*/
/**
* WooCommerce Order Generator Admin.
*
* @since NEXT
*/
class WCOG_Admin {
/**
* Parent plugin class
*
* @var WooCommerce_Order_Generator
* @since NEXT
*/
protected $plugin = null;
/**
* @var string
*/
protected $option_prefix = 'wcog_';
/**
* Admin page hook
*
* @var String
*/
public $hook;
/**
* Constructor
*
* @since NEXT
* @param WooCommerce_Order_Generator $plugin Main plugin object.
* @return void
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
$this->hooks();
}
/**
* Initiate our hooks
*
* @since NEXT
* @return void
*/
public function hooks() {
add_action( 'admin_init', array( $this, 'register_stuffs' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
/**
* Registering stuff, nothing special
*
* @author JayWood
*/
public function register_stuffs() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_register_script( 'wcog_admin', WooCommerce_Order_Generator::url( "assets/js/woocommerce-order-generator{$min}.js" ), array( 'jquery' ), $this->plugin->version, true );
wp_register_style( 'wcog_admin', WooCommerce_Order_Generator::url( "assets/css/woocommerce-order-generator{$min}.css" ), false );
}
/**
* Load the admin menu up
*
* @author JayWood
*/
public function admin_menu() {
$this->hook = add_management_page( __( 'Order Generator', 'woocommerce-order-generator' ), __( 'Order Generator', 'woocommerce-order-generator' ), 'manage_options', 'woocommerce-order-generator', array( $this, 'render_view' ) );
// Setup sections etc...
foreach ( $this->get_settings_config() as $section ) {
add_settings_section( $section['id'], $section['name'], array( $this->plugin->settings, $section['group'] ), $this->hook );
if ( isset( $section['fields'] ) ) {
foreach ( $section['fields'] as $option_data ) {
register_setting( 'woocommerce-order-generator', $this->option_prefix . $option_data['id'] );
add_settings_field(
$this->option_prefix . $option_data['id'],
$option_data['name'],
array(
$this->plugin->settings,
$option_data['type']
),
$this->hook,
$section['id'],
array(
'id' => $this->option_prefix . $option_data['id'],
'name' => $this->option_prefix . $option_data['id'],
'desc' => $option_data['desc'],
'options' => isset( $option_data['options'] ) ? $option_data['options'] : false,
)
);
}
}
}
}
/**
* The Settings Fields
*
* type refers to a callable method inside the WCOG_Settings class
*
* @author JayWood
* @return array
*/
public function get_settings_config() {
return array(
array(
'id' => 'general-settings',
'name' => __( 'General Settings', 'woocommerce-order-generator' ),
'group' => 'def_group',
'fields' => array(
array(
'id' => 'max-products',
'name' => __( 'Maximum Products', 'woocommerce-order-generator' ),
'desc' => __( 'The maximum amount of products in a user order.', 'woocommerce-order-generator' ),
'type' => 'number',
),
array(
'id' => 'user_type',
'name' => __( 'User Type', 'woocommerce-order-generator' ),
'desc' => __( 'Determines rather the user should be a guest or registerd user.', 'woocommerce-order-generator' ),
'type' => 'select',
'options' => array(
'registered' => __( 'Registered', 'woocommerce-order-generator' ),
'guest' => __( 'Guest', 'woocommerce-order-generator' ),
),
),
),
)
);
}
/**
* Render's the admin page
*
* @author JayWood
*/
public function render_view() {
WooCommerce_Order_Generator::include_view( 'admin-settings' );
}
}
<?php
/**
* Class WCOG_Settings
*
* Handles the heavy lifting of building the options page.
*
* @author JayWood
*/
class WCOG_Settings {
public function def_group() {}
/**
* Just gets a default set of arguments to make sure they're always set.
*
* @param $args
*
* @author JayWood
* @return array
*/
private function get_default_args( $args ) {
return wp_parse_args( $args, array(
'id' => '',
'name' => '',
'desc' => '',
'default' => '',
'options' => false,
) );
}
/**
* @param array $haystack
* @param mixed $cur
* @param bool $show
*
* @author JayWood
* @return string
*/
private function selected_array( $haystack, $cur, $show = true ) {
if ( is_array( $haystack ) ) {
if ( ! empty( $cur ) && in_array( $cur, $haystack ) ) {
$cur = $haystack = 1;
} else {
$cur = 0;
$haystack = 1;
}
}
return selected( $haystack, $cur, $show );
}
/**
* @param array $haystack
* @param mixed $cur
* @param bool $show
*
* @author JayWood
* @return string
*/
private function checked_array( $haystack, $cur, $show = true ) {
if ( is_array( $haystack ) ) {
if ( ! empty( $cur ) && in_array( $cur, $haystack ) ) {
$cur = $haystack = 1;
} else {
$cur = 0;
$haystack = 1;
}
}
return checked( $haystack, $cur, $show );
}
/**
* @param array $args
*
* @author JayWood
*/
public function check( $args = array() ) {
$args = $this->get_default_args( $args );
$field_id = $args['id'];
$description = $args['desc'];
$options = $args['options'];
$default = empty( $args['default'] ) ? array() : $args['default'];
if ( ! $options || empty( $field_id ) ) {
return;
}
$option_value = get_option( $field_id , $default );
?><fieldset><?php
foreach ( $options as $op_value => $label ) {
?>
<label for="<?php echo $op_value; ?>">
<input id="<?php echo $op_value; ?>" type="checkbox" value="<?php echo $op_value; ?>" name="<?php echo $field_id; ?>[]" <?php $this->checked_array( $option_value, $op_value ); ?>/><?php echo $label; ?>
</label>
<?php
}
if ( ! empty( $description ) ) {
?><p class="description"><?php echo $description; ?></p><?php
}
?></fieldset><?php
}
/**
* @param array $args
*
* @author JayWood
*/
public function number( $args = array() ) {
$args = $this->get_default_args( $args );
$field_id = $args['id'];
$description = $args['desc'];
$default = empty( $args['default'] ) ? array() : $args['default'];
$options = empty( $args['options'] ) ? array() : $args['options'];
if ( empty( $field_id ) ) {
return;
}
$option_value = get_option( $field_id, $default );
$attributes = '';
if ( ! empty( $options ) ) {
foreach ( $options as $k => $v ) {
$attributes .= $k . '="' . $v . '"';
}
}
?><input type="number" name="<?php echo $field_id; ?>" value="<?php echo $option_value; ?>" id="<?php echo $field_id; ?>" <?php echo $attributes; ?>/><?php
if ( ! empty( $description ) ) {
?><p class="description"><?php echo $description; ?></p><?php
}
}
/**
* @param array $args
*
* @author JayWood
*/
public function text( $args = array() ) {
$args = $this->get_default_args( $args );
$field_id = $args['id'];
$description = $args['desc'];
$default = empty( $args['default'] ) ? array() : $args['default'];
if ( empty( $field_id ) ) {
return;
}
$option_value = get_option( $field_id, $default );
?><input type="text" name="<?php echo $field_id; ?>" value="<?php echo esc_attr( $option_value ); ?>" id="<?php echo $field_id; ?>" class="regular-text" /><?php
if ( ! empty( $description ) ) {
?><p class="description"><?php echo $description; ?></p><?php
}
}
/**
* @param array $args
*
* @author JayWood
*/
public function radio( $args = array() ) {
$args = $this->get_default_args( $args );
$field_id = $args['id'];
$description = $args['desc'];
$options = $args['options'];
$default = $args['default'];
if ( ! $options || empty( $field_id ) ) {
return;
}
$option_value = get_option( $field_id, $default );
?><fieldset><?php
foreach ( $options as $op_value => $label ) {
?>
<label for="<?php echo $op_value; ?>">
<input id="<?php echo $op_value; ?>" type="radio" value="<?php echo $op_value; ?>" name="<?php echo $field_id; ?>" <?php checked( $option_value, $op_value ); ?>/><?php echo $label; ?>
</label><br />
<?php
}
if ( ! empty( $description ) ) {
?><p class="description"><?php echo $description; ?></p><?php
}
?></fieldset><?php
}
/**
* @param array $args
*
* @author JayWood
*/
public function media( $args = array() ) {
$args = $this->get_default_args( $args );
$field_id = $args['id'];
$description = $args['desc'];
$options = $args['options'];
$default = $args['default'];
if ( empty( $field_id ) ) {
return;
}
$option_value = get_option( $field_id, $default );
if ( ! empty( $option_value ) ) {
$option_value = esc_url( $option_value );
}
$label = isset( $options['label'] ) ? $options['label'] : __( 'Upload', 'content-warning-v2' );
$uploader_title = isset( $options['uploader-title'] ) ? sprintf( 'data-uploader-title="%s"', $options['uploader-title'] ) : '';
$uploader_button = isset( $options['uploader-btn'] ) ? sprintf( 'data-uploader-btn-txt="%s"', $options['uploader-btn'] ) : '';
?><fieldset>
<input type="text" name="<?php echo $field_id; ?>" id="<?php echo $field_id; ?>" value="<?php echo $option_value; ?>" class="regular-text" />
<input type="button" class="button button-secondary upload_image_button" value="<?php echo $label; ?>" data-target-id="<?php echo $field_id; ?>" <?php echo $uploader_button; ?> <?php echo $uploader_title; ?> />
</fieldset><?php
if ( ! empty( $description ) ) {
?><p class="description"><?php echo $description; ?></p><?php
}
}
/**
* @param array $args
*
* @author JayWood
*/
public function color( $args = array() ) {
$args = $this->get_default_args( $args );
$field_id = $args['id'];
$description = $args['desc'];
$default = empty( $args['default'] ) ? '' : $args['default'];
$options = empty( $args['options'] ) ? array() : $args['options'];
if ( empty( $field_id ) ) {
return;
}
$option_value = get_option( $field_id, $default );
$attributes = '';
if ( ! empty( $options ) ) {
foreach ( $options as $k => $v ) {
$attributes .= $k . '="' . $v . '"';
}
}
?><input type="text" name="<?php echo $field_id; ?>" value="<?php echo esc_attr( $option_value ); ?>" id="<?php echo $field_id; ?>" class="regular-text color_select" <?php echo $attributes; ?>/><?php
if ( ! empty( $description ) ) {
?><p class="description"><?php echo $description; ?></p><?php
}
}
/**
* @param array $args
*
* @author JayWood
*/
public function textbox( $args = array() ) {
$args = $this->get_default_args( $args );
$field_id = $args['id'];
$description = $args['desc'];
$default = empty( $args['default'] ) ? '' : $args['default'];
$options = empty( $args['options'] ) ? array() : $args['options'];
if ( empty( $field_id ) ) {
return;
}
$option_value = get_option( $field_id, $default );
$attributes = '';
if ( ! empty( $options ) ) {
foreach ( $options as $k => $v ) {
$attributes .= $k . '="' . $v . '"';
}
}
?><textarea name="<?php echo $field_id; ?>" id="<?php echo $field_id; ?>" class="regular-text" <?php echo $attributes; ?>><?php echo esc_attr( $option_value ); ?></textarea><?php
if ( ! empty( $description ) ) {
?><p class="description"><?php echo $description; ?></p><?php
}
}
public function editor( $args = array() ) {
$args = $this->get_default_args( $args );
$field_id = $args['id'];
$description = $args['desc'];
$default = empty( $args['default'] ) ? '' : $args['default'];
$options = empty( $args['options'] ) ? array() : $args['options'];
if ( empty( $field_id ) ) {
return;
}
$option_value = get_option( $field_id, $default );
wp_editor( $option_value, $field_id, $options );
if ( ! empty( $description ) ) {
?><p class="description"><?php echo $description; ?></p><?php
}
}
public function select( $args = array() ) {
$args = $this->get_default_args( $args );
$field_id = $args['id'];
$description = $args['desc'];
$default = empty( $args['default'] ) ? '' : $args['default'];
$options = empty( $args['options'] ) ? array() : $args['options'];
if ( empty( $field_id ) ) {
return;
}
$option_value = get_option( $field_id, $default );
?>
<select name="<?php echo $field_id; ?>" id="<?php echo $field_id; ?>">
<?php foreach ( $options as $k => $v ) : ?>
<option value="<?php echo $k; ?>" <?php selected( $option_value, $k ); ?>><?php echo $v; ?></option>
<?php endforeach; ?>
</select>
<?php
if ( ! empty( $description ) ) {
?><p class="description"><?php echo $description; ?></p><?php
}
}
/**
* @param array $args
*
* @author JayWood
*/
public function select2_multi( $args = array() ) {
$args = $this->get_default_args( $args );
$field_id = $args['id'];
$description = $args['desc'];
$default = empty( $args['default'] ) ? '' : $args['default'];
$options = empty( $args['options'] ) ? array() : $args['options'];
if ( empty( $field_id ) ) {
return;
}
$option_value = get_option( $field_id, $default );
?>
<select name="<?php echo $field_id; ?>[]" id="<?php echo $field_id; ?>" class="cwv2_select2 widefat" multiple="multiple">
<?php foreach ( $options as $k => $v ) : ?>
<option value="<?php echo $k; ?>" <?php $this->selected_array( $option_value, $k ); ?>><?php echo $v; ?></option>
<?php endforeach; ?>
</select>
<?php
if ( ! empty( $description ) ) {
?><p class="description"><?php echo $description; ?></p><?php
}
}
}
<div class="wrap">
<h1><?php _e( 'Order Generator', 'woocommerce-order-generator' ); ?></h1>
<p><?php _e( 'You will need to setup some basic settings for the generator.', 'woocommerce-order-generator' ); ?></p>
<form method="post" action="options.php">
<?php settings_fields( 'woocommerce-order-generator' ); ?>
<?php // admin is a property of the main class, which references the class-admin.php below ?>
<?php do_settings_sections( WooCommerce_Order_Generator::get_instance()->admin->hook ); ?>
<?php submit_button(); ?>
</form>
</div>
@JayWood
Copy link
Author

JayWood commented Nov 28, 2016

This series of snippets was literally copied and pasted for myself, but feel free to re-use/modify how you see fit. Using this method ( in most cases ) is better if you only need a few options, but really can't see a reason for a bloated library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment