| 1 |
<?php |
| 2 |
/** |
| 3 |
* HootKit Config |
| 4 |
* This file is loaded during plugin load |
| 5 |
* |
| 6 |
* @package Hootkit |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace HootKit\Inc; |
| 10 |
|
| 11 |
// If this file is called directly, abort. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
die; |
| 14 |
} |
| 15 |
|
| 16 |
if ( ! class_exists( '\HootKit\Inc\HKConfig' ) ) : |
| 17 |
|
| 18 |
class HKConfig { |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Instance |
| 22 |
*/ |
| 23 |
private static $instance; |
| 24 |
|
| 25 |
/** |
| 26 |
* Config |
| 27 |
*/ |
| 28 |
public static $config = array(); |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor |
| 32 |
*/ |
| 33 |
public function __construct() { |
| 34 |
add_action( 'after_setup_theme', array( $this, 'themeregister' ), 90 ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Register theme config |
| 39 |
*/ |
| 40 |
public function themeregister() { |
| 41 |
$themeconfig = apply_filters( 'hootkit_register', array() ); |
| 42 |
|
| 43 |
if ( !empty( $themeconfig ) && \is_array( $themeconfig ) ) { |
| 44 |
$themeconfig = $this->maybe_restructure( $themeconfig ); |
| 45 |
self::$config = wp_parse_args( $themeconfig, self::defaults() ); |
| 46 |
$this->sanitizeconfig(); |
| 47 |
$this->setactivemodules(); |
| 48 |
do_action( 'hootkit_config_registered' ); |
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Restructure config array from theme if needed |
| 55 |
*/ |
| 56 |
private function maybe_restructure( $themeconfig ) { |
| 57 |
if ( !empty( $themeconfig['modules'] ) ) { |
| 58 |
if ( !\is_array( $themeconfig['modules'] ) ) { |
| 59 |
unset( $themeconfig['modules'] ); |
| 60 |
} else { |
| 61 |
// 1. Rename slider slugs |
| 62 |
if ( !empty( $themeconfig['modules']['sliders'] ) && is_array( $themeconfig['modules']['sliders'] ) ) { |
| 63 |
foreach ( $themeconfig['modules']['sliders'] as $slkey => $name ) { |
| 64 |
if ( \in_array( $name, array( 'image', 'postimage' ) ) ) |
| 65 |
$themeconfig['modules']['sliders'][$slkey] = 'slider-' . $name; |
| 66 |
} |
| 67 |
} |
| 68 |
// 2. Restructure to new format |
| 69 |
$modules = array(); |
| 70 |
foreach ( Manifest::$modtypes as $type ) { |
| 71 |
$modules[ $type ] = isset( $themeconfig['modules'][ $type ] ) && is_array( $themeconfig['modules'][ $type ] ) ? $themeconfig['modules'][ $type ] : array(); |
| 72 |
} |
| 73 |
if ( isset( $modules['widget'] ) && is_array( $modules['widget'] ) ) { |
| 74 |
foreach ( array( 'widgets', 'sliders', 'woocommerce' ) as $oldkeys ) { |
| 75 |
if ( isset( $themeconfig['modules'][ $oldkeys ] ) && is_array( $themeconfig['modules'][ $oldkeys ] ) ) { |
| 76 |
$modules['widget'] = array_merge( $modules['widget'], $themeconfig['modules'][ $oldkeys ] ); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
$themeconfig['modules'] = $modules; |
| 81 |
} |
| 82 |
} |
| 83 |
$themeconfig['supports_version'] = isset( $themeconfig['supports_version'] ) ? ( |
| 84 |
$themeconfig['supports_version'] === 'v2' ? array( 'widgets-v2' ) : ( |
| 85 |
\is_array( $themeconfig['supports_version'] ) ? $themeconfig['supports_version'] : array() |
| 86 |
) |
| 87 |
) : array(); |
| 88 |
if ( !defined( 'HOOT_PREMIUM_VERSION' ) && empty( $themeconfig['theme-filters'] ) ) { |
| 89 |
$slug = false; |
| 90 |
if ( function_exists( 'olius_abouttag' ) ) $slug = 'olius'; |
| 91 |
elseif ( function_exists( 'strute_abouttag' ) ) $slug = 'strute'; |
| 92 |
elseif ( function_exists( 'nirvata_abouttag' ) ) $slug = 'nirvata'; |
| 93 |
if ( $slug ) { |
| 94 |
$themeconfig['theme-filters'] = array( |
| 95 |
'fnspace' => $slug, |
| 96 |
'abouttags' => array( 'fullshot' ), |
| 97 |
'customizer' => array( 'pattern_pnote', 'sblayoutpnote', 'colorspnote', 'typopnote', 'archivetypepnote', 'singlemetapnote', 'article_background_pnote', 'article_maxwidth_pnote', 'topann_content', 'header_image_title', 'header_image_subtitle', 'header_image_text' ), |
| 98 |
); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return $themeconfig; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Sanitize config values from theme and/or the default config values |
| 107 |
*/ |
| 108 |
public function sanitizeconfig() { |
| 109 |
/* |
| 110 |
* Sanitize Theme Supported Modules against HootKit manifest modules |
| 111 |
* Arrange in order of HK manifest modules |
| 112 |
* Add woocommerce modules only if plugin is active - Else add to 'wc-inactive' |
| 113 |
*/ |
| 114 |
self::$config['wc-inactive'] = Manifest::$modtypesarray; |
| 115 |
$wc = class_exists( 'WooCommerce' ); |
| 116 |
$store = Manifest::$modtypesarray; |
| 117 |
if ( !empty( self::$config['modules'] ) && \is_array( self::$config['modules'] ) ) { |
| 118 |
foreach ( self::$config['modules'] as $type => $thememods ) { |
| 119 |
if ( is_array( $thememods ) ) { |
| 120 |
// Arrange in order of HK manifest modules |
| 121 |
$hkmodules = hootkit()->get_mfmods_oftype( $type ); |
| 122 |
foreach ( $hkmodules as $modid => $modatts ) { |
| 123 |
if ( \in_array( $modid, $thememods ) ) { |
| 124 |
// If this is a WC module - add to 'modules' only if WC is available |
| 125 |
// Else add to 'wc-inactive' |
| 126 |
if ( isset( $modatts['requires'] ) && \in_array( 'woocommerce', $modatts['requires'] ) ) { |
| 127 |
if ( $wc ) { $store[ $type ][] = $modid; } |
| 128 |
else { self::$config['wc-inactive'][ $type ][] = $modid; } |
| 129 |
} else { |
| 130 |
$store[ $type ][] = $modid; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
self::$config['modules'] = $store; |
| 138 |
|
| 139 |
/* Sanitize Theme Supported Premium Modules against HootKit Manifest modules */ |
| 140 |
if ( !empty( self::$config['premium'] ) && \is_array( self::$config['premium'] ) ) { |
| 141 |
$hkmodules = hootkit()->get_mfmods_oftype( 'all', true ); |
| 142 |
foreach ( self::$config['premium'] as $modkey => $modid ) { |
| 143 |
if ( ! in_array( $modid, $hkmodules ) ) |
| 144 |
unset( self::$config['premium'][$modkey] ); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/* Sanitize Theme specific supported settings against HootKit supported settings */ |
| 149 |
if ( !empty( self::$config['supports'] ) && \is_array( self::$config['supports'] ) ) { |
| 150 |
$hksupports = hootkit()->get_manifest( 'supports' ); |
| 151 |
foreach ( self::$config['supports'] as $skey => $support ) { |
| 152 |
if ( !in_array( $support, $hksupports ) ) { |
| 153 |
unset( self::$config['supports'][ $skey ] ); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/* Sanitize Theme specific dashboard plugs against HootKit supported dashboard plugs */ |
| 159 |
$store = hootkit()->get_manifest( 'dashboard' ); |
| 160 |
if ( !empty( self::$config['dashboard'] ) && \is_array( self::$config['dashboard'] ) ) { |
| 161 |
foreach ( $store as $key => $value ) { |
| 162 |
if ( isset( self::$config['dashboard'][ $key ] ) && is_scalar( self::$config['dashboard'][ $key ] ) ) { |
| 163 |
$store[ $key ] = self::$config['dashboard'][ $key ]; |
| 164 |
} |
| 165 |
} |
| 166 |
/* Basic dependency checks for dashboard */ |
| 167 |
$store['dashmenu'] = !empty( $store['dashmenu'] ) && is_string( $store['dashmenu'] ) && !empty( $store['aboutfilter'] ) && is_string( $store['aboutfilter'] ) ? $store['dashmenu'] : ''; |
| 168 |
$hasrequired = ( function_exists( 'hoot_dashboard' ) |
| 169 |
&& !empty( $store['tabfilter'] ) && is_string( $store['tabfilter'] ) |
| 170 |
&& !empty( $store['tabaction'] ) && is_string( $store['tabaction'] ) |
| 171 |
); |
| 172 |
foreach ( array( 'settings', 'code', 'tools', 'import' ) as $checkid ) { |
| 173 |
$store[ $checkid ] = $hasrequired && !empty( $store[ $checkid ] ) && is_string( $store[ $checkid ] ) ? $store[ $checkid ] : ''; |
| 174 |
} |
| 175 |
$store['import'] = !empty( $store['import'] ) && is_string( $store['import'] ) && !empty( $store['import_id'] ) && is_string( $store['import_id'] ) ? $store['import'] : ''; |
| 176 |
} |
| 177 |
self::$config['dashboard'] = $store; |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Set User Activated modules |
| 183 |
* |
| 184 |
* @since 1.1.0 |
| 185 |
*/ |
| 186 |
public function setactivemodules() { |
| 187 |
|
| 188 |
$dbvalue = get_option( 'hootkit-activemods', false ); |
| 189 |
$dbvalue = \is_array( $dbvalue ) && !empty( $dbvalue ) ? $dbvalue : array(); |
| 190 |
$disabled = !empty( $dbvalue[ 'disabled' ] ) && \is_array( $dbvalue[ 'disabled' ] ) ? $dbvalue[ 'disabled' ] : array(); |
| 191 |
|
| 192 |
$store = Manifest::$modtypesarray; |
| 193 |
foreach ( $store as $type => $arr ) { |
| 194 |
|
| 195 |
// User has not modified any default settings yet |
| 196 |
// Hence set default active modules => all deactive if (bool) false ; all active if empty |
| 197 |
if ( empty( $dbvalue ) ) { |
| 198 |
$store[ $type ] = hootkit()->get_config( 'modules', $type ); |
| 199 |
$themeactivemods = self::$config['activemods']; |
| 200 |
if ( $themeactivemods === false ) { |
| 201 |
$store[ $type ] = array(); |
| 202 |
} elseif ( is_array( $themeactivemods ) && isset( $themeactivemods[ $type ] ) ) { |
| 203 |
if ( $themeactivemods[ $type ] === false ) { |
| 204 |
$store[ $type ] = array(); |
| 205 |
} elseif ( !empty( $themeactivemods[ $type ] ) && \is_array( $themeactivemods[ $type ] ) ) { |
| 206 |
$store[ $type ] = $themeactivemods[ $type ]; |
| 207 |
} |
| 208 |
} |
| 209 |
} else { |
| 210 |
if ( \in_array( $type, $disabled ) ) { |
| 211 |
$store[ $type ] = array(); |
| 212 |
} elseif ( !empty( $dbvalue[ $type ] ) && \is_array( $dbvalue[ $type ] ) ) { |
| 213 |
foreach ( hootkit()->get_config( 'modules', $type ) as $check ) { |
| 214 |
if ( !isset( $dbvalue[ $type ][ $check ] ) || $dbvalue[ $type ][ $check ] == 'yes' ) |
| 215 |
$store[ $type ][] = $check; |
| 216 |
} |
| 217 |
} else { |
| 218 |
$store[ $type ] = hootkit()->get_config( 'modules', $type ); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
} |
| 223 |
self::$config['activemods'] = apply_filters( 'hootkit_active_modules', $store ); |
| 224 |
|
| 225 |
/* |
| 226 |
* Sanitize Active Modules against HootKit manifest modules |
| 227 |
* Arrange in order of HK manifest modules |
| 228 |
* Add woocommerce modules only if plugin is active (Ex: User saves settings with WC active; later disabled WC) |
| 229 |
*/ |
| 230 |
$wc = class_exists( 'WooCommerce' ); |
| 231 |
$store = Manifest::$modtypesarray; |
| 232 |
foreach ( $store as $type => $arr ) { |
| 233 |
if ( !empty( self::$config['activemods'][ $type ] ) ) { |
| 234 |
// Arrange in order of HK manifest modules |
| 235 |
$hkmodules = hootkit()->get_mfmods_oftype( $type ); |
| 236 |
foreach ( $hkmodules as $modid => $modatts ) { |
| 237 |
if ( \in_array( $modid, self::$config['activemods'][ $type ] ) ) { |
| 238 |
// If this is a WC module - add to 'activemods' only if WC is available |
| 239 |
// Else skip it |
| 240 |
if ( isset( $modatts['requires'] ) && \in_array( 'woocommerce', $modatts['requires'] ) ) { |
| 241 |
if ( $wc ) { $store[ $type ][] = $modid; } |
| 242 |
} else { |
| 243 |
$store[ $type ][] = $modid; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
self::$config['activemods'] = $store; |
| 250 |
self::$config['disabledmodtypes'] = $disabled; |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Config Structure (Defaults) |
| 256 |
*/ |
| 257 |
public static function defaults() { |
| 258 |
return array( |
| 259 |
|
| 260 |
/** Required - Themes should register these for best performance **/ |
| 261 |
|
| 262 |
// Theme Supported Modules - @see 'mods' for available list |
| 263 |
'modules' => array( |
| 264 |
'widget' => array(), |
| 265 |
'block' => array(), |
| 266 |
'misc' => array(), |
| 267 |
), |
| 268 |
// Theme supports - @see 'mods' for available list |
| 269 |
'supports' => array(), |
| 270 |
// Version Support |
| 271 |
'supports_version' => array(), |
| 272 |
// Premium modules list |
| 273 |
'premium' => array(), |
| 274 |
// Theme filters to be applied [fnspace,abouttags,customizer] |
| 275 |
'theme-filters' => array(), |
| 276 |
|
| 277 |
/** Optional / Plugin Generated */ |
| 278 |
|
| 279 |
// Extracted list from 'modules' if WC is inactive |
| 280 |
'wc-inactive' => array( |
| 281 |
'widget' => array(), |
| 282 |
'block' => array(), |
| 283 |
'misc' => array(), |
| 284 |
), |
| 285 |
// Active Modules (user settings) |
| 286 |
// Optional: Themes can pass an array here to set them as defaults (before user settings saved) |
| 287 |
// Set to false for all deactive by default. Anything else is all active by default. |
| 288 |
'activemods' => array( |
| 289 |
'widget' => array(), |
| 290 |
'block' => array(), |
| 291 |
'misc' => array(), |
| 292 |
), |
| 293 |
// Disabled Mod Types (user settings). |
| 294 |
'disabledmodtypes' => array(), |
| 295 |
// Default Styles |
| 296 |
'presets' => array( |
| 297 |
'white' => __( 'White', 'hootkit' ), |
| 298 |
'black' => __( 'Black', 'hootkit' ), |
| 299 |
'brown' => __( 'Brown', 'hootkit' ), |
| 300 |
'blue' => __( 'Blue', 'hootkit' ), |
| 301 |
'cyan' => __( 'Cyan', 'hootkit' ), |
| 302 |
'green' => __( 'Green', 'hootkit' ), |
| 303 |
'yellow' => __( 'Yellow', 'hootkit' ), |
| 304 |
'amber' => __( 'Amber', 'hootkit' ), |
| 305 |
'orange' => __( 'Orange', 'hootkit' ), |
| 306 |
'red' => __( 'Red', 'hootkit' ), |
| 307 |
'pink' => __( 'Pink', 'hootkit' ), |
| 308 |
), |
| 309 |
// Default Styles |
| 310 |
'presetcombo' => array( |
| 311 |
'white' => __( 'White', 'hootkit' ), |
| 312 |
'black' => __( 'Black', 'hootkit' ), |
| 313 |
'brown' => __( 'Brown', 'hootkit' ), |
| 314 |
'brownbright' => __( 'Brown (Bright)', 'hootkit' ), |
| 315 |
'blue' => __( 'Blue', 'hootkit' ), |
| 316 |
'bluebright' => __( 'Blue (Bright)', 'hootkit' ), |
| 317 |
'cyan' => __( 'Cyan', 'hootkit' ), |
| 318 |
'cyanbright' => __( 'Cyan (Bright)', 'hootkit' ), |
| 319 |
'green' => __( 'Green', 'hootkit' ), |
| 320 |
'greenbright' => __( 'Green (Bright)', 'hootkit' ), |
| 321 |
'yellow' => __( 'Yellow', 'hootkit' ), |
| 322 |
'yellowbright' => __( 'Yellow (Bright)', 'hootkit' ), |
| 323 |
'amber' => __( 'Amber', 'hootkit' ), |
| 324 |
'amberbright' => __( 'Amber (Bright)', 'hootkit' ), |
| 325 |
'orange' => __( 'Orange', 'hootkit' ), |
| 326 |
'orangebright' => __( 'Orange (Bright)', 'hootkit' ), |
| 327 |
'red' => __( 'Red', 'hootkit' ), |
| 328 |
'redbright' => __( 'Red (Bright)', 'hootkit' ), |
| 329 |
'pink' => __( 'Pink', 'hootkit' ), |
| 330 |
'pinkbright' => __( 'Pink (Bright)', 'hootkit' ), |
| 331 |
), |
| 332 |
); |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Returns the instance |
| 337 |
*/ |
| 338 |
public static function get_instance() { |
| 339 |
if ( ! isset( self::$instance ) ) { |
| 340 |
self::$instance = new self(); |
| 341 |
} |
| 342 |
return self::$instance; |
| 343 |
} |
| 344 |
|
| 345 |
} |
| 346 |
|
| 347 |
HKConfig::get_instance(); |
| 348 |
|
| 349 |
endif; |