| 1 |
<?php |
| 2 |
/** |
| 3 |
* Selection Lite |
| 4 |
* Carefully selected Elementor addons bundle, for building the most awesome websites |
| 5 |
* |
| 6 |
* @encoding UTF-8 |
| 7 |
* @version 1.12 |
| 8 |
* @copyright (C) 2018-2024 Merkulove ( https://merkulov.design/ ). All rights reserved. |
| 9 |
* @license GPLv3 |
| 10 |
* @contributors merkulove, vladcherviakov, phoenixmkua, podolianochka, viktorialev01 |
| 11 |
* @support help@merkulov.design |
| 12 |
**/ |
| 13 |
|
| 14 |
namespace Merkulove\SelectionLite\Unity; |
| 15 |
|
| 16 |
/** Exit if accessed directly. */ |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
header( 'Status: 403 Forbidden' ); |
| 19 |
header( 'HTTP/1.1 403 Forbidden' ); |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* SINGLETON: Class used to implement any tab with settings. |
| 25 |
* |
| 26 |
* @since 1.0 |
| 27 |
* |
| 28 |
**/ |
| 29 |
final class TabGeneral extends Tab { |
| 30 |
|
| 31 |
/** |
| 32 |
* The one true TabGeneral. |
| 33 |
* |
| 34 |
* @since 1.0 |
| 35 |
* @access private |
| 36 |
* @var TabGeneral |
| 37 |
**/ |
| 38 |
private static $instance; |
| 39 |
|
| 40 |
/** |
| 41 |
* Render form with all settings fields. |
| 42 |
* |
| 43 |
* @since 1.0 |
| 44 |
* @access public |
| 45 |
* |
| 46 |
* @param string $tab_slug - Slug of current tab. |
| 47 |
* |
| 48 |
* @return void |
| 49 |
**/ |
| 50 |
public function do_settings( $tab_slug ) { |
| 51 |
|
| 52 |
/** No status tab, nothing to do. */ |
| 53 |
if ( ! $this->is_enabled( $tab_slug ) ) { return; } |
| 54 |
|
| 55 |
/** Render title. */ |
| 56 |
$this->render_title( $tab_slug ); |
| 57 |
|
| 58 |
/** Render fields. */ |
| 59 |
$this->do_settings_base( $tab_slug ); |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Generate General Tab. |
| 65 |
* |
| 66 |
* @since 1.0 |
| 67 |
* @access public |
| 68 |
* |
| 69 |
* @param string $tab_slug - Slug of current tab. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
**/ |
| 73 |
public function add_settings( $tab_slug ) { |
| 74 |
|
| 75 |
/** Custom General Tab. */ |
| 76 |
$this->add_settings_base( $tab_slug ); |
| 77 |
|
| 78 |
$group = 'SelectionLite' . $tab_slug . 'OptionsGroup'; |
| 79 |
$section = 'mdp_selection_lite_' . $tab_slug . '_page_status_section'; |
| 80 |
|
| 81 |
/** Exit if no fields to process. */ |
| 82 |
if ( empty( Plugin::get_tabs()[$tab_slug]['fields'] ) ) { return; } |
| 83 |
|
| 84 |
$fields = Plugin::get_tabs()[$tab_slug]['fields']; |
| 85 |
|
| 86 |
/** Create settings for each field. */ |
| 87 |
foreach ( $fields as $key => $field ) { |
| 88 |
|
| 89 |
/** Prepare field label. */ |
| 90 |
$label = isset( $field[ 'label' ] ) && ! empty( $field[ 'label' ] ) ? $field[ 'label' ] : ''; |
| 91 |
|
| 92 |
/** Hide label for header fields. */ |
| 93 |
if ( 'header' === $field['type'] ) { $label = ''; } |
| 94 |
|
| 95 |
/** Create field. */ |
| 96 |
add_settings_field( $key, $label, [ $this, 'create_field' ], $group, $section, [ 'key' => $key, 'type' => $field[ 'type' ], 'tab_slug' => $tab_slug ] ); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Render Settings field. |
| 104 |
* |
| 105 |
* @param array $args - Array of params for render: field key and type. |
| 106 |
* |
| 107 |
* @since 1.0 |
| 108 |
* @access public |
| 109 |
* |
| 110 |
* @return void |
| 111 |
**/ |
| 112 |
public function create_field( $args = [] ) { |
| 113 |
|
| 114 |
/** Do we have custom handler for this field type? */ |
| 115 |
$handler = $this->get_field_handler( $args ); |
| 116 |
if ( is_array( $handler ) && is_callable( $handler ) ) { |
| 117 |
|
| 118 |
/** Call custom render for field. */ |
| 119 |
$handler( $args[ 'key' ], $args[ 'tab_slug' ] ); |
| 120 |
return; |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
/** In field haven't custom render check maybe we have standard handler for this field type? */ |
| 125 |
if ( ! is_callable( [ $this, 'render_' . $args[ 'type' ] ] ) ) { |
| 126 |
?><div class="mdc-system-warn"><?php esc_html_e( 'Handler for this field type not found.' ); ?></div><?php |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
/** Call render field by type. */ |
| 131 |
$this->{'render_' . $args[ 'type' ]}( $args['key'], $args['tab_slug'] ); |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Return custom handler for field or false. |
| 137 |
* |
| 138 |
* @param array $args - Array of params for render: field key and type. |
| 139 |
* |
| 140 |
* @since 1.0 |
| 141 |
* @access public |
| 142 |
* |
| 143 |
* @return array|false |
| 144 |
**/ |
| 145 |
private function get_field_handler( $args ) { |
| 146 |
|
| 147 |
/** Get field. */ |
| 148 |
$tabs = Plugin::get_tabs(); |
| 149 |
$tab = $tabs[ $args[ 'tab_slug' ] ]; |
| 150 |
$fields = $tab[ 'fields' ]; |
| 151 |
$field = $fields[ $args[ 'key' ] ]; |
| 152 |
|
| 153 |
if ( ! empty( $field[ 'render' ] ) ) { |
| 154 |
return $field[ 'render' ]; |
| 155 |
} |
| 156 |
|
| 157 |
return false; |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Prepare general field params. |
| 163 |
* |
| 164 |
* @param $tab_slug |
| 165 |
* @param $key |
| 166 |
* |
| 167 |
* @return array |
| 168 |
**/ |
| 169 |
public function prepare_general_params( $tab_slug, $key ) { |
| 170 |
|
| 171 |
/** Get field settings. */ |
| 172 |
$field = Plugin::get_tabs()[ $tab_slug ][ 'fields' ][ $key ]; |
| 173 |
|
| 174 |
/** Prepare label, description and attributes. */ |
| 175 |
if ( isset( $field[ 'placeholder' ] ) && ! empty( $field[ 'placeholder' ] ) ) { |
| 176 |
$label = $field[ 'placeholder' ]; |
| 177 |
} else { |
| 178 |
$label = isset( $field[ 'label' ] ) && ! empty( $field[ 'label' ] ) ? $field[ 'label' ] : ''; |
| 179 |
} |
| 180 |
$description = ! empty( $field[ 'description' ] ) ? $field[ 'description' ] : ''; |
| 181 |
|
| 182 |
$attr = $this->prepare_attr( $key, $tab_slug, $field ); |
| 183 |
|
| 184 |
return [ $field, $label, $description, $attr ]; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Prepare array with attributes. |
| 189 |
* |
| 190 |
* @param string $key - Field key. |
| 191 |
* @param string $tab_slug - Tab slug to which the field belongs. |
| 192 |
* @param array $field |
| 193 |
* |
| 194 |
* @since 1.0 |
| 195 |
* @access private |
| 196 |
* |
| 197 |
* @return array |
| 198 |
**/ |
| 199 |
private function prepare_attr( $key, $tab_slug, $field ) { |
| 200 |
|
| 201 |
$name = 'mdp_selection_lite_' . $tab_slug . '_settings'; |
| 202 |
|
| 203 |
$attr = [ |
| 204 |
'name' => $name . '[' . $key . ']', |
| 205 |
'id' => $name . '_' . $key, |
| 206 |
]; |
| 207 |
|
| 208 |
if ( ! empty( $field['attr'] ) ) { |
| 209 |
$attr = array_merge( $attr, $field['attr'] ); |
| 210 |
} |
| 211 |
|
| 212 |
return $attr; |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Main TabGeneral Instance. |
| 218 |
* Insures that only one instance of TabGeneral exists in memory at any one time. |
| 219 |
* |
| 220 |
* @static |
| 221 |
* @return TabGeneral |
| 222 |
**/ |
| 223 |
public static function get_instance() { |
| 224 |
|
| 225 |
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) { |
| 226 |
|
| 227 |
self::$instance = new self; |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
return self::$instance; |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
} |
| 236 |
|