| 1 |
<?php |
| 2 |
namespace Dudlewebs\WPMCS; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
class Integration { |
| 7 |
private static $instance = null; |
| 8 |
|
| 9 |
private $integrations = []; |
| 10 |
public static $source_types = []; |
| 11 |
public static $source_labels = []; |
| 12 |
|
| 13 |
/** |
| 14 |
* Admin constructor. |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
$this->integrations = [ |
| 19 |
'MediaLibrary', |
| 20 |
'Acf', |
| 21 |
'Imagify', |
| 22 |
'WooCommerce', |
| 23 |
]; |
| 24 |
|
| 25 |
// Initialize setup |
| 26 |
$this->init(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Initializes all registered integrations. |
| 31 |
* |
| 32 |
* Iterates over the list of registered integrations, checks if they are installed, |
| 33 |
* and if so, initializes their instances. Additionally, it gathers and merges source types |
| 34 |
* and labels from each integration into the static properties $source_types and $source_labels. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
*/ |
| 38 |
|
| 39 |
public function init() { |
| 40 |
// Init all integrations |
| 41 |
if (!empty($this->integrations)) { |
| 42 |
foreach ($this->integrations as $integration) { |
| 43 |
$this->process_integration($integration); |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 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 |
} |
| 71 |
|
| 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 |
/** |
| 103 |
* Get Item Meta According to Integration |
| 104 |
* @param int $post_id |
| 105 |
* @param string $key |
| 106 |
* @param mixed $default |
| 107 |
* @param string|false $meta_name |
| 108 |
* @param int|false $expire |
| 109 |
* @param string $source_type |
| 110 |
* @return mixed|null|false |
| 111 |
* @throws \Exception |
| 112 |
* @since 1.0.0 |
| 113 |
*/ |
| 114 |
public static function get_meta($post_id, $key, $default = false, $meta_name = false, $expire = false, $source_type = 'media_library') { |
| 115 |
if(isset(self::$source_types[$source_type])) { |
| 116 |
$source_type_data = self::$source_types[$source_type]; |
| 117 |
$meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key; |
| 118 |
$table = isset($source_type_data['table']) ? $source_type_data['table'] : false; |
| 119 |
|
| 120 |
if (is_array($table)) { |
| 121 |
return Utils::get_custom_meta($post_id, $meta_key, $default, $meta_name, $expire, $table); |
| 122 |
} |
| 123 |
|
| 124 |
// Update it - handle in seperate integration by calling common function |
| 125 |
switch($table) { |
| 126 |
case 'posts': |
| 127 |
return Utils::get_meta($post_id, $meta_key, $default, $meta_name, $expire); |
| 128 |
case 'users': |
| 129 |
return Utils::get_user_meta($post_id, $meta_key, $default, $meta_name, $expire); |
| 130 |
default: return $default; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Update Meta According to Integration |
| 137 |
* @param int $post_id |
| 138 |
* @param string $key |
| 139 |
* @param mixed $options |
| 140 |
* @param string|false $meta_name |
| 141 |
* @param int|false $expire |
| 142 |
* @param string $source_type |
| 143 |
* @return bool|int |
| 144 |
* @throws \Exception |
| 145 |
* @since 1.0.0 |
| 146 |
*/ |
| 147 |
public static function update_meta($post_id, $key, $options, $meta_name = false, $expire = false, $source_type = 'media_library') { |
| 148 |
if(isset(self::$source_types[$source_type])) { |
| 149 |
$source_type_data = self::$source_types[$source_type]; |
| 150 |
$meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key; |
| 151 |
$table = isset($source_type_data['table']) ? $source_type_data['table'] : false; |
| 152 |
|
| 153 |
if (is_array($table)) { |
| 154 |
return Utils::update_custom_meta($post_id, $meta_key, $options, $meta_name, $expire, $table); |
| 155 |
} |
| 156 |
|
| 157 |
switch($table) { |
| 158 |
case 'posts': |
| 159 |
return Utils::update_meta($post_id, $meta_key, $options, $meta_name, $expire); |
| 160 |
case 'users': |
| 161 |
return Utils::update_user_meta($post_id, $meta_key, $options, $meta_name, $expire); |
| 162 |
default: null; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Delete Meta According to Integration |
| 169 |
* @param int $post_id |
| 170 |
* @param string $key |
| 171 |
* @param string|false $meta_name |
| 172 |
* @param string $source_type |
| 173 |
* @return bool |
| 174 |
* @throws \Exception |
| 175 |
* @since 1.0.0 |
| 176 |
*/ |
| 177 |
public static function delete_meta($post_id, $key, $meta_name = false, $source_type = 'media_library') { |
| 178 |
if(isset(self::$source_types[$source_type])) { |
| 179 |
$source_type_data = self::$source_types[$source_type]; |
| 180 |
if($key !== false) { |
| 181 |
$meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key; |
| 182 |
} else { |
| 183 |
$meta_key = false; |
| 184 |
} |
| 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 |
|
| 191 |
switch($table) { |
| 192 |
case 'posts': |
| 193 |
return Utils::delete_meta($post_id, $meta_key, $meta_name); |
| 194 |
case 'users': |
| 195 |
return Utils::delete_user_meta($post_id, $meta_key, $meta_name); |
| 196 |
default: null; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 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 |
|
| 232 |
|
| 233 |
/** |
| 234 |
* Return an instance of the class. |
| 235 |
* |
| 236 |
* @return Integration |
| 237 |
* @since 1.0.0 |
| 238 |
*/ |
| 239 |
public static function instance(){ |
| 240 |
if (is_null(self::$instance)) { |
| 241 |
self::$instance = new self(); |
| 242 |
} |
| 243 |
return self::$instance; |
| 244 |
} |
| 245 |
|
| 246 |
} |