PluginProbe
Media Cloud Sync / 1.4.1
Media Cloud Sync v1.4.1
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
← All changes | includes/integrations/integrations.php +111 -24 1.2.121.4.1 View file →
@@ -4,11 +4,8 @@
4 4 defined('ABSPATH') || exit;
5 5
6 6 class Integration {
7 7 private static $instance = null;
8 - private $assets_url;
9 - private $version;
10 - private $token;
11 8
12 9 private $integrations = [];
13 10 public static $source_types = [];
14 11 public static $source_labels = [];
@@ -17,15 +14,13 @@
17 14 * Admin constructor.
18 15 * @since 1.0.0
19 16 */
20 17 public function __construct() {
21 - $this->assets_url = WPMCS_ASSETS_URL;
22 - $this->version = WPMCS_VERSION;
23 - $this->token = WPMCS_TOKEN;
24 -
25 18 $this->integrations = [
26 19 'MediaLibrary',
27 - 'Acf'
20 + 'Acf',
21 + 'Imagify',
22 + 'WooCommerce',
28 23 ];
29 24
30 25 // Initialize setup
31 26 $this->init();
@@ -44,26 +39,68 @@
44 39 public function init() {
45 40 // Init all integrations
46 41 if (!empty($this->integrations)) {
47 42 foreach ($this->integrations as $integration) {
48 - $integration = __NAMESPACE__ . '\\' . $integration;
49 - if (class_exists($integration) && $integration::is_installed()) {
50 - $integration::instance();
51 - // Add Source types from all integrations
52 - if (property_exists($integration, 'source_types')) {
53 - self::$source_types = array_merge(self::$source_types, $integration::$source_types);
54 - }
55 - // Add prefix and label from all integrations
56 - if (property_exists($integration, 'source_type_prefix') && property_exists($integration, 'label')) {
57 - self::$source_labels[$integration::$source_type_prefix] = $integration::$label;
58 - }
59 - }
43 + $this->process_integration($integration);
60 44 }
61 45 }
62 46 }
63 47
48 + /**
49 + * Processes a single integration: registers its source types/labels and
50 + * instantiates it if installed and the service is enabled.
51 + *
52 + * @param string $integration Unqualified integration class name.
53 + * @since 1.4.1
54 + */
55 + private function process_integration($integration) {
56 + $integration = __NAMESPACE__ . '\\' . $integration;
57 + if (class_exists($integration) && $integration::is_installed()) {
58 + if (property_exists($integration, 'source_types')) {
59 + self::$source_types = array_merge(self::$source_types, $integration::$source_types);
60 + }
61 + // get_label() (not a $label property) because a static property default can't
62 + // call __() — PHP only allows constant expressions there.
63 + if (property_exists($integration, 'source_type_prefix') && method_exists($integration, 'get_label')) {
64 + self::$source_labels[$integration::$source_type_prefix] = $integration::get_label();
65 + }
66 + if (Utils::is_service_enabled()) {
67 + $integration::instance();
68 + }
69 + }
70 + }
64 71
65 72 /**
73 + * Registers additional integration classes after construction, mirroring
74 + * Sync::add_sync_classes() — lets Pro register a new integration whenever,
75 + * regardless of load order relative to this class's own construction.
76 + *
77 + * @param array $classes Unqualified integration class names to add.
78 + * @since 1.4.1
79 + */
80 + public function add_integrations($classes = []) {
81 + $new_classes = array_diff($classes, $this->integrations);
82 + $this->integrations = array_merge($this->integrations, $new_classes);
83 + foreach ($new_classes as $integration) {
84 + $this->process_integration($integration);
85 + }
86 + }
87 +
88 + /**
89 + * Gets the handler class associated with a given source type.
90 + *
91 + * @param string $source_type The source type to look up.
92 + *
93 + * @return string|null The handler class associated with the source type, or null if not found.
94 + */
95 + public static function get_handler_class($source_type = 'media_library') {
96 + return class_exists(__NAMESPACE__ . '\\' . self::$source_types[$source_type]['class'])
97 + ? __NAMESPACE__ . '\\' . self::$source_types[$source_type]['class']
98 + : null;
99 + }
100 +
101 +
102 + /**
66 103 * Get Item Meta According to Integration
67 104 * @param int $post_id
68 105 * @param string $key
69 106 * @param mixed $default
@@ -79,15 +116,19 @@
79 116 $source_type_data = self::$source_types[$source_type];
80 117 $meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key;
81 118 $table = isset($source_type_data['table']) ? $source_type_data['table'] : false;
82 119
120 + if (is_array($table)) {
121 + return Utils::get_custom_meta($post_id, $meta_key, $default, $meta_name, $expire, $table);
122 + }
123 +
83 124 // Update it - handle in seperate integration by calling common function
84 125 switch($table) {
85 - case 'posts':
126 + case 'posts':
86 127 return Utils::get_meta($post_id, $meta_key, $default, $meta_name, $expire);
87 128 case 'users':
88 129 return Utils::get_user_meta($post_id, $meta_key, $default, $meta_name, $expire);
89 - default: null;
130 + default: return $default;
90 131 }
91 132 }
92 133 }
93 134
@@ -108,10 +149,14 @@
108 149 $source_type_data = self::$source_types[$source_type];
109 150 $meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key;
110 151 $table = isset($source_type_data['table']) ? $source_type_data['table'] : false;
111 152
153 + if (is_array($table)) {
154 + return Utils::update_custom_meta($post_id, $meta_key, $options, $meta_name, $expire, $table);
155 + }
156 +
112 157 switch($table) {
113 - case 'posts':
158 + case 'posts':
114 159 return Utils::update_meta($post_id, $meta_key, $options, $meta_name, $expire);
115 160 case 'users':
116 161 return Utils::update_user_meta($post_id, $meta_key, $options, $meta_name, $expire);
117 162 default: null;
@@ -137,10 +182,15 @@
137 182 } else {
138 183 $meta_key = false;
139 184 }
140 185 $table = isset($source_type_data['table']) ? $source_type_data['table'] : false;
186 +
187 + if (is_array($table)) {
188 + return Utils::delete_custom_meta($post_id, $meta_key, $meta_name, $table);
189 + }
190 +
141 191 switch($table) {
142 - case 'posts':
192 + case 'posts':
143 193 return Utils::delete_meta($post_id, $meta_key, $meta_name);
144 194 case 'users':
145 195 return Utils::delete_user_meta($post_id, $meta_key, $meta_name);
146 196 default: null;
@@ -147,9 +197,46 @@
147 197 }
148 198 }
149 199 }
150 200
201 + /**
202 + * Clear all meta
203 + *
204 + * @param bool $flush_cache Whether to flush the plugin object cache after each table clear.
205 + */
206 + public static function clear_all_meta($flush_cache = true) {
207 + if (!empty(self::$source_types)) {
208 + foreach (self::$source_types as $source_type => $source_type_data) {
209 + $table = isset($source_type_data['table']) ? $source_type_data['table'] : false;
210 +
211 + if (is_array($table)) {
212 + // Custom backends (e.g. BuddyBoss groupmeta) aren't bulk-cleared here —
213 + // no generic raw table name to target; individual keys are still
214 + // cleaned up as their owning items are deleted.
215 + continue;
216 + }
217 +
218 + switch($table) {
219 + case 'posts':
220 + Utils::clear_all_meta(false, 'postmeta', $flush_cache);
221 + break;
222 + case 'users':
223 + Utils::clear_all_meta(false, 'usermeta', $flush_cache);
224 + break;
225 + default: null;
226 + }
227 + }
228 + }
229 + }
230 +
231 +
151 232
233 + /**
234 + * Return an instance of the class.
235 + *
236 + * @return Integration
237 + * @since 1.0.0
238 + */
152 239 public static function instance(){
153 240 if (is_null(self::$instance)) {
154 241 self::$instance = new self();
155 242 }