function pepvn_MinifyHtml($input_data) { $input_data = (array) $input_data; $input_data = implode(PHP_EOL, $input_data); $input_data = (string) $input_data; $input_data = trim($input_data); $keyCache1 = Utils::hashKey(array('pepvn_MinifyHtml', $input_data)); $tmp = PepVN_Data::$cachePermanentObject->get_cache($keyCache1); if (null !== $tmp) { return $tmp; } $findAndReplace1 = array(); $rsOne = PepVN_Data::escapeHtmlTagsAndContents($input_data, 'pre;code;textarea;input'); $input_data = $rsOne['content']; unset($rsOne['content']); if (!empty($rsOne['patterns'])) { $findAndReplace1 = array_merge($findAndReplace1, $rsOne['patterns']); } unset($rsOne); $input_data = Minify_HTML::minify($input_data, array('jsCleanComments' => true, 'cssMinifier' => 'pepvn_MinifyCss', 'jsMinifier' => 'pepvn_MinifyJavascript')); $findAndReplace2 = array(); preg_match_all('#<(script|style)[^><]*?>.*?</\\1>#is', $input_data, $matched1); if (!empty($matched1[0])) { $matched1 = $matched1[0]; foreach ($matched1 as $key1 => $value1) { unset($matched1[$key1]); $findAndReplace2[$value1] = '__' . hash('crc32b', md5($value1)) . '__'; } } $matched1 = 0; if (!empty($findAndReplace2)) { $input_data = str_replace(array_keys($findAndReplace2), array_values($findAndReplace2), $input_data); $findAndReplace1 = array_merge($findAndReplace1, $findAndReplace2); unset($findAndReplace2); } $patterns1 = array('#>[\\s \\t]+<#is' => '><', '#[\\s \\t]+#is' => ' '); $input_data = preg_replace(array_keys($patterns1), array_values($patterns1), $input_data); if (!empty($findAndReplace1)) { $input_data = str_replace(array_values($findAndReplace1), array_keys($findAndReplace1), $input_data); unset($findAndReplace1); } $input_data = trim($input_data); PepVN_Data::$cachePermanentObject->set_cache($keyCache1, $input_data); return $input_data; }
public function process_text($input_text, $postId = 0) { $classMethodKey = crc32(__CLASS__ . __METHOD__); $options = self::getOption(); if (isset($options['optimize_traffic_modules']) && !PepVN_Data::isEmptyArray($options['optimize_traffic_modules'])) { } else { return $input_text; } $keyCacheProcessText = Utils::hashKey(array($classMethodKey, $input_text, 'process_text', $options['optimize_traffic_modules'], $postId)); $tmp = TempDataAndCacheFile::get_cache($keyCacheProcessText, true); if (null !== $tmp) { return $tmp; } $wpExtend = $this->di->getShared('wpExtend'); global $wpdb; $postId = (int) $postId; if ($postId) { $post = $wpExtend->get_post($post_id); } else { global $post; } $patternsEscaped1 = array(); $rsOne = PepVN_Data::escapeByPattern($input_text, array('pattern' => '#<([a-z]+)[^><]+class=(\'|\\")[^><\'\\"]*?wp\\-caption[^><\'\\"]*?\\2[^><]*?>.*?</\\1>#is', 'target_patterns' => array(0), 'wrap_target_patterns' => '')); $input_text = $rsOne['content']; if (!empty($rsOne['patterns'])) { $patternsEscaped1 = array_merge($patternsEscaped1, $rsOne['patterns']); } unset($rsOne); $rsOne = PepVN_Data::escapeHtmlTagsAndContents($input_text, 'a;table;pre;ol;ul;blockquote'); $input_text = $rsOne['content']; if (!empty($rsOne['patterns'])) { $patternsEscaped1 = array_merge($patternsEscaped1, $rsOne['patterns']); } unset($rsOne); $original_InputText1 = $input_text; $post->ID = (int) $post->ID; $rsGetTerms = $wpExtend->getTermsByPostId($post->ID); $rsGetTerms2 = $rsGetTerms; $rsGetTerms2 = array_keys($rsGetTerms2); $rsGetTerms2 = $this->_clean_terms($rsGetTerms2); $postExcerpt1 = $post->post_content; $postExcerpt1 = PepVN_Data::mb_substr($postExcerpt1, 0, 360); $allPostTextCombined = $post->post_title . ' ' . PHP_EOL . ' ' . $post->post_excerpt . ' ' . PHP_EOL . implode(' ', $rsGetTerms2) . ' ' . PHP_EOL . ' ' . $post->post_content; $allPostTextCombined = $this->_remove_escaped_string($allPostTextCombined); $patternsModulesReplaceText = array(); $groupModules_ByModuleType = array(); $groupModules_PositionsAddedToQueue = array(); $groupModules_ByFixedTypeBeginOrEnd = array(); foreach ($options['optimize_traffic_modules'] as $keyOne => $valueOne) { if (isset($valueOne['module_type']) && $valueOne['module_type']) { if (isset($valueOne['module_position'])) { if (!in_array($valueOne['module_position'], $groupModules_PositionsAddedToQueue)) { $valueOne['module_mumber_of_items'] = abs((int) $valueOne['module_mumber_of_items']); if ($valueOne['module_mumber_of_items'] < 1) { $valueOne['module_mumber_of_items'] = 1; } else { if ($valueOne['module_mumber_of_items'] > 10) { $valueOne['module_mumber_of_items'] = 10; } } $groupModules_PositionsAddedToQueue[] = $valueOne['module_position']; $groupModules_ByModuleType[$valueOne['module_type']][$valueOne['module_position']] = $valueOne; } } } } $numberElementContentInText = 0; preg_match_all('/<(p|h1|h2|h3|h4|h5|h6)(\\s+[^><]*?)?>.*?<\\/\\1>/is', $original_InputText1, $matchedElementContentInText); if (isset($matchedElementContentInText[0]) && $matchedElementContentInText[0]) { if (!empty($matchedElementContentInText[0])) { foreach ($matchedElementContentInText[0] as $key1 => $value1) { $valueTemp1 = $value1; $valueTemp1 = AnalyzeText::cleanRawTextForProcessSearch($valueTemp1); $checkStatus2 = false; if ($valueTemp1) { $valueTemp2 = explode(' ', $valueTemp1); if (count($valueTemp2) > 5) { $checkStatus2 = true; } else { if (preg_match('#<(h1|h2|h3|h4|h5|h6)(\\s+[^><]*?)?>.*?</\\1>#is', $value1, $matched1)) { $checkStatus2 = true; } } } if (!$checkStatus2) { unset($matchedElementContentInText[0][$key1]); } } } $numberElementContentInText = count($matchedElementContentInText[0]); } if (isset($groupModules_ByModuleType['flyout']) && $groupModules_ByModuleType['flyout'] && !empty($groupModules_ByModuleType['flyout'])) { foreach ($groupModules_ByModuleType['flyout'] as $keyOne => $valueOne) { if (isset($valueOne['module_type'])) { $postsIdsFound1 = array(); $rsSearchPost1 = $this->search_post_by_text($allPostTextCombined, array('group_text_weight' => array(array('text' => $post->post_title, 'weight' => 20), array('text' => $post->post_excerpt, 'weight' => 2), array('text' => implode(' ', $rsGetTerms2), 'weight' => 30), array('text' => $postExcerpt1, 'weight' => 2)), 'exclude_posts_ids' => array($post->ID), 'post_id_less_than' => $post->ID, 'limit' => $valueOne['module_mumber_of_items'], 'key_cache' => $valueOne['module_type'] . '_' . $valueOne['module_position'])); if ($rsSearchPost1) { foreach ($rsSearchPost1 as $keyTwo => $valueTwo) { $postsIdsFound1[] = $valueTwo['post_id']; } } if ($postsIdsFound1 && !empty($postsIdsFound1)) { $rsCreateTrafficModule1 = $this->create_traffic_module(array('option' => $valueOne, 'data' => array('posts_ids' => $postsIdsFound1))); if ($rsCreateTrafficModule1['module']) { $input_text .= ' ' . $rsCreateTrafficModule1['module']; } } } } } if (isset($groupModules_ByModuleType['fixed']) && !PepVN_Data::isEmptyArray($groupModules_ByModuleType['fixed'])) { foreach ($groupModules_ByModuleType['fixed'] as $keyOne => $valueOne) { if (isset($valueOne['module_position'])) { $valueOne['module_position'] = (int) $valueOne['module_position']; if (0 == $valueOne['module_position'] || 100 == $valueOne['module_position']) { $groupModules_ByFixedTypeBeginOrEnd[$valueOne['module_position']] = $valueOne; unset($groupModules_ByModuleType['fixed'][$keyOne]); } } } if ($groupModules_ByFixedTypeBeginOrEnd && !empty($groupModules_ByFixedTypeBeginOrEnd)) { ksort($groupModules_ByFixedTypeBeginOrEnd); foreach ($groupModules_ByFixedTypeBeginOrEnd as $keyOne => $valueOne) { if (isset($valueOne['module_position'])) { $valueOne['module_position'] = (int) $valueOne['module_position']; $postsIdsFound1 = array(); $rsSearchPost1 = $this->search_post_by_text($allPostTextCombined, array('group_text_weight' => array(array('text' => $post->post_title, 'weight' => 20), array('text' => $post->post_excerpt, 'weight' => 2), array('text' => implode(' ', $rsGetTerms2), 'weight' => 30), array('text' => $postExcerpt1, 'weight' => 2)), 'exclude_posts_ids' => array($post->ID), 'post_id_less_than' => $post->ID, 'limit' => $valueOne['module_mumber_of_items'], 'key_cache' => $valueOne['module_type'] . '_' . $valueOne['module_position'])); if ($rsSearchPost1) { foreach ($rsSearchPost1 as $keyTwo => $valueTwo) { $postsIdsFound1[] = $valueTwo['post_id']; } } if (!empty($postsIdsFound1)) { $rsCreateTrafficModule1 = $this->create_traffic_module(array('option' => $valueOne, 'data' => array('posts_ids' => $postsIdsFound1))); if ($rsCreateTrafficModule1['module']) { if (0 == $valueOne['module_position']) { $input_text = $rsCreateTrafficModule1['module'] . ' ' . $input_text; } else { if (100 == $valueOne['module_position']) { $input_text .= ' ' . $rsCreateTrafficModule1['module']; } } } } } } } if ($numberElementContentInText > 0) { ksort($groupModules_ByModuleType['fixed']); $arrayMatchedElementContentInTextIsProcessed = array(); foreach ($groupModules_ByModuleType['fixed'] as $keyOne => $valueOne) { if (isset($valueOne['module_position'])) { $valueOne['module_position'] = (int) $valueOne['module_position']; $originalTextNeedProcess1 = ''; $rawTextNeedProcess1 = ''; $iNumber1 = 0; foreach ($matchedElementContentInText[0] as $keyTwo => $valueTwo) { if (!in_array($valueTwo, $arrayMatchedElementContentInTextIsProcessed)) { $originalTextNeedProcess1 .= ' ' . $valueTwo; } $iNumber1++; $currentPercentPos = $iNumber1 / $numberElementContentInText * 100; $currentPercentPos = (int) $currentPercentPos; if ($currentPercentPos >= $valueOne['module_position'] && $originalTextNeedProcess1) { if (preg_match('#<(p)(\\s+[^><]*?)?>.*?</\\1>#is', $valueTwo, $matched2)) { $originalTextNeedProcess1 = $this->_remove_escaped_string($originalTextNeedProcess1); $postsIdsFound1 = array(); $rsSearchPost1 = $this->search_post_by_text($originalTextNeedProcess1, array('group_text_weight' => array(array('text' => $post->post_title, 'weight' => 20), array('text' => $post->post_excerpt, 'weight' => 2), array('text' => implode(' ', $rsGetTerms2), 'weight' => 30), array('text' => $originalTextNeedProcess1, 'weight' => 2)), 'exclude_posts_ids' => array($post->ID), 'post_id_less_than' => $post->ID, 'limit' => $valueOne['module_mumber_of_items'], 'key_cache' => $valueOne['module_type'] . '_' . $valueOne['module_position'])); if ($rsSearchPost1) { foreach ($rsSearchPost1 as $keyThree => $valueThree) { $postsIdsFound1[] = $valueThree['post_id']; } } if ($postsIdsFound1 && !empty($postsIdsFound1)) { $rsCreateTrafficModule1 = $this->create_traffic_module(array('option' => $valueOne, 'data' => array('posts_ids' => $postsIdsFound1))); if ($rsCreateTrafficModule1['module']) { $originalTextNeedProcess1 = ''; $patternsModulesReplaceText_K = $valueTwo; $patternsModulesReplaceText_V = $valueTwo . ' ' . $rsCreateTrafficModule1['module']; $patternsModulesReplaceText[$patternsModulesReplaceText_K] = $patternsModulesReplaceText_V; break; } } } } } } } $arrayMatchedElementContentInTextIsProcessed = 0; } } if (!empty($patternsModulesReplaceText)) { $tmp = array(); foreach ($patternsModulesReplaceText as $key1 => $value1) { unset($patternsModulesReplaceText[$key1]); $key1 = '#' . Utils::preg_quote($key1) . '#'; $tmp[$key1] = $value1; } if (!empty($tmp)) { $input_text = preg_replace(array_keys($tmp), array_values($tmp), $input_text, 1); } unset($tmp); } unset($patternsModulesReplaceText); if (!empty($patternsEscaped1)) { $input_text = str_replace(array_values($patternsEscaped1), array_keys($patternsEscaped1), $input_text); } unset($patternsEscaped1); TempDataAndCacheFile::set_cache($keyCacheProcessText, $input_text, true); return $input_text; }
public function process_text($text) { $options = self::getOption(); $classMethodKey = Hash::crc32b(__CLASS__ . '_' . __METHOD__); $textChecksum = Utils::hashKey($text); $keyCacheProcessText = Utils::hashKey(array($classMethodKey, $textChecksum, 'keyCacheProcessText', $options)); $tmp = TempDataAndCacheFile::get_cache($keyCacheProcessText, true); if (null !== $tmp) { return $tmp; } global $post; $wpExtend = $this->di->getShared('wpExtend'); $analyzeText = $this->di->getShared('analyzeText'); $rsGetTerms = $wpExtend->getTermsByPostId($post->ID); $autolinks_case_sensitive = false; if (isset($options['autolinks_case_sensitive']) && 'on' === $options['autolinks_case_sensitive']) { $autolinks_case_sensitive = true; } $maxlinks = (int) $options['maxlinks']; if ($maxlinks < 0) { $maxlinks = 0; } $currentPostId = 0; if (isset($post->ID) && $post->ID) { $currentPostId = (int) $post->ID; } $currentPostType = ''; if (isset($post->post_type) && $post->post_type) { $currentPostType = $post->post_type; } $patternsEscaped = array(); $rsOne = PepVN_Data::escapeHtmlTagsAndContents($text, 'a;pre;script;style;link;meta;input;textarea;iframe;video;audio;object'); $text = $rsOne['content']; if (!empty($rsOne['patterns'])) { $patternsEscaped = array_merge($patternsEscaped, $rsOne['patterns']); } unset($rsOne); if (isset($options['exclude_heading']) && 'on' === $options['exclude_heading']) { //escape a and h1 -> h6 $rsOne = PepVN_Data::escapeHtmlTagsAndContents($text, 'a;h1;h2;h3;h4;h5;h6'); $text = $rsOne['content']; if (!empty($rsOne['patterns'])) { $patternsEscaped = array_merge($patternsEscaped, $rsOne['patterns']); } unset($rsOne); } $rsOne = PepVN_Data::escapeHtmlTags($text); $text = $rsOne['content']; if (!empty($rsOne['patterns'])) { $patternsEscaped = array_merge($patternsEscaped, $rsOne['patterns']); } unset($rsOne); $text = ' ' . trim($text) . ' '; $group_keywords1 = $this->_get_data_custom_keywords(); $numberTotalLinksAdded = 0; $targetPostTypesForSearch = array('post', 'page'); if ($group_keywords1) { if (!empty($group_keywords1)) { //calculate weights of keywords $group_keywords2 = array_keys($group_keywords1); if (!$autolinks_case_sensitive) { $group_keywords2 = implode(';', $group_keywords2); $group_keywords2 = PepVN_Data::strtolower($group_keywords2); $group_keywords2 = $analyzeText->frequencyOfAppearanceKeywordsInText($group_keywords2, PepVN_Data::strtolower($text)); } else { $group_keywords2 = implode(';', $group_keywords2); $group_keywords2 = $analyzeText->frequencyOfAppearanceKeywordsInText($group_keywords2, $text); } if (!empty($group_keywords2)) { arsort($group_keywords2); $numberTotalLinks = 0; foreach ($group_keywords2 as $key1 => $value1) { if ($maxlinks > 0) { if ($numberTotalLinksAdded >= $maxlinks) { break; } } $targetKeywordClean = PepVN_Data::strtolower(PepVN_Data::cleanKeyword($key1)); $checkStatus1 = false; $targetLink1 = false; if (isset($group_keywords1[$key1])) { $targetLink2 = false; $targetLinkTitle2 = false; if ($group_keywords1[$key1] && !empty($group_keywords1[$key1])) { $targetLinks1 = $group_keywords1[$key1]; if (!empty($targetLinks1)) { shuffle($targetLinks1); foreach ($targetLinks1 as $key2 => $value2) { $value2 = trim($value2); if ($value2) { if (!isset(PepVN_Data::$cacheData[$classMethodKey]['linksAdded'][$value2])) { $targetLink2 = $value2; $targetLinkTitle2 = $key1; break; } } } } } if (!$targetLink2) { if ($targetPostTypesForSearch && !empty($targetPostTypesForSearch)) { $rsTwo = $analyzeText->search_posts(array('keyword' => $key1, 'post_types' => $targetPostTypesForSearch)); foreach ($rsTwo as $keyTwo => $valueTwo) { unset($rsTwo[$keyTwo]); $checkStatus2 = false; if ($valueTwo['post_id'] != $currentPostId) { $checkStatus2 = true; } else { if ($currentPostType === 'post') { if ('on' === $options['link_to_postself']) { $checkStatus2 = true; } } else { if ($currentPostType === 'page') { if ('on' === $options['link_to_pageself']) { $checkStatus2 = true; } } } } if ($checkStatus2) { if (isset(PepVN_Data::$cacheData[$classMethodKey]['linksAdded'][$valueTwo['post_link']])) { $checkStatus2 = false; } } if ($checkStatus2) { $targetLink2 = $valueTwo['post_link']; $targetLinkTitle2 = $valueTwo['post_title']; break; } } unset($rsTwo); } } if ($targetLink2) { $patterns2 = '#([\\s ,;\\.\\t]+)(' . Utils::preg_quote($key1) . ')([\\s ,;\\.\\t]+)#'; if (!$autolinks_case_sensitive) { $patterns2 .= 'i'; } $replace2 = '\\1<a href="' . $targetLink2 . '" ' . ('on' === $options['autolinks_new_window'] ? ' target="_bank" ' : '') . ' title="'; if ($targetLinkTitle2) { $targetLinkTitle2 = PepVN_Data::cleanKeyword($targetLinkTitle2); } if ($targetLinkTitle2) { $replace2 .= $targetLinkTitle2 . '">'; } else { $replace2 .= '\\2">'; } $replace2 .= '<strong>\\2</strong></a>\\3'; $text = preg_replace($patterns2, $replace2, $text, 1, $count2); $count2 = (int) $count2; if ($count2 > 0) { PepVN_Data::$cacheData[$classMethodKey]['linksAdded'][$targetLink2] = 1; PepVN_Data::$cacheData[$classMethodKey]['keywordsAdded'][$targetKeywordClean] = 1; $rsTwo = PepVN_Data::escapeHtmlTagsAndContents($text, 'a;strong'); $text = $rsTwo['content']; if (!empty($rsTwo['patterns'])) { $patternsEscaped = array_merge($patternsEscaped, $rsTwo['patterns']); } unset($rsTwo); $numberTotalLinksAdded += $count2; if ($maxlinks > 0) { if ($numberTotalLinksAdded >= $maxlinks) { break; } } } } } } } } } if (!empty($patternsEscaped)) { $text = str_replace(array_values($patternsEscaped), array_keys($patternsEscaped), $text); } unset($patternsEscaped); $text = $this->process_attributes_links($text); $text = trim($text); TempDataAndCacheFile::set_cache($keyCacheProcessText, $text, true); return $text; }