| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\ERP\Framework; |
| 4 |
|
| 5 |
/** |
| 6 |
* ERP Admin settings class |
| 7 |
*/ |
| 8 |
class ERP_Admin_Settings { |
| 9 |
|
| 10 |
/** |
| 11 |
* Settings array |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private static $settings = []; |
| 16 |
|
| 17 |
/** |
| 18 |
* Sections |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
private static $section = []; |
| 23 |
|
| 24 |
/** |
| 25 |
* Include all settings file |
| 26 |
* |
| 27 |
* @since 0.1 |
| 28 |
* |
| 29 |
* @return array |
| 30 |
*/ |
| 31 |
public static function get_settings() { |
| 32 |
if ( !self::$settings ) { |
| 33 |
$settings = []; |
| 34 |
|
| 35 |
$settings[] = include __DIR__ . '/settings/general.php'; |
| 36 |
$settings[] = include __DIR__ . '/settings/email.php'; |
| 37 |
|
| 38 |
$settings = apply_filters( 'erp_settings_pages', $settings ); |
| 39 |
|
| 40 |
// Display integrations tab only if any integration exist. |
| 41 |
$integrations = wperp()->integration->get_integrations(); |
| 42 |
|
| 43 |
if ( ! empty( $integrations ) ) { |
| 44 |
$settings[] = include __DIR__ . '/settings/integration.php'; |
| 45 |
} |
| 46 |
|
| 47 |
$licenses = erp_addon_licenses(); |
| 48 |
|
| 49 |
if ( $licenses ) { |
| 50 |
$settings[] = include __DIR__ . '/settings/license.php'; |
| 51 |
} |
| 52 |
|
| 53 |
self::$settings = $settings; |
| 54 |
} |
| 55 |
|
| 56 |
return self::$settings; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get current tab and subtab/section |
| 61 |
* |
| 62 |
* @since 0.1 |
| 63 |
* |
| 64 |
* @return array() |
| 65 |
*/ |
| 66 |
public static function get_current_tab_and_section() { |
| 67 |
$settings = self::get_settings(); |
| 68 |
$query_arg = [ 'tab' => false, 'subtab' => false ]; |
| 69 |
|
| 70 |
if ( ! isset( $settings[0] ) ) { |
| 71 |
return $query_arg; |
| 72 |
} |
| 73 |
|
| 74 |
$default = $settings[0]->get_id(); |
| 75 |
|
| 76 |
if ( empty( $default ) ) { |
| 77 |
return $query_arg; |
| 78 |
} |
| 79 |
|
| 80 |
$current_tab = $query_arg['tab'] = isset( $_GET['tab'] ) ? sanitize_title( wp_unslash( $_GET['tab'] ) ) : $settings[0]->get_id(); |
| 81 |
|
| 82 |
foreach ( $settings as $obj ) { |
| 83 |
$sections[$obj->get_id()] = isset( $obj->sections ) ? $obj->sections : []; |
| 84 |
} |
| 85 |
|
| 86 |
if ( ! isset( $sections[$current_tab] ) ) { |
| 87 |
return $query_arg; |
| 88 |
} |
| 89 |
|
| 90 |
if ( ! is_array( $sections[$current_tab] ) ) { |
| 91 |
return $query_arg; |
| 92 |
} |
| 93 |
|
| 94 |
if ( ! count( $sections[$current_tab] ) ) { |
| 95 |
return $query_arg; |
| 96 |
} |
| 97 |
|
| 98 |
$query_arg['subtab'] = isset( $_GET['section'] ) ? sanitize_title( wp_unslash( $_GET['section'] ) ) : key( $sections[$current_tab] ); |
| 99 |
|
| 100 |
return $query_arg; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Show settings all tab and subtab fields |
| 105 |
* |
| 106 |
* @since 0.1 |
| 107 |
* |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
public static function output() { |
| 111 |
global $current_section, $current_tab, $current_class; |
| 112 |
|
| 113 |
$settings = self::get_settings(); |
| 114 |
$query_arg = self::get_current_tab_and_section(); |
| 115 |
$current_tab = $query_arg['tab']; |
| 116 |
$current_section = $query_arg['subtab']; |
| 117 |
$sections = []; |
| 118 |
|
| 119 |
if ( ! $settings ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
echo '<h2 class="nav-tab-wrapper erp-nav-tab-wrapper">'; |
| 124 |
|
| 125 |
foreach ( $settings as $obj ) { |
| 126 |
$url = sprintf( 'admin.php?page=erp-settings&tab=%s', $obj->get_id() ); |
| 127 |
$class = ( $current_tab == $obj->get_id() ) ? ' nav-tab-active' : ''; |
| 128 |
|
| 129 |
if ( $current_tab == $obj->get_id() && $current_class === null ) { |
| 130 |
$current_class = $obj; |
| 131 |
} |
| 132 |
|
| 133 |
printf( '<a class="nav-tab%s" href="%s">%s</a>', esc_attr( $class ), esc_url( $url ), esc_html( $obj->get_label() ) ); |
| 134 |
} |
| 135 |
|
| 136 |
echo '</h2>'; |
| 137 |
|
| 138 |
self::section_output(); |
| 139 |
|
| 140 |
$current_class->save( $current_section ); |
| 141 |
$current_class->output( $current_section ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Show settings subtab fields |
| 146 |
* |
| 147 |
* @since 0.1 |
| 148 |
* |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
public static function section_output() { |
| 152 |
$settings = self::get_settings(); |
| 153 |
$query_arg = self::get_current_tab_and_section(); |
| 154 |
$current_tab = $query_arg['tab']; |
| 155 |
$current_section = $query_arg['subtab']; |
| 156 |
$sections = []; |
| 157 |
|
| 158 |
if ( ! $current_section ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
foreach ( $settings as $obj ) { |
| 163 |
$sections[$obj->get_id()] = isset( $obj->sections ) ? $obj->sections : []; |
| 164 |
} |
| 165 |
|
| 166 |
$tab_sections = $sections[$current_tab]; |
| 167 |
|
| 168 |
// don't print sub-sections if only one section available |
| 169 |
if ( count( $tab_sections ) < 1 ) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
if ( ! array_key_exists( $current_section, $tab_sections ) ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
echo '<ul class="erp-subsubsub">'; |
| 178 |
|
| 179 |
echo '<li>'; |
| 180 |
|
| 181 |
foreach ( $tab_sections as $slug => $label ) { |
| 182 |
$url = 'admin.php?page=erp-settings&tab=' . $current_tab . '§ion=' . $slug; |
| 183 |
$class = ( $current_section == $slug ) ? ' erp-nav-tab-active' : ''; |
| 184 |
$link[] = '<a class="erp-nav-tab' . $class . '" href="' . $url . '">' . $label . '</a>'; |
| 185 |
} |
| 186 |
|
| 187 |
echo wp_kses_post( implode( ' | </li><li>', $link ) ); |
| 188 |
echo '</li>'; |
| 189 |
|
| 190 |
echo '</ul>'; |
| 191 |
} |
| 192 |
} |
| 193 |
|