PluginProbe
Media Cloud Sync / 1.3.0
Media Cloud Sync v1.3.0
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 1.3.0 All 34 releases
media-cloud-sync / includes / integrations / integrations.php

integrations.php in Media Cloud Sync 1.3.0, at includes/integrations/integrations.php

178 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Dudlewebs\WPMCS;
3
4 defined('ABSPATH') || exit;
5
6 class Integration {
7 private static $instance = null;
8 private $assets_url;
9 private $version;
10 private $token;
11
12 private $integrations = [];
13 public static $source_types = [];
14 public static $source_labels = [];
15
16 /**
17 * Admin constructor.
18 * @since 1.0.0
19 */
20 public function __construct() {
21 $this->assets_url = WPMCS_ASSETS_URL;
22 $this->version = WPMCS_VERSION;
23 $this->token = WPMCS_TOKEN;
24
25 $this->integrations = [
26 'MediaLibrary',
27 'Acf'
28 ];
29
30 // Initialize setup
31 $this->init();
32 }
33
34 /**
35 * Initializes all registered integrations.
36 *
37 * Iterates over the list of registered integrations, checks if they are installed,
38 * and if so, initializes their instances. Additionally, it gathers and merges source types
39 * and labels from each integration into the static properties $source_types and $source_labels.
40 *
41 * @since 1.0.0
42 */
43
44 public function init() {
45 // Init all integrations
46 if (!empty($this->integrations)) {
47 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 }
60 }
61 }
62 }
63
64 /**
65 * Gets the handler class associated with a given source type.
66 *
67 * @param string $source_type The source type to look up.
68 *
69 * @return string|null The handler class associated with the source type, or null if not found.
70 */
71 public static function get_handler_class($source_type = 'media_library') {
72 return class_exists(__NAMESPACE__ . '\\' . self::$source_types[$source_type]['class'])
73 ? __NAMESPACE__ . '\\' . self::$source_types[$source_type]['class']
74 : null;
75 }
76
77
78 /**
79 * Get Item Meta According to Integration
80 * @param int $post_id
81 * @param string $key
82 * @param mixed $default
83 * @param string|false $meta_name
84 * @param int|false $expire
85 * @param string $source_type
86 * @return mixed|null|false
87 * @throws \Exception
88 * @since 1.0.0
89 */
90 public static function get_meta($post_id, $key, $default = false, $meta_name = false, $expire = false, $source_type = 'media_library') {
91 if(isset(self::$source_types[$source_type])) {
92 $source_type_data = self::$source_types[$source_type];
93 $meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key;
94 $table = isset($source_type_data['table']) ? $source_type_data['table'] : false;
95
96 // Update it - handle in seperate integration by calling common function
97 switch($table) {
98 case 'posts':
99 return Utils::get_meta($post_id, $meta_key, $default, $meta_name, $expire);
100 case 'users':
101 return Utils::get_user_meta($post_id, $meta_key, $default, $meta_name, $expire);
102 default: null;
103 }
104 }
105 }
106
107 /**
108 * Update Meta According to Integration
109 * @param int $post_id
110 * @param string $key
111 * @param mixed $options
112 * @param string|false $meta_name
113 * @param int|false $expire
114 * @param string $source_type
115 * @return bool|int
116 * @throws \Exception
117 * @since 1.0.0
118 */
119 public static function update_meta($post_id, $key, $options, $meta_name = false, $expire = false, $source_type = 'media_library') {
120 if(isset(self::$source_types[$source_type])) {
121 $source_type_data = self::$source_types[$source_type];
122 $meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key;
123 $table = isset($source_type_data['table']) ? $source_type_data['table'] : false;
124
125 switch($table) {
126 case 'posts':
127 return Utils::update_meta($post_id, $meta_key, $options, $meta_name, $expire);
128 case 'users':
129 return Utils::update_user_meta($post_id, $meta_key, $options, $meta_name, $expire);
130 default: null;
131 }
132 }
133 }
134
135 /**
136 * Delete Meta According to Integration
137 * @param int $post_id
138 * @param string $key
139 * @param string|false $meta_name
140 * @param string $source_type
141 * @return bool
142 * @throws \Exception
143 * @since 1.0.0
144 */
145 public static function delete_meta($post_id, $key, $meta_name = false, $source_type = 'media_library') {
146 if(isset(self::$source_types[$source_type])) {
147 $source_type_data = self::$source_types[$source_type];
148 if($key !== false) {
149 $meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key;
150 } else {
151 $meta_key = false;
152 }
153 $table = isset($source_type_data['table']) ? $source_type_data['table'] : false;
154 switch($table) {
155 case 'posts':
156 return Utils::delete_meta($post_id, $meta_key, $meta_name);
157 case 'users':
158 return Utils::delete_user_meta($post_id, $meta_key, $meta_name);
159 default: null;
160 }
161 }
162 }
163
164
165 /**
166 * Return an instance of the class.
167 *
168 * @return Integration
169 * @since 1.0.0
170 */
171 public static function instance(){
172 if (is_null(self::$instance)) {
173 self::$instance = new self();
174 }
175 return self::$instance;
176 }
177
178 }