Exemple #1
0
function pepvn_MinifyJavascript($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_MinifyJavascript', $input_data));
    $tmp = PepVN_Data::$cachePermanentObject->get_cache($keyCache1);
    if (null !== $tmp) {
        return $tmp;
    }
    $rsOne = PepVN_Data::escapeByPattern($input_data, array('pattern' => '#[\\+\\-]+[ \\t\\s]+[\\+\\-]+#is', 'target_patterns' => array(0), 'wrap_target_patterns' => '+'));
    $input_data = $rsOne['content'];
    unset($rsOne['content']);
    /*
    $pepVN_JavaScriptPacker = null;$pepVN_JavaScriptPacker = new PepVN_JavaScriptPacker($input_data, 'Normal', true, false);
    $input_data = $pepVN_JavaScriptPacker->pack();
    $pepVN_JavaScriptPacker=0;unset($pepVN_JavaScriptPacker);
    */
    $input_data = JSMin::minify($input_data);
    if (!PepVN_Data::isEmptyArray($rsOne['patterns'])) {
        $input_data = str_replace(array_values($rsOne['patterns']), array_keys($rsOne['patterns']), $input_data);
    }
    unset($rsOne);
    $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;
 }