module-option.php
549 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | // check setting |
| 8 | if(!acf_get_setting('acfe/modules/options')){ |
| 9 | return; |
| 10 | } |
| 11 | |
| 12 | if(!class_exists('acfe_module_options')): |
| 13 | |
| 14 | class acfe_module_options{ |
| 15 | |
| 16 | // vars |
| 17 | var $action = 'list'; |
| 18 | |
| 19 | /** |
| 20 | * Construct |
| 21 | */ |
| 22 | function __construct(){ |
| 23 | |
| 24 | acfe_include('includes/modules/option/module-option-table.php'); |
| 25 | |
| 26 | add_filter('set-screen-option', array($this, 'set_screen_option'), 10, 3); |
| 27 | add_action('admin_menu', array($this, 'admin_menu')); |
| 28 | add_action('acf/save_post', array($this, 'save_post'), 5); |
| 29 | |
| 30 | } |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * set_screen_option |
| 35 | * |
| 36 | * @param $status |
| 37 | * @param $option |
| 38 | * @param $value |
| 39 | * |
| 40 | * @return mixed |
| 41 | */ |
| 42 | function set_screen_option($status, $option, $value){ |
| 43 | |
| 44 | if($option === 'options_per_page'){ |
| 45 | return $value; |
| 46 | } |
| 47 | |
| 48 | return $status; |
| 49 | |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * admin_menu |
| 55 | */ |
| 56 | function admin_menu(){ |
| 57 | |
| 58 | if(acf_get_setting('show_admin')){ |
| 59 | |
| 60 | $page = add_submenu_page('options-general.php', __('Options', 'acfe'), __('Options', 'acfe'), acf_get_setting('capability'), 'acfe-options', array($this, 'admin_html')); |
| 61 | |
| 62 | add_action("load-{$page}", array($this, 'admin_load')); |
| 63 | |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * admin_load |
| 71 | */ |
| 72 | function admin_load(){ |
| 73 | |
| 74 | // messages |
| 75 | if($message = acf_maybe_get_GET('message')){ |
| 76 | |
| 77 | switch($message){ |
| 78 | |
| 79 | case 'deleted': { |
| 80 | acf_add_admin_notice(__('Option has been deleted', 'acfe'), 'success'); |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | case 'bulk-deleted': { |
| 85 | acf_add_admin_notice(__('Options have been deleted', 'acfe'), 'success'); |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | case 'updated': { |
| 90 | acf_add_admin_notice(__('Option has been updated', 'acfe'), 'success'); |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | case 'added': { |
| 95 | acf_add_admin_notice(__('Option has been added', 'acfe'), 'success'); |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | |
| 103 | // default: list |
| 104 | $this->action = 'list'; |
| 105 | |
| 106 | // edit or delete |
| 107 | if(acfe_maybe_get_REQUEST('action', '-1') !== '-1'){ |
| 108 | $this->action = $_REQUEST['action']; |
| 109 | |
| 110 | // bulk-delete |
| 111 | }elseif(acfe_maybe_get_REQUEST('action2', '-1') !== '-1'){ |
| 112 | $this->action = $_REQUEST['action2']; |
| 113 | } |
| 114 | |
| 115 | // load |
| 116 | switch($this->action){ |
| 117 | |
| 118 | case 'list': { |
| 119 | $this->load_list(); |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | case 'edit': |
| 124 | case 'add': { |
| 125 | $this->load_edit(); |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | case 'delete': { |
| 130 | $this->load_delete(); |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | case 'bulk-delete': { |
| 135 | $this->load_bulk_delete(); |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | } |
| 140 | |
| 141 | // enqueue |
| 142 | acf_enqueue_scripts(); |
| 143 | |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * admin_html |
| 149 | */ |
| 150 | function admin_html(){ |
| 151 | |
| 152 | if($this->action === 'list'){ |
| 153 | $this->html_list(); |
| 154 | |
| 155 | }elseif($this->action === 'edit' || $this->action === 'add'){ |
| 156 | $this->html_edit(); |
| 157 | } |
| 158 | |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * load_list |
| 164 | */ |
| 165 | function load_list(){ |
| 166 | |
| 167 | add_screen_option('per_page', array( |
| 168 | 'label' => 'Options', |
| 169 | 'default' => 100, |
| 170 | 'option' => 'options_per_page' |
| 171 | )); |
| 172 | |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /** |
| 177 | * load_edit |
| 178 | */ |
| 179 | function load_edit(){ |
| 180 | |
| 181 | // nonce |
| 182 | if(acf_verify_nonce('acfe-options-edit')){ |
| 183 | |
| 184 | // save data |
| 185 | if(acf_validate_save_post(true)){ |
| 186 | |
| 187 | acf_save_post('acfe_options_edit'); |
| 188 | |
| 189 | $redirect = add_query_arg(array('message' => 'updated')); |
| 190 | |
| 191 | if($this->action === 'add'){ |
| 192 | $redirect = sprintf('?page=%s&message=added', esc_attr($_REQUEST['page'])); |
| 193 | } |
| 194 | |
| 195 | wp_redirect($redirect); |
| 196 | exit; |
| 197 | |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | |
| 202 | // actions |
| 203 | add_action('acf/input/admin_head', array($this, 'add_metaboxes')); |
| 204 | |
| 205 | // add columns support |
| 206 | add_screen_option('layout_columns', array( |
| 207 | 'max' => 2, |
| 208 | 'default' => 2, |
| 209 | )); |
| 210 | |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /** |
| 215 | * load_delete |
| 216 | */ |
| 217 | function load_delete(){ |
| 218 | |
| 219 | // nonce |
| 220 | $nonce = esc_attr($_REQUEST['_wpnonce']); |
| 221 | |
| 222 | // verify |
| 223 | if(!wp_verify_nonce($nonce, 'acfe_options_delete_option')){ |
| 224 | wp_die('Cheatin’, huh?'); |
| 225 | } |
| 226 | |
| 227 | // delete |
| 228 | $this->delete_option(absint($_GET['option'])); |
| 229 | |
| 230 | // redirect |
| 231 | wp_redirect(sprintf('?page=%s&message=deleted', esc_attr($_REQUEST['page']))); |
| 232 | exit; |
| 233 | |
| 234 | } |
| 235 | |
| 236 | |
| 237 | /** |
| 238 | * load_bulk_delete |
| 239 | */ |
| 240 | function load_bulk_delete(){ |
| 241 | |
| 242 | // nonce |
| 243 | $nonce = esc_attr($_REQUEST['_wpnonce']); |
| 244 | |
| 245 | // verify |
| 246 | if(!wp_verify_nonce($nonce, 'bulk-options')){ |
| 247 | wp_die('Cheatin’, huh?'); |
| 248 | } |
| 249 | |
| 250 | // ids |
| 251 | $delete_ids = esc_sql($_REQUEST['bulk-delete']); |
| 252 | |
| 253 | // loop |
| 254 | foreach($delete_ids as $id){ |
| 255 | $this->delete_option($id); |
| 256 | } |
| 257 | |
| 258 | wp_redirect(sprintf('?page=%s&message=bulk-deleted', esc_attr($_REQUEST['page']))); |
| 259 | exit; |
| 260 | |
| 261 | } |
| 262 | |
| 263 | |
| 264 | /** |
| 265 | * html_list |
| 266 | */ |
| 267 | function html_list(){ |
| 268 | acfe_get_view('html-options-list'); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | /** |
| 273 | * html_edit |
| 274 | */ |
| 275 | function html_edit(){ |
| 276 | acfe_get_view('html-options-edit'); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | /** |
| 281 | * save_post |
| 282 | * |
| 283 | * @param $post_id |
| 284 | */ |
| 285 | function save_post($post_id){ |
| 286 | |
| 287 | // validate |
| 288 | if($post_id !== 'acfe_options_edit'){ |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | // vars |
| 293 | $option_name = wp_unslash($_POST['acf']['field_acfe_options_edit_name']); |
| 294 | $option_value = wp_unslash($_POST['acf']['field_acfe_options_edit_value']); |
| 295 | $autoload = $_POST['acf']['field_acfe_options_edit_autoload']; |
| 296 | |
| 297 | // value serialized? |
| 298 | $option_value = maybe_unserialize($option_value); |
| 299 | |
| 300 | // update |
| 301 | update_option($option_name, $option_value, $autoload); |
| 302 | |
| 303 | // flush acf |
| 304 | $_POST['acf'] = array(); |
| 305 | |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /** |
| 310 | * delete_option |
| 311 | * |
| 312 | * @param $id |
| 313 | */ |
| 314 | function delete_option($id){ |
| 315 | |
| 316 | global $wpdb; |
| 317 | |
| 318 | $wpdb->delete("{$wpdb->options}", array('option_id' => $id), array('%d')); |
| 319 | |
| 320 | } |
| 321 | |
| 322 | |
| 323 | /** |
| 324 | * add_metaboxes |
| 325 | */ |
| 326 | function add_metaboxes(){ |
| 327 | |
| 328 | $option = array( |
| 329 | 'option_id' => 0, |
| 330 | 'option_name' => '', |
| 331 | 'option_value' => '', |
| 332 | 'autoload' => 'no', |
| 333 | ); |
| 334 | |
| 335 | $option_id = absint(acfe_maybe_get_REQUEST('option')); |
| 336 | |
| 337 | if($option_id){ |
| 338 | |
| 339 | global $wpdb; |
| 340 | |
| 341 | $get_option = $wpdb->get_row("SELECT * FROM {$wpdb->options} WHERE option_id = '$option_id'", 'ARRAY_A'); |
| 342 | |
| 343 | if(!empty($get_option)){ |
| 344 | $option = $get_option; |
| 345 | } |
| 346 | |
| 347 | } |
| 348 | |
| 349 | $fields = array(); |
| 350 | |
| 351 | $fields[] = array( |
| 352 | 'label' => __('Name', 'acfe'), |
| 353 | 'key' => 'field_acfe_options_edit_name', |
| 354 | 'name' => 'field_acfe_options_edit_name', |
| 355 | 'type' => 'text', |
| 356 | 'prefix' => 'acf', |
| 357 | 'instructions' => '', |
| 358 | 'required' => true, |
| 359 | 'conditional_logic' => false, |
| 360 | 'default_value' => '', |
| 361 | 'placeholder' => '', |
| 362 | 'prepend' => '', |
| 363 | 'append' => '', |
| 364 | 'maxlength' => '', |
| 365 | 'value' => $option['option_name'], |
| 366 | 'wrapper' => array( |
| 367 | 'width' => '', |
| 368 | 'class' => '', |
| 369 | 'id' => '', |
| 370 | ), |
| 371 | ); |
| 372 | |
| 373 | // serialized || html |
| 374 | if(is_serialized($option['option_value']) || $option['option_value'] != strip_tags($option['option_value'])){ |
| 375 | |
| 376 | $class = 'code'; |
| 377 | $type = 'serialized'; |
| 378 | $instructions = 'Use this <a href="https://duzun.me/playground/serialize" target="_blank">online tool</a> to unserialize/seriliaze data.'; |
| 379 | |
| 380 | if($option['option_value'] != strip_tags($option['option_value'])){ |
| 381 | $type = 'HTML'; |
| 382 | $instructions = ''; |
| 383 | } |
| 384 | |
| 385 | $instructions = '<code>' . $type . '</code>' . $instructions; |
| 386 | |
| 387 | } |
| 388 | |
| 389 | // json |
| 390 | elseif(acfe_is_json($option['option_value'])){ |
| 391 | |
| 392 | $class = 'code'; |
| 393 | $type = 'json'; |
| 394 | $instructions = 'Use this <a href="http://solutions.weblite.ca/php2json/" target="_blank">online tool</a> to decode/encode json.'; |
| 395 | $instructions = '<code>' . $type . '</code>' . $instructions; |
| 396 | |
| 397 | } |
| 398 | |
| 399 | // string |
| 400 | else{ |
| 401 | |
| 402 | $class = ''; |
| 403 | $instructions = ''; |
| 404 | if(!empty($option['option_value'])){ |
| 405 | $instructions = '<code>string</code>'; |
| 406 | } |
| 407 | |
| 408 | } |
| 409 | |
| 410 | $fields[] = array( |
| 411 | 'label' => __('Value', 'acfe'), |
| 412 | 'key' => 'field_acfe_options_edit_value', |
| 413 | 'name' => 'field_acfe_options_edit_value', |
| 414 | 'type' => 'textarea', |
| 415 | 'prefix' => 'acf', |
| 416 | 'instructions' => $instructions, |
| 417 | 'required' => false, |
| 418 | 'conditional_logic' => false, |
| 419 | 'default_value' => '', |
| 420 | 'placeholder' => '', |
| 421 | 'prepend' => '', |
| 422 | 'append' => '', |
| 423 | 'maxlength' => '', |
| 424 | 'value' => $option['option_value'], |
| 425 | 'class' => $class, |
| 426 | 'wrapper' => array( |
| 427 | 'width' => '', |
| 428 | 'class' => '', |
| 429 | 'id' => '', |
| 430 | ), |
| 431 | ); |
| 432 | |
| 433 | $fields[] = array( |
| 434 | 'label' => __('Autoload', 'acfe'), |
| 435 | 'key' => 'field_acfe_options_edit_autoload', |
| 436 | 'name' => 'field_acfe_options_edit_autoload', |
| 437 | 'type' => 'select', |
| 438 | 'prefix' => 'acf', |
| 439 | 'instructions' => '', |
| 440 | 'required' => true, |
| 441 | 'conditional_logic' => false, |
| 442 | 'default_value' => '', |
| 443 | 'placeholder' => '', |
| 444 | 'prepend' => '', |
| 445 | 'append' => '', |
| 446 | 'maxlength' => '', |
| 447 | 'value' => $option['autoload'], |
| 448 | 'choices' => array( |
| 449 | 'no' => __('No', 'acfe'), |
| 450 | 'yes' => __('Yes', 'acfe'), |
| 451 | ), |
| 452 | 'wrapper' => array( |
| 453 | 'width' => '', |
| 454 | 'class' => '', |
| 455 | 'id' => '', |
| 456 | ), |
| 457 | ); |
| 458 | |
| 459 | |
| 460 | // prepare field group |
| 461 | $field_group = array( |
| 462 | 'ID' => 0, |
| 463 | 'key' => 'group_acfe_options_edit', |
| 464 | 'style' => 'default', |
| 465 | 'label_placement' => 'left', |
| 466 | 'instruction_placement' => 'label', |
| 467 | 'fields' => $fields, |
| 468 | ); |
| 469 | |
| 470 | $metabox_submit_title = __('Submit', 'acf'); |
| 471 | $metabox_main_title = __('Add Option', 'acfe'); |
| 472 | |
| 473 | if(!empty($option['option_id'])){ |
| 474 | |
| 475 | $metabox_submit_title = __('Edit', 'acf'); |
| 476 | $metabox_main_title = __('Edit Option', 'acfe'); |
| 477 | |
| 478 | } |
| 479 | |
| 480 | // submit Metabox |
| 481 | add_meta_box('submitdiv', $metabox_submit_title, function($post, $args) use($option){ |
| 482 | |
| 483 | $delete_nonce = wp_create_nonce('acfe_options_delete_option'); |
| 484 | |
| 485 | ?> |
| 486 | <div id="major-publishing-actions"> |
| 487 | |
| 488 | <?php if(!empty($option['option_id'])){ ?> |
| 489 | |
| 490 | <div id="delete-action"> |
| 491 | <a class="submitdelete deletion" style="color:#a00;" href="<?php echo sprintf('?page=%s&action=%s&option=%s&_wpnonce=%s', esc_attr($_REQUEST['page']), 'delete', $option['option_id'], $delete_nonce); ?>"> |
| 492 | <?php _e('Delete'); ?> |
| 493 | </a> |
| 494 | </div> |
| 495 | |
| 496 | <?php } ?> |
| 497 | |
| 498 | <div id="publishing-action"> |
| 499 | <span class="spinner"></span> |
| 500 | <input type="submit" accesskey="p" value="<?php _e('Update'); ?>" class="button button-primary button-large" id="publish" name="publish"> |
| 501 | </div> |
| 502 | |
| 503 | <div class="clear"></div> |
| 504 | |
| 505 | </div> |
| 506 | <?php |
| 507 | }, 'acf_options_page', 'side', 'high'); |
| 508 | |
| 509 | // main metabox |
| 510 | add_meta_box('acf-group_acfe_options_edit', $metabox_main_title, function($post, $args){ |
| 511 | |
| 512 | // extract args |
| 513 | extract($args); // all variables from the add_meta_box function |
| 514 | extract($args); // all variables from the args argument |
| 515 | |
| 516 | // vars |
| 517 | $o = array( |
| 518 | 'id' => $id, |
| 519 | 'key' => $field_group['key'], |
| 520 | 'style' => $field_group['style'], |
| 521 | 'label' => $field_group['label_placement'], |
| 522 | 'editLink' => '', |
| 523 | 'editTitle' => __('Edit field group', 'acf'), |
| 524 | 'visibility' => true |
| 525 | ); |
| 526 | |
| 527 | // load fields |
| 528 | $fields = $field_group['fields']; |
| 529 | |
| 530 | // render |
| 531 | acf_render_fields($fields, 'acfe-options-edit', 'div', $field_group['instruction_placement']); |
| 532 | |
| 533 | ?> |
| 534 | <script type="text/javascript"> |
| 535 | if(typeof acf !== 'undefined'){ |
| 536 | acf.newPostbox(<?php echo json_encode($o); ?>); |
| 537 | } |
| 538 | </script> |
| 539 | <?php |
| 540 | |
| 541 | }, 'acf_options_page', 'normal', 'high', array('field_group' => $field_group)); |
| 542 | |
| 543 | } |
| 544 | |
| 545 | } |
| 546 | |
| 547 | new acfe_module_options(); |
| 548 | |
| 549 | endif; |