Beispiel #1
0
    public function find_related_posts($input_parameters)
    {
        $resultData = array();
        $classMethodKey = Hash::crc32b(__CLASS__ . __METHOD__);
        if (!isset($input_parameters['post_types'])) {
            $input_parameters['post_types'] = array('post', 'page');
        }
        $input_parameters['post_types'] = PepVN_Data::cleanArray($input_parameters['post_types']);
        if (empty($input_parameters['post_types'])) {
            return $resultData;
        }
        /*
        	input_parameters['keywords'] = array(
        		keyword a => (float)w
        	);
        */
        $keywords = array();
        $valueTemp = (array) $input_parameters['keywords'];
        foreach ($valueTemp as $key1 => $value1) {
            $key1 = preg_replace('#[\'\\"\\\\]+#i', ' ', $key1);
            $key1 = preg_replace('#[\\s]+#is', ' ', $key1);
            $key1 = self::cleanRawTextForProcessSearch($key1);
            $key1 = trim($key1);
            if ($key1) {
                $keywords[$key1] = (double) $value1;
            }
        }
        if (empty($keywords)) {
            return $resultData;
        }
        global $wpdb;
        $options = self::getOption();
        $keywords = array_slice($keywords, 0, 30);
        //max 30 keywords
        $input_parameters['keywords'] = $keywords;
        unset($keywords);
        if (!isset($input_parameters['limit'])) {
            $input_parameters['limit'] = 10;
        }
        $input_parameters['limit'] = (int) $input_parameters['limit'];
        if (!isset($input_parameters['exclude_posts_ids'])) {
            $input_parameters['exclude_posts_ids'] = array();
        }
        $input_parameters['exclude_posts_ids'] = (array) $input_parameters['exclude_posts_ids'];
        foreach ($input_parameters['exclude_posts_ids'] as $key1 => $value1) {
            $input_parameters['exclude_posts_ids'][$key1] = (int) $value1;
        }
        $input_parameters['exclude_posts_ids'] = array_unique($input_parameters['exclude_posts_ids']);
        arsort($input_parameters['exclude_posts_ids']);
        $input_parameters['exclude_posts_ids'] = array_values($input_parameters['exclude_posts_ids']);
        if (!isset($input_parameters['post_id_less_than'])) {
            $input_parameters['post_id_less_than'] = 0;
        }
        $input_parameters['post_id_less_than'] = (int) $input_parameters['post_id_less_than'];
        $keyCache1 = array($classMethodKey, $input_parameters);
        if (!isset($options['db_has_fulltext_status'])) {
            $options['db_has_fulltext_status'] = 'n';
        }
        if (isset($options['db_has_fulltext_status']) && 'y' === $options['db_has_fulltext_status']) {
            $keyCache1[] = 'db_has_fulltext_status';
        }
        $keyCache1 = Utils::hashKey($keyCache1);
        $resultData = TempDataAndCacheFile::get_cache($keyCache1, true);
        if (null === $resultData) {
            $resultData = array();
            $wpExtend = $this->di->getShared('wpExtend');
            $weightOfFields = self::$_weightOfFields;
            $tablePostName = $wpdb->posts;
            $combinedKeywords = array('kw' => '', 'w' => 0);
            foreach ($input_parameters['keywords'] as $key1 => $value1) {
                $combinedKeywords['kw'] .= ' ' . $key1;
                $combinedKeywords['w'] += $value1;
            }
            $combinedKeywords['kw'] = trim($combinedKeywords['kw']);
            if ($combinedKeywords['kw']) {
                $combinedKeywords['w'] = $combinedKeywords['w'] / count($input_parameters['keywords']);
                $input_parameters['keywords'][$combinedKeywords['kw']] = $combinedKeywords['w'];
            }
            $queryString_Where_PostType = array();
            foreach ($input_parameters['post_types'] as $keyOne => $valueOne) {
                if ($valueOne) {
                    $valueOne = trim($valueOne);
                    if ($valueOne) {
                        $queryString_Where_PostType[] = ' ( `' . $tablePostName . '`.`post_type` = "' . $valueOne . '" ) ';
                    }
                }
            }
            $queryString_Where_PostType = implode(' OR ', $queryString_Where_PostType);
            $queryString_Where_PostType = trim($queryString_Where_PostType);
            $totalKeywordWeight = 1;
            if ('y' === $options['db_has_fulltext_status']) {
                $weightOfFields['post_content'] = 30;
                $arrayScoreMatchString = array();
                foreach ($input_parameters['keywords'] as $keyword => $keywordWeight) {
                    foreach ($weightOfFields as $field => $weight) {
                        $arrayScoreMatchString[] = ' ( ( ( MATCH(`' . $tablePostName . '`.`' . $field . '`) AGAINST("' . $keyword . '" IN NATURAL LANGUAGE MODE) ) * ' . $weightOfFields[$field] . ' ) * ' . (double) $keywordWeight . ')';
                    }
                    $totalKeywordWeight += $keywordWeight / 1.5;
                }
                $queryString1 = '
SELECT `' . $tablePostName . '`.`ID` , `' . $tablePostName . '`.`post_title` , `' . $tablePostName . '`.`post_content` , (
	(' . implode(' + ', $arrayScoreMatchString) . ') / 1
) AS wpxtraffic_score
FROM ' . $tablePostName . '
WHERE ( ( `' . $tablePostName . '`.`post_status` = "publish" ) AND !(`' . $tablePostName . '`.`post_password` > "") 
	';
                if ($queryString_Where_PostType) {
                    $queryString1 .= ' AND ( ' . $queryString_Where_PostType . ' ) ';
                }
                if (!empty($input_parameters['exclude_posts_ids'])) {
                    $queryString1 .= ' AND ( `' . $tablePostName . '`.`ID` NOT IN (' . implode(',', $input_parameters['exclude_posts_ids']) . ') ) ';
                }
                if ($input_parameters['post_id_less_than'] > 1 && $input_parameters['post_id_less_than'] > $input_parameters['limit']) {
                    $queryString1 .= ' AND ( `' . $tablePostName . '`.`ID` < ' . $input_parameters['post_id_less_than'] . ' ) ';
                }
                $queryString1 .= ' ) ';
                $queryString1 .= ' 
GROUP BY `' . $tablePostName . '`.`ID`
ORDER BY wpxtraffic_score DESC 
';
            } else {
                $arrayScoreMatchString = array();
                foreach ($input_parameters['keywords'] as $keyword => $keywordWeight) {
                    $keywordLength = strlen($keyword);
                    foreach ($weightOfFields as $field => $weight) {
                        $arrayScoreMatchString[] = ' ( ( IFNULL((ROUND((LENGTH(LOWER(`' . $tablePostName . '`.`' . $field . '`))-LENGTH(REPLACE(LOWER(`' . $tablePostName . '`.`' . $field . '`), "' . $keyword . '", "")))/' . $keywordLength . ')),0) * ' . $weight . ') * ' . (double) $keywordWeight . ') ';
                    }
                    $totalKeywordWeight += $keywordWeight;
                }
                $queryString1 = '
SELECT `' . $tablePostName . '`.`ID` , `' . $tablePostName . '`.`post_title` , `' . $tablePostName . '`.`post_content`, (
	(' . implode(' + ', $arrayScoreMatchString) . ')
) AS wpxtraffic_score
FROM ' . $tablePostName . '
WHERE ( ( `' . $tablePostName . '`.`post_status` = "publish" ) AND !(`' . $tablePostName . '`.`post_password` > "") 
';
                if ($queryString_Where_PostType) {
                    $queryString1 .= ' AND ( ' . $queryString_Where_PostType . ' ) ';
                }
                if (!empty($input_parameters['exclude_posts_ids'])) {
                    $queryString1 .= ' AND ( `' . $tablePostName . '`.`ID` NOT IN (' . implode(',', $input_parameters['exclude_posts_ids']) . ') ) ';
                }
                if ($input_parameters['post_id_less_than'] > 1 && $input_parameters['post_id_less_than'] > $input_parameters['limit']) {
                    $queryString1 .= ' AND ( `' . $tablePostName . '`.`ID` < ' . $input_parameters['post_id_less_than'] . ' ) ';
                }
                $queryString1 .= ' ) ';
                $queryString1 .= ' 
GROUP BY `' . $tablePostName . '`.`ID` 
ORDER BY wpxtraffic_score DESC 
';
            }
            $queryString1 .= ' LIMIT 0,' . $input_parameters['limit'] . ' ';
            $rsOne = $wpdb->get_results($queryString1);
            unset($queryString1);
            if ($rsOne) {
                foreach ($rsOne as $keyOne => $valueOne) {
                    unset($rsOne[$keyOne]);
                    if ($valueOne) {
                        if (isset($valueOne->wpxtraffic_score)) {
                            $valueOne->wpxtraffic_score = (double) $valueOne->wpxtraffic_score / $totalKeywordWeight;
                            if ($valueOne->wpxtraffic_score >= 1) {
                                $postId = (int) $valueOne->ID;
                                if ($postId) {
                                    /*
                                    $foundKeywordStatus = false;
                                    
                                    
                                    $valueTemp1 = $valueOne->post_title;
                                    $valueTemp1 = $this->cleanRawTextForProcessSearch($valueTemp1);
                                    foreach($input_parameters['keywords'] as $key1 => $value1) {
                                    	if(false !== stripos($valueTemp1, $key1)) {
                                    		$foundKeywordStatus = true;
                                    		break;
                                    	}
                                    }
                                    
                                    if(!$foundKeywordStatus) {
                                    	$valueTemp1 = $valueOne->post_content;
                                    	$valueTemp1 = PepVN_Data::mb_substr($valueTemp1, 0 ,500);
                                    	$valueTemp1 = $this->cleanRawTextForProcessSearch($valueTemp1);
                                    	foreach($input_parameters['keywords'] as $key1 => $value1) {
                                    		if(false !== stripos($valueTemp1, $key1)) {
                                    			$foundKeywordStatus = true;
                                    			break;
                                    		}
                                    	}
                                    }
                                    */
                                    $foundKeywordStatus = true;
                                    if ($foundKeywordStatus) {
                                        $postLink = $wpExtend->get_permalink($postId, false);
                                        if ($postLink) {
                                            $postLink = trailingslashit($postLink);
                                            $resultData[$postId] = array('post_id' => $postId, 'post_title' => $valueOne->post_title, 'post_link' => $postLink, 'wpxtraffic_score' => $valueOne->wpxtraffic_score);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            unset($rsOne);
            TempDataAndCacheFile::set_cache($keyCache1, $resultData, true);
        }
        return $resultData;
    }
 private function _clean_terms($input_terms)
 {
     $keyCache1 = Utils::hashKey(array(__METHOD__, $input_terms));
     $resultData = TempDataAndCacheFile::get_cache($keyCache1);
     if (null !== $resultData) {
         return $resultData;
     }
     $input_terms = (array) $input_terms;
     $input_terms = implode(';', $input_terms);
     $input_terms = PepVN_Data::strtolower($input_terms);
     $input_terms = AnalyzeText::analysisKeyword_RemovePunctuations($input_terms, ';');
     $input_terms = Text::reduceSpace($input_terms);
     $input_terms = explode(';', $input_terms);
     $input_terms = PepVN_Data::cleanArray($input_terms);
     TempDataAndCacheFile::set_cache($keyCache1, $input_terms);
     return $input_terms;
 }
Beispiel #3
0
 private function _get_patterns_for_parse_custom_tag_of_image($input_parameters)
 {
     $classMethodKey = crc32(__CLASS__ . __METHOD__);
     $wpExtend = $this->di->getShared('wpExtend');
     $resultData = false;
     $postObj = false;
     if (isset($input_parameters['post_id']) && $input_parameters['post_id']) {
         $input_parameters['post_id'] = (int) $input_parameters['post_id'];
         if ($input_parameters['post_id'] > 0) {
             $postObj = $wpExtend->get_post($input_parameters['post_id']);
         }
     } else {
         global $post;
         if ($post) {
             if (isset($post->ID) && $post->ID) {
                 $postObj = $post;
             }
         }
     }
     if ($postObj) {
         if (isset($postObj->ID) && $postObj->ID) {
             $keyCache1 = Utils::hashKey(array($classMethodKey, $input_parameters, $postObj->ID));
             $resultData = TempDataAndCacheFile::get_cache($keyCache1);
             if (null === $resultData) {
                 $resultData = array();
                 $arrayPatterns1 = array('title' => '', 'category' => '', 'tags' => '', 'product_cat' => '', 'product_tag' => '', 'img_name' => '', 'img_title' => '', 'img_alt' => '');
                 if (isset($input_parameters['patterns']) && $input_parameters['patterns']) {
                     $input_parameters['patterns'] = (array) $input_parameters['patterns'];
                     $input_parameters['patterns'] = PepVN_Data::cleanArray($input_parameters['patterns']);
                     if (!empty($input_parameters['patterns'])) {
                         foreach ($input_parameters['patterns'] as $key1 => $value1) {
                             $key1 = trim($key1);
                             if ($key1) {
                                 $arrayPatterns1[$key1] = $value1;
                             }
                         }
                     }
                 }
                 $rsGetTerms = $wpExtend->getTermsByPostId($postObj->ID);
                 foreach ($arrayPatterns1 as $key1 => $value1) {
                     if (empty($value1)) {
                         if ('title' === $key1) {
                             $value1 = $postObj->post_title;
                         } else {
                             $rsTwo = array();
                             foreach ($rsGetTerms as $key2 => $value2) {
                                 if ($value2) {
                                     if (isset($value2['term_id']) && $value2['term_id']) {
                                         if ($key1 === $value2['termType']) {
                                             $rsTwo[] = $value2['name'];
                                         }
                                     }
                                 }
                             }
                             $rsTwo = PepVN_Data::cleanArray($rsTwo);
                             if (!empty($rsTwo)) {
                                 $value1 = implode(' ', $rsTwo);
                             }
                         }
                     }
                     $value1 = trim($value1);
                     $resultData['%' . $key1] = $value1;
                 }
                 TempDataAndCacheFile::set_cache($keyCache1, $resultData);
             }
         }
     }
     return $resultData;
 }
Beispiel #4
0
 private function _parse_data_custom_keywords($text, $input_configs)
 {
     $keyCache1 = Utils::hashKey(array('_parse_data_custom_keywords', $text));
     $resultData = TempDataAndCacheFile::get_cache($keyCache1);
     if (null === $resultData) {
         /*
         	$resultData = array(
         		'keyword' => array(
         			link1
         			,link2
         		)
         	);
         */
         $resultData = array();
         $case_sensitive = false;
         if (isset($input_configs['case_sensitive']) && 'on' === $input_configs['case_sensitive']) {
             $case_sensitive = true;
         }
         $text = (array) $text;
         $text = implode(PHP_EOL, $text);
         $text = trim($text);
         $text = explode(PHP_EOL, $text);
         foreach ($text as $key1 => $value1) {
             $temp1 = $this->_explode_and_clean_data($value1);
             if (!empty($temp1)) {
                 $keywords1 = array();
                 $links1 = array();
                 foreach ($temp1 as $key2 => $value2) {
                     unset($temp1[$key2]);
                     $value2 = trim($value2);
                     if (preg_match('#^https?://.+#i', $value2)) {
                         $links1[] = $value2;
                     } else {
                         if (!$case_sensitive) {
                             $keywords1[] = mb_strtolower($value2, 'UTF-8');
                         } else {
                             $keywords1[] = $value2;
                         }
                     }
                 }
                 if (!empty($keywords1)) {
                     foreach ($keywords1 as $key2 => $value2) {
                         unset($keywords1[$key2]);
                         if (!isset($resultData[$value2])) {
                             $resultData[$value2] = array();
                         }
                         $resultData[$value2] = array_merge($resultData[$value2], $links1);
                     }
                 }
                 unset($keywords1, $links1);
             }
         }
         if (!empty($resultData)) {
             foreach ($resultData as $key1 => $value1) {
                 if (!empty($value1)) {
                     $value1 = PepVN_Data::cleanArray($value1);
                     $value1 = array_unique($value1);
                     $resultData[$key1] = $value1;
                 }
             }
         }
         TempDataAndCacheFile::set_cache($keyCache1, $resultData);
     }
     return $resultData;
 }