| 1 |
<?php |
| 2 |
/** |
| 3 |
* ListModel |
| 4 |
* |
| 5 |
* @package RadiusTheme\SB |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace RadiusTheme\SB\Models\Base; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
use RadiusTheme\SB\Helpers\Fns; |
| 13 |
use RadiusTheme\SB\Models\DataModel; |
| 14 |
|
| 15 |
/** |
| 16 |
* ListModel |
| 17 |
*/ |
| 18 |
abstract class ListModel { |
| 19 |
/** |
| 20 |
* List ID |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $list_id; |
| 25 |
|
| 26 |
/** |
| 27 |
* List Title |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $title; |
| 32 |
|
| 33 |
/** |
| 34 |
* List Short Title |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
protected $short_title; |
| 39 |
|
| 40 |
/** |
| 41 |
* List Description |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
protected $description; |
| 46 |
|
| 47 |
/** |
| 48 |
* List |
| 49 |
* |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
private $full_list = []; |
| 53 |
|
| 54 |
/** |
| 55 |
* Active List |
| 56 |
* |
| 57 |
* @var array |
| 58 |
*/ |
| 59 |
private $active_list = []; |
| 60 |
|
| 61 |
/** |
| 62 |
* Inactive List |
| 63 |
* |
| 64 |
* @var array |
| 65 |
*/ |
| 66 |
private $inactive_list = []; |
| 67 |
|
| 68 |
/** |
| 69 |
* Options |
| 70 |
* |
| 71 |
* @var array |
| 72 |
*/ |
| 73 |
private $db_options = []; |
| 74 |
|
| 75 |
/** |
| 76 |
* Categories |
| 77 |
* |
| 78 |
* @var array |
| 79 |
*/ |
| 80 |
protected $categories = []; |
| 81 |
|
| 82 |
/** |
| 83 |
* Constructor |
| 84 |
*/ |
| 85 |
public function __construct() { |
| 86 |
$this->db_options = DataModel::source()->get_option( $this->list_id, [] ); |
| 87 |
$raw_list = apply_filters( 'rtsb/' . $this->list_id . '/list', $this->raw_list() ); |
| 88 |
|
| 89 |
if ( ! empty( $raw_list ) ) { |
| 90 |
foreach ( $raw_list as $name => $item ) { |
| 91 |
|
| 92 |
if ( ! empty( $item['package'] ) && 'pro-disabled' === $item['package'] ) { |
| 93 |
$item['fields'] = []; |
| 94 |
$item['active'] = false; |
| 95 |
$this->full_list[ $name ] = $item; |
| 96 |
$this->inactive_list[ $name ] = $item; |
| 97 |
continue; |
| 98 |
} |
| 99 |
|
| 100 |
if ( ! isset( $this->db_options[ $name ]['active'] ) && ! empty( $item['active'] ) ) { |
| 101 |
$item['active'] = 'on'; |
| 102 |
} else { |
| 103 |
$item['active'] = isset( $this->db_options[ $name ]['active'] ) && 'on' === $this->db_options[ $name ]['active'] ? 'on' : ''; |
| 104 |
} |
| 105 |
|
| 106 |
if ( ! empty( $item['fields'] ) ) { |
| 107 |
foreach ( $item['fields'] as $field_key => $field ) { |
| 108 |
$the_value = $this->db_options[ $name ][ $field_key ] ?? ( $item['fields'][ $field_key ]['value'] ?? '' ); |
| 109 |
if ( 'repeaters' === $field['type'] && empty( $the_value ) && ! empty( $field['defaults'] ) ) { |
| 110 |
$repeater_defaults = array_values( $field['defaults'] ); |
| 111 |
$the_value = wp_json_encode( $repeater_defaults ); |
| 112 |
} elseif ( 'repeaters' === $field['type'] && ! empty( $the_value ) ) { |
| 113 |
$the_value = json_decode( $the_value ); |
| 114 |
} |
| 115 |
$sanitized_value = Fns::stripslashes_value( $the_value ); |
| 116 |
$item['fields'][ $field_key ]['value'] = $sanitized_value; |
| 117 |
} |
| 118 |
} |
| 119 |
$this->full_list[ $name ] = $item; |
| 120 |
|
| 121 |
if ( 'on' === $item['active'] ) { |
| 122 |
$this->active_list[ $name ] = $item; |
| 123 |
} else { |
| 124 |
$this->inactive_list[ $name ] = $item; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @return string Pro Package |
| 132 |
*/ |
| 133 |
protected function pro_package() { |
| 134 |
return rtsb()->has_pro() ? 'pro' : 'pro-disabled'; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @return string Pro Package |
| 139 |
*/ |
| 140 |
protected function pro_version_checker() { |
| 141 |
$has_pro = rtsb()->has_pro(); |
| 142 |
$pro_ver = defined( 'RTSBPRO_VERSION' ) ? RTSBPRO_VERSION : 0; |
| 143 |
|
| 144 |
if ( ! $has_pro ) { |
| 145 |
return 'free'; |
| 146 |
} |
| 147 |
|
| 148 |
if ( version_compare( $pro_ver, '2.0.0', '>=' ) ) { |
| 149 |
return 'pass'; |
| 150 |
} |
| 151 |
|
| 152 |
return 'fail'; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Is widget active |
| 157 |
* |
| 158 |
* @param string $key Key. |
| 159 |
* |
| 160 |
* @return bool |
| 161 |
*/ |
| 162 |
public function is_widget_active( $key ) { |
| 163 |
return isset( $this->active_list[ $key ] ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get list |
| 168 |
* |
| 169 |
* @param bool $list List. |
| 170 |
* @param string $filter_type Filter type. |
| 171 |
* |
| 172 |
* @return mixed |
| 173 |
*/ |
| 174 |
public function get_list( $list = true, $filter_type = 'full' ) { |
| 175 |
if ( true !== $list && isset( $this->full_list[ $list ] ) ) { |
| 176 |
return $this->full_list[ $list ]; |
| 177 |
} |
| 178 |
|
| 179 |
return $this->{$filter_type . '_list'}; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Get section |
| 184 |
* |
| 185 |
* @return array |
| 186 |
*/ |
| 187 |
public function get_section() { |
| 188 |
return [ |
| 189 |
'title' => $this->title, |
| 190 |
'short_title' => ! empty( $this->short_title ) ? $this->short_title : $this->title, |
| 191 |
'description' => $this->description, |
| 192 |
'list' => $this->get_list(), |
| 193 |
'id' => $this->list_id, |
| 194 |
'categories' => $this->categories, |
| 195 |
]; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Get fields |
| 200 |
* |
| 201 |
* @param string $key Key. |
| 202 |
* |
| 203 |
* @return array|mixed |
| 204 |
*/ |
| 205 |
public function get_fields( $key ) { |
| 206 |
return isset( $this->full_list[ $key ]['fields'] ) ? $this->full_list[ $key ]['fields'] : []; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Get data |
| 211 |
* |
| 212 |
* @return array |
| 213 |
*/ |
| 214 |
public function get_data() { |
| 215 |
return $this->db_options; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Get raw list |
| 220 |
* |
| 221 |
* @return array |
| 222 |
*/ |
| 223 |
abstract protected function raw_list(); |
| 224 |
} |
| 225 |
|