Skip to content

Instantly share code, notes, and snippets.

@joelstransky
Last active July 16, 2016 02:15
Show Gist options
  • Save joelstransky/79efd8435e4be950c85284190b36cb8f to your computer and use it in GitHub Desktop.
Save joelstransky/79efd8435e4be950c85284190b36cb8f to your computer and use it in GitHub Desktop.
<?php
// https://codex.wordpress.org/Creating_Options_Pages#Example_.232
class VpaMceSettingsPage
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
// include acf UI
add_action('admin_enqueue_scripts', array( $this, 'on_admin_enqueue_scripts' ) );
}
public function on_admin_enqueue_scripts() {
wp_enqueue_script( 'select2', plugins_url('../assets/vendor/select2-3.5.4/select2.min.js', __FILE__), array('jquery'), '', true );
wp_enqueue_style('select2', plugins_url('../assets/vendor/select2-3.5.4/select2.css', __FILE__) );
wp_enqueue_script( 'vpa_mce_choose_post', plugins_url('../assets/js/vpa-mce-choose-post.js', __FILE__), array('jquery', 'select2'), '', true );
$data = array(
'ajax_callback' => 'vpa_mce_posts_dropdown',
// 'post_author' => $author_id,
// 'display_name' => isset( $author_data->display_name ) ? $author_data->display_name : null,
// 'post_type' => get_post_type(),
'nonce' => wp_create_nonce( 'vpa-mce-choose-post-nonce' ),
'placeholder_text' => __( 'Select a post', 'vpa-mce-choose-post-dropdown' ),
);
wp_localize_script( 'vpa_mce_choose_post', 'vpa_mce_config', $data );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'VPA MCE Insert Shortcodes',
'VPA MCE Insert Shortcode Settings',
'manage_options',
'mce-vpa-setting-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
// delete_option( 'dcdc_vpa_mce_insert_shortcodes' );
$this->options = get_option( 'dcdc_vpa_mce_insert_shortcodes' );
write_log(array('option is', $this->options));
?>
<div class="wrap">
<h2>VPA MCE Insert Shortcode Settings</h2>
<form id="mce-vpa-form" method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'mce_vpa_insert_shortcodes_option_group' );
do_settings_sections( 'mce-vpa-setting-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'mce_vpa_insert_shortcodes_option_group', // Option group
'dcdc_vpa_mce_insert_shortcodes', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'dcdc_vpa_mce_insert_shortcodes_section', // ID
'Global Denial Message', // Title
array( $this, 'print_dcdc_vpa_mce_insert_shortcodes_info' ), // Callback
'mce-vpa-setting-admin' // Page
);
add_settings_field(
'post_ids', // ID
'Message Post', // Title
array( $this, 'post_ids_callback' ), // Callback
'mce-vpa-setting-admin', // Page
'dcdc_vpa_mce_insert_shortcodes_section' // Section
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
wp_die(var_dump($input));
// write_log( array('input is', gettype($input)));
// $new_input = array( 'post_ids' => -1 );
// if( isset( $input['post_ids'] ) )
// $new_input['post_ids'] = absint( $input['post_ids'] );
// return $new_input;
return $input;
}
/**
* Print the Section text
*/
public function print_dcdc_vpa_mce_insert_shortcodes_info()
{
print 'Choose a post containing your access denial message:';
}
/**
* Get the settings option array and print one of its values
*/
public function post_ids_callback()
{
printf(
'<input type="text" name="message_post" id="vpa-post-search" value="%s" />',
isset( $this->options['post_ids'] ) ? esc_attr( $this->options['post_ids']) : ''
);
}
}
if( is_admin() )
$vpa_mce_settings_page = new VpaMceSettingsPage();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment