| 1 |
<?php |
| 2 |
/** |
| 3 |
* Validates and loads core connectors, integrated connectors, and |
| 4 |
* connectors registered using the "wp_stream_connectors" hook. |
| 5 |
* |
| 6 |
* @package WP_Stream |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WP_Stream; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class - Connectors |
| 13 |
*/ |
| 14 |
class Connectors { |
| 15 |
/** |
| 16 |
* Holds instance of plugin object |
| 17 |
* |
| 18 |
* @var Plugin |
| 19 |
*/ |
| 20 |
public $plugin; |
| 21 |
|
| 22 |
/** |
| 23 |
* Registered connectors. |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
public $connectors = array(); |
| 28 |
|
| 29 |
/** |
| 30 |
* Contexts registered to Connectors |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
public $contexts = array(); |
| 35 |
|
| 36 |
/** |
| 37 |
* Action taxonomy terms |
| 38 |
* |
| 39 |
* Holds slug to localized label association |
| 40 |
* |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
public $term_labels = array( |
| 44 |
'stream_connector' => array(), |
| 45 |
'stream_context' => array(), |
| 46 |
'stream_action' => array(), |
| 47 |
); |
| 48 |
|
| 49 |
/** |
| 50 |
* Admin notice messages |
| 51 |
* |
| 52 |
* @var array |
| 53 |
*/ |
| 54 |
protected $admin_notices = array(); |
| 55 |
|
| 56 |
/** |
| 57 |
* Class constructor. |
| 58 |
* |
| 59 |
* @param Plugin $plugin Instance of plugin object. |
| 60 |
*/ |
| 61 |
public function __construct( $plugin ) { |
| 62 |
$this->plugin = $plugin; |
| 63 |
$this->load_connectors(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Load built-in connectors |
| 68 |
*/ |
| 69 |
public function load_connectors() { |
| 70 |
$connectors = array( |
| 71 |
/** |
| 72 |
* Core Connectors |
| 73 |
*/ |
| 74 |
'blogs', |
| 75 |
'comments', |
| 76 |
'editor', |
| 77 |
'installer', |
| 78 |
'media', |
| 79 |
'menus', |
| 80 |
'posts', |
| 81 |
'settings', |
| 82 |
'taxonomies', |
| 83 |
'users', |
| 84 |
'widgets', |
| 85 |
|
| 86 |
/** |
| 87 |
* Integrated Connectors |
| 88 |
*/ |
| 89 |
'acf', |
| 90 |
'bbpress', |
| 91 |
'buddypress', |
| 92 |
'edd', |
| 93 |
'gravityforms', |
| 94 |
'jetpack', |
| 95 |
'mercator', |
| 96 |
'user-switching', |
| 97 |
'woocommerce', |
| 98 |
'wordpress-seo', |
| 99 |
); |
| 100 |
|
| 101 |
$classes = array(); |
| 102 |
foreach ( $connectors as $connector ) { |
| 103 |
// Load connector class file. |
| 104 |
include_once $this->plugin->locations['dir'] . '/connectors/class-connector-' . $connector . '.php'; |
| 105 |
|
| 106 |
// Set fully qualified class name. |
| 107 |
$class_name = sprintf( '\WP_Stream\Connector_%s', str_replace( '-', '_', $connector ) ); |
| 108 |
|
| 109 |
// Bail if no class loaded. |
| 110 |
if ( ! class_exists( $class_name ) ) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
// Initialize connector. |
| 115 |
$class = new $class_name( $this->plugin->log ); |
| 116 |
|
| 117 |
// Check if the connector class extends WP_Stream\Connector. |
| 118 |
if ( ! is_subclass_of( $class, 'WP_Stream\Connector' ) ) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
// Check if the connector events are allowed to be registered in the WP Admin. |
| 123 |
if ( is_admin() && ! $class->register_admin ) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
// Check if the connector events are allowed to be registered in the WP Frontend. |
| 128 |
if ( ! is_admin() && ! $class->register_frontend ) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
// Run any final validations the connector may have before used. |
| 133 |
if ( $class->is_dependency_satisfied() ) { |
| 134 |
$classes[ $class->name ] = $class; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Allows for adding additional connectors via classes that extend Connector. |
| 140 |
* |
| 141 |
* @param array $classes An array of Connector objects. |
| 142 |
*/ |
| 143 |
$this->connectors = apply_filters( 'wp_stream_connectors', $classes ); |
| 144 |
|
| 145 |
if ( empty( $this->connectors ) ) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
foreach ( $this->connectors as $connector ) { |
| 150 |
if ( ! method_exists( $connector, 'get_label' ) ) { |
| 151 |
continue; |
| 152 |
} |
| 153 |
$this->term_labels['stream_connector'][ $connector->name ] = $connector->get_label(); |
| 154 |
} |
| 155 |
|
| 156 |
// Get excluded connectors. |
| 157 |
$excluded_connectors = array(); |
| 158 |
|
| 159 |
foreach ( $this->connectors as $connector ) { |
| 160 |
|
| 161 |
// Register error for invalid any connector class. |
| 162 |
if ( ! method_exists( $connector, 'get_label' ) ) { |
| 163 |
/* translators: %s: connector class name, intended to provide help to developers (e.g. "Connector_BuddyPress") */ |
| 164 |
$this->plugin->admin->notice( sprintf( __( '%s class wasn\'t loaded because it doesn\'t implement the get_label method.', 'stream' ), $connector->name, 'Connector' ), true ); |
| 165 |
continue; |
| 166 |
} |
| 167 |
if ( ! method_exists( $connector, 'register' ) ) { |
| 168 |
/* translators: %s: connector class name, intended to provide help to developers (e.g. "Connector_BuddyPress") */ |
| 169 |
$this->plugin->admin->notice( sprintf( __( '%s class wasn\'t loaded because it doesn\'t implement the register method.', 'stream' ), $connector->name, 'Connector' ), true ); |
| 170 |
continue; |
| 171 |
} |
| 172 |
if ( ! method_exists( $connector, 'get_context_labels' ) ) { |
| 173 |
/* translators: %s: connector class name, intended to provide help to developers (e.g. "Connector_BuddyPress") */ |
| 174 |
$this->plugin->admin->notice( sprintf( __( '%s class wasn\'t loaded because it doesn\'t implement the get_context_labels method.', 'stream' ), $connector->name, 'Connector' ), true ); |
| 175 |
continue; |
| 176 |
} |
| 177 |
if ( ! method_exists( $connector, 'get_action_labels' ) ) { |
| 178 |
/* translators: %s: connector class name, intended to provide help to developers (e.g. "Connector_BuddyPress") */ |
| 179 |
$this->plugin->admin->notice( sprintf( __( '%s class wasn\'t loaded because it doesn\'t implement the get_action_labels method.', 'stream' ), $connector->name, 'Connector' ), true ); |
| 180 |
continue; |
| 181 |
} |
| 182 |
|
| 183 |
// Check if the connectors extends the Connector class, if not skip it. |
| 184 |
if ( ! is_subclass_of( $connector, '\WP_Stream\Connector' ) ) { |
| 185 |
/* translators: %s: connector class name, intended to provide help to developers (e.g. "Connector_BuddyPress") */ |
| 186 |
$this->plugin->admin->notice( sprintf( __( '%1$s class wasn\'t loaded because it doesn\'t extends the %2$s class.', 'stream' ), $connector->name, 'Connector' ), true ); |
| 187 |
continue; |
| 188 |
} |
| 189 |
|
| 190 |
// Store connector label. |
| 191 |
if ( ! in_array( $connector->name, $this->term_labels['stream_connector'], true ) ) { |
| 192 |
$this->term_labels['stream_connector'][ $connector->name ] = $connector->get_label(); |
| 193 |
} |
| 194 |
|
| 195 |
$connector_name = $connector->name; |
| 196 |
$is_excluded = in_array( $connector_name, $excluded_connectors, true ); |
| 197 |
|
| 198 |
/** |
| 199 |
* Allows excluded connectors to be overridden and registered. |
| 200 |
* |
| 201 |
* @param bool $is_excluded True if excluded, otherwise false. |
| 202 |
* @param string $connector The current connector's slug. |
| 203 |
* @param array $excluded_connectors An array of all excluded connector slugs. |
| 204 |
*/ |
| 205 |
$is_excluded_connector = apply_filters( 'wp_stream_check_connector_is_excluded', $is_excluded, $connector_name, $excluded_connectors ); |
| 206 |
|
| 207 |
if ( $is_excluded_connector ) { |
| 208 |
continue; |
| 209 |
} |
| 210 |
|
| 211 |
$connector->register(); |
| 212 |
|
| 213 |
// Link context labels to their connector. |
| 214 |
$this->contexts[ $connector->name ] = $connector->get_context_labels(); |
| 215 |
|
| 216 |
// Add new terms to our label lookup array. |
| 217 |
$this->term_labels['stream_action'] = array_merge( |
| 218 |
$this->term_labels['stream_action'], |
| 219 |
$connector->get_action_labels() |
| 220 |
); |
| 221 |
$this->term_labels['stream_context'] = array_merge( |
| 222 |
$this->term_labels['stream_context'], |
| 223 |
$connector->get_context_labels() |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
$labels = $this->term_labels['stream_connector']; |
| 228 |
|
| 229 |
/** |
| 230 |
* Fires after all connectors have been registered. |
| 231 |
* |
| 232 |
* @param array $labels All register connectors labels array |
| 233 |
* @param Connectors $connectors The Connectors object |
| 234 |
*/ |
| 235 |
do_action( 'wp_stream_after_connectors_registration', $labels, $this ); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Unregisters the context hooks for all connectors. |
| 240 |
*/ |
| 241 |
public function unload_connectors() { |
| 242 |
foreach ( $this->connectors as $connector ) { |
| 243 |
$connector->unregister(); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Reregisters the context hooks for all connectors. |
| 249 |
*/ |
| 250 |
public function reload_connectors() { |
| 251 |
foreach ( $this->connectors as $connector ) { |
| 252 |
$connector->register(); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Unregisters the context hooks for a connectors. |
| 258 |
* |
| 259 |
* @param string $name Name of the connector. |
| 260 |
*/ |
| 261 |
public function unload_connector( $name ) { |
| 262 |
if ( ! empty( $this->connectors[ $name ] ) ) { |
| 263 |
$this->connectors[ $name ]->unregister(); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Reregisters the context hooks for a connector. |
| 269 |
* |
| 270 |
* @param string $name Name of the connector. |
| 271 |
*/ |
| 272 |
public function reload_connector( $name ) { |
| 273 |
if ( ! empty( $this->connectors[ $name ] ) ) { |
| 274 |
$this->connectors[ $name ]->register(); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|