/** * Creates a slug to be used for pretty URLs. * * @link http://pepvn.com/the-perfect-php-clean-url-generator * @param $string * @param array $replace * @param string $separator * @return mixed * @throws \WpPepVN\Exception */ public static function generate($string, $separator = '-', $replace = null, $lowercase = true) { if (System::extension_loaded('iconv')) { /** * Save the old locale and set the new locale to UTF-8 */ $oldLocale = setlocale(LC_ALL, 'en_US.UTF-8'); $string = iconv('UTF-8', 'ASCII//TRANSLIT', $string); } if ($replace) { if (is_array($replace) || is_string($replace)) { $string = str_replace((array) $replace, ' ', $string); } } $string = preg_replace('#[^a-zA-Z0-9/_\\|\\+ \\-]+#is', ' ', $string); if ($lowercase) { $string = strtolower($string); } $string = preg_replace('#[\\s \\t\\-/_\\|\\+]+#', $separator, $string); $string = trim($string, $separator); if (System::extension_loaded('iconv')) { /** * Revert back to the old locale */ setlocale(LC_ALL, $oldLocale); } return $string; }
private function _function_exists($name) { $k = 'fcex' . $name; if (isset(self::$_wpextend_tempData[$k])) { return self::$_wpextend_tempData[$k]; } else { self::$_wpextend_tempData[$k] = System::function_exists($name); return self::$_wpextend_tempData[$k]; } }
public static function has_algos($algorithm_name) { if (!isset(self::$_tempData['hash_algos'])) { self::$_tempData['hash_algos'] = array(); if (System::function_exists('hash_algos')) { self::$_tempData['hash_algos'] = hash_algos(); self::$_tempData['hash_algos'] = array_flip(self::$_tempData['hash_algos']); } } return isset(self::$_tempData['hash_algos'][$algorithm_name]); }
private function _initCacheObject() { $pepvnDirCachePathTemp = $this->_configs['folder']; if (!is_dir($pepvnDirCachePathTemp)) { System::mkdir($pepvnDirCachePathTemp); } if (is_dir($pepvnDirCachePathTemp) && is_readable($pepvnDirCachePathTemp) && is_writable($pepvnDirCachePathTemp)) { $pepvnCacheHashKeySaltTemp = PepVN_Data::$defaultParams['fullDomainName'] . $this->_key; if (defined('WP_PEPVN_SITE_SALT')) { $pepvnCacheHashKeySaltTemp .= '_' . WP_PEPVN_SITE_SALT; } $this->_cacheObject = new PepVN_CacheSimpleFile(array('cache_timeout' => 86400, 'hash_key_method' => 'crc32b', 'hash_key_salt' => hash('crc32b', md5($pepvnCacheHashKeySaltTemp)), 'gzcompress_level' => 2, 'key_prefix' => 'dtstvr_', 'cache_dir' => $pepvnDirCachePathTemp)); } else { $this->_cacheObject = new PepVN_CacheSimpleFile(array()); } }
$cacheStatus = false; $dirPath = WP_OPTIMIZE_BY_XTRAFFIC_PLUGIN_STORAGES_CACHE_DIR . 'phcnscf' . DIRECTORY_SEPARATOR; System::mkdir($dirPath); if (is_dir($dirPath) && is_readable($dirPath) && is_writable($dirPath)) { $cacheStatus = true; } if ($cacheStatus) { return new CacheBackendFile(new CacheFrontendData(array('lifetime' => WP_PEPVN_CACHE_TIMEOUT_NORMAL)), array('cacheDir' => $dirPath, 'prefix' => WP_PEPVN_CACHE_PREFIX)); } else { return new CacheBackendMemory(new CacheFrontendNone()); } }, true); $di->set('cachePermanent', function () use($config, $di) { $cacheStatus = false; $dirPath = WP_OPTIMIZE_BY_XTRAFFIC_PLUGIN_STORAGES_CACHE_DIR . 'phcnscfpm' . DIRECTORY_SEPARATOR; System::mkdir($dirPath); if (is_dir($dirPath) && is_readable($dirPath) && is_writable($dirPath)) { $cacheStatus = true; } if ($cacheStatus) { return new CacheBackendFile(new CacheFrontendData(array('lifetime' => WP_PEPVN_CACHE_TIMEOUT_NORMAL)), array('cacheDir' => $dirPath, 'prefix' => WP_PEPVN_CACHE_PREFIX)); } else { return new CacheBackendMemory(new CacheFrontendNone()); } }, true); /* * Config for only this plugin */ /* $url = $di->get('url'); $url->setDI($di);
public function on_plugin_activation() { $dir = $this->_folderStorePath; $objects = scandir($dir); if (is_array($objects)) { $objects = array_diff($objects, array('.', '..')); foreach ($objects as $objIndex => $objPath) { unset($objects[$objIndex]); if ($objPath) { if (strlen($objPath) > 5) { $objFullPath = $dir . $objPath; if (is_dir($objFullPath)) { if (is_readable($objFullPath) && is_writable($objFullPath)) { System::rmdirR($objFullPath); } } } } } } }
public static function initCacheObject() { //cacheObject : store cache for short time (less than 1 day) $pepvnDirCachePathTemp = WP_OPTIMIZE_BY_XTRAFFIC_PLUGIN_STORAGES_CACHE_DIR . 's' . DIRECTORY_SEPARATOR; if (!is_dir($pepvnDirCachePathTemp)) { System::mkdir($pepvnDirCachePathTemp); } if (is_dir($pepvnDirCachePathTemp) && is_readable($pepvnDirCachePathTemp) && is_writable($pepvnDirCachePathTemp)) { $pepvnCacheHashKeySaltTemp = self::$defaultParams['fullDomainName'] . $pepvnDirCachePathTemp; if (defined('WP_PEPVN_SITE_SALT')) { $pepvnCacheHashKeySaltTemp .= '_' . WP_PEPVN_SITE_SALT; } self::$cacheObject = new \WPOptimizeByxTraffic\Application\Service\PepVN_CacheSimpleFile(array('cache_timeout' => 86400, 'hash_key_method' => 'crc32b', 'hash_key_salt' => hash('crc32b', md5($pepvnCacheHashKeySaltTemp)), 'gzcompress_level' => 2, 'key_prefix' => 'dts_', 'cache_dir' => $pepvnDirCachePathTemp)); } else { self::$cacheObject = new \WPOptimizeByxTraffic\Application\Service\PepVN_CacheSimpleFile(array()); } //cachePermanentObject : store cache for long time (>6 days) $pepvnDirCachePathTemp = WP_OPTIMIZE_BY_XTRAFFIC_PLUGIN_STORAGES_CACHE_DIR . 'pm' . DIRECTORY_SEPARATOR; if (!is_dir($pepvnDirCachePathTemp)) { System::mkdir($pepvnDirCachePathTemp); } if (is_dir($pepvnDirCachePathTemp) && is_readable($pepvnDirCachePathTemp) && is_writable($pepvnDirCachePathTemp)) { $pepvnCacheHashKeySaltTemp = self::$defaultParams['fullDomainName'] . $pepvnDirCachePathTemp; if (defined('WP_PEPVN_SITE_SALT')) { $pepvnCacheHashKeySaltTemp .= '_' . WP_PEPVN_SITE_SALT; } $pepvnCacheTimeoutTemp = 86400 * 6; PepVN_Data::$cachePermanentObject = new \WPOptimizeByxTraffic\Application\Service\PepVN_CacheSimpleFile(array('cache_timeout' => $pepvnCacheTimeoutTemp, 'hash_key_method' => 'crc32b', 'hash_key_salt' => hash('crc32b', md5($pepvnCacheHashKeySaltTemp)), 'gzcompress_level' => 2, 'key_prefix' => 'dtpm_', 'cache_dir' => $pepvnDirCachePathTemp)); } else { PepVN_Data::$cachePermanentObject = new \WPOptimizeByxTraffic\Application\Service\PepVN_CacheSimpleFile(array()); } /* //cacheByTagObject $pepvnDirCachePathTemp = WP_OPTIMIZE_BY_XTRAFFIC_PLUGIN_STORAGES_CACHE_DIR.'cbtg'.DIRECTORY_SEPARATOR; if(!is_dir($pepvnDirCachePathTemp)) { PepVN_Data::createFolder($pepvnDirCachePathTemp); } if(is_dir($pepvnDirCachePathTemp) && is_readable($pepvnDirCachePathTemp) && is_writable($pepvnDirCachePathTemp)) { $pepvnCacheHashKeySaltTemp = __FILE__ . PepVN_Data::$defaultParams['fullDomainName'] . $pepvnDirCachePathTemp; if(defined('WP_PEPVN_SITE_SALT')) { $pepvnCacheHashKeySaltTemp .= '_'.WP_PEPVN_SITE_SALT; } $pepvnCacheTimeoutTemp = 86400 * 6; PepVN_Data::$cacheByTagObject = new PepVN_Cache( array( 'cache_timeout' => $pepvnCacheTimeoutTemp //int : seconds ,'hash_key_method' => 'crc32b' ,'hash_key_salt' => hash('crc32b',md5($pepvnCacheHashKeySaltTemp)) ,'gzcompress_level' => 2 ,'key_prefix' => 'cbtg_dt_' ,'cache_methods' => array( 'file' => array( 'cache_timeout' => $pepvnCacheTimeoutTemp , 'cache_dir' => $pepvnDirCachePathTemp ), ) ) ); } else { PepVN_Data::$cacheByTagObject = new PepVN_Cache(array()); } //*/ }
public function clean_cache($data_type = ',common,') { $data_type = (array) $data_type; $data_type = implode(',', $data_type); $data_type = preg_replace('#[\\,\\;]+#is', ';', $data_type); $data_type = explode(';', $data_type); foreach ($data_type as $key1 => $value1) { $value1 = trim($value1); if (!$value1) { unset($data_type[$key1]); } } $data_type = array_unique($data_type); $data_type = array_values($data_type); $data_type = array_flip($data_type); $staticVarObject = $this->di->getShared('staticVar'); $staticVarData = $staticVarObject->get(); if (!isset($staticVarData['last_time_clean_cache_all'])) { $staticVarData['last_time_clean_cache_all'] = 0; } if ($staticVarData['last_time_clean_cache_all'] <= PepVN_Data::$defaultParams['requestTime'] - 86400) { //is timeout $data_type['all'] = 1; } if (isset($data_type['all'])) { $staticVarData['last_time_clean_cache_all'] = PepVN_Data::$defaultParams['requestTime']; $staticVarObject->save($staticVarData); } unset($staticVarData, $staticVarObject); $timestampNow = PepVN_Data::$defaultParams['requestTime']; $timestampNow = (int) $timestampNow; //clean all cache data short time (<= 1 day) if (PepVN_Data::$cacheDbObject) { PepVN_Data::$cacheDbObject->clean(array('clean_mode' => PepVN_Cache::CLEANING_MODE_ALL)); } if (PepVN_Data::$cacheObject) { PepVN_Data::$cacheObject->clean(array('clean_mode' => PepVN_CacheSimpleFile::CLEANING_MODE_ALL)); } global $wp_object_cache; if (isset($wp_object_cache) && $wp_object_cache) { if (is_object($wp_object_cache)) { $wp_object_cache->flush(); } } if (PepVN_Data::$cacheWpObject) { PepVN_Data::$cacheWpObject->clean(array('clean_mode' => PepVN_Cache::CLEANING_MODE_ALL)); } $cache = $this->di->getShared('cache'); $cache->flush(); /* * Clean all data in these folders */ $arrayPaths = array(); $keyTemp = WP_OPTIMIZE_BY_XTRAFFIC_PLUGIN_STORAGES_CACHE_DIR . 'db' . DIRECTORY_SEPARATOR; $arrayPaths[$keyTemp] = 1; foreach ($arrayPaths as $path1 => $value1) { unset($arrayPaths[$path1]); if ($path1) { $objects = System::scandir($path1); foreach ($objects as $objIndex => $objPath) { unset($objects[$objIndex]); if ($objPath) { if (is_file($objPath)) { if (is_readable($objPath) && is_writable($objPath)) { unlink($objPath); } } } } } } if (isset($data_type['cache_permanent']) || isset($data_type['all'])) { if (PepVN_Data::$cachePermanentObject) { PepVN_Data::$cachePermanentObject->clean(array('clean_mode' => PepVN_CacheSimpleFile::CLEANING_MODE_ALL)); } $cache = $this->di->getShared('cachePermanent'); $cache->flush(); } if (isset($data_type['cache_tag']) || isset($data_type['all'])) { if (PepVN_Data::$cacheByTagObject) { PepVN_Data::$cacheByTagObject->clean(array('clean_mode' => PepVN_Cache::CLEANING_MODE_ALL)); } } if (isset($data_type['all'])) { $arrayPaths = array(); $keyTemp = WP_CONTENT_PEPVN_DIR . 'cache' . DIRECTORY_SEPARATOR . 'static-files' . DIRECTORY_SEPARATOR; $arrayPaths[$keyTemp] = 24; //hours $keyTemp = WP_OPTIMIZE_BY_XTRAFFIC_PLUGIN_STORAGES_CACHE_DIR . 'images' . DIRECTORY_SEPARATOR; $arrayPaths[$keyTemp] = 24; //hours foreach ($arrayPaths as $path1 => $timeout) { unset($arrayPaths[$path1]); $timeout = (int) $timeout; $timeoutSeconds = $timeout * 3600; if ($path1) { $objects = System::scandir($path1); foreach ($objects as $objIndex => $objPath) { unset($objects[$objIndex]); if ($objPath) { if (is_file($objPath)) { if (is_readable($objPath) && is_writable($objPath)) { $fileatime = fileatime($objPath); if ($fileatime && $fileatime > 0) { if ($fileatime + $timeoutSeconds <= $timestampNow) { //is timeout unlink($objPath); } } else { $filemtime = filemtime($objPath); if ($filemtime && $filemtime > 0) { if ($filemtime + $timeoutSeconds <= $timestampNow) { //is timeout unlink($objPath); } } } } } } } } } } $wpExtend = $this->di->getShared('wpExtend'); $remote = $this->di->getShared('remote'); $arrayUrlNeedRequest = array(array('url' => $wpExtend->get_home_url(), 'config' => array('method' => 'PURGE', 'headers' => array('host' => PepVN_Data::$defaultParams['fullDomainName'], 'X-Purge-Method' => 'default'), 'timeout' => 1, 'redirection' => 1)), array('url' => ($wpExtend->is_ssl() ? 'https://' : 'http://') . '127.0.0.1/', 'config' => array('method' => 'PURGE', 'headers' => array('host' => PepVN_Data::$defaultParams['fullDomainName'], 'X-Purge-Method' => 'default'), 'timeout' => 1, 'redirection' => 1))); foreach ($arrayUrlNeedRequest as $value1) { $remote->request($value1['url'], $value1['config']); } $hook = $this->di->getShared('hook'); if ($hook->has_action('clean_cache')) { $hook->do_action('clean_cache'); } if ($wpExtend->is_admin()) { $adminNotice = $this->di->getShared('adminNotice'); $adminNotice->add_notice('<b>' . WP_OPTIMIZE_BY_XTRAFFIC_PLUGIN_NAME . '</b> : ' . 'All caches have been removed.', 'success'); } }
/** * Utility to normalize a string's encoding to UTF-32. */ public final function normalizeEncoding($str) { /** * mbstring is required here */ if (!System::function_exists("mb_convert_encoding")) { throw new Exception("Extension \\'mbstring\\' is required"); } /** * Convert to UTF-32 (4 byte characters, regardless of actual number of bytes in * the character). */ return mb_convert_encoding($str, "UTF-32", $this->detectEncoding($str)); }