BrightyCorePlugin   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 72
c 1
b 0
f 0
dl 0
loc 150
rs 10
wmc 17

10 Methods

Rating   Name   Duplication   Size   Complexity  
A register_settings() 0 2 1
A sanitize_enabled_features() 0 6 1
A register_additional_feature() 0 2 1
A is_feature_enabled() 0 6 2
A render_settings_page() 0 2 1
A __construct() 0 29 2
A brighty_core_register_required_plugins() 0 50 1
A init_core_features() 0 10 3
A add_settings_page() 0 7 1
A init_additional_features() 0 5 4
1
<?php
2
namespace BrightyCorePlugin;
3
4
class BrightyCorePlugin {
5
    private $enabled_features = array();
6
    private $additional_features = array();
7
    private $core_features = array();
8
      
9
10
    public function __construct() {
11
12
        // Define a list of core features (Modules)
13
        $this->core_features = array(
14
            'clients' => 'BrightyCorePlugin\Clients_Feature',
15
            'invoices' => 'BrightyCorePlugin\Invoices_Feature',
16
            'domains' => 'BrightyCorePlugin\Domains_Feature',
17
            // 'hosting_accounts' => 'BrightyCorePlugin\Hosting_Accounts_Feature',
18
            // 'projects' => 'BrightyCorePlugin\Projects_Feature',
19
            // 'leads' => 'BrightyCorePlugin\Leads_Feature',
20
        );
21
22
        //enable all modules by default @options page
23
        if (get_option('brighty_core_plugin_enabled_features') === false) {
24
            update_option('brighty_core_plugin_enabled_features', $this->core_features);
25
        }
26
        // Load enabled features from options
27
        $this->enabled_features = get_option('brighty_core_plugin_enabled_features', array());
28
       
29
        // Register hooks
30
        add_action('admin_menu', array($this, 'add_settings_page'));
31
        add_action('admin_init', array($this, 'register_settings'));
32
        add_action( 'tgmpa_register', array($this, 'brighty_core_register_required_plugins'));
33
       
34
        // Initialize core features
35
        $this->init_core_features();
36
37
        // Initialize additional features registered by other plugins
38
        $this->init_additional_features();
39
    }
40
41
    public function sanitize_enabled_features($input) {
42
        
43
        // Filter the input to keep only valid core feature values
44
        $input = array_intersect($input, $this->$core_features);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $core_features seems to be never defined.
Loading history...
45
    
46
        return $input;
47
    }
48
49
    public function brighty_core_register_required_plugins(){
50
            /* See TGMPA Documentation for details
51
             * Array of plugin arrays. Required keys are name and slug.
52
             * If the source is NOT from the .org repo, then source is also required.
53
             */
54
            $plugins = array(
55
                array(
56
                    'name'      => 'Metabox',
57
                    'slug'      => 'meta-box',
58
                    'required'  => true,
59
                ),
60
61
                array(
62
                    'name'      => 'Woocommerce',
63
                    'slug'      => 'woocommerce',
64
                    'required'  => true,
65
                ),
66
67
                array(
68
                    'name'      => 'Email Verification for WooCommerce',
69
                    'slug'      => 'woocommerce',
70
                    'required'  => true,
71
                ),
72
                array(
73
                    'name'      => 'TeraWallet',
74
                    'slug'      => 'woo-wallet',
75
                    'required'  => true,
76
                )
77
                ,
78
                array(
79
                    'name'      => 'WP Mail Log',
80
                    'slug'      => 'wp-mail-log',
81
                    'required'  => true,
82
                ),
83
            );
84
        
85
            $config = array(
86
                'id'           => 'brighty-core',          // Unique ID for hashing notices for multiple instances of TGMPA.
87
                'default_path' => '',                      // Default absolute path to bundled plugins.
88
                'menu'         => 'tgmpa-install-plugins', // Menu slug.
89
                'parent_slug'  => 'plugins.php',            // Parent menu slug.
90
                'capability'   => 'manage_options',    // Capability needed to view plugin install page, should be a capability associated with the parent menu used.
91
                'has_notices'  => true,                    // Show admin notices or not.
92
                'dismissable'  => false,                    // If false, a user cannot dismiss the nag message.
93
                'dismiss_msg'  => '',                      // If 'dismissable' is false, this message will be output at top of nag.
94
                'is_automatic' => true,                   // Automatically activate plugins after installation or not.
95
                'message'      => '',                      // Message to output right before the plugins table.
96
            );
97
        
98
            tgmpa( $plugins, $config );
99
    }
100
101
    public function add_settings_page() {
102
        add_options_page(
103
            'Brighty Settings',
104
            'Brighty Core',
105
            'manage_options',
106
            'brighty-core-settings',
107
            array($this, 'render_settings_page')
108
        );
109
    }
110
111
    public function render_settings_page() {
112
        include(BRIGHTY_PLUGIN_DIR_PATH . 'templates/settings-page.php');
113
    }
114
115
    public function register_settings() {
116
        register_setting('brighty_core_plugin_options', 'brighty_core_plugin_enabled_features');
117
    }
118
119
    public function is_feature_enabled($feature) {
120
        if(is_array($this->enabled_features)){
121
122
            return in_array($feature, $this->enabled_features);
123
        }
124
        return false;
125
    }
126
127
128
    private function init_core_features() {
129
            // Include core feature classes and initialize them if enabled
130
            foreach ($this->core_features as $feature_name => $class_name) {
131
                // Include the class file conditionally based on the feature status
132
                if ($this->is_feature_enabled($feature_name)) {
133
                    require_once(BRIGHTY_PLUGIN_DIR_PATH . 'includes/class.' . strtolower($feature_name) . '.php');
134
                    
135
                    // Initialize the feature class
136
                    $feature_instance = new $class_name($this);
137
                    $feature_instance->init();
138
                }
139
            }
140
141
    }
142
    
143
144
145
    public function register_additional_feature($feature_class) {
146
        $this->additional_features[] = $feature_class;
147
    }
148
149
    private function init_additional_features() {
150
        foreach ($this->additional_features as $feature_class) {
151
            if (class_exists($feature_class) && in_array('Brighty_Core_Feature_Interface', class_implements($feature_class))) {
152
                $feature = new $feature_class($this);
153
                $feature->init();
154
            }
155
        }
156
    }
157
}
158