| 1 |
<?php |
| 2 |
/** |
| 3 |
* Integrations: Integrations collection class |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 2.6.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\Integrations; |
| 12 |
|
| 13 |
use Parsely\Parsely; |
| 14 |
|
| 15 |
/** |
| 16 |
* Integrations are registered to this collection. |
| 17 |
* |
| 18 |
* The `integrate()` method is called on each registered integration, on the |
| 19 |
* init hook. |
| 20 |
* |
| 21 |
* @since 2.6.0 |
| 22 |
*/ |
| 23 |
class Integrations { |
| 24 |
/** |
| 25 |
* Instance of Parsely class. |
| 26 |
* |
| 27 |
* @var Parsely |
| 28 |
*/ |
| 29 |
private $parsely; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
* |
| 34 |
* @param Parsely $parsely Instance of Parsely class. |
| 35 |
*/ |
| 36 |
public function __construct( Parsely $parsely ) { |
| 37 |
$this->parsely = $parsely; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Collection of registered integrations. |
| 42 |
* |
| 43 |
* @var array |
| 44 |
*/ |
| 45 |
private $integrations = array(); |
| 46 |
|
| 47 |
/** |
| 48 |
* Registers an integration. |
| 49 |
* |
| 50 |
* @since 2.6.0 |
| 51 |
* |
| 52 |
* @param string $key A unique identifier for the integration. |
| 53 |
* @param string|object $class_or_object Fully-qualified class name, or an instantiated object. |
| 54 |
* If a class name is passed, it will be instantiated. |
| 55 |
*/ |
| 56 |
public function register( string $key, $class_or_object ): void { |
| 57 |
// If a Foo::class or other fully qualified class name is passed, instantiate it. |
| 58 |
if ( ! is_object( $class_or_object ) ) { |
| 59 |
$class_or_object = new $class_or_object( $this->parsely ); |
| 60 |
} |
| 61 |
$this->integrations[ $key ] = $class_or_object; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Integrates each integration by calling the method that does the |
| 66 |
* add_action() and add_filter() calls. |
| 67 |
* |
| 68 |
* @since 2.6.0 |
| 69 |
*/ |
| 70 |
public function integrate(): void { |
| 71 |
foreach ( $this->integrations as $integration ) { |
| 72 |
$integration->integrate(); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|