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