| 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 |
$integration = __NAMESPACE__ . '\\' . $integration; |
| 44 |
if (class_exists($integration) && $integration::is_installed()) { |
| 45 |
if (property_exists($integration, 'source_types')) { |
| 46 |
self::$source_types = array_merge(self::$source_types, $integration::$source_types); |
| 47 |
} |
| 48 |
if (property_exists($integration, 'source_type_prefix') && property_exists($integration, 'label')) { |
| 49 |
self::$source_labels[$integration::$source_type_prefix] = $integration::$label; |
| 50 |
} |
| 51 |
if (Utils::is_service_enabled()) { |
| 52 |
$integration::instance(); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Gets the handler class associated with a given source type. |
| 61 |
* |
| 62 |
* @param string $source_type The source type to look up. |
| 63 |
* |
| 64 |
* @return string|null The handler class associated with the source type, or null if not found. |
| 65 |
*/ |
| 66 |
public static function get_handler_class($source_type = 'media_library') { |
| 67 |
return class_exists(__NAMESPACE__ . '\\' . self::$source_types[$source_type]['class']) |
| 68 |
? __NAMESPACE__ . '\\' . self::$source_types[$source_type]['class'] |
| 69 |
: null; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
/** |
| 74 |
* Get Item Meta According to Integration |
| 75 |
* @param int $post_id |
| 76 |
* @param string $key |
| 77 |
* @param mixed $default |
| 78 |
* @param string|false $meta_name |
| 79 |
* @param int|false $expire |
| 80 |
* @param string $source_type |
| 81 |
* @return mixed|null|false |
| 82 |
* @throws \Exception |
| 83 |
* @since 1.0.0 |
| 84 |
*/ |
| 85 |
public static function get_meta($post_id, $key, $default = false, $meta_name = false, $expire = false, $source_type = 'media_library') { |
| 86 |
if(isset(self::$source_types[$source_type])) { |
| 87 |
$source_type_data = self::$source_types[$source_type]; |
| 88 |
$meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key; |
| 89 |
$table = isset($source_type_data['table']) ? $source_type_data['table'] : false; |
| 90 |
|
| 91 |
// Update it - handle in seperate integration by calling common function |
| 92 |
switch($table) { |
| 93 |
case 'posts': |
| 94 |
return Utils::get_meta($post_id, $meta_key, $default, $meta_name, $expire); |
| 95 |
case 'users': |
| 96 |
return Utils::get_user_meta($post_id, $meta_key, $default, $meta_name, $expire); |
| 97 |
default: null; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Update Meta According to Integration |
| 104 |
* @param int $post_id |
| 105 |
* @param string $key |
| 106 |
* @param mixed $options |
| 107 |
* @param string|false $meta_name |
| 108 |
* @param int|false $expire |
| 109 |
* @param string $source_type |
| 110 |
* @return bool|int |
| 111 |
* @throws \Exception |
| 112 |
* @since 1.0.0 |
| 113 |
*/ |
| 114 |
public static function update_meta($post_id, $key, $options, $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 |
switch($table) { |
| 121 |
case 'posts': |
| 122 |
return Utils::update_meta($post_id, $meta_key, $options, $meta_name, $expire); |
| 123 |
case 'users': |
| 124 |
return Utils::update_user_meta($post_id, $meta_key, $options, $meta_name, $expire); |
| 125 |
default: null; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Delete Meta According to Integration |
| 132 |
* @param int $post_id |
| 133 |
* @param string $key |
| 134 |
* @param string|false $meta_name |
| 135 |
* @param string $source_type |
| 136 |
* @return bool |
| 137 |
* @throws \Exception |
| 138 |
* @since 1.0.0 |
| 139 |
*/ |
| 140 |
public static function delete_meta($post_id, $key, $meta_name = false, $source_type = 'media_library') { |
| 141 |
if(isset(self::$source_types[$source_type])) { |
| 142 |
$source_type_data = self::$source_types[$source_type]; |
| 143 |
if($key !== false) { |
| 144 |
$meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key; |
| 145 |
} else { |
| 146 |
$meta_key = false; |
| 147 |
} |
| 148 |
$table = isset($source_type_data['table']) ? $source_type_data['table'] : false; |
| 149 |
switch($table) { |
| 150 |
case 'posts': |
| 151 |
return Utils::delete_meta($post_id, $meta_key, $meta_name); |
| 152 |
case 'users': |
| 153 |
return Utils::delete_user_meta($post_id, $meta_key, $meta_name); |
| 154 |
default: null; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Clear all meta |
| 161 |
* |
| 162 |
* @param bool $flush_cache Whether to flush the plugin object cache after each table clear. |
| 163 |
*/ |
| 164 |
public static function clear_all_meta($flush_cache = true) { |
| 165 |
if (!empty(self::$source_types)) { |
| 166 |
foreach (self::$source_types as $source_type => $source_type_data) { |
| 167 |
$table = isset($source_type_data['table']) ? $source_type_data['table'] : false; |
| 168 |
switch($table) { |
| 169 |
case 'posts': |
| 170 |
Utils::clear_all_meta(false, 'postmeta', $flush_cache); |
| 171 |
break; |
| 172 |
case 'users': |
| 173 |
Utils::clear_all_meta(false, 'usermeta', $flush_cache); |
| 174 |
break; |
| 175 |
default: null; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
/** |
| 184 |
* Return an instance of the class. |
| 185 |
* |
| 186 |
* @return Integration |
| 187 |
* @since 1.0.0 |
| 188 |
*/ |
| 189 |
public static function instance(){ |
| 190 |
if (is_null(self::$instance)) { |
| 191 |
self::$instance = new self(); |
| 192 |
} |
| 193 |
return self::$instance; |
| 194 |
} |
| 195 |
|
| 196 |
} |