コード例 #1
0
 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;
 }