|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Integrations management class |
| 4 | + * |
| 5 | + * @package Newspack |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Newspack\Reader_Activation; |
| 9 | + |
| 10 | +defined( 'ABSPATH' ) || exit; |
| 11 | + |
| 12 | +/** |
| 13 | + * Integrations Management Class. |
| 14 | + * |
| 15 | + * Manages registration, enabling/disabling, and retrieval of integrations. |
| 16 | + */ |
| 17 | +class Integrations { |
| 18 | + /** |
| 19 | + * Registered integrations. |
| 20 | + * |
| 21 | + * @var Integration[] |
| 22 | + */ |
| 23 | + private static $integrations = []; |
| 24 | + |
| 25 | + /** |
| 26 | + * Option name for storing enabled integrations. |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + const OPTION_NAME = 'newspack_reader_activation_enabled_integrations'; |
| 31 | + |
| 32 | + /** |
| 33 | + * Initialize integrations system. |
| 34 | + */ |
| 35 | + public static function init() { |
| 36 | + // Include required files. |
| 37 | + require_once __DIR__ . '/integrations/class-integration.php'; |
| 38 | + |
| 39 | + self::register_native_integrations(); |
| 40 | + |
| 41 | + // Hook for other plugins/code to register their integrations. |
| 42 | + do_action( 'newspack_reader_activation_register_integrations' ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Register native integrations. |
| 47 | + */ |
| 48 | + private static function register_native_integrations() { |
| 49 | + self::register( new Integrations\ESP() ); |
| 50 | + |
| 51 | + // hardcode ESP integration as enabled for now. |
| 52 | + self::enable( 'esp' ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Register a new integration. |
| 57 | + * |
| 58 | + * @param Integration $integration The integration instance to register. |
| 59 | + * |
| 60 | + * @return bool True if registered successfully, false if already registered. |
| 61 | + */ |
| 62 | + public static function register( $integration ) { |
| 63 | + if ( ! $integration instanceof Integration ) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + $id = $integration->get_id(); |
| 68 | + |
| 69 | + if ( isset( self::$integrations[ $id ] ) ) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + self::$integrations[ $id ] = $integration; |
| 74 | + |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Enable an integration. |
| 80 | + * |
| 81 | + * @param string $integration_id The integration ID to enable. |
| 82 | + * |
| 83 | + * @return bool True if enabled successfully, false otherwise. |
| 84 | + */ |
| 85 | + public static function enable( $integration_id ) { |
| 86 | + if ( ! isset( self::$integrations[ $integration_id ] ) ) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + $enabled = self::get_enabled_integration_ids(); |
| 91 | + |
| 92 | + if ( in_array( $integration_id, $enabled, true ) ) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + $enabled[] = $integration_id; |
| 97 | + |
| 98 | + return update_option( self::OPTION_NAME, $enabled ); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Disable an integration. |
| 103 | + * |
| 104 | + * @param string $integration_id The integration ID to disable. |
| 105 | + * |
| 106 | + * @return bool True if disabled successfully, false otherwise. |
| 107 | + */ |
| 108 | + public static function disable( $integration_id ) { |
| 109 | + $enabled = self::get_enabled_integration_ids(); |
| 110 | + |
| 111 | + $key = array_search( $integration_id, $enabled, true ); |
| 112 | + |
| 113 | + if ( false === $key ) { |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + unset( $enabled[ $key ] ); |
| 118 | + |
| 119 | + return update_option( self::OPTION_NAME, array_values( $enabled ) ); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Get all available integrations. |
| 124 | + * |
| 125 | + * @return Integration[] Array of all registered integration instances. |
| 126 | + */ |
| 127 | + public static function get_available_integrations() { |
| 128 | + return self::$integrations; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Get active integrations. |
| 133 | + * |
| 134 | + * @return Integration[] Array of enabled integration instances. |
| 135 | + */ |
| 136 | + public static function get_active_integrations() { |
| 137 | + $enabled_ids = self::get_enabled_integration_ids(); |
| 138 | + $active = []; |
| 139 | + |
| 140 | + foreach ( $enabled_ids as $id ) { |
| 141 | + if ( isset( self::$integrations[ $id ] ) ) { |
| 142 | + $active[ $id ] = self::$integrations[ $id ]; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return $active; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Get a specific integration by ID. |
| 151 | + * |
| 152 | + * @param string $integration_id The integration ID. |
| 153 | + * |
| 154 | + * @return Integration|null The integration instance or null if not found. |
| 155 | + */ |
| 156 | + public static function get_integration( $integration_id ) { |
| 157 | + return self::$integrations[ $integration_id ] ?? null; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Check if an integration is enabled. |
| 162 | + * |
| 163 | + * @param string $integration_id The integration ID. |
| 164 | + * |
| 165 | + * @return bool True if enabled, false otherwise. |
| 166 | + */ |
| 167 | + public static function is_enabled( $integration_id ) { |
| 168 | + $enabled_ids = self::get_enabled_integration_ids(); |
| 169 | + return in_array( $integration_id, $enabled_ids, true ); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Get enabled integration IDs from option. |
| 174 | + * |
| 175 | + * @return array Array of enabled integration IDs. |
| 176 | + */ |
| 177 | + private static function get_enabled_integration_ids() { |
| 178 | + $enabled = get_option( self::OPTION_NAME, [] ); |
| 179 | + |
| 180 | + if ( ! is_array( $enabled ) ) { |
| 181 | + return []; |
| 182 | + } |
| 183 | + |
| 184 | + return $enabled; |
| 185 | + } |
| 186 | +} |
0 commit comments