コード例 #1
0
ファイル: postie-functions.php プロジェクト: donwea/nhap.org
/**
 * This function determines the categories ids for the post
 * @return array
 */
function tag_Categories(&$subject, $defaultCategory, $category_match)
{
    $original_subject = $subject;
    $post_categories = array();
    $matchtypes = array();
    $matches = array();
    if (preg_match_all('/\\[(.[^\\[]*)\\]/', $subject, $matches)) {
        // [<category1>] [<category2>] <Subject>
        //preg_match("/]([^\[]*)$/", $subject, $subject_matches);
        //$subject = trim($subject_matches[1]);
        $matchtypes[] = $matches;
    }
    if (preg_match_all('/-(.[^-]*)-/', $subject, $matches)) {
        // -<category>- -<category2>- <Subject>
        //preg_match("/-(.[^-]*)$/", $subject, $subject_matches);
        //$subject = trim($subject_matches[1]);
        $matchtypes[] = $matches;
    }
    if (preg_match('/(.+): (.*)/', $subject, $matches)) {
        // <category>:<Subject>
        $category = lookup_category($matches[1], $category_match);
        if (!empty($category)) {
            DebugEcho("colon category: {$category}");
            $subject = trim($matches[2]);
            $post_categories[] = $category;
        }
    }
    DebugEcho("tag_Categories: found categories");
    DebugDump($matchtypes);
    foreach ($matchtypes as $matches) {
        if (count($matches)) {
            $i = 0;
            foreach ($matches[1] as $match) {
                $category = lookup_category($match, $category_match);
                if (!empty($category)) {
                    $subject = str_replace($matches[0][$i], '', $subject);
                    $post_categories[] = $category;
                }
                $i++;
            }
        }
    }
    if (!count($post_categories)) {
        $post_categories[] = $defaultCategory;
        $subject = $original_subject;
    }
    $subject = trim($subject);
    return $post_categories;
}
コード例 #2
0
/**
 * This function determines the categories ids for the post
 * @return array
 */
function tag_Categories(&$subject, $defaultCategory, $category_match, $post_id)
{
    $original_subject = $subject;
    $found = false;
    $post_categories = array();
    $matchtypes = array();
    $matches = array();
    if (preg_match_all('/\\[(.[^\\[]*)\\]/', $subject, $matches)) {
        // [<category1>] [<category2>] <Subject>
        //preg_match("/]([^\[]*)$/", $subject, $subject_matches);
        //$subject = trim($subject_matches[1]);
        $matchtypes[] = $matches;
    }
    if (preg_match_all('/-(.[^-]*)-/', $subject, $matches)) {
        // -<category>- -<category2>- <Subject>
        //preg_match("/-(.[^-]*)$/", $subject, $subject_matches);
        //$subject = trim($subject_matches[1]);
        $matchtypes[] = $matches;
    }
    if (preg_match('/(.+): (.*)/', $subject, $matches)) {
        // <category>:<Subject>
        $category = lookup_category($matches[1], $category_match);
        if (!empty($category)) {
            $found = true;
            DebugEcho("colon category: {$category}");
            $subject = trim($matches[2]);
            DebugEcho("colon category: subject: {$subject}");
            $tax = lookup_taxonomy($category);
            if ('category' == $tax) {
                $post_categories[] = $category;
            } else {
                DebugEcho("colon category: custom taxonomy {$tax}");
                wp_set_object_terms($post_id, $category, $tax);
            }
        }
    }
    DebugEcho("tag_Categories: found categories");
    DebugDump($matchtypes);
    foreach ($matchtypes as $matches) {
        if (count($matches)) {
            $i = 0;
            foreach ($matches[1] as $match) {
                $category = lookup_category($match, $category_match);
                if (!empty($category)) {
                    $found = true;
                    $subject = str_replace($matches[0][$i], '', $subject);
                    DebugEcho("tag_Categories: subject: {$subject}");
                    $tax = lookup_taxonomy($category);
                    if ('category' == $tax) {
                        $post_categories[] = $category;
                    } else {
                        DebugEcho("tag_Categories: custom taxonomy {$tax}");
                        wp_set_object_terms($post_id, $category, $tax);
                    }
                }
                $i++;
            }
        }
    }
    if (!$found) {
        $post_categories[] = $defaultCategory;
        $subject = $original_subject;
    }
    $subject = trim($subject);
    return $post_categories;
}