| 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 |
$this->load_components(); |
| 30 |
|
| 31 |
add_action( 'after_setup_theme', function () { |
| 32 |
add_action( 'f12_cf7_doubleoptin_ui_after_load_compatibilities', array( |
| 33 |
$this, |
| 34 |
'registerComponents' |
| 35 |
), 10, 1 ); |
| 36 |
do_action( 'f12_cf7_doubleoptin_ui_after_load_compatibilities', $this ); |
| 37 |
} ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Loads the components for the current application. |
| 42 |
* |
| 43 |
* This method is responsible for loading the components for the current application. |
| 44 |
* It retrieves the directories where the components are located using the `get_component_directories()` |
| 45 |
* method and then iterates through each directory to load the components using the `load()` method. |
| 46 |
* |
| 47 |
* @return void |
| 48 |
* @since 3.0.0 |
| 49 |
* @see get_component_directories() |
| 50 |
* @see load() |
| 51 |
*/ |
| 52 |
private function load_components() { |
| 53 |
$directories = $this->get_component_directories(); |
| 54 |
|
| 55 |
foreach ( $directories as $directory ) { |
| 56 |
$this->load( $directory, 0 ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Retrieves the component directories. |
| 62 |
* |
| 63 |
* This method allows developers to retrieve an array of component directories. These directories are used for |
| 64 |
* displaying UI pages. |
| 65 |
* |
| 66 |
* @return array An array of component directories. |
| 67 |
* |
| 68 |
* @since 3.0.0 |
| 69 |
*/ |
| 70 |
private function get_component_directories() { |
| 71 |
/** |
| 72 |
* Component Directories |
| 73 |
* |
| 74 |
* This filter allows developers to add custom ui directories which will be used to display ui pages. |
| 75 |
* |
| 76 |
* @param array $directories |
| 77 |
* |
| 78 |
* @since 3.0.0 |
| 79 |
*/ |
| 80 |
return apply_filters( 'f12_cf7_doubleoption_compatbility_get_component_directories', [ dirname( dirname( __FILE__ ) ) . '/compatibility' ] ); |
| 81 |
} |
| 82 |
|
| 83 |
public function registerComponents( $Compatibility ) { |
| 84 |
foreach ( $this->components as $component ) { |
| 85 |
if ( isset( $component['name'] ) && isset( $component['path'] ) ) { |
| 86 |
require_once( $component['path'] ); |
| 87 |
new $component['name']( $this->Controller ); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
private function addComponent( $name, $path ) { |
| 93 |
$this->components[] = array( |
| 94 |
'name' => $name, |
| 95 |
'path' => $path |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
private function load( $directory, $lvl ) { |
| 100 |
if ( is_dir( $directory ) ) { |
| 101 |
$handle = opendir( $directory ); |
| 102 |
|
| 103 |
if ( ! $handle ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
while ( false !== ( $entry = readdir( $handle ) ) ) { |
| 108 |
if ( $entry != '.' && $entry != '..' ) { |
| 109 |
if ( is_dir( $directory . '/' . $entry ) && $lvl == 0 ) { |
| 110 |
$this->load( $directory . '/' . $entry, $lvl + 1 ); |
| 111 |
} else { |
| 112 |
if ( preg_match( '!Controller([a-zA-Z_0-9]+)\.class\.php!', $entry, $matches ) ) { |
| 113 |
if ( isset( $matches[1] ) ) { |
| 114 |
$this->addComponent( '\\' . __NAMESPACE__ . '\Controller' . $matches[1], $directory . '/' . $entry ); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |