コード例 #1
0
ファイル: functions.php プロジェクト: ViktorLinnasaar/hanset
function icl_get_tax_children_recursive($id, $taxonomy = 'category')
{
    global $wpdb;
    $id = (array) $id;
    $children = $wpdb->get_col($wpdb->prepare("SELECT term_id FROM {$wpdb->term_taxonomy} x WHERE x.taxonomy=%s AND parent IN (" . join(',', $id) . ")", $taxonomy));
    if (!empty($children)) {
        $children = array_merge($children, icl_get_tax_children_recursive($children));
    }
    return $children;
}
コード例 #2
0
 /**
  * get documents
  *
  * @param array $args
  *
  * @return mixed
  */
 function get_documents($args)
 {
     $parent_id = false;
     $parent_all = false;
     $to_lang = false;
     $from_lang = false;
     $tstatus = false;
     $sort_by = false;
     $sort_order = false;
     $limit_no = 0;
     extract($args);
     global $wpdb, $wp_query, $sitepress;
     $t_el_types = array_keys($sitepress->get_translatable_documents());
     // SELECT
     $select = " p.ID AS post_id, p.post_title, p.post_content, p.post_type, p.post_status, p.post_date, t.trid, t.source_language_code <> '' AS is_translation";
     $active_languages = $sitepress->get_active_languages();
     if ($to_lang) {
         $select .= ", iclts.status, iclts.needs_update";
     } else {
         foreach ($active_languages as $lang) {
             if ($lang['code'] == $from_lang) {
                 continue;
             }
             $tbl_alias_suffix = str_replace('-', '_', $lang['code']);
             $select .= ", iclts_{$tbl_alias_suffix}.status AS status_{$tbl_alias_suffix}, iclts_{$tbl_alias_suffix}.needs_update AS needs_update_{$tbl_alias_suffix}";
         }
     }
     // FROM
     $from = " {$wpdb->posts} p";
     // JOIN
     $join = "";
     $join .= " LEFT JOIN {$wpdb->prefix}icl_translations t ON t.element_id=p.ID\n";
     if ($to_lang) {
         $tbl_alias_suffix = str_replace('-', '_', $to_lang);
         $join .= " LEFT JOIN {$wpdb->prefix}icl_translations iclt_{$tbl_alias_suffix}\n\t\t\t\t\t\tON iclt_{$tbl_alias_suffix}.trid=t.trid AND iclt_{$tbl_alias_suffix}.language_code='{$to_lang}'\n";
         $join .= " LEFT JOIN {$wpdb->prefix}icl_translation_status iclts ON iclts.translation_id=iclt_{$tbl_alias_suffix}.translation_id\n";
     } else {
         foreach ($active_languages as $lang) {
             if ($lang['code'] == $from_lang) {
                 continue;
             }
             $tbl_alias_suffix = str_replace('-', '_', $lang['code']);
             $join .= " LEFT JOIN {$wpdb->prefix}icl_translations iclt_{$tbl_alias_suffix}\n\t\t\t\t\t\tON iclt_{$tbl_alias_suffix}.trid=t.trid AND iclt_{$tbl_alias_suffix}.language_code='{$lang['code']}'\n";
             $join .= " LEFT JOIN {$wpdb->prefix}icl_translation_status iclts_{$tbl_alias_suffix}\n\t\t\t\t\t\tON iclts_{$tbl_alias_suffix}.translation_id=iclt_{$tbl_alias_suffix}.translation_id\n";
         }
     }
     // WHERE
     $where = " t.language_code = '{$from_lang}' AND p.post_status NOT IN ('trash', 'auto-draft') \n";
     if (!empty($type)) {
         $where .= " AND p.post_type = '{$type}'";
         $where .= " AND t.element_type = 'post_{$type}'\n";
     } else {
         $where .= " AND p.post_type IN ('" . join("','", $t_el_types) . "')\n";
         foreach ($t_el_types as $k => $v) {
             $t_el_types[$k] = 'post_' . $v;
         }
         $where .= " AND t.element_type IN ('" . join("','", $t_el_types) . "')\n";
     }
     if (!empty($title)) {
         $where .= " AND p.post_title LIKE '%" . esc_sql($title) . "%'\n";
     }
     if (!empty($status)) {
         $where .= " AND p.post_status = '{$status}'\n";
     }
     if (isset($from_date)) {
         $where .= " AND p.post_date > '{$from_date}'\n";
     }
     if (isset($to_date)) {
         $where .= " AND p.post_date > '{$to_date}'\n";
     }
     if ($tstatus) {
         if ($to_lang) {
             if ($tstatus == 'not') {
                 $where .= " AND (iclts.status IS NULL OR iclts.status = " . ICL_TM_WAITING_FOR_TRANSLATOR . " OR iclts.needs_update = 1)\n";
             } elseif ($tstatus == 'need-update') {
                 $where .= " AND iclts.needs_update = 1\n";
             } elseif ($tstatus == 'in_progress') {
                 $where .= " AND iclts.status = " . ICL_TM_IN_PROGRESS . " AND iclts.needs_update = 0\n";
             } elseif ($tstatus == 'complete') {
                 $where .= " AND (iclts.status = " . ICL_TM_COMPLETE . " OR iclts.status = " . ICL_TM_DUPLICATE . ") AND iclts.needs_update = 0\n";
             }
         } elseif ($active_languages && count($active_languages) > 1) {
             if ($tstatus == 'not') {
                 $where .= " AND (";
                 $wheres = array();
                 foreach ($active_languages as $lang) {
                     if ($lang['code'] == $from_lang) {
                         continue;
                     }
                     $tbl_alias_suffix = str_replace('-', '_', $lang['code']);
                     $wheres[] = "iclts_{$tbl_alias_suffix}.status IS NULL OR iclts_{$tbl_alias_suffix}.status = " . ICL_TM_WAITING_FOR_TRANSLATOR . " OR iclts_{$tbl_alias_suffix}.needs_update = 1\n";
                 }
                 $where .= join(' OR ', $wheres) . ")";
             } elseif ($tstatus == 'need-update') {
                 $where .= " AND (";
                 $wheres = array();
                 foreach ($active_languages as $lang) {
                     if ($lang['code'] == $from_lang) {
                         continue;
                     }
                     $tbl_alias_suffix = str_replace('-', '_', $lang['code']);
                     $wheres[] = "iclts_{$tbl_alias_suffix}.needs_update = 1\n";
                 }
                 $where .= join(' OR ', $wheres) . ")";
             } elseif ($tstatus == 'in_progress') {
                 $where .= " AND (";
                 $wheres = array();
                 foreach ($active_languages as $lang) {
                     if ($lang['code'] == $from_lang) {
                         continue;
                     }
                     $tbl_alias_suffix = str_replace('-', '_', $lang['code']);
                     $wheres[] = "iclts_{$tbl_alias_suffix}.status = " . ICL_TM_IN_PROGRESS . "\n";
                 }
                 $where .= join(' OR ', $wheres) . ")";
             } elseif ($tstatus == 'complete') {
                 foreach ($active_languages as $lang) {
                     if ($lang['code'] == $from_lang) {
                         continue;
                     }
                     $tbl_alias_suffix = str_replace('-', '_', $lang['code']);
                     $where .= " AND (iclts_{$tbl_alias_suffix}.status = " . ICL_TM_COMPLETE . " OR iclts_{$tbl_alias_suffix}.status = " . ICL_TM_DUPLICATE . ") AND iclts_{$tbl_alias_suffix}.needs_update = 0\n";
                 }
             }
         }
     }
     if (isset($parent_type) && $parent_type == 'page' && $parent_id > 0) {
         if ($parent_all) {
             $children = icl_get_post_children_recursive($parent_id);
             if (!$children) {
                 $children[] = -1;
             }
             $where .= ' AND p.ID IN (' . join(',', $children) . ')';
         } else {
             $where .= ' AND p.post_parent=' . intval($parent_id);
         }
     }
     if (isset($parent_type) && $parent_type == 'category' && $parent_id > 0) {
         if ($parent_all) {
             $children = icl_get_tax_children_recursive($parent_id);
             $children[] = $parent_id;
             $join .= "  JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id\n\t\t\t\t\t\t\tJOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id AND taxonomy = 'category'\n\t\t\t\t\t\t\tJOIN {$wpdb->terms} tm ON tt.term_id = tm.term_id AND tm.term_id IN(" . join(',', $children) . ")";
         } else {
             $join .= "  JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id\n\t\t\t\t\t\t\tJOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id AND taxonomy = 'category'\n\t\t\t\t\t\t\tJOIN {$wpdb->terms} tm ON tt.term_id = tm.term_id AND tm.term_id = " . intval($parent_id);
         }
     }
     // ORDER
     if ($sort_by) {
         $order = " {$sort_by} ";
     } else {
         $order = " p.post_date DESC";
     }
     if ($sort_order) {
         $order .= $sort_order;
     } else {
         $order .= 'DESC';
     }
     // LIMIT
     if (!isset($_GET['paged'])) {
         $_GET['paged'] = 1;
     }
     $offset = ($_GET['paged'] - 1) * $limit_no;
     $limit = " " . $offset . ',' . $limit_no;
     $sql = "\n\t\t\tSELECT SQL_CALC_FOUND_ROWS {$select}\n\t\t\tFROM {$from}\n\t\t\t{$join}\n\t\t\tWHERE {$where}\n\t\t\tORDER BY {$order}\n\t\t\tLIMIT {$limit}\n\t\t";
     $results = $wpdb->get_results($sql);
     $count = $wpdb->get_var("SELECT FOUND_ROWS()");
     $wp_query->found_posts = $count;
     $wp_query->query_vars['posts_per_page'] = $limit_no;
     $wp_query->max_num_pages = ceil($wp_query->found_posts / $limit_no);
     // post process
     foreach ($results as $k => $v) {
         if ($v->is_translation) {
             $source_language = $wpdb->get_var($wpdb->prepare("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND source_language_code IS NULL", $v->trid));
             $_tmp = 'status_' . $source_language;
             $v->{$_tmp} = ICL_TM_COMPLETE;
         }
     }
     return $results;
 }