action = 'list';
// edit or delete
if(acfe_maybe_get_REQUEST('action', '-1') !== '-1'){
$this->action = $_REQUEST['action'];
// bulk-delete
}elseif(acfe_maybe_get_REQUEST('action2', '-1') !== '-1'){
$this->action = $_REQUEST['action2'];
}
// load
switch($this->action){
case 'list': {
$this->load_list();
break;
}
case 'edit':
case 'add': {
$this->load_edit();
break;
}
case 'delete': {
$this->load_delete();
break;
}
case 'bulk-delete': {
$this->load_bulk_delete();
break;
}
}
// enqueue
acf_enqueue_scripts();
}
/**
* admin_html
*/
function admin_html(){
if($this->action === 'list'){
$this->html_list();
}elseif($this->action === 'edit' || $this->action === 'add'){
$this->html_edit();
}
}
/**
* load_list
*/
function load_list(){
add_screen_option('per_page', array(
'label' => 'Options',
'default' => 100,
'option' => 'options_per_page'
));
}
/**
* load_edit
*/
function load_edit(){
// nonce
if(acf_verify_nonce('acfe-options-edit')){
// save data
if(acf_validate_save_post(true)){
acf_save_post('acfe_options_edit');
$redirect = add_query_arg(array('message' => 'updated'));
if($this->action === 'add'){
$redirect = sprintf('?page=%s&message=added', esc_attr($_REQUEST['page']));
}
wp_redirect($redirect);
exit;
}
}
// actions
add_action('acf/input/admin_head', array($this, 'add_metaboxes'));
// add columns support
add_screen_option('layout_columns', array(
'max' => 2,
'default' => 2,
));
}
/**
* load_delete
*/
function load_delete(){
// nonce
$nonce = esc_attr($_REQUEST['_wpnonce']);
// verify
if(!wp_verify_nonce($nonce, 'acfe_options_delete_option')){
wp_die('Cheatin’, huh?');
}
// delete
$this->delete_option(absint($_GET['option']));
// redirect
wp_redirect(sprintf('?page=%s&message=deleted', esc_attr($_REQUEST['page'])));
exit;
}
/**
* load_bulk_delete
*/
function load_bulk_delete(){
// nonce
$nonce = esc_attr($_REQUEST['_wpnonce']);
// verify
if(!wp_verify_nonce($nonce, 'bulk-options')){
wp_die('Cheatin’, huh?');
}
// ids
$delete_ids = esc_sql($_REQUEST['bulk-delete']);
// loop
foreach($delete_ids as $id){
$this->delete_option($id);
}
wp_redirect(sprintf('?page=%s&message=bulk-deleted', esc_attr($_REQUEST['page'])));
exit;
}
/**
* html_list
*/
function html_list(){
acfe_get_view('html-options-list');
}
/**
* html_edit
*/
function html_edit(){
acfe_get_view('html-options-edit');
}
/**
* save_post
*
* @param $post_id
*/
function save_post($post_id){
// validate
if($post_id !== 'acfe_options_edit'){
return;
}
// vars
$option_name = wp_unslash($_POST['acf']['field_acfe_options_edit_name']);
$option_value = wp_unslash($_POST['acf']['field_acfe_options_edit_value']);
$autoload = $_POST['acf']['field_acfe_options_edit_autoload'];
// value serialized?
$option_value = maybe_unserialize($option_value);
// update
update_option($option_name, $option_value, $autoload);
// flush acf
$_POST['acf'] = array();
}
/**
* delete_option
*
* @param $id
*/
function delete_option($id){
global $wpdb;
$wpdb->delete("{$wpdb->options}", array('option_id' => $id), array('%d'));
}
/**
* add_metaboxes
*/
function add_metaboxes(){
$option = array(
'option_id' => 0,
'option_name' => '',
'option_value' => '',
'autoload' => 'no',
);
$option_id = absint(acfe_maybe_get_REQUEST('option'));
if($option_id){
global $wpdb;
$get_option = $wpdb->get_row("SELECT * FROM {$wpdb->options} WHERE option_id = '$option_id'", 'ARRAY_A');
if(!empty($get_option)){
$option = $get_option;
}
}
$fields = array();
$fields[] = array(
'label' => __('Name', 'acfe'),
'key' => 'field_acfe_options_edit_name',
'name' => 'field_acfe_options_edit_name',
'type' => 'text',
'prefix' => 'acf',
'instructions' => '',
'required' => true,
'conditional_logic' => false,
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'value' => $option['option_name'],
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
);
// serialized || html
if(is_serialized($option['option_value']) || $option['option_value'] != strip_tags($option['option_value'])){
$class = 'code';
$type = 'serialized';
$instructions = 'Use this online tool to unserialize/seriliaze data.';
if($option['option_value'] != strip_tags($option['option_value'])){
$type = 'HTML';
$instructions = '';
}
$instructions = '' . $type . '' . $instructions;
}
// json
elseif(acfe_is_json($option['option_value'])){
$class = 'code';
$type = 'json';
$instructions = 'Use this online tool to decode/encode json.';
$instructions = '' . $type . '' . $instructions;
}
// string
else{
$class = '';
$instructions = '';
if(!empty($option['option_value'])){
$instructions = 'string';
}
}
$fields[] = array(
'label' => __('Value', 'acfe'),
'key' => 'field_acfe_options_edit_value',
'name' => 'field_acfe_options_edit_value',
'type' => 'textarea',
'prefix' => 'acf',
'instructions' => $instructions,
'required' => false,
'conditional_logic' => false,
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'value' => $option['option_value'],
'class' => $class,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
);
$fields[] = array(
'label' => __('Autoload', 'acfe'),
'key' => 'field_acfe_options_edit_autoload',
'name' => 'field_acfe_options_edit_autoload',
'type' => 'select',
'prefix' => 'acf',
'instructions' => '',
'required' => true,
'conditional_logic' => false,
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'value' => $option['autoload'],
'choices' => array(
'no' => __('No', 'acfe'),
'yes' => __('Yes', 'acfe'),
),
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
);
// prepare field group
$field_group = array(
'ID' => 0,
'key' => 'group_acfe_options_edit',
'style' => 'default',
'label_placement' => 'left',
'instruction_placement' => 'label',
'fields' => $fields,
);
$metabox_submit_title = __('Submit', 'acf');
$metabox_main_title = __('Add Option', 'acfe');
if(!empty($option['option_id'])){
$metabox_submit_title = __('Edit', 'acf');
$metabox_main_title = __('Edit Option', 'acfe');
}
// submit Metabox
add_meta_box('submitdiv', $metabox_submit_title, function($post, $args) use($option){
$delete_nonce = wp_create_nonce('acfe_options_delete_option');
?>