| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Settings_Addons |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Admin/Classes/Settings |
| 7 |
* @version 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Shared settings tab for LearnPress add-ons. |
| 14 |
*/ |
| 15 |
class LP_Settings_Addons extends LP_Abstract_Settings_Page { |
| 16 |
/** |
| 17 |
* Constructor. |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
$this->id = 'addons'; |
| 21 |
$this->text = esc_html__( 'Addons', 'learnpress' ); |
| 22 |
|
| 23 |
parent::__construct(); |
| 24 |
|
| 25 |
add_filter( 'learn-press/admin/submenu-has-sections', array( $this, 'force_section_nav' ), 10, 3 ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Always show the section navigation for the Addons tab, even with a single |
| 30 |
* add-on section, so each add-on reads as a distinct section like Payments. |
| 31 |
* |
| 32 |
* @param bool $has_sections Current decision (>1 section). |
| 33 |
* @param array<string, mixed> $sections Sections for the active tab. |
| 34 |
* @param string $active_tab Active settings tab id. |
| 35 |
* |
| 36 |
* @return bool |
| 37 |
*/ |
| 38 |
public function force_section_nav( $has_sections, $sections, $active_tab ) { |
| 39 |
if ( $this->id === $active_tab && ! empty( $sections ) ) { |
| 40 |
return true; |
| 41 |
} |
| 42 |
|
| 43 |
return $has_sections; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Sections contributed by active add-ons. |
| 48 |
* |
| 49 |
* @return array<string, string> |
| 50 |
*/ |
| 51 |
public function get_sections() { |
| 52 |
$sections = apply_filters( 'learn-press/settings/addons/sections', array() ); |
| 53 |
|
| 54 |
return is_array( $sections ) ? $sections : array(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Whether any add-on registered a settings section. |
| 59 |
* |
| 60 |
* @return bool |
| 61 |
*/ |
| 62 |
public function has_sections(): bool { |
| 63 |
return ! empty( $this->get_sections() ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Field dispatch via add-on filters. |
| 68 |
* |
| 69 |
* @param string|array $section Section key or keys. |
| 70 |
* @param string $tab Tab key. |
| 71 |
* |
| 72 |
* @return array<int, array<string, mixed>> |
| 73 |
*/ |
| 74 |
public function get_settings( $section = '', $tab = '' ) { |
| 75 |
if ( ! $section ) { |
| 76 |
$section = array_keys( $this->get_sections() ); |
| 77 |
} |
| 78 |
|
| 79 |
settype( $section, 'array' ); |
| 80 |
|
| 81 |
$return = array(); |
| 82 |
|
| 83 |
foreach ( $section as $sec ) { |
| 84 |
$fields = apply_filters( "learn-press/settings/addons/fields-{$sec}", array() ); |
| 85 |
|
| 86 |
if ( $fields && is_array( $fields ) ) { |
| 87 |
$return = array_merge( $return, $fields ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return $return; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return new LP_Settings_Addons(); |
| 96 |
|