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