| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Models\Base; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
use RadiusTheme\SB\Helpers\Fns; |
| 8 |
use RadiusTheme\SB\Models\DataModel; |
| 9 |
|
| 10 |
abstract class ListModel { |
| 11 |
|
| 12 |
protected $list_id; |
| 13 |
|
| 14 |
protected $title; |
| 15 |
protected $short_title; |
| 16 |
protected $description; |
| 17 |
private $full_list = []; |
| 18 |
private $active_list = []; |
| 19 |
private $inactive_list = []; |
| 20 |
private $db_options = []; |
| 21 |
protected $categories = []; |
| 22 |
|
| 23 |
public function __construct() { |
| 24 |
$this->db_options = DataModel::source()->get_option( $this->list_id, [] ); |
| 25 |
$raw_list = apply_filters( 'rtsb/' . $this->list_id . '/list', $this->raw_list() ); |
| 26 |
|
| 27 |
if ( ! empty( $raw_list ) ) { |
| 28 |
foreach ( $raw_list as $name => $item ) { |
| 29 |
|
| 30 |
if ( ! empty( $item['package'] ) && $item['package'] === 'pro-disabled' ) { |
| 31 |
$item['fields'] = []; |
| 32 |
$item['active'] = false; |
| 33 |
$this->full_list[ $name ] = $item; |
| 34 |
$this->inactive_list[ $name ] = $item; |
| 35 |
continue; |
| 36 |
} |
| 37 |
|
| 38 |
if ( ! isset( $this->db_options[ $name ]['active'] ) && ! empty( $item['active'] ) ) { |
| 39 |
$item['active'] = 'on'; |
| 40 |
} else { |
| 41 |
$item['active'] = isset( $this->db_options[ $name ]['active'] ) && $this->db_options[ $name ]['active'] === 'on' ? 'on' : ''; |
| 42 |
} |
| 43 |
|
| 44 |
if ( ! empty( $item['fields'] ) ) { |
| 45 |
foreach ( $item['fields'] as $field_key => $field ) { |
| 46 |
$the_value = $this->db_options[ $name ][ $field_key ] ?? ( $item['fields'][ $field_key ]['value'] ?? '' ); |
| 47 |
if ( 'repeaters' === $field['type'] && empty( $the_value ) && ! empty( $field['defaults'] ) ) { |
| 48 |
$repeater_defaults = array_values( $field['defaults'] ); |
| 49 |
$the_value = wp_json_encode( $repeater_defaults ); |
| 50 |
} elseif ( 'repeaters' === $field['type'] && ! empty( $the_value ) ) { |
| 51 |
$the_value = json_decode( $the_value ); |
| 52 |
} |
| 53 |
$sanitized_value = Fns::stripslashes_value( $the_value ); |
| 54 |
$item['fields'][ $field_key ]['value'] = $sanitized_value; |
| 55 |
} |
| 56 |
} |
| 57 |
$this->full_list[ $name ] = $item; |
| 58 |
|
| 59 |
if ( 'on' === $item['active'] ) { |
| 60 |
$this->active_list[ $name ] = $item; |
| 61 |
} else { |
| 62 |
$this->inactive_list[ $name ] = $item; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @return string Pro Package |
| 70 |
*/ |
| 71 |
protected function pro_package() { |
| 72 |
return rtsb()->has_pro() ? 'pro' : 'pro-disabled'; |
| 73 |
} |
| 74 |
/** |
| 75 |
* @param $key |
| 76 |
* |
| 77 |
* @return bool |
| 78 |
*/ |
| 79 |
public function is_widget_active( $key ) { |
| 80 |
return isset( $this->active_list[ $key ] ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @param mixed $list true | anything |
| 85 |
* @param string $filter_type full|active|inactive |
| 86 |
* |
| 87 |
* @return mixed |
| 88 |
*/ |
| 89 |
public function get_list( $list = true, $filter_type = 'full' ) { |
| 90 |
if ( $list !== true && isset( $this->full_list[ $list ] ) ) { |
| 91 |
return $this->full_list[ $list ]; |
| 92 |
} |
| 93 |
|
| 94 |
return $this->{$filter_type . '_list'}; |
| 95 |
} |
| 96 |
|
| 97 |
public function get_section() { |
| 98 |
return [ |
| 99 |
'title' => $this->title, |
| 100 |
'short_title' => ! empty( $this->short_title ) ? $this->short_title : $this->title, |
| 101 |
'description' => $this->description, |
| 102 |
'list' => $this->get_list(), |
| 103 |
'id' => $this->list_id, |
| 104 |
'categories' => $this->categories, |
| 105 |
]; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @param $key |
| 110 |
* |
| 111 |
* @return array|mixed |
| 112 |
*/ |
| 113 |
public function get_fields( $key ) { |
| 114 |
return isset( $this->full_list[ $key ]['fields'] ) ? $this->full_list[ $key ]['fields'] : []; |
| 115 |
} |
| 116 |
|
| 117 |
public function get_data() { |
| 118 |
return $this->db_options; |
| 119 |
} |
| 120 |
|
| 121 |
abstract protected function raw_list(); |
| 122 |
} |
| 123 |
|