Skip to content

Commit

Permalink
feature base class poc for standardized separation of concerns using …
Browse files Browse the repository at this point in the history
…Capabilities class
  • Loading branch information
diosmosis committed Nov 28, 2024
1 parent cb5b59a commit 0060951
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
24 changes: 22 additions & 2 deletions classes/WpMatomo.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class WpMatomo {
*/
public static $settings;

/**
* @var \WpMatomo\Feature[]
*/
private $features = [];

public function __construct() {
$this->declare_woocommerce_hpos_compatible();

Expand All @@ -65,8 +70,7 @@ public function __construct() {

add_action( 'init', [ $this, 'init_plugin' ] );

$capabilities = new Capabilities( self::$settings );
$capabilities->register_hooks();
$this->init_features();

$roles = new Roles( self::$settings );
$roles->register_hooks();
Expand Down Expand Up @@ -278,4 +282,20 @@ public static function is_async_archiving_manually_disabled() {
private static function is_async_archiving_disabled_by_setting() {
return self::$settings->is_async_archiving_disabled_by_option();
}

private function init_features() {
$this->features = [
new Capabilities( self::$settings ),
];

foreach ( $this->features as $feature ) {
if ( $feature->is_enabled() ) {
$feature->register_hooks();
}

// ajax methods must be present even if other hooks should not be added,
// since ajax requests go through admin-ajax.php
$feature->register_ajax();
}
}
}
2 changes: 1 addition & 1 deletion classes/WpMatomo/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
exit; // if accessed directly
}

class Capabilities {
class Capabilities extends Feature {

const KEY_NONE = 'none_matomo';

Expand Down
34 changes: 34 additions & 0 deletions classes/WpMatomo/Feature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
* @package matomo
*/

namespace WpMatomo;

abstract class Feature {

public function is_enabled() {
return true;
}

public function register_ajax() {
// empty
}

public function register_hooks() {
// empty
}

/**
* Optional. For tests only.
*
* @return void
*/
public function remove_hooks() {
// empty
}
}

0 comments on commit 0060951

Please sign in to comment.