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