Advertisement
GochiSiyan

OptionAbstract.php

May 16th, 2021
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. <?php
  2. /**
  3. * @author Jegtheme
  4. */
  5.  
  6. namespace JNews\Archive\Builder;
  7.  
  8. use Jeg\Form\Form_Archive;
  9.  
  10. abstract class OptionAbstract {
  11.  
  12. protected static $instance;
  13.  
  14. protected $prefix;
  15.  
  16. public static function getInstance() {
  17. $class = get_called_class();
  18.  
  19. if ( ! isset( self::$instance[ $class ] ) ) {
  20. static::$instance[ $class ] = new $class();
  21. }
  22.  
  23. return static::$instance[ $class ];
  24. }
  25.  
  26. private function __construct() {
  27. $this->setup_hook();
  28. }
  29.  
  30. public function render_options( $tag ) {
  31. $id = $this->get_id( $tag );
  32.  
  33. if ( null !== $id ) {
  34. $segments = $this->prepare_segments();
  35. $fields = $this->prepare_fields( $id );
  36. $id = 'archive-' . $id;
  37.  
  38. if ( class_exists( 'Jeg\Form\Form_Archive' ) ) {
  39. Form_Archive::render_form($id, $segments, $fields);
  40. }
  41. }
  42. }
  43.  
  44. public function prepare_fields( $term_id ) {
  45. $setting = array();
  46. $fields = $this->get_options();
  47.  
  48. foreach ( $fields as $key => $field ) {
  49. $setting[ $key ] = array();
  50. $setting[ $key ]['id'] = $key;
  51. $setting[ $key ]['fieldID'] = $key . '_' . $term_id;
  52. $setting[ $key ]['fieldName'] = $key;
  53. $setting[ $key ]['type'] = $field['type'];
  54. $setting[ $key ]['title'] = isset( $field['title'] ) ? $field['title'] : '';
  55. $setting[ $key ]['description'] = isset( $field['desc'] ) ? $field['desc'] : '';
  56. $setting[ $key ]['segment'] = isset( $field['segment'] ) ? sanitize_title_with_dashes( $field['segment'] ) : '';
  57. $setting[ $key ]['default'] = isset( $field['default'] ) ? $field['default'] : '';
  58. $setting[ $key ]['priority'] = isset( $field['priority'] ) ? $field['priority'] : 10;
  59. $setting[ $key ]['options'] = isset( $field['options'] ) ? $field['options'] : array();
  60. $setting[ $key ]['dependency'] = isset( $field['dependency'] ) ? $field['dependency'] : array();
  61. $setting[ $key ]['multiple'] = isset( $field['multiple'] ) ? $field['multiple'] : 1;
  62. $setting[ $key ]['ajax'] = isset( $field['ajax'] ) ? $field['ajax'] : '';
  63. $setting[ $key ]['nonce'] = isset( $field['nonce'] ) ? $field['nonce'] : '';
  64. $setting[ $key ]['fields'] = isset( $field['fields'] ) ? $field['fields'] : array();
  65. $setting[ $key ]['row_label'] = isset( $field['row_label'] ) ? $field['row_label'] : array();
  66.  
  67. $setting[ $key ]['value'] = $this->get_value( $key, $term_id, $setting[ $key ]['default'] );
  68.  
  69. // only for image type
  70. if ( 'image' === $setting[ $key ]['type'] ) {
  71. $image = wp_get_attachment_image_src( $setting[ $key ]['value'], 'full' );
  72. if ( isset( $image[0] ) ) {
  73. $setting[ $key ]['imageUrl'] = $image[0];
  74. }
  75. }
  76. }
  77.  
  78. return $setting;
  79. }
  80.  
  81. public function get_value( $key, $term_id, $default ) {
  82. $value = get_option( $this->prefix . $key, false );
  83.  
  84. if ( isset( $value[ $term_id ] ) ) {
  85. return $value[ $term_id ];
  86. } else {
  87. return $default;
  88. }
  89. }
  90.  
  91. protected function save_value( $key, $term_id, $value ) {
  92. $values = get_option( $this->prefix . $key, array() );
  93. $values[ $term_id ] = $value;
  94. update_option( $this->prefix . $key, $values );
  95. }
  96.  
  97. protected function do_save( $options, $input ) {
  98. foreach ( $options as $key => $field ) {
  99. if ( isset( $field['items'] ) ) {
  100. foreach ( $field['items'] as $key1 => $value1 ) {
  101. $option = isset( $_POST[ $key1 ] ) ? $_POST[ $key1 ] : false;
  102. $this->save_value( $key1, $input, $option );
  103. }
  104. } else {
  105. $option = isset( $_POST[ $key ] ) ? $_POST[ $key ] : false;
  106. $this->save_value( $key, $input, $option );
  107. }
  108. }
  109. }
  110.  
  111. abstract protected function get_options();
  112.  
  113. abstract protected function setup_hook();
  114.  
  115. abstract protected function prepare_segments();
  116.  
  117. abstract protected function get_id( $tag );
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement