| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Compatibility |
| 10 |
*/ |
| 11 |
class Compatibility { |
| 12 |
/** |
| 13 |
* @var array<string, string> |
| 14 |
*/ |
| 15 |
private $components = array(); |
| 16 |
|
| 17 |
/** |
| 18 |
* @var CF7DoubleOptIn $Controller |
| 19 |
*/ |
| 20 |
private $Controller; |
| 21 |
|
| 22 |
/** |
| 23 |
* UI constructor. |
| 24 |
* |
| 25 |
* @param $slug |
| 26 |
*/ |
| 27 |
public function __construct( $Controller ) { |
| 28 |
$this->Controller = $Controller; |
| 29 |
|
| 30 |
$this->get_logger()->debug( 'Loading components for the UI.', [ |
| 31 |
'plugin' => 'double-opt-in', |
| 32 |
] ); |
| 33 |
$this->load_components(); |
| 34 |
|
| 35 |
$this->get_logger()->debug( 'Registering a callback for the after_setup_theme action.', [ |
| 36 |
'plugin' => 'double-opt-in', |
| 37 |
'hook' => 'after_setup_theme', |
| 38 |
] ); |
| 39 |
add_action( 'after_setup_theme', function () { |
| 40 |
$this->get_logger()->debug( 'after_setup_theme hook triggered. Registering components.', [ |
| 41 |
'plugin' => 'double-opt-in', |
| 42 |
] ); |
| 43 |
|
| 44 |
add_action( 'f12_cf7_doubleoptin_ui_after_load_compatibilities', array( |
| 45 |
$this, |
| 46 |
'registerComponents' |
| 47 |
), 10, 1 ); |
| 48 |
|
| 49 |
$this->get_logger()->info( 'Triggering UI after load compatibilities action.', [ |
| 50 |
'plugin' => 'double-opt-in', |
| 51 |
'hook' => 'f12_cf7_doubleoptin_ui_after_load_compatibilities', |
| 52 |
] ); |
| 53 |
do_action( 'f12_cf7_doubleoptin_ui_after_load_compatibilities', $this ); |
| 54 |
|
| 55 |
$this->get_logger()->notice( 'UI initialization actions are complete.', [ |
| 56 |
'plugin' => 'double-opt-in', |
| 57 |
] ); |
| 58 |
} ); |
| 59 |
} |
| 60 |
|
| 61 |
public function get_logger() { |
| 62 |
return $this->Controller->get_logger(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Loads the components for the current application. |
| 67 |
* |
| 68 |
* This method is responsible for loading the components for the current application. |
| 69 |
* It retrieves the directories where the components are located using the `get_component_directories()` |
| 70 |
* method and then iterates through each directory to load the components using the `load()` method. |
| 71 |
* |
| 72 |
* @return void |
| 73 |
* @since 3.0.0 |
| 74 |
* @see get_component_directories() |
| 75 |
* @see load() |
| 76 |
*/ |
| 77 |
private function load_components() { |
| 78 |
$this->get_logger()->info( 'Starting to load UI components from directories.', [ |
| 79 |
'plugin' => 'double-opt-in', |
| 80 |
] ); |
| 81 |
|
| 82 |
$directories = $this->get_component_directories(); |
| 83 |
|
| 84 |
if ( empty( $directories ) ) { |
| 85 |
$this->get_logger()->warning( 'No component directories found to load.', [ |
| 86 |
'plugin' => 'double-opt-in', |
| 87 |
] ); |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$this->get_logger()->debug( 'Found ' . count( $directories ) . ' component directories.', [ |
| 92 |
'plugin' => 'double-opt-in', |
| 93 |
'directories' => $directories, |
| 94 |
] ); |
| 95 |
|
| 96 |
foreach ( $directories as $directory ) { |
| 97 |
$this->get_logger()->info( 'Loading components from directory: ' . $directory, [ |
| 98 |
'plugin' => 'double-opt-in', |
| 99 |
'directory' => $directory, |
| 100 |
] ); |
| 101 |
$this->load( $directory, 0 ); |
| 102 |
} |
| 103 |
|
| 104 |
$this->get_logger()->notice( 'All UI components have been loaded.', [ |
| 105 |
'plugin' => 'double-opt-in', |
| 106 |
] ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Retrieves the component directories. |
| 111 |
* |
| 112 |
* This method allows developers to retrieve an array of component directories. These directories are used for |
| 113 |
* displaying UI pages. |
| 114 |
* |
| 115 |
* @return array An array of component directories. |
| 116 |
* |
| 117 |
* @since 3.0.0 |
| 118 |
*/ |
| 119 |
private function get_component_directories() { |
| 120 |
$this->get_logger()->info( 'Retrieving component directories from filter.', [ |
| 121 |
'plugin' => 'double-opt-in', |
| 122 |
] ); |
| 123 |
|
| 124 |
/** |
| 125 |
* Component Directories |
| 126 |
* |
| 127 |
* This filter allows developers to add custom ui directories which will be used to display ui pages. |
| 128 |
* |
| 129 |
* @param array $directories |
| 130 |
* |
| 131 |
* @since 3.0.0 |
| 132 |
*/ |
| 133 |
$directories = apply_filters( 'f12_cf7_doubleoption_compatbility_get_component_directories', [ dirname( dirname( __FILE__ ) ) . '/compatibility' ] ); |
| 134 |
|
| 135 |
// Log the result of the filter application. |
| 136 |
if ( empty( $directories ) ) { |
| 137 |
$this->get_logger()->warning( 'Component directories filter returned an empty array.', [ |
| 138 |
'plugin' => 'double-opt-in', |
| 139 |
] ); |
| 140 |
} else { |
| 141 |
$this->get_logger()->debug( 'Found ' . count( $directories ) . ' component directories.', [ |
| 142 |
'plugin' => 'double-opt-in', |
| 143 |
'directories' => $directories, |
| 144 |
] ); |
| 145 |
} |
| 146 |
|
| 147 |
return $directories; |
| 148 |
} |
| 149 |
|
| 150 |
public function registerComponents( $Compatibility ) { |
| 151 |
$this->get_logger()->info( 'Starting component registration.', [ |
| 152 |
'plugin' => 'double-opt-in', |
| 153 |
] ); |
| 154 |
|
| 155 |
if ( empty( $this->components ) ) { |
| 156 |
$this->get_logger()->warning( 'No components found to register.', [ |
| 157 |
'plugin' => 'double-opt-in', |
| 158 |
] ); |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
$this->get_logger()->debug( 'Found ' . count($this->components) . ' components to process.', [ |
| 163 |
'plugin' => 'double-opt-in', |
| 164 |
'components' => $this->components, |
| 165 |
] ); |
| 166 |
|
| 167 |
foreach ( $this->components as $component ) { |
| 168 |
if ( isset( $component['name'] ) && isset( $component['path'] ) ) { |
| 169 |
$this->get_logger()->info( 'Registering component: ' . $component['name'], [ |
| 170 |
'plugin' => 'double-opt-in', |
| 171 |
'component_name' => $component['name'], |
| 172 |
'component_path' => $component['path'], |
| 173 |
] ); |
| 174 |
|
| 175 |
require_once( $component['path'] ); |
| 176 |
|
| 177 |
if ( class_exists($component['name']) ) { |
| 178 |
new $component['name']( $this->Controller ); |
| 179 |
$this->get_logger()->notice( 'Component ' . $component['name'] . ' successfully registered.', [ |
| 180 |
'plugin' => 'double-opt-in', |
| 181 |
] ); |
| 182 |
} else { |
| 183 |
$this->get_logger()->error( 'Failed to register component. Class ' . $component['name'] . ' not found after requiring file.', [ |
| 184 |
'plugin' => 'double-opt-in', |
| 185 |
'component_path' => $component['path'], |
| 186 |
] ); |
| 187 |
} |
| 188 |
} else { |
| 189 |
$this->get_logger()->warning( 'Skipped a component due to missing "name" or "path" keys.', [ |
| 190 |
'plugin' => 'double-opt-in', |
| 191 |
'component_data' => $component, |
| 192 |
] ); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
$this->get_logger()->info( 'All available components have been processed.', [ |
| 197 |
'plugin' => 'double-opt-in', |
| 198 |
] ); |
| 199 |
} |
| 200 |
|
| 201 |
private function addComponent( $name, $path ) { |
| 202 |
$this->get_logger()->info( 'Adding new component to the list.', [ |
| 203 |
'plugin' => 'double-opt-in', |
| 204 |
'component_name' => $name, |
| 205 |
'component_path' => $path, |
| 206 |
] ); |
| 207 |
|
| 208 |
$this->components[] = array( |
| 209 |
'name' => $name, |
| 210 |
'path' => $path |
| 211 |
); |
| 212 |
|
| 213 |
$this->get_logger()->debug( 'Component "' . $name . '" successfully added.', [ |
| 214 |
'plugin' => 'double-opt-in', |
| 215 |
'total_components' => count($this->components), |
| 216 |
] ); |
| 217 |
} |
| 218 |
|
| 219 |
private function load( $directory, $lvl ) { |
| 220 |
$this->get_logger()->info( 'Starting component loading process from directory.', [ |
| 221 |
'plugin' => 'double-opt-in', |
| 222 |
'directory' => $directory, |
| 223 |
'level' => $lvl, |
| 224 |
] ); |
| 225 |
|
| 226 |
if ( ! is_dir( $directory ) ) { |
| 227 |
$this->get_logger()->warning( 'Specified directory does not exist or is not a directory.', [ |
| 228 |
'plugin' => 'double-opt-in', |
| 229 |
'directory' => $directory, |
| 230 |
] ); |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
$handle = opendir( $directory ); |
| 235 |
|
| 236 |
if ( ! $handle ) { |
| 237 |
$this->get_logger()->error( 'Failed to open directory handle.', [ |
| 238 |
'plugin' => 'double-opt-in', |
| 239 |
'directory' => $directory, |
| 240 |
] ); |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
while ( false !== ( $entry = readdir( $handle ) ) ) { |
| 245 |
if ( $entry != '.' && $entry != '..' ) { |
| 246 |
$full_path = $directory . '/' . $entry; |
| 247 |
|
| 248 |
if ( is_dir( $full_path ) && $lvl == 0 ) { |
| 249 |
$this->get_logger()->debug( 'Found a subdirectory. Recursively loading.', [ |
| 250 |
'plugin' => 'double-opt-in', |
| 251 |
'subdirectory' => $entry, |
| 252 |
'current_level' => $lvl, |
| 253 |
] ); |
| 254 |
$this->load( $full_path, $lvl + 1 ); |
| 255 |
} else { |
| 256 |
if ( preg_match( '!Controller([a-zA-Z_0-9]+)\.class\.php!', $entry, $matches ) ) { |
| 257 |
$this->get_logger()->info( 'Found a potential controller file.', [ |
| 258 |
'plugin' => 'double-opt-in', |
| 259 |
'filename' => $entry, |
| 260 |
] ); |
| 261 |
|
| 262 |
if ( isset( $matches[1] ) ) { |
| 263 |
$component_name = '\\' . __NAMESPACE__ . '\Controller' . $matches[1]; |
| 264 |
$this->get_logger()->debug( 'Adding component to the list.', [ |
| 265 |
'plugin' => 'double-opt-in', |
| 266 |
'component_name' => $component_name, |
| 267 |
'file_path' => $full_path, |
| 268 |
] ); |
| 269 |
$this->addComponent( $component_name, $full_path ); |
| 270 |
} else { |
| 271 |
$this->get_logger()->warning( 'Controller filename matched, but could not extract component name.', [ |
| 272 |
'plugin' => 'double-opt-in', |
| 273 |
'filename' => $entry, |
| 274 |
] ); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
closedir( $handle ); |
| 282 |
|
| 283 |
$this->get_logger()->notice( 'Finished loading components from directory: ' . $directory, [ |
| 284 |
'plugin' => 'double-opt-in', |
| 285 |
] ); |
| 286 |
} |
| 287 |
} |
| 288 |
} |