├── spark.info ├── config ├── autoload.php └── wp.php ├── README.markdown └── libraries └── wp.php /spark.info: -------------------------------------------------------------------------------- 1 | name: CodeIgniter-WP 2 | version: 0.2.0 3 | compatibility: 2.0.3 -------------------------------------------------------------------------------- /config/autoload.php: -------------------------------------------------------------------------------- 1 | path = '../../blogg.brollopsbild.com/htdocs/'; 4 | # $config['wp']['homepage']->path = '../../homepage/'; you can specify several wordpress installations like this 5 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | CodeIgniter-WP 2 | ================ 3 | 4 | Fetch data from one or many WordPress installations. This is a very early version and quite rough. It only supports the methods below. More to come. 5 | 6 | 7 | Requirements 8 | ------------ 9 | 10 | 1. PHP 5.3+ 11 | 2. CodeIgniter 2.0.0 - 2.0.3 12 | 13 | 14 | Usage 15 | ----- 16 | 17 | First - open up sparks/config/wp.php and tell the spark where you have your WordPress install root (ie where the wp-config.php is located on the server). 18 | Note that you need both file and mysql access to the WordPress installations. 19 | Next in your controller: 20 | 21 | $this->load->spark('ciwp/0.1.0'); 22 | print_r($this->wp->get_post('blog', 1); 23 | print_r($this->wp->get_installations()); 24 | print_r($this->wp->wp_get_recent_posts('blog', array())); 25 | print_r($this->wp->get_post_meta('blog', array())); 26 | print_r($this->wp->get_comments('blog',array())); 27 | print_r($this->wp->get_children('blog',array())); 28 | print_r($this->wp->wp_insert_post('blog',array())); 29 | print_r($this->wp->wp_insert_comment('blog','array())); 30 | print_r($this->wp->get_users('blog','array('include' => 1, 'exlude' => 3, 'number' => 5))); 31 | 32 | Methods in this library will take the same arguments as the WordPress function in the Codex. 33 | 34 | Wordpress Codex: http://codex.wordpress.org/ 35 | -------------------------------------------------------------------------------- /libraries/wp.php: -------------------------------------------------------------------------------- 1 | CI =& get_instance(); 18 | $this->installations = $this->CI->config->item('wp'); 19 | if($this->installations){ 20 | foreach($this->installations as $installation){ 21 | $installation->wpconfig = $this->_get_wpconfig($installation); 22 | } 23 | }else{ 24 | log_message('error', 'WP Spark: no installations specified in config/wp.php - ' . $installation); 25 | } 26 | } 27 | 28 | /* ---------------------- 29 | Public functions 30 | -----------------------*/ 31 | 32 | public function get_installations(){ 33 | return $this->installations; 34 | } 35 | 36 | public function get_post_meta($installation = '', $args = array()){ 37 | $wpdb = $this->installations[$installation]->wpconfig->wpdb; 38 | $wpdb 39 | ->select('meta_value') 40 | ->from('postmeta') 41 | ->where('post_id', $args['post_id']) 42 | ->where('meta_key', $args['key']); 43 | $posts = $wpdb->get(); 44 | if($posts->num_rows()>0){ 45 | if($args['single']){ 46 | return $posts->row()->meta_value; 47 | }else{ 48 | foreach($posts->result() as $meta){ 49 | $post_meta_array[] = $meta->meta_value; 50 | } 51 | return $post_meta_array; 52 | } 53 | } 54 | 55 | } 56 | 57 | public function wp_get_recent_posts($installation = '', $args = array()) { 58 | $defaults = array( 59 | 'numberposts' => 10, 60 | 'offset' => 0, 61 | 'category' => 0, 62 | 'orderby' => 'post_date', 63 | 'order' => 'DESC', 64 | 'include' => '', 65 | 'exclude' => '', 66 | 'meta_key' => '', 67 | 'meta_value' =>'', 68 | 'post_type' => 'post', 69 | 'post_status' => 'draft, publish, future, pending, private' 70 | ); 71 | $r = $this->_wp_parse_args($args, $defaults); 72 | $results = $this->get_posts($installation, $r); 73 | return $results ? $results : false; 74 | } 75 | 76 | public function get_post($installation = '', $ID = 0){ 77 | $wpdb = $this->installations[$installation]->wpconfig->wpdb; 78 | $wpdb 79 | ->select('*') 80 | ->from('posts') 81 | ->where('ID', $ID); 82 | $posts = $wpdb->get(); 83 | if($posts->num_rows()>0){ 84 | return $posts->row(); 85 | } 86 | } 87 | 88 | public function wp_insert_post($installation = '', $args = array()){ 89 | $defaults = array( 90 | 'menu_order' => '', 91 | 'comment_status' => 'open', 92 | 'ping_status' => 'open', 93 | 'pinged' => '', 94 | 'post_author' => '', 95 | 'post_content' => '', 96 | 'post_date' => date('Y-m-d H:i:s'), 97 | 'post_date_gmt' => date('Y-m-d H:i:s'), 98 | 'post_modified_gmt' => date('Y-m-d H:i:s'), 99 | 'post_modified' => date('Y-m-d H:i:s'), 100 | 'post_excerpt' => '', 101 | 'post_name' => '', 102 | 'post_parent' => '', 103 | 'post_password' => '', 104 | 'post_status' => 'draft', 105 | 'post_title' => '', 106 | 'post_type' => 'post', 107 | 'to_ping' => '', 108 | ); 109 | 110 | $default_post_categorys = array('post_category' => array(1)); 111 | if($args['post_category']){ 112 | $post_categorys = array('post_category' => $args['post_category']); 113 | } 114 | 115 | $default_tags = array('tags_input' => ''); 116 | if($args['tags_input']){ 117 | $tags = array('tags_input' => $args['tags_input']); 118 | } 119 | 120 | if($args['ID']){ 121 | $id = $args['ID']; 122 | } 123 | 124 | unset($args['post_category']); 125 | unset($args['tags_input']); 126 | unset($args['ID']); 127 | 128 | $r = $this->_wp_parse_args($args,$defaults); 129 | $wpdb = $this->installations[$installation]->wpconfig->wpdb; 130 | if($id) { 131 | if($wpdb->where('ID',$id)->update('posts',$r)) { 132 | return $id; 133 | } else { 134 | return false; 135 | } 136 | } else { 137 | if($wpdb->insert('posts',$r)) { 138 | return $wpdb->insert_id(); 139 | } else { 140 | return false; 141 | } 142 | } 143 | } 144 | 145 | public function get_comments($installation = '', $args = array()){ 146 | $defaults = array( 147 | 'author_email' => '', 148 | 'ID' => '', 149 | 'karma' => '', 150 | 'number' => '', 151 | 'offset' => '', 152 | 'orderby' => 'comment_date_gmt', 153 | 'order' => 'DESC', 154 | 'parent' => '', 155 | 'post_id' => '', 156 | 'status' => '', 157 | 'type' => '', 158 | 'user_id' => '' 159 | ); 160 | 161 | $r = $this->_wp_parse_args($args, $defaults); 162 | $wpdb = $this->installations[$installation]->wpconfig->wpdb; 163 | $wpdb 164 | ->select('*') 165 | ->from('comments'); 166 | if($r['author_email']){ 167 | $wpdb->where_in('comment_author_email',$r['author_email']); 168 | } 169 | if($r['ID']){ 170 | $wpdb->where_in('comment_ID',$r['ID']); 171 | } 172 | if($r['karma']){ 173 | $wpdb->where_in('comment_karma',$r['karma']); 174 | } 175 | if($r['parent']){ 176 | $wpdb->where_in('comment_parent',$r['parent']); 177 | } 178 | if($r['post_id']){ 179 | $wpdb->where_in('comment_post_ID',$r['post_id']); 180 | } 181 | if($r['status']){ 182 | $wpdb->where_in('comment_approved',$r['status']); 183 | } 184 | if($r['type']){ 185 | $wpdb->where_in('comment_type',$r['type']); 186 | } 187 | if($r['user_id']){ 188 | $wpdb->where_in('user_id',$r['user_id']); 189 | } 190 | $wpdb->order_by($r['orderby'], $r['order']); 191 | if($r['number'] >= 0){ 192 | if($r['offset'] >= 1){ 193 | $wpdb->limit($r['number'],$r['offset']); 194 | } else { 195 | $wpdb->limit($r['number']); 196 | } 197 | } 198 | $comments = $wpdb->get(); 199 | if($comments->num_rows()>0){ 200 | return $comments->result(); 201 | } 202 | } 203 | 204 | public function wp_insert_comment($installation = '', $args = array()){ 205 | $defaults = array( 206 | 'comment_post_ID' => '', 207 | 'comment_author' => '', 208 | 'comment_author_email' => '', 209 | 'comment_author_url' => '', 210 | 'comment_content' => '', 211 | 'comment_type' => '', 212 | 'comment_parent' => 0, 213 | 'user_id' => '', 214 | 'comment_author_IP' => '', 215 | 'comment_agent' => '', 216 | 'comment_date' => '', 217 | 'comment_date_gmt' => '', 218 | 'comment_approved' => '' 219 | ); 220 | $r = $this->_wp_parse_args($args, $defaults); 221 | $wpdb = $this->installations[$installation]->wpconfig->wpdb; 222 | if($wpdb->insert('comments',$r)){ 223 | return true; 224 | } else { 225 | return false; 226 | } 227 | } 228 | 229 | public function get_children($installation = '', $args = array()){ 230 | $defaults = array( 231 | 'post_parent' => 0, 232 | 'offset' => 0, 233 | 'category' => '', 234 | 'orderby' => 'post_date', 235 | 'order' => 'DESC', 236 | 'post_status' => 'any', 237 | 'post_type' => 'any', 238 | 'include' => '', 239 | 'exclude' => '', 240 | 'meta_key' => '', 241 | 'meta_value' => '', 242 | 'post_mime_type' => '', 243 | 'post_parent' => 0, 244 | 'numberposts' => -1 245 | ); 246 | 247 | $r = $this->_wp_parse_args($args, $defaults); 248 | $wpdb = $this->installations[$installation]->wpconfig->wpdb; 249 | $wpdb 250 | ->select('*') 251 | ->from('posts') 252 | ->where('post_parent',$r['post_parent']); 253 | if($r['post_type'] !== 'any'){ 254 | $wpdb->where_in('post_type', explode(', ',$r['post_type'])); 255 | } 256 | if($r['post_status'] !== 'any'){ 257 | $wpdb->where_in('post_status', explode(', ',$r['post_status'])); 258 | } 259 | $wpdb->order_by($r['orderby'], $r['order']); 260 | if($r['numberposts'] !== -1){ 261 | if($r['offset'] >= 1) { 262 | $wpdb->limit($r['numberposts'],$r['offset']); 263 | } else { 264 | $wpdb->limit($r['numberposts']); 265 | } 266 | } 267 | $posts = $wpdb->get(); 268 | if($posts->num_rows()>0){ 269 | return $posts->result(); 270 | } 271 | } 272 | 273 | public function get_users($installation = '', $args = array()){ 274 | $defaults = array( 275 | 'include' => array(), 276 | 'exclude' => array(), 277 | 'orderby' => 'user_login', 278 | 'order' => 'ASC', 279 | 'offset' => '', 280 | 'number' => '' 281 | ); 282 | 283 | $r = $this->_wp_parse_args($args,$defaults); 284 | $wpdb = $this->installations[$installation]->wpconfig->wpdb; 285 | $wpdb 286 | ->select('*') 287 | ->from('users'); 288 | if($r['include']){ 289 | $count = count($r['include']); 290 | for($i=0;$i<$count;$i++){ 291 | $wpdb->or_where('ID',$r['include'][$i]); 292 | } 293 | } 294 | if($r['exclude']){ 295 | $count = count($r['exclude']); 296 | for($i=0;$i<$count;$i++){ 297 | $wpdb->where_not_in('ID',$r['exclude'][$i]); 298 | } 299 | } 300 | $wpdb->order_by($r['orderby'], $r['order']); 301 | if($r['number'] !== -1){ 302 | if($r['offset'] >= 1) { 303 | $wpdb->limit($r['number'],$r['offset']); 304 | } else { 305 | $wpdb->limit($r['number']); 306 | } 307 | } 308 | 309 | $posts = $wpdb->get(); 310 | if($posts->num_rows()>0){ 311 | return $posts->result(); 312 | } 313 | } 314 | 315 | /* ---------------------- 316 | Private functions 317 | -----------------------*/ 318 | 319 | private function get_posts($installation = '', $args = array()){ 320 | $defaults = array( 321 | 'numberposts' => 10, 322 | 'offset' => 0, 323 | 'category' => 0, 324 | 'orderby' => 'post_date', 325 | 'order' => 'DESC', 326 | 'include' => '', 327 | 'exclude' => '', 328 | 'meta_key' => '', 329 | 'meta_value' => '', 330 | 'post_type' => 'post', 331 | 'post_status' => 'draft, publish, future, pending, private' 332 | ); 333 | $r = $this->_wp_parse_args($args, $defaults); 334 | $wpdb = $this->installations[$installation]->wpconfig->wpdb; 335 | $wpdb 336 | ->select('*') 337 | ->from('posts') 338 | ->where('post_type', $r['post_type']) 339 | ->where_in('post_status', explode(', ',$r['post_status'])) 340 | ->order_by($r['orderby'], $r['order']) 341 | ->limit($r['numberposts'], $r['offset']); 342 | $posts = $wpdb->get(); 343 | if($posts->num_rows()>0){ 344 | return $posts->result(); 345 | } 346 | } 347 | 348 | private function _wp_parse_args($args, $defaults = '') { 349 | if(is_object($args)){ 350 | $r = get_object_vars($args); 351 | }else if(is_array($args)){ 352 | $r =& $args; 353 | }else{ 354 | $this->_wp_parse_str($args, $r); 355 | } 356 | if(is_array($defaults)){ 357 | return array_merge($defaults, $r); 358 | } 359 | return $r; 360 | } 361 | 362 | private function _wp_parse_str($string, &$array) { 363 | parse_str($string, $array); 364 | } 365 | 366 | private function _wp_connect($wpconfig){ 367 | $db['hostname'] = $wpconfig->DB_HOST; 368 | $db['username'] = $wpconfig->DB_USER; 369 | $db['password'] = $wpconfig->DB_PASSWORD; 370 | $db['database'] = $wpconfig->DB_NAME; 371 | $db['dbdriver'] = 'mysql'; 372 | $db['dbprefix'] = $wpconfig->DB_PREFIX ?: 'wp_'; 373 | $db['pconnect'] = TRUE; 374 | $db['db_debug'] = TRUE; 375 | $db['cache_on'] = FALSE; 376 | $db['cachedir'] = ''; 377 | $db['char_set'] = $wpconfig->DB_CHARSET ?: 'utf8'; 378 | $db['dbcollat'] = $wpconfig->DB_COLLATE ?: 'utf8_general_ci'; 379 | $db['swap_pre'] = ''; 380 | $db['autoinit'] = FALSE; 381 | $db['stricton'] = FALSE; 382 | return $this->CI->load->database($db, true); 383 | } 384 | 385 | private function _get_wpconfig($wp = ''){ 386 | $config_file = read_file($wp->path.'wp-config.php'); 387 | if($config_file){ 388 | $lines = explode("\n", $config_file); 389 | if($lines){ 390 | foreach($lines as $line){ 391 | if(strstr($line,'define(')){ 392 | $regexp = '!define\(\'(.*?)\'(.*?)\'(.*?)\'\)!'; 393 | preg_match_all($regexp, $line, $matches); 394 | if(isset($matches[1][0]) && isset($matches[3][0])){ 395 | $wpconfig->{$matches[1][0]} = $matches[3][0]; 396 | } 397 | }else if(strstr($line,'$table_prefix')){ 398 | $regexp = '!\$table_prefix(.*?)\'(.*?)\'!'; 399 | preg_match_all($regexp, $line, $matches); 400 | if(isset($matches[2][0])){ 401 | $wpconfig->DB_PREFIX = $matches[2][0]; 402 | } 403 | } 404 | } 405 | if($wpconfig){ 406 | $wpconfig->wpdb = $this->_wp_connect($wpconfig); 407 | unset($wpconfig->DB_PASSWORD, $wpconfig->DB_HOST, $wpconfig->DB_NAME, $wpconfig->DB_USER); 408 | return $wpconfig; 409 | } 410 | } 411 | }else{ 412 | log_message('error', 'WP Spark: unable to read '.$wp->path.'wp-config.php'); 413 | } 414 | } 415 | 416 | /* ---------------------- 417 | Search Functions 418 | -----------------------*/ 419 | /** 420 | * Get Total posts by search query 421 | * 422 | * @param string $query Search Query 423 | * @param integer $post_per_page Number of posts per page 424 | * @param integer $current_page Current Page 425 | * 426 | * @return array Results Array 427 | */ 428 | function find_posts($installation, $query, $post_per_page,$current_page) 429 | { 430 | 431 | $offset = ( $current_page-1 ) * $post_per_page; 432 | 433 | if ( $current_page == 1 ) 434 | { 435 | $offset = 0; 436 | } 437 | 438 | $sql = "SELECT * FROM wp_posts WHERE MATCH ( post_title, post_content ) AGAINST (?) AND post_status='publish' LIMIT ?,?"; 439 | //$query = $this->db->query($sql, array($query, $offset, $post_per_page)); 440 | 441 | $wpdb = $this->installations[$installation]->wpconfig->wpdb; 442 | $query = $wpdb->query($sql, array($query, $offset, $post_per_page)); 443 | 444 | /* 445 | $posts = $wpdb->get(); 446 | if( $posts->num_rows() >0 ) 447 | { 448 | return $posts->result(); 449 | } 450 | */ 451 | 452 | return $query->result_array(); 453 | } 454 | 455 | /** 456 | * Return Number of Rows in Search 457 | * 458 | * @param unknown $query Description 459 | * 460 | * @return integer Number of Rows 461 | */ 462 | function get_numrows($installation, $query) { 463 | 464 | $rows=0; 465 | 466 | $sql = "SELECT * FROM wp_posts WHERE MATCH ( post_title, post_content ) AGAINST (?) AND post_status='publish'"; 467 | 468 | $wpdb = $this->installations[$installation]->wpconfig->wpdb; 469 | $query = $wpdb->query($sql, array($query)); 470 | 471 | $rows=$query->num_rows(); 472 | 473 | return $rows; 474 | } 475 | 476 | } 477 | --------------------------------------------------------------------------------