PluginProbe
Media Cloud Sync / 1.2.13
Media Cloud Sync v1.2.13
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.2.13, at includes/integrations/integrations.php

159 lines 5.6 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 /**
66 * Get Item Meta According to Integration
67 * @param int $post_id
68 * @param string $key
69 * @param mixed $default
70 * @param string|false $meta_name
71 * @param int|false $expire
72 * @param string $source_type
73 * @return mixed|null|false
74 * @throws \Exception
75 * @since 1.0.0
76 */
77 public static function get_meta($post_id, $key, $default = false, $meta_name = false, $expire = false, $source_type = 'media_library') {
78 if(isset(self::$source_types[$source_type])) {
79 $source_type_data = self::$source_types[$source_type];
80 $meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key;
81 $table = isset($source_type_data['table']) ? $source_type_data['table'] : false;
82
83 // Update it - handle in seperate integration by calling common function
84 switch($table) {
85 case 'posts':
86 return Utils::get_meta($post_id, $meta_key, $default, $meta_name, $expire);
87 case 'users':
88 return Utils::get_user_meta($post_id, $meta_key, $default, $meta_name, $expire);
89 default: null;
90 }
91 }
92 }
93
94 /**
95 * Update Meta According to Integration
96 * @param int $post_id
97 * @param string $key
98 * @param mixed $options
99 * @param string|false $meta_name
100 * @param int|false $expire
101 * @param string $source_type
102 * @return bool|int
103 * @throws \Exception
104 * @since 1.0.0
105 */
106 public static function update_meta($post_id, $key, $options, $meta_name = false, $expire = false, $source_type = 'media_library') {
107 if(isset(self::$source_types[$source_type])) {
108 $source_type_data = self::$source_types[$source_type];
109 $meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key;
110 $table = isset($source_type_data['table']) ? $source_type_data['table'] : false;
111
112 switch($table) {
113 case 'posts':
114 return Utils::update_meta($post_id, $meta_key, $options, $meta_name, $expire);
115 case 'users':
116 return Utils::update_user_meta($post_id, $meta_key, $options, $meta_name, $expire);
117 default: null;
118 }
119 }
120 }
121
122 /**
123 * Delete Meta According to Integration
124 * @param int $post_id
125 * @param string $key
126 * @param string|false $meta_name
127 * @param string $source_type
128 * @return bool
129 * @throws \Exception
130 * @since 1.0.0
131 */
132 public static function delete_meta($post_id, $key, $meta_name = false, $source_type = 'media_library') {
133 if(isset(self::$source_types[$source_type])) {
134 $source_type_data = self::$source_types[$source_type];
135 if($key !== false) {
136 $meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key;
137 } else {
138 $meta_key = false;
139 }
140 $table = isset($source_type_data['table']) ? $source_type_data['table'] : false;
141 switch($table) {
142 case 'posts':
143 return Utils::delete_meta($post_id, $meta_key, $meta_name);
144 case 'users':
145 return Utils::delete_user_meta($post_id, $meta_key, $meta_name);
146 default: null;
147 }
148 }
149 }
150
151
152 public static function instance(){
153 if (is_null(self::$instance)) {
154 self::$instance = new self();
155 }
156 return self::$instance;
157 }
158
159 }