- Spostate tutte le funzioni all'interno di una classe
- Aggiunta la funzione is_administrator()
- Fix sulla funzione _debug
- Aggiunto hook per inizializzare la classe
- Aggiunta la documentazione phpDoc
Eccovi il sorgente della nuova versione...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | /* Plugin Name: WP Development Utilities Plugin URI: http://www.fabiocicerchia.it/projects/wp-development-utilities/ Description: Extends the template tags with more functions that are very useful. Author: Fabio Cicerchia Version: 1.4 License: GPL Author URI: http://www.fabiocicerchia.it */ /** * Extends the template tags with more functions that are very useful. * * @package wp_development_utilities * @author Fabio Cicerchia <info@fabiocicerchia.it> * @license http://opensource.org/licenses/gpl-license.php GNU Public License * @version $Id: $; */ class WP_Dev_Utilities { /** * Contains the informations of logged user * * @var object * @access private */ var $userdata; /** * Constructor * * @access public * @global object $userdata */ function WP_Dev_Utilities() { global $userdata; get_currentuserinfo(); $this->userdata = $userdata; } /** * Return the id of the post permalink * * @access public * @param string $permalink * @global object $wpdb * @return int */ function get_post_id_from_permalink($permalink) { global $wpdb; $permalink_structure = get_option('permalink_structure'); $regex = $permalink_structure; $rewritecode = array( "%year%" => "YEAR(`post_date`)='%s'", "%monthnum%" => "MONTH(`post_date`)='%s'", "%day%" => "DAYOFMONTH(`post_date`)='%s'", "%hour%" => "HOUR(`post_date`)='%s'", "%minute%" => "MINUTE(`post_date`)='%s'", "%second%" => "SECOND(`post_date`)='%s'", "%postname%" => "`post_name`='%s'", "%post_id%" => "`ID`='%s'", "%category%" => "`ID` IN (%s)", "%author%" => "`post_author`='%s'", "%pagename%" => "post_name`='%s'"); $codes = array_keys($rewritecode); $token = array(); $ordered_codes = explode('%', $permalink_structure); foreach($ordered_codes as $value) { if (in_array("%$value%", $codes)) { $token[] = "%$value%"; } } foreach($codes as $value) { $regex = str_replace($value, '(.+)', $regex); } $regex = '/.*' . $_SERVER['HTTP_HOST'] . str_replace('/', '\/', $regex) . '$/'; preg_match_all($regex, $permalink, $matches); for ($i = 1; $i < count($matches); $i++) { $values[$token[$i - 1]] = $matches[$i][0]; } $where = array(''); foreach($codes as $value) { if (!isset($values[$value])) continue; if ($value == '%category%') { $parent = 0; $posts = array(); $values[$value] = explode('/', $values[$value]); foreach($values[$value] as $v) { //USE DISTINCT $q2 = "SELECT p.ID as id, t.term_id as parent_ti FROM $wpdb->posts p, $wpdb->terms t, $wpdb->term_relationships tr, $wpdb->term_taxonomy tt WHERE tr.object_id=p.ID AND tt.term_taxonomy_id=tr.term_taxonomy_id AND t.term_id=tt.term_id AND t.slug='$v' AND p.post_status='publish' AND tt.parent=$parent;"; $res = $wpdb->get_results($q2, ARRAY_A); $posts[] = $res; $q2 = "SELECT t.term_id as parent_ti FROM $wpdb->terms t, $wpdb->term_taxonomy tt WHERE t.slug='$v' AND tt.term_id=t.term_id AND tt.parent=$parent LIMIT 1;"; $res = $wpdb->get_results($q2, ARRAY_A); $parent = $res[0]['parent_ti']; } $ids = array(); foreach($posts as $v) { if (is_array($v)) { foreach ($v as $post) { $ids[] = $post['id']; } } } $values[$value] = implode(', ', $ids); } else if ($value == '%author%') { $q2 = "SELECT `ID` FROM $wpdb->users WHERE `display_name`='" . $values['%author%'] . "'"; $res = $wpdb->get_results($q2, ARRAY_A); $values[$value] = $res[0]['ID']; } $where[] = sprintf($rewritecode[$value], $values[$value]); } $query = "SELECT `ID` FROM $wpdb->posts WHERE `post_status`='publish'" . implode(' AND ', $where) . " LIMIT 1;"; $res = $wpdb->get_results($query, ARRAY_A); return $res[0]['ID']; } /** * Return the contents of the post permalink * * @access public * @param string $permalink * @return string */ function get_post_content_from_permalink($permalink) { $id = $this->get_post_id_from_permalink($permalink); return $this->get_post_content_from_id($id); } /** * Return the content of the post from his id * * @access public * @param int $id * @global object $wpdb * @return string */ function get_post_content_from_id($id) { global $wpdb; $query = "SELECT `post_content` FROM $wpdb->posts WHERE ID=$id LIMIT 1;"; $res = $wpdb->get_results($query, ARRAY_A); return $res[0]['post_content']; } /** * Returns the name of the user logged on * * @access public * @return string */ function get_logged_username() { return $this->userdata->user_login; } /** * Check if logged user has the administrator privileges * * @access public * @return bool */ function is_administrator() { return $this->userdata->wp_capabilities['administrator']; } /** * A function to print variable only if the user logged as administrator * * @access public * @param mixed $variable * @param bool $exit * @param bool $use_printf */ function _debug($variable, $exit = true, $use_printf = false) { if ($this->is_administrator()) { if ($use_printf) print_f($variable); else var_dump($variable); if ($exit) exit; } } /** * Returns the number of pages of a post * * @access public * @param int $id * @return int */ function count_num_pages_post($id) { $content = $this->get_post_content_from_id($id); return count(split('<!--'.'nextpage-->', $content)); } } /** * Init the class plugin * * @access public * @global object $wp_dev_utilities */ function wp_dev_utilities_init() { global $wp_dev_utilities; $wp_dev_utilities = new WP_Dev_Utilities(); } add_action('get_header', 'wp_dev_utilities_init'); |








