classes.php
来自「php 开发的内容管理系统」· PHP 代码 · 共 1,728 行 · 第 1/4 页
PHP
1,728 行
<?phpclass WP_Query { var $query; var $query_vars; var $queried_object; var $queried_object_id; var $request; var $posts; var $post_count = 0; var $current_post = -1; var $in_the_loop = false; var $post; var $is_single = false; var $is_preview = false; var $is_page = false; var $is_archive = false; var $is_date = false; var $is_year = false; var $is_month = false; var $is_day = false; var $is_time = false; var $is_author = false; var $is_category = false; var $is_search = false; var $is_feed = false; var $is_trackback = false; var $is_home = false; var $is_404 = false; var $is_comments_popup = false; var $is_admin = false; var $is_attachment = false; function init_query_flags() { $this->is_single = false; $this->is_page = false; $this->is_archive = false; $this->is_date = false; $this->is_year = false; $this->is_month = false; $this->is_day = false; $this->is_time = false; $this->is_author = false; $this->is_category = false; $this->is_search = false; $this->is_feed = false; $this->is_trackback = false; $this->is_home = false; $this->is_404 = false; $this->is_paged = false; $this->is_admin = false; $this->is_attachment = false; } function init () { unset($this->posts); unset($this->query); unset($this->query_vars); unset($this->queried_object); unset($this->queried_object_id); $this->post_count = 0; $this->current_post = -1; $this->in_the_loop = false; $this->init_query_flags(); } // Reparse the query vars. function parse_query_vars() { $this->parse_query(''); } // Parse a query string and set query type booleans. function parse_query ($query) { if ( !empty($query) || !isset($this->query) ) { $this->init(); parse_str($query, $qv); $this->query = $query; $this->query_vars = $qv; } if ('404' == $qv['error']) { $this->is_404 = true; if ( !empty($query) ) { do_action('parse_query', array(&$this)); } return; } $qv['m'] = (int) $qv['m']; $qv['p'] = (int) $qv['p']; // Compat. Map subpost to attachment. if ( '' != $qv['subpost'] ) $qv['attachment'] = $qv['subpost']; if ( '' != $qv['subpost_id'] ) $qv['attachment_id'] = $qv['subpost_id']; if ( ('' != $qv['attachment']) || (int) $qv['attachment_id'] ) { $this->is_single = true; $this->is_attachment = true; } elseif ('' != $qv['name']) { $this->is_single = true; } elseif ( $qv['p'] ) { $this->is_single = true; } elseif (('' != $qv['hour']) && ('' != $qv['minute']) &&('' != $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day'])) { // If year, month, day, hour, minute, and second are set, a single // post is being queried. $this->is_single = true; } elseif ('' != $qv['static'] || '' != $qv['pagename'] || '' != $qv['page_id']) { $this->is_page = true; $this->is_single = false; } elseif (!empty($qv['s'])) { $this->is_search = true; switch ($qv['show_post_type']) { case 'page' : $this->is_page = true; break; case 'attachment' : $this->is_attachment = true; break; } } else { // Look for archive queries. Dates, categories, authors. if ( (int) $qv['second']) { $this->is_time = true; $this->is_date = true; } if ( (int) $qv['minute']) { $this->is_time = true; $this->is_date = true; } if ( (int) $qv['hour']) { $this->is_time = true; $this->is_date = true; } if ( (int) $qv['day']) { if (! $this->is_date) { $this->is_day = true; $this->is_date = true; } } if ( (int) $qv['monthnum']) { if (! $this->is_date) { $this->is_month = true; $this->is_date = true; } } if ( (int) $qv['year']) { if (! $this->is_date) { $this->is_year = true; $this->is_date = true; } } if ( (int) $qv['m']) { $this->is_date = true; if (strlen($qv['m']) > 9) { $this->is_time = true; } else if (strlen($qv['m']) > 7) { $this->is_day = true; } else if (strlen($qv['m']) > 5) { $this->is_month = true; } else { $this->is_year = true; } } if ('' != $qv['w']) { $this->is_date = true; } if (empty($qv['cat']) || ($qv['cat'] == '0')) { $this->is_category = false; } else { if (stristr($qv['cat'],'-')) { $this->is_category = false; } else { $this->is_category = true; } } if ('' != $qv['category_name']) { $this->is_category = true; } if ((empty($qv['author'])) || ($qv['author'] == '0')) { $this->is_author = false; } else { $this->is_author = true; } if ('' != $qv['author_name']) { $this->is_author = true; } if ( ($this->is_date || $this->is_author || $this->is_category)) { $this->is_archive = true; } if ( 'attachment' == $qv['show_post_type'] ) { $this->is_attachment = true; } } if ('' != $qv['feed']) { $this->is_feed = true; } if ('' != $qv['tb']) { $this->is_trackback = true; } if ('' != $qv['paged']) { $this->is_paged = true; } if ('' != $qv['comments_popup']) { $this->is_comments_popup = true; } //if we're previewing inside the write screen if ('' != $qv['preview']) { $this->is_preview = true; } if (strstr($_SERVER['PHP_SELF'], 'wp-admin/')) { $this->is_admin = true; } if ( ! ($this->is_attachment || $this->is_archive || $this->is_single || $this->is_page || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup)) { $this->is_home = true; } if ( !empty($query) ) { do_action('parse_query', array(&$this)); } } function set_404() { $this->init_query_flags(); $this->is_404 = true; } function get($query_var) { if (isset($this->query_vars[$query_var])) { return $this->query_vars[$query_var]; } return ''; } function set($query_var, $value) { $this->query_vars[$query_var] = $value; } function &get_posts() { global $wpdb, $pagenow, $user_ID; do_action('pre_get_posts', array(&$this)); // Shorthand. $q = $this->query_vars; // First let's clear some variables $whichcat = ''; $whichauthor = ''; $whichpage = ''; $result = ''; $where = ''; $limits = ''; $distinct = ''; $join = ''; if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 ) $q['posts_per_page'] = get_settings('posts_per_page'); if ( !isset($q['what_to_show']) ) $q['what_to_show'] = get_settings('what_to_show'); if ( isset($q['showposts']) && $q['showposts'] ) { $q['showposts'] = (int) $q['showposts']; $q['posts_per_page'] = $q['showposts']; } if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) ) $q['posts_per_page'] = $q['posts_per_archive_page']; if ( !isset($q['nopaging']) ) { if ($q['posts_per_page'] == -1) { $q['nopaging'] = true; } else { $q['nopaging'] = false; } } if ( $this->is_feed ) { $q['posts_per_page'] = get_settings('posts_per_rss'); $q['what_to_show'] = 'posts'; } if (isset($q['page'])) { $q['page'] = trim($q['page'], '/'); $q['page'] = (int) $q['page']; $q['page'] = abs($q['page']); } $add_hours = intval(get_settings('gmt_offset')); $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours)); $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"; // If a month is specified in the querystring, load that month if ( (int) $q['m'] ) { $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']); $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4); if (strlen($q['m'])>5) $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2); if (strlen($q['m'])>7) $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2); if (strlen($q['m'])>9) $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2); if (strlen($q['m'])>11) $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2); if (strlen($q['m'])>13) $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2); } if ( (int) $q['hour'] ) { $q['hour'] = '' . intval($q['hour']); $where .= " AND HOUR(post_date)='" . $q['hour'] . "'"; } if ( (int) $q['minute'] ) { $q['minute'] = '' . intval($q['minute']); $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'"; } if ( (int) $q['second'] ) { $q['second'] = '' . intval($q['second']); $where .= " AND SECOND(post_date)='" . $q['second'] . "'"; } if ( (int) $q['year'] ) { $q['year'] = '' . intval($q['year']); $where .= " AND YEAR(post_date)='" . $q['year'] . "'"; } if ( (int) $q['monthnum'] ) { $q['monthnum'] = '' . intval($q['monthnum']); $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'"; } if ( (int) $q['day'] ) { $q['day'] = '' . intval($q['day']); $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'"; } // Compat. Map subpost to attachment. if ( '' != $q['subpost'] ) $q['attachment'] = $q['subpost']; if ( '' != $q['subpost_id'] ) $q['attachment_id'] = $q['subpost_id']; if ('' != $q['name']) { $q['name'] = sanitize_title($q['name']); $where .= " AND post_name = '" . $q['name'] . "'"; } else if ('' != $q['pagename']) { $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename']))); $page_paths = '/' . trim($q['pagename'], '/'); $q['pagename'] = sanitize_title(basename($page_paths)); $q['name'] = $q['pagename']; $page_paths = explode('/', $page_paths); foreach($page_paths as $pathdir) $page_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir); $all_page_ids = get_all_page_ids(); $reqpage = 0; if (is_array($all_page_ids)) { foreach ( $all_page_ids as $page_id ) { $page = get_page($page_id); if ( $page->fullpath == $page_path ) { $reqpage = $page_id; break; } } } $where .= " AND (ID = '$reqpage')"; } elseif ('' != $q['attachment']) { $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment']))); $attach_paths = '/' . trim($q['attachment'], '/'); $q['attachment'] = sanitize_title(basename($attach_paths)); $q['name'] = $q['attachment']; $where .= " AND post_name = '" . $q['attachment'] . "'"; } if ( (int) $q['w'] ) { $q['w'] = ''.intval($q['w']); $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'"; } if ( intval($q['comments_popup']) ) $q['p'] = intval($q['comments_popup']); // If a attachment is requested by number, let it supercede any post number. if ( ($q['attachment_id'] != '') && (intval($q['attachment_id']) != 0) ) $q['p'] = (int) $q['attachment_id']; // If a post number is specified, load that post if (($q['p'] != '') && intval($q['p']) != 0) { $q['p'] = (int) $q['p']; $where = ' AND ID = ' . $q['p']; } if (($q['page_id'] != '') && (intval($q['page_id']) != 0)) { $q['page_id'] = intval($q['page_id']); $q['p'] = $q['page_id']; $where = ' AND ID = '.$q['page_id']; } // If a search pattern is specified, load the posts that match if (!empty($q['s'])) { $q['s'] = addslashes_gpc($q['s']); $search = ' AND ('; $q['s'] = preg_replace('/, +/', ' ', $q['s']); $q['s'] = str_replace(',', ' ', $q['s']); $q['s'] = str_replace('"', ' ', $q['s']); $q['s'] = trim($q['s']); if ($q['exact']) { $n = ''; } else {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?