assets_url = WPMCS_ASSETS_URL; $this->version = WPMCS_VERSION; $this->token = WPMCS_TOKEN; $this->integrations = [ 'MediaLibrary', 'Acf' ]; // Initialize setup $this->init(); } /** * Initializes all registered integrations. * * Iterates over the list of registered integrations, checks if they are installed, * and if so, initializes their instances. Additionally, it gathers and merges source types * and labels from each integration into the static properties $source_types and $source_labels. * * @since 1.0.0 */ public function init() { // Init all integrations if (!empty($this->integrations)) { foreach ($this->integrations as $integration) { $integration = __NAMESPACE__ . '\\' . $integration; if (class_exists($integration) && $integration::is_installed()) { $integration::instance(); // Add Source types from all integrations if (property_exists($integration, 'source_types')) { self::$source_types = array_merge(self::$source_types, $integration::$source_types); } // Add prefix and label from all integrations if (property_exists($integration, 'source_type_prefix') && property_exists($integration, 'label')) { self::$source_labels[$integration::$source_type_prefix] = $integration::$label; } } } } } /** * Get Item Meta According to Integration * @param int $post_id * @param string $key * @param mixed $default * @param string|false $meta_name * @param int|false $expire * @param string $source_type * @return mixed|null|false * @throws \Exception * @since 1.0.0 */ public static function get_meta($post_id, $key, $default = false, $meta_name = false, $expire = false, $source_type = 'media_library') { if(isset(self::$source_types[$source_type])) { $source_type_data = self::$source_types[$source_type]; $meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key; $table = isset($source_type_data['table']) ? $source_type_data['table'] : false; // Update it - handle in seperate integration by calling common function switch($table) { case 'posts': return Utils::get_meta($post_id, $meta_key, $default, $meta_name, $expire); case 'users': return Utils::get_user_meta($post_id, $meta_key, $default, $meta_name, $expire); default: null; } } } /** * Update Meta According to Integration * @param int $post_id * @param string $key * @param mixed $options * @param string|false $meta_name * @param int|false $expire * @param string $source_type * @return bool|int * @throws \Exception * @since 1.0.0 */ public static function update_meta($post_id, $key, $options, $meta_name = false, $expire = false, $source_type = 'media_library') { if(isset(self::$source_types[$source_type])) { $source_type_data = self::$source_types[$source_type]; $meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key; $table = isset($source_type_data['table']) ? $source_type_data['table'] : false; switch($table) { case 'posts': return Utils::update_meta($post_id, $meta_key, $options, $meta_name, $expire); case 'users': return Utils::update_user_meta($post_id, $meta_key, $options, $meta_name, $expire); default: null; } } } /** * Delete Meta According to Integration * @param int $post_id * @param string $key * @param string|false $meta_name * @param string $source_type * @return bool * @throws \Exception * @since 1.0.0 */ public static function delete_meta($post_id, $key, $meta_name = false, $source_type = 'media_library') { if(isset(self::$source_types[$source_type])) { $source_type_data = self::$source_types[$source_type]; if($key !== false) { $meta_key = (isset($source_type_data['meta_prefix']) ? $source_type_data['meta_prefix'].'_' : '') . $key; } else { $meta_key = false; } $table = isset($source_type_data['table']) ? $source_type_data['table'] : false; switch($table) { case 'posts': return Utils::delete_meta($post_id, $meta_key, $meta_name); case 'users': return Utils::delete_user_meta($post_id, $meta_key, $meta_name); default: null; } } } public static function instance(){ if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } }