/** * @param string $meta_type * @param int $object_id * @param string $multi_key 'abc' or 'ab/c/def' * @param null|mixed $default_value If no option found in the database, this value will be returned * @param bool|null $get_original_value Original value from db, no changes and translations * * @return mixed|null */ public static function get($meta_type, $object_id, $multi_key, $default_value = null, $get_original_value = null) { if ($get_original_value === null) { $get_original_value = is_admin(); } if (empty($multi_key)) { trigger_error('Key not specified', E_USER_WARNING); return null; } $multi_key = explode('/', $multi_key); $key = array_shift($multi_key); $multi_key = implode('/', $multi_key); $cache_key = self::$cache_key . '/' . $meta_type . '/' . $object_id . '/' . $key; try { $values = FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $values = array(); $values['original'] = get_metadata($meta_type, $object_id, $key, true); $values['prepared'] = fw_prepare_option_value($values['original']); FW_Cache::set($cache_key, $values); } if (empty($multi_key)) { return $values[$get_original_value ? 'original' : 'prepared']; } else { return fw_akg($multi_key, $values[$get_original_value ? 'original' : 'prepared'], $default_value); } }
/** * @param int $post_id * @param string $multi_key 'abc' or 'ab/c/def' * @param array|string|int|bool $set_value */ public static function set($post_id, $multi_key, $set_value) { if (empty($multi_key)) { trigger_error('Key not specified', E_USER_WARNING); return; } $multi_key = explode('/', $multi_key); $key = array_shift($multi_key); $multi_key = implode('/', $multi_key); $cache_key = self::$cache_key . '/' . $post_id . '/' . $key; if (empty($multi_key) && $multi_key !== '0') { /** Replace entire meta */ fw_update_post_meta($post_id, $key, $set_value); FW_Cache::del($cache_key); } else { /** Change only specified key */ $values = array(); $values['original'] = self::get($post_id, $key, true); $values['prepared'] = self::get($post_id, $key, false); fw_aks($multi_key, $set_value, $values['original']); fw_aks($multi_key, fw_prepare_option_value($set_value), $values['prepared']); FW_Cache::set($cache_key, $values); fw_update_post_meta($post_id, $key, $values['original']); } }
/** * {@inheritdoc} * @param array $args * * zip - file path * * dir - where the zip file will be extract */ public function execute(array $args, array $state = array()) { if (!isset($args['zip'])) { return new WP_Error('no_zip', __('Zip file not specified', 'fw')); } else { $args['zip'] = fw_fix_path($args['zip']); } if (!isset($args['dir'])) { return new WP_Error('no_dir', __('Destination dir not specified', 'fw')); } else { $args['dir'] = fw_fix_path($args['dir']); } if (empty($state)) { if (!fw_ext_backups_is_dir_empty($args['dir'])) { return new WP_Error('destination_not_empty', __('Destination dir is not empty', 'fw')); } $state = array('entry' => '', 'extracted_files' => 0); } wp_cache_flush(); FW_Cache::clear(); if (is_wp_error($extract_result = fw_ext_backups_unzip_partial($args['zip'], $args['dir'], $state['entry']))) { return $extract_result; } else { if ($extract_result['finished']) { return true; } else { $state['entry'] = $extract_result['last_entry']; $state['extracted_files'] += $extract_result['extracted_files']; return $state; } } }
/** * Returns fonts * @return array */ public function get_fonts() { $cache_key = 'fw_option_type/' . $this->get_type(); try { return FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $fonts = array('standard' => apply_filters('fw_option_type_typography_v2_standard_fonts', array("Arial", "Verdana", "Trebuchet", "Georgia", "Times New Roman", "Tahoma", "Palatino", "Helvetica", "Calibri", "Myriad Pro", "Lucida", "Arial Black", "Gill Sans", "Geneva", "Impact", "Serif")), 'google' => json_decode(fw_get_google_fonts_v2(), true)); FW_Cache::set($cache_key, $fonts); return $fonts; } }
private function get_updates($force_check = false) { $cache_key = 'fw_ext_update/updates'; // use cache because this method may be called multiple times (to prevent useless requests to update servers) try { return FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $updates = array('framework' => $this->get_framework_update($force_check), 'theme' => $this->get_theme_update($force_check), 'extensions' => $this->get_extensions_with_updates($force_check)); FW_Cache::set($cache_key, $updates); return $updates; } }
/** * Get builder item width data * * Default widths are specified in the config, but some builder types can have custom widths * * Usage example: * <div class="<?php echo esc_attr(fw_ext_builder_get_item_width('builder-type', $item['width'] .'/frontend_class')) ?>" > * * @param string $builder_type Builder option type (some builders can have different item widths) * @param null|string $width_id Specify width id (accepts multikey) or leave empty to get all widths * @param null|mixed $default_value Return this value if specified key does not exist * @return array */ function fw_ext_builder_get_item_width($builder_type, $width_id = null, $default_value = null) { try { $cache_key = fw()->extensions->get('builder')->get_cache_key('item_widths/' . $builder_type); $widths = FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $widths = apply_filters('fw_builder_item_widths:' . $builder_type, fw()->extensions->get('builder')->get_config('default_item_widths')); FW_Cache::set($cache_key, $widths); } if (is_null($width_id)) { return $widths; } else { return fw_akg($width_id, $widths, $default_value); } }
/** * @param string $builder_type * @return array|mixed * @since 1.1.14 */ protected function get_predefined_templates($builder_type) { $cache_id = 'fw_ext_builder/predefined_templates/' . $builder_type . '/' . $this->get_type(); try { return FW_Cache::get($cache_id); } catch (FW_Cache_Not_Found_Exception $e) { $templates = array(); foreach (apply_filters('fw_ext_builder:predefined_templates:' . $builder_type . ':' . $this->get_type(), array()) as $id => $template) { if (isset($template['title']) && is_string($template['title']) && isset($template['json']) && is_string($template['json']) && null !== json_decode($template['json'])) { $templates[$id] = array('title' => $template['title'], 'json' => $template['json'], 'type' => 'predefined'); } else { trigger_error('Invalid predefined template: ' . $id, E_USER_WARNING); } } FW_Cache::set($cache_id, $templates); return $templates; } }
/** * Alternative for update_option() * @param string $option_name * @param string|null $specific_multi_key * @param array|string|int|bool $set_value */ public static function set($option_name, $specific_multi_key = null, $set_value) { $cache_key = self::$cache_key . '/' . $option_name; if ($specific_multi_key === null) { /** Replace entire option */ update_option($option_name, $set_value, false); FW_Cache::del($cache_key); } else { /** Change only specified key */ $values = array(); $values['original'] = self::get($option_name, null, true); $values['prepared'] = self::get($option_name, null, false); fw_aks($specific_multi_key, $set_value, $values['original']); fw_aks($specific_multi_key, fw_prepare_option_value($set_value), $values['prepared']); FW_Cache::set($cache_key, $values); update_option($option_name, $values['original'], false); } }
/** * Check if menu item is a MegaMenu item or is inside a MegaMenu item * @param WP_Post $item * @return bool */ function fw_ext_mega_menu_is_mm_item($item) { $cache_key = fw_ext('megamenu')->get_cache_key('/mm_item'); try { $mm_items = FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $mm_items = array(); } if (isset($mm_items[$item->ID])) { return $mm_items[$item->ID]; } $cursor_item = array('id' => $item->ID, 'parent' => $item->menu_item_parent); do { $is_mm_item = fw_ext_mega_menu_get_meta($cursor_item['id'], 'enabled'); } while (!$is_mm_item && intval($cursor_item['parent']) !== 0 && ($cursor_item = get_post($cursor_item['parent'])) && ($cursor_item = array('id' => $cursor_item->ID, 'parent' => get_post_meta($cursor_item->ID, '_menu_item_menu_item_parent', true)))); $mm_items[$item->ID] = (bool) $is_mm_item; FW_Cache::set($cache_key, $mm_items); return $mm_items[$item->ID]; }
/** * @return array {base_dir_real_path => base_dir_wp_filesystem_path} */ public static function get_base_dirs_map() { /** @var WP_Filesystem_Base $wp_filesystem */ global $wp_filesystem; if (!$wp_filesystem) { trigger_error('Filesystem is not available', E_USER_ERROR); } try { $cache_key = 'fw_wp_filesystem/base_dirs_map'; return FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $themes_dir = get_theme_root(); // Account for relative theme roots if ('/themes' == $themes_dir || !is_dir($themes_dir)) { $themes_dir = WP_CONTENT_DIR . $themes_dir; } $dirs = array(fw_fix_path(ABSPATH) => fw_fix_path($wp_filesystem->abspath()), fw_fix_path(WP_CONTENT_DIR) => fw_fix_path($wp_filesystem->wp_content_dir()), fw_fix_path(WP_PLUGIN_DIR) => fw_fix_path($wp_filesystem->wp_plugins_dir()), fw_fix_path($themes_dir) => fw_fix_path($wp_filesystem->wp_themes_dir())); FW_Cache::set($cache_key, $dirs); return $dirs; } }
/** * {@inheritdoc} * @param array $args * * source_dir - everything from this directory will be added in zip * * destination_dir - where the zip file will be created * * Warning! * Zip can't be executed in steps, it will execute way too long, * because it is impossible to update a zip file, every time you add a file to zip, * a new temp copy of original zip is created with new modifications, it is compressed, * and the original zip is replaced. So when the zip will grow in size, * just adding a single file, will take a very long time. */ public function execute(array $args, array $state = array()) { if (!isset($args['source_dir'])) { return new WP_Error('no_source_dir', __('Source dir not specified', 'fw')); } else { $args['source_dir'] = fw_fix_path($args['source_dir']); } if (!isset($args['destination_dir'])) { return new WP_Error('no_destination_dir', __('Destination dir not specified', 'fw')); } else { $args['destination_dir'] = fw_fix_path($args['destination_dir']); } if (!class_exists('ZipArchive')) { return new WP_Error('zip_ext_missing', __('Zip extension missing', 'fw')); } $zip_path = $args['source_dir'] . '/' . implode('-', array('fw-backup', date('Y_m_d-H_i_s'), fw_ext('backups')->manifest->get_version())) . '.zip'; $zip = new ZipArchive(); if (false === ($zip_error_code = $zip->open($zip_path, ZipArchive::CREATE))) { return new WP_Error('cannot_open_zip', sprintf(__('Cannot open zip (Error code: %s)', 'fw'), $zip_error_code)); } $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($args['source_dir']), RecursiveIteratorIterator::LEAVES_ONLY); foreach ($files as $name => $file) { if (!$file->isDir()) { // Skip directories (they would be added automatically) if (($file_path = $file->getRealPath()) !== $zip_path) { $zip->addFile($file_path, substr(fw_fix_path($file_path), strlen($args['source_dir']) + 1)); } } } wp_cache_flush(); FW_Cache::clear(); // Zip archive will be created only after closing object if (!$zip->close()) { return new WP_Error('cannot_close_zip', __('Cannot close the zip file', 'fw')); } if (!rename($zip_path, $args['destination_dir'] . '/' . basename($zip_path))) { return new WP_Error('cannot_move_zip', __('Cannot move zip in destination dir', 'fw')); } return true; }
public final function get_settings_options() { $cache_key = $this->get_cache_key() . '/settings_options'; try { return FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $path = $this->get_declared_path('/settings-options.php'); if (!file_exists($path)) { FW_Cache::set($cache_key, array()); return array(); } $variables = fw_get_variables_from_file($path, array('options' => array())); FW_Cache::set($cache_key, $variables['options']); return $variables['options']; } }
/** * Empty the cache */ public static function clear() { self::$cache = array(); }
public final function set($item_id = null, $option_id = null, $value, array $extra_data = array()) { FW_Cache::del($cache_key_values = $this->get_cache_key('values', $item_id, $extra_data)); FW_Cache::del($cache_key_values_processed = $this->get_cache_key('values:processed', $item_id, $extra_data)); try { $options = FW_Cache::get($cache_key = $this->get_cache_key('options', $item_id, $extra_data)); } catch (FW_Cache_Not_Found_Exception $e) { FW_Cache::set($cache_key, array()); // prevent recursion FW_Cache::set($cache_key, $options = fw_extract_only_options($this->get_options($item_id, $extra_data))); } $sub_keys = null; if ($option_id) { $option_id = explode('/', $option_id); // 'option_id/sub/keys' $_option_id = array_shift($option_id); // 'option_id' $sub_keys = empty($option_id) ? null : implode('/', $option_id); // 'sub/keys' $option_id = $_option_id; unset($_option_id); $old_values = is_array($old_values = $this->get_values($item_id, $extra_data)) ? $old_values : array(); $old_value = isset($old_values[$option_id]) ? $old_values[$option_id] : null; if ($sub_keys) { // update sub_key in old_value and use the entire value $new_value = $old_value; fw_aks($sub_keys, $value, $new_value); $value = $new_value; unset($new_value); $old_value = fw_akg($sub_keys, $old_value); } if (isset($options[$option_id])) { $value = fw()->backend->option_type($options[$option_id]['type'])->storage_save($option_id, $options[$option_id], $value, $this->get_fw_storage_params($item_id, $extra_data)); } $old_values[$option_id] = $value; $this->set_values($item_id, $old_values, $extra_data); unset($old_values); } else { $old_value = is_array($old_values = $this->get_values($item_id, $extra_data)) ? $old_values : array(); if (!is_array($value)) { $value = array(); } foreach ($value as $_option_id => $_option_value) { if (isset($options[$_option_id])) { $value[$_option_id] = fw()->backend->option_type($options[$_option_id]['type'])->storage_save($_option_id, $options[$_option_id], $_option_value, $this->get_fw_storage_params($item_id, $extra_data)); } } $this->set_values($item_id, $value, $extra_data); } FW_Cache::del($cache_key_values); // fixes https://github.com/ThemeFuse/Unyson/issues/1538 FW_Cache::del($cache_key_values_processed); $this->_after_set($item_id, $option_id, $sub_keys, $old_value, $extra_data); }
private function get_sets() { $cache_key = 'fw_option_type_icon/sets'; try { return FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $sets = apply_filters('fw_option_type_icon_sets', $this->get_default_sets()); // do not allow overwrite default sets $sets = array_merge($sets, $this->get_default_sets()); FW_Cache::set($cache_key, $sets); return $sets; } }
public function import_fp($fp, $keep_users_tables = false, $fix_foreign_database = false, $keep_options = false) { /** * @var wpdb $wpdb */ global $wpdb; $helper = new FW_Backup_Helper_Database(); $exporter = new FW_Backup_Export_Database(); /** * fixme: all options should have bool for wp_option autoload | array( 'option_name' => (bool)autoload ) */ $option_list = array($wpdb->prefix . 'user_roles', 'siteurl', 'blogname', 'blog_charset', 'blogdescription', 'admin_email', 'mailserver_url', 'mailserver_login', 'mailserver_pass', 'mailserver_port', 'ftp_credentials', 'use_ssl', 'template', 'stylesheet', 'current_theme', 'WPLANG'); $option_list = apply_filters('fw_ext_backup_import_skip_options', $option_list); // Preserve some options $before = array_map('get_option', $option_list); $before = array_combine($option_list, $before); // Preserve Backup History and Backup Settings $history = $exporter->export_history(); $settings = $exporter->export_settings(); // Import database (preserve user related tables) // ============================================== if ($keep_users_tables) { $foreign_prefix = $exporter->import_fp($fp, array($wpdb->users, $wpdb->usermeta)); } else { $foreign_prefix = $exporter->import_fp($fp); } wp_cache_flush(); FW_Cache::clear(); $fw_extensions_data = get_option('fw_extensions', array()); if (!empty($fw_extensions_data[$this->backup()->get_name()]['wp_upload_dir']['baseurl']) && $fix_foreign_database) { $wp_upload_dir = wp_upload_dir(); // Fix database if ($fix_foreign_database) { $helper->fix_foreign_database(array(fw_get_url_without_scheme($fw_extensions_data[$this->backup()->get_name()]['wp_upload_dir']['baseurl']) => fw_get_url_without_scheme($wp_upload_dir['baseurl']), str_replace('/', '\\/', fw_get_url_without_scheme($fw_extensions_data[$this->backup()->get_name()]['wp_upload_dir']['baseurl'] . '/')) => str_replace('/', '\\/', fw_get_url_without_scheme($wp_upload_dir['baseurl'] . '/')), str_replace('/', '\\\\/', fw_get_url_without_scheme($fw_extensions_data[$this->backup()->get_name()]['wp_upload_dir']['baseurl'] . '/')) => str_replace('/', '\\\\/', fw_get_url_without_scheme($wp_upload_dir['baseurl'] . '/')), str_replace('/', '\\\\/', fw_get_url_without_scheme($fw_extensions_data[$this->backup()->get_name()]['wp_upload_dir']['baseurl'] . '/')) => str_replace('/', '\\\\/', fw_get_url_without_scheme($wp_upload_dir['baseurl'] . '/')))); } } // Restore Backup History and Settings $exporter->import_history($history); $exporter->import_settings($settings); // Fix database if ($fix_foreign_database) { $uploadDir = wp_upload_dir(); $uploadOld = fw_get_url_without_scheme(site_url() . '/wp-content/uploads/'); $uploadNew = fw_get_url_without_scheme($uploadDir['baseurl'] . '/'); $helper->fix_foreign_database(array($uploadOld => $uploadNew, str_replace('/', '\\/', $uploadOld) => str_replace('/', '\\/', $uploadNew), str_replace('/', '\\\\/', $uploadOld) => str_replace('/', '\\\\/', $uploadNew), str_replace('/', '\\\\/', $uploadOld) => str_replace('/', '\\\\/', $uploadNew), site_url() => $before['siteurl'], site_url() . '/' => $before['siteurl'] . '/', fw_get_url_without_scheme(site_url() . '/') => fw_get_url_without_scheme($before['siteurl'] . '/'), str_replace('/', '\\/', fw_get_url_without_scheme(site_url() . '/')) => str_replace('/', '\\/', fw_get_url_without_scheme($before['siteurl'] . '/')), str_replace('/', '\\\\/', fw_get_url_without_scheme(site_url() . '/')) => str_replace('/', '\\\\/', fw_get_url_without_scheme($before['siteurl'] . '/')), str_replace('/', '\\\\/', fw_get_url_without_scheme(site_url() . '/')) => str_replace('/', '\\\\/', fw_get_url_without_scheme($before['siteurl'] . '/')))); $helper->fix_wp_options($foreign_prefix); } wp_cache_flush(); FW_Cache::clear(); // Restore options if ($keep_options) { // WP keeps stylesheet settings in theme_mods_{stylesheet} option, // that means that if stylesheet option has different value in dump file and in database // new theme_mods_{stylesheet} should be rename to old theme_mods_{stylesheet} $stylesheet = get_option('stylesheet'); if ($before['stylesheet'] != $stylesheet) { $theme_mods_before = 'theme_mods_' . $before['stylesheet']; $theme_mods_after = 'theme_mods_' . $stylesheet; $query = $wpdb->prepare("\n\t\t\t\t\tDELETE FROM\n\t\t\t\t\t\t{$wpdb->options}\n\t\t\t\t\tWHERE\n\t\t\t\t\t option_name = %s\n\t\t\t\t", $theme_mods_before); $wpdb->query($query); $query = $wpdb->prepare("\n\t\t\t\t\tUPDATE\n\t\t\t\t\t\t{$wpdb->options}\n\t\t\t\t\tSET\n\t\t\t\t\t\toption_name = %s\n\t\t\t\t\tWHERE\n\t\t\t\t\t\toption_name = %s\n\t\t\t\t", $theme_mods_before, $theme_mods_after); $wpdb->query($query); } // Restore all saved options array_map('update_option', array_keys($before), $before); } // Actualize settings $this->backup()->cron()->reschedule(); wp_cache_flush(); FW_Cache::clear(); }
/** * @return Array with Google fonts */ function fw_get_google_fonts() { $cache_key = 'fw_google_fonts'; try { return FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $fonts = apply_filters('fw_google_fonts', include dirname(__FILE__) . '/fw-google-fonts.json.php'); FW_Cache::set($cache_key, $fonts); return $fonts; } }
/** * Scan all directories for extensions * * @param bool $reset_cache * @return array */ private function get_installed_extensions($reset_cache = false) { $cache_key = $this->get_cache_key('installed_extensions'); if ($reset_cache) { FW_Cache::del($cache_key); } try { return FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $extensions = array(); foreach (fw()->extensions->get_locations() as $location) { // leave only used keys $location = array('path' => $location['path'], 'is' => $location['is']); $this->read_extensions($location, $extensions); } FW_Cache::set($cache_key, $extensions); return $extensions; } }
/** * Return config key value, or entire config array * Config array is merged from child configs * @param string|null $key Multi key format accepted: 'a/b/c' * @return mixed|null */ public final function get_config($key = null) { $cache_key = self::$cache_key . '/config'; try { $config = FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $config = array(); if (file_exists(FW_PT_CUSTOM_DIR . '/theme/config.php')) { $variables = fw_get_variables_from_file(FW_PT_CUSTOM_DIR . '/theme/config.php', array('cfg' => null)); if (!empty($variables['cfg'])) { $config = array_merge($config, $variables['cfg']); unset($variables); } } if (FW_CT && file_exists(FW_CT_CUSTOM_DIR . '/theme/config.php')) { $variables = fw_get_variables_from_file(FW_CT_CUSTOM_DIR . '/theme/config.php', array('cfg' => null)); if (!empty($variables['cfg'])) { $config = array_merge($config, $variables['cfg']); unset($variables); } } unset($path); FW_Cache::set($cache_key, $config); } return $key === null ? $config : fw_akg($key, $config); }
/** * All backups (zip) will go in this directory * @return string */ public function get_backups_dir() { $cache_key = $this->get_cache_key('/dir'); try { return FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $uploads = wp_upload_dir(); $dir = fw_fix_path($uploads['basedir']) . '/fw-backup'; FW_Cache::set($cache_key, $dir); return $dir; } }
/** * @return Array with Google fonts */ function fw_get_google_fonts() { $cache_key = 'fw_google_fonts'; try { return FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $fonts = apply_filters('fw_google_fonts', json_decode(file_get_contents(dirname(__FILE__) . '/fw-google-fonts.json'), true)); FW_Cache::set($cache_key, $fonts); return $fonts; } }
public function get_locations() { $cache_key = 'fw_extensions_locations'; try { return FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { /** * { '/hello/world/extensions' => 'https://hello.com/world/extensions' } */ $custom_locations = apply_filters('fw_extensions_locations', array()); $customizations_locations = array(); if (is_child_theme()) { $customizations_locations[fw_get_stylesheet_customizations_directory('/extensions')] = fw_get_stylesheet_customizations_directory_uri('/extensions'); } $customizations_locations[fw_get_template_customizations_directory('/extensions')] = fw_get_template_customizations_directory_uri('/extensions'); $customizations_locations += $custom_locations; $locations = array(); $locations[fw_get_framework_directory('/extensions')] = array('path' => fw_get_framework_directory('/extensions'), 'uri' => fw_get_framework_directory_uri('/extensions'), 'customizations_locations' => $customizations_locations, 'is' => array('framework' => true, 'custom' => false, 'theme' => false)); foreach ($custom_locations as $path => $uri) { unset($customizations_locations[$path]); $locations[$path] = array('path' => $path, 'uri' => $uri, 'customizations_locations' => $customizations_locations, 'is' => array('framework' => false, 'custom' => true, 'theme' => false)); } array_pop($customizations_locations); $locations[fw_get_template_customizations_directory('/extensions')] = array('path' => fw_get_template_customizations_directory('/extensions'), 'uri' => fw_get_template_customizations_directory_uri('/extensions'), 'customizations_locations' => $customizations_locations, 'is' => array('framework' => false, 'custom' => false, 'theme' => true)); if (is_child_theme()) { array_pop($customizations_locations); $locations[fw_get_stylesheet_customizations_directory('/extensions')] = array('path' => fw_get_stylesheet_customizations_directory('/extensions'), 'uri' => fw_get_stylesheet_customizations_directory_uri('/extensions'), 'customizations_locations' => $customizations_locations, 'is' => array('framework' => false, 'custom' => false, 'theme' => true)); } FW_Cache::set($cache_key, $locations); return $locations; } }
/** * Return config key value, or entire config array * Config array is merged from child configs * @param string|null $key Multi key format accepted: 'a/b/c' * @param mixed $default_value * @return mixed|null */ public final function get_config($key = null, $default_value = null) { $cache_key = self::$cache_key . '/config'; try { $config = FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $config = array('settings_form_ajax_submit' => true, 'settings_form_side_tabs' => false); if (file_exists(fw_get_template_customizations_directory('/theme/config.php'))) { $variables = fw_get_variables_from_file(fw_get_template_customizations_directory('/theme/config.php'), array('cfg' => null)); if (!empty($variables['cfg'])) { $config = array_merge($config, $variables['cfg']); unset($variables); } } if (is_child_theme() && file_exists(fw_get_stylesheet_customizations_directory('/theme/config.php'))) { $variables = fw_get_variables_from_file(fw_get_stylesheet_customizations_directory('/theme/config.php'), array('cfg' => null)); if (!empty($variables['cfg'])) { $config = array_merge($config, $variables['cfg']); unset($variables); } } unset($path); FW_Cache::set($cache_key, $config); } return $key === null ? $config : fw_akg($key, $config, $default_value); }
/** * Empty the cache * @param mixed $dummy When method is used in add_filter() * @return mixed */ public static function clear($dummy = null) { self::$cache = array(); /** * This method is used in add_filter() so to not break anything return filter value */ return $dummy; }
/** * Get a customizer framework option value from the database * * @param string|null $option_id Specific option id (accepts multikey). null - all options * @param null|mixed $default_value If no option found in the database, this value will be returned * // fixme: Maybe add this parameter? @ param null|bool $get_original_value Original value is that with no translations and other changes * * @return mixed|null */ function fw_get_db_customizer_option($option_id = null, $default_value = null) { // note: this contains only changed controls/options $all_db_values = get_theme_mod(FW_Option_Type::get_default_name_prefix(), array()); $cache_key = 'fw_default_options_values/customizer'; try { $all_default_values = FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { // extract the default values from options array $all_default_values = fw_get_options_values_from_input(fw()->theme->get_customizer_options(), array()); FW_Cache::set($cache_key, $all_default_values); } if (is_null($option_id)) { return array_merge($all_default_values, $all_db_values); } else { $base_key = explode('/', $option_id); // note: option_id can be a multi-key 'a/b/c' $base_key = array_shift($base_key); $all_db_values = array_key_exists($base_key, $all_db_values) ? $all_db_values : $all_default_values; return fw_akg($option_id, $all_db_values, $default_value); } }
/** * Get extension's settings option value from the database * * @param string $extension_name * @param string|null $option_id * @param null|mixed $default_value If no option found in the database, this value will be returned * @param null|bool $get_original_value Original value is that with no translations and other changes * * @return mixed|null */ function fw_get_db_ext_settings_option($extension_name, $option_id = null, $default_value = null, $get_original_value = null) { if (!($extension = fw()->extensions->get($extension_name))) { trigger_error('Invalid extension: ' . $extension_name, E_USER_WARNING); return null; } $value = FW_WP_Option::get('fw_ext_settings_options:' . $extension_name, $option_id, $default_value, $get_original_value); if (is_null($value)) { /** * Maybe the options was never saved or the given option id does not exists * Extract the default values from the options array and try to find there the option id */ $cache_key = 'fw_default_options_values/ext_settings:' . $extension_name; try { $all_options_values = FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { // extract the default values from options array $all_options_values = fw_get_options_values_from_input($extension->get_settings_options(), array()); FW_Cache::set($cache_key, $all_options_values); } if (empty($option_id)) { // option id not specified, return all options values return $all_options_values; } else { return fw_akg($option_id, $all_options_values, $default_value); } } else { return $value; } }
/** * Get a customizer framework option value from the database * * @param string|null $option_id Specific option id (accepts multikey). null - all options * @param null|mixed $default_value If no option found in the database, this value will be returned * // fixme: Maybe add this parameter? @ param null|bool $get_original_value Original value is that with no translations and other changes * * @return mixed|null */ function fw_get_db_customizer_option($option_id = null, $default_value = null) { // note: this contains only changed controls/options $db_values = get_theme_mod(FW_Option_Type::get_default_name_prefix(), null); if (!is_null($default_value) && is_null($option_id ? fw_akg($option_id, $db_values) : $db_values)) { /** * Default value was provided in case db value is empty. * * Do not extract default values from options files (below) * maybe this function was called from options files and it will cause infinite loop, * that's why the developer provided a default value to prevent that. */ return $default_value; } if (is_null($db_values)) { $db_values = array(); } if (is_null($option_id) || ($base_key = explode('/', $option_id)) && ($base_key = array_shift($base_key)) && !array_key_exists($base_key, $db_values)) { $cache_key = 'fw_default_options_values/customizer'; try { $default_values = FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { // extract the default values from options array $default_values = fw_get_options_values_from_input(fw()->theme->get_customizer_options(), array()); FW_Cache::set($cache_key, $default_values); } $db_values = array_merge($default_values, $db_values); } return is_null($option_id) ? $db_values : fw_akg($option_id, $db_values, $default_value); }
/** * Return config key value, or entire config array * Config array is merged from child configs * @param string|null $key Multi key format accepted: 'a/b/c' * @return mixed|null */ public final function get_config($key = null) { $cache_key = $this->get_cache_key() . '/config'; try { $config = FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { list($search_in_framework, $search_in_parent_theme, $search_in_child_theme) = $this->correct_search_in_locations(true, true, true); $rel_path = $this->get_rel_path() . '/config.php'; $config = array(); $paths = array(); if ($search_in_framework) { $paths[] = FW_EXTENSIONS_DIR . $rel_path; } if ($search_in_parent_theme) { $paths[] = FW_PT_EXTENSIONS_DIR . $rel_path; } if ($search_in_child_theme) { $paths[] = FW_CT_EXTENSIONS_DIR . $rel_path; } foreach ($paths as $path) { if (file_exists($path)) { $variables = fw_get_variables_from_file($path, array('cfg' => null)); if (!empty($variables['cfg'])) { $config = array_merge($config, $variables['cfg']); unset($variables); } } } unset($path); FW_Cache::set($cache_key, $config); } return $key === null ? $config : fw_akg($key, $config); }
/** * Render a meta box * * @param string $id * @param string $title * @param string $content HTML * @param array $other Optional elements * * @return string Generated meta box html */ public function render_box($id, $title, $content, $other = array()) { if (!function_exists('add_meta_box')) { trigger_error('Try call this method later (\'admin_init\' action), add_meta_box() function does not exists yet.', E_USER_WARNING); return ''; } $other = array_merge(array('html_before_title' => false, 'html_after_title' => false, 'attr' => array()), $other); $placeholders = array('id' => '{{meta_box_id}}', 'title' => '{{meta_box_title}}', 'content' => '{{meta_box_content}}'); $placeholders['html_before_title'] = '{{meta_box_html_before_title}}'; $placeholders['html_after_title'] = '{{meta_box_html_after_title}}'; $placeholders['attr'] = '{{meta_box_attr}}'; $placeholders['attr_class'] = '{{meta_box_attr_class}}'; $cache_key = 'fw_meta_box_template'; try { $meta_box_template = FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $temp_screen_id = 'fw-temp-meta-box-screen-id-' . fw_unique_increment(); $context = 'normal'; add_meta_box($placeholders['id'], $placeholders['title'], $this->print_meta_box_content_callback, $temp_screen_id, $context, 'default', $placeholders['content']); ob_start(); do_meta_boxes($temp_screen_id, $context, null); $meta_box_template = ob_get_clean(); remove_meta_box($id, $temp_screen_id, $context); $meta_box_template = str_replace('<div id="' . $context . '-sortables" class="meta-box-sortables">', '', $meta_box_template); $meta_box_template = explode('</div>', $meta_box_template); array_pop($meta_box_template); $meta_box_template = implode('</div>', $meta_box_template); // add 'fw-postbox' class and some attr related placeholders $meta_box_template = str_replace('class="postbox', $placeholders['attr'] . ' class="postbox fw-postbox' . $placeholders['attr_class'], $meta_box_template); $meta_box_template = str_replace('<span>' . $placeholders['title'] . '</span>', '<small class="fw-html-before-title">' . $placeholders['html_before_title'] . '</small>' . '<span>' . $placeholders['title'] . '</span>' . '<small class="fw-html-after-title">' . $placeholders['html_after_title'] . '</small>', $meta_box_template); FW_Cache::set($cache_key, $meta_box_template); } $attr_class = ''; if (isset($other['attr']['class'])) { $attr_class = ' ' . $other['attr']['class']; unset($other['attr']['class']); } unset($other['attr']['id']); // replace placeholders with data/content return str_replace(array($placeholders['id'], $placeholders['title'], $placeholders['content'], $placeholders['html_before_title'], $placeholders['html_after_title'], $placeholders['attr'], $placeholders['attr_class']), array(esc_attr($id), $title, $content, $other['html_before_title'], $other['html_after_title'], fw_attr_to_html($other['attr']), esc_attr($attr_class)), $meta_box_template); }
/** * @return Array with Google fonts */ function fw_get_google_fonts() { $cache_key = 'fw_google_fonts'; try { return FW_Cache::get($cache_key); } catch (FW_Cache_Not_Found_Exception $e) { $g_fonts = json_decode(fw_get_google_fonts_v2(), true); $old_fonts = (include dirname(__FILE__) . '/fw-google-fonts.json.php'); $fonts = array(); foreach ($g_fonts['items'] as $font) { $fonts[$font['family']] = array('family' => $font['family'], 'variants' => $font['variants'], 'position' => isset($old_fonts[$font['family']]) ? $old_fonts[$font['family']]['position'] : 99999); } $fonts = apply_filters('fw_google_fonts', $fonts); FW_Cache::set($cache_key, $fonts); return $fonts; } }