| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Logs Connectors. |
| 4 |
* |
| 5 |
* This file handles all interactions with the Client DB. |
| 6 |
* |
| 7 |
* @package MainWP/Dashboard |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace MainWP\Dashboard\Module\Log; |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Class Log_Connectors |
| 19 |
*/ |
| 20 |
class Log_Connectors { |
| 21 |
|
| 22 |
/** |
| 23 |
* Holds instance of manager object |
| 24 |
* |
| 25 |
* @var Log_Manager |
| 26 |
*/ |
| 27 |
public $manager; |
| 28 |
|
| 29 |
/** |
| 30 |
* Connectors registered |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
public $connectors = array(); |
| 35 |
|
| 36 |
/** |
| 37 |
* Contexts registered to Connectors |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
public $contexts = array(); |
| 42 |
|
| 43 |
/** |
| 44 |
* Action taxonomy terms |
| 45 |
* Holds slug to localized label association |
| 46 |
* |
| 47 |
* @var array |
| 48 |
*/ |
| 49 |
public $term_labels = array( |
| 50 |
'logs_connector' => array(), |
| 51 |
'logs_context' => array(), |
| 52 |
'logs_action' => array(), |
| 53 |
); |
| 54 |
|
| 55 |
/** |
| 56 |
* Constructor. |
| 57 |
* |
| 58 |
* Run each time the class is called. |
| 59 |
* |
| 60 |
* @param Log_Manager $manager The main manager class. |
| 61 |
*/ |
| 62 |
public function __construct( $manager ) { |
| 63 |
$this->manager = $manager; |
| 64 |
$this->load_connectors(); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Load built-in connectors |
| 69 |
* |
| 70 |
* @uses \MainWP\Dashboard\Module\Log\Log_Connector |
| 71 |
*/ |
| 72 |
public function load_connectors() { //phpcs:ignore -- NOSONAR - complex method. |
| 73 |
$connectors = $this->manager->get_internal_connectors(); |
| 74 |
|
| 75 |
$enabled_logging = ! empty( $this->manager->settings->options['enabled'] ) ? true : false; |
| 76 |
if ( $enabled_logging ) { |
| 77 |
$connectors = array_merge( |
| 78 |
$connectors, |
| 79 |
array( |
| 80 |
'site', |
| 81 |
'client', |
| 82 |
'posts', |
| 83 |
'installer', |
| 84 |
'user', |
| 85 |
'non-mainwp-changes', |
| 86 |
'changes-logs', |
| 87 |
) |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
$classes = array(); |
| 92 |
foreach ( $connectors as $connector ) { |
| 93 |
include_once $this->manager->locations['dir'] . 'connectors/class-connector-' . $connector . '.php'; // NOSONAR - WP compatible. |
| 94 |
$class_name = sprintf( '\MainWP\Dashboard\Module\Log\Connector_%s', str_replace( '-', '_', $connector ) ); |
| 95 |
if ( ! class_exists( $class_name ) ) { |
| 96 |
continue; |
| 97 |
} |
| 98 |
$class = new $class_name( $this->manager->log ); |
| 99 |
$classes[ $class->name ] = $class; |
| 100 |
} |
| 101 |
|
| 102 |
if ( $enabled_logging ) { |
| 103 |
/** |
| 104 |
* Allows for adding additional connectors via classes that extend Connector. |
| 105 |
* |
| 106 |
* @param array $classes An array of Connector objects. |
| 107 |
*/ |
| 108 |
$this->connectors = apply_filters( 'mainwp_module_log_connectors', $classes, $this->manager->log ); |
| 109 |
} else { |
| 110 |
$this->connectors = $classes; // do not load external connectors. |
| 111 |
} |
| 112 |
|
| 113 |
if ( empty( $this->connectors ) ) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
foreach ( $this->connectors as $connector ) { |
| 118 |
if ( ! method_exists( $connector, 'get_label' ) ) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
$this->term_labels['logs_connector'][ $connector->name ] = $connector->get_label(); |
| 122 |
} |
| 123 |
|
| 124 |
// Get excluded connectors. |
| 125 |
$excluded_connectors = array(); |
| 126 |
|
| 127 |
foreach ( $this->connectors as $connector ) { |
| 128 |
if ( ! method_exists( $connector, 'get_label' ) ) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
if ( ! method_exists( $connector, 'register' ) ) { |
| 132 |
continue; |
| 133 |
} |
| 134 |
if ( ! method_exists( $connector, 'get_context_labels' ) ) { |
| 135 |
continue; |
| 136 |
} |
| 137 |
if ( ! method_exists( $connector, 'get_action_labels' ) ) { |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
// Check if the connectors extends the Connector class, if not skip it. |
| 142 |
if ( ! is_subclass_of( $connector, '\MainWP\Dashboard\Module\Log\Log_Connector' ) ) { |
| 143 |
continue; |
| 144 |
} |
| 145 |
|
| 146 |
// Store connector label. |
| 147 |
if ( ! in_array( $connector->name, $this->term_labels['logs_connector'], true ) ) { |
| 148 |
$this->term_labels['logs_connector'][ $connector->name ] = $connector->get_label(); |
| 149 |
} |
| 150 |
|
| 151 |
$connector_name = $connector->name; |
| 152 |
$is_excluded = in_array( $connector_name, $excluded_connectors, true ); |
| 153 |
|
| 154 |
/** |
| 155 |
* Allows excluded connectors to be overridden and registered. |
| 156 |
* |
| 157 |
* @param bool $is_excluded True if excluded, otherwise false. |
| 158 |
* @param string $connector The current connector's slug. |
| 159 |
* @param array $excluded_connectors An array of all excluded connector slugs. |
| 160 |
*/ |
| 161 |
$is_excluded_connector = apply_filters( 'mainwp_module_log_check_connector_is_excluded', $is_excluded, $connector_name, $excluded_connectors ); |
| 162 |
|
| 163 |
if ( $is_excluded_connector ) { |
| 164 |
continue; |
| 165 |
} |
| 166 |
|
| 167 |
$connector->register(); |
| 168 |
|
| 169 |
// Link context labels to their connector. |
| 170 |
$this->contexts[ $connector->name ] = $connector->get_context_labels(); |
| 171 |
|
| 172 |
// Add new terms to our label lookup array. |
| 173 |
$this->term_labels['logs_action'] = array_merge( |
| 174 |
$this->term_labels['logs_action'], |
| 175 |
$connector->get_action_labels() |
| 176 |
); |
| 177 |
$this->term_labels['logs_context'] = array_merge( |
| 178 |
$this->term_labels['logs_context'], |
| 179 |
$connector->get_context_labels() |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
$labels = $this->term_labels['logs_connector']; |
| 184 |
|
| 185 |
/** |
| 186 |
* Fires after all connectors have been registered. |
| 187 |
* |
| 188 |
* @param array $labels All register connectors labels array |
| 189 |
* @param Connectors $connectors The Connectors object |
| 190 |
*/ |
| 191 |
do_action( 'mainwp_module_log_after_connectors_registration', $labels, $this ); |
| 192 |
} |
| 193 |
} |
| 194 |
|