| 1 |
<?php |
| 2 |
|
| 3 |
class WP_Stream_Connectors { |
| 4 |
|
| 5 |
/** |
| 6 |
* Connectors registered |
| 7 |
* |
| 8 |
* @var array |
| 9 |
*/ |
| 10 |
public static $connectors = array(); |
| 11 |
|
| 12 |
/** |
| 13 |
* Contexts registered to Connectors |
| 14 |
* |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
public static $contexts = array(); |
| 18 |
|
| 19 |
/** |
| 20 |
* Action taxonomy terms |
| 21 |
* Holds slug to localized label association |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
public static $term_labels = array( |
| 26 |
'stream_connector' => array(), |
| 27 |
'stream_context' => array(), |
| 28 |
'stream_action' => array(), |
| 29 |
); |
| 30 |
|
| 31 |
/** |
| 32 |
* Admin notice messages |
| 33 |
* |
| 34 |
* @since 1.2.3 |
| 35 |
* @var array |
| 36 |
*/ |
| 37 |
protected static $admin_notices = array(); |
| 38 |
|
| 39 |
/** |
| 40 |
* Load built-in connectors |
| 41 |
*/ |
| 42 |
public static function load() { |
| 43 |
add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) ); |
| 44 |
|
| 45 |
$connectors = array( |
| 46 |
// Core |
| 47 |
'comments', |
| 48 |
'editor', |
| 49 |
'installer', |
| 50 |
'media', |
| 51 |
'menus', |
| 52 |
'posts', |
| 53 |
'settings', |
| 54 |
'taxonomies', |
| 55 |
'users', |
| 56 |
'widgets', |
| 57 |
|
| 58 |
// Extras |
| 59 |
'acf', |
| 60 |
'bbpress', |
| 61 |
'buddypress', |
| 62 |
'edd', |
| 63 |
'gravityforms', |
| 64 |
'jetpack', |
| 65 |
'stream', |
| 66 |
'woocommerce', |
| 67 |
'wordpress-seo', |
| 68 |
); |
| 69 |
|
| 70 |
$classes = array(); |
| 71 |
foreach ( $connectors as $connector ) { |
| 72 |
include_once WP_STREAM_DIR . '/connectors/class-wp-stream-connector-' . $connector .'.php'; |
| 73 |
$class = sprintf( 'WP_Stream_Connector_%s', str_replace( '-', '_', $connector ) ); |
| 74 |
if ( $class::is_dependency_satisfied() ) { |
| 75 |
$classes[] = $class; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Allows for adding additional connectors via classes that extend WP_Stream_Connector. |
| 81 |
* |
| 82 |
* @since 0.0.2 |
| 83 |
* |
| 84 |
* @param array $classes An array of connector class names. |
| 85 |
*/ |
| 86 |
self::$connectors = apply_filters( 'wp_stream_connectors', $classes ); |
| 87 |
|
| 88 |
foreach ( self::$connectors as $connector ) { |
| 89 |
self::$term_labels['stream_connector'][ $connector::$name ] = $connector::get_label(); |
| 90 |
} |
| 91 |
|
| 92 |
// Get excluded connectors |
| 93 |
$excluded_connectors = array(); |
| 94 |
|
| 95 |
foreach ( self::$connectors as $connector ) { |
| 96 |
// Check if the connectors extends the WP_Stream_Connector class, if not skip it |
| 97 |
if ( ! is_subclass_of( $connector, 'WP_Stream_Connector' ) ) { |
| 98 |
self::$admin_notices[] = sprintf( |
| 99 |
__( "%s class wasn't loaded because it doesn't extends the %s class.", 'stream' ), |
| 100 |
$connector, |
| 101 |
'WP_Stream_Connector' |
| 102 |
); |
| 103 |
|
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
// Store connector label |
| 108 |
if ( ! in_array( $connector::$name, self::$term_labels['stream_connector'] ) ) { |
| 109 |
self::$term_labels['stream_connector'][ $connector::$name ] = $connector::get_label(); |
| 110 |
} |
| 111 |
|
| 112 |
$connector_name = $connector::$name; |
| 113 |
$is_excluded = in_array( $connector_name, $excluded_connectors ); |
| 114 |
|
| 115 |
/** |
| 116 |
* Allows excluded connectors to be overridden and registered. |
| 117 |
* |
| 118 |
* @since 1.3.0 |
| 119 |
* |
| 120 |
* @param bool $is_excluded True if excluded, otherwise false. |
| 121 |
* @param string $connector The current connector's slug. |
| 122 |
* @param array $excluded_connectors An array of all excluded connector slugs. |
| 123 |
*/ |
| 124 |
$is_excluded_connector = apply_filters( 'wp_stream_check_connector_is_excluded', $is_excluded, $connector_name, $excluded_connectors ); |
| 125 |
|
| 126 |
if ( $is_excluded_connector ) { |
| 127 |
continue; |
| 128 |
} |
| 129 |
|
| 130 |
$connector::register(); |
| 131 |
|
| 132 |
// Link context labels to their connector |
| 133 |
self::$contexts[ $connector::$name ] = $connector::get_context_labels(); |
| 134 |
|
| 135 |
// Add new terms to our label lookup array |
| 136 |
self::$term_labels['stream_action'] = array_merge( |
| 137 |
self::$term_labels['stream_action'], |
| 138 |
$connector::get_action_labels() |
| 139 |
); |
| 140 |
self::$term_labels['stream_context'] = array_merge( |
| 141 |
self::$term_labels['stream_context'], |
| 142 |
$connector::get_context_labels() |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
$connectors = self::$term_labels['stream_connector']; |
| 147 |
|
| 148 |
/** |
| 149 |
* Fires after all connectors have been registered. |
| 150 |
* |
| 151 |
* @since 1.3.0 |
| 152 |
* |
| 153 |
* @param array all register connectors labels array |
| 154 |
*/ |
| 155 |
do_action( 'wp_stream_after_connectors_registration', $connectors ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Print admin notices |
| 160 |
* |
| 161 |
* @since 1.2.3 |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
public static function admin_notices() { |
| 166 |
if ( ! empty( self::$admin_notices ) ) : |
| 167 |
?> |
| 168 |
<div class="error"> |
| 169 |
<?php foreach ( self::$admin_notices as $message ) : ?> |
| 170 |
<?php echo wpautop( esc_html( $message ) ) // xss ok ?> |
| 171 |
<?php endforeach; ?> |
| 172 |
</div> |
| 173 |
<?php |
| 174 |
endif; |
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
|