public function process_text($text) { $device_screen_width = $this->_check_get_screen_width(); $options = self::getOption(); if (!isset($options['optimize_images_alttext'])) { $options['optimize_images_alttext'] = ''; } if (!isset($options['optimize_images_titletext'])) { $options['optimize_images_titletext'] = ''; } $optimize_images_alttext = trim($options['optimize_images_alttext']); $optimize_images_titletext = trim($options['optimize_images_titletext']); if ('on' !== $options['optimize_images_optimize_image_file_enable'] && !$optimize_images_alttext && !$optimize_images_titletext) { return $text; } global $wpdb, $post; $classMethodKey = crc32(__CLASS__ . __METHOD__); $currentPostId = 0; if (isset($post->ID) && $post->ID) { $currentPostId = $post->ID; } $currentPostId = (int) $currentPostId; if (!$currentPostId) { if (System::function_exists('get_the_ID')) { $tmp = get_the_ID(); $tmp = (int) $tmp; if ($tmp > 0) { $currentPostId = $tmp; } } } $parametersPrimary = array(); $patternsEscaped = array(); $patternsReplace = array(); $patternsReplaceImgSrc = array(); $patternsPregReplace = array(); $rsSettingWatermarksFirstOptions = $this->parse_watermarks_first_options(array('options' => $options)); $paramsWatermarkOptions = $rsSettingWatermarksFirstOptions['paramsWatermarkOptions']; $options = $rsSettingWatermarksFirstOptions['options']; unset($rsSettingWatermarksFirstOptions); $options['optimize_images_rename_img_filename_value'] = trim($options['optimize_images_rename_img_filename_value']); $keyCacheProcessText = Utils::hashKey(array($classMethodKey, $text, 'process_text', 'dvsw_' . $device_screen_width, $options)); $tmp = TempDataAndCacheFile::get_cache($keyCacheProcessText, true); if (null !== $tmp) { return $tmp; } //Begin process image file if ('on' === $options['optimize_images_optimize_image_file_enable'] && $this->_isSystemReadyToHandleImagesStatus) { preg_match_all('#<img[^<>]+(\'|\\")(https?:)?//' . Utils::preg_quote(PepVN_Data::$defaultParams['fullDomainName']) . '[^\'\\"]+\\.(' . implode('|', $this->imageExtensionsAllow) . ')\\??[^\'\\"]*\\1#is', $text, $matched1); if (isset($matched1[0]) && $matched1[0] && !empty($matched1[0])) { $matched1 = $matched1[0]; foreach ($matched1 as $key1 => $value1) { unset($matched1[$key1]); if ($value1) { preg_match('#(\'|\\")((https?:)?//[^\\"\']+)\\1#i', $value1, $matched2); if (isset($matched2[2]) && $matched2[2]) { $matched2 = trim($matched2[2]); if ($matched2) { $imgName1 = WP_OPTIMIZE_BY_XTRAFFIC_PLUGIN_SLUG; $imgInfo1 = pathinfo($matched2); if (isset($imgInfo1['filename'])) { $imgName1 = $imgInfo1['filename']; } $processImageOptions1 = array('optimized_image_file_name' => trim($imgName1), 'original_image_src' => $matched2, 'action' => 'do_process_image'); if ($device_screen_width > 0) { $processImageOptions1['resize_max_width'] = $device_screen_width; } $rsProcessImage1 = $this->process_image($processImageOptions1); unset($processImageOptions1); if ($rsProcessImage1['image_optimized_file_url']) { $valueTemp1 = '//' . PepVN_Data::removeProtocolUrl($matched2); $valueTemp2 = '//' . PepVN_Data::removeProtocolUrl($rsProcessImage1['image_optimized_file_url']); if ($valueTemp1 !== $valueTemp2) { $patternsReplaceImgSrc[$valueTemp1] = $valueTemp2; } unset($valueTemp1, $valueTemp2); } unset($rsProcessImage1); } } } } } } unset($matched1); //Begin process image's attributes if (preg_match_all('#<img[^>]+\\\\?>#i', $text, $matched1)) { if (isset($matched1[0]) && is_array($matched1[0]) && !empty($matched1[0])) { $matched1 = $matched1[0]; foreach ($matched1 as $keyOne => $valueOne) { unset($matched1[$keyOne]); $oldImgTag = $valueOne; $newImgTag = $valueOne; $imgTitle1 = ''; $imgAlt1 = ''; $imgName = ''; $imgFullName = ''; $imgSrc = ''; $imgInfo = false; $matched2 = 0; if (preg_match('#title=(\'|")([^"\']+)\\1#i', $valueOne, $matched2)) { if (isset($matched2[2]) && $matched2[2]) { $imgTitle1 = trim($matched2[2]); } } $matched2 = 0; if (preg_match('#alt=(\'|")([^"\']+)\\1#i', $valueOne, $matched2)) { if (isset($matched2[2]) && $matched2[2]) { $imgAlt1 = trim($matched2[2]); } } $matched2 = 0; if (preg_match('#src=(\'|")(https?://[^"\']+)\\1#i', $valueOne, $matched2)) { if (isset($matched2[2]) && $matched2[2]) { $imgSrc = trim($matched2[2]); $imgName = trim($matched2[2]); } } $matched2 = 0; $imgName = trim($imgName); if ($imgName) { $imgInfo = pathinfo($imgName); if (isset($imgInfo['filename'])) { $imgName = $imgInfo['filename']; $imgFullName = $imgInfo['basename']; } } $imgName = trim($imgName); $imgTitle1 = PepVN_Data::cleanKeyword($imgTitle1); $imgAlt1 = PepVN_Data::cleanKeyword($imgAlt1); $imgName = PepVN_Data::cleanKeyword($imgName); if ($imgAlt1 && 'on' === $options['optimize_images_override_alt'] || !$imgAlt1) { $optimize_images_alttext1 = $this->parse_custom_tag_of_image(array('raw_tag' => $optimize_images_alttext, 'post_id' => $currentPostId, 'patterns' => array('img_name' => $imgName, 'img_title' => $imgTitle1, 'img_alt' => $imgAlt1))); if ($optimize_images_alttext1) { $newImgTag = preg_replace('#alt=(\'|")([^"\']+)?\\1#i', '', $newImgTag); $newImgTag = preg_replace('#<img(.+)#is', '<img alt="' . $optimize_images_alttext1 . '" \\1', $newImgTag); } } if ($imgTitle1 && 'on' === $options['optimize_images_override_alt'] || !$imgTitle1) { $optimize_images_titletext1 = $this->parse_custom_tag_of_image(array('raw_tag' => $optimize_images_titletext, 'post_id' => $currentPostId, 'patterns' => array('img_name' => $imgName, 'img_title' => $imgTitle1, 'img_alt' => $imgAlt1))); if ($optimize_images_titletext1) { $newImgTag = preg_replace('#title=(\'|")([^"\']+)?\\1#i', '', $newImgTag); $newImgTag = preg_replace('#<img(.+)#is', '<img title="' . $optimize_images_titletext1 . '" \\1', $newImgTag); } } if ($imgSrc) { //Begin Process Image File $checkStatus2 = false; if ($this->_isSystemReadyToHandleImagesStatus) { if (Utils::isUrl($imgSrc)) { $checkStatus2 = true; } } if ($checkStatus2) { $imgNewName = $imgName; if ($options['optimize_images_rename_img_filename_value']) { $optimize_images_rename_img_filename_value = $options['optimize_images_rename_img_filename_value']; $optimize_images_rename_img_filename_value = $this->parse_custom_tag_of_image(array('raw_tag' => $optimize_images_rename_img_filename_value, 'post_id' => $currentPostId, 'patterns' => array('img_name' => $imgName, 'img_title' => $imgTitle1, 'img_alt' => $imgAlt1))); if ($optimize_images_rename_img_filename_value) { $imgNewName = $optimize_images_rename_img_filename_value; } } $max_size_image_tag = $this->get_max_size_image_tag($oldImgTag); $imgOptimizedFilePathExists1 = false; $processImageOptions1 = array('optimized_image_file_name' => $imgNewName, 'original_image_src' => $imgSrc, 'options' => $options, 'paramsWatermarkOptions' => $paramsWatermarkOptions); if ($max_size_image_tag['width'] > 0) { $processImageOptions1['resize_max_width'] = $max_size_image_tag['width']; } if ($max_size_image_tag['height'] > 0) { $processImageOptions1['resize_max_height'] = $max_size_image_tag['height']; } $rsProcessImage1 = $this->process_image($processImageOptions1); unset($processImageOptions1); if ($rsProcessImage1['image_optimized_file_path']) { $imgOptimizedFilePathExists1 = $rsProcessImage1['image_optimized_file_path']; } if (false === $imgOptimizedFilePathExists1) { if (isset($options['optimize_images_optimize_image_file_enable']) && 'on' === $options['optimize_images_optimize_image_file_enable']) { $processImageOptions1 = array('optimized_image_file_name' => $imgNewName, 'original_image_src' => $imgSrc, 'options' => $options, 'paramsWatermarkOptions' => $paramsWatermarkOptions, 'action' => 'do_process_image'); if ($max_size_image_tag['width'] > 0) { $processImageOptions1['resize_max_width'] = $max_size_image_tag['width']; } if ($max_size_image_tag['height'] > 0) { $processImageOptions1['resize_max_height'] = $max_size_image_tag['height']; } $rsProcessImage1 = $this->process_image($processImageOptions1); unset($processImageOptions1); if ($rsProcessImage1['image_optimized_file_path']) { $imgOptimizedFilePathExists1 = $rsProcessImage1['image_optimized_file_path']; } } } if ($imgOptimizedFilePathExists1) { if (is_file($imgOptimizedFilePathExists1)) { if (filesize($imgOptimizedFilePathExists1) > 0) { $rs_getimagesize1 = getimagesize($imgOptimizedFilePathExists1); if (isset($rs_getimagesize1[0]) && $rs_getimagesize1[0]) { $newImgTag = $this->check_set_size_image_tag($newImgTag, array('width' => $rs_getimagesize1[0], 'height' => $rs_getimagesize1[1]), true); $imgSrc2 = str_replace($this->_folderStorePath, $this->_folderStoreUrl, $imgOptimizedFilePathExists1); $newImgTag = str_replace($imgSrc, $imgSrc2, $newImgTag); $patternsReplaceImgSrc[$imgSrc] = $imgSrc2; } } } } } } if ($oldImgTag !== $newImgTag) { $patternsReplace[$oldImgTag] = $newImgTag; } } } if ($patternsReplace && !empty($patternsReplace)) { $text = str_replace(array_keys($patternsReplace), array_values($patternsReplace), $text); } unset($patternsReplace); if ($patternsPregReplace && !empty($patternsPregReplace)) { $text = preg_replace(array_keys($patternsPregReplace), array_values($patternsPregReplace), $text); } unset($patternsPregReplace); if ($patternsReplaceImgSrc && !empty($patternsReplaceImgSrc)) { $patternsReplaceImgSrc2 = array(); foreach ($patternsReplaceImgSrc as $keyOne => $valueOne) { unset($patternsReplaceImgSrc[$keyOne]); if ($keyOne && $valueOne) { $tmp1 = '#([\'\\"\\s \\t]*?)' . PepVN_Data::preg_quote($keyOne) . '([\'\\"\\s \\t]*?)#is'; $tmp2 = '\\1' . $valueOne . '\\2'; $patternsReplaceImgSrc2[$tmp1] = $tmp2; } } if (!empty($patternsReplaceImgSrc2)) { $text = preg_replace(array_keys($patternsReplaceImgSrc2), array_values($patternsReplaceImgSrc2), $text); } } } /* if('on' === $options['optimize_images_images_lazy_load_enable']) { //$text = $this->process_allimagestags_lazyload($text,0); } */ $text = trim($text); TempDataAndCacheFile::set_cache($keyCacheProcessText, $text, true); return $text; }
public function cleanArrayKeywords($keywords) { $resultData = array(); $keywords = (array) $keywords; $keywords = implode(';', $keywords); $keywords = preg_replace('#[\\,\\;]+#is', ';', $keywords); $keywords = explode(';', $keywords); foreach ($keywords as $key1 => $value1) { unset($keywords[$key1]); $value1 = PepVN_Data::cleanKeyword($value1); if ($value1) { $resultData[] = $value1; } } return $resultData; }
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; }